]>
Commit | Line | Data |
---|---|---|
26ae337b | 1 | #include "cache.h" |
697cc8ef | 2 | #include "lockfile.h" |
26ae337b | 3 | #include "sequencer.h" |
26ae337b | 4 | #include "dir.h" |
043a4492 RR |
5 | #include "object.h" |
6 | #include "commit.h" | |
7 | #include "tag.h" | |
8 | #include "run-command.h" | |
9 | #include "exec_cmd.h" | |
10 | #include "utf8.h" | |
11 | #include "cache-tree.h" | |
12 | #include "diff.h" | |
13 | #include "revision.h" | |
14 | #include "rerere.h" | |
15 | #include "merge-recursive.h" | |
16 | #include "refs.h" | |
b27cfb0d | 17 | #include "argv-array.h" |
a1c75762 | 18 | #include "quote.h" |
967dfd4d | 19 | #include "trailer.h" |
043a4492 RR |
20 | |
21 | #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION" | |
26ae337b | 22 | |
5ed75e2a | 23 | const char sign_off_header[] = "Signed-off-by: "; |
cd650a4e | 24 | static const char cherry_picked_prefix[] = "(cherry picked from commit "; |
5ed75e2a | 25 | |
8a2a0f53 JS |
26 | GIT_PATH_FUNC(git_path_seq_dir, "sequencer") |
27 | ||
28 | static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") | |
29 | static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts") | |
30 | static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") | |
f932729c | 31 | |
b5a67045 JS |
32 | /* |
33 | * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and | |
34 | * GIT_AUTHOR_DATE that will be used for the commit that is currently | |
35 | * being rebased. | |
36 | */ | |
37 | static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script") | |
a1c75762 JS |
38 | /* |
39 | * The following files are written by git-rebase just after parsing the | |
40 | * command-line (and are only consumed, not modified, by the sequencer). | |
41 | */ | |
42 | static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt") | |
b5a67045 JS |
43 | |
44 | /* We will introduce the 'interactive rebase' mode later */ | |
45 | static inline int is_rebase_i(const struct replay_opts *opts) | |
46 | { | |
47 | return 0; | |
48 | } | |
49 | ||
285abf56 JS |
50 | static const char *get_dir(const struct replay_opts *opts) |
51 | { | |
52 | return git_path_seq_dir(); | |
53 | } | |
54 | ||
c0246501 JS |
55 | static const char *get_todo_path(const struct replay_opts *opts) |
56 | { | |
57 | return git_path_todo_file(); | |
58 | } | |
59 | ||
bab4d109 BC |
60 | /* |
61 | * Returns 0 for non-conforming footer | |
62 | * Returns 1 for conforming footer | |
63 | * Returns 2 when sob exists within conforming footer | |
64 | * Returns 3 when sob exists within conforming footer as last entry | |
65 | */ | |
66 | static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, | |
67 | int ignore_footer) | |
b971e04f | 68 | { |
967dfd4d JT |
69 | struct trailer_info info; |
70 | int i; | |
71 | int found_sob = 0, found_sob_last = 0; | |
b971e04f | 72 | |
967dfd4d | 73 | trailer_info_get(&info, sb->buf); |
b971e04f | 74 | |
967dfd4d | 75 | if (info.trailer_start == info.trailer_end) |
b971e04f BC |
76 | return 0; |
77 | ||
967dfd4d JT |
78 | for (i = 0; i < info.trailer_nr; i++) |
79 | if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { | |
80 | found_sob = 1; | |
81 | if (i == info.trailer_nr - 1) | |
82 | found_sob_last = 1; | |
83 | } | |
b971e04f | 84 | |
967dfd4d | 85 | trailer_info_release(&info); |
bab4d109 | 86 | |
967dfd4d | 87 | if (found_sob_last) |
bab4d109 BC |
88 | return 3; |
89 | if (found_sob) | |
90 | return 2; | |
b971e04f BC |
91 | return 1; |
92 | } | |
5ed75e2a | 93 | |
a1c75762 JS |
94 | static const char *gpg_sign_opt_quoted(struct replay_opts *opts) |
95 | { | |
96 | static struct strbuf buf = STRBUF_INIT; | |
97 | ||
98 | strbuf_reset(&buf); | |
99 | if (opts->gpg_sign) | |
100 | sq_quotef(&buf, "-S%s", opts->gpg_sign); | |
101 | return buf.buf; | |
102 | } | |
103 | ||
2863584f | 104 | int sequencer_remove_state(struct replay_opts *opts) |
26ae337b | 105 | { |
285abf56 | 106 | struct strbuf dir = STRBUF_INIT; |
03a4e260 JS |
107 | int i; |
108 | ||
109 | free(opts->gpg_sign); | |
110 | free(opts->strategy); | |
111 | for (i = 0; i < opts->xopts_nr; i++) | |
112 | free(opts->xopts[i]); | |
113 | free(opts->xopts); | |
26ae337b | 114 | |
285abf56 JS |
115 | strbuf_addf(&dir, "%s", get_dir(opts)); |
116 | remove_dir_recursively(&dir, 0); | |
117 | strbuf_release(&dir); | |
2863584f JS |
118 | |
119 | return 0; | |
26ae337b | 120 | } |
043a4492 RR |
121 | |
122 | static const char *action_name(const struct replay_opts *opts) | |
123 | { | |
c28cbc5e | 124 | return opts->action == REPLAY_REVERT ? N_("revert") : N_("cherry-pick"); |
043a4492 RR |
125 | } |
126 | ||
043a4492 RR |
127 | struct commit_message { |
128 | char *parent_label; | |
7b35eaf8 JK |
129 | char *label; |
130 | char *subject; | |
043a4492 RR |
131 | const char *message; |
132 | }; | |
133 | ||
39755964 JS |
134 | static const char *short_commit_name(struct commit *commit) |
135 | { | |
136 | return find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV); | |
137 | } | |
138 | ||
043a4492 RR |
139 | static int get_message(struct commit *commit, struct commit_message *out) |
140 | { | |
043a4492 | 141 | const char *abbrev, *subject; |
7b35eaf8 | 142 | int subject_len; |
043a4492 | 143 | |
7b35eaf8 | 144 | out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding()); |
39755964 | 145 | abbrev = short_commit_name(commit); |
043a4492 RR |
146 | |
147 | subject_len = find_commit_subject(out->message, &subject); | |
148 | ||
7b35eaf8 JK |
149 | out->subject = xmemdupz(subject, subject_len); |
150 | out->label = xstrfmt("%s... %s", abbrev, out->subject); | |
151 | out->parent_label = xstrfmt("parent of %s", out->label); | |
152 | ||
043a4492 RR |
153 | return 0; |
154 | } | |
155 | ||
d74a4e57 | 156 | static void free_message(struct commit *commit, struct commit_message *msg) |
043a4492 RR |
157 | { |
158 | free(msg->parent_label); | |
7b35eaf8 JK |
159 | free(msg->label); |
160 | free(msg->subject); | |
b66103c3 | 161 | unuse_commit_buffer(commit, msg->message); |
043a4492 RR |
162 | } |
163 | ||
ed727b19 | 164 | static void print_advice(int show_hint, struct replay_opts *opts) |
043a4492 RR |
165 | { |
166 | char *msg = getenv("GIT_CHERRY_PICK_HELP"); | |
167 | ||
168 | if (msg) { | |
169 | fprintf(stderr, "%s\n", msg); | |
170 | /* | |
41ccfdd9 | 171 | * A conflict has occurred but the porcelain |
043a4492 RR |
172 | * (typically rebase --interactive) wants to take care |
173 | * of the commit itself so remove CHERRY_PICK_HEAD | |
174 | */ | |
f932729c | 175 | unlink(git_path_cherry_pick_head()); |
043a4492 RR |
176 | return; |
177 | } | |
178 | ||
ed727b19 PH |
179 | if (show_hint) { |
180 | if (opts->no_commit) | |
181 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
182 | "with 'git add <paths>' or 'git rm <paths>'")); | |
183 | else | |
184 | advise(_("after resolving the conflicts, mark the corrected paths\n" | |
185 | "with 'git add <paths>' or 'git rm <paths>'\n" | |
186 | "and commit the result with 'git commit'")); | |
187 | } | |
043a4492 RR |
188 | } |
189 | ||
f56fffef JS |
190 | static int write_message(const void *buf, size_t len, const char *filename, |
191 | int append_eol) | |
043a4492 RR |
192 | { |
193 | static struct lock_file msg_file; | |
194 | ||
4ef3d8f0 JS |
195 | int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0); |
196 | if (msg_fd < 0) | |
93b3df6f | 197 | return error_errno(_("could not lock '%s'"), filename); |
75871495 | 198 | if (write_in_full(msg_fd, buf, len) < 0) { |
4f66c837 | 199 | rollback_lock_file(&msg_file); |
93b3df6f | 200 | return error_errno(_("could not write to '%s'"), filename); |
4f66c837 | 201 | } |
f56fffef JS |
202 | if (append_eol && write(msg_fd, "\n", 1) < 0) { |
203 | rollback_lock_file(&msg_file); | |
35871806 | 204 | return error_errno(_("could not write eol to '%s'"), filename); |
f56fffef | 205 | } |
4f66c837 JS |
206 | if (commit_lock_file(&msg_file) < 0) { |
207 | rollback_lock_file(&msg_file); | |
93b3df6f | 208 | return error(_("failed to finalize '%s'."), filename); |
4f66c837 | 209 | } |
4ef3d8f0 JS |
210 | |
211 | return 0; | |
043a4492 RR |
212 | } |
213 | ||
1dfc84e9 JS |
214 | /* |
215 | * Reads a file that was presumably written by a shell script, i.e. with an | |
216 | * end-of-line marker that needs to be stripped. | |
217 | * | |
218 | * Note that only the last end-of-line marker is stripped, consistent with the | |
219 | * behavior of "$(cat path)" in a shell script. | |
220 | * | |
221 | * Returns 1 if the file was read, 0 if it could not be read or does not exist. | |
222 | */ | |
223 | static int read_oneliner(struct strbuf *buf, | |
224 | const char *path, int skip_if_empty) | |
225 | { | |
226 | int orig_len = buf->len; | |
227 | ||
228 | if (!file_exists(path)) | |
229 | return 0; | |
230 | ||
231 | if (strbuf_read_file(buf, path, 0) < 0) { | |
232 | warning_errno(_("could not read '%s'"), path); | |
233 | return 0; | |
234 | } | |
235 | ||
236 | if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { | |
237 | if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r') | |
238 | --buf->len; | |
239 | buf->buf[buf->len] = '\0'; | |
240 | } | |
241 | ||
242 | if (skip_if_empty && buf->len == orig_len) | |
243 | return 0; | |
244 | ||
245 | return 1; | |
246 | } | |
247 | ||
043a4492 RR |
248 | static struct tree *empty_tree(void) |
249 | { | |
cba595bd | 250 | return lookup_tree(EMPTY_TREE_SHA1_BIN); |
043a4492 RR |
251 | } |
252 | ||
253 | static int error_dirty_index(struct replay_opts *opts) | |
254 | { | |
255 | if (read_cache_unmerged()) | |
c28cbc5e | 256 | return error_resolve_conflict(_(action_name(opts))); |
043a4492 | 257 | |
93b3df6f | 258 | error(_("your local changes would be overwritten by %s."), |
c28cbc5e | 259 | _(action_name(opts))); |
043a4492 RR |
260 | |
261 | if (advice_commit_before_merge) | |
93b3df6f | 262 | advise(_("commit your changes or stash them to proceed.")); |
043a4492 RR |
263 | return -1; |
264 | } | |
265 | ||
334ae397 | 266 | static int fast_forward_to(const unsigned char *to, const unsigned char *from, |
eb4be1cb | 267 | int unborn, struct replay_opts *opts) |
043a4492 | 268 | { |
d668d16c | 269 | struct ref_transaction *transaction; |
eb4be1cb | 270 | struct strbuf sb = STRBUF_INIT; |
d668d16c | 271 | struct strbuf err = STRBUF_INIT; |
043a4492 RR |
272 | |
273 | read_cache(); | |
db699a8a | 274 | if (checkout_fast_forward(from, to, 1)) |
0e408fc3 | 275 | return -1; /* the callee should have complained already */ |
651ab9f5 | 276 | |
c28cbc5e | 277 | strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts))); |
d668d16c RS |
278 | |
279 | transaction = ref_transaction_begin(&err); | |
280 | if (!transaction || | |
281 | ref_transaction_update(transaction, "HEAD", | |
282 | to, unborn ? null_sha1 : from, | |
1d147bdf | 283 | 0, sb.buf, &err) || |
db7516ab | 284 | ref_transaction_commit(transaction, &err)) { |
d668d16c RS |
285 | ref_transaction_free(transaction); |
286 | error("%s", err.buf); | |
287 | strbuf_release(&sb); | |
288 | strbuf_release(&err); | |
289 | return -1; | |
290 | } | |
651ab9f5 | 291 | |
eb4be1cb | 292 | strbuf_release(&sb); |
d668d16c RS |
293 | strbuf_release(&err); |
294 | ref_transaction_free(transaction); | |
295 | return 0; | |
043a4492 RR |
296 | } |
297 | ||
75c961b7 JH |
298 | void append_conflicts_hint(struct strbuf *msgbuf) |
299 | { | |
300 | int i; | |
301 | ||
261f315b JH |
302 | strbuf_addch(msgbuf, '\n'); |
303 | strbuf_commented_addf(msgbuf, "Conflicts:\n"); | |
75c961b7 JH |
304 | for (i = 0; i < active_nr;) { |
305 | const struct cache_entry *ce = active_cache[i++]; | |
306 | if (ce_stage(ce)) { | |
261f315b | 307 | strbuf_commented_addf(msgbuf, "\t%s\n", ce->name); |
75c961b7 JH |
308 | while (i < active_nr && !strcmp(ce->name, |
309 | active_cache[i]->name)) | |
310 | i++; | |
311 | } | |
312 | } | |
313 | } | |
314 | ||
043a4492 RR |
315 | static int do_recursive_merge(struct commit *base, struct commit *next, |
316 | const char *base_label, const char *next_label, | |
317 | unsigned char *head, struct strbuf *msgbuf, | |
318 | struct replay_opts *opts) | |
319 | { | |
320 | struct merge_options o; | |
321 | struct tree *result, *next_tree, *base_tree, *head_tree; | |
03b86647 | 322 | int clean; |
03a4e260 | 323 | char **xopt; |
043a4492 RR |
324 | static struct lock_file index_lock; |
325 | ||
b3e83cc7 | 326 | hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR); |
043a4492 RR |
327 | |
328 | read_cache(); | |
329 | ||
330 | init_merge_options(&o); | |
331 | o.ancestor = base ? base_label : "(empty tree)"; | |
332 | o.branch1 = "HEAD"; | |
333 | o.branch2 = next ? next_label : "(empty tree)"; | |
334 | ||
335 | head_tree = parse_tree_indirect(head); | |
336 | next_tree = next ? next->tree : empty_tree(); | |
337 | base_tree = base ? base->tree : empty_tree(); | |
338 | ||
339 | for (xopt = opts->xopts; xopt != opts->xopts + opts->xopts_nr; xopt++) | |
340 | parse_merge_opt(&o, *xopt); | |
341 | ||
342 | clean = merge_trees(&o, | |
343 | head_tree, | |
344 | next_tree, base_tree, &result); | |
548009c0 | 345 | strbuf_release(&o.obuf); |
f241ff0d JS |
346 | if (clean < 0) |
347 | return clean; | |
043a4492 RR |
348 | |
349 | if (active_cache_changed && | |
03b86647 | 350 | write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) |
043a4492 | 351 | /* TRANSLATORS: %s will be "revert" or "cherry-pick" */ |
c527b55e | 352 | return error(_("%s: Unable to write new index file"), |
c28cbc5e | 353 | _(action_name(opts))); |
043a4492 RR |
354 | rollback_lock_file(&index_lock); |
355 | ||
5ed75e2a | 356 | if (opts->signoff) |
bab4d109 | 357 | append_signoff(msgbuf, 0, 0); |
5ed75e2a | 358 | |
75c961b7 JH |
359 | if (!clean) |
360 | append_conflicts_hint(msgbuf); | |
043a4492 RR |
361 | |
362 | return !clean; | |
363 | } | |
364 | ||
b27cfb0d NH |
365 | static int is_index_unchanged(void) |
366 | { | |
367 | unsigned char head_sha1[20]; | |
368 | struct commit *head_commit; | |
369 | ||
7695d118 | 370 | if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, head_sha1, NULL)) |
93b3df6f | 371 | return error(_("could not resolve HEAD commit\n")); |
b27cfb0d NH |
372 | |
373 | head_commit = lookup_commit(head_sha1); | |
4b580061 NH |
374 | |
375 | /* | |
376 | * If head_commit is NULL, check_commit, called from | |
377 | * lookup_commit, would have indicated that head_commit is not | |
378 | * a commit object already. parse_commit() will return failure | |
379 | * without further complaints in such a case. Otherwise, if | |
380 | * the commit is invalid, parse_commit() will complain. So | |
381 | * there is nothing for us to say here. Just return failure. | |
382 | */ | |
383 | if (parse_commit(head_commit)) | |
384 | return -1; | |
b27cfb0d NH |
385 | |
386 | if (!active_cache_tree) | |
387 | active_cache_tree = cache_tree(); | |
388 | ||
389 | if (!cache_tree_fully_valid(active_cache_tree)) | |
d0cfc3e8 | 390 | if (cache_tree_update(&the_index, 0)) |
93b3df6f | 391 | return error(_("unable to update cache tree\n")); |
b27cfb0d | 392 | |
ed1c9977 | 393 | return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash); |
b27cfb0d NH |
394 | } |
395 | ||
b5a67045 JS |
396 | /* |
397 | * Read the author-script file into an environment block, ready for use in | |
398 | * run_command(), that can be free()d afterwards. | |
399 | */ | |
400 | static char **read_author_script(void) | |
401 | { | |
402 | struct strbuf script = STRBUF_INIT; | |
403 | int i, count = 0; | |
404 | char *p, *p2, **env; | |
405 | size_t env_size; | |
406 | ||
407 | if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0) | |
408 | return NULL; | |
409 | ||
410 | for (p = script.buf; *p; p++) | |
411 | if (skip_prefix(p, "'\\\\''", (const char **)&p2)) | |
412 | strbuf_splice(&script, p - script.buf, p2 - p, "'", 1); | |
413 | else if (*p == '\'') | |
414 | strbuf_splice(&script, p-- - script.buf, 1, "", 0); | |
415 | else if (*p == '\n') { | |
416 | *p = '\0'; | |
417 | count++; | |
418 | } | |
419 | ||
420 | env_size = (count + 1) * sizeof(*env); | |
421 | strbuf_grow(&script, env_size); | |
422 | memmove(script.buf + env_size, script.buf, script.len); | |
423 | p = script.buf + env_size; | |
424 | env = (char **)strbuf_detach(&script, NULL); | |
425 | ||
426 | for (i = 0; i < count; i++) { | |
427 | env[i] = p; | |
428 | p += strlen(p) + 1; | |
429 | } | |
430 | env[count] = NULL; | |
431 | ||
432 | return env; | |
433 | } | |
434 | ||
791eb870 JS |
435 | static const char staged_changes_advice[] = |
436 | N_("you have staged changes in your working tree\n" | |
437 | "If these changes are meant to be squashed into the previous commit, run:\n" | |
438 | "\n" | |
439 | " git commit --amend %s\n" | |
440 | "\n" | |
441 | "If they are meant to go into a new commit, run:\n" | |
442 | "\n" | |
443 | " git commit %s\n" | |
444 | "\n" | |
445 | "In both cases, once you're done, continue with:\n" | |
446 | "\n" | |
447 | " git rebase --continue\n"); | |
448 | ||
043a4492 RR |
449 | /* |
450 | * If we are cherry-pick, and if the merge did not result in | |
451 | * hand-editing, we will hit this commit and inherit the original | |
452 | * author date and name. | |
b5a67045 | 453 | * |
043a4492 RR |
454 | * If we are revert, or if our cherry-pick results in a hand merge, |
455 | * we had better say that the current user is responsible for that. | |
b5a67045 JS |
456 | * |
457 | * An exception is when run_git_commit() is called during an | |
458 | * interactive rebase: in that case, we will want to retain the | |
459 | * author metadata. | |
043a4492 | 460 | */ |
ac2b0e8f | 461 | static int run_git_commit(const char *defmsg, struct replay_opts *opts, |
0009426d JS |
462 | int allow_empty, int edit, int amend, |
463 | int cleanup_commit_message) | |
043a4492 | 464 | { |
b5a67045 | 465 | char **env = NULL; |
b27cfb0d NH |
466 | struct argv_array array; |
467 | int rc; | |
17d65f03 | 468 | const char *value; |
b27cfb0d | 469 | |
b5a67045 JS |
470 | if (is_rebase_i(opts)) { |
471 | env = read_author_script(); | |
a1c75762 JS |
472 | if (!env) { |
473 | const char *gpg_opt = gpg_sign_opt_quoted(opts); | |
474 | ||
791eb870 JS |
475 | return error(_(staged_changes_advice), |
476 | gpg_opt, gpg_opt); | |
a1c75762 | 477 | } |
b5a67045 JS |
478 | } |
479 | ||
b27cfb0d NH |
480 | argv_array_init(&array); |
481 | argv_array_push(&array, "commit"); | |
482 | argv_array_push(&array, "-n"); | |
043a4492 | 483 | |
9240beda JS |
484 | if (amend) |
485 | argv_array_push(&array, "--amend"); | |
3bdd5522 JK |
486 | if (opts->gpg_sign) |
487 | argv_array_pushf(&array, "-S%s", opts->gpg_sign); | |
043a4492 | 488 | if (opts->signoff) |
b27cfb0d | 489 | argv_array_push(&array, "-s"); |
b5a67045 JS |
490 | if (defmsg) |
491 | argv_array_pushl(&array, "-F", defmsg, NULL); | |
0009426d JS |
492 | if (cleanup_commit_message) |
493 | argv_array_push(&array, "--cleanup=strip"); | |
a1c75762 | 494 | if (edit) |
b5a67045 | 495 | argv_array_push(&array, "-e"); |
0009426d JS |
496 | else if (!cleanup_commit_message && |
497 | !opts->signoff && !opts->record_origin && | |
b5a67045 JS |
498 | git_config_get_value("commit.cleanup", &value)) |
499 | argv_array_push(&array, "--cleanup=verbatim"); | |
b27cfb0d | 500 | |
ac2b0e8f | 501 | if (allow_empty) |
b27cfb0d | 502 | argv_array_push(&array, "--allow-empty"); |
df478b74 | 503 | |
4bee9584 CW |
504 | if (opts->allow_empty_message) |
505 | argv_array_push(&array, "--allow-empty-message"); | |
506 | ||
b5a67045 JS |
507 | rc = run_command_v_opt_cd_env(array.argv, RUN_GIT_CMD, NULL, |
508 | (const char *const *)env); | |
b27cfb0d | 509 | argv_array_clear(&array); |
b5a67045 JS |
510 | free(env); |
511 | ||
b27cfb0d NH |
512 | return rc; |
513 | } | |
514 | ||
515 | static int is_original_commit_empty(struct commit *commit) | |
516 | { | |
517 | const unsigned char *ptree_sha1; | |
518 | ||
519 | if (parse_commit(commit)) | |
93b3df6f | 520 | return error(_("could not parse commit %s\n"), |
f2fd0760 | 521 | oid_to_hex(&commit->object.oid)); |
b27cfb0d NH |
522 | if (commit->parents) { |
523 | struct commit *parent = commit->parents->item; | |
524 | if (parse_commit(parent)) | |
93b3df6f | 525 | return error(_("could not parse parent commit %s\n"), |
f2fd0760 | 526 | oid_to_hex(&parent->object.oid)); |
ed1c9977 | 527 | ptree_sha1 = parent->tree->object.oid.hash; |
b27cfb0d NH |
528 | } else { |
529 | ptree_sha1 = EMPTY_TREE_SHA1_BIN; /* commit is root */ | |
043a4492 | 530 | } |
043a4492 | 531 | |
ed1c9977 | 532 | return !hashcmp(ptree_sha1, commit->tree->object.oid.hash); |
043a4492 RR |
533 | } |
534 | ||
ac2b0e8f JH |
535 | /* |
536 | * Do we run "git commit" with "--allow-empty"? | |
537 | */ | |
538 | static int allow_empty(struct replay_opts *opts, struct commit *commit) | |
539 | { | |
540 | int index_unchanged, empty_commit; | |
541 | ||
542 | /* | |
543 | * Three cases: | |
544 | * | |
545 | * (1) we do not allow empty at all and error out. | |
546 | * | |
547 | * (2) we allow ones that were initially empty, but | |
548 | * forbid the ones that become empty; | |
549 | * | |
550 | * (3) we allow both. | |
551 | */ | |
552 | if (!opts->allow_empty) | |
553 | return 0; /* let "git commit" barf as necessary */ | |
554 | ||
555 | index_unchanged = is_index_unchanged(); | |
556 | if (index_unchanged < 0) | |
557 | return index_unchanged; | |
558 | if (!index_unchanged) | |
559 | return 0; /* we do not have to say --allow-empty */ | |
560 | ||
561 | if (opts->keep_redundant_commits) | |
562 | return 1; | |
563 | ||
564 | empty_commit = is_original_commit_empty(commit); | |
565 | if (empty_commit < 0) | |
566 | return empty_commit; | |
567 | if (!empty_commit) | |
568 | return 0; | |
569 | else | |
570 | return 1; | |
571 | } | |
572 | ||
004fefa7 JS |
573 | enum todo_command { |
574 | TODO_PICK = 0, | |
575 | TODO_REVERT | |
576 | }; | |
577 | ||
578 | static const char *todo_command_strings[] = { | |
579 | "pick", | |
580 | "revert" | |
581 | }; | |
582 | ||
583 | static const char *command_to_string(const enum todo_command command) | |
584 | { | |
2ae38f2a | 585 | if ((size_t)command < ARRAY_SIZE(todo_command_strings)) |
004fefa7 JS |
586 | return todo_command_strings[command]; |
587 | die("Unknown command: %d", command); | |
588 | } | |
589 | ||
590 | ||
591 | static int do_pick_commit(enum todo_command command, struct commit *commit, | |
592 | struct replay_opts *opts) | |
043a4492 RR |
593 | { |
594 | unsigned char head[20]; | |
595 | struct commit *base, *next, *parent; | |
596 | const char *base_label, *next_label; | |
d74a4e57 | 597 | struct commit_message msg = { NULL, NULL, NULL, NULL }; |
043a4492 | 598 | struct strbuf msgbuf = STRBUF_INIT; |
c8d1351d | 599 | int res, unborn = 0, allow; |
043a4492 RR |
600 | |
601 | if (opts->no_commit) { | |
602 | /* | |
603 | * We do not intend to commit immediately. We just want to | |
604 | * merge the differences in, so let's compute the tree | |
605 | * that represents the "current" state for merge-recursive | |
606 | * to work on. | |
607 | */ | |
608 | if (write_cache_as_tree(head, 0, NULL)) | |
93b3df6f | 609 | return error(_("your index file is unmerged.")); |
043a4492 | 610 | } else { |
334ae397 MZ |
611 | unborn = get_sha1("HEAD", head); |
612 | if (unborn) | |
613 | hashcpy(head, EMPTY_TREE_SHA1_BIN); | |
018ec3c8 | 614 | if (index_differs_from(unborn ? EMPTY_TREE_SHA1_HEX : "HEAD", 0, 0)) |
043a4492 RR |
615 | return error_dirty_index(opts); |
616 | } | |
617 | discard_cache(); | |
618 | ||
619 | if (!commit->parents) { | |
620 | parent = NULL; | |
621 | } | |
622 | else if (commit->parents->next) { | |
623 | /* Reverting or cherry-picking a merge commit */ | |
624 | int cnt; | |
625 | struct commit_list *p; | |
626 | ||
627 | if (!opts->mainline) | |
93b3df6f | 628 | return error(_("commit %s is a merge but no -m option was given."), |
f2fd0760 | 629 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
630 | |
631 | for (cnt = 1, p = commit->parents; | |
632 | cnt != opts->mainline && p; | |
633 | cnt++) | |
634 | p = p->next; | |
635 | if (cnt != opts->mainline || !p) | |
93b3df6f | 636 | return error(_("commit %s does not have parent %d"), |
f2fd0760 | 637 | oid_to_hex(&commit->object.oid), opts->mainline); |
043a4492 RR |
638 | parent = p->item; |
639 | } else if (0 < opts->mainline) | |
93b3df6f | 640 | return error(_("mainline was specified but commit %s is not a merge."), |
f2fd0760 | 641 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
642 | else |
643 | parent = commit->parents->item; | |
644 | ||
334ae397 | 645 | if (opts->allow_ff && |
ed1c9977 | 646 | ((parent && !hashcmp(parent->object.oid.hash, head)) || |
334ae397 | 647 | (!parent && unborn))) |
ed1c9977 | 648 | return fast_forward_to(commit->object.oid.hash, head, unborn, opts); |
043a4492 RR |
649 | |
650 | if (parent && parse_commit(parent) < 0) | |
004fefa7 JS |
651 | /* TRANSLATORS: The first %s will be a "todo" command like |
652 | "revert" or "pick", the second %s a SHA1. */ | |
043a4492 | 653 | return error(_("%s: cannot parse parent commit %s"), |
004fefa7 JS |
654 | command_to_string(command), |
655 | oid_to_hex(&parent->object.oid)); | |
043a4492 RR |
656 | |
657 | if (get_message(commit, &msg) != 0) | |
93b3df6f | 658 | return error(_("cannot get commit message for %s"), |
f2fd0760 | 659 | oid_to_hex(&commit->object.oid)); |
043a4492 RR |
660 | |
661 | /* | |
662 | * "commit" is an existing commit. We would want to apply | |
663 | * the difference it introduces since its first parent "prev" | |
664 | * on top of the current HEAD if we are cherry-pick. Or the | |
665 | * reverse of it if we are revert. | |
666 | */ | |
667 | ||
004fefa7 | 668 | if (command == TODO_REVERT) { |
043a4492 RR |
669 | base = commit; |
670 | base_label = msg.label; | |
671 | next = parent; | |
672 | next_label = msg.parent_label; | |
673 | strbuf_addstr(&msgbuf, "Revert \""); | |
674 | strbuf_addstr(&msgbuf, msg.subject); | |
675 | strbuf_addstr(&msgbuf, "\"\n\nThis reverts commit "); | |
f2fd0760 | 676 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
677 | |
678 | if (commit->parents && commit->parents->next) { | |
679 | strbuf_addstr(&msgbuf, ", reversing\nchanges made to "); | |
f2fd0760 | 680 | strbuf_addstr(&msgbuf, oid_to_hex(&parent->object.oid)); |
043a4492 RR |
681 | } |
682 | strbuf_addstr(&msgbuf, ".\n"); | |
683 | } else { | |
684 | const char *p; | |
685 | ||
686 | base = parent; | |
687 | base_label = msg.parent_label; | |
688 | next = commit; | |
689 | next_label = msg.label; | |
690 | ||
691 | /* | |
692 | * Append the commit log message to msgbuf; it starts | |
693 | * after the tree, parent, author, committer | |
694 | * information followed by "\n\n". | |
695 | */ | |
696 | p = strstr(msg.message, "\n\n"); | |
88ef402f JS |
697 | if (p) |
698 | strbuf_addstr(&msgbuf, skip_blank_lines(p + 2)); | |
043a4492 RR |
699 | |
700 | if (opts->record_origin) { | |
bab4d109 | 701 | if (!has_conforming_footer(&msgbuf, NULL, 0)) |
b971e04f | 702 | strbuf_addch(&msgbuf, '\n'); |
cd650a4e | 703 | strbuf_addstr(&msgbuf, cherry_picked_prefix); |
f2fd0760 | 704 | strbuf_addstr(&msgbuf, oid_to_hex(&commit->object.oid)); |
043a4492 RR |
705 | strbuf_addstr(&msgbuf, ")\n"); |
706 | } | |
707 | } | |
708 | ||
004fefa7 | 709 | if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) { |
043a4492 RR |
710 | res = do_recursive_merge(base, next, base_label, next_label, |
711 | head, &msgbuf, opts); | |
f241ff0d JS |
712 | if (res < 0) |
713 | return res; | |
75871495 | 714 | res |= write_message(msgbuf.buf, msgbuf.len, |
f56fffef | 715 | git_path_merge_msg(), 0); |
043a4492 RR |
716 | } else { |
717 | struct commit_list *common = NULL; | |
718 | struct commit_list *remotes = NULL; | |
719 | ||
75871495 | 720 | res = write_message(msgbuf.buf, msgbuf.len, |
f56fffef | 721 | git_path_merge_msg(), 0); |
043a4492 RR |
722 | |
723 | commit_list_insert(base, &common); | |
724 | commit_list_insert(next, &remotes); | |
03a4e260 JS |
725 | res |= try_merge_command(opts->strategy, |
726 | opts->xopts_nr, (const char **)opts->xopts, | |
043a4492 RR |
727 | common, sha1_to_hex(head), remotes); |
728 | free_commit_list(common); | |
729 | free_commit_list(remotes); | |
730 | } | |
452202c7 | 731 | strbuf_release(&msgbuf); |
043a4492 RR |
732 | |
733 | /* | |
734 | * If the merge was clean or if it failed due to conflict, we write | |
735 | * CHERRY_PICK_HEAD for the subsequent invocation of commit to use. | |
736 | * However, if the merge did not even start, then we don't want to | |
737 | * write it at all. | |
738 | */ | |
004fefa7 | 739 | if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) && |
dbfad033 JS |
740 | update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL, |
741 | REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) | |
742 | res = -1; | |
004fefa7 | 743 | if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && |
dbfad033 JS |
744 | update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL, |
745 | REF_NODEREF, UPDATE_REFS_MSG_ON_ERR)) | |
746 | res = -1; | |
043a4492 RR |
747 | |
748 | if (res) { | |
004fefa7 | 749 | error(command == TODO_REVERT |
043a4492 RR |
750 | ? _("could not revert %s... %s") |
751 | : _("could not apply %s... %s"), | |
39755964 | 752 | short_commit_name(commit), msg.subject); |
ed727b19 | 753 | print_advice(res == 1, opts); |
043a4492 | 754 | rerere(opts->allow_rerere_auto); |
c8d1351d | 755 | goto leave; |
043a4492 RR |
756 | } |
757 | ||
c8d1351d | 758 | allow = allow_empty(opts, commit); |
706728a3 FC |
759 | if (allow < 0) { |
760 | res = allow; | |
761 | goto leave; | |
762 | } | |
c8d1351d | 763 | if (!opts->no_commit) |
b5a67045 | 764 | res = run_git_commit(opts->edit ? NULL : git_path_merge_msg(), |
0009426d | 765 | opts, allow, opts->edit, 0, 0); |
c8d1351d FC |
766 | |
767 | leave: | |
d74a4e57 | 768 | free_message(commit, &msg); |
043a4492 RR |
769 | |
770 | return res; | |
771 | } | |
772 | ||
c3e8618c | 773 | static int prepare_revs(struct replay_opts *opts) |
043a4492 | 774 | { |
a73e22e9 MZ |
775 | /* |
776 | * picking (but not reverting) ranges (but not individual revisions) | |
777 | * should be done in reverse | |
778 | */ | |
779 | if (opts->action == REPLAY_PICK && !opts->revs->no_walk) | |
043a4492 RR |
780 | opts->revs->reverse ^= 1; |
781 | ||
782 | if (prepare_revision_walk(opts->revs)) | |
c3e8618c | 783 | return error(_("revision walk setup failed")); |
043a4492 RR |
784 | |
785 | if (!opts->revs->commits) | |
c3e8618c JS |
786 | return error(_("empty commit set passed")); |
787 | return 0; | |
043a4492 RR |
788 | } |
789 | ||
0d9c6dc9 | 790 | static int read_and_refresh_cache(struct replay_opts *opts) |
043a4492 RR |
791 | { |
792 | static struct lock_file index_lock; | |
793 | int index_fd = hold_locked_index(&index_lock, 0); | |
49fb937e JS |
794 | if (read_index_preload(&the_index, NULL) < 0) { |
795 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 796 | return error(_("git %s: failed to read the index"), |
c28cbc5e | 797 | _(action_name(opts))); |
49fb937e | 798 | } |
043a4492 | 799 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); |
33c297aa | 800 | if (the_index.cache_changed && index_fd >= 0) { |
49fb937e JS |
801 | if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) { |
802 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 803 | return error(_("git %s: failed to refresh the index"), |
c28cbc5e | 804 | _(action_name(opts))); |
49fb937e | 805 | } |
043a4492 RR |
806 | } |
807 | rollback_lock_file(&index_lock); | |
0d9c6dc9 | 808 | return 0; |
043a4492 RR |
809 | } |
810 | ||
004fefa7 JS |
811 | struct todo_item { |
812 | enum todo_command command; | |
813 | struct commit *commit; | |
c22f7dfb JS |
814 | const char *arg; |
815 | int arg_len; | |
004fefa7 JS |
816 | size_t offset_in_buf; |
817 | }; | |
818 | ||
819 | struct todo_list { | |
820 | struct strbuf buf; | |
821 | struct todo_item *items; | |
822 | int nr, alloc, current; | |
823 | }; | |
824 | ||
825 | #define TODO_LIST_INIT { STRBUF_INIT } | |
826 | ||
827 | static void todo_list_release(struct todo_list *todo_list) | |
043a4492 | 828 | { |
004fefa7 JS |
829 | strbuf_release(&todo_list->buf); |
830 | free(todo_list->items); | |
831 | todo_list->items = NULL; | |
832 | todo_list->nr = todo_list->alloc = 0; | |
833 | } | |
043a4492 | 834 | |
004fefa7 JS |
835 | static struct todo_item *append_new_todo(struct todo_list *todo_list) |
836 | { | |
837 | ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); | |
838 | return todo_list->items + todo_list->nr++; | |
043a4492 RR |
839 | } |
840 | ||
004fefa7 | 841 | static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) |
043a4492 RR |
842 | { |
843 | unsigned char commit_sha1[20]; | |
043a4492 | 844 | char *end_of_object_name; |
004fefa7 JS |
845 | int i, saved, status, padding; |
846 | ||
8f8550b3 JS |
847 | /* left-trim */ |
848 | bol += strspn(bol, " \t"); | |
849 | ||
004fefa7 JS |
850 | for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++) |
851 | if (skip_prefix(bol, todo_command_strings[i], &bol)) { | |
852 | item->command = i; | |
853 | break; | |
854 | } | |
855 | if (i >= ARRAY_SIZE(todo_command_strings)) | |
856 | return -1; | |
043a4492 RR |
857 | |
858 | /* Eat up extra spaces/ tabs before object name */ | |
859 | padding = strspn(bol, " \t"); | |
860 | if (!padding) | |
004fefa7 | 861 | return -1; |
043a4492 RR |
862 | bol += padding; |
863 | ||
004fefa7 | 864 | end_of_object_name = (char *) bol + strcspn(bol, " \t\n"); |
043a4492 RR |
865 | saved = *end_of_object_name; |
866 | *end_of_object_name = '\0'; | |
867 | status = get_sha1(bol, commit_sha1); | |
868 | *end_of_object_name = saved; | |
869 | ||
c22f7dfb JS |
870 | item->arg = end_of_object_name + strspn(end_of_object_name, " \t"); |
871 | item->arg_len = (int)(eol - item->arg); | |
872 | ||
043a4492 | 873 | if (status < 0) |
004fefa7 | 874 | return -1; |
043a4492 | 875 | |
004fefa7 JS |
876 | item->commit = lookup_commit_reference(commit_sha1); |
877 | return !item->commit; | |
043a4492 RR |
878 | } |
879 | ||
004fefa7 | 880 | static int parse_insn_buffer(char *buf, struct todo_list *todo_list) |
043a4492 | 881 | { |
004fefa7 JS |
882 | struct todo_item *item; |
883 | char *p = buf, *next_p; | |
884 | int i, res = 0; | |
043a4492 | 885 | |
004fefa7 | 886 | for (i = 1; *p; i++, p = next_p) { |
043a4492 | 887 | char *eol = strchrnul(p, '\n'); |
004fefa7 JS |
888 | |
889 | next_p = *eol ? eol + 1 /* skip LF */ : eol; | |
890 | ||
6307041d JS |
891 | if (p != eol && eol[-1] == '\r') |
892 | eol--; /* strip Carriage Return */ | |
893 | ||
004fefa7 JS |
894 | item = append_new_todo(todo_list); |
895 | item->offset_in_buf = p - todo_list->buf.buf; | |
896 | if (parse_insn_line(item, p, eol)) { | |
93b3df6f | 897 | res = error(_("invalid line %d: %.*s"), |
004fefa7 JS |
898 | i, (int)(eol - p), p); |
899 | item->command = -1; | |
900 | } | |
043a4492 | 901 | } |
004fefa7 | 902 | if (!todo_list->nr) |
93b3df6f | 903 | return error(_("no commits parsed.")); |
004fefa7 | 904 | return res; |
043a4492 RR |
905 | } |
906 | ||
004fefa7 | 907 | static int read_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
908 | struct replay_opts *opts) |
909 | { | |
c0246501 | 910 | const char *todo_file = get_todo_path(opts); |
043a4492 RR |
911 | int fd, res; |
912 | ||
004fefa7 | 913 | strbuf_reset(&todo_list->buf); |
c0246501 | 914 | fd = open(todo_file, O_RDONLY); |
043a4492 | 915 | if (fd < 0) |
93b3df6f | 916 | return error_errno(_("could not open '%s'"), todo_file); |
004fefa7 | 917 | if (strbuf_read(&todo_list->buf, fd, 0) < 0) { |
043a4492 | 918 | close(fd); |
93b3df6f | 919 | return error(_("could not read '%s'."), todo_file); |
043a4492 RR |
920 | } |
921 | close(fd); | |
922 | ||
004fefa7 | 923 | res = parse_insn_buffer(todo_list->buf.buf, todo_list); |
2eeaf1b3 | 924 | if (res) |
93b3df6f | 925 | return error(_("unusable instruction sheet: '%s'"), todo_file); |
2eeaf1b3 JS |
926 | |
927 | if (!is_rebase_i(opts)) { | |
004fefa7 JS |
928 | enum todo_command valid = |
929 | opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT; | |
930 | int i; | |
931 | ||
932 | for (i = 0; i < todo_list->nr; i++) | |
933 | if (valid == todo_list->items[i].command) | |
934 | continue; | |
935 | else if (valid == TODO_PICK) | |
93b3df6f | 936 | return error(_("cannot cherry-pick during a revert.")); |
004fefa7 | 937 | else |
93b3df6f | 938 | return error(_("cannot revert during a cherry-pick.")); |
004fefa7 JS |
939 | } |
940 | ||
0ae42a03 | 941 | return 0; |
043a4492 RR |
942 | } |
943 | ||
03a4e260 JS |
944 | static int git_config_string_dup(char **dest, |
945 | const char *var, const char *value) | |
946 | { | |
947 | if (!value) | |
948 | return config_error_nonbool(var); | |
949 | free(*dest); | |
950 | *dest = xstrdup(value); | |
951 | return 0; | |
952 | } | |
953 | ||
043a4492 RR |
954 | static int populate_opts_cb(const char *key, const char *value, void *data) |
955 | { | |
956 | struct replay_opts *opts = data; | |
957 | int error_flag = 1; | |
958 | ||
959 | if (!value) | |
960 | error_flag = 0; | |
961 | else if (!strcmp(key, "options.no-commit")) | |
962 | opts->no_commit = git_config_bool_or_int(key, value, &error_flag); | |
963 | else if (!strcmp(key, "options.edit")) | |
964 | opts->edit = git_config_bool_or_int(key, value, &error_flag); | |
965 | else if (!strcmp(key, "options.signoff")) | |
966 | opts->signoff = git_config_bool_or_int(key, value, &error_flag); | |
967 | else if (!strcmp(key, "options.record-origin")) | |
968 | opts->record_origin = git_config_bool_or_int(key, value, &error_flag); | |
969 | else if (!strcmp(key, "options.allow-ff")) | |
970 | opts->allow_ff = git_config_bool_or_int(key, value, &error_flag); | |
971 | else if (!strcmp(key, "options.mainline")) | |
972 | opts->mainline = git_config_int(key, value); | |
973 | else if (!strcmp(key, "options.strategy")) | |
03a4e260 | 974 | git_config_string_dup(&opts->strategy, key, value); |
3253553e | 975 | else if (!strcmp(key, "options.gpg-sign")) |
03a4e260 | 976 | git_config_string_dup(&opts->gpg_sign, key, value); |
043a4492 RR |
977 | else if (!strcmp(key, "options.strategy-option")) { |
978 | ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc); | |
979 | opts->xopts[opts->xopts_nr++] = xstrdup(value); | |
980 | } else | |
93b3df6f | 981 | return error(_("invalid key: %s"), key); |
043a4492 RR |
982 | |
983 | if (!error_flag) | |
93b3df6f | 984 | return error(_("invalid value for %s: %s"), key, value); |
043a4492 RR |
985 | |
986 | return 0; | |
987 | } | |
988 | ||
5adf9bdc | 989 | static int read_populate_opts(struct replay_opts *opts) |
043a4492 | 990 | { |
a1c75762 JS |
991 | if (is_rebase_i(opts)) { |
992 | struct strbuf buf = STRBUF_INIT; | |
993 | ||
994 | if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) { | |
995 | if (!starts_with(buf.buf, "-S")) | |
996 | strbuf_reset(&buf); | |
997 | else { | |
998 | free(opts->gpg_sign); | |
999 | opts->gpg_sign = xstrdup(buf.buf + 2); | |
1000 | } | |
1001 | } | |
1002 | strbuf_release(&buf); | |
1003 | ||
b5a67045 | 1004 | return 0; |
a1c75762 | 1005 | } |
b5a67045 | 1006 | |
f932729c | 1007 | if (!file_exists(git_path_opts_file())) |
0d00da7b JS |
1008 | return 0; |
1009 | /* | |
1010 | * The function git_parse_source(), called from git_config_from_file(), | |
1011 | * may die() in case of a syntactically incorrect file. We do not care | |
1012 | * about this case, though, because we wrote that file ourselves, so we | |
1013 | * are pretty certain that it is syntactically correct. | |
1014 | */ | |
5adf9bdc | 1015 | if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0) |
93b3df6f | 1016 | return error(_("malformed options sheet: '%s'"), |
0d00da7b JS |
1017 | git_path_opts_file()); |
1018 | return 0; | |
043a4492 RR |
1019 | } |
1020 | ||
004fefa7 | 1021 | static int walk_revs_populate_todo(struct todo_list *todo_list, |
043a4492 RR |
1022 | struct replay_opts *opts) |
1023 | { | |
004fefa7 JS |
1024 | enum todo_command command = opts->action == REPLAY_PICK ? |
1025 | TODO_PICK : TODO_REVERT; | |
1026 | const char *command_string = todo_command_strings[command]; | |
043a4492 | 1027 | struct commit *commit; |
043a4492 | 1028 | |
34b0528b JS |
1029 | if (prepare_revs(opts)) |
1030 | return -1; | |
043a4492 | 1031 | |
004fefa7 JS |
1032 | while ((commit = get_revision(opts->revs))) { |
1033 | struct todo_item *item = append_new_todo(todo_list); | |
1034 | const char *commit_buffer = get_commit_buffer(commit, NULL); | |
1035 | const char *subject; | |
1036 | int subject_len; | |
1037 | ||
1038 | item->command = command; | |
1039 | item->commit = commit; | |
c22f7dfb JS |
1040 | item->arg = NULL; |
1041 | item->arg_len = 0; | |
004fefa7 JS |
1042 | item->offset_in_buf = todo_list->buf.len; |
1043 | subject_len = find_commit_subject(commit_buffer, &subject); | |
1044 | strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string, | |
1045 | short_commit_name(commit), subject_len, subject); | |
1046 | unuse_commit_buffer(commit, commit_buffer); | |
1047 | } | |
34b0528b | 1048 | return 0; |
043a4492 RR |
1049 | } |
1050 | ||
1051 | static int create_seq_dir(void) | |
1052 | { | |
f932729c | 1053 | if (file_exists(git_path_seq_dir())) { |
043a4492 RR |
1054 | error(_("a cherry-pick or revert is already in progress")); |
1055 | advise(_("try \"git cherry-pick (--continue | --quit | --abort)\"")); | |
1056 | return -1; | |
1057 | } | |
f932729c | 1058 | else if (mkdir(git_path_seq_dir(), 0777) < 0) |
93b3df6f | 1059 | return error_errno(_("could not create sequencer directory '%s'"), |
f6e82b0d | 1060 | git_path_seq_dir()); |
043a4492 RR |
1061 | return 0; |
1062 | } | |
1063 | ||
311fd397 | 1064 | static int save_head(const char *head) |
043a4492 | 1065 | { |
043a4492 RR |
1066 | static struct lock_file head_lock; |
1067 | struct strbuf buf = STRBUF_INIT; | |
1068 | int fd; | |
1069 | ||
311fd397 JS |
1070 | fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0); |
1071 | if (fd < 0) { | |
1072 | rollback_lock_file(&head_lock); | |
93b3df6f | 1073 | return error_errno(_("could not lock HEAD")); |
311fd397 | 1074 | } |
043a4492 | 1075 | strbuf_addf(&buf, "%s\n", head); |
311fd397 JS |
1076 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
1077 | rollback_lock_file(&head_lock); | |
93b3df6f | 1078 | return error_errno(_("could not write to '%s'"), |
311fd397 JS |
1079 | git_path_head_file()); |
1080 | } | |
1081 | if (commit_lock_file(&head_lock) < 0) { | |
1082 | rollback_lock_file(&head_lock); | |
93b3df6f | 1083 | return error(_("failed to finalize '%s'."), git_path_head_file()); |
311fd397 JS |
1084 | } |
1085 | return 0; | |
043a4492 RR |
1086 | } |
1087 | ||
1088 | static int reset_for_rollback(const unsigned char *sha1) | |
1089 | { | |
1090 | const char *argv[4]; /* reset --merge <arg> + NULL */ | |
1091 | argv[0] = "reset"; | |
1092 | argv[1] = "--merge"; | |
1093 | argv[2] = sha1_to_hex(sha1); | |
1094 | argv[3] = NULL; | |
1095 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
1096 | } | |
1097 | ||
1098 | static int rollback_single_pick(void) | |
1099 | { | |
1100 | unsigned char head_sha1[20]; | |
1101 | ||
f932729c JK |
1102 | if (!file_exists(git_path_cherry_pick_head()) && |
1103 | !file_exists(git_path_revert_head())) | |
043a4492 | 1104 | return error(_("no cherry-pick or revert in progress")); |
7695d118 | 1105 | if (read_ref_full("HEAD", 0, head_sha1, NULL)) |
043a4492 RR |
1106 | return error(_("cannot resolve HEAD")); |
1107 | if (is_null_sha1(head_sha1)) | |
1108 | return error(_("cannot abort from a branch yet to be born")); | |
1109 | return reset_for_rollback(head_sha1); | |
1110 | } | |
1111 | ||
2863584f | 1112 | int sequencer_rollback(struct replay_opts *opts) |
043a4492 | 1113 | { |
043a4492 RR |
1114 | FILE *f; |
1115 | unsigned char sha1[20]; | |
1116 | struct strbuf buf = STRBUF_INIT; | |
1117 | ||
f932729c | 1118 | f = fopen(git_path_head_file(), "r"); |
043a4492 RR |
1119 | if (!f && errno == ENOENT) { |
1120 | /* | |
1121 | * There is no multiple-cherry-pick in progress. | |
1122 | * If CHERRY_PICK_HEAD or REVERT_HEAD indicates | |
1123 | * a single-cherry-pick in progress, abort that. | |
1124 | */ | |
1125 | return rollback_single_pick(); | |
1126 | } | |
1127 | if (!f) | |
f7ed1953 | 1128 | return error_errno(_("cannot open '%s'"), git_path_head_file()); |
8f309aeb | 1129 | if (strbuf_getline_lf(&buf, f)) { |
f7ed1953 | 1130 | error(_("cannot read '%s': %s"), git_path_head_file(), |
f932729c | 1131 | ferror(f) ? strerror(errno) : _("unexpected end of file")); |
043a4492 RR |
1132 | fclose(f); |
1133 | goto fail; | |
1134 | } | |
1135 | fclose(f); | |
1136 | if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') { | |
1137 | error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"), | |
f932729c | 1138 | git_path_head_file()); |
043a4492 RR |
1139 | goto fail; |
1140 | } | |
0f974e21 MG |
1141 | if (is_null_sha1(sha1)) { |
1142 | error(_("cannot abort from a branch yet to be born")); | |
1143 | goto fail; | |
1144 | } | |
043a4492 RR |
1145 | if (reset_for_rollback(sha1)) |
1146 | goto fail; | |
043a4492 | 1147 | strbuf_release(&buf); |
2863584f | 1148 | return sequencer_remove_state(opts); |
043a4492 RR |
1149 | fail: |
1150 | strbuf_release(&buf); | |
1151 | return -1; | |
1152 | } | |
1153 | ||
004fefa7 | 1154 | static int save_todo(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 1155 | { |
043a4492 | 1156 | static struct lock_file todo_lock; |
004fefa7 JS |
1157 | const char *todo_path = get_todo_path(opts); |
1158 | int next = todo_list->current, offset, fd; | |
043a4492 | 1159 | |
004fefa7 | 1160 | fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); |
221675de | 1161 | if (fd < 0) |
93b3df6f | 1162 | return error_errno(_("could not lock '%s'"), todo_path); |
004fefa7 JS |
1163 | offset = next < todo_list->nr ? |
1164 | todo_list->items[next].offset_in_buf : todo_list->buf.len; | |
1165 | if (write_in_full(fd, todo_list->buf.buf + offset, | |
1166 | todo_list->buf.len - offset) < 0) | |
93b3df6f | 1167 | return error_errno(_("could not write to '%s'"), todo_path); |
004fefa7 | 1168 | if (commit_lock_file(&todo_lock) < 0) |
93b3df6f | 1169 | return error(_("failed to finalize '%s'."), todo_path); |
221675de | 1170 | return 0; |
043a4492 RR |
1171 | } |
1172 | ||
88d5a271 | 1173 | static int save_opts(struct replay_opts *opts) |
043a4492 | 1174 | { |
f932729c | 1175 | const char *opts_file = git_path_opts_file(); |
88d5a271 | 1176 | int res = 0; |
043a4492 RR |
1177 | |
1178 | if (opts->no_commit) | |
88d5a271 | 1179 | res |= git_config_set_in_file_gently(opts_file, "options.no-commit", "true"); |
043a4492 | 1180 | if (opts->edit) |
88d5a271 | 1181 | res |= git_config_set_in_file_gently(opts_file, "options.edit", "true"); |
043a4492 | 1182 | if (opts->signoff) |
88d5a271 | 1183 | res |= git_config_set_in_file_gently(opts_file, "options.signoff", "true"); |
043a4492 | 1184 | if (opts->record_origin) |
88d5a271 | 1185 | res |= git_config_set_in_file_gently(opts_file, "options.record-origin", "true"); |
043a4492 | 1186 | if (opts->allow_ff) |
88d5a271 | 1187 | res |= git_config_set_in_file_gently(opts_file, "options.allow-ff", "true"); |
043a4492 RR |
1188 | if (opts->mainline) { |
1189 | struct strbuf buf = STRBUF_INIT; | |
1190 | strbuf_addf(&buf, "%d", opts->mainline); | |
88d5a271 | 1191 | res |= git_config_set_in_file_gently(opts_file, "options.mainline", buf.buf); |
043a4492 RR |
1192 | strbuf_release(&buf); |
1193 | } | |
1194 | if (opts->strategy) | |
88d5a271 | 1195 | res |= git_config_set_in_file_gently(opts_file, "options.strategy", opts->strategy); |
3253553e | 1196 | if (opts->gpg_sign) |
88d5a271 | 1197 | res |= git_config_set_in_file_gently(opts_file, "options.gpg-sign", opts->gpg_sign); |
043a4492 RR |
1198 | if (opts->xopts) { |
1199 | int i; | |
1200 | for (i = 0; i < opts->xopts_nr; i++) | |
88d5a271 | 1201 | res |= git_config_set_multivar_in_file_gently(opts_file, |
043a4492 RR |
1202 | "options.strategy-option", |
1203 | opts->xopts[i], "^$", 0); | |
1204 | } | |
88d5a271 | 1205 | return res; |
043a4492 RR |
1206 | } |
1207 | ||
004fefa7 | 1208 | static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts) |
043a4492 | 1209 | { |
043a4492 RR |
1210 | int res; |
1211 | ||
1212 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
1213 | if (opts->allow_ff) | |
1214 | assert(!(opts->signoff || opts->no_commit || | |
1215 | opts->record_origin || opts->edit)); | |
0d9c6dc9 JS |
1216 | if (read_and_refresh_cache(opts)) |
1217 | return -1; | |
043a4492 | 1218 | |
004fefa7 JS |
1219 | while (todo_list->current < todo_list->nr) { |
1220 | struct todo_item *item = todo_list->items + todo_list->current; | |
1221 | if (save_todo(todo_list, opts)) | |
221675de | 1222 | return -1; |
004fefa7 JS |
1223 | res = do_pick_commit(item->command, item->commit, opts); |
1224 | todo_list->current++; | |
043a4492 RR |
1225 | if (res) |
1226 | return res; | |
1227 | } | |
1228 | ||
1229 | /* | |
1230 | * Sequence of picks finished successfully; cleanup by | |
1231 | * removing the .git/sequencer directory | |
1232 | */ | |
2863584f | 1233 | return sequencer_remove_state(opts); |
043a4492 RR |
1234 | } |
1235 | ||
1236 | static int continue_single_pick(void) | |
1237 | { | |
1238 | const char *argv[] = { "commit", NULL }; | |
1239 | ||
f932729c JK |
1240 | if (!file_exists(git_path_cherry_pick_head()) && |
1241 | !file_exists(git_path_revert_head())) | |
043a4492 RR |
1242 | return error(_("no cherry-pick or revert in progress")); |
1243 | return run_command_v_opt(argv, RUN_GIT_CMD); | |
1244 | } | |
1245 | ||
2863584f | 1246 | int sequencer_continue(struct replay_opts *opts) |
043a4492 | 1247 | { |
004fefa7 JS |
1248 | struct todo_list todo_list = TODO_LIST_INIT; |
1249 | int res; | |
043a4492 | 1250 | |
2863584f JS |
1251 | if (read_and_refresh_cache(opts)) |
1252 | return -1; | |
1253 | ||
c0246501 | 1254 | if (!file_exists(get_todo_path(opts))) |
043a4492 | 1255 | return continue_single_pick(); |
004fefa7 | 1256 | if (read_populate_opts(opts)) |
0ae42a03 | 1257 | return -1; |
004fefa7 JS |
1258 | if ((res = read_populate_todo(&todo_list, opts))) |
1259 | goto release_todo_list; | |
043a4492 RR |
1260 | |
1261 | /* Verify that the conflict has been resolved */ | |
f932729c JK |
1262 | if (file_exists(git_path_cherry_pick_head()) || |
1263 | file_exists(git_path_revert_head())) { | |
004fefa7 JS |
1264 | res = continue_single_pick(); |
1265 | if (res) | |
1266 | goto release_todo_list; | |
043a4492 | 1267 | } |
65036021 | 1268 | if (index_differs_from("HEAD", 0, 0)) { |
004fefa7 JS |
1269 | res = error_dirty_index(opts); |
1270 | goto release_todo_list; | |
1271 | } | |
1272 | todo_list.current++; | |
1273 | res = pick_commits(&todo_list, opts); | |
1274 | release_todo_list: | |
1275 | todo_list_release(&todo_list); | |
1276 | return res; | |
043a4492 RR |
1277 | } |
1278 | ||
1279 | static int single_pick(struct commit *cmit, struct replay_opts *opts) | |
1280 | { | |
1281 | setenv(GIT_REFLOG_ACTION, action_name(opts), 0); | |
004fefa7 JS |
1282 | return do_pick_commit(opts->action == REPLAY_PICK ? |
1283 | TODO_PICK : TODO_REVERT, cmit, opts); | |
043a4492 RR |
1284 | } |
1285 | ||
1286 | int sequencer_pick_revisions(struct replay_opts *opts) | |
1287 | { | |
004fefa7 | 1288 | struct todo_list todo_list = TODO_LIST_INIT; |
043a4492 | 1289 | unsigned char sha1[20]; |
004fefa7 | 1290 | int i, res; |
043a4492 | 1291 | |
2863584f | 1292 | assert(opts->revs); |
0d9c6dc9 JS |
1293 | if (read_and_refresh_cache(opts)) |
1294 | return -1; | |
043a4492 | 1295 | |
21246dbb MV |
1296 | for (i = 0; i < opts->revs->pending.nr; i++) { |
1297 | unsigned char sha1[20]; | |
1298 | const char *name = opts->revs->pending.objects[i].name; | |
1299 | ||
1300 | /* This happens when using --stdin. */ | |
1301 | if (!strlen(name)) | |
1302 | continue; | |
1303 | ||
1304 | if (!get_sha1(name, sha1)) { | |
7c0b0d8d JH |
1305 | if (!lookup_commit_reference_gently(sha1, 1)) { |
1306 | enum object_type type = sha1_object_info(sha1, NULL); | |
b9b946d4 JS |
1307 | return error(_("%s: can't cherry-pick a %s"), |
1308 | name, typename(type)); | |
7c0b0d8d | 1309 | } |
21246dbb | 1310 | } else |
b9b946d4 | 1311 | return error(_("%s: bad revision"), name); |
21246dbb MV |
1312 | } |
1313 | ||
043a4492 RR |
1314 | /* |
1315 | * If we were called as "git cherry-pick <commit>", just | |
1316 | * cherry-pick/revert it, set CHERRY_PICK_HEAD / | |
1317 | * REVERT_HEAD, and don't touch the sequencer state. | |
1318 | * This means it is possible to cherry-pick in the middle | |
1319 | * of a cherry-pick sequence. | |
1320 | */ | |
1321 | if (opts->revs->cmdline.nr == 1 && | |
1322 | opts->revs->cmdline.rev->whence == REV_CMD_REV && | |
1323 | opts->revs->no_walk && | |
1324 | !opts->revs->cmdline.rev->flags) { | |
1325 | struct commit *cmit; | |
1326 | if (prepare_revision_walk(opts->revs)) | |
b9b946d4 | 1327 | return error(_("revision walk setup failed")); |
043a4492 RR |
1328 | cmit = get_revision(opts->revs); |
1329 | if (!cmit || get_revision(opts->revs)) | |
b9b946d4 | 1330 | return error("BUG: expected exactly one commit from walk"); |
043a4492 RR |
1331 | return single_pick(cmit, opts); |
1332 | } | |
1333 | ||
1334 | /* | |
1335 | * Start a new cherry-pick/ revert sequence; but | |
1336 | * first, make sure that an existing one isn't in | |
1337 | * progress | |
1338 | */ | |
1339 | ||
34b0528b JS |
1340 | if (walk_revs_populate_todo(&todo_list, opts) || |
1341 | create_seq_dir() < 0) | |
043a4492 | 1342 | return -1; |
0f974e21 | 1343 | if (get_sha1("HEAD", sha1) && (opts->action == REPLAY_REVERT)) |
93b3df6f | 1344 | return error(_("can't revert as initial commit")); |
311fd397 JS |
1345 | if (save_head(sha1_to_hex(sha1))) |
1346 | return -1; | |
88d5a271 JS |
1347 | if (save_opts(opts)) |
1348 | return -1; | |
004fefa7 JS |
1349 | res = pick_commits(&todo_list, opts); |
1350 | todo_list_release(&todo_list); | |
1351 | return res; | |
043a4492 | 1352 | } |
5ed75e2a | 1353 | |
bab4d109 | 1354 | void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag) |
5ed75e2a | 1355 | { |
bab4d109 | 1356 | unsigned no_dup_sob = flag & APPEND_SIGNOFF_DEDUP; |
5ed75e2a | 1357 | struct strbuf sob = STRBUF_INIT; |
bab4d109 | 1358 | int has_footer; |
5ed75e2a MV |
1359 | |
1360 | strbuf_addstr(&sob, sign_off_header); | |
1361 | strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"), | |
1362 | getenv("GIT_COMMITTER_EMAIL"))); | |
1363 | strbuf_addch(&sob, '\n'); | |
bab4d109 BC |
1364 | |
1365 | /* | |
1366 | * If the whole message buffer is equal to the sob, pretend that we | |
1367 | * found a conforming footer with a matching sob | |
1368 | */ | |
1369 | if (msgbuf->len - ignore_footer == sob.len && | |
1370 | !strncmp(msgbuf->buf, sob.buf, sob.len)) | |
1371 | has_footer = 3; | |
1372 | else | |
1373 | has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer); | |
1374 | ||
33f2f9ab BC |
1375 | if (!has_footer) { |
1376 | const char *append_newlines = NULL; | |
1377 | size_t len = msgbuf->len - ignore_footer; | |
1378 | ||
8c613fd5 BC |
1379 | if (!len) { |
1380 | /* | |
1381 | * The buffer is completely empty. Leave foom for | |
1382 | * the title and body to be filled in by the user. | |
1383 | */ | |
33f2f9ab | 1384 | append_newlines = "\n\n"; |
8c613fd5 BC |
1385 | } else if (msgbuf->buf[len - 1] != '\n') { |
1386 | /* | |
1387 | * Incomplete line. Complete the line and add a | |
1388 | * blank one so that there is an empty line between | |
1389 | * the message body and the sob. | |
1390 | */ | |
1391 | append_newlines = "\n\n"; | |
1392 | } else if (len == 1) { | |
1393 | /* | |
1394 | * Buffer contains a single newline. Add another | |
1395 | * so that we leave room for the title and body. | |
1396 | */ | |
1397 | append_newlines = "\n"; | |
1398 | } else if (msgbuf->buf[len - 2] != '\n') { | |
1399 | /* | |
1400 | * Buffer ends with a single newline. Add another | |
1401 | * so that there is an empty line between the message | |
1402 | * body and the sob. | |
1403 | */ | |
33f2f9ab | 1404 | append_newlines = "\n"; |
8c613fd5 | 1405 | } /* else, the buffer already ends with two newlines. */ |
33f2f9ab BC |
1406 | |
1407 | if (append_newlines) | |
1408 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
1409 | append_newlines, strlen(append_newlines)); | |
5ed75e2a | 1410 | } |
bab4d109 BC |
1411 | |
1412 | if (has_footer != 3 && (!no_dup_sob || has_footer != 2)) | |
1413 | strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, | |
1414 | sob.buf, sob.len); | |
1415 | ||
5ed75e2a MV |
1416 | strbuf_release(&sob); |
1417 | } |