@matheusbsbs / matheus-bagliano / commits / 0a8e3036a3
Fork 0

reset: parse rev as tree-ish in patch mode

Since 2f328c3d ("reset $sha1 $pathspec: require $sha1 only to be treeish", 2013-01-14), we allowed "git reset $object -- $path" to reset individual paths that match the pathspec to take the blob from a tree object, not necessarily a commit, while the form to reset the tip of the current branch to some other commit still must be given a commit. Like resetting with paths, "git reset --patch" does not update HEAD, and need not require a commit. The path-filtered form, "git reset --patch $object -- $pathspec", has accepted a tree-ish since 2f328c3d. "git reset --patch" is documented as accepting a <tree-ish> since bf44142f ("reset: update documentation to require only tree-ish with paths", 2013-01-16). Documentation changes are not required. Loosen the restriction that requires a commit for the unfiltered "git reset --patch $object". Signed-off-by: Nika Layzell <nika@thelayzells.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nika Layzell committed Nov 24, 2019 at 20:25 UTC 0a8e3036a304d99de24432d65a3d3920dd6e2e46
2 files changed +22 -1
builtin/reset.c
+1 -1
@@ -320,7 +320,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
320 if (unborn) {
321 /* reset on unborn branch: treat as reset to empty tree */
322 oidcpy(&oid, the_hash_algo->empty_tree);
323 - } else if (!pathspec.nr) {
323 + } else if (!pathspec.nr && !patch_mode) {
324 struct commit *commit;
325 if (get_oid_committish(rev, &oid))
326 die(_("Failed to resolve '%s' as a valid revision."), rev);
t/t7105-reset-patch.sh
+21
@@ -38,6 +38,27 @@ test_expect_success PERL 'git reset -p HEAD^' '
38 test_i18ngrep "Apply" output
39 '
40
41 +test_expect_success PERL 'git reset -p HEAD^^{tree}' '
42 + test_write_lines n y | git reset -p HEAD^^{tree} >output &&
43 + verify_state dir/foo work parent &&
44 + verify_saved_state bar &&
45 + test_i18ngrep "Apply" output
46 +'
47 +
48 +test_expect_success PERL 'git reset -p HEAD^:dir/foo (blob fails)' '
49 + set_and_save_state dir/foo work work &&
50 + test_must_fail git reset -p HEAD^:dir/foo &&
51 + verify_saved_state dir/foo &&
52 + verify_saved_state bar
53 +'
54 +
55 +test_expect_success PERL 'git reset -p aaaaaaaa (unknown fails)' '
56 + set_and_save_state dir/foo work work &&
57 + test_must_fail git reset -p aaaaaaaa &&
58 + verify_saved_state dir/foo &&
59 + verify_saved_state bar
60 +'
61 +
62 # The idea in the rest is that bar sorts first, so we always say 'y'
63 # first and if the path limiter fails it'll apply to bar instead of
64 # dir/foo. There's always an extra 'n' to reject edits to dir/foo in