Subject: [WIP/PATCH 1/4] reset -p: accept "git reset -p <tree>"

From: Jonathan Nieder <[email-masked]>
Date: Tue, 19 Oct 2010 17:11:47 -0500

When reset -p was implemented (v1.6.5-rc0~5^2~7, 2009-08-15), it
piggy-backed on an existing "git reset" check to verify that the
<rev> argument represents a valid commit.  By dropping that
check, we can use reset -p to apply changes from an arbitrary
tree; for example, from the linux-2.6 tree:

	git reset -p 2.6.11 -- Makefile

add--interactive already rejects invalid refs.

	$ git init >dev/null 2>&1; git reset -p HEAD; echo $?
	fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
	Use '--' to separate paths from revisions
	128

Unfortunate side-effect: reset -p will accept a blob for <rev>,
too.

	$ git reset -p HEAD:git.c; echo $?
	error: bad tree object HEAD:git.c
	No changes.
	0

Signed-off-by: Jonathan Nieder <[email-masked]>
---
 builtin/reset.c        |   12 ++++++------
 t/t7105-reset-patch.sh |   12 ++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/builtin/reset.c b/builtin/reset.c
index 0037be4..a52e6f8 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -297,24 +297,24 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 			/* Otherwise we treat this as a filename */
 			verify_filename(prefix, argv[i]);
 		}
 	}
 
+	if (patch_mode) {
+		if (reset_type != NONE)
+			die("--patch is incompatible with --{hard,mixed,soft}");
+		return interactive_reset(rev, argv + i, prefix);
+	}
+
 	if (get_sha1(rev, sha1))
 		die("Failed to resolve '%s' as a valid ref.", rev);
 
 	commit = lookup_commit_reference(sha1);
 	if (!commit)
 		die("Could not parse object '%s'.", rev);
 	hashcpy(sha1, commit->object.sha1);
 
-	if (patch_mode) {
-		if (reset_type != NONE)
-			die("--patch is incompatible with --{hard,mixed,soft}");
-		return interactive_reset(rev, argv + i, prefix);
-	}
-
 	/* git reset tree [--] paths... can be used to
 	 * load chosen paths from the tree into the index without
 	 * affecting the working tree nor HEAD. */
 	if (i < argc) {
 		if (reset_type == MIXED)
diff --git a/t/t7105-reset-patch.sh b/t/t7105-reset-patch.sh
index 9891e2c..ba3ff42 100755
--- a/t/t7105-reset-patch.sh
+++ b/t/t7105-reset-patch.sh
@@ -46,10 +46,22 @@ test_expect_success PERL 'git reset -p dir' '
 	(echo y; echo n) | git reset -p dir &&
 	verify_state dir/foo work head &&
 	verify_saved_state bar
 '
 
+test_expect_success PERL 'git reset -p <tree> dir' '
+	set_state dir/foo work work &&
+	(echo y; echo n) | git reset -p HEAD^{tree} dir &&
+	verify_state dir/foo work head &&
+	verify_saved_state bar
+'
+
+test_expect_failure PERL 'git reset -p <blob>' '
+	set_state dir/foo work work &&
+	test_must_fail git reset -p HEAD:dir/foo
+'
+
 test_expect_success PERL 'git reset -p -- foo (inside dir)' '
 	set_state dir/foo work work
 	(echo y; echo n) | (cd dir && git reset -p -- foo) &&
 	verify_state dir/foo work head &&
 	verify_saved_state bar
-- 
1.7.2.3