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