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