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 occurred 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
,
275 struct ref_lock
*ref_lock
;
278 if (checkout_fast_forward(from
, to
, 1))
279 exit(1); /* the callee should have complained already */
280 ref_lock
= lock_any_ref_for_update("HEAD", unborn
? null_sha1
: from
, 0);
281 return write_ref_sha1(ref_lock
, to
, "cherry-pick");
284 static int do_recursive_merge(struct commit
*base
, struct commit
*next
,
285 const char *base_label
, const char *next_label
,
286 unsigned char *head
, struct strbuf
*msgbuf
,
287 struct replay_opts
*opts
)
289 struct merge_options o
;
290 struct tree
*result
, *next_tree
, *base_tree
, *head_tree
;
293 static struct lock_file index_lock
;
295 index_fd
= hold_locked_index(&index_lock
, 1);
299 init_merge_options(&o
);
300 o
.ancestor
= base
? base_label
: "(empty tree)";
302 o
.branch2
= next
? next_label
: "(empty tree)";
304 head_tree
= parse_tree_indirect(head
);
305 next_tree
= next
? next
->tree
: empty_tree();
306 base_tree
= base
? base
->tree
: empty_tree();
308 for (xopt
= opts
->xopts
; xopt
!= opts
->xopts
+ opts
->xopts_nr
; xopt
++)
309 parse_merge_opt(&o
, *xopt
);
311 clean
= merge_trees(&o
,
313 next_tree
, base_tree
, &result
);
315 if (active_cache_changed
&&
316 (write_cache(index_fd
, active_cache
, active_nr
) ||
317 commit_locked_index(&index_lock
)))
318 /* TRANSLATORS: %s will be "revert" or "cherry-pick" */
319 die(_("%s: Unable to write new index file"), action_name(opts
));
320 rollback_lock_file(&index_lock
);
323 append_signoff(msgbuf
, 0, 0);
327 strbuf_addstr(msgbuf
, "\nConflicts:\n");
328 for (i
= 0; i
< active_nr
;) {
329 struct cache_entry
*ce
= active_cache
[i
++];
331 strbuf_addch(msgbuf
, '\t');
332 strbuf_addstr(msgbuf
, ce
->name
);
333 strbuf_addch(msgbuf
, '\n');
334 while (i
< active_nr
&& !strcmp(ce
->name
,
335 active_cache
[i
]->name
))
344 static int is_index_unchanged(void)
346 unsigned char head_sha1
[20];
347 struct commit
*head_commit
;
349 if (!resolve_ref_unsafe("HEAD", head_sha1
, 1, NULL
))
350 return error(_("Could not resolve HEAD commit\n"));
352 head_commit
= lookup_commit(head_sha1
);
355 * If head_commit is NULL, check_commit, called from
356 * lookup_commit, would have indicated that head_commit is not
357 * a commit object already. parse_commit() will return failure
358 * without further complaints in such a case. Otherwise, if
359 * the commit is invalid, parse_commit() will complain. So
360 * there is nothing for us to say here. Just return failure.
362 if (parse_commit(head_commit
))
365 if (!active_cache_tree
)
366 active_cache_tree
= cache_tree();
368 if (!cache_tree_fully_valid(active_cache_tree
))
369 if (cache_tree_update(active_cache_tree
, active_cache
,
371 return error(_("Unable to update cache tree\n"));
373 return !hashcmp(active_cache_tree
->sha1
, head_commit
->tree
->object
.sha1
);
377 * If we are cherry-pick, and if the merge did not result in
378 * hand-editing, we will hit this commit and inherit the original
379 * author date and name.
380 * If we are revert, or if our cherry-pick results in a hand merge,
381 * we had better say that the current user is responsible for that.
383 static int run_git_commit(const char *defmsg
, struct replay_opts
*opts
,
386 struct argv_array array
;
389 argv_array_init(&array
);
390 argv_array_push(&array
, "commit");
391 argv_array_push(&array
, "-n");
394 argv_array_push(&array
, "-s");
396 argv_array_push(&array
, "-F");
397 argv_array_push(&array
, defmsg
);
401 argv_array_push(&array
, "--allow-empty");
403 if (opts
->allow_empty_message
)
404 argv_array_push(&array
, "--allow-empty-message");
406 rc
= run_command_v_opt(array
.argv
, RUN_GIT_CMD
);
407 argv_array_clear(&array
);
411 static int is_original_commit_empty(struct commit
*commit
)
413 const unsigned char *ptree_sha1
;
415 if (parse_commit(commit
))
416 return error(_("Could not parse commit %s\n"),
417 sha1_to_hex(commit
->object
.sha1
));
418 if (commit
->parents
) {
419 struct commit
*parent
= commit
->parents
->item
;
420 if (parse_commit(parent
))
421 return error(_("Could not parse parent commit %s\n"),
422 sha1_to_hex(parent
->object
.sha1
));
423 ptree_sha1
= parent
->tree
->object
.sha1
;
425 ptree_sha1
= EMPTY_TREE_SHA1_BIN
; /* commit is root */
428 return !hashcmp(ptree_sha1
, commit
->tree
->object
.sha1
);
432 * Do we run "git commit" with "--allow-empty"?
434 static int allow_empty(struct replay_opts
*opts
, struct commit
*commit
)
436 int index_unchanged
, empty_commit
;
441 * (1) we do not allow empty at all and error out.
443 * (2) we allow ones that were initially empty, but
444 * forbid the ones that become empty;
448 if (!opts
->allow_empty
)
449 return 0; /* let "git commit" barf as necessary */
451 index_unchanged
= is_index_unchanged();
452 if (index_unchanged
< 0)
453 return index_unchanged
;
454 if (!index_unchanged
)
455 return 0; /* we do not have to say --allow-empty */
457 if (opts
->keep_redundant_commits
)
460 empty_commit
= is_original_commit_empty(commit
);
461 if (empty_commit
< 0)
469 static int do_pick_commit(struct commit
*commit
, struct replay_opts
*opts
)
471 unsigned char head
[20];
472 struct commit
*base
, *next
, *parent
;
473 const char *base_label
, *next_label
;
474 struct commit_message msg
= { NULL
, NULL
, NULL
, NULL
, NULL
};
476 struct strbuf msgbuf
= STRBUF_INIT
;
477 int res
, unborn
= 0, allow
;
479 if (opts
->no_commit
) {
481 * We do not intend to commit immediately. We just want to
482 * merge the differences in, so let's compute the tree
483 * that represents the "current" state for merge-recursive
486 if (write_cache_as_tree(head
, 0, NULL
))
487 die (_("Your index file is unmerged."));
489 unborn
= get_sha1("HEAD", head
);
491 hashcpy(head
, EMPTY_TREE_SHA1_BIN
);
492 if (index_differs_from(unborn
? EMPTY_TREE_SHA1_HEX
: "HEAD", 0))
493 return error_dirty_index(opts
);
497 if (!commit
->parents
) {
500 else if (commit
->parents
->next
) {
501 /* Reverting or cherry-picking a merge commit */
503 struct commit_list
*p
;
506 return error(_("Commit %s is a merge but no -m option was given."),
507 sha1_to_hex(commit
->object
.sha1
));
509 for (cnt
= 1, p
= commit
->parents
;
510 cnt
!= opts
->mainline
&& p
;
513 if (cnt
!= opts
->mainline
|| !p
)
514 return error(_("Commit %s does not have parent %d"),
515 sha1_to_hex(commit
->object
.sha1
), opts
->mainline
);
517 } else if (0 < opts
->mainline
)
518 return error(_("Mainline was specified but commit %s is not a merge."),
519 sha1_to_hex(commit
->object
.sha1
));
521 parent
= commit
->parents
->item
;
523 if (opts
->allow_ff
&&
524 ((parent
&& !hashcmp(parent
->object
.sha1
, head
)) ||
525 (!parent
&& unborn
)))
526 return fast_forward_to(commit
->object
.sha1
, head
, unborn
);
528 if (parent
&& parse_commit(parent
) < 0)
529 /* TRANSLATORS: The first %s will be "revert" or
530 "cherry-pick", the second %s a SHA1 */
531 return error(_("%s: cannot parse parent commit %s"),
532 action_name(opts
), sha1_to_hex(parent
->object
.sha1
));
534 if (get_message(commit
, &msg
) != 0)
535 return error(_("Cannot get commit message for %s"),
536 sha1_to_hex(commit
->object
.sha1
));
539 * "commit" is an existing commit. We would want to apply
540 * the difference it introduces since its first parent "prev"
541 * on top of the current HEAD if we are cherry-pick. Or the
542 * reverse of it if we are revert.
545 defmsg
= git_pathdup("MERGE_MSG");
547 if (opts
->action
== REPLAY_REVERT
) {
549 base_label
= msg
.label
;
551 next_label
= msg
.parent_label
;
552 strbuf_addstr(&msgbuf
, "Revert \"");
553 strbuf_addstr(&msgbuf
, msg
.subject
);
554 strbuf_addstr(&msgbuf
, "\"\n\nThis reverts commit ");
555 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
557 if (commit
->parents
&& commit
->parents
->next
) {
558 strbuf_addstr(&msgbuf
, ", reversing\nchanges made to ");
559 strbuf_addstr(&msgbuf
, sha1_to_hex(parent
->object
.sha1
));
561 strbuf_addstr(&msgbuf
, ".\n");
566 base_label
= msg
.parent_label
;
568 next_label
= msg
.label
;
571 * Append the commit log message to msgbuf; it starts
572 * after the tree, parent, author, committer
573 * information followed by "\n\n".
575 p
= strstr(msg
.message
, "\n\n");
578 strbuf_addstr(&msgbuf
, p
);
581 if (opts
->record_origin
) {
582 if (!has_conforming_footer(&msgbuf
, NULL
, 0))
583 strbuf_addch(&msgbuf
, '\n');
584 strbuf_addstr(&msgbuf
, cherry_picked_prefix
);
585 strbuf_addstr(&msgbuf
, sha1_to_hex(commit
->object
.sha1
));
586 strbuf_addstr(&msgbuf
, ")\n");
590 if (!opts
->strategy
|| !strcmp(opts
->strategy
, "recursive") || opts
->action
== REPLAY_REVERT
) {
591 res
= do_recursive_merge(base
, next
, base_label
, next_label
,
592 head
, &msgbuf
, opts
);
593 write_message(&msgbuf
, defmsg
);
595 struct commit_list
*common
= NULL
;
596 struct commit_list
*remotes
= NULL
;
598 write_message(&msgbuf
, defmsg
);
600 commit_list_insert(base
, &common
);
601 commit_list_insert(next
, &remotes
);
602 res
= try_merge_command(opts
->strategy
, opts
->xopts_nr
, opts
->xopts
,
603 common
, sha1_to_hex(head
), remotes
);
604 free_commit_list(common
);
605 free_commit_list(remotes
);
609 * If the merge was clean or if it failed due to conflict, we write
610 * CHERRY_PICK_HEAD for the subsequent invocation of commit to use.
611 * However, if the merge did not even start, then we don't want to
614 if (opts
->action
== REPLAY_PICK
&& !opts
->no_commit
&& (res
== 0 || res
== 1))
615 write_cherry_pick_head(commit
, "CHERRY_PICK_HEAD");
616 if (opts
->action
== REPLAY_REVERT
&& ((opts
->no_commit
&& res
== 0) || res
== 1))
617 write_cherry_pick_head(commit
, "REVERT_HEAD");
620 error(opts
->action
== REPLAY_REVERT
621 ? _("could not revert %s... %s")
622 : _("could not apply %s... %s"),
623 find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
),
625 print_advice(res
== 1, opts
);
626 rerere(opts
->allow_rerere_auto
);
630 allow
= allow_empty(opts
, commit
);
635 if (!opts
->no_commit
)
636 res
= run_git_commit(defmsg
, opts
, allow
);
645 static void prepare_revs(struct replay_opts
*opts
)
648 * picking (but not reverting) ranges (but not individual revisions)
649 * should be done in reverse
651 if (opts
->action
== REPLAY_PICK
&& !opts
->revs
->no_walk
)
652 opts
->revs
->reverse
^= 1;
654 if (prepare_revision_walk(opts
->revs
))
655 die(_("revision walk setup failed"));
657 if (!opts
->revs
->commits
)
658 die(_("empty commit set passed"));
661 static void read_and_refresh_cache(struct replay_opts
*opts
)
663 static struct lock_file index_lock
;
664 int index_fd
= hold_locked_index(&index_lock
, 0);
665 if (read_index_preload(&the_index
, NULL
) < 0)
666 die(_("git %s: failed to read the index"), action_name(opts
));
667 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, NULL
, NULL
, NULL
);
668 if (the_index
.cache_changed
) {
669 if (write_index(&the_index
, index_fd
) ||
670 commit_locked_index(&index_lock
))
671 die(_("git %s: failed to refresh the index"), action_name(opts
));
673 rollback_lock_file(&index_lock
);
676 static int format_todo(struct strbuf
*buf
, struct commit_list
*todo_list
,
677 struct replay_opts
*opts
)
679 struct commit_list
*cur
= NULL
;
680 const char *sha1_abbrev
= NULL
;
681 const char *action_str
= opts
->action
== REPLAY_REVERT
? "revert" : "pick";
685 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
686 sha1_abbrev
= find_unique_abbrev(cur
->item
->object
.sha1
, DEFAULT_ABBREV
);
687 subject_len
= find_commit_subject(cur
->item
->buffer
, &subject
);
688 strbuf_addf(buf
, "%s %s %.*s\n", action_str
, sha1_abbrev
,
689 subject_len
, subject
);
694 static struct commit
*parse_insn_line(char *bol
, char *eol
, struct replay_opts
*opts
)
696 unsigned char commit_sha1
[20];
697 enum replay_action action
;
698 char *end_of_object_name
;
699 int saved
, status
, padding
;
701 if (!prefixcmp(bol
, "pick")) {
702 action
= REPLAY_PICK
;
703 bol
+= strlen("pick");
704 } else if (!prefixcmp(bol
, "revert")) {
705 action
= REPLAY_REVERT
;
706 bol
+= strlen("revert");
710 /* Eat up extra spaces/ tabs before object name */
711 padding
= strspn(bol
, " \t");
716 end_of_object_name
= bol
+ strcspn(bol
, " \t\n");
717 saved
= *end_of_object_name
;
718 *end_of_object_name
= '\0';
719 status
= get_sha1(bol
, commit_sha1
);
720 *end_of_object_name
= saved
;
723 * Verify that the action matches up with the one in
724 * opts; we don't support arbitrary instructions
726 if (action
!= opts
->action
) {
727 const char *action_str
;
728 action_str
= action
== REPLAY_REVERT
? "revert" : "cherry-pick";
729 error(_("Cannot %s during a %s"), action_str
, action_name(opts
));
736 return lookup_commit_reference(commit_sha1
);
739 static int parse_insn_buffer(char *buf
, struct commit_list
**todo_list
,
740 struct replay_opts
*opts
)
742 struct commit_list
**next
= todo_list
;
743 struct commit
*commit
;
747 for (i
= 1; *p
; i
++) {
748 char *eol
= strchrnul(p
, '\n');
749 commit
= parse_insn_line(p
, eol
, opts
);
751 return error(_("Could not parse line %d."), i
);
752 next
= commit_list_append(commit
, next
);
753 p
= *eol
? eol
+ 1 : eol
;
756 return error(_("No commits parsed."));
760 static void read_populate_todo(struct commit_list
**todo_list
,
761 struct replay_opts
*opts
)
763 const char *todo_file
= git_path(SEQ_TODO_FILE
);
764 struct strbuf buf
= STRBUF_INIT
;
767 fd
= open(todo_file
, O_RDONLY
);
769 die_errno(_("Could not open %s"), todo_file
);
770 if (strbuf_read(&buf
, fd
, 0) < 0) {
772 strbuf_release(&buf
);
773 die(_("Could not read %s."), todo_file
);
777 res
= parse_insn_buffer(buf
.buf
, todo_list
, opts
);
778 strbuf_release(&buf
);
780 die(_("Unusable instruction sheet: %s"), todo_file
);
783 static int populate_opts_cb(const char *key
, const char *value
, void *data
)
785 struct replay_opts
*opts
= data
;
790 else if (!strcmp(key
, "options.no-commit"))
791 opts
->no_commit
= git_config_bool_or_int(key
, value
, &error_flag
);
792 else if (!strcmp(key
, "options.edit"))
793 opts
->edit
= git_config_bool_or_int(key
, value
, &error_flag
);
794 else if (!strcmp(key
, "options.signoff"))
795 opts
->signoff
= git_config_bool_or_int(key
, value
, &error_flag
);
796 else if (!strcmp(key
, "options.record-origin"))
797 opts
->record_origin
= git_config_bool_or_int(key
, value
, &error_flag
);
798 else if (!strcmp(key
, "options.allow-ff"))
799 opts
->allow_ff
= git_config_bool_or_int(key
, value
, &error_flag
);
800 else if (!strcmp(key
, "options.mainline"))
801 opts
->mainline
= git_config_int(key
, value
);
802 else if (!strcmp(key
, "options.strategy"))
803 git_config_string(&opts
->strategy
, key
, value
);
804 else if (!strcmp(key
, "options.strategy-option")) {
805 ALLOC_GROW(opts
->xopts
, opts
->xopts_nr
+ 1, opts
->xopts_alloc
);
806 opts
->xopts
[opts
->xopts_nr
++] = xstrdup(value
);
808 return error(_("Invalid key: %s"), key
);
811 return error(_("Invalid value for %s: %s"), key
, value
);
816 static void read_populate_opts(struct replay_opts
**opts_ptr
)
818 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
820 if (!file_exists(opts_file
))
822 if (git_config_from_file(populate_opts_cb
, opts_file
, *opts_ptr
) < 0)
823 die(_("Malformed options sheet: %s"), opts_file
);
826 static void walk_revs_populate_todo(struct commit_list
**todo_list
,
827 struct replay_opts
*opts
)
829 struct commit
*commit
;
830 struct commit_list
**next
;
835 while ((commit
= get_revision(opts
->revs
)))
836 next
= commit_list_append(commit
, next
);
839 static int create_seq_dir(void)
841 const char *seq_dir
= git_path(SEQ_DIR
);
843 if (file_exists(seq_dir
)) {
844 error(_("a cherry-pick or revert is already in progress"));
845 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
848 else if (mkdir(seq_dir
, 0777) < 0)
849 die_errno(_("Could not create sequencer directory %s"), seq_dir
);
853 static void save_head(const char *head
)
855 const char *head_file
= git_path(SEQ_HEAD_FILE
);
856 static struct lock_file head_lock
;
857 struct strbuf buf
= STRBUF_INIT
;
860 fd
= hold_lock_file_for_update(&head_lock
, head_file
, LOCK_DIE_ON_ERROR
);
861 strbuf_addf(&buf
, "%s\n", head
);
862 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0)
863 die_errno(_("Could not write to %s"), head_file
);
864 if (commit_lock_file(&head_lock
) < 0)
865 die(_("Error wrapping up %s."), head_file
);
868 static int reset_for_rollback(const unsigned char *sha1
)
870 const char *argv
[4]; /* reset --merge <arg> + NULL */
873 argv
[2] = sha1_to_hex(sha1
);
875 return run_command_v_opt(argv
, RUN_GIT_CMD
);
878 static int rollback_single_pick(void)
880 unsigned char head_sha1
[20];
882 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
883 !file_exists(git_path("REVERT_HEAD")))
884 return error(_("no cherry-pick or revert in progress"));
885 if (read_ref_full("HEAD", head_sha1
, 0, NULL
))
886 return error(_("cannot resolve HEAD"));
887 if (is_null_sha1(head_sha1
))
888 return error(_("cannot abort from a branch yet to be born"));
889 return reset_for_rollback(head_sha1
);
892 static int sequencer_rollback(struct replay_opts
*opts
)
894 const char *filename
;
896 unsigned char sha1
[20];
897 struct strbuf buf
= STRBUF_INIT
;
899 filename
= git_path(SEQ_HEAD_FILE
);
900 f
= fopen(filename
, "r");
901 if (!f
&& errno
== ENOENT
) {
903 * There is no multiple-cherry-pick in progress.
904 * If CHERRY_PICK_HEAD or REVERT_HEAD indicates
905 * a single-cherry-pick in progress, abort that.
907 return rollback_single_pick();
910 return error(_("cannot open %s: %s"), filename
,
912 if (strbuf_getline(&buf
, f
, '\n')) {
913 error(_("cannot read %s: %s"), filename
, ferror(f
) ?
914 strerror(errno
) : _("unexpected end of file"));
919 if (get_sha1_hex(buf
.buf
, sha1
) || buf
.buf
[40] != '\0') {
920 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
924 if (reset_for_rollback(sha1
))
926 remove_sequencer_state();
927 strbuf_release(&buf
);
930 strbuf_release(&buf
);
934 static void save_todo(struct commit_list
*todo_list
, struct replay_opts
*opts
)
936 const char *todo_file
= git_path(SEQ_TODO_FILE
);
937 static struct lock_file todo_lock
;
938 struct strbuf buf
= STRBUF_INIT
;
941 fd
= hold_lock_file_for_update(&todo_lock
, todo_file
, LOCK_DIE_ON_ERROR
);
942 if (format_todo(&buf
, todo_list
, opts
) < 0)
943 die(_("Could not format %s."), todo_file
);
944 if (write_in_full(fd
, buf
.buf
, buf
.len
) < 0) {
945 strbuf_release(&buf
);
946 die_errno(_("Could not write to %s"), todo_file
);
948 if (commit_lock_file(&todo_lock
) < 0) {
949 strbuf_release(&buf
);
950 die(_("Error wrapping up %s."), todo_file
);
952 strbuf_release(&buf
);
955 static void save_opts(struct replay_opts
*opts
)
957 const char *opts_file
= git_path(SEQ_OPTS_FILE
);
960 git_config_set_in_file(opts_file
, "options.no-commit", "true");
962 git_config_set_in_file(opts_file
, "options.edit", "true");
964 git_config_set_in_file(opts_file
, "options.signoff", "true");
965 if (opts
->record_origin
)
966 git_config_set_in_file(opts_file
, "options.record-origin", "true");
968 git_config_set_in_file(opts_file
, "options.allow-ff", "true");
969 if (opts
->mainline
) {
970 struct strbuf buf
= STRBUF_INIT
;
971 strbuf_addf(&buf
, "%d", opts
->mainline
);
972 git_config_set_in_file(opts_file
, "options.mainline", buf
.buf
);
973 strbuf_release(&buf
);
976 git_config_set_in_file(opts_file
, "options.strategy", opts
->strategy
);
979 for (i
= 0; i
< opts
->xopts_nr
; i
++)
980 git_config_set_multivar_in_file(opts_file
,
981 "options.strategy-option",
982 opts
->xopts
[i
], "^$", 0);
986 static int pick_commits(struct commit_list
*todo_list
, struct replay_opts
*opts
)
988 struct commit_list
*cur
;
991 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
993 assert(!(opts
->signoff
|| opts
->no_commit
||
994 opts
->record_origin
|| opts
->edit
));
995 read_and_refresh_cache(opts
);
997 for (cur
= todo_list
; cur
; cur
= cur
->next
) {
998 save_todo(cur
, opts
);
999 res
= do_pick_commit(cur
->item
, opts
);
1005 * Sequence of picks finished successfully; cleanup by
1006 * removing the .git/sequencer directory
1008 remove_sequencer_state();
1012 static int continue_single_pick(void)
1014 const char *argv
[] = { "commit", NULL
};
1016 if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
1017 !file_exists(git_path("REVERT_HEAD")))
1018 return error(_("no cherry-pick or revert in progress"));
1019 return run_command_v_opt(argv
, RUN_GIT_CMD
);
1022 static int sequencer_continue(struct replay_opts
*opts
)
1024 struct commit_list
*todo_list
= NULL
;
1026 if (!file_exists(git_path(SEQ_TODO_FILE
)))
1027 return continue_single_pick();
1028 read_populate_opts(&opts
);
1029 read_populate_todo(&todo_list
, opts
);
1031 /* Verify that the conflict has been resolved */
1032 if (file_exists(git_path("CHERRY_PICK_HEAD")) ||
1033 file_exists(git_path("REVERT_HEAD"))) {
1034 int ret
= continue_single_pick();
1038 if (index_differs_from("HEAD", 0))
1039 return error_dirty_index(opts
);
1040 todo_list
= todo_list
->next
;
1041 return pick_commits(todo_list
, opts
);
1044 static int single_pick(struct commit
*cmit
, struct replay_opts
*opts
)
1046 setenv(GIT_REFLOG_ACTION
, action_name(opts
), 0);
1047 return do_pick_commit(cmit
, opts
);
1050 int sequencer_pick_revisions(struct replay_opts
*opts
)
1052 struct commit_list
*todo_list
= NULL
;
1053 unsigned char sha1
[20];
1056 if (opts
->subcommand
== REPLAY_NONE
)
1059 read_and_refresh_cache(opts
);
1062 * Decide what to do depending on the arguments; a fresh
1063 * cherry-pick should be handled differently from an existing
1064 * one that is being continued
1066 if (opts
->subcommand
== REPLAY_REMOVE_STATE
) {
1067 remove_sequencer_state();
1070 if (opts
->subcommand
== REPLAY_ROLLBACK
)
1071 return sequencer_rollback(opts
);
1072 if (opts
->subcommand
== REPLAY_CONTINUE
)
1073 return sequencer_continue(opts
);
1075 for (i
= 0; i
< opts
->revs
->pending
.nr
; i
++) {
1076 unsigned char sha1
[20];
1077 const char *name
= opts
->revs
->pending
.objects
[i
].name
;
1079 /* This happens when using --stdin. */
1083 if (!get_sha1(name
, sha1
)) {
1084 if (!lookup_commit_reference_gently(sha1
, 1)) {
1085 enum object_type type
= sha1_object_info(sha1
, NULL
);
1086 die(_("%s: can't cherry-pick a %s"), name
, typename(type
));
1089 die(_("%s: bad revision"), name
);
1093 * If we were called as "git cherry-pick <commit>", just
1094 * cherry-pick/revert it, set CHERRY_PICK_HEAD /
1095 * REVERT_HEAD, and don't touch the sequencer state.
1096 * This means it is possible to cherry-pick in the middle
1097 * of a cherry-pick sequence.
1099 if (opts
->revs
->cmdline
.nr
== 1 &&
1100 opts
->revs
->cmdline
.rev
->whence
== REV_CMD_REV
&&
1101 opts
->revs
->no_walk
&&
1102 !opts
->revs
->cmdline
.rev
->flags
) {
1103 struct commit
*cmit
;
1104 if (prepare_revision_walk(opts
->revs
))
1105 die(_("revision walk setup failed"));
1106 cmit
= get_revision(opts
->revs
);
1107 if (!cmit
|| get_revision(opts
->revs
))
1108 die("BUG: expected exactly one commit from walk");
1109 return single_pick(cmit
, opts
);
1113 * Start a new cherry-pick/ revert sequence; but
1114 * first, make sure that an existing one isn't in
1118 walk_revs_populate_todo(&todo_list
, opts
);
1119 if (create_seq_dir() < 0)
1121 if (get_sha1("HEAD", sha1
)) {
1122 if (opts
->action
== REPLAY_REVERT
)
1123 return error(_("Can't revert as initial commit"));
1124 return error(_("Can't cherry-pick into empty head"));
1126 save_head(sha1_to_hex(sha1
));
1128 return pick_commits(todo_list
, opts
);
1131 void append_signoff(struct strbuf
*msgbuf
, int ignore_footer
, unsigned flag
)
1133 unsigned no_dup_sob
= flag
& APPEND_SIGNOFF_DEDUP
;
1134 struct strbuf sob
= STRBUF_INIT
;
1137 strbuf_addstr(&sob
, sign_off_header
);
1138 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
1139 getenv("GIT_COMMITTER_EMAIL")));
1140 strbuf_addch(&sob
, '\n');
1143 * If the whole message buffer is equal to the sob, pretend that we
1144 * found a conforming footer with a matching sob
1146 if (msgbuf
->len
- ignore_footer
== sob
.len
&&
1147 !strncmp(msgbuf
->buf
, sob
.buf
, sob
.len
))
1150 has_footer
= has_conforming_footer(msgbuf
, &sob
, ignore_footer
);
1153 const char *append_newlines
= NULL
;
1154 size_t len
= msgbuf
->len
- ignore_footer
;
1158 * The buffer is completely empty. Leave foom for
1159 * the title and body to be filled in by the user.
1161 append_newlines
= "\n\n";
1162 } else if (msgbuf
->buf
[len
- 1] != '\n') {
1164 * Incomplete line. Complete the line and add a
1165 * blank one so that there is an empty line between
1166 * the message body and the sob.
1168 append_newlines
= "\n\n";
1169 } else if (len
== 1) {
1171 * Buffer contains a single newline. Add another
1172 * so that we leave room for the title and body.
1174 append_newlines
= "\n";
1175 } else if (msgbuf
->buf
[len
- 2] != '\n') {
1177 * Buffer ends with a single newline. Add another
1178 * so that there is an empty line between the message
1181 append_newlines
= "\n";
1182 } /* else, the buffer already ends with two newlines. */
1184 if (append_newlines
)
1185 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1186 append_newlines
, strlen(append_newlines
));
1189 if (has_footer
!= 3 && (!no_dup_sob
|| has_footer
!= 2))
1190 strbuf_splice(msgbuf
, msgbuf
->len
- ignore_footer
, 0,
1193 strbuf_release(&sob
);