]>
Commit | Line | Data |
---|---|---|
26ae337b | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
697cc8ef | 3 | #include "lockfile.h" |
26ae337b | 4 | #include "dir.h" |
043a4492 RR |
5 | #include "object.h" |
6 | #include "commit.h" | |
0505d604 | 7 | #include "sequencer.h" |
043a4492 RR |
8 | #include "tag.h" |
9 | #include "run-command.h" | |
d807c4a0 | 10 | #include "exec-cmd.h" |
043a4492 RR |
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" |
a87a6f3c PW |
24 | #include "notes-utils.h" |
25 | #include "sigchain.h" | |
9055e401 JS |
26 | #include "unpack-trees.h" |
27 | #include "worktree.h" | |
1644c73c JS |
28 | #include "oidmap.h" |
29 | #include "oidset.h" | |
043a4492 RR |
30 | |
31 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" | |
26ae337b | 32 | |
5ed75e2a | 33 | const char sign_off_header[] = "Signed-off-by: "; |
cd650a4e | 34 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
5ed75e2a | 35 | |
66618a50 PW |
36 | GIT_PATH_FUNC(git_path_commit_editmsg, "COMMIT_EDITMSG") |
37 | ||
8a2a0f53 JS |
38 | GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
39 | ||
40 | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") | |
41 | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") | |
42 | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") | |
1e41229d | 43 | static GIT_PATH_FUNC(git_path_abort_safety_file, "sequencer/abort-safety") |
f932729c | 44 | |
84583957 JS |
45 | static GIT_PATH_FUNC(rebase_path, "rebase-merge") |
46 | /* | |
47 | * The file containing rebase commands, comments, and empty lines. | |
48 | * This file is created by "git rebase -i" then edited by the user. As | |
49 | * the lines are processed, they are removed from the front of this | |
50 | * file and written to the tail of 'done'. | |
51 | */ | |
52 | static GIT_PATH_FUNC(rebase_path_todo, "rebase-merge/git-rebase-todo") | |
1df6df0c JS |
53 | /* |
54 | * The rebase command lines that have already been processed. A line | |
55 | * is moved here when it is first handled, before any associated user | |
56 | * actions. | |
57 | */ | |
58 | static GIT_PATH_FUNC(rebase_path_done, "rebase-merge/done") | |
ef80069a JS |
59 | /* |
60 | * The file to keep track of how many commands were already processed (e.g. | |
61 | * for the prompt). | |
62 | */ | |
63 | static GIT_PATH_FUNC(rebase_path_msgnum, "rebase-merge/msgnum"); | |
64 | /* | |
65 | * The file to keep track of how many commands are to be processed in total | |
66 | * (e.g. for the prompt). | |
67 | */ | |
68 | static GIT_PATH_FUNC(rebase_path_msgtotal, "rebase-merge/end"); | |
6e98de72 JS |
69 | /* |
70 | * The commit message that is planned to be used for any changes that | |
71 | * need to be committed following a user interaction. | |
72 | */ | |
73 | static GIT_PATH_FUNC(rebase_path_message, "rebase-merge/message") | |
74 | /* | |
75 | * The file into which is accumulated the suggested commit message for | |
76 | * squash/fixup commands. When the first of a series of squash/fixups | |
77 | * is seen, the file is created and the commit message from the | |
78 | * previous commit and from the first squash/fixup commit are written | |
79 | * to it. The commit message for each subsequent squash/fixup commit | |
80 | * is appended to the file as it is processed. | |
81 | * | |
82 | * The first line of the file is of the form | |
83 | * # This is a combination of $count commits. | |
84 | * where $count is the number of commits whose messages have been | |
85 | * written to the file so far (including the initial "pick" commit). | |
86 | * Each time that a commit message is processed, this line is read and | |
87 | * updated. It is deleted just before the combined commit is made. | |
88 | */ | |
89 | static GIT_PATH_FUNC(rebase_path_squash_msg, "rebase-merge/message-squash") | |
90 | /* | |
91 | * If the current series of squash/fixups has not yet included a squash | |
92 | * command, then this file exists and holds the commit message of the | |
93 | * original "pick" commit. (If the series ends without a "squash" | |
94 | * command, then this can be used as the commit message of the combined | |
95 | * commit without opening the editor.) | |
96 | */ | |
97 | static GIT_PATH_FUNC(rebase_path_fixup_msg, "rebase-merge/message-fixup") | |
b5a67045 JS |
98 | /* |
99 | * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and | |
100 | * GIT_AUTHOR_DATE that will be used for the commit that is currently | |
101 | * being rebased. | |
102 | */ | |
103 | static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") | |
56dc3ab0 JS |
104 | /* |
105 | * When an "edit" rebase command is being processed, the SHA1 of the | |
106 | * commit to be edited is recorded in this file. When "git rebase | |
107 | * --continue" is executed, if there are any staged changes then they | |
108 | * will be amended to the HEAD commit, but only provided the HEAD | |
109 | * commit is still the commit to be edited. When any other rebase | |
110 | * command is processed, this file is deleted. | |
111 | */ | |
112 | static GIT_PATH_FUNC(rebase_path_amend, "rebase-merge/amend") | |
113 | /* | |
114 | * When we stop at a given patch via the "edit" command, this file contains | |
115 | * the abbreviated commit name of the corresponding patch. | |
116 | */ | |
117 | static GIT_PATH_FUNC(rebase_path_stopped_sha, "rebase-merge/stopped-sha") | |
25cb8df9 JS |
118 | /* |
119 | * For the post-rewrite hook, we make a list of rewritten commits and | |
120 | * their new sha1s. The rewritten-pending list keeps the sha1s of | |
121 | * commits that have been processed, but not committed yet, | |
122 | * e.g. because they are waiting for a 'squash' command. | |
123 | */ | |
124 | static GIT_PATH_FUNC(rebase_path_rewritten_list, "rebase-merge/rewritten-list") | |
125 | static GIT_PATH_FUNC(rebase_path_rewritten_pending, | |
126 | "rebase-merge/rewritten-pending") | |
9055e401 | 127 | |
d87d48b2 JS |
128 | /* |
129 | * The path of the file containig the OID of the "squash onto" commit, i.e. | |
130 | * the dummy commit used for `reset [new root]`. | |
131 | */ | |
132 | static GIT_PATH_FUNC(rebase_path_squash_onto, "rebase-merge/squash-onto") | |
133 | ||
9055e401 JS |
134 | /* |
135 | * The path of the file listing refs that need to be deleted after the rebase | |
136 | * finishes. This is used by the `label` command to record the need for cleanup. | |
137 | */ | |
138 | static GIT_PATH_FUNC(rebase_path_refs_to_delete, "rebase-merge/refs-to-delete") | |
139 | ||
a1c75762 JS |
140 | /* |
141 | * The following files are written by git-rebase just after parsing the | |
142 | * command-line (and are only consumed, not modified, by the sequencer). | |
143 | */ | |
144 | static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") | |
556907f1 JS |
145 | static GIT_PATH_FUNC(rebase_path_orig_head, "rebase-merge/orig-head") |
146 | static GIT_PATH_FUNC(rebase_path_verbose, "rebase-merge/verbose") | |
a852ec7f | 147 | static GIT_PATH_FUNC(rebase_path_signoff, "rebase-merge/signoff") |
4b83ce9f JS |
148 | static GIT_PATH_FUNC(rebase_path_head_name, "rebase-merge/head-name") |
149 | static GIT_PATH_FUNC(rebase_path_onto, "rebase-merge/onto") | |
796c7972 | 150 | static GIT_PATH_FUNC(rebase_path_autostash, "rebase-merge/autostash") |
ca6c6b45 JS |
151 | static GIT_PATH_FUNC(rebase_path_strategy, "rebase-merge/strategy") |
152 | static GIT_PATH_FUNC(rebase_path_strategy_opts, "rebase-merge/strategy_opts") | |
9b6d7a62 | 153 | static GIT_PATH_FUNC(rebase_path_allow_rerere_autoupdate, "rebase-merge/allow_rerere_autoupdate") |
b5a67045 | 154 | |
28d6daed PW |
155 | static int git_sequencer_config(const char *k, const char *v, void *cb) |
156 | { | |
157 | struct replay_opts *opts = cb; | |
158 | int status; | |
159 | ||
160 | if (!strcmp(k, "commit.cleanup")) { | |
161 | const char *s; | |
162 | ||
163 | status = git_config_string(&s, k, v); | |
164 | if (status) | |
165 | return status; | |
166 | ||
167 | if (!strcmp(s, "verbatim")) | |
168 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; | |
169 | else if (!strcmp(s, "whitespace")) | |
170 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; | |
171 | else if (!strcmp(s, "strip")) | |
172 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_ALL; | |
173 | else if (!strcmp(s, "scissors")) | |
174 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_SPACE; | |
175 | else | |
176 | warning(_("invalid commit message cleanup mode '%s'"), | |
177 | s); | |
178 | ||
179 | return status; | |
180 | } | |
181 | ||
182 | if (!strcmp(k, "commit.gpgsign")) { | |
ed1e5282 | 183 | opts->gpg_sign = git_config_bool(k, v) ? xstrdup("") : NULL; |
28d6daed PW |
184 | return 0; |
185 | } | |
186 | ||
187 | status = git_gpg_config(k, v, NULL); | |
188 | if (status) | |
189 | return status; | |
190 | ||
191 | return git_diff_basic_config(k, v, NULL); | |
192 | } | |
193 | ||
194 | void sequencer_init_config(struct replay_opts *opts) | |
195 | { | |
196 | opts->default_msg_cleanup = COMMIT_MSG_CLEANUP_NONE; | |
197 | git_config(git_sequencer_config, opts); | |
198 | } | |
199 | ||
b5a67045 JS |
200 | static inline int is_rebase_i(const struct replay_opts *opts) |
201 | { | |
84583957 | 202 | return opts->action == REPLAY_INTERACTIVE_REBASE; |
b5a67045 JS |
203 | } |
204 | ||
285abf56 JS |
205 | static const char *get_dir(const struct replay_opts *opts) |
206 | { | |
84583957 JS |
207 | if (is_rebase_i(opts)) |
208 | return rebase_path(); | |
285abf56 JS |
209 | return git_path_seq_dir(); |
210 | } | |
211 | ||
c0246501 JS |
212 | static const char *get_todo_path(const struct replay_opts *opts) |
213 | { | |
84583957 JS |
214 | if (is_rebase_i(opts)) |
215 | return rebase_path_todo(); | |
c0246501 JS |
216 | return git_path_todo_file(); |
217 | } | |
218 | ||
bab4d109 BC |
219 | /* |
220 | * Returns 0 for non-conforming footer | |
221 | * Returns 1 for conforming footer | |
222 | * Returns 2 when sob exists within conforming footer | |
223 | * Returns 3 when sob exists within conforming footer as last entry | |
224 | */ | |
225 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, | |
226 | int ignore_footer) | |
b971e04f | 227 | { |
967dfd4d JT |
228 | struct trailer_info info; |
229 | int i; | |
230 | int found_sob = 0, found_sob_last = 0; | |
b971e04f | 231 | |
967dfd4d | 232 | trailer_info_get(&info, sb->buf); |
b971e04f | 233 | |
967dfd4d | 234 | if (info.trailer_start == info.trailer_end) |
b971e04f BC |
235 | return 0; |
236 | ||
967dfd4d JT |
237 | for (i = 0; i < info.trailer_nr; i++) |
238 | if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { | |
239 | found_sob = 1; | |
240 | if (i == info.trailer_nr - 1) | |
241 | found_sob_last = 1; | |
242 | } | |
b971e04f | 243 | |
967dfd4d | 244 | trailer_info_release(&info); |
bab4d109 | 245 | |
967dfd4d | 246 | if (found_sob_last) |
bab4d109 BC |
247 | return 3; |
248 | if (found_sob) | |
249 | return 2; | |
b971e04f BC |
250 | return 1; |
251 | } | |
5ed75e2a | 252 | |
a1c75762 JS |
253 | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
254 | { | |
255 | static struct strbuf buf = STRBUF_INIT; | |
256 | ||
257 | strbuf_reset(&buf); | |
258 | if (opts->gpg_sign) | |
259 | sq_quotef(&buf, "-S%s", opts->gpg_sign); | |
260 | return buf.buf; | |
261 | } | |
262 | ||
2863584f | 263 | int sequencer_remove_state(struct replay_opts *opts) |
26ae337b | 264 | { |
9055e401 | 265 | struct strbuf buf = STRBUF_INIT; |
03a4e260 JS |
266 | int i; |
267 | ||
9055e401 JS |
268 | if (is_rebase_i(opts) && |
269 | strbuf_read_file(&buf, rebase_path_refs_to_delete(), 0) > 0) { | |
270 | char *p = buf.buf; | |
271 | while (*p) { | |
272 | char *eol = strchr(p, '\n'); | |
273 | if (eol) | |
274 | *eol = '\0'; | |
275 | if (delete_ref("(rebase -i) cleanup", p, NULL, 0) < 0) | |
276 | warning(_("could not delete '%s'"), p); | |
277 | if (!eol) | |
278 | break; | |
279 | p = eol + 1; | |
280 | } | |
281 | } | |
282 | ||
03a4e260 JS |
283 | free(opts->gpg_sign); |
284 | free(opts->strategy); | |
285 | for (i = 0; i < opts->xopts_nr; i++) | |
286 | free(opts->xopts[i]); | |
287 | free(opts->xopts); | |
26ae337b | 288 | |
9055e401 JS |
289 | strbuf_reset(&buf); |
290 | strbuf_addstr(&buf, get_dir(opts)); | |
291 | remove_dir_recursively(&buf, 0); | |
292 | strbuf_release(&buf); | |
2863584f JS |
293 | |
294 | return 0; | |
26ae337b | 295 | } |
043a4492 RR |
296 | |
297 | static const char *action_name(const struct replay_opts *opts) | |
298 | { | |
84583957 JS |
299 | switch (opts->action) { |
300 | case REPLAY_REVERT: | |
301 | return N_("revert"); | |
302 | case REPLAY_PICK: | |
303 | return N_("cherry-pick"); | |
304 | case REPLAY_INTERACTIVE_REBASE: | |
305 | return N_("rebase -i"); | |
306 | } | |
307 | die(_("Unknown action: %d"), opts->action); | |
043a4492 RR |
308 | } |
309 | ||
043a4492 RR |
310 | struct commit_message { |
311 | char *parent_label; | |
7b35eaf8 JK |
312 | char *label; |
313 | char *subject; | |
043a4492 RR |
314 | const char *message; |
315 | }; | |
316 | ||
39755964 JS |
317 | static const char *short_commit_name(struct commit *commit) |
318 | { | |
aab9583f | 319 | return find_unique_abbrev(&commit->object.oid, DEFAULT_ABBREV); |
39755964 JS |
320 | } |
321 | ||
043a4492 RR |
322 | static int get_message(struct commit *commit, struct commit_message *out) |
323 | { | |
043a4492 | 324 | const char *abbrev, *subject; |
7b35eaf8 | 325 | int subject_len; |
043a4492 | 326 | |
7b35eaf8 | 327 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
39755964 | 328 | abbrev = short_commit_name(commit); |
043a4492 RR |
329 | |
330 | subject_len = find_commit_subject(out->message, &subject); | |
331 | ||
7b35eaf8 JK |
332 | out->subject = xmemdupz(subject, subject_len); |
333 | out->label = xstrfmt("%s... %s", abbrev, out->subject); | |
334 | out->parent_label = xstrfmt("parent of %s", out->label); | |
335 | ||
043a4492 RR |
336 | return 0; |
337 | } | |
338 | ||
d74a4e57 | 339 | static void free_message(struct commit *commit, struct commit_message *msg) |
043a4492 RR |
340 | { |
341 | free(msg->parent_label); | |
7b35eaf8 JK |
342 | free(msg->label); |
343 | free(msg->subject); | |
b66103c3 | 344 | unuse_commit_buffer(commit, msg->message); |
043a4492 RR |
345 | } |
346 | ||
ed727b19 | 347 | static void print_advice(int show_hint, struct replay_opts *opts) |
043a4492 RR |
348 | { |
349 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); | |
350 | ||
351 | if (msg) { | |
352 | fprintf(stderr, "%s\n", msg); | |
353 | /* | |
41ccfdd9 | 354 | * A conflict has occurred but the porcelain |
043a4492 RR |
355 | * (typically rebase --interactive) wants to take care |
356 | * of the commit itself so remove CHERRY_PICK_HEAD | |
357 | */ | |
f932729c | 358 | unlink(git_path_cherry_pick_head()); |
043a4492 RR |
359 | return; |
360 | } | |
361 | ||
ed727b19 PH |
362 | if (show_hint) { |
363 | if (opts->no_commit) | |
364 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
365 | "with 'git add <paths>' or 'git rm <paths>'")); | |
366 | else | |
367 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
368 | "with 'git add <paths>' or 'git rm <paths>'\n" | |
369 | "and commit the result with 'git commit'")); | |
370 | } | |
043a4492 RR |
371 | } |
372 | ||
f56fffef JS |
373 | static int write_message(const void *buf, size_t len, const char *filename, |
374 | int append_eol) | |
043a4492 | 375 | { |
14bca6c6 | 376 | struct lock_file msg_file = LOCK_INIT; |
043a4492 | 377 | |
4ef3d8f0 JS |
378 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
379 | if (msg_fd < 0) | |
93b3df6f | 380 | return error_errno(_("could not lock '%s'"), filename); |
75871495 | 381 | if (write_in_full(msg_fd, buf, len) < 0) { |
bf5c0571 | 382 | error_errno(_("could not write to '%s'"), filename); |
4f66c837 | 383 | rollback_lock_file(&msg_file); |
bf5c0571 | 384 | return -1; |
4f66c837 | 385 | } |
f56fffef | 386 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
bf5c0571 | 387 | error_errno(_("could not write eol to '%s'"), filename); |
f56fffef | 388 | rollback_lock_file(&msg_file); |
bf5c0571 | 389 | return -1; |
f56fffef | 390 | } |
350292a1 MÅ |
391 | if (commit_lock_file(&msg_file) < 0) |
392 | return error(_("failed to finalize '%s'"), filename); | |
4ef3d8f0 JS |
393 | |
394 | return 0; | |
043a4492 RR |
395 | } |
396 | ||
1dfc84e9 JS |
397 | /* |
398 | * Reads a file that was presumably written by a shell script, i.e. with an | |
399 | * end-of-line marker that needs to be stripped. | |
400 | * | |
401 | * Note that only the last end-of-line marker is stripped, consistent with the | |
402 | * behavior of "$(cat path)" in a shell script. | |
403 | * | |
404 | * Returns 1 if the file was read, 0 if it could not be read or does not exist. | |
405 | */ | |
406 | static int read_oneliner(struct strbuf *buf, | |
407 | const char *path, int skip_if_empty) | |
408 | { | |
409 | int orig_len = buf->len; | |
410 | ||
411 | if (!file_exists(path)) | |
412 | return 0; | |
413 | ||
414 | if (strbuf_read_file(buf, path, 0) < 0) { | |
415 | warning_errno(_("could not read '%s'"), path); | |
416 | return 0; | |
417 | } | |
418 | ||
419 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { | |
420 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') | |
421 | --buf->len; | |
422 | buf->buf[buf->len] = '\0'; | |
423 | } | |
424 | ||
425 | if (skip_if_empty && buf->len == orig_len) | |
426 | return 0; | |
427 | ||
428 | return 1; | |
429 | } | |
430 | ||
043a4492 RR |
431 | static struct tree *empty_tree(void) |
432 | { | |
eb0ccfd7 | 433 | return lookup_tree(the_hash_algo->empty_tree); |
043a4492 RR |
434 | } |
435 | ||
436 | static int error_dirty_index(struct replay_opts *opts) | |
437 | { | |
438 | if (read_cache_unmerged()) | |
c28cbc5e | 439 | return error_resolve_conflict(_(action_name(opts))); |
043a4492 | 440 | |
93b3df6f | 441 | error(_("your local changes would be overwritten by %s."), |
c28cbc5e | 442 | _(action_name(opts))); |
043a4492 RR |
443 | |
444 | if (advice_commit_before_merge) | |
93b3df6f | 445 | advise(_("commit your changes or stash them to proceed.")); |
043a4492 RR |
446 | return -1; |
447 | } | |
448 | ||
1e41229d SB |
449 | static void update_abort_safety_file(void) |
450 | { | |
451 | struct object_id head; | |
452 | ||
453 | /* Do nothing on a single-pick */ | |
454 | if (!file_exists(git_path_seq_dir())) | |
455 | return; | |
456 | ||
457 | if (!get_oid("HEAD", &head)) | |
458 | write_file(git_path_abort_safety_file(), "%s", oid_to_hex(&head)); | |
459 | else | |
460 | write_file(git_path_abort_safety_file(), "%s", ""); | |
461 | } | |
462 | ||
ace976b2 | 463 | static int fast_forward_to(const struct object_id *to, const struct object_id *from, |
eb4be1cb | 464 | int unborn, struct replay_opts *opts) |
043a4492 | 465 | { |
d668d16c | 466 | struct ref_transaction *transaction; |
eb4be1cb | 467 | struct strbuf sb = STRBUF_INIT; |
d668d16c | 468 | struct strbuf err = STRBUF_INIT; |
043a4492 RR |
469 | |
470 | read_cache(); | |
db699a8a | 471 | if (checkout_fast_forward(from, to, 1)) |
0e408fc3 | 472 | return -1; /* the callee should have complained already */ |
651ab9f5 | 473 | |
c28cbc5e | 474 | strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts))); |
d668d16c RS |
475 | |
476 | transaction = ref_transaction_begin(&err); | |
477 | if (!transaction || | |
478 | ref_transaction_update(transaction, "HEAD", | |
d87d48b2 JS |
479 | to, unborn && !is_rebase_i(opts) ? |
480 | &null_oid : from, | |
1d147bdf | 481 | 0, sb.buf, &err) || |
db7516ab | 482 | ref_transaction_commit(transaction, &err)) { |
d668d16c RS |
483 | ref_transaction_free(transaction); |
484 | error("%s", err.buf); | |
485 | strbuf_release(&sb); | |
486 | strbuf_release(&err); | |
487 | return -1; | |
488 | } | |
651ab9f5 | 489 | |
eb4be1cb | 490 | strbuf_release(&sb); |
d668d16c RS |
491 | strbuf_release(&err); |
492 | ref_transaction_free(transaction); | |
1e41229d | 493 | update_abort_safety_file(); |
d668d16c | 494 | return 0; |
043a4492 RR |
495 | } |
496 | ||
75c961b7 JH |
497 | void append_conflicts_hint(struct strbuf *msgbuf) |
498 | { | |
499 | int i; | |
500 | ||
261f315b JH |
501 | strbuf_addch(msgbuf, '\n'); |
502 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); | |
75c961b7 JH |
503 | for (i = 0; i < active_nr;) { |
504 | const struct cache_entry *ce = active_cache[i++]; | |
505 | if (ce_stage(ce)) { | |
261f315b | 506 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
75c961b7 JH |
507 | while (i < active_nr && !strcmp(ce->name, |
508 | active_cache[i]->name)) | |
509 | i++; | |
510 | } | |
511 | } | |
512 | } | |
513 | ||
043a4492 RR |
514 | static int do_recursive_merge(struct commit *base, struct commit *next, |
515 | const char *base_label, const char *next_label, | |
48be4c62 | 516 | struct object_id *head, struct strbuf *msgbuf, |
043a4492 RR |
517 | struct replay_opts *opts) |
518 | { | |
519 | struct merge_options o; | |
520 | struct tree *result, *next_tree, *base_tree, *head_tree; | |
03b86647 | 521 | int clean; |
03a4e260 | 522 | char **xopt; |
14bca6c6 | 523 | struct lock_file index_lock = LOCK_INIT; |
043a4492 | 524 | |
bd588867 PW |
525 | if (hold_locked_index(&index_lock, LOCK_REPORT_ON_ERROR) < 0) |
526 | return -1; | |
043a4492 RR |
527 | |
528 | read_cache(); | |
529 | ||
530 | init_merge_options(&o); | |
531 | o.ancestor = base ? base_label : "(empty tree)"; | |
532 | o.branch1 = "HEAD"; | |
533 | o.branch2 = next ? next_label : "(empty tree)"; | |
62fdb652 JS |
534 | if (is_rebase_i(opts)) |
535 | o.buffer_output = 2; | |
9268cf4a | 536 | o.show_rename_progress = 1; |
043a4492 RR |
537 | |
538 | head_tree = parse_tree_indirect(head); | |
539 | next_tree = next ? next->tree : empty_tree(); | |
540 | base_tree = base ? base->tree : empty_tree(); | |
541 | ||
542 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) | |
543 | parse_merge_opt(&o, *xopt); | |
544 | ||
545 | clean = merge_trees(&o, | |
546 | head_tree, | |
547 | next_tree, base_tree, &result); | |
62fdb652 JS |
548 | if (is_rebase_i(opts) && clean <= 0) |
549 | fputs(o.obuf.buf, stdout); | |
548009c0 | 550 | strbuf_release(&o.obuf); |
b520abf1 | 551 | diff_warn_rename_limit("merge.renamelimit", o.needed_rename_limit, 0); |
64816524 MÅ |
552 | if (clean < 0) { |
553 | rollback_lock_file(&index_lock); | |
f241ff0d | 554 | return clean; |
64816524 | 555 | } |
043a4492 | 556 | |
61000814 MÅ |
557 | if (write_locked_index(&the_index, &index_lock, |
558 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) | |
66f5f6dc ÆAB |
559 | /* |
560 | * TRANSLATORS: %s will be "revert", "cherry-pick" or | |
84583957 JS |
561 | * "rebase -i". |
562 | */ | |
c527b55e | 563 | return error(_("%s: Unable to write new index file"), |
c28cbc5e | 564 | _(action_name(opts))); |
043a4492 | 565 | |
75c961b7 JH |
566 | if (!clean) |
567 | append_conflicts_hint(msgbuf); | |
043a4492 RR |
568 | |
569 | return !clean; | |
570 | } | |
571 | ||
ba97aea1 JS |
572 | static struct object_id *get_cache_tree_oid(void) |
573 | { | |
574 | if (!active_cache_tree) | |
575 | active_cache_tree = cache_tree(); | |
576 | ||
577 | if (!cache_tree_fully_valid(active_cache_tree)) | |
578 | if (cache_tree_update(&the_index, 0)) { | |
579 | error(_("unable to update cache tree")); | |
580 | return NULL; | |
581 | } | |
582 | ||
583 | return &active_cache_tree->oid; | |
584 | } | |
585 | ||
b27cfb0d NH |
586 | static int is_index_unchanged(void) |
587 | { | |
ba97aea1 | 588 | struct object_id head_oid, *cache_tree_oid; |
b27cfb0d NH |
589 | struct commit *head_commit; |
590 | ||
49e61479 | 591 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) |
aee42e1f | 592 | return error(_("could not resolve HEAD commit")); |
b27cfb0d | 593 | |
bc83266a | 594 | head_commit = lookup_commit(&head_oid); |
4b580061 NH |
595 | |
596 | /* | |
597 | * If head_commit is NULL, check_commit, called from | |
598 | * lookup_commit, would have indicated that head_commit is not | |
599 | * a commit object already. parse_commit() will return failure | |
600 | * without further complaints in such a case. Otherwise, if | |
601 | * the commit is invalid, parse_commit() will complain. So | |
602 | * there is nothing for us to say here. Just return failure. | |
603 | */ | |
604 | if (parse_commit(head_commit)) | |
605 | return -1; | |
b27cfb0d | 606 | |
ba97aea1 JS |
607 | if (!(cache_tree_oid = get_cache_tree_oid())) |
608 | return -1; | |
b27cfb0d | 609 | |
ba97aea1 | 610 | return !oidcmp(cache_tree_oid, &head_commit->tree->object.oid); |
b27cfb0d NH |
611 | } |
612 | ||
0473f28a JS |
613 | static int write_author_script(const char *message) |
614 | { | |
615 | struct strbuf buf = STRBUF_INIT; | |
616 | const char *eol; | |
617 | int res; | |
618 | ||
619 | for (;;) | |
620 | if (!*message || starts_with(message, "\n")) { | |
621 | missing_author: | |
622 | /* Missing 'author' line? */ | |
623 | unlink(rebase_path_author_script()); | |
624 | return 0; | |
625 | } else if (skip_prefix(message, "author ", &message)) | |
626 | break; | |
627 | else if ((eol = strchr(message, '\n'))) | |
628 | message = eol + 1; | |
629 | else | |
630 | goto missing_author; | |
631 | ||
632 | strbuf_addstr(&buf, "GIT_AUTHOR_NAME='"); | |
633 | while (*message && *message != '\n' && *message != '\r') | |
634 | if (skip_prefix(message, " <", &message)) | |
635 | break; | |
636 | else if (*message != '\'') | |
637 | strbuf_addch(&buf, *(message++)); | |
638 | else | |
639 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); | |
640 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_EMAIL='"); | |
641 | while (*message && *message != '\n' && *message != '\r') | |
642 | if (skip_prefix(message, "> ", &message)) | |
643 | break; | |
644 | else if (*message != '\'') | |
645 | strbuf_addch(&buf, *(message++)); | |
646 | else | |
647 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); | |
648 | strbuf_addstr(&buf, "'\nGIT_AUTHOR_DATE='@"); | |
649 | while (*message && *message != '\n' && *message != '\r') | |
650 | if (*message != '\'') | |
651 | strbuf_addch(&buf, *(message++)); | |
652 | else | |
653 | strbuf_addf(&buf, "'\\\\%c'", *(message++)); | |
654 | res = write_message(buf.buf, buf.len, rebase_path_author_script(), 1); | |
655 | strbuf_release(&buf); | |
656 | return res; | |
657 | } | |
658 | ||
b5a67045 | 659 | /* |
a2a20b0d JS |
660 | * Read a list of environment variable assignments (such as the author-script |
661 | * file) into an environment block. Returns -1 on error, 0 otherwise. | |
b5a67045 | 662 | */ |
a2a20b0d | 663 | static int read_env_script(struct argv_array *env) |
b5a67045 JS |
664 | { |
665 | struct strbuf script = STRBUF_INIT; | |
666 | int i, count = 0; | |
a2a20b0d | 667 | char *p, *p2; |
b5a67045 JS |
668 | |
669 | if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0) | |
a2a20b0d | 670 | return -1; |
b5a67045 JS |
671 | |
672 | for (p = script.buf; *p; p++) | |
673 | if (skip_prefix(p, "'\\\\''", (const char **)&p2)) | |
674 | strbuf_splice(&script, p - script.buf, p2 - p, "'", 1); | |
675 | else if (*p == '\'') | |
676 | strbuf_splice(&script, p-- - script.buf, 1, "", 0); | |
677 | else if (*p == '\n') { | |
678 | *p = '\0'; | |
679 | count++; | |
680 | } | |
681 | ||
a2a20b0d JS |
682 | for (i = 0, p = script.buf; i < count; i++) { |
683 | argv_array_push(env, p); | |
b5a67045 JS |
684 | p += strlen(p) + 1; |
685 | } | |
b5a67045 | 686 | |
a2a20b0d | 687 | return 0; |
b5a67045 JS |
688 | } |
689 | ||
356ee465 PW |
690 | static char *get_author(const char *message) |
691 | { | |
692 | size_t len; | |
693 | const char *a; | |
694 | ||
695 | a = find_commit_header(message, "author", &len); | |
696 | if (a) | |
697 | return xmemdupz(a, len); | |
698 | ||
699 | return NULL; | |
700 | } | |
701 | ||
d87d48b2 JS |
702 | /* Read author-script and return an ident line (author <email> timestamp) */ |
703 | static const char *read_author_ident(struct strbuf *buf) | |
704 | { | |
705 | const char *keys[] = { | |
706 | "GIT_AUTHOR_NAME=", "GIT_AUTHOR_EMAIL=", "GIT_AUTHOR_DATE=" | |
707 | }; | |
708 | char *in, *out, *eol; | |
709 | int i = 0, len; | |
710 | ||
711 | if (strbuf_read_file(buf, rebase_path_author_script(), 256) <= 0) | |
712 | return NULL; | |
713 | ||
714 | /* dequote values and construct ident line in-place */ | |
715 | for (in = out = buf->buf; i < 3 && in - buf->buf < buf->len; i++) { | |
716 | if (!skip_prefix(in, keys[i], (const char **)&in)) { | |
717 | warning("could not parse '%s' (looking for '%s'", | |
718 | rebase_path_author_script(), keys[i]); | |
719 | return NULL; | |
720 | } | |
721 | ||
722 | eol = strchrnul(in, '\n'); | |
723 | *eol = '\0'; | |
724 | sq_dequote(in); | |
725 | len = strlen(in); | |
726 | ||
727 | if (i > 0) /* separate values by spaces */ | |
728 | *(out++) = ' '; | |
729 | if (i == 1) /* email needs to be surrounded by <...> */ | |
730 | *(out++) = '<'; | |
731 | memmove(out, in, len); | |
732 | out += len; | |
733 | if (i == 1) /* email needs to be surrounded by <...> */ | |
734 | *(out++) = '>'; | |
735 | in = eol + 1; | |
736 | } | |
737 | ||
738 | if (i < 3) { | |
739 | warning("could not parse '%s' (looking for '%s')", | |
740 | rebase_path_author_script(), keys[i]); | |
741 | return NULL; | |
742 | } | |
743 | ||
744 | buf->len = out - buf->buf; | |
745 | return buf->buf; | |
746 | } | |
747 | ||
791eb870 JS |
748 | static const char staged_changes_advice[] = |
749 | N_("you have staged changes in your working tree\n" | |
750 | "If these changes are meant to be squashed into the previous commit, run:\n" | |
751 | "\n" | |
752 | " git commit --amend %s\n" | |
753 | "\n" | |
754 | "If they are meant to go into a new commit, run:\n" | |
755 | "\n" | |
756 | " git commit %s\n" | |
757 | "\n" | |
758 | "In both cases, once you're done, continue with:\n" | |
759 | "\n" | |
760 | " git rebase --continue\n"); | |
761 | ||
789b3eff JS |
762 | #define ALLOW_EMPTY (1<<0) |
763 | #define EDIT_MSG (1<<1) | |
764 | #define AMEND_MSG (1<<2) | |
765 | #define CLEANUP_MSG (1<<3) | |
b92ff6e8 | 766 | #define VERIFY_MSG (1<<4) |
d87d48b2 | 767 | #define CREATE_ROOT_COMMIT (1<<5) |
789b3eff | 768 | |
043a4492 RR |
769 | /* |
770 | * If we are cherry-pick, and if the merge did not result in | |
771 | * hand-editing, we will hit this commit and inherit the original | |
772 | * author date and name. | |
b5a67045 | 773 | * |
043a4492 RR |
774 | * If we are revert, or if our cherry-pick results in a hand merge, |
775 | * we had better say that the current user is responsible for that. | |
b5a67045 JS |
776 | * |
777 | * An exception is when run_git_commit() is called during an | |
778 | * interactive rebase: in that case, we will want to retain the | |
779 | * author metadata. | |
043a4492 | 780 | */ |
ac2b0e8f | 781 | static int run_git_commit(const char *defmsg, struct replay_opts *opts, |
789b3eff | 782 | unsigned int flags) |
043a4492 | 783 | { |
07d968ef | 784 | struct child_process cmd = CHILD_PROCESS_INIT; |
17d65f03 | 785 | const char *value; |
b27cfb0d | 786 | |
d87d48b2 JS |
787 | if (flags & CREATE_ROOT_COMMIT) { |
788 | struct strbuf msg = STRBUF_INIT, script = STRBUF_INIT; | |
789 | const char *author = is_rebase_i(opts) ? | |
790 | read_author_ident(&script) : NULL; | |
791 | struct object_id root_commit, *cache_tree_oid; | |
792 | int res = 0; | |
793 | ||
794 | if (!defmsg) | |
795 | BUG("root commit without message"); | |
796 | ||
797 | if (!(cache_tree_oid = get_cache_tree_oid())) | |
798 | res = -1; | |
799 | ||
800 | if (!res) | |
801 | res = strbuf_read_file(&msg, defmsg, 0); | |
802 | ||
803 | if (res <= 0) | |
804 | res = error_errno(_("could not read '%s'"), defmsg); | |
805 | else | |
806 | res = commit_tree(msg.buf, msg.len, cache_tree_oid, | |
807 | NULL, &root_commit, author, | |
808 | opts->gpg_sign); | |
809 | ||
810 | strbuf_release(&msg); | |
811 | strbuf_release(&script); | |
812 | if (!res) { | |
813 | update_ref(NULL, "CHERRY_PICK_HEAD", &root_commit, NULL, | |
814 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR); | |
815 | res = update_ref(NULL, "HEAD", &root_commit, NULL, 0, | |
816 | UPDATE_REFS_MSG_ON_ERR); | |
817 | } | |
818 | return res < 0 ? error(_("writing root commit")) : 0; | |
819 | } | |
820 | ||
07d968ef JS |
821 | cmd.git_cmd = 1; |
822 | ||
b5a67045 | 823 | if (is_rebase_i(opts)) { |
789b3eff | 824 | if (!(flags & EDIT_MSG)) { |
9a757c49 JS |
825 | cmd.stdout_to_stderr = 1; |
826 | cmd.err = -1; | |
827 | } | |
828 | ||
07d968ef | 829 | if (read_env_script(&cmd.env_array)) { |
a1c75762 JS |
830 | const char *gpg_opt = gpg_sign_opt_quoted(opts); |
831 | ||
791eb870 JS |
832 | return error(_(staged_changes_advice), |
833 | gpg_opt, gpg_opt); | |
a1c75762 | 834 | } |
b5a67045 JS |
835 | } |
836 | ||
07d968ef | 837 | argv_array_push(&cmd.args, "commit"); |
043a4492 | 838 | |
b92ff6e8 JS |
839 | if (!(flags & VERIFY_MSG)) |
840 | argv_array_push(&cmd.args, "-n"); | |
789b3eff | 841 | if ((flags & AMEND_MSG)) |
07d968ef | 842 | argv_array_push(&cmd.args, "--amend"); |
3bdd5522 | 843 | if (opts->gpg_sign) |
07d968ef | 844 | argv_array_pushf(&cmd.args, "-S%s", opts->gpg_sign); |
b5a67045 | 845 | if (defmsg) |
07d968ef | 846 | argv_array_pushl(&cmd.args, "-F", defmsg, NULL); |
789b3eff | 847 | if ((flags & CLEANUP_MSG)) |
07d968ef | 848 | argv_array_push(&cmd.args, "--cleanup=strip"); |
789b3eff | 849 | if ((flags & EDIT_MSG)) |
07d968ef | 850 | argv_array_push(&cmd.args, "-e"); |
789b3eff | 851 | else if (!(flags & CLEANUP_MSG) && |
0009426d | 852 | !opts->signoff && !opts->record_origin && |
b5a67045 | 853 | git_config_get_value("commit.cleanup", &value)) |
07d968ef | 854 | argv_array_push(&cmd.args, "--cleanup=verbatim"); |
b27cfb0d | 855 | |
789b3eff | 856 | if ((flags & ALLOW_EMPTY)) |
07d968ef | 857 | argv_array_push(&cmd.args, "--allow-empty"); |
df478b74 | 858 | |
4bee9584 | 859 | if (opts->allow_empty_message) |
07d968ef | 860 | argv_array_push(&cmd.args, "--allow-empty-message"); |
4bee9584 | 861 | |
9a757c49 JS |
862 | if (cmd.err == -1) { |
863 | /* hide stderr on success */ | |
864 | struct strbuf buf = STRBUF_INIT; | |
865 | int rc = pipe_command(&cmd, | |
866 | NULL, 0, | |
867 | /* stdout is already redirected */ | |
868 | NULL, 0, | |
869 | &buf, 0); | |
870 | if (rc) | |
871 | fputs(buf.buf, stderr); | |
872 | strbuf_release(&buf); | |
873 | return rc; | |
874 | } | |
b5a67045 | 875 | |
07d968ef | 876 | return run_command(&cmd); |
b27cfb0d NH |
877 | } |
878 | ||
d0aaa46f PW |
879 | static int rest_is_empty(const struct strbuf *sb, int start) |
880 | { | |
881 | int i, eol; | |
882 | const char *nl; | |
883 | ||
884 | /* Check if the rest is just whitespace and Signed-off-by's. */ | |
885 | for (i = start; i < sb->len; i++) { | |
886 | nl = memchr(sb->buf + i, '\n', sb->len - i); | |
887 | if (nl) | |
888 | eol = nl - sb->buf; | |
889 | else | |
890 | eol = sb->len; | |
891 | ||
892 | if (strlen(sign_off_header) <= eol - i && | |
893 | starts_with(sb->buf + i, sign_off_header)) { | |
894 | i = eol; | |
895 | continue; | |
896 | } | |
897 | while (i < eol) | |
898 | if (!isspace(sb->buf[i++])) | |
899 | return 0; | |
900 | } | |
901 | ||
902 | return 1; | |
903 | } | |
904 | ||
905 | /* | |
906 | * Find out if the message in the strbuf contains only whitespace and | |
907 | * Signed-off-by lines. | |
908 | */ | |
909 | int message_is_empty(const struct strbuf *sb, | |
910 | enum commit_msg_cleanup_mode cleanup_mode) | |
911 | { | |
912 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) | |
913 | return 0; | |
914 | return rest_is_empty(sb, 0); | |
915 | } | |
916 | ||
917 | /* | |
918 | * See if the user edited the message in the editor or left what | |
919 | * was in the template intact | |
920 | */ | |
921 | int template_untouched(const struct strbuf *sb, const char *template_file, | |
922 | enum commit_msg_cleanup_mode cleanup_mode) | |
923 | { | |
924 | struct strbuf tmpl = STRBUF_INIT; | |
925 | const char *start; | |
926 | ||
927 | if (cleanup_mode == COMMIT_MSG_CLEANUP_NONE && sb->len) | |
928 | return 0; | |
929 | ||
930 | if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0) | |
931 | return 0; | |
932 | ||
933 | strbuf_stripspace(&tmpl, cleanup_mode == COMMIT_MSG_CLEANUP_ALL); | |
934 | if (!skip_prefix(sb->buf, tmpl.buf, &start)) | |
935 | start = sb->buf; | |
936 | strbuf_release(&tmpl); | |
937 | return rest_is_empty(sb, start - sb->buf); | |
938 | } | |
939 | ||
0505d604 PW |
940 | int update_head_with_reflog(const struct commit *old_head, |
941 | const struct object_id *new_head, | |
942 | const char *action, const struct strbuf *msg, | |
943 | struct strbuf *err) | |
944 | { | |
945 | struct ref_transaction *transaction; | |
946 | struct strbuf sb = STRBUF_INIT; | |
947 | const char *nl; | |
948 | int ret = 0; | |
949 | ||
950 | if (action) { | |
951 | strbuf_addstr(&sb, action); | |
952 | strbuf_addstr(&sb, ": "); | |
953 | } | |
954 | ||
955 | nl = strchr(msg->buf, '\n'); | |
956 | if (nl) { | |
957 | strbuf_add(&sb, msg->buf, nl + 1 - msg->buf); | |
958 | } else { | |
959 | strbuf_addbuf(&sb, msg); | |
960 | strbuf_addch(&sb, '\n'); | |
961 | } | |
962 | ||
963 | transaction = ref_transaction_begin(err); | |
964 | if (!transaction || | |
965 | ref_transaction_update(transaction, "HEAD", new_head, | |
966 | old_head ? &old_head->object.oid : &null_oid, | |
967 | 0, sb.buf, err) || | |
968 | ref_transaction_commit(transaction, err)) { | |
969 | ret = -1; | |
970 | } | |
971 | ref_transaction_free(transaction); | |
972 | strbuf_release(&sb); | |
973 | ||
974 | return ret; | |
975 | } | |
976 | ||
a87a6f3c PW |
977 | static int run_rewrite_hook(const struct object_id *oldoid, |
978 | const struct object_id *newoid) | |
979 | { | |
980 | struct child_process proc = CHILD_PROCESS_INIT; | |
981 | const char *argv[3]; | |
982 | int code; | |
983 | struct strbuf sb = STRBUF_INIT; | |
984 | ||
985 | argv[0] = find_hook("post-rewrite"); | |
986 | if (!argv[0]) | |
987 | return 0; | |
988 | ||
989 | argv[1] = "amend"; | |
990 | argv[2] = NULL; | |
991 | ||
992 | proc.argv = argv; | |
993 | proc.in = -1; | |
994 | proc.stdout_to_stderr = 1; | |
995 | ||
996 | code = start_command(&proc); | |
997 | if (code) | |
998 | return code; | |
999 | strbuf_addf(&sb, "%s %s\n", oid_to_hex(oldoid), oid_to_hex(newoid)); | |
1000 | sigchain_push(SIGPIPE, SIG_IGN); | |
1001 | write_in_full(proc.in, sb.buf, sb.len); | |
1002 | close(proc.in); | |
1003 | strbuf_release(&sb); | |
1004 | sigchain_pop(SIGPIPE); | |
1005 | return finish_command(&proc); | |
1006 | } | |
1007 | ||
1008 | void commit_post_rewrite(const struct commit *old_head, | |
1009 | const struct object_id *new_head) | |
1010 | { | |
1011 | struct notes_rewrite_cfg *cfg; | |
1012 | ||
1013 | cfg = init_copy_notes_for_rewrite("amend"); | |
1014 | if (cfg) { | |
1015 | /* we are amending, so old_head is not NULL */ | |
1016 | copy_note_for_rewrite(cfg, &old_head->object.oid, new_head); | |
1017 | finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'"); | |
1018 | } | |
1019 | run_rewrite_hook(&old_head->object.oid, new_head); | |
1020 | } | |
1021 | ||
66618a50 PW |
1022 | static int run_prepare_commit_msg_hook(struct strbuf *msg, const char *commit) |
1023 | { | |
1024 | struct argv_array hook_env = ARGV_ARRAY_INIT; | |
1025 | int ret; | |
1026 | const char *name; | |
1027 | ||
1028 | name = git_path_commit_editmsg(); | |
1029 | if (write_message(msg->buf, msg->len, name, 0)) | |
1030 | return -1; | |
1031 | ||
1032 | argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", get_index_file()); | |
1033 | argv_array_push(&hook_env, "GIT_EDITOR=:"); | |
1034 | if (commit) | |
1035 | ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name, | |
1036 | "commit", commit, NULL); | |
1037 | else | |
1038 | ret = run_hook_le(hook_env.argv, "prepare-commit-msg", name, | |
1039 | "message", NULL); | |
1040 | if (ret) | |
1041 | ret = error(_("'prepare-commit-msg' hook failed")); | |
1042 | argv_array_clear(&hook_env); | |
1043 | ||
1044 | return ret; | |
1045 | } | |
1046 | ||
e47c6caf PW |
1047 | static const char implicit_ident_advice_noconfig[] = |
1048 | N_("Your name and email address were configured automatically based\n" | |
1049 | "on your username and hostname. Please check that they are accurate.\n" | |
1050 | "You can suppress this message by setting them explicitly. Run the\n" | |
1051 | "following command and follow the instructions in your editor to edit\n" | |
1052 | "your configuration file:\n" | |
1053 | "\n" | |
1054 | " git config --global --edit\n" | |
1055 | "\n" | |
1056 | "After doing this, you may fix the identity used for this commit with:\n" | |
1057 | "\n" | |
1058 | " git commit --amend --reset-author\n"); | |
1059 | ||
1060 | static const char implicit_ident_advice_config[] = | |
1061 | N_("Your name and email address were configured automatically based\n" | |
1062 | "on your username and hostname. Please check that they are accurate.\n" | |
1063 | "You can suppress this message by setting them explicitly:\n" | |
1064 | "\n" | |
1065 | " git config --global user.name \"Your Name\"\n" | |
1066 | " git config --global user.email you@example.com\n" | |
1067 | "\n" | |
1068 | "After doing this, you may fix the identity used for this commit with:\n" | |
1069 | "\n" | |
1070 | " git commit --amend --reset-author\n"); | |
1071 | ||
1072 | static const char *implicit_ident_advice(void) | |
1073 | { | |
1074 | char *user_config = expand_user_path("~/.gitconfig", 0); | |
1075 | char *xdg_config = xdg_config_home("config"); | |
1076 | int config_exists = file_exists(user_config) || file_exists(xdg_config); | |
1077 | ||
1078 | free(user_config); | |
1079 | free(xdg_config); | |
1080 | ||
1081 | if (config_exists) | |
1082 | return _(implicit_ident_advice_config); | |
1083 | else | |
1084 | return _(implicit_ident_advice_noconfig); | |
1085 | ||
1086 | } | |
1087 | ||
1088 | void print_commit_summary(const char *prefix, const struct object_id *oid, | |
1089 | unsigned int flags) | |
1090 | { | |
1091 | struct rev_info rev; | |
1092 | struct commit *commit; | |
1093 | struct strbuf format = STRBUF_INIT; | |
1094 | const char *head; | |
1095 | struct pretty_print_context pctx = {0}; | |
1096 | struct strbuf author_ident = STRBUF_INIT; | |
1097 | struct strbuf committer_ident = STRBUF_INIT; | |
1098 | ||
1099 | commit = lookup_commit(oid); | |
1100 | if (!commit) | |
1101 | die(_("couldn't look up newly created commit")); | |
1102 | if (parse_commit(commit)) | |
1103 | die(_("could not parse newly created commit")); | |
1104 | ||
1105 | strbuf_addstr(&format, "format:%h] %s"); | |
1106 | ||
1107 | format_commit_message(commit, "%an <%ae>", &author_ident, &pctx); | |
1108 | format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx); | |
1109 | if (strbuf_cmp(&author_ident, &committer_ident)) { | |
1110 | strbuf_addstr(&format, "\n Author: "); | |
1111 | strbuf_addbuf_percentquote(&format, &author_ident); | |
1112 | } | |
1113 | if (flags & SUMMARY_SHOW_AUTHOR_DATE) { | |
1114 | struct strbuf date = STRBUF_INIT; | |
1115 | ||
1116 | format_commit_message(commit, "%ad", &date, &pctx); | |
1117 | strbuf_addstr(&format, "\n Date: "); | |
1118 | strbuf_addbuf_percentquote(&format, &date); | |
1119 | strbuf_release(&date); | |
1120 | } | |
1121 | if (!committer_ident_sufficiently_given()) { | |
1122 | strbuf_addstr(&format, "\n Committer: "); | |
1123 | strbuf_addbuf_percentquote(&format, &committer_ident); | |
1124 | if (advice_implicit_identity) { | |
1125 | strbuf_addch(&format, '\n'); | |
1126 | strbuf_addstr(&format, implicit_ident_advice()); | |
1127 | } | |
1128 | } | |
1129 | strbuf_release(&author_ident); | |
1130 | strbuf_release(&committer_ident); | |
1131 | ||
1132 | init_revisions(&rev, prefix); | |
1133 | setup_revisions(0, NULL, &rev, NULL); | |
1134 | ||
1135 | rev.diff = 1; | |
1136 | rev.diffopt.output_format = | |
1137 | DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY; | |
1138 | ||
1139 | rev.verbose_header = 1; | |
1140 | rev.show_root_diff = 1; | |
1141 | get_commit_format(format.buf, &rev); | |
1142 | rev.always_show_header = 0; | |
0f57f731 | 1143 | rev.diffopt.detect_rename = DIFF_DETECT_RENAME; |
e47c6caf PW |
1144 | rev.diffopt.break_opt = 0; |
1145 | diff_setup_done(&rev.diffopt); | |
1146 | ||
1147 | head = resolve_ref_unsafe("HEAD", 0, NULL, NULL); | |
1148 | if (!head) | |
1149 | die_errno(_("unable to resolve HEAD after creating commit")); | |
1150 | if (!strcmp(head, "HEAD")) | |
1151 | head = _("detached HEAD"); | |
1152 | else | |
1153 | skip_prefix(head, "refs/heads/", &head); | |
1154 | printf("[%s%s ", head, (flags & SUMMARY_INITIAL_COMMIT) ? | |
1155 | _(" (root-commit)") : ""); | |
1156 | ||
1157 | if (!log_tree_commit(&rev, commit)) { | |
1158 | rev.always_show_header = 1; | |
1159 | rev.use_terminator = 1; | |
1160 | log_tree_commit(&rev, commit); | |
1161 | } | |
1162 | ||
1163 | strbuf_release(&format); | |
1164 | } | |
1165 | ||
356ee465 PW |
1166 | static int parse_head(struct commit **head) |
1167 | { | |
1168 | struct commit *current_head; | |
1169 | struct object_id oid; | |
1170 | ||
1171 | if (get_oid("HEAD", &oid)) { | |
1172 | current_head = NULL; | |
1173 | } else { | |
1174 | current_head = lookup_commit_reference(&oid); | |
1175 | if (!current_head) | |
1176 | return error(_("could not parse HEAD")); | |
1177 | if (oidcmp(&oid, ¤t_head->object.oid)) { | |
1178 | warning(_("HEAD %s is not a commit!"), | |
1179 | oid_to_hex(&oid)); | |
1180 | } | |
1181 | if (parse_commit(current_head)) | |
1182 | return error(_("could not parse HEAD commit")); | |
1183 | } | |
1184 | *head = current_head; | |
1185 | ||
1186 | return 0; | |
1187 | } | |
1188 | ||
1189 | /* | |
1190 | * Try to commit without forking 'git commit'. In some cases we need | |
1191 | * to run 'git commit' to display an error message | |
1192 | * | |
1193 | * Returns: | |
1194 | * -1 - error unable to commit | |
1195 | * 0 - success | |
1196 | * 1 - run 'git commit' | |
1197 | */ | |
1198 | static int try_to_commit(struct strbuf *msg, const char *author, | |
1199 | struct replay_opts *opts, unsigned int flags, | |
1200 | struct object_id *oid) | |
1201 | { | |
1202 | struct object_id tree; | |
1203 | struct commit *current_head; | |
1204 | struct commit_list *parents = NULL; | |
1205 | struct commit_extra_header *extra = NULL; | |
1206 | struct strbuf err = STRBUF_INIT; | |
66618a50 | 1207 | struct strbuf commit_msg = STRBUF_INIT; |
356ee465 | 1208 | char *amend_author = NULL; |
66618a50 | 1209 | const char *hook_commit = NULL; |
356ee465 PW |
1210 | enum commit_msg_cleanup_mode cleanup; |
1211 | int res = 0; | |
1212 | ||
1213 | if (parse_head(¤t_head)) | |
1214 | return -1; | |
1215 | ||
1216 | if (flags & AMEND_MSG) { | |
1217 | const char *exclude_gpgsig[] = { "gpgsig", NULL }; | |
1218 | const char *out_enc = get_commit_output_encoding(); | |
1219 | const char *message = logmsg_reencode(current_head, NULL, | |
1220 | out_enc); | |
1221 | ||
1222 | if (!msg) { | |
1223 | const char *orig_message = NULL; | |
1224 | ||
1225 | find_commit_subject(message, &orig_message); | |
66618a50 | 1226 | msg = &commit_msg; |
356ee465 | 1227 | strbuf_addstr(msg, orig_message); |
66618a50 | 1228 | hook_commit = "HEAD"; |
356ee465 PW |
1229 | } |
1230 | author = amend_author = get_author(message); | |
1231 | unuse_commit_buffer(current_head, message); | |
1232 | if (!author) { | |
1233 | res = error(_("unable to parse commit author")); | |
1234 | goto out; | |
1235 | } | |
1236 | parents = copy_commit_list(current_head->parents); | |
1237 | extra = read_commit_extra_headers(current_head, exclude_gpgsig); | |
1238 | } else if (current_head) { | |
1239 | commit_list_insert(current_head, &parents); | |
1240 | } | |
1241 | ||
fc5cb99f | 1242 | if (write_cache_as_tree(&tree, 0, NULL)) { |
356ee465 PW |
1243 | res = error(_("git write-tree failed to write a tree")); |
1244 | goto out; | |
1245 | } | |
1246 | ||
1247 | if (!(flags & ALLOW_EMPTY) && !oidcmp(current_head ? | |
1248 | ¤t_head->tree->object.oid : | |
1249 | &empty_tree_oid, &tree)) { | |
1250 | res = 1; /* run 'git commit' to display error message */ | |
1251 | goto out; | |
1252 | } | |
1253 | ||
66618a50 PW |
1254 | if (find_hook("prepare-commit-msg")) { |
1255 | res = run_prepare_commit_msg_hook(msg, hook_commit); | |
1256 | if (res) | |
1257 | goto out; | |
1258 | if (strbuf_read_file(&commit_msg, git_path_commit_editmsg(), | |
1259 | 2048) < 0) { | |
1260 | res = error_errno(_("unable to read commit message " | |
1261 | "from '%s'"), | |
1262 | git_path_commit_editmsg()); | |
1263 | goto out; | |
1264 | } | |
1265 | msg = &commit_msg; | |
1266 | } | |
1267 | ||
1268 | cleanup = (flags & CLEANUP_MSG) ? COMMIT_MSG_CLEANUP_ALL : | |
1269 | opts->default_msg_cleanup; | |
1270 | ||
1271 | if (cleanup != COMMIT_MSG_CLEANUP_NONE) | |
1272 | strbuf_stripspace(msg, cleanup == COMMIT_MSG_CLEANUP_ALL); | |
1273 | if (!opts->allow_empty_message && message_is_empty(msg, cleanup)) { | |
1274 | res = 1; /* run 'git commit' to display error message */ | |
1275 | goto out; | |
1276 | } | |
1277 | ||
8be8342b JH |
1278 | if (commit_tree_extended(msg->buf, msg->len, &tree, parents, |
1279 | oid, author, opts->gpg_sign, extra)) { | |
356ee465 PW |
1280 | res = error(_("failed to write commit object")); |
1281 | goto out; | |
1282 | } | |
1283 | ||
1284 | if (update_head_with_reflog(current_head, oid, | |
1285 | getenv("GIT_REFLOG_ACTION"), msg, &err)) { | |
1286 | res = error("%s", err.buf); | |
1287 | goto out; | |
1288 | } | |
1289 | ||
1290 | if (flags & AMEND_MSG) | |
1291 | commit_post_rewrite(current_head, oid); | |
1292 | ||
1293 | out: | |
1294 | free_commit_extra_headers(extra); | |
1295 | strbuf_release(&err); | |
66618a50 | 1296 | strbuf_release(&commit_msg); |
356ee465 PW |
1297 | free(amend_author); |
1298 | ||
1299 | return res; | |
1300 | } | |
1301 | ||
1302 | static int do_commit(const char *msg_file, const char *author, | |
1303 | struct replay_opts *opts, unsigned int flags) | |
1304 | { | |
1305 | int res = 1; | |
1306 | ||
d87d48b2 JS |
1307 | if (!(flags & EDIT_MSG) && !(flags & VERIFY_MSG) && |
1308 | !(flags & CREATE_ROOT_COMMIT)) { | |
356ee465 PW |
1309 | struct object_id oid; |
1310 | struct strbuf sb = STRBUF_INIT; | |
1311 | ||
1312 | if (msg_file && strbuf_read_file(&sb, msg_file, 2048) < 0) | |
1313 | return error_errno(_("unable to read commit message " | |
1314 | "from '%s'"), | |
1315 | msg_file); | |
1316 | ||
1317 | res = try_to_commit(msg_file ? &sb : NULL, author, opts, flags, | |
1318 | &oid); | |
1319 | strbuf_release(&sb); | |
1320 | if (!res) { | |
1321 | unlink(git_path_cherry_pick_head()); | |
1322 | unlink(git_path_merge_msg()); | |
1323 | if (!is_rebase_i(opts)) | |
1324 | print_commit_summary(NULL, &oid, | |
1325 | SUMMARY_SHOW_AUTHOR_DATE); | |
1326 | return res; | |
1327 | } | |
1328 | } | |
1329 | if (res == 1) | |
1330 | return run_git_commit(msg_file, opts, flags); | |
1331 | ||
1332 | return res; | |
1333 | } | |
1334 | ||
b27cfb0d NH |
1335 | static int is_original_commit_empty(struct commit *commit) |
1336 | { | |
092bbcdf | 1337 | const struct object_id *ptree_oid; |
b27cfb0d NH |
1338 | |
1339 | if (parse_commit(commit)) | |
aee42e1f | 1340 | return error(_("could not parse commit %s"), |
f2fd0760 | 1341 | oid_to_hex(&commit->object.oid)); |
b27cfb0d NH |
1342 | if (commit->parents) { |
1343 | struct commit *parent = commit->parents->item; | |
1344 | if (parse_commit(parent)) | |
aee42e1f | 1345 | return error(_("could not parse parent commit %s"), |
f2fd0760 | 1346 | oid_to_hex(&parent->object.oid)); |
092bbcdf | 1347 | ptree_oid = &parent->tree->object.oid; |
b27cfb0d | 1348 | } else { |
eb0ccfd7 | 1349 | ptree_oid = the_hash_algo->empty_tree; /* commit is root */ |
043a4492 | 1350 | } |
043a4492 | 1351 | |
092bbcdf | 1352 | return !oidcmp(ptree_oid, &commit->tree->object.oid); |
043a4492 RR |
1353 | } |
1354 | ||
ac2b0e8f JH |
1355 | /* |
1356 | * Do we run "git commit" with "--allow-empty"? | |
1357 | */ | |
1358 | static int allow_empty(struct replay_opts *opts, struct commit *commit) | |
1359 | { | |
1360 | int index_unchanged, empty_commit; | |
1361 | ||
1362 | /* | |
1363 | * Three cases: | |
1364 | * | |
1365 | * (1) we do not allow empty at all and error out. | |
1366 | * | |
1367 | * (2) we allow ones that were initially empty, but | |
1368 | * forbid the ones that become empty; | |
1369 | * | |
1370 | * (3) we allow both. | |
1371 | */ | |
1372 | if (!opts->allow_empty) | |
1373 | return 0; /* let "git commit" barf as necessary */ | |
1374 | ||
1375 | index_unchanged = is_index_unchanged(); | |
1376 | if (index_unchanged < 0) | |
1377 | return index_unchanged; | |
1378 | if (!index_unchanged) | |
1379 | return 0; /* we do not have to say --allow-empty */ | |
1380 | ||
1381 | if (opts->keep_redundant_commits) | |
1382 | return 1; | |
1383 | ||
1384 | empty_commit = is_original_commit_empty(commit); | |
1385 | if (empty_commit < 0) | |
1386 | return empty_commit; | |
1387 | if (!empty_commit) | |
1388 | return 0; | |
1389 | else | |
1390 | return 1; | |
1391 | } | |
1392 | ||
25c43667 JS |
1393 | /* |
1394 | * Note that ordering matters in this enum. Not only must it match the mapping | |
1395 | * below, it is also divided into several sections that matter. When adding | |
1396 | * new commands, make sure you add it in the right section. | |
1397 | */ | |
004fefa7 | 1398 | enum todo_command { |
25c43667 | 1399 | /* commands that handle commits */ |
004fefa7 | 1400 | TODO_PICK = 0, |
25c43667 | 1401 | TODO_REVERT, |
56dc3ab0 | 1402 | TODO_EDIT, |
04efc8b5 | 1403 | TODO_REWORD, |
6e98de72 JS |
1404 | TODO_FIXUP, |
1405 | TODO_SQUASH, | |
311af526 JS |
1406 | /* commands that do something else than handling a single commit */ |
1407 | TODO_EXEC, | |
9055e401 JS |
1408 | TODO_LABEL, |
1409 | TODO_RESET, | |
4c68e7dd | 1410 | TODO_MERGE, |
25c43667 | 1411 | /* commands that do nothing but are counted for reporting progress */ |
b3fdd581 | 1412 | TODO_NOOP, |
ac191470 JS |
1413 | TODO_DROP, |
1414 | /* comments (not counted for reporting progress) */ | |
1415 | TODO_COMMENT | |
004fefa7 JS |
1416 | }; |
1417 | ||
414697a9 JS |
1418 | static struct { |
1419 | char c; | |
1420 | const char *str; | |
1421 | } todo_command_info[] = { | |
1422 | { 'p', "pick" }, | |
1423 | { 0, "revert" }, | |
1424 | { 'e', "edit" }, | |
04efc8b5 | 1425 | { 'r', "reword" }, |
414697a9 JS |
1426 | { 'f', "fixup" }, |
1427 | { 's', "squash" }, | |
1428 | { 'x', "exec" }, | |
9055e401 JS |
1429 | { 'l', "label" }, |
1430 | { 't', "reset" }, | |
4c68e7dd | 1431 | { 'm', "merge" }, |
b3fdd581 | 1432 | { 0, "noop" }, |
ac191470 JS |
1433 | { 'd', "drop" }, |
1434 | { 0, NULL } | |
004fefa7 JS |
1435 | }; |
1436 | ||
1437 | static const char *command_to_string(const enum todo_command command) | |
1438 | { | |
ac191470 | 1439 | if (command < TODO_COMMENT) |
414697a9 | 1440 | return todo_command_info[command].str; |
004fefa7 JS |
1441 | die("Unknown command: %d", command); |
1442 | } | |
1443 | ||
ee5462d6 | 1444 | static char command_to_char(const enum todo_command command) |
d8ae6c84 LB |
1445 | { |
1446 | if (command < TODO_COMMENT && todo_command_info[command].c) | |
1447 | return todo_command_info[command].c; | |
1448 | return comment_line_char; | |
1449 | } | |
1450 | ||
25c43667 JS |
1451 | static int is_noop(const enum todo_command command) |
1452 | { | |
b3fdd581 | 1453 | return TODO_NOOP <= command; |
25c43667 | 1454 | } |
004fefa7 | 1455 | |
6e98de72 JS |
1456 | static int is_fixup(enum todo_command command) |
1457 | { | |
1458 | return command == TODO_FIXUP || command == TODO_SQUASH; | |
1459 | } | |
1460 | ||
d87d48b2 JS |
1461 | /* Does this command create a (non-merge) commit? */ |
1462 | static int is_pick_or_similar(enum todo_command command) | |
1463 | { | |
1464 | switch (command) { | |
1465 | case TODO_PICK: | |
1466 | case TODO_REVERT: | |
1467 | case TODO_EDIT: | |
1468 | case TODO_REWORD: | |
1469 | case TODO_FIXUP: | |
1470 | case TODO_SQUASH: | |
1471 | return 1; | |
1472 | default: | |
1473 | return 0; | |
1474 | } | |
1475 | } | |
1476 | ||
6e98de72 JS |
1477 | static int update_squash_messages(enum todo_command command, |
1478 | struct commit *commit, struct replay_opts *opts) | |
1479 | { | |
1480 | struct strbuf buf = STRBUF_INIT; | |
1481 | int count, res; | |
1482 | const char *message, *body; | |
1483 | ||
1484 | if (file_exists(rebase_path_squash_msg())) { | |
1485 | struct strbuf header = STRBUF_INIT; | |
1486 | char *eol, *p; | |
1487 | ||
1488 | if (strbuf_read_file(&buf, rebase_path_squash_msg(), 2048) <= 0) | |
1489 | return error(_("could not read '%s'"), | |
1490 | rebase_path_squash_msg()); | |
1491 | ||
1492 | p = buf.buf + 1; | |
1493 | eol = strchrnul(buf.buf, '\n'); | |
1494 | if (buf.buf[0] != comment_line_char || | |
1495 | (p += strcspn(p, "0123456789\n")) == eol) | |
1496 | return error(_("unexpected 1st line of squash message:" | |
1497 | "\n\n\t%.*s"), | |
1498 | (int)(eol - buf.buf), buf.buf); | |
1499 | count = strtol(p, NULL, 10); | |
1500 | ||
1501 | if (count < 1) | |
1502 | return error(_("invalid 1st line of squash message:\n" | |
1503 | "\n\t%.*s"), | |
1504 | (int)(eol - buf.buf), buf.buf); | |
1505 | ||
1506 | strbuf_addf(&header, "%c ", comment_line_char); | |
1507 | strbuf_addf(&header, | |
1508 | _("This is a combination of %d commits."), ++count); | |
1509 | strbuf_splice(&buf, 0, eol - buf.buf, header.buf, header.len); | |
1510 | strbuf_release(&header); | |
1511 | } else { | |
33d66df3 | 1512 | struct object_id head; |
6e98de72 JS |
1513 | struct commit *head_commit; |
1514 | const char *head_message, *body; | |
1515 | ||
33d66df3 | 1516 | if (get_oid("HEAD", &head)) |
6e98de72 | 1517 | return error(_("need a HEAD to fixup")); |
bc83266a | 1518 | if (!(head_commit = lookup_commit_reference(&head))) |
6e98de72 JS |
1519 | return error(_("could not read HEAD")); |
1520 | if (!(head_message = get_commit_buffer(head_commit, NULL))) | |
1521 | return error(_("could not read HEAD's commit message")); | |
1522 | ||
1523 | find_commit_subject(head_message, &body); | |
1524 | if (write_message(body, strlen(body), | |
1525 | rebase_path_fixup_msg(), 0)) { | |
1526 | unuse_commit_buffer(head_commit, head_message); | |
1527 | return error(_("cannot write '%s'"), | |
1528 | rebase_path_fixup_msg()); | |
1529 | } | |
1530 | ||
1531 | count = 2; | |
1532 | strbuf_addf(&buf, "%c ", comment_line_char); | |
1533 | strbuf_addf(&buf, _("This is a combination of %d commits."), | |
1534 | count); | |
1535 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
1536 | strbuf_addstr(&buf, _("This is the 1st commit message:")); | |
1537 | strbuf_addstr(&buf, "\n\n"); | |
1538 | strbuf_addstr(&buf, body); | |
1539 | ||
1540 | unuse_commit_buffer(head_commit, head_message); | |
1541 | } | |
1542 | ||
1543 | if (!(message = get_commit_buffer(commit, NULL))) | |
1544 | return error(_("could not read commit message of %s"), | |
1545 | oid_to_hex(&commit->object.oid)); | |
1546 | find_commit_subject(message, &body); | |
1547 | ||
1548 | if (command == TODO_SQUASH) { | |
1549 | unlink(rebase_path_fixup_msg()); | |
1550 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
1551 | strbuf_addf(&buf, _("This is the commit message #%d:"), count); | |
1552 | strbuf_addstr(&buf, "\n\n"); | |
1553 | strbuf_addstr(&buf, body); | |
1554 | } else if (command == TODO_FIXUP) { | |
1555 | strbuf_addf(&buf, "\n%c ", comment_line_char); | |
1556 | strbuf_addf(&buf, _("The commit message #%d will be skipped:"), | |
1557 | count); | |
1558 | strbuf_addstr(&buf, "\n\n"); | |
1559 | strbuf_add_commented_lines(&buf, body, strlen(body)); | |
1560 | } else | |
1561 | return error(_("unknown command: %d"), command); | |
1562 | unuse_commit_buffer(commit, message); | |
1563 | ||
1564 | res = write_message(buf.buf, buf.len, rebase_path_squash_msg(), 0); | |
1565 | strbuf_release(&buf); | |
1566 | return res; | |
1567 | } | |
1568 | ||
25cb8df9 JS |
1569 | static void flush_rewritten_pending(void) { |
1570 | struct strbuf buf = STRBUF_INIT; | |
092bbcdf | 1571 | struct object_id newoid; |
25cb8df9 JS |
1572 | FILE *out; |
1573 | ||
092bbcdf | 1574 | if (strbuf_read_file(&buf, rebase_path_rewritten_pending(), (GIT_MAX_HEXSZ + 1) * 2) > 0 && |
1575 | !get_oid("HEAD", &newoid) && | |
e9d983f1 | 1576 | (out = fopen_or_warn(rebase_path_rewritten_list(), "a"))) { |
25cb8df9 JS |
1577 | char *bol = buf.buf, *eol; |
1578 | ||
1579 | while (*bol) { | |
1580 | eol = strchrnul(bol, '\n'); | |
1581 | fprintf(out, "%.*s %s\n", (int)(eol - bol), | |
092bbcdf | 1582 | bol, oid_to_hex(&newoid)); |
25cb8df9 JS |
1583 | if (!*eol) |
1584 | break; | |
1585 | bol = eol + 1; | |
1586 | } | |
1587 | fclose(out); | |
1588 | unlink(rebase_path_rewritten_pending()); | |
1589 | } | |
1590 | strbuf_release(&buf); | |
1591 | } | |
1592 | ||
1593 | static void record_in_rewritten(struct object_id *oid, | |
1594 | enum todo_command next_command) { | |
e9d983f1 | 1595 | FILE *out = fopen_or_warn(rebase_path_rewritten_pending(), "a"); |
25cb8df9 JS |
1596 | |
1597 | if (!out) | |
1598 | return; | |
1599 | ||
1600 | fprintf(out, "%s\n", oid_to_hex(oid)); | |
1601 | fclose(out); | |
1602 | ||
1603 | if (!is_fixup(next_command)) | |
1604 | flush_rewritten_pending(); | |
1605 | } | |
1606 | ||
004fefa7 | 1607 | static int do_pick_commit(enum todo_command command, struct commit *commit, |
6e98de72 | 1608 | struct replay_opts *opts, int final_fixup) |
043a4492 | 1609 | { |
789b3eff JS |
1610 | unsigned int flags = opts->edit ? EDIT_MSG : 0; |
1611 | const char *msg_file = opts->edit ? NULL : git_path_merge_msg(); | |
ace976b2 | 1612 | struct object_id head; |
043a4492 RR |
1613 | struct commit *base, *next, *parent; |
1614 | const char *base_label, *next_label; | |
356ee465 | 1615 | char *author = NULL; |
d74a4e57 | 1616 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
043a4492 | 1617 | struct strbuf msgbuf = STRBUF_INIT; |
789b3eff | 1618 | int res, unborn = 0, allow; |
043a4492 RR |
1619 | |
1620 | if (opts->no_commit) { | |
1621 | /* | |
1622 | * We do not intend to commit immediately. We just want to | |
1623 | * merge the differences in, so let's compute the tree | |
1624 | * that represents the "current" state for merge-recursive | |
1625 | * to work on. | |
1626 | */ | |
fc5cb99f | 1627 | if (write_cache_as_tree(&head, 0, NULL)) |
93b3df6f | 1628 | return error(_("your index file is unmerged.")); |
043a4492 | 1629 | } else { |
ace976b2 | 1630 | unborn = get_oid("HEAD", &head); |
d87d48b2 JS |
1631 | /* Do we want to generate a root commit? */ |
1632 | if (is_pick_or_similar(command) && opts->have_squash_onto && | |
1633 | !oidcmp(&head, &opts->squash_onto)) { | |
1634 | if (is_fixup(command)) | |
1635 | return error(_("cannot fixup root commit")); | |
1636 | flags |= CREATE_ROOT_COMMIT; | |
1637 | unborn = 1; | |
1638 | } else if (unborn) | |
eb0ccfd7 | 1639 | oidcpy(&head, the_hash_algo->empty_tree); |
02f2f56b BW |
1640 | if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", |
1641 | NULL, 0)) | |
043a4492 RR |
1642 | return error_dirty_index(opts); |
1643 | } | |
1644 | discard_cache(); | |
1645 | ||
637666c8 | 1646 | if (!commit->parents) |
043a4492 | 1647 | parent = NULL; |
043a4492 RR |
1648 | else if (commit->parents->next) { |
1649 | /* Reverting or cherry-picking a merge commit */ | |
1650 | int cnt; | |
1651 | struct commit_list *p; | |
1652 | ||
1653 | if (!opts->mainline) | |
93b3df6f | 1654 | return error(_("commit %s is a merge but no -m option was given."), |
f2fd0760 | 1655 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1656 | |
1657 | for (cnt = 1, p = commit->parents; | |
1658 | cnt != opts->mainline && p; | |
1659 | cnt++) | |
1660 | p = p->next; | |
1661 | if (cnt != opts->mainline || !p) | |
93b3df6f | 1662 | return error(_("commit %s does not have parent %d"), |
f2fd0760 | 1663 | oid_to_hex(&commit->object.oid), opts->mainline); |
043a4492 RR |
1664 | parent = p->item; |
1665 | } else if (0 < opts->mainline) | |
93b3df6f | 1666 | return error(_("mainline was specified but commit %s is not a merge."), |
f2fd0760 | 1667 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1668 | else |
1669 | parent = commit->parents->item; | |
1670 | ||
bcbb68be JS |
1671 | if (get_message(commit, &msg) != 0) |
1672 | return error(_("cannot get commit message for %s"), | |
1673 | oid_to_hex(&commit->object.oid)); | |
1674 | ||
6e98de72 | 1675 | if (opts->allow_ff && !is_fixup(command) && |
ace976b2 | 1676 | ((parent && !oidcmp(&parent->object.oid, &head)) || |
bcbb68be JS |
1677 | (!parent && unborn))) { |
1678 | if (is_rebase_i(opts)) | |
1679 | write_author_script(msg.message); | |
ace976b2 | 1680 | res = fast_forward_to(&commit->object.oid, &head, unborn, |
bcbb68be JS |
1681 | opts); |
1682 | if (res || command != TODO_REWORD) | |
1683 | goto leave; | |
5f8f9277 | 1684 | flags |= EDIT_MSG | AMEND_MSG | VERIFY_MSG; |
bcbb68be JS |
1685 | msg_file = NULL; |
1686 | goto fast_forward_edit; | |
1687 | } | |
043a4492 | 1688 | if (parent && parse_commit(parent) < 0) |
004fefa7 JS |
1689 | /* TRANSLATORS: The first %s will be a "todo" command like |
1690 | "revert" or "pick", the second %s a SHA1. */ | |
043a4492 | 1691 | return error(_("%s: cannot parse parent commit %s"), |
004fefa7 JS |
1692 | command_to_string(command), |
1693 | oid_to_hex(&parent->object.oid)); | |
043a4492 | 1694 | |
043a4492 RR |
1695 | /* |
1696 | * "commit" is an existing commit. We would want to apply | |
1697 | * the difference it introduces since its first parent "prev" | |
1698 | * on top of the current HEAD if we are cherry-pick. Or the | |
1699 | * reverse of it if we are revert. | |
1700 | */ | |
1701 | ||
004fefa7 | 1702 | if (command == TODO_REVERT) { |
043a4492 RR |
1703 | base = commit; |
1704 | base_label = msg.label; | |
1705 | next = parent; | |
1706 | next_label = msg.parent_label; | |
1707 | strbuf_addstr(&msgbuf, "Revert \""); | |
1708 | strbuf_addstr(&msgbuf, msg.subject); | |
1709 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); | |
f2fd0760 | 1710 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1711 | |
1712 | if (commit->parents && commit->parents->next) { | |
1713 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); | |
f2fd0760 | 1714 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
043a4492 RR |
1715 | } |
1716 | strbuf_addstr(&msgbuf, ".\n"); | |
1717 | } else { | |
1718 | const char *p; | |
1719 | ||
1720 | base = parent; | |
1721 | base_label = msg.parent_label; | |
1722 | next = commit; | |
1723 | next_label = msg.label; | |
1724 | ||
23aa5142 JS |
1725 | /* Append the commit log message to msgbuf. */ |
1726 | if (find_commit_subject(msg.message, &p)) | |
1727 | strbuf_addstr(&msgbuf, p); | |
043a4492 RR |
1728 | |
1729 | if (opts->record_origin) { | |
44dc738a | 1730 | strbuf_complete_line(&msgbuf); |
bab4d109 | 1731 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
b971e04f | 1732 | strbuf_addch(&msgbuf, '\n'); |
cd650a4e | 1733 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
f2fd0760 | 1734 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
1735 | strbuf_addstr(&msgbuf, ")\n"); |
1736 | } | |
356ee465 PW |
1737 | if (!is_fixup(command)) |
1738 | author = get_author(msg.message); | |
043a4492 RR |
1739 | } |
1740 | ||
04efc8b5 | 1741 | if (command == TODO_REWORD) |
b92ff6e8 | 1742 | flags |= EDIT_MSG | VERIFY_MSG; |
04efc8b5 | 1743 | else if (is_fixup(command)) { |
6e98de72 JS |
1744 | if (update_squash_messages(command, commit, opts)) |
1745 | return -1; | |
789b3eff | 1746 | flags |= AMEND_MSG; |
6e98de72 JS |
1747 | if (!final_fixup) |
1748 | msg_file = rebase_path_squash_msg(); | |
1749 | else if (file_exists(rebase_path_fixup_msg())) { | |
789b3eff | 1750 | flags |= CLEANUP_MSG; |
6e98de72 JS |
1751 | msg_file = rebase_path_fixup_msg(); |
1752 | } else { | |
ca03e067 | 1753 | const char *dest = git_path_squash_msg(); |
6e98de72 JS |
1754 | unlink(dest); |
1755 | if (copy_file(dest, rebase_path_squash_msg(), 0666)) | |
1756 | return error(_("could not rename '%s' to '%s'"), | |
1757 | rebase_path_squash_msg(), dest); | |
ca03e067 | 1758 | unlink(git_path_merge_msg()); |
6e98de72 | 1759 | msg_file = dest; |
789b3eff | 1760 | flags |= EDIT_MSG; |
6e98de72 JS |
1761 | } |
1762 | } | |
1763 | ||
a852ec7f | 1764 | if (opts->signoff && !is_fixup(command)) |
b34eeea3 PW |
1765 | append_signoff(&msgbuf, 0, 0); |
1766 | ||
0473f28a JS |
1767 | if (is_rebase_i(opts) && write_author_script(msg.message) < 0) |
1768 | res = -1; | |
1769 | else if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) { | |
043a4492 | 1770 | res = do_recursive_merge(base, next, base_label, next_label, |
48be4c62 | 1771 | &head, &msgbuf, opts); |
f241ff0d JS |
1772 | if (res < 0) |
1773 | return res; | |
75871495 | 1774 | res |= write_message(msgbuf.buf, msgbuf.len, |
f56fffef | 1775 | git_path_merge_msg(), 0); |
043a4492 RR |
1776 | } else { |
1777 | struct commit_list *common = NULL; | |
1778 | struct commit_list *remotes = NULL; | |
1779 | ||
75871495 | 1780 | res = write_message(msgbuf.buf, msgbuf.len, |
f56fffef | 1781 | git_path_merge_msg(), 0); |
043a4492 RR |
1782 | |
1783 | commit_list_insert(base, &common); | |
1784 | commit_list_insert(next, &remotes); | |
03a4e260 JS |
1785 | res |= try_merge_command(opts->strategy, |
1786 | opts->xopts_nr, (const char **)opts->xopts, | |
ace976b2 | 1787 | common, oid_to_hex(&head), remotes); |
043a4492 RR |
1788 | free_commit_list(common); |
1789 | free_commit_list(remotes); | |
1790 | } | |
452202c7 | 1791 | strbuf_release(&msgbuf); |
043a4492 RR |
1792 | |
1793 | /* | |
1794 | * If the merge was clean or if it failed due to conflict, we write | |
1795 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. | |
1796 | * However, if the merge did not even start, then we don't want to | |
1797 | * write it at all. | |
1798 | */ | |
004fefa7 | 1799 | if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) && |
ae077771 | 1800 | update_ref(NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL, |
91774afc | 1801 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
dbfad033 | 1802 | res = -1; |
004fefa7 | 1803 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
ae077771 | 1804 | update_ref(NULL, "REVERT_HEAD", &commit->object.oid, NULL, |
91774afc | 1805 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) |
dbfad033 | 1806 | res = -1; |
043a4492 RR |
1807 | |
1808 | if (res) { | |
004fefa7 | 1809 | error(command == TODO_REVERT |
043a4492 RR |
1810 | ? _("could not revert %s... %s") |
1811 | : _("could not apply %s... %s"), | |
39755964 | 1812 | short_commit_name(commit), msg.subject); |
ed727b19 | 1813 | print_advice(res == 1, opts); |
043a4492 | 1814 | rerere(opts->allow_rerere_auto); |
c8d1351d | 1815 | goto leave; |
043a4492 RR |
1816 | } |
1817 | ||
c8d1351d | 1818 | allow = allow_empty(opts, commit); |
706728a3 FC |
1819 | if (allow < 0) { |
1820 | res = allow; | |
1821 | goto leave; | |
789b3eff JS |
1822 | } else if (allow) |
1823 | flags |= ALLOW_EMPTY; | |
356ee465 | 1824 | if (!opts->no_commit) { |
bcbb68be | 1825 | fast_forward_edit: |
356ee465 PW |
1826 | if (author || command == TODO_REVERT || (flags & AMEND_MSG)) |
1827 | res = do_commit(msg_file, author, opts, flags); | |
1828 | else | |
1829 | res = error(_("unable to parse commit author")); | |
1830 | } | |
6e98de72 JS |
1831 | |
1832 | if (!res && final_fixup) { | |
1833 | unlink(rebase_path_fixup_msg()); | |
1834 | unlink(rebase_path_squash_msg()); | |
1835 | } | |
c8d1351d FC |
1836 | |
1837 | leave: | |
d74a4e57 | 1838 | free_message(commit, &msg); |
356ee465 | 1839 | free(author); |
1e41229d | 1840 | update_abort_safety_file(); |
043a4492 RR |
1841 | |
1842 | return res; | |
1843 | } | |
1844 | ||
c3e8618c | 1845 | static int prepare_revs(struct replay_opts *opts) |
043a4492 | 1846 | { |
a73e22e9 MZ |
1847 | /* |
1848 | * picking (but not reverting) ranges (but not individual revisions) | |
1849 | * should be done in reverse | |
1850 | */ | |
1851 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) | |
043a4492 RR |
1852 | opts->revs->reverse ^= 1; |
1853 | ||
1854 | if (prepare_revision_walk(opts->revs)) | |
c3e8618c | 1855 | return error(_("revision walk setup failed")); |
043a4492 RR |
1856 | |
1857 | if (!opts->revs->commits) | |
c3e8618c JS |
1858 | return error(_("empty commit set passed")); |
1859 | return 0; | |
043a4492 RR |
1860 | } |
1861 | ||
0d9c6dc9 | 1862 | static int read_and_refresh_cache(struct replay_opts *opts) |
043a4492 | 1863 | { |
14bca6c6 | 1864 | struct lock_file index_lock = LOCK_INIT; |
043a4492 | 1865 | int index_fd = hold_locked_index(&index_lock, 0); |
49fb937e JS |
1866 | if (read_index_preload(&the_index, NULL) < 0) { |
1867 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 1868 | return error(_("git %s: failed to read the index"), |
c28cbc5e | 1869 | _(action_name(opts))); |
49fb937e | 1870 | } |
043a4492 | 1871 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
61000814 MÅ |
1872 | if (index_fd >= 0) { |
1873 | if (write_locked_index(&the_index, &index_lock, | |
1874 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) { | |
0d9c6dc9 | 1875 | return error(_("git %s: failed to refresh the index"), |
c28cbc5e | 1876 | _(action_name(opts))); |
49fb937e | 1877 | } |
043a4492 | 1878 | } |
0d9c6dc9 | 1879 | return 0; |
043a4492 RR |
1880 | } |
1881 | ||
4c68e7dd JS |
1882 | enum todo_item_flags { |
1883 | TODO_EDIT_MERGE_MSG = 1 | |
1884 | }; | |
1885 | ||
004fefa7 JS |
1886 | struct todo_item { |
1887 | enum todo_command command; | |
1888 | struct commit *commit; | |
4c68e7dd | 1889 | unsigned int flags; |
c22f7dfb JS |
1890 | const char *arg; |
1891 | int arg_len; | |
004fefa7 JS |
1892 | size_t offset_in_buf; |
1893 | }; | |
1894 | ||
1895 | struct todo_list { | |
1896 | struct strbuf buf; | |
1897 | struct todo_item *items; | |
1898 | int nr, alloc, current; | |
968492e4 | 1899 | int done_nr, total_nr; |
54fd3243 | 1900 | struct stat_data stat; |
004fefa7 JS |
1901 | }; |
1902 | ||
1903 | #define TODO_LIST_INIT { STRBUF_INIT } | |
1904 | ||
1905 | static void todo_list_release(struct todo_list *todo_list) | |
043a4492 | 1906 | { |
004fefa7 | 1907 | strbuf_release(&todo_list->buf); |
6a83d902 | 1908 | FREE_AND_NULL(todo_list->items); |
004fefa7 JS |
1909 | todo_list->nr = todo_list->alloc = 0; |
1910 | } | |
043a4492 | 1911 | |
004fefa7 JS |
1912 | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
1913 | { | |
1914 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); | |
1915 | return todo_list->items + todo_list->nr++; | |
043a4492 RR |
1916 | } |
1917 | ||
004fefa7 | 1918 | static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) |
043a4492 | 1919 | { |
1e43ed98 | 1920 | struct object_id commit_oid; |
043a4492 | 1921 | char *end_of_object_name; |
004fefa7 JS |
1922 | int i, saved, status, padding; |
1923 | ||
4c68e7dd JS |
1924 | item->flags = 0; |
1925 | ||
8f8550b3 JS |
1926 | /* left-trim */ |
1927 | bol += strspn(bol, " \t"); | |
1928 | ||
25c43667 | 1929 | if (bol == eol || *bol == '\r' || *bol == comment_line_char) { |
ac191470 | 1930 | item->command = TODO_COMMENT; |
25c43667 JS |
1931 | item->commit = NULL; |
1932 | item->arg = bol; | |
1933 | item->arg_len = eol - bol; | |
1934 | return 0; | |
1935 | } | |
1936 | ||
ac191470 | 1937 | for (i = 0; i < TODO_COMMENT; i++) |
414697a9 JS |
1938 | if (skip_prefix(bol, todo_command_info[i].str, &bol)) { |
1939 | item->command = i; | |
1940 | break; | |
1941 | } else if (bol[1] == ' ' && *bol == todo_command_info[i].c) { | |
1942 | bol++; | |
004fefa7 JS |
1943 | item->command = i; |
1944 | break; | |
1945 | } | |
ac191470 | 1946 | if (i >= TODO_COMMENT) |
004fefa7 | 1947 | return -1; |
043a4492 | 1948 | |
66afa24f JS |
1949 | /* Eat up extra spaces/ tabs before object name */ |
1950 | padding = strspn(bol, " \t"); | |
1951 | bol += padding; | |
1952 | ||
25c43667 | 1953 | if (item->command == TODO_NOOP) { |
66afa24f JS |
1954 | if (bol != eol) |
1955 | return error(_("%s does not accept arguments: '%s'"), | |
1956 | command_to_string(item->command), bol); | |
25c43667 JS |
1957 | item->commit = NULL; |
1958 | item->arg = bol; | |
1959 | item->arg_len = eol - bol; | |
1960 | return 0; | |
1961 | } | |
1962 | ||
043a4492 | 1963 | if (!padding) |
66afa24f JS |
1964 | return error(_("missing arguments for %s"), |
1965 | command_to_string(item->command)); | |
043a4492 | 1966 | |
9055e401 JS |
1967 | if (item->command == TODO_EXEC || item->command == TODO_LABEL || |
1968 | item->command == TODO_RESET) { | |
7dcbb3cb | 1969 | item->commit = NULL; |
311af526 JS |
1970 | item->arg = bol; |
1971 | item->arg_len = (int)(eol - bol); | |
1972 | return 0; | |
1973 | } | |
1974 | ||
4c68e7dd JS |
1975 | if (item->command == TODO_MERGE) { |
1976 | if (skip_prefix(bol, "-C", &bol)) | |
1977 | bol += strspn(bol, " \t"); | |
1978 | else if (skip_prefix(bol, "-c", &bol)) { | |
1979 | bol += strspn(bol, " \t"); | |
1980 | item->flags |= TODO_EDIT_MERGE_MSG; | |
1981 | } else { | |
1982 | item->flags |= TODO_EDIT_MERGE_MSG; | |
1983 | item->commit = NULL; | |
1984 | item->arg = bol; | |
1985 | item->arg_len = (int)(eol - bol); | |
1986 | return 0; | |
1987 | } | |
1988 | } | |
1989 | ||
004fefa7 | 1990 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
043a4492 RR |
1991 | saved = *end_of_object_name; |
1992 | *end_of_object_name = '\0'; | |
1e43ed98 | 1993 | status = get_oid(bol, &commit_oid); |
043a4492 RR |
1994 | *end_of_object_name = saved; |
1995 | ||
c22f7dfb JS |
1996 | item->arg = end_of_object_name + strspn(end_of_object_name, " \t"); |
1997 | item->arg_len = (int)(eol - item->arg); | |
1998 | ||
043a4492 | 1999 | if (status < 0) |
004fefa7 | 2000 | return -1; |
043a4492 | 2001 | |
bc83266a | 2002 | item->commit = lookup_commit_reference(&commit_oid); |
004fefa7 | 2003 | return !item->commit; |
043a4492 RR |
2004 | } |
2005 | ||
004fefa7 | 2006 | static int parse_insn_buffer(char *buf, struct todo_list *todo_list) |
043a4492 | 2007 | { |
004fefa7 JS |
2008 | struct todo_item *item; |
2009 | char *p = buf, *next_p; | |
6e98de72 | 2010 | int i, res = 0, fixup_okay = file_exists(rebase_path_done()); |
043a4492 | 2011 | |
004fefa7 | 2012 | for (i = 1; *p; i++, p = next_p) { |
043a4492 | 2013 | char *eol = strchrnul(p, '\n'); |
004fefa7 JS |
2014 | |
2015 | next_p = *eol ? eol + 1 /* skip LF */ : eol; | |
2016 | ||
6307041d JS |
2017 | if (p != eol && eol[-1] == '\r') |
2018 | eol--; /* strip Carriage Return */ | |
2019 | ||
004fefa7 JS |
2020 | item = append_new_todo(todo_list); |
2021 | item->offset_in_buf = p - todo_list->buf.buf; | |
2022 | if (parse_insn_line(item, p, eol)) { | |
93b3df6f | 2023 | res = error(_("invalid line %d: %.*s"), |
004fefa7 | 2024 | i, (int)(eol - p), p); |
6e98de72 | 2025 | item->command = TODO_NOOP; |
004fefa7 | 2026 | } |
6e98de72 JS |
2027 | |
2028 | if (fixup_okay) | |
2029 | ; /* do nothing */ | |
2030 | else if (is_fixup(item->command)) | |
2031 | return error(_("cannot '%s' without a previous commit"), | |
2032 | command_to_string(item->command)); | |
2033 | else if (!is_noop(item->command)) | |
2034 | fixup_okay = 1; | |
043a4492 | 2035 | } |
52865279 | 2036 | |
004fefa7 | 2037 | return res; |
043a4492 RR |
2038 | } |
2039 | ||
968492e4 JS |
2040 | static int count_commands(struct todo_list *todo_list) |
2041 | { | |
2042 | int count = 0, i; | |
2043 | ||
2044 | for (i = 0; i < todo_list->nr; i++) | |
2045 | if (todo_list->items[i].command != TODO_COMMENT) | |
2046 | count++; | |
2047 | ||
2048 | return count; | |
2049 | } | |
2050 | ||
a01c2a5f JS |
2051 | static int get_item_line_offset(struct todo_list *todo_list, int index) |
2052 | { | |
2053 | return index < todo_list->nr ? | |
2054 | todo_list->items[index].offset_in_buf : todo_list->buf.len; | |
2055 | } | |
2056 | ||
2057 | static const char *get_item_line(struct todo_list *todo_list, int index) | |
2058 | { | |
2059 | return todo_list->buf.buf + get_item_line_offset(todo_list, index); | |
2060 | } | |
2061 | ||
2062 | static int get_item_line_length(struct todo_list *todo_list, int index) | |
2063 | { | |
2064 | return get_item_line_offset(todo_list, index + 1) | |
2065 | - get_item_line_offset(todo_list, index); | |
2066 | } | |
2067 | ||
87805600 RS |
2068 | static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path) |
2069 | { | |
2070 | int fd; | |
2071 | ssize_t len; | |
2072 | ||
2073 | fd = open(path, O_RDONLY); | |
2074 | if (fd < 0) | |
2075 | return error_errno(_("could not open '%s'"), path); | |
2076 | len = strbuf_read(sb, fd, 0); | |
2077 | close(fd); | |
2078 | if (len < 0) | |
2079 | return error(_("could not read '%s'."), path); | |
2080 | return len; | |
2081 | } | |
2082 | ||
004fefa7 | 2083 | static int read_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
2084 | struct replay_opts *opts) |
2085 | { | |
54fd3243 | 2086 | struct stat st; |
c0246501 | 2087 | const char *todo_file = get_todo_path(opts); |
87805600 | 2088 | int res; |
043a4492 | 2089 | |
004fefa7 | 2090 | strbuf_reset(&todo_list->buf); |
87805600 RS |
2091 | if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0) |
2092 | return -1; | |
043a4492 | 2093 | |
54fd3243 SH |
2094 | res = stat(todo_file, &st); |
2095 | if (res) | |
2096 | return error(_("could not stat '%s'"), todo_file); | |
2097 | fill_stat_data(&todo_list->stat, &st); | |
2098 | ||
004fefa7 | 2099 | res = parse_insn_buffer(todo_list->buf.buf, todo_list); |
27fdbb96 JS |
2100 | if (res) { |
2101 | if (is_rebase_i(opts)) | |
2102 | return error(_("please fix this using " | |
2103 | "'git rebase --edit-todo'.")); | |
93b3df6f | 2104 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
27fdbb96 | 2105 | } |
2eeaf1b3 | 2106 | |
52865279 JS |
2107 | if (!todo_list->nr && |
2108 | (!is_rebase_i(opts) || !file_exists(rebase_path_done()))) | |
2109 | return error(_("no commits parsed.")); | |
2110 | ||
2eeaf1b3 | 2111 | if (!is_rebase_i(opts)) { |
004fefa7 JS |
2112 | enum todo_command valid = |
2113 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; | |
2114 | int i; | |
2115 | ||
2116 | for (i = 0; i < todo_list->nr; i++) | |
2117 | if (valid == todo_list->items[i].command) | |
2118 | continue; | |
2119 | else if (valid == TODO_PICK) | |
93b3df6f | 2120 | return error(_("cannot cherry-pick during a revert.")); |
004fefa7 | 2121 | else |
93b3df6f | 2122 | return error(_("cannot revert during a cherry-pick.")); |
004fefa7 JS |
2123 | } |
2124 | ||
968492e4 JS |
2125 | if (is_rebase_i(opts)) { |
2126 | struct todo_list done = TODO_LIST_INIT; | |
e9d983f1 | 2127 | FILE *f = fopen_or_warn(rebase_path_msgtotal(), "w"); |
968492e4 JS |
2128 | |
2129 | if (strbuf_read_file(&done.buf, rebase_path_done(), 0) > 0 && | |
2130 | !parse_insn_buffer(done.buf.buf, &done)) | |
2131 | todo_list->done_nr = count_commands(&done); | |
2132 | else | |
2133 | todo_list->done_nr = 0; | |
2134 | ||
2135 | todo_list->total_nr = todo_list->done_nr | |
2136 | + count_commands(todo_list); | |
968492e4 | 2137 | todo_list_release(&done); |
ef80069a JS |
2138 | |
2139 | if (f) { | |
2140 | fprintf(f, "%d\n", todo_list->total_nr); | |
2141 | fclose(f); | |
2142 | } | |
968492e4 JS |
2143 | } |
2144 | ||
0ae42a03 | 2145 | return 0; |
043a4492 RR |
2146 | } |
2147 | ||
03a4e260 JS |
2148 | static int git_config_string_dup(char **dest, |
2149 | const char *var, const char *value) | |
2150 | { | |
2151 | if (!value) | |
2152 | return config_error_nonbool(var); | |
2153 | free(*dest); | |
2154 | *dest = xstrdup(value); | |
2155 | return 0; | |
2156 | } | |
2157 | ||
043a4492 RR |
2158 | static int populate_opts_cb(const char *key, const char *value, void *data) |
2159 | { | |
2160 | struct replay_opts *opts = data; | |
2161 | int error_flag = 1; | |
2162 | ||
2163 | if (!value) | |
2164 | error_flag = 0; | |
2165 | else if (!strcmp(key, "options.no-commit")) | |
2166 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); | |
2167 | else if (!strcmp(key, "options.edit")) | |
2168 | opts->edit = git_config_bool_or_int(key, value, &error_flag); | |
2169 | else if (!strcmp(key, "options.signoff")) | |
2170 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); | |
2171 | else if (!strcmp(key, "options.record-origin")) | |
2172 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); | |
2173 | else if (!strcmp(key, "options.allow-ff")) | |
2174 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); | |
2175 | else if (!strcmp(key, "options.mainline")) | |
2176 | opts->mainline = git_config_int(key, value); | |
2177 | else if (!strcmp(key, "options.strategy")) | |
03a4e260 | 2178 | git_config_string_dup(&opts->strategy, key, value); |
3253553e | 2179 | else if (!strcmp(key, "options.gpg-sign")) |
03a4e260 | 2180 | git_config_string_dup(&opts->gpg_sign, key, value); |
043a4492 RR |
2181 | else if (!strcmp(key, "options.strategy-option")) { |
2182 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); | |
2183 | opts->xopts[opts->xopts_nr++] = xstrdup(value); | |
8d8cb4b0 PW |
2184 | } else if (!strcmp(key, "options.allow-rerere-auto")) |
2185 | opts->allow_rerere_auto = | |
2186 | git_config_bool_or_int(key, value, &error_flag) ? | |
2187 | RERERE_AUTOUPDATE : RERERE_NOAUTOUPDATE; | |
2188 | else | |
93b3df6f | 2189 | return error(_("invalid key: %s"), key); |
043a4492 RR |
2190 | |
2191 | if (!error_flag) | |
93b3df6f | 2192 | return error(_("invalid value for %s: %s"), key, value); |
043a4492 RR |
2193 | |
2194 | return 0; | |
2195 | } | |
2196 | ||
ca6c6b45 JS |
2197 | static void read_strategy_opts(struct replay_opts *opts, struct strbuf *buf) |
2198 | { | |
2199 | int i; | |
2200 | ||
2201 | strbuf_reset(buf); | |
2202 | if (!read_oneliner(buf, rebase_path_strategy(), 0)) | |
2203 | return; | |
2204 | opts->strategy = strbuf_detach(buf, NULL); | |
2205 | if (!read_oneliner(buf, rebase_path_strategy_opts(), 0)) | |
2206 | return; | |
2207 | ||
2208 | opts->xopts_nr = split_cmdline(buf->buf, (const char ***)&opts->xopts); | |
2209 | for (i = 0; i < opts->xopts_nr; i++) { | |
2210 | const char *arg = opts->xopts[i]; | |
2211 | ||
2212 | skip_prefix(arg, "--", &arg); | |
2213 | opts->xopts[i] = xstrdup(arg); | |
2214 | } | |
2215 | } | |
2216 | ||
5adf9bdc | 2217 | static int read_populate_opts(struct replay_opts *opts) |
043a4492 | 2218 | { |
a1c75762 JS |
2219 | if (is_rebase_i(opts)) { |
2220 | struct strbuf buf = STRBUF_INIT; | |
2221 | ||
2222 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) { | |
2223 | if (!starts_with(buf.buf, "-S")) | |
2224 | strbuf_reset(&buf); | |
2225 | else { | |
2226 | free(opts->gpg_sign); | |
2227 | opts->gpg_sign = xstrdup(buf.buf + 2); | |
2228 | } | |
9b6d7a62 PW |
2229 | strbuf_reset(&buf); |
2230 | } | |
2231 | ||
2232 | if (read_oneliner(&buf, rebase_path_allow_rerere_autoupdate(), 1)) { | |
2233 | if (!strcmp(buf.buf, "--rerere-autoupdate")) | |
2234 | opts->allow_rerere_auto = RERERE_AUTOUPDATE; | |
2235 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) | |
2236 | opts->allow_rerere_auto = RERERE_NOAUTOUPDATE; | |
2237 | strbuf_reset(&buf); | |
a1c75762 | 2238 | } |
a1c75762 | 2239 | |
556907f1 JS |
2240 | if (file_exists(rebase_path_verbose())) |
2241 | opts->verbose = 1; | |
2242 | ||
a852ec7f PW |
2243 | if (file_exists(rebase_path_signoff())) { |
2244 | opts->allow_ff = 0; | |
2245 | opts->signoff = 1; | |
2246 | } | |
2247 | ||
ca6c6b45 JS |
2248 | read_strategy_opts(opts, &buf); |
2249 | strbuf_release(&buf); | |
2250 | ||
d87d48b2 JS |
2251 | if (read_oneliner(&buf, rebase_path_squash_onto(), 0)) { |
2252 | if (get_oid_hex(buf.buf, &opts->squash_onto) < 0) | |
2253 | return error(_("unusable squash-onto")); | |
2254 | opts->have_squash_onto = 1; | |
2255 | } | |
2256 | ||
b5a67045 | 2257 | return 0; |
a1c75762 | 2258 | } |
b5a67045 | 2259 | |
f932729c | 2260 | if (!file_exists(git_path_opts_file())) |
0d00da7b JS |
2261 | return 0; |
2262 | /* | |
2263 | * The function git_parse_source(), called from git_config_from_file(), | |
2264 | * may die() in case of a syntactically incorrect file. We do not care | |
2265 | * about this case, though, because we wrote that file ourselves, so we | |
2266 | * are pretty certain that it is syntactically correct. | |
2267 | */ | |
5adf9bdc | 2268 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
93b3df6f | 2269 | return error(_("malformed options sheet: '%s'"), |
0d00da7b JS |
2270 | git_path_opts_file()); |
2271 | return 0; | |
043a4492 RR |
2272 | } |
2273 | ||
004fefa7 | 2274 | static int walk_revs_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
2275 | struct replay_opts *opts) |
2276 | { | |
004fefa7 JS |
2277 | enum todo_command command = opts->action == REPLAY_PICK ? |
2278 | TODO_PICK : TODO_REVERT; | |
414697a9 | 2279 | const char *command_string = todo_command_info[command].str; |
043a4492 | 2280 | struct commit *commit; |
043a4492 | 2281 | |
34b0528b JS |
2282 | if (prepare_revs(opts)) |
2283 | return -1; | |
043a4492 | 2284 | |
004fefa7 JS |
2285 | while ((commit = get_revision(opts->revs))) { |
2286 | struct todo_item *item = append_new_todo(todo_list); | |
2287 | const char *commit_buffer = get_commit_buffer(commit, NULL); | |
2288 | const char *subject; | |
2289 | int subject_len; | |
2290 | ||
2291 | item->command = command; | |
2292 | item->commit = commit; | |
c22f7dfb JS |
2293 | item->arg = NULL; |
2294 | item->arg_len = 0; | |
004fefa7 JS |
2295 | item->offset_in_buf = todo_list->buf.len; |
2296 | subject_len = find_commit_subject(commit_buffer, &subject); | |
2297 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, | |
2298 | short_commit_name(commit), subject_len, subject); | |
2299 | unuse_commit_buffer(commit, commit_buffer); | |
2300 | } | |
34b0528b | 2301 | return 0; |
043a4492 RR |
2302 | } |
2303 | ||
2304 | static int create_seq_dir(void) | |
2305 | { | |
f932729c | 2306 | if (file_exists(git_path_seq_dir())) { |
043a4492 RR |
2307 | error(_("a cherry-pick or revert is already in progress")); |
2308 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); | |
2309 | return -1; | |
a70d8f80 | 2310 | } else if (mkdir(git_path_seq_dir(), 0777) < 0) |
93b3df6f | 2311 | return error_errno(_("could not create sequencer directory '%s'"), |
f6e82b0d | 2312 | git_path_seq_dir()); |
043a4492 RR |
2313 | return 0; |
2314 | } | |
2315 | ||
311fd397 | 2316 | static int save_head(const char *head) |
043a4492 | 2317 | { |
14bca6c6 | 2318 | struct lock_file head_lock = LOCK_INIT; |
043a4492 RR |
2319 | struct strbuf buf = STRBUF_INIT; |
2320 | int fd; | |
ed3f9a12 | 2321 | ssize_t written; |
043a4492 | 2322 | |
311fd397 | 2323 | fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); |
350292a1 | 2324 | if (fd < 0) |
93b3df6f | 2325 | return error_errno(_("could not lock HEAD")); |
043a4492 | 2326 | strbuf_addf(&buf, "%s\n", head); |
ed3f9a12 RS |
2327 | written = write_in_full(fd, buf.buf, buf.len); |
2328 | strbuf_release(&buf); | |
2329 | if (written < 0) { | |
bf5c0571 | 2330 | error_errno(_("could not write to '%s'"), git_path_head_file()); |
311fd397 | 2331 | rollback_lock_file(&head_lock); |
bf5c0571 | 2332 | return -1; |
311fd397 | 2333 | } |
350292a1 MÅ |
2334 | if (commit_lock_file(&head_lock) < 0) |
2335 | return error(_("failed to finalize '%s'"), git_path_head_file()); | |
311fd397 | 2336 | return 0; |
043a4492 RR |
2337 | } |
2338 | ||
1e41229d SB |
2339 | static int rollback_is_safe(void) |
2340 | { | |
2341 | struct strbuf sb = STRBUF_INIT; | |
2342 | struct object_id expected_head, actual_head; | |
2343 | ||
2344 | if (strbuf_read_file(&sb, git_path_abort_safety_file(), 0) >= 0) { | |
2345 | strbuf_trim(&sb); | |
2346 | if (get_oid_hex(sb.buf, &expected_head)) { | |
2347 | strbuf_release(&sb); | |
2348 | die(_("could not parse %s"), git_path_abort_safety_file()); | |
2349 | } | |
2350 | strbuf_release(&sb); | |
2351 | } | |
2352 | else if (errno == ENOENT) | |
2353 | oidclr(&expected_head); | |
2354 | else | |
2355 | die_errno(_("could not read '%s'"), git_path_abort_safety_file()); | |
2356 | ||
2357 | if (get_oid("HEAD", &actual_head)) | |
2358 | oidclr(&actual_head); | |
2359 | ||
2360 | return !oidcmp(&actual_head, &expected_head); | |
2361 | } | |
2362 | ||
092bbcdf | 2363 | static int reset_for_rollback(const struct object_id *oid) |
043a4492 RR |
2364 | { |
2365 | const char *argv[4]; /* reset --merge <arg> + NULL */ | |
1e41229d | 2366 | |
043a4492 RR |
2367 | argv[0] = "reset"; |
2368 | argv[1] = "--merge"; | |
092bbcdf | 2369 | argv[2] = oid_to_hex(oid); |
043a4492 RR |
2370 | argv[3] = NULL; |
2371 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
2372 | } | |
2373 | ||
2374 | static int rollback_single_pick(void) | |
2375 | { | |
092bbcdf | 2376 | struct object_id head_oid; |
043a4492 | 2377 | |
f932729c JK |
2378 | if (!file_exists(git_path_cherry_pick_head()) && |
2379 | !file_exists(git_path_revert_head())) | |
043a4492 | 2380 | return error(_("no cherry-pick or revert in progress")); |
34c290a6 | 2381 | if (read_ref_full("HEAD", 0, &head_oid, NULL)) |
043a4492 | 2382 | return error(_("cannot resolve HEAD")); |
092bbcdf | 2383 | if (is_null_oid(&head_oid)) |
043a4492 | 2384 | return error(_("cannot abort from a branch yet to be born")); |
092bbcdf | 2385 | return reset_for_rollback(&head_oid); |
043a4492 RR |
2386 | } |
2387 | ||
2863584f | 2388 | int sequencer_rollback(struct replay_opts *opts) |
043a4492 | 2389 | { |
043a4492 | 2390 | FILE *f; |
092bbcdf | 2391 | struct object_id oid; |
043a4492 | 2392 | struct strbuf buf = STRBUF_INIT; |
092bbcdf | 2393 | const char *p; |
043a4492 | 2394 | |
f932729c | 2395 | f = fopen(git_path_head_file(), "r"); |
043a4492 RR |
2396 | if (!f && errno == ENOENT) { |
2397 | /* | |
2398 | * There is no multiple-cherry-pick in progress. | |
2399 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates | |
2400 | * a single-cherry-pick in progress, abort that. | |
2401 | */ | |
2402 | return rollback_single_pick(); | |
2403 | } | |
2404 | if (!f) | |
f7ed1953 | 2405 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
8f309aeb | 2406 | if (strbuf_getline_lf(&buf, f)) { |
f7ed1953 | 2407 | error(_("cannot read '%s': %s"), git_path_head_file(), |
f932729c | 2408 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
043a4492 RR |
2409 | fclose(f); |
2410 | goto fail; | |
2411 | } | |
2412 | fclose(f); | |
092bbcdf | 2413 | if (parse_oid_hex(buf.buf, &oid, &p) || *p != '\0') { |
043a4492 | 2414 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), |
f932729c | 2415 | git_path_head_file()); |
043a4492 RR |
2416 | goto fail; |
2417 | } | |
092bbcdf | 2418 | if (is_null_oid(&oid)) { |
0f974e21 MG |
2419 | error(_("cannot abort from a branch yet to be born")); |
2420 | goto fail; | |
2421 | } | |
1e41229d SB |
2422 | |
2423 | if (!rollback_is_safe()) { | |
2424 | /* Do not error, just do not rollback */ | |
2425 | warning(_("You seem to have moved HEAD. " | |
2426 | "Not rewinding, check your HEAD!")); | |
2427 | } else | |
092bbcdf | 2428 | if (reset_for_rollback(&oid)) |
043a4492 | 2429 | goto fail; |
043a4492 | 2430 | strbuf_release(&buf); |
2863584f | 2431 | return sequencer_remove_state(opts); |
043a4492 RR |
2432 | fail: |
2433 | strbuf_release(&buf); | |
2434 | return -1; | |
2435 | } | |
2436 | ||
004fefa7 | 2437 | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 2438 | { |
14bca6c6 | 2439 | struct lock_file todo_lock = LOCK_INIT; |
004fefa7 JS |
2440 | const char *todo_path = get_todo_path(opts); |
2441 | int next = todo_list->current, offset, fd; | |
043a4492 | 2442 | |
84583957 JS |
2443 | /* |
2444 | * rebase -i writes "git-rebase-todo" without the currently executing | |
2445 | * command, appending it to "done" instead. | |
2446 | */ | |
2447 | if (is_rebase_i(opts)) | |
2448 | next++; | |
2449 | ||
004fefa7 | 2450 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
221675de | 2451 | if (fd < 0) |
93b3df6f | 2452 | return error_errno(_("could not lock '%s'"), todo_path); |
a01c2a5f | 2453 | offset = get_item_line_offset(todo_list, next); |
004fefa7 JS |
2454 | if (write_in_full(fd, todo_list->buf.buf + offset, |
2455 | todo_list->buf.len - offset) < 0) | |
93b3df6f | 2456 | return error_errno(_("could not write to '%s'"), todo_path); |
004fefa7 | 2457 | if (commit_lock_file(&todo_lock) < 0) |
350292a1 | 2458 | return error(_("failed to finalize '%s'"), todo_path); |
1df6df0c | 2459 | |
a01c2a5f JS |
2460 | if (is_rebase_i(opts) && next > 0) { |
2461 | const char *done = rebase_path_done(); | |
2462 | int fd = open(done, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
2463 | int ret = 0; | |
1df6df0c | 2464 | |
a01c2a5f JS |
2465 | if (fd < 0) |
2466 | return 0; | |
2467 | if (write_in_full(fd, get_item_line(todo_list, next - 1), | |
2468 | get_item_line_length(todo_list, next - 1)) | |
2469 | < 0) | |
2470 | ret = error_errno(_("could not write to '%s'"), done); | |
2471 | if (close(fd) < 0) | |
2472 | ret = error_errno(_("failed to finalize '%s'"), done); | |
2473 | return ret; | |
1df6df0c | 2474 | } |
221675de | 2475 | return 0; |
043a4492 RR |
2476 | } |
2477 | ||
88d5a271 | 2478 | static int save_opts(struct replay_opts *opts) |
043a4492 | 2479 | { |
f932729c | 2480 | const char *opts_file = git_path_opts_file(); |
88d5a271 | 2481 | int res = 0; |
043a4492 RR |
2482 | |
2483 | if (opts->no_commit) | |
88d5a271 | 2484 | res |= git_config_set_in_file_gently(opts_file, "options.no-commit", "true"); |
043a4492 | 2485 | if (opts->edit) |
88d5a271 | 2486 | res |= git_config_set_in_file_gently(opts_file, "options.edit", "true"); |
043a4492 | 2487 | if (opts->signoff) |
88d5a271 | 2488 | res |= git_config_set_in_file_gently(opts_file, "options.signoff", "true"); |
043a4492 | 2489 | if (opts->record_origin) |
88d5a271 | 2490 | res |= git_config_set_in_file_gently(opts_file, "options.record-origin", "true"); |
043a4492 | 2491 | if (opts->allow_ff) |
88d5a271 | 2492 | res |= git_config_set_in_file_gently(opts_file, "options.allow-ff", "true"); |
043a4492 RR |
2493 | if (opts->mainline) { |
2494 | struct strbuf buf = STRBUF_INIT; | |
2495 | strbuf_addf(&buf, "%d", opts->mainline); | |
88d5a271 | 2496 | res |= git_config_set_in_file_gently(opts_file, "options.mainline", buf.buf); |
043a4492 RR |
2497 | strbuf_release(&buf); |
2498 | } | |
2499 | if (opts->strategy) | |
88d5a271 | 2500 | res |= git_config_set_in_file_gently(opts_file, "options.strategy", opts->strategy); |
3253553e | 2501 | if (opts->gpg_sign) |
88d5a271 | 2502 | res |= git_config_set_in_file_gently(opts_file, "options.gpg-sign", opts->gpg_sign); |
043a4492 RR |
2503 | if (opts->xopts) { |
2504 | int i; | |
2505 | for (i = 0; i < opts->xopts_nr; i++) | |
88d5a271 | 2506 | res |= git_config_set_multivar_in_file_gently(opts_file, |
043a4492 RR |
2507 | "options.strategy-option", |
2508 | opts->xopts[i], "^$", 0); | |
2509 | } | |
8d8cb4b0 PW |
2510 | if (opts->allow_rerere_auto) |
2511 | res |= git_config_set_in_file_gently(opts_file, "options.allow-rerere-auto", | |
2512 | opts->allow_rerere_auto == RERERE_AUTOUPDATE ? | |
2513 | "true" : "false"); | |
88d5a271 | 2514 | return res; |
043a4492 RR |
2515 | } |
2516 | ||
56dc3ab0 JS |
2517 | static int make_patch(struct commit *commit, struct replay_opts *opts) |
2518 | { | |
2519 | struct strbuf buf = STRBUF_INIT; | |
2520 | struct rev_info log_tree_opt; | |
2521 | const char *subject, *p; | |
2522 | int res = 0; | |
2523 | ||
2524 | p = short_commit_name(commit); | |
2525 | if (write_message(p, strlen(p), rebase_path_stopped_sha(), 1) < 0) | |
2526 | return -1; | |
fbd7a232 NTND |
2527 | if (update_ref("rebase", "REBASE_HEAD", &commit->object.oid, |
2528 | NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) | |
2529 | res |= error(_("could not update %s"), "REBASE_HEAD"); | |
56dc3ab0 JS |
2530 | |
2531 | strbuf_addf(&buf, "%s/patch", get_dir(opts)); | |
2532 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); | |
2533 | init_revisions(&log_tree_opt, NULL); | |
2534 | log_tree_opt.abbrev = 0; | |
2535 | log_tree_opt.diff = 1; | |
2536 | log_tree_opt.diffopt.output_format = DIFF_FORMAT_PATCH; | |
2537 | log_tree_opt.disable_stdin = 1; | |
2538 | log_tree_opt.no_commit_id = 1; | |
2539 | log_tree_opt.diffopt.file = fopen(buf.buf, "w"); | |
2540 | log_tree_opt.diffopt.use_color = GIT_COLOR_NEVER; | |
2541 | if (!log_tree_opt.diffopt.file) | |
2542 | res |= error_errno(_("could not open '%s'"), buf.buf); | |
2543 | else { | |
2544 | res |= log_tree_commit(&log_tree_opt, commit); | |
2545 | fclose(log_tree_opt.diffopt.file); | |
2546 | } | |
2547 | strbuf_reset(&buf); | |
2548 | ||
2549 | strbuf_addf(&buf, "%s/message", get_dir(opts)); | |
2550 | if (!file_exists(buf.buf)) { | |
2551 | const char *commit_buffer = get_commit_buffer(commit, NULL); | |
2552 | find_commit_subject(commit_buffer, &subject); | |
2553 | res |= write_message(subject, strlen(subject), buf.buf, 1); | |
2554 | unuse_commit_buffer(commit, commit_buffer); | |
2555 | } | |
2556 | strbuf_release(&buf); | |
2557 | ||
2558 | return res; | |
2559 | } | |
2560 | ||
2561 | static int intend_to_amend(void) | |
2562 | { | |
092bbcdf | 2563 | struct object_id head; |
56dc3ab0 JS |
2564 | char *p; |
2565 | ||
092bbcdf | 2566 | if (get_oid("HEAD", &head)) |
56dc3ab0 JS |
2567 | return error(_("cannot read HEAD")); |
2568 | ||
092bbcdf | 2569 | p = oid_to_hex(&head); |
56dc3ab0 JS |
2570 | return write_message(p, strlen(p), rebase_path_amend(), 1); |
2571 | } | |
2572 | ||
2573 | static int error_with_patch(struct commit *commit, | |
2574 | const char *subject, int subject_len, | |
2575 | struct replay_opts *opts, int exit_code, int to_amend) | |
2576 | { | |
2577 | if (make_patch(commit, opts)) | |
2578 | return -1; | |
2579 | ||
2580 | if (to_amend) { | |
2581 | if (intend_to_amend()) | |
2582 | return -1; | |
2583 | ||
2584 | fprintf(stderr, "You can amend the commit now, with\n" | |
2585 | "\n" | |
2586 | " git commit --amend %s\n" | |
2587 | "\n" | |
2588 | "Once you are satisfied with your changes, run\n" | |
2589 | "\n" | |
2590 | " git rebase --continue\n", gpg_sign_opt_quoted(opts)); | |
2591 | } else if (exit_code) | |
2592 | fprintf(stderr, "Could not apply %s... %.*s\n", | |
2593 | short_commit_name(commit), subject_len, subject); | |
2594 | ||
2595 | return exit_code; | |
2596 | } | |
2597 | ||
6e98de72 JS |
2598 | static int error_failed_squash(struct commit *commit, |
2599 | struct replay_opts *opts, int subject_len, const char *subject) | |
2600 | { | |
2601 | if (rename(rebase_path_squash_msg(), rebase_path_message())) | |
2602 | return error(_("could not rename '%s' to '%s'"), | |
2603 | rebase_path_squash_msg(), rebase_path_message()); | |
2604 | unlink(rebase_path_fixup_msg()); | |
ca03e067 JK |
2605 | unlink(git_path_merge_msg()); |
2606 | if (copy_file(git_path_merge_msg(), rebase_path_message(), 0666)) | |
6e98de72 | 2607 | return error(_("could not copy '%s' to '%s'"), |
ca03e067 | 2608 | rebase_path_message(), git_path_merge_msg()); |
6e98de72 JS |
2609 | return error_with_patch(commit, subject, subject_len, opts, 1, 0); |
2610 | } | |
2611 | ||
311af526 JS |
2612 | static int do_exec(const char *command_line) |
2613 | { | |
09d7b6c6 | 2614 | struct argv_array child_env = ARGV_ARRAY_INIT; |
311af526 JS |
2615 | const char *child_argv[] = { NULL, NULL }; |
2616 | int dirty, status; | |
2617 | ||
2618 | fprintf(stderr, "Executing: %s\n", command_line); | |
2619 | child_argv[0] = command_line; | |
09d7b6c6 JK |
2620 | argv_array_pushf(&child_env, "GIT_DIR=%s", absolute_path(get_git_dir())); |
2621 | status = run_command_v_opt_cd_env(child_argv, RUN_USING_SHELL, NULL, | |
2622 | child_env.argv); | |
311af526 JS |
2623 | |
2624 | /* force re-reading of the cache */ | |
2625 | if (discard_cache() < 0 || read_cache() < 0) | |
2626 | return error(_("could not read index")); | |
2627 | ||
2628 | dirty = require_clean_work_tree("rebase", NULL, 1, 1); | |
2629 | ||
2630 | if (status) { | |
2631 | warning(_("execution failed: %s\n%s" | |
2632 | "You can fix the problem, and then run\n" | |
2633 | "\n" | |
2634 | " git rebase --continue\n" | |
2635 | "\n"), | |
2636 | command_line, | |
2637 | dirty ? N_("and made changes to the index and/or the " | |
2638 | "working tree\n") : ""); | |
2639 | if (status == 127) | |
2640 | /* command not found */ | |
2641 | status = 1; | |
2642 | } else if (dirty) { | |
2643 | warning(_("execution succeeded: %s\nbut " | |
2644 | "left changes to the index and/or the working tree\n" | |
2645 | "Commit or stash your changes, and then run\n" | |
2646 | "\n" | |
2647 | " git rebase --continue\n" | |
2648 | "\n"), command_line); | |
2649 | status = 1; | |
2650 | } | |
2651 | ||
09d7b6c6 JK |
2652 | argv_array_clear(&child_env); |
2653 | ||
311af526 JS |
2654 | return status; |
2655 | } | |
2656 | ||
9055e401 JS |
2657 | static int safe_append(const char *filename, const char *fmt, ...) |
2658 | { | |
2659 | va_list ap; | |
2660 | struct lock_file lock = LOCK_INIT; | |
2661 | int fd = hold_lock_file_for_update(&lock, filename, | |
2662 | LOCK_REPORT_ON_ERROR); | |
2663 | struct strbuf buf = STRBUF_INIT; | |
2664 | ||
2665 | if (fd < 0) | |
2666 | return -1; | |
2667 | ||
2668 | if (strbuf_read_file(&buf, filename, 0) < 0 && errno != ENOENT) { | |
2669 | error_errno(_("could not read '%s'"), filename); | |
2670 | rollback_lock_file(&lock); | |
2671 | return -1; | |
2672 | } | |
2673 | strbuf_complete(&buf, '\n'); | |
2674 | va_start(ap, fmt); | |
2675 | strbuf_vaddf(&buf, fmt, ap); | |
2676 | va_end(ap); | |
2677 | ||
2678 | if (write_in_full(fd, buf.buf, buf.len) < 0) { | |
2679 | error_errno(_("could not write to '%s'"), filename); | |
2680 | strbuf_release(&buf); | |
2681 | rollback_lock_file(&lock); | |
2682 | return -1; | |
2683 | } | |
2684 | if (commit_lock_file(&lock) < 0) { | |
2685 | strbuf_release(&buf); | |
2686 | rollback_lock_file(&lock); | |
2687 | return error(_("failed to finalize '%s'"), filename); | |
2688 | } | |
2689 | ||
2690 | strbuf_release(&buf); | |
2691 | return 0; | |
2692 | } | |
2693 | ||
2694 | static int do_label(const char *name, int len) | |
2695 | { | |
2696 | struct ref_store *refs = get_main_ref_store(); | |
2697 | struct ref_transaction *transaction; | |
2698 | struct strbuf ref_name = STRBUF_INIT, err = STRBUF_INIT; | |
2699 | struct strbuf msg = STRBUF_INIT; | |
2700 | int ret = 0; | |
2701 | struct object_id head_oid; | |
2702 | ||
2703 | if (len == 1 && *name == '#') | |
2704 | return error("Illegal label name: '%.*s'", len, name); | |
2705 | ||
2706 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); | |
2707 | strbuf_addf(&msg, "rebase -i (label) '%.*s'", len, name); | |
2708 | ||
2709 | transaction = ref_store_transaction_begin(refs, &err); | |
2710 | if (!transaction) { | |
2711 | error("%s", err.buf); | |
2712 | ret = -1; | |
2713 | } else if (get_oid("HEAD", &head_oid)) { | |
2714 | error(_("could not read HEAD")); | |
2715 | ret = -1; | |
2716 | } else if (ref_transaction_update(transaction, ref_name.buf, &head_oid, | |
2717 | NULL, 0, msg.buf, &err) < 0 || | |
2718 | ref_transaction_commit(transaction, &err)) { | |
2719 | error("%s", err.buf); | |
2720 | ret = -1; | |
2721 | } | |
2722 | ref_transaction_free(transaction); | |
2723 | strbuf_release(&err); | |
2724 | strbuf_release(&msg); | |
2725 | ||
2726 | if (!ret) | |
2727 | ret = safe_append(rebase_path_refs_to_delete(), | |
2728 | "%s\n", ref_name.buf); | |
2729 | strbuf_release(&ref_name); | |
2730 | ||
2731 | return ret; | |
2732 | } | |
2733 | ||
2734 | static const char *reflog_message(struct replay_opts *opts, | |
2735 | const char *sub_action, const char *fmt, ...); | |
2736 | ||
2737 | static int do_reset(const char *name, int len, struct replay_opts *opts) | |
2738 | { | |
2739 | struct strbuf ref_name = STRBUF_INIT; | |
2740 | struct object_id oid; | |
2741 | struct lock_file lock = LOCK_INIT; | |
2742 | struct tree_desc desc; | |
2743 | struct tree *tree; | |
2744 | struct unpack_trees_options unpack_tree_opts; | |
2745 | int ret = 0, i; | |
2746 | ||
2747 | if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) | |
2748 | return -1; | |
2749 | ||
2750 | /* Determine the length of the label */ | |
2751 | for (i = 0; i < len; i++) | |
2752 | if (isspace(name[i])) | |
2753 | len = i; | |
2754 | ||
2755 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name); | |
2756 | if (get_oid(ref_name.buf, &oid) && | |
2757 | get_oid(ref_name.buf + strlen("refs/rewritten/"), &oid)) { | |
2758 | error(_("could not read '%s'"), ref_name.buf); | |
2759 | rollback_lock_file(&lock); | |
2760 | strbuf_release(&ref_name); | |
2761 | return -1; | |
2762 | } | |
2763 | ||
2764 | memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); | |
2765 | setup_unpack_trees_porcelain(&unpack_tree_opts, "reset"); | |
2766 | unpack_tree_opts.head_idx = 1; | |
2767 | unpack_tree_opts.src_index = &the_index; | |
2768 | unpack_tree_opts.dst_index = &the_index; | |
2769 | unpack_tree_opts.fn = oneway_merge; | |
2770 | unpack_tree_opts.merge = 1; | |
2771 | unpack_tree_opts.update = 1; | |
2772 | ||
2773 | if (read_cache_unmerged()) { | |
2774 | rollback_lock_file(&lock); | |
2775 | strbuf_release(&ref_name); | |
2776 | return error_resolve_conflict(_(action_name(opts))); | |
2777 | } | |
2778 | ||
2779 | if (!fill_tree_descriptor(&desc, &oid)) { | |
2780 | error(_("failed to find tree of %s"), oid_to_hex(&oid)); | |
2781 | rollback_lock_file(&lock); | |
2782 | free((void *)desc.buffer); | |
2783 | strbuf_release(&ref_name); | |
2784 | return -1; | |
2785 | } | |
2786 | ||
2787 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { | |
2788 | rollback_lock_file(&lock); | |
2789 | free((void *)desc.buffer); | |
2790 | strbuf_release(&ref_name); | |
2791 | return -1; | |
2792 | } | |
2793 | ||
2794 | tree = parse_tree_indirect(&oid); | |
2795 | prime_cache_tree(&the_index, tree); | |
2796 | ||
2797 | if (write_locked_index(&the_index, &lock, COMMIT_LOCK) < 0) | |
2798 | ret = error(_("could not write index")); | |
2799 | free((void *)desc.buffer); | |
2800 | ||
2801 | if (!ret) | |
2802 | ret = update_ref(reflog_message(opts, "reset", "'%.*s'", | |
2803 | len, name), "HEAD", &oid, | |
2804 | NULL, 0, UPDATE_REFS_MSG_ON_ERR); | |
2805 | ||
2806 | strbuf_release(&ref_name); | |
2807 | return ret; | |
2808 | } | |
2809 | ||
4c68e7dd JS |
2810 | static int do_merge(struct commit *commit, const char *arg, int arg_len, |
2811 | int flags, struct replay_opts *opts) | |
2812 | { | |
2813 | int run_commit_flags = (flags & TODO_EDIT_MERGE_MSG) ? | |
2814 | EDIT_MSG | VERIFY_MSG : 0; | |
2815 | struct strbuf ref_name = STRBUF_INIT; | |
2816 | struct commit *head_commit, *merge_commit, *i; | |
2817 | struct commit_list *bases, *j, *reversed = NULL; | |
2818 | struct merge_options o; | |
d1e8b011 | 2819 | int merge_arg_len, oneline_offset, can_fast_forward, ret; |
4c68e7dd JS |
2820 | static struct lock_file lock; |
2821 | const char *p; | |
2822 | ||
2823 | if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) { | |
2824 | ret = -1; | |
2825 | goto leave_merge; | |
2826 | } | |
2827 | ||
2828 | head_commit = lookup_commit_reference_by_name("HEAD"); | |
2829 | if (!head_commit) { | |
2830 | ret = error(_("cannot merge without a current revision")); | |
2831 | goto leave_merge; | |
2832 | } | |
2833 | ||
2834 | oneline_offset = arg_len; | |
2835 | merge_arg_len = strcspn(arg, " \t\n"); | |
2836 | p = arg + merge_arg_len; | |
2837 | p += strspn(p, " \t\n"); | |
2838 | if (*p == '#' && (!p[1] || isspace(p[1]))) { | |
2839 | p += 1 + strspn(p + 1, " \t\n"); | |
2840 | oneline_offset = p - arg; | |
2841 | } else if (p - arg < arg_len) | |
2842 | BUG("octopus merges are not supported yet: '%s'", p); | |
2843 | ||
2844 | strbuf_addf(&ref_name, "refs/rewritten/%.*s", merge_arg_len, arg); | |
2845 | merge_commit = lookup_commit_reference_by_name(ref_name.buf); | |
2846 | if (!merge_commit) { | |
2847 | /* fall back to non-rewritten ref or commit */ | |
2848 | strbuf_splice(&ref_name, 0, strlen("refs/rewritten/"), "", 0); | |
2849 | merge_commit = lookup_commit_reference_by_name(ref_name.buf); | |
2850 | } | |
2851 | ||
2852 | if (!merge_commit) { | |
2853 | ret = error(_("could not resolve '%s'"), ref_name.buf); | |
2854 | goto leave_merge; | |
2855 | } | |
2856 | ||
2857 | if (commit) { | |
2858 | const char *message = get_commit_buffer(commit, NULL); | |
2859 | const char *body; | |
2860 | int len; | |
2861 | ||
2862 | if (!message) { | |
2863 | ret = error(_("could not get commit message of '%s'"), | |
2864 | oid_to_hex(&commit->object.oid)); | |
2865 | goto leave_merge; | |
2866 | } | |
2867 | write_author_script(message); | |
2868 | find_commit_subject(message, &body); | |
2869 | len = strlen(body); | |
2870 | ret = write_message(body, len, git_path_merge_msg(), 0); | |
2871 | unuse_commit_buffer(commit, message); | |
2872 | if (ret) { | |
2873 | error_errno(_("could not write '%s'"), | |
2874 | git_path_merge_msg()); | |
2875 | goto leave_merge; | |
2876 | } | |
2877 | } else { | |
2878 | struct strbuf buf = STRBUF_INIT; | |
2879 | int len; | |
2880 | ||
2881 | strbuf_addf(&buf, "author %s", git_author_info(0)); | |
2882 | write_author_script(buf.buf); | |
2883 | strbuf_reset(&buf); | |
2884 | ||
2885 | if (oneline_offset < arg_len) { | |
2886 | p = arg + oneline_offset; | |
2887 | len = arg_len - oneline_offset; | |
2888 | } else { | |
2889 | strbuf_addf(&buf, "Merge branch '%.*s'", | |
2890 | merge_arg_len, arg); | |
2891 | p = buf.buf; | |
2892 | len = buf.len; | |
2893 | } | |
2894 | ||
2895 | ret = write_message(p, len, git_path_merge_msg(), 0); | |
2896 | strbuf_release(&buf); | |
2897 | if (ret) { | |
2898 | error_errno(_("could not write '%s'"), | |
2899 | git_path_merge_msg()); | |
2900 | goto leave_merge; | |
2901 | } | |
2902 | } | |
2903 | ||
d1e8b011 JS |
2904 | /* |
2905 | * If HEAD is not identical to the first parent of the original merge | |
2906 | * commit, we cannot fast-forward. | |
2907 | */ | |
2908 | can_fast_forward = opts->allow_ff && commit && commit->parents && | |
2909 | !oidcmp(&commit->parents->item->object.oid, | |
2910 | &head_commit->object.oid); | |
2911 | ||
2912 | /* | |
2913 | * If the merge head is different from the original one, we cannot | |
2914 | * fast-forward. | |
2915 | */ | |
2916 | if (can_fast_forward) { | |
2917 | struct commit_list *second_parent = commit->parents->next; | |
2918 | ||
2919 | if (second_parent && !second_parent->next && | |
2920 | oidcmp(&merge_commit->object.oid, | |
2921 | &second_parent->item->object.oid)) | |
2922 | can_fast_forward = 0; | |
2923 | } | |
2924 | ||
2925 | if (can_fast_forward && commit->parents->next && | |
2926 | !commit->parents->next->next && | |
2927 | !oidcmp(&commit->parents->next->item->object.oid, | |
2928 | &merge_commit->object.oid)) { | |
2929 | rollback_lock_file(&lock); | |
2930 | ret = fast_forward_to(&commit->object.oid, | |
2931 | &head_commit->object.oid, 0, opts); | |
2932 | goto leave_merge; | |
2933 | } | |
2934 | ||
4c68e7dd JS |
2935 | write_message(oid_to_hex(&merge_commit->object.oid), GIT_SHA1_HEXSZ, |
2936 | git_path_merge_head(), 0); | |
2937 | write_message("no-ff", 5, git_path_merge_mode(), 0); | |
2938 | ||
2939 | bases = get_merge_bases(head_commit, merge_commit); | |
7ccdf65b JS |
2940 | if (bases && !oidcmp(&merge_commit->object.oid, |
2941 | &bases->item->object.oid)) { | |
2942 | ret = 0; | |
2943 | /* skip merging an ancestor of HEAD */ | |
2944 | goto leave_merge; | |
2945 | } | |
2946 | ||
4c68e7dd JS |
2947 | for (j = bases; j; j = j->next) |
2948 | commit_list_insert(j->item, &reversed); | |
2949 | free_commit_list(bases); | |
2950 | ||
2951 | read_cache(); | |
2952 | init_merge_options(&o); | |
2953 | o.branch1 = "HEAD"; | |
2954 | o.branch2 = ref_name.buf; | |
2955 | o.buffer_output = 2; | |
2956 | ||
2957 | ret = merge_recursive(&o, head_commit, merge_commit, reversed, &i); | |
2958 | if (ret <= 0) | |
2959 | fputs(o.obuf.buf, stdout); | |
2960 | strbuf_release(&o.obuf); | |
2961 | if (ret < 0) { | |
2962 | error(_("could not even attempt to merge '%.*s'"), | |
2963 | merge_arg_len, arg); | |
2964 | goto leave_merge; | |
2965 | } | |
2966 | /* | |
2967 | * The return value of merge_recursive() is 1 on clean, and 0 on | |
2968 | * unclean merge. | |
2969 | * | |
2970 | * Let's reverse that, so that do_merge() returns 0 upon success and | |
2971 | * 1 upon failed merge (keeping the return value -1 for the cases where | |
2972 | * we will want to reschedule the `merge` command). | |
2973 | */ | |
2974 | ret = !ret; | |
2975 | ||
2976 | if (active_cache_changed && | |
2977 | write_locked_index(&the_index, &lock, COMMIT_LOCK)) { | |
2978 | ret = error(_("merge: Unable to write new index file")); | |
2979 | goto leave_merge; | |
2980 | } | |
2981 | ||
2982 | rollback_lock_file(&lock); | |
2983 | if (ret) | |
2984 | rerere(opts->allow_rerere_auto); | |
2985 | else | |
2986 | /* | |
2987 | * In case of problems, we now want to return a positive | |
2988 | * value (a negative one would indicate that the `merge` | |
2989 | * command needs to be rescheduled). | |
2990 | */ | |
2991 | ret = !!run_git_commit(git_path_merge_msg(), opts, | |
2992 | run_commit_flags); | |
2993 | ||
2994 | leave_merge: | |
2995 | strbuf_release(&ref_name); | |
2996 | rollback_lock_file(&lock); | |
2997 | return ret; | |
2998 | } | |
2999 | ||
6e98de72 JS |
3000 | static int is_final_fixup(struct todo_list *todo_list) |
3001 | { | |
3002 | int i = todo_list->current; | |
3003 | ||
3004 | if (!is_fixup(todo_list->items[i].command)) | |
3005 | return 0; | |
3006 | ||
3007 | while (++i < todo_list->nr) | |
3008 | if (is_fixup(todo_list->items[i].command)) | |
3009 | return 0; | |
3010 | else if (!is_noop(todo_list->items[i].command)) | |
3011 | break; | |
3012 | return 1; | |
3013 | } | |
3014 | ||
25cb8df9 JS |
3015 | static enum todo_command peek_command(struct todo_list *todo_list, int offset) |
3016 | { | |
3017 | int i; | |
3018 | ||
3019 | for (i = todo_list->current + offset; i < todo_list->nr; i++) | |
3020 | if (!is_noop(todo_list->items[i].command)) | |
3021 | return todo_list->items[i].command; | |
3022 | ||
3023 | return -1; | |
3024 | } | |
3025 | ||
796c7972 JS |
3026 | static int apply_autostash(struct replay_opts *opts) |
3027 | { | |
3028 | struct strbuf stash_sha1 = STRBUF_INIT; | |
3029 | struct child_process child = CHILD_PROCESS_INIT; | |
3030 | int ret = 0; | |
3031 | ||
3032 | if (!read_oneliner(&stash_sha1, rebase_path_autostash(), 1)) { | |
3033 | strbuf_release(&stash_sha1); | |
3034 | return 0; | |
3035 | } | |
3036 | strbuf_trim(&stash_sha1); | |
3037 | ||
3038 | child.git_cmd = 1; | |
79a62269 PW |
3039 | child.no_stdout = 1; |
3040 | child.no_stderr = 1; | |
796c7972 JS |
3041 | argv_array_push(&child.args, "stash"); |
3042 | argv_array_push(&child.args, "apply"); | |
3043 | argv_array_push(&child.args, stash_sha1.buf); | |
3044 | if (!run_command(&child)) | |
cdb866b3 | 3045 | fprintf(stderr, _("Applied autostash.\n")); |
796c7972 JS |
3046 | else { |
3047 | struct child_process store = CHILD_PROCESS_INIT; | |
3048 | ||
3049 | store.git_cmd = 1; | |
3050 | argv_array_push(&store.args, "stash"); | |
3051 | argv_array_push(&store.args, "store"); | |
3052 | argv_array_push(&store.args, "-m"); | |
3053 | argv_array_push(&store.args, "autostash"); | |
3054 | argv_array_push(&store.args, "-q"); | |
3055 | argv_array_push(&store.args, stash_sha1.buf); | |
3056 | if (run_command(&store)) | |
3057 | ret = error(_("cannot store %s"), stash_sha1.buf); | |
3058 | else | |
cdb866b3 JS |
3059 | fprintf(stderr, |
3060 | _("Applying autostash resulted in conflicts.\n" | |
3061 | "Your changes are safe in the stash.\n" | |
3062 | "You can run \"git stash pop\" or" | |
3063 | " \"git stash drop\" at any time.\n")); | |
796c7972 JS |
3064 | } |
3065 | ||
3066 | strbuf_release(&stash_sha1); | |
3067 | return ret; | |
3068 | } | |
3069 | ||
96e832a5 JS |
3070 | static const char *reflog_message(struct replay_opts *opts, |
3071 | const char *sub_action, const char *fmt, ...) | |
3072 | { | |
3073 | va_list ap; | |
3074 | static struct strbuf buf = STRBUF_INIT; | |
3075 | ||
3076 | va_start(ap, fmt); | |
3077 | strbuf_reset(&buf); | |
3078 | strbuf_addstr(&buf, action_name(opts)); | |
3079 | if (sub_action) | |
3080 | strbuf_addf(&buf, " (%s)", sub_action); | |
3081 | if (fmt) { | |
3082 | strbuf_addstr(&buf, ": "); | |
3083 | strbuf_vaddf(&buf, fmt, ap); | |
3084 | } | |
3085 | va_end(ap); | |
3086 | ||
3087 | return buf.buf; | |
3088 | } | |
3089 | ||
cb5206ea JS |
3090 | static const char rescheduled_advice[] = |
3091 | N_("Could not execute the todo command\n" | |
3092 | "\n" | |
3093 | " %.*s" | |
3094 | "\n" | |
3095 | "It has been rescheduled; To edit the command before continuing, please\n" | |
3096 | "edit the todo list first:\n" | |
3097 | "\n" | |
3098 | " git rebase --edit-todo\n" | |
3099 | " git rebase --continue\n"); | |
3100 | ||
004fefa7 | 3101 | static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 3102 | { |
9055e401 | 3103 | int res = 0, reschedule = 0; |
043a4492 RR |
3104 | |
3105 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
3106 | if (opts->allow_ff) | |
3107 | assert(!(opts->signoff || opts->no_commit || | |
3108 | opts->record_origin || opts->edit)); | |
0d9c6dc9 JS |
3109 | if (read_and_refresh_cache(opts)) |
3110 | return -1; | |
043a4492 | 3111 | |
004fefa7 JS |
3112 | while (todo_list->current < todo_list->nr) { |
3113 | struct todo_item *item = todo_list->items + todo_list->current; | |
3114 | if (save_todo(todo_list, opts)) | |
221675de | 3115 | return -1; |
6e98de72 | 3116 | if (is_rebase_i(opts)) { |
ef80069a JS |
3117 | if (item->command != TODO_COMMENT) { |
3118 | FILE *f = fopen(rebase_path_msgnum(), "w"); | |
3119 | ||
3120 | todo_list->done_nr++; | |
3121 | ||
3122 | if (f) { | |
3123 | fprintf(f, "%d\n", todo_list->done_nr); | |
3124 | fclose(f); | |
3125 | } | |
968492e4 | 3126 | fprintf(stderr, "Rebasing (%d/%d)%s", |
ef80069a | 3127 | todo_list->done_nr, |
968492e4 JS |
3128 | todo_list->total_nr, |
3129 | opts->verbose ? "\n" : "\r"); | |
ef80069a | 3130 | } |
6e98de72 JS |
3131 | unlink(rebase_path_message()); |
3132 | unlink(rebase_path_author_script()); | |
3133 | unlink(rebase_path_stopped_sha()); | |
3134 | unlink(rebase_path_amend()); | |
fbd7a232 | 3135 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); |
6e98de72 JS |
3136 | } |
3137 | if (item->command <= TODO_SQUASH) { | |
8ab37ef2 JS |
3138 | if (is_rebase_i(opts)) |
3139 | setenv("GIT_REFLOG_ACTION", reflog_message(opts, | |
3140 | command_to_string(item->command), NULL), | |
3141 | 1); | |
25c43667 | 3142 | res = do_pick_commit(item->command, item->commit, |
6e98de72 | 3143 | opts, is_final_fixup(todo_list)); |
9d7bf3cf JS |
3144 | if (is_rebase_i(opts) && res < 0) { |
3145 | /* Reschedule */ | |
cb5206ea JS |
3146 | advise(_(rescheduled_advice), |
3147 | get_item_line_length(todo_list, | |
3148 | todo_list->current), | |
3149 | get_item_line(todo_list, | |
3150 | todo_list->current)); | |
9d7bf3cf JS |
3151 | todo_list->current--; |
3152 | if (save_todo(todo_list, opts)) | |
3153 | return -1; | |
3154 | } | |
56dc3ab0 JS |
3155 | if (item->command == TODO_EDIT) { |
3156 | struct commit *commit = item->commit; | |
3157 | if (!res) | |
99429213 | 3158 | fprintf(stderr, |
a42e1b41 | 3159 | _("Stopped at %s... %.*s\n"), |
56dc3ab0 JS |
3160 | short_commit_name(commit), |
3161 | item->arg_len, item->arg); | |
3162 | return error_with_patch(commit, | |
3163 | item->arg, item->arg_len, opts, res, | |
3164 | !res); | |
3165 | } | |
25cb8df9 JS |
3166 | if (is_rebase_i(opts) && !res) |
3167 | record_in_rewritten(&item->commit->object.oid, | |
3168 | peek_command(todo_list, 1)); | |
6e98de72 JS |
3169 | if (res && is_fixup(item->command)) { |
3170 | if (res == 1) | |
3171 | intend_to_amend(); | |
3172 | return error_failed_squash(item->commit, opts, | |
3173 | item->arg_len, item->arg); | |
9055e401 | 3174 | } else if (res && is_rebase_i(opts) && item->commit) |
4a5146f9 | 3175 | return res | error_with_patch(item->commit, |
04efc8b5 JS |
3176 | item->arg, item->arg_len, opts, res, |
3177 | item->command == TODO_REWORD); | |
311af526 JS |
3178 | } else if (item->command == TODO_EXEC) { |
3179 | char *end_of_arg = (char *)(item->arg + item->arg_len); | |
3180 | int saved = *end_of_arg; | |
54fd3243 | 3181 | struct stat st; |
311af526 JS |
3182 | |
3183 | *end_of_arg = '\0'; | |
3184 | res = do_exec(item->arg); | |
3185 | *end_of_arg = saved; | |
54fd3243 SH |
3186 | |
3187 | /* Reread the todo file if it has changed. */ | |
3188 | if (res) | |
3189 | ; /* fall through */ | |
3190 | else if (stat(get_todo_path(opts), &st)) | |
3191 | res = error_errno(_("could not stat '%s'"), | |
3192 | get_todo_path(opts)); | |
3193 | else if (match_stat_data(&todo_list->stat, &st)) { | |
3194 | todo_list_release(todo_list); | |
3195 | if (read_populate_todo(todo_list, opts)) | |
3196 | res = -1; /* message was printed */ | |
3197 | /* `current` will be incremented below */ | |
3198 | todo_list->current = -1; | |
3199 | } | |
9055e401 JS |
3200 | } else if (item->command == TODO_LABEL) { |
3201 | if ((res = do_label(item->arg, item->arg_len))) | |
3202 | reschedule = 1; | |
3203 | } else if (item->command == TODO_RESET) { | |
3204 | if ((res = do_reset(item->arg, item->arg_len, opts))) | |
3205 | reschedule = 1; | |
4c68e7dd JS |
3206 | } else if (item->command == TODO_MERGE) { |
3207 | if ((res = do_merge(item->commit, | |
3208 | item->arg, item->arg_len, | |
3209 | item->flags, opts)) < 0) | |
3210 | reschedule = 1; | |
537e7d61 JS |
3211 | else if (item->commit) |
3212 | record_in_rewritten(&item->commit->object.oid, | |
3213 | peek_command(todo_list, 1)); | |
3214 | if (res > 0) | |
4c68e7dd JS |
3215 | /* failed with merge conflicts */ |
3216 | return error_with_patch(item->commit, | |
3217 | item->arg, | |
3218 | item->arg_len, opts, | |
3219 | res, 0); | |
56dc3ab0 | 3220 | } else if (!is_noop(item->command)) |
25c43667 JS |
3221 | return error(_("unknown command %d"), item->command); |
3222 | ||
9055e401 JS |
3223 | if (reschedule) { |
3224 | advise(_(rescheduled_advice), | |
3225 | get_item_line_length(todo_list, | |
3226 | todo_list->current), | |
3227 | get_item_line(todo_list, todo_list->current)); | |
3228 | todo_list->current--; | |
3229 | if (save_todo(todo_list, opts)) | |
3230 | return -1; | |
4c68e7dd JS |
3231 | if (item->commit) |
3232 | return error_with_patch(item->commit, | |
3233 | item->arg, | |
3234 | item->arg_len, opts, | |
3235 | res, 0); | |
9055e401 JS |
3236 | } |
3237 | ||
004fefa7 | 3238 | todo_list->current++; |
043a4492 RR |
3239 | if (res) |
3240 | return res; | |
3241 | } | |
3242 | ||
56dc3ab0 | 3243 | if (is_rebase_i(opts)) { |
4b83ce9f | 3244 | struct strbuf head_ref = STRBUF_INIT, buf = STRBUF_INIT; |
25cb8df9 | 3245 | struct stat st; |
556907f1 | 3246 | |
56dc3ab0 JS |
3247 | /* Stopped in the middle, as planned? */ |
3248 | if (todo_list->current < todo_list->nr) | |
3249 | return 0; | |
556907f1 | 3250 | |
4b83ce9f JS |
3251 | if (read_oneliner(&head_ref, rebase_path_head_name(), 0) && |
3252 | starts_with(head_ref.buf, "refs/")) { | |
96e832a5 | 3253 | const char *msg; |
092bbcdf | 3254 | struct object_id head, orig; |
4b83ce9f JS |
3255 | int res; |
3256 | ||
092bbcdf | 3257 | if (get_oid("HEAD", &head)) { |
4b83ce9f JS |
3258 | res = error(_("cannot read HEAD")); |
3259 | cleanup_head_ref: | |
3260 | strbuf_release(&head_ref); | |
3261 | strbuf_release(&buf); | |
3262 | return res; | |
3263 | } | |
3264 | if (!read_oneliner(&buf, rebase_path_orig_head(), 0) || | |
092bbcdf | 3265 | get_oid_hex(buf.buf, &orig)) { |
4b83ce9f JS |
3266 | res = error(_("could not read orig-head")); |
3267 | goto cleanup_head_ref; | |
3268 | } | |
4ab867b8 | 3269 | strbuf_reset(&buf); |
4b83ce9f JS |
3270 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) { |
3271 | res = error(_("could not read 'onto'")); | |
3272 | goto cleanup_head_ref; | |
3273 | } | |
96e832a5 JS |
3274 | msg = reflog_message(opts, "finish", "%s onto %s", |
3275 | head_ref.buf, buf.buf); | |
ae077771 | 3276 | if (update_ref(msg, head_ref.buf, &head, &orig, |
91774afc | 3277 | REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { |
4b83ce9f JS |
3278 | res = error(_("could not update %s"), |
3279 | head_ref.buf); | |
3280 | goto cleanup_head_ref; | |
3281 | } | |
96e832a5 | 3282 | msg = reflog_message(opts, "finish", "returning to %s", |
4b83ce9f | 3283 | head_ref.buf); |
96e832a5 | 3284 | if (create_symref("HEAD", head_ref.buf, msg)) { |
4b83ce9f JS |
3285 | res = error(_("could not update HEAD to %s"), |
3286 | head_ref.buf); | |
3287 | goto cleanup_head_ref; | |
3288 | } | |
3289 | strbuf_reset(&buf); | |
3290 | } | |
3291 | ||
556907f1 JS |
3292 | if (opts->verbose) { |
3293 | struct rev_info log_tree_opt; | |
3294 | struct object_id orig, head; | |
3295 | ||
3296 | memset(&log_tree_opt, 0, sizeof(log_tree_opt)); | |
3297 | init_revisions(&log_tree_opt, NULL); | |
3298 | log_tree_opt.diff = 1; | |
3299 | log_tree_opt.diffopt.output_format = | |
3300 | DIFF_FORMAT_DIFFSTAT; | |
3301 | log_tree_opt.disable_stdin = 1; | |
3302 | ||
3303 | if (read_oneliner(&buf, rebase_path_orig_head(), 0) && | |
e82caf38 | 3304 | !get_oid(buf.buf, &orig) && |
3305 | !get_oid("HEAD", &head)) { | |
66f414f8 BW |
3306 | diff_tree_oid(&orig, &head, "", |
3307 | &log_tree_opt.diffopt); | |
556907f1 JS |
3308 | log_tree_diff_flush(&log_tree_opt); |
3309 | } | |
3310 | } | |
25cb8df9 JS |
3311 | flush_rewritten_pending(); |
3312 | if (!stat(rebase_path_rewritten_list(), &st) && | |
3313 | st.st_size > 0) { | |
3314 | struct child_process child = CHILD_PROCESS_INIT; | |
79516045 JS |
3315 | const char *post_rewrite_hook = |
3316 | find_hook("post-rewrite"); | |
25cb8df9 JS |
3317 | |
3318 | child.in = open(rebase_path_rewritten_list(), O_RDONLY); | |
3319 | child.git_cmd = 1; | |
3320 | argv_array_push(&child.args, "notes"); | |
3321 | argv_array_push(&child.args, "copy"); | |
3322 | argv_array_push(&child.args, "--for-rewrite=rebase"); | |
3323 | /* we don't care if this copying failed */ | |
3324 | run_command(&child); | |
79516045 JS |
3325 | |
3326 | if (post_rewrite_hook) { | |
3327 | struct child_process hook = CHILD_PROCESS_INIT; | |
3328 | ||
3329 | hook.in = open(rebase_path_rewritten_list(), | |
3330 | O_RDONLY); | |
3331 | hook.stdout_to_stderr = 1; | |
3332 | argv_array_push(&hook.args, post_rewrite_hook); | |
3333 | argv_array_push(&hook.args, "rebase"); | |
3334 | /* we don't care if this hook failed */ | |
3335 | run_command(&hook); | |
3336 | } | |
25cb8df9 | 3337 | } |
796c7972 | 3338 | apply_autostash(opts); |
25cb8df9 | 3339 | |
5da4966f JS |
3340 | fprintf(stderr, "Successfully rebased and updated %s.\n", |
3341 | head_ref.buf); | |
3342 | ||
556907f1 | 3343 | strbuf_release(&buf); |
4b83ce9f | 3344 | strbuf_release(&head_ref); |
56dc3ab0 JS |
3345 | } |
3346 | ||
043a4492 RR |
3347 | /* |
3348 | * Sequence of picks finished successfully; cleanup by | |
3349 | * removing the .git/sequencer directory | |
3350 | */ | |
2863584f | 3351 | return sequencer_remove_state(opts); |
043a4492 RR |
3352 | } |
3353 | ||
3354 | static int continue_single_pick(void) | |
3355 | { | |
3356 | const char *argv[] = { "commit", NULL }; | |
3357 | ||
f932729c JK |
3358 | if (!file_exists(git_path_cherry_pick_head()) && |
3359 | !file_exists(git_path_revert_head())) | |
043a4492 RR |
3360 | return error(_("no cherry-pick or revert in progress")); |
3361 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
3362 | } | |
3363 | ||
9d93ccd1 JS |
3364 | static int commit_staged_changes(struct replay_opts *opts) |
3365 | { | |
789b3eff | 3366 | unsigned int flags = ALLOW_EMPTY | EDIT_MSG; |
9d93ccd1 JS |
3367 | |
3368 | if (has_unstaged_changes(1)) | |
3369 | return error(_("cannot rebase: You have unstaged changes.")); | |
52632209 | 3370 | if (!has_uncommitted_changes(0)) { |
ca03e067 | 3371 | const char *cherry_pick_head = git_path_cherry_pick_head(); |
52632209 JS |
3372 | |
3373 | if (file_exists(cherry_pick_head) && unlink(cherry_pick_head)) | |
3374 | return error(_("could not remove CHERRY_PICK_HEAD")); | |
9d93ccd1 | 3375 | return 0; |
52632209 | 3376 | } |
9d93ccd1 JS |
3377 | |
3378 | if (file_exists(rebase_path_amend())) { | |
3379 | struct strbuf rev = STRBUF_INIT; | |
092bbcdf | 3380 | struct object_id head, to_amend; |
9d93ccd1 | 3381 | |
092bbcdf | 3382 | if (get_oid("HEAD", &head)) |
9d93ccd1 JS |
3383 | return error(_("cannot amend non-existing commit")); |
3384 | if (!read_oneliner(&rev, rebase_path_amend(), 0)) | |
3385 | return error(_("invalid file: '%s'"), rebase_path_amend()); | |
092bbcdf | 3386 | if (get_oid_hex(rev.buf, &to_amend)) |
9d93ccd1 JS |
3387 | return error(_("invalid contents: '%s'"), |
3388 | rebase_path_amend()); | |
092bbcdf | 3389 | if (oidcmp(&head, &to_amend)) |
9d93ccd1 JS |
3390 | return error(_("\nYou have uncommitted changes in your " |
3391 | "working tree. Please, commit them\n" | |
3392 | "first and then run 'git rebase " | |
3393 | "--continue' again.")); | |
3394 | ||
3395 | strbuf_release(&rev); | |
789b3eff | 3396 | flags |= AMEND_MSG; |
9d93ccd1 JS |
3397 | } |
3398 | ||
789b3eff | 3399 | if (run_git_commit(rebase_path_message(), opts, flags)) |
9d93ccd1 JS |
3400 | return error(_("could not commit staged changes.")); |
3401 | unlink(rebase_path_amend()); | |
3402 | return 0; | |
3403 | } | |
3404 | ||
2863584f | 3405 | int sequencer_continue(struct replay_opts *opts) |
043a4492 | 3406 | { |
004fefa7 JS |
3407 | struct todo_list todo_list = TODO_LIST_INIT; |
3408 | int res; | |
043a4492 | 3409 | |
2863584f JS |
3410 | if (read_and_refresh_cache(opts)) |
3411 | return -1; | |
3412 | ||
9d93ccd1 JS |
3413 | if (is_rebase_i(opts)) { |
3414 | if (commit_staged_changes(opts)) | |
3415 | return -1; | |
4258a6da | 3416 | } else if (!file_exists(get_todo_path(opts))) |
043a4492 | 3417 | return continue_single_pick(); |
004fefa7 | 3418 | if (read_populate_opts(opts)) |
0ae42a03 | 3419 | return -1; |
004fefa7 JS |
3420 | if ((res = read_populate_todo(&todo_list, opts))) |
3421 | goto release_todo_list; | |
043a4492 | 3422 | |
4258a6da JS |
3423 | if (!is_rebase_i(opts)) { |
3424 | /* Verify that the conflict has been resolved */ | |
3425 | if (file_exists(git_path_cherry_pick_head()) || | |
3426 | file_exists(git_path_revert_head())) { | |
3427 | res = continue_single_pick(); | |
3428 | if (res) | |
3429 | goto release_todo_list; | |
3430 | } | |
02f2f56b | 3431 | if (index_differs_from("HEAD", NULL, 0)) { |
4258a6da | 3432 | res = error_dirty_index(opts); |
004fefa7 | 3433 | goto release_todo_list; |
4258a6da JS |
3434 | } |
3435 | todo_list.current++; | |
ca98c6d4 JS |
3436 | } else if (file_exists(rebase_path_stopped_sha())) { |
3437 | struct strbuf buf = STRBUF_INIT; | |
3438 | struct object_id oid; | |
3439 | ||
3440 | if (read_oneliner(&buf, rebase_path_stopped_sha(), 1) && | |
e82caf38 | 3441 | !get_oid_committish(buf.buf, &oid)) |
ca98c6d4 JS |
3442 | record_in_rewritten(&oid, peek_command(&todo_list, 0)); |
3443 | strbuf_release(&buf); | |
043a4492 | 3444 | } |
4258a6da | 3445 | |
004fefa7 JS |
3446 | res = pick_commits(&todo_list, opts); |
3447 | release_todo_list: | |
3448 | todo_list_release(&todo_list); | |
3449 | return res; | |
043a4492 RR |
3450 | } |
3451 | ||
3452 | static int single_pick(struct commit *cmit, struct replay_opts *opts) | |
3453 | { | |
3454 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
004fefa7 | 3455 | return do_pick_commit(opts->action == REPLAY_PICK ? |
6e98de72 | 3456 | TODO_PICK : TODO_REVERT, cmit, opts, 0); |
043a4492 RR |
3457 | } |
3458 | ||
3459 | int sequencer_pick_revisions(struct replay_opts *opts) | |
3460 | { | |
004fefa7 | 3461 | struct todo_list todo_list = TODO_LIST_INIT; |
1e43ed98 | 3462 | struct object_id oid; |
004fefa7 | 3463 | int i, res; |
043a4492 | 3464 | |
2863584f | 3465 | assert(opts->revs); |
0d9c6dc9 JS |
3466 | if (read_and_refresh_cache(opts)) |
3467 | return -1; | |
043a4492 | 3468 | |
21246dbb | 3469 | for (i = 0; i < opts->revs->pending.nr; i++) { |
1e43ed98 | 3470 | struct object_id oid; |
21246dbb MV |
3471 | const char *name = opts->revs->pending.objects[i].name; |
3472 | ||
3473 | /* This happens when using --stdin. */ | |
3474 | if (!strlen(name)) | |
3475 | continue; | |
3476 | ||
1e43ed98 | 3477 | if (!get_oid(name, &oid)) { |
bc83266a | 3478 | if (!lookup_commit_reference_gently(&oid, 1)) { |
abef9020 | 3479 | enum object_type type = oid_object_info(&oid, |
3480 | NULL); | |
b9b946d4 | 3481 | return error(_("%s: can't cherry-pick a %s"), |
debca9d2 | 3482 | name, type_name(type)); |
7c0b0d8d | 3483 | } |
21246dbb | 3484 | } else |
b9b946d4 | 3485 | return error(_("%s: bad revision"), name); |
21246dbb MV |
3486 | } |
3487 | ||
043a4492 RR |
3488 | /* |
3489 | * If we were called as "git cherry-pick <commit>", just | |
3490 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / | |
3491 | * REVERT_HEAD, and don't touch the sequencer state. | |
3492 | * This means it is possible to cherry-pick in the middle | |
3493 | * of a cherry-pick sequence. | |
3494 | */ | |
3495 | if (opts->revs->cmdline.nr == 1 && | |
3496 | opts->revs->cmdline.rev->whence == REV_CMD_REV && | |
3497 | opts->revs->no_walk && | |
3498 | !opts->revs->cmdline.rev->flags) { | |
3499 | struct commit *cmit; | |
3500 | if (prepare_revision_walk(opts->revs)) | |
b9b946d4 | 3501 | return error(_("revision walk setup failed")); |
043a4492 RR |
3502 | cmit = get_revision(opts->revs); |
3503 | if (!cmit || get_revision(opts->revs)) | |
b9b946d4 | 3504 | return error("BUG: expected exactly one commit from walk"); |
043a4492 RR |
3505 | return single_pick(cmit, opts); |
3506 | } | |
3507 | ||
3508 | /* | |
3509 | * Start a new cherry-pick/ revert sequence; but | |
3510 | * first, make sure that an existing one isn't in | |
3511 | * progress | |
3512 | */ | |
3513 | ||
34b0528b JS |
3514 | if (walk_revs_populate_todo(&todo_list, opts) || |
3515 | create_seq_dir() < 0) | |
043a4492 | 3516 | return -1; |
1e43ed98 | 3517 | if (get_oid("HEAD", &oid) && (opts->action == REPLAY_REVERT)) |
93b3df6f | 3518 | return error(_("can't revert as initial commit")); |
1e43ed98 | 3519 | if (save_head(oid_to_hex(&oid))) |
311fd397 | 3520 | return -1; |
88d5a271 JS |
3521 | if (save_opts(opts)) |
3522 | return -1; | |
1e41229d | 3523 | update_abort_safety_file(); |
004fefa7 JS |
3524 | res = pick_commits(&todo_list, opts); |
3525 | todo_list_release(&todo_list); | |
3526 | return res; | |
043a4492 | 3527 | } |
5ed75e2a | 3528 | |
bab4d109 | 3529 | void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) |
5ed75e2a | 3530 | { |
bab4d109 | 3531 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
5ed75e2a | 3532 | struct strbuf sob = STRBUF_INIT; |
bab4d109 | 3533 | int has_footer; |
5ed75e2a MV |
3534 | |
3535 | strbuf_addstr(&sob, sign_off_header); | |
3536 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), | |
3537 | getenv("GIT_COMMITTER_EMAIL"))); | |
3538 | strbuf_addch(&sob, '\n'); | |
bab4d109 | 3539 | |
44dc738a JT |
3540 | if (!ignore_footer) |
3541 | strbuf_complete_line(msgbuf); | |
3542 | ||
bab4d109 BC |
3543 | /* |
3544 | * If the whole message buffer is equal to the sob, pretend that we | |
3545 | * found a conforming footer with a matching sob | |
3546 | */ | |
3547 | if (msgbuf->len - ignore_footer == sob.len && | |
3548 | !strncmp(msgbuf->buf, sob.buf, sob.len)) | |
3549 | has_footer = 3; | |
3550 | else | |
3551 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); | |
3552 | ||
33f2f9ab BC |
3553 | if (!has_footer) { |
3554 | const char *append_newlines = NULL; | |
3555 | size_t len = msgbuf->len - ignore_footer; | |
3556 | ||
8c613fd5 BC |
3557 | if (!len) { |
3558 | /* | |
3559 | * The buffer is completely empty. Leave foom for | |
3560 | * the title and body to be filled in by the user. | |
3561 | */ | |
33f2f9ab | 3562 | append_newlines = "\n\n"; |
8c613fd5 BC |
3563 | } else if (len == 1) { |
3564 | /* | |
3565 | * Buffer contains a single newline. Add another | |
3566 | * so that we leave room for the title and body. | |
3567 | */ | |
3568 | append_newlines = "\n"; | |
3569 | } else if (msgbuf->buf[len - 2] != '\n') { | |
3570 | /* | |
3571 | * Buffer ends with a single newline. Add another | |
3572 | * so that there is an empty line between the message | |
3573 | * body and the sob. | |
3574 | */ | |
33f2f9ab | 3575 | append_newlines = "\n"; |
8c613fd5 | 3576 | } /* else, the buffer already ends with two newlines. */ |
33f2f9ab BC |
3577 | |
3578 | if (append_newlines) | |
3579 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
3580 | append_newlines, strlen(append_newlines)); | |
5ed75e2a | 3581 | } |
bab4d109 BC |
3582 | |
3583 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) | |
3584 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
3585 | sob.buf, sob.len); | |
3586 | ||
5ed75e2a MV |
3587 | strbuf_release(&sob); |
3588 | } | |
62db5247 | 3589 | |
1644c73c JS |
3590 | struct labels_entry { |
3591 | struct hashmap_entry entry; | |
3592 | char label[FLEX_ARRAY]; | |
3593 | }; | |
3594 | ||
3595 | static int labels_cmp(const void *fndata, const struct labels_entry *a, | |
3596 | const struct labels_entry *b, const void *key) | |
3597 | { | |
3598 | return key ? strcmp(a->label, key) : strcmp(a->label, b->label); | |
3599 | } | |
3600 | ||
3601 | struct string_entry { | |
3602 | struct oidmap_entry entry; | |
3603 | char string[FLEX_ARRAY]; | |
3604 | }; | |
3605 | ||
3606 | struct label_state { | |
3607 | struct oidmap commit2label; | |
3608 | struct hashmap labels; | |
3609 | struct strbuf buf; | |
3610 | }; | |
3611 | ||
3612 | static const char *label_oid(struct object_id *oid, const char *label, | |
3613 | struct label_state *state) | |
3614 | { | |
3615 | struct labels_entry *labels_entry; | |
3616 | struct string_entry *string_entry; | |
3617 | struct object_id dummy; | |
3618 | size_t len; | |
3619 | int i; | |
3620 | ||
3621 | string_entry = oidmap_get(&state->commit2label, oid); | |
3622 | if (string_entry) | |
3623 | return string_entry->string; | |
3624 | ||
3625 | /* | |
3626 | * For "uninteresting" commits, i.e. commits that are not to be | |
3627 | * rebased, and which can therefore not be labeled, we use a unique | |
3628 | * abbreviation of the commit name. This is slightly more complicated | |
3629 | * than calling find_unique_abbrev() because we also need to make | |
3630 | * sure that the abbreviation does not conflict with any other | |
3631 | * label. | |
3632 | * | |
3633 | * We disallow "interesting" commits to be labeled by a string that | |
3634 | * is a valid full-length hash, to ensure that we always can find an | |
3635 | * abbreviation for any uninteresting commit's names that does not | |
3636 | * clash with any other label. | |
3637 | */ | |
3638 | if (!label) { | |
3639 | char *p; | |
3640 | ||
3641 | strbuf_reset(&state->buf); | |
3642 | strbuf_grow(&state->buf, GIT_SHA1_HEXSZ); | |
3643 | label = p = state->buf.buf; | |
3644 | ||
3645 | find_unique_abbrev_r(p, oid, default_abbrev); | |
3646 | ||
3647 | /* | |
3648 | * We may need to extend the abbreviated hash so that there is | |
3649 | * no conflicting label. | |
3650 | */ | |
3651 | if (hashmap_get_from_hash(&state->labels, strihash(p), p)) { | |
3652 | size_t i = strlen(p) + 1; | |
3653 | ||
3654 | oid_to_hex_r(p, oid); | |
3655 | for (; i < GIT_SHA1_HEXSZ; i++) { | |
3656 | char save = p[i]; | |
3657 | p[i] = '\0'; | |
3658 | if (!hashmap_get_from_hash(&state->labels, | |
3659 | strihash(p), p)) | |
3660 | break; | |
3661 | p[i] = save; | |
3662 | } | |
3663 | } | |
3664 | } else if (((len = strlen(label)) == GIT_SHA1_RAWSZ && | |
3665 | !get_oid_hex(label, &dummy)) || | |
3666 | (len == 1 && *label == '#') || | |
3667 | hashmap_get_from_hash(&state->labels, | |
3668 | strihash(label), label)) { | |
3669 | /* | |
3670 | * If the label already exists, or if the label is a valid full | |
3671 | * OID, or the label is a '#' (which we use as a separator | |
3672 | * between merge heads and oneline), we append a dash and a | |
3673 | * number to make it unique. | |
3674 | */ | |
3675 | struct strbuf *buf = &state->buf; | |
3676 | ||
3677 | strbuf_reset(buf); | |
3678 | strbuf_add(buf, label, len); | |
3679 | ||
3680 | for (i = 2; ; i++) { | |
3681 | strbuf_setlen(buf, len); | |
3682 | strbuf_addf(buf, "-%d", i); | |
3683 | if (!hashmap_get_from_hash(&state->labels, | |
3684 | strihash(buf->buf), | |
3685 | buf->buf)) | |
3686 | break; | |
3687 | } | |
3688 | ||
3689 | label = buf->buf; | |
3690 | } | |
3691 | ||
3692 | FLEX_ALLOC_STR(labels_entry, label, label); | |
3693 | hashmap_entry_init(labels_entry, strihash(label)); | |
3694 | hashmap_add(&state->labels, labels_entry); | |
3695 | ||
3696 | FLEX_ALLOC_STR(string_entry, string, label); | |
3697 | oidcpy(&string_entry->entry.oid, oid); | |
3698 | oidmap_put(&state->commit2label, string_entry); | |
3699 | ||
3700 | return string_entry->string; | |
3701 | } | |
3702 | ||
3703 | static int make_script_with_merges(struct pretty_print_context *pp, | |
3704 | struct rev_info *revs, FILE *out, | |
3705 | unsigned flags) | |
3706 | { | |
3707 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; | |
7543f6f4 | 3708 | int rebase_cousins = flags & TODO_LIST_REBASE_COUSINS; |
1644c73c JS |
3709 | struct strbuf buf = STRBUF_INIT, oneline = STRBUF_INIT; |
3710 | struct strbuf label = STRBUF_INIT; | |
3711 | struct commit_list *commits = NULL, **tail = &commits, *iter; | |
3712 | struct commit_list *tips = NULL, **tips_tail = &tips; | |
3713 | struct commit *commit; | |
3714 | struct oidmap commit2todo = OIDMAP_INIT; | |
3715 | struct string_entry *entry; | |
3716 | struct oidset interesting = OIDSET_INIT, child_seen = OIDSET_INIT, | |
3717 | shown = OIDSET_INIT; | |
3718 | struct label_state state = { OIDMAP_INIT, { NULL }, STRBUF_INIT }; | |
3719 | ||
3720 | int abbr = flags & TODO_LIST_ABBREVIATE_CMDS; | |
3721 | const char *cmd_pick = abbr ? "p" : "pick", | |
3722 | *cmd_label = abbr ? "l" : "label", | |
3723 | *cmd_reset = abbr ? "t" : "reset", | |
3724 | *cmd_merge = abbr ? "m" : "merge"; | |
3725 | ||
3726 | oidmap_init(&commit2todo, 0); | |
3727 | oidmap_init(&state.commit2label, 0); | |
3728 | hashmap_init(&state.labels, (hashmap_cmp_fn) labels_cmp, NULL, 0); | |
3729 | strbuf_init(&state.buf, 32); | |
3730 | ||
3731 | if (revs->cmdline.nr && (revs->cmdline.rev[0].flags & BOTTOM)) { | |
3732 | struct object_id *oid = &revs->cmdline.rev[0].item->oid; | |
3733 | FLEX_ALLOC_STR(entry, string, "onto"); | |
3734 | oidcpy(&entry->entry.oid, oid); | |
3735 | oidmap_put(&state.commit2label, entry); | |
3736 | } | |
3737 | ||
3738 | /* | |
3739 | * First phase: | |
3740 | * - get onelines for all commits | |
3741 | * - gather all branch tips (i.e. 2nd or later parents of merges) | |
3742 | * - label all branch tips | |
3743 | */ | |
3744 | while ((commit = get_revision(revs))) { | |
3745 | struct commit_list *to_merge; | |
3746 | int is_octopus; | |
3747 | const char *p1, *p2; | |
3748 | struct object_id *oid; | |
3749 | int is_empty; | |
3750 | ||
3751 | tail = &commit_list_insert(commit, tail)->next; | |
3752 | oidset_insert(&interesting, &commit->object.oid); | |
3753 | ||
3754 | is_empty = is_original_commit_empty(commit); | |
3755 | if (!is_empty && (commit->object.flags & PATCHSAME)) | |
3756 | continue; | |
3757 | ||
3758 | strbuf_reset(&oneline); | |
3759 | pretty_print_commit(pp, commit, &oneline); | |
3760 | ||
3761 | to_merge = commit->parents ? commit->parents->next : NULL; | |
3762 | if (!to_merge) { | |
3763 | /* non-merge commit: easy case */ | |
3764 | strbuf_reset(&buf); | |
3765 | if (!keep_empty && is_empty) | |
3766 | strbuf_addf(&buf, "%c ", comment_line_char); | |
3767 | strbuf_addf(&buf, "%s %s %s", cmd_pick, | |
3768 | oid_to_hex(&commit->object.oid), | |
3769 | oneline.buf); | |
3770 | ||
3771 | FLEX_ALLOC_STR(entry, string, buf.buf); | |
3772 | oidcpy(&entry->entry.oid, &commit->object.oid); | |
3773 | oidmap_put(&commit2todo, entry); | |
3774 | ||
3775 | continue; | |
3776 | } | |
3777 | ||
3778 | is_octopus = to_merge && to_merge->next; | |
3779 | ||
3780 | if (is_octopus) | |
3781 | BUG("Octopus merges not yet supported"); | |
3782 | ||
3783 | /* Create a label */ | |
3784 | strbuf_reset(&label); | |
3785 | if (skip_prefix(oneline.buf, "Merge ", &p1) && | |
3786 | (p1 = strchr(p1, '\'')) && | |
3787 | (p2 = strchr(++p1, '\''))) | |
3788 | strbuf_add(&label, p1, p2 - p1); | |
3789 | else if (skip_prefix(oneline.buf, "Merge pull request ", | |
3790 | &p1) && | |
3791 | (p1 = strstr(p1, " from "))) | |
3792 | strbuf_addstr(&label, p1 + strlen(" from ")); | |
3793 | else | |
3794 | strbuf_addbuf(&label, &oneline); | |
3795 | ||
3796 | for (p1 = label.buf; *p1; p1++) | |
3797 | if (isspace(*p1)) | |
3798 | *(char *)p1 = '-'; | |
3799 | ||
3800 | strbuf_reset(&buf); | |
3801 | strbuf_addf(&buf, "%s -C %s", | |
3802 | cmd_merge, oid_to_hex(&commit->object.oid)); | |
3803 | ||
3804 | /* label the tip of merged branch */ | |
3805 | oid = &to_merge->item->object.oid; | |
3806 | strbuf_addch(&buf, ' '); | |
3807 | ||
3808 | if (!oidset_contains(&interesting, oid)) | |
3809 | strbuf_addstr(&buf, label_oid(oid, NULL, &state)); | |
3810 | else { | |
3811 | tips_tail = &commit_list_insert(to_merge->item, | |
3812 | tips_tail)->next; | |
3813 | ||
3814 | strbuf_addstr(&buf, label_oid(oid, label.buf, &state)); | |
3815 | } | |
3816 | strbuf_addf(&buf, " # %s", oneline.buf); | |
3817 | ||
3818 | FLEX_ALLOC_STR(entry, string, buf.buf); | |
3819 | oidcpy(&entry->entry.oid, &commit->object.oid); | |
3820 | oidmap_put(&commit2todo, entry); | |
3821 | } | |
3822 | ||
3823 | /* | |
3824 | * Second phase: | |
3825 | * - label branch points | |
3826 | * - add HEAD to the branch tips | |
3827 | */ | |
3828 | for (iter = commits; iter; iter = iter->next) { | |
3829 | struct commit_list *parent = iter->item->parents; | |
3830 | for (; parent; parent = parent->next) { | |
3831 | struct object_id *oid = &parent->item->object.oid; | |
3832 | if (!oidset_contains(&interesting, oid)) | |
3833 | continue; | |
3834 | if (!oidset_contains(&child_seen, oid)) | |
3835 | oidset_insert(&child_seen, oid); | |
3836 | else | |
3837 | label_oid(oid, "branch-point", &state); | |
3838 | } | |
3839 | ||
3840 | /* Add HEAD as implict "tip of branch" */ | |
3841 | if (!iter->next) | |
3842 | tips_tail = &commit_list_insert(iter->item, | |
3843 | tips_tail)->next; | |
3844 | } | |
3845 | ||
3846 | /* | |
3847 | * Third phase: output the todo list. This is a bit tricky, as we | |
3848 | * want to avoid jumping back and forth between revisions. To | |
3849 | * accomplish that goal, we walk backwards from the branch tips, | |
3850 | * gathering commits not yet shown, reversing the list on the fly, | |
3851 | * then outputting that list (labeling revisions as needed). | |
3852 | */ | |
3853 | fprintf(out, "%s onto\n", cmd_label); | |
3854 | for (iter = tips; iter; iter = iter->next) { | |
3855 | struct commit_list *list = NULL, *iter2; | |
3856 | ||
3857 | commit = iter->item; | |
3858 | if (oidset_contains(&shown, &commit->object.oid)) | |
3859 | continue; | |
3860 | entry = oidmap_get(&state.commit2label, &commit->object.oid); | |
3861 | ||
3862 | if (entry) | |
3863 | fprintf(out, "\n# Branch %s\n", entry->string); | |
3864 | else | |
3865 | fprintf(out, "\n"); | |
3866 | ||
3867 | while (oidset_contains(&interesting, &commit->object.oid) && | |
3868 | !oidset_contains(&shown, &commit->object.oid)) { | |
3869 | commit_list_insert(commit, &list); | |
3870 | if (!commit->parents) { | |
3871 | commit = NULL; | |
3872 | break; | |
3873 | } | |
3874 | commit = commit->parents->item; | |
3875 | } | |
3876 | ||
3877 | if (!commit) | |
3878 | fprintf(out, "%s onto\n", cmd_reset); | |
3879 | else { | |
3880 | const char *to = NULL; | |
3881 | ||
3882 | entry = oidmap_get(&state.commit2label, | |
3883 | &commit->object.oid); | |
3884 | if (entry) | |
3885 | to = entry->string; | |
7543f6f4 JS |
3886 | else if (!rebase_cousins) |
3887 | to = label_oid(&commit->object.oid, NULL, | |
3888 | &state); | |
1644c73c JS |
3889 | |
3890 | if (!to || !strcmp(to, "onto")) | |
3891 | fprintf(out, "%s onto\n", cmd_reset); | |
3892 | else { | |
3893 | strbuf_reset(&oneline); | |
3894 | pretty_print_commit(pp, commit, &oneline); | |
3895 | fprintf(out, "%s %s # %s\n", | |
3896 | cmd_reset, to, oneline.buf); | |
3897 | } | |
3898 | } | |
3899 | ||
3900 | for (iter2 = list; iter2; iter2 = iter2->next) { | |
3901 | struct object_id *oid = &iter2->item->object.oid; | |
3902 | entry = oidmap_get(&commit2todo, oid); | |
3903 | /* only show if not already upstream */ | |
3904 | if (entry) | |
3905 | fprintf(out, "%s\n", entry->string); | |
3906 | entry = oidmap_get(&state.commit2label, oid); | |
3907 | if (entry) | |
3908 | fprintf(out, "%s %s\n", | |
3909 | cmd_label, entry->string); | |
3910 | oidset_insert(&shown, oid); | |
3911 | } | |
3912 | ||
3913 | free_commit_list(list); | |
3914 | } | |
3915 | ||
3916 | free_commit_list(commits); | |
3917 | free_commit_list(tips); | |
3918 | ||
3919 | strbuf_release(&label); | |
3920 | strbuf_release(&oneline); | |
3921 | strbuf_release(&buf); | |
3922 | ||
3923 | oidmap_free(&commit2todo, 1); | |
3924 | oidmap_free(&state.commit2label, 1); | |
3925 | hashmap_free(&state.labels, 1); | |
3926 | strbuf_release(&state.buf); | |
3927 | ||
3928 | return 0; | |
3929 | } | |
3930 | ||
313a48ea LB |
3931 | int sequencer_make_script(FILE *out, int argc, const char **argv, |
3932 | unsigned flags) | |
62db5247 JS |
3933 | { |
3934 | char *format = NULL; | |
3935 | struct pretty_print_context pp = {0}; | |
3936 | struct strbuf buf = STRBUF_INIT; | |
3937 | struct rev_info revs; | |
3938 | struct commit *commit; | |
313a48ea | 3939 | int keep_empty = flags & TODO_LIST_KEEP_EMPTY; |
d8ae6c84 | 3940 | const char *insn = flags & TODO_LIST_ABBREVIATE_CMDS ? "p" : "pick"; |
1644c73c | 3941 | int rebase_merges = flags & TODO_LIST_REBASE_MERGES; |
62db5247 JS |
3942 | |
3943 | init_revisions(&revs, NULL); | |
3944 | revs.verbose_header = 1; | |
1644c73c JS |
3945 | if (!rebase_merges) |
3946 | revs.max_parents = 1; | |
76ea2358 | 3947 | revs.cherry_mark = 1; |
62db5247 JS |
3948 | revs.limited = 1; |
3949 | revs.reverse = 1; | |
3950 | revs.right_only = 1; | |
3951 | revs.sort_order = REV_SORT_IN_GRAPH_ORDER; | |
3952 | revs.topo_order = 1; | |
3953 | ||
3954 | revs.pretty_given = 1; | |
3955 | git_config_get_string("rebase.instructionFormat", &format); | |
3956 | if (!format || !*format) { | |
3957 | free(format); | |
3958 | format = xstrdup("%s"); | |
3959 | } | |
3960 | get_commit_format(format, &revs); | |
3961 | free(format); | |
3962 | pp.fmt = revs.commit_format; | |
3963 | pp.output_encoding = get_log_output_encoding(); | |
3964 | ||
3965 | if (setup_revisions(argc, argv, &revs, NULL) > 1) | |
3966 | return error(_("make_script: unhandled options")); | |
3967 | ||
3968 | if (prepare_revision_walk(&revs) < 0) | |
3969 | return error(_("make_script: error preparing revisions")); | |
3970 | ||
1644c73c JS |
3971 | if (rebase_merges) |
3972 | return make_script_with_merges(&pp, &revs, out, flags); | |
3973 | ||
62db5247 | 3974 | while ((commit = get_revision(&revs))) { |
76ea2358 PW |
3975 | int is_empty = is_original_commit_empty(commit); |
3976 | ||
3977 | if (!is_empty && (commit->object.flags & PATCHSAME)) | |
3978 | continue; | |
62db5247 | 3979 | strbuf_reset(&buf); |
76ea2358 | 3980 | if (!keep_empty && is_empty) |
62db5247 | 3981 | strbuf_addf(&buf, "%c ", comment_line_char); |
d8ae6c84 LB |
3982 | strbuf_addf(&buf, "%s %s ", insn, |
3983 | oid_to_hex(&commit->object.oid)); | |
62db5247 JS |
3984 | pretty_print_commit(&pp, commit, &buf); |
3985 | strbuf_addch(&buf, '\n'); | |
3986 | fputs(buf.buf, out); | |
3987 | } | |
3988 | strbuf_release(&buf); | |
3989 | return 0; | |
3990 | } | |
3546c8d9 | 3991 | |
0cce4a27 LB |
3992 | /* |
3993 | * Add commands after pick and (series of) squash/fixup commands | |
3994 | * in the todo list. | |
3995 | */ | |
3996 | int sequencer_add_exec_commands(const char *commands) | |
3546c8d9 JS |
3997 | { |
3998 | const char *todo_file = rebase_path_todo(); | |
3999 | struct todo_list todo_list = TODO_LIST_INIT; | |
0cce4a27 LB |
4000 | struct todo_item *item; |
4001 | struct strbuf *buf = &todo_list.buf; | |
4002 | size_t offset = 0, commands_len = strlen(commands); | |
4003 | int i, first; | |
3546c8d9 | 4004 | |
0cce4a27 | 4005 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
3546c8d9 | 4006 | return error(_("could not read '%s'."), todo_file); |
3546c8d9 | 4007 | |
0cce4a27 | 4008 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) { |
3546c8d9 JS |
4009 | todo_list_release(&todo_list); |
4010 | return error(_("unusable todo list: '%s'"), todo_file); | |
4011 | } | |
4012 | ||
0cce4a27 LB |
4013 | first = 1; |
4014 | /* insert <commands> before every pick except the first one */ | |
4015 | for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) { | |
4016 | if (item->command == TODO_PICK && !first) { | |
4017 | strbuf_insert(buf, item->offset_in_buf + offset, | |
4018 | commands, commands_len); | |
4019 | offset += commands_len; | |
4020 | } | |
4021 | first = 0; | |
4022 | } | |
4023 | ||
4024 | /* append final <commands> */ | |
4025 | strbuf_add(buf, commands, commands_len); | |
4026 | ||
4027 | i = write_message(buf->buf, buf->len, todo_file, 0); | |
4028 | todo_list_release(&todo_list); | |
4029 | return i; | |
4030 | } | |
3546c8d9 | 4031 | |
313a48ea | 4032 | int transform_todos(unsigned flags) |
3546c8d9 JS |
4033 | { |
4034 | const char *todo_file = rebase_path_todo(); | |
4035 | struct todo_list todo_list = TODO_LIST_INIT; | |
8dccc7a6 LB |
4036 | struct strbuf buf = STRBUF_INIT; |
4037 | struct todo_item *item; | |
4038 | int i; | |
3546c8d9 | 4039 | |
8dccc7a6 | 4040 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
3546c8d9 | 4041 | return error(_("could not read '%s'."), todo_file); |
3546c8d9 | 4042 | |
8dccc7a6 | 4043 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) { |
3546c8d9 | 4044 | todo_list_release(&todo_list); |
3546c8d9 JS |
4045 | return error(_("unusable todo list: '%s'"), todo_file); |
4046 | } | |
4047 | ||
8dccc7a6 LB |
4048 | for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) { |
4049 | /* if the item is not a command write it and continue */ | |
4050 | if (item->command >= TODO_COMMENT) { | |
4051 | strbuf_addf(&buf, "%.*s\n", item->arg_len, item->arg); | |
4052 | continue; | |
3546c8d9 | 4053 | } |
8dccc7a6 LB |
4054 | |
4055 | /* add command to the buffer */ | |
d8ae6c84 LB |
4056 | if (flags & TODO_LIST_ABBREVIATE_CMDS) |
4057 | strbuf_addch(&buf, command_to_char(item->command)); | |
4058 | else | |
4059 | strbuf_addstr(&buf, command_to_string(item->command)); | |
8dccc7a6 LB |
4060 | |
4061 | /* add commit id */ | |
4062 | if (item->commit) { | |
313a48ea | 4063 | const char *oid = flags & TODO_LIST_SHORTEN_IDS ? |
8dccc7a6 LB |
4064 | short_commit_name(item->commit) : |
4065 | oid_to_hex(&item->commit->object.oid); | |
4066 | ||
4c68e7dd JS |
4067 | if (item->command == TODO_MERGE) { |
4068 | if (item->flags & TODO_EDIT_MERGE_MSG) | |
4069 | strbuf_addstr(&buf, " -c"); | |
4070 | else | |
4071 | strbuf_addstr(&buf, " -C"); | |
4072 | } | |
4073 | ||
8dccc7a6 | 4074 | strbuf_addf(&buf, " %s", oid); |
3546c8d9 | 4075 | } |
4c68e7dd | 4076 | |
8dccc7a6 | 4077 | /* add all the rest */ |
c7b4d79c JS |
4078 | if (!item->arg_len) |
4079 | strbuf_addch(&buf, '\n'); | |
4080 | else | |
4081 | strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg); | |
3546c8d9 | 4082 | } |
8dccc7a6 LB |
4083 | |
4084 | i = write_message(buf.buf, buf.len, todo_file, 0); | |
3546c8d9 | 4085 | todo_list_release(&todo_list); |
8dccc7a6 | 4086 | return i; |
3546c8d9 | 4087 | } |
94399949 JS |
4088 | |
4089 | enum check_level { | |
4090 | CHECK_IGNORE = 0, CHECK_WARN, CHECK_ERROR | |
4091 | }; | |
4092 | ||
4093 | static enum check_level get_missing_commit_check_level(void) | |
4094 | { | |
4095 | const char *value; | |
4096 | ||
4097 | if (git_config_get_value("rebase.missingcommitscheck", &value) || | |
4098 | !strcasecmp("ignore", value)) | |
4099 | return CHECK_IGNORE; | |
4100 | if (!strcasecmp("warn", value)) | |
4101 | return CHECK_WARN; | |
4102 | if (!strcasecmp("error", value)) | |
4103 | return CHECK_ERROR; | |
dfab1eac | 4104 | warning(_("unrecognized setting %s for option " |
94399949 JS |
4105 | "rebase.missingCommitsCheck. Ignoring."), value); |
4106 | return CHECK_IGNORE; | |
4107 | } | |
4108 | ||
4109 | /* | |
4110 | * Check if the user dropped some commits by mistake | |
4111 | * Behaviour determined by rebase.missingCommitsCheck. | |
4112 | * Check if there is an unrecognized command or a | |
4113 | * bad SHA-1 in a command. | |
4114 | */ | |
4115 | int check_todo_list(void) | |
4116 | { | |
4117 | enum check_level check_level = get_missing_commit_check_level(); | |
4118 | struct strbuf todo_file = STRBUF_INIT; | |
4119 | struct todo_list todo_list = TODO_LIST_INIT; | |
4120 | struct strbuf missing = STRBUF_INIT; | |
87805600 | 4121 | int advise_to_edit_todo = 0, res = 0, i; |
94399949 JS |
4122 | |
4123 | strbuf_addstr(&todo_file, rebase_path_todo()); | |
87805600 RS |
4124 | if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) { |
4125 | res = -1; | |
94399949 JS |
4126 | goto leave_check; |
4127 | } | |
94399949 JS |
4128 | advise_to_edit_todo = res = |
4129 | parse_insn_buffer(todo_list.buf.buf, &todo_list); | |
4130 | ||
4131 | if (res || check_level == CHECK_IGNORE) | |
4132 | goto leave_check; | |
4133 | ||
4134 | /* Mark the commits in git-rebase-todo as seen */ | |
4135 | for (i = 0; i < todo_list.nr; i++) { | |
4136 | struct commit *commit = todo_list.items[i].commit; | |
4137 | if (commit) | |
4138 | commit->util = (void *)1; | |
4139 | } | |
4140 | ||
4141 | todo_list_release(&todo_list); | |
4142 | strbuf_addstr(&todo_file, ".backup"); | |
87805600 RS |
4143 | if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) { |
4144 | res = -1; | |
94399949 JS |
4145 | goto leave_check; |
4146 | } | |
94399949 JS |
4147 | strbuf_release(&todo_file); |
4148 | res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list); | |
4149 | ||
4150 | /* Find commits in git-rebase-todo.backup yet unseen */ | |
4151 | for (i = todo_list.nr - 1; i >= 0; i--) { | |
4152 | struct todo_item *item = todo_list.items + i; | |
4153 | struct commit *commit = item->commit; | |
4154 | if (commit && !commit->util) { | |
4155 | strbuf_addf(&missing, " - %s %.*s\n", | |
4156 | short_commit_name(commit), | |
4157 | item->arg_len, item->arg); | |
4158 | commit->util = (void *)1; | |
4159 | } | |
4160 | } | |
4161 | ||
4162 | /* Warn about missing commits */ | |
4163 | if (!missing.len) | |
4164 | goto leave_check; | |
4165 | ||
4166 | if (check_level == CHECK_ERROR) | |
4167 | advise_to_edit_todo = res = 1; | |
4168 | ||
4169 | fprintf(stderr, | |
4170 | _("Warning: some commits may have been dropped accidentally.\n" | |
4171 | "Dropped commits (newer to older):\n")); | |
4172 | ||
4173 | /* Make the list user-friendly and display */ | |
4174 | fputs(missing.buf, stderr); | |
4175 | strbuf_release(&missing); | |
4176 | ||
4177 | fprintf(stderr, _("To avoid this message, use \"drop\" to " | |
4178 | "explicitly remove a commit.\n\n" | |
4179 | "Use 'git config rebase.missingCommitsCheck' to change " | |
4180 | "the level of warnings.\n" | |
4181 | "The possible behaviours are: ignore, warn, error.\n\n")); | |
4182 | ||
4183 | leave_check: | |
4184 | strbuf_release(&todo_file); | |
4185 | todo_list_release(&todo_list); | |
4186 | ||
4187 | if (advise_to_edit_todo) | |
4188 | fprintf(stderr, | |
4189 | _("You can fix this with 'git rebase --edit-todo' " | |
4190 | "and then run 'git rebase --continue'.\n" | |
4191 | "Or you can abort the rebase with 'git rebase" | |
4192 | " --abort'.\n")); | |
4193 | ||
4194 | return res; | |
4195 | } | |
cdac2b01 | 4196 | |
73646bfd RS |
4197 | static int rewrite_file(const char *path, const char *buf, size_t len) |
4198 | { | |
4199 | int rc = 0; | |
c8cee96e | 4200 | int fd = open(path, O_WRONLY | O_TRUNC); |
73646bfd RS |
4201 | if (fd < 0) |
4202 | return error_errno(_("could not open '%s' for writing"), path); | |
4203 | if (write_in_full(fd, buf, len) < 0) | |
4204 | rc = error_errno(_("could not write to '%s'"), path); | |
9360ec00 SR |
4205 | if (close(fd) && !rc) |
4206 | rc = error_errno(_("could not close '%s'"), path); | |
73646bfd RS |
4207 | return rc; |
4208 | } | |
4209 | ||
cdac2b01 JS |
4210 | /* skip picking commits whose parents are unchanged */ |
4211 | int skip_unnecessary_picks(void) | |
4212 | { | |
4213 | const char *todo_file = rebase_path_todo(); | |
4214 | struct strbuf buf = STRBUF_INIT; | |
4215 | struct todo_list todo_list = TODO_LIST_INIT; | |
4216 | struct object_id onto_oid, *oid = &onto_oid, *parent_oid; | |
4217 | int fd, i; | |
4218 | ||
4219 | if (!read_oneliner(&buf, rebase_path_onto(), 0)) | |
4220 | return error(_("could not read 'onto'")); | |
4221 | if (get_oid(buf.buf, &onto_oid)) { | |
4222 | strbuf_release(&buf); | |
4223 | return error(_("need a HEAD to fixup")); | |
4224 | } | |
4225 | strbuf_release(&buf); | |
4226 | ||
87805600 RS |
4227 | if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0) |
4228 | return -1; | |
cdac2b01 JS |
4229 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) { |
4230 | todo_list_release(&todo_list); | |
4231 | return -1; | |
4232 | } | |
4233 | ||
4234 | for (i = 0; i < todo_list.nr; i++) { | |
4235 | struct todo_item *item = todo_list.items + i; | |
4236 | ||
4237 | if (item->command >= TODO_NOOP) | |
4238 | continue; | |
4239 | if (item->command != TODO_PICK) | |
4240 | break; | |
4241 | if (parse_commit(item->commit)) { | |
4242 | todo_list_release(&todo_list); | |
4243 | return error(_("could not parse commit '%s'"), | |
4244 | oid_to_hex(&item->commit->object.oid)); | |
4245 | } | |
4246 | if (!item->commit->parents) | |
4247 | break; /* root commit */ | |
4248 | if (item->commit->parents->next) | |
4249 | break; /* merge commit */ | |
4250 | parent_oid = &item->commit->parents->item->object.oid; | |
4251 | if (hashcmp(parent_oid->hash, oid->hash)) | |
4252 | break; | |
4253 | oid = &item->commit->object.oid; | |
4254 | } | |
4255 | if (i > 0) { | |
a01c2a5f | 4256 | int offset = get_item_line_offset(&todo_list, i); |
cdac2b01 JS |
4257 | const char *done_path = rebase_path_done(); |
4258 | ||
4259 | fd = open(done_path, O_CREAT | O_WRONLY | O_APPEND, 0666); | |
4260 | if (fd < 0) { | |
4261 | error_errno(_("could not open '%s' for writing"), | |
4262 | done_path); | |
4263 | todo_list_release(&todo_list); | |
4264 | return -1; | |
4265 | } | |
4266 | if (write_in_full(fd, todo_list.buf.buf, offset) < 0) { | |
4267 | error_errno(_("could not write to '%s'"), done_path); | |
4268 | todo_list_release(&todo_list); | |
4269 | close(fd); | |
4270 | return -1; | |
4271 | } | |
4272 | close(fd); | |
4273 | ||
73646bfd RS |
4274 | if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset, |
4275 | todo_list.buf.len - offset) < 0) { | |
cdac2b01 | 4276 | todo_list_release(&todo_list); |
cdac2b01 JS |
4277 | return -1; |
4278 | } | |
cdac2b01 JS |
4279 | |
4280 | todo_list.current = i; | |
4281 | if (is_fixup(peek_command(&todo_list, 0))) | |
4282 | record_in_rewritten(oid, peek_command(&todo_list, 0)); | |
4283 | } | |
4284 | ||
4285 | todo_list_release(&todo_list); | |
4286 | printf("%s\n", oid_to_hex(oid)); | |
4287 | ||
4288 | return 0; | |
4289 | } | |
c44a4c65 JS |
4290 | |
4291 | struct subject2item_entry { | |
4292 | struct hashmap_entry entry; | |
4293 | int i; | |
4294 | char subject[FLEX_ARRAY]; | |
4295 | }; | |
4296 | ||
4297 | static int subject2item_cmp(const void *fndata, | |
4298 | const struct subject2item_entry *a, | |
4299 | const struct subject2item_entry *b, const void *key) | |
4300 | { | |
4301 | return key ? strcmp(a->subject, key) : strcmp(a->subject, b->subject); | |
4302 | } | |
4303 | ||
4304 | /* | |
4305 | * Rearrange the todo list that has both "pick commit-id msg" and "pick | |
4306 | * commit-id fixup!/squash! msg" in it so that the latter is put immediately | |
4307 | * after the former, and change "pick" to "fixup"/"squash". | |
4308 | * | |
4309 | * Note that if the config has specified a custom instruction format, each log | |
4310 | * message will have to be retrieved from the commit (as the oneline in the | |
4311 | * script cannot be trusted) in order to normalize the autosquash arrangement. | |
4312 | */ | |
4313 | int rearrange_squash(void) | |
4314 | { | |
4315 | const char *todo_file = rebase_path_todo(); | |
4316 | struct todo_list todo_list = TODO_LIST_INIT; | |
4317 | struct hashmap subject2item; | |
87805600 | 4318 | int res = 0, rearranged = 0, *next, *tail, i; |
c44a4c65 JS |
4319 | char **subjects; |
4320 | ||
87805600 RS |
4321 | if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0) |
4322 | return -1; | |
c44a4c65 JS |
4323 | if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) { |
4324 | todo_list_release(&todo_list); | |
4325 | return -1; | |
4326 | } | |
4327 | ||
4328 | /* | |
4329 | * The hashmap maps onelines to the respective todo list index. | |
4330 | * | |
4331 | * If any items need to be rearranged, the next[i] value will indicate | |
4332 | * which item was moved directly after the i'th. | |
4333 | * | |
4334 | * In that case, last[i] will indicate the index of the latest item to | |
4335 | * be moved to appear after the i'th. | |
4336 | */ | |
4337 | hashmap_init(&subject2item, (hashmap_cmp_fn) subject2item_cmp, | |
4338 | NULL, todo_list.nr); | |
4339 | ALLOC_ARRAY(next, todo_list.nr); | |
4340 | ALLOC_ARRAY(tail, todo_list.nr); | |
4341 | ALLOC_ARRAY(subjects, todo_list.nr); | |
4342 | for (i = 0; i < todo_list.nr; i++) { | |
4343 | struct strbuf buf = STRBUF_INIT; | |
4344 | struct todo_item *item = todo_list.items + i; | |
4345 | const char *commit_buffer, *subject, *p; | |
4346 | size_t subject_len; | |
4347 | int i2 = -1; | |
4348 | struct subject2item_entry *entry; | |
4349 | ||
4350 | next[i] = tail[i] = -1; | |
2f6b1d13 | 4351 | if (!item->commit || item->command == TODO_DROP) { |
c44a4c65 JS |
4352 | subjects[i] = NULL; |
4353 | continue; | |
4354 | } | |
4355 | ||
4356 | if (is_fixup(item->command)) { | |
4357 | todo_list_release(&todo_list); | |
4358 | return error(_("the script was already rearranged.")); | |
4359 | } | |
4360 | ||
4361 | item->commit->util = item; | |
4362 | ||
4363 | parse_commit(item->commit); | |
4364 | commit_buffer = get_commit_buffer(item->commit, NULL); | |
4365 | find_commit_subject(commit_buffer, &subject); | |
4366 | format_subject(&buf, subject, " "); | |
4367 | subject = subjects[i] = strbuf_detach(&buf, &subject_len); | |
4368 | unuse_commit_buffer(item->commit, commit_buffer); | |
4369 | if ((skip_prefix(subject, "fixup! ", &p) || | |
4370 | skip_prefix(subject, "squash! ", &p))) { | |
4371 | struct commit *commit2; | |
4372 | ||
4373 | for (;;) { | |
4374 | while (isspace(*p)) | |
4375 | p++; | |
4376 | if (!skip_prefix(p, "fixup! ", &p) && | |
4377 | !skip_prefix(p, "squash! ", &p)) | |
4378 | break; | |
4379 | } | |
4380 | ||
4381 | if ((entry = hashmap_get_from_hash(&subject2item, | |
4382 | strhash(p), p))) | |
4383 | /* found by title */ | |
4384 | i2 = entry->i; | |
4385 | else if (!strchr(p, ' ') && | |
4386 | (commit2 = | |
4387 | lookup_commit_reference_by_name(p)) && | |
4388 | commit2->util) | |
4389 | /* found by commit name */ | |
4390 | i2 = (struct todo_item *)commit2->util | |
4391 | - todo_list.items; | |
4392 | else { | |
4393 | /* copy can be a prefix of the commit subject */ | |
4394 | for (i2 = 0; i2 < i; i2++) | |
4395 | if (subjects[i2] && | |
4396 | starts_with(subjects[i2], p)) | |
4397 | break; | |
4398 | if (i2 == i) | |
4399 | i2 = -1; | |
4400 | } | |
4401 | } | |
4402 | if (i2 >= 0) { | |
4403 | rearranged = 1; | |
4404 | todo_list.items[i].command = | |
4405 | starts_with(subject, "fixup!") ? | |
4406 | TODO_FIXUP : TODO_SQUASH; | |
4407 | if (next[i2] < 0) | |
4408 | next[i2] = i; | |
4409 | else | |
4410 | next[tail[i2]] = i; | |
4411 | tail[i2] = i; | |
4412 | } else if (!hashmap_get_from_hash(&subject2item, | |
4413 | strhash(subject), subject)) { | |
4414 | FLEX_ALLOC_MEM(entry, subject, subject, subject_len); | |
4415 | entry->i = i; | |
4416 | hashmap_entry_init(entry, strhash(entry->subject)); | |
4417 | hashmap_put(&subject2item, entry); | |
4418 | } | |
4419 | } | |
4420 | ||
4421 | if (rearranged) { | |
4422 | struct strbuf buf = STRBUF_INIT; | |
4423 | ||
4424 | for (i = 0; i < todo_list.nr; i++) { | |
4425 | enum todo_command command = todo_list.items[i].command; | |
4426 | int cur = i; | |
4427 | ||
4428 | /* | |
4429 | * Initially, all commands are 'pick's. If it is a | |
4430 | * fixup or a squash now, we have rearranged it. | |
4431 | */ | |
4432 | if (is_fixup(command)) | |
4433 | continue; | |
4434 | ||
4435 | while (cur >= 0) { | |
a01c2a5f JS |
4436 | const char *bol = |
4437 | get_item_line(&todo_list, cur); | |
4438 | const char *eol = | |
4439 | get_item_line(&todo_list, cur + 1); | |
c44a4c65 JS |
4440 | |
4441 | /* replace 'pick', by 'fixup' or 'squash' */ | |
4442 | command = todo_list.items[cur].command; | |
4443 | if (is_fixup(command)) { | |
4444 | strbuf_addstr(&buf, | |
4445 | todo_command_info[command].str); | |
4446 | bol += strcspn(bol, " \t"); | |
4447 | } | |
4448 | ||
4449 | strbuf_add(&buf, bol, eol - bol); | |
4450 | ||
4451 | cur = next[cur]; | |
4452 | } | |
4453 | } | |
4454 | ||
73646bfd | 4455 | res = rewrite_file(todo_file, buf.buf, buf.len); |
c44a4c65 JS |
4456 | strbuf_release(&buf); |
4457 | } | |
4458 | ||
4459 | free(next); | |
4460 | free(tail); | |
4461 | for (i = 0; i < todo_list.nr; i++) | |
4462 | free(subjects[i]); | |
4463 | free(subjects); | |
4464 | hashmap_free(&subject2item, 1); | |
4465 | todo_list_release(&todo_list); | |
4466 | ||
4467 | return res; | |
4468 | } |