Skip to content

Commit bfca631

Browse files
committed
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
2 parents 7596fe9 + 191faaf commit bfca631

File tree

6 files changed

+85
-5
lines changed

6 files changed

+85
-5
lines changed

Documentation/config/revert.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
revert.reference::
2+
Setting this variable to true makes `git revert` to behave
3+
as if the `--reference` option is given.

Documentation/git-revert.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,15 @@ effect to your index in a row.
117117
Allow the rerere mechanism to update the index with the
118118
result of auto-conflict resolution if possible.
119119

120+
--reference::
121+
Instead of starting the body of the log message with "This
122+
reverts <full object name of the commit being reverted>.",
123+
refer to the commit using "--pretty=reference" format
124+
(cf. linkgit:git-log[1]). The `revert.reference`
125+
configuration variable can be used to enable this option by
126+
default.
127+
128+
120129
SEQUENCER SUBCOMMANDS
121130
---------------------
122131
include::sequencer.txt[]

builtin/revert.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
130130
OPT_END(),
131131
};
132132
options = parse_options_concat(options, cp_extra);
133+
} else if (opts->action == REPLAY_REVERT) {
134+
struct option cp_extra[] = {
135+
OPT_BOOL(0, "reference", &opts->commit_use_reference,
136+
N_("use the 'reference' format to refer to commits")),
137+
OPT_END(),
138+
};
139+
options = parse_options_concat(options, cp_extra);
133140
}
134141

135142
argc = parse_options(argc, argv, NULL, options, usage_str,

sequencer.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
221221
return ret;
222222
}
223223

224+
if (opts->action == REPLAY_REVERT && !strcmp(k, "revert.reference"))
225+
opts->commit_use_reference = git_config_bool(k, v);
226+
224227
status = git_gpg_config(k, v, NULL);
225228
if (status)
226229
return status;
@@ -2059,6 +2062,20 @@ static int should_edit(struct replay_opts *opts) {
20592062
return opts->edit;
20602063
}
20612064

2065+
static void refer_to_commit(struct replay_opts *opts,
2066+
struct strbuf *msgbuf, struct commit *commit)
2067+
{
2068+
if (opts->commit_use_reference) {
2069+
struct pretty_print_context ctx = {
2070+
.abbrev = DEFAULT_ABBREV,
2071+
.date_mode.type = DATE_SHORT,
2072+
};
2073+
format_commit_message(commit, "%h (%s, %ad)", msgbuf, &ctx);
2074+
} else {
2075+
strbuf_addstr(msgbuf, oid_to_hex(&commit->object.oid));
2076+
}
2077+
}
2078+
20622079
static int do_pick_commit(struct repository *r,
20632080
struct todo_item *item,
20642081
struct replay_opts *opts,
@@ -2167,14 +2184,20 @@ static int do_pick_commit(struct repository *r,
21672184
base_label = msg.label;
21682185
next = parent;
21692186
next_label = msg.parent_label;
2170-
strbuf_addstr(&msgbuf, "Revert \"");
2171-
strbuf_addstr(&msgbuf, msg.subject);
2172-
strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit ");
2173-
strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid));
2187+
if (opts->commit_use_reference) {
2188+
strbuf_addstr(&msgbuf,
2189+
"# *** SAY WHY WE ARE REVERTING ON THE TITLE LINE ***");
2190+
} else {
2191+
strbuf_addstr(&msgbuf, "Revert \"");
2192+
strbuf_addstr(&msgbuf, msg.subject);
2193+
strbuf_addstr(&msgbuf, "\"");
2194+
}
2195+
strbuf_addstr(&msgbuf, "\n\nThis reverts commit ");
2196+
refer_to_commit(opts, &msgbuf, commit);
21742197

21752198
if (commit->parents && commit->parents->next) {
21762199
strbuf_addstr(&msgbuf, ", reversing\nchanges made to ");
2177-
strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid));
2200+
refer_to_commit(opts, &msgbuf, parent);
21782201
}
21792202
strbuf_addstr(&msgbuf, ".\n");
21802203
} else {

sequencer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct replay_opts {
4949
int reschedule_failed_exec;
5050
int committer_date_is_author_date;
5151
int ignore_date;
52+
int commit_use_reference;
5253

5354
int mainline;
5455

t/t3501-revert-cherry-pick.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ test_expect_success 'cherry-pick works with dirty renamed file' '
158158
'
159159

160160
test_expect_success 'advice from failed revert' '
161+
test_when_finished "git reset --hard" &&
161162
test_commit --no-tag "add dream" dream dream &&
162163
dream_oid=$(git rev-parse --short HEAD) &&
163164
cat <<-EOF >expected &&
@@ -173,4 +174,40 @@ test_expect_success 'advice from failed revert' '
173174
test_must_fail git revert HEAD^ 2>actual &&
174175
test_cmp expected actual
175176
'
177+
178+
test_expect_success 'identification of reverted commit (default)' '
179+
test_commit to-ident &&
180+
test_when_finished "git reset --hard to-ident" &&
181+
git checkout --detach to-ident &&
182+
git revert --no-edit HEAD &&
183+
git cat-file commit HEAD >actual.raw &&
184+
grep "^This reverts " actual.raw >actual &&
185+
echo "This reverts commit $(git rev-parse HEAD^)." >expect &&
186+
test_cmp expect actual
187+
'
188+
189+
test_expect_success 'identification of reverted commit (--reference)' '
190+
git checkout --detach to-ident &&
191+
git revert --reference --no-edit HEAD &&
192+
git cat-file commit HEAD >actual.raw &&
193+
grep "^This reverts " actual.raw >actual &&
194+
echo "This reverts commit $(git show -s --pretty=reference HEAD^)." >expect &&
195+
test_cmp expect actual
196+
'
197+
198+
test_expect_success 'identification of reverted commit (revert.reference)' '
199+
git checkout --detach to-ident &&
200+
git -c revert.reference=true revert --no-edit HEAD &&
201+
git cat-file commit HEAD >actual.raw &&
202+
grep "^This reverts " actual.raw >actual &&
203+
echo "This reverts commit $(git show -s --pretty=reference HEAD^)." >expect &&
204+
test_cmp expect actual
205+
'
206+
207+
test_expect_success 'cherry-pick is unaware of --reference (for now)' '
208+
test_when_finished "git reset --hard" &&
209+
test_must_fail git cherry-pick --reference HEAD 2>actual &&
210+
grep "^usage: git cherry-pick" actual
211+
'
212+
176213
test_done

0 commit comments

Comments
 (0)