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 static GIT_PATH_FUNC(git_path_todo_file
, SEQ_TODO_FILE
)
25 static GIT_PATH_FUNC(git_path_opts_file
, SEQ_OPTS_FILE
)
26 static GIT_PATH_FUNC(git_path_seq_dir
, SEQ_DIR
)
27 static GIT_PATH_FUNC(git_path_head_file
, SEQ_HEAD_FILE
)
29 static int is_rfc2822_line(const char *buf
, int len
)
33 for (i
= 0; i
< len
; i
++) {
37 if (!isalnum(ch
) && ch
!= '-')
44 static int is_cherry_picked_from_line(const char *buf
, int len
)
47 * We only care that it looks roughly like (cherry picked from ...)
49 return len
> strlen(cherry_picked_prefix
) + 1 &&
50 starts_with(buf
, cherry_picked_prefix
) && buf
[len
- 1] == ')';
54 * Returns 0 for non-conforming footer
55 * Returns 1 for conforming footer
56 * Returns 2 when sob exists within conforming footer
57 * Returns 3 when sob exists within conforming footer as last entry
59 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
64 int len
= sb
->len
- ignore_footer
;
65 const char *buf
= sb
->buf
;
68 /* footer must end with newline */
69 if (!len
|| buf
[len
- 1] != '\n')
73 for (i
= len
- 1; i
> 0; i
--) {
75 if (prev
== '\n' && ch
== '\n') /* paragraph break */
80 /* require at least one blank line */
81 if (prev
!= '\n' || buf
[i
] != '\n')
84 /* advance to start of last paragraph */
85 while (i
< len
- 1 && buf
[i
] == '\n')
88 for (; i
< len
; i
= k
) {
91 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
95 found_rfc2822
= is_rfc2822_line(buf
+ i
, k
- i
- 1);
96 if (found_rfc2822
&& sob
&&
97 !strncmp(buf
+ i
, sob
->buf
, sob
->len
))
100 if (!(found_rfc2822
||
101 is_cherry_picked_from_line(buf
+ i
, k
- i
- 1)))
111 static void remove_sequencer_state(void)
113 struct strbuf seq_dir
= STRBUF_INIT
;
115 strbuf_addstr(&seq_dir
, git_path(SEQ_DIR
));
116 remove_dir_recursively(&seq_dir
, 0);
117 strbuf_release(&seq_dir
);
120 static const char *action_name(const struct replay_opts
*opts
)
122 return opts
->action
== REPLAY_REVERT
? "revert" : "cherry-pick";
125 struct commit_message
{
132 static int get_message(struct commit
*commit
, struct commit_message
*out
)
134 const char *abbrev
, *subject
;
137 out
->message
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
138 abbrev
= find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
);
140 subject_len
= find_commit_subject(out
->message
, &subject
);
142 out
->subject
= xmemdupz(subject
, subject_len
);
143 out
->label
= xstrfmt("%s... %s", abbrev
, out
->subject
);
144 out
->parent_label
= xstrfmt("parent of %s", out
->label
);
149 static void free_message(struct commit
*commit
, struct commit_message
*msg
)
151 free(msg
->parent_label
);
154 unuse_commit_buffer(commit
, msg
->message
);
157 static void print_advice(int show_hint
, struct replay_opts
*opts
)
159 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
162 fprintf(stderr
, "%s\n", msg
);
164 * A conflict has occurred but the porcelain
165 * (typically rebase --interactive) wants to take care
166 * of the commit itself so remove CHERRY_PICK_HEAD
168 unlink(git_path_cherry_pick_head());
174 advise(_("after resolving the conflicts, mark the corrected paths\n"
175 "with 'git add <paths>' or 'git rm <paths>'"));
177 advise(_("after resolving the conflicts, mark the corrected paths\n"
178 "with 'git add <paths>' or 'git rm <paths>'\n"
179 "and commit the result with 'git commit'"));
183 static int write_message(struct strbuf
*msgbuf
, const char *filename
)
185 static struct lock_file msg_file
;
187 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
, 0);
189 return error_errno(_("Could not lock '%s'"), filename
);
190 if (write_in_full(msg_fd
, msgbuf
->buf
, msgbuf
->len
) < 0)
191 return error_errno(_("Could not write to %s"), filename
);
192 strbuf_release(msgbuf
);
193 if (commit_lock_file(&msg_file
) < 0)
194 return error(_("Error wrapping up %s."), filename
);
199 static struct tree
*empty_tree(void)
201 return lookup_tree(EMPTY_TREE_SHA1_BIN
);
204 static int error_dirty_index(struct replay_opts
*opts
)
206 if (read_cache_unmerged())
207 return error_resolve_conflict(action_name(opts
));
209 /* Different translation strings for cherry-pick and revert */
210 if (opts
->action
== REPLAY_PICK
)
211 error(_("Your local changes would be overwritten by cherry-pick."));
213 error(_("Your local changes would be overwritten by revert."));
215 if (advice_commit_before_merge
)
216 advise(_("Commit your changes or stash them to proceed."));
220 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
,
221 int unborn
, struct replay_opts
*opts
)
223 struct ref_transaction
*transaction
;
224 struct strbuf sb
= STRBUF_INIT
;
225 struct strbuf err
= STRBUF_INIT
;
228 if (checkout_fast_forward(from
, to
, 1))
229 return -1; /* the callee should have complained already */
231 strbuf_addf(&sb
, _("%s: fast-forward"), action_name(opts
));
233 transaction
= ref_transaction_begin(&err
);
235 ref_transaction_update(transaction
, "HEAD",
236 to
, unborn
? null_sha1
: from
,
238 ref_transaction_commit(transaction
, &err
)) {
239 ref_transaction_free(transaction
);
240 error("%s", err
.buf
);
242 strbuf_release(&err
);
247 strbuf_release(&err
);
248 ref_transaction_free(transaction
);
252 void append_conflicts_hint(struct strbuf
*msgbuf
)
256 strbuf_addch(msgbuf
, '\n');
257 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
258 for (i
= 0; i
< active_nr
;) {
259 const struct cache_entry
*ce
= active_cache
[i
++];
261 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
262 while (i
< active_nr
&& !strcmp(ce
->name
,
263 active_cache
[i
]->name
))
269 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
270 const char *base_label
, const char *next_label
,
271 unsigned char *head
, struct strbuf
*msgbuf
,
272 struct replay_opts
*opts
)
274 struct merge_options o
;
275 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
278 static struct lock_file index_lock
;
280 hold_locked_index(&index_lock
, 1);
284 init_merge_options(&o
);
285 o
.ancestor
= base
? base_label
: "(empty tree)";
287 o
.branch2
= next
? next_label
: "(empty tree)";
289 head_tree
= parse_tree_indirect(head
);
290 next_tree
= next
? next
->tree
: empty_tree();
291 base_tree
= base
? base
->tree
: empty_tree();
293 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
294 parse_merge_opt(&o
, *xopt
);
296 clean
= merge_trees(&o
,
298 next_tree
, base_tree
, &result
);
299 strbuf_release(&o
.obuf
);
303 if (active_cache_changed
&&
304 write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
305 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
306 return error(_("%s: Unable to write new index file"),
308 rollback_lock_file(&index_lock
);
311 append_signoff(msgbuf
, 0, 0);
314 append_conflicts_hint(msgbuf
);
319 static int is_index_unchanged(void)
321 unsigned char head_sha1
[20];
322 struct commit
*head_commit
;
324 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, head_sha1
, NULL
))
325 return error(_("Could not resolve HEAD commit\n"));
327 head_commit
= lookup_commit(head_sha1
);
330 * If head_commit is NULL, check_commit, called from
331 * lookup_commit, would have indicated that head_commit is not
332 * a commit object already. parse_commit() will return failure
333 * without further complaints in such a case. Otherwise, if
334 * the commit is invalid, parse_commit() will complain. So
335 * there is nothing for us to say here. Just return failure.
337 if (parse_commit(head_commit
))
340 if (!active_cache_tree
)
341 active_cache_tree
= cache_tree();
343 if (!cache_tree_fully_valid(active_cache_tree
))
344 if (cache_tree_update(&the_index
, 0))
345 return error(_("Unable to update cache tree\n"));
347 return !hashcmp(active_cache_tree
->sha1
, head_commit
->tree
->object
.oid
.hash
);
351 * If we are cherry-pick, and if the merge did not result in
352 * hand-editing, we will hit this commit and inherit the original
353 * author date and name.
354 * If we are revert, or if our cherry-pick results in a hand merge,
355 * we had better say that the current user is responsible for that.
357 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
360 struct argv_array array
;
364 argv_array_init(&array
);
365 argv_array_push(&array
, "commit");
366 argv_array_push(&array
, "-n");
369 argv_array_pushf(&array
, "-S%s", opts
->gpg_sign
);
371 argv_array_push(&array
, "-s");
373 argv_array_push(&array
, "-F");
374 argv_array_push(&array
, defmsg
);
375 if (!opts
->signoff
&&
376 !opts
->record_origin
&&
377 git_config_get_value("commit.cleanup", &value
))
378 argv_array_push(&array
, "--cleanup=verbatim");
382 argv_array_push(&array
, "--allow-empty");
384 if (opts
->allow_empty_message
)
385 argv_array_push(&array
, "--allow-empty-message");
387 rc
= run_command_v_opt(array
.argv
, RUN_GIT_CMD
);
388 argv_array_clear(&array
);
392 static int is_original_commit_empty(struct commit
*commit
)
394 const unsigned char *ptree_sha1
;
396 if (parse_commit(commit
))
397 return error(_("Could not parse commit %s\n"),
398 oid_to_hex(&commit
->object
.oid
));
399 if (commit
->parents
) {
400 struct commit
*parent
= commit
->parents
->item
;
401 if (parse_commit(parent
))
402 return error(_("Could not parse parent commit %s\n"),
403 oid_to_hex(&parent
->object
.oid
));
404 ptree_sha1
= parent
->tree
->object
.oid
.hash
;
406 ptree_sha1
= EMPTY_TREE_SHA1_BIN
; /* commit is root */
409 return !hashcmp(ptree_sha1
, commit
->tree
->object
.oid
.hash
);
413 * Do we run "git commit" with "--allow-empty"?
415 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
417 int index_unchanged
, empty_commit
;
422 * (1) we do not allow empty at all and error out.
424 * (2) we allow ones that were initially empty, but
425 * forbid the ones that become empty;
429 if (!opts
->allow_empty
)
430 return 0; /* let "git commit" barf as necessary */
432 index_unchanged
= is_index_unchanged();
433 if (index_unchanged
< 0)
434 return index_unchanged
;
435 if (!index_unchanged
)
436 return 0; /* we do not have to say --allow-empty */
438 if (opts
->keep_redundant_commits
)
441 empty_commit
= is_original_commit_empty(commit
);
442 if (empty_commit
< 0)
450 static int do_pick_commit(struct commit
*commit
, struct replay_opts
*opts
)
452 unsigned char head
[20];
453 struct commit
*base
, *next
, *parent
;
454 const char *base_label
, *next_label
;
455 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
456 struct strbuf msgbuf
= STRBUF_INIT
;
457 int res
, unborn
= 0, allow
;
459 if (opts
->no_commit
) {
461 * We do not intend to commit immediately. We just want to
462 * merge the differences in, so let's compute the tree
463 * that represents the "current" state for merge-recursive
466 if (write_cache_as_tree(head
, 0, NULL
))
467 return error(_("Your index file is unmerged."));
469 unborn
= get_sha1("HEAD", head
);
471 hashcpy(head
, EMPTY_TREE_SHA1_BIN
);
472 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD", 0))
473 return error_dirty_index(opts
);
477 if (!commit
->parents
) {
480 else if (commit
->parents
->next
) {
481 /* Reverting or cherry-picking a merge commit */
483 struct commit_list
*p
;
486 return error(_("Commit %s is a merge but no -m option was given."),
487 oid_to_hex(&commit
->object
.oid
));
489 for (cnt
= 1, p
= commit
->parents
;
490 cnt
!= opts
->mainline
&& p
;
493 if (cnt
!= opts
->mainline
|| !p
)
494 return error(_("Commit %s does not have parent %d"),
495 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
497 } else if (0 < opts
->mainline
)
498 return error(_("Mainline was specified but commit %s is not a merge."),
499 oid_to_hex(&commit
->object
.oid
));
501 parent
= commit
->parents
->item
;
503 if (opts
->allow_ff
&&
504 ((parent
&& !hashcmp(parent
->object
.oid
.hash
, head
)) ||
505 (!parent
&& unborn
)))
506 return fast_forward_to(commit
->object
.oid
.hash
, head
, unborn
, opts
);
508 if (parent
&& parse_commit(parent
) < 0)
509 /* TRANSLATORS: The first %s will be "revert" or
510 "cherry-pick", the second %s a SHA1 */
511 return error(_("%s: cannot parse parent commit %s"),
512 action_name(opts
), oid_to_hex(&parent
->object
.oid
));
514 if (get_message(commit
, &msg
) != 0)
515 return error(_("Cannot get commit message for %s"),
516 oid_to_hex(&commit
->object
.oid
));
519 * "commit" is an existing commit. We would want to apply
520 * the difference it introduces since its first parent "prev"
521 * on top of the current HEAD if we are cherry-pick. Or the
522 * reverse of it if we are revert.
525 if (opts
->action
== REPLAY_REVERT
) {
527 base_label
= msg
.label
;
529 next_label
= msg
.parent_label
;
530 strbuf_addstr(&msgbuf
, "Revert \"");
531 strbuf_addstr(&msgbuf
, msg
.subject
);
532 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
533 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
535 if (commit
->parents
&& commit
->parents
->next
) {
536 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
537 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
539 strbuf_addstr(&msgbuf
, ".\n");
544 base_label
= msg
.parent_label
;
546 next_label
= msg
.label
;
549 * Append the commit log message to msgbuf; it starts
550 * after the tree, parent, author, committer
551 * information followed by "\n\n".
553 p
= strstr(msg
.message
, "\n\n");
555 strbuf_addstr(&msgbuf
, skip_blank_lines(p
+ 2));
557 if (opts
->record_origin
) {
558 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
559 strbuf_addch(&msgbuf
, '\n');
560 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
561 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
562 strbuf_addstr(&msgbuf
, ")\n");
566 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || opts
->action
== REPLAY_REVERT
) {
567 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
568 head
, &msgbuf
, opts
);
571 res
|= write_message(&msgbuf
, git_path_merge_msg());
573 struct commit_list
*common
= NULL
;
574 struct commit_list
*remotes
= NULL
;
576 res
= write_message(&msgbuf
, git_path_merge_msg());
578 commit_list_insert(base
, &common
);
579 commit_list_insert(next
, &remotes
);
580 res
|= try_merge_command(opts
->strategy
, opts
->xopts_nr
, opts
->xopts
,
581 common
, sha1_to_hex(head
), remotes
);
582 free_commit_list(common
);
583 free_commit_list(remotes
);
587 * If the merge was clean or if it failed due to conflict, we write
588 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
589 * However, if the merge did not even start, then we don't want to
592 if (opts
->action
== REPLAY_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1) &&
593 update_ref(NULL
, "CHERRY_PICK_HEAD", commit
->object
.oid
.hash
, NULL
,
594 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
596 if (opts
->action
== REPLAY_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1) &&
597 update_ref(NULL
, "REVERT_HEAD", commit
->object
.oid
.hash
, NULL
,
598 REF_NODEREF
, UPDATE_REFS_MSG_ON_ERR
))
602 error(opts
->action
== REPLAY_REVERT
603 ? _("could not revert %s... %s")
604 : _("could not apply %s... %s"),
605 find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
),
607 print_advice(res
== 1, opts
);
608 rerere(opts
->allow_rerere_auto
);
612 allow
= allow_empty(opts
, commit
);
617 if (!opts
->no_commit
)
618 res
= run_git_commit(git_path_merge_msg(), opts
, allow
);
621 free_message(commit
, &msg
);
626 static int prepare_revs(struct replay_opts
*opts
)
629 * picking (but not reverting) ranges (but not individual revisions)
630 * should be done in reverse
632 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
633 opts
->revs
->reverse
^= 1;
635 if (prepare_revision_walk(opts
->revs
))
636 return error(_("revision walk setup failed"));
638 if (!opts
->revs
->commits
)
639 return error(_("empty commit set passed"));
643 static int read_and_refresh_cache(struct replay_opts
*opts
)
645 static struct lock_file index_lock
;
646 int index_fd
= hold_locked_index(&index_lock
, 0);
647 if (read_index_preload(&the_index
, NULL
) < 0) {
648 rollback_lock_file(&index_lock
);
649 return error(_("git %s: failed to read the index"),
652 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
653 if (the_index
.cache_changed
&& index_fd
>= 0) {
654 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
)) {
655 rollback_lock_file(&index_lock
);
656 return error(_("git %s: failed to refresh the index"),
660 rollback_lock_file(&index_lock
);
664 static int format_todo(struct strbuf
*buf
, struct commit_list
*todo_list
,
665 struct replay_opts
*opts
)
667 struct commit_list
*cur
= NULL
;
668 const char *sha1_abbrev
= NULL
;
669 const char *action_str
= opts
->action
== REPLAY_REVERT
? "revert" : "pick";
673 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
674 const char *commit_buffer
= get_commit_buffer(cur
->item
, NULL
);
675 sha1_abbrev
= find_unique_abbrev(cur
->item
->object
.oid
.hash
, DEFAULT_ABBREV
);
676 subject_len
= find_commit_subject(commit_buffer
, &subject
);
677 strbuf_addf(buf
, "%s %s %.*s\n", action_str
, sha1_abbrev
,
678 subject_len
, subject
);
679 unuse_commit_buffer(cur
->item
, commit_buffer
);
684 static struct commit
*parse_insn_line(char *bol
, char *eol
, struct replay_opts
*opts
)
686 unsigned char commit_sha1
[20];
687 enum replay_action action
;
688 char *end_of_object_name
;
689 int saved
, status
, padding
;
691 if (starts_with(bol
, "pick")) {
692 action
= REPLAY_PICK
;
693 bol
+= strlen("pick");
694 } else if (starts_with(bol
, "revert")) {
695 action
= REPLAY_REVERT
;
696 bol
+= strlen("revert");
700 /* Eat up extra spaces/ tabs before object name */
701 padding
= strspn(bol
, " \t");
706 end_of_object_name
= bol
+ strcspn(bol
, " \t\n");
707 saved
= *end_of_object_name
;
708 *end_of_object_name
= '\0';
709 status
= get_sha1(bol
, commit_sha1
);
710 *end_of_object_name
= saved
;
713 * Verify that the action matches up with the one in
714 * opts; we don't support arbitrary instructions
716 if (action
!= opts
->action
) {
717 if (action
== REPLAY_REVERT
)
718 error((opts
->action
== REPLAY_REVERT
)
719 ? _("Cannot revert during another revert.")
720 : _("Cannot revert during a cherry-pick."));
722 error((opts
->action
== REPLAY_REVERT
)
723 ? _("Cannot cherry-pick during a revert.")
724 : _("Cannot cherry-pick during another cherry-pick."));
731 return lookup_commit_reference(commit_sha1
);
734 static int parse_insn_buffer(char *buf
, struct commit_list
**todo_list
,
735 struct replay_opts
*opts
)
737 struct commit_list
**next
= todo_list
;
738 struct commit
*commit
;
742 for (i
= 1; *p
; i
++) {
743 char *eol
= strchrnul(p
, '\n');
744 commit
= parse_insn_line(p
, eol
, opts
);
746 return error(_("Could not parse line %d."), i
);
747 next
= commit_list_append(commit
, next
);
748 p
= *eol
? eol
+ 1 : eol
;
751 return error(_("No commits parsed."));
755 static int read_populate_todo(struct commit_list
**todo_list
,
756 struct replay_opts
*opts
)
758 struct strbuf buf
= STRBUF_INIT
;
761 fd
= open(git_path_todo_file(), O_RDONLY
);
763 return error_errno(_("Could not open %s"),
764 git_path_todo_file());
765 if (strbuf_read(&buf
, fd
, 0) < 0) {
767 strbuf_release(&buf
);
768 return error(_("Could not read %s."), git_path_todo_file());
772 res
= parse_insn_buffer(buf
.buf
, todo_list
, opts
);
773 strbuf_release(&buf
);
775 return error(_("Unusable instruction sheet: %s"),
776 git_path_todo_file());
780 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
782 struct replay_opts
*opts
= data
;
787 else if (!strcmp(key
, "options.no-commit"))
788 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
789 else if (!strcmp(key
, "options.edit"))
790 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
791 else if (!strcmp(key
, "options.signoff"))
792 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
793 else if (!strcmp(key
, "options.record-origin"))
794 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
795 else if (!strcmp(key
, "options.allow-ff"))
796 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
797 else if (!strcmp(key
, "options.mainline"))
798 opts
->mainline
= git_config_int(key
, value
);
799 else if (!strcmp(key
, "options.strategy"))
800 git_config_string(&opts
->strategy
, key
, value
);
801 else if (!strcmp(key
, "options.gpg-sign"))
802 git_config_string(&opts
->gpg_sign
, key
, value
);
803 else if (!strcmp(key
, "options.strategy-option")) {
804 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
805 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
807 return error(_("Invalid key: %s"), key
);
810 return error(_("Invalid value for %s: %s"), key
, value
);
815 static int read_populate_opts(struct replay_opts
**opts
)
817 if (!file_exists(git_path_opts_file()))
820 * The function git_parse_source(), called from git_config_from_file(),
821 * may die() in case of a syntactically incorrect file. We do not care
822 * about this case, though, because we wrote that file ourselves, so we
823 * are pretty certain that it is syntactically correct.
825 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), *opts
) < 0)
826 return error(_("Malformed options sheet: %s"),
827 git_path_opts_file());
831 static int walk_revs_populate_todo(struct commit_list
**todo_list
,
832 struct replay_opts
*opts
)
834 struct commit
*commit
;
835 struct commit_list
**next
;
837 if (prepare_revs(opts
))
841 while ((commit
= get_revision(opts
->revs
)))
842 next
= commit_list_append(commit
, next
);
846 static int create_seq_dir(void)
848 if (file_exists(git_path_seq_dir())) {
849 error(_("a cherry-pick or revert is already in progress"));
850 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
853 else if (mkdir(git_path_seq_dir(), 0777) < 0)
854 return error_errno(_("Could not create sequencer directory %s"),
859 static int save_head(const char *head
)
861 static struct lock_file head_lock
;
862 struct strbuf buf
= STRBUF_INIT
;
865 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), 0);
867 rollback_lock_file(&head_lock
);
868 return error_errno(_("Could not lock HEAD"));
870 strbuf_addf(&buf
, "%s\n", head
);
871 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
872 rollback_lock_file(&head_lock
);
873 return error_errno(_("Could not write to %s"),
874 git_path_head_file());
876 if (commit_lock_file(&head_lock
) < 0) {
877 rollback_lock_file(&head_lock
);
878 return error(_("Error wrapping up %s."), git_path_head_file());
883 static int reset_for_rollback(const unsigned char *sha1
)
885 const char *argv
[4]; /* reset --merge <arg> + NULL */
888 argv
[2] = sha1_to_hex(sha1
);
890 return run_command_v_opt(argv
, RUN_GIT_CMD
);
893 static int rollback_single_pick(void)
895 unsigned char head_sha1
[20];
897 if (!file_exists(git_path_cherry_pick_head()) &&
898 !file_exists(git_path_revert_head()))
899 return error(_("no cherry-pick or revert in progress"));
900 if (read_ref_full("HEAD", 0, head_sha1
, NULL
))
901 return error(_("cannot resolve HEAD"));
902 if (is_null_sha1(head_sha1
))
903 return error(_("cannot abort from a branch yet to be born"));
904 return reset_for_rollback(head_sha1
);
907 static int sequencer_rollback(struct replay_opts
*opts
)
910 unsigned char sha1
[20];
911 struct strbuf buf
= STRBUF_INIT
;
913 f
= fopen(git_path_head_file(), "r");
914 if (!f
&& errno
== ENOENT
) {
916 * There is no multiple-cherry-pick in progress.
917 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
918 * a single-cherry-pick in progress, abort that.
920 return rollback_single_pick();
923 return error_errno(_("cannot open %s"), git_path_head_file());
924 if (strbuf_getline_lf(&buf
, f
)) {
925 error(_("cannot read %s: %s"), git_path_head_file(),
926 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
931 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
932 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
933 git_path_head_file());
936 if (is_null_sha1(sha1
)) {
937 error(_("cannot abort from a branch yet to be born"));
940 if (reset_for_rollback(sha1
))
942 remove_sequencer_state();
943 strbuf_release(&buf
);
946 strbuf_release(&buf
);
950 static int save_todo(struct commit_list
*todo_list
, struct replay_opts
*opts
)
952 static struct lock_file todo_lock
;
953 struct strbuf buf
= STRBUF_INIT
;
956 fd
= hold_lock_file_for_update(&todo_lock
, git_path_todo_file(), 0);
958 return error_errno(_("Could not lock '%s'"),
959 git_path_todo_file());
960 if (format_todo(&buf
, todo_list
, opts
) < 0) {
961 strbuf_release(&buf
);
962 return error(_("Could not format %s."), git_path_todo_file());
964 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
965 strbuf_release(&buf
);
966 return error_errno(_("Could not write to %s"),
967 git_path_todo_file());
969 if (commit_lock_file(&todo_lock
) < 0) {
970 strbuf_release(&buf
);
971 return error(_("Error wrapping up %s."), git_path_todo_file());
973 strbuf_release(&buf
);
977 static int save_opts(struct replay_opts
*opts
)
979 const char *opts_file
= git_path_opts_file();
983 res
|= git_config_set_in_file_gently(opts_file
, "options.no-commit", "true");
985 res
|= git_config_set_in_file_gently(opts_file
, "options.edit", "true");
987 res
|= git_config_set_in_file_gently(opts_file
, "options.signoff", "true");
988 if (opts
->record_origin
)
989 res
|= git_config_set_in_file_gently(opts_file
, "options.record-origin", "true");
991 res
|= git_config_set_in_file_gently(opts_file
, "options.allow-ff", "true");
992 if (opts
->mainline
) {
993 struct strbuf buf
= STRBUF_INIT
;
994 strbuf_addf(&buf
, "%d", opts
->mainline
);
995 res
|= git_config_set_in_file_gently(opts_file
, "options.mainline", buf
.buf
);
996 strbuf_release(&buf
);
999 res
|= git_config_set_in_file_gently(opts_file
, "options.strategy", opts
->strategy
);
1001 res
|= git_config_set_in_file_gently(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
1004 for (i
= 0; i
< opts
->xopts_nr
; i
++)
1005 res
|= git_config_set_multivar_in_file_gently(opts_file
,
1006 "options.strategy-option",
1007 opts
->xopts
[i
], "^$", 0);
1012 static int pick_commits(struct commit_list
*todo_list
, struct replay_opts
*opts
)
1014 struct commit_list
*cur
;
1017 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1019 assert(!(opts
->signoff
|| opts
->no_commit
||
1020 opts
->record_origin
|| opts
->edit
));
1021 if (read_and_refresh_cache(opts
))
1024 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
1025 if (save_todo(cur
, opts
))
1027 res
= do_pick_commit(cur
->item
, opts
);
1033 * Sequence of picks finished successfully; cleanup by
1034 * removing the .git/sequencer directory
1036 remove_sequencer_state();
1040 static int continue_single_pick(void)
1042 const char *argv
[] = { "commit", NULL
};
1044 if (!file_exists(git_path_cherry_pick_head()) &&
1045 !file_exists(git_path_revert_head()))
1046 return error(_("no cherry-pick or revert in progress"));
1047 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1050 static int sequencer_continue(struct replay_opts
*opts
)
1052 struct commit_list
*todo_list
= NULL
;
1054 if (!file_exists(git_path_todo_file()))
1055 return continue_single_pick();
1056 if (read_populate_opts(&opts
) ||
1057 read_populate_todo(&todo_list
, opts
))
1060 /* Verify that the conflict has been resolved */
1061 if (file_exists(git_path_cherry_pick_head()) ||
1062 file_exists(git_path_revert_head())) {
1063 int ret
= continue_single_pick();
1067 if (index_differs_from("HEAD", 0))
1068 return error_dirty_index(opts
);
1069 todo_list
= todo_list
->next
;
1070 return pick_commits(todo_list
, opts
);
1073 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1075 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1076 return do_pick_commit(cmit
, opts
);
1079 int sequencer_pick_revisions(struct replay_opts
*opts
)
1081 struct commit_list
*todo_list
= NULL
;
1082 unsigned char sha1
[20];
1085 if (opts
->subcommand
== REPLAY_NONE
)
1088 if (read_and_refresh_cache(opts
))
1092 * Decide what to do depending on the arguments; a fresh
1093 * cherry-pick should be handled differently from an existing
1094 * one that is being continued
1096 if (opts
->subcommand
== REPLAY_REMOVE_STATE
) {
1097 remove_sequencer_state();
1100 if (opts
->subcommand
== REPLAY_ROLLBACK
)
1101 return sequencer_rollback(opts
);
1102 if (opts
->subcommand
== REPLAY_CONTINUE
)
1103 return sequencer_continue(opts
);
1105 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
1106 unsigned char sha1
[20];
1107 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
1109 /* This happens when using --stdin. */
1113 if (!get_sha1(name
, sha1
)) {
1114 if (!lookup_commit_reference_gently(sha1
, 1)) {
1115 enum object_type type
= sha1_object_info(sha1
, NULL
);
1116 return error(_("%s: can't cherry-pick a %s"),
1117 name
, typename(type
));
1120 return error(_("%s: bad revision"), name
);
1124 * If we were called as "git cherry-pick <commit>", just
1125 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1126 * REVERT_HEAD, and don't touch the sequencer state.
1127 * This means it is possible to cherry-pick in the middle
1128 * of a cherry-pick sequence.
1130 if (opts
->revs
->cmdline
.nr
== 1 &&
1131 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
1132 opts
->revs
->no_walk
&&
1133 !opts
->revs
->cmdline
.rev
->flags
) {
1134 struct commit
*cmit
;
1135 if (prepare_revision_walk(opts
->revs
))
1136 return error(_("revision walk setup failed"));
1137 cmit
= get_revision(opts
->revs
);
1138 if (!cmit
|| get_revision(opts
->revs
))
1139 return error("BUG: expected exactly one commit from walk");
1140 return single_pick(cmit
, opts
);
1144 * Start a new cherry-pick/ revert sequence; but
1145 * first, make sure that an existing one isn't in
1149 if (walk_revs_populate_todo(&todo_list
, opts
) ||
1150 create_seq_dir() < 0)
1152 if (get_sha1("HEAD", sha1
) && (opts
->action
== REPLAY_REVERT
))
1153 return error(_("Can't revert as initial commit"));
1154 if (save_head(sha1_to_hex(sha1
)))
1156 if (save_opts(opts
))
1158 return pick_commits(todo_list
, opts
);
1161 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
1163 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
1164 struct strbuf sob
= STRBUF_INIT
;
1167 strbuf_addstr(&sob
, sign_off_header
);
1168 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
1169 getenv("GIT_COMMITTER_EMAIL")));
1170 strbuf_addch(&sob
, '\n');
1173 * If the whole message buffer is equal to the sob, pretend that we
1174 * found a conforming footer with a matching sob
1176 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
1177 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
1180 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
1183 const char *append_newlines
= NULL
;
1184 size_t len
= msgbuf
->len
- ignore_footer
;
1188 * The buffer is completely empty. Leave foom for
1189 * the title and body to be filled in by the user.
1191 append_newlines
= "\n\n";
1192 } else if (msgbuf
->buf
[len
- 1] != '\n') {
1194 * Incomplete line. Complete the line and add a
1195 * blank one so that there is an empty line between
1196 * the message body and the sob.
1198 append_newlines
= "\n\n";
1199 } else if (len
== 1) {
1201 * Buffer contains a single newline. Add another
1202 * so that we leave room for the title and body.
1204 append_newlines
= "\n";
1205 } else if (msgbuf
->buf
[len
- 2] != '\n') {
1207 * Buffer ends with a single newline. Add another
1208 * so that there is an empty line between the message
1211 append_newlines
= "\n";
1212 } /* else, the buffer already ends with two newlines. */
1214 if (append_newlines
)
1215 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1216 append_newlines
, strlen(append_newlines
));
1219 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
1220 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1223 strbuf_release(&sob
);