merge-base: handle --fork-point without reflog
authorJeff King <peff@peff.net>
Wed, 12 Oct 2016 20:10:40 +0000 (16:10 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 12 Oct 2016 21:30:16 +0000 (14:30 -0700)
The --fork-point option looks in the reflog to try to find
where a derived branch forked from a base branch. However,
if the reflog for the base branch is totally empty (as it
commonly is right after cloning, which does not write a
reflog entry), then our for_each_reflog call will not find
any entries, and we will come up with no merge base, even
though there may be one with the current tip of the base.

We can fix this by just adding the current tip to
our list of collected entries.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/merge-base.c
t/t6010-merge-base.sh
index c0d1822eb3ad371b9ab0b830aed2cc2741a978bd..b572a37c2611b2a8c20b1db7edac68623d371a38 100644 (file)
@@ -173,6 +173,9 @@ static int handle_fork_point(int argc, const char **argv)
        revs.initial = 1;
        for_each_reflog_ent(refname, collect_one_reflog_ent, &revs);
 
+       if (!revs.nr && !get_sha1(refname, sha1))
+               add_one_commit(sha1, &revs);
+
        for (i = 0; i < revs.nr; i++)
                revs.commit[i]->object.flags &= ~TMP_MARK;
 
index 39b3238da211957eeec36a9c7b911cd5942de6dd..de3bf0741c06a9f0b7163794190dba46ba2d462b 100755 (executable)
@@ -258,6 +258,12 @@ test_expect_success 'using reflog to find the fork point' '
        test_cmp expect3 actual
 '
 
+test_expect_success '--fork-point works with empty reflog' '
+       git -c core.logallrefupdates=false branch no-reflog base &&
+       git merge-base --fork-point no-reflog derived &&
+       test_cmp expect3 actual
+'
+
 test_expect_success 'merge-base --octopus --all for complex tree' '
        # Best common ancestor for JE, JAA and JDD is JC
        #             JE