]>
Commit | Line | Data |
---|---|---|
55071ea2 PK |
1 | /* |
2 | * "git rebase" builtin command | |
3 | * | |
4 | * Copyright (c) 2018 Pratik Karki | |
5 | */ | |
6 | ||
7 | #include "builtin.h" | |
8 | #include "run-command.h" | |
9 | #include "exec-cmd.h" | |
10 | #include "argv-array.h" | |
11 | #include "dir.h" | |
ac7f467f PK |
12 | #include "packfile.h" |
13 | #include "refs.h" | |
14 | #include "quote.h" | |
15 | #include "config.h" | |
16 | #include "cache-tree.h" | |
17 | #include "unpack-trees.h" | |
18 | #include "lockfile.h" | |
f28d40d3 | 19 | #include "parse-options.h" |
075bc852 | 20 | #include "commit.h" |
bff014da | 21 | #include "diff.h" |
e0333e5c | 22 | #include "wt-status.h" |
9a48a615 | 23 | #include "revision.h" |
e0720a38 | 24 | #include "commit-reach.h" |
122420c2 | 25 | #include "rerere.h" |
5aec9271 | 26 | #include "branch.h" |
f28d40d3 PK |
27 | |
28 | static char const * const builtin_rebase_usage[] = { | |
29 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " | |
30 | "[<upstream>] [<branch>]"), | |
31 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " | |
32 | "--root [<branch>]"), | |
33 | N_("git rebase --continue | --abort | --skip | --edit-todo"), | |
34 | NULL | |
35 | }; | |
ac7f467f PK |
36 | |
37 | static GIT_PATH_FUNC(apply_dir, "rebase-apply") | |
38 | static GIT_PATH_FUNC(merge_dir, "rebase-merge") | |
39 | ||
40 | enum rebase_type { | |
41 | REBASE_UNSPECIFIED = -1, | |
42 | REBASE_AM, | |
43 | REBASE_MERGE, | |
44 | REBASE_INTERACTIVE, | |
45 | REBASE_PRESERVE_MERGES | |
46 | }; | |
55071ea2 PK |
47 | |
48 | static int use_builtin_rebase(void) | |
49 | { | |
50 | struct child_process cp = CHILD_PROCESS_INIT; | |
51 | struct strbuf out = STRBUF_INIT; | |
62c23938 ÆAB |
52 | int ret, env = git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1); |
53 | ||
54 | if (env != -1) | |
55 | return env; | |
55071ea2 PK |
56 | |
57 | argv_array_pushl(&cp.args, | |
58 | "config", "--bool", "rebase.usebuiltin", NULL); | |
59 | cp.git_cmd = 1; | |
60 | if (capture_command(&cp, &out, 6)) { | |
61 | strbuf_release(&out); | |
5541bd5b | 62 | return 1; |
55071ea2 PK |
63 | } |
64 | ||
65 | strbuf_trim(&out); | |
66 | ret = !strcmp("true", out.buf); | |
67 | strbuf_release(&out); | |
68 | return ret; | |
69 | } | |
70 | ||
ac7f467f PK |
71 | struct rebase_options { |
72 | enum rebase_type type; | |
73 | const char *state_dir; | |
74 | struct commit *upstream; | |
75 | const char *upstream_name; | |
06e4775a | 76 | const char *upstream_arg; |
ac7f467f PK |
77 | char *head_name; |
78 | struct object_id orig_head; | |
79 | struct commit *onto; | |
80 | const char *onto_name; | |
81 | const char *revisions; | |
e65123a7 | 82 | const char *switch_to; |
ac7f467f | 83 | int root; |
9dba809a | 84 | struct object_id *squash_onto; |
ac7f467f PK |
85 | struct commit *restrict_revision; |
86 | int dont_finish_rebase; | |
b4c8eb02 PK |
87 | enum { |
88 | REBASE_NO_QUIET = 1<<0, | |
bff014da PK |
89 | REBASE_VERBOSE = 1<<1, |
90 | REBASE_DIFFSTAT = 1<<2, | |
1ed9c14f | 91 | REBASE_FORCE = 1<<3, |
c54dacb5 | 92 | REBASE_INTERACTIVE_EXPLICIT = 1<<4, |
b4c8eb02 | 93 | } flags; |
f5769680 | 94 | struct argv_array git_am_opts; |
f9573628 | 95 | const char *action; |
73d51ed0 | 96 | int signoff; |
ead98c11 | 97 | int allow_rerere_autoupdate; |
002ee2fe | 98 | int keep_empty; |
051910a9 | 99 | int autosquash; |
12026a41 | 100 | char *gpg_sign_opt; |
6defce2b | 101 | int autostash; |
68e46d78 | 102 | char *cmd; |
9b3a448b | 103 | int allow_empty_message; |
3c3588c7 | 104 | int rebase_merges, rebase_cousins; |
ba1905a5 | 105 | char *strategy, *strategy_opts; |
cda614e4 | 106 | struct strbuf git_format_patch_opt; |
ac7f467f PK |
107 | }; |
108 | ||
9a48a615 PK |
109 | static int is_interactive(struct rebase_options *opts) |
110 | { | |
111 | return opts->type == REBASE_INTERACTIVE || | |
112 | opts->type == REBASE_PRESERVE_MERGES; | |
113 | } | |
114 | ||
002ee2fe PK |
115 | static void imply_interactive(struct rebase_options *opts, const char *option) |
116 | { | |
117 | switch (opts->type) { | |
118 | case REBASE_AM: | |
119 | die(_("%s requires an interactive rebase"), option); | |
120 | break; | |
121 | case REBASE_INTERACTIVE: | |
122 | case REBASE_PRESERVE_MERGES: | |
123 | break; | |
124 | case REBASE_MERGE: | |
125 | /* we silently *upgrade* --merge to --interactive if needed */ | |
126 | default: | |
127 | opts->type = REBASE_INTERACTIVE; /* implied */ | |
128 | break; | |
129 | } | |
130 | } | |
131 | ||
ac7f467f PK |
132 | /* Returns the filename prefixed by the state_dir */ |
133 | static const char *state_dir_path(const char *filename, struct rebase_options *opts) | |
134 | { | |
135 | static struct strbuf path = STRBUF_INIT; | |
136 | static size_t prefix_len; | |
137 | ||
138 | if (!prefix_len) { | |
139 | strbuf_addf(&path, "%s/", opts->state_dir); | |
140 | prefix_len = path.len; | |
141 | } | |
142 | ||
143 | strbuf_setlen(&path, prefix_len); | |
144 | strbuf_addstr(&path, filename); | |
145 | return path.buf; | |
146 | } | |
147 | ||
f9573628 PK |
148 | /* Read one file, then strip line endings */ |
149 | static int read_one(const char *path, struct strbuf *buf) | |
150 | { | |
151 | if (strbuf_read_file(buf, path, 0) < 0) | |
152 | return error_errno(_("could not read '%s'"), path); | |
153 | strbuf_trim_trailing_newline(buf); | |
154 | return 0; | |
155 | } | |
156 | ||
157 | /* Initialize the rebase options from the state directory. */ | |
158 | static int read_basic_state(struct rebase_options *opts) | |
159 | { | |
160 | struct strbuf head_name = STRBUF_INIT; | |
161 | struct strbuf buf = STRBUF_INIT; | |
162 | struct object_id oid; | |
163 | ||
164 | if (read_one(state_dir_path("head-name", opts), &head_name) || | |
165 | read_one(state_dir_path("onto", opts), &buf)) | |
166 | return -1; | |
167 | opts->head_name = starts_with(head_name.buf, "refs/") ? | |
168 | xstrdup(head_name.buf) : NULL; | |
169 | strbuf_release(&head_name); | |
170 | if (get_oid(buf.buf, &oid)) | |
171 | return error(_("could not get 'onto': '%s'"), buf.buf); | |
172 | opts->onto = lookup_commit_or_die(&oid, buf.buf); | |
173 | ||
174 | /* | |
175 | * We always write to orig-head, but interactive rebase used to write to | |
176 | * head. Fall back to reading from head to cover for the case that the | |
177 | * user upgraded git with an ongoing interactive rebase. | |
178 | */ | |
179 | strbuf_reset(&buf); | |
180 | if (file_exists(state_dir_path("orig-head", opts))) { | |
181 | if (read_one(state_dir_path("orig-head", opts), &buf)) | |
182 | return -1; | |
183 | } else if (read_one(state_dir_path("head", opts), &buf)) | |
184 | return -1; | |
185 | if (get_oid(buf.buf, &opts->orig_head)) | |
186 | return error(_("invalid orig-head: '%s'"), buf.buf); | |
187 | ||
899b49c4 | 188 | if (file_exists(state_dir_path("quiet", opts))) |
f9573628 PK |
189 | opts->flags &= ~REBASE_NO_QUIET; |
190 | else | |
191 | opts->flags |= REBASE_NO_QUIET; | |
192 | ||
193 | if (file_exists(state_dir_path("verbose", opts))) | |
194 | opts->flags |= REBASE_VERBOSE; | |
195 | ||
73d51ed0 PK |
196 | if (file_exists(state_dir_path("signoff", opts))) { |
197 | opts->signoff = 1; | |
198 | opts->flags |= REBASE_FORCE; | |
199 | } | |
200 | ||
ead98c11 PK |
201 | if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) { |
202 | strbuf_reset(&buf); | |
203 | if (read_one(state_dir_path("allow_rerere_autoupdate", opts), | |
204 | &buf)) | |
205 | return -1; | |
206 | if (!strcmp(buf.buf, "--rerere-autoupdate")) | |
207 | opts->allow_rerere_autoupdate = 1; | |
208 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) | |
209 | opts->allow_rerere_autoupdate = 0; | |
210 | else | |
211 | warning(_("ignoring invalid allow_rerere_autoupdate: " | |
212 | "'%s'"), buf.buf); | |
213 | } else | |
214 | opts->allow_rerere_autoupdate = -1; | |
215 | ||
12026a41 PK |
216 | if (file_exists(state_dir_path("gpg_sign_opt", opts))) { |
217 | strbuf_reset(&buf); | |
218 | if (read_one(state_dir_path("gpg_sign_opt", opts), | |
219 | &buf)) | |
220 | return -1; | |
221 | free(opts->gpg_sign_opt); | |
222 | opts->gpg_sign_opt = xstrdup(buf.buf); | |
223 | } | |
224 | ||
ba1905a5 PK |
225 | if (file_exists(state_dir_path("strategy", opts))) { |
226 | strbuf_reset(&buf); | |
227 | if (read_one(state_dir_path("strategy", opts), &buf)) | |
228 | return -1; | |
229 | free(opts->strategy); | |
230 | opts->strategy = xstrdup(buf.buf); | |
231 | } | |
232 | ||
233 | if (file_exists(state_dir_path("strategy_opts", opts))) { | |
234 | strbuf_reset(&buf); | |
235 | if (read_one(state_dir_path("strategy_opts", opts), &buf)) | |
236 | return -1; | |
237 | free(opts->strategy_opts); | |
238 | opts->strategy_opts = xstrdup(buf.buf); | |
239 | } | |
240 | ||
f9573628 PK |
241 | strbuf_release(&buf); |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
6defce2b PK |
246 | static int apply_autostash(struct rebase_options *opts) |
247 | { | |
248 | const char *path = state_dir_path("autostash", opts); | |
249 | struct strbuf autostash = STRBUF_INIT; | |
250 | struct child_process stash_apply = CHILD_PROCESS_INIT; | |
251 | ||
252 | if (!file_exists(path)) | |
253 | return 0; | |
254 | ||
71064e60 | 255 | if (read_one(path, &autostash)) |
6defce2b | 256 | return error(_("Could not read '%s'"), path); |
b98e914e JS |
257 | /* Ensure that the hash is not mistaken for a number */ |
258 | strbuf_addstr(&autostash, "^0"); | |
6defce2b PK |
259 | argv_array_pushl(&stash_apply.args, |
260 | "stash", "apply", autostash.buf, NULL); | |
261 | stash_apply.git_cmd = 1; | |
262 | stash_apply.no_stderr = stash_apply.no_stdout = | |
263 | stash_apply.no_stdin = 1; | |
264 | if (!run_command(&stash_apply)) | |
265 | printf(_("Applied autostash.\n")); | |
266 | else { | |
267 | struct argv_array args = ARGV_ARRAY_INIT; | |
268 | int res = 0; | |
269 | ||
270 | argv_array_pushl(&args, | |
271 | "stash", "store", "-m", "autostash", "-q", | |
272 | autostash.buf, NULL); | |
273 | if (run_command_v_opt(args.argv, RUN_GIT_CMD)) | |
274 | res = error(_("Cannot store %s"), autostash.buf); | |
275 | argv_array_clear(&args); | |
276 | strbuf_release(&autostash); | |
277 | if (res) | |
278 | return res; | |
279 | ||
280 | fprintf(stderr, | |
281 | _("Applying autostash resulted in conflicts.\n" | |
282 | "Your changes are safe in the stash.\n" | |
283 | "You can run \"git stash pop\" or \"git stash drop\" " | |
284 | "at any time.\n")); | |
285 | } | |
286 | ||
287 | strbuf_release(&autostash); | |
288 | return 0; | |
289 | } | |
290 | ||
ac7f467f PK |
291 | static int finish_rebase(struct rebase_options *opts) |
292 | { | |
293 | struct strbuf dir = STRBUF_INIT; | |
294 | const char *argv_gc_auto[] = { "gc", "--auto", NULL }; | |
295 | ||
296 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); | |
6defce2b | 297 | apply_autostash(opts); |
ac7f467f PK |
298 | close_all_packs(the_repository->objects); |
299 | /* | |
300 | * We ignore errors in 'gc --auto', since the | |
301 | * user should see them. | |
302 | */ | |
303 | run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); | |
304 | strbuf_addstr(&dir, opts->state_dir); | |
305 | remove_dir_recursively(&dir, 0); | |
306 | strbuf_release(&dir); | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | static struct commit *peel_committish(const char *name) | |
312 | { | |
313 | struct object *obj; | |
314 | struct object_id oid; | |
315 | ||
316 | if (get_oid(name, &oid)) | |
317 | return NULL; | |
318 | obj = parse_object(the_repository, &oid); | |
319 | return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); | |
320 | } | |
321 | ||
322 | static void add_var(struct strbuf *buf, const char *name, const char *value) | |
323 | { | |
324 | if (!value) | |
325 | strbuf_addf(buf, "unset %s; ", name); | |
326 | else { | |
327 | strbuf_addf(buf, "%s=", name); | |
328 | sq_quote_buf(buf, value); | |
329 | strbuf_addstr(buf, "; "); | |
330 | } | |
331 | } | |
332 | ||
bc24382c JS |
333 | static const char *resolvemsg = |
334 | N_("Resolve all conflicts manually, mark them as resolved with\n" | |
335 | "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n" | |
336 | "You can instead skip this commit: run \"git rebase --skip\".\n" | |
337 | "To abort and get back to the state before \"git rebase\", run " | |
338 | "\"git rebase --abort\"."); | |
339 | ||
ac7f467f PK |
340 | static int run_specific_rebase(struct rebase_options *opts) |
341 | { | |
342 | const char *argv[] = { NULL, NULL }; | |
f5769680 | 343 | struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT; |
ac7f467f PK |
344 | int status; |
345 | const char *backend, *backend_func; | |
346 | ||
bc24382c JS |
347 | if (opts->type == REBASE_INTERACTIVE) { |
348 | /* Run builtin interactive rebase */ | |
349 | struct child_process child = CHILD_PROCESS_INIT; | |
350 | ||
351 | argv_array_pushf(&child.env_array, "GIT_CHERRY_PICK_HELP=%s", | |
352 | resolvemsg); | |
353 | if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) { | |
354 | argv_array_push(&child.env_array, "GIT_EDITOR=:"); | |
355 | opts->autosquash = 0; | |
356 | } | |
357 | ||
358 | child.git_cmd = 1; | |
359 | argv_array_push(&child.args, "rebase--interactive"); | |
360 | ||
361 | if (opts->action) | |
362 | argv_array_pushf(&child.args, "--%s", opts->action); | |
363 | if (opts->keep_empty) | |
364 | argv_array_push(&child.args, "--keep-empty"); | |
365 | if (opts->rebase_merges) | |
366 | argv_array_push(&child.args, "--rebase-merges"); | |
367 | if (opts->rebase_cousins) | |
368 | argv_array_push(&child.args, "--rebase-cousins"); | |
369 | if (opts->autosquash) | |
370 | argv_array_push(&child.args, "--autosquash"); | |
371 | if (opts->flags & REBASE_VERBOSE) | |
372 | argv_array_push(&child.args, "--verbose"); | |
373 | if (opts->flags & REBASE_FORCE) | |
374 | argv_array_push(&child.args, "--no-ff"); | |
375 | if (opts->restrict_revision) | |
376 | argv_array_pushf(&child.args, | |
377 | "--restrict-revision=^%s", | |
378 | oid_to_hex(&opts->restrict_revision->object.oid)); | |
379 | if (opts->upstream) | |
380 | argv_array_pushf(&child.args, "--upstream=%s", | |
381 | oid_to_hex(&opts->upstream->object.oid)); | |
382 | if (opts->onto) | |
383 | argv_array_pushf(&child.args, "--onto=%s", | |
384 | oid_to_hex(&opts->onto->object.oid)); | |
385 | if (opts->squash_onto) | |
386 | argv_array_pushf(&child.args, "--squash-onto=%s", | |
387 | oid_to_hex(opts->squash_onto)); | |
388 | if (opts->onto_name) | |
389 | argv_array_pushf(&child.args, "--onto-name=%s", | |
390 | opts->onto_name); | |
391 | argv_array_pushf(&child.args, "--head-name=%s", | |
392 | opts->head_name ? | |
393 | opts->head_name : "detached HEAD"); | |
394 | if (opts->strategy) | |
395 | argv_array_pushf(&child.args, "--strategy=%s", | |
396 | opts->strategy); | |
397 | if (opts->strategy_opts) | |
398 | argv_array_pushf(&child.args, "--strategy-opts=%s", | |
399 | opts->strategy_opts); | |
400 | if (opts->switch_to) | |
401 | argv_array_pushf(&child.args, "--switch-to=%s", | |
402 | opts->switch_to); | |
403 | if (opts->cmd) | |
404 | argv_array_pushf(&child.args, "--cmd=%s", opts->cmd); | |
405 | if (opts->allow_empty_message) | |
406 | argv_array_push(&child.args, "--allow-empty-message"); | |
407 | if (opts->allow_rerere_autoupdate > 0) | |
408 | argv_array_push(&child.args, "--rerere-autoupdate"); | |
409 | else if (opts->allow_rerere_autoupdate == 0) | |
410 | argv_array_push(&child.args, "--no-rerere-autoupdate"); | |
411 | if (opts->gpg_sign_opt) | |
412 | argv_array_push(&child.args, opts->gpg_sign_opt); | |
413 | if (opts->signoff) | |
414 | argv_array_push(&child.args, "--signoff"); | |
415 | ||
416 | status = run_command(&child); | |
417 | goto finished_rebase; | |
418 | } | |
419 | ||
ac7f467f PK |
420 | add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir())); |
421 | add_var(&script_snippet, "state_dir", opts->state_dir); | |
422 | ||
423 | add_var(&script_snippet, "upstream_name", opts->upstream_name); | |
f9573628 PK |
424 | add_var(&script_snippet, "upstream", opts->upstream ? |
425 | oid_to_hex(&opts->upstream->object.oid) : NULL); | |
d4c569f8 PK |
426 | add_var(&script_snippet, "head_name", |
427 | opts->head_name ? opts->head_name : "detached HEAD"); | |
ac7f467f | 428 | add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head)); |
f9573628 PK |
429 | add_var(&script_snippet, "onto", opts->onto ? |
430 | oid_to_hex(&opts->onto->object.oid) : NULL); | |
ac7f467f PK |
431 | add_var(&script_snippet, "onto_name", opts->onto_name); |
432 | add_var(&script_snippet, "revisions", opts->revisions); | |
433 | add_var(&script_snippet, "restrict_revision", opts->restrict_revision ? | |
434 | oid_to_hex(&opts->restrict_revision->object.oid) : NULL); | |
b4c8eb02 PK |
435 | add_var(&script_snippet, "GIT_QUIET", |
436 | opts->flags & REBASE_NO_QUIET ? "" : "t"); | |
f5769680 JS |
437 | sq_quote_argv_pretty(&buf, opts->git_am_opts.argv); |
438 | add_var(&script_snippet, "git_am_opt", buf.buf); | |
439 | strbuf_release(&buf); | |
bff014da PK |
440 | add_var(&script_snippet, "verbose", |
441 | opts->flags & REBASE_VERBOSE ? "t" : ""); | |
442 | add_var(&script_snippet, "diffstat", | |
443 | opts->flags & REBASE_DIFFSTAT ? "t" : ""); | |
1ed9c14f PK |
444 | add_var(&script_snippet, "force_rebase", |
445 | opts->flags & REBASE_FORCE ? "t" : ""); | |
e65123a7 PK |
446 | if (opts->switch_to) |
447 | add_var(&script_snippet, "switch_to", opts->switch_to); | |
f9573628 | 448 | add_var(&script_snippet, "action", opts->action ? opts->action : ""); |
73d51ed0 | 449 | add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : ""); |
ead98c11 PK |
450 | add_var(&script_snippet, "allow_rerere_autoupdate", |
451 | opts->allow_rerere_autoupdate < 0 ? "" : | |
452 | opts->allow_rerere_autoupdate ? | |
453 | "--rerere-autoupdate" : "--no-rerere-autoupdate"); | |
002ee2fe | 454 | add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : ""); |
051910a9 | 455 | add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : ""); |
12026a41 | 456 | add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt); |
68e46d78 | 457 | add_var(&script_snippet, "cmd", opts->cmd); |
9b3a448b PK |
458 | add_var(&script_snippet, "allow_empty_message", |
459 | opts->allow_empty_message ? "--allow-empty-message" : ""); | |
3c3588c7 PK |
460 | add_var(&script_snippet, "rebase_merges", |
461 | opts->rebase_merges ? "t" : ""); | |
462 | add_var(&script_snippet, "rebase_cousins", | |
463 | opts->rebase_cousins ? "t" : ""); | |
ba1905a5 PK |
464 | add_var(&script_snippet, "strategy", opts->strategy); |
465 | add_var(&script_snippet, "strategy_opts", opts->strategy_opts); | |
9dba809a PK |
466 | add_var(&script_snippet, "rebase_root", opts->root ? "t" : ""); |
467 | add_var(&script_snippet, "squash_onto", | |
468 | opts->squash_onto ? oid_to_hex(opts->squash_onto) : ""); | |
cda614e4 PK |
469 | add_var(&script_snippet, "git_format_patch_opt", |
470 | opts->git_format_patch_opt.buf); | |
ac7f467f | 471 | |
3dba9d08 PK |
472 | if (is_interactive(opts) && |
473 | !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) { | |
474 | strbuf_addstr(&script_snippet, | |
475 | "GIT_EDITOR=:; export GIT_EDITOR; "); | |
476 | opts->autosquash = 0; | |
477 | } | |
ac7f467f PK |
478 | |
479 | switch (opts->type) { | |
480 | case REBASE_AM: | |
481 | backend = "git-rebase--am"; | |
482 | backend_func = "git_rebase__am"; | |
483 | break; | |
ac7f467f PK |
484 | case REBASE_MERGE: |
485 | backend = "git-rebase--merge"; | |
486 | backend_func = "git_rebase__merge"; | |
487 | break; | |
488 | case REBASE_PRESERVE_MERGES: | |
489 | backend = "git-rebase--preserve-merges"; | |
490 | backend_func = "git_rebase__preserve_merges"; | |
491 | break; | |
492 | default: | |
493 | BUG("Unhandled rebase type %d", opts->type); | |
494 | break; | |
495 | } | |
496 | ||
497 | strbuf_addf(&script_snippet, | |
498 | ". git-sh-setup && . git-rebase--common &&" | |
499 | " . %s && %s", backend, backend_func); | |
500 | argv[0] = script_snippet.buf; | |
501 | ||
502 | status = run_command_v_opt(argv, RUN_USING_SHELL); | |
bc24382c | 503 | finished_rebase: |
ac7f467f PK |
504 | if (opts->dont_finish_rebase) |
505 | ; /* do nothing */ | |
bc24382c JS |
506 | else if (opts->type == REBASE_INTERACTIVE) |
507 | ; /* interactive rebase cleans up after itself */ | |
ac7f467f PK |
508 | else if (status == 0) { |
509 | if (!file_exists(state_dir_path("stopped-sha", opts))) | |
510 | finish_rebase(opts); | |
511 | } else if (status == 2) { | |
512 | struct strbuf dir = STRBUF_INIT; | |
513 | ||
6defce2b | 514 | apply_autostash(opts); |
ac7f467f PK |
515 | strbuf_addstr(&dir, opts->state_dir); |
516 | remove_dir_recursively(&dir, 0); | |
517 | strbuf_release(&dir); | |
518 | die("Nothing to do"); | |
519 | } | |
520 | ||
521 | strbuf_release(&script_snippet); | |
522 | ||
523 | return status ? -1 : 0; | |
524 | } | |
525 | ||
526 | #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" | |
527 | ||
73d6d7b2 | 528 | #define RESET_HEAD_DETACH (1<<0) |
bac2a1e3 | 529 | #define RESET_HEAD_HARD (1<<1) |
73d6d7b2 | 530 | |
ac7f467f | 531 | static int reset_head(struct object_id *oid, const char *action, |
73d6d7b2 | 532 | const char *switch_to_branch, unsigned flags, |
fa443d40 | 533 | const char *reflog_orig_head, const char *reflog_head) |
ac7f467f | 534 | { |
73d6d7b2 | 535 | unsigned detach_head = flags & RESET_HEAD_DETACH; |
bac2a1e3 | 536 | unsigned reset_hard = flags & RESET_HEAD_HARD; |
ac7f467f | 537 | struct object_id head_oid; |
bac2a1e3 | 538 | struct tree_desc desc[2] = { { NULL }, { NULL } }; |
ac7f467f PK |
539 | struct lock_file lock = LOCK_INIT; |
540 | struct unpack_trees_options unpack_tree_opts; | |
541 | struct tree *tree; | |
542 | const char *reflog_action; | |
543 | struct strbuf msg = STRBUF_INIT; | |
544 | size_t prefix_len; | |
545 | struct object_id *orig = NULL, oid_orig, | |
546 | *old_orig = NULL, oid_old_orig; | |
bac2a1e3 | 547 | int ret = 0, nr = 0; |
ac7f467f | 548 | |
d4c569f8 PK |
549 | if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) |
550 | BUG("Not a fully qualified branch: '%s'", switch_to_branch); | |
551 | ||
3249c125 JS |
552 | if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) { |
553 | ret = -1; | |
554 | goto leave_reset_head; | |
555 | } | |
ac7f467f | 556 | |
bac2a1e3 JS |
557 | if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) { |
558 | ret = error(_("could not determine HEAD revision")); | |
559 | goto leave_reset_head; | |
ac7f467f PK |
560 | } |
561 | ||
bac2a1e3 JS |
562 | if (!oid) |
563 | oid = &head_oid; | |
564 | ||
ac7f467f PK |
565 | memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); |
566 | setup_unpack_trees_porcelain(&unpack_tree_opts, action); | |
567 | unpack_tree_opts.head_idx = 1; | |
568 | unpack_tree_opts.src_index = the_repository->index; | |
569 | unpack_tree_opts.dst_index = the_repository->index; | |
bac2a1e3 | 570 | unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge; |
ac7f467f PK |
571 | unpack_tree_opts.update = 1; |
572 | unpack_tree_opts.merge = 1; | |
573 | if (!detach_head) | |
574 | unpack_tree_opts.reset = 1; | |
575 | ||
576 | if (read_index_unmerged(the_repository->index) < 0) { | |
3249c125 JS |
577 | ret = error(_("could not read index")); |
578 | goto leave_reset_head; | |
ac7f467f PK |
579 | } |
580 | ||
bac2a1e3 | 581 | if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) { |
4d86503e JS |
582 | ret = error(_("failed to find tree of %s"), |
583 | oid_to_hex(&head_oid)); | |
bac2a1e3 | 584 | goto leave_reset_head; |
ac7f467f PK |
585 | } |
586 | ||
bac2a1e3 | 587 | if (!fill_tree_descriptor(&desc[nr++], oid)) { |
3249c125 JS |
588 | ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); |
589 | goto leave_reset_head; | |
ac7f467f PK |
590 | } |
591 | ||
bac2a1e3 | 592 | if (unpack_trees(nr, desc, &unpack_tree_opts)) { |
3249c125 JS |
593 | ret = -1; |
594 | goto leave_reset_head; | |
ac7f467f PK |
595 | } |
596 | ||
597 | tree = parse_tree_indirect(oid); | |
598 | prime_cache_tree(the_repository->index, tree); | |
599 | ||
3249c125 | 600 | if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) { |
ac7f467f | 601 | ret = error(_("could not write index")); |
3249c125 JS |
602 | goto leave_reset_head; |
603 | } | |
ac7f467f PK |
604 | |
605 | reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); | |
606 | strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); | |
607 | prefix_len = msg.len; | |
608 | ||
609 | if (!get_oid("ORIG_HEAD", &oid_old_orig)) | |
610 | old_orig = &oid_old_orig; | |
611 | if (!get_oid("HEAD", &oid_orig)) { | |
612 | orig = &oid_orig; | |
fa443d40 PK |
613 | if (!reflog_orig_head) { |
614 | strbuf_addstr(&msg, "updating ORIG_HEAD"); | |
615 | reflog_orig_head = msg.buf; | |
616 | } | |
617 | update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0, | |
ac7f467f PK |
618 | UPDATE_REFS_MSG_ON_ERR); |
619 | } else if (old_orig) | |
620 | delete_ref(NULL, "ORIG_HEAD", old_orig, 0); | |
fa443d40 PK |
621 | if (!reflog_head) { |
622 | strbuf_setlen(&msg, prefix_len); | |
623 | strbuf_addstr(&msg, "updating HEAD"); | |
624 | reflog_head = msg.buf; | |
625 | } | |
ac7f467f | 626 | if (!switch_to_branch) |
176f5d96 JS |
627 | ret = update_ref(reflog_head, "HEAD", oid, orig, |
628 | detach_head ? REF_NO_DEREF : 0, | |
ac7f467f PK |
629 | UPDATE_REFS_MSG_ON_ERR); |
630 | else { | |
631 | ret = create_symref("HEAD", switch_to_branch, msg.buf); | |
632 | if (!ret) | |
fa443d40 | 633 | ret = update_ref(reflog_head, "HEAD", oid, NULL, 0, |
ac7f467f PK |
634 | UPDATE_REFS_MSG_ON_ERR); |
635 | } | |
636 | ||
3249c125 | 637 | leave_reset_head: |
ac7f467f | 638 | strbuf_release(&msg); |
3249c125 | 639 | rollback_lock_file(&lock); |
bac2a1e3 JS |
640 | while (nr) |
641 | free((void *)desc[--nr].buffer); | |
ac7f467f PK |
642 | return ret; |
643 | } | |
644 | ||
bff014da PK |
645 | static int rebase_config(const char *var, const char *value, void *data) |
646 | { | |
647 | struct rebase_options *opts = data; | |
648 | ||
649 | if (!strcmp(var, "rebase.stat")) { | |
650 | if (git_config_bool(var, value)) | |
651 | opts->flags |= REBASE_DIFFSTAT; | |
652 | else | |
653 | opts->flags &= !REBASE_DIFFSTAT; | |
654 | return 0; | |
655 | } | |
656 | ||
051910a9 PK |
657 | if (!strcmp(var, "rebase.autosquash")) { |
658 | opts->autosquash = git_config_bool(var, value); | |
659 | return 0; | |
660 | } | |
661 | ||
12026a41 PK |
662 | if (!strcmp(var, "commit.gpgsign")) { |
663 | free(opts->gpg_sign_opt); | |
664 | opts->gpg_sign_opt = git_config_bool(var, value) ? | |
665 | xstrdup("-S") : NULL; | |
666 | return 0; | |
667 | } | |
668 | ||
6defce2b PK |
669 | if (!strcmp(var, "rebase.autostash")) { |
670 | opts->autostash = git_config_bool(var, value); | |
671 | return 0; | |
672 | } | |
673 | ||
bff014da PK |
674 | return git_default_config(var, value, data); |
675 | } | |
676 | ||
9a48a615 PK |
677 | /* |
678 | * Determines whether the commits in from..to are linear, i.e. contain | |
679 | * no merge commits. This function *expects* `from` to be an ancestor of | |
680 | * `to`. | |
681 | */ | |
682 | static int is_linear_history(struct commit *from, struct commit *to) | |
683 | { | |
684 | while (to && to != from) { | |
685 | parse_commit(to); | |
686 | if (!to->parents) | |
687 | return 1; | |
688 | if (to->parents->next) | |
689 | return 0; | |
690 | to = to->parents->item; | |
691 | } | |
692 | return 1; | |
693 | } | |
694 | ||
695 | static int can_fast_forward(struct commit *onto, struct object_id *head_oid, | |
696 | struct object_id *merge_base) | |
697 | { | |
698 | struct commit *head = lookup_commit(the_repository, head_oid); | |
699 | struct commit_list *merge_bases; | |
700 | int res; | |
701 | ||
702 | if (!head) | |
703 | return 0; | |
704 | ||
705 | merge_bases = get_merge_bases(onto, head); | |
706 | if (merge_bases && !merge_bases->next) { | |
707 | oidcpy(merge_base, &merge_bases->item->object.oid); | |
b17ca8f9 | 708 | res = oideq(merge_base, &onto->object.oid); |
9a48a615 PK |
709 | } else { |
710 | oidcpy(merge_base, &null_oid); | |
711 | res = 0; | |
712 | } | |
713 | free_commit_list(merge_bases); | |
714 | return res && is_linear_history(onto, head); | |
715 | } | |
716 | ||
361badd3 PK |
717 | /* -i followed by -m is still -i */ |
718 | static int parse_opt_merge(const struct option *opt, const char *arg, int unset) | |
719 | { | |
720 | struct rebase_options *opts = opt->value; | |
721 | ||
517fe807 JK |
722 | BUG_ON_OPT_NEG(unset); |
723 | BUG_ON_OPT_ARG(arg); | |
724 | ||
361badd3 PK |
725 | if (!is_interactive(opts)) |
726 | opts->type = REBASE_MERGE; | |
727 | ||
728 | return 0; | |
729 | } | |
730 | ||
731 | /* -i followed by -p is still explicitly interactive, but -p alone is not */ | |
732 | static int parse_opt_interactive(const struct option *opt, const char *arg, | |
733 | int unset) | |
734 | { | |
735 | struct rebase_options *opts = opt->value; | |
736 | ||
517fe807 JK |
737 | BUG_ON_OPT_NEG(unset); |
738 | BUG_ON_OPT_ARG(arg); | |
739 | ||
361badd3 PK |
740 | opts->type = REBASE_INTERACTIVE; |
741 | opts->flags |= REBASE_INTERACTIVE_EXPLICIT; | |
742 | ||
743 | return 0; | |
744 | } | |
745 | ||
8f5986d9 PK |
746 | static void NORETURN error_on_missing_default_upstream(void) |
747 | { | |
748 | struct branch *current_branch = branch_get(NULL); | |
749 | ||
750 | printf(_("%s\n" | |
751 | "Please specify which branch you want to rebase against.\n" | |
752 | "See git-rebase(1) for details.\n" | |
753 | "\n" | |
754 | " git rebase '<branch>'\n" | |
755 | "\n"), | |
756 | current_branch ? _("There is no tracking information for " | |
757 | "the current branch.") : | |
758 | _("You are not currently on a branch.")); | |
759 | ||
760 | if (current_branch) { | |
761 | const char *remote = current_branch->remote_name; | |
762 | ||
763 | if (!remote) | |
764 | remote = _("<remote>"); | |
765 | ||
766 | printf(_("If you wish to set tracking information for this " | |
767 | "branch you can do so with:\n" | |
768 | "\n" | |
769 | " git branch --set-upstream-to=%s/<branch> %s\n" | |
770 | "\n"), | |
771 | remote, current_branch->name); | |
772 | } | |
773 | exit(1); | |
774 | } | |
775 | ||
13a5a9f0 JS |
776 | static void set_reflog_action(struct rebase_options *options) |
777 | { | |
778 | const char *env; | |
779 | struct strbuf buf = STRBUF_INIT; | |
780 | ||
781 | if (!is_interactive(options)) | |
782 | return; | |
783 | ||
784 | env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); | |
785 | if (env && strcmp("rebase", env)) | |
786 | return; /* only override it if it is "rebase" */ | |
787 | ||
788 | strbuf_addf(&buf, "rebase -i (%s)", options->action); | |
789 | setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1); | |
790 | strbuf_release(&buf); | |
791 | } | |
792 | ||
55071ea2 PK |
793 | int cmd_rebase(int argc, const char **argv, const char *prefix) |
794 | { | |
ac7f467f PK |
795 | struct rebase_options options = { |
796 | .type = REBASE_UNSPECIFIED, | |
b4c8eb02 | 797 | .flags = REBASE_NO_QUIET, |
f5769680 | 798 | .git_am_opts = ARGV_ARRAY_INIT, |
ead98c11 | 799 | .allow_rerere_autoupdate = -1, |
9b3a448b | 800 | .allow_empty_message = 1, |
cda614e4 | 801 | .git_format_patch_opt = STRBUF_INIT, |
ac7f467f PK |
802 | }; |
803 | const char *branch_name; | |
f9573628 | 804 | int ret, flags, total_argc, in_progress = 0; |
06e4775a | 805 | int ok_to_skip_pre_rebase = 0; |
ac7f467f PK |
806 | struct strbuf msg = STRBUF_INIT; |
807 | struct strbuf revisions = STRBUF_INIT; | |
c54dacb5 | 808 | struct strbuf buf = STRBUF_INIT; |
075bc852 | 809 | struct object_id merge_base; |
f9573628 PK |
810 | enum { |
811 | NO_ACTION, | |
812 | ACTION_CONTINUE, | |
122420c2 | 813 | ACTION_SKIP, |
5e5d9619 | 814 | ACTION_ABORT, |
5a614945 | 815 | ACTION_QUIT, |
51e9ea6d PK |
816 | ACTION_EDIT_TODO, |
817 | ACTION_SHOW_CURRENT_PATCH, | |
f9573628 | 818 | } action = NO_ACTION; |
12026a41 | 819 | const char *gpg_sign = NULL; |
68e46d78 | 820 | struct string_list exec = STRING_LIST_INIT_NODUP; |
3c3588c7 | 821 | const char *rebase_merges = NULL; |
92d0d74e | 822 | int fork_point = -1; |
ba1905a5 | 823 | struct string_list strategy_options = STRING_LIST_INIT_NODUP; |
9dba809a PK |
824 | struct object_id squash_onto; |
825 | char *squash_onto_name = NULL; | |
f28d40d3 PK |
826 | struct option builtin_rebase_options[] = { |
827 | OPT_STRING(0, "onto", &options.onto_name, | |
828 | N_("revision"), | |
829 | N_("rebase onto given branch instead of upstream")), | |
06e4775a PK |
830 | OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase, |
831 | N_("allow pre-rebase hook to run")), | |
b4c8eb02 PK |
832 | OPT_NEGBIT('q', "quiet", &options.flags, |
833 | N_("be quiet. implies --no-stat"), | |
bff014da PK |
834 | REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT), |
835 | OPT_BIT('v', "verbose", &options.flags, | |
836 | N_("display a diffstat of what changed upstream"), | |
837 | REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), | |
838 | {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL, | |
839 | N_("do not show diffstat of what changed upstream"), | |
840 | PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT }, | |
73d51ed0 PK |
841 | OPT_BOOL(0, "signoff", &options.signoff, |
842 | N_("add a Signed-off-by: line to each commit")), | |
f5769680 JS |
843 | OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options.git_am_opts, |
844 | NULL, N_("passed to 'git am'"), | |
845 | PARSE_OPT_NOARG), | |
846 | OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date", | |
847 | &options.git_am_opts, NULL, | |
848 | N_("passed to 'git am'"), PARSE_OPT_NOARG), | |
849 | OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL, | |
850 | N_("passed to 'git am'"), PARSE_OPT_NOARG), | |
851 | OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"), | |
852 | N_("passed to 'git apply'"), 0), | |
853 | OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts, | |
854 | N_("action"), N_("passed to 'git apply'"), 0), | |
1ed9c14f PK |
855 | OPT_BIT('f', "force-rebase", &options.flags, |
856 | N_("cherry-pick all commits, even if unchanged"), | |
857 | REBASE_FORCE), | |
858 | OPT_BIT(0, "no-ff", &options.flags, | |
859 | N_("cherry-pick all commits, even if unchanged"), | |
860 | REBASE_FORCE), | |
f9573628 PK |
861 | OPT_CMDMODE(0, "continue", &action, N_("continue"), |
862 | ACTION_CONTINUE), | |
122420c2 PK |
863 | OPT_CMDMODE(0, "skip", &action, |
864 | N_("skip current patch and continue"), ACTION_SKIP), | |
5e5d9619 PK |
865 | OPT_CMDMODE(0, "abort", &action, |
866 | N_("abort and check out the original branch"), | |
867 | ACTION_ABORT), | |
5a614945 PK |
868 | OPT_CMDMODE(0, "quit", &action, |
869 | N_("abort but keep HEAD where it is"), ACTION_QUIT), | |
51e9ea6d PK |
870 | OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list " |
871 | "during an interactive rebase"), ACTION_EDIT_TODO), | |
872 | OPT_CMDMODE(0, "show-current-patch", &action, | |
873 | N_("show the patch file being applied or merged"), | |
874 | ACTION_SHOW_CURRENT_PATCH), | |
361badd3 PK |
875 | { OPTION_CALLBACK, 'm', "merge", &options, NULL, |
876 | N_("use merging strategies to rebase"), | |
877 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
878 | parse_opt_merge }, | |
879 | { OPTION_CALLBACK, 'i', "interactive", &options, NULL, | |
880 | N_("let the user edit the list of commits to rebase"), | |
881 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
882 | parse_opt_interactive }, | |
883 | OPT_SET_INT('p', "preserve-merges", &options.type, | |
884 | N_("try to recreate merges instead of ignoring " | |
885 | "them"), REBASE_PRESERVE_MERGES), | |
ead98c11 PK |
886 | OPT_BOOL(0, "rerere-autoupdate", |
887 | &options.allow_rerere_autoupdate, | |
eff199a6 | 888 | N_("allow rerere to update index with resolved " |
ead98c11 | 889 | "conflict")), |
002ee2fe PK |
890 | OPT_BOOL('k', "keep-empty", &options.keep_empty, |
891 | N_("preserve empty commits during rebase")), | |
051910a9 PK |
892 | OPT_BOOL(0, "autosquash", &options.autosquash, |
893 | N_("move commits that begin with " | |
894 | "squash!/fixup! under -i")), | |
12026a41 PK |
895 | { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"), |
896 | N_("GPG-sign commits"), | |
897 | PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, | |
6defce2b PK |
898 | OPT_BOOL(0, "autostash", &options.autostash, |
899 | N_("automatically stash/stash pop before and after")), | |
68e46d78 PK |
900 | OPT_STRING_LIST('x', "exec", &exec, N_("exec"), |
901 | N_("add exec lines after each commit of the " | |
902 | "editable list")), | |
9b3a448b PK |
903 | OPT_BOOL(0, "allow-empty-message", |
904 | &options.allow_empty_message, | |
905 | N_("allow rebasing commits with empty messages")), | |
3c3588c7 PK |
906 | {OPTION_STRING, 'r', "rebase-merges", &rebase_merges, |
907 | N_("mode"), | |
908 | N_("try to rebase merges instead of skipping them"), | |
909 | PARSE_OPT_OPTARG, NULL, (intptr_t)""}, | |
92d0d74e PK |
910 | OPT_BOOL(0, "fork-point", &fork_point, |
911 | N_("use 'merge-base --fork-point' to refine upstream")), | |
ba1905a5 PK |
912 | OPT_STRING('s', "strategy", &options.strategy, |
913 | N_("strategy"), N_("use the given merge strategy")), | |
914 | OPT_STRING_LIST('X', "strategy-option", &strategy_options, | |
915 | N_("option"), | |
916 | N_("pass the argument through to the merge " | |
917 | "strategy")), | |
9dba809a PK |
918 | OPT_BOOL(0, "root", &options.root, |
919 | N_("rebase all reachable commits up to the root(s)")), | |
f28d40d3 PK |
920 | OPT_END(), |
921 | }; | |
f5769680 | 922 | int i; |
ac7f467f | 923 | |
55071ea2 PK |
924 | /* |
925 | * NEEDSWORK: Once the builtin rebase has been tested enough | |
926 | * and git-legacy-rebase.sh is retired to contrib/, this preamble | |
927 | * can be removed. | |
928 | */ | |
929 | ||
930 | if (!use_builtin_rebase()) { | |
931 | const char *path = mkpath("%s/git-legacy-rebase", | |
932 | git_exec_path()); | |
933 | ||
934 | if (sane_execvp(path, (char **)argv) < 0) | |
935 | die_errno(_("could not exec %s"), path); | |
936 | else | |
937 | BUG("sane_execvp() returned???"); | |
938 | } | |
939 | ||
f28d40d3 PK |
940 | if (argc == 2 && !strcmp(argv[1], "-h")) |
941 | usage_with_options(builtin_rebase_usage, | |
942 | builtin_rebase_options); | |
943 | ||
55071ea2 PK |
944 | prefix = setup_git_directory(); |
945 | trace_repo_setup(prefix); | |
946 | setup_work_tree(); | |
947 | ||
bff014da PK |
948 | git_config(rebase_config, &options); |
949 | ||
0eabf4b9 PK |
950 | strbuf_reset(&buf); |
951 | strbuf_addf(&buf, "%s/applying", apply_dir()); | |
952 | if(file_exists(buf.buf)) | |
953 | die(_("It looks like 'git am' is in progress. Cannot rebase.")); | |
954 | ||
c54dacb5 PK |
955 | if (is_directory(apply_dir())) { |
956 | options.type = REBASE_AM; | |
957 | options.state_dir = apply_dir(); | |
958 | } else if (is_directory(merge_dir())) { | |
959 | strbuf_reset(&buf); | |
960 | strbuf_addf(&buf, "%s/rewritten", merge_dir()); | |
961 | if (is_directory(buf.buf)) { | |
962 | options.type = REBASE_PRESERVE_MERGES; | |
963 | options.flags |= REBASE_INTERACTIVE_EXPLICIT; | |
964 | } else { | |
965 | strbuf_reset(&buf); | |
966 | strbuf_addf(&buf, "%s/interactive", merge_dir()); | |
967 | if(file_exists(buf.buf)) { | |
968 | options.type = REBASE_INTERACTIVE; | |
969 | options.flags |= REBASE_INTERACTIVE_EXPLICIT; | |
970 | } else | |
971 | options.type = REBASE_MERGE; | |
972 | } | |
973 | options.state_dir = merge_dir(); | |
974 | } | |
975 | ||
976 | if (options.type != REBASE_UNSPECIFIED) | |
977 | in_progress = 1; | |
978 | ||
f9573628 | 979 | total_argc = argc; |
f28d40d3 PK |
980 | argc = parse_options(argc, argv, prefix, |
981 | builtin_rebase_options, | |
982 | builtin_rebase_usage, 0); | |
983 | ||
f9573628 PK |
984 | if (action != NO_ACTION && total_argc != 2) { |
985 | usage_with_options(builtin_rebase_usage, | |
986 | builtin_rebase_options); | |
987 | } | |
988 | ||
f28d40d3 PK |
989 | if (argc > 2) |
990 | usage_with_options(builtin_rebase_usage, | |
991 | builtin_rebase_options); | |
ac7f467f | 992 | |
d732a570 PK |
993 | if (action != NO_ACTION && !in_progress) |
994 | die(_("No rebase in progress?")); | |
13a5a9f0 | 995 | setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0); |
d732a570 | 996 | |
51e9ea6d PK |
997 | if (action == ACTION_EDIT_TODO && !is_interactive(&options)) |
998 | die(_("The --edit-todo action can only be used during " | |
999 | "interactive rebase.")); | |
1000 | ||
f9573628 PK |
1001 | switch (action) { |
1002 | case ACTION_CONTINUE: { | |
1003 | struct object_id head; | |
1004 | struct lock_file lock_file = LOCK_INIT; | |
1005 | int fd; | |
1006 | ||
1007 | options.action = "continue"; | |
13a5a9f0 | 1008 | set_reflog_action(&options); |
f9573628 PK |
1009 | |
1010 | /* Sanity check */ | |
1011 | if (get_oid("HEAD", &head)) | |
1012 | die(_("Cannot read HEAD")); | |
1013 | ||
1014 | fd = hold_locked_index(&lock_file, 0); | |
1015 | if (read_index(the_repository->index) < 0) | |
1016 | die(_("could not read index")); | |
1017 | refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, | |
1018 | NULL); | |
1019 | if (0 <= fd) | |
1020 | update_index_if_able(the_repository->index, | |
1021 | &lock_file); | |
1022 | rollback_lock_file(&lock_file); | |
1023 | ||
1024 | if (has_unstaged_changes(1)) { | |
1025 | puts(_("You must edit all merge conflicts and then\n" | |
1026 | "mark them as resolved using git add")); | |
1027 | exit(1); | |
1028 | } | |
1029 | if (read_basic_state(&options)) | |
1030 | exit(1); | |
1031 | goto run_rebase; | |
1032 | } | |
122420c2 PK |
1033 | case ACTION_SKIP: { |
1034 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
1035 | ||
1036 | options.action = "skip"; | |
13a5a9f0 | 1037 | set_reflog_action(&options); |
122420c2 PK |
1038 | |
1039 | rerere_clear(&merge_rr); | |
1040 | string_list_clear(&merge_rr, 1); | |
1041 | ||
bac2a1e3 JS |
1042 | if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD, |
1043 | NULL, NULL) < 0) | |
122420c2 | 1044 | die(_("could not discard worktree changes")); |
5aec9271 | 1045 | remove_branch_state(); |
122420c2 PK |
1046 | if (read_basic_state(&options)) |
1047 | exit(1); | |
1048 | goto run_rebase; | |
1049 | } | |
5e5d9619 PK |
1050 | case ACTION_ABORT: { |
1051 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
1052 | options.action = "abort"; | |
13a5a9f0 | 1053 | set_reflog_action(&options); |
5e5d9619 PK |
1054 | |
1055 | rerere_clear(&merge_rr); | |
1056 | string_list_clear(&merge_rr, 1); | |
1057 | ||
1058 | if (read_basic_state(&options)) | |
1059 | exit(1); | |
1060 | if (reset_head(&options.orig_head, "reset", | |
bac2a1e3 JS |
1061 | options.head_name, RESET_HEAD_HARD, |
1062 | NULL, NULL) < 0) | |
5e5d9619 PK |
1063 | die(_("could not move back to %s"), |
1064 | oid_to_hex(&options.orig_head)); | |
5aec9271 | 1065 | remove_branch_state(); |
5e5d9619 PK |
1066 | ret = finish_rebase(&options); |
1067 | goto cleanup; | |
1068 | } | |
5a614945 PK |
1069 | case ACTION_QUIT: { |
1070 | strbuf_reset(&buf); | |
1071 | strbuf_addstr(&buf, options.state_dir); | |
1072 | ret = !!remove_dir_recursively(&buf, 0); | |
1073 | if (ret) | |
1074 | die(_("could not remove '%s'"), options.state_dir); | |
1075 | goto cleanup; | |
1076 | } | |
51e9ea6d PK |
1077 | case ACTION_EDIT_TODO: |
1078 | options.action = "edit-todo"; | |
1079 | options.dont_finish_rebase = 1; | |
1080 | goto run_rebase; | |
1081 | case ACTION_SHOW_CURRENT_PATCH: | |
1082 | options.action = "show-current-patch"; | |
1083 | options.dont_finish_rebase = 1; | |
1084 | goto run_rebase; | |
1085 | case NO_ACTION: | |
1086 | break; | |
f9573628 | 1087 | default: |
51e9ea6d | 1088 | BUG("action: %d", action); |
f9573628 PK |
1089 | } |
1090 | ||
c54dacb5 PK |
1091 | /* Make sure no rebase is in progress */ |
1092 | if (in_progress) { | |
1093 | const char *last_slash = strrchr(options.state_dir, '/'); | |
1094 | const char *state_dir_base = | |
1095 | last_slash ? last_slash + 1 : options.state_dir; | |
1096 | const char *cmd_live_rebase = | |
1097 | "git rebase (--continue | --abort | --skip)"; | |
1098 | strbuf_reset(&buf); | |
1099 | strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir); | |
1100 | die(_("It seems that there is already a %s directory, and\n" | |
1101 | "I wonder if you are in the middle of another rebase. " | |
1102 | "If that is the\n" | |
1103 | "case, please try\n\t%s\n" | |
1104 | "If that is not the case, please\n\t%s\n" | |
1105 | "and run me again. I am stopping in case you still " | |
1106 | "have something\n" | |
1107 | "valuable there.\n"), | |
1108 | state_dir_base, cmd_live_rebase, buf.buf); | |
1109 | } | |
1110 | ||
f5769680 | 1111 | for (i = 0; i < options.git_am_opts.argc; i++) { |
04519d72 | 1112 | const char *option = options.git_am_opts.argv[i], *p; |
f5769680 JS |
1113 | if (!strcmp(option, "--committer-date-is-author-date") || |
1114 | !strcmp(option, "--ignore-date") || | |
1115 | !strcmp(option, "--whitespace=fix") || | |
1116 | !strcmp(option, "--whitespace=strip")) | |
1117 | options.flags |= REBASE_FORCE; | |
04519d72 JS |
1118 | else if (skip_prefix(option, "-C", &p)) { |
1119 | while (*p) | |
1120 | if (!isdigit(*(p++))) | |
1121 | die(_("switch `C' expects a " | |
1122 | "numerical value")); | |
1123 | } else if (skip_prefix(option, "--whitespace=", &p)) { | |
1124 | if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") && | |
1125 | strcmp(p, "error") && strcmp(p, "error-all")) | |
1126 | die("Invalid whitespace option: '%s'", p); | |
1127 | } | |
38dbcef2 PK |
1128 | } |
1129 | ||
f5769680 JS |
1130 | if (!(options.flags & REBASE_NO_QUIET)) |
1131 | argv_array_push(&options.git_am_opts, "-q"); | |
53f9e5be | 1132 | |
002ee2fe PK |
1133 | if (options.keep_empty) |
1134 | imply_interactive(&options, "--keep-empty"); | |
1135 | ||
12026a41 PK |
1136 | if (gpg_sign) { |
1137 | free(options.gpg_sign_opt); | |
1138 | options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign); | |
1139 | } | |
1140 | ||
68e46d78 PK |
1141 | if (exec.nr) { |
1142 | int i; | |
1143 | ||
1144 | imply_interactive(&options, "--exec"); | |
1145 | ||
1146 | strbuf_reset(&buf); | |
1147 | for (i = 0; i < exec.nr; i++) | |
1148 | strbuf_addf(&buf, "exec %s\n", exec.items[i].string); | |
1149 | options.cmd = xstrdup(buf.buf); | |
1150 | } | |
1151 | ||
3c3588c7 PK |
1152 | if (rebase_merges) { |
1153 | if (!*rebase_merges) | |
1154 | ; /* default mode; do nothing */ | |
1155 | else if (!strcmp("rebase-cousins", rebase_merges)) | |
1156 | options.rebase_cousins = 1; | |
1157 | else if (strcmp("no-rebase-cousins", rebase_merges)) | |
1158 | die(_("Unknown mode: %s"), rebase_merges); | |
1159 | options.rebase_merges = 1; | |
1160 | imply_interactive(&options, "--rebase-merges"); | |
1161 | } | |
1162 | ||
ba1905a5 PK |
1163 | if (strategy_options.nr) { |
1164 | int i; | |
1165 | ||
1166 | if (!options.strategy) | |
1167 | options.strategy = "recursive"; | |
1168 | ||
1169 | strbuf_reset(&buf); | |
1170 | for (i = 0; i < strategy_options.nr; i++) | |
1171 | strbuf_addf(&buf, " --%s", | |
1172 | strategy_options.items[i].string); | |
1173 | options.strategy_opts = xstrdup(buf.buf); | |
1174 | } | |
1175 | ||
1176 | if (options.strategy) { | |
1177 | options.strategy = xstrdup(options.strategy); | |
1178 | switch (options.type) { | |
1179 | case REBASE_AM: | |
1180 | die(_("--strategy requires --merge or --interactive")); | |
1181 | case REBASE_MERGE: | |
1182 | case REBASE_INTERACTIVE: | |
1183 | case REBASE_PRESERVE_MERGES: | |
1184 | /* compatible */ | |
1185 | break; | |
1186 | case REBASE_UNSPECIFIED: | |
1187 | options.type = REBASE_MERGE; | |
1188 | break; | |
1189 | default: | |
1190 | BUG("unhandled rebase type (%d)", options.type); | |
1191 | } | |
1192 | } | |
1193 | ||
9dba809a PK |
1194 | if (options.root && !options.onto_name) |
1195 | imply_interactive(&options, "--root without --onto"); | |
1196 | ||
cda614e4 PK |
1197 | if (isatty(2) && options.flags & REBASE_NO_QUIET) |
1198 | strbuf_addstr(&options.git_format_patch_opt, " --progress"); | |
1199 | ||
ac7f467f PK |
1200 | switch (options.type) { |
1201 | case REBASE_MERGE: | |
1202 | case REBASE_INTERACTIVE: | |
1203 | case REBASE_PRESERVE_MERGES: | |
1204 | options.state_dir = merge_dir(); | |
1205 | break; | |
1206 | case REBASE_AM: | |
1207 | options.state_dir = apply_dir(); | |
1208 | break; | |
1209 | default: | |
1210 | /* the default rebase backend is `--am` */ | |
1211 | options.type = REBASE_AM; | |
1212 | options.state_dir = apply_dir(); | |
1213 | break; | |
1214 | } | |
1215 | ||
f5769680 | 1216 | if (options.git_am_opts.argc) { |
b361bd75 | 1217 | /* all am options except -q are compatible only with --am */ |
f5769680 JS |
1218 | for (i = options.git_am_opts.argc - 1; i >= 0; i--) |
1219 | if (strcmp(options.git_am_opts.argv[i], "-q")) | |
1220 | break; | |
b361bd75 | 1221 | |
f5769680 | 1222 | if (is_interactive(&options) && i >= 0) |
72ee6731 EN |
1223 | die(_("cannot combine am options " |
1224 | "with interactive options")); | |
f5769680 | 1225 | if (options.type == REBASE_MERGE && i >= 0) |
72ee6731 | 1226 | die(_("cannot combine am options with merge options ")); |
b361bd75 PK |
1227 | } |
1228 | ||
73d51ed0 PK |
1229 | if (options.signoff) { |
1230 | if (options.type == REBASE_PRESERVE_MERGES) | |
1231 | die("cannot combine '--signoff' with " | |
1232 | "'--preserve-merges'"); | |
f5769680 | 1233 | argv_array_push(&options.git_am_opts, "--signoff"); |
73d51ed0 PK |
1234 | options.flags |= REBASE_FORCE; |
1235 | } | |
1236 | ||
b361bd75 PK |
1237 | if (options.type == REBASE_PRESERVE_MERGES) |
1238 | /* | |
1239 | * Note: incompatibility with --signoff handled in signoff block above | |
1240 | * Note: incompatibility with --interactive is just a strong warning; | |
1241 | * git-rebase.txt caveats with "unless you know what you are doing" | |
1242 | */ | |
1243 | if (options.rebase_merges) | |
c913c596 | 1244 | die(_("cannot combine '--preserve-merges' with " |
b361bd75 PK |
1245 | "'--rebase-merges'")); |
1246 | ||
1247 | if (options.rebase_merges) { | |
1248 | if (strategy_options.nr) | |
c913c596 | 1249 | die(_("cannot combine '--rebase-merges' with " |
b361bd75 PK |
1250 | "'--strategy-option'")); |
1251 | if (options.strategy) | |
c913c596 | 1252 | die(_("cannot combine '--rebase-merges' with " |
b361bd75 PK |
1253 | "'--strategy'")); |
1254 | } | |
1255 | ||
ac7f467f | 1256 | if (!options.root) { |
8f5986d9 PK |
1257 | if (argc < 1) { |
1258 | struct branch *branch; | |
1259 | ||
1260 | branch = branch_get(NULL); | |
1261 | options.upstream_name = branch_get_upstream(branch, | |
1262 | NULL); | |
1263 | if (!options.upstream_name) | |
1264 | error_on_missing_default_upstream(); | |
1265 | if (fork_point < 0) | |
1266 | fork_point = 1; | |
1267 | } else { | |
f28d40d3 | 1268 | options.upstream_name = argv[0]; |
ac7f467f PK |
1269 | argc--; |
1270 | argv++; | |
1271 | if (!strcmp(options.upstream_name, "-")) | |
1272 | options.upstream_name = "@{-1}"; | |
1273 | } | |
1274 | options.upstream = peel_committish(options.upstream_name); | |
1275 | if (!options.upstream) | |
1276 | die(_("invalid upstream '%s'"), options.upstream_name); | |
06e4775a | 1277 | options.upstream_arg = options.upstream_name; |
9dba809a PK |
1278 | } else { |
1279 | if (!options.onto_name) { | |
1280 | if (commit_tree("", 0, the_hash_algo->empty_tree, NULL, | |
1281 | &squash_onto, NULL, NULL) < 0) | |
1282 | die(_("Could not create new root commit")); | |
1283 | options.squash_onto = &squash_onto; | |
1284 | options.onto_name = squash_onto_name = | |
1285 | xstrdup(oid_to_hex(&squash_onto)); | |
1286 | } | |
1287 | options.upstream_name = NULL; | |
1288 | options.upstream = NULL; | |
1289 | if (argc > 1) | |
1290 | usage_with_options(builtin_rebase_usage, | |
1291 | builtin_rebase_options); | |
1292 | options.upstream_arg = "--root"; | |
1293 | } | |
ac7f467f PK |
1294 | |
1295 | /* Make sure the branch to rebase onto is valid. */ | |
1296 | if (!options.onto_name) | |
1297 | options.onto_name = options.upstream_name; | |
1298 | if (strstr(options.onto_name, "...")) { | |
075bc852 PK |
1299 | if (get_oid_mb(options.onto_name, &merge_base) < 0) |
1300 | die(_("'%s': need exactly one merge base"), | |
1301 | options.onto_name); | |
1302 | options.onto = lookup_commit_or_die(&merge_base, | |
1303 | options.onto_name); | |
ac7f467f PK |
1304 | } else { |
1305 | options.onto = peel_committish(options.onto_name); | |
1306 | if (!options.onto) | |
1307 | die(_("Does not point to a valid commit '%s'"), | |
1308 | options.onto_name); | |
1309 | } | |
1310 | ||
1311 | /* | |
1312 | * If the branch to rebase is given, that is the branch we will rebase | |
1313 | * branch_name -- branch/commit being rebased, or | |
1314 | * HEAD (already detached) | |
1315 | * orig_head -- commit object name of tip of the branch before rebasing | |
d4c569f8 | 1316 | * head_name -- refs/heads/<that-branch> or NULL (detached HEAD) |
ac7f467f | 1317 | */ |
e65123a7 PK |
1318 | if (argc == 1) { |
1319 | /* Is it "rebase other branchname" or "rebase other commit"? */ | |
1320 | branch_name = argv[0]; | |
1321 | options.switch_to = argv[0]; | |
1322 | ||
1323 | /* Is it a local branch? */ | |
1324 | strbuf_reset(&buf); | |
1325 | strbuf_addf(&buf, "refs/heads/%s", branch_name); | |
1326 | if (!read_ref(buf.buf, &options.orig_head)) | |
1327 | options.head_name = xstrdup(buf.buf); | |
1328 | /* If not is it a valid ref (branch or commit)? */ | |
1329 | else if (!get_oid(branch_name, &options.orig_head)) | |
1330 | options.head_name = NULL; | |
1331 | else | |
1332 | die(_("fatal: no such branch/commit '%s'"), | |
1333 | branch_name); | |
1334 | } else if (argc == 0) { | |
ac7f467f PK |
1335 | /* Do not need to switch branches, we are already on it. */ |
1336 | options.head_name = | |
1337 | xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL, | |
1338 | &flags)); | |
1339 | if (!options.head_name) | |
1340 | die(_("No such ref: %s"), "HEAD"); | |
1341 | if (flags & REF_ISSYMREF) { | |
1342 | if (!skip_prefix(options.head_name, | |
1343 | "refs/heads/", &branch_name)) | |
1344 | branch_name = options.head_name; | |
1345 | ||
1346 | } else { | |
d4c569f8 PK |
1347 | free(options.head_name); |
1348 | options.head_name = NULL; | |
ac7f467f PK |
1349 | branch_name = "HEAD"; |
1350 | } | |
1351 | if (get_oid("HEAD", &options.orig_head)) | |
1352 | die(_("Could not resolve HEAD to a revision")); | |
e65123a7 PK |
1353 | } else |
1354 | BUG("unexpected number of arguments left to parse"); | |
ac7f467f | 1355 | |
92d0d74e PK |
1356 | if (fork_point > 0) { |
1357 | struct commit *head = | |
1358 | lookup_commit_reference(the_repository, | |
1359 | &options.orig_head); | |
1360 | options.restrict_revision = | |
1361 | get_fork_point(options.upstream_name, head); | |
1362 | } | |
1363 | ||
e0333e5c PK |
1364 | if (read_index(the_repository->index) < 0) |
1365 | die(_("could not read index")); | |
1366 | ||
6defce2b PK |
1367 | if (options.autostash) { |
1368 | struct lock_file lock_file = LOCK_INIT; | |
1369 | int fd; | |
1370 | ||
1371 | fd = hold_locked_index(&lock_file, 0); | |
1372 | refresh_cache(REFRESH_QUIET); | |
1373 | if (0 <= fd) | |
1374 | update_index_if_able(&the_index, &lock_file); | |
1375 | rollback_lock_file(&lock_file); | |
1376 | ||
ffae8b2f | 1377 | if (has_unstaged_changes(1) || has_uncommitted_changes(1)) { |
6defce2b PK |
1378 | const char *autostash = |
1379 | state_dir_path("autostash", &options); | |
1380 | struct child_process stash = CHILD_PROCESS_INIT; | |
1381 | struct object_id oid; | |
1382 | struct commit *head = | |
1383 | lookup_commit_reference(the_repository, | |
1384 | &options.orig_head); | |
1385 | ||
1386 | argv_array_pushl(&stash.args, | |
1387 | "stash", "create", "autostash", NULL); | |
1388 | stash.git_cmd = 1; | |
1389 | stash.no_stdin = 1; | |
1390 | strbuf_reset(&buf); | |
1391 | if (capture_command(&stash, &buf, GIT_MAX_HEXSZ)) | |
1392 | die(_("Cannot autostash")); | |
1393 | strbuf_trim_trailing_newline(&buf); | |
1394 | if (get_oid(buf.buf, &oid)) | |
1395 | die(_("Unexpected stash response: '%s'"), | |
1396 | buf.buf); | |
1397 | strbuf_reset(&buf); | |
1398 | strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV); | |
1399 | ||
1400 | if (safe_create_leading_directories_const(autostash)) | |
1401 | die(_("Could not create directory for '%s'"), | |
1402 | options.state_dir); | |
12aeb00a | 1403 | write_file(autostash, "%s", oid_to_hex(&oid)); |
6defce2b PK |
1404 | printf(_("Created autostash: %s\n"), buf.buf); |
1405 | if (reset_head(&head->object.oid, "reset --hard", | |
bac2a1e3 | 1406 | NULL, RESET_HEAD_HARD, NULL, NULL) < 0) |
6defce2b PK |
1407 | die(_("could not reset --hard")); |
1408 | printf(_("HEAD is now at %s"), | |
1409 | find_unique_abbrev(&head->object.oid, | |
1410 | DEFAULT_ABBREV)); | |
1411 | strbuf_reset(&buf); | |
1412 | pp_commit_easy(CMIT_FMT_ONELINE, head, &buf); | |
1413 | if (buf.len > 0) | |
1414 | printf(" %s", buf.buf); | |
1415 | putchar('\n'); | |
1416 | ||
1417 | if (discard_index(the_repository->index) < 0 || | |
1418 | read_index(the_repository->index) < 0) | |
1419 | die(_("could not read index")); | |
1420 | } | |
1421 | } | |
1422 | ||
e0333e5c PK |
1423 | if (require_clean_work_tree("rebase", |
1424 | _("Please commit or stash them."), 1, 1)) { | |
1425 | ret = 1; | |
1426 | goto cleanup; | |
ac7f467f PK |
1427 | } |
1428 | ||
9a48a615 PK |
1429 | /* |
1430 | * Now we are rebasing commits upstream..orig_head (or with --root, | |
1431 | * everything leading up to orig_head) on top of onto. | |
1432 | */ | |
1433 | ||
1434 | /* | |
1435 | * Check if we are already based on onto with linear history, | |
1436 | * but this should be done only when upstream and onto are the same | |
1437 | * and if this is not an interactive rebase. | |
1438 | */ | |
1439 | if (can_fast_forward(options.onto, &options.orig_head, &merge_base) && | |
1440 | !is_interactive(&options) && !options.restrict_revision && | |
9dba809a | 1441 | options.upstream && |
9a48a615 PK |
1442 | !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) { |
1443 | int flag; | |
1444 | ||
1ed9c14f | 1445 | if (!(options.flags & REBASE_FORCE)) { |
e65123a7 PK |
1446 | /* Lazily switch to the target branch if needed... */ |
1447 | if (options.switch_to) { | |
1448 | struct object_id oid; | |
1449 | ||
1450 | if (get_oid(options.switch_to, &oid) < 0) { | |
1451 | ret = !!error(_("could not parse '%s'"), | |
1452 | options.switch_to); | |
1453 | goto cleanup; | |
1454 | } | |
1455 | ||
1456 | strbuf_reset(&buf); | |
13a5a9f0 JS |
1457 | strbuf_addf(&buf, "%s: checkout %s", |
1458 | getenv(GIT_REFLOG_ACTION_ENVIRONMENT), | |
e65123a7 PK |
1459 | options.switch_to); |
1460 | if (reset_head(&oid, "checkout", | |
fa443d40 | 1461 | options.head_name, 0, |
13a5a9f0 | 1462 | NULL, buf.buf) < 0) { |
e65123a7 PK |
1463 | ret = !!error(_("could not switch to " |
1464 | "%s"), | |
1465 | options.switch_to); | |
1466 | goto cleanup; | |
1467 | } | |
1468 | } | |
1469 | ||
1ed9c14f PK |
1470 | if (!(options.flags & REBASE_NO_QUIET)) |
1471 | ; /* be quiet */ | |
1472 | else if (!strcmp(branch_name, "HEAD") && | |
1473 | resolve_ref_unsafe("HEAD", 0, NULL, &flag)) | |
1474 | puts(_("HEAD is up to date.")); | |
1475 | else | |
1476 | printf(_("Current branch %s is up to date.\n"), | |
1477 | branch_name); | |
1478 | ret = !!finish_rebase(&options); | |
1479 | goto cleanup; | |
1480 | } else if (!(options.flags & REBASE_NO_QUIET)) | |
9a48a615 PK |
1481 | ; /* be quiet */ |
1482 | else if (!strcmp(branch_name, "HEAD") && | |
1483 | resolve_ref_unsafe("HEAD", 0, NULL, &flag)) | |
1484 | puts(_("HEAD is up to date, rebase forced.")); | |
1485 | else | |
1486 | printf(_("Current branch %s is up to date, rebase " | |
1487 | "forced.\n"), branch_name); | |
1488 | } | |
1489 | ||
06e4775a PK |
1490 | /* If a hook exists, give it a chance to interrupt*/ |
1491 | if (!ok_to_skip_pre_rebase && | |
1492 | run_hook_le(NULL, "pre-rebase", options.upstream_arg, | |
1493 | argc ? argv[0] : NULL, NULL)) | |
1494 | die(_("The pre-rebase hook refused to rebase.")); | |
1495 | ||
bff014da PK |
1496 | if (options.flags & REBASE_DIFFSTAT) { |
1497 | struct diff_options opts; | |
1498 | ||
8797f0f0 JS |
1499 | if (options.flags & REBASE_VERBOSE) { |
1500 | if (is_null_oid(&merge_base)) | |
1501 | printf(_("Changes to %s:\n"), | |
1502 | oid_to_hex(&options.onto->object.oid)); | |
1503 | else | |
1504 | printf(_("Changes from %s to %s:\n"), | |
1505 | oid_to_hex(&merge_base), | |
1506 | oid_to_hex(&options.onto->object.oid)); | |
1507 | } | |
bff014da PK |
1508 | |
1509 | /* We want color (if set), but no pager */ | |
1510 | diff_setup(&opts); | |
1511 | opts.stat_width = -1; /* use full terminal width */ | |
1512 | opts.stat_graph_width = -1; /* respect statGraphWidth config */ | |
1513 | opts.output_format |= | |
1514 | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
1515 | opts.detect_rename = DIFF_DETECT_RENAME; | |
1516 | diff_setup_done(&opts); | |
8797f0f0 JS |
1517 | diff_tree_oid(is_null_oid(&merge_base) ? |
1518 | the_hash_algo->empty_tree : &merge_base, | |
1519 | &options.onto->object.oid, "", &opts); | |
bff014da PK |
1520 | diffcore_std(&opts); |
1521 | diff_flush(&opts); | |
1522 | } | |
1523 | ||
361badd3 PK |
1524 | if (is_interactive(&options)) |
1525 | goto run_rebase; | |
1526 | ||
bff014da PK |
1527 | /* Detach HEAD and reset the tree */ |
1528 | if (options.flags & REBASE_NO_QUIET) | |
1529 | printf(_("First, rewinding head to replay your work on top of " | |
1530 | "it...\n")); | |
1531 | ||
13a5a9f0 JS |
1532 | strbuf_addf(&msg, "%s: checkout %s", |
1533 | getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name); | |
73d6d7b2 JS |
1534 | if (reset_head(&options.onto->object.oid, "checkout", NULL, |
1535 | RESET_HEAD_DETACH, NULL, msg.buf)) | |
ac7f467f PK |
1536 | die(_("Could not detach HEAD")); |
1537 | strbuf_release(&msg); | |
1538 | ||
7eecfa56 PK |
1539 | /* |
1540 | * If the onto is a proper descendant of the tip of the branch, then | |
1541 | * we just fast-forwarded. | |
1542 | */ | |
1543 | strbuf_reset(&msg); | |
1544 | if (!oidcmp(&merge_base, &options.orig_head)) { | |
eff199a6 | 1545 | printf(_("Fast-forwarded %s to %s.\n"), |
7eecfa56 PK |
1546 | branch_name, options.onto_name); |
1547 | strbuf_addf(&msg, "rebase finished: %s onto %s", | |
1548 | options.head_name ? options.head_name : "detached HEAD", | |
1549 | oid_to_hex(&options.onto->object.oid)); | |
1550 | reset_head(NULL, "Fast-forwarded", options.head_name, 0, | |
1551 | "HEAD", msg.buf); | |
1552 | strbuf_release(&msg); | |
1553 | ret = !!finish_rebase(&options); | |
1554 | goto cleanup; | |
1555 | } | |
1556 | ||
ac7f467f PK |
1557 | strbuf_addf(&revisions, "%s..%s", |
1558 | options.root ? oid_to_hex(&options.onto->object.oid) : | |
1559 | (options.restrict_revision ? | |
1560 | oid_to_hex(&options.restrict_revision->object.oid) : | |
1561 | oid_to_hex(&options.upstream->object.oid)), | |
1562 | oid_to_hex(&options.orig_head)); | |
1563 | ||
1564 | options.revisions = revisions.buf; | |
1565 | ||
f9573628 | 1566 | run_rebase: |
ac7f467f PK |
1567 | ret = !!run_specific_rebase(&options); |
1568 | ||
e0333e5c | 1569 | cleanup: |
ac7f467f PK |
1570 | strbuf_release(&revisions); |
1571 | free(options.head_name); | |
12026a41 | 1572 | free(options.gpg_sign_opt); |
68e46d78 | 1573 | free(options.cmd); |
9dba809a | 1574 | free(squash_onto_name); |
ac7f467f | 1575 | return ret; |
55071ea2 | 1576 | } |