]> git.ipfire.org Git - thirdparty/git.git/commitdiff
revert: --reference should apply only to 'revert', not 'cherry-pick'
authorJunio C Hamano <gitster@pobox.com>
Tue, 31 May 2022 16:22:20 +0000 (09:22 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 31 May 2022 16:40:51 +0000 (09:40 -0700)
As 'revert' and 'cherry-pick' share a lot of code, it is easy to
modify the behaviour of one command and inadvertently affect the
other.  An earlier change to teach the '--reference' option and the
'revert.reference' configuration variable to the former was not
careful enough and 'cherry-pick --reference' wasn't rejected as an
error.

It is possible to think 'cherry-pick -x' might benefit from the
'--reference' option, but it is fundamentally different from
'revert' in at least two ways to make it questionable:

 - 'revert' names a commit that is ancestor of the resulting commit,
   so an abbreviated object name with human readable title is
   sufficient to identify the named commit uniquely without using
   the full object name.  On the other hand, 'cherry-pick'
   usually [*] picks a commit that is not an ancestor.  It might be
   even picking a private commit that never becomes part of the
   public history.

 - The whole commit message of 'cherry-pick' is a copy of the
   original commit, and there is nothing gained to repeat only the
   title part on 'cherry-picked from' message.

[*] well, you could revert and then you can pick the original that
    was reverted to get back to where you were, but then you can
    revert the revert to do the same thing.

Helped-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/revert.c
sequencer.c
t/t3501-revert-cherry-pick.sh

index ada51e46b9f2616ea4ac46a7c82bccca915462cb..f84c253f4c6f65ba273263d92658c74378b0de74 100644 (file)
@@ -116,8 +116,6 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
                        N_("option for merge strategy"), option_parse_x),
                { OPTION_STRING, 'S', "gpg-sign", &opts->gpg_sign, N_("key-id"),
                  N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
-               OPT_BOOL(0, "reference", &opts->commit_use_reference,
-                        N_("use the 'reference' format to refer to commits")),
                OPT_END()
        };
        struct option *options = base_options;
@@ -132,6 +130,13 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
                        OPT_END(),
                };
                options = parse_options_concat(options, cp_extra);
+       } else if (opts->action == REPLAY_REVERT) {
+               struct option cp_extra[] = {
+                       OPT_BOOL(0, "reference", &opts->commit_use_reference,
+                                N_("use the 'reference' format to refer to commits")),
+                       OPT_END(),
+               };
+               options = parse_options_concat(options, cp_extra);
        }
 
        argc = parse_options(argc, argv, NULL, options, usage_str,
index 96fec6ef6d908d4966bccd3482a0d1eb76906da8..4b66a1f79c75f5314053aac18a5e6aa1895faad1 100644 (file)
@@ -221,7 +221,7 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
                return ret;
        }
 
-       if (!strcmp(k, "revert.reference"))
+       if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference"))
                opts->commit_use_reference = git_config_bool(k, v);
 
        status = git_gpg_config(k, v, NULL);
index a386ae9e885db4c977c7b689b9c6d42be7764438..fb4466599bc834c6af3f7ed083f57322b23287e1 100755 (executable)
@@ -205,4 +205,10 @@ test_expect_success 'identification of reverted commit (revert.reference)' '
        test_cmp expect actual
 '
 
+test_expect_success 'cherry-pick is unaware of --reference (for now)' '
+       test_when_finished "git reset --hard" &&
+       test_must_fail git cherry-pick --reference HEAD 2>actual &&
+       grep "^usage: git cherry-pick" actual
+'
+
 test_done