]>
Commit | Line | Data |
---|---|---|
26ae337b | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
697cc8ef | 3 | #include "lockfile.h" |
26ae337b | 4 | #include "sequencer.h" |
26ae337b | 5 | #include "dir.h" |
043a4492 RR |
6 | #include "object.h" |
7 | #include "commit.h" | |
8 | #include "tag.h" | |
9 | #include "run-command.h" | |
10 | #include "exec_cmd.h" | |
11 | #include "utf8.h" | |
12 | #include "cache-tree.h" | |
13 | #include "diff.h" | |
14 | #include "revision.h" | |
15 | #include "rerere.h" | |
16 | #include "merge-recursive.h" | |
17 | #include "refs.h" | |
b27cfb0d | 18 | #include "argv-array.h" |
a1c75762 | 19 | #include "quote.h" |
967dfd4d | 20 | #include "trailer.h" |
56dc3ab0 | 21 | #include "log-tree.h" |
311af526 | 22 | #include "wt-status.h" |
c44a4c65 | 23 | #include "hashmap.h" |
043a4492 RR |
24 | |
25 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" | |
26ae337b | 26 | |
5ed75e2a | 27 | const char sign_off_header[] = "Signed-off-by: "; |
cd650a4e | 28 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
5ed75e2a | 29 | |
8a2a0f53 JS |
30 | GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
31 | ||
32 | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") | |
33 | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") | |
34 | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") | |
1e41229d | 35 | static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety") |
f932729c | 36 | |
84583957 JS |
37 | static GIT_PATH_FUNC(rebase_path, "rebase-merge") |
38 | /* | |
39 | * The file containing rebase commands, comments, and empty lines. | |
40 | * This file is created by "git rebase -i" then edited by the user. As | |
41 | * the lines are processed, they are removed from the front of this | |
42 | * file and written to the tail of 'done'. | |
43 | */ | |
44 | static GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") | |
1df6df0c JS |
45 | /* |
46 | * The rebase command lines that have already been processed. A line | |
47 | * is moved here when it is first handled, before any associated user | |
48 | * actions. | |
49 | */ | |
50 | static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done") | |
ef80069a JS |
51 | /* |
52 | * The file to keep track of how many commands were already processed (e.g. | |
53 | * for the prompt). | |
54 | */ | |
55 | static GIT_PATH_FUNC(rebase_path_msgnum, "rebase-merge/msgnum"); | |
56 | /* | |
57 | * The file to keep track of how many commands are to be processed in total | |
58 | * (e.g. for the prompt). | |
59 | */ | |
60 | static GIT_PATH_FUNC(rebase_path_msgtotal, "rebase-merge/end"); | |
6e98de72 JS |
61 | /* |
62 | * The commit message that is planned to be used for any changes that | |
63 | * need to be committed following a user interaction. | |
64 | */ | |
65 | static GIT_PATH_FUNC(rebase_path_message, "rebase-merge/message") | |
66 | /* | |
67 | * The file into which is accumulated the suggested commit message for | |
68 | * squash/fixup commands. When the first of a series of squash/fixups | |
69 | * is seen, the file is created and the commit message from the | |
70 | * previous commit and from the first squash/fixup commit are written | |
71 | * to it. The commit message for each subsequent squash/fixup commit | |
72 | * is appended to the file as it is processed. | |
73 | * | |
74 | * The first line of the file is of the form | |
75 | * # This is a combination of $count commits. | |
76 | * where $count is the number of commits whose messages have been | |
77 | * written to the file so far (including the initial "pick" commit). | |
78 | * Each time that a commit message is processed, this line is read and | |
79 | * updated. It is deleted just before the combined commit is made. | |
80 | */ | |
81 | static GIT_PATH_FUNC(rebase_path_squash_msg, "rebase-merge/message-squash") | |
82 | /* | |
83 | * If the current series of squash/fixups has not yet included a squash | |
84 | * command, then this file exists and holds the commit message of the | |
85 | * original "pick" commit. (If the series ends without a "squash" | |
86 | * command, then this can be used as the commit message of the combined | |
87 | * commit without opening the editor.) | |
88 | */ | |
89 | static GIT_PATH_FUNC(rebase_path_fixup_msg, "rebase-merge/message-fixup") | |
b5a67045 JS |
90 | /* |
91 | * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and | |
92 | * GIT_AUTHOR_DATE that will be used for the commit that is currently | |
93 | * being rebased. | |
94 | */ | |
95 | static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") | |
56dc3ab0 JS |
96 | /* |
97 | * When an "edit" rebase command is being processed, the SHA1 of the | |
98 | * commit to be edited is recorded in this file. When "git rebase | |
99 | * --continue" is executed, if there are any staged changes then they | |
100 | * will be amended to the HEAD commit, but only provided the HEAD | |
101 | * commit is still the commit to be edited. When any other rebase | |
102 | * command is processed, this file is deleted. | |
103 | */ | |
104 | static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend") | |
105 | /* | |
106 | * When we stop at a given patch via the "edit" command, this file contains | |
107 | * the abbreviated commit name of the corresponding patch. | |
108 | */ | |
109 | static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha") | |
25cb8df9 JS |
110 | /* |
111 | * For the post-rewrite hook, we make a list of rewritten commits and | |
112 | * their new sha1s. The rewritten-pending list keeps the sha1s of | |
113 | * commits that have been processed, but not committed yet, | |
114 | * e.g. because they are waiting for a 'squash' command. | |
115 | */ | |
116 | static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list") | |
117 | static GIT_PATH_FUNC(rebase_path_rewritten_pending, | |
118 | "rebase-merge/rewritten-pending") | |
a1c75762 JS |
119 | /* |
120 | * The following files are written by git-rebase just after parsing the | |
121 | * command-line (and are only consumed, not modified, by the sequencer). | |
122 | */ | |
123 | static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") | |
556907f1 JS |
124 | static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") |
125 | static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") | |
4b83ce9f JS |
126 | static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") |
127 | static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") | |
796c7972 | 128 | static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") |
ca6c6b45 JS |
129 | static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy") |
130 | static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") | |
9b6d7a62 | 131 | static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate") |
b5a67045 | 132 | |
b5a67045 JS |
133 | static inline int is_rebase_i(const struct replay_opts *opts) |
134 | { | |
84583957 | 135 | return opts->action == REPLAY_INTERACTIVE_REBASE; |
b5a67045 JS |
136 | } |
137 | ||
285abf56 JS |
138 | static const char *get_dir(const struct replay_opts *opts) |
139 | { | |
84583957 JS |
140 | if (is_rebase_i(opts)) |
141 | return rebase_path(); | |
285abf56 JS |
142 | return git_path_seq_dir(); |
143 | } | |
144 | ||
c0246501 JS |
145 | static const char *get_todo_path(const struct replay_opts *opts) |
146 | { | |
84583957 JS |
147 | if (is_rebase_i(opts)) |
148 | return rebase_path_todo(); | |
c0246501 JS |
149 | return git_path_todo_file(); |
150 | } | |
151 | ||
bab4d109 BC |
152 | /* |
153 | * Returns 0 for non-conforming footer | |
154 | * Returns 1 for conforming footer | |
155 | * Returns 2 when sob exists within conforming footer | |
156 | * Returns 3 when sob exists within conforming footer as last entry | |
157 | */ | |
158 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, | |
159 | int ignore_footer) | |
b971e04f | 160 | { |
967dfd4d JT |
161 | struct trailer_info info; |
162 | int i; | |
163 | int found_sob = 0, found_sob_last = 0; | |
b971e04f | 164 | |
967dfd4d | 165 | trailer_info_get(&info, sb->buf); |
b971e04f | 166 | |
967dfd4d | 167 | if (info.trailer_start == info.trailer_end) |
b971e04f BC |
168 | return 0; |
169 | ||
967dfd4d JT |
170 | for (i = 0; i < info.trailer_nr; i++) |
171 | if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { | |
172 | found_sob = 1; | |
173 | if (i == info.trailer_nr - 1) | |
174 | found_sob_last = 1; | |
175 | } | |
b971e04f | 176 | |
967dfd4d | 177 | trailer_info_release(&info); |
bab4d109 | 178 | |
967dfd4d | 179 | if (found_sob_last) |
bab4d109 BC |
180 | return 3; |
181 | if (found_sob) | |
182 | return 2; | |
b971e04f BC |
183 | return 1; |
184 | } | |
5ed75e2a | 185 | |
a1c75762 JS |
186 | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
187 | { | |
188 | static struct strbuf buf = STRBUF_INIT; | |
189 | ||
190 | strbuf_reset(&buf); | |
191 | if (opts->gpg_sign) | |
192 | sq_quotef(&buf, "-S%s", opts->gpg_sign); | |
193 | return buf.buf; | |
194 | } | |
195 | ||
2863584f | 196 | int sequencer_remove_state(struct replay_opts *opts) |
26ae337b | 197 | { |
285abf56 | 198 | struct strbuf dir = STRBUF_INIT; |
03a4e260 JS |
199 | int i; |
200 | ||
201 | free(opts->gpg_sign); | |
202 | free(opts->strategy); | |
203 | for (i = 0; i < opts->xopts_nr; i++) | |
204 | free(opts->xopts[i]); | |
205 | free(opts->xopts); | |
26ae337b | 206 | |
72d4a9a7 | 207 | strbuf_addstr(&dir, get_dir(opts)); |
285abf56 JS |
208 | remove_dir_recursively(&dir, 0); |
209 | strbuf_release(&dir); | |
2863584f JS |
210 | |
211 | return 0; | |
26ae337b | 212 | } |
043a4492 RR |
213 | |
214 | static const char *action_name(const struct replay_opts *opts) | |
215 | { | |
84583957 JS |
216 | switch (opts->action) { |
217 | case REPLAY_REVERT: | |
218 | return N_("revert"); | |
219 | case REPLAY_PICK: | |
220 | return N_("cherry-pick"); | |
221 | case REPLAY_INTERACTIVE_REBASE: | |
222 | return N_("rebase -i"); | |
223 | } | |
224 | die(_("Unknown action: %d"), opts->action); | |
043a4492 RR |
225 | } |
226 | ||
043a4492 RR |
227 | struct commit_message { |
228 | char *parent_label; | |
7b35eaf8 JK |
229 | char *label; |
230 | char *subject; | |
043a4492 RR |
231 | const char *message; |
232 | }; | |
233 | ||
39755964 JS |
234 | static const char *short_commit_name(struct commit *commit) |
235 | { | |
236 | return find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV); | |
237 | } | |
238 | ||
043a4492 RR |
239 | static int get_message(struct commit *commit, struct commit_message *out) |
240 | { | |
043a4492 | 241 | const char *abbrev, *subject; |
7b35eaf8 | 242 | int subject_len; |
043a4492 | 243 | |
7b35eaf8 | 244 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
39755964 | 245 | abbrev = short_commit_name(commit); |
043a4492 RR |
246 | |
247 | subject_len = find_commit_subject(out->message, &subject); | |
248 | ||
7b35eaf8 JK |
249 | out->subject = xmemdupz(subject, subject_len); |
250 | out->label = xstrfmt("%s... %s", abbrev, out->subject); | |
251 | out->parent_label = xstrfmt("parent of %s", out->label); | |
252 | ||
043a4492 RR |
253 | return 0; |
254 | } | |
255 | ||
d74a4e57 | 256 | static void free_message(struct commit *commit, struct commit_message *msg) |
043a4492 RR |
257 | { |
258 | free(msg->parent_label); | |
7b35eaf8 JK |
259 | free(msg->label); |
260 | free(msg->subject); | |
b66103c3 | 261 | unuse_commit_buffer(commit, msg->message); |
043a4492 RR |
262 | } |
263 | ||
ed727b19 | 264 | static void print_advice(int show_hint, struct replay_opts *opts) |
043a4492 RR |
265 | { |
266 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); | |
267 | ||
268 | if (msg) { | |
269 | fprintf(stderr, "%s\n", msg); | |
270 | /* | |
41ccfdd9 | 271 | * A conflict has occurred but the porcelain |
043a4492 RR |
272 | * (typically rebase --interactive) wants to take care |
273 | * of the commit itself so remove CHERRY_PICK_HEAD | |
274 | */ | |
f932729c | 275 | unlink(git_path_cherry_pick_head()); |
043a4492 RR |
276 | return; |
277 | } | |
278 | ||
ed727b19 PH |
279 | if (show_hint) { |
280 | if (opts->no_commit) | |
281 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
282 | "with 'git add <paths>' or 'git rm <paths>'")); | |
283 | else | |
284 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
285 | "with 'git add <paths>' or 'git rm <paths>'\n" | |
286 | "and commit the result with 'git commit'")); | |
287 | } | |
043a4492 RR |
288 | } |
289 | ||
f56fffef JS |
290 | static int write_message(const void *buf, size_t len, const char *filename, |
291 | int append_eol) | |
043a4492 RR |
292 | { |
293 | static struct lock_file msg_file; | |
294 | ||
4ef3d8f0 JS |
295 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
296 | if (msg_fd < 0) | |
93b3df6f | 297 | return error_errno(_("could not lock '%s'"), filename); |
75871495 | 298 | if (write_in_full(msg_fd, buf, len) < 0) { |
4f66c837 | 299 | rollback_lock_file(&msg_file); |
93b3df6f | 300 | return error_errno(_("could not write to '%s'"), filename); |
4f66c837 | 301 | } |
f56fffef JS |
302 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
303 | rollback_lock_file(&msg_file); | |
35871806 | 304 | return error_errno(_("could not write eol to '%s'"), filename); |
f56fffef | 305 | } |
4f66c837 JS |
306 | if (commit_lock_file(&msg_file) < 0) { |
307 | rollback_lock_file(&msg_file); | |
93b3df6f | 308 | return error(_("failed to finalize '%s'."), filename); |
4f66c837 | 309 | } |
4ef3d8f0 JS |
310 | |
311 | return 0; | |
043a4492 RR |
312 | } |
313 | ||
1dfc84e9 JS |
314 | /* |
315 | * Reads a file that was presumably written by a shell script, i.e. with an | |
316 | * end-of-line marker that needs to be stripped. | |
317 | * | |
318 | * Note that only the last end-of-line marker is stripped, consistent with the | |
319 | * behavior of "$(cat path)" in a shell script. | |
320 | * | |
321 | * Returns 1 if the file was read, 0 if it could not be read or does not exist. | |
322 | */ | |
323 | static int read_oneliner(struct strbuf *buf, | |
324 | const char *path, int skip_if_empty) | |
325 | { | |
326 | int orig_len = buf->len; | |
327 | ||
328 | if (!file_exists(path)) | |
329 | return 0; | |
330 | ||
331 | if (strbuf_read_file(buf, path, 0) < 0) { | |
332 | warning_errno(_("could not read '%s'"), path); | |
333 | return 0; | |
334 | } | |
335 | ||
336 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { | |
337 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') | |
338 | --buf->len; | |
339 | buf->buf[buf->len] = '\0'; | |
340 | } | |
341 | ||
342 | if (skip_if_empty && buf->len == orig_len) | |
343 | return 0; | |
344 | ||
345 | return 1; | |
346 | } | |
347 | ||
043a4492 RR |
348 | static struct tree *empty_tree(void) |
349 | { | |
eb0ccfd7 | 350 | return lookup_tree(the_hash_algo->empty_tree); |
043a4492 RR |
351 | } |
352 | ||
353 | static int error_dirty_index(struct replay_opts *opts) | |
354 | { | |
355 | if (read_cache_unmerged()) | |
c28cbc5e | 356 | return error_resolve_conflict(_(action_name(opts))); |
043a4492 | 357 | |
93b3df6f | 358 | error(_("your local changes would be overwritten by %s."), |
c28cbc5e | 359 | _(action_name(opts))); |
043a4492 RR |
360 | |
361 | if (advice_commit_before_merge) | |
93b3df6f | 362 | advise(_("commit your changes or stash them to proceed.")); |
043a4492 RR |
363 | return -1; |
364 | } | |
365 | ||
1e41229d SB |
366 | static void update_abort_safety_file(void) |
367 | { | |
368 | struct object_id head; | |
369 | ||
370 | /* Do nothing on a single-pick */ | |
371 | if (!file_exists(git_path_seq_dir())) | |
372 | return; | |
373 | ||
374 | if (!get_oid("HEAD", &head)) | |
375 | write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head)); | |
376 | else | |
377 | write_file(git_path_abort_safety_file(), "%s", ""); | |
378 | } | |
379 | ||
ace976b2 | 380 | static int fast_forward_to(const struct object_id *to, const struct object_id *from, |
eb4be1cb | 381 | int unborn, struct replay_opts *opts) |
043a4492 | 382 | { |
d668d16c | 383 | struct ref_transaction *transaction; |
eb4be1cb | 384 | struct strbuf sb = STRBUF_INIT; |
d668d16c | 385 | struct strbuf err = STRBUF_INIT; |
043a4492 RR |
386 | |
387 | read_cache(); | |
db699a8a | 388 | if (checkout_fast_forward(from, to, 1)) |
0e408fc3 | 389 | return -1; /* the callee should have complained already */ |
651ab9f5 | 390 | |
c28cbc5e | 391 | strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts))); |
d668d16c RS |
392 | |
393 | transaction = ref_transaction_begin(&err); | |
394 | if (!transaction || | |
395 | ref_transaction_update(transaction, "HEAD", | |
89f3bbdd | 396 | to, unborn ? &null_oid : from, |
1d147bdf | 397 | 0, sb.buf, &err) || |
db7516ab | 398 | ref_transaction_commit(transaction, &err)) { |
d668d16c RS |
399 | ref_transaction_free(transaction); |
400 | error("%s", err.buf); | |
401 | strbuf_release(&sb); | |
402 | strbuf_release(&err); | |
403 | return -1; | |
404 | } | |
651ab9f5 | 405 | |
eb4be1cb | 406 | strbuf_release(&sb); |
d668d16c RS |
407 | strbuf_release(&err); |
408 | ref_transaction_free(transaction); | |
1e41229d | 409 | update_abort_safety_file(); |
d668d16c | 410 | return 0; |
043a4492 RR |
411 | } |
412 | ||
75c961b7 JH |
413 | void append_conflicts_hint(struct strbuf *msgbuf) |
414 | { | |
415 | int i; | |
416 | ||
261f315b JH |
417 | strbuf_addch(msgbuf, '\n'); |
418 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); | |
75c961b7 JH |
419 | for (i = 0; i < active_nr;) { |
420 | const struct cache_entry *ce = active_cache[i++]; | |
421 | if (ce_stage(ce)) { | |
261f315b | 422 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
75c961b7 JH |
423 | while (i < active_nr && !strcmp(ce->name, |
424 | active_cache[i]->name)) | |
425 | i++; | |
426 | } | |
427 | } | |
428 | } | |
429 | ||
043a4492 RR |
430 | static int do_recursive_merge(struct commit *base, struct commit *next, |
431 | const char *base_label, const char *next_label, | |
48be4c62 | 432 | struct object_id *head, struct strbuf *msgbuf, |
043a4492 RR |
433 | struct replay_opts *opts) |
434 | { | |
435 | struct merge_options o; | |
436 | struct tree *result, *next_tree, *base_tree, *head_tree; | |
03b86647 | 437 | int clean; |
03a4e260 | 438 | char **xopt; |
043a4492 RR |
439 | static struct lock_file index_lock; |
440 | ||
bd588867 PW |
441 | if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0) |
442 | return -1; | |
043a4492 RR |
443 | |
444 | read_cache(); | |
445 | ||
446 | init_merge_options(&o); | |
447 | o.ancestor = base ? base_label : "(empty tree)"; | |
448 | o.branch1 = "HEAD"; | |
449 | o.branch2 = next ? next_label : "(empty tree)"; | |
62fdb652 JS |
450 | if (is_rebase_i(opts)) |
451 | o.buffer_output = 2; | |
9268cf4a | 452 | o.show_rename_progress = 1; |
043a4492 RR |
453 | |
454 | head_tree = parse_tree_indirect(head); | |
455 | next_tree = next ? next->tree : empty_tree(); | |
456 | base_tree = base ? base->tree : empty_tree(); | |
457 | ||
458 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) | |
459 | parse_merge_opt(&o, *xopt); | |
460 | ||
461 | clean = merge_trees(&o, | |
462 | head_tree, | |
463 | next_tree, base_tree, &result); | |
62fdb652 JS |
464 | if (is_rebase_i(opts) && clean <= 0) |
465 | fputs(o.obuf.buf, stdout); | |
548009c0 | 466 | strbuf_release(&o.obuf); |
b520abf1 | 467 | diff_warn_rename_limit("merge.renamelimit", o.needed_rename_limit, 0); |
f241ff0d JS |
468 | if (clean < 0) |
469 | return clean; | |
043a4492 RR |
470 | |
471 | if (active_cache_changed && | |
03b86647 | 472 | write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
66f5f6dc ÆAB |
473 | /* |
474 | * TRANSLATORS: %s will be "revert", "cherry-pick" or | |
84583957 JS |
475 | * "rebase -i". |
476 | */ | |
c527b55e | 477 | return error(_("%s: Unable to write new index file"), |
c28cbc5e | 478 | _(action_name(opts))); |
043a4492 RR |
479 | rollback_lock_file(&index_lock); |
480 | ||
5ed75e2a | 481 | if (opts->signoff) |
bab4d109 | 482 | append_signoff(msgbuf, 0, 0); |
5ed75e2a | 483 | |
75c961b7 JH |
484 | if (!clean) |
485 | append_conflicts_hint(msgbuf); | |
043a4492 RR |
486 | |
487 | return !clean; | |
488 | } | |
489 | ||
b27cfb0d NH |
490 | static int is_index_unchanged(void) |
491 | { | |
33d66df3 | 492 | struct object_id head_oid; |
b27cfb0d NH |
493 | struct commit *head_commit; |
494 | ||
49e61479 | 495 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) |
aee42e1f | 496 | return error(_("could not resolve HEAD commit")); |
b27cfb0d | 497 | |
bc83266a | 498 | head_commit = lookup_commit(&head_oid); |
4b580061 NH |
499 | |
500 | /* | |
501 | * If head_commit is NULL, check_commit, called from | |
502 | * lookup_commit, would have indicated that head_commit is not | |
503 | * a commit object already. parse_commit() will return failure | |
504 | * without further complaints in such a case. Otherwise, if | |
505 | * the commit is invalid, parse_commit() will complain. So | |
506 | * there is nothing for us to say here. Just return failure. | |
507 | */ | |
508 | if (parse_commit(head_commit)) | |
509 | return -1; | |
b27cfb0d NH |
510 | |
511 | if (!active_cache_tree) | |
512 | active_cache_tree = cache_tree(); | |
513 | ||
514 | if (!cache_tree_fully_valid(active_cache_tree)) | |
d0cfc3e8 | 515 | if (cache_tree_update(&the_index, 0)) |
aee42e1f | 516 | return error(_("unable to update cache tree")); |
b27cfb0d | 517 | |
e0a92804 | 518 | return !oidcmp(&active_cache_tree->oid, |
519 | &head_commit->tree->object.oid); | |
b27cfb0d NH |
520 | } |
521 | ||
0473f28a JS |
522 | static int write_author_script(const char *message) |
523 | { | |
524 | struct strbuf buf = STRBUF_INIT; | |
525 | const char *eol; | |
526 | int res; | |
527 | ||
528 | for (;;) | |
529 | if (!*message || starts_with(message, "\n")) { | |
530 | missing_author: | |
531 | /* Missing 'author' line? */ | |
532 | unlink(rebase_path_author_script()); | |
533 | return 0; | |
534 | } else if (skip_prefix(message, "author ", &message)) | |
535 | break; | |
536 | else if ((eol = strchr(message, '\n'))) | |
537 | message = eol + 1; | |
538 | else | |
539 | goto missing_author; | |
540 | ||
541 | strbuf_addstr(&buf, "GIT_AUTHOR_NAME='"); | |
542 | while (*message && *message != '\n' && *message != '\r') | |
543 | if (skip_prefix(message, " <", &message)) | |
544 | break; | |
545 | else if (*message != '\'') | |
546 | strbuf_addch(&buf, *(message++)); | |
547 | else | |
548 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); | |
549 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='"); | |
550 | while (*message && *message != '\n' && *message != '\r') | |
551 | if (skip_prefix(message, "> ", &message)) | |
552 | break; | |
553 | else if (*message != '\'') | |
554 | strbuf_addch(&buf, *(message++)); | |
555 | else | |
556 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); | |
557 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@"); | |
558 | while (*message && *message != '\n' && *message != '\r') | |
559 | if (*message != '\'') | |
560 | strbuf_addch(&buf, *(message++)); | |
561 | else | |
562 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); | |
563 | res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1); | |
564 | strbuf_release(&buf); | |
565 | return res; | |
566 | } | |
567 | ||
b5a67045 | 568 | /* |
a2a20b0d JS |
569 | * Read a list of environment variable assignments (such as the author-script |
570 | * file) into an environment block. Returns -1 on error, 0 otherwise. | |
b5a67045 | 571 | */ |
a2a20b0d | 572 | static int read_env_script(struct argv_array *env) |
b5a67045 JS |
573 | { |
574 | struct strbuf script = STRBUF_INIT; | |
575 | int i, count = 0; | |
a2a20b0d | 576 | char *p, *p2; |
b5a67045 JS |
577 | |
578 | if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0) | |
a2a20b0d | 579 | return -1; |
b5a67045 JS |
580 | |
581 | for (p = script.buf; *p; p++) | |
582 | if (skip_prefix(p, "'\\\\''", (const char **)&p2)) | |
583 | strbuf_splice(&script, p - script.buf, p2 - p, "'", 1); | |
584 | else if (*p == '\'') | |
585 | strbuf_splice(&script, p-- - script.buf, 1, "", 0); | |
586 | else if (*p == '\n') { | |
587 | *p = '\0'; | |
588 | count++; | |
589 | } | |
590 | ||
a2a20b0d JS |
591 | for (i = 0, p = script.buf; i < count; i++) { |
592 | argv_array_push(env, p); | |
b5a67045 JS |
593 | p += strlen(p) + 1; |
594 | } | |
b5a67045 | 595 | |
a2a20b0d | 596 | return 0; |
b5a67045 JS |
597 | } |
598 | ||
791eb870 JS |
599 | static const char staged_changes_advice[] = |
600 | N_("you have staged changes in your working tree\n" | |
601 | "If these changes are meant to be squashed into the previous commit, run:\n" | |
602 | "\n" | |
603 | " git commit --amend %s\n" | |
604 | "\n" | |
605 | "If they are meant to go into a new commit, run:\n" | |
606 | "\n" | |
607 | " git commit %s\n" | |
608 | "\n" | |
609 | "In both cases, once you're done, continue with:\n" | |
610 | "\n" | |
611 | " git rebase --continue\n"); | |
612 | ||
789b3eff JS |
613 | #define ALLOW_EMPTY (1<<0) |
614 | #define EDIT_MSG (1<<1) | |
615 | #define AMEND_MSG (1<<2) | |
616 | #define CLEANUP_MSG (1<<3) | |
b92ff6e8 | 617 | #define VERIFY_MSG (1<<4) |
789b3eff | 618 | |
043a4492 RR |
619 | /* |
620 | * If we are cherry-pick, and if the merge did not result in | |
621 | * hand-editing, we will hit this commit and inherit the original | |
622 | * author date and name. | |
b5a67045 | 623 | * |
043a4492 RR |
624 | * If we are revert, or if our cherry-pick results in a hand merge, |
625 | * we had better say that the current user is responsible for that. | |
b5a67045 JS |
626 | * |
627 | * An exception is when run_git_commit() is called during an | |
628 | * interactive rebase: in that case, we will want to retain the | |
629 | * author metadata. | |
043a4492 | 630 | */ |
ac2b0e8f | 631 | static int run_git_commit(const char *defmsg, struct replay_opts *opts, |
789b3eff | 632 | unsigned int flags) |
043a4492 | 633 | { |
07d968ef | 634 | struct child_process cmd = CHILD_PROCESS_INIT; |
17d65f03 | 635 | const char *value; |
b27cfb0d | 636 | |
07d968ef JS |
637 | cmd.git_cmd = 1; |
638 | ||
b5a67045 | 639 | if (is_rebase_i(opts)) { |
789b3eff | 640 | if (!(flags & EDIT_MSG)) { |
9a757c49 JS |
641 | cmd.stdout_to_stderr = 1; |
642 | cmd.err = -1; | |
643 | } | |
644 | ||
07d968ef | 645 | if (read_env_script(&cmd.env_array)) { |
a1c75762 JS |
646 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
647 | ||
791eb870 JS |
648 | return error(_(staged_changes_advice), |
649 | gpg_opt, gpg_opt); | |
a1c75762 | 650 | } |
b5a67045 JS |
651 | } |
652 | ||
07d968ef | 653 | argv_array_push(&cmd.args, "commit"); |
043a4492 | 654 | |
b92ff6e8 JS |
655 | if (!(flags & VERIFY_MSG)) |
656 | argv_array_push(&cmd.args, "-n"); | |
789b3eff | 657 | if ((flags & AMEND_MSG)) |
07d968ef | 658 | argv_array_push(&cmd.args, "--amend"); |
3bdd5522 | 659 | if (opts->gpg_sign) |
07d968ef | 660 | argv_array_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
043a4492 | 661 | if (opts->signoff) |
07d968ef | 662 | argv_array_push(&cmd.args, "-s"); |
b5a67045 | 663 | if (defmsg) |
07d968ef | 664 | argv_array_pushl(&cmd.args, "-F", defmsg, NULL); |
789b3eff | 665 | if ((flags & CLEANUP_MSG)) |
07d968ef | 666 | argv_array_push(&cmd.args, "--cleanup=strip"); |
789b3eff | 667 | if ((flags & EDIT_MSG)) |
07d968ef | 668 | argv_array_push(&cmd.args, "-e"); |
789b3eff | 669 | else if (!(flags & CLEANUP_MSG) && |
0009426d | 670 | !opts->signoff && !opts->record_origin && |
b5a67045 | 671 | git_config_get_value("commit.cleanup", &value)) |
07d968ef | 672 | argv_array_push(&cmd.args, "--cleanup=verbatim"); |
b27cfb0d | 673 | |
789b3eff | 674 | if ((flags & ALLOW_EMPTY)) |
07d968ef | 675 | argv_array_push(&cmd.args, "--allow-empty"); |
df478b74 | 676 | |
4bee9584 | 677 | if (opts->allow_empty_message) |
07d968ef | 678 | argv_array_push(&cmd.args, "--allow-empty-message"); |
4bee9584 | 679 | |
9a757c49 JS |
680 | if (cmd.err == -1) { |
681 | /* hide stderr on success */ | |
682 | struct strbuf buf = STRBUF_INIT; | |
683 | int rc = pipe_command(&cmd, | |
684 | NULL, 0, | |
685 | /* stdout is already redirected */ | |
686 | NULL, 0, | |
687 | &buf, 0); | |
688 | if (rc) | |
689 | fputs(buf.buf, stderr); | |
690 | strbuf_release(&buf); | |
691 | return rc; | |
692 | } | |
b5a67045 | 693 | |
07d968ef | 694 | return run_command(&cmd); |
b27cfb0d NH |
695 | } |
696 | ||
697 | static int is_original_commit_empty(struct commit *commit) | |
698 | { | |
092bbcdf | 699 | const struct object_id *ptree_oid; |
b27cfb0d NH |
700 | |
701 | if (parse_commit(commit)) | |
aee42e1f | 702 | return error(_("could not parse commit %s"), |
f2fd0760 | 703 | oid_to_hex(&commit->object.oid)); |
b27cfb0d NH |
704 | if (commit->parents) { |
705 | struct commit *parent = commit->parents->item; | |
706 | if (parse_commit(parent)) | |
aee42e1f | 707 | return error(_("could not parse parent commit %s"), |
f2fd0760 | 708 | oid_to_hex(&parent->object.oid)); |
092bbcdf | 709 | ptree_oid = &parent->tree->object.oid; |
b27cfb0d | 710 | } else { |
eb0ccfd7 | 711 | ptree_oid = the_hash_algo->empty_tree; /* commit is root */ |
043a4492 | 712 | } |
043a4492 | 713 | |
092bbcdf | 714 | return !oidcmp(ptree_oid, &commit->tree->object.oid); |
043a4492 RR |
715 | } |
716 | ||
ac2b0e8f JH |
717 | /* |
718 | * Do we run "git commit" with "--allow-empty"? | |
719 | */ | |
720 | static int allow_empty(struct replay_opts *opts, struct commit *commit) | |
721 | { | |
722 | int index_unchanged, empty_commit; | |
723 | ||
724 | /* | |
725 | * Three cases: | |
726 | * | |
727 | * (1) we do not allow empty at all and error out. | |
728 | * | |
729 | * (2) we allow ones that were initially empty, but | |
730 | * forbid the ones that become empty; | |
731 | * | |
732 | * (3) we allow both. | |
733 | */ | |
734 | if (!opts->allow_empty) | |
735 | return 0; /* let "git commit" barf as necessary */ | |
736 | ||
737 | index_unchanged = is_index_unchanged(); | |
738 | if (index_unchanged < 0) | |
739 | return index_unchanged; | |
740 | if (!index_unchanged) | |
741 | return 0; /* we do not have to say --allow-empty */ | |
742 | ||
743 | if (opts->keep_redundant_commits) | |
744 | return 1; | |
745 | ||
746 | empty_commit = is_original_commit_empty(commit); | |
747 | if (empty_commit < 0) | |
748 | return empty_commit; | |
749 | if (!empty_commit) | |
750 | return 0; | |
751 | else | |
752 | return 1; | |
753 | } | |
754 | ||
25c43667 JS |
755 | /* |
756 | * Note that ordering matters in this enum. Not only must it match the mapping | |
757 | * below, it is also divided into several sections that matter. When adding | |
758 | * new commands, make sure you add it in the right section. | |
759 | */ | |
004fefa7 | 760 | enum todo_command { |
25c43667 | 761 | /* commands that handle commits */ |
004fefa7 | 762 | TODO_PICK = 0, |
25c43667 | 763 | TODO_REVERT, |
56dc3ab0 | 764 | TODO_EDIT, |
04efc8b5 | 765 | TODO_REWORD, |
6e98de72 JS |
766 | TODO_FIXUP, |
767 | TODO_SQUASH, | |
311af526 JS |
768 | /* commands that do something else than handling a single commit */ |
769 | TODO_EXEC, | |
25c43667 | 770 | /* commands that do nothing but are counted for reporting progress */ |
b3fdd581 | 771 | TODO_NOOP, |
ac191470 JS |
772 | TODO_DROP, |
773 | /* comments (not counted for reporting progress) */ | |
774 | TODO_COMMENT | |
004fefa7 JS |
775 | }; |
776 | ||
414697a9 JS |
777 | static struct { |
778 | char c; | |
779 | const char *str; | |
780 | } todo_command_info[] = { | |
781 | { 'p', "pick" }, | |
782 | { 0, "revert" }, | |
783 | { 'e', "edit" }, | |
04efc8b5 | 784 | { 'r', "reword" }, |
414697a9 JS |
785 | { 'f', "fixup" }, |
786 | { 's', "squash" }, | |
787 | { 'x', "exec" }, | |
b3fdd581 | 788 | { 0, "noop" }, |
ac191470 JS |
789 | { 'd', "drop" }, |
790 | { 0, NULL } | |
004fefa7 JS |
791 | }; |
792 | ||
793 | static const char *command_to_string(const enum todo_command command) | |
794 | { | |
ac191470 | 795 | if (command < TODO_COMMENT) |
414697a9 | 796 | return todo_command_info[command].str; |
004fefa7 JS |
797 | die("Unknown command: %d", command); |
798 | } | |
799 | ||
ee5462d6 | 800 | static char command_to_char(const enum todo_command command) |
d8ae6c84 LB |
801 | { |
802 | if (command < TODO_COMMENT && todo_command_info[command].c) | |
803 | return todo_command_info[command].c; | |
804 | return comment_line_char; | |
805 | } | |
806 | ||
25c43667 JS |
807 | static int is_noop(const enum todo_command command) |
808 | { | |
b3fdd581 | 809 | return TODO_NOOP <= command; |
25c43667 | 810 | } |
004fefa7 | 811 | |
6e98de72 JS |
812 | static int is_fixup(enum todo_command command) |
813 | { | |
814 | return command == TODO_FIXUP || command == TODO_SQUASH; | |
815 | } | |
816 | ||
817 | static int update_squash_messages(enum todo_command command, | |
818 | struct commit *commit, struct replay_opts *opts) | |
819 | { | |
820 | struct strbuf buf = STRBUF_INIT; | |
821 | int count, res; | |
822 | const char *message, *body; | |
823 | ||
824 | if (file_exists(rebase_path_squash_msg())) { | |
825 | struct strbuf header = STRBUF_INIT; | |
826 | char *eol, *p; | |
827 | ||
828 | if (strbuf_read_file(&buf, rebase_path_squash_msg(), 2048) <= 0) | |
829 | return error(_("could not read '%s'"), | |
830 | rebase_path_squash_msg()); | |
831 | ||
832 | p = buf.buf + 1; | |
833 | eol = strchrnul(buf.buf, '\n'); | |
834 | if (buf.buf[0] != comment_line_char || | |
835 | (p += strcspn(p, "0123456789\n")) == eol) | |
836 | return error(_("unexpected 1st line of squash message:" | |
837 | "\n\n\t%.*s"), | |
838 | (int)(eol - buf.buf), buf.buf); | |
839 | count = strtol(p, NULL, 10); | |
840 | ||
841 | if (count < 1) | |
842 | return error(_("invalid 1st line of squash message:\n" | |
843 | "\n\t%.*s"), | |
844 | (int)(eol - buf.buf), buf.buf); | |
845 | ||
846 | strbuf_addf(&header, "%c ", comment_line_char); | |
847 | strbuf_addf(&header, | |
848 | _("This is a combination of %d commits."), ++count); | |
849 | strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len); | |
850 | strbuf_release(&header); | |
851 | } else { | |
33d66df3 | 852 | struct object_id head; |
6e98de72 JS |
853 | struct commit *head_commit; |
854 | const char *head_message, *body; | |
855 | ||
33d66df3 | 856 | if (get_oid("HEAD", &head)) |
6e98de72 | 857 | return error(_("need a HEAD to fixup")); |
bc83266a | 858 | if (!(head_commit = lookup_commit_reference(&head))) |
6e98de72 JS |
859 | return error(_("could not read HEAD")); |
860 | if (!(head_message = get_commit_buffer(head_commit, NULL))) | |
861 | return error(_("could not read HEAD's commit message")); | |
862 | ||
863 | find_commit_subject(head_message, &body); | |
864 | if (write_message(body, strlen(body), | |
865 | rebase_path_fixup_msg(), 0)) { | |
866 | unuse_commit_buffer(head_commit, head_message); | |
867 | return error(_("cannot write '%s'"), | |
868 | rebase_path_fixup_msg()); | |
869 | } | |
870 | ||
871 | count = 2; | |
872 | strbuf_addf(&buf, "%c ", comment_line_char); | |
873 | strbuf_addf(&buf, _("This is a combination of %d commits."), | |
874 | count); | |
875 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
876 | strbuf_addstr(&buf, _("This is the 1st commit message:")); | |
877 | strbuf_addstr(&buf, "\n\n"); | |
878 | strbuf_addstr(&buf, body); | |
879 | ||
880 | unuse_commit_buffer(head_commit, head_message); | |
881 | } | |
882 | ||
883 | if (!(message = get_commit_buffer(commit, NULL))) | |
884 | return error(_("could not read commit message of %s"), | |
885 | oid_to_hex(&commit->object.oid)); | |
886 | find_commit_subject(message, &body); | |
887 | ||
888 | if (command == TODO_SQUASH) { | |
889 | unlink(rebase_path_fixup_msg()); | |
890 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
891 | strbuf_addf(&buf, _("This is the commit message #%d:"), count); | |
892 | strbuf_addstr(&buf, "\n\n"); | |
893 | strbuf_addstr(&buf, body); | |
894 | } else if (command == TODO_FIXUP) { | |
895 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
896 | strbuf_addf(&buf, _("The commit message #%d will be skipped:"), | |
897 | count); | |
898 | strbuf_addstr(&buf, "\n\n"); | |
899 | strbuf_add_commented_lines(&buf, body, strlen(body)); | |
900 | } else | |
901 | return error(_("unknown command: %d"), command); | |
902 | unuse_commit_buffer(commit, message); | |
903 | ||
904 | res = write_message(buf.buf, buf.len, rebase_path_squash_msg(), 0); | |
905 | strbuf_release(&buf); | |
906 | return res; | |
907 | } | |
908 | ||
25cb8df9 JS |
909 | static void flush_rewritten_pending(void) { |
910 | struct strbuf buf = STRBUF_INIT; | |
092bbcdf | 911 | struct object_id newoid; |
25cb8df9 JS |
912 | FILE *out; |
913 | ||
092bbcdf | 914 | if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 && |
915 | !get_oid("HEAD", &newoid) && | |
e9d983f1 | 916 | (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) { |
25cb8df9 JS |
917 | char *bol = buf.buf, *eol; |
918 | ||
919 | while (*bol) { | |
920 | eol = strchrnul(bol, '\n'); | |
921 | fprintf(out, "%.*s %s\n", (int)(eol - bol), | |
092bbcdf | 922 | bol, oid_to_hex(&newoid)); |
25cb8df9 JS |
923 | if (!*eol) |
924 | break; | |
925 | bol = eol + 1; | |
926 | } | |
927 | fclose(out); | |
928 | unlink(rebase_path_rewritten_pending()); | |
929 | } | |
930 | strbuf_release(&buf); | |
931 | } | |
932 | ||
933 | static void record_in_rewritten(struct object_id *oid, | |
934 | enum todo_command next_command) { | |
e9d983f1 | 935 | FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a"); |
25cb8df9 JS |
936 | |
937 | if (!out) | |
938 | return; | |
939 | ||
940 | fprintf(out, "%s\n", oid_to_hex(oid)); | |
941 | fclose(out); | |
942 | ||
943 | if (!is_fixup(next_command)) | |
944 | flush_rewritten_pending(); | |
945 | } | |
946 | ||
004fefa7 | 947 | static int do_pick_commit(enum todo_command command, struct commit *commit, |
6e98de72 | 948 | struct replay_opts *opts, int final_fixup) |
043a4492 | 949 | { |
789b3eff JS |
950 | unsigned int flags = opts->edit ? EDIT_MSG : 0; |
951 | const char *msg_file = opts->edit ? NULL : git_path_merge_msg(); | |
ace976b2 | 952 | struct object_id head; |
043a4492 RR |
953 | struct commit *base, *next, *parent; |
954 | const char *base_label, *next_label; | |
d74a4e57 | 955 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
043a4492 | 956 | struct strbuf msgbuf = STRBUF_INIT; |
789b3eff | 957 | int res, unborn = 0, allow; |
043a4492 RR |
958 | |
959 | if (opts->no_commit) { | |
960 | /* | |
961 | * We do not intend to commit immediately. We just want to | |
962 | * merge the differences in, so let's compute the tree | |
963 | * that represents the "current" state for merge-recursive | |
964 | * to work on. | |
965 | */ | |
ace976b2 | 966 | if (write_cache_as_tree(head.hash, 0, NULL)) |
93b3df6f | 967 | return error(_("your index file is unmerged.")); |
043a4492 | 968 | } else { |
ace976b2 | 969 | unborn = get_oid("HEAD", &head); |
334ae397 | 970 | if (unborn) |
eb0ccfd7 | 971 | oidcpy(&head, the_hash_algo->empty_tree); |
02f2f56b BW |
972 | if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", |
973 | NULL, 0)) | |
043a4492 RR |
974 | return error_dirty_index(opts); |
975 | } | |
976 | discard_cache(); | |
977 | ||
637666c8 | 978 | if (!commit->parents) |
043a4492 | 979 | parent = NULL; |
043a4492 RR |
980 | else if (commit->parents->next) { |
981 | /* Reverting or cherry-picking a merge commit */ | |
982 | int cnt; | |
983 | struct commit_list *p; | |
984 | ||
985 | if (!opts->mainline) | |
93b3df6f | 986 | return error(_("commit %s is a merge but no -m option was given."), |
f2fd0760 | 987 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
988 | |
989 | for (cnt = 1, p = commit->parents; | |
990 | cnt != opts->mainline && p; | |
991 | cnt++) | |
992 | p = p->next; | |
993 | if (cnt != opts->mainline || !p) | |
93b3df6f | 994 | return error(_("commit %s does not have parent %d"), |
f2fd0760 | 995 | oid_to_hex(&commit->object.oid), opts->mainline); |
043a4492 RR |
996 | parent = p->item; |
997 | } else if (0 < opts->mainline) | |
93b3df6f | 998 | return error(_("mainline was specified but commit %s is not a merge."), |
f2fd0760 | 999 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1000 | else |
1001 | parent = commit->parents->item; | |
1002 | ||
bcbb68be JS |
1003 | if (get_message(commit, &msg) != 0) |
1004 | return error(_("cannot get commit message for %s"), | |
1005 | oid_to_hex(&commit->object.oid)); | |
1006 | ||
6e98de72 | 1007 | if (opts->allow_ff && !is_fixup(command) && |
ace976b2 | 1008 | ((parent && !oidcmp(&parent->object.oid, &head)) || |
bcbb68be JS |
1009 | (!parent && unborn))) { |
1010 | if (is_rebase_i(opts)) | |
1011 | write_author_script(msg.message); | |
ace976b2 | 1012 | res = fast_forward_to(&commit->object.oid, &head, unborn, |
bcbb68be JS |
1013 | opts); |
1014 | if (res || command != TODO_REWORD) | |
1015 | goto leave; | |
5f8f9277 | 1016 | flags |= EDIT_MSG | AMEND_MSG | VERIFY_MSG; |
bcbb68be JS |
1017 | msg_file = NULL; |
1018 | goto fast_forward_edit; | |
1019 | } | |
043a4492 | 1020 | if (parent && parse_commit(parent) < 0) |
004fefa7 JS |
1021 | /* TRANSLATORS: The first %s will be a "todo" command like |
1022 | "revert" or "pick", the second %s a SHA1. */ | |
043a4492 | 1023 | return error(_("%s: cannot parse parent commit %s"), |
004fefa7 JS |
1024 | command_to_string(command), |
1025 | oid_to_hex(&parent->object.oid)); | |
043a4492 | 1026 | |
043a4492 RR |
1027 | /* |
1028 | * "commit" is an existing commit. We would want to apply | |
1029 | * the difference it introduces since its first parent "prev" | |
1030 | * on top of the current HEAD if we are cherry-pick. Or the | |
1031 | * reverse of it if we are revert. | |
1032 | */ | |
1033 | ||
004fefa7 | 1034 | if (command == TODO_REVERT) { |
043a4492 RR |
1035 | base = commit; |
1036 | base_label = msg.label; | |
1037 | next = parent; | |
1038 | next_label = msg.parent_label; | |
1039 | strbuf_addstr(&msgbuf, "Revert \""); | |
1040 | strbuf_addstr(&msgbuf, msg.subject); | |
1041 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); | |
f2fd0760 | 1042 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1043 | |
1044 | if (commit->parents && commit->parents->next) { | |
1045 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); | |
f2fd0760 | 1046 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
043a4492 RR |
1047 | } |
1048 | strbuf_addstr(&msgbuf, ".\n"); | |
1049 | } else { | |
1050 | const char *p; | |
1051 | ||
1052 | base = parent; | |
1053 | base_label = msg.parent_label; | |
1054 | next = commit; | |
1055 | next_label = msg.label; | |
1056 | ||
23aa5142 JS |
1057 | /* Append the commit log message to msgbuf. */ |
1058 | if (find_commit_subject(msg.message, &p)) | |
1059 | strbuf_addstr(&msgbuf, p); | |
043a4492 RR |
1060 | |
1061 | if (opts->record_origin) { | |
44dc738a | 1062 | strbuf_complete_line(&msgbuf); |
bab4d109 | 1063 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
b971e04f | 1064 | strbuf_addch(&msgbuf, '\n'); |
cd650a4e | 1065 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
f2fd0760 | 1066 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1067 | strbuf_addstr(&msgbuf, ")\n"); |
1068 | } | |
1069 | } | |
1070 | ||
04efc8b5 | 1071 | if (command == TODO_REWORD) |
b92ff6e8 | 1072 | flags |= EDIT_MSG | VERIFY_MSG; |
04efc8b5 | 1073 | else if (is_fixup(command)) { |
6e98de72 JS |
1074 | if (update_squash_messages(command, commit, opts)) |
1075 | return -1; | |
789b3eff | 1076 | flags |= AMEND_MSG; |
6e98de72 JS |
1077 | if (!final_fixup) |
1078 | msg_file = rebase_path_squash_msg(); | |
1079 | else if (file_exists(rebase_path_fixup_msg())) { | |
789b3eff | 1080 | flags |= CLEANUP_MSG; |
6e98de72 JS |
1081 | msg_file = rebase_path_fixup_msg(); |
1082 | } else { | |
ca03e067 | 1083 | const char *dest = git_path_squash_msg(); |
6e98de72 JS |
1084 | unlink(dest); |
1085 | if (copy_file(dest, rebase_path_squash_msg(), 0666)) | |
1086 | return error(_("could not rename '%s' to '%s'"), | |
1087 | rebase_path_squash_msg(), dest); | |
ca03e067 | 1088 | unlink(git_path_merge_msg()); |
6e98de72 | 1089 | msg_file = dest; |
789b3eff | 1090 | flags |= EDIT_MSG; |
6e98de72 JS |
1091 | } |
1092 | } | |
1093 | ||
0473f28a JS |
1094 | if (is_rebase_i(opts) && write_author_script(msg.message) < 0) |
1095 | res = -1; | |
1096 | else if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) { | |
043a4492 | 1097 | res = do_recursive_merge(base, next, base_label, next_label, |
48be4c62 | 1098 | &head, &msgbuf, opts); |
f241ff0d JS |
1099 | if (res < 0) |
1100 | return res; | |
75871495 | 1101 | res |= write_message(msgbuf.buf, msgbuf.len, |
f56fffef | 1102 | git_path_merge_msg(), 0); |
043a4492 RR |
1103 | } else { |
1104 | struct commit_list *common = NULL; | |
1105 | struct commit_list *remotes = NULL; | |
1106 | ||
75871495 | 1107 | res = write_message(msgbuf.buf, msgbuf.len, |
f56fffef | 1108 | git_path_merge_msg(), 0); |
043a4492 RR |
1109 | |
1110 | commit_list_insert(base, &common); | |
1111 | commit_list_insert(next, &remotes); | |
03a4e260 JS |
1112 | res |= try_merge_command(opts->strategy, |
1113 | opts->xopts_nr, (const char **)opts->xopts, | |
ace976b2 | 1114 | common, oid_to_hex(&head), remotes); |
043a4492 RR |
1115 | free_commit_list(common); |
1116 | free_commit_list(remotes); | |
1117 | } | |
452202c7 | 1118 | strbuf_release(&msgbuf); |
043a4492 RR |
1119 | |
1120 | /* | |
1121 | * If the merge was clean or if it failed due to conflict, we write | |
1122 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. | |
1123 | * However, if the merge did not even start, then we don't want to | |
1124 | * write it at all. | |
1125 | */ | |
004fefa7 | 1126 | if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) && |
ae077771 | 1127 | update_ref(NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL, |
91774afc | 1128 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
dbfad033 | 1129 | res = -1; |
004fefa7 | 1130 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
ae077771 | 1131 | update_ref(NULL, "REVERT_HEAD", &commit->object.oid, NULL, |
91774afc | 1132 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
dbfad033 | 1133 | res = -1; |
043a4492 RR |
1134 | |
1135 | if (res) { | |
004fefa7 | 1136 | error(command == TODO_REVERT |
043a4492 RR |
1137 | ? _("could not revert %s... %s") |
1138 | : _("could not apply %s... %s"), | |
39755964 | 1139 | short_commit_name(commit), msg.subject); |
ed727b19 | 1140 | print_advice(res == 1, opts); |
043a4492 | 1141 | rerere(opts->allow_rerere_auto); |
c8d1351d | 1142 | goto leave; |
043a4492 RR |
1143 | } |
1144 | ||
c8d1351d | 1145 | allow = allow_empty(opts, commit); |
706728a3 FC |
1146 | if (allow < 0) { |
1147 | res = allow; | |
1148 | goto leave; | |
789b3eff JS |
1149 | } else if (allow) |
1150 | flags |= ALLOW_EMPTY; | |
c8d1351d | 1151 | if (!opts->no_commit) |
bcbb68be | 1152 | fast_forward_edit: |
789b3eff | 1153 | res = run_git_commit(msg_file, opts, flags); |
6e98de72 JS |
1154 | |
1155 | if (!res && final_fixup) { | |
1156 | unlink(rebase_path_fixup_msg()); | |
1157 | unlink(rebase_path_squash_msg()); | |
1158 | } | |
c8d1351d FC |
1159 | |
1160 | leave: | |
d74a4e57 | 1161 | free_message(commit, &msg); |
1e41229d | 1162 | update_abort_safety_file(); |
043a4492 RR |
1163 | |
1164 | return res; | |
1165 | } | |
1166 | ||
c3e8618c | 1167 | static int prepare_revs(struct replay_opts *opts) |
043a4492 | 1168 | { |
a73e22e9 MZ |
1169 | /* |
1170 | * picking (but not reverting) ranges (but not individual revisions) | |
1171 | * should be done in reverse | |
1172 | */ | |
1173 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) | |
043a4492 RR |
1174 | opts->revs->reverse ^= 1; |
1175 | ||
1176 | if (prepare_revision_walk(opts->revs)) | |
c3e8618c | 1177 | return error(_("revision walk setup failed")); |
043a4492 RR |
1178 | |
1179 | if (!opts->revs->commits) | |
c3e8618c JS |
1180 | return error(_("empty commit set passed")); |
1181 | return 0; | |
043a4492 RR |
1182 | } |
1183 | ||
0d9c6dc9 | 1184 | static int read_and_refresh_cache(struct replay_opts *opts) |
043a4492 RR |
1185 | { |
1186 | static struct lock_file index_lock; | |
1187 | int index_fd = hold_locked_index(&index_lock, 0); | |
49fb937e JS |
1188 | if (read_index_preload(&the_index, NULL) < 0) { |
1189 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 1190 | return error(_("git %s: failed to read the index"), |
c28cbc5e | 1191 | _(action_name(opts))); |
49fb937e | 1192 | } |
043a4492 | 1193 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
33c297aa | 1194 | if (the_index.cache_changed && index_fd >= 0) { |
49fb937e | 1195 | if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) { |
0d9c6dc9 | 1196 | return error(_("git %s: failed to refresh the index"), |
c28cbc5e | 1197 | _(action_name(opts))); |
49fb937e | 1198 | } |
043a4492 RR |
1199 | } |
1200 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 1201 | return 0; |
043a4492 RR |
1202 | } |
1203 | ||
004fefa7 JS |
1204 | struct todo_item { |
1205 | enum todo_command command; | |
1206 | struct commit *commit; | |
c22f7dfb JS |
1207 | const char *arg; |
1208 | int arg_len; | |
004fefa7 JS |
1209 | size_t offset_in_buf; |
1210 | }; | |
1211 | ||
1212 | struct todo_list { | |
1213 | struct strbuf buf; | |
1214 | struct todo_item *items; | |
1215 | int nr, alloc, current; | |
968492e4 | 1216 | int done_nr, total_nr; |
54fd3243 | 1217 | struct stat_data stat; |
004fefa7 JS |
1218 | }; |
1219 | ||
1220 | #define TODO_LIST_INIT { STRBUF_INIT } | |
1221 | ||
1222 | static void todo_list_release(struct todo_list *todo_list) | |
043a4492 | 1223 | { |
004fefa7 | 1224 | strbuf_release(&todo_list->buf); |
6a83d902 | 1225 | FREE_AND_NULL(todo_list->items); |
004fefa7 JS |
1226 | todo_list->nr = todo_list->alloc = 0; |
1227 | } | |
043a4492 | 1228 | |
004fefa7 JS |
1229 | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
1230 | { | |
1231 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); | |
1232 | return todo_list->items + todo_list->nr++; | |
043a4492 RR |
1233 | } |
1234 | ||
004fefa7 | 1235 | static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) |
043a4492 | 1236 | { |
1e43ed98 | 1237 | struct object_id commit_oid; |
043a4492 | 1238 | char *end_of_object_name; |
004fefa7 JS |
1239 | int i, saved, status, padding; |
1240 | ||
8f8550b3 JS |
1241 | /* left-trim */ |
1242 | bol += strspn(bol, " \t"); | |
1243 | ||
25c43667 | 1244 | if (bol == eol || *bol == '\r' || *bol == comment_line_char) { |
ac191470 | 1245 | item->command = TODO_COMMENT; |
25c43667 JS |
1246 | item->commit = NULL; |
1247 | item->arg = bol; | |
1248 | item->arg_len = eol - bol; | |
1249 | return 0; | |
1250 | } | |
1251 | ||
ac191470 | 1252 | for (i = 0; i < TODO_COMMENT; i++) |
414697a9 JS |
1253 | if (skip_prefix(bol, todo_command_info[i].str, &bol)) { |
1254 | item->command = i; | |
1255 | break; | |
1256 | } else if (bol[1] == ' ' && *bol == todo_command_info[i].c) { | |
1257 | bol++; | |
004fefa7 JS |
1258 | item->command = i; |
1259 | break; | |
1260 | } | |
ac191470 | 1261 | if (i >= TODO_COMMENT) |
004fefa7 | 1262 | return -1; |
043a4492 | 1263 | |
66afa24f JS |
1264 | /* Eat up extra spaces/ tabs before object name */ |
1265 | padding = strspn(bol, " \t"); | |
1266 | bol += padding; | |
1267 | ||
25c43667 | 1268 | if (item->command == TODO_NOOP) { |
66afa24f JS |
1269 | if (bol != eol) |
1270 | return error(_("%s does not accept arguments: '%s'"), | |
1271 | command_to_string(item->command), bol); | |
25c43667 JS |
1272 | item->commit = NULL; |
1273 | item->arg = bol; | |
1274 | item->arg_len = eol - bol; | |
1275 | return 0; | |
1276 | } | |
1277 | ||
043a4492 | 1278 | if (!padding) |
66afa24f JS |
1279 | return error(_("missing arguments for %s"), |
1280 | command_to_string(item->command)); | |
043a4492 | 1281 | |
311af526 | 1282 | if (item->command == TODO_EXEC) { |
7dcbb3cb | 1283 | item->commit = NULL; |
311af526 JS |
1284 | item->arg = bol; |
1285 | item->arg_len = (int)(eol - bol); | |
1286 | return 0; | |
1287 | } | |
1288 | ||
004fefa7 | 1289 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
043a4492 RR |
1290 | saved = *end_of_object_name; |
1291 | *end_of_object_name = '\0'; | |
1e43ed98 | 1292 | status = get_oid(bol, &commit_oid); |
043a4492 RR |
1293 | *end_of_object_name = saved; |
1294 | ||
c22f7dfb JS |
1295 | item->arg = end_of_object_name + strspn(end_of_object_name, " \t"); |
1296 | item->arg_len = (int)(eol - item->arg); | |
1297 | ||
043a4492 | 1298 | if (status < 0) |
004fefa7 | 1299 | return -1; |
043a4492 | 1300 | |
bc83266a | 1301 | item->commit = lookup_commit_reference(&commit_oid); |
004fefa7 | 1302 | return !item->commit; |
043a4492 RR |
1303 | } |
1304 | ||
004fefa7 | 1305 | static int parse_insn_buffer(char *buf, struct todo_list *todo_list) |
043a4492 | 1306 | { |
004fefa7 JS |
1307 | struct todo_item *item; |
1308 | char *p = buf, *next_p; | |
6e98de72 | 1309 | int i, res = 0, fixup_okay = file_exists(rebase_path_done()); |
043a4492 | 1310 | |
004fefa7 | 1311 | for (i = 1; *p; i++, p = next_p) { |
043a4492 | 1312 | char *eol = strchrnul(p, '\n'); |
004fefa7 JS |
1313 | |
1314 | next_p = *eol ? eol + 1 /* skip LF */ : eol; | |
1315 | ||
6307041d JS |
1316 | if (p != eol && eol[-1] == '\r') |
1317 | eol--; /* strip Carriage Return */ | |
1318 | ||
004fefa7 JS |
1319 | item = append_new_todo(todo_list); |
1320 | item->offset_in_buf = p - todo_list->buf.buf; | |
1321 | if (parse_insn_line(item, p, eol)) { | |
93b3df6f | 1322 | res = error(_("invalid line %d: %.*s"), |
004fefa7 | 1323 | i, (int)(eol - p), p); |
6e98de72 | 1324 | item->command = TODO_NOOP; |
004fefa7 | 1325 | } |
6e98de72 JS |
1326 | |
1327 | if (fixup_okay) | |
1328 | ; /* do nothing */ | |
1329 | else if (is_fixup(item->command)) | |
1330 | return error(_("cannot '%s' without a previous commit"), | |
1331 | command_to_string(item->command)); | |
1332 | else if (!is_noop(item->command)) | |
1333 | fixup_okay = 1; | |
043a4492 | 1334 | } |
52865279 | 1335 | |
004fefa7 | 1336 | return res; |
043a4492 RR |
1337 | } |
1338 | ||
968492e4 JS |
1339 | static int count_commands(struct todo_list *todo_list) |
1340 | { | |
1341 | int count = 0, i; | |
1342 | ||
1343 | for (i = 0; i < todo_list->nr; i++) | |
1344 | if (todo_list->items[i].command != TODO_COMMENT) | |
1345 | count++; | |
1346 | ||
1347 | return count; | |
1348 | } | |
1349 | ||
004fefa7 | 1350 | static int read_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
1351 | struct replay_opts *opts) |
1352 | { | |
54fd3243 | 1353 | struct stat st; |
c0246501 | 1354 | const char *todo_file = get_todo_path(opts); |
043a4492 RR |
1355 | int fd, res; |
1356 | ||
004fefa7 | 1357 | strbuf_reset(&todo_list->buf); |
c0246501 | 1358 | fd = open(todo_file, O_RDONLY); |
043a4492 | 1359 | if (fd < 0) |
93b3df6f | 1360 | return error_errno(_("could not open '%s'"), todo_file); |
004fefa7 | 1361 | if (strbuf_read(&todo_list->buf, fd, 0) < 0) { |
043a4492 | 1362 | close(fd); |
93b3df6f | 1363 | return error(_("could not read '%s'."), todo_file); |
043a4492 RR |
1364 | } |
1365 | close(fd); | |
1366 | ||
54fd3243 SH |
1367 | res = stat(todo_file, &st); |
1368 | if (res) | |
1369 | return error(_("could not stat '%s'"), todo_file); | |
1370 | fill_stat_data(&todo_list->stat, &st); | |
1371 | ||
004fefa7 | 1372 | res = parse_insn_buffer(todo_list->buf.buf, todo_list); |
27fdbb96 JS |
1373 | if (res) { |
1374 | if (is_rebase_i(opts)) | |
1375 | return error(_("please fix this using " | |
1376 | "'git rebase --edit-todo'.")); | |
93b3df6f | 1377 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
27fdbb96 | 1378 | } |
2eeaf1b3 | 1379 | |
52865279 JS |
1380 | if (!todo_list->nr && |
1381 | (!is_rebase_i(opts) || !file_exists(rebase_path_done()))) | |
1382 | return error(_("no commits parsed.")); | |
1383 | ||
2eeaf1b3 | 1384 | if (!is_rebase_i(opts)) { |
004fefa7 JS |
1385 | enum todo_command valid = |
1386 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; | |
1387 | int i; | |
1388 | ||
1389 | for (i = 0; i < todo_list->nr; i++) | |
1390 | if (valid == todo_list->items[i].command) | |
1391 | continue; | |
1392 | else if (valid == TODO_PICK) | |
93b3df6f | 1393 | return error(_("cannot cherry-pick during a revert.")); |
004fefa7 | 1394 | else |
93b3df6f | 1395 | return error(_("cannot revert during a cherry-pick.")); |
004fefa7 JS |
1396 | } |
1397 | ||
968492e4 JS |
1398 | if (is_rebase_i(opts)) { |
1399 | struct todo_list done = TODO_LIST_INIT; | |
e9d983f1 | 1400 | FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w"); |
968492e4 JS |
1401 | |
1402 | if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 && | |
1403 | !parse_insn_buffer(done.buf.buf, &done)) | |
1404 | todo_list->done_nr = count_commands(&done); | |
1405 | else | |
1406 | todo_list->done_nr = 0; | |
1407 | ||
1408 | todo_list->total_nr = todo_list->done_nr | |
1409 | + count_commands(todo_list); | |
968492e4 | 1410 | todo_list_release(&done); |
ef80069a JS |
1411 | |
1412 | if (f) { | |
1413 | fprintf(f, "%d\n", todo_list->total_nr); | |
1414 | fclose(f); | |
1415 | } | |
968492e4 JS |
1416 | } |
1417 | ||
0ae42a03 | 1418 | return 0; |
043a4492 RR |
1419 | } |
1420 | ||
03a4e260 JS |
1421 | static int git_config_string_dup(char **dest, |
1422 | const char *var, const char *value) | |
1423 | { | |
1424 | if (!value) | |
1425 | return config_error_nonbool(var); | |
1426 | free(*dest); | |
1427 | *dest = xstrdup(value); | |
1428 | return 0; | |
1429 | } | |
1430 | ||
043a4492 RR |
1431 | static int populate_opts_cb(const char *key, const char *value, void *data) |
1432 | { | |
1433 | struct replay_opts *opts = data; | |
1434 | int error_flag = 1; | |
1435 | ||
1436 | if (!value) | |
1437 | error_flag = 0; | |
1438 | else if (!strcmp(key, "options.no-commit")) | |
1439 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); | |
1440 | else if (!strcmp(key, "options.edit")) | |
1441 | opts->edit = git_config_bool_or_int(key, value, &error_flag); | |
1442 | else if (!strcmp(key, "options.signoff")) | |
1443 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); | |
1444 | else if (!strcmp(key, "options.record-origin")) | |
1445 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); | |
1446 | else if (!strcmp(key, "options.allow-ff")) | |
1447 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); | |
1448 | else if (!strcmp(key, "options.mainline")) | |
1449 | opts->mainline = git_config_int(key, value); | |
1450 | else if (!strcmp(key, "options.strategy")) | |
03a4e260 | 1451 | git_config_string_dup(&opts->strategy, key, value); |
3253553e | 1452 | else if (!strcmp(key, "options.gpg-sign")) |
03a4e260 | 1453 | git_config_string_dup(&opts->gpg_sign, key, value); |
043a4492 RR |
1454 | else if (!strcmp(key, "options.strategy-option")) { |
1455 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); | |
1456 | opts->xopts[opts->xopts_nr++] = xstrdup(value); | |
8d8cb4b0 PW |
1457 | } else if (!strcmp(key, "options.allow-rerere-auto")) |
1458 | opts->allow_rerere_auto = | |
1459 | git_config_bool_or_int(key, value, &error_flag) ? | |
1460 | RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE; | |
1461 | else | |
93b3df6f | 1462 | return error(_("invalid key: %s"), key); |
043a4492 RR |
1463 | |
1464 | if (!error_flag) | |
93b3df6f | 1465 | return error(_("invalid value for %s: %s"), key, value); |
043a4492 RR |
1466 | |
1467 | return 0; | |
1468 | } | |
1469 | ||
ca6c6b45 JS |
1470 | static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) |
1471 | { | |
1472 | int i; | |
1473 | ||
1474 | strbuf_reset(buf); | |
1475 | if (!read_oneliner(buf, rebase_path_strategy(), 0)) | |
1476 | return; | |
1477 | opts->strategy = strbuf_detach(buf, NULL); | |
1478 | if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) | |
1479 | return; | |
1480 | ||
1481 | opts->xopts_nr = split_cmdline(buf->buf, (const char ***)&opts->xopts); | |
1482 | for (i = 0; i < opts->xopts_nr; i++) { | |
1483 | const char *arg = opts->xopts[i]; | |
1484 | ||
1485 | skip_prefix(arg, "--", &arg); | |
1486 | opts->xopts[i] = xstrdup(arg); | |
1487 | } | |
1488 | } | |
1489 | ||
5adf9bdc | 1490 | static int read_populate_opts(struct replay_opts *opts) |
043a4492 | 1491 | { |
a1c75762 JS |
1492 | if (is_rebase_i(opts)) { |
1493 | struct strbuf buf = STRBUF_INIT; | |
1494 | ||
1495 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) { | |
1496 | if (!starts_with(buf.buf, "-S")) | |
1497 | strbuf_reset(&buf); | |
1498 | else { | |
1499 | free(opts->gpg_sign); | |
1500 | opts->gpg_sign = xstrdup(buf.buf + 2); | |
1501 | } | |
9b6d7a62 PW |
1502 | strbuf_reset(&buf); |
1503 | } | |
1504 | ||
1505 | if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(), 1)) { | |
1506 | if (!strcmp(buf.buf, "--rerere-autoupdate")) | |
1507 | opts->allow_rerere_auto = RERERE_AUTOUPDATE; | |
1508 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) | |
1509 | opts->allow_rerere_auto = RERERE_NOAUTOUPDATE; | |
1510 | strbuf_reset(&buf); | |
a1c75762 | 1511 | } |
a1c75762 | 1512 | |
556907f1 JS |
1513 | if (file_exists(rebase_path_verbose())) |
1514 | opts->verbose = 1; | |
1515 | ||
ca6c6b45 JS |
1516 | read_strategy_opts(opts, &buf); |
1517 | strbuf_release(&buf); | |
1518 | ||
b5a67045 | 1519 | return 0; |
a1c75762 | 1520 | } |
b5a67045 | 1521 | |
f932729c | 1522 | if (!file_exists(git_path_opts_file())) |
0d00da7b JS |
1523 | return 0; |
1524 | /* | |
1525 | * The function git_parse_source(), called from git_config_from_file(), | |
1526 | * may die() in case of a syntactically incorrect file. We do not care | |
1527 | * about this case, though, because we wrote that file ourselves, so we | |
1528 | * are pretty certain that it is syntactically correct. | |
1529 | */ | |
5adf9bdc | 1530 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
93b3df6f | 1531 | return error(_("malformed options sheet: '%s'"), |
0d00da7b JS |
1532 | git_path_opts_file()); |
1533 | return 0; | |
043a4492 RR |
1534 | } |
1535 | ||
004fefa7 | 1536 | static int walk_revs_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
1537 | struct replay_opts *opts) |
1538 | { | |
004fefa7 JS |
1539 | enum todo_command command = opts->action == REPLAY_PICK ? |
1540 | TODO_PICK : TODO_REVERT; | |
414697a9 | 1541 | const char *command_string = todo_command_info[command].str; |
043a4492 | 1542 | struct commit *commit; |
043a4492 | 1543 | |
34b0528b JS |
1544 | if (prepare_revs(opts)) |
1545 | return -1; | |
043a4492 | 1546 | |
004fefa7 JS |
1547 | while ((commit = get_revision(opts->revs))) { |
1548 | struct todo_item *item = append_new_todo(todo_list); | |
1549 | const char *commit_buffer = get_commit_buffer(commit, NULL); | |
1550 | const char *subject; | |
1551 | int subject_len; | |
1552 | ||
1553 | item->command = command; | |
1554 | item->commit = commit; | |
c22f7dfb JS |
1555 | item->arg = NULL; |
1556 | item->arg_len = 0; | |
004fefa7 JS |
1557 | item->offset_in_buf = todo_list->buf.len; |
1558 | subject_len = find_commit_subject(commit_buffer, &subject); | |
1559 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, | |
1560 | short_commit_name(commit), subject_len, subject); | |
1561 | unuse_commit_buffer(commit, commit_buffer); | |
1562 | } | |
34b0528b | 1563 | return 0; |
043a4492 RR |
1564 | } |
1565 | ||
1566 | static int create_seq_dir(void) | |
1567 | { | |
f932729c | 1568 | if (file_exists(git_path_seq_dir())) { |
043a4492 RR |
1569 | error(_("a cherry-pick or revert is already in progress")); |
1570 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); | |
1571 | return -1; | |
a70d8f80 | 1572 | } else if (mkdir(git_path_seq_dir(), 0777) < 0) |
93b3df6f | 1573 | return error_errno(_("could not create sequencer directory '%s'"), |
f6e82b0d | 1574 | git_path_seq_dir()); |
043a4492 RR |
1575 | return 0; |
1576 | } | |
1577 | ||
311fd397 | 1578 | static int save_head(const char *head) |
043a4492 | 1579 | { |
043a4492 RR |
1580 | static struct lock_file head_lock; |
1581 | struct strbuf buf = STRBUF_INIT; | |
1582 | int fd; | |
ed3f9a12 | 1583 | ssize_t written; |
043a4492 | 1584 | |
311fd397 JS |
1585 | fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); |
1586 | if (fd < 0) { | |
1587 | rollback_lock_file(&head_lock); | |
93b3df6f | 1588 | return error_errno(_("could not lock HEAD")); |
311fd397 | 1589 | } |
043a4492 | 1590 | strbuf_addf(&buf, "%s\n", head); |
ed3f9a12 RS |
1591 | written = write_in_full(fd, buf.buf, buf.len); |
1592 | strbuf_release(&buf); | |
1593 | if (written < 0) { | |
311fd397 | 1594 | rollback_lock_file(&head_lock); |
93b3df6f | 1595 | return error_errno(_("could not write to '%s'"), |
311fd397 JS |
1596 | git_path_head_file()); |
1597 | } | |
1598 | if (commit_lock_file(&head_lock) < 0) { | |
1599 | rollback_lock_file(&head_lock); | |
93b3df6f | 1600 | return error(_("failed to finalize '%s'."), git_path_head_file()); |
311fd397 JS |
1601 | } |
1602 | return 0; | |
043a4492 RR |
1603 | } |
1604 | ||
1e41229d SB |
1605 | static int rollback_is_safe(void) |
1606 | { | |
1607 | struct strbuf sb = STRBUF_INIT; | |
1608 | struct object_id expected_head, actual_head; | |
1609 | ||
1610 | if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) { | |
1611 | strbuf_trim(&sb); | |
1612 | if (get_oid_hex(sb.buf, &expected_head)) { | |
1613 | strbuf_release(&sb); | |
1614 | die(_("could not parse %s"), git_path_abort_safety_file()); | |
1615 | } | |
1616 | strbuf_release(&sb); | |
1617 | } | |
1618 | else if (errno == ENOENT) | |
1619 | oidclr(&expected_head); | |
1620 | else | |
1621 | die_errno(_("could not read '%s'"), git_path_abort_safety_file()); | |
1622 | ||
1623 | if (get_oid("HEAD", &actual_head)) | |
1624 | oidclr(&actual_head); | |
1625 | ||
1626 | return !oidcmp(&actual_head, &expected_head); | |
1627 | } | |
1628 | ||
092bbcdf | 1629 | static int reset_for_rollback(const struct object_id *oid) |
043a4492 RR |
1630 | { |
1631 | const char *argv[4]; /* reset --merge <arg> + NULL */ | |
1e41229d | 1632 | |
043a4492 RR |
1633 | argv[0] = "reset"; |
1634 | argv[1] = "--merge"; | |
092bbcdf | 1635 | argv[2] = oid_to_hex(oid); |
043a4492 RR |
1636 | argv[3] = NULL; |
1637 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
1638 | } | |
1639 | ||
1640 | static int rollback_single_pick(void) | |
1641 | { | |
092bbcdf | 1642 | struct object_id head_oid; |
043a4492 | 1643 | |
f932729c JK |
1644 | if (!file_exists(git_path_cherry_pick_head()) && |
1645 | !file_exists(git_path_revert_head())) | |
043a4492 | 1646 | return error(_("no cherry-pick or revert in progress")); |
34c290a6 | 1647 | if (read_ref_full("HEAD", 0, &head_oid, NULL)) |
043a4492 | 1648 | return error(_("cannot resolve HEAD")); |
092bbcdf | 1649 | if (is_null_oid(&head_oid)) |
043a4492 | 1650 | return error(_("cannot abort from a branch yet to be born")); |
092bbcdf | 1651 | return reset_for_rollback(&head_oid); |
043a4492 RR |
1652 | } |
1653 | ||
2863584f | 1654 | int sequencer_rollback(struct replay_opts *opts) |
043a4492 | 1655 | { |
043a4492 | 1656 | FILE *f; |
092bbcdf | 1657 | struct object_id oid; |
043a4492 | 1658 | struct strbuf buf = STRBUF_INIT; |
092bbcdf | 1659 | const char *p; |
043a4492 | 1660 | |
f932729c | 1661 | f = fopen(git_path_head_file(), "r"); |
043a4492 RR |
1662 | if (!f && errno == ENOENT) { |
1663 | /* | |
1664 | * There is no multiple-cherry-pick in progress. | |
1665 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates | |
1666 | * a single-cherry-pick in progress, abort that. | |
1667 | */ | |
1668 | return rollback_single_pick(); | |
1669 | } | |
1670 | if (!f) | |
f7ed1953 | 1671 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
8f309aeb | 1672 | if (strbuf_getline_lf(&buf, f)) { |
f7ed1953 | 1673 | error(_("cannot read '%s': %s"), git_path_head_file(), |
f932729c | 1674 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
043a4492 RR |
1675 | fclose(f); |
1676 | goto fail; | |
1677 | } | |
1678 | fclose(f); | |
092bbcdf | 1679 | if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') { |
043a4492 | 1680 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
f932729c | 1681 | git_path_head_file()); |
043a4492 RR |
1682 | goto fail; |
1683 | } | |
092bbcdf | 1684 | if (is_null_oid(&oid)) { |
0f974e21 MG |
1685 | error(_("cannot abort from a branch yet to be born")); |
1686 | goto fail; | |
1687 | } | |
1e41229d SB |
1688 | |
1689 | if (!rollback_is_safe()) { | |
1690 | /* Do not error, just do not rollback */ | |
1691 | warning(_("You seem to have moved HEAD. " | |
1692 | "Not rewinding, check your HEAD!")); | |
1693 | } else | |
092bbcdf | 1694 | if (reset_for_rollback(&oid)) |
043a4492 | 1695 | goto fail; |
043a4492 | 1696 | strbuf_release(&buf); |
2863584f | 1697 | return sequencer_remove_state(opts); |
043a4492 RR |
1698 | fail: |
1699 | strbuf_release(&buf); | |
1700 | return -1; | |
1701 | } | |
1702 | ||
004fefa7 | 1703 | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 1704 | { |
043a4492 | 1705 | static struct lock_file todo_lock; |
004fefa7 JS |
1706 | const char *todo_path = get_todo_path(opts); |
1707 | int next = todo_list->current, offset, fd; | |
043a4492 | 1708 | |
84583957 JS |
1709 | /* |
1710 | * rebase -i writes "git-rebase-todo" without the currently executing | |
1711 | * command, appending it to "done" instead. | |
1712 | */ | |
1713 | if (is_rebase_i(opts)) | |
1714 | next++; | |
1715 | ||
004fefa7 | 1716 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
221675de | 1717 | if (fd < 0) |
93b3df6f | 1718 | return error_errno(_("could not lock '%s'"), todo_path); |
004fefa7 JS |
1719 | offset = next < todo_list->nr ? |
1720 | todo_list->items[next].offset_in_buf : todo_list->buf.len; | |
1721 | if (write_in_full(fd, todo_list->buf.buf + offset, | |
1722 | todo_list->buf.len - offset) < 0) | |
93b3df6f | 1723 | return error_errno(_("could not write to '%s'"), todo_path); |
004fefa7 | 1724 | if (commit_lock_file(&todo_lock) < 0) |
93b3df6f | 1725 | return error(_("failed to finalize '%s'."), todo_path); |
1df6df0c JS |
1726 | |
1727 | if (is_rebase_i(opts)) { | |
1728 | const char *done_path = rebase_path_done(); | |
1729 | int fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
1730 | int prev_offset = !next ? 0 : | |
1731 | todo_list->items[next - 1].offset_in_buf; | |
1732 | ||
1733 | if (fd >= 0 && offset > prev_offset && | |
1734 | write_in_full(fd, todo_list->buf.buf + prev_offset, | |
1735 | offset - prev_offset) < 0) { | |
1736 | close(fd); | |
1737 | return error_errno(_("could not write to '%s'"), | |
1738 | done_path); | |
1739 | } | |
1740 | if (fd >= 0) | |
1741 | close(fd); | |
1742 | } | |
221675de | 1743 | return 0; |
043a4492 RR |
1744 | } |
1745 | ||
88d5a271 | 1746 | static int save_opts(struct replay_opts *opts) |
043a4492 | 1747 | { |
f932729c | 1748 | const char *opts_file = git_path_opts_file(); |
88d5a271 | 1749 | int res = 0; |
043a4492 RR |
1750 | |
1751 | if (opts->no_commit) | |
88d5a271 | 1752 | res |= git_config_set_in_file_gently(opts_file, "options.no-commit", "true"); |
043a4492 | 1753 | if (opts->edit) |
88d5a271 | 1754 | res |= git_config_set_in_file_gently(opts_file, "options.edit", "true"); |
043a4492 | 1755 | if (opts->signoff) |
88d5a271 | 1756 | res |= git_config_set_in_file_gently(opts_file, "options.signoff", "true"); |
043a4492 | 1757 | if (opts->record_origin) |
88d5a271 | 1758 | res |= git_config_set_in_file_gently(opts_file, "options.record-origin", "true"); |
043a4492 | 1759 | if (opts->allow_ff) |
88d5a271 | 1760 | res |= git_config_set_in_file_gently(opts_file, "options.allow-ff", "true"); |
043a4492 RR |
1761 | if (opts->mainline) { |
1762 | struct strbuf buf = STRBUF_INIT; | |
1763 | strbuf_addf(&buf, "%d", opts->mainline); | |
88d5a271 | 1764 | res |= git_config_set_in_file_gently(opts_file, "options.mainline", buf.buf); |
043a4492 RR |
1765 | strbuf_release(&buf); |
1766 | } | |
1767 | if (opts->strategy) | |
88d5a271 | 1768 | res |= git_config_set_in_file_gently(opts_file, "options.strategy", opts->strategy); |
3253553e | 1769 | if (opts->gpg_sign) |
88d5a271 | 1770 | res |= git_config_set_in_file_gently(opts_file, "options.gpg-sign", opts->gpg_sign); |
043a4492 RR |
1771 | if (opts->xopts) { |
1772 | int i; | |
1773 | for (i = 0; i < opts->xopts_nr; i++) | |
88d5a271 | 1774 | res |= git_config_set_multivar_in_file_gently(opts_file, |
043a4492 RR |
1775 | "options.strategy-option", |
1776 | opts->xopts[i], "^$", 0); | |
1777 | } | |
8d8cb4b0 PW |
1778 | if (opts->allow_rerere_auto) |
1779 | res |= git_config_set_in_file_gently(opts_file, "options.allow-rerere-auto", | |
1780 | opts->allow_rerere_auto == RERERE_AUTOUPDATE ? | |
1781 | "true" : "false"); | |
88d5a271 | 1782 | return res; |
043a4492 RR |
1783 | } |
1784 | ||
56dc3ab0 JS |
1785 | static int make_patch(struct commit *commit, struct replay_opts *opts) |
1786 | { | |
1787 | struct strbuf buf = STRBUF_INIT; | |
1788 | struct rev_info log_tree_opt; | |
1789 | const char *subject, *p; | |
1790 | int res = 0; | |
1791 | ||
1792 | p = short_commit_name(commit); | |
1793 | if (write_message(p, strlen(p), rebase_path_stopped_sha(), 1) < 0) | |
1794 | return -1; | |
1795 | ||
1796 | strbuf_addf(&buf, "%s/patch", get_dir(opts)); | |
1797 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); | |
1798 | init_revisions(&log_tree_opt, NULL); | |
1799 | log_tree_opt.abbrev = 0; | |
1800 | log_tree_opt.diff = 1; | |
1801 | log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH; | |
1802 | log_tree_opt.disable_stdin = 1; | |
1803 | log_tree_opt.no_commit_id = 1; | |
1804 | log_tree_opt.diffopt.file = fopen(buf.buf, "w"); | |
1805 | log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER; | |
1806 | if (!log_tree_opt.diffopt.file) | |
1807 | res |= error_errno(_("could not open '%s'"), buf.buf); | |
1808 | else { | |
1809 | res |= log_tree_commit(&log_tree_opt, commit); | |
1810 | fclose(log_tree_opt.diffopt.file); | |
1811 | } | |
1812 | strbuf_reset(&buf); | |
1813 | ||
1814 | strbuf_addf(&buf, "%s/message", get_dir(opts)); | |
1815 | if (!file_exists(buf.buf)) { | |
1816 | const char *commit_buffer = get_commit_buffer(commit, NULL); | |
1817 | find_commit_subject(commit_buffer, &subject); | |
1818 | res |= write_message(subject, strlen(subject), buf.buf, 1); | |
1819 | unuse_commit_buffer(commit, commit_buffer); | |
1820 | } | |
1821 | strbuf_release(&buf); | |
1822 | ||
1823 | return res; | |
1824 | } | |
1825 | ||
1826 | static int intend_to_amend(void) | |
1827 | { | |
092bbcdf | 1828 | struct object_id head; |
56dc3ab0 JS |
1829 | char *p; |
1830 | ||
092bbcdf | 1831 | if (get_oid("HEAD", &head)) |
56dc3ab0 JS |
1832 | return error(_("cannot read HEAD")); |
1833 | ||
092bbcdf | 1834 | p = oid_to_hex(&head); |
56dc3ab0 JS |
1835 | return write_message(p, strlen(p), rebase_path_amend(), 1); |
1836 | } | |
1837 | ||
1838 | static int error_with_patch(struct commit *commit, | |
1839 | const char *subject, int subject_len, | |
1840 | struct replay_opts *opts, int exit_code, int to_amend) | |
1841 | { | |
1842 | if (make_patch(commit, opts)) | |
1843 | return -1; | |
1844 | ||
1845 | if (to_amend) { | |
1846 | if (intend_to_amend()) | |
1847 | return -1; | |
1848 | ||
1849 | fprintf(stderr, "You can amend the commit now, with\n" | |
1850 | "\n" | |
1851 | " git commit --amend %s\n" | |
1852 | "\n" | |
1853 | "Once you are satisfied with your changes, run\n" | |
1854 | "\n" | |
1855 | " git rebase --continue\n", gpg_sign_opt_quoted(opts)); | |
1856 | } else if (exit_code) | |
1857 | fprintf(stderr, "Could not apply %s... %.*s\n", | |
1858 | short_commit_name(commit), subject_len, subject); | |
1859 | ||
1860 | return exit_code; | |
1861 | } | |
1862 | ||
6e98de72 JS |
1863 | static int error_failed_squash(struct commit *commit, |
1864 | struct replay_opts *opts, int subject_len, const char *subject) | |
1865 | { | |
1866 | if (rename(rebase_path_squash_msg(), rebase_path_message())) | |
1867 | return error(_("could not rename '%s' to '%s'"), | |
1868 | rebase_path_squash_msg(), rebase_path_message()); | |
1869 | unlink(rebase_path_fixup_msg()); | |
ca03e067 JK |
1870 | unlink(git_path_merge_msg()); |
1871 | if (copy_file(git_path_merge_msg(), rebase_path_message(), 0666)) | |
6e98de72 | 1872 | return error(_("could not copy '%s' to '%s'"), |
ca03e067 | 1873 | rebase_path_message(), git_path_merge_msg()); |
6e98de72 JS |
1874 | return error_with_patch(commit, subject, subject_len, opts, 1, 0); |
1875 | } | |
1876 | ||
311af526 JS |
1877 | static int do_exec(const char *command_line) |
1878 | { | |
09d7b6c6 | 1879 | struct argv_array child_env = ARGV_ARRAY_INIT; |
311af526 JS |
1880 | const char *child_argv[] = { NULL, NULL }; |
1881 | int dirty, status; | |
1882 | ||
1883 | fprintf(stderr, "Executing: %s\n", command_line); | |
1884 | child_argv[0] = command_line; | |
09d7b6c6 JK |
1885 | argv_array_pushf(&child_env, "GIT_DIR=%s", absolute_path(get_git_dir())); |
1886 | status = run_command_v_opt_cd_env(child_argv, RUN_USING_SHELL, NULL, | |
1887 | child_env.argv); | |
311af526 JS |
1888 | |
1889 | /* force re-reading of the cache */ | |
1890 | if (discard_cache() < 0 || read_cache() < 0) | |
1891 | return error(_("could not read index")); | |
1892 | ||
1893 | dirty = require_clean_work_tree("rebase", NULL, 1, 1); | |
1894 | ||
1895 | if (status) { | |
1896 | warning(_("execution failed: %s\n%s" | |
1897 | "You can fix the problem, and then run\n" | |
1898 | "\n" | |
1899 | " git rebase --continue\n" | |
1900 | "\n"), | |
1901 | command_line, | |
1902 | dirty ? N_("and made changes to the index and/or the " | |
1903 | "working tree\n") : ""); | |
1904 | if (status == 127) | |
1905 | /* command not found */ | |
1906 | status = 1; | |
1907 | } else if (dirty) { | |
1908 | warning(_("execution succeeded: %s\nbut " | |
1909 | "left changes to the index and/or the working tree\n" | |
1910 | "Commit or stash your changes, and then run\n" | |
1911 | "\n" | |
1912 | " git rebase --continue\n" | |
1913 | "\n"), command_line); | |
1914 | status = 1; | |
1915 | } | |
1916 | ||
09d7b6c6 JK |
1917 | argv_array_clear(&child_env); |
1918 | ||
311af526 JS |
1919 | return status; |
1920 | } | |
1921 | ||
6e98de72 JS |
1922 | static int is_final_fixup(struct todo_list *todo_list) |
1923 | { | |
1924 | int i = todo_list->current; | |
1925 | ||
1926 | if (!is_fixup(todo_list->items[i].command)) | |
1927 | return 0; | |
1928 | ||
1929 | while (++i < todo_list->nr) | |
1930 | if (is_fixup(todo_list->items[i].command)) | |
1931 | return 0; | |
1932 | else if (!is_noop(todo_list->items[i].command)) | |
1933 | break; | |
1934 | return 1; | |
1935 | } | |
1936 | ||
25cb8df9 JS |
1937 | static enum todo_command peek_command(struct todo_list *todo_list, int offset) |
1938 | { | |
1939 | int i; | |
1940 | ||
1941 | for (i = todo_list->current + offset; i < todo_list->nr; i++) | |
1942 | if (!is_noop(todo_list->items[i].command)) | |
1943 | return todo_list->items[i].command; | |
1944 | ||
1945 | return -1; | |
1946 | } | |
1947 | ||
796c7972 JS |
1948 | static int apply_autostash(struct replay_opts *opts) |
1949 | { | |
1950 | struct strbuf stash_sha1 = STRBUF_INIT; | |
1951 | struct child_process child = CHILD_PROCESS_INIT; | |
1952 | int ret = 0; | |
1953 | ||
1954 | if (!read_oneliner(&stash_sha1, rebase_path_autostash(), 1)) { | |
1955 | strbuf_release(&stash_sha1); | |
1956 | return 0; | |
1957 | } | |
1958 | strbuf_trim(&stash_sha1); | |
1959 | ||
1960 | child.git_cmd = 1; | |
79a62269 PW |
1961 | child.no_stdout = 1; |
1962 | child.no_stderr = 1; | |
796c7972 JS |
1963 | argv_array_push(&child.args, "stash"); |
1964 | argv_array_push(&child.args, "apply"); | |
1965 | argv_array_push(&child.args, stash_sha1.buf); | |
1966 | if (!run_command(&child)) | |
cdb866b3 | 1967 | fprintf(stderr, _("Applied autostash.\n")); |
796c7972 JS |
1968 | else { |
1969 | struct child_process store = CHILD_PROCESS_INIT; | |
1970 | ||
1971 | store.git_cmd = 1; | |
1972 | argv_array_push(&store.args, "stash"); | |
1973 | argv_array_push(&store.args, "store"); | |
1974 | argv_array_push(&store.args, "-m"); | |
1975 | argv_array_push(&store.args, "autostash"); | |
1976 | argv_array_push(&store.args, "-q"); | |
1977 | argv_array_push(&store.args, stash_sha1.buf); | |
1978 | if (run_command(&store)) | |
1979 | ret = error(_("cannot store %s"), stash_sha1.buf); | |
1980 | else | |
cdb866b3 JS |
1981 | fprintf(stderr, |
1982 | _("Applying autostash resulted in conflicts.\n" | |
1983 | "Your changes are safe in the stash.\n" | |
1984 | "You can run \"git stash pop\" or" | |
1985 | " \"git stash drop\" at any time.\n")); | |
796c7972 JS |
1986 | } |
1987 | ||
1988 | strbuf_release(&stash_sha1); | |
1989 | return ret; | |
1990 | } | |
1991 | ||
96e832a5 JS |
1992 | static const char *reflog_message(struct replay_opts *opts, |
1993 | const char *sub_action, const char *fmt, ...) | |
1994 | { | |
1995 | va_list ap; | |
1996 | static struct strbuf buf = STRBUF_INIT; | |
1997 | ||
1998 | va_start(ap, fmt); | |
1999 | strbuf_reset(&buf); | |
2000 | strbuf_addstr(&buf, action_name(opts)); | |
2001 | if (sub_action) | |
2002 | strbuf_addf(&buf, " (%s)", sub_action); | |
2003 | if (fmt) { | |
2004 | strbuf_addstr(&buf, ": "); | |
2005 | strbuf_vaddf(&buf, fmt, ap); | |
2006 | } | |
2007 | va_end(ap); | |
2008 | ||
2009 | return buf.buf; | |
2010 | } | |
2011 | ||
004fefa7 | 2012 | static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 2013 | { |
56dc3ab0 | 2014 | int res = 0; |
043a4492 RR |
2015 | |
2016 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
2017 | if (opts->allow_ff) | |
2018 | assert(!(opts->signoff || opts->no_commit || | |
2019 | opts->record_origin || opts->edit)); | |
0d9c6dc9 JS |
2020 | if (read_and_refresh_cache(opts)) |
2021 | return -1; | |
043a4492 | 2022 | |
004fefa7 JS |
2023 | while (todo_list->current < todo_list->nr) { |
2024 | struct todo_item *item = todo_list->items + todo_list->current; | |
2025 | if (save_todo(todo_list, opts)) | |
221675de | 2026 | return -1; |
6e98de72 | 2027 | if (is_rebase_i(opts)) { |
ef80069a JS |
2028 | if (item->command != TODO_COMMENT) { |
2029 | FILE *f = fopen(rebase_path_msgnum(), "w"); | |
2030 | ||
2031 | todo_list->done_nr++; | |
2032 | ||
2033 | if (f) { | |
2034 | fprintf(f, "%d\n", todo_list->done_nr); | |
2035 | fclose(f); | |
2036 | } | |
968492e4 | 2037 | fprintf(stderr, "Rebasing (%d/%d)%s", |
ef80069a | 2038 | todo_list->done_nr, |
968492e4 JS |
2039 | todo_list->total_nr, |
2040 | opts->verbose ? "\n" : "\r"); | |
ef80069a | 2041 | } |
6e98de72 JS |
2042 | unlink(rebase_path_message()); |
2043 | unlink(rebase_path_author_script()); | |
2044 | unlink(rebase_path_stopped_sha()); | |
2045 | unlink(rebase_path_amend()); | |
2046 | } | |
2047 | if (item->command <= TODO_SQUASH) { | |
8ab37ef2 JS |
2048 | if (is_rebase_i(opts)) |
2049 | setenv("GIT_REFLOG_ACTION", reflog_message(opts, | |
2050 | command_to_string(item->command), NULL), | |
2051 | 1); | |
25c43667 | 2052 | res = do_pick_commit(item->command, item->commit, |
6e98de72 | 2053 | opts, is_final_fixup(todo_list)); |
9d7bf3cf JS |
2054 | if (is_rebase_i(opts) && res < 0) { |
2055 | /* Reschedule */ | |
2056 | todo_list->current--; | |
2057 | if (save_todo(todo_list, opts)) | |
2058 | return -1; | |
2059 | } | |
56dc3ab0 JS |
2060 | if (item->command == TODO_EDIT) { |
2061 | struct commit *commit = item->commit; | |
2062 | if (!res) | |
99429213 | 2063 | fprintf(stderr, |
a42e1b41 | 2064 | _("Stopped at %s... %.*s\n"), |
56dc3ab0 JS |
2065 | short_commit_name(commit), |
2066 | item->arg_len, item->arg); | |
2067 | return error_with_patch(commit, | |
2068 | item->arg, item->arg_len, opts, res, | |
2069 | !res); | |
2070 | } | |
25cb8df9 JS |
2071 | if (is_rebase_i(opts) && !res) |
2072 | record_in_rewritten(&item->commit->object.oid, | |
2073 | peek_command(todo_list, 1)); | |
6e98de72 JS |
2074 | if (res && is_fixup(item->command)) { |
2075 | if (res == 1) | |
2076 | intend_to_amend(); | |
2077 | return error_failed_squash(item->commit, opts, | |
2078 | item->arg_len, item->arg); | |
4a5146f9 JS |
2079 | } else if (res && is_rebase_i(opts)) |
2080 | return res | error_with_patch(item->commit, | |
04efc8b5 JS |
2081 | item->arg, item->arg_len, opts, res, |
2082 | item->command == TODO_REWORD); | |
311af526 JS |
2083 | } else if (item->command == TODO_EXEC) { |
2084 | char *end_of_arg = (char *)(item->arg + item->arg_len); | |
2085 | int saved = *end_of_arg; | |
54fd3243 | 2086 | struct stat st; |
311af526 JS |
2087 | |
2088 | *end_of_arg = '\0'; | |
2089 | res = do_exec(item->arg); | |
2090 | *end_of_arg = saved; | |
54fd3243 SH |
2091 | |
2092 | /* Reread the todo file if it has changed. */ | |
2093 | if (res) | |
2094 | ; /* fall through */ | |
2095 | else if (stat(get_todo_path(opts), &st)) | |
2096 | res = error_errno(_("could not stat '%s'"), | |
2097 | get_todo_path(opts)); | |
2098 | else if (match_stat_data(&todo_list->stat, &st)) { | |
2099 | todo_list_release(todo_list); | |
2100 | if (read_populate_todo(todo_list, opts)) | |
2101 | res = -1; /* message was printed */ | |
2102 | /* `current` will be incremented below */ | |
2103 | todo_list->current = -1; | |
2104 | } | |
56dc3ab0 | 2105 | } else if (!is_noop(item->command)) |
25c43667 JS |
2106 | return error(_("unknown command %d"), item->command); |
2107 | ||
004fefa7 | 2108 | todo_list->current++; |
043a4492 RR |
2109 | if (res) |
2110 | return res; | |
2111 | } | |
2112 | ||
56dc3ab0 | 2113 | if (is_rebase_i(opts)) { |
4b83ce9f | 2114 | struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT; |
25cb8df9 | 2115 | struct stat st; |
556907f1 | 2116 | |
56dc3ab0 JS |
2117 | /* Stopped in the middle, as planned? */ |
2118 | if (todo_list->current < todo_list->nr) | |
2119 | return 0; | |
556907f1 | 2120 | |
4b83ce9f JS |
2121 | if (read_oneliner(&head_ref, rebase_path_head_name(), 0) && |
2122 | starts_with(head_ref.buf, "refs/")) { | |
96e832a5 | 2123 | const char *msg; |
092bbcdf | 2124 | struct object_id head, orig; |
4b83ce9f JS |
2125 | int res; |
2126 | ||
092bbcdf | 2127 | if (get_oid("HEAD", &head)) { |
4b83ce9f JS |
2128 | res = error(_("cannot read HEAD")); |
2129 | cleanup_head_ref: | |
2130 | strbuf_release(&head_ref); | |
2131 | strbuf_release(&buf); | |
2132 | return res; | |
2133 | } | |
2134 | if (!read_oneliner(&buf, rebase_path_orig_head(), 0) || | |
092bbcdf | 2135 | get_oid_hex(buf.buf, &orig)) { |
4b83ce9f JS |
2136 | res = error(_("could not read orig-head")); |
2137 | goto cleanup_head_ref; | |
2138 | } | |
4ab867b8 | 2139 | strbuf_reset(&buf); |
4b83ce9f JS |
2140 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) { |
2141 | res = error(_("could not read 'onto'")); | |
2142 | goto cleanup_head_ref; | |
2143 | } | |
96e832a5 JS |
2144 | msg = reflog_message(opts, "finish", "%s onto %s", |
2145 | head_ref.buf, buf.buf); | |
ae077771 | 2146 | if (update_ref(msg, head_ref.buf, &head, &orig, |
91774afc | 2147 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { |
4b83ce9f JS |
2148 | res = error(_("could not update %s"), |
2149 | head_ref.buf); | |
2150 | goto cleanup_head_ref; | |
2151 | } | |
96e832a5 | 2152 | msg = reflog_message(opts, "finish", "returning to %s", |
4b83ce9f | 2153 | head_ref.buf); |
96e832a5 | 2154 | if (create_symref("HEAD", head_ref.buf, msg)) { |
4b83ce9f JS |
2155 | res = error(_("could not update HEAD to %s"), |
2156 | head_ref.buf); | |
2157 | goto cleanup_head_ref; | |
2158 | } | |
2159 | strbuf_reset(&buf); | |
2160 | } | |
2161 | ||
556907f1 JS |
2162 | if (opts->verbose) { |
2163 | struct rev_info log_tree_opt; | |
2164 | struct object_id orig, head; | |
2165 | ||
2166 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); | |
2167 | init_revisions(&log_tree_opt, NULL); | |
2168 | log_tree_opt.diff = 1; | |
2169 | log_tree_opt.diffopt.output_format = | |
2170 | DIFF_FORMAT_DIFFSTAT; | |
2171 | log_tree_opt.disable_stdin = 1; | |
2172 | ||
2173 | if (read_oneliner(&buf, rebase_path_orig_head(), 0) && | |
e82caf38 | 2174 | !get_oid(buf.buf, &orig) && |
2175 | !get_oid("HEAD", &head)) { | |
66f414f8 BW |
2176 | diff_tree_oid(&orig, &head, "", |
2177 | &log_tree_opt.diffopt); | |
556907f1 JS |
2178 | log_tree_diff_flush(&log_tree_opt); |
2179 | } | |
2180 | } | |
25cb8df9 JS |
2181 | flush_rewritten_pending(); |
2182 | if (!stat(rebase_path_rewritten_list(), &st) && | |
2183 | st.st_size > 0) { | |
2184 | struct child_process child = CHILD_PROCESS_INIT; | |
79516045 JS |
2185 | const char *post_rewrite_hook = |
2186 | find_hook("post-rewrite"); | |
25cb8df9 JS |
2187 | |
2188 | child.in = open(rebase_path_rewritten_list(), O_RDONLY); | |
2189 | child.git_cmd = 1; | |
2190 | argv_array_push(&child.args, "notes"); | |
2191 | argv_array_push(&child.args, "copy"); | |
2192 | argv_array_push(&child.args, "--for-rewrite=rebase"); | |
2193 | /* we don't care if this copying failed */ | |
2194 | run_command(&child); | |
79516045 JS |
2195 | |
2196 | if (post_rewrite_hook) { | |
2197 | struct child_process hook = CHILD_PROCESS_INIT; | |
2198 | ||
2199 | hook.in = open(rebase_path_rewritten_list(), | |
2200 | O_RDONLY); | |
2201 | hook.stdout_to_stderr = 1; | |
2202 | argv_array_push(&hook.args, post_rewrite_hook); | |
2203 | argv_array_push(&hook.args, "rebase"); | |
2204 | /* we don't care if this hook failed */ | |
2205 | run_command(&hook); | |
2206 | } | |
25cb8df9 | 2207 | } |
796c7972 | 2208 | apply_autostash(opts); |
25cb8df9 | 2209 | |
5da4966f JS |
2210 | fprintf(stderr, "Successfully rebased and updated %s.\n", |
2211 | head_ref.buf); | |
2212 | ||
556907f1 | 2213 | strbuf_release(&buf); |
4b83ce9f | 2214 | strbuf_release(&head_ref); |
56dc3ab0 JS |
2215 | } |
2216 | ||
043a4492 RR |
2217 | /* |
2218 | * Sequence of picks finished successfully; cleanup by | |
2219 | * removing the .git/sequencer directory | |
2220 | */ | |
2863584f | 2221 | return sequencer_remove_state(opts); |
043a4492 RR |
2222 | } |
2223 | ||
2224 | static int continue_single_pick(void) | |
2225 | { | |
2226 | const char *argv[] = { "commit", NULL }; | |
2227 | ||
f932729c JK |
2228 | if (!file_exists(git_path_cherry_pick_head()) && |
2229 | !file_exists(git_path_revert_head())) | |
043a4492 RR |
2230 | return error(_("no cherry-pick or revert in progress")); |
2231 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
2232 | } | |
2233 | ||
9d93ccd1 JS |
2234 | static int commit_staged_changes(struct replay_opts *opts) |
2235 | { | |
789b3eff | 2236 | unsigned int flags = ALLOW_EMPTY | EDIT_MSG; |
9d93ccd1 JS |
2237 | |
2238 | if (has_unstaged_changes(1)) | |
2239 | return error(_("cannot rebase: You have unstaged changes.")); | |
52632209 | 2240 | if (!has_uncommitted_changes(0)) { |
ca03e067 | 2241 | const char *cherry_pick_head = git_path_cherry_pick_head(); |
52632209 JS |
2242 | |
2243 | if (file_exists(cherry_pick_head) && unlink(cherry_pick_head)) | |
2244 | return error(_("could not remove CHERRY_PICK_HEAD")); | |
9d93ccd1 | 2245 | return 0; |
52632209 | 2246 | } |
9d93ccd1 JS |
2247 | |
2248 | if (file_exists(rebase_path_amend())) { | |
2249 | struct strbuf rev = STRBUF_INIT; | |
092bbcdf | 2250 | struct object_id head, to_amend; |
9d93ccd1 | 2251 | |
092bbcdf | 2252 | if (get_oid("HEAD", &head)) |
9d93ccd1 JS |
2253 | return error(_("cannot amend non-existing commit")); |
2254 | if (!read_oneliner(&rev, rebase_path_amend(), 0)) | |
2255 | return error(_("invalid file: '%s'"), rebase_path_amend()); | |
092bbcdf | 2256 | if (get_oid_hex(rev.buf, &to_amend)) |
9d93ccd1 JS |
2257 | return error(_("invalid contents: '%s'"), |
2258 | rebase_path_amend()); | |
092bbcdf | 2259 | if (oidcmp(&head, &to_amend)) |
9d93ccd1 JS |
2260 | return error(_("\nYou have uncommitted changes in your " |
2261 | "working tree. Please, commit them\n" | |
2262 | "first and then run 'git rebase " | |
2263 | "--continue' again.")); | |
2264 | ||
2265 | strbuf_release(&rev); | |
789b3eff | 2266 | flags |= AMEND_MSG; |
9d93ccd1 JS |
2267 | } |
2268 | ||
789b3eff | 2269 | if (run_git_commit(rebase_path_message(), opts, flags)) |
9d93ccd1 JS |
2270 | return error(_("could not commit staged changes.")); |
2271 | unlink(rebase_path_amend()); | |
2272 | return 0; | |
2273 | } | |
2274 | ||
2863584f | 2275 | int sequencer_continue(struct replay_opts *opts) |
043a4492 | 2276 | { |
004fefa7 JS |
2277 | struct todo_list todo_list = TODO_LIST_INIT; |
2278 | int res; | |
043a4492 | 2279 | |
2863584f JS |
2280 | if (read_and_refresh_cache(opts)) |
2281 | return -1; | |
2282 | ||
9d93ccd1 JS |
2283 | if (is_rebase_i(opts)) { |
2284 | if (commit_staged_changes(opts)) | |
2285 | return -1; | |
4258a6da | 2286 | } else if (!file_exists(get_todo_path(opts))) |
043a4492 | 2287 | return continue_single_pick(); |
004fefa7 | 2288 | if (read_populate_opts(opts)) |
0ae42a03 | 2289 | return -1; |
004fefa7 JS |
2290 | if ((res = read_populate_todo(&todo_list, opts))) |
2291 | goto release_todo_list; | |
043a4492 | 2292 | |
4258a6da JS |
2293 | if (!is_rebase_i(opts)) { |
2294 | /* Verify that the conflict has been resolved */ | |
2295 | if (file_exists(git_path_cherry_pick_head()) || | |
2296 | file_exists(git_path_revert_head())) { | |
2297 | res = continue_single_pick(); | |
2298 | if (res) | |
2299 | goto release_todo_list; | |
2300 | } | |
02f2f56b | 2301 | if (index_differs_from("HEAD", NULL, 0)) { |
4258a6da | 2302 | res = error_dirty_index(opts); |
004fefa7 | 2303 | goto release_todo_list; |
4258a6da JS |
2304 | } |
2305 | todo_list.current++; | |
ca98c6d4 JS |
2306 | } else if (file_exists(rebase_path_stopped_sha())) { |
2307 | struct strbuf buf = STRBUF_INIT; | |
2308 | struct object_id oid; | |
2309 | ||
2310 | if (read_oneliner(&buf, rebase_path_stopped_sha(), 1) && | |
e82caf38 | 2311 | !get_oid_committish(buf.buf, &oid)) |
ca98c6d4 JS |
2312 | record_in_rewritten(&oid, peek_command(&todo_list, 0)); |
2313 | strbuf_release(&buf); | |
043a4492 | 2314 | } |
4258a6da | 2315 | |
004fefa7 JS |
2316 | res = pick_commits(&todo_list, opts); |
2317 | release_todo_list: | |
2318 | todo_list_release(&todo_list); | |
2319 | return res; | |
043a4492 RR |
2320 | } |
2321 | ||
2322 | static int single_pick(struct commit *cmit, struct replay_opts *opts) | |
2323 | { | |
2324 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
004fefa7 | 2325 | return do_pick_commit(opts->action == REPLAY_PICK ? |
6e98de72 | 2326 | TODO_PICK : TODO_REVERT, cmit, opts, 0); |
043a4492 RR |
2327 | } |
2328 | ||
2329 | int sequencer_pick_revisions(struct replay_opts *opts) | |
2330 | { | |
004fefa7 | 2331 | struct todo_list todo_list = TODO_LIST_INIT; |
1e43ed98 | 2332 | struct object_id oid; |
004fefa7 | 2333 | int i, res; |
043a4492 | 2334 | |
2863584f | 2335 | assert(opts->revs); |
0d9c6dc9 JS |
2336 | if (read_and_refresh_cache(opts)) |
2337 | return -1; | |
043a4492 | 2338 | |
21246dbb | 2339 | for (i = 0; i < opts->revs->pending.nr; i++) { |
1e43ed98 | 2340 | struct object_id oid; |
21246dbb MV |
2341 | const char *name = opts->revs->pending.objects[i].name; |
2342 | ||
2343 | /* This happens when using --stdin. */ | |
2344 | if (!strlen(name)) | |
2345 | continue; | |
2346 | ||
1e43ed98 | 2347 | if (!get_oid(name, &oid)) { |
bc83266a | 2348 | if (!lookup_commit_reference_gently(&oid, 1)) { |
1e43ed98 | 2349 | enum object_type type = sha1_object_info(oid.hash, NULL); |
b9b946d4 | 2350 | return error(_("%s: can't cherry-pick a %s"), |
debca9d2 | 2351 | name, type_name(type)); |
7c0b0d8d | 2352 | } |
21246dbb | 2353 | } else |
b9b946d4 | 2354 | return error(_("%s: bad revision"), name); |
21246dbb MV |
2355 | } |
2356 | ||
043a4492 RR |
2357 | /* |
2358 | * If we were called as "git cherry-pick <commit>", just | |
2359 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / | |
2360 | * REVERT_HEAD, and don't touch the sequencer state. | |
2361 | * This means it is possible to cherry-pick in the middle | |
2362 | * of a cherry-pick sequence. | |
2363 | */ | |
2364 | if (opts->revs->cmdline.nr == 1 && | |
2365 | opts->revs->cmdline.rev->whence == REV_CMD_REV && | |
2366 | opts->revs->no_walk && | |
2367 | !opts->revs->cmdline.rev->flags) { | |
2368 | struct commit *cmit; | |
2369 | if (prepare_revision_walk(opts->revs)) | |
b9b946d4 | 2370 | return error(_("revision walk setup failed")); |
043a4492 RR |
2371 | cmit = get_revision(opts->revs); |
2372 | if (!cmit || get_revision(opts->revs)) | |
b9b946d4 | 2373 | return error("BUG: expected exactly one commit from walk"); |
043a4492 RR |
2374 | return single_pick(cmit, opts); |
2375 | } | |
2376 | ||
2377 | /* | |
2378 | * Start a new cherry-pick/ revert sequence; but | |
2379 | * first, make sure that an existing one isn't in | |
2380 | * progress | |
2381 | */ | |
2382 | ||
34b0528b JS |
2383 | if (walk_revs_populate_todo(&todo_list, opts) || |
2384 | create_seq_dir() < 0) | |
043a4492 | 2385 | return -1; |
1e43ed98 | 2386 | if (get_oid("HEAD", &oid) && (opts->action == REPLAY_REVERT)) |
93b3df6f | 2387 | return error(_("can't revert as initial commit")); |
1e43ed98 | 2388 | if (save_head(oid_to_hex(&oid))) |
311fd397 | 2389 | return -1; |
88d5a271 JS |
2390 | if (save_opts(opts)) |
2391 | return -1; | |
1e41229d | 2392 | update_abort_safety_file(); |
004fefa7 JS |
2393 | res = pick_commits(&todo_list, opts); |
2394 | todo_list_release(&todo_list); | |
2395 | return res; | |
043a4492 | 2396 | } |
5ed75e2a | 2397 | |
bab4d109 | 2398 | void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) |
5ed75e2a | 2399 | { |
bab4d109 | 2400 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
5ed75e2a | 2401 | struct strbuf sob = STRBUF_INIT; |
bab4d109 | 2402 | int has_footer; |
5ed75e2a MV |
2403 | |
2404 | strbuf_addstr(&sob, sign_off_header); | |
2405 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), | |
2406 | getenv("GIT_COMMITTER_EMAIL"))); | |
2407 | strbuf_addch(&sob, '\n'); | |
bab4d109 | 2408 | |
44dc738a JT |
2409 | if (!ignore_footer) |
2410 | strbuf_complete_line(msgbuf); | |
2411 | ||
bab4d109 BC |
2412 | /* |
2413 | * If the whole message buffer is equal to the sob, pretend that we | |
2414 | * found a conforming footer with a matching sob | |
2415 | */ | |
2416 | if (msgbuf->len - ignore_footer == sob.len && | |
2417 | !strncmp(msgbuf->buf, sob.buf, sob.len)) | |
2418 | has_footer = 3; | |
2419 | else | |
2420 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); | |
2421 | ||
33f2f9ab BC |
2422 | if (!has_footer) { |
2423 | const char *append_newlines = NULL; | |
2424 | size_t len = msgbuf->len - ignore_footer; | |
2425 | ||
8c613fd5 BC |
2426 | if (!len) { |
2427 | /* | |
2428 | * The buffer is completely empty. Leave foom for | |
2429 | * the title and body to be filled in by the user. | |
2430 | */ | |
33f2f9ab | 2431 | append_newlines = "\n\n"; |
8c613fd5 BC |
2432 | } else if (len == 1) { |
2433 | /* | |
2434 | * Buffer contains a single newline. Add another | |
2435 | * so that we leave room for the title and body. | |
2436 | */ | |
2437 | append_newlines = "\n"; | |
2438 | } else if (msgbuf->buf[len - 2] != '\n') { | |
2439 | /* | |
2440 | * Buffer ends with a single newline. Add another | |
2441 | * so that there is an empty line between the message | |
2442 | * body and the sob. | |
2443 | */ | |
33f2f9ab | 2444 | append_newlines = "\n"; |
8c613fd5 | 2445 | } /* else, the buffer already ends with two newlines. */ |
33f2f9ab BC |
2446 | |
2447 | if (append_newlines) | |
2448 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
2449 | append_newlines, strlen(append_newlines)); | |
5ed75e2a | 2450 | } |
bab4d109 BC |
2451 | |
2452 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) | |
2453 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
2454 | sob.buf, sob.len); | |
2455 | ||
5ed75e2a MV |
2456 | strbuf_release(&sob); |
2457 | } | |
62db5247 | 2458 | |
313a48ea LB |
2459 | int sequencer_make_script(FILE *out, int argc, const char **argv, |
2460 | unsigned flags) | |
62db5247 JS |
2461 | { |
2462 | char *format = NULL; | |
2463 | struct pretty_print_context pp = {0}; | |
2464 | struct strbuf buf = STRBUF_INIT; | |
2465 | struct rev_info revs; | |
2466 | struct commit *commit; | |
313a48ea | 2467 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
d8ae6c84 | 2468 | const char *insn = flags & TODO_LIST_ABBREVIATE_CMDS ? "p" : "pick"; |
62db5247 JS |
2469 | |
2470 | init_revisions(&revs, NULL); | |
2471 | revs.verbose_header = 1; | |
2472 | revs.max_parents = 1; | |
2473 | revs.cherry_pick = 1; | |
2474 | revs.limited = 1; | |
2475 | revs.reverse = 1; | |
2476 | revs.right_only = 1; | |
2477 | revs.sort_order = REV_SORT_IN_GRAPH_ORDER; | |
2478 | revs.topo_order = 1; | |
2479 | ||
2480 | revs.pretty_given = 1; | |
2481 | git_config_get_string("rebase.instructionFormat", &format); | |
2482 | if (!format || !*format) { | |
2483 | free(format); | |
2484 | format = xstrdup("%s"); | |
2485 | } | |
2486 | get_commit_format(format, &revs); | |
2487 | free(format); | |
2488 | pp.fmt = revs.commit_format; | |
2489 | pp.output_encoding = get_log_output_encoding(); | |
2490 | ||
2491 | if (setup_revisions(argc, argv, &revs, NULL) > 1) | |
2492 | return error(_("make_script: unhandled options")); | |
2493 | ||
2494 | if (prepare_revision_walk(&revs) < 0) | |
2495 | return error(_("make_script: error preparing revisions")); | |
2496 | ||
2497 | while ((commit = get_revision(&revs))) { | |
2498 | strbuf_reset(&buf); | |
2499 | if (!keep_empty && is_original_commit_empty(commit)) | |
2500 | strbuf_addf(&buf, "%c ", comment_line_char); | |
d8ae6c84 LB |
2501 | strbuf_addf(&buf, "%s %s ", insn, |
2502 | oid_to_hex(&commit->object.oid)); | |
62db5247 JS |
2503 | pretty_print_commit(&pp, commit, &buf); |
2504 | strbuf_addch(&buf, '\n'); | |
2505 | fputs(buf.buf, out); | |
2506 | } | |
2507 | strbuf_release(&buf); | |
2508 | return 0; | |
2509 | } | |
3546c8d9 | 2510 | |
0cce4a27 LB |
2511 | /* |
2512 | * Add commands after pick and (series of) squash/fixup commands | |
2513 | * in the todo list. | |
2514 | */ | |
2515 | int sequencer_add_exec_commands(const char *commands) | |
3546c8d9 JS |
2516 | { |
2517 | const char *todo_file = rebase_path_todo(); | |
2518 | struct todo_list todo_list = TODO_LIST_INIT; | |
0cce4a27 LB |
2519 | struct todo_item *item; |
2520 | struct strbuf *buf = &todo_list.buf; | |
2521 | size_t offset = 0, commands_len = strlen(commands); | |
2522 | int i, first; | |
3546c8d9 | 2523 | |
0cce4a27 | 2524 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
3546c8d9 | 2525 | return error(_("could not read '%s'."), todo_file); |
3546c8d9 | 2526 | |
0cce4a27 | 2527 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) { |
3546c8d9 JS |
2528 | todo_list_release(&todo_list); |
2529 | return error(_("unusable todo list: '%s'"), todo_file); | |
2530 | } | |
2531 | ||
0cce4a27 LB |
2532 | first = 1; |
2533 | /* insert <commands> before every pick except the first one */ | |
2534 | for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) { | |
2535 | if (item->command == TODO_PICK && !first) { | |
2536 | strbuf_insert(buf, item->offset_in_buf + offset, | |
2537 | commands, commands_len); | |
2538 | offset += commands_len; | |
2539 | } | |
2540 | first = 0; | |
2541 | } | |
2542 | ||
2543 | /* append final <commands> */ | |
2544 | strbuf_add(buf, commands, commands_len); | |
2545 | ||
2546 | i = write_message(buf->buf, buf->len, todo_file, 0); | |
2547 | todo_list_release(&todo_list); | |
2548 | return i; | |
2549 | } | |
3546c8d9 | 2550 | |
313a48ea | 2551 | int transform_todos(unsigned flags) |
3546c8d9 JS |
2552 | { |
2553 | const char *todo_file = rebase_path_todo(); | |
2554 | struct todo_list todo_list = TODO_LIST_INIT; | |
8dccc7a6 LB |
2555 | struct strbuf buf = STRBUF_INIT; |
2556 | struct todo_item *item; | |
2557 | int i; | |
3546c8d9 | 2558 | |
8dccc7a6 | 2559 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
3546c8d9 | 2560 | return error(_("could not read '%s'."), todo_file); |
3546c8d9 | 2561 | |
8dccc7a6 | 2562 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) { |
3546c8d9 | 2563 | todo_list_release(&todo_list); |
3546c8d9 JS |
2564 | return error(_("unusable todo list: '%s'"), todo_file); |
2565 | } | |
2566 | ||
8dccc7a6 LB |
2567 | for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) { |
2568 | /* if the item is not a command write it and continue */ | |
2569 | if (item->command >= TODO_COMMENT) { | |
2570 | strbuf_addf(&buf, "%.*s\n", item->arg_len, item->arg); | |
2571 | continue; | |
3546c8d9 | 2572 | } |
8dccc7a6 LB |
2573 | |
2574 | /* add command to the buffer */ | |
d8ae6c84 LB |
2575 | if (flags & TODO_LIST_ABBREVIATE_CMDS) |
2576 | strbuf_addch(&buf, command_to_char(item->command)); | |
2577 | else | |
2578 | strbuf_addstr(&buf, command_to_string(item->command)); | |
8dccc7a6 LB |
2579 | |
2580 | /* add commit id */ | |
2581 | if (item->commit) { | |
313a48ea | 2582 | const char *oid = flags & TODO_LIST_SHORTEN_IDS ? |
8dccc7a6 LB |
2583 | short_commit_name(item->commit) : |
2584 | oid_to_hex(&item->commit->object.oid); | |
2585 | ||
2586 | strbuf_addf(&buf, " %s", oid); | |
3546c8d9 | 2587 | } |
8dccc7a6 | 2588 | /* add all the rest */ |
c7b4d79c JS |
2589 | if (!item->arg_len) |
2590 | strbuf_addch(&buf, '\n'); | |
2591 | else | |
2592 | strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg); | |
3546c8d9 | 2593 | } |
8dccc7a6 LB |
2594 | |
2595 | i = write_message(buf.buf, buf.len, todo_file, 0); | |
3546c8d9 | 2596 | todo_list_release(&todo_list); |
8dccc7a6 | 2597 | return i; |
3546c8d9 | 2598 | } |
94399949 JS |
2599 | |
2600 | enum check_level { | |
2601 | CHECK_IGNORE = 0, CHECK_WARN, CHECK_ERROR | |
2602 | }; | |
2603 | ||
2604 | static enum check_level get_missing_commit_check_level(void) | |
2605 | { | |
2606 | const char *value; | |
2607 | ||
2608 | if (git_config_get_value("rebase.missingcommitscheck", &value) || | |
2609 | !strcasecmp("ignore", value)) | |
2610 | return CHECK_IGNORE; | |
2611 | if (!strcasecmp("warn", value)) | |
2612 | return CHECK_WARN; | |
2613 | if (!strcasecmp("error", value)) | |
2614 | return CHECK_ERROR; | |
dfab1eac | 2615 | warning(_("unrecognized setting %s for option " |
94399949 JS |
2616 | "rebase.missingCommitsCheck. Ignoring."), value); |
2617 | return CHECK_IGNORE; | |
2618 | } | |
2619 | ||
2620 | /* | |
2621 | * Check if the user dropped some commits by mistake | |
2622 | * Behaviour determined by rebase.missingCommitsCheck. | |
2623 | * Check if there is an unrecognized command or a | |
2624 | * bad SHA-1 in a command. | |
2625 | */ | |
2626 | int check_todo_list(void) | |
2627 | { | |
2628 | enum check_level check_level = get_missing_commit_check_level(); | |
2629 | struct strbuf todo_file = STRBUF_INIT; | |
2630 | struct todo_list todo_list = TODO_LIST_INIT; | |
2631 | struct strbuf missing = STRBUF_INIT; | |
2632 | int advise_to_edit_todo = 0, res = 0, fd, i; | |
2633 | ||
2634 | strbuf_addstr(&todo_file, rebase_path_todo()); | |
2635 | fd = open(todo_file.buf, O_RDONLY); | |
2636 | if (fd < 0) { | |
2637 | res = error_errno(_("could not open '%s'"), todo_file.buf); | |
2638 | goto leave_check; | |
2639 | } | |
2640 | if (strbuf_read(&todo_list.buf, fd, 0) < 0) { | |
2641 | close(fd); | |
2642 | res = error(_("could not read '%s'."), todo_file.buf); | |
2643 | goto leave_check; | |
2644 | } | |
2645 | close(fd); | |
2646 | advise_to_edit_todo = res = | |
2647 | parse_insn_buffer(todo_list.buf.buf, &todo_list); | |
2648 | ||
2649 | if (res || check_level == CHECK_IGNORE) | |
2650 | goto leave_check; | |
2651 | ||
2652 | /* Mark the commits in git-rebase-todo as seen */ | |
2653 | for (i = 0; i < todo_list.nr; i++) { | |
2654 | struct commit *commit = todo_list.items[i].commit; | |
2655 | if (commit) | |
2656 | commit->util = (void *)1; | |
2657 | } | |
2658 | ||
2659 | todo_list_release(&todo_list); | |
2660 | strbuf_addstr(&todo_file, ".backup"); | |
2661 | fd = open(todo_file.buf, O_RDONLY); | |
2662 | if (fd < 0) { | |
2663 | res = error_errno(_("could not open '%s'"), todo_file.buf); | |
2664 | goto leave_check; | |
2665 | } | |
2666 | if (strbuf_read(&todo_list.buf, fd, 0) < 0) { | |
2667 | close(fd); | |
2668 | res = error(_("could not read '%s'."), todo_file.buf); | |
2669 | goto leave_check; | |
2670 | } | |
2671 | close(fd); | |
2672 | strbuf_release(&todo_file); | |
2673 | res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list); | |
2674 | ||
2675 | /* Find commits in git-rebase-todo.backup yet unseen */ | |
2676 | for (i = todo_list.nr - 1; i >= 0; i--) { | |
2677 | struct todo_item *item = todo_list.items + i; | |
2678 | struct commit *commit = item->commit; | |
2679 | if (commit && !commit->util) { | |
2680 | strbuf_addf(&missing, " - %s %.*s\n", | |
2681 | short_commit_name(commit), | |
2682 | item->arg_len, item->arg); | |
2683 | commit->util = (void *)1; | |
2684 | } | |
2685 | } | |
2686 | ||
2687 | /* Warn about missing commits */ | |
2688 | if (!missing.len) | |
2689 | goto leave_check; | |
2690 | ||
2691 | if (check_level == CHECK_ERROR) | |
2692 | advise_to_edit_todo = res = 1; | |
2693 | ||
2694 | fprintf(stderr, | |
2695 | _("Warning: some commits may have been dropped accidentally.\n" | |
2696 | "Dropped commits (newer to older):\n")); | |
2697 | ||
2698 | /* Make the list user-friendly and display */ | |
2699 | fputs(missing.buf, stderr); | |
2700 | strbuf_release(&missing); | |
2701 | ||
2702 | fprintf(stderr, _("To avoid this message, use \"drop\" to " | |
2703 | "explicitly remove a commit.\n\n" | |
2704 | "Use 'git config rebase.missingCommitsCheck' to change " | |
2705 | "the level of warnings.\n" | |
2706 | "The possible behaviours are: ignore, warn, error.\n\n")); | |
2707 | ||
2708 | leave_check: | |
2709 | strbuf_release(&todo_file); | |
2710 | todo_list_release(&todo_list); | |
2711 | ||
2712 | if (advise_to_edit_todo) | |
2713 | fprintf(stderr, | |
2714 | _("You can fix this with 'git rebase --edit-todo' " | |
2715 | "and then run 'git rebase --continue'.\n" | |
2716 | "Or you can abort the rebase with 'git rebase" | |
2717 | " --abort'.\n")); | |
2718 | ||
2719 | return res; | |
2720 | } | |
cdac2b01 | 2721 | |
73646bfd RS |
2722 | static int rewrite_file(const char *path, const char *buf, size_t len) |
2723 | { | |
2724 | int rc = 0; | |
c8cee96e | 2725 | int fd = open(path, O_WRONLY | O_TRUNC); |
73646bfd RS |
2726 | if (fd < 0) |
2727 | return error_errno(_("could not open '%s' for writing"), path); | |
2728 | if (write_in_full(fd, buf, len) < 0) | |
2729 | rc = error_errno(_("could not write to '%s'"), path); | |
9360ec00 SR |
2730 | if (close(fd) && !rc) |
2731 | rc = error_errno(_("could not close '%s'"), path); | |
73646bfd RS |
2732 | return rc; |
2733 | } | |
2734 | ||
cdac2b01 JS |
2735 | /* skip picking commits whose parents are unchanged */ |
2736 | int skip_unnecessary_picks(void) | |
2737 | { | |
2738 | const char *todo_file = rebase_path_todo(); | |
2739 | struct strbuf buf = STRBUF_INIT; | |
2740 | struct todo_list todo_list = TODO_LIST_INIT; | |
2741 | struct object_id onto_oid, *oid = &onto_oid, *parent_oid; | |
2742 | int fd, i; | |
2743 | ||
2744 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) | |
2745 | return error(_("could not read 'onto'")); | |
2746 | if (get_oid(buf.buf, &onto_oid)) { | |
2747 | strbuf_release(&buf); | |
2748 | return error(_("need a HEAD to fixup")); | |
2749 | } | |
2750 | strbuf_release(&buf); | |
2751 | ||
2752 | fd = open(todo_file, O_RDONLY); | |
2753 | if (fd < 0) { | |
2754 | return error_errno(_("could not open '%s'"), todo_file); | |
2755 | } | |
2756 | if (strbuf_read(&todo_list.buf, fd, 0) < 0) { | |
2757 | close(fd); | |
2758 | return error(_("could not read '%s'."), todo_file); | |
2759 | } | |
2760 | close(fd); | |
2761 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) { | |
2762 | todo_list_release(&todo_list); | |
2763 | return -1; | |
2764 | } | |
2765 | ||
2766 | for (i = 0; i < todo_list.nr; i++) { | |
2767 | struct todo_item *item = todo_list.items + i; | |
2768 | ||
2769 | if (item->command >= TODO_NOOP) | |
2770 | continue; | |
2771 | if (item->command != TODO_PICK) | |
2772 | break; | |
2773 | if (parse_commit(item->commit)) { | |
2774 | todo_list_release(&todo_list); | |
2775 | return error(_("could not parse commit '%s'"), | |
2776 | oid_to_hex(&item->commit->object.oid)); | |
2777 | } | |
2778 | if (!item->commit->parents) | |
2779 | break; /* root commit */ | |
2780 | if (item->commit->parents->next) | |
2781 | break; /* merge commit */ | |
2782 | parent_oid = &item->commit->parents->item->object.oid; | |
2783 | if (hashcmp(parent_oid->hash, oid->hash)) | |
2784 | break; | |
2785 | oid = &item->commit->object.oid; | |
2786 | } | |
2787 | if (i > 0) { | |
2788 | int offset = i < todo_list.nr ? | |
2789 | todo_list.items[i].offset_in_buf : todo_list.buf.len; | |
2790 | const char *done_path = rebase_path_done(); | |
2791 | ||
2792 | fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
2793 | if (fd < 0) { | |
2794 | error_errno(_("could not open '%s' for writing"), | |
2795 | done_path); | |
2796 | todo_list_release(&todo_list); | |
2797 | return -1; | |
2798 | } | |
2799 | if (write_in_full(fd, todo_list.buf.buf, offset) < 0) { | |
2800 | error_errno(_("could not write to '%s'"), done_path); | |
2801 | todo_list_release(&todo_list); | |
2802 | close(fd); | |
2803 | return -1; | |
2804 | } | |
2805 | close(fd); | |
2806 | ||
73646bfd RS |
2807 | if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset, |
2808 | todo_list.buf.len - offset) < 0) { | |
cdac2b01 | 2809 | todo_list_release(&todo_list); |
cdac2b01 JS |
2810 | return -1; |
2811 | } | |
cdac2b01 JS |
2812 | |
2813 | todo_list.current = i; | |
2814 | if (is_fixup(peek_command(&todo_list, 0))) | |
2815 | record_in_rewritten(oid, peek_command(&todo_list, 0)); | |
2816 | } | |
2817 | ||
2818 | todo_list_release(&todo_list); | |
2819 | printf("%s\n", oid_to_hex(oid)); | |
2820 | ||
2821 | return 0; | |
2822 | } | |
c44a4c65 JS |
2823 | |
2824 | struct subject2item_entry { | |
2825 | struct hashmap_entry entry; | |
2826 | int i; | |
2827 | char subject[FLEX_ARRAY]; | |
2828 | }; | |
2829 | ||
2830 | static int subject2item_cmp(const void *fndata, | |
2831 | const struct subject2item_entry *a, | |
2832 | const struct subject2item_entry *b, const void *key) | |
2833 | { | |
2834 | return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject); | |
2835 | } | |
2836 | ||
2837 | /* | |
2838 | * Rearrange the todo list that has both "pick commit-id msg" and "pick | |
2839 | * commit-id fixup!/squash! msg" in it so that the latter is put immediately | |
2840 | * after the former, and change "pick" to "fixup"/"squash". | |
2841 | * | |
2842 | * Note that if the config has specified a custom instruction format, each log | |
2843 | * message will have to be retrieved from the commit (as the oneline in the | |
2844 | * script cannot be trusted) in order to normalize the autosquash arrangement. | |
2845 | */ | |
2846 | int rearrange_squash(void) | |
2847 | { | |
2848 | const char *todo_file = rebase_path_todo(); | |
2849 | struct todo_list todo_list = TODO_LIST_INIT; | |
2850 | struct hashmap subject2item; | |
2851 | int res = 0, rearranged = 0, *next, *tail, fd, i; | |
2852 | char **subjects; | |
2853 | ||
2854 | fd = open(todo_file, O_RDONLY); | |
2855 | if (fd < 0) | |
2856 | return error_errno(_("could not open '%s'"), todo_file); | |
2857 | if (strbuf_read(&todo_list.buf, fd, 0) < 0) { | |
2858 | close(fd); | |
2859 | return error(_("could not read '%s'."), todo_file); | |
2860 | } | |
2861 | close(fd); | |
2862 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) { | |
2863 | todo_list_release(&todo_list); | |
2864 | return -1; | |
2865 | } | |
2866 | ||
2867 | /* | |
2868 | * The hashmap maps onelines to the respective todo list index. | |
2869 | * | |
2870 | * If any items need to be rearranged, the next[i] value will indicate | |
2871 | * which item was moved directly after the i'th. | |
2872 | * | |
2873 | * In that case, last[i] will indicate the index of the latest item to | |
2874 | * be moved to appear after the i'th. | |
2875 | */ | |
2876 | hashmap_init(&subject2item, (hashmap_cmp_fn) subject2item_cmp, | |
2877 | NULL, todo_list.nr); | |
2878 | ALLOC_ARRAY(next, todo_list.nr); | |
2879 | ALLOC_ARRAY(tail, todo_list.nr); | |
2880 | ALLOC_ARRAY(subjects, todo_list.nr); | |
2881 | for (i = 0; i < todo_list.nr; i++) { | |
2882 | struct strbuf buf = STRBUF_INIT; | |
2883 | struct todo_item *item = todo_list.items + i; | |
2884 | const char *commit_buffer, *subject, *p; | |
2885 | size_t subject_len; | |
2886 | int i2 = -1; | |
2887 | struct subject2item_entry *entry; | |
2888 | ||
2889 | next[i] = tail[i] = -1; | |
2890 | if (item->command >= TODO_EXEC) { | |
2891 | subjects[i] = NULL; | |
2892 | continue; | |
2893 | } | |
2894 | ||
2895 | if (is_fixup(item->command)) { | |
2896 | todo_list_release(&todo_list); | |
2897 | return error(_("the script was already rearranged.")); | |
2898 | } | |
2899 | ||
2900 | item->commit->util = item; | |
2901 | ||
2902 | parse_commit(item->commit); | |
2903 | commit_buffer = get_commit_buffer(item->commit, NULL); | |
2904 | find_commit_subject(commit_buffer, &subject); | |
2905 | format_subject(&buf, subject, " "); | |
2906 | subject = subjects[i] = strbuf_detach(&buf, &subject_len); | |
2907 | unuse_commit_buffer(item->commit, commit_buffer); | |
2908 | if ((skip_prefix(subject, "fixup! ", &p) || | |
2909 | skip_prefix(subject, "squash! ", &p))) { | |
2910 | struct commit *commit2; | |
2911 | ||
2912 | for (;;) { | |
2913 | while (isspace(*p)) | |
2914 | p++; | |
2915 | if (!skip_prefix(p, "fixup! ", &p) && | |
2916 | !skip_prefix(p, "squash! ", &p)) | |
2917 | break; | |
2918 | } | |
2919 | ||
2920 | if ((entry = hashmap_get_from_hash(&subject2item, | |
2921 | strhash(p), p))) | |
2922 | /* found by title */ | |
2923 | i2 = entry->i; | |
2924 | else if (!strchr(p, ' ') && | |
2925 | (commit2 = | |
2926 | lookup_commit_reference_by_name(p)) && | |
2927 | commit2->util) | |
2928 | /* found by commit name */ | |
2929 | i2 = (struct todo_item *)commit2->util | |
2930 | - todo_list.items; | |
2931 | else { | |
2932 | /* copy can be a prefix of the commit subject */ | |
2933 | for (i2 = 0; i2 < i; i2++) | |
2934 | if (subjects[i2] && | |
2935 | starts_with(subjects[i2], p)) | |
2936 | break; | |
2937 | if (i2 == i) | |
2938 | i2 = -1; | |
2939 | } | |
2940 | } | |
2941 | if (i2 >= 0) { | |
2942 | rearranged = 1; | |
2943 | todo_list.items[i].command = | |
2944 | starts_with(subject, "fixup!") ? | |
2945 | TODO_FIXUP : TODO_SQUASH; | |
2946 | if (next[i2] < 0) | |
2947 | next[i2] = i; | |
2948 | else | |
2949 | next[tail[i2]] = i; | |
2950 | tail[i2] = i; | |
2951 | } else if (!hashmap_get_from_hash(&subject2item, | |
2952 | strhash(subject), subject)) { | |
2953 | FLEX_ALLOC_MEM(entry, subject, subject, subject_len); | |
2954 | entry->i = i; | |
2955 | hashmap_entry_init(entry, strhash(entry->subject)); | |
2956 | hashmap_put(&subject2item, entry); | |
2957 | } | |
2958 | } | |
2959 | ||
2960 | if (rearranged) { | |
2961 | struct strbuf buf = STRBUF_INIT; | |
2962 | ||
2963 | for (i = 0; i < todo_list.nr; i++) { | |
2964 | enum todo_command command = todo_list.items[i].command; | |
2965 | int cur = i; | |
2966 | ||
2967 | /* | |
2968 | * Initially, all commands are 'pick's. If it is a | |
2969 | * fixup or a squash now, we have rearranged it. | |
2970 | */ | |
2971 | if (is_fixup(command)) | |
2972 | continue; | |
2973 | ||
2974 | while (cur >= 0) { | |
2975 | int offset = todo_list.items[cur].offset_in_buf; | |
2976 | int end_offset = cur + 1 < todo_list.nr ? | |
2977 | todo_list.items[cur + 1].offset_in_buf : | |
2978 | todo_list.buf.len; | |
2979 | char *bol = todo_list.buf.buf + offset; | |
2980 | char *eol = todo_list.buf.buf + end_offset; | |
2981 | ||
2982 | /* replace 'pick', by 'fixup' or 'squash' */ | |
2983 | command = todo_list.items[cur].command; | |
2984 | if (is_fixup(command)) { | |
2985 | strbuf_addstr(&buf, | |
2986 | todo_command_info[command].str); | |
2987 | bol += strcspn(bol, " \t"); | |
2988 | } | |
2989 | ||
2990 | strbuf_add(&buf, bol, eol - bol); | |
2991 | ||
2992 | cur = next[cur]; | |
2993 | } | |
2994 | } | |
2995 | ||
73646bfd | 2996 | res = rewrite_file(todo_file, buf.buf, buf.len); |
c44a4c65 JS |
2997 | strbuf_release(&buf); |
2998 | } | |
2999 | ||
3000 | free(next); | |
3001 | free(tail); | |
3002 | for (i = 0; i < todo_list.nr; i++) | |
3003 | free(subjects[i]); | |
3004 | free(subjects); | |
3005 | hashmap_free(&subject2item, 1); | |
3006 | todo_list_release(&todo_list); | |
3007 | ||
3008 | return res; | |
3009 | } |