Make sure heads/foo and tags/foo do not confuse things.
When both heads/foo and tags/foo exist, get_sha1_basic("foo") picked up the tag without complaining, which is quite confusing. Make sure we require unambiguous form, "heads/foo" or "tags/foo" in such cases. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Nov 21, 2005 at 00:43 UTC
924215024c72bc359fad920010dd6b356a5140c8
2 files changed
+24
-6
sha1_name.c
+14
-3
@@ -236,6 +236,7 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
236
NULL
237
};
238
const char **p;
239
+ int found = 0;
240
241
if (len == 40 && !get_sha1_hex(str, sha1))
242
return 0;
@@ -246,10 +247,20 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
247
248
for (p = prefix; *p; p++) {
249
char *pathname = git_path("%s/%.*s", *p, len, str);
249
- if (!read_ref(pathname, sha1))
250
- return 0;
250
+ if (!read_ref(pathname, sha1)) {
251
+ /* Must be unique; i.e. when heads/foo and
252
+ * tags/foo are both present, reject "foo".
253
+ * Note that read_ref() eventually calls
254
+ * get_sha1_hex() which can smudge initial
255
+ * part of the buffer even if what is read
256
+ * is found to be invalid halfway.
257
+ */
258
+ if (1 < found++)
259
+ return -1;
260
+ }
261
}
252
-
262
+ if (found == 1)
263
+ return 0;
264
return -1;
265
}
266
show-branch.c
+10
-3
@@ -313,9 +313,16 @@ static int append_ref(const char *refname, const unsigned char *sha1)
313
314
static int append_head_ref(const char *refname, const unsigned char *sha1)
315
{
316
- if (strncmp(refname, "refs/heads/", 11))
316
+ unsigned char tmp[20];
317
+ int ofs = 11;
318
+ if (strncmp(refname, "refs/heads/", ofs))
319
return 0;
318
- return append_ref(refname + 11, sha1);
320
+ /* If both heads/foo and tags/foo exists, get_sha1 would
321
+ * get confused.
322
+ */
323
+ if (get_sha1(refname + ofs, tmp) || memcmp(tmp, sha1, 20))
324
+ ofs = 5;
325
+ return append_ref(refname + ofs, sha1);
326
}
327
328
static int append_tag_ref(const char *refname, const unsigned char *sha1)
@@ -470,7 +477,7 @@ int main(int ac, char **av)
477
if (MAX_REVS <= num_rev)
478
die("cannot handle more than %d revs.", MAX_REVS);
479
if (get_sha1(ref_name[num_rev], revkey))
473
- usage(show_branch_usage);
480
+ die("'%s' is not a valid ref.\n", ref_name[num_rev]);
481
commit = lookup_commit_reference(revkey);
482
if (!commit)
483
die("cannot find commit %s (%s)",