Skip to content

Commit 5595635

Browse files
committed
resolve_ref(): report breakage to the caller without warning
629cd3a (resolve_ref(): emit warnings for improperly-formatted references, 2011-09-15) made resolve_ref() warn against files that are found in the directories the ref dwimmery looks at. The intent may be good, but these messages come from a wrong level of the API hierarchy. Instead record the breakage in "flags" whose purpose is to explain the result of the function to the caller, who is in a much better position to make intelligent decision based on the information. This updates sha1_name.c::dwim_ref() to warn against such a broken candidate only when it does not appear directly below $GIT_DIR to restore the traditional behaviour, as we know many files directly underneath $GIT_DIR/ are not refs. Warning against "git show config --" with "$GIT_DIR/config does not look like a well-formed ref" does not make sense, and we may later tweak the dwimmery not to even consider them as candidates, but that is a longer term topic. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 98ac34b commit 5595635

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

refs.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,6 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
489489
ssize_t len;
490490
char buffer[256];
491491
static char ref_buffer[256];
492-
char path[PATH_MAX];
493492

494493
if (flag)
495494
*flag = 0;
@@ -498,6 +497,7 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
498497
return NULL;
499498

500499
for (;;) {
500+
char path[PATH_MAX];
501501
struct stat st;
502502
char *buf;
503503
int fd;
@@ -570,21 +570,22 @@ const char *resolve_ref(const char *ref, unsigned char *sha1, int reading, int *
570570
*/
571571
if (prefixcmp(buffer, "ref:"))
572572
break;
573+
if (flag)
574+
*flag |= REF_ISSYMREF;
573575
buf = buffer + 4;
574576
while (isspace(*buf))
575577
buf++;
576578
if (check_refname_format(buf, REFNAME_ALLOW_ONELEVEL)) {
577-
warning("symbolic reference in %s is formatted incorrectly",
578-
path);
579+
if (flag)
580+
*flag |= REF_ISBROKEN;
579581
return NULL;
580582
}
581583
ref = strcpy(ref_buffer, buf);
582-
if (flag)
583-
*flag |= REF_ISSYMREF;
584584
}
585585
/* Please note that FETCH_HEAD has a second line containing other data. */
586586
if (get_sha1_hex(buffer, sha1) || (buffer[40] != '\0' && !isspace(buffer[40]))) {
587-
warning("reference in %s is formatted incorrectly", path);
587+
if (flag)
588+
*flag |= REF_ISBROKEN;
588589
return NULL;
589590
}
590591
return ref;
@@ -1107,8 +1108,11 @@ int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref)
11071108
*ref = xstrdup(r);
11081109
if (!warn_ambiguous_refs)
11091110
break;
1110-
} else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD"))
1111+
} else if ((flag & REF_ISSYMREF) && strcmp(fullref, "HEAD")) {
11111112
warning("ignoring dangling symref %s.", fullref);
1113+
} else if ((flag & REF_ISBROKEN) && strchr(fullref, '/')) {
1114+
warning("ignoring broken ref %s.", fullref);
1115+
}
11121116
}
11131117
free(last_branch);
11141118
return refs_found;

0 commit comments

Comments
 (0)