From 5602b500c3cd9ac308bf9af0d5f0a79bd2195346 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Wed, 7 Oct 2020 00:56:15 -0700 Subject: 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: -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= 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 limit rename attempts up to paths. -O reorder diffs according to the . -S 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 /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 Signed-off-by: Junio C Hamano diff --git a/builtin/checkout.c b/builtin/checkout.c index af849c6..9a33b00 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -470,6 +470,19 @@ static int checkout_paths(const struct checkout_opts *opts, if (opts->patch_mode) { const char *patch_mode; + const char *rev = new_branch_info->name; + char rev_oid[GIT_MAX_HEXSZ + 1]; + + /* + * Since rev can be in the form of `...` (which is not + * recognized by diff-index), we will always replace the name + * with the hex of the commit (whether it's in `...` form or + * not) for the run_add_interactive() machinery to work + * properly. However, there is special logic for the HEAD case + * so we mustn't replace that. + */ + if (rev && strcmp(rev, "HEAD")) + rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid); if (opts->checkout_index && opts->checkout_worktree) patch_mode = "--patch=checkout"; @@ -480,7 +493,7 @@ static int checkout_paths(const struct checkout_opts *opts, else BUG("either flag must have been set, worktree=%d, index=%d", opts->checkout_worktree, opts->checkout_index); - return run_add_interactive(new_branch_info->name, patch_mode, &opts->pathspec); + return run_add_interactive(rev, patch_mode, &opts->pathspec); } repo_hold_locked_index(the_repository, &lock_file, LOCK_DIE_ON_ERROR); diff --git a/t/t2016-checkout-patch.sh b/t/t2016-checkout-patch.sh index 47aeb0b..999620e 100755 --- a/t/t2016-checkout-patch.sh +++ b/t/t2016-checkout-patch.sh @@ -59,6 +59,13 @@ test_expect_success PERL 'git checkout -p HEAD with change already staged' ' verify_state dir/foo head head ' +test_expect_success PERL 'git checkout -p HEAD^...' ' + # the third n is to get out in case it mistakenly does not apply + test_write_lines n y n | git checkout -p HEAD^... && + verify_saved_state bar && + verify_state dir/foo parent parent +' + test_expect_success PERL 'git checkout -p HEAD^' ' # the third n is to get out in case it mistakenly does not apply test_write_lines n y n | git checkout -p HEAD^ && diff --git a/t/t2071-restore-patch.sh b/t/t2071-restore-patch.sh index 98b2476..b5c5c0f 100755 --- a/t/t2071-restore-patch.sh +++ b/t/t2071-restore-patch.sh @@ -60,6 +60,14 @@ test_expect_success PERL 'git restore -p --source=HEAD^' ' verify_state dir/foo parent index ' +test_expect_success PERL 'git restore -p --source=HEAD^...' ' + set_state dir/foo work index && + # the third n is to get out in case it mistakenly does not apply + test_write_lines n y n | git restore -p --source=HEAD^... && + verify_saved_state bar && + verify_state dir/foo parent index +' + test_expect_success PERL 'git restore -p handles deletion' ' set_state dir/foo work index && rm dir/foo && -- cgit v0.10.2-6-g49f6 From c693ef781bef002bca15be74bec3a6a65c679eb5 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Wed, 7 Oct 2020 00:56:16 -0700 Subject: Doc: document "A...B" form for in checkout and switch Using "A...B" has been supported for the argument for a while. However, its support has never been explicitly documented. Explicitly document it so that users know that it is available. Signed-off-by: Denton Liu Signed-off-by: Junio C Hamano diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt index 5b697ee..8c2f736 100644 --- a/Documentation/git-checkout.txt +++ b/Documentation/git-checkout.txt @@ -350,6 +350,10 @@ leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. :: Tree to checkout from (when paths are given). If not specified, the index will be used. ++ +As a special case, you may use `"A...B"` as a shortcut for the +merge base of `A` and `B` if there is exactly one merge base. You can +leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. \--:: Do not interpret any more arguments as options. diff --git a/Documentation/git-restore.txt b/Documentation/git-restore.txt index 84c6c40..55bde91 100644 --- a/Documentation/git-restore.txt +++ b/Documentation/git-restore.txt @@ -40,6 +40,10 @@ OPTIONS + If not specified, the contents are restored from `HEAD` if `--staged` is given, otherwise from the index. ++ +As a special case, you may use `"A...B"` as a shortcut for the +merge base of `A` and `B` if there is exactly one merge base. You can +leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. -p:: --patch:: -- cgit v0.10.2-6-g49f6 From f82a9e517fc063bb97063e4df2cdde2fd1f7ffbe Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Wed, 7 Oct 2020 00:56:17 -0700 Subject: add-patch: add NEEDSWORK about comparing commits The two versions of add-patch has special-casing for the literal revision "HEAD". However, we want to handle other ways of saying "HEAD" in the same way.[0] Add a NEEDSWORK to the add-patch code that does this so that it can be addressed later. [0]: https://lore.kernel.org/git/xmqqsgat7ttf.fsf@gitster.c.googlers.com/ Signed-off-by: Denton Liu Signed-off-by: Junio C Hamano diff --git a/add-patch.c b/add-patch.c index f899389..64ab7fa 100644 --- a/add-patch.c +++ b/add-patch.c @@ -1646,6 +1646,14 @@ int run_add_p(struct repository *r, enum add_p_mode mode, if (mode == ADD_P_STASH) s.mode = &patch_mode_stash; else if (mode == ADD_P_RESET) { + /* + * NEEDSWORK: Instead of comparing to the literal "HEAD", + * compare the commit objects instead so that other ways of + * saying the same thing (such as "@") are also handled + * appropriately. + * + * This applies to the cases below too. + */ if (!revision || !strcmp(revision, "HEAD")) s.mode = &patch_mode_reset_head; else diff --git a/git-add--interactive.perl b/git-add--interactive.perl index f36c007..d5ef7fc 100755 --- a/git-add--interactive.perl +++ b/git-add--interactive.perl @@ -1807,6 +1807,13 @@ sub process_args { $arg = shift @ARGV or die __("missing --"); if ($arg ne '--') { $patch_mode_revision = $arg; + + # NEEDSWORK: Instead of comparing to the literal "HEAD", + # compare the commit objects instead so that other ways of + # saying the same thing (such as "@") are also handled + # appropriately. + # + # This applies to the cases below too. $patch_mode = ($arg eq 'HEAD' ? 'reset_head' : 'reset_nothead'); $arg = shift @ARGV or die __("missing --"); -- cgit v0.10.2-6-g49f6 From 35166b1fb54d6c3ca0d9150fd766598a2d3d17b2 Mon Sep 17 00:00:00 2001 From: Denton Liu Date: Wed, 7 Oct 2020 00:56:18 -0700 Subject: t2016: add a NEEDSWORK about the PERL prerequisite Since the builtin add-p is used when $GIT_TEST_ADD_I_USE_BUILTIN is given, we should replace the PERL prerequisite with an ADD_I prerequisite which first checks if $GIT_TEST_ADD_I_USE_BUILTIN is defined before checking PERL.[0] Mark this in a NEEDSWORK so that it can be addressed at a later time. [0]: https://lore.kernel.org/git/xmqqsgat7ttf.fsf@gitster.c.googlers.com/ Signed-off-by: Denton Liu Signed-off-by: Junio C Hamano diff --git a/t/t2016-checkout-patch.sh b/t/t2016-checkout-patch.sh index 999620e..d91a329 100755 --- a/t/t2016-checkout-patch.sh +++ b/t/t2016-checkout-patch.sh @@ -18,6 +18,10 @@ test_expect_success PERL 'setup' ' # note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar' +# NEEDSWORK: Since the builtin add-p is used when $GIT_TEST_ADD_I_USE_BUILTIN +# is given, we should replace the PERL prerequisite with an ADD_I prerequisite +# which first checks if $GIT_TEST_ADD_I_USE_BUILTIN is defined before checking +# PERL. test_expect_success PERL 'saying "n" does nothing' ' set_and_save_state dir/foo work head && test_write_lines n n | git checkout -p && -- cgit v0.10.2-6-g49f6