* [PATCH] merge-base: add tests for --is-ancestor
@ 2026-07-25 17:00 Nikolaus Schuetz via GitGitGadget
2026-07-28 22:48 ` Junio C Hamano
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Nikolaus Schuetz via GitGitGadget @ 2026-07-25 17:00 UTC (permalink / raw)
To: git; +Cc: Nikolaus Schuetz, Nikolaus Schuetz
From: Nikolaus Schuetz <nikolauspschuetz@gmail.com>
`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>
---
merge-base: add tests for --is-ancestor
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2186%2Fnikolauspschuetz%2Ft6010-test-is-ancestor-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2186/nikolauspschuetz/t6010-test-is-ancestor-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/2186
t/t6010-merge-base.sh | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/t/t6010-merge-base.sh b/t/t6010-merge-base.sh
index 44c726ea39..d28d9dab2c 100755
--- a/t/t6010-merge-base.sh
+++ b/t/t6010-merge-base.sh
@@ -305,4 +305,38 @@ test_expect_success 'merge-base --octopus --all for complex tree' '
test_cmp expected actual
'
+test_expect_success 'setup --is-ancestor' '
+ git init is-ancestor &&
+ (
+ cd is-ancestor &&
+ test_commit one &&
+ test_commit two &&
+ git checkout -b side one &&
+ test_commit three
+ )
+'
+
+test_expect_success '--is-ancestor parent and child' '
+ git -C is-ancestor merge-base --is-ancestor one two &&
+ test_expect_code 1 git -C is-ancestor merge-base --is-ancestor two one
+'
+
+test_expect_success '--is-ancestor self' '
+ git -C is-ancestor merge-base --is-ancestor two two
+'
+
+test_expect_success '--is-ancestor diverged commits' '
+ test_expect_code 1 git -C is-ancestor merge-base --is-ancestor three two
+'
+
+test_expect_success '--is-ancestor exit 128 non-existent commit' '
+ test_expect_code 128 git -C is-ancestor merge-base --is-ancestor one no-such-commit &&
+ test_expect_code 128 git -C is-ancestor merge-base --is-ancestor no-such-commit one
+'
+
+test_expect_success '--is-ancestor and --all cannot be used together' '
+ test_expect_code 128 git -C is-ancestor merge-base --is-ancestor --all one two 2>err &&
+ test_grep "options .--is-ancestor. and .--all. cannot be used together" err
+'
+
test_done
base-commit: f60db8d575adb79761d363e026fb49bddf330c73
--
gitgitgadget
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH] merge-base: add tests for --is-ancestor
2026-07-25 17:00 [PATCH] merge-base: add tests for --is-ancestor Nikolaus Schuetz via GitGitGadget
@ 2026-07-28 22:48 ` Junio C Hamano
2026-07-29 9:39 ` Phillip Wood
2026-07-30 6:19 ` [PATCH v2] " Nikolaus Schuetz via GitGitGadget
2 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2026-07-28 22:48 UTC (permalink / raw)
To: Nikolaus Schuetz via GitGitGadget; +Cc: git, Nikolaus Schuetz
"Nikolaus Schuetz via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Nikolaus Schuetz <nikolauspschuetz@gmail.com>
>
> `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>
> ---
> merge-base: add tests for --is-ancestor
>
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2186%2Fnikolauspschuetz%2Ft6010-test-is-ancestor-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2186/nikolauspschuetz/t6010-test-is-ancestor-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/2186
>
> t/t6010-merge-base.sh | 34 ++++++++++++++++++++++++++++++++++
> 1 file changed, 34 insertions(+)
Thanks, will queue.
>
> diff --git a/t/t6010-merge-base.sh b/t/t6010-merge-base.sh
> index 44c726ea39..d28d9dab2c 100755
> --- a/t/t6010-merge-base.sh
> +++ b/t/t6010-merge-base.sh
> @@ -305,4 +305,38 @@ test_expect_success 'merge-base --octopus --all for complex tree' '
> test_cmp expected actual
> '
>
> +test_expect_success 'setup --is-ancestor' '
> + git init is-ancestor &&
> + (
> + cd is-ancestor &&
> + test_commit one &&
> + test_commit two &&
> + git checkout -b side one &&
> + test_commit three
> + )
> +'
> +
> +test_expect_success '--is-ancestor parent and child' '
> + git -C is-ancestor merge-base --is-ancestor one two &&
> + test_expect_code 1 git -C is-ancestor merge-base --is-ancestor two one
> +'
> +
> +test_expect_success '--is-ancestor self' '
> + git -C is-ancestor merge-base --is-ancestor two two
> +'
> +
> +test_expect_success '--is-ancestor diverged commits' '
> + test_expect_code 1 git -C is-ancestor merge-base --is-ancestor three two
> +'
> +
> +test_expect_success '--is-ancestor exit 128 non-existent commit' '
> + test_expect_code 128 git -C is-ancestor merge-base --is-ancestor one no-such-commit &&
> + test_expect_code 128 git -C is-ancestor merge-base --is-ancestor no-such-commit one
> +'
> +
> +test_expect_success '--is-ancestor and --all cannot be used together' '
> + test_expect_code 128 git -C is-ancestor merge-base --is-ancestor --all one two 2>err &&
> + test_grep "options .--is-ancestor. and .--all. cannot be used together" err
> +'
> +
> test_done
>
> base-commit: f60db8d575adb79761d363e026fb49bddf330c73
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] merge-base: add tests for --is-ancestor
2026-07-25 17:00 [PATCH] merge-base: add tests for --is-ancestor Nikolaus Schuetz via GitGitGadget
2026-07-28 22:48 ` Junio C Hamano
@ 2026-07-29 9:39 ` Phillip Wood
2026-07-29 15:31 ` Junio C Hamano
2026-07-30 6:19 ` [PATCH v2] " Nikolaus Schuetz via GitGitGadget
2 siblings, 1 reply; 6+ messages in thread
From: Phillip Wood @ 2026-07-29 9:39 UTC (permalink / raw)
To: Nikolaus Schuetz via GitGitGadget, git; +Cc: Nikolaus Schuetz
Hi Nikolaus
On 25/07/2026 18:00, Nikolaus Schuetz via GitGitGadget wrote:
> From: Nikolaus Schuetz <nikolauspschuetz@gmail.com>
>
> `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.
Thanks for adding some tests for this option
> diff --git a/t/t6010-merge-base.sh b/t/t6010-merge-base.sh
> index 44c726ea39..d28d9dab2c 100755
> --- a/t/t6010-merge-base.sh
> +++ b/t/t6010-merge-base.sh
> @@ -305,4 +305,38 @@ test_expect_success 'merge-base --octopus --all for complex tree' '
> test_cmp expected actual
> '
>
> +test_expect_success 'setup --is-ancestor' '
> + git init is-ancestor &&
> + (
> + cd is-ancestor &&
> + test_commit one &&
> + test_commit two &&
> + git checkout -b side one &&
> + test_commit three
> + )
> +'
Do we really need to create a new repository? None of the existing tests
do that - can't we just use the commits created by the first test?
Having said that, the tests below look good. We could perhaps have a
test to check that it fails with one commit or three commits but what
you have here is clearly a very useful improvement in our test coverage.
Thanks
Phillip
> +test_expect_success '--is-ancestor parent and child' '
> + git -C is-ancestor merge-base --is-ancestor one two &&
> + test_expect_code 1 git -C is-ancestor merge-base --is-ancestor two one
> +'
> +
> +test_expect_success '--is-ancestor self' '
> + git -C is-ancestor merge-base --is-ancestor two two
> +'
> +
> +test_expect_success '--is-ancestor diverged commits' '
> + test_expect_code 1 git -C is-ancestor merge-base --is-ancestor three two
> +'
> +
> +test_expect_success '--is-ancestor exit 128 non-existent commit' '
> + test_expect_code 128 git -C is-ancestor merge-base --is-ancestor one no-such-commit &&
> + test_expect_code 128 git -C is-ancestor merge-base --is-ancestor no-such-commit one
> +'
> +
> +test_expect_success '--is-ancestor and --all cannot be used together' '
> + test_expect_code 128 git -C is-ancestor merge-base --is-ancestor --all one two 2>err &&
> + test_grep "options .--is-ancestor. and .--all. cannot be used together" err
> +'
> +
> test_done
>
> base-commit: f60db8d575adb79761d363e026fb49bddf330c73
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] merge-base: add tests for --is-ancestor
2026-07-29 9:39 ` Phillip Wood
@ 2026-07-29 15:31 ` Junio C Hamano
0 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2026-07-29 15:31 UTC (permalink / raw)
To: Phillip Wood; +Cc: Nikolaus Schuetz via GitGitGadget, git, Nikolaus Schuetz
Phillip Wood <phillip.wood123@gmail.com> writes:
> Thanks for adding some tests for this option
>
>> diff --git a/t/t6010-merge-base.sh b/t/t6010-merge-base.sh
>> index 44c726ea39..d28d9dab2c 100755
>> --- a/t/t6010-merge-base.sh
>> +++ b/t/t6010-merge-base.sh
>> @@ -305,4 +305,38 @@ test_expect_success 'merge-base --octopus --all for complex tree' '
>> test_cmp expected actual
>> '
>>
>> +test_expect_success 'setup --is-ancestor' '
>> + git init is-ancestor &&
>> + (
>> + cd is-ancestor &&
>> + test_commit one &&
>> + test_commit two &&
>> + git checkout -b side one &&
>> + test_commit three
>> + )
>> +'
>
> Do we really need to create a new repository? None of the existing tests
> do that - can't we just use the commits created by the first test?
Good point. It is often a lot more work to do so when writing these
new tests, and I suspect that is the reason why this new repository
was created, but I do not know whether, in the long run, it is
cheaper and more maintainable to reuse the test history that has been
used by all existing tests above. The next developer who needs to
tweak the first test has 5 (the number of tests below) extra
constraints if we go that route, but a separate setup means the two
sets of tests are more isolated.
> Having said that, the tests below look good. We could perhaps have a
> test to check that it fails with one commit or three commits but what
> you have here is clearly a very useful improvement in our test coverage.
Thanks.
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2] merge-base: add tests for --is-ancestor
2026-07-25 17:00 [PATCH] merge-base: add tests for --is-ancestor Nikolaus Schuetz via GitGitGadget
2026-07-28 22:48 ` Junio C Hamano
2026-07-29 9:39 ` Phillip Wood
@ 2026-07-30 6:19 ` Nikolaus Schuetz via GitGitGadget
2026-07-30 16:02 ` Junio C Hamano
2 siblings, 1 reply; 6+ messages in thread
From: Nikolaus Schuetz via GitGitGadget @ 2026-07-30 6:19 UTC (permalink / raw)
To: git; +Cc: Phillip Wood, Nikolaus Schuetz, Nikolaus Schuetz
From: Nikolaus Schuetz <nikolauspschuetz@gmail.com>
`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>
---
merge-base: add tests for --is-ancestor
Changes since v1, per review:
* Reuse the E---D---C---B---A history and the G/H merges from the "set
up G and H" test instead of creating a separate repository.
* Add a test that --is-ancestor requires exactly two commits: too few
is a usage error (exit 129), while more than two is rejected with
"--is-ancestor takes exactly two commits" (exit 128).
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2186%2Fnikolauspschuetz%2Ft6010-test-is-ancestor-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2186/nikolauspschuetz/t6010-test-is-ancestor-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/2186
Range-diff vs v1:
1: e512e35788 ! 1: 319bf89d25 merge-base: add tests for --is-ancestor
@@ t/t6010-merge-base.sh: test_expect_success 'merge-base --octopus --all for compl
test_cmp expected actual
'
-+test_expect_success 'setup --is-ancestor' '
-+ git init is-ancestor &&
-+ (
-+ cd is-ancestor &&
-+ test_commit one &&
-+ test_commit two &&
-+ git checkout -b side one &&
-+ test_commit three
-+ )
++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 parent and child' '
-+ git -C is-ancestor merge-base --is-ancestor one two &&
-+ test_expect_code 1 git -C is-ancestor merge-base --is-ancestor two one
++test_expect_success '--is-ancestor treats a commit as its own ancestor' '
++ git merge-base --is-ancestor $A $A
+'
+
-+test_expect_success '--is-ancestor self' '
-+ git -C is-ancestor merge-base --is-ancestor two two
++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 diverged commits' '
-+ test_expect_code 1 git -C is-ancestor merge-base --is-ancestor three two
++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 exit 128 non-existent commit' '
-+ test_expect_code 128 git -C is-ancestor merge-base --is-ancestor one no-such-commit &&
-+ test_expect_code 128 git -C is-ancestor merge-base --is-ancestor no-such-commit one
++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 -C is-ancestor merge-base --is-ancestor --all one two 2>err &&
++ 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
+'
+
t/t6010-merge-base.sh | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
diff --git a/t/t6010-merge-base.sh b/t/t6010-merge-base.sh
index 44c726ea39..21f09a678f 100755
--- a/t/t6010-merge-base.sh
+++ b/t/t6010-merge-base.sh
@@ -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
base-commit: f60db8d575adb79761d363e026fb49bddf330c73
--
gitgitgadget
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v2] merge-base: add tests for --is-ancestor
2026-07-30 6:19 ` [PATCH v2] " Nikolaus Schuetz via GitGitGadget
@ 2026-07-30 16:02 ` Junio C Hamano
0 siblings, 0 replies; 6+ messages in thread
From: Junio C Hamano @ 2026-07-30 16:02 UTC (permalink / raw)
To: Nikolaus Schuetz via GitGitGadget; +Cc: git, Phillip Wood, Nikolaus Schuetz
"Nikolaus Schuetz via GitGitGadget" <gitgitgadget@gmail.com> writes:
> Changes since v1, per review:
>
> * Reuse the E---D---C---B---A history and the G/H merges from the "set
> up G and H" test instead of creating a separate repository.
Looks good.
> * Add a test that --is-ancestor requires exactly two commits: too few
> is a usage error (exit 129), while more than two is rejected with
> "--is-ancestor takes exactly two commits" (exit 128).
OK.
Will queue. Thanks.
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-07-30 16:02 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-25 17:00 [PATCH] merge-base: add tests for --is-ancestor Nikolaus Schuetz via GitGitGadget
2026-07-28 22:48 ` Junio C Hamano
2026-07-29 9:39 ` Phillip Wood
2026-07-29 15:31 ` Junio C Hamano
2026-07-30 6:19 ` [PATCH v2] " Nikolaus Schuetz via GitGitGadget
2026-07-30 16:02 ` Junio C Hamano
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox