Skip to content

Commit 5602b50

Browse files
Denton-Lgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 47ae905 commit 5602b50

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

builtin/checkout.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,19 @@ static int checkout_paths(const struct checkout_opts *opts,
470470

471471
if (opts->patch_mode) {
472472
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);
473486

474487
if (opts->checkout_index && opts->checkout_worktree)
475488
patch_mode = "--patch=checkout";
@@ -480,7 +493,7 @@ static int checkout_paths(const struct checkout_opts *opts,
480493
else
481494
BUG("either flag must have been set, worktree=%d, index=%d",
482495
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);
484497
}
485498

486499
repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR);

t/t2016-checkout-patch.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ test_expect_success PERL 'git checkout -p HEAD with change already staged' '
5959
verify_state dir/foo head head
6060
'
6161

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+
6269
test_expect_success PERL 'git checkout -p HEAD^' '
6370
# the third n is to get out in case it mistakenly does not apply
6471
test_write_lines n y n | git checkout -p HEAD^ &&

t/t2071-restore-patch.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ test_expect_success PERL 'git restore -p --source=HEAD^' '
6060
verify_state dir/foo parent index
6161
'
6262

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+
6371
test_expect_success PERL 'git restore -p handles deletion' '
6472
set_state dir/foo work index &&
6573
rm dir/foo &&

0 commit comments

Comments
 (0)