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