8 #include "run-command.h"
11 #include "cache-tree.h"
15 #include "merge-recursive.h"
17 #include "argv-array.h"
19 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
21 const char sign_off_header
[] = "Signed-off-by: ";
22 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
24 GIT_PATH_FUNC(git_path_seq_dir
, "sequencer")
26 static GIT_PATH_FUNC(git_path_todo_file
, "sequencer/todo")
27 static GIT_PATH_FUNC(git_path_opts_file
, "sequencer/opts")
28 static GIT_PATH_FUNC(git_path_head_file
, "sequencer/head")
30 static const char *get_dir(const struct replay_opts
*opts
)
32 return git_path_seq_dir();
35 static const char *get_todo_path(const struct replay_opts
*opts
)
37 return git_path_todo_file();
40 static int is_rfc2822_line(const char *buf
, int len
)
44 for (i
= 0; i
< len
; i
++) {
48 if (!isalnum(ch
) && ch
!= '-')
55 static int is_cherry_picked_from_line(const char *buf
, int len
)
58 * We only care that it looks roughly like (cherry picked from ...)
60 return len
> strlen(cherry_picked_prefix
) + 1 &&
61 starts_with(buf
, cherry_picked_prefix
) && buf
[len
- 1] == ')';
65 * Returns 0 for non-conforming footer
66 * Returns 1 for conforming footer
67 * Returns 2 when sob exists within conforming footer
68 * Returns 3 when sob exists within conforming footer as last entry
70 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
75 int len
= sb
->len
- ignore_footer
;
76 const char *buf
= sb
->buf
;
79 /* footer must end with newline */
80 if (!len
|| buf
[len
- 1] != '\n')
84 for (i
= len
- 1; i
> 0; i
--) {
86 if (prev
== '\n' && ch
== '\n') /* paragraph break */
91 /* require at least one blank line */
92 if (prev
!= '\n' || buf
[i
] != '\n')
95 /* advance to start of last paragraph */
96 while (i
< len
- 1 && buf
[i
] == '\n')
99 for (; i
< len
; i
= k
) {
102 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
106 found_rfc2822
= is_rfc2822_line(buf
+ i
, k
- i
- 1);
107 if (found_rfc2822
&& sob
&&
108 !strncmp(buf
+ i
, sob
->buf
, sob
->len
))
111 if (!(found_rfc2822
||
112 is_cherry_picked_from_line(buf
+ i
, k
- i
- 1)))
122 static void remove_sequencer_state(const struct replay_opts
*opts
)
124 struct strbuf dir
= STRBUF_INIT
;
127 free(opts
->gpg_sign
);
128 free(opts
->strategy
);
129 for (i
= 0; i
< opts
->xopts_nr
; i
++)
130 free(opts
->xopts
[i
]);
133 strbuf_addf(&dir
, "%s", get_dir(opts
));
134 remove_dir_recursively(&dir
, 0);
135 strbuf_release(&dir
);
138 static const char *action_name(const struct replay_opts
*opts
)
140 return opts
->action
== REPLAY_REVERT
? "revert" : "cherry-pick";
143 struct commit_message
{
150 static const char *short_commit_name(struct commit
*commit
)
152 return find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
);
155 static int get_message(struct commit
*commit
, struct commit_message
*out
)
157 const char *abbrev
, *subject
;
160 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
161 abbrev
= short_commit_name(commit
);
163 subject_len
= find_commit_subject(out
->message
, &subject
);
165 out
->subject
= xmemdupz(subject
, subject_len
);
166 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
167 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
172 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
174 free(msg
->parent_label
);
177 unuse_commit_buffer(commit
, msg
->message
);
180 static void print_advice(int show_hint
, struct replay_opts
*opts
)
182 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
185 fprintf(stderr
, "%s\n", msg
);
187 * A conflict has occurred but the porcelain
188 * (typically rebase --interactive) wants to take care
189 * of the commit itself so remove CHERRY_PICK_HEAD
191 unlink(git_path_cherry_pick_head());
197 advise(_("after resolving the conflicts, mark the corrected paths\n"
198 "with 'git add <paths>' or 'git rm <paths>'"));
200 advise(_("after resolving the conflicts, mark the corrected paths\n"
201 "with 'git add <paths>' or 'git rm <paths>'\n"
202 "and commit the result with 'git commit'"));
206 static int write_message(struct strbuf
*msgbuf
, const char *filename
)
208 static struct lock_file msg_file
;
210 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
212 return error_errno(_("Could not lock '%s'"), filename
);
213 if (write_in_full(msg_fd
, msgbuf
->buf
, msgbuf
->len
) < 0)
214 return error_errno(_("Could not write to %s"), filename
);
215 strbuf_release(msgbuf
);
216 if (commit_lock_file(&msg_file
) < 0)
217 return error(_("Error wrapping up %s."), filename
);
222 static struct tree
*empty_tree(void)
224 return lookup_tree(EMPTY_TREE_SHA1_BIN
);
227 static int error_dirty_index(struct replay_opts
*opts
)
229 if (read_cache_unmerged())
230 return error_resolve_conflict(action_name(opts
));
232 /* Different translation strings for cherry-pick and revert */
233 if (opts
->action
== REPLAY_PICK
)
234 error(_("Your local changes would be overwritten by cherry-pick."));
236 error(_("Your local changes would be overwritten by revert."));
238 if (advice_commit_before_merge
)
239 advise(_("Commit your changes or stash them to proceed."));
243 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
,
244 int unborn
, struct replay_opts
*opts
)
246 struct ref_transaction
*transaction
;
247 struct strbuf sb
= STRBUF_INIT
;
248 struct strbuf err
= STRBUF_INIT
;
251 if (checkout_fast_forward(from
, to
, 1))
252 return -1; /* the callee should have complained already */
254 strbuf_addf(&sb
, _("%s: fast-forward"), action_name(opts
));
256 transaction
= ref_transaction_begin(&err
);
258 ref_transaction_update(transaction
, "HEAD",
259 to
, unborn
? null_sha1
: from
,
261 ref_transaction_commit(transaction
, &err
)) {
262 ref_transaction_free(transaction
);
263 error("%s", err
.buf
);
265 strbuf_release(&err
);
270 strbuf_release(&err
);
271 ref_transaction_free(transaction
);
275 void append_conflicts_hint(struct strbuf
*msgbuf
)
279 strbuf_addch(msgbuf
, '\n');
280 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
281 for (i
= 0; i
< active_nr
;) {
282 const struct cache_entry
*ce
= active_cache
[i
++];
284 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
285 while (i
< active_nr
&& !strcmp(ce
->name
,
286 active_cache
[i
]->name
))
292 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
293 const char *base_label
, const char *next_label
,
294 unsigned char *head
, struct strbuf
*msgbuf
,
295 struct replay_opts
*opts
)
297 struct merge_options o
;
298 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
301 static struct lock_file index_lock
;
303 hold_locked_index(&index_lock
, 1);
307 init_merge_options(&o
);
308 o
.ancestor
= base
? base_label
: "(empty tree)";
310 o
.branch2
= next
? next_label
: "(empty tree)";
312 head_tree
= parse_tree_indirect(head
);
313 next_tree
= next
? next
->tree
: empty_tree();
314 base_tree
= base
? base
->tree
: empty_tree();
316 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
317 parse_merge_opt(&o
, *xopt
);
319 clean
= merge_trees(&o
,
321 next_tree
, base_tree
, &result
);
322 strbuf_release(&o
.obuf
);
326 if (active_cache_changed
&&
327 write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
328 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
329 return error(_("%s: Unable to write new index file"),
331 rollback_lock_file(&index_lock
);
334 append_signoff(msgbuf
, 0, 0);
337 append_conflicts_hint(msgbuf
);
342 static int is_index_unchanged(void)
344 unsigned char head_sha1
[20];
345 struct commit
*head_commit
;
347 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, head_sha1
, NULL
))
348 return error(_("Could not resolve HEAD commit\n"));
350 head_commit
= lookup_commit(head_sha1
);
353 * If head_commit is NULL, check_commit, called from
354 * lookup_commit, would have indicated that head_commit is not
355 * a commit object already. parse_commit() will return failure
356 * without further complaints in such a case. Otherwise, if
357 * the commit is invalid, parse_commit() will complain. So
358 * there is nothing for us to say here. Just return failure.
360 if (parse_commit(head_commit
))
363 if (!active_cache_tree
)
364 active_cache_tree
= cache_tree();
366 if (!cache_tree_fully_valid(active_cache_tree
))
367 if (cache_tree_update(&the_index
, 0))
368 return error(_("Unable to update cache tree\n"));
370 return !hashcmp(active_cache_tree
->sha1
, head_commit
->tree
->object
.oid
.hash
);
374 * If we are cherry-pick, and if the merge did not result in
375 * hand-editing, we will hit this commit and inherit the original
376 * author date and name.
377 * If we are revert, or if our cherry-pick results in a hand merge,
378 * we had better say that the current user is responsible for that.
380 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
383 struct argv_array array
;
387 argv_array_init(&array
);
388 argv_array_push(&array
, "commit");
389 argv_array_push(&array
, "-n");
392 argv_array_pushf(&array
, "-S%s", opts
->gpg_sign
);
394 argv_array_push(&array
, "-s");
396 argv_array_push(&array
, "-F");
397 argv_array_push(&array
, defmsg
);
398 if (!opts
->signoff
&&
399 !opts
->record_origin
&&
400 git_config_get_value("commit.cleanup", &value
))
401 argv_array_push(&array
, "--cleanup=verbatim");
405 argv_array_push(&array
, "--allow-empty");
407 if (opts
->allow_empty_message
)
408 argv_array_push(&array
, "--allow-empty-message");
410 rc
= run_command_v_opt(array
.argv
, RUN_GIT_CMD
);
411 argv_array_clear(&array
);
415 static int is_original_commit_empty(struct commit
*commit
)
417 const unsigned char *ptree_sha1
;
419 if (parse_commit(commit
))
420 return error(_("Could not parse commit %s\n"),
421 oid_to_hex(&commit
->object
.oid
));
422 if (commit
->parents
) {
423 struct commit
*parent
= commit
->parents
->item
;
424 if (parse_commit(parent
))
425 return error(_("Could not parse parent commit %s\n"),
426 oid_to_hex(&parent
->object
.oid
));
427 ptree_sha1
= parent
->tree
->object
.oid
.hash
;
429 ptree_sha1
= EMPTY_TREE_SHA1_BIN
; /* commit is root */
432 return !hashcmp(ptree_sha1
, commit
->tree
->object
.oid
.hash
);
436 * Do we run "git commit" with "--allow-empty"?
438 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
440 int index_unchanged
, empty_commit
;
445 * (1) we do not allow empty at all and error out.
447 * (2) we allow ones that were initially empty, but
448 * forbid the ones that become empty;
452 if (!opts
->allow_empty
)
453 return 0; /* let "git commit" barf as necessary */
455 index_unchanged
= is_index_unchanged();
456 if (index_unchanged
< 0)
457 return index_unchanged
;
458 if (!index_unchanged
)
459 return 0; /* we do not have to say --allow-empty */
461 if (opts
->keep_redundant_commits
)
464 empty_commit
= is_original_commit_empty(commit
);
465 if (empty_commit
< 0)
478 static const char *todo_command_strings
[] = {
483 static const char *command_to_string(const enum todo_command command
)
485 if (command
< ARRAY_SIZE(todo_command_strings
))
486 return todo_command_strings
[command
];
487 die("Unknown command: %d", command
);
491 static int do_pick_commit(enum todo_command command
, struct commit
*commit
,
492 struct replay_opts
*opts
)
494 unsigned char head
[20];
495 struct commit
*base
, *next
, *parent
;
496 const char *base_label
, *next_label
;
497 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
498 struct strbuf msgbuf
= STRBUF_INIT
;
499 int res
, unborn
= 0, allow
;
501 if (opts
->no_commit
) {
503 * We do not intend to commit immediately. We just want to
504 * merge the differences in, so let's compute the tree
505 * that represents the "current" state for merge-recursive
508 if (write_cache_as_tree(head
, 0, NULL
))
509 return error(_("Your index file is unmerged."));
511 unborn
= get_sha1("HEAD", head
);
513 hashcpy(head
, EMPTY_TREE_SHA1_BIN
);
514 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD", 0))
515 return error_dirty_index(opts
);
519 if (!commit
->parents
) {
522 else if (commit
->parents
->next
) {
523 /* Reverting or cherry-picking a merge commit */
525 struct commit_list
*p
;
528 return error(_("Commit %s is a merge but no -m option was given."),
529 oid_to_hex(&commit
->object
.oid
));
531 for (cnt
= 1, p
= commit
->parents
;
532 cnt
!= opts
->mainline
&& p
;
535 if (cnt
!= opts
->mainline
|| !p
)
536 return error(_("Commit %s does not have parent %d"),
537 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
539 } else if (0 < opts
->mainline
)
540 return error(_("Mainline was specified but commit %s is not a merge."),
541 oid_to_hex(&commit
->object
.oid
));
543 parent
= commit
->parents
->item
;
545 if (opts
->allow_ff
&&
546 ((parent
&& !hashcmp(parent
->object
.oid
.hash
, head
)) ||
547 (!parent
&& unborn
)))
548 return fast_forward_to(commit
->object
.oid
.hash
, head
, unborn
, opts
);
550 if (parent
&& parse_commit(parent
) < 0)
551 /* TRANSLATORS: The first %s will be a "todo" command like
552 "revert" or "pick", the second %s a SHA1. */
553 return error(_("%s: cannot parse parent commit %s"),
554 command_to_string(command
),
555 oid_to_hex(&parent
->object
.oid
));
557 if (get_message(commit
, &msg
) != 0)
558 return error(_("Cannot get commit message for %s"),
559 oid_to_hex(&commit
->object
.oid
));
562 * "commit" is an existing commit. We would want to apply
563 * the difference it introduces since its first parent "prev"
564 * on top of the current HEAD if we are cherry-pick. Or the
565 * reverse of it if we are revert.
568 if (command
== TODO_REVERT
) {
570 base_label
= msg
.label
;
572 next_label
= msg
.parent_label
;
573 strbuf_addstr(&msgbuf
, "Revert \"");
574 strbuf_addstr(&msgbuf
, msg
.subject
);
575 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
576 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
578 if (commit
->parents
&& commit
->parents
->next
) {
579 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
580 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
582 strbuf_addstr(&msgbuf
, ".\n");
587 base_label
= msg
.parent_label
;
589 next_label
= msg
.label
;
592 * Append the commit log message to msgbuf; it starts
593 * after the tree, parent, author, committer
594 * information followed by "\n\n".
596 p
= strstr(msg
.message
, "\n\n");
598 strbuf_addstr(&msgbuf
, skip_blank_lines(p
+ 2));
600 if (opts
->record_origin
) {
601 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
602 strbuf_addch(&msgbuf
, '\n');
603 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
604 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
605 strbuf_addstr(&msgbuf
, ")\n");
609 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || command
== TODO_REVERT
) {
610 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
611 head
, &msgbuf
, opts
);
614 res
|= write_message(&msgbuf
, git_path_merge_msg());
616 struct commit_list
*common
= NULL
;
617 struct commit_list
*remotes
= NULL
;
619 res
= write_message(&msgbuf
, git_path_merge_msg());
621 commit_list_insert(base
, &common
);
622 commit_list_insert(next
, &remotes
);
623 res
|= try_merge_command(opts
->strategy
,
624 opts
->xopts_nr
, (const char **)opts
->xopts
,
625 common
, sha1_to_hex(head
), remotes
);
626 free_commit_list(common
);
627 free_commit_list(remotes
);
631 * If the merge was clean or if it failed due to conflict, we write
632 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
633 * However, if the merge did not even start, then we don't want to
636 if (command
== TODO_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
637 update_ref(NULL
, "CHERRY_PICK_HEAD", commit
->object
.oid
.hash
, NULL
,
638 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
640 if (command
== TODO_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
641 update_ref(NULL
, "REVERT_HEAD", commit
->object
.oid
.hash
, NULL
,
642 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
646 error(command
== TODO_REVERT
647 ? _("could not revert %s... %s")
648 : _("could not apply %s... %s"),
649 short_commit_name(commit
), msg
.subject
);
650 print_advice(res
== 1, opts
);
651 rerere(opts
->allow_rerere_auto
);
655 allow
= allow_empty(opts
, commit
);
660 if (!opts
->no_commit
)
661 res
= run_git_commit(git_path_merge_msg(), opts
, allow
);
664 free_message(commit
, &msg
);
669 static int prepare_revs(struct replay_opts
*opts
)
672 * picking (but not reverting) ranges (but not individual revisions)
673 * should be done in reverse
675 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
676 opts
->revs
->reverse
^= 1;
678 if (prepare_revision_walk(opts
->revs
))
679 return error(_("revision walk setup failed"));
681 if (!opts
->revs
->commits
)
682 return error(_("empty commit set passed"));
686 static int read_and_refresh_cache(struct replay_opts
*opts
)
688 static struct lock_file index_lock
;
689 int index_fd
= hold_locked_index(&index_lock
, 0);
690 if (read_index_preload(&the_index
, NULL
) < 0) {
691 rollback_lock_file(&index_lock
);
692 return error(_("git %s: failed to read the index"),
695 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
696 if (the_index
.cache_changed
&& index_fd
>= 0) {
697 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
)) {
698 rollback_lock_file(&index_lock
);
699 return error(_("git %s: failed to refresh the index"),
703 rollback_lock_file(&index_lock
);
708 enum todo_command command
;
709 struct commit
*commit
;
710 size_t offset_in_buf
;
715 struct todo_item
*items
;
716 int nr
, alloc
, current
;
719 #define TODO_LIST_INIT { STRBUF_INIT }
721 static void todo_list_release(struct todo_list
*todo_list
)
723 strbuf_release(&todo_list
->buf
);
724 free(todo_list
->items
);
725 todo_list
->items
= NULL
;
726 todo_list
->nr
= todo_list
->alloc
= 0;
729 static struct todo_item
*append_new_todo(struct todo_list
*todo_list
)
731 ALLOC_GROW(todo_list
->items
, todo_list
->nr
+ 1, todo_list
->alloc
);
732 return todo_list
->items
+ todo_list
->nr
++;
735 static int parse_insn_line(struct todo_item
*item
, const char *bol
, char *eol
)
737 unsigned char commit_sha1
[20];
738 char *end_of_object_name
;
739 int i
, saved
, status
, padding
;
741 for (i
= 0; i
< ARRAY_SIZE(todo_command_strings
); i
++)
742 if (skip_prefix(bol
, todo_command_strings
[i
], &bol
)) {
746 if (i
>= ARRAY_SIZE(todo_command_strings
))
749 /* Eat up extra spaces/ tabs before object name */
750 padding
= strspn(bol
, " \t");
755 end_of_object_name
= (char *) bol
+ strcspn(bol
, " \t\n");
756 saved
= *end_of_object_name
;
757 *end_of_object_name
= '\0';
758 status
= get_sha1(bol
, commit_sha1
);
759 *end_of_object_name
= saved
;
764 item
->commit
= lookup_commit_reference(commit_sha1
);
765 return !item
->commit
;
768 static int parse_insn_buffer(char *buf
, struct todo_list
*todo_list
)
770 struct todo_item
*item
;
771 char *p
= buf
, *next_p
;
774 for (i
= 1; *p
; i
++, p
= next_p
) {
775 char *eol
= strchrnul(p
, '\n');
777 next_p
= *eol
? eol
+ 1 /* skip LF */ : eol
;
779 if (p
!= eol
&& eol
[-1] == '\r')
780 eol
--; /* strip Carriage Return */
782 item
= append_new_todo(todo_list
);
783 item
->offset_in_buf
= p
- todo_list
->buf
.buf
;
784 if (parse_insn_line(item
, p
, eol
)) {
785 res
= error(_("Invalid line %d: %.*s"),
786 i
, (int)(eol
- p
), p
);
791 return error(_("No commits parsed."));
795 static int read_populate_todo(struct todo_list
*todo_list
,
796 struct replay_opts
*opts
)
798 const char *todo_file
= get_todo_path(opts
);
801 strbuf_reset(&todo_list
->buf
);
802 fd
= open(todo_file
, O_RDONLY
);
804 return error_errno(_("Could not open %s"), todo_file
);
805 if (strbuf_read(&todo_list
->buf
, fd
, 0) < 0) {
807 return error(_("Could not read %s."), todo_file
);
811 res
= parse_insn_buffer(todo_list
->buf
.buf
, todo_list
);
813 enum todo_command valid
=
814 opts
->action
== REPLAY_PICK
? TODO_PICK
: TODO_REVERT
;
817 for (i
= 0; i
< todo_list
->nr
; i
++)
818 if (valid
== todo_list
->items
[i
].command
)
820 else if (valid
== TODO_PICK
)
821 return error(_("Cannot cherry-pick during a revert."));
823 return error(_("Cannot revert during a cherry-pick."));
827 return error(_("Unusable instruction sheet: %s"), todo_file
);
831 static int git_config_string_dup(char **dest
,
832 const char *var
, const char *value
)
835 return config_error_nonbool(var
);
837 *dest
= xstrdup(value
);
841 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
843 struct replay_opts
*opts
= data
;
848 else if (!strcmp(key
, "options.no-commit"))
849 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
850 else if (!strcmp(key
, "options.edit"))
851 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
852 else if (!strcmp(key
, "options.signoff"))
853 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
854 else if (!strcmp(key
, "options.record-origin"))
855 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
856 else if (!strcmp(key
, "options.allow-ff"))
857 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
858 else if (!strcmp(key
, "options.mainline"))
859 opts
->mainline
= git_config_int(key
, value
);
860 else if (!strcmp(key
, "options.strategy"))
861 git_config_string_dup(&opts
->strategy
, key
, value
);
862 else if (!strcmp(key
, "options.gpg-sign"))
863 git_config_string_dup(&opts
->gpg_sign
, key
, value
);
864 else if (!strcmp(key
, "options.strategy-option")) {
865 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
866 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
868 return error(_("Invalid key: %s"), key
);
871 return error(_("Invalid value for %s: %s"), key
, value
);
876 static int read_populate_opts(struct replay_opts
*opts
)
878 if (!file_exists(git_path_opts_file()))
881 * The function git_parse_source(), called from git_config_from_file(),
882 * may die() in case of a syntactically incorrect file. We do not care
883 * about this case, though, because we wrote that file ourselves, so we
884 * are pretty certain that it is syntactically correct.
886 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), opts
) < 0)
887 return error(_("Malformed options sheet: %s"),
888 git_path_opts_file());
892 static int walk_revs_populate_todo(struct todo_list
*todo_list
,
893 struct replay_opts
*opts
)
895 enum todo_command command
= opts
->action
== REPLAY_PICK
?
896 TODO_PICK
: TODO_REVERT
;
897 const char *command_string
= todo_command_strings
[command
];
898 struct commit
*commit
;
900 if (prepare_revs(opts
))
903 while ((commit
= get_revision(opts
->revs
))) {
904 struct todo_item
*item
= append_new_todo(todo_list
);
905 const char *commit_buffer
= get_commit_buffer(commit
, NULL
);
909 item
->command
= command
;
910 item
->commit
= commit
;
911 item
->offset_in_buf
= todo_list
->buf
.len
;
912 subject_len
= find_commit_subject(commit_buffer
, &subject
);
913 strbuf_addf(&todo_list
->buf
, "%s %s %.*s\n", command_string
,
914 short_commit_name(commit
), subject_len
, subject
);
915 unuse_commit_buffer(commit
, commit_buffer
);
920 static int create_seq_dir(void)
922 if (file_exists(git_path_seq_dir())) {
923 error(_("a cherry-pick or revert is already in progress"));
924 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
927 else if (mkdir(git_path_seq_dir(), 0777) < 0)
928 return error_errno(_("Could not create sequencer directory %s"),
933 static int save_head(const char *head
)
935 static struct lock_file head_lock
;
936 struct strbuf buf
= STRBUF_INIT
;
939 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
941 rollback_lock_file(&head_lock
);
942 return error_errno(_("Could not lock HEAD"));
944 strbuf_addf(&buf
, "%s\n", head
);
945 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
946 rollback_lock_file(&head_lock
);
947 return error_errno(_("Could not write to %s"),
948 git_path_head_file());
950 if (commit_lock_file(&head_lock
) < 0) {
951 rollback_lock_file(&head_lock
);
952 return error(_("Error wrapping up %s."), git_path_head_file());
957 static int reset_for_rollback(const unsigned char *sha1
)
959 const char *argv
[4]; /* reset --merge <arg> + NULL */
962 argv
[2] = sha1_to_hex(sha1
);
964 return run_command_v_opt(argv
, RUN_GIT_CMD
);
967 static int rollback_single_pick(void)
969 unsigned char head_sha1
[20];
971 if (!file_exists(git_path_cherry_pick_head()) &&
972 !file_exists(git_path_revert_head()))
973 return error(_("no cherry-pick or revert in progress"));
974 if (read_ref_full("HEAD", 0, head_sha1
, NULL
))
975 return error(_("cannot resolve HEAD"));
976 if (is_null_sha1(head_sha1
))
977 return error(_("cannot abort from a branch yet to be born"));
978 return reset_for_rollback(head_sha1
);
981 static int sequencer_rollback(struct replay_opts
*opts
)
984 unsigned char sha1
[20];
985 struct strbuf buf
= STRBUF_INIT
;
987 f
= fopen(git_path_head_file(), "r");
988 if (!f
&& errno
== ENOENT
) {
990 * There is no multiple-cherry-pick in progress.
991 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
992 * a single-cherry-pick in progress, abort that.
994 return rollback_single_pick();
997 return error_errno(_("cannot open %s"), git_path_head_file());
998 if (strbuf_getline_lf(&buf
, f
)) {
999 error(_("cannot read %s: %s"), git_path_head_file(),
1000 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
1005 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
1006 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
1007 git_path_head_file());
1010 if (is_null_sha1(sha1
)) {
1011 error(_("cannot abort from a branch yet to be born"));
1014 if (reset_for_rollback(sha1
))
1016 remove_sequencer_state(opts
);
1017 strbuf_release(&buf
);
1020 strbuf_release(&buf
);
1024 static int save_todo(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1026 static struct lock_file todo_lock
;
1027 const char *todo_path
= get_todo_path(opts
);
1028 int next
= todo_list
->current
, offset
, fd
;
1030 fd
= hold_lock_file_for_update(&todo_lock
, todo_path
, 0);
1032 return error_errno(_("Could not lock '%s'"), todo_path
);
1033 offset
= next
< todo_list
->nr
?
1034 todo_list
->items
[next
].offset_in_buf
: todo_list
->buf
.len
;
1035 if (write_in_full(fd
, todo_list
->buf
.buf
+ offset
,
1036 todo_list
->buf
.len
- offset
) < 0)
1037 return error_errno(_("Could not write to '%s'"), todo_path
);
1038 if (commit_lock_file(&todo_lock
) < 0)
1039 return error(_("Error wrapping up %s."), todo_path
);
1043 static int save_opts(struct replay_opts
*opts
)
1045 const char *opts_file
= git_path_opts_file();
1048 if (opts
->no_commit
)
1049 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
1051 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
1053 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
1054 if (opts
->record_origin
)
1055 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
1057 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
1058 if (opts
->mainline
) {
1059 struct strbuf buf
= STRBUF_INIT
;
1060 strbuf_addf(&buf
, "%d", opts
->mainline
);
1061 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
1062 strbuf_release(&buf
);
1065 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
1067 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
1070 for (i
= 0; i
< opts
->xopts_nr
; i
++)
1071 res
|= git_config_set_multivar_in_file_gently(opts_file
,
1072 "options.strategy-option",
1073 opts
->xopts
[i
], "^$", 0);
1078 static int pick_commits(struct todo_list
*todo_list
, struct replay_opts
*opts
)
1082 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1084 assert(!(opts
->signoff
|| opts
->no_commit
||
1085 opts
->record_origin
|| opts
->edit
));
1086 if (read_and_refresh_cache(opts
))
1089 while (todo_list
->current
< todo_list
->nr
) {
1090 struct todo_item
*item
= todo_list
->items
+ todo_list
->current
;
1091 if (save_todo(todo_list
, opts
))
1093 res
= do_pick_commit(item
->command
, item
->commit
, opts
);
1094 todo_list
->current
++;
1100 * Sequence of picks finished successfully; cleanup by
1101 * removing the .git/sequencer directory
1103 remove_sequencer_state(opts
);
1107 static int continue_single_pick(void)
1109 const char *argv
[] = { "commit", NULL
};
1111 if (!file_exists(git_path_cherry_pick_head()) &&
1112 !file_exists(git_path_revert_head()))
1113 return error(_("no cherry-pick or revert in progress"));
1114 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1117 static int sequencer_continue(struct replay_opts
*opts
)
1119 struct todo_list todo_list
= TODO_LIST_INIT
;
1122 if (!file_exists(get_todo_path(opts
)))
1123 return continue_single_pick();
1124 if (read_populate_opts(opts
))
1126 if ((res
= read_populate_todo(&todo_list
, opts
)))
1127 goto release_todo_list
;
1129 /* Verify that the conflict has been resolved */
1130 if (file_exists(git_path_cherry_pick_head()) ||
1131 file_exists(git_path_revert_head())) {
1132 res
= continue_single_pick();
1134 goto release_todo_list
;
1136 if (index_differs_from("HEAD", 0)) {
1137 res
= error_dirty_index(opts
);
1138 goto release_todo_list
;
1140 todo_list
.current
++;
1141 res
= pick_commits(&todo_list
, opts
);
1143 todo_list_release(&todo_list
);
1147 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1149 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1150 return do_pick_commit(opts
->action
== REPLAY_PICK
?
1151 TODO_PICK
: TODO_REVERT
, cmit
, opts
);
1154 int sequencer_pick_revisions(struct replay_opts
*opts
)
1156 struct todo_list todo_list
= TODO_LIST_INIT
;
1157 unsigned char sha1
[20];
1160 if (opts
->subcommand
== REPLAY_NONE
)
1163 if (read_and_refresh_cache(opts
))
1167 * Decide what to do depending on the arguments; a fresh
1168 * cherry-pick should be handled differently from an existing
1169 * one that is being continued
1171 if (opts
->subcommand
== REPLAY_REMOVE_STATE
) {
1172 remove_sequencer_state(opts
);
1175 if (opts
->subcommand
== REPLAY_ROLLBACK
)
1176 return sequencer_rollback(opts
);
1177 if (opts
->subcommand
== REPLAY_CONTINUE
)
1178 return sequencer_continue(opts
);
1180 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
1181 unsigned char sha1
[20];
1182 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
1184 /* This happens when using --stdin. */
1188 if (!get_sha1(name
, sha1
)) {
1189 if (!lookup_commit_reference_gently(sha1
, 1)) {
1190 enum object_type type
= sha1_object_info(sha1
, NULL
);
1191 return error(_("%s: can't cherry-pick a %s"),
1192 name
, typename(type
));
1195 return error(_("%s: bad revision"), name
);
1199 * If we were called as "git cherry-pick <commit>", just
1200 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1201 * REVERT_HEAD, and don't touch the sequencer state.
1202 * This means it is possible to cherry-pick in the middle
1203 * of a cherry-pick sequence.
1205 if (opts
->revs
->cmdline
.nr
== 1 &&
1206 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
1207 opts
->revs
->no_walk
&&
1208 !opts
->revs
->cmdline
.rev
->flags
) {
1209 struct commit
*cmit
;
1210 if (prepare_revision_walk(opts
->revs
))
1211 return error(_("revision walk setup failed"));
1212 cmit
= get_revision(opts
->revs
);
1213 if (!cmit
|| get_revision(opts
->revs
))
1214 return error("BUG: expected exactly one commit from walk");
1215 return single_pick(cmit
, opts
);
1219 * Start a new cherry-pick/ revert sequence; but
1220 * first, make sure that an existing one isn't in
1224 if (walk_revs_populate_todo(&todo_list
, opts
) ||
1225 create_seq_dir() < 0)
1227 if (get_sha1("HEAD", sha1
) && (opts
->action
== REPLAY_REVERT
))
1228 return error(_("Can't revert as initial commit"));
1229 if (save_head(sha1_to_hex(sha1
)))
1231 if (save_opts(opts
))
1233 res
= pick_commits(&todo_list
, opts
);
1234 todo_list_release(&todo_list
);
1238 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
1240 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
1241 struct strbuf sob
= STRBUF_INIT
;
1244 strbuf_addstr(&sob
, sign_off_header
);
1245 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
1246 getenv("GIT_COMMITTER_EMAIL")));
1247 strbuf_addch(&sob
, '\n');
1250 * If the whole message buffer is equal to the sob, pretend that we
1251 * found a conforming footer with a matching sob
1253 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
1254 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
1257 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
1260 const char *append_newlines
= NULL
;
1261 size_t len
= msgbuf
->len
- ignore_footer
;
1265 * The buffer is completely empty. Leave foom for
1266 * the title and body to be filled in by the user.
1268 append_newlines
= "\n\n";
1269 } else if (msgbuf
->buf
[len
- 1] != '\n') {
1271 * Incomplete line. Complete the line and add a
1272 * blank one so that there is an empty line between
1273 * the message body and the sob.
1275 append_newlines
= "\n\n";
1276 } else if (len
== 1) {
1278 * Buffer contains a single newline. Add another
1279 * so that we leave room for the title and body.
1281 append_newlines
= "\n";
1282 } else if (msgbuf
->buf
[len
- 2] != '\n') {
1284 * Buffer ends with a single newline. Add another
1285 * so that there is an empty line between the message
1288 append_newlines
= "\n";
1289 } /* else, the buffer already ends with two newlines. */
1291 if (append_newlines
)
1292 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1293 append_newlines
, strlen(append_newlines
));
1296 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
1297 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1300 strbuf_release(&sob
);