]>
Commit | Line | Data |
---|---|---|
53bbcfbd AG |
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 get_revision_ranges(const char *upstream, const char *onto, | |
17 | const char **head_hash, | |
18 | char **revisions, char **shortrevisions) | |
19 | { | |
20 | const char *base_rev = upstream ? upstream : onto, *shorthead; | |
21 | struct object_id orig_head; | |
22 | ||
23 | if (get_oid("HEAD", &orig_head)) | |
24 | return error(_("no HEAD?")); | |
25 | ||
26 | *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ); | |
27 | *revisions = xstrfmt("%s...%s", base_rev, *head_hash); | |
28 | ||
29 | shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV); | |
30 | ||
31 | if (upstream) { | |
32 | const char *shortrev; | |
33 | struct object_id rev_oid; | |
34 | ||
35 | get_oid(base_rev, &rev_oid); | |
36 | shortrev = find_unique_abbrev(&rev_oid, DEFAULT_ABBREV); | |
37 | ||
38 | *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead); | |
39 | } else | |
40 | *shortrevisions = xstrdup(shorthead); | |
41 | ||
42 | return 0; | |
43 | } | |
44 | ||
45 | static int init_basic_state(struct replay_opts *opts, const char *head_name, | |
46 | const char *onto, const char *orig_head) | |
47 | { | |
48 | FILE *interactive; | |
49 | ||
50 | if (!is_directory(path_state_dir()) && mkdir_in_gitdir(path_state_dir())) | |
51 | return error_errno(_("could not create temporary %s"), path_state_dir()); | |
52 | ||
53 | delete_reflog("REBASE_HEAD"); | |
54 | ||
55 | interactive = fopen(path_interactive(), "w"); | |
56 | if (!interactive) | |
57 | return error_errno(_("could not mark as interactive")); | |
58 | fclose(interactive); | |
59 | ||
60 | return write_basic_state(opts, head_name, onto, orig_head); | |
61 | } | |
62 | ||
63 | static int do_interactive_rebase(struct replay_opts *opts, unsigned flags, | |
64 | const char *switch_to, const char *upstream, | |
65 | const char *onto, const char *onto_name, | |
66 | const char *squash_onto, const char *head_name, | |
67 | const char *restrict_revision, char *raw_strategies, | |
683153a4 | 68 | struct string_list *commands, unsigned autosquash) |
53bbcfbd AG |
69 | { |
70 | int ret; | |
71 | const char *head_hash = NULL; | |
72 | char *revisions = NULL, *shortrevisions = NULL; | |
73 | struct argv_array make_script_args = ARGV_ARRAY_INIT; | |
74 | FILE *todo_list; | |
75 | ||
76 | if (prepare_branch_to_be_rebased(opts, switch_to)) | |
77 | return -1; | |
78 | ||
79 | if (get_revision_ranges(upstream, onto, &head_hash, | |
80 | &revisions, &shortrevisions)) | |
81 | return -1; | |
82 | ||
83 | if (raw_strategies) | |
84 | parse_strategy_opts(opts, raw_strategies); | |
85 | ||
86 | if (init_basic_state(opts, head_name, onto, head_hash)) { | |
87 | free(revisions); | |
88 | free(shortrevisions); | |
89 | ||
90 | return -1; | |
91 | } | |
92 | ||
93 | if (!upstream && squash_onto) | |
94 | write_file(path_squash_onto(), "%s\n", squash_onto); | |
95 | ||
96 | todo_list = fopen(rebase_path_todo(), "w"); | |
97 | if (!todo_list) { | |
98 | free(revisions); | |
99 | free(shortrevisions); | |
100 | ||
101 | return error_errno(_("could not open %s"), rebase_path_todo()); | |
102 | } | |
103 | ||
104 | argv_array_pushl(&make_script_args, "", revisions, NULL); | |
105 | if (restrict_revision) | |
106 | argv_array_push(&make_script_args, restrict_revision); | |
107 | ||
f11c9580 | 108 | ret = sequencer_make_script(the_repository, todo_list, |
53bbcfbd AG |
109 | make_script_args.argc, make_script_args.argv, |
110 | flags); | |
111 | fclose(todo_list); | |
112 | ||
113 | if (ret) | |
114 | error(_("could not generate todo list")); | |
115 | else { | |
116 | discard_cache(); | |
005af339 NTND |
117 | ret = complete_action(the_repository, opts, flags, |
118 | shortrevisions, onto_name, onto, | |
683153a4 | 119 | head_hash, commands, autosquash); |
53bbcfbd AG |
120 | } |
121 | ||
122 | free(revisions); | |
123 | free(shortrevisions); | |
124 | argv_array_clear(&make_script_args); | |
125 | ||
126 | return ret; | |
127 | } | |
128 | ||
129 | static const char * const builtin_rebase_interactive_usage[] = { | |
130 | N_("git rebase--interactive [<options>]"), | |
131 | NULL | |
132 | }; | |
133 | ||
134 | int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) | |
135 | { | |
136 | struct replay_opts opts = REPLAY_OPTS_INIT; | |
137 | unsigned flags = 0, keep_empty = 0, rebase_merges = 0, autosquash = 0; | |
0af129b2 | 138 | int abbreviate_commands = 0, rebase_cousins = -1, ret = 0; |
53bbcfbd AG |
139 | const char *onto = NULL, *onto_name = NULL, *restrict_revision = NULL, |
140 | *squash_onto = NULL, *upstream = NULL, *head_name = NULL, | |
141 | *switch_to = NULL, *cmd = NULL; | |
683153a4 | 142 | struct string_list commands = STRING_LIST_INIT_DUP; |
53bbcfbd | 143 | char *raw_strategies = NULL; |
0af129b2 | 144 | enum { |
34b47315 AG |
145 | NONE = 0, CONTINUE, SKIP, EDIT_TODO, SHOW_CURRENT_PATCH, |
146 | SHORTEN_OIDS, EXPAND_OIDS, CHECK_TODO_LIST, REARRANGE_SQUASH, ADD_EXEC | |
0af129b2 | 147 | } command = 0; |
53bbcfbd AG |
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")), | |
0af129b2 AG |
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), | |
34b47315 AG |
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), | |
53bbcfbd AG |
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_END() | |
198 | }; | |
199 | ||
200 | sequencer_init_config(&opts); | |
201 | git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands); | |
202 | ||
203 | opts.action = REPLAY_INTERACTIVE_REBASE; | |
204 | opts.allow_ff = 1; | |
205 | opts.allow_empty = 1; | |
206 | ||
207 | if (argc == 1) | |
208 | usage_with_options(builtin_rebase_interactive_usage, options); | |
209 | ||
210 | argc = parse_options(argc, argv, NULL, options, | |
211 | builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0); | |
212 | ||
213 | opts.gpg_sign = xstrdup_or_null(opts.gpg_sign); | |
214 | ||
215 | flags |= keep_empty ? TODO_LIST_KEEP_EMPTY : 0; | |
216 | flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0; | |
217 | flags |= rebase_merges ? TODO_LIST_REBASE_MERGES : 0; | |
218 | flags |= rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0; | |
34b47315 | 219 | flags |= command == SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0; |
53bbcfbd AG |
220 | |
221 | if (rebase_cousins >= 0 && !rebase_merges) | |
222 | warning(_("--[no-]rebase-cousins has no effect without " | |
223 | "--rebase-merges")); | |
224 | ||
683153a4 AG |
225 | if (cmd && *cmd) { |
226 | string_list_split(&commands, cmd, '\n', -1); | |
227 | ||
228 | /* rebase.c adds a new line to cmd after every command, | |
229 | * so here the last command is always empty */ | |
230 | string_list_remove_empty_items(&commands, 0); | |
231 | } | |
232 | ||
0af129b2 AG |
233 | switch (command) { |
234 | case NONE: | |
235 | if (!onto && !upstream) | |
236 | die(_("a base commit must be provided with --upstream or --onto")); | |
237 | ||
238 | ret = do_interactive_rebase(&opts, flags, switch_to, upstream, onto, | |
239 | onto_name, squash_onto, head_name, restrict_revision, | |
683153a4 | 240 | raw_strategies, &commands, autosquash); |
0af129b2 AG |
241 | break; |
242 | case SKIP: { | |
243 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
244 | ||
55e6b354 | 245 | rerere_clear(the_repository, &merge_rr); |
0af129b2 AG |
246 | /* fallthrough */ |
247 | case CONTINUE: | |
f11c9580 | 248 | ret = sequencer_continue(the_repository, &opts); |
0af129b2 AG |
249 | break; |
250 | } | |
251 | case EDIT_TODO: | |
36e7ed69 | 252 | ret = edit_todo_list(the_repository, flags); |
0af129b2 AG |
253 | break; |
254 | case SHOW_CURRENT_PATCH: { | |
255 | struct child_process cmd = CHILD_PROCESS_INIT; | |
256 | ||
257 | cmd.git_cmd = 1; | |
258 | argv_array_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL); | |
259 | ret = run_command(&cmd); | |
260 | ||
261 | break; | |
262 | } | |
34b47315 AG |
263 | case SHORTEN_OIDS: |
264 | case EXPAND_OIDS: | |
cbef27d6 | 265 | ret = transform_todo_file(the_repository, flags); |
34b47315 AG |
266 | break; |
267 | case CHECK_TODO_LIST: | |
6ca89c6f | 268 | ret = check_todo_list_from_file(the_repository); |
34b47315 AG |
269 | break; |
270 | case REARRANGE_SQUASH: | |
f2a04904 | 271 | ret = rearrange_squash_in_todo_file(the_repository); |
34b47315 AG |
272 | break; |
273 | case ADD_EXEC: | |
683153a4 | 274 | ret = sequencer_add_exec_commands(the_repository, &commands); |
34b47315 | 275 | break; |
0af129b2 AG |
276 | default: |
277 | BUG("invalid command '%d'", command); | |
278 | } | |
53bbcfbd | 279 | |
683153a4 | 280 | string_list_clear(&commands, 0); |
0af129b2 | 281 | return !!ret; |
53bbcfbd | 282 | } |