Skip to content

Commit 21afd08

Browse files
artagnongitster
authored andcommitted
revert: Don't implicitly stomp pending sequencer operation
Protect the user from forgetting about a pending sequencer operation by immediately erroring out when an existing cherry-pick or revert operation is in progress like: $ git cherry-pick foo ... conflict ... $ git cherry-pick moo error: .git/sequencer already exists hint: A cherry-pick or revert is in progress hint: Use --reset to forget about it fatal: cherry-pick failed A naive version of this would break the following established ways of working: $ git cherry-pick foo ... conflict ... $ git reset --hard # I actually meant "moo" when I said "foo" $ git cherry-pick moo $ git cherry-pick foo ... conflict ... $ git commit # commit the resolution $ git cherry-pick moo # New operation However, the previous patches "reset: Make reset remove the sequencer state" and "revert: Remove sequencer state when no commits are pending" make sure that this does not happen. Signed-off-by: Ramkumar Ramachandra <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2d27daa commit 21afd08

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

builtin/revert.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ struct replay_opts {
6666

6767
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
6868

69+
static void fatal(const char *advice, ...)
70+
{
71+
va_list params;
72+
73+
va_start(params, advice);
74+
vreportf("fatal: ", advice, params);
75+
va_end(params);
76+
}
77+
6978
static const char *action_name(const struct replay_opts *opts)
7079
{
7180
return opts->action == REVERT ? "revert" : "cherry-pick";
@@ -673,13 +682,15 @@ static void walk_revs_populate_todo(struct commit_list **todo_list,
673682
next = commit_list_append(commit, next);
674683
}
675684

676-
static void create_seq_dir(void)
685+
static int create_seq_dir(void)
677686
{
678687
const char *seq_dir = git_path(SEQ_DIR);
679688

680-
if (!(file_exists(seq_dir) && is_directory(seq_dir))
681-
&& mkdir(seq_dir, 0777) < 0)
689+
if (file_exists(seq_dir))
690+
return error(_("%s already exists."), seq_dir);
691+
else if (mkdir(seq_dir, 0777) < 0)
682692
die_errno(_("Could not create sequencer directory '%s'."), seq_dir);
693+
return 0;
683694
}
684695

685696
static void save_head(const char *head)
@@ -801,9 +812,18 @@ static int pick_revisions(struct replay_opts *opts)
801812
remove_sequencer_state(1);
802813
return 0;
803814
} else {
804-
/* Start a new cherry-pick/ revert sequence */
815+
/*
816+
* Start a new cherry-pick/ revert sequence; but
817+
* first, make sure that an existing one isn't in
818+
* progress
819+
*/
820+
805821
walk_revs_populate_todo(&todo_list, opts);
806-
create_seq_dir();
822+
if (create_seq_dir() < 0) {
823+
fatal(_("A cherry-pick or revert is in progress."));
824+
advise(_("Use --reset to forget about it"));
825+
exit(128);
826+
}
807827
if (get_sha1("HEAD", sha1)) {
808828
if (opts->action == REVERT)
809829
die(_("Can't revert as initial commit"));

t/t3510-cherry-pick-sequence.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,13 @@ test_expect_success 'cherry-pick cleans up sequencer state when one commit is le
106106
test_cmp expect actual
107107
'
108108

109+
test_expect_success 'cherry-pick does not implicitly stomp an existing operation' '
110+
pristine_detach initial &&
111+
test_must_fail git cherry-pick base..anotherpick &&
112+
test-chmtime -v +0 .git/sequencer >expect &&
113+
test_must_fail git cherry-pick unrelatedpick &&
114+
test-chmtime -v +0 .git/sequencer >actual &&
115+
test_cmp expect actual
116+
'
117+
109118
test_done

0 commit comments

Comments
 (0)