Merge branch 'jc/revert-show-parent-info'

"git revert" learns "--reference" option to use more human-readable
reference to the commit it reverts in the message template it
prepares for the user.

* jc/revert-show-parent-info:
  revert: --reference should apply only to 'revert', not 'cherry-pick'
  revert: optionally refer to commit in the "reference" format
This commit is contained in:
Junio C Hamano 2022-06-15 15:09:27 -07:00
commit bfca631634
6 changed files with 85 additions and 5 deletions

View File

@ -0,0 +1,3 @@
revert.reference::
Setting this variable to true makes `git revert` to behave
as if the `--reference` option is given.

View File

@ -117,6 +117,15 @@ effect to your index in a row.
Allow the rerere mechanism to update the index with the
result of auto-conflict resolution if possible.
--reference::
Instead of starting the body of the log message with "This
reverts <full object name of the commit being reverted>.",
refer to the commit using "--pretty=reference" format
(cf. linkgit:git-log[1]). The `revert.reference`
configuration variable can be used to enable this option by
default.
SEQUENCER SUBCOMMANDS
---------------------
include::sequencer.txt[]

View File

@ -130,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,

View File

@ -221,6 +221,9 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
return ret;
}
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);
if (status)
return status;
@ -2059,6 +2062,20 @@ static int should_edit(struct replay_opts *opts) {
return opts->edit;
}
static void refer_to_commit(struct replay_opts *opts,
struct strbuf *msgbuf, struct commit *commit)
{
if (opts->commit_use_reference) {
struct pretty_print_context ctx = {
.abbrev = DEFAULT_ABBREV,
.date_mode.type = DATE_SHORT,
};
format_commit_message(commit, "%h (%s, %ad)", msgbuf, &ctx);
} else {
strbuf_addstr(msgbuf, oid_to_hex(&commit->object.oid));
}
}
static int do_pick_commit(struct repository *r,
struct todo_item *item,
struct replay_opts *opts,
@ -2167,14 +2184,20 @@ static int do_pick_commit(struct repository *r,
base_label = msg.label;
next = parent;
next_label = msg.parent_label;
strbuf_addstr(&msgbuf, "Revert \"");
strbuf_addstr(&msgbuf, msg.subject);
strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit ");
strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid));
if (opts->commit_use_reference) {
strbuf_addstr(&msgbuf,
"# *** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***");
} else {
strbuf_addstr(&msgbuf, "Revert \"");
strbuf_addstr(&msgbuf, msg.subject);
strbuf_addstr(&msgbuf, "\"");
}
strbuf_addstr(&msgbuf, "\n\nThis reverts commit ");
refer_to_commit(opts, &msgbuf, commit);
if (commit->parents && commit->parents->next) {
strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid));
refer_to_commit(opts, &msgbuf, parent);
}
strbuf_addstr(&msgbuf, ".\n");
} else {

View File

@ -49,6 +49,7 @@ struct replay_opts {
int reschedule_failed_exec;
int committer_date_is_author_date;
int ignore_date;
int commit_use_reference;
int mainline;

View File

@ -158,6 +158,7 @@ test_expect_success 'cherry-pick works with dirty renamed file' '
'
test_expect_success 'advice from failed revert' '
test_when_finished "git reset --hard" &&
test_commit --no-tag "add dream" dream dream &&
dream_oid=$(git rev-parse --short HEAD) &&
cat <<-EOF >expected &&
@ -173,4 +174,40 @@ test_expect_success 'advice from failed revert' '
test_must_fail git revert HEAD^ 2>actual &&
test_cmp expected actual
'
test_expect_success 'identification of reverted commit (default)' '
test_commit to-ident &&
test_when_finished "git reset --hard to-ident" &&
git checkout --detach to-ident &&
git revert --no-edit HEAD &&
git cat-file commit HEAD >actual.raw &&
grep "^This reverts " actual.raw >actual &&
echo "This reverts commit $(git rev-parse HEAD^)." >expect &&
test_cmp expect actual
'
test_expect_success 'identification of reverted commit (--reference)' '
git checkout --detach to-ident &&
git revert --reference --no-edit HEAD &&
git cat-file commit HEAD >actual.raw &&
grep "^This reverts " actual.raw >actual &&
echo "This reverts commit $(git show -s --pretty=reference HEAD^)." >expect &&
test_cmp expect actual
'
test_expect_success 'identification of reverted commit (revert.reference)' '
git checkout --detach to-ident &&
git -c revert.reference=true revert --no-edit HEAD &&
git cat-file commit HEAD >actual.raw &&
grep "^This reverts " actual.raw >actual &&
echo "This reverts commit $(git show -s --pretty=reference HEAD^)." >expect &&
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