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

[PATCH 2/3] checkout: clear commit marks after detached-orphan check

From
Jeff King <peff@peff.net>
Date
Mar 20, 2011, 09:09 UTC
Message-ID
<20110320090918.GB15948@sigill.intra.peff.net>
In-Reply-To
<20110320090111.GA15641@sigill.intra.peff.net>

When leaving a detached HEAD, we do a revision walk to make sure the commit we are leaving isn't being orphaned. However, this leaves crufty marks in the commit objects which can confuse later walkers, like the one in stat_tracking_info.

Let's clean up after ourselves to prevent this conflict.
Signed-off-by: Jeff King <peff@peff.net>
---
This uses for_each_ref to re-find the list of commits we touched in our
traversal, which feels a little hacky. prepare_revision_walk already
generates the exact list of tips, but unfortunately writes it
into revs->commits, which then gets munged by limit_list in the second
half of prepare_revision_walk. I wonder if it should keep a copy
elsewhere in revs, and then we could add:
  clear_all_commit_marks(&revs);
to let callers clean up after themselves easily.

I also just clear all marks; we could do just the ones that the revision walker marks, but this seemed more future-proof to me than a set list of marks.

 builtin/checkout.c         |   13 +++++++++++++
 t/t2020-checkout-detach.sh |   13 +++++++++++++
 2 files changed, 26 insertions(+), 0 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 2bf02f2..f88d2c8 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -603,6 +603,16 @@ static int add_one_ref_to_rev_list_arg(const char *refname,
 	return 0;
 }
 
+static int clear_commit_marks_from_one_ref(const char *refname,
+				      const unsigned char *sha1,
+				      int flags,
+				      void *cb_data)
+{
+	struct commit *commit = lookup_commit_reference_gently(sha1, 1);
+	if (commit)
+		clear_commit_marks(commit, -1);
+	return 0;
+}
 
 static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
 {
@@ -674,6 +684,9 @@ static void orphaned_commit_warning(struct commit *commit)
 		suggest_reattach(commit, &revs);
 	else
 		describe_detached_head("Previous HEAD position was", commit);
+
+	clear_commit_marks(commit, -1);
+	for_each_ref(clear_commit_marks_from_one_ref, NULL);
 }
 
 static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
diff --git a/t/t2020-checkout-detach.sh b/t/t2020-checkout-detach.sh
index bfeb2a6..569b27f 100755
--- a/t/t2020-checkout-detach.sh
+++ b/t/t2020-checkout-detach.sh
@@ -126,4 +126,17 @@ test_expect_success 'checkout does not warn leaving reachable commit' '
 	check_no_orphan_warning stderr
 '
 
+cat >expect <<'EOF'
+Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
+EOF
+test_expect_success 'tracking count is accurate after orphan check' '
+	reset &&
+	git branch child master^ &&
+	git config branch.child.remote . &&
+	git config branch.child.merge refs/heads/master &&
+	git checkout child^ &&
+	git checkout child >stdout &&
+	test_cmp expect stdout
+'
+
 test_done
-- 
1.7.2.5.10.g62fe7
Previous: Jeff KingNext: Junio C Hamano
Message 7 of 28 in “[bug] git checkout lies about number of ahead commits when switching from detached HEAD”
  1. Piotr KrukowieckiMar 19, 2011
  2. Jeff KingMar 19, 2011
  3. Jeff KingMar 19, 2011
  4. Junio C HamanoMar 20, 2011
  5. Jeff KingMar 20, 2011
  6. 1/3 checkout: add basic tests for detached-orphan warningJeff King, Mar 20, 2011
  7. 2/3 checkout: clear commit marks after detached-orphan checkJeff King, Mar 20, 2011
  8. Junio C HamanoMar 20, 2011
  9. i18n: use test_i18ncmp in t2020 (checkout --detach)Jonathan Nieder, Apr 13, 2012
  10. checkout --detached test: write supporting files before start of testsJonathan Nieder, Apr 13, 2012
  11. Jeff KingApr 13, 2012
  12. Jonathan NiederApr 13, 2012
  13. Jeff KingApr 14, 2012
  14. Jeff KingApr 13, 2012
  15. Jonathan NiederApr 13, 2012
  16. Jeff KingApr 14, 2012
  17. 0/3 Re: i18n: use test_i18ncmp in t2020 (checkout --detach)Jonathan Nieder, Apr 14, 2012
  18. 1/3 test: do not rely on US English tracking-info messagesJonathan Nieder, Apr 14, 2012
  19. 2/3 test: use test_i18ncmp for "Patch format detection failed" messageJonathan Nieder, Apr 14, 2012
  20. 3/3 test: am of empty patch should not succeedJonathan Nieder, Apr 14, 2012
  21. Jeff KingApr 14, 2012
  22. Jonathan NiederApr 14, 2012
  23. Jeff KingApr 14, 2012
  24. Jonathan NiederApr 14, 2012
  25. 3/3 checkout: tweak detached-orphan warning formatJeff King, Mar 20, 2011
  26. Junio C HamanoMar 20, 2011
  27. Jeff KingMar 21, 2011
  28. Junio C HamanoMar 21, 2011

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.