Skip to content

Commit 1a2b985

Browse files
Denton-Lgitster
authored andcommitted
cherry-pick/revert: add scissors line on merge conflict
Fix a bug where the scissors line is placed after the Conflicts: section, in the case where a merge conflict occurs and commit.cleanup = scissors. Helped-by: Phillip Wood <[email protected]> Signed-off-by: Denton Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dc42e9a commit 1a2b985

File tree

7 files changed

+122
-14
lines changed

7 files changed

+122
-14
lines changed

Documentation/git-cherry-pick.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ OPTIONS
5757
With this option, 'git cherry-pick' will let you edit the commit
5858
message prior to committing.
5959

60+
--cleanup=<mode>::
61+
This option determines how the commit message will be cleaned up before
62+
being passed on to the commit machinery. See linkgit:git-commit[1] for more
63+
details. In particular, if the '<mode>' is given a value of `scissors`,
64+
scissors will be appended to `MERGE_MSG` before being passed on in the case
65+
of a conflict.
66+
6067
-x::
6168
When recording the commit, append a line that says
6269
"(cherry picked from commit ...)" to the original commit

Documentation/git-revert.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ more details.
6666
With this option, 'git revert' will not start the commit
6767
message editor.
6868

69+
--cleanup=<mode>::
70+
This option determines how the commit message will be cleaned up before
71+
being passed on to the commit machinery. See linkgit:git-commit[1] for more
72+
details. In particular, if the '<mode>' is given a value of `scissors`,
73+
scissors will be appended to `MERGE_MSG` before being passed on in the case
74+
of a conflict.
75+
6976
-n::
7077
--no-commit::
7178
Usually the command automatically creates some commits with

builtin/merge.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -927,14 +927,8 @@ static int suggest_conflicts(void)
927927
* Thus, we will get the cleanup mode which is returned when we _are_
928928
* using an editor.
929929
*/
930-
if (get_cleanup_mode(cleanup_arg, 1) == COMMIT_MSG_CLEANUP_SCISSORS) {
931-
fputc('\n', fp);
932-
wt_status_add_cut_line(fp);
933-
/* comments out the newline from append_conflicts_hint */
934-
fputc(comment_line_char, fp);
935-
}
936-
937-
append_conflicts_hint(&the_index, &msgbuf);
930+
append_conflicts_hint(&the_index, &msgbuf,
931+
get_cleanup_mode(cleanup_arg, 1));
938932
fputs(msgbuf.buf, fp);
939933
strbuf_release(&msgbuf);
940934
fclose(fp);

builtin/revert.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
9696
{
9797
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
9898
const char *me = action_name(opts);
99+
const char *cleanup_arg = NULL;
99100
int cmd = 0;
100101
struct option base_options[] = {
101102
OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
102103
OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
103104
OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
105+
OPT_CLEANUP(&cleanup_arg),
104106
OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
105107
OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
106108
OPT_NOOP_NOARG('r', NULL),
@@ -137,6 +139,11 @@ static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
137139
if (opts->keep_redundant_commits)
138140
opts->allow_empty = 1;
139141

142+
if (cleanup_arg) {
143+
opts->default_msg_cleanup = get_cleanup_mode(cleanup_arg, 1);
144+
opts->explicit_cleanup = 1;
145+
}
146+
140147
/* Check for incompatible command line arguments */
141148
if (cmd) {
142149
char *this_operation;

sequencer.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ static int git_sequencer_config(const char *k, const char *v, void *cb)
182182
opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL;
183183
opts->explicit_cleanup = 1;
184184
} else if (!strcmp(s, "scissors")) {
185-
opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE;
185+
opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SCISSORS;
186186
opts->explicit_cleanup = 1;
187187
} else {
188188
warning(_("invalid commit message cleanup mode '%s'"),
@@ -554,10 +554,16 @@ static const char *describe_cleanup_mode(int cleanup_mode)
554554
}
555555

556556
void append_conflicts_hint(struct index_state *istate,
557-
struct strbuf *msgbuf)
557+
struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode)
558558
{
559559
int i;
560560

561+
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
562+
strbuf_addch(msgbuf, '\n');
563+
wt_status_append_cut_line(msgbuf);
564+
strbuf_addch(msgbuf, comment_line_char);
565+
}
566+
561567
strbuf_addch(msgbuf, '\n');
562568
strbuf_commented_addf(msgbuf, "Conflicts:\n");
563569
for (i = 0; i < istate->cache_nr;) {
@@ -625,7 +631,8 @@ static int do_recursive_merge(struct repository *r,
625631
_(action_name(opts)));
626632

627633
if (!clean)
628-
append_conflicts_hint(r->index, msgbuf);
634+
append_conflicts_hint(r->index, msgbuf,
635+
opts->default_msg_cleanup);
629636

630637
return !clean;
631638
}
@@ -944,7 +951,6 @@ static int run_git_commit(struct repository *r,
944951
unsigned int flags)
945952
{
946953
struct child_process cmd = CHILD_PROCESS_INIT;
947-
const char *value;
948954

949955
if ((flags & CREATE_ROOT_COMMIT) && !(flags & AMEND_MSG)) {
950956
struct strbuf msg = STRBUF_INIT, script = STRBUF_INIT;
@@ -1014,7 +1020,7 @@ static int run_git_commit(struct repository *r,
10141020
argv_array_push(&cmd.args, "-e");
10151021
else if (!(flags & CLEANUP_MSG) &&
10161022
!opts->signoff && !opts->record_origin &&
1017-
git_config_get_value("commit.cleanup", &value))
1023+
!opts->explicit_cleanup)
10181024
argv_array_push(&cmd.args, "--cleanup=verbatim");
10191025

10201026
if ((flags & ALLOW_EMPTY))

sequencer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ int rearrange_squash(struct repository *r);
116116
*/
117117
void append_signoff(struct strbuf *msgbuf, size_t ignore_footer, unsigned flag);
118118

119-
void append_conflicts_hint(struct index_state *istate, struct strbuf *msgbuf);
119+
void append_conflicts_hint(struct index_state *istate,
120+
struct strbuf *msgbuf, enum commit_msg_cleanup_mode cleanup_mode);
120121
enum commit_msg_cleanup_mode get_cleanup_mode(const char *cleanup_arg,
121122
int use_editor);
122123

t/t3507-cherry-pick-conflict.sh

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,46 @@ test_expect_success 'failed cherry-pick registers participants in index' '
189189
test_cmp expected actual
190190
'
191191

192+
test_expect_success \
193+
'cherry-pick conflict, ensure commit.cleanup = scissors places scissors line properly' '
194+
pristine_detach initial &&
195+
git config commit.cleanup scissors &&
196+
cat <<-EOF >expected &&
197+
picked
198+
199+
# ------------------------ >8 ------------------------
200+
# Do not modify or remove the line above.
201+
# Everything below it will be ignored.
202+
#
203+
# Conflicts:
204+
# foo
205+
EOF
206+
207+
test_must_fail git cherry-pick picked &&
208+
209+
test_i18ncmp expected .git/MERGE_MSG
210+
'
211+
212+
test_expect_success \
213+
'cherry-pick conflict, ensure cleanup=scissors places scissors line properly' '
214+
pristine_detach initial &&
215+
git config --unset commit.cleanup &&
216+
cat <<-EOF >expected &&
217+
picked
218+
219+
# ------------------------ >8 ------------------------
220+
# Do not modify or remove the line above.
221+
# Everything below it will be ignored.
222+
#
223+
# Conflicts:
224+
# foo
225+
EOF
226+
227+
test_must_fail git cherry-pick --cleanup=scissors picked &&
228+
229+
test_i18ncmp expected .git/MERGE_MSG
230+
'
231+
192232
test_expect_success 'failed cherry-pick describes conflict in work tree' '
193233
pristine_detach initial &&
194234
cat <<-EOF >expected &&
@@ -335,6 +375,52 @@ test_expect_success 'revert conflict, diff3 -m style' '
335375
test_cmp expected actual
336376
'
337377

378+
test_expect_success \
379+
'revert conflict, ensure commit.cleanup = scissors places scissors line properly' '
380+
pristine_detach initial &&
381+
git config commit.cleanup scissors &&
382+
cat >expected <<-EOF &&
383+
Revert "picked"
384+
385+
This reverts commit OBJID.
386+
387+
# ------------------------ >8 ------------------------
388+
# Do not modify or remove the line above.
389+
# Everything below it will be ignored.
390+
#
391+
# Conflicts:
392+
# foo
393+
EOF
394+
395+
test_must_fail git revert picked &&
396+
397+
sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
398+
test_i18ncmp expected actual
399+
'
400+
401+
test_expect_success \
402+
'revert conflict, ensure cleanup=scissors places scissors line properly' '
403+
pristine_detach initial &&
404+
git config --unset commit.cleanup &&
405+
cat >expected <<-EOF &&
406+
Revert "picked"
407+
408+
This reverts commit OBJID.
409+
410+
# ------------------------ >8 ------------------------
411+
# Do not modify or remove the line above.
412+
# Everything below it will be ignored.
413+
#
414+
# Conflicts:
415+
# foo
416+
EOF
417+
418+
test_must_fail git revert --cleanup=scissors picked &&
419+
420+
sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
421+
test_i18ncmp expected actual
422+
'
423+
338424
test_expect_success 'failed cherry-pick does not forget -s' '
339425
pristine_detach initial &&
340426
test_must_fail git cherry-pick -s picked &&

0 commit comments

Comments
 (0)