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 <artagnon@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Ramkumar Ramachandra committed
Aug 4, 2011 at 16:09 UTC
26ae337be11e440420d8ec7ce415425daaabe573
8 files changed
+111
-20
Documentation/git-cherry-pick.txt
+5
@@ -8,6 +8,7 @@ git-cherry-pick - Apply the changes introduced by some existing commits
8
SYNOPSIS
9
--------
10
'git cherry-pick' [--edit] [-n] [-m parent-number] [-s] [-x] [--ff] <commit>...
11
+'git cherry-pick' --reset
12
13
DESCRIPTION
14
-----------
@@ -109,6 +110,10 @@ effect to your index in a row.
110
Pass the merge strategy-specific option through to the
111
merge strategy. See linkgit:git-merge[1] for details.
112
113
+SEQUENCER SUBCOMMANDS
114
+---------------------
115
+include::sequencer.txt[]
116
+
117
EXAMPLES
118
--------
119
git cherry-pick master::
Documentation/git-revert.txt
+5
@@ -8,6 +8,7 @@ git-revert - Revert some existing commits
8
SYNOPSIS
9
--------
10
'git revert' [--edit | --no-edit] [-n] [-m parent-number] [-s] <commit>...
11
+'git revert' --reset
12
13
DESCRIPTION
14
-----------
@@ -90,6 +91,10 @@ effect to your index in a row.
91
Pass the merge strategy-specific option through to the
92
merge strategy. See linkgit:git-merge[1] for details.
93
94
+SEQUENCER SUBCOMMANDS
95
+---------------------
96
+include::sequencer.txt[]
97
+
98
EXAMPLES
99
--------
100
git revert HEAD~3::
Documentation/sequencer.txt
new
+4
@@ -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
+2
@@ -551,6 +551,7 @@ LIB_H += rerere.h
551
LIB_H += resolve-undo.h
552
LIB_H += revision.h
553
LIB_H += run-command.h
554
+LIB_H += sequencer.h
555
LIB_H += sha1-array.h
556
LIB_H += sha1-lookup.h
557
LIB_H += sideband.h
@@ -654,6 +655,7 @@ LIB_OBJS += revision.o
655
LIB_OBJS += run-command.o
656
LIB_OBJS += server-info.o
657
LIB_OBJS += setup.o
658
+LIB_OBJS += sequencer.o
659
LIB_OBJS += sha1-array.o
660
LIB_OBJS += sha1-lookup.o
661
LIB_OBJS += sha1_file.o
builtin/revert.c
+43
-19
@@ -14,6 +14,7 @@
14
#include "merge-recursive.h"
15
#include "refs.h"
16
#include "dir.h"
17
+#include "sequencer.h"
18
19
/*
20
* This implements the builtins revert and cherry-pick.
@@ -28,18 +29,22 @@
29
30
static const char * const revert_usage[] = {
31
"git revert [options] <commit-ish>",
32
+ "git revert <subcommand>",
33
NULL
34
};
35
36
static const char * const cherry_pick_usage[] = {
37
"git cherry-pick [options] <commit-ish>",
38
+ "git cherry-pick <subcommand>",
39
NULL
40
};
41
42
enum replay_action { REVERT, CHERRY_PICK };
43
+enum replay_subcommand { REPLAY_NONE, REPLAY_RESET };
44
45
struct replay_opts {
46
enum replay_action action;
47
+ enum replay_subcommand subcommand;
48
49
/* Boolean options */
50
int edit;
@@ -61,11 +66,6 @@ struct replay_opts {
66
67
#define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
68
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
-
69
static const char *action_name(const struct replay_opts *opts)
70
{
71
return opts->action == REVERT ? "revert" : "cherry-pick";
@@ -113,7 +113,9 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
113
const char * const * usage_str = revert_or_cherry_pick_usage(opts);
114
const char *me = action_name(opts);
115
int noop;
116
+ int reset = 0;
117
struct option options[] = {
118
+ OPT_BOOLEAN(0, "reset", &reset, "forget the current operation"),
119
OPT_BOOLEAN('n', "no-commit", &opts->no_commit, "don't automatically commit"),
120
OPT_BOOLEAN('e', "edit", &opts->edit, "edit the commit message"),
121
{ 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)
144
opts->commit_argc = parse_options(argc, argv, NULL, options, usage_str,
145
PARSE_OPT_KEEP_ARGV0 |
146
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)
168
usage_with_options(usage_str, options);
169
170
if (opts->allow_ff)
@@ -729,7 +751,6 @@ static void save_opts(struct replay_opts *opts)
751
752
static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
753
{
732
- struct strbuf buf = STRBUF_INIT;
754
struct commit_list *cur;
755
int res;
756
@@ -750,9 +771,7 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
771
* Sequence of picks finished successfully; cleanup by
772
* removing the .git/sequencer directory
773
*/
753
- strbuf_addf(&buf, "%s", git_path(SEQ_DIR));
754
- remove_dir_recursively(&buf, 0);
755
- strbuf_release(&buf);
774
+ remove_sequencer_state(1);
775
return 0;
776
}
777
@@ -768,16 +787,21 @@ static int pick_revisions(struct replay_opts *opts)
787
* cherry-pick should be handled differently from an existing
788
* one that is being continued
789
*/
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);
804
}
778
- save_head(sha1_to_hex(sha1));
779
- save_opts(opts);
780
-
805
return pick_commits(todo_list, opts);
806
}
807
sequencer.c
new
+19
@@ -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
new
+20
@@ -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
+13
-1
@@ -13,7 +13,7 @@ test_description='Test cherry-pick continuation features
13
. ./test-lib.sh
14
15
pristine_detach () {
16
- rm -rf .git/sequencer &&
16
+ git cherry-pick --reset &&
17
git checkout -f "$1^0" &&
18
git read-tree -u --reset HEAD &&
19
git clean -d -f -f -q -x
@@ -70,4 +70,16 @@ test_expect_success 'cherry-pick cleans up sequencer state upon success' '
70
test_path_is_missing .git/sequencer
71
'
72
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
+
85
test_done