Skip to content

Commit c51d136

Browse files
author
Junio C Hamano
committed
get_sha1_basic(): try refs/... and finally refs/remotes/$foo/HEAD
This implements the suggestion by Jeff King to use refs/remotes/$foo/HEAD to interpret a shorthand "$foo" to mean the primary branch head of a tracked remote. clone needs to be told about this convention as well. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 2f8acdb commit c51d136

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

sha1_name.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,18 +235,21 @@ static int ambiguous_path(const char *path, int len)
235235

236236
static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
237237
{
238-
static const char *prefix[] = {
239-
"",
240-
"refs",
241-
"refs/tags",
242-
"refs/heads",
243-
"refs/remotes",
238+
static const char *fmt[] = {
239+
"/%.*s",
240+
"refs/%.*s",
241+
"refs/tags/%.*s",
242+
"refs/heads/%.*s",
243+
"refs/remotes/%.*s",
244+
"refs/remotes/%.*s/HEAD",
244245
NULL
245246
};
246247
const char **p;
247248
const char *warning = "warning: refname '%.*s' is ambiguous.\n";
248249
char *pathname;
249250
int already_found = 0;
251+
unsigned char *this_result;
252+
unsigned char sha1_from_ref[20];
250253

251254
if (len == 40 && !get_sha1_hex(str, sha1))
252255
return 0;
@@ -255,11 +258,9 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
255258
if (ambiguous_path(str, len))
256259
return -1;
257260

258-
for (p = prefix; *p; p++) {
259-
unsigned char sha1_from_ref[20];
260-
unsigned char *this_result =
261-
already_found ? sha1_from_ref : sha1;
262-
pathname = git_path("%s/%.*s", *p, len, str);
261+
for (p = fmt; *p; p++) {
262+
this_result = already_found ? sha1_from_ref : sha1;
263+
pathname = git_path(*p, len, str);
263264
if (!read_ref(pathname, this_result)) {
264265
if (warn_ambiguous_refs) {
265266
if (already_found &&

0 commit comments

Comments
 (0)