]> git.ipfire.org Git - thirdparty/git.git/commitdiff
merge-base: add tests for --is-ancestor
authorNikolaus Schuetz <nikolauspschuetz@gmail.com>
Thu, 30 Jul 2026 06:19:09 +0000 (06:19 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Jul 2026 16:01:51 +0000 (09:01 -0700)
`git merge-base --is-ancestor A B` is used a lot in scripts but has no
tests. Add some to t6010 covering its exit codes: 0 when A is an
ancestor of B, 1 when it is not, and 128 (not 1) when given a bad
argument. Also check that --is-ancestor and --all can't be combined,
and that the resulting error names both options.

Signed-off-by: Nikolaus Schuetz <nikolauspschuetz@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t6010-merge-base.sh

index 44c726ea397cf10a2c522690653ecb696f96cc8b..21f09a678f268b255eecce32299c35236413fd21 100755 (executable)
@@ -305,4 +305,35 @@ test_expect_success 'merge-base --octopus --all for complex tree' '
        test_cmp expected actual
 '
 
+test_expect_success '--is-ancestor with an ancestor and a descendant' '
+       git merge-base --is-ancestor $E $A &&
+       test_expect_code 1 git merge-base --is-ancestor $A $E
+'
+
+test_expect_success '--is-ancestor treats a commit as its own ancestor' '
+       git merge-base --is-ancestor $A $A
+'
+
+test_expect_success '--is-ancestor with diverged commits' '
+       test_expect_code 1 git merge-base --is-ancestor $G $H &&
+       test_expect_code 1 git merge-base --is-ancestor $H $G
+'
+
+test_expect_success '--is-ancestor exits 128 on a bad commit' '
+       test_expect_code 128 git merge-base --is-ancestor $A no-such-commit &&
+       test_expect_code 128 git merge-base --is-ancestor no-such-commit $A
+'
+
+test_expect_success '--is-ancestor requires exactly two commits' '
+       test_expect_code 129 git merge-base --is-ancestor &&
+       test_expect_code 129 git merge-base --is-ancestor $A &&
+       test_expect_code 128 git merge-base --is-ancestor $E $A $B 2>err &&
+       test_grep ".--is-ancestor takes exactly two commits" err
+'
+
+test_expect_success '--is-ancestor and --all cannot be used together' '
+       test_expect_code 128 git merge-base --is-ancestor --all $E $A 2>err &&
+       test_grep "options .--is-ancestor. and .--all. cannot be used together" err
+'
+
 test_done