Skip to content

Commit 26ae337

Browse files
artagnongitster
authored andcommitted
revert: Introduce --reset to remove sequencer state
To explicitly remove the sequencer state for a fresh cherry-pick or revert invocation, introduce a new subcommand called "--reset" to remove the sequencer state. Take the opportunity to publicly expose the sequencer paths, and a generic function called "remove_sequencer_state" that various git programs can use to remove the sequencer state in a uniform manner; "git reset" uses it later in this series. Introducing this public API is also in line with our long-term goal of eventually factoring out functions from revert.c into a generic commit sequencer. Signed-off-by: Ramkumar Ramachandra <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 21b1477 commit 26ae337

File tree

8 files changed

+111
-20
lines changed

8 files changed

+111
-20
lines changed

Documentation/git-cherry-pick.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ git-cherry-pick - Apply the changes introduced by some existing commits
88
SYNOPSIS
99
--------
1010
'git cherry-pick' [--edit] [-n] [-m parent-number] [-s] [-x] [--ff] <commit>...
11+
'git cherry-pick' --reset
1112

1213
DESCRIPTION
1314
-----------
@@ -109,6 +110,10 @@ effect to your index in a row.
109110
Pass the merge strategy-specific option through to the
110111
merge strategy. See linkgit:git-merge[1] for details.
111112

113+
SEQUENCER SUBCOMMANDS
114+
---------------------
115+
include::sequencer.txt[]
116+
112117
EXAMPLES
113118
--------
114119
git cherry-pick master::

Documentation/git-revert.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ git-revert - Revert some existing commits
88
SYNOPSIS
99
--------
1010
'git revert' [--edit | --no-edit] [-n] [-m parent-number] [-s] <commit>...
11+
'git revert' --reset
1112

1213
DESCRIPTION
1314
-----------
@@ -90,6 +91,10 @@ effect to your index in a row.
9091
Pass the merge strategy-specific option through to the
9192
merge strategy. See linkgit:git-merge[1] for details.
9293

94+
SEQUENCER SUBCOMMANDS
95+
---------------------
96+
include::sequencer.txt[]
97+
9398
EXAMPLES
9499
--------
95100
git revert HEAD~3::

Documentation/sequencer.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--reset::
2+
Forget about the current operation in progress. Can be used
3+
to clear the sequencer state after a failed cherry-pick or
4+
revert.

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,7 @@ LIB_H += rerere.h
551551
LIB_H += resolve-undo.h
552552
LIB_H += revision.h
553553
LIB_H += run-command.h
554+
LIB_H += sequencer.h
554555
LIB_H += sha1-array.h
555556
LIB_H += sha1-lookup.h
556557
LIB_H += sideband.h
@@ -654,6 +655,7 @@ LIB_OBJS += revision.o
654655
LIB_OBJS += run-command.o
655656
LIB_OBJS += server-info.o
656657
LIB_OBJS += setup.o
658+
LIB_OBJS += sequencer.o
657659
LIB_OBJS += sha1-array.o
658660
LIB_OBJS += sha1-lookup.o
659661
LIB_OBJS += sha1_file.o

builtin/revert.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "merge-recursive.h"
1515
#include "refs.h"
1616
#include "dir.h"
17+
#include "sequencer.h"
1718

1819
/*
1920
* This implements the builtins revert and cherry-pick.
@@ -28,18 +29,22 @@
2829

2930
static const char * const revert_usage[] = {
3031
"git revert [options] <commit-ish>",
32+
"git revert <subcommand>",
3133
NULL
3234
};
3335

3436
static const char * const cherry_pick_usage[] = {
3537
"git cherry-pick [options] <commit-ish>",
38+
"git cherry-pick <subcommand>",
3639
NULL
3740
};
3841

3942
enum replay_action { REVERT, CHERRY_PICK };
43+
enum replay_subcommand { REPLAY_NONE, REPLAY_RESET };
4044

4145
struct replay_opts {
4246
enum replay_action action;
47+
enum replay_subcommand subcommand;
4348

4449
/* Boolean options */
4550
int edit;
@@ -61,11 +66,6 @@ struct replay_opts {
6166

6267
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
6368

64-
#define SEQ_DIR "sequencer"
65-
#define SEQ_HEAD_FILE "sequencer/head"
66-
#define SEQ_TODO_FILE "sequencer/todo"
67-
#define SEQ_OPTS_FILE "sequencer/opts"
68-
6969
static const char *action_name(const struct replay_opts *opts)
7070
{
7171
return opts->action == REVERT ? "revert" : "cherry-pick";
@@ -113,7 +113,9 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
113113
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
114114
const char *me = action_name(opts);
115115
int noop;
116+
int reset = 0;
116117
struct option options[] = {
118+
OPT_BOOLEAN(0, "reset", &reset, "forget the current operation"),
117119
OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
118120
OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"),
119121
{ OPTION_BOOLEAN, 'r', NULL, &noop, NULL, "no-op (backward compatibility)",
@@ -142,7 +144,27 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
142144
opts->commit_argc = parse_options(argc, argv, NULL, options, usage_str,
143145
PARSE_OPT_KEEP_ARGV0 |
144146
PARSE_OPT_KEEP_UNKNOWN);
145-
if (opts->commit_argc < 2)
147+
148+
/* Set the subcommand */
149+
if (reset)
150+
opts->subcommand = REPLAY_RESET;
151+
else
152+
opts->subcommand = REPLAY_NONE;
153+
154+
/* Check for incompatible command line arguments */
155+
if (opts->subcommand == REPLAY_RESET) {
156+
verify_opt_compatible(me, "--reset",
157+
"--no-commit", opts->no_commit,
158+
"--signoff", opts->signoff,
159+
"--mainline", opts->mainline,
160+
"--strategy", opts->strategy ? 1 : 0,
161+
"--strategy-option", opts->xopts ? 1 : 0,
162+
"-x", opts->record_origin,
163+
"--ff", opts->allow_ff,
164+
NULL);
165+
}
166+
167+
else if (opts->commit_argc < 2)
146168
usage_with_options(usage_str, options);
147169

148170
if (opts->allow_ff)
@@ -729,7 +751,6 @@ static void save_opts(struct replay_opts *opts)
729751

730752
static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
731753
{
732-
struct strbuf buf = STRBUF_INIT;
733754
struct commit_list *cur;
734755
int res;
735756

@@ -750,9 +771,7 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
750771
* Sequence of picks finished successfully; cleanup by
751772
* removing the .git/sequencer directory
752773
*/
753-
strbuf_addf(&buf, "%s", git_path(SEQ_DIR));
754-
remove_dir_recursively(&buf, 0);
755-
strbuf_release(&buf);
774+
remove_sequencer_state(1);
756775
return 0;
757776
}
758777

@@ -768,16 +787,21 @@ static int pick_revisions(struct replay_opts *opts)
768787
* cherry-pick should be handled differently from an existing
769788
* one that is being continued
770789
*/
771-
walk_revs_populate_todo(&todo_list, opts);
772-
create_seq_dir();
773-
if (get_sha1("HEAD", sha1)) {
774-
if (opts->action == REVERT)
775-
die(_("Can't revert as initial commit"));
776-
die(_("Can't cherry-pick into empty head"));
790+
if (opts->subcommand == REPLAY_RESET) {
791+
remove_sequencer_state(1);
792+
return 0;
793+
} else {
794+
/* Start a new cherry-pick/ revert sequence */
795+
walk_revs_populate_todo(&todo_list, opts);
796+
create_seq_dir();
797+
if (get_sha1("HEAD", sha1)) {
798+
if (opts->action == REVERT)
799+
die(_("Can't revert as initial commit"));
800+
die(_("Can't cherry-pick into empty head"));
801+
}
802+
save_head(sha1_to_hex(sha1));
803+
save_opts(opts);
777804
}
778-
save_head(sha1_to_hex(sha1));
779-
save_opts(opts);
780-
781805
return pick_commits(todo_list, opts);
782806
}
783807

sequencer.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "cache.h"
2+
#include "sequencer.h"
3+
#include "strbuf.h"
4+
#include "dir.h"
5+
6+
void remove_sequencer_state(int aggressive)
7+
{
8+
struct strbuf seq_dir = STRBUF_INIT;
9+
struct strbuf seq_old_dir = STRBUF_INIT;
10+
11+
strbuf_addf(&seq_dir, "%s", git_path(SEQ_DIR));
12+
strbuf_addf(&seq_old_dir, "%s", git_path(SEQ_OLD_DIR));
13+
remove_dir_recursively(&seq_old_dir, 0);
14+
rename(git_path(SEQ_DIR), git_path(SEQ_OLD_DIR));
15+
if (aggressive)
16+
remove_dir_recursively(&seq_old_dir, 0);
17+
strbuf_release(&seq_dir);
18+
strbuf_release(&seq_old_dir);
19+
}

sequencer.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef SEQUENCER_H
2+
#define SEQUENCER_H
3+
4+
#define SEQ_DIR "sequencer"
5+
#define SEQ_OLD_DIR "sequencer-old"
6+
#define SEQ_HEAD_FILE "sequencer/head"
7+
#define SEQ_TODO_FILE "sequencer/todo"
8+
#define SEQ_OPTS_FILE "sequencer/opts"
9+
10+
/*
11+
* Removes SEQ_OLD_DIR and renames SEQ_DIR to SEQ_OLD_DIR, ignoring
12+
* any errors. Intended to be used by 'git reset'.
13+
*
14+
* With the aggressive flag, it additionally removes SEQ_OLD_DIR,
15+
* ignoring any errors. Inteded to be used by the sequencer's
16+
* '--reset' subcommand.
17+
*/
18+
void remove_sequencer_state(int aggressive);
19+
20+
#endif

t/t3510-cherry-pick-sequence.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test_description='Test cherry-pick continuation features
1313
. ./test-lib.sh
1414

1515
pristine_detach () {
16-
rm -rf .git/sequencer &&
16+
git cherry-pick --reset &&
1717
git checkout -f "$1^0" &&
1818
git read-tree -u --reset HEAD &&
1919
git clean -d -f -f -q -x
@@ -70,4 +70,16 @@ test_expect_success 'cherry-pick cleans up sequencer state upon success' '
7070
test_path_is_missing .git/sequencer
7171
'
7272

73+
test_expect_success '--reset does not complain when no cherry-pick is in progress' '
74+
pristine_detach initial &&
75+
git cherry-pick --reset
76+
'
77+
78+
test_expect_success '--reset cleans up sequencer state' '
79+
pristine_detach initial &&
80+
test_must_fail git cherry-pick base..picked &&
81+
git cherry-pick --reset &&
82+
test_path_is_missing .git/sequencer
83+
'
84+
7385
test_done

0 commit comments

Comments
 (0)