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