]>
Commit | Line | Data |
---|---|---|
26ae337b | 1 | #include "cache.h" |
697cc8ef | 2 | #include "lockfile.h" |
26ae337b | 3 | #include "sequencer.h" |
26ae337b | 4 | #include "dir.h" |
043a4492 RR |
5 | #include "object.h" |
6 | #include "commit.h" | |
7 | #include "tag.h" | |
8 | #include "run-command.h" | |
9 | #include "exec_cmd.h" | |
10 | #include "utf8.h" | |
11 | #include "cache-tree.h" | |
12 | #include "diff.h" | |
13 | #include "revision.h" | |
14 | #include "rerere.h" | |
15 | #include "merge-recursive.h" | |
16 | #include "refs.h" | |
b27cfb0d | 17 | #include "argv-array.h" |
a1c75762 | 18 | #include "quote.h" |
967dfd4d | 19 | #include "trailer.h" |
56dc3ab0 | 20 | #include "log-tree.h" |
311af526 | 21 | #include "wt-status.h" |
043a4492 RR |
22 | |
23 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" | |
26ae337b | 24 | |
5ed75e2a | 25 | const char sign_off_header[] = "Signed-off-by: "; |
cd650a4e | 26 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
5ed75e2a | 27 | |
8a2a0f53 JS |
28 | GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
29 | ||
30 | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") | |
31 | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") | |
32 | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") | |
1e41229d | 33 | static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety") |
f932729c | 34 | |
84583957 JS |
35 | static GIT_PATH_FUNC(rebase_path, "rebase-merge") |
36 | /* | |
37 | * The file containing rebase commands, comments, and empty lines. | |
38 | * This file is created by "git rebase -i" then edited by the user. As | |
39 | * the lines are processed, they are removed from the front of this | |
40 | * file and written to the tail of 'done'. | |
41 | */ | |
42 | static GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") | |
1df6df0c JS |
43 | /* |
44 | * The rebase command lines that have already been processed. A line | |
45 | * is moved here when it is first handled, before any associated user | |
46 | * actions. | |
47 | */ | |
48 | static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done") | |
6e98de72 JS |
49 | /* |
50 | * The commit message that is planned to be used for any changes that | |
51 | * need to be committed following a user interaction. | |
52 | */ | |
53 | static GIT_PATH_FUNC(rebase_path_message, "rebase-merge/message") | |
54 | /* | |
55 | * The file into which is accumulated the suggested commit message for | |
56 | * squash/fixup commands. When the first of a series of squash/fixups | |
57 | * is seen, the file is created and the commit message from the | |
58 | * previous commit and from the first squash/fixup commit are written | |
59 | * to it. The commit message for each subsequent squash/fixup commit | |
60 | * is appended to the file as it is processed. | |
61 | * | |
62 | * The first line of the file is of the form | |
63 | * # This is a combination of $count commits. | |
64 | * where $count is the number of commits whose messages have been | |
65 | * written to the file so far (including the initial "pick" commit). | |
66 | * Each time that a commit message is processed, this line is read and | |
67 | * updated. It is deleted just before the combined commit is made. | |
68 | */ | |
69 | static GIT_PATH_FUNC(rebase_path_squash_msg, "rebase-merge/message-squash") | |
70 | /* | |
71 | * If the current series of squash/fixups has not yet included a squash | |
72 | * command, then this file exists and holds the commit message of the | |
73 | * original "pick" commit. (If the series ends without a "squash" | |
74 | * command, then this can be used as the commit message of the combined | |
75 | * commit without opening the editor.) | |
76 | */ | |
77 | static GIT_PATH_FUNC(rebase_path_fixup_msg, "rebase-merge/message-fixup") | |
b5a67045 JS |
78 | /* |
79 | * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and | |
80 | * GIT_AUTHOR_DATE that will be used for the commit that is currently | |
81 | * being rebased. | |
82 | */ | |
83 | static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") | |
56dc3ab0 JS |
84 | /* |
85 | * When an "edit" rebase command is being processed, the SHA1 of the | |
86 | * commit to be edited is recorded in this file. When "git rebase | |
87 | * --continue" is executed, if there are any staged changes then they | |
88 | * will be amended to the HEAD commit, but only provided the HEAD | |
89 | * commit is still the commit to be edited. When any other rebase | |
90 | * command is processed, this file is deleted. | |
91 | */ | |
92 | static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend") | |
93 | /* | |
94 | * When we stop at a given patch via the "edit" command, this file contains | |
95 | * the abbreviated commit name of the corresponding patch. | |
96 | */ | |
97 | static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha") | |
25cb8df9 JS |
98 | /* |
99 | * For the post-rewrite hook, we make a list of rewritten commits and | |
100 | * their new sha1s. The rewritten-pending list keeps the sha1s of | |
101 | * commits that have been processed, but not committed yet, | |
102 | * e.g. because they are waiting for a 'squash' command. | |
103 | */ | |
104 | static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list") | |
105 | static GIT_PATH_FUNC(rebase_path_rewritten_pending, | |
106 | "rebase-merge/rewritten-pending") | |
a1c75762 JS |
107 | /* |
108 | * The following files are written by git-rebase just after parsing the | |
109 | * command-line (and are only consumed, not modified, by the sequencer). | |
110 | */ | |
111 | static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") | |
556907f1 JS |
112 | static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") |
113 | static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") | |
4b83ce9f JS |
114 | static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") |
115 | static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") | |
796c7972 | 116 | static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") |
ca6c6b45 JS |
117 | static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy") |
118 | static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") | |
b5a67045 | 119 | |
b5a67045 JS |
120 | static inline int is_rebase_i(const struct replay_opts *opts) |
121 | { | |
84583957 | 122 | return opts->action == REPLAY_INTERACTIVE_REBASE; |
b5a67045 JS |
123 | } |
124 | ||
285abf56 JS |
125 | static const char *get_dir(const struct replay_opts *opts) |
126 | { | |
84583957 JS |
127 | if (is_rebase_i(opts)) |
128 | return rebase_path(); | |
285abf56 JS |
129 | return git_path_seq_dir(); |
130 | } | |
131 | ||
c0246501 JS |
132 | static const char *get_todo_path(const struct replay_opts *opts) |
133 | { | |
84583957 JS |
134 | if (is_rebase_i(opts)) |
135 | return rebase_path_todo(); | |
c0246501 JS |
136 | return git_path_todo_file(); |
137 | } | |
138 | ||
bab4d109 BC |
139 | /* |
140 | * Returns 0 for non-conforming footer | |
141 | * Returns 1 for conforming footer | |
142 | * Returns 2 when sob exists within conforming footer | |
143 | * Returns 3 when sob exists within conforming footer as last entry | |
144 | */ | |
145 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, | |
146 | int ignore_footer) | |
b971e04f | 147 | { |
967dfd4d JT |
148 | struct trailer_info info; |
149 | int i; | |
150 | int found_sob = 0, found_sob_last = 0; | |
b971e04f | 151 | |
967dfd4d | 152 | trailer_info_get(&info, sb->buf); |
b971e04f | 153 | |
967dfd4d | 154 | if (info.trailer_start == info.trailer_end) |
b971e04f BC |
155 | return 0; |
156 | ||
967dfd4d JT |
157 | for (i = 0; i < info.trailer_nr; i++) |
158 | if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { | |
159 | found_sob = 1; | |
160 | if (i == info.trailer_nr - 1) | |
161 | found_sob_last = 1; | |
162 | } | |
b971e04f | 163 | |
967dfd4d | 164 | trailer_info_release(&info); |
bab4d109 | 165 | |
967dfd4d | 166 | if (found_sob_last) |
bab4d109 BC |
167 | return 3; |
168 | if (found_sob) | |
169 | return 2; | |
b971e04f BC |
170 | return 1; |
171 | } | |
5ed75e2a | 172 | |
a1c75762 JS |
173 | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
174 | { | |
175 | static struct strbuf buf = STRBUF_INIT; | |
176 | ||
177 | strbuf_reset(&buf); | |
178 | if (opts->gpg_sign) | |
179 | sq_quotef(&buf, "-S%s", opts->gpg_sign); | |
180 | return buf.buf; | |
181 | } | |
182 | ||
2863584f | 183 | int sequencer_remove_state(struct replay_opts *opts) |
26ae337b | 184 | { |
285abf56 | 185 | struct strbuf dir = STRBUF_INIT; |
03a4e260 JS |
186 | int i; |
187 | ||
188 | free(opts->gpg_sign); | |
189 | free(opts->strategy); | |
190 | for (i = 0; i < opts->xopts_nr; i++) | |
191 | free(opts->xopts[i]); | |
192 | free(opts->xopts); | |
26ae337b | 193 | |
285abf56 JS |
194 | strbuf_addf(&dir, "%s", get_dir(opts)); |
195 | remove_dir_recursively(&dir, 0); | |
196 | strbuf_release(&dir); | |
2863584f JS |
197 | |
198 | return 0; | |
26ae337b | 199 | } |
043a4492 RR |
200 | |
201 | static const char *action_name(const struct replay_opts *opts) | |
202 | { | |
84583957 JS |
203 | switch (opts->action) { |
204 | case REPLAY_REVERT: | |
205 | return N_("revert"); | |
206 | case REPLAY_PICK: | |
207 | return N_("cherry-pick"); | |
208 | case REPLAY_INTERACTIVE_REBASE: | |
209 | return N_("rebase -i"); | |
210 | } | |
211 | die(_("Unknown action: %d"), opts->action); | |
043a4492 RR |
212 | } |
213 | ||
043a4492 RR |
214 | struct commit_message { |
215 | char *parent_label; | |
7b35eaf8 JK |
216 | char *label; |
217 | char *subject; | |
043a4492 RR |
218 | const char *message; |
219 | }; | |
220 | ||
39755964 JS |
221 | static const char *short_commit_name(struct commit *commit) |
222 | { | |
223 | return find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV); | |
224 | } | |
225 | ||
043a4492 RR |
226 | static int get_message(struct commit *commit, struct commit_message *out) |
227 | { | |
043a4492 | 228 | const char *abbrev, *subject; |
7b35eaf8 | 229 | int subject_len; |
043a4492 | 230 | |
7b35eaf8 | 231 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
39755964 | 232 | abbrev = short_commit_name(commit); |
043a4492 RR |
233 | |
234 | subject_len = find_commit_subject(out->message, &subject); | |
235 | ||
7b35eaf8 JK |
236 | out->subject = xmemdupz(subject, subject_len); |
237 | out->label = xstrfmt("%s... %s", abbrev, out->subject); | |
238 | out->parent_label = xstrfmt("parent of %s", out->label); | |
239 | ||
043a4492 RR |
240 | return 0; |
241 | } | |
242 | ||
d74a4e57 | 243 | static void free_message(struct commit *commit, struct commit_message *msg) |
043a4492 RR |
244 | { |
245 | free(msg->parent_label); | |
7b35eaf8 JK |
246 | free(msg->label); |
247 | free(msg->subject); | |
b66103c3 | 248 | unuse_commit_buffer(commit, msg->message); |
043a4492 RR |
249 | } |
250 | ||
ed727b19 | 251 | static void print_advice(int show_hint, struct replay_opts *opts) |
043a4492 RR |
252 | { |
253 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); | |
254 | ||
255 | if (msg) { | |
256 | fprintf(stderr, "%s\n", msg); | |
257 | /* | |
41ccfdd9 | 258 | * A conflict has occurred but the porcelain |
043a4492 RR |
259 | * (typically rebase --interactive) wants to take care |
260 | * of the commit itself so remove CHERRY_PICK_HEAD | |
261 | */ | |
f932729c | 262 | unlink(git_path_cherry_pick_head()); |
043a4492 RR |
263 | return; |
264 | } | |
265 | ||
ed727b19 PH |
266 | if (show_hint) { |
267 | if (opts->no_commit) | |
268 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
269 | "with 'git add <paths>' or 'git rm <paths>'")); | |
270 | else | |
271 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
272 | "with 'git add <paths>' or 'git rm <paths>'\n" | |
273 | "and commit the result with 'git commit'")); | |
274 | } | |
043a4492 RR |
275 | } |
276 | ||
f56fffef JS |
277 | static int write_message(const void *buf, size_t len, const char *filename, |
278 | int append_eol) | |
043a4492 RR |
279 | { |
280 | static struct lock_file msg_file; | |
281 | ||
4ef3d8f0 JS |
282 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
283 | if (msg_fd < 0) | |
93b3df6f | 284 | return error_errno(_("could not lock '%s'"), filename); |
75871495 | 285 | if (write_in_full(msg_fd, buf, len) < 0) { |
4f66c837 | 286 | rollback_lock_file(&msg_file); |
93b3df6f | 287 | return error_errno(_("could not write to '%s'"), filename); |
4f66c837 | 288 | } |
f56fffef JS |
289 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
290 | rollback_lock_file(&msg_file); | |
35871806 | 291 | return error_errno(_("could not write eol to '%s'"), filename); |
f56fffef | 292 | } |
4f66c837 JS |
293 | if (commit_lock_file(&msg_file) < 0) { |
294 | rollback_lock_file(&msg_file); | |
93b3df6f | 295 | return error(_("failed to finalize '%s'."), filename); |
4f66c837 | 296 | } |
4ef3d8f0 JS |
297 | |
298 | return 0; | |
043a4492 RR |
299 | } |
300 | ||
1dfc84e9 JS |
301 | /* |
302 | * Reads a file that was presumably written by a shell script, i.e. with an | |
303 | * end-of-line marker that needs to be stripped. | |
304 | * | |
305 | * Note that only the last end-of-line marker is stripped, consistent with the | |
306 | * behavior of "$(cat path)" in a shell script. | |
307 | * | |
308 | * Returns 1 if the file was read, 0 if it could not be read or does not exist. | |
309 | */ | |
310 | static int read_oneliner(struct strbuf *buf, | |
311 | const char *path, int skip_if_empty) | |
312 | { | |
313 | int orig_len = buf->len; | |
314 | ||
315 | if (!file_exists(path)) | |
316 | return 0; | |
317 | ||
318 | if (strbuf_read_file(buf, path, 0) < 0) { | |
319 | warning_errno(_("could not read '%s'"), path); | |
320 | return 0; | |
321 | } | |
322 | ||
323 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { | |
324 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') | |
325 | --buf->len; | |
326 | buf->buf[buf->len] = '\0'; | |
327 | } | |
328 | ||
329 | if (skip_if_empty && buf->len == orig_len) | |
330 | return 0; | |
331 | ||
332 | return 1; | |
333 | } | |
334 | ||
043a4492 RR |
335 | static struct tree *empty_tree(void) |
336 | { | |
cba595bd | 337 | return lookup_tree(EMPTY_TREE_SHA1_BIN); |
043a4492 RR |
338 | } |
339 | ||
340 | static int error_dirty_index(struct replay_opts *opts) | |
341 | { | |
342 | if (read_cache_unmerged()) | |
c28cbc5e | 343 | return error_resolve_conflict(_(action_name(opts))); |
043a4492 | 344 | |
93b3df6f | 345 | error(_("your local changes would be overwritten by %s."), |
c28cbc5e | 346 | _(action_name(opts))); |
043a4492 RR |
347 | |
348 | if (advice_commit_before_merge) | |
93b3df6f | 349 | advise(_("commit your changes or stash them to proceed.")); |
043a4492 RR |
350 | return -1; |
351 | } | |
352 | ||
1e41229d SB |
353 | static void update_abort_safety_file(void) |
354 | { | |
355 | struct object_id head; | |
356 | ||
357 | /* Do nothing on a single-pick */ | |
358 | if (!file_exists(git_path_seq_dir())) | |
359 | return; | |
360 | ||
361 | if (!get_oid("HEAD", &head)) | |
362 | write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head)); | |
363 | else | |
364 | write_file(git_path_abort_safety_file(), "%s", ""); | |
365 | } | |
366 | ||
334ae397 | 367 | static int fast_forward_to(const unsigned char *to, const unsigned char *from, |
eb4be1cb | 368 | int unborn, struct replay_opts *opts) |
043a4492 | 369 | { |
d668d16c | 370 | struct ref_transaction *transaction; |
eb4be1cb | 371 | struct strbuf sb = STRBUF_INIT; |
d668d16c | 372 | struct strbuf err = STRBUF_INIT; |
043a4492 RR |
373 | |
374 | read_cache(); | |
db699a8a | 375 | if (checkout_fast_forward(from, to, 1)) |
0e408fc3 | 376 | return -1; /* the callee should have complained already */ |
651ab9f5 | 377 | |
c28cbc5e | 378 | strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts))); |
d668d16c RS |
379 | |
380 | transaction = ref_transaction_begin(&err); | |
381 | if (!transaction || | |
382 | ref_transaction_update(transaction, "HEAD", | |
383 | to, unborn ? null_sha1 : from, | |
1d147bdf | 384 | 0, sb.buf, &err) || |
db7516ab | 385 | ref_transaction_commit(transaction, &err)) { |
d668d16c RS |
386 | ref_transaction_free(transaction); |
387 | error("%s", err.buf); | |
388 | strbuf_release(&sb); | |
389 | strbuf_release(&err); | |
390 | return -1; | |
391 | } | |
651ab9f5 | 392 | |
eb4be1cb | 393 | strbuf_release(&sb); |
d668d16c RS |
394 | strbuf_release(&err); |
395 | ref_transaction_free(transaction); | |
1e41229d | 396 | update_abort_safety_file(); |
d668d16c | 397 | return 0; |
043a4492 RR |
398 | } |
399 | ||
75c961b7 JH |
400 | void append_conflicts_hint(struct strbuf *msgbuf) |
401 | { | |
402 | int i; | |
403 | ||
261f315b JH |
404 | strbuf_addch(msgbuf, '\n'); |
405 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); | |
75c961b7 JH |
406 | for (i = 0; i < active_nr;) { |
407 | const struct cache_entry *ce = active_cache[i++]; | |
408 | if (ce_stage(ce)) { | |
261f315b | 409 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
75c961b7 JH |
410 | while (i < active_nr && !strcmp(ce->name, |
411 | active_cache[i]->name)) | |
412 | i++; | |
413 | } | |
414 | } | |
415 | } | |
416 | ||
043a4492 RR |
417 | static int do_recursive_merge(struct commit *base, struct commit *next, |
418 | const char *base_label, const char *next_label, | |
419 | unsigned char *head, struct strbuf *msgbuf, | |
420 | struct replay_opts *opts) | |
421 | { | |
422 | struct merge_options o; | |
423 | struct tree *result, *next_tree, *base_tree, *head_tree; | |
03b86647 | 424 | int clean; |
03a4e260 | 425 | char **xopt; |
043a4492 RR |
426 | static struct lock_file index_lock; |
427 | ||
b3e83cc7 | 428 | hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); |
043a4492 RR |
429 | |
430 | read_cache(); | |
431 | ||
432 | init_merge_options(&o); | |
433 | o.ancestor = base ? base_label : "(empty tree)"; | |
434 | o.branch1 = "HEAD"; | |
435 | o.branch2 = next ? next_label : "(empty tree)"; | |
436 | ||
437 | head_tree = parse_tree_indirect(head); | |
438 | next_tree = next ? next->tree : empty_tree(); | |
439 | base_tree = base ? base->tree : empty_tree(); | |
440 | ||
441 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) | |
442 | parse_merge_opt(&o, *xopt); | |
443 | ||
444 | clean = merge_trees(&o, | |
445 | head_tree, | |
446 | next_tree, base_tree, &result); | |
548009c0 | 447 | strbuf_release(&o.obuf); |
f241ff0d JS |
448 | if (clean < 0) |
449 | return clean; | |
043a4492 RR |
450 | |
451 | if (active_cache_changed && | |
03b86647 | 452 | write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
84583957 JS |
453 | /* TRANSLATORS: %s will be "revert", "cherry-pick" or |
454 | * "rebase -i". | |
455 | */ | |
c527b55e | 456 | return error(_("%s: Unable to write new index file"), |
c28cbc5e | 457 | _(action_name(opts))); |
043a4492 RR |
458 | rollback_lock_file(&index_lock); |
459 | ||
5ed75e2a | 460 | if (opts->signoff) |
bab4d109 | 461 | append_signoff(msgbuf, 0, 0); |
5ed75e2a | 462 | |
75c961b7 JH |
463 | if (!clean) |
464 | append_conflicts_hint(msgbuf); | |
043a4492 RR |
465 | |
466 | return !clean; | |
467 | } | |
468 | ||
b27cfb0d NH |
469 | static int is_index_unchanged(void) |
470 | { | |
471 | unsigned char head_sha1[20]; | |
472 | struct commit *head_commit; | |
473 | ||
7695d118 | 474 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL)) |
93b3df6f | 475 | return error(_("could not resolve HEAD commit\n")); |
b27cfb0d NH |
476 | |
477 | head_commit = lookup_commit(head_sha1); | |
4b580061 NH |
478 | |
479 | /* | |
480 | * If head_commit is NULL, check_commit, called from | |
481 | * lookup_commit, would have indicated that head_commit is not | |
482 | * a commit object already. parse_commit() will return failure | |
483 | * without further complaints in such a case. Otherwise, if | |
484 | * the commit is invalid, parse_commit() will complain. So | |
485 | * there is nothing for us to say here. Just return failure. | |
486 | */ | |
487 | if (parse_commit(head_commit)) | |
488 | return -1; | |
b27cfb0d NH |
489 | |
490 | if (!active_cache_tree) | |
491 | active_cache_tree = cache_tree(); | |
492 | ||
493 | if (!cache_tree_fully_valid(active_cache_tree)) | |
d0cfc3e8 | 494 | if (cache_tree_update(&the_index, 0)) |
93b3df6f | 495 | return error(_("unable to update cache tree\n")); |
b27cfb0d | 496 | |
ed1c9977 | 497 | return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash); |
b27cfb0d NH |
498 | } |
499 | ||
0473f28a JS |
500 | static int write_author_script(const char *message) |
501 | { | |
502 | struct strbuf buf = STRBUF_INIT; | |
503 | const char *eol; | |
504 | int res; | |
505 | ||
506 | for (;;) | |
507 | if (!*message || starts_with(message, "\n")) { | |
508 | missing_author: | |
509 | /* Missing 'author' line? */ | |
510 | unlink(rebase_path_author_script()); | |
511 | return 0; | |
512 | } else if (skip_prefix(message, "author ", &message)) | |
513 | break; | |
514 | else if ((eol = strchr(message, '\n'))) | |
515 | message = eol + 1; | |
516 | else | |
517 | goto missing_author; | |
518 | ||
519 | strbuf_addstr(&buf, "GIT_AUTHOR_NAME='"); | |
520 | while (*message && *message != '\n' && *message != '\r') | |
521 | if (skip_prefix(message, " <", &message)) | |
522 | break; | |
523 | else if (*message != '\'') | |
524 | strbuf_addch(&buf, *(message++)); | |
525 | else | |
526 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); | |
527 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='"); | |
528 | while (*message && *message != '\n' && *message != '\r') | |
529 | if (skip_prefix(message, "> ", &message)) | |
530 | break; | |
531 | else if (*message != '\'') | |
532 | strbuf_addch(&buf, *(message++)); | |
533 | else | |
534 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); | |
535 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@"); | |
536 | while (*message && *message != '\n' && *message != '\r') | |
537 | if (*message != '\'') | |
538 | strbuf_addch(&buf, *(message++)); | |
539 | else | |
540 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); | |
541 | res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1); | |
542 | strbuf_release(&buf); | |
543 | return res; | |
544 | } | |
545 | ||
b5a67045 JS |
546 | /* |
547 | * Read the author-script file into an environment block, ready for use in | |
548 | * run_command(), that can be free()d afterwards. | |
549 | */ | |
550 | static char **read_author_script(void) | |
551 | { | |
552 | struct strbuf script = STRBUF_INIT; | |
553 | int i, count = 0; | |
554 | char *p, *p2, **env; | |
555 | size_t env_size; | |
556 | ||
557 | if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0) | |
558 | return NULL; | |
559 | ||
560 | for (p = script.buf; *p; p++) | |
561 | if (skip_prefix(p, "'\\\\''", (const char **)&p2)) | |
562 | strbuf_splice(&script, p - script.buf, p2 - p, "'", 1); | |
563 | else if (*p == '\'') | |
564 | strbuf_splice(&script, p-- - script.buf, 1, "", 0); | |
565 | else if (*p == '\n') { | |
566 | *p = '\0'; | |
567 | count++; | |
568 | } | |
569 | ||
570 | env_size = (count + 1) * sizeof(*env); | |
571 | strbuf_grow(&script, env_size); | |
572 | memmove(script.buf + env_size, script.buf, script.len); | |
573 | p = script.buf + env_size; | |
574 | env = (char **)strbuf_detach(&script, NULL); | |
575 | ||
576 | for (i = 0; i < count; i++) { | |
577 | env[i] = p; | |
578 | p += strlen(p) + 1; | |
579 | } | |
580 | env[count] = NULL; | |
581 | ||
582 | return env; | |
583 | } | |
584 | ||
791eb870 JS |
585 | static const char staged_changes_advice[] = |
586 | N_("you have staged changes in your working tree\n" | |
587 | "If these changes are meant to be squashed into the previous commit, run:\n" | |
588 | "\n" | |
589 | " git commit --amend %s\n" | |
590 | "\n" | |
591 | "If they are meant to go into a new commit, run:\n" | |
592 | "\n" | |
593 | " git commit %s\n" | |
594 | "\n" | |
595 | "In both cases, once you're done, continue with:\n" | |
596 | "\n" | |
597 | " git rebase --continue\n"); | |
598 | ||
043a4492 RR |
599 | /* |
600 | * If we are cherry-pick, and if the merge did not result in | |
601 | * hand-editing, we will hit this commit and inherit the original | |
602 | * author date and name. | |
b5a67045 | 603 | * |
043a4492 RR |
604 | * If we are revert, or if our cherry-pick results in a hand merge, |
605 | * we had better say that the current user is responsible for that. | |
b5a67045 JS |
606 | * |
607 | * An exception is when run_git_commit() is called during an | |
608 | * interactive rebase: in that case, we will want to retain the | |
609 | * author metadata. | |
043a4492 | 610 | */ |
ac2b0e8f | 611 | static int run_git_commit(const char *defmsg, struct replay_opts *opts, |
0009426d JS |
612 | int allow_empty, int edit, int amend, |
613 | int cleanup_commit_message) | |
043a4492 | 614 | { |
b5a67045 | 615 | char **env = NULL; |
b27cfb0d NH |
616 | struct argv_array array; |
617 | int rc; | |
17d65f03 | 618 | const char *value; |
b27cfb0d | 619 | |
b5a67045 JS |
620 | if (is_rebase_i(opts)) { |
621 | env = read_author_script(); | |
a1c75762 JS |
622 | if (!env) { |
623 | const char *gpg_opt = gpg_sign_opt_quoted(opts); | |
624 | ||
791eb870 JS |
625 | return error(_(staged_changes_advice), |
626 | gpg_opt, gpg_opt); | |
a1c75762 | 627 | } |
b5a67045 JS |
628 | } |
629 | ||
b27cfb0d NH |
630 | argv_array_init(&array); |
631 | argv_array_push(&array, "commit"); | |
632 | argv_array_push(&array, "-n"); | |
043a4492 | 633 | |
9240beda JS |
634 | if (amend) |
635 | argv_array_push(&array, "--amend"); | |
3bdd5522 JK |
636 | if (opts->gpg_sign) |
637 | argv_array_pushf(&array, "-S%s", opts->gpg_sign); | |
043a4492 | 638 | if (opts->signoff) |
b27cfb0d | 639 | argv_array_push(&array, "-s"); |
b5a67045 JS |
640 | if (defmsg) |
641 | argv_array_pushl(&array, "-F", defmsg, NULL); | |
0009426d JS |
642 | if (cleanup_commit_message) |
643 | argv_array_push(&array, "--cleanup=strip"); | |
a1c75762 | 644 | if (edit) |
b5a67045 | 645 | argv_array_push(&array, "-e"); |
0009426d JS |
646 | else if (!cleanup_commit_message && |
647 | !opts->signoff && !opts->record_origin && | |
b5a67045 JS |
648 | git_config_get_value("commit.cleanup", &value)) |
649 | argv_array_push(&array, "--cleanup=verbatim"); | |
b27cfb0d | 650 | |
ac2b0e8f | 651 | if (allow_empty) |
b27cfb0d | 652 | argv_array_push(&array, "--allow-empty"); |
df478b74 | 653 | |
4bee9584 CW |
654 | if (opts->allow_empty_message) |
655 | argv_array_push(&array, "--allow-empty-message"); | |
656 | ||
b5a67045 JS |
657 | rc = run_command_v_opt_cd_env(array.argv, RUN_GIT_CMD, NULL, |
658 | (const char *const *)env); | |
b27cfb0d | 659 | argv_array_clear(&array); |
b5a67045 JS |
660 | free(env); |
661 | ||
b27cfb0d NH |
662 | return rc; |
663 | } | |
664 | ||
665 | static int is_original_commit_empty(struct commit *commit) | |
666 | { | |
667 | const unsigned char *ptree_sha1; | |
668 | ||
669 | if (parse_commit(commit)) | |
93b3df6f | 670 | return error(_("could not parse commit %s\n"), |
f2fd0760 | 671 | oid_to_hex(&commit->object.oid)); |
b27cfb0d NH |
672 | if (commit->parents) { |
673 | struct commit *parent = commit->parents->item; | |
674 | if (parse_commit(parent)) | |
93b3df6f | 675 | return error(_("could not parse parent commit %s\n"), |
f2fd0760 | 676 | oid_to_hex(&parent->object.oid)); |
ed1c9977 | 677 | ptree_sha1 = parent->tree->object.oid.hash; |
b27cfb0d NH |
678 | } else { |
679 | ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */ | |
043a4492 | 680 | } |
043a4492 | 681 | |
ed1c9977 | 682 | return !hashcmp(ptree_sha1, commit->tree->object.oid.hash); |
043a4492 RR |
683 | } |
684 | ||
ac2b0e8f JH |
685 | /* |
686 | * Do we run "git commit" with "--allow-empty"? | |
687 | */ | |
688 | static int allow_empty(struct replay_opts *opts, struct commit *commit) | |
689 | { | |
690 | int index_unchanged, empty_commit; | |
691 | ||
692 | /* | |
693 | * Three cases: | |
694 | * | |
695 | * (1) we do not allow empty at all and error out. | |
696 | * | |
697 | * (2) we allow ones that were initially empty, but | |
698 | * forbid the ones that become empty; | |
699 | * | |
700 | * (3) we allow both. | |
701 | */ | |
702 | if (!opts->allow_empty) | |
703 | return 0; /* let "git commit" barf as necessary */ | |
704 | ||
705 | index_unchanged = is_index_unchanged(); | |
706 | if (index_unchanged < 0) | |
707 | return index_unchanged; | |
708 | if (!index_unchanged) | |
709 | return 0; /* we do not have to say --allow-empty */ | |
710 | ||
711 | if (opts->keep_redundant_commits) | |
712 | return 1; | |
713 | ||
714 | empty_commit = is_original_commit_empty(commit); | |
715 | if (empty_commit < 0) | |
716 | return empty_commit; | |
717 | if (!empty_commit) | |
718 | return 0; | |
719 | else | |
720 | return 1; | |
721 | } | |
722 | ||
25c43667 JS |
723 | /* |
724 | * Note that ordering matters in this enum. Not only must it match the mapping | |
725 | * below, it is also divided into several sections that matter. When adding | |
726 | * new commands, make sure you add it in the right section. | |
727 | */ | |
004fefa7 | 728 | enum todo_command { |
25c43667 | 729 | /* commands that handle commits */ |
004fefa7 | 730 | TODO_PICK = 0, |
25c43667 | 731 | TODO_REVERT, |
56dc3ab0 | 732 | TODO_EDIT, |
04efc8b5 | 733 | TODO_REWORD, |
6e98de72 JS |
734 | TODO_FIXUP, |
735 | TODO_SQUASH, | |
311af526 JS |
736 | /* commands that do something else than handling a single commit */ |
737 | TODO_EXEC, | |
25c43667 | 738 | /* commands that do nothing but are counted for reporting progress */ |
b3fdd581 JS |
739 | TODO_NOOP, |
740 | TODO_DROP | |
004fefa7 JS |
741 | }; |
742 | ||
414697a9 JS |
743 | static struct { |
744 | char c; | |
745 | const char *str; | |
746 | } todo_command_info[] = { | |
747 | { 'p', "pick" }, | |
748 | { 0, "revert" }, | |
749 | { 'e', "edit" }, | |
04efc8b5 | 750 | { 'r', "reword" }, |
414697a9 JS |
751 | { 'f', "fixup" }, |
752 | { 's', "squash" }, | |
753 | { 'x', "exec" }, | |
b3fdd581 JS |
754 | { 0, "noop" }, |
755 | { 'd', "drop" } | |
004fefa7 JS |
756 | }; |
757 | ||
758 | static const char *command_to_string(const enum todo_command command) | |
759 | { | |
414697a9 JS |
760 | if ((size_t)command < ARRAY_SIZE(todo_command_info)) |
761 | return todo_command_info[command].str; | |
004fefa7 JS |
762 | die("Unknown command: %d", command); |
763 | } | |
764 | ||
25c43667 JS |
765 | static int is_noop(const enum todo_command command) |
766 | { | |
b3fdd581 | 767 | return TODO_NOOP <= command; |
25c43667 | 768 | } |
004fefa7 | 769 | |
6e98de72 JS |
770 | static int is_fixup(enum todo_command command) |
771 | { | |
772 | return command == TODO_FIXUP || command == TODO_SQUASH; | |
773 | } | |
774 | ||
775 | static int update_squash_messages(enum todo_command command, | |
776 | struct commit *commit, struct replay_opts *opts) | |
777 | { | |
778 | struct strbuf buf = STRBUF_INIT; | |
779 | int count, res; | |
780 | const char *message, *body; | |
781 | ||
782 | if (file_exists(rebase_path_squash_msg())) { | |
783 | struct strbuf header = STRBUF_INIT; | |
784 | char *eol, *p; | |
785 | ||
786 | if (strbuf_read_file(&buf, rebase_path_squash_msg(), 2048) <= 0) | |
787 | return error(_("could not read '%s'"), | |
788 | rebase_path_squash_msg()); | |
789 | ||
790 | p = buf.buf + 1; | |
791 | eol = strchrnul(buf.buf, '\n'); | |
792 | if (buf.buf[0] != comment_line_char || | |
793 | (p += strcspn(p, "0123456789\n")) == eol) | |
794 | return error(_("unexpected 1st line of squash message:" | |
795 | "\n\n\t%.*s"), | |
796 | (int)(eol - buf.buf), buf.buf); | |
797 | count = strtol(p, NULL, 10); | |
798 | ||
799 | if (count < 1) | |
800 | return error(_("invalid 1st line of squash message:\n" | |
801 | "\n\t%.*s"), | |
802 | (int)(eol - buf.buf), buf.buf); | |
803 | ||
804 | strbuf_addf(&header, "%c ", comment_line_char); | |
805 | strbuf_addf(&header, | |
806 | _("This is a combination of %d commits."), ++count); | |
807 | strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len); | |
808 | strbuf_release(&header); | |
809 | } else { | |
810 | unsigned char head[20]; | |
811 | struct commit *head_commit; | |
812 | const char *head_message, *body; | |
813 | ||
814 | if (get_sha1("HEAD", head)) | |
815 | return error(_("need a HEAD to fixup")); | |
816 | if (!(head_commit = lookup_commit_reference(head))) | |
817 | return error(_("could not read HEAD")); | |
818 | if (!(head_message = get_commit_buffer(head_commit, NULL))) | |
819 | return error(_("could not read HEAD's commit message")); | |
820 | ||
821 | find_commit_subject(head_message, &body); | |
822 | if (write_message(body, strlen(body), | |
823 | rebase_path_fixup_msg(), 0)) { | |
824 | unuse_commit_buffer(head_commit, head_message); | |
825 | return error(_("cannot write '%s'"), | |
826 | rebase_path_fixup_msg()); | |
827 | } | |
828 | ||
829 | count = 2; | |
830 | strbuf_addf(&buf, "%c ", comment_line_char); | |
831 | strbuf_addf(&buf, _("This is a combination of %d commits."), | |
832 | count); | |
833 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
834 | strbuf_addstr(&buf, _("This is the 1st commit message:")); | |
835 | strbuf_addstr(&buf, "\n\n"); | |
836 | strbuf_addstr(&buf, body); | |
837 | ||
838 | unuse_commit_buffer(head_commit, head_message); | |
839 | } | |
840 | ||
841 | if (!(message = get_commit_buffer(commit, NULL))) | |
842 | return error(_("could not read commit message of %s"), | |
843 | oid_to_hex(&commit->object.oid)); | |
844 | find_commit_subject(message, &body); | |
845 | ||
846 | if (command == TODO_SQUASH) { | |
847 | unlink(rebase_path_fixup_msg()); | |
848 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
849 | strbuf_addf(&buf, _("This is the commit message #%d:"), count); | |
850 | strbuf_addstr(&buf, "\n\n"); | |
851 | strbuf_addstr(&buf, body); | |
852 | } else if (command == TODO_FIXUP) { | |
853 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
854 | strbuf_addf(&buf, _("The commit message #%d will be skipped:"), | |
855 | count); | |
856 | strbuf_addstr(&buf, "\n\n"); | |
857 | strbuf_add_commented_lines(&buf, body, strlen(body)); | |
858 | } else | |
859 | return error(_("unknown command: %d"), command); | |
860 | unuse_commit_buffer(commit, message); | |
861 | ||
862 | res = write_message(buf.buf, buf.len, rebase_path_squash_msg(), 0); | |
863 | strbuf_release(&buf); | |
864 | return res; | |
865 | } | |
866 | ||
25cb8df9 JS |
867 | static void flush_rewritten_pending(void) { |
868 | struct strbuf buf = STRBUF_INIT; | |
869 | unsigned char newsha1[20]; | |
870 | FILE *out; | |
871 | ||
872 | if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), 82) > 0 && | |
873 | !get_sha1("HEAD", newsha1) && | |
874 | (out = fopen(rebase_path_rewritten_list(), "a"))) { | |
875 | char *bol = buf.buf, *eol; | |
876 | ||
877 | while (*bol) { | |
878 | eol = strchrnul(bol, '\n'); | |
879 | fprintf(out, "%.*s %s\n", (int)(eol - bol), | |
880 | bol, sha1_to_hex(newsha1)); | |
881 | if (!*eol) | |
882 | break; | |
883 | bol = eol + 1; | |
884 | } | |
885 | fclose(out); | |
886 | unlink(rebase_path_rewritten_pending()); | |
887 | } | |
888 | strbuf_release(&buf); | |
889 | } | |
890 | ||
891 | static void record_in_rewritten(struct object_id *oid, | |
892 | enum todo_command next_command) { | |
893 | FILE *out = fopen(rebase_path_rewritten_pending(), "a"); | |
894 | ||
895 | if (!out) | |
896 | return; | |
897 | ||
898 | fprintf(out, "%s\n", oid_to_hex(oid)); | |
899 | fclose(out); | |
900 | ||
901 | if (!is_fixup(next_command)) | |
902 | flush_rewritten_pending(); | |
903 | } | |
904 | ||
004fefa7 | 905 | static int do_pick_commit(enum todo_command command, struct commit *commit, |
6e98de72 | 906 | struct replay_opts *opts, int final_fixup) |
043a4492 | 907 | { |
6e98de72 JS |
908 | int edit = opts->edit, cleanup_commit_message = 0; |
909 | const char *msg_file = edit ? NULL : git_path_merge_msg(); | |
043a4492 RR |
910 | unsigned char head[20]; |
911 | struct commit *base, *next, *parent; | |
912 | const char *base_label, *next_label; | |
d74a4e57 | 913 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
043a4492 | 914 | struct strbuf msgbuf = STRBUF_INIT; |
bcbb68be | 915 | int res, unborn = 0, amend = 0, allow = 0; |
043a4492 RR |
916 | |
917 | if (opts->no_commit) { | |
918 | /* | |
919 | * We do not intend to commit immediately. We just want to | |
920 | * merge the differences in, so let's compute the tree | |
921 | * that represents the "current" state for merge-recursive | |
922 | * to work on. | |
923 | */ | |
924 | if (write_cache_as_tree(head, 0, NULL)) | |
93b3df6f | 925 | return error(_("your index file is unmerged.")); |
043a4492 | 926 | } else { |
334ae397 MZ |
927 | unborn = get_sha1("HEAD", head); |
928 | if (unborn) | |
929 | hashcpy(head, EMPTY_TREE_SHA1_BIN); | |
018ec3c8 | 930 | if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0, 0)) |
043a4492 RR |
931 | return error_dirty_index(opts); |
932 | } | |
933 | discard_cache(); | |
934 | ||
637666c8 | 935 | if (!commit->parents) |
043a4492 | 936 | parent = NULL; |
043a4492 RR |
937 | else if (commit->parents->next) { |
938 | /* Reverting or cherry-picking a merge commit */ | |
939 | int cnt; | |
940 | struct commit_list *p; | |
941 | ||
942 | if (!opts->mainline) | |
93b3df6f | 943 | return error(_("commit %s is a merge but no -m option was given."), |
f2fd0760 | 944 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
945 | |
946 | for (cnt = 1, p = commit->parents; | |
947 | cnt != opts->mainline && p; | |
948 | cnt++) | |
949 | p = p->next; | |
950 | if (cnt != opts->mainline || !p) | |
93b3df6f | 951 | return error(_("commit %s does not have parent %d"), |
f2fd0760 | 952 | oid_to_hex(&commit->object.oid), opts->mainline); |
043a4492 RR |
953 | parent = p->item; |
954 | } else if (0 < opts->mainline) | |
93b3df6f | 955 | return error(_("mainline was specified but commit %s is not a merge."), |
f2fd0760 | 956 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
957 | else |
958 | parent = commit->parents->item; | |
959 | ||
bcbb68be JS |
960 | if (get_message(commit, &msg) != 0) |
961 | return error(_("cannot get commit message for %s"), | |
962 | oid_to_hex(&commit->object.oid)); | |
963 | ||
6e98de72 | 964 | if (opts->allow_ff && !is_fixup(command) && |
ed1c9977 | 965 | ((parent && !hashcmp(parent->object.oid.hash, head)) || |
bcbb68be JS |
966 | (!parent && unborn))) { |
967 | if (is_rebase_i(opts)) | |
968 | write_author_script(msg.message); | |
969 | res = fast_forward_to(commit->object.oid.hash, head, unborn, | |
970 | opts); | |
971 | if (res || command != TODO_REWORD) | |
972 | goto leave; | |
973 | edit = amend = 1; | |
974 | msg_file = NULL; | |
975 | goto fast_forward_edit; | |
976 | } | |
043a4492 | 977 | if (parent && parse_commit(parent) < 0) |
004fefa7 JS |
978 | /* TRANSLATORS: The first %s will be a "todo" command like |
979 | "revert" or "pick", the second %s a SHA1. */ | |
043a4492 | 980 | return error(_("%s: cannot parse parent commit %s"), |
004fefa7 JS |
981 | command_to_string(command), |
982 | oid_to_hex(&parent->object.oid)); | |
043a4492 | 983 | |
043a4492 RR |
984 | /* |
985 | * "commit" is an existing commit. We would want to apply | |
986 | * the difference it introduces since its first parent "prev" | |
987 | * on top of the current HEAD if we are cherry-pick. Or the | |
988 | * reverse of it if we are revert. | |
989 | */ | |
990 | ||
004fefa7 | 991 | if (command == TODO_REVERT) { |
043a4492 RR |
992 | base = commit; |
993 | base_label = msg.label; | |
994 | next = parent; | |
995 | next_label = msg.parent_label; | |
996 | strbuf_addstr(&msgbuf, "Revert \""); | |
997 | strbuf_addstr(&msgbuf, msg.subject); | |
998 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); | |
f2fd0760 | 999 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1000 | |
1001 | if (commit->parents && commit->parents->next) { | |
1002 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); | |
f2fd0760 | 1003 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
043a4492 RR |
1004 | } |
1005 | strbuf_addstr(&msgbuf, ".\n"); | |
1006 | } else { | |
1007 | const char *p; | |
1008 | ||
1009 | base = parent; | |
1010 | base_label = msg.parent_label; | |
1011 | next = commit; | |
1012 | next_label = msg.label; | |
1013 | ||
23aa5142 JS |
1014 | /* Append the commit log message to msgbuf. */ |
1015 | if (find_commit_subject(msg.message, &p)) | |
1016 | strbuf_addstr(&msgbuf, p); | |
043a4492 RR |
1017 | |
1018 | if (opts->record_origin) { | |
bab4d109 | 1019 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
b971e04f | 1020 | strbuf_addch(&msgbuf, '\n'); |
cd650a4e | 1021 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
f2fd0760 | 1022 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1023 | strbuf_addstr(&msgbuf, ")\n"); |
1024 | } | |
1025 | } | |
1026 | ||
04efc8b5 JS |
1027 | if (command == TODO_REWORD) |
1028 | edit = 1; | |
1029 | else if (is_fixup(command)) { | |
6e98de72 JS |
1030 | if (update_squash_messages(command, commit, opts)) |
1031 | return -1; | |
1032 | amend = 1; | |
1033 | if (!final_fixup) | |
1034 | msg_file = rebase_path_squash_msg(); | |
1035 | else if (file_exists(rebase_path_fixup_msg())) { | |
1036 | cleanup_commit_message = 1; | |
1037 | msg_file = rebase_path_fixup_msg(); | |
1038 | } else { | |
1039 | const char *dest = git_path("SQUASH_MSG"); | |
1040 | unlink(dest); | |
1041 | if (copy_file(dest, rebase_path_squash_msg(), 0666)) | |
1042 | return error(_("could not rename '%s' to '%s'"), | |
1043 | rebase_path_squash_msg(), dest); | |
1044 | unlink(git_path("MERGE_MSG")); | |
1045 | msg_file = dest; | |
1046 | edit = 1; | |
1047 | } | |
1048 | } | |
1049 | ||
0473f28a JS |
1050 | if (is_rebase_i(opts) && write_author_script(msg.message) < 0) |
1051 | res = -1; | |
1052 | else if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) { | |
043a4492 RR |
1053 | res = do_recursive_merge(base, next, base_label, next_label, |
1054 | head, &msgbuf, opts); | |
f241ff0d JS |
1055 | if (res < 0) |
1056 | return res; | |
75871495 | 1057 | res |= write_message(msgbuf.buf, msgbuf.len, |
f56fffef | 1058 | git_path_merge_msg(), 0); |
043a4492 RR |
1059 | } else { |
1060 | struct commit_list *common = NULL; | |
1061 | struct commit_list *remotes = NULL; | |
1062 | ||
75871495 | 1063 | res = write_message(msgbuf.buf, msgbuf.len, |
f56fffef | 1064 | git_path_merge_msg(), 0); |
043a4492 RR |
1065 | |
1066 | commit_list_insert(base, &common); | |
1067 | commit_list_insert(next, &remotes); | |
03a4e260 JS |
1068 | res |= try_merge_command(opts->strategy, |
1069 | opts->xopts_nr, (const char **)opts->xopts, | |
043a4492 RR |
1070 | common, sha1_to_hex(head), remotes); |
1071 | free_commit_list(common); | |
1072 | free_commit_list(remotes); | |
1073 | } | |
452202c7 | 1074 | strbuf_release(&msgbuf); |
043a4492 RR |
1075 | |
1076 | /* | |
1077 | * If the merge was clean or if it failed due to conflict, we write | |
1078 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. | |
1079 | * However, if the merge did not even start, then we don't want to | |
1080 | * write it at all. | |
1081 | */ | |
004fefa7 | 1082 | if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) && |
dbfad033 JS |
1083 | update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL, |
1084 | REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) | |
1085 | res = -1; | |
004fefa7 | 1086 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
dbfad033 JS |
1087 | update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL, |
1088 | REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) | |
1089 | res = -1; | |
043a4492 RR |
1090 | |
1091 | if (res) { | |
004fefa7 | 1092 | error(command == TODO_REVERT |
043a4492 RR |
1093 | ? _("could not revert %s... %s") |
1094 | : _("could not apply %s... %s"), | |
39755964 | 1095 | short_commit_name(commit), msg.subject); |
ed727b19 | 1096 | print_advice(res == 1, opts); |
043a4492 | 1097 | rerere(opts->allow_rerere_auto); |
c8d1351d | 1098 | goto leave; |
043a4492 RR |
1099 | } |
1100 | ||
c8d1351d | 1101 | allow = allow_empty(opts, commit); |
706728a3 FC |
1102 | if (allow < 0) { |
1103 | res = allow; | |
1104 | goto leave; | |
1105 | } | |
c8d1351d | 1106 | if (!opts->no_commit) |
bcbb68be | 1107 | fast_forward_edit: |
6e98de72 JS |
1108 | res = run_git_commit(msg_file, opts, allow, edit, amend, |
1109 | cleanup_commit_message); | |
1110 | ||
1111 | if (!res && final_fixup) { | |
1112 | unlink(rebase_path_fixup_msg()); | |
1113 | unlink(rebase_path_squash_msg()); | |
1114 | } | |
c8d1351d FC |
1115 | |
1116 | leave: | |
d74a4e57 | 1117 | free_message(commit, &msg); |
1e41229d | 1118 | update_abort_safety_file(); |
043a4492 RR |
1119 | |
1120 | return res; | |
1121 | } | |
1122 | ||
c3e8618c | 1123 | static int prepare_revs(struct replay_opts *opts) |
043a4492 | 1124 | { |
a73e22e9 MZ |
1125 | /* |
1126 | * picking (but not reverting) ranges (but not individual revisions) | |
1127 | * should be done in reverse | |
1128 | */ | |
1129 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) | |
043a4492 RR |
1130 | opts->revs->reverse ^= 1; |
1131 | ||
1132 | if (prepare_revision_walk(opts->revs)) | |
c3e8618c | 1133 | return error(_("revision walk setup failed")); |
043a4492 RR |
1134 | |
1135 | if (!opts->revs->commits) | |
c3e8618c JS |
1136 | return error(_("empty commit set passed")); |
1137 | return 0; | |
043a4492 RR |
1138 | } |
1139 | ||
0d9c6dc9 | 1140 | static int read_and_refresh_cache(struct replay_opts *opts) |
043a4492 RR |
1141 | { |
1142 | static struct lock_file index_lock; | |
1143 | int index_fd = hold_locked_index(&index_lock, 0); | |
49fb937e JS |
1144 | if (read_index_preload(&the_index, NULL) < 0) { |
1145 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 1146 | return error(_("git %s: failed to read the index"), |
c28cbc5e | 1147 | _(action_name(opts))); |
49fb937e | 1148 | } |
043a4492 | 1149 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
33c297aa | 1150 | if (the_index.cache_changed && index_fd >= 0) { |
49fb937e JS |
1151 | if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) { |
1152 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 1153 | return error(_("git %s: failed to refresh the index"), |
c28cbc5e | 1154 | _(action_name(opts))); |
49fb937e | 1155 | } |
043a4492 RR |
1156 | } |
1157 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 1158 | return 0; |
043a4492 RR |
1159 | } |
1160 | ||
004fefa7 JS |
1161 | struct todo_item { |
1162 | enum todo_command command; | |
1163 | struct commit *commit; | |
c22f7dfb JS |
1164 | const char *arg; |
1165 | int arg_len; | |
004fefa7 JS |
1166 | size_t offset_in_buf; |
1167 | }; | |
1168 | ||
1169 | struct todo_list { | |
1170 | struct strbuf buf; | |
1171 | struct todo_item *items; | |
1172 | int nr, alloc, current; | |
1173 | }; | |
1174 | ||
1175 | #define TODO_LIST_INIT { STRBUF_INIT } | |
1176 | ||
1177 | static void todo_list_release(struct todo_list *todo_list) | |
043a4492 | 1178 | { |
004fefa7 JS |
1179 | strbuf_release(&todo_list->buf); |
1180 | free(todo_list->items); | |
1181 | todo_list->items = NULL; | |
1182 | todo_list->nr = todo_list->alloc = 0; | |
1183 | } | |
043a4492 | 1184 | |
004fefa7 JS |
1185 | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
1186 | { | |
1187 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); | |
1188 | return todo_list->items + todo_list->nr++; | |
043a4492 RR |
1189 | } |
1190 | ||
004fefa7 | 1191 | static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) |
043a4492 RR |
1192 | { |
1193 | unsigned char commit_sha1[20]; | |
043a4492 | 1194 | char *end_of_object_name; |
004fefa7 JS |
1195 | int i, saved, status, padding; |
1196 | ||
8f8550b3 JS |
1197 | /* left-trim */ |
1198 | bol += strspn(bol, " \t"); | |
1199 | ||
25c43667 JS |
1200 | if (bol == eol || *bol == '\r' || *bol == comment_line_char) { |
1201 | item->command = TODO_NOOP; | |
1202 | item->commit = NULL; | |
1203 | item->arg = bol; | |
1204 | item->arg_len = eol - bol; | |
1205 | return 0; | |
1206 | } | |
1207 | ||
414697a9 JS |
1208 | for (i = 0; i < ARRAY_SIZE(todo_command_info); i++) |
1209 | if (skip_prefix(bol, todo_command_info[i].str, &bol)) { | |
1210 | item->command = i; | |
1211 | break; | |
1212 | } else if (bol[1] == ' ' && *bol == todo_command_info[i].c) { | |
1213 | bol++; | |
004fefa7 JS |
1214 | item->command = i; |
1215 | break; | |
1216 | } | |
414697a9 | 1217 | if (i >= ARRAY_SIZE(todo_command_info)) |
004fefa7 | 1218 | return -1; |
043a4492 | 1219 | |
25c43667 JS |
1220 | if (item->command == TODO_NOOP) { |
1221 | item->commit = NULL; | |
1222 | item->arg = bol; | |
1223 | item->arg_len = eol - bol; | |
1224 | return 0; | |
1225 | } | |
1226 | ||
043a4492 RR |
1227 | /* Eat up extra spaces/ tabs before object name */ |
1228 | padding = strspn(bol, " \t"); | |
1229 | if (!padding) | |
004fefa7 | 1230 | return -1; |
043a4492 RR |
1231 | bol += padding; |
1232 | ||
311af526 JS |
1233 | if (item->command == TODO_EXEC) { |
1234 | item->arg = bol; | |
1235 | item->arg_len = (int)(eol - bol); | |
1236 | return 0; | |
1237 | } | |
1238 | ||
004fefa7 | 1239 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
043a4492 RR |
1240 | saved = *end_of_object_name; |
1241 | *end_of_object_name = '\0'; | |
1242 | status = get_sha1(bol, commit_sha1); | |
1243 | *end_of_object_name = saved; | |
1244 | ||
c22f7dfb JS |
1245 | item->arg = end_of_object_name + strspn(end_of_object_name, " \t"); |
1246 | item->arg_len = (int)(eol - item->arg); | |
1247 | ||
043a4492 | 1248 | if (status < 0) |
004fefa7 | 1249 | return -1; |
043a4492 | 1250 | |
004fefa7 JS |
1251 | item->commit = lookup_commit_reference(commit_sha1); |
1252 | return !item->commit; | |
043a4492 RR |
1253 | } |
1254 | ||
004fefa7 | 1255 | static int parse_insn_buffer(char *buf, struct todo_list *todo_list) |
043a4492 | 1256 | { |
004fefa7 JS |
1257 | struct todo_item *item; |
1258 | char *p = buf, *next_p; | |
6e98de72 | 1259 | int i, res = 0, fixup_okay = file_exists(rebase_path_done()); |
043a4492 | 1260 | |
004fefa7 | 1261 | for (i = 1; *p; i++, p = next_p) { |
043a4492 | 1262 | char *eol = strchrnul(p, '\n'); |
004fefa7 JS |
1263 | |
1264 | next_p = *eol ? eol + 1 /* skip LF */ : eol; | |
1265 | ||
6307041d JS |
1266 | if (p != eol && eol[-1] == '\r') |
1267 | eol--; /* strip Carriage Return */ | |
1268 | ||
004fefa7 JS |
1269 | item = append_new_todo(todo_list); |
1270 | item->offset_in_buf = p - todo_list->buf.buf; | |
1271 | if (parse_insn_line(item, p, eol)) { | |
93b3df6f | 1272 | res = error(_("invalid line %d: %.*s"), |
004fefa7 | 1273 | i, (int)(eol - p), p); |
6e98de72 | 1274 | item->command = TODO_NOOP; |
004fefa7 | 1275 | } |
6e98de72 JS |
1276 | |
1277 | if (fixup_okay) | |
1278 | ; /* do nothing */ | |
1279 | else if (is_fixup(item->command)) | |
1280 | return error(_("cannot '%s' without a previous commit"), | |
1281 | command_to_string(item->command)); | |
1282 | else if (!is_noop(item->command)) | |
1283 | fixup_okay = 1; | |
043a4492 | 1284 | } |
52865279 | 1285 | |
004fefa7 | 1286 | return res; |
043a4492 RR |
1287 | } |
1288 | ||
004fefa7 | 1289 | static int read_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
1290 | struct replay_opts *opts) |
1291 | { | |
c0246501 | 1292 | const char *todo_file = get_todo_path(opts); |
043a4492 RR |
1293 | int fd, res; |
1294 | ||
004fefa7 | 1295 | strbuf_reset(&todo_list->buf); |
c0246501 | 1296 | fd = open(todo_file, O_RDONLY); |
043a4492 | 1297 | if (fd < 0) |
93b3df6f | 1298 | return error_errno(_("could not open '%s'"), todo_file); |
004fefa7 | 1299 | if (strbuf_read(&todo_list->buf, fd, 0) < 0) { |
043a4492 | 1300 | close(fd); |
93b3df6f | 1301 | return error(_("could not read '%s'."), todo_file); |
043a4492 RR |
1302 | } |
1303 | close(fd); | |
1304 | ||
004fefa7 | 1305 | res = parse_insn_buffer(todo_list->buf.buf, todo_list); |
2eeaf1b3 | 1306 | if (res) |
93b3df6f | 1307 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
2eeaf1b3 | 1308 | |
52865279 JS |
1309 | if (!todo_list->nr && |
1310 | (!is_rebase_i(opts) || !file_exists(rebase_path_done()))) | |
1311 | return error(_("no commits parsed.")); | |
1312 | ||
2eeaf1b3 | 1313 | if (!is_rebase_i(opts)) { |
004fefa7 JS |
1314 | enum todo_command valid = |
1315 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; | |
1316 | int i; | |
1317 | ||
1318 | for (i = 0; i < todo_list->nr; i++) | |
1319 | if (valid == todo_list->items[i].command) | |
1320 | continue; | |
1321 | else if (valid == TODO_PICK) | |
93b3df6f | 1322 | return error(_("cannot cherry-pick during a revert.")); |
004fefa7 | 1323 | else |
93b3df6f | 1324 | return error(_("cannot revert during a cherry-pick.")); |
004fefa7 JS |
1325 | } |
1326 | ||
0ae42a03 | 1327 | return 0; |
043a4492 RR |
1328 | } |
1329 | ||
03a4e260 JS |
1330 | static int git_config_string_dup(char **dest, |
1331 | const char *var, const char *value) | |
1332 | { | |
1333 | if (!value) | |
1334 | return config_error_nonbool(var); | |
1335 | free(*dest); | |
1336 | *dest = xstrdup(value); | |
1337 | return 0; | |
1338 | } | |
1339 | ||
043a4492 RR |
1340 | static int populate_opts_cb(const char *key, const char *value, void *data) |
1341 | { | |
1342 | struct replay_opts *opts = data; | |
1343 | int error_flag = 1; | |
1344 | ||
1345 | if (!value) | |
1346 | error_flag = 0; | |
1347 | else if (!strcmp(key, "options.no-commit")) | |
1348 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); | |
1349 | else if (!strcmp(key, "options.edit")) | |
1350 | opts->edit = git_config_bool_or_int(key, value, &error_flag); | |
1351 | else if (!strcmp(key, "options.signoff")) | |
1352 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); | |
1353 | else if (!strcmp(key, "options.record-origin")) | |
1354 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); | |
1355 | else if (!strcmp(key, "options.allow-ff")) | |
1356 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); | |
1357 | else if (!strcmp(key, "options.mainline")) | |
1358 | opts->mainline = git_config_int(key, value); | |
1359 | else if (!strcmp(key, "options.strategy")) | |
03a4e260 | 1360 | git_config_string_dup(&opts->strategy, key, value); |
3253553e | 1361 | else if (!strcmp(key, "options.gpg-sign")) |
03a4e260 | 1362 | git_config_string_dup(&opts->gpg_sign, key, value); |
043a4492 RR |
1363 | else if (!strcmp(key, "options.strategy-option")) { |
1364 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); | |
1365 | opts->xopts[opts->xopts_nr++] = xstrdup(value); | |
1366 | } else | |
93b3df6f | 1367 | return error(_("invalid key: %s"), key); |
043a4492 RR |
1368 | |
1369 | if (!error_flag) | |
93b3df6f | 1370 | return error(_("invalid value for %s: %s"), key, value); |
043a4492 RR |
1371 | |
1372 | return 0; | |
1373 | } | |
1374 | ||
ca6c6b45 JS |
1375 | static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) |
1376 | { | |
1377 | int i; | |
1378 | ||
1379 | strbuf_reset(buf); | |
1380 | if (!read_oneliner(buf, rebase_path_strategy(), 0)) | |
1381 | return; | |
1382 | opts->strategy = strbuf_detach(buf, NULL); | |
1383 | if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) | |
1384 | return; | |
1385 | ||
1386 | opts->xopts_nr = split_cmdline(buf->buf, (const char ***)&opts->xopts); | |
1387 | for (i = 0; i < opts->xopts_nr; i++) { | |
1388 | const char *arg = opts->xopts[i]; | |
1389 | ||
1390 | skip_prefix(arg, "--", &arg); | |
1391 | opts->xopts[i] = xstrdup(arg); | |
1392 | } | |
1393 | } | |
1394 | ||
5adf9bdc | 1395 | static int read_populate_opts(struct replay_opts *opts) |
043a4492 | 1396 | { |
a1c75762 JS |
1397 | if (is_rebase_i(opts)) { |
1398 | struct strbuf buf = STRBUF_INIT; | |
1399 | ||
1400 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) { | |
1401 | if (!starts_with(buf.buf, "-S")) | |
1402 | strbuf_reset(&buf); | |
1403 | else { | |
1404 | free(opts->gpg_sign); | |
1405 | opts->gpg_sign = xstrdup(buf.buf + 2); | |
1406 | } | |
1407 | } | |
a1c75762 | 1408 | |
556907f1 JS |
1409 | if (file_exists(rebase_path_verbose())) |
1410 | opts->verbose = 1; | |
1411 | ||
ca6c6b45 JS |
1412 | read_strategy_opts(opts, &buf); |
1413 | strbuf_release(&buf); | |
1414 | ||
b5a67045 | 1415 | return 0; |
a1c75762 | 1416 | } |
b5a67045 | 1417 | |
f932729c | 1418 | if (!file_exists(git_path_opts_file())) |
0d00da7b JS |
1419 | return 0; |
1420 | /* | |
1421 | * The function git_parse_source(), called from git_config_from_file(), | |
1422 | * may die() in case of a syntactically incorrect file. We do not care | |
1423 | * about this case, though, because we wrote that file ourselves, so we | |
1424 | * are pretty certain that it is syntactically correct. | |
1425 | */ | |
5adf9bdc | 1426 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
93b3df6f | 1427 | return error(_("malformed options sheet: '%s'"), |
0d00da7b JS |
1428 | git_path_opts_file()); |
1429 | return 0; | |
043a4492 RR |
1430 | } |
1431 | ||
004fefa7 | 1432 | static int walk_revs_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
1433 | struct replay_opts *opts) |
1434 | { | |
004fefa7 JS |
1435 | enum todo_command command = opts->action == REPLAY_PICK ? |
1436 | TODO_PICK : TODO_REVERT; | |
414697a9 | 1437 | const char *command_string = todo_command_info[command].str; |
043a4492 | 1438 | struct commit *commit; |
043a4492 | 1439 | |
34b0528b JS |
1440 | if (prepare_revs(opts)) |
1441 | return -1; | |
043a4492 | 1442 | |
004fefa7 JS |
1443 | while ((commit = get_revision(opts->revs))) { |
1444 | struct todo_item *item = append_new_todo(todo_list); | |
1445 | const char *commit_buffer = get_commit_buffer(commit, NULL); | |
1446 | const char *subject; | |
1447 | int subject_len; | |
1448 | ||
1449 | item->command = command; | |
1450 | item->commit = commit; | |
c22f7dfb JS |
1451 | item->arg = NULL; |
1452 | item->arg_len = 0; | |
004fefa7 JS |
1453 | item->offset_in_buf = todo_list->buf.len; |
1454 | subject_len = find_commit_subject(commit_buffer, &subject); | |
1455 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, | |
1456 | short_commit_name(commit), subject_len, subject); | |
1457 | unuse_commit_buffer(commit, commit_buffer); | |
1458 | } | |
34b0528b | 1459 | return 0; |
043a4492 RR |
1460 | } |
1461 | ||
1462 | static int create_seq_dir(void) | |
1463 | { | |
f932729c | 1464 | if (file_exists(git_path_seq_dir())) { |
043a4492 RR |
1465 | error(_("a cherry-pick or revert is already in progress")); |
1466 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); | |
1467 | return -1; | |
a70d8f80 | 1468 | } else if (mkdir(git_path_seq_dir(), 0777) < 0) |
93b3df6f | 1469 | return error_errno(_("could not create sequencer directory '%s'"), |
f6e82b0d | 1470 | git_path_seq_dir()); |
043a4492 RR |
1471 | return 0; |
1472 | } | |
1473 | ||
311fd397 | 1474 | static int save_head(const char *head) |
043a4492 | 1475 | { |
043a4492 RR |
1476 | static struct lock_file head_lock; |
1477 | struct strbuf buf = STRBUF_INIT; | |
1478 | int fd; | |
1479 | ||
311fd397 JS |
1480 | fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); |
1481 | if (fd < 0) { | |
1482 | rollback_lock_file(&head_lock); | |
93b3df6f | 1483 | return error_errno(_("could not lock HEAD")); |
311fd397 | 1484 | } |
043a4492 | 1485 | strbuf_addf(&buf, "%s\n", head); |
311fd397 JS |
1486 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
1487 | rollback_lock_file(&head_lock); | |
93b3df6f | 1488 | return error_errno(_("could not write to '%s'"), |
311fd397 JS |
1489 | git_path_head_file()); |
1490 | } | |
1491 | if (commit_lock_file(&head_lock) < 0) { | |
1492 | rollback_lock_file(&head_lock); | |
93b3df6f | 1493 | return error(_("failed to finalize '%s'."), git_path_head_file()); |
311fd397 JS |
1494 | } |
1495 | return 0; | |
043a4492 RR |
1496 | } |
1497 | ||
1e41229d SB |
1498 | static int rollback_is_safe(void) |
1499 | { | |
1500 | struct strbuf sb = STRBUF_INIT; | |
1501 | struct object_id expected_head, actual_head; | |
1502 | ||
1503 | if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) { | |
1504 | strbuf_trim(&sb); | |
1505 | if (get_oid_hex(sb.buf, &expected_head)) { | |
1506 | strbuf_release(&sb); | |
1507 | die(_("could not parse %s"), git_path_abort_safety_file()); | |
1508 | } | |
1509 | strbuf_release(&sb); | |
1510 | } | |
1511 | else if (errno == ENOENT) | |
1512 | oidclr(&expected_head); | |
1513 | else | |
1514 | die_errno(_("could not read '%s'"), git_path_abort_safety_file()); | |
1515 | ||
1516 | if (get_oid("HEAD", &actual_head)) | |
1517 | oidclr(&actual_head); | |
1518 | ||
1519 | return !oidcmp(&actual_head, &expected_head); | |
1520 | } | |
1521 | ||
043a4492 RR |
1522 | static int reset_for_rollback(const unsigned char *sha1) |
1523 | { | |
1524 | const char *argv[4]; /* reset --merge <arg> + NULL */ | |
1e41229d | 1525 | |
043a4492 RR |
1526 | argv[0] = "reset"; |
1527 | argv[1] = "--merge"; | |
1528 | argv[2] = sha1_to_hex(sha1); | |
1529 | argv[3] = NULL; | |
1530 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
1531 | } | |
1532 | ||
1533 | static int rollback_single_pick(void) | |
1534 | { | |
1535 | unsigned char head_sha1[20]; | |
1536 | ||
f932729c JK |
1537 | if (!file_exists(git_path_cherry_pick_head()) && |
1538 | !file_exists(git_path_revert_head())) | |
043a4492 | 1539 | return error(_("no cherry-pick or revert in progress")); |
7695d118 | 1540 | if (read_ref_full("HEAD", 0, head_sha1, NULL)) |
043a4492 RR |
1541 | return error(_("cannot resolve HEAD")); |
1542 | if (is_null_sha1(head_sha1)) | |
1543 | return error(_("cannot abort from a branch yet to be born")); | |
1544 | return reset_for_rollback(head_sha1); | |
1545 | } | |
1546 | ||
2863584f | 1547 | int sequencer_rollback(struct replay_opts *opts) |
043a4492 | 1548 | { |
043a4492 RR |
1549 | FILE *f; |
1550 | unsigned char sha1[20]; | |
1551 | struct strbuf buf = STRBUF_INIT; | |
1552 | ||
f932729c | 1553 | f = fopen(git_path_head_file(), "r"); |
043a4492 RR |
1554 | if (!f && errno == ENOENT) { |
1555 | /* | |
1556 | * There is no multiple-cherry-pick in progress. | |
1557 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates | |
1558 | * a single-cherry-pick in progress, abort that. | |
1559 | */ | |
1560 | return rollback_single_pick(); | |
1561 | } | |
1562 | if (!f) | |
f7ed1953 | 1563 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
8f309aeb | 1564 | if (strbuf_getline_lf(&buf, f)) { |
f7ed1953 | 1565 | error(_("cannot read '%s': %s"), git_path_head_file(), |
f932729c | 1566 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
043a4492 RR |
1567 | fclose(f); |
1568 | goto fail; | |
1569 | } | |
1570 | fclose(f); | |
1571 | if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') { | |
1572 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), | |
f932729c | 1573 | git_path_head_file()); |
043a4492 RR |
1574 | goto fail; |
1575 | } | |
0f974e21 MG |
1576 | if (is_null_sha1(sha1)) { |
1577 | error(_("cannot abort from a branch yet to be born")); | |
1578 | goto fail; | |
1579 | } | |
1e41229d SB |
1580 | |
1581 | if (!rollback_is_safe()) { | |
1582 | /* Do not error, just do not rollback */ | |
1583 | warning(_("You seem to have moved HEAD. " | |
1584 | "Not rewinding, check your HEAD!")); | |
1585 | } else | |
043a4492 RR |
1586 | if (reset_for_rollback(sha1)) |
1587 | goto fail; | |
043a4492 | 1588 | strbuf_release(&buf); |
2863584f | 1589 | return sequencer_remove_state(opts); |
043a4492 RR |
1590 | fail: |
1591 | strbuf_release(&buf); | |
1592 | return -1; | |
1593 | } | |
1594 | ||
004fefa7 | 1595 | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 1596 | { |
043a4492 | 1597 | static struct lock_file todo_lock; |
004fefa7 JS |
1598 | const char *todo_path = get_todo_path(opts); |
1599 | int next = todo_list->current, offset, fd; | |
043a4492 | 1600 | |
84583957 JS |
1601 | /* |
1602 | * rebase -i writes "git-rebase-todo" without the currently executing | |
1603 | * command, appending it to "done" instead. | |
1604 | */ | |
1605 | if (is_rebase_i(opts)) | |
1606 | next++; | |
1607 | ||
004fefa7 | 1608 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
221675de | 1609 | if (fd < 0) |
93b3df6f | 1610 | return error_errno(_("could not lock '%s'"), todo_path); |
004fefa7 JS |
1611 | offset = next < todo_list->nr ? |
1612 | todo_list->items[next].offset_in_buf : todo_list->buf.len; | |
1613 | if (write_in_full(fd, todo_list->buf.buf + offset, | |
1614 | todo_list->buf.len - offset) < 0) | |
93b3df6f | 1615 | return error_errno(_("could not write to '%s'"), todo_path); |
004fefa7 | 1616 | if (commit_lock_file(&todo_lock) < 0) |
93b3df6f | 1617 | return error(_("failed to finalize '%s'."), todo_path); |
1df6df0c JS |
1618 | |
1619 | if (is_rebase_i(opts)) { | |
1620 | const char *done_path = rebase_path_done(); | |
1621 | int fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
1622 | int prev_offset = !next ? 0 : | |
1623 | todo_list->items[next - 1].offset_in_buf; | |
1624 | ||
1625 | if (fd >= 0 && offset > prev_offset && | |
1626 | write_in_full(fd, todo_list->buf.buf + prev_offset, | |
1627 | offset - prev_offset) < 0) { | |
1628 | close(fd); | |
1629 | return error_errno(_("could not write to '%s'"), | |
1630 | done_path); | |
1631 | } | |
1632 | if (fd >= 0) | |
1633 | close(fd); | |
1634 | } | |
221675de | 1635 | return 0; |
043a4492 RR |
1636 | } |
1637 | ||
88d5a271 | 1638 | static int save_opts(struct replay_opts *opts) |
043a4492 | 1639 | { |
f932729c | 1640 | const char *opts_file = git_path_opts_file(); |
88d5a271 | 1641 | int res = 0; |
043a4492 RR |
1642 | |
1643 | if (opts->no_commit) | |
88d5a271 | 1644 | res |= git_config_set_in_file_gently(opts_file, "options.no-commit", "true"); |
043a4492 | 1645 | if (opts->edit) |
88d5a271 | 1646 | res |= git_config_set_in_file_gently(opts_file, "options.edit", "true"); |
043a4492 | 1647 | if (opts->signoff) |
88d5a271 | 1648 | res |= git_config_set_in_file_gently(opts_file, "options.signoff", "true"); |
043a4492 | 1649 | if (opts->record_origin) |
88d5a271 | 1650 | res |= git_config_set_in_file_gently(opts_file, "options.record-origin", "true"); |
043a4492 | 1651 | if (opts->allow_ff) |
88d5a271 | 1652 | res |= git_config_set_in_file_gently(opts_file, "options.allow-ff", "true"); |
043a4492 RR |
1653 | if (opts->mainline) { |
1654 | struct strbuf buf = STRBUF_INIT; | |
1655 | strbuf_addf(&buf, "%d", opts->mainline); | |
88d5a271 | 1656 | res |= git_config_set_in_file_gently(opts_file, "options.mainline", buf.buf); |
043a4492 RR |
1657 | strbuf_release(&buf); |
1658 | } | |
1659 | if (opts->strategy) | |
88d5a271 | 1660 | res |= git_config_set_in_file_gently(opts_file, "options.strategy", opts->strategy); |
3253553e | 1661 | if (opts->gpg_sign) |
88d5a271 | 1662 | res |= git_config_set_in_file_gently(opts_file, "options.gpg-sign", opts->gpg_sign); |
043a4492 RR |
1663 | if (opts->xopts) { |
1664 | int i; | |
1665 | for (i = 0; i < opts->xopts_nr; i++) | |
88d5a271 | 1666 | res |= git_config_set_multivar_in_file_gently(opts_file, |
043a4492 RR |
1667 | "options.strategy-option", |
1668 | opts->xopts[i], "^$", 0); | |
1669 | } | |
88d5a271 | 1670 | return res; |
043a4492 RR |
1671 | } |
1672 | ||
56dc3ab0 JS |
1673 | static int make_patch(struct commit *commit, struct replay_opts *opts) |
1674 | { | |
1675 | struct strbuf buf = STRBUF_INIT; | |
1676 | struct rev_info log_tree_opt; | |
1677 | const char *subject, *p; | |
1678 | int res = 0; | |
1679 | ||
1680 | p = short_commit_name(commit); | |
1681 | if (write_message(p, strlen(p), rebase_path_stopped_sha(), 1) < 0) | |
1682 | return -1; | |
1683 | ||
1684 | strbuf_addf(&buf, "%s/patch", get_dir(opts)); | |
1685 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); | |
1686 | init_revisions(&log_tree_opt, NULL); | |
1687 | log_tree_opt.abbrev = 0; | |
1688 | log_tree_opt.diff = 1; | |
1689 | log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH; | |
1690 | log_tree_opt.disable_stdin = 1; | |
1691 | log_tree_opt.no_commit_id = 1; | |
1692 | log_tree_opt.diffopt.file = fopen(buf.buf, "w"); | |
1693 | log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER; | |
1694 | if (!log_tree_opt.diffopt.file) | |
1695 | res |= error_errno(_("could not open '%s'"), buf.buf); | |
1696 | else { | |
1697 | res |= log_tree_commit(&log_tree_opt, commit); | |
1698 | fclose(log_tree_opt.diffopt.file); | |
1699 | } | |
1700 | strbuf_reset(&buf); | |
1701 | ||
1702 | strbuf_addf(&buf, "%s/message", get_dir(opts)); | |
1703 | if (!file_exists(buf.buf)) { | |
1704 | const char *commit_buffer = get_commit_buffer(commit, NULL); | |
1705 | find_commit_subject(commit_buffer, &subject); | |
1706 | res |= write_message(subject, strlen(subject), buf.buf, 1); | |
1707 | unuse_commit_buffer(commit, commit_buffer); | |
1708 | } | |
1709 | strbuf_release(&buf); | |
1710 | ||
1711 | return res; | |
1712 | } | |
1713 | ||
1714 | static int intend_to_amend(void) | |
1715 | { | |
1716 | unsigned char head[20]; | |
1717 | char *p; | |
1718 | ||
1719 | if (get_sha1("HEAD", head)) | |
1720 | return error(_("cannot read HEAD")); | |
1721 | ||
1722 | p = sha1_to_hex(head); | |
1723 | return write_message(p, strlen(p), rebase_path_amend(), 1); | |
1724 | } | |
1725 | ||
1726 | static int error_with_patch(struct commit *commit, | |
1727 | const char *subject, int subject_len, | |
1728 | struct replay_opts *opts, int exit_code, int to_amend) | |
1729 | { | |
1730 | if (make_patch(commit, opts)) | |
1731 | return -1; | |
1732 | ||
1733 | if (to_amend) { | |
1734 | if (intend_to_amend()) | |
1735 | return -1; | |
1736 | ||
1737 | fprintf(stderr, "You can amend the commit now, with\n" | |
1738 | "\n" | |
1739 | " git commit --amend %s\n" | |
1740 | "\n" | |
1741 | "Once you are satisfied with your changes, run\n" | |
1742 | "\n" | |
1743 | " git rebase --continue\n", gpg_sign_opt_quoted(opts)); | |
1744 | } else if (exit_code) | |
1745 | fprintf(stderr, "Could not apply %s... %.*s\n", | |
1746 | short_commit_name(commit), subject_len, subject); | |
1747 | ||
1748 | return exit_code; | |
1749 | } | |
1750 | ||
6e98de72 JS |
1751 | static int error_failed_squash(struct commit *commit, |
1752 | struct replay_opts *opts, int subject_len, const char *subject) | |
1753 | { | |
1754 | if (rename(rebase_path_squash_msg(), rebase_path_message())) | |
1755 | return error(_("could not rename '%s' to '%s'"), | |
1756 | rebase_path_squash_msg(), rebase_path_message()); | |
1757 | unlink(rebase_path_fixup_msg()); | |
1758 | unlink(git_path("MERGE_MSG")); | |
1759 | if (copy_file(git_path("MERGE_MSG"), rebase_path_message(), 0666)) | |
1760 | return error(_("could not copy '%s' to '%s'"), | |
1761 | rebase_path_message(), git_path("MERGE_MSG")); | |
1762 | return error_with_patch(commit, subject, subject_len, opts, 1, 0); | |
1763 | } | |
1764 | ||
311af526 JS |
1765 | static int do_exec(const char *command_line) |
1766 | { | |
1767 | const char *child_argv[] = { NULL, NULL }; | |
1768 | int dirty, status; | |
1769 | ||
1770 | fprintf(stderr, "Executing: %s\n", command_line); | |
1771 | child_argv[0] = command_line; | |
1772 | status = run_command_v_opt(child_argv, RUN_USING_SHELL); | |
1773 | ||
1774 | /* force re-reading of the cache */ | |
1775 | if (discard_cache() < 0 || read_cache() < 0) | |
1776 | return error(_("could not read index")); | |
1777 | ||
1778 | dirty = require_clean_work_tree("rebase", NULL, 1, 1); | |
1779 | ||
1780 | if (status) { | |
1781 | warning(_("execution failed: %s\n%s" | |
1782 | "You can fix the problem, and then run\n" | |
1783 | "\n" | |
1784 | " git rebase --continue\n" | |
1785 | "\n"), | |
1786 | command_line, | |
1787 | dirty ? N_("and made changes to the index and/or the " | |
1788 | "working tree\n") : ""); | |
1789 | if (status == 127) | |
1790 | /* command not found */ | |
1791 | status = 1; | |
1792 | } else if (dirty) { | |
1793 | warning(_("execution succeeded: %s\nbut " | |
1794 | "left changes to the index and/or the working tree\n" | |
1795 | "Commit or stash your changes, and then run\n" | |
1796 | "\n" | |
1797 | " git rebase --continue\n" | |
1798 | "\n"), command_line); | |
1799 | status = 1; | |
1800 | } | |
1801 | ||
1802 | return status; | |
1803 | } | |
1804 | ||
6e98de72 JS |
1805 | static int is_final_fixup(struct todo_list *todo_list) |
1806 | { | |
1807 | int i = todo_list->current; | |
1808 | ||
1809 | if (!is_fixup(todo_list->items[i].command)) | |
1810 | return 0; | |
1811 | ||
1812 | while (++i < todo_list->nr) | |
1813 | if (is_fixup(todo_list->items[i].command)) | |
1814 | return 0; | |
1815 | else if (!is_noop(todo_list->items[i].command)) | |
1816 | break; | |
1817 | return 1; | |
1818 | } | |
1819 | ||
25cb8df9 JS |
1820 | static enum todo_command peek_command(struct todo_list *todo_list, int offset) |
1821 | { | |
1822 | int i; | |
1823 | ||
1824 | for (i = todo_list->current + offset; i < todo_list->nr; i++) | |
1825 | if (!is_noop(todo_list->items[i].command)) | |
1826 | return todo_list->items[i].command; | |
1827 | ||
1828 | return -1; | |
1829 | } | |
1830 | ||
796c7972 JS |
1831 | static int apply_autostash(struct replay_opts *opts) |
1832 | { | |
1833 | struct strbuf stash_sha1 = STRBUF_INIT; | |
1834 | struct child_process child = CHILD_PROCESS_INIT; | |
1835 | int ret = 0; | |
1836 | ||
1837 | if (!read_oneliner(&stash_sha1, rebase_path_autostash(), 1)) { | |
1838 | strbuf_release(&stash_sha1); | |
1839 | return 0; | |
1840 | } | |
1841 | strbuf_trim(&stash_sha1); | |
1842 | ||
1843 | child.git_cmd = 1; | |
1844 | argv_array_push(&child.args, "stash"); | |
1845 | argv_array_push(&child.args, "apply"); | |
1846 | argv_array_push(&child.args, stash_sha1.buf); | |
1847 | if (!run_command(&child)) | |
1848 | printf(_("Applied autostash.")); | |
1849 | else { | |
1850 | struct child_process store = CHILD_PROCESS_INIT; | |
1851 | ||
1852 | store.git_cmd = 1; | |
1853 | argv_array_push(&store.args, "stash"); | |
1854 | argv_array_push(&store.args, "store"); | |
1855 | argv_array_push(&store.args, "-m"); | |
1856 | argv_array_push(&store.args, "autostash"); | |
1857 | argv_array_push(&store.args, "-q"); | |
1858 | argv_array_push(&store.args, stash_sha1.buf); | |
1859 | if (run_command(&store)) | |
1860 | ret = error(_("cannot store %s"), stash_sha1.buf); | |
1861 | else | |
1862 | printf(_("Applying autostash resulted in conflicts.\n" | |
1863 | "Your changes are safe in the stash.\n" | |
1864 | "You can run \"git stash pop\" or" | |
1865 | " \"git stash drop\" at any time.\n")); | |
1866 | } | |
1867 | ||
1868 | strbuf_release(&stash_sha1); | |
1869 | return ret; | |
1870 | } | |
1871 | ||
96e832a5 JS |
1872 | static const char *reflog_message(struct replay_opts *opts, |
1873 | const char *sub_action, const char *fmt, ...) | |
1874 | { | |
1875 | va_list ap; | |
1876 | static struct strbuf buf = STRBUF_INIT; | |
1877 | ||
1878 | va_start(ap, fmt); | |
1879 | strbuf_reset(&buf); | |
1880 | strbuf_addstr(&buf, action_name(opts)); | |
1881 | if (sub_action) | |
1882 | strbuf_addf(&buf, " (%s)", sub_action); | |
1883 | if (fmt) { | |
1884 | strbuf_addstr(&buf, ": "); | |
1885 | strbuf_vaddf(&buf, fmt, ap); | |
1886 | } | |
1887 | va_end(ap); | |
1888 | ||
1889 | return buf.buf; | |
1890 | } | |
1891 | ||
004fefa7 | 1892 | static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 1893 | { |
56dc3ab0 | 1894 | int res = 0; |
043a4492 RR |
1895 | |
1896 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
1897 | if (opts->allow_ff) | |
1898 | assert(!(opts->signoff || opts->no_commit || | |
1899 | opts->record_origin || opts->edit)); | |
0d9c6dc9 JS |
1900 | if (read_and_refresh_cache(opts)) |
1901 | return -1; | |
043a4492 | 1902 | |
004fefa7 JS |
1903 | while (todo_list->current < todo_list->nr) { |
1904 | struct todo_item *item = todo_list->items + todo_list->current; | |
1905 | if (save_todo(todo_list, opts)) | |
221675de | 1906 | return -1; |
6e98de72 JS |
1907 | if (is_rebase_i(opts)) { |
1908 | unlink(rebase_path_message()); | |
1909 | unlink(rebase_path_author_script()); | |
1910 | unlink(rebase_path_stopped_sha()); | |
1911 | unlink(rebase_path_amend()); | |
1912 | } | |
1913 | if (item->command <= TODO_SQUASH) { | |
8ab37ef2 JS |
1914 | if (is_rebase_i(opts)) |
1915 | setenv("GIT_REFLOG_ACTION", reflog_message(opts, | |
1916 | command_to_string(item->command), NULL), | |
1917 | 1); | |
25c43667 | 1918 | res = do_pick_commit(item->command, item->commit, |
6e98de72 | 1919 | opts, is_final_fixup(todo_list)); |
9d7bf3cf JS |
1920 | if (is_rebase_i(opts) && res < 0) { |
1921 | /* Reschedule */ | |
1922 | todo_list->current--; | |
1923 | if (save_todo(todo_list, opts)) | |
1924 | return -1; | |
1925 | } | |
56dc3ab0 JS |
1926 | if (item->command == TODO_EDIT) { |
1927 | struct commit *commit = item->commit; | |
1928 | if (!res) | |
1929 | warning(_("stopped at %s... %.*s"), | |
1930 | short_commit_name(commit), | |
1931 | item->arg_len, item->arg); | |
1932 | return error_with_patch(commit, | |
1933 | item->arg, item->arg_len, opts, res, | |
1934 | !res); | |
1935 | } | |
25cb8df9 JS |
1936 | if (is_rebase_i(opts) && !res) |
1937 | record_in_rewritten(&item->commit->object.oid, | |
1938 | peek_command(todo_list, 1)); | |
6e98de72 JS |
1939 | if (res && is_fixup(item->command)) { |
1940 | if (res == 1) | |
1941 | intend_to_amend(); | |
1942 | return error_failed_squash(item->commit, opts, | |
1943 | item->arg_len, item->arg); | |
4a5146f9 JS |
1944 | } else if (res && is_rebase_i(opts)) |
1945 | return res | error_with_patch(item->commit, | |
04efc8b5 JS |
1946 | item->arg, item->arg_len, opts, res, |
1947 | item->command == TODO_REWORD); | |
311af526 JS |
1948 | } else if (item->command == TODO_EXEC) { |
1949 | char *end_of_arg = (char *)(item->arg + item->arg_len); | |
1950 | int saved = *end_of_arg; | |
1951 | ||
1952 | *end_of_arg = '\0'; | |
1953 | res = do_exec(item->arg); | |
1954 | *end_of_arg = saved; | |
56dc3ab0 | 1955 | } else if (!is_noop(item->command)) |
25c43667 JS |
1956 | return error(_("unknown command %d"), item->command); |
1957 | ||
004fefa7 | 1958 | todo_list->current++; |
043a4492 RR |
1959 | if (res) |
1960 | return res; | |
1961 | } | |
1962 | ||
56dc3ab0 | 1963 | if (is_rebase_i(opts)) { |
4b83ce9f | 1964 | struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT; |
25cb8df9 | 1965 | struct stat st; |
556907f1 | 1966 | |
56dc3ab0 JS |
1967 | /* Stopped in the middle, as planned? */ |
1968 | if (todo_list->current < todo_list->nr) | |
1969 | return 0; | |
556907f1 | 1970 | |
4b83ce9f JS |
1971 | if (read_oneliner(&head_ref, rebase_path_head_name(), 0) && |
1972 | starts_with(head_ref.buf, "refs/")) { | |
96e832a5 | 1973 | const char *msg; |
4b83ce9f JS |
1974 | unsigned char head[20], orig[20]; |
1975 | int res; | |
1976 | ||
1977 | if (get_sha1("HEAD", head)) { | |
1978 | res = error(_("cannot read HEAD")); | |
1979 | cleanup_head_ref: | |
1980 | strbuf_release(&head_ref); | |
1981 | strbuf_release(&buf); | |
1982 | return res; | |
1983 | } | |
1984 | if (!read_oneliner(&buf, rebase_path_orig_head(), 0) || | |
1985 | get_sha1_hex(buf.buf, orig)) { | |
1986 | res = error(_("could not read orig-head")); | |
1987 | goto cleanup_head_ref; | |
1988 | } | |
4b83ce9f JS |
1989 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) { |
1990 | res = error(_("could not read 'onto'")); | |
1991 | goto cleanup_head_ref; | |
1992 | } | |
96e832a5 JS |
1993 | msg = reflog_message(opts, "finish", "%s onto %s", |
1994 | head_ref.buf, buf.buf); | |
1995 | if (update_ref(msg, head_ref.buf, head, orig, | |
4b83ce9f JS |
1996 | REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) { |
1997 | res = error(_("could not update %s"), | |
1998 | head_ref.buf); | |
1999 | goto cleanup_head_ref; | |
2000 | } | |
96e832a5 | 2001 | msg = reflog_message(opts, "finish", "returning to %s", |
4b83ce9f | 2002 | head_ref.buf); |
96e832a5 | 2003 | if (create_symref("HEAD", head_ref.buf, msg)) { |
4b83ce9f JS |
2004 | res = error(_("could not update HEAD to %s"), |
2005 | head_ref.buf); | |
2006 | goto cleanup_head_ref; | |
2007 | } | |
2008 | strbuf_reset(&buf); | |
2009 | } | |
2010 | ||
556907f1 JS |
2011 | if (opts->verbose) { |
2012 | struct rev_info log_tree_opt; | |
2013 | struct object_id orig, head; | |
2014 | ||
2015 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); | |
2016 | init_revisions(&log_tree_opt, NULL); | |
2017 | log_tree_opt.diff = 1; | |
2018 | log_tree_opt.diffopt.output_format = | |
2019 | DIFF_FORMAT_DIFFSTAT; | |
2020 | log_tree_opt.disable_stdin = 1; | |
2021 | ||
2022 | if (read_oneliner(&buf, rebase_path_orig_head(), 0) && | |
2023 | !get_sha1(buf.buf, orig.hash) && | |
2024 | !get_sha1("HEAD", head.hash)) { | |
2025 | diff_tree_sha1(orig.hash, head.hash, | |
2026 | "", &log_tree_opt.diffopt); | |
2027 | log_tree_diff_flush(&log_tree_opt); | |
2028 | } | |
2029 | } | |
25cb8df9 JS |
2030 | flush_rewritten_pending(); |
2031 | if (!stat(rebase_path_rewritten_list(), &st) && | |
2032 | st.st_size > 0) { | |
2033 | struct child_process child = CHILD_PROCESS_INIT; | |
79516045 JS |
2034 | const char *post_rewrite_hook = |
2035 | find_hook("post-rewrite"); | |
25cb8df9 JS |
2036 | |
2037 | child.in = open(rebase_path_rewritten_list(), O_RDONLY); | |
2038 | child.git_cmd = 1; | |
2039 | argv_array_push(&child.args, "notes"); | |
2040 | argv_array_push(&child.args, "copy"); | |
2041 | argv_array_push(&child.args, "--for-rewrite=rebase"); | |
2042 | /* we don't care if this copying failed */ | |
2043 | run_command(&child); | |
79516045 JS |
2044 | |
2045 | if (post_rewrite_hook) { | |
2046 | struct child_process hook = CHILD_PROCESS_INIT; | |
2047 | ||
2048 | hook.in = open(rebase_path_rewritten_list(), | |
2049 | O_RDONLY); | |
2050 | hook.stdout_to_stderr = 1; | |
2051 | argv_array_push(&hook.args, post_rewrite_hook); | |
2052 | argv_array_push(&hook.args, "rebase"); | |
2053 | /* we don't care if this hook failed */ | |
2054 | run_command(&hook); | |
2055 | } | |
25cb8df9 | 2056 | } |
796c7972 | 2057 | apply_autostash(opts); |
25cb8df9 | 2058 | |
556907f1 | 2059 | strbuf_release(&buf); |
4b83ce9f | 2060 | strbuf_release(&head_ref); |
56dc3ab0 JS |
2061 | } |
2062 | ||
043a4492 RR |
2063 | /* |
2064 | * Sequence of picks finished successfully; cleanup by | |
2065 | * removing the .git/sequencer directory | |
2066 | */ | |
2863584f | 2067 | return sequencer_remove_state(opts); |
043a4492 RR |
2068 | } |
2069 | ||
2070 | static int continue_single_pick(void) | |
2071 | { | |
2072 | const char *argv[] = { "commit", NULL }; | |
2073 | ||
f932729c JK |
2074 | if (!file_exists(git_path_cherry_pick_head()) && |
2075 | !file_exists(git_path_revert_head())) | |
043a4492 RR |
2076 | return error(_("no cherry-pick or revert in progress")); |
2077 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
2078 | } | |
2079 | ||
9d93ccd1 JS |
2080 | static int commit_staged_changes(struct replay_opts *opts) |
2081 | { | |
2082 | int amend = 0; | |
2083 | ||
2084 | if (has_unstaged_changes(1)) | |
2085 | return error(_("cannot rebase: You have unstaged changes.")); | |
52632209 JS |
2086 | if (!has_uncommitted_changes(0)) { |
2087 | const char *cherry_pick_head = git_path("CHERRY_PICK_HEAD"); | |
2088 | ||
2089 | if (file_exists(cherry_pick_head) && unlink(cherry_pick_head)) | |
2090 | return error(_("could not remove CHERRY_PICK_HEAD")); | |
9d93ccd1 | 2091 | return 0; |
52632209 | 2092 | } |
9d93ccd1 JS |
2093 | |
2094 | if (file_exists(rebase_path_amend())) { | |
2095 | struct strbuf rev = STRBUF_INIT; | |
2096 | unsigned char head[20], to_amend[20]; | |
2097 | ||
2098 | if (get_sha1("HEAD", head)) | |
2099 | return error(_("cannot amend non-existing commit")); | |
2100 | if (!read_oneliner(&rev, rebase_path_amend(), 0)) | |
2101 | return error(_("invalid file: '%s'"), rebase_path_amend()); | |
2102 | if (get_sha1_hex(rev.buf, to_amend)) | |
2103 | return error(_("invalid contents: '%s'"), | |
2104 | rebase_path_amend()); | |
2105 | if (hashcmp(head, to_amend)) | |
2106 | return error(_("\nYou have uncommitted changes in your " | |
2107 | "working tree. Please, commit them\n" | |
2108 | "first and then run 'git rebase " | |
2109 | "--continue' again.")); | |
2110 | ||
2111 | strbuf_release(&rev); | |
2112 | amend = 1; | |
2113 | } | |
2114 | ||
2115 | if (run_git_commit(rebase_path_message(), opts, 1, 1, amend, 0)) | |
2116 | return error(_("could not commit staged changes.")); | |
2117 | unlink(rebase_path_amend()); | |
2118 | return 0; | |
2119 | } | |
2120 | ||
2863584f | 2121 | int sequencer_continue(struct replay_opts *opts) |
043a4492 | 2122 | { |
004fefa7 JS |
2123 | struct todo_list todo_list = TODO_LIST_INIT; |
2124 | int res; | |
043a4492 | 2125 | |
2863584f JS |
2126 | if (read_and_refresh_cache(opts)) |
2127 | return -1; | |
2128 | ||
9d93ccd1 JS |
2129 | if (is_rebase_i(opts)) { |
2130 | if (commit_staged_changes(opts)) | |
2131 | return -1; | |
4258a6da | 2132 | } else if (!file_exists(get_todo_path(opts))) |
043a4492 | 2133 | return continue_single_pick(); |
004fefa7 | 2134 | if (read_populate_opts(opts)) |
0ae42a03 | 2135 | return -1; |
004fefa7 JS |
2136 | if ((res = read_populate_todo(&todo_list, opts))) |
2137 | goto release_todo_list; | |
043a4492 | 2138 | |
4258a6da JS |
2139 | if (!is_rebase_i(opts)) { |
2140 | /* Verify that the conflict has been resolved */ | |
2141 | if (file_exists(git_path_cherry_pick_head()) || | |
2142 | file_exists(git_path_revert_head())) { | |
2143 | res = continue_single_pick(); | |
2144 | if (res) | |
2145 | goto release_todo_list; | |
2146 | } | |
2147 | if (index_differs_from("HEAD", 0, 0)) { | |
2148 | res = error_dirty_index(opts); | |
004fefa7 | 2149 | goto release_todo_list; |
4258a6da JS |
2150 | } |
2151 | todo_list.current++; | |
ca98c6d4 JS |
2152 | } else if (file_exists(rebase_path_stopped_sha())) { |
2153 | struct strbuf buf = STRBUF_INIT; | |
2154 | struct object_id oid; | |
2155 | ||
2156 | if (read_oneliner(&buf, rebase_path_stopped_sha(), 1) && | |
2157 | !get_sha1_committish(buf.buf, oid.hash)) | |
2158 | record_in_rewritten(&oid, peek_command(&todo_list, 0)); | |
2159 | strbuf_release(&buf); | |
043a4492 | 2160 | } |
4258a6da | 2161 | |
004fefa7 JS |
2162 | res = pick_commits(&todo_list, opts); |
2163 | release_todo_list: | |
2164 | todo_list_release(&todo_list); | |
2165 | return res; | |
043a4492 RR |
2166 | } |
2167 | ||
2168 | static int single_pick(struct commit *cmit, struct replay_opts *opts) | |
2169 | { | |
2170 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
004fefa7 | 2171 | return do_pick_commit(opts->action == REPLAY_PICK ? |
6e98de72 | 2172 | TODO_PICK : TODO_REVERT, cmit, opts, 0); |
043a4492 RR |
2173 | } |
2174 | ||
2175 | int sequencer_pick_revisions(struct replay_opts *opts) | |
2176 | { | |
004fefa7 | 2177 | struct todo_list todo_list = TODO_LIST_INIT; |
043a4492 | 2178 | unsigned char sha1[20]; |
004fefa7 | 2179 | int i, res; |
043a4492 | 2180 | |
2863584f | 2181 | assert(opts->revs); |
0d9c6dc9 JS |
2182 | if (read_and_refresh_cache(opts)) |
2183 | return -1; | |
043a4492 | 2184 | |
21246dbb MV |
2185 | for (i = 0; i < opts->revs->pending.nr; i++) { |
2186 | unsigned char sha1[20]; | |
2187 | const char *name = opts->revs->pending.objects[i].name; | |
2188 | ||
2189 | /* This happens when using --stdin. */ | |
2190 | if (!strlen(name)) | |
2191 | continue; | |
2192 | ||
2193 | if (!get_sha1(name, sha1)) { | |
7c0b0d8d JH |
2194 | if (!lookup_commit_reference_gently(sha1, 1)) { |
2195 | enum object_type type = sha1_object_info(sha1, NULL); | |
b9b946d4 JS |
2196 | return error(_("%s: can't cherry-pick a %s"), |
2197 | name, typename(type)); | |
7c0b0d8d | 2198 | } |
21246dbb | 2199 | } else |
b9b946d4 | 2200 | return error(_("%s: bad revision"), name); |
21246dbb MV |
2201 | } |
2202 | ||
043a4492 RR |
2203 | /* |
2204 | * If we were called as "git cherry-pick <commit>", just | |
2205 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / | |
2206 | * REVERT_HEAD, and don't touch the sequencer state. | |
2207 | * This means it is possible to cherry-pick in the middle | |
2208 | * of a cherry-pick sequence. | |
2209 | */ | |
2210 | if (opts->revs->cmdline.nr == 1 && | |
2211 | opts->revs->cmdline.rev->whence == REV_CMD_REV && | |
2212 | opts->revs->no_walk && | |
2213 | !opts->revs->cmdline.rev->flags) { | |
2214 | struct commit *cmit; | |
2215 | if (prepare_revision_walk(opts->revs)) | |
b9b946d4 | 2216 | return error(_("revision walk setup failed")); |
043a4492 RR |
2217 | cmit = get_revision(opts->revs); |
2218 | if (!cmit || get_revision(opts->revs)) | |
b9b946d4 | 2219 | return error("BUG: expected exactly one commit from walk"); |
043a4492 RR |
2220 | return single_pick(cmit, opts); |
2221 | } | |
2222 | ||
2223 | /* | |
2224 | * Start a new cherry-pick/ revert sequence; but | |
2225 | * first, make sure that an existing one isn't in | |
2226 | * progress | |
2227 | */ | |
2228 | ||
34b0528b JS |
2229 | if (walk_revs_populate_todo(&todo_list, opts) || |
2230 | create_seq_dir() < 0) | |
043a4492 | 2231 | return -1; |
0f974e21 | 2232 | if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT)) |
93b3df6f | 2233 | return error(_("can't revert as initial commit")); |
311fd397 JS |
2234 | if (save_head(sha1_to_hex(sha1))) |
2235 | return -1; | |
88d5a271 JS |
2236 | if (save_opts(opts)) |
2237 | return -1; | |
1e41229d | 2238 | update_abort_safety_file(); |
004fefa7 JS |
2239 | res = pick_commits(&todo_list, opts); |
2240 | todo_list_release(&todo_list); | |
2241 | return res; | |
043a4492 | 2242 | } |
5ed75e2a | 2243 | |
bab4d109 | 2244 | void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) |
5ed75e2a | 2245 | { |
bab4d109 | 2246 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
5ed75e2a | 2247 | struct strbuf sob = STRBUF_INIT; |
bab4d109 | 2248 | int has_footer; |
5ed75e2a MV |
2249 | |
2250 | strbuf_addstr(&sob, sign_off_header); | |
2251 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), | |
2252 | getenv("GIT_COMMITTER_EMAIL"))); | |
2253 | strbuf_addch(&sob, '\n'); | |
bab4d109 BC |
2254 | |
2255 | /* | |
2256 | * If the whole message buffer is equal to the sob, pretend that we | |
2257 | * found a conforming footer with a matching sob | |
2258 | */ | |
2259 | if (msgbuf->len - ignore_footer == sob.len && | |
2260 | !strncmp(msgbuf->buf, sob.buf, sob.len)) | |
2261 | has_footer = 3; | |
2262 | else | |
2263 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); | |
2264 | ||
33f2f9ab BC |
2265 | if (!has_footer) { |
2266 | const char *append_newlines = NULL; | |
2267 | size_t len = msgbuf->len - ignore_footer; | |
2268 | ||
8c613fd5 BC |
2269 | if (!len) { |
2270 | /* | |
2271 | * The buffer is completely empty. Leave foom for | |
2272 | * the title and body to be filled in by the user. | |
2273 | */ | |
33f2f9ab | 2274 | append_newlines = "\n\n"; |
8c613fd5 BC |
2275 | } else if (msgbuf->buf[len - 1] != '\n') { |
2276 | /* | |
2277 | * Incomplete line. Complete the line and add a | |
2278 | * blank one so that there is an empty line between | |
2279 | * the message body and the sob. | |
2280 | */ | |
2281 | append_newlines = "\n\n"; | |
2282 | } else if (len == 1) { | |
2283 | /* | |
2284 | * Buffer contains a single newline. Add another | |
2285 | * so that we leave room for the title and body. | |
2286 | */ | |
2287 | append_newlines = "\n"; | |
2288 | } else if (msgbuf->buf[len - 2] != '\n') { | |
2289 | /* | |
2290 | * Buffer ends with a single newline. Add another | |
2291 | * so that there is an empty line between the message | |
2292 | * body and the sob. | |
2293 | */ | |
33f2f9ab | 2294 | append_newlines = "\n"; |
8c613fd5 | 2295 | } /* else, the buffer already ends with two newlines. */ |
33f2f9ab BC |
2296 | |
2297 | if (append_newlines) | |
2298 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
2299 | append_newlines, strlen(append_newlines)); | |
5ed75e2a | 2300 | } |
bab4d109 BC |
2301 | |
2302 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) | |
2303 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
2304 | sob.buf, sob.len); | |
2305 | ||
5ed75e2a MV |
2306 | strbuf_release(&sob); |
2307 | } |