All of lore.kernel.org
 help / color / mirror / Atom feed
From: Brian Lyles <brianmlyles@gmail.com>
To: git@vger.kernel.org
Cc: Brian Lyles <brianmlyles@gmail.com>,
	newren@gmail.com, me@ttaylorr.com, phillip.wood123@gmail.com,
	gitster@pobox.com, Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: [PATCH v3 4/7] sequencer: treat error reading HEAD as unborn branch
Date: Sun, 10 Mar 2024 13:42:03 -0500	[thread overview]
Message-ID: <20240310184602.539656-5-brianmlyles@gmail.com> (raw)
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.

Add a new test to cover this scenario. While modelled off of the
existing 'cherry-pick on unborn branch' test, some improvements can be
made:

- Use `git switch --orphan unborn` instead of `git checkout --orphan
  unborn` to avoid the need for a separate `rm -rf *` call
- Avoid using `--quiet` in the `git diff` call to make debugging easier
  in the event of a failure

Make these improvements to the existing test as well as the new test.

Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Brian Lyles <brianmlyles@gmail.com>
---

Differences from v2:

- Minor code and test cleanup per [1] and the other replies in that
  thread

[1]: https://lore.kernel.org/git/62247a1c-0249-4ce1-8626-fe97b89c23dc@gmail.com/

 sequencer.c                   | 35 +++++++++++++++++++++--------------
 t/t3501-revert-cherry-pick.sh | 14 +++++++++++---
 2 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/sequencer.c b/sequencer.c
index f49a871ac0..a62ce244c1 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -770,29 +770,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;
+	const struct object_id *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..8a1d154ca6 100755
--- a/t/t3501-revert-cherry-pick.sh
+++ b/t/t3501-revert-cherry-pick.sh
@@ -104,11 +104,19 @@ test_expect_success 'revert forbidden on dirty working tree' '
 '
 
 test_expect_success 'cherry-pick on unborn branch' '
-	git checkout --orphan unborn &&
+	git switch --orphan unborn &&
 	git rm --cached -r . &&
-	rm -rf * &&
 	git cherry-pick initial &&
-	git diff --quiet initial &&
+	git diff initial &&
+	test_cmp_rev ! initial HEAD
+'
+
+test_expect_success 'cherry-pick on unborn branch with --allow-empty' '
+	git checkout --detach &&
+	git branch -D unborn &&
+	git switch --orphan unborn &&
+	git cherry-pick initial --allow-empty &&
+	git diff initial &&
 	test_cmp_rev ! initial HEAD
 '
 
-- 
2.43.0


  parent reply	other threads:[~2024-03-10 18:47 UTC|newest]

Thread overview: 118+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-19  5:59 [PATCH 1/4] sequencer: Do not require `allow_empty` for redundant commit options brianmlyles
2024-01-19  5:59 ` [PATCH 2/4] docs: Clean up `--empty` formatting in `git-rebase` and `git-am` brianmlyles
2024-01-23 14:24   ` Phillip Wood
2024-01-27 21:22     ` Brian Lyles
2024-02-01 14:02       ` Phillip Wood
2024-01-19  5:59 ` [PATCH 3/4] rebase: Update `--empty=ask` to `--empty=drop` brianmlyles
2024-01-23 14:24   ` Phillip Wood
2024-01-27 21:49     ` Brian Lyles
2024-02-01 14:02       ` Phillip Wood
2024-01-19  5:59 ` [PATCH 4/4] cherry-pick: Add `--empty` for more robust redundant commit handling brianmlyles
2024-01-20 20:24   ` Kristoffer Haugsbakk
2024-01-21 18:28     ` Brian Lyles
2024-01-21 22:05       ` Kristoffer Haugsbakk
2024-01-21 22:41       ` Junio C Hamano
2024-01-22 10:40       ` Phillip Wood
2024-01-22 20:55       ` Kristoffer Haugsbakk
2024-01-23  5:23         ` Brian Lyles
2024-01-23  7:11           ` Kristoffer Haugsbakk
2024-01-23 17:32           ` Junio C Hamano
2024-01-23 18:41             ` Subject: [PATCH] CoC: whitespace fix Junio C Hamano
2024-01-24  3:06               ` Elijah Newren
2024-01-23 18:49             ` [PATCH 4/4] cherry-pick: Add `--empty` for more robust redundant commit handling Junio C Hamano
2024-01-23 14:25   ` Phillip Wood
2024-01-23 18:01     ` Junio C Hamano
2024-01-28  0:07       ` Brian Lyles
2024-01-27 23:56     ` Brian Lyles
2024-01-20 21:38 ` [PATCH 1/4] sequencer: Do not require `allow_empty` for redundant commit options Kristoffer Haugsbakk
2024-01-21 18:19   ` Brian Lyles
2024-01-23 14:23 ` Phillip Wood
2024-01-23 18:18   ` Junio C Hamano
2024-01-24 11:01   ` Phillip Wood
2024-01-24 11:01 ` Phillip Wood
2024-01-27 23:30   ` Brian Lyles
2024-01-28 16:36     ` Brian Lyles
2024-01-29 10:55       ` Phillip Wood
2024-02-10  5:50         ` Brian Lyles
2024-02-01 10:57     ` Phillip Wood
2024-02-10  4:34       ` Brian Lyles
2024-02-10  7:43 ` [PATCH v2 0/8] cherry-pick: add `--empty` Brian Lyles
2024-02-22 16:39   ` phillip.wood123
2024-02-10  7:43 ` [PATCH v2 1/8] docs: address inaccurate `--empty` default with `--exec` Brian Lyles
2024-02-10  7:43 ` [PATCH v2 2/8] docs: clean up `--empty` formatting in git-rebase(1) and git-am(1) Brian Lyles
2024-02-10  7:43 ` [PATCH v2 3/8] rebase: update `--empty=ask` to `--empty=drop` Brian Lyles
2024-02-11  4:54   ` Brian Lyles
2024-02-14 11:05     ` Phillip Wood
2024-02-22 16:34   ` phillip.wood123
2024-02-22 18:27     ` Junio C Hamano
2024-02-10  7:43 ` [PATCH v2 4/8] sequencer: treat error reading HEAD as unborn branch Brian Lyles
2024-02-22 16:34   ` phillip.wood123
2024-02-23  5:28     ` Brian Lyles
2024-02-25 16:57       ` phillip.wood123
2024-02-10  7:43 ` [PATCH v2 5/8] sequencer: do not require `allow_empty` for redundant commit options Brian Lyles
2024-02-22 16:35   ` phillip.wood123
2024-02-10  7:43 ` [PATCH v2 6/8] cherry-pick: decouple `--allow-empty` and `--keep-redundant-commits` Brian Lyles
2024-02-22 16:35   ` Phillip Wood
2024-02-22 18:41     ` Junio C Hamano
2024-02-10  7:43 ` [PATCH v2 7/8] cherry-pick: enforce `--keep-redundant-commits` incompatibility Brian Lyles
2024-02-22 16:35   ` Phillip Wood
2024-02-23  6:23     ` Brian Lyles
2024-02-23 17:41       ` Junio C Hamano
2024-02-25 16:58       ` phillip.wood123
2024-02-26  3:04         ` Brian Lyles
2024-02-10  7:43 ` [PATCH v2 8/8] cherry-pick: add `--empty` for more robust redundant commit handling Brian Lyles
2024-02-11 20:50   ` Jean-Noël AVILA
2024-02-12  1:35     ` Brian Lyles
2024-02-22 16:36   ` phillip.wood123
2024-02-23  6:58     ` Brian Lyles
2024-02-25 16:57       ` phillip.wood123
2024-02-26  2:21         ` Brian Lyles
2024-02-26  3:32         ` Brian Lyles
2024-02-27 10:39           ` phillip.wood123
2024-02-27 17:33             ` Junio C Hamano
2024-03-10 18:41 ` [PATCH v3 0/7] " Brian Lyles
2024-03-13 16:12   ` phillip.wood123
2024-03-10 18:42 ` [PATCH v3 1/7] docs: address inaccurate `--empty` default with `--exec` Brian Lyles
2024-03-10 18:42 ` [PATCH v3 2/7] docs: clean up `--empty` formatting in git-rebase(1) and git-am(1) Brian Lyles
2024-03-10 18:42 ` [PATCH v3 3/7] rebase: update `--empty=ask` to `--empty=stop` Brian Lyles
2024-03-10 18:42 ` Brian Lyles [this message]
2024-03-11  0:07   ` [PATCH v3 4/7] sequencer: treat error reading HEAD as unborn branch Junio C Hamano
2024-03-11 16:54     ` Junio C Hamano
2024-03-12  2:04       ` Brian Lyles
2024-03-12 22:25         ` Junio C Hamano
2024-03-16  3:05           ` Brian Lyles
2024-03-13 15:10   ` phillip.wood123
2024-03-16  3:07     ` Brian Lyles
2024-03-10 18:42 ` [PATCH v3 5/7] sequencer: do not require `allow_empty` for redundant commit options Brian Lyles
2024-03-10 18:42 ` [PATCH v3 6/7] cherry-pick: enforce `--keep-redundant-commits` incompatibility Brian Lyles
2024-03-10 18:42 ` [PATCH v3 7/7] cherry-pick: add `--empty` for more robust redundant commit handling Brian Lyles
2024-03-13 16:10   ` phillip.wood123
2024-03-13 17:17     ` Junio C Hamano
2024-03-16  5:20       ` Brian Lyles
2024-03-20 19:35         ` phillip.wood123
2024-03-20 23:36 ` [PATCH v4 0/7] " Brian Lyles
2024-03-25 14:38   ` phillip.wood123
2024-03-25 16:12     ` Brian Lyles
2024-03-25 19:36       ` phillip.wood123
2024-03-25 20:57         ` Junio C Hamano
2024-03-20 23:36 ` [PATCH v4 1/7] docs: address inaccurate `--empty` default with `--exec` Brian Lyles
2024-03-20 23:36 ` [PATCH v4 2/7] docs: clean up `--empty` formatting in git-rebase(1) and git-am(1) Brian Lyles
2024-03-20 23:36 ` [PATCH v4 3/7] rebase: update `--empty=ask` to `--empty=stop` Brian Lyles
2024-03-20 23:36 ` [PATCH v4 4/7] sequencer: handle unborn branch with `--allow-empty` Brian Lyles
2024-03-21  9:52   ` Dirk Gouders
2024-03-21 16:22     ` Junio C Hamano
2024-03-21 19:45       ` Dirk Gouders
2024-03-20 23:37 ` [PATCH v4 5/7] sequencer: do not require `allow_empty` for redundant commit options Brian Lyles
2024-03-20 23:37 ` [PATCH v4 6/7] cherry-pick: enforce `--keep-redundant-commits` incompatibility Brian Lyles
2024-03-20 23:37 ` [PATCH v4 7/7] cherry-pick: add `--empty` for more robust redundant commit handling Brian Lyles
2024-03-25 23:16 ` [PATCH v5 0/7] " Brian Lyles
2024-03-26 14:45   ` phillip.wood123
2024-03-26 18:28     ` Junio C Hamano
2024-03-27 16:37       ` phillip.wood123
2024-03-25 23:16 ` [PATCH v5 1/7] docs: address inaccurate `--empty` default with `--exec` Brian Lyles
2024-03-25 23:16 ` [PATCH v5 2/7] docs: clean up `--empty` formatting in git-rebase(1) and git-am(1) Brian Lyles
2024-03-25 23:16 ` [PATCH v5 3/7] rebase: update `--empty=ask` to `--empty=stop` Brian Lyles
2024-03-25 23:16 ` [PATCH v5 4/7] sequencer: handle unborn branch with `--allow-empty` Brian Lyles
2024-03-25 23:16 ` [PATCH v5 5/7] sequencer: do not require `allow_empty` for redundant commit options Brian Lyles
2024-03-25 23:16 ` [PATCH v5 6/7] cherry-pick: enforce `--keep-redundant-commits` incompatibility Brian Lyles
2024-03-25 23:16 ` [PATCH v5 7/7] cherry-pick: add `--empty` for more robust redundant commit handling Brian Lyles

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240310184602.539656-5-brianmlyles@gmail.com \
    --to=brianmlyles@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=me@ttaylorr.com \
    --cc=newren@gmail.com \
    --cc=phillip.wood123@gmail.com \
    --cc=phillip.wood@dunelm.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.