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