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

[PATCH v2 4/8] sequencer: treat error reading HEAD as unborn branch

From
Brian Lyles <brianmlyles@gmail.com>
Date
Feb 10, 2024, 07:43 UTC
Message-ID
<20240210074859.552497-5-brianmlyles@gmail.com>
In-Reply-To
<20240119060721.3734775-2-brianmlyles@gmail.com>

When using git-cherry-pick(1) with `--allow-empty` while on an unborn branch, an error is thrown. This is inconsistent with the same cherry-pick when `--allow-empty` is not specified.

Treat a failure reading HEAD as an unborn branch in `is_index_unchanged`. This is consistent with other sequencer logic such as `do_pick_commit`. When on an unborn branch, use the `empty_tree` as the tree to compare against.

Signed-off-by: Brian Lyles <brianmlyles@gmail.com>
Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
This is another new commit that was not present in v1.
See this comment[1] from Phillip for context.
[1]: https://lore.kernel.org/git/b5213705-4cd6-40ef-8c5f-32b214534b8b@gmail.com/
 sequencer.c                   | 36 ++++++++++++++++++++---------------
 t/t3501-revert-cherry-pick.sh | 11 +++++++++++
 2 files changed, 32 insertions(+), 15 deletions(-)
diff --git a/sequencer.c b/sequencer.c
index 3cc88d8a80..b1b19512de 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -769,30 +769,36 @@ static struct object_id *get_cache_tree_oid(struct index_state *istate)
 
 static int is_index_unchanged(struct repository *r)
 {
-	struct object_id head_oid, *cache_tree_oid;
+	struct object_id head_oid, *cache_tree_oid, head_tree_oid;
 	struct commit *head_commit;
 	struct index_state *istate = r->index;
 
-	if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL))
-		return error(_("could not resolve HEAD commit"));
+	if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) {
+		/*
+		 * Treat an error reading HEAD as an unborn branch.
+		 */
+		head_tree_oid = *the_hash_algo->empty_tree;
+	} else {
+		head_commit = lookup_commit(r, &head_oid);
 
-	head_commit = lookup_commit(r, &head_oid);
+		/*
+		 * If head_commit is NULL, check_commit, called from
+		 * lookup_commit, would have indicated that head_commit is not
+		 * a commit object already.  repo_parse_commit() will return failure
+		 * without further complaints in such a case.  Otherwise, if
+		 * the commit is invalid, repo_parse_commit() will complain.  So
+		 * there is nothing for us to say here.  Just return failure.
+		 */
+		if (repo_parse_commit(r, head_commit))
+			return -1;
 
-	/*
-	 * If head_commit is NULL, check_commit, called from
-	 * lookup_commit, would have indicated that head_commit is not
-	 * a commit object already.  repo_parse_commit() will return failure
-	 * without further complaints in such a case.  Otherwise, if
-	 * the commit is invalid, repo_parse_commit() will complain.  So
-	 * there is nothing for us to say here.  Just return failure.
-	 */
-	if (repo_parse_commit(r, head_commit))
-		return -1;
+		head_tree_oid = *get_commit_tree_oid(head_commit);
+	}
 
 	if (!(cache_tree_oid = get_cache_tree_oid(istate)))
 		return -1;
 
-	return oideq(cache_tree_oid, get_commit_tree_oid(head_commit));
+	return oideq(cache_tree_oid, &head_tree_oid);
 }
 
 static int write_author_script(const char *message)
diff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh
index aeab689a98..390e0ed186 100755
--- a/t/t3501-revert-cherry-pick.sh
+++ b/t/t3501-revert-cherry-pick.sh
@@ -112,6 +112,17 @@ test_expect_success 'cherry-pick on unborn branch' '
 	test_cmp_rev ! initial HEAD
 '
 
+test_expect_success 'cherry-pick on unborn branch with --allow-empty' '
+	git checkout main &&
+	git branch -D unborn &&
+	git checkout --orphan unborn &&
+	git rm --cached -r . &&
+	rm -rf * &&
+	git cherry-pick initial --allow-empty &&
+	git diff --quiet initial &&
+	test_cmp_rev ! initial HEAD
+'
+
 test_expect_success 'cherry-pick "-" to pick from previous branch' '
 	git checkout unborn &&
 	test_commit to-pick actual content &&
-- 
2.43.0
Previous: Junio C HamanoNext: phillip.wood123@gmail.com
Message 48 of 118 in “sequencer: Do not require `allow_empty` for redundant commit options”
  1. 1/4 sequencer: Do not require `allow_empty` for redundant commit optionsbrianmlyles@gmail.com, Jan 19, 2024
  2. 2/4 docs: Clean up `--empty` formatting in `git-rebase` and `git-am`brianmlyles@gmail.com, Jan 19, 2024
  3. Phillip WoodJan 23, 2024
  4. Brian LylesJan 27, 2024
  5. Phillip WoodFeb 1, 2024
  6. 3/4 rebase: Update `--empty=ask` to `--empty=drop`brianmlyles@gmail.com, Jan 19, 2024
  7. Phillip WoodJan 23, 2024
  8. Brian LylesJan 27, 2024
  9. Phillip WoodFeb 1, 2024
  10. 4/4 cherry-pick: Add `--empty` for more robust redundant commit handlingbrianmlyles@gmail.com, Jan 19, 2024
  11. Kristoffer HaugsbakkJan 20, 2024
  12. Brian LylesJan 21, 2024
  13. Kristoffer HaugsbakkJan 21, 2024
  14. Junio C HamanoJan 21, 2024
  15. Phillip WoodJan 22, 2024
  16. Kristoffer HaugsbakkJan 22, 2024
  17. Brian LylesJan 23, 2024
  18. Kristoffer HaugsbakkJan 23, 2024
  19. Junio C HamanoJan 23, 2024
  20. Subject: [PATCH] CoC: whitespace fixJunio C Hamano, Jan 23, 2024
  21. Elijah NewrenJan 24, 2024
  22. Junio C HamanoJan 23, 2024
  23. Phillip WoodJan 23, 2024
  24. Junio C HamanoJan 23, 2024
  25. Brian LylesJan 28, 2024
  26. Brian LylesJan 27, 2024
  27. Kristoffer HaugsbakkJan 20, 2024
  28. Brian LylesJan 21, 2024
  29. Phillip WoodJan 23, 2024
  30. Junio C HamanoJan 23, 2024
  31. Phillip WoodJan 24, 2024
  32. Phillip WoodJan 24, 2024
  33. Brian LylesJan 27, 2024
  34. Brian LylesJan 28, 2024
  35. Phillip WoodJan 29, 2024
  36. Brian LylesFeb 10, 2024
  37. Phillip WoodFeb 1, 2024
  38. Brian LylesFeb 10, 2024
  39. 0/8 cherry-pick: add `--empty`Brian Lyles, Feb 10, 2024
  40. phillip.wood123@gmail.comFeb 22, 2024
  41. 1/8 docs: address inaccurate `--empty` default with `--exec`Brian Lyles, Feb 10, 2024
  42. 2/8 docs: clean up `--empty` formatting in git-rebase(1) and git-am(1)Brian Lyles, Feb 10, 2024
  43. 3/8 rebase: update `--empty=ask` to `--empty=drop`Brian Lyles, Feb 10, 2024
  44. Brian LylesFeb 11, 2024
  45. Phillip WoodFeb 14, 2024
  46. phillip.wood123@gmail.comFeb 22, 2024
  47. Junio C HamanoFeb 22, 2024
  48. 4/8 sequencer: treat error reading HEAD as unborn branchBrian Lyles, Feb 10, 2024
  49. phillip.wood123@gmail.comFeb 22, 2024
  50. Brian LylesFeb 23, 2024
  51. phillip.wood123@gmail.comFeb 25, 2024
  52. 5/8 sequencer: do not require `allow_empty` for redundant commit optionsBrian Lyles, Feb 10, 2024
  53. phillip.wood123@gmail.comFeb 22, 2024
  54. 6/8 cherry-pick: decouple `--allow-empty` and `--keep-redundant-commits`Brian Lyles, Feb 10, 2024
  55. Phillip WoodFeb 22, 2024
  56. Junio C HamanoFeb 22, 2024
  57. 7/8 cherry-pick: enforce `--keep-redundant-commits` incompatibilityBrian Lyles, Feb 10, 2024
  58. Phillip WoodFeb 22, 2024
  59. Brian LylesFeb 23, 2024
  60. Junio C HamanoFeb 23, 2024
  61. phillip.wood123@gmail.comFeb 25, 2024
  62. Brian LylesFeb 26, 2024
  63. 8/8 cherry-pick: add `--empty` for more robust redundant commit handlingBrian Lyles, Feb 10, 2024
  64. Jean-Noël AVILAFeb 11, 2024
  65. Brian LylesFeb 12, 2024
  66. phillip.wood123@gmail.comFeb 22, 2024
  67. Brian LylesFeb 23, 2024
  68. phillip.wood123@gmail.comFeb 25, 2024
  69. Brian LylesFeb 26, 2024
  70. Brian LylesFeb 26, 2024
  71. phillip.wood123@gmail.comFeb 27, 2024
  72. Junio C HamanoFeb 27, 2024
  73. 0/7 cherry-pick: add `--empty` for more robust redundant commit handlingBrian Lyles, Mar 10, 2024
  74. phillip.wood123@gmail.comMar 13, 2024
  75. 1/7 docs: address inaccurate `--empty` default with `--exec`Brian Lyles, Mar 10, 2024
  76. 2/7 docs: clean up `--empty` formatting in git-rebase(1) and git-am(1)Brian Lyles, Mar 10, 2024
  77. 3/7 rebase: update `--empty=ask` to `--empty=stop`Brian Lyles, Mar 10, 2024
  78. 4/7 sequencer: treat error reading HEAD as unborn branchBrian Lyles, Mar 10, 2024
  79. Junio C HamanoMar 11, 2024
  80. Junio C HamanoMar 11, 2024
  81. Brian LylesMar 12, 2024
  82. Junio C HamanoMar 12, 2024
  83. Brian LylesMar 16, 2024
  84. phillip.wood123@gmail.comMar 13, 2024
  85. Brian LylesMar 16, 2024
  86. 5/7 sequencer: do not require `allow_empty` for redundant commit optionsBrian Lyles, Mar 10, 2024
  87. 6/7 cherry-pick: enforce `--keep-redundant-commits` incompatibilityBrian Lyles, Mar 10, 2024
  88. 7/7 cherry-pick: add `--empty` for more robust redundant commit handlingBrian Lyles, Mar 10, 2024
  89. phillip.wood123@gmail.comMar 13, 2024
  90. Junio C HamanoMar 13, 2024
  91. Brian LylesMar 16, 2024
  92. phillip.wood123@gmail.comMar 20, 2024
  93. 0/7 cherry-pick: add `--empty` for more robust redundant commit handlingBrian Lyles, Mar 20, 2024
  94. phillip.wood123@gmail.comMar 25, 2024
  95. Brian LylesMar 25, 2024
  96. phillip.wood123@gmail.comMar 25, 2024
  97. Junio C HamanoMar 25, 2024
  98. 1/7 docs: address inaccurate `--empty` default with `--exec`Brian Lyles, Mar 20, 2024
  99. 2/7 docs: clean up `--empty` formatting in git-rebase(1) and git-am(1)Brian Lyles, Mar 20, 2024
  100. 3/7 rebase: update `--empty=ask` to `--empty=stop`Brian Lyles, Mar 20, 2024
  101. 4/7 sequencer: handle unborn branch with `--allow-empty`Brian Lyles, Mar 20, 2024
  102. Dirk GoudersMar 21, 2024
  103. Junio C HamanoMar 21, 2024
  104. Dirk GoudersMar 21, 2024
  105. 5/7 sequencer: do not require `allow_empty` for redundant commit optionsBrian Lyles, Mar 20, 2024
  106. 6/7 cherry-pick: enforce `--keep-redundant-commits` incompatibilityBrian Lyles, Mar 20, 2024
  107. 7/7 cherry-pick: add `--empty` for more robust redundant commit handlingBrian Lyles, Mar 20, 2024
  108. 0/7 cherry-pick: add `--empty` for more robust redundant commit handlingBrian Lyles, Mar 25, 2024
  109. phillip.wood123@gmail.comMar 26, 2024
  110. Junio C HamanoMar 26, 2024
  111. phillip.wood123@gmail.comMar 27, 2024
  112. 1/7 docs: address inaccurate `--empty` default with `--exec`Brian Lyles, Mar 25, 2024
  113. 2/7 docs: clean up `--empty` formatting in git-rebase(1) and git-am(1)Brian Lyles, Mar 25, 2024
  114. 3/7 rebase: update `--empty=ask` to `--empty=stop`Brian Lyles, Mar 25, 2024
  115. 4/7 sequencer: handle unborn branch with `--allow-empty`Brian Lyles, Mar 25, 2024
  116. 5/7 sequencer: do not require `allow_empty` for redundant commit optionsBrian Lyles, Mar 25, 2024
  117. 6/7 cherry-pick: enforce `--keep-redundant-commits` incompatibilityBrian Lyles, Mar 25, 2024
  118. 7/7 cherry-pick: add `--empty` for more robust redundant commit handlingBrian Lyles, Mar 25, 2024

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.