3bf1da69402dbfffe93ae6caa0c0de5d705a73f5
1#include "builtin.h"
2#include "cache.h"
3#include "config.h"
4#include "parse-options.h"
5#include "sequencer.h"
6#include "rebase-interactive.h"
7#include "argv-array.h"
8#include "refs.h"
9#include "rerere.h"
10#include "run-command.h"
11
12static GIT_PATH_FUNC(path_state_dir, "rebase-merge/")
13static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
14static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
15
16static int add_exec_commands(struct string_list *commands)
17{
18 const char *todo_file = rebase_path_todo();
19 struct todo_list todo_list = TODO_LIST_INIT;
20 int res;
21
22 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
23 return error_errno(_("could not read '%s'."), todo_file);
24
25 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
26 &todo_list)) {
27 todo_list_release(&todo_list);
28 return error(_("unusable todo list: '%s'"), todo_file);
29 }
30
31 todo_list_add_exec_commands(&todo_list, commands);
32 res = todo_list_write_to_file(the_repository, &todo_list,
33 todo_file, NULL, NULL, -1, 0);
34 todo_list_release(&todo_list);
35
36 if (res)
37 return error_errno(_("could not write '%s'."), todo_file);
38 return 0;
39}
40
41static int get_revision_ranges(const char *upstream, const char *onto,
42 const char **head_hash,
43 char **revisions, char **shortrevisions)
44{
45 const char *base_rev = upstream ? upstream : onto, *shorthead;
46 struct object_id orig_head;
47
48 if (get_oid("HEAD", &orig_head))
49 return error(_("no HEAD?"));
50
51 *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
52 *revisions = xstrfmt("%s...%s", base_rev, *head_hash);
53
54 shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV);
55
56 if (upstream) {
57 const char *shortrev;
58 struct object_id rev_oid;
59
60 get_oid(base_rev, &rev_oid);
61 shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV);
62
63 *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
64 } else
65 *shortrevisions = xstrdup(shorthead);
66
67 return 0;
68}
69
70static int init_basic_state(struct replay_opts *opts, const char *head_name,
71 const char *onto, const char *orig_head)
72{
73 FILE *interactive;
74
75 if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir()))
76 return error_errno(_("could not create temporary %s"), path_state_dir());
77
78 delete_reflog("REBASE_HEAD");
79
80 interactive = fopen(path_interactive(), "w");
81 if (!interactive)
82 return error_errno(_("could not mark as interactive"));
83 fclose(interactive);
84
85 return write_basic_state(opts, head_name, onto, orig_head);
86}
87
88static int do_interactive_rebase(struct replay_opts *opts, unsigned flags,
89 const char *switch_to, const char *upstream,
90 const char *onto, const char *onto_name,
91 const char *squash_onto, const char *head_name,
92 const char *restrict_revision, char *raw_strategies,
93 struct string_list *commands, unsigned autosquash)
94{
95 int ret;
96 const char *head_hash = NULL;
97 char *revisions = NULL, *shortrevisions = NULL;
98 struct argv_array make_script_args = ARGV_ARRAY_INIT;
99 struct todo_list todo_list = TODO_LIST_INIT;
100
101 if (prepare_branch_to_be_rebased(opts, switch_to))
102 return -1;
103
104 if (get_revision_ranges(upstream, onto, &head_hash,
105 &revisions, &shortrevisions))
106 return -1;
107
108 if (raw_strategies)
109 parse_strategy_opts(opts, raw_strategies);
110
111 if (init_basic_state(opts, head_name, onto, head_hash)) {
112 free(revisions);
113 free(shortrevisions);
114
115 return -1;
116 }
117
118 if (!upstream && squash_onto)
119 write_file(path_squash_onto(), "%s\n", squash_onto);
120
121 argv_array_pushl(&make_script_args, "", revisions, NULL);
122 if (restrict_revision)
123 argv_array_push(&make_script_args, restrict_revision);
124
125 ret = sequencer_make_script(the_repository, &todo_list.buf,
126 make_script_args.argc, make_script_args.argv,
127 flags);
128
129 if (ret)
130 error(_("could not generate todo list"));
131 else {
132 discard_cache();
133 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
134 &todo_list))
135 BUG("unusable todo list");
136
137 ret = complete_action(the_repository, opts, flags, shortrevisions, onto_name,
138 onto, head_hash, commands, autosquash, &todo_list);
139 }
140
141 free(revisions);
142 free(shortrevisions);
143 todo_list_release(&todo_list);
144 argv_array_clear(&make_script_args);
145
146 return ret;
147}
148
149static const char * const builtin_rebase_interactive_usage[] = {
150 N_("git rebase--interactive [<options>]"),
151 NULL
152};
153
154int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
155{
156 struct replay_opts opts = REPLAY_OPTS_INIT;
157 unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
158 int abbreviate_commands = 0, rebase_cousins = -1, ret = 0;
159 const char *onto = NULL, *onto_name = NULL, *restrict_revision = NULL,
160 *squash_onto = NULL, *upstream = NULL, *head_name = NULL,
161 *switch_to = NULL, *cmd = NULL;
162 struct string_list commands = STRING_LIST_INIT_DUP;
163 char *raw_strategies = NULL;
164 enum {
165 NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH,
166 SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC
167 } command = 0;
168 struct option options[] = {
169 OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
170 OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")),
171 OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
172 N_("allow commits with empty messages")),
173 OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
174 OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
175 N_("keep original branch points of cousins")),
176 OPT_BOOL(0, "autosquash", &autosquash,
177 N_("move commits that begin with squash!/fixup!")),
178 OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
179 OPT__VERBOSE(&opts.verbose, N_("be verbose")),
180 OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
181 CONTINUE),
182 OPT_CMDMODE(0, "skip", &command, N_("skip commit"), SKIP),
183 OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list"),
184 EDIT_TODO),
185 OPT_CMDMODE(0, "show-current-patch", &command, N_("show the current patch"),
186 SHOW_CURRENT_PATCH),
187 OPT_CMDMODE(0, "shorten-ids", &command,
188 N_("shorten commit ids in the todo list"), SHORTEN_OIDS),
189 OPT_CMDMODE(0, "expand-ids", &command,
190 N_("expand commit ids in the todo list"), EXPAND_OIDS),
191 OPT_CMDMODE(0, "check-todo-list", &command,
192 N_("check the todo list"), CHECK_TODO_LIST),
193 OPT_CMDMODE(0, "rearrange-squash", &command,
194 N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
195 OPT_CMDMODE(0, "add-exec-commands", &command,
196 N_("insert exec commands in todo list"), ADD_EXEC),
197 OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
198 OPT_STRING(0, "restrict-revision", &restrict_revision,
199 N_("restrict-revision"), N_("restrict revision")),
200 OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
201 N_("squash onto")),
202 OPT_STRING(0, "upstream", &upstream, N_("upstream"),
203 N_("the upstream commit")),
204 OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")),
205 { OPTION_STRING, 'S', "gpg-sign", &opts.gpg_sign, N_("key-id"),
206 N_("GPG-sign commits"),
207 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
208 OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"),
209 N_("rebase strategy")),
210 OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"),
211 N_("strategy options")),
212 OPT_STRING(0, "switch-to", &switch_to, N_("switch-to"),
213 N_("the branch or commit to checkout")),
214 OPT_STRING(0, "onto-name", &onto_name, N_("onto-name"), N_("onto name")),
215 OPT_STRING(0, "cmd", &cmd, N_("cmd"), N_("the command to run")),
216 OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto),
217 OPT_END()
218 };
219
220 sequencer_init_config(&opts);
221 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
222
223 opts.action = REPLAY_INTERACTIVE_REBASE;
224 opts.allow_ff = 1;
225 opts.allow_empty = 1;
226
227 if (argc == 1)
228 usage_with_options(builtin_rebase_interactive_usage, options);
229
230 argc = parse_options(argc, argv, NULL, options,
231 builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0);
232
233 opts.gpg_sign = xstrdup_or_null(opts.gpg_sign);
234
235 flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
236 flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
237 flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
238 flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
239 flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
240
241 if (rebase_cousins >= 0 && !rebase_merges)
242 warning(_("--[no-]rebase-cousins has no effect without "
243 "--rebase-merges"));
244
245 if (cmd && *cmd) {
246 string_list_split(&commands, cmd, '\n', -1);
247
248 /* rebase.c adds a new line to cmd after every command,
249 * so here the last command is always empty */
250 string_list_remove_empty_items(&commands, 0);
251 }
252
253 switch (command) {
254 case NONE:
255 if (!onto && !upstream)
256 die(_("a base commit must be provided with --upstream or --onto"));
257
258 ret = do_interactive_rebase(&opts, flags, switch_to, upstream, onto,
259 onto_name, squash_onto, head_name, restrict_revision,
260 raw_strategies, &commands, autosquash);
261 break;
262 case SKIP: {
263 struct string_list merge_rr = STRING_LIST_INIT_DUP;
264
265 rerere_clear(the_repository, &merge_rr);
266 /* fallthrough */
267 case CONTINUE:
268 ret = sequencer_continue(the_repository, &opts);
269 break;
270 }
271 case EDIT_TODO:
272 ret = edit_todo_list(the_repository, flags);
273 break;
274 case SHOW_CURRENT_PATCH: {
275 struct child_process cmd = CHILD_PROCESS_INIT;
276
277 cmd.git_cmd = 1;
278 argv_array_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL);
279 ret = run_command(&cmd);
280
281 break;
282 }
283 case SHORTEN_OIDS:
284 case EXPAND_OIDS:
285 ret = transform_todo_file(the_repository, flags);
286 break;
287 case CHECK_TODO_LIST:
288 ret = check_todo_list_from_file(the_repository);
289 break;
290 case REARRANGE_SQUASH:
291 ret = rearrange_squash_in_todo_file(the_repository);
292 break;
293 case ADD_EXEC:
294 ret = add_exec_commands(&commands);
295 break;
296 default:
297 BUG("invalid command '%d'", command);
298 }
299
300 string_list_clear(&commands, 0);
301 return !!ret;
302}