git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Rubén Justo <rjusto@gmail.com>
Date
Apr 22, 2023, 22:19 UTC
Message-ID
<417ae16c-9ba7-1e6d-c8d7-5b20a188b4fe@gmail.com>
In-Reply-To
<f702476a-543a-da9b-ccd9-4431c80471e1@gmail.com>

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(-)
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
Previous: Rubén JustoNext: Andrei Rybak
Message 10 of 12 in “warn when unreachable commits are left behind”
  1. 0/3 warn when unreachable commits are left behindRubén Justo, Apr 22, 2023
  2. 1/3 checkout: move orphaned_commit_warning()Rubén Justo, Apr 22, 2023
  3. 2/3 worktree: warn when removing a worktree with orphan commitsRubén Justo, Apr 22, 2023
  4. Junio C HamanoApr 24, 2023
  5. Rubén JustoApr 26, 2023
  6. Junio C HamanoApr 27, 2023
  7. Eric SunshineApr 27, 2023
  8. Junio C HamanoApr 28, 2023
  9. Rubén JustoApr 27, 2023
  10. 3/3 checkout: warn when unreachable commits after using --orphanRubén Justo, Apr 22, 2023
  11. Andrei RybakApr 27, 2023
  12. Rubén JustoApr 27, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.