@matheusbsbs / matheus-bagliano / commits / 5602b500c3
Fork 0

builtin/checkout: fix `git checkout -p HEAD...` bug

Running `git checkout -p` with a merge-base rev results in an error: $ git checkout -p HEAD... usage: git diff-index [-m] [--cached] [<common-diff-options>] <tree-ish> [<path>...] common diff options: -z output diff-raw with lines terminated with NUL. -p output patch format. -u synonym for -p. --patch-with-raw output both a patch and the diff-raw format. --stat show diffstat instead of patch. --numstat show numeric diffstat instead of patch. --patch-with-stat output a patch and prepend its diffstat. --name-only show only names of changed files. --name-status show names and status of changed files. --full-index show full object name on index lines. --abbrev=<n> abbreviate object names in diff-tree header and diff-raw. -R swap input file pairs. -B detect complete rewrites. -M detect renames. -C detect copies. --find-copies-harder try unchanged files as candidate for copy detection. -l<n> limit rename attempts up to <n> paths. -O<file> reorder diffs according to the <file>. -S<string> find filepair whose only one side contains the string. --pickaxe-all show all files diff when -S is used and hit is found. -a --text treat all files as text. Cannot close git diff-index --cached --numstat --summary HEAD... -- () at <redacted>/libexec/git-core/git-add--interactive line 183. This happens because checkout passes the literal argument (in the example, `HEAD...`) to diff-index which does not recognise merge-base revs. Fix this by using the hex of the found commit instead of the given name. Note that "HEAD" is handled specially in run_add_interactive() so it's explicitly not changed. Signed-off-by: Denton Liu <liu.denton@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Denton Liu committed Oct 7, 2020 at 00:56 UTC 5602b500c3cd9ac308bf9af0d5f0a79bd2195346
3 files changed +29 -1
builtin/checkout.c
+14 -1
@@ -470,6 +470,19 @@ static int checkout_paths(const struct checkout_opts *opts,
470
471 if (opts->patch_mode) {
472 const char *patch_mode;
473 + const char *rev = new_branch_info->name;
474 + char rev_oid[GIT_MAX_HEXSZ + 1];
475 +
476 + /*
477 + * Since rev can be in the form of `<a>...<b>` (which is not
478 + * recognized by diff-index), we will always replace the name
479 + * with the hex of the commit (whether it's in `...` form or
480 + * not) for the run_add_interactive() machinery to work
481 + * properly. However, there is special logic for the HEAD case
482 + * so we mustn't replace that.
483 + */
484 + if (rev && strcmp(rev, "HEAD"))
485 + rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid);
486
487 if (opts->checkout_index && opts->checkout_worktree)
488 patch_mode = "--patch=checkout";
@@ -480,7 +493,7 @@ static int checkout_paths(const struct checkout_opts *opts,
493 else
494 BUG("either flag must have been set, worktree=%d, index=%d",
495 opts->checkout_worktree, opts->checkout_index);
483 - return run_add_interactive(new_branch_info->name, patch_mode, &opts->pathspec);
496 + return run_add_interactive(rev, patch_mode, &opts->pathspec);
497 }
498
499 repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);
t/t2016-checkout-patch.sh
+7
@@ -59,6 +59,13 @@ test_expect_success PERL 'git checkout -p HEAD with change already staged' '
59 verify_state dir/foo head head
60 '
61
62 +test_expect_success PERL 'git checkout -p HEAD^...' '
63 + # the third n is to get out in case it mistakenly does not apply
64 + test_write_lines n y n | git checkout -p HEAD^... &&
65 + verify_saved_state bar &&
66 + verify_state dir/foo parent parent
67 +'
68 +
69 test_expect_success PERL 'git checkout -p HEAD^' '
70 # the third n is to get out in case it mistakenly does not apply
71 test_write_lines n y n | git checkout -p HEAD^ &&
t/t2071-restore-patch.sh
+8
@@ -60,6 +60,14 @@ test_expect_success PERL 'git restore -p --source=HEAD^' '
60 verify_state dir/foo parent index
61 '
62
63 +test_expect_success PERL 'git restore -p --source=HEAD^...' '
64 + set_state dir/foo work index &&
65 + # the third n is to get out in case it mistakenly does not apply
66 + test_write_lines n y n | git restore -p --source=HEAD^... &&
67 + verify_saved_state bar &&
68 + verify_state dir/foo parent index
69 +'
70 +
71 test_expect_success PERL 'git restore -p handles deletion' '
72 set_state dir/foo work index &&
73 rm dir/foo &&