Skip to content

Commit c64ec66

Browse files
committed
Merge branch 'jk/describe-blob'
"git describe <blob>" misbehaves and/or crashes in some corner cases, which has been taught to exit with failure gracefully. * jk/describe-blob: describe: pass commit to describe_commit() describe: handle blob traversal with no commits describe: catch unborn branch in describe_blob() describe: error if blob not found describe: pass oid struct by const pointer
2 parents fea9d18 + 7c10e48 commit c64ec66

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

builtin/describe.c

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -352,26 +352,24 @@ static void append_suffix(int depth, const struct object_id *oid, struct strbuf
352352
repo_find_unique_abbrev(the_repository, oid, abbrev));
353353
}
354354

355-
static void describe_commit(struct object_id *oid, struct strbuf *dst)
355+
static void describe_commit(struct commit *cmit, struct strbuf *dst)
356356
{
357-
struct commit *cmit, *gave_up_on = NULL;
357+
struct commit *gave_up_on = NULL;
358358
struct lazy_queue queue = LAZY_QUEUE_INIT;
359359
struct commit_name *n;
360360
struct possible_tag all_matches[MAX_TAGS];
361361
unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
362362
unsigned long seen_commits = 0;
363363
unsigned int unannotated_cnt = 0;
364364

365-
cmit = lookup_commit_reference(the_repository, oid);
366-
367365
n = find_commit_name(&cmit->object.oid);
368366
if (n && (tags || all || n->prio == 2)) {
369367
/*
370368
* Exact match to an existing ref.
371369
*/
372370
append_name(n, dst);
373371
if (n->misnamed || longformat)
374-
append_suffix(0, n->tag ? get_tagged_oid(n->tag) : oid, dst);
372+
append_suffix(0, n->tag ? get_tagged_oid(n->tag) : &cmit->object.oid, dst);
375373
if (suffix)
376374
strbuf_addstr(dst, suffix);
377375
return;
@@ -528,39 +526,47 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
528526
}
529527

530528
struct process_commit_data {
531-
struct object_id current_commit;
532-
struct object_id looking_for;
529+
struct commit *current_commit;
530+
const struct object_id *looking_for;
533531
struct strbuf *dst;
534532
struct rev_info *revs;
535533
};
536534

537535
static void process_commit(struct commit *commit, void *data)
538536
{
539537
struct process_commit_data *pcd = data;
540-
pcd->current_commit = commit->object.oid;
538+
pcd->current_commit = commit;
541539
}
542540

543541
static void process_object(struct object *obj, const char *path, void *data)
544542
{
545543
struct process_commit_data *pcd = data;
546544

547-
if (oideq(&pcd->looking_for, &obj->oid) && !pcd->dst->len) {
545+
if (oideq(pcd->looking_for, &obj->oid) && !pcd->dst->len) {
548546
reset_revision_walk();
549-
describe_commit(&pcd->current_commit, pcd->dst);
550-
strbuf_addf(pcd->dst, ":%s", path);
547+
if (pcd->current_commit) {
548+
describe_commit(pcd->current_commit, pcd->dst);
549+
strbuf_addf(pcd->dst, ":%s", path);
550+
}
551551
free_commit_list(pcd->revs->commits);
552552
pcd->revs->commits = NULL;
553553
}
554554
}
555555

556-
static void describe_blob(struct object_id oid, struct strbuf *dst)
556+
static void describe_blob(const struct object_id *oid, struct strbuf *dst)
557557
{
558558
struct rev_info revs;
559559
struct strvec args = STRVEC_INIT;
560-
struct process_commit_data pcd = { *null_oid(the_hash_algo), oid, dst, &revs};
560+
struct object_id head_oid;
561+
struct process_commit_data pcd = { NULL, oid, dst, &revs};
562+
563+
if (repo_get_oid(the_repository, "HEAD", &head_oid))
564+
die(_("cannot search for blob '%s' on an unborn branch"),
565+
oid_to_hex(oid));
561566

562567
strvec_pushl(&args, "internal: The first arg is not parsed",
563-
"--objects", "--in-commit-order", "--reverse", "HEAD",
568+
"--objects", "--in-commit-order", "--reverse",
569+
oid_to_hex(&head_oid),
564570
NULL);
565571

566572
repo_init_revisions(the_repository, &revs, NULL);
@@ -574,6 +580,9 @@ static void describe_blob(struct object_id oid, struct strbuf *dst)
574580
reset_revision_walk();
575581
release_revisions(&revs);
576582
strvec_clear(&args);
583+
584+
if (!dst->len)
585+
die(_("blob '%s' not reachable from HEAD"), oid_to_hex(oid));
577586
}
578587

579588
static void describe(const char *arg, int last_one)
@@ -590,10 +599,10 @@ static void describe(const char *arg, int last_one)
590599
cmit = lookup_commit_reference_gently(the_repository, &oid, 1);
591600

592601
if (cmit)
593-
describe_commit(&oid, &sb);
602+
describe_commit(cmit, &sb);
594603
else if (odb_read_object_info(the_repository->objects,
595604
&oid, NULL) == OBJ_BLOB)
596-
describe_blob(oid, &sb);
605+
describe_blob(&oid, &sb);
597606
else
598607
die(_("%s is neither a commit nor blob"), arg);
599608

t/t6120-describe.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,36 @@ test_expect_success 'describe tag object' '
409409
test_grep "fatal: test-blob-1 is neither a commit nor blob" actual
410410
'
411411

412+
test_expect_success 'describe an unreachable blob' '
413+
blob=$(echo not-found-anywhere | git hash-object -w --stdin) &&
414+
test_must_fail git describe $blob 2>actual &&
415+
test_grep "blob .$blob. not reachable from HEAD" actual
416+
'
417+
418+
test_expect_success 'describe blob on an unborn branch' '
419+
oldbranch=$(git symbolic-ref HEAD) &&
420+
test_when_finished "git symbolic-ref HEAD $oldbranch" &&
421+
git symbolic-ref HEAD refs/heads/does-not-exist &&
422+
test_must_fail git describe test-blob 2>actual &&
423+
test_grep "cannot search .* on an unborn branch" actual
424+
'
425+
426+
# This test creates a repository state that we generally try to disallow: HEAD
427+
# is pointing to an object that is not a commit. The ref update code forbids
428+
# non-commit writes directly to HEAD or to any branch in refs/heads/. But we
429+
# can use the loophole of pointing HEAD to another non-branch ref (something we
430+
# should forbid, but don't for historical reasons).
431+
#
432+
# Do not take this test as an endorsement of the loophole! If we ever tighten
433+
# it, it is reasonable to just drop this test entirely.
434+
test_expect_success 'describe blob on a non-commit HEAD' '
435+
oldbranch=$(git symbolic-ref HEAD) &&
436+
test_when_finished "git symbolic-ref HEAD $oldbranch" &&
437+
git symbolic-ref HEAD refs/tags/test-blob &&
438+
test_must_fail git describe test-blob 2>actual &&
439+
test_grep "blob .* not reachable from HEAD" actual
440+
'
441+
412442
test_expect_success ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
413443
i=1 &&
414444
while test $i -lt 8000

0 commit comments

Comments
 (0)