7 #include "run-command.h"
10 #include "cache-tree.h"
14 #include "merge-recursive.h"
16 #include "argv-array.h"
18 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
20 const char sign_off_header
[] = "Signed-off-by: ";
21 static const char cherry_picked_prefix
[] = "(cherry picked from commit ";
23 static int is_rfc2822_line(const char *buf
, int len
)
27 for (i
= 0; i
< len
; i
++) {
31 if (!isalnum(ch
) && ch
!= '-')
38 static int is_cherry_picked_from_line(const char *buf
, int len
)
41 * We only care that it looks roughly like (cherry picked from ...)
43 return len
> strlen(cherry_picked_prefix
) + 1 &&
44 !prefixcmp(buf
, cherry_picked_prefix
) && buf
[len
- 1] == ')';
48 * Returns 0 for non-conforming footer
49 * Returns 1 for conforming footer
50 * Returns 2 when sob exists within conforming footer
51 * Returns 3 when sob exists within conforming footer as last entry
53 static int has_conforming_footer(struct strbuf
*sb
, struct strbuf
*sob
,
58 int len
= sb
->len
- ignore_footer
;
59 const char *buf
= sb
->buf
;
62 /* footer must end with newline */
63 if (!len
|| buf
[len
- 1] != '\n')
67 for (i
= len
- 1; i
> 0; i
--) {
69 if (prev
== '\n' && ch
== '\n') /* paragraph break */
74 /* require at least one blank line */
75 if (prev
!= '\n' || buf
[i
] != '\n')
78 /* advance to start of last paragraph */
79 while (i
< len
- 1 && buf
[i
] == '\n')
82 for (; i
< len
; i
= k
) {
85 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
89 found_rfc2822
= is_rfc2822_line(buf
+ i
, k
- i
- 1);
90 if (found_rfc2822
&& sob
&&
91 !strncmp(buf
+ i
, sob
->buf
, sob
->len
))
94 if (!(found_rfc2822
||
95 is_cherry_picked_from_line(buf
+ i
, k
- i
- 1)))
105 static void remove_sequencer_state(void)
107 struct strbuf seq_dir
= STRBUF_INIT
;
109 strbuf_addf(&seq_dir
, "%s", git_path(SEQ_DIR
));
110 remove_dir_recursively(&seq_dir
, 0);
111 strbuf_release(&seq_dir
);
114 static const char *action_name(const struct replay_opts
*opts
)
116 return opts
->action
== REPLAY_REVERT
? "revert" : "cherry-pick";
119 static char *get_encoding(const char *message
);
121 struct commit_message
{
125 char *reencoded_message
;
129 static int get_message(struct commit
*commit
, struct commit_message
*out
)
131 const char *encoding
;
132 const char *abbrev
, *subject
;
133 int abbrev_len
, subject_len
;
138 encoding
= get_encoding(commit
->buffer
);
141 if (!git_commit_encoding
)
142 git_commit_encoding
= "UTF-8";
144 out
->reencoded_message
= NULL
;
145 out
->message
= commit
->buffer
;
146 if (same_encoding(encoding
, git_commit_encoding
))
147 out
->reencoded_message
= reencode_string(commit
->buffer
,
148 git_commit_encoding
, encoding
);
149 if (out
->reencoded_message
)
150 out
->message
= out
->reencoded_message
;
152 abbrev
= find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
);
153 abbrev_len
= strlen(abbrev
);
155 subject_len
= find_commit_subject(out
->message
, &subject
);
157 out
->parent_label
= xmalloc(strlen("parent of ") + abbrev_len
+
158 strlen("... ") + subject_len
+ 1);
159 q
= out
->parent_label
;
160 q
= mempcpy(q
, "parent of ", strlen("parent of "));
162 q
= mempcpy(q
, abbrev
, abbrev_len
);
163 q
= mempcpy(q
, "... ", strlen("... "));
165 q
= mempcpy(q
, subject
, subject_len
);
170 static void free_message(struct commit_message
*msg
)
172 free(msg
->parent_label
);
173 free(msg
->reencoded_message
);
176 static char *get_encoding(const char *message
)
178 const char *p
= message
, *eol
;
180 while (*p
&& *p
!= '\n') {
181 for (eol
= p
+ 1; *eol
&& *eol
!= '\n'; eol
++)
183 if (!prefixcmp(p
, "encoding ")) {
184 char *result
= xmalloc(eol
- 8 - p
);
185 strlcpy(result
, p
+ 9, eol
- 8 - p
);
195 static void write_cherry_pick_head(struct commit
*commit
, const char *pseudoref
)
197 const char *filename
;
199 struct strbuf buf
= STRBUF_INIT
;
201 strbuf_addf(&buf
, "%s\n", sha1_to_hex(commit
->object
.sha1
));
203 filename
= git_path("%s", pseudoref
);
204 fd
= open(filename
, O_WRONLY
| O_CREAT
, 0666);
206 die_errno(_("Could not open '%s' for writing"), filename
);
207 if (write_in_full(fd
, buf
.buf
, buf
.len
) != buf
.len
|| close(fd
))
208 die_errno(_("Could not write to '%s'"), filename
);
209 strbuf_release(&buf
);
212 static void print_advice(int show_hint
, struct replay_opts
*opts
)
214 char *msg
= getenv("GIT_CHERRY_PICK_HELP");
217 fprintf(stderr
, "%s\n", msg
);
219 * A conflict has occured but the porcelain
220 * (typically rebase --interactive) wants to take care
221 * of the commit itself so remove CHERRY_PICK_HEAD
223 unlink(git_path("CHERRY_PICK_HEAD"));
229 advise(_("after resolving the conflicts, mark the corrected paths\n"
230 "with 'git add <paths>' or 'git rm <paths>'"));
232 advise(_("after resolving the conflicts, mark the corrected paths\n"
233 "with 'git add <paths>' or 'git rm <paths>'\n"
234 "and commit the result with 'git commit'"));
238 static void write_message(struct strbuf
*msgbuf
, const char *filename
)
240 static struct lock_file msg_file
;
242 int msg_fd
= hold_lock_file_for_update(&msg_file
, filename
,
244 if (write_in_full(msg_fd
, msgbuf
->buf
, msgbuf
->len
) < 0)
245 die_errno(_("Could not write to %s"), filename
);
246 strbuf_release(msgbuf
);
247 if (commit_lock_file(&msg_file
) < 0)
248 die(_("Error wrapping up %s"), filename
);
251 static struct tree
*empty_tree(void)
253 return lookup_tree(EMPTY_TREE_SHA1_BIN
);
256 static int error_dirty_index(struct replay_opts
*opts
)
258 if (read_cache_unmerged())
259 return error_resolve_conflict(action_name(opts
));
261 /* Different translation strings for cherry-pick and revert */
262 if (opts
->action
== REPLAY_PICK
)
263 error(_("Your local changes would be overwritten by cherry-pick."));
265 error(_("Your local changes would be overwritten by revert."));
267 if (advice_commit_before_merge
)
268 advise(_("Commit your changes or stash them to proceed."));
272 static int fast_forward_to(const unsigned char *to
, const unsigned char *from
)
274 struct ref_lock
*ref_lock
;
277 if (checkout_fast_forward(from
, to
, 1))
278 exit(1); /* the callee should have complained already */
279 ref_lock
= lock_any_ref_for_update("HEAD", from
, 0);
280 return write_ref_sha1(ref_lock
, to
, "cherry-pick");
283 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
284 const char *base_label
, const char *next_label
,
285 unsigned char *head
, struct strbuf
*msgbuf
,
286 struct replay_opts
*opts
)
288 struct merge_options o
;
289 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
292 static struct lock_file index_lock
;
294 index_fd
= hold_locked_index(&index_lock
, 1);
298 init_merge_options(&o
);
299 o
.ancestor
= base
? base_label
: "(empty tree)";
301 o
.branch2
= next
? next_label
: "(empty tree)";
303 head_tree
= parse_tree_indirect(head
);
304 next_tree
= next
? next
->tree
: empty_tree();
305 base_tree
= base
? base
->tree
: empty_tree();
307 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
308 parse_merge_opt(&o
, *xopt
);
310 clean
= merge_trees(&o
,
312 next_tree
, base_tree
, &result
);
314 if (active_cache_changed
&&
315 (write_cache(index_fd
, active_cache
, active_nr
) ||
316 commit_locked_index(&index_lock
)))
317 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
318 die(_("%s: Unable to write new index file"), action_name(opts
));
319 rollback_lock_file(&index_lock
);
322 append_signoff(msgbuf
, 0, 0);
326 strbuf_addstr(msgbuf
, "\nConflicts:\n");
327 for (i
= 0; i
< active_nr
;) {
328 struct cache_entry
*ce
= active_cache
[i
++];
330 strbuf_addch(msgbuf
, '\t');
331 strbuf_addstr(msgbuf
, ce
->name
);
332 strbuf_addch(msgbuf
, '\n');
333 while (i
< active_nr
&& !strcmp(ce
->name
,
334 active_cache
[i
]->name
))
343 static int is_index_unchanged(void)
345 unsigned char head_sha1
[20];
346 struct commit
*head_commit
;
348 if (!resolve_ref_unsafe("HEAD", head_sha1
, 1, NULL
))
349 return error(_("Could not resolve HEAD commit\n"));
351 head_commit
= lookup_commit(head_sha1
);
354 * If head_commit is NULL, check_commit, called from
355 * lookup_commit, would have indicated that head_commit is not
356 * a commit object already. parse_commit() will return failure
357 * without further complaints in such a case. Otherwise, if
358 * the commit is invalid, parse_commit() will complain. So
359 * there is nothing for us to say here. Just return failure.
361 if (parse_commit(head_commit
))
364 if (!active_cache_tree
)
365 active_cache_tree
= cache_tree();
367 if (!cache_tree_fully_valid(active_cache_tree
))
368 if (cache_tree_update(active_cache_tree
, active_cache
,
370 return error(_("Unable to update cache tree\n"));
372 return !hashcmp(active_cache_tree
->sha1
, head_commit
->tree
->object
.sha1
);
376 * If we are cherry-pick, and if the merge did not result in
377 * hand-editing, we will hit this commit and inherit the original
378 * author date and name.
379 * If we are revert, or if our cherry-pick results in a hand merge,
380 * we had better say that the current user is responsible for that.
382 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
385 struct argv_array array
;
388 argv_array_init(&array
);
389 argv_array_push(&array
, "commit");
390 argv_array_push(&array
, "-n");
393 argv_array_push(&array
, "-s");
395 argv_array_push(&array
, "-F");
396 argv_array_push(&array
, defmsg
);
400 argv_array_push(&array
, "--allow-empty");
402 if (opts
->allow_empty_message
)
403 argv_array_push(&array
, "--allow-empty-message");
405 rc
= run_command_v_opt(array
.argv
, RUN_GIT_CMD
);
406 argv_array_clear(&array
);
410 static int is_original_commit_empty(struct commit
*commit
)
412 const unsigned char *ptree_sha1
;
414 if (parse_commit(commit
))
415 return error(_("Could not parse commit %s\n"),
416 sha1_to_hex(commit
->object
.sha1
));
417 if (commit
->parents
) {
418 struct commit
*parent
= commit
->parents
->item
;
419 if (parse_commit(parent
))
420 return error(_("Could not parse parent commit %s\n"),
421 sha1_to_hex(parent
->object
.sha1
));
422 ptree_sha1
= parent
->tree
->object
.sha1
;
424 ptree_sha1
= EMPTY_TREE_SHA1_BIN
; /* commit is root */
427 return !hashcmp(ptree_sha1
, commit
->tree
->object
.sha1
);
431 * Do we run "git commit" with "--allow-empty"?
433 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
435 int index_unchanged
, empty_commit
;
440 * (1) we do not allow empty at all and error out.
442 * (2) we allow ones that were initially empty, but
443 * forbid the ones that become empty;
447 if (!opts
->allow_empty
)
448 return 0; /* let "git commit" barf as necessary */
450 index_unchanged
= is_index_unchanged();
451 if (index_unchanged
< 0)
452 return index_unchanged
;
453 if (!index_unchanged
)
454 return 0; /* we do not have to say --allow-empty */
456 if (opts
->keep_redundant_commits
)
459 empty_commit
= is_original_commit_empty(commit
);
460 if (empty_commit
< 0)
468 static int do_pick_commit(struct commit
*commit
, struct replay_opts
*opts
)
470 unsigned char head
[20];
471 struct commit
*base
, *next
, *parent
;
472 const char *base_label
, *next_label
;
473 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
, NULL
};
475 struct strbuf msgbuf
= STRBUF_INIT
;
478 if (opts
->no_commit
) {
480 * We do not intend to commit immediately. We just want to
481 * merge the differences in, so let's compute the tree
482 * that represents the "current" state for merge-recursive
485 if (write_cache_as_tree(head
, 0, NULL
))
486 die (_("Your index file is unmerged."));
488 if (get_sha1("HEAD", head
))
489 return error(_("You do not have a valid HEAD"));
490 if (index_differs_from("HEAD", 0))
491 return error_dirty_index(opts
);
495 if (!commit
->parents
) {
498 else if (commit
->parents
->next
) {
499 /* Reverting or cherry-picking a merge commit */
501 struct commit_list
*p
;
504 return error(_("Commit %s is a merge but no -m option was given."),
505 sha1_to_hex(commit
->object
.sha1
));
507 for (cnt
= 1, p
= commit
->parents
;
508 cnt
!= opts
->mainline
&& p
;
511 if (cnt
!= opts
->mainline
|| !p
)
512 return error(_("Commit %s does not have parent %d"),
513 sha1_to_hex(commit
->object
.sha1
), opts
->mainline
);
515 } else if (0 < opts
->mainline
)
516 return error(_("Mainline was specified but commit %s is not a merge."),
517 sha1_to_hex(commit
->object
.sha1
));
519 parent
= commit
->parents
->item
;
521 if (opts
->allow_ff
&& parent
&& !hashcmp(parent
->object
.sha1
, head
))
522 return fast_forward_to(commit
->object
.sha1
, head
);
524 if (parent
&& parse_commit(parent
) < 0)
525 /* TRANSLATORS: The first %s will be "revert" or
526 "cherry-pick", the second %s a SHA1 */
527 return error(_("%s: cannot parse parent commit %s"),
528 action_name(opts
), sha1_to_hex(parent
->object
.sha1
));
530 if (get_message(commit
, &msg
) != 0)
531 return error(_("Cannot get commit message for %s"),
532 sha1_to_hex(commit
->object
.sha1
));
535 * "commit" is an existing commit. We would want to apply
536 * the difference it introduces since its first parent "prev"
537 * on top of the current HEAD if we are cherry-pick. Or the
538 * reverse of it if we are revert.
541 defmsg
= git_pathdup("MERGE_MSG");
543 if (opts
->action
== REPLAY_REVERT
) {
545 base_label
= msg
.label
;
547 next_label
= msg
.parent_label
;
548 strbuf_addstr(&msgbuf
, "Revert \"");
549 strbuf_addstr(&msgbuf
, msg
.subject
);
550 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
551 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
553 if (commit
->parents
&& commit
->parents
->next
) {
554 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
555 strbuf_addstr(&msgbuf
, sha1_to_hex(parent
->object
.sha1
));
557 strbuf_addstr(&msgbuf
, ".\n");
562 base_label
= msg
.parent_label
;
564 next_label
= msg
.label
;
567 * Append the commit log message to msgbuf; it starts
568 * after the tree, parent, author, committer
569 * information followed by "\n\n".
571 p
= strstr(msg
.message
, "\n\n");
574 strbuf_addstr(&msgbuf
, p
);
577 if (opts
->record_origin
) {
578 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
579 strbuf_addch(&msgbuf
, '\n');
580 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
581 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
582 strbuf_addstr(&msgbuf
, ")\n");
586 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || opts
->action
== REPLAY_REVERT
) {
587 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
588 head
, &msgbuf
, opts
);
589 write_message(&msgbuf
, defmsg
);
591 struct commit_list
*common
= NULL
;
592 struct commit_list
*remotes
= NULL
;
594 write_message(&msgbuf
, defmsg
);
596 commit_list_insert(base
, &common
);
597 commit_list_insert(next
, &remotes
);
598 res
= try_merge_command(opts
->strategy
, opts
->xopts_nr
, opts
->xopts
,
599 common
, sha1_to_hex(head
), remotes
);
600 free_commit_list(common
);
601 free_commit_list(remotes
);
605 * If the merge was clean or if it failed due to conflict, we write
606 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
607 * However, if the merge did not even start, then we don't want to
610 if (opts
->action
== REPLAY_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1))
611 write_cherry_pick_head(commit
, "CHERRY_PICK_HEAD");
612 if (opts
->action
== REPLAY_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1))
613 write_cherry_pick_head(commit
, "REVERT_HEAD");
616 error(opts
->action
== REPLAY_REVERT
617 ? _("could not revert %s... %s")
618 : _("could not apply %s... %s"),
619 find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
),
621 print_advice(res
== 1, opts
);
622 rerere(opts
->allow_rerere_auto
);
624 int allow
= allow_empty(opts
, commit
);
627 if (!opts
->no_commit
)
628 res
= run_git_commit(defmsg
, opts
, allow
);
637 static void prepare_revs(struct replay_opts
*opts
)
640 * picking (but not reverting) ranges (but not individual revisions)
641 * should be done in reverse
643 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
644 opts
->revs
->reverse
^= 1;
646 if (prepare_revision_walk(opts
->revs
))
647 die(_("revision walk setup failed"));
649 if (!opts
->revs
->commits
)
650 die(_("empty commit set passed"));
653 static void read_and_refresh_cache(struct replay_opts
*opts
)
655 static struct lock_file index_lock
;
656 int index_fd
= hold_locked_index(&index_lock
, 0);
657 if (read_index_preload(&the_index
, NULL
) < 0)
658 die(_("git %s: failed to read the index"), action_name(opts
));
659 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
660 if (the_index
.cache_changed
) {
661 if (write_index(&the_index
, index_fd
) ||
662 commit_locked_index(&index_lock
))
663 die(_("git %s: failed to refresh the index"), action_name(opts
));
665 rollback_lock_file(&index_lock
);
668 static int format_todo(struct strbuf
*buf
, struct commit_list
*todo_list
,
669 struct replay_opts
*opts
)
671 struct commit_list
*cur
= NULL
;
672 const char *sha1_abbrev
= NULL
;
673 const char *action_str
= opts
->action
== REPLAY_REVERT
? "revert" : "pick";
677 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
678 sha1_abbrev
= find_unique_abbrev(cur
->item
->object
.sha1
, DEFAULT_ABBREV
);
679 subject_len
= find_commit_subject(cur
->item
->buffer
, &subject
);
680 strbuf_addf(buf
, "%s %s %.*s\n", action_str
, sha1_abbrev
,
681 subject_len
, subject
);
686 static struct commit
*parse_insn_line(char *bol
, char *eol
, struct replay_opts
*opts
)
688 unsigned char commit_sha1
[20];
689 enum replay_action action
;
690 char *end_of_object_name
;
691 int saved
, status
, padding
;
693 if (!prefixcmp(bol
, "pick")) {
694 action
= REPLAY_PICK
;
695 bol
+= strlen("pick");
696 } else if (!prefixcmp(bol
, "revert")) {
697 action
= REPLAY_REVERT
;
698 bol
+= strlen("revert");
702 /* Eat up extra spaces/ tabs before object name */
703 padding
= strspn(bol
, " \t");
708 end_of_object_name
= bol
+ strcspn(bol
, " \t\n");
709 saved
= *end_of_object_name
;
710 *end_of_object_name
= '\0';
711 status
= get_sha1(bol
, commit_sha1
);
712 *end_of_object_name
= saved
;
715 * Verify that the action matches up with the one in
716 * opts; we don't support arbitrary instructions
718 if (action
!= opts
->action
) {
719 const char *action_str
;
720 action_str
= action
== REPLAY_REVERT
? "revert" : "cherry-pick";
721 error(_("Cannot %s during a %s"), action_str
, action_name(opts
));
728 return lookup_commit_reference(commit_sha1
);
731 static int parse_insn_buffer(char *buf
, struct commit_list
**todo_list
,
732 struct replay_opts
*opts
)
734 struct commit_list
**next
= todo_list
;
735 struct commit
*commit
;
739 for (i
= 1; *p
; i
++) {
740 char *eol
= strchrnul(p
, '\n');
741 commit
= parse_insn_line(p
, eol
, opts
);
743 return error(_("Could not parse line %d."), i
);
744 next
= commit_list_append(commit
, next
);
745 p
= *eol
? eol
+ 1 : eol
;
748 return error(_("No commits parsed."));
752 static void read_populate_todo(struct commit_list
**todo_list
,
753 struct replay_opts
*opts
)
755 const char *todo_file
= git_path(SEQ_TODO_FILE
);
756 struct strbuf buf
= STRBUF_INIT
;
759 fd
= open(todo_file
, O_RDONLY
);
761 die_errno(_("Could not open %s"), todo_file
);
762 if (strbuf_read(&buf
, fd
, 0) < 0) {
764 strbuf_release(&buf
);
765 die(_("Could not read %s."), todo_file
);
769 res
= parse_insn_buffer(buf
.buf
, todo_list
, opts
);
770 strbuf_release(&buf
);
772 die(_("Unusable instruction sheet: %s"), todo_file
);
775 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
777 struct replay_opts
*opts
= data
;
782 else if (!strcmp(key
, "options.no-commit"))
783 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
784 else if (!strcmp(key
, "options.edit"))
785 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
786 else if (!strcmp(key
, "options.signoff"))
787 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
788 else if (!strcmp(key
, "options.record-origin"))
789 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
790 else if (!strcmp(key
, "options.allow-ff"))
791 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
792 else if (!strcmp(key
, "options.mainline"))
793 opts
->mainline
= git_config_int(key
, value
);
794 else if (!strcmp(key
, "options.strategy"))
795 git_config_string(&opts
->strategy
, key
, value
);
796 else if (!strcmp(key
, "options.strategy-option")) {
797 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
798 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
800 return error(_("Invalid key: %s"), key
);
803 return error(_("Invalid value for %s: %s"), key
, value
);
808 static void read_populate_opts(struct replay_opts
**opts_ptr
)
810 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
812 if (!file_exists(opts_file
))
814 if (git_config_from_file(populate_opts_cb
, opts_file
, *opts_ptr
) < 0)
815 die(_("Malformed options sheet: %s"), opts_file
);
818 static void walk_revs_populate_todo(struct commit_list
**todo_list
,
819 struct replay_opts
*opts
)
821 struct commit
*commit
;
822 struct commit_list
**next
;
827 while ((commit
= get_revision(opts
->revs
)))
828 next
= commit_list_append(commit
, next
);
831 static int create_seq_dir(void)
833 const char *seq_dir
= git_path(SEQ_DIR
);
835 if (file_exists(seq_dir
)) {
836 error(_("a cherry-pick or revert is already in progress"));
837 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
840 else if (mkdir(seq_dir
, 0777) < 0)
841 die_errno(_("Could not create sequencer directory %s"), seq_dir
);
845 static void save_head(const char *head
)
847 const char *head_file
= git_path(SEQ_HEAD_FILE
);
848 static struct lock_file head_lock
;
849 struct strbuf buf
= STRBUF_INIT
;
852 fd
= hold_lock_file_for_update(&head_lock
, head_file
, LOCK_DIE_ON_ERROR
);
853 strbuf_addf(&buf
, "%s\n", head
);
854 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0)
855 die_errno(_("Could not write to %s"), head_file
);
856 if (commit_lock_file(&head_lock
) < 0)
857 die(_("Error wrapping up %s."), head_file
);
860 static int reset_for_rollback(const unsigned char *sha1
)
862 const char *argv
[4]; /* reset --merge <arg> + NULL */
865 argv
[2] = sha1_to_hex(sha1
);
867 return run_command_v_opt(argv
, RUN_GIT_CMD
);
870 static int rollback_single_pick(void)
872 unsigned char head_sha1
[20];
874 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
875 !file_exists(git_path("REVERT_HEAD")))
876 return error(_("no cherry-pick or revert in progress"));
877 if (read_ref_full("HEAD", head_sha1
, 0, NULL
))
878 return error(_("cannot resolve HEAD"));
879 if (is_null_sha1(head_sha1
))
880 return error(_("cannot abort from a branch yet to be born"));
881 return reset_for_rollback(head_sha1
);
884 static int sequencer_rollback(struct replay_opts
*opts
)
886 const char *filename
;
888 unsigned char sha1
[20];
889 struct strbuf buf
= STRBUF_INIT
;
891 filename
= git_path(SEQ_HEAD_FILE
);
892 f
= fopen(filename
, "r");
893 if (!f
&& errno
== ENOENT
) {
895 * There is no multiple-cherry-pick in progress.
896 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
897 * a single-cherry-pick in progress, abort that.
899 return rollback_single_pick();
902 return error(_("cannot open %s: %s"), filename
,
904 if (strbuf_getline(&buf
, f
, '\n')) {
905 error(_("cannot read %s: %s"), filename
, ferror(f
) ?
906 strerror(errno
) : _("unexpected end of file"));
911 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
912 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
916 if (reset_for_rollback(sha1
))
918 remove_sequencer_state();
919 strbuf_release(&buf
);
922 strbuf_release(&buf
);
926 static void save_todo(struct commit_list
*todo_list
, struct replay_opts
*opts
)
928 const char *todo_file
= git_path(SEQ_TODO_FILE
);
929 static struct lock_file todo_lock
;
930 struct strbuf buf
= STRBUF_INIT
;
933 fd
= hold_lock_file_for_update(&todo_lock
, todo_file
, LOCK_DIE_ON_ERROR
);
934 if (format_todo(&buf
, todo_list
, opts
) < 0)
935 die(_("Could not format %s."), todo_file
);
936 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
937 strbuf_release(&buf
);
938 die_errno(_("Could not write to %s"), todo_file
);
940 if (commit_lock_file(&todo_lock
) < 0) {
941 strbuf_release(&buf
);
942 die(_("Error wrapping up %s."), todo_file
);
944 strbuf_release(&buf
);
947 static void save_opts(struct replay_opts
*opts
)
949 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
952 git_config_set_in_file(opts_file
, "options.no-commit", "true");
954 git_config_set_in_file(opts_file
, "options.edit", "true");
956 git_config_set_in_file(opts_file
, "options.signoff", "true");
957 if (opts
->record_origin
)
958 git_config_set_in_file(opts_file
, "options.record-origin", "true");
960 git_config_set_in_file(opts_file
, "options.allow-ff", "true");
961 if (opts
->mainline
) {
962 struct strbuf buf
= STRBUF_INIT
;
963 strbuf_addf(&buf
, "%d", opts
->mainline
);
964 git_config_set_in_file(opts_file
, "options.mainline", buf
.buf
);
965 strbuf_release(&buf
);
968 git_config_set_in_file(opts_file
, "options.strategy", opts
->strategy
);
971 for (i
= 0; i
< opts
->xopts_nr
; i
++)
972 git_config_set_multivar_in_file(opts_file
,
973 "options.strategy-option",
974 opts
->xopts
[i
], "^$", 0);
978 static int pick_commits(struct commit_list
*todo_list
, struct replay_opts
*opts
)
980 struct commit_list
*cur
;
983 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
985 assert(!(opts
->signoff
|| opts
->no_commit
||
986 opts
->record_origin
|| opts
->edit
));
987 read_and_refresh_cache(opts
);
989 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
990 save_todo(cur
, opts
);
991 res
= do_pick_commit(cur
->item
, opts
);
997 * Sequence of picks finished successfully; cleanup by
998 * removing the .git/sequencer directory
1000 remove_sequencer_state();
1004 static int continue_single_pick(void)
1006 const char *argv
[] = { "commit", NULL
};
1008 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
1009 !file_exists(git_path("REVERT_HEAD")))
1010 return error(_("no cherry-pick or revert in progress"));
1011 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1014 static int sequencer_continue(struct replay_opts
*opts
)
1016 struct commit_list
*todo_list
= NULL
;
1018 if (!file_exists(git_path(SEQ_TODO_FILE
)))
1019 return continue_single_pick();
1020 read_populate_opts(&opts
);
1021 read_populate_todo(&todo_list
, opts
);
1023 /* Verify that the conflict has been resolved */
1024 if (file_exists(git_path("CHERRY_PICK_HEAD")) ||
1025 file_exists(git_path("REVERT_HEAD"))) {
1026 int ret
= continue_single_pick();
1030 if (index_differs_from("HEAD", 0))
1031 return error_dirty_index(opts
);
1032 todo_list
= todo_list
->next
;
1033 return pick_commits(todo_list
, opts
);
1036 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1038 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1039 return do_pick_commit(cmit
, opts
);
1042 int sequencer_pick_revisions(struct replay_opts
*opts
)
1044 struct commit_list
*todo_list
= NULL
;
1045 unsigned char sha1
[20];
1047 if (opts
->subcommand
== REPLAY_NONE
)
1050 read_and_refresh_cache(opts
);
1053 * Decide what to do depending on the arguments; a fresh
1054 * cherry-pick should be handled differently from an existing
1055 * one that is being continued
1057 if (opts
->subcommand
== REPLAY_REMOVE_STATE
) {
1058 remove_sequencer_state();
1061 if (opts
->subcommand
== REPLAY_ROLLBACK
)
1062 return sequencer_rollback(opts
);
1063 if (opts
->subcommand
== REPLAY_CONTINUE
)
1064 return sequencer_continue(opts
);
1067 * If we were called as "git cherry-pick <commit>", just
1068 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1069 * REVERT_HEAD, and don't touch the sequencer state.
1070 * This means it is possible to cherry-pick in the middle
1071 * of a cherry-pick sequence.
1073 if (opts
->revs
->cmdline
.nr
== 1 &&
1074 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
1075 opts
->revs
->no_walk
&&
1076 !opts
->revs
->cmdline
.rev
->flags
) {
1077 struct commit
*cmit
;
1078 if (prepare_revision_walk(opts
->revs
))
1079 die(_("revision walk setup failed"));
1080 cmit
= get_revision(opts
->revs
);
1081 if (!cmit
|| get_revision(opts
->revs
))
1082 die("BUG: expected exactly one commit from walk");
1083 return single_pick(cmit
, opts
);
1087 * Start a new cherry-pick/ revert sequence; but
1088 * first, make sure that an existing one isn't in
1092 walk_revs_populate_todo(&todo_list
, opts
);
1093 if (create_seq_dir() < 0)
1095 if (get_sha1("HEAD", sha1
)) {
1096 if (opts
->action
== REPLAY_REVERT
)
1097 return error(_("Can't revert as initial commit"));
1098 return error(_("Can't cherry-pick into empty head"));
1100 save_head(sha1_to_hex(sha1
));
1102 return pick_commits(todo_list
, opts
);
1105 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
1107 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
1108 struct strbuf sob
= STRBUF_INIT
;
1111 strbuf_addstr(&sob
, sign_off_header
);
1112 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
1113 getenv("GIT_COMMITTER_EMAIL")));
1114 strbuf_addch(&sob
, '\n');
1117 * If the whole message buffer is equal to the sob, pretend that we
1118 * found a conforming footer with a matching sob
1120 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
1121 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
1124 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
1127 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0, "\n", 1);
1129 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
1130 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1133 strbuf_release(&sob
);