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