Merge branch 'jk/diff-no-index-initialize'
[git/raj.git] / builtin / rebase--interactive.c
blob888390f9114321a489f8210659487ef2d2897fae
1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
2 #include "builtin.h"
3 #include "cache.h"
4 #include "config.h"
5 #include "parse-options.h"
6 #include "sequencer.h"
7 #include "rebase-interactive.h"
8 #include "argv-array.h"
9 #include "refs.h"
10 #include "rerere.h"
11 #include "run-command.h"
13 static GIT_PATH_FUNC(path_state_dir, "rebase-merge/")
14 static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
15 static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
17 static int get_revision_ranges(const char *upstream, const char *onto,
18 const char **head_hash,
19 char **revisions, char **shortrevisions)
21 const char *base_rev = upstream ? upstream : onto, *shorthead;
22 struct object_id orig_head;
24 if (get_oid("HEAD", &orig_head))
25 return error(_("no HEAD?"));
27 *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
28 *revisions = xstrfmt("%s...%s", base_rev, *head_hash);
30 shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV);
32 if (upstream) {
33 const char *shortrev;
34 struct object_id rev_oid;
36 get_oid(base_rev, &rev_oid);
37 shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV);
39 *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
40 } else
41 *shortrevisions = xstrdup(shorthead);
43 return 0;
46 static int init_basic_state(struct replay_opts *opts, const char *head_name,
47 const char *onto, const char *orig_head)
49 FILE *interactive;
51 if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir()))
52 return error_errno(_("could not create temporary %s"), path_state_dir());
54 delete_reflog("REBASE_HEAD");
56 interactive = fopen(path_interactive(), "w");
57 if (!interactive)
58 return error_errno(_("could not mark as interactive"));
59 fclose(interactive);
61 return write_basic_state(opts, head_name, onto, orig_head);
64 static int do_interactive_rebase(struct replay_opts *opts, unsigned flags,
65 const char *switch_to, const char *upstream,
66 const char *onto, const char *onto_name,
67 const char *squash_onto, const char *head_name,
68 const char *restrict_revision, char *raw_strategies,
69 const char *cmd, unsigned autosquash)
71 int ret;
72 const char *head_hash = NULL;
73 char *revisions = NULL, *shortrevisions = NULL;
74 struct argv_array make_script_args = ARGV_ARRAY_INIT;
75 FILE *todo_list;
77 if (prepare_branch_to_be_rebased(opts, switch_to))
78 return -1;
80 if (get_revision_ranges(upstream, onto, &head_hash,
81 &revisions, &shortrevisions))
82 return -1;
84 if (raw_strategies)
85 parse_strategy_opts(opts, raw_strategies);
87 if (init_basic_state(opts, head_name, onto, head_hash)) {
88 free(revisions);
89 free(shortrevisions);
91 return -1;
94 if (!upstream && squash_onto)
95 write_file(path_squash_onto(), "%s\n", squash_onto);
97 todo_list = fopen(rebase_path_todo(), "w");
98 if (!todo_list) {
99 free(revisions);
100 free(shortrevisions);
102 return error_errno(_("could not open %s"), rebase_path_todo());
105 argv_array_pushl(&make_script_args, "", revisions, NULL);
106 if (restrict_revision)
107 argv_array_push(&make_script_args, restrict_revision);
109 ret = sequencer_make_script(the_repository, todo_list,
110 make_script_args.argc, make_script_args.argv,
111 flags);
112 fclose(todo_list);
114 if (ret)
115 error(_("could not generate todo list"));
116 else {
117 discard_cache();
118 ret = complete_action(the_repository, opts, flags,
119 shortrevisions, onto_name, onto,
120 head_hash, cmd, autosquash);
123 free(revisions);
124 free(shortrevisions);
125 argv_array_clear(&make_script_args);
127 return ret;
130 static const char * const builtin_rebase_interactive_usage[] = {
131 N_("git rebase--interactive [<options>]"),
132 NULL
135 int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
137 struct replay_opts opts = REPLAY_OPTS_INIT;
138 unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0;
139 int abbreviate_commands = 0, rebase_cousins = -1, ret = 0;
140 const char *onto = NULL, *onto_name = NULL, *restrict_revision = NULL,
141 *squash_onto = NULL, *upstream = NULL, *head_name = NULL,
142 *switch_to = NULL, *cmd = NULL;
143 char *raw_strategies = NULL;
144 enum {
145 NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH,
146 SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC
147 } command = 0;
148 struct option options[] = {
149 OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
150 OPT_BOOL(0, "keep-empty", &keep_empty, N_("keep empty commits")),
151 OPT_BOOL(0, "allow-empty-message", &opts.allow_empty_message,
152 N_("allow commits with empty messages")),
153 OPT_BOOL(0, "rebase-merges", &rebase_merges, N_("rebase merge commits")),
154 OPT_BOOL(0, "rebase-cousins", &rebase_cousins,
155 N_("keep original branch points of cousins")),
156 OPT_BOOL(0, "autosquash", &autosquash,
157 N_("move commits that begin with squash!/fixup!")),
158 OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
159 OPT__VERBOSE(&opts.verbose, N_("be verbose")),
160 OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
161 CONTINUE),
162 OPT_CMDMODE(0, "skip", &command, N_("skip commit"), SKIP),
163 OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list"),
164 EDIT_TODO),
165 OPT_CMDMODE(0, "show-current-patch", &command, N_("show the current patch"),
166 SHOW_CURRENT_PATCH),
167 OPT_CMDMODE(0, "shorten-ids", &command,
168 N_("shorten commit ids in the todo list"), SHORTEN_OIDS),
169 OPT_CMDMODE(0, "expand-ids", &command,
170 N_("expand commit ids in the todo list"), EXPAND_OIDS),
171 OPT_CMDMODE(0, "check-todo-list", &command,
172 N_("check the todo list"), CHECK_TODO_LIST),
173 OPT_CMDMODE(0, "rearrange-squash", &command,
174 N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
175 OPT_CMDMODE(0, "add-exec-commands", &command,
176 N_("insert exec commands in todo list"), ADD_EXEC),
177 OPT_STRING(0, "onto", &onto, N_("onto"), N_("onto")),
178 OPT_STRING(0, "restrict-revision", &restrict_revision,
179 N_("restrict-revision"), N_("restrict revision")),
180 OPT_STRING(0, "squash-onto", &squash_onto, N_("squash-onto"),
181 N_("squash onto")),
182 OPT_STRING(0, "upstream", &upstream, N_("upstream"),
183 N_("the upstream commit")),
184 OPT_STRING(0, "head-name", &head_name, N_("head-name"), N_("head name")),
185 { OPTION_STRING, 'S', "gpg-sign", &opts.gpg_sign, N_("key-id"),
186 N_("GPG-sign commits"),
187 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
188 OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"),
189 N_("rebase strategy")),
190 OPT_STRING(0, "strategy-opts", &raw_strategies, N_("strategy-opts"),
191 N_("strategy options")),
192 OPT_STRING(0, "switch-to", &switch_to, N_("switch-to"),
193 N_("the branch or commit to checkout")),
194 OPT_STRING(0, "onto-name", &onto_name, N_("onto-name"), N_("onto name")),
195 OPT_STRING(0, "cmd", &cmd, N_("cmd"), N_("the command to run")),
196 OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_auto),
197 OPT_BOOL(0, "reschedule-failed-exec", &opts.reschedule_failed_exec,
198 N_("automatically re-schedule any `exec` that fails")),
199 OPT_END()
202 sequencer_init_config(&opts);
203 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
205 opts.action = REPLAY_INTERACTIVE_REBASE;
206 opts.allow_ff = 1;
207 opts.allow_empty = 1;
209 if (argc == 1)
210 usage_with_options(builtin_rebase_interactive_usage, options);
212 argc = parse_options(argc, argv, NULL, options,
213 builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0);
215 opts.gpg_sign = xstrdup_or_null(opts.gpg_sign);
217 flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
218 flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
219 flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
220 flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
221 flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
223 if (rebase_cousins >= 0 && !rebase_merges)
224 warning(_("--[no-]rebase-cousins has no effect without "
225 "--rebase-merges"));
227 switch (command) {
228 case NONE:
229 if (!onto && !upstream)
230 die(_("a base commit must be provided with --upstream or --onto"));
232 ret = do_interactive_rebase(&opts, flags, switch_to, upstream, onto,
233 onto_name, squash_onto, head_name, restrict_revision,
234 raw_strategies, cmd, autosquash);
235 break;
236 case SKIP: {
237 struct string_list merge_rr = STRING_LIST_INIT_DUP;
239 rerere_clear(the_repository, &merge_rr);
240 /* fallthrough */
241 case CONTINUE:
242 ret = sequencer_continue(the_repository, &opts);
243 break;
245 case EDIT_TODO:
246 ret = edit_todo_list(the_repository, flags);
247 break;
248 case SHOW_CURRENT_PATCH: {
249 struct child_process cmd = CHILD_PROCESS_INIT;
251 cmd.git_cmd = 1;
252 argv_array_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL);
253 ret = run_command(&cmd);
255 break;
257 case SHORTEN_OIDS:
258 case EXPAND_OIDS:
259 ret = transform_todos(the_repository, flags);
260 break;
261 case CHECK_TODO_LIST:
262 ret = check_todo_list(the_repository);
263 break;
264 case REARRANGE_SQUASH:
265 ret = rearrange_squash(the_repository);
266 break;
267 case ADD_EXEC:
268 ret = sequencer_add_exec_commands(the_repository, cmd);
269 break;
270 default:
271 BUG("invalid command '%d'", command);
274 return !!ret;