diff mbox series

[3/3] checkout: warn when unreachable commits after using --orphan

Message ID 417ae16c-9ba7-1e6d-c8d7-5b20a188b4fe@gmail.com (mailing list archive)
State New, archived
Headers show
Series warn when unreachable commits are left behind | expand

Commit Message

Rubén Justo April 22, 2023, 10:19 p.m. UTC
In 8e2dc6ac06 (commit: give final warning when reattaching HEAD to leave
commits behind, 2011-02-18) we introduced a warning to be issued when,
while checking out, the tip commit being left behind is not connected to
any ref.

We assumed that if the commit to be checked out is the same commit
currently checked out, we would omit the warning.  This makes sense
because we're going to have HEAD pointing to the same commit anyway, so
there is nothing to warn about.

However, with "--orphan" the target commit is not going to be used as
HEAD in the worktree, but a new orphan branch being created, which is
not going to be connected to the previous commit.  Therefore, we need
to check if the commit it is reachable and warn otherwise.

Let's fix the condition we introduced in 8e2dc6ac06, considering the
"--orphan" flag situation.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 builtin/checkout.c         | 8 ++++++--
 t/t2020-checkout-detach.sh | 9 +++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

Comments

Andrei Rybak April 27, 2023, 12:28 a.m. UTC | #1
On 23/04/2023 00:19, Rubén Justo wrote:
> diff --git a/t/t2020-checkout-detach.sh b/t/t2020-checkout-detach.sh
> index 2eab6474f8..6762a9a572 100755
> --- a/t/t2020-checkout-detach.sh
> +++ b/t/t2020-checkout-detach.sh
> @@ -124,6 +124,15 @@ test_expect_success 'checkout warns on orphan commits: output' '
>   	check_orphan_warning stderr "2 commits"
>   '
>   
> +test_expect_success 'checkout --orphan warns on orphan commits' '
> +	git checkout "$orphan2" &&
> +	git checkout --orphan orphan 2>stderr
> +'
> +
> +test_expect_success 'checkout --orphan warns on orphan commits: output' '
> +	check_orphan_warning stderr "2 commits"
> +'

These two tests could be a single test.

	test_expect_success 'checkout --orphan warns on orphan commits' '
		git checkout "$orphan2" &&
		git checkout --orphan orphan 2>stderr &&
		check_orphan_warning stderr "2 commits"
	'

Validating output like this in a separate step is an artifact of
the old way of checking localized strings.  Tests were split into
two in f06f08b78c ("i18n: mark checkout plural warning for
translation", 2011-04-10) and then prerequisite C_LOCALE_OUTPUT
was removed in f2c8c8007c ("i18n: use test_i18ngrep in t2020,
t2204, t3030, and t3200", 2011-04-12).  Usage of test_i18ngrep
was then removed in 1108cea7f8 ("tests: remove most uses of
test_i18ncmp", 2021-02-11).

> +
>   test_expect_success 'checkout warns orphaning 1 of 2 commits' '
>   	git checkout "$orphan2" &&
>   	git checkout HEAD^ 2>stderr
Rubén Justo April 27, 2023, 11:09 p.m. UTC | #2
On 27/4/23 2:28, Andrei Rybak wrote:
> On 23/04/2023 00:19, Rubén Justo wrote:
>> diff --git a/t/t2020-checkout-detach.sh b/t/t2020-checkout-detach.sh
>> index 2eab6474f8..6762a9a572 100755
>> --- a/t/t2020-checkout-detach.sh
>> +++ b/t/t2020-checkout-detach.sh
>> @@ -124,6 +124,15 @@ test_expect_success 'checkout warns on orphan commits: output' '
>>       check_orphan_warning stderr "2 commits"
>>   '
>>   +test_expect_success 'checkout --orphan warns on orphan commits' '
>> +    git checkout "$orphan2" &&
>> +    git checkout --orphan orphan 2>stderr
>> +'
>> +
>> +test_expect_success 'checkout --orphan warns on orphan commits: output' '
>> +    check_orphan_warning stderr "2 commits"
>> +'
> 
> These two tests could be a single test.
> 
>     test_expect_success 'checkout --orphan warns on orphan commits' '
>         git checkout "$orphan2" &&
>         git checkout --orphan orphan 2>stderr &&
>         check_orphan_warning stderr "2 commits"
> 

OK
    '
> 
> Validating output like this in a separate step is an artifact of
> the old way of checking localized strings.  Tests were split into
> two in f06f08b78c ("i18n: mark checkout plural warning for
> translation", 2011-04-10) and then prerequisite C_LOCALE_OUTPUT
> was removed in f2c8c8007c ("i18n: use test_i18ngrep in t2020,
> t2204, t3030, and t3200", 2011-04-12).  Usage of test_i18ngrep
> was then removed in 1108cea7f8 ("tests: remove most uses of
> test_i18ncmp", 2021-02-11).

Thank you!
diff mbox series

Patch

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 85ac4bca00..7fad3161b4 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1050,8 +1050,12 @@  static int switch_branches(const struct checkout_opts *opts,
 		}
 	}
 
-	if (!opts->quiet && !old_branch_info.path && old_branch_info.commit && new_branch_info->commit != old_branch_info.commit)
-		orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit, 1);
+	if (!opts->quiet && !old_branch_info.path && old_branch_info.commit) {
+		if (new_branch_info->commit != old_branch_info.commit)
+			orphaned_commit_warning(old_branch_info.commit, new_branch_info->commit, 1);
+		else if (opts->new_orphan_branch)
+			orphaned_commit_warning(old_branch_info.commit, NULL, 1);
+	}
 
 	update_refs_for_switch(opts, &old_branch_info, new_branch_info);
 
diff --git a/t/t2020-checkout-detach.sh b/t/t2020-checkout-detach.sh
index 2eab6474f8..6762a9a572 100755
--- a/t/t2020-checkout-detach.sh
+++ b/t/t2020-checkout-detach.sh
@@ -124,6 +124,15 @@  test_expect_success 'checkout warns on orphan commits: output' '
 	check_orphan_warning stderr "2 commits"
 '
 
+test_expect_success 'checkout --orphan warns on orphan commits' '
+	git checkout "$orphan2" &&
+	git checkout --orphan orphan 2>stderr
+'
+
+test_expect_success 'checkout --orphan warns on orphan commits: output' '
+	check_orphan_warning stderr "2 commits"
+'
+
 test_expect_success 'checkout warns orphaning 1 of 2 commits' '
 	git checkout "$orphan2" &&
 	git checkout HEAD^ 2>stderr