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_addf(&seq_dir
, "%s", 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 void 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
,
189 if (write_in_full(msg_fd
, msgbuf
->buf
, msgbuf
->len
) < 0)
190 die_errno(_("Could not write to %s"), filename
);
191 strbuf_release(msgbuf
);
192 if (commit_lock_file(&msg_file
) < 0)
193 die(_("Error wrapping up %s"), filename
);
196 static struct tree
*empty_tree(void)
198 return lookup_tree(EMPTY_TREE_SHA1_BIN
);
201 static int error_dirty_index(struct replay_opts
*opts
)
203 if (read_cache_unmerged())
204 return error_resolve_conflict(action_name(opts
));
206 /* Different translation strings for cherry-pick and revert */
207 if (opts
->action
== REPLAY_PICK
)
208 error(_("Your local changes would be overwritten by cherry-pick."));
210 error(_("Your local changes would be overwritten by revert."));
212 if (advice_commit_before_merge
)
213 advise(_("Commit your changes or stash them to proceed."));
217 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
,
218 int unborn
, struct replay_opts
*opts
)
220 struct ref_transaction
*transaction
;
221 struct strbuf sb
= STRBUF_INIT
;
222 struct strbuf err
= STRBUF_INIT
;
225 if (checkout_fast_forward(from
, to
, 1))
226 exit(128); /* the callee should have complained already */
228 strbuf_addf(&sb
, _("%s: fast-forward"), action_name(opts
));
230 transaction
= ref_transaction_begin(&err
);
232 ref_transaction_update(transaction
, "HEAD",
233 to
, unborn
? null_sha1
: from
,
235 ref_transaction_commit(transaction
, &err
)) {
236 ref_transaction_free(transaction
);
237 error("%s", err
.buf
);
239 strbuf_release(&err
);
244 strbuf_release(&err
);
245 ref_transaction_free(transaction
);
249 void append_conflicts_hint(struct strbuf
*msgbuf
)
253 strbuf_addch(msgbuf
, '\n');
254 strbuf_commented_addf(msgbuf
, "Conflicts:\n");
255 for (i
= 0; i
< active_nr
;) {
256 const struct cache_entry
*ce
= active_cache
[i
++];
258 strbuf_commented_addf(msgbuf
, "\t%s\n", ce
->name
);
259 while (i
< active_nr
&& !strcmp(ce
->name
,
260 active_cache
[i
]->name
))
266 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
267 const char *base_label
, const char *next_label
,
268 unsigned char *head
, struct strbuf
*msgbuf
,
269 struct replay_opts
*opts
)
271 struct merge_options o
;
272 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
275 static struct lock_file index_lock
;
277 hold_locked_index(&index_lock
, 1);
281 init_merge_options(&o
);
282 o
.ancestor
= base
? base_label
: "(empty tree)";
284 o
.branch2
= next
? next_label
: "(empty tree)";
286 head_tree
= parse_tree_indirect(head
);
287 next_tree
= next
? next
->tree
: empty_tree();
288 base_tree
= base
? base
->tree
: empty_tree();
290 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
291 parse_merge_opt(&o
, *xopt
);
293 clean
= merge_trees(&o
,
295 next_tree
, base_tree
, &result
);
297 if (active_cache_changed
&&
298 write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
299 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
300 die(_("%s: Unable to write new index file"), action_name(opts
));
301 rollback_lock_file(&index_lock
);
304 append_signoff(msgbuf
, 0, 0);
307 append_conflicts_hint(msgbuf
);
312 static int is_index_unchanged(void)
314 unsigned char head_sha1
[20];
315 struct commit
*head_commit
;
317 if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING
, head_sha1
, NULL
))
318 return error(_("Could not resolve HEAD commit\n"));
320 head_commit
= lookup_commit(head_sha1
);
323 * If head_commit is NULL, check_commit, called from
324 * lookup_commit, would have indicated that head_commit is not
325 * a commit object already. parse_commit() will return failure
326 * without further complaints in such a case. Otherwise, if
327 * the commit is invalid, parse_commit() will complain. So
328 * there is nothing for us to say here. Just return failure.
330 if (parse_commit(head_commit
))
333 if (!active_cache_tree
)
334 active_cache_tree
= cache_tree();
336 if (!cache_tree_fully_valid(active_cache_tree
))
337 if (cache_tree_update(&the_index
, 0))
338 return error(_("Unable to update cache tree\n"));
340 return !hashcmp(active_cache_tree
->sha1
, head_commit
->tree
->object
.oid
.hash
);
344 * If we are cherry-pick, and if the merge did not result in
345 * hand-editing, we will hit this commit and inherit the original
346 * author date and name.
347 * If we are revert, or if our cherry-pick results in a hand merge,
348 * we had better say that the current user is responsible for that.
350 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
353 struct argv_array array
;
357 argv_array_init(&array
);
358 argv_array_push(&array
, "commit");
359 argv_array_push(&array
, "-n");
362 argv_array_pushf(&array
, "-S%s", opts
->gpg_sign
);
364 argv_array_push(&array
, "-s");
366 argv_array_push(&array
, "-F");
367 argv_array_push(&array
, defmsg
);
368 if (!opts
->signoff
&&
369 !opts
->record_origin
&&
370 git_config_get_value("commit.cleanup", &value
))
371 argv_array_push(&array
, "--cleanup=verbatim");
375 argv_array_push(&array
, "--allow-empty");
377 if (opts
->allow_empty_message
)
378 argv_array_push(&array
, "--allow-empty-message");
380 rc
= run_command_v_opt(array
.argv
, RUN_GIT_CMD
);
381 argv_array_clear(&array
);
385 static int is_original_commit_empty(struct commit
*commit
)
387 const unsigned char *ptree_sha1
;
389 if (parse_commit(commit
))
390 return error(_("Could not parse commit %s\n"),
391 oid_to_hex(&commit
->object
.oid
));
392 if (commit
->parents
) {
393 struct commit
*parent
= commit
->parents
->item
;
394 if (parse_commit(parent
))
395 return error(_("Could not parse parent commit %s\n"),
396 oid_to_hex(&parent
->object
.oid
));
397 ptree_sha1
= parent
->tree
->object
.oid
.hash
;
399 ptree_sha1
= EMPTY_TREE_SHA1_BIN
; /* commit is root */
402 return !hashcmp(ptree_sha1
, commit
->tree
->object
.oid
.hash
);
406 * Do we run "git commit" with "--allow-empty"?
408 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
410 int index_unchanged
, empty_commit
;
415 * (1) we do not allow empty at all and error out.
417 * (2) we allow ones that were initially empty, but
418 * forbid the ones that become empty;
422 if (!opts
->allow_empty
)
423 return 0; /* let "git commit" barf as necessary */
425 index_unchanged
= is_index_unchanged();
426 if (index_unchanged
< 0)
427 return index_unchanged
;
428 if (!index_unchanged
)
429 return 0; /* we do not have to say --allow-empty */
431 if (opts
->keep_redundant_commits
)
434 empty_commit
= is_original_commit_empty(commit
);
435 if (empty_commit
< 0)
443 static int do_pick_commit(struct commit
*commit
, struct replay_opts
*opts
)
445 unsigned char head
[20];
446 struct commit
*base
, *next
, *parent
;
447 const char *base_label
, *next_label
;
448 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
};
449 struct strbuf msgbuf
= STRBUF_INIT
;
450 int res
, unborn
= 0, allow
;
452 if (opts
->no_commit
) {
454 * We do not intend to commit immediately. We just want to
455 * merge the differences in, so let's compute the tree
456 * that represents the "current" state for merge-recursive
459 if (write_cache_as_tree(head
, 0, NULL
))
460 die (_("Your index file is unmerged."));
462 unborn
= get_sha1("HEAD", head
);
464 hashcpy(head
, EMPTY_TREE_SHA1_BIN
);
465 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD", 0))
466 return error_dirty_index(opts
);
470 if (!commit
->parents
) {
473 else if (commit
->parents
->next
) {
474 /* Reverting or cherry-picking a merge commit */
476 struct commit_list
*p
;
479 return error(_("Commit %s is a merge but no -m option was given."),
480 oid_to_hex(&commit
->object
.oid
));
482 for (cnt
= 1, p
= commit
->parents
;
483 cnt
!= opts
->mainline
&& p
;
486 if (cnt
!= opts
->mainline
|| !p
)
487 return error(_("Commit %s does not have parent %d"),
488 oid_to_hex(&commit
->object
.oid
), opts
->mainline
);
490 } else if (0 < opts
->mainline
)
491 return error(_("Mainline was specified but commit %s is not a merge."),
492 oid_to_hex(&commit
->object
.oid
));
494 parent
= commit
->parents
->item
;
496 if (opts
->allow_ff
&&
497 ((parent
&& !hashcmp(parent
->object
.oid
.hash
, head
)) ||
498 (!parent
&& unborn
)))
499 return fast_forward_to(commit
->object
.oid
.hash
, head
, unborn
, opts
);
501 if (parent
&& parse_commit(parent
) < 0)
502 /* TRANSLATORS: The first %s will be "revert" or
503 "cherry-pick", the second %s a SHA1 */
504 return error(_("%s: cannot parse parent commit %s"),
505 action_name(opts
), oid_to_hex(&parent
->object
.oid
));
507 if (get_message(commit
, &msg
) != 0)
508 return error(_("Cannot get commit message for %s"),
509 oid_to_hex(&commit
->object
.oid
));
512 * "commit" is an existing commit. We would want to apply
513 * the difference it introduces since its first parent "prev"
514 * on top of the current HEAD if we are cherry-pick. Or the
515 * reverse of it if we are revert.
518 if (opts
->action
== REPLAY_REVERT
) {
520 base_label
= msg
.label
;
522 next_label
= msg
.parent_label
;
523 strbuf_addstr(&msgbuf
, "Revert \"");
524 strbuf_addstr(&msgbuf
, msg
.subject
);
525 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
526 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
528 if (commit
->parents
&& commit
->parents
->next
) {
529 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
530 strbuf_addstr(&msgbuf
, oid_to_hex(&parent
->object
.oid
));
532 strbuf_addstr(&msgbuf
, ".\n");
537 base_label
= msg
.parent_label
;
539 next_label
= msg
.label
;
542 * Append the commit log message to msgbuf; it starts
543 * after the tree, parent, author, committer
544 * information followed by "\n\n".
546 p
= strstr(msg
.message
, "\n\n");
549 strbuf_addstr(&msgbuf
, p
);
552 if (opts
->record_origin
) {
553 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
554 strbuf_addch(&msgbuf
, '\n');
555 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
556 strbuf_addstr(&msgbuf
, oid_to_hex(&commit
->object
.oid
));
557 strbuf_addstr(&msgbuf
, ")\n");
561 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || opts
->action
== REPLAY_REVERT
) {
562 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
563 head
, &msgbuf
, opts
);
564 write_message(&msgbuf
, git_path_merge_msg());
566 struct commit_list
*common
= NULL
;
567 struct commit_list
*remotes
= NULL
;
569 write_message(&msgbuf
, git_path_merge_msg());
571 commit_list_insert(base
, &common
);
572 commit_list_insert(next
, &remotes
);
573 res
= try_merge_command(opts
->strategy
, opts
->xopts_nr
, opts
->xopts
,
574 common
, sha1_to_hex(head
), remotes
);
575 free_commit_list(common
);
576 free_commit_list(remotes
);
580 * If the merge was clean or if it failed due to conflict, we write
581 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
582 * However, if the merge did not even start, then we don't want to
585 if (opts
->action
== REPLAY_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1))
586 update_ref(NULL
, "CHERRY_PICK_HEAD", commit
->object
.oid
.hash
, NULL
,
587 REF_NODEREF
, UPDATE_REFS_DIE_ON_ERR
);
588 if (opts
->action
== REPLAY_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1))
589 update_ref(NULL
, "REVERT_HEAD", commit
->object
.oid
.hash
, NULL
,
590 REF_NODEREF
, UPDATE_REFS_DIE_ON_ERR
);
593 error(opts
->action
== REPLAY_REVERT
594 ? _("could not revert %s... %s")
595 : _("could not apply %s... %s"),
596 find_unique_abbrev(commit
->object
.oid
.hash
, DEFAULT_ABBREV
),
598 print_advice(res
== 1, opts
);
599 rerere(opts
->allow_rerere_auto
);
603 allow
= allow_empty(opts
, commit
);
608 if (!opts
->no_commit
)
609 res
= run_git_commit(git_path_merge_msg(), opts
, allow
);
612 free_message(commit
, &msg
);
617 static void prepare_revs(struct replay_opts
*opts
)
620 * picking (but not reverting) ranges (but not individual revisions)
621 * should be done in reverse
623 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
624 opts
->revs
->reverse
^= 1;
626 if (prepare_revision_walk(opts
->revs
))
627 die(_("revision walk setup failed"));
629 if (!opts
->revs
->commits
)
630 die(_("empty commit set passed"));
633 static void read_and_refresh_cache(struct replay_opts
*opts
)
635 static struct lock_file index_lock
;
636 int index_fd
= hold_locked_index(&index_lock
, 0);
637 if (read_index_preload(&the_index
, NULL
) < 0)
638 die(_("git %s: failed to read the index"), action_name(opts
));
639 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
640 if (the_index
.cache_changed
&& index_fd
>= 0) {
641 if (write_locked_index(&the_index
, &index_lock
, COMMIT_LOCK
))
642 die(_("git %s: failed to refresh the index"), action_name(opts
));
644 rollback_lock_file(&index_lock
);
647 static int format_todo(struct strbuf
*buf
, struct commit_list
*todo_list
,
648 struct replay_opts
*opts
)
650 struct commit_list
*cur
= NULL
;
651 const char *sha1_abbrev
= NULL
;
652 const char *action_str
= opts
->action
== REPLAY_REVERT
? "revert" : "pick";
656 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
657 const char *commit_buffer
= get_commit_buffer(cur
->item
, NULL
);
658 sha1_abbrev
= find_unique_abbrev(cur
->item
->object
.oid
.hash
, DEFAULT_ABBREV
);
659 subject_len
= find_commit_subject(commit_buffer
, &subject
);
660 strbuf_addf(buf
, "%s %s %.*s\n", action_str
, sha1_abbrev
,
661 subject_len
, subject
);
662 unuse_commit_buffer(cur
->item
, commit_buffer
);
667 static struct commit
*parse_insn_line(char *bol
, char *eol
, struct replay_opts
*opts
)
669 unsigned char commit_sha1
[20];
670 enum replay_action action
;
671 char *end_of_object_name
;
672 int saved
, status
, padding
;
674 if (starts_with(bol
, "pick")) {
675 action
= REPLAY_PICK
;
676 bol
+= strlen("pick");
677 } else if (starts_with(bol
, "revert")) {
678 action
= REPLAY_REVERT
;
679 bol
+= strlen("revert");
683 /* Eat up extra spaces/ tabs before object name */
684 padding
= strspn(bol
, " \t");
689 end_of_object_name
= bol
+ strcspn(bol
, " \t\n");
690 saved
= *end_of_object_name
;
691 *end_of_object_name
= '\0';
692 status
= get_sha1(bol
, commit_sha1
);
693 *end_of_object_name
= saved
;
696 * Verify that the action matches up with the one in
697 * opts; we don't support arbitrary instructions
699 if (action
!= opts
->action
) {
700 if (action
== REPLAY_REVERT
)
701 error((opts
->action
== REPLAY_REVERT
)
702 ? _("Cannot revert during a another revert.")
703 : _("Cannot revert during a cherry-pick."));
705 error((opts
->action
== REPLAY_REVERT
)
706 ? _("Cannot cherry-pick during a revert.")
707 : _("Cannot cherry-pick during another cherry-pick."));
714 return lookup_commit_reference(commit_sha1
);
717 static int parse_insn_buffer(char *buf
, struct commit_list
**todo_list
,
718 struct replay_opts
*opts
)
720 struct commit_list
**next
= todo_list
;
721 struct commit
*commit
;
725 for (i
= 1; *p
; i
++) {
726 char *eol
= strchrnul(p
, '\n');
727 commit
= parse_insn_line(p
, eol
, opts
);
729 return error(_("Could not parse line %d."), i
);
730 next
= commit_list_append(commit
, next
);
731 p
= *eol
? eol
+ 1 : eol
;
734 return error(_("No commits parsed."));
738 static void read_populate_todo(struct commit_list
**todo_list
,
739 struct replay_opts
*opts
)
741 struct strbuf buf
= STRBUF_INIT
;
744 fd
= open(git_path_todo_file(), O_RDONLY
);
746 die_errno(_("Could not open %s"), git_path_todo_file());
747 if (strbuf_read(&buf
, fd
, 0) < 0) {
749 strbuf_release(&buf
);
750 die(_("Could not read %s."), git_path_todo_file());
754 res
= parse_insn_buffer(buf
.buf
, todo_list
, opts
);
755 strbuf_release(&buf
);
757 die(_("Unusable instruction sheet: %s"), git_path_todo_file());
760 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
762 struct replay_opts
*opts
= data
;
767 else if (!strcmp(key
, "options.no-commit"))
768 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
769 else if (!strcmp(key
, "options.edit"))
770 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
771 else if (!strcmp(key
, "options.signoff"))
772 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
773 else if (!strcmp(key
, "options.record-origin"))
774 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
775 else if (!strcmp(key
, "options.allow-ff"))
776 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
777 else if (!strcmp(key
, "options.mainline"))
778 opts
->mainline
= git_config_int(key
, value
);
779 else if (!strcmp(key
, "options.strategy"))
780 git_config_string(&opts
->strategy
, key
, value
);
781 else if (!strcmp(key
, "options.gpg-sign"))
782 git_config_string(&opts
->gpg_sign
, key
, value
);
783 else if (!strcmp(key
, "options.strategy-option")) {
784 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
785 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
787 return error(_("Invalid key: %s"), key
);
790 return error(_("Invalid value for %s: %s"), key
, value
);
795 static void read_populate_opts(struct replay_opts
**opts_ptr
)
797 if (!file_exists(git_path_opts_file()))
799 if (git_config_from_file(populate_opts_cb
, git_path_opts_file(), *opts_ptr
) < 0)
800 die(_("Malformed options sheet: %s"), git_path_opts_file());
803 static void walk_revs_populate_todo(struct commit_list
**todo_list
,
804 struct replay_opts
*opts
)
806 struct commit
*commit
;
807 struct commit_list
**next
;
812 while ((commit
= get_revision(opts
->revs
)))
813 next
= commit_list_append(commit
, next
);
816 static int create_seq_dir(void)
818 if (file_exists(git_path_seq_dir())) {
819 error(_("a cherry-pick or revert is already in progress"));
820 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
823 else if (mkdir(git_path_seq_dir(), 0777) < 0)
824 die_errno(_("Could not create sequencer directory %s"),
829 static void save_head(const char *head
)
831 static struct lock_file head_lock
;
832 struct strbuf buf
= STRBUF_INIT
;
835 fd
= hold_lock_file_for_update(&head_lock
, git_path_head_file(), LOCK_DIE_ON_ERROR
);
836 strbuf_addf(&buf
, "%s\n", head
);
837 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0)
838 die_errno(_("Could not write to %s"), git_path_head_file());
839 if (commit_lock_file(&head_lock
) < 0)
840 die(_("Error wrapping up %s."), git_path_head_file());
843 static int reset_for_rollback(const unsigned char *sha1
)
845 const char *argv
[4]; /* reset --merge <arg> + NULL */
848 argv
[2] = sha1_to_hex(sha1
);
850 return run_command_v_opt(argv
, RUN_GIT_CMD
);
853 static int rollback_single_pick(void)
855 unsigned char head_sha1
[20];
857 if (!file_exists(git_path_cherry_pick_head()) &&
858 !file_exists(git_path_revert_head()))
859 return error(_("no cherry-pick or revert in progress"));
860 if (read_ref_full("HEAD", 0, head_sha1
, NULL
))
861 return error(_("cannot resolve HEAD"));
862 if (is_null_sha1(head_sha1
))
863 return error(_("cannot abort from a branch yet to be born"));
864 return reset_for_rollback(head_sha1
);
867 static int sequencer_rollback(struct replay_opts
*opts
)
870 unsigned char sha1
[20];
871 struct strbuf buf
= STRBUF_INIT
;
873 f
= fopen(git_path_head_file(), "r");
874 if (!f
&& errno
== ENOENT
) {
876 * There is no multiple-cherry-pick in progress.
877 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
878 * a single-cherry-pick in progress, abort that.
880 return rollback_single_pick();
883 return error_errno(_("cannot open %s"), git_path_head_file());
884 if (strbuf_getline_lf(&buf
, f
)) {
885 error(_("cannot read %s: %s"), git_path_head_file(),
886 ferror(f
) ? strerror(errno
) : _("unexpected end of file"));
891 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
892 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
893 git_path_head_file());
896 if (reset_for_rollback(sha1
))
898 remove_sequencer_state();
899 strbuf_release(&buf
);
902 strbuf_release(&buf
);
906 static void save_todo(struct commit_list
*todo_list
, struct replay_opts
*opts
)
908 static struct lock_file todo_lock
;
909 struct strbuf buf
= STRBUF_INIT
;
912 fd
= hold_lock_file_for_update(&todo_lock
, git_path_todo_file(), LOCK_DIE_ON_ERROR
);
913 if (format_todo(&buf
, todo_list
, opts
) < 0)
914 die(_("Could not format %s."), git_path_todo_file());
915 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
916 strbuf_release(&buf
);
917 die_errno(_("Could not write to %s"), git_path_todo_file());
919 if (commit_lock_file(&todo_lock
) < 0) {
920 strbuf_release(&buf
);
921 die(_("Error wrapping up %s."), git_path_todo_file());
923 strbuf_release(&buf
);
926 static void save_opts(struct replay_opts
*opts
)
928 const char *opts_file
= git_path_opts_file();
931 git_config_set_in_file(opts_file
, "options.no-commit", "true");
933 git_config_set_in_file(opts_file
, "options.edit", "true");
935 git_config_set_in_file(opts_file
, "options.signoff", "true");
936 if (opts
->record_origin
)
937 git_config_set_in_file(opts_file
, "options.record-origin", "true");
939 git_config_set_in_file(opts_file
, "options.allow-ff", "true");
940 if (opts
->mainline
) {
941 struct strbuf buf
= STRBUF_INIT
;
942 strbuf_addf(&buf
, "%d", opts
->mainline
);
943 git_config_set_in_file(opts_file
, "options.mainline", buf
.buf
);
944 strbuf_release(&buf
);
947 git_config_set_in_file(opts_file
, "options.strategy", opts
->strategy
);
949 git_config_set_in_file(opts_file
, "options.gpg-sign", opts
->gpg_sign
);
952 for (i
= 0; i
< opts
->xopts_nr
; i
++)
953 git_config_set_multivar_in_file(opts_file
,
954 "options.strategy-option",
955 opts
->xopts
[i
], "^$", 0);
959 static int pick_commits(struct commit_list
*todo_list
, struct replay_opts
*opts
)
961 struct commit_list
*cur
;
964 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
966 assert(!(opts
->signoff
|| opts
->no_commit
||
967 opts
->record_origin
|| opts
->edit
));
968 read_and_refresh_cache(opts
);
970 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
971 save_todo(cur
, opts
);
972 res
= do_pick_commit(cur
->item
, opts
);
978 * Sequence of picks finished successfully; cleanup by
979 * removing the .git/sequencer directory
981 remove_sequencer_state();
985 static int continue_single_pick(void)
987 const char *argv
[] = { "commit", NULL
};
989 if (!file_exists(git_path_cherry_pick_head()) &&
990 !file_exists(git_path_revert_head()))
991 return error(_("no cherry-pick or revert in progress"));
992 return run_command_v_opt(argv
, RUN_GIT_CMD
);
995 static int sequencer_continue(struct replay_opts
*opts
)
997 struct commit_list
*todo_list
= NULL
;
999 if (!file_exists(git_path_todo_file()))
1000 return continue_single_pick();
1001 read_populate_opts(&opts
);
1002 read_populate_todo(&todo_list
, opts
);
1004 /* Verify that the conflict has been resolved */
1005 if (file_exists(git_path_cherry_pick_head()) ||
1006 file_exists(git_path_revert_head())) {
1007 int ret
= continue_single_pick();
1011 if (index_differs_from("HEAD", 0))
1012 return error_dirty_index(opts
);
1013 todo_list
= todo_list
->next
;
1014 return pick_commits(todo_list
, opts
);
1017 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1019 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1020 return do_pick_commit(cmit
, opts
);
1023 int sequencer_pick_revisions(struct replay_opts
*opts
)
1025 struct commit_list
*todo_list
= NULL
;
1026 unsigned char sha1
[20];
1029 if (opts
->subcommand
== REPLAY_NONE
)
1032 read_and_refresh_cache(opts
);
1035 * Decide what to do depending on the arguments; a fresh
1036 * cherry-pick should be handled differently from an existing
1037 * one that is being continued
1039 if (opts
->subcommand
== REPLAY_REMOVE_STATE
) {
1040 remove_sequencer_state();
1043 if (opts
->subcommand
== REPLAY_ROLLBACK
)
1044 return sequencer_rollback(opts
);
1045 if (opts
->subcommand
== REPLAY_CONTINUE
)
1046 return sequencer_continue(opts
);
1048 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
1049 unsigned char sha1
[20];
1050 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
1052 /* This happens when using --stdin. */
1056 if (!get_sha1(name
, sha1
)) {
1057 if (!lookup_commit_reference_gently(sha1
, 1)) {
1058 enum object_type type
= sha1_object_info(sha1
, NULL
);
1059 die(_("%s: can't cherry-pick a %s"), name
, typename(type
));
1062 die(_("%s: bad revision"), name
);
1066 * If we were called as "git cherry-pick <commit>", just
1067 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1068 * REVERT_HEAD, and don't touch the sequencer state.
1069 * This means it is possible to cherry-pick in the middle
1070 * of a cherry-pick sequence.
1072 if (opts
->revs
->cmdline
.nr
== 1 &&
1073 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
1074 opts
->revs
->no_walk
&&
1075 !opts
->revs
->cmdline
.rev
->flags
) {
1076 struct commit
*cmit
;
1077 if (prepare_revision_walk(opts
->revs
))
1078 die(_("revision walk setup failed"));
1079 cmit
= get_revision(opts
->revs
);
1080 if (!cmit
|| get_revision(opts
->revs
))
1081 die("BUG: expected exactly one commit from walk");
1082 return single_pick(cmit
, opts
);
1086 * Start a new cherry-pick/ revert sequence; but
1087 * first, make sure that an existing one isn't in
1091 walk_revs_populate_todo(&todo_list
, opts
);
1092 if (create_seq_dir() < 0)
1094 if (get_sha1("HEAD", sha1
)) {
1095 if (opts
->action
== REPLAY_REVERT
)
1096 return error(_("Can't revert as initial commit"));
1097 return error(_("Can't cherry-pick into empty head"));
1099 save_head(sha1_to_hex(sha1
));
1101 return pick_commits(todo_list
, opts
);
1104 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
1106 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
1107 struct strbuf sob
= STRBUF_INIT
;
1110 strbuf_addstr(&sob
, sign_off_header
);
1111 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
1112 getenv("GIT_COMMITTER_EMAIL")));
1113 strbuf_addch(&sob
, '\n');
1116 * If the whole message buffer is equal to the sob, pretend that we
1117 * found a conforming footer with a matching sob
1119 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
1120 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
1123 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
1126 const char *append_newlines
= NULL
;
1127 size_t len
= msgbuf
->len
- ignore_footer
;
1131 * The buffer is completely empty. Leave foom for
1132 * the title and body to be filled in by the user.
1134 append_newlines
= "\n\n";
1135 } else if (msgbuf
->buf
[len
- 1] != '\n') {
1137 * Incomplete line. Complete the line and add a
1138 * blank one so that there is an empty line between
1139 * the message body and the sob.
1141 append_newlines
= "\n\n";
1142 } else if (len
== 1) {
1144 * Buffer contains a single newline. Add another
1145 * so that we leave room for the title and body.
1147 append_newlines
= "\n";
1148 } else if (msgbuf
->buf
[len
- 2] != '\n') {
1150 * Buffer ends with a single newline. Add another
1151 * so that there is an empty line between the message
1154 append_newlines
= "\n";
1155 } /* else, the buffer already ends with two newlines. */
1157 if (append_newlines
)
1158 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1159 append_newlines
, strlen(append_newlines
));
1162 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
1163 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1166 strbuf_release(&sob
);