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