git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Cc: Alejandro Sanchez <asanchez1987@gmail.com>, git@vger.kernel.org
Subject: [PATCH v2 4/4] am: fix --interactive HEAD tree resolution
Date: Fri, 24 May 2019 02:46:27 -0400	[thread overview]
Message-ID: <20190524064626.GE25694@sigill.intra.peff.net> (raw)
In-Reply-To: <20190524063955.GD25694@sigill.intra.peff.net>

In --interactive mode, "git am --resolved" will try to generate a patch
based on what is in the index, so that it can prompt "apply this
patch?". To do so it needs the tree of HEAD, which it tries to get with
get_oid_tree(). However, this doesn't yield a tree object; the "tree"
part just means "if you must disambiguate short oids, then prefer trees"
(and we do not need to disambiguate at all, since we are feeding a ref).

Instead, we must parse the oid as a commit (which should always be true
in a non-corrupt repository), and access its tree pointer manually.

This has been broken since the conversion to C in 7ff2683253
(builtin-am: implement -i/--interactive, 2015-08-04), but there was no
test coverage because of interactive-mode's insistence on having a tty.
That was lifted in the previous commit, so we can now add a test for
this case.

Note that before this patch, the test would result in a BUG() which
comes from 3506dc9445 (has_uncommitted_changes(): fall back to empty
tree, 2018-07-11). But before that, we'd have simply segfaulted (and in
fact this is the exact type of case the BUG() added there was trying to
catch!).

Signed-off-by: Jeff King <peff@peff.net>
---
This addresses points raised in Dscho's review (except for the test_cmp
thing). Since there were no changes to patches 1-3, I didn't bother
re-posting. Diff against v1 below.

  diff --git a/builtin/am.c b/builtin/am.c
  index 33bd7a6eab..78389d08b6 100644
  --- a/builtin/am.c
  +++ b/builtin/am.c
  @@ -1340,14 +1340,7 @@ static void write_index_patch(const struct am_state *state)
   	FILE *fp;
   
   	if (!get_oid("HEAD", &head)) {
  -		struct object *obj;
  -		struct commit *commit;
  -
  -		obj = parse_object_or_die(&head, NULL);
  -		commit = object_as_type(the_repository, obj, OBJ_COMMIT, 0);
  -		if (!commit)
  -			die("unable to parse HEAD as a commit");
  -
  +		struct commit *commit = lookup_commit_or_die(&head, "HEAD");
   		tree = get_commit_tree(commit);
   	} else
   		tree = lookup_tree(the_repository,
  diff --git a/t/t4257-am-interactive.sh b/t/t4257-am-interactive.sh
  index 6989bf7aba..5344bd248a 100755
  --- a/t/t4257-am-interactive.sh
  +++ b/t/t4257-am-interactive.sh
  @@ -23,7 +23,8 @@ test_expect_success 'applying all patches generates conflict' '
   
   test_expect_success 'interactive am can apply a single patch' '
   	git reset --hard base &&
  -	printf "%s\n" y n | git am -i mbox &&
  +	# apply the first, but not the second
  +	test_write_lines y n | git am -i mbox &&
   
   	echo no-conflict >expect &&
   	git log -1 --format=%s >actual &&
  @@ -32,10 +33,12 @@ test_expect_success 'interactive am can apply a single patch' '
   
   test_expect_success 'interactive am can resolve conflict' '
   	git reset --hard base &&
  -	printf "%s\n" y y | test_must_fail git am -i mbox &&
  +	# apply both; the second one will conflict
  +	test_write_lines y y | test_must_fail git am -i mbox &&
   	echo resolved >file &&
   	git add -u &&
  -	printf "%s\n" v y | git am -i --resolved &&
  +	# interactive "--resolved" will ask us if we want to apply the result
  +	echo y | git am -i --resolved &&
   
   	echo conflict-patch >expect &&
   	git log -1 --format=%s >actual &&

 builtin/am.c              |  7 +++---
 t/t4257-am-interactive.sh | 52 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 3 deletions(-)
 create mode 100755 t/t4257-am-interactive.sh

diff --git a/builtin/am.c b/builtin/am.c
index ea16b844f1..78389d08b6 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1339,9 +1339,10 @@ static void write_index_patch(const struct am_state *state)
 	struct rev_info rev_info;
 	FILE *fp;
 
-	if (!get_oid_tree("HEAD", &head))
-		tree = lookup_tree(the_repository, &head);
-	else
+	if (!get_oid("HEAD", &head)) {
+		struct commit *commit = lookup_commit_or_die(&head, "HEAD");
+		tree = get_commit_tree(commit);
+	} else
 		tree = lookup_tree(the_repository,
 				   the_repository->hash_algo->empty_tree);
 
diff --git a/t/t4257-am-interactive.sh b/t/t4257-am-interactive.sh
new file mode 100755
index 0000000000..5344bd248a
--- /dev/null
+++ b/t/t4257-am-interactive.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='am --interactive tests'
+. ./test-lib.sh
+
+test_expect_success 'set up patches to apply' '
+	test_commit unrelated &&
+	test_commit no-conflict &&
+	test_commit conflict-patch file patch &&
+	git format-patch --stdout -2 >mbox &&
+
+	git reset --hard unrelated &&
+	test_commit conflict-master file master base
+'
+
+# Sanity check our setup.
+test_expect_success 'applying all patches generates conflict' '
+	test_must_fail git am mbox &&
+	echo resolved >file &&
+	git add -u &&
+	git am --resolved
+'
+
+test_expect_success 'interactive am can apply a single patch' '
+	git reset --hard base &&
+	# apply the first, but not the second
+	test_write_lines y n | git am -i mbox &&
+
+	echo no-conflict >expect &&
+	git log -1 --format=%s >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'interactive am can resolve conflict' '
+	git reset --hard base &&
+	# apply both; the second one will conflict
+	test_write_lines y y | test_must_fail git am -i mbox &&
+	echo resolved >file &&
+	git add -u &&
+	# interactive "--resolved" will ask us if we want to apply the result
+	echo y | git am -i --resolved &&
+
+	echo conflict-patch >expect &&
+	git log -1 --format=%s >actual &&
+	test_cmp expect actual &&
+
+	echo resolved >expect &&
+	git cat-file blob HEAD:file >actual &&
+	test_cmp expect actual
+'
+
+test_done
-- 
2.22.0.rc1.549.gadb183c4cb


  reply	other threads:[~2019-05-24  6:46 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-20  8:35 Abort (core dumped) Alejandro Sanchez
2019-05-20 10:02 ` Jeff King
2019-05-20 12:06   ` [PATCH 0/4] fix BUG() with "git am -i --resolved" Jeff King
2019-05-20 12:07     ` [PATCH 1/4] am: simplify prompt response handling Jeff King
2019-05-20 12:09     ` [PATCH 2/4] am: read interactive input from stdin Jeff King
2019-05-20 12:11     ` [PATCH 3/4] am: drop tty requirement for --interactive Jeff King
2019-05-20 12:50       ` Jeff King
2019-05-23  6:44         ` Johannes Schindelin
2019-05-24  6:27           ` Jeff King
2021-11-02 16:48             ` [PATCH 0/2] prompt.c: split up git_prompt(), read from /dev/tty, not STDIN Ævar Arnfjörð Bjarmason
2021-11-02 16:48               ` [PATCH 1/2] prompt.c: split up the password and non-password handling Ævar Arnfjörð Bjarmason
2021-11-03 11:53                 ` Jeff King
2021-11-03 17:28                   ` Junio C Hamano
2021-11-02 16:48               ` [PATCH 2/2] prompt.c: add and use a GIT_TEST_TERMINAL_PROMPT=true Ævar Arnfjörð Bjarmason
2021-11-03 11:57                 ` Jeff King
2021-11-03 15:12                   ` Ævar Arnfjörð Bjarmason
2021-11-04  9:58                     ` Jeff King
2021-11-03 17:42                   ` Junio C Hamano
2021-11-04  8:48                     ` Johannes Schindelin
2021-11-04  9:47                       ` Jeff King
2021-11-04  9:53                     ` Jeff King
2019-05-20 12:13     ` [PATCH 4/4] am: fix --interactive HEAD tree resolution Jeff King
2019-05-23  7:12       ` Johannes Schindelin
2019-05-24  6:39         ` Jeff King
2019-05-24  6:46           ` Jeff King [this message]
2019-05-28 11:06           ` Johannes Schindelin
2019-05-28 21:35             ` Jeff King
2019-05-29 11:56               ` Johannes Schindelin
2019-09-26 14:20                 ` Alejandro Sanchez
2019-09-26 21:11                   ` Jeff King

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20190524064626.GE25694@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=asanchez1987@gmail.com \
    --cc=git@vger.kernel.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).