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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxx>
---
 builtin/checkout.c         | 8 ++++++--
 t/t2020-checkout-detach.sh | 9 +++++++++
 2 files changed, 15 insertions(+), 2 deletions(-)

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
-- 
2.39.2



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux