4 * Based on git-am.sh by Junio C Hamano.
6 #define USE_THE_INDEX_VARIABLE
13 #include "environment.h"
17 #include "parse-options.h"
19 #include "run-command.h"
24 #include "cache-tree.h"
29 #include "unpack-trees.h"
31 #include "object-name.h"
32 #include "sequencer.h"
34 #include "merge-recursive.h"
36 #include "notes-utils.h"
41 #include "string-list.h"
44 #include "repository.h"
49 * Returns the length of the first line of msg.
51 static int linelen(const char *msg
)
53 return strchrnul(msg
, '\n') - msg
;
57 * Returns true if `str` consists of only whitespace, false otherwise.
59 static int str_isspace(const char *str
)
69 PATCH_FORMAT_UNKNOWN
= 0,
72 PATCH_FORMAT_STGIT_SERIES
,
79 KEEP_TRUE
, /* pass -k flag to git-mailinfo */
80 KEEP_NON_PATCH
/* pass -b flag to git-mailinfo */
85 SCISSORS_FALSE
= 0, /* pass --no-scissors to git-mailinfo */
86 SCISSORS_TRUE
/* pass --scissors to git-mailinfo */
92 SIGNOFF_EXPLICIT
/* --signoff was set on the command-line */
95 enum show_patch_type
{
101 STOP_ON_EMPTY_COMMIT
= 0, /* output errors and stop in the middle of an am session */
102 DROP_EMPTY_COMMIT
, /* skip with a notice message, unless "--quiet" has been passed */
103 KEEP_EMPTY_COMMIT
, /* keep recording as empty commits */
107 /* state directory path */
110 /* current and last patch numbers, 1-indexed */
114 /* commit metadata and message */
121 /* when --rebasing, records the original commit the patch came from */
122 struct object_id orig_commit
;
124 /* number of digits in patch filename */
127 /* various operating modes and command line options */
132 int signoff
; /* enum signoff_type */
134 int keep
; /* enum keep_type */
136 int scissors
; /* enum scissors_type */
137 int quoted_cr
; /* enum quoted_cr_action */
138 int empty_type
; /* enum empty_action */
139 struct strvec git_apply_opts
;
140 const char *resolvemsg
;
141 int committer_date_is_author_date
;
143 int allow_rerere_autoupdate
;
144 const char *sign_commit
;
149 * Initializes am_state with the default values.
151 static void am_state_init(struct am_state
*state
)
155 memset(state
, 0, sizeof(*state
));
157 state
->dir
= git_pathdup("rebase-apply");
161 git_config_get_bool("am.threeway", &state
->threeway
);
165 git_config_get_bool("am.messageid", &state
->message_id
);
167 state
->scissors
= SCISSORS_UNSET
;
168 state
->quoted_cr
= quoted_cr_unset
;
170 strvec_init(&state
->git_apply_opts
);
172 if (!git_config_get_bool("commit.gpgsign", &gpgsign
))
173 state
->sign_commit
= gpgsign
? "" : NULL
;
177 * Releases memory allocated by an am_state.
179 static void am_state_release(struct am_state
*state
)
182 free(state
->author_name
);
183 free(state
->author_email
);
184 free(state
->author_date
);
186 strvec_clear(&state
->git_apply_opts
);
189 static int am_option_parse_quoted_cr(const struct option
*opt
,
190 const char *arg
, int unset
)
192 BUG_ON_OPT_NEG(unset
);
194 if (mailinfo_parse_quoted_cr_action(arg
, opt
->value
) != 0)
195 return error(_("bad action '%s' for '%s'"), arg
, "--quoted-cr");
199 static int am_option_parse_empty(const struct option
*opt
,
200 const char *arg
, int unset
)
202 int *opt_value
= opt
->value
;
204 BUG_ON_OPT_NEG(unset
);
206 if (!strcmp(arg
, "stop"))
207 *opt_value
= STOP_ON_EMPTY_COMMIT
;
208 else if (!strcmp(arg
, "drop"))
209 *opt_value
= DROP_EMPTY_COMMIT
;
210 else if (!strcmp(arg
, "keep"))
211 *opt_value
= KEEP_EMPTY_COMMIT
;
213 return error(_("invalid value for '%s': '%s'"), "--empty", arg
);
219 * Returns path relative to the am_state directory.
221 static inline const char *am_path(const struct am_state
*state
, const char *path
)
223 return mkpath("%s/%s", state
->dir
, path
);
227 * For convenience to call write_file()
229 static void write_state_text(const struct am_state
*state
,
230 const char *name
, const char *string
)
232 write_file(am_path(state
, name
), "%s", string
);
235 static void write_state_count(const struct am_state
*state
,
236 const char *name
, int value
)
238 write_file(am_path(state
, name
), "%d", value
);
241 static void write_state_bool(const struct am_state
*state
,
242 const char *name
, int value
)
244 write_state_text(state
, name
, value
? "t" : "f");
248 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
251 __attribute__((format (printf
, 3, 4)))
252 static void say(const struct am_state
*state
, FILE *fp
, const char *fmt
, ...)
258 vfprintf(fp
, fmt
, ap
);
265 * Returns 1 if there is an am session in progress, 0 otherwise.
267 static int am_in_progress(const struct am_state
*state
)
271 if (lstat(state
->dir
, &st
) < 0 || !S_ISDIR(st
.st_mode
))
273 if (lstat(am_path(state
, "last"), &st
) || !S_ISREG(st
.st_mode
))
275 if (lstat(am_path(state
, "next"), &st
) || !S_ISREG(st
.st_mode
))
281 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
282 * number of bytes read on success, -1 if the file does not exist. If `trim` is
283 * set, trailing whitespace will be removed.
285 static int read_state_file(struct strbuf
*sb
, const struct am_state
*state
,
286 const char *file
, int trim
)
290 if (strbuf_read_file(sb
, am_path(state
, file
), 0) >= 0) {
300 die_errno(_("could not read '%s'"), am_path(state
, file
));
304 * Reads and parses the state directory's "author-script" file, and sets
305 * state->author_name, state->author_email and state->author_date accordingly.
306 * Returns 0 on success, -1 if the file could not be parsed.
308 * The author script is of the format:
310 * GIT_AUTHOR_NAME='$author_name'
311 * GIT_AUTHOR_EMAIL='$author_email'
312 * GIT_AUTHOR_DATE='$author_date'
314 * where $author_name, $author_email and $author_date are quoted. We are strict
315 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
316 * script, and thus if the file differs from what this function expects, it is
317 * better to bail out than to do something that the user does not expect.
319 static int read_am_author_script(struct am_state
*state
)
321 const char *filename
= am_path(state
, "author-script");
323 assert(!state
->author_name
);
324 assert(!state
->author_email
);
325 assert(!state
->author_date
);
327 return read_author_script(filename
, &state
->author_name
,
328 &state
->author_email
, &state
->author_date
, 1);
332 * Saves state->author_name, state->author_email and state->author_date in the
333 * state directory's "author-script" file.
335 static void write_author_script(const struct am_state
*state
)
337 struct strbuf sb
= STRBUF_INIT
;
339 strbuf_addstr(&sb
, "GIT_AUTHOR_NAME=");
340 sq_quote_buf(&sb
, state
->author_name
);
341 strbuf_addch(&sb
, '\n');
343 strbuf_addstr(&sb
, "GIT_AUTHOR_EMAIL=");
344 sq_quote_buf(&sb
, state
->author_email
);
345 strbuf_addch(&sb
, '\n');
347 strbuf_addstr(&sb
, "GIT_AUTHOR_DATE=");
348 sq_quote_buf(&sb
, state
->author_date
);
349 strbuf_addch(&sb
, '\n');
351 write_state_text(state
, "author-script", sb
.buf
);
357 * Reads the commit message from the state directory's "final-commit" file,
358 * setting state->msg to its contents and state->msg_len to the length of its
361 * Returns 0 on success, -1 if the file does not exist.
363 static int read_commit_msg(struct am_state
*state
)
365 struct strbuf sb
= STRBUF_INIT
;
369 if (read_state_file(&sb
, state
, "final-commit", 0) < 0) {
374 state
->msg
= strbuf_detach(&sb
, &state
->msg_len
);
379 * Saves state->msg in the state directory's "final-commit" file.
381 static void write_commit_msg(const struct am_state
*state
)
383 const char *filename
= am_path(state
, "final-commit");
384 write_file_buf(filename
, state
->msg
, state
->msg_len
);
388 * Loads state from disk.
390 static void am_load(struct am_state
*state
)
392 struct strbuf sb
= STRBUF_INIT
;
394 if (read_state_file(&sb
, state
, "next", 1) < 0)
395 BUG("state file 'next' does not exist");
396 state
->cur
= strtol(sb
.buf
, NULL
, 10);
398 if (read_state_file(&sb
, state
, "last", 1) < 0)
399 BUG("state file 'last' does not exist");
400 state
->last
= strtol(sb
.buf
, NULL
, 10);
402 if (read_am_author_script(state
) < 0)
403 die(_("could not parse author script"));
405 read_commit_msg(state
);
407 if (read_state_file(&sb
, state
, "original-commit", 1) < 0)
408 oidclr(&state
->orig_commit
);
409 else if (get_oid_hex(sb
.buf
, &state
->orig_commit
) < 0)
410 die(_("could not parse %s"), am_path(state
, "original-commit"));
412 read_state_file(&sb
, state
, "threeway", 1);
413 state
->threeway
= !strcmp(sb
.buf
, "t");
415 read_state_file(&sb
, state
, "quiet", 1);
416 state
->quiet
= !strcmp(sb
.buf
, "t");
418 read_state_file(&sb
, state
, "sign", 1);
419 state
->signoff
= !strcmp(sb
.buf
, "t");
421 read_state_file(&sb
, state
, "utf8", 1);
422 state
->utf8
= !strcmp(sb
.buf
, "t");
424 if (file_exists(am_path(state
, "rerere-autoupdate"))) {
425 read_state_file(&sb
, state
, "rerere-autoupdate", 1);
426 state
->allow_rerere_autoupdate
= strcmp(sb
.buf
, "t") ?
427 RERERE_NOAUTOUPDATE
: RERERE_AUTOUPDATE
;
429 state
->allow_rerere_autoupdate
= 0;
432 read_state_file(&sb
, state
, "keep", 1);
433 if (!strcmp(sb
.buf
, "t"))
434 state
->keep
= KEEP_TRUE
;
435 else if (!strcmp(sb
.buf
, "b"))
436 state
->keep
= KEEP_NON_PATCH
;
438 state
->keep
= KEEP_FALSE
;
440 read_state_file(&sb
, state
, "messageid", 1);
441 state
->message_id
= !strcmp(sb
.buf
, "t");
443 read_state_file(&sb
, state
, "scissors", 1);
444 if (!strcmp(sb
.buf
, "t"))
445 state
->scissors
= SCISSORS_TRUE
;
446 else if (!strcmp(sb
.buf
, "f"))
447 state
->scissors
= SCISSORS_FALSE
;
449 state
->scissors
= SCISSORS_UNSET
;
451 read_state_file(&sb
, state
, "quoted-cr", 1);
453 state
->quoted_cr
= quoted_cr_unset
;
454 else if (mailinfo_parse_quoted_cr_action(sb
.buf
, &state
->quoted_cr
) != 0)
455 die(_("could not parse %s"), am_path(state
, "quoted-cr"));
457 read_state_file(&sb
, state
, "apply-opt", 1);
458 strvec_clear(&state
->git_apply_opts
);
459 if (sq_dequote_to_strvec(sb
.buf
, &state
->git_apply_opts
) < 0)
460 die(_("could not parse %s"), am_path(state
, "apply-opt"));
462 state
->rebasing
= !!file_exists(am_path(state
, "rebasing"));
468 * Removes the am_state directory, forcefully terminating the current am
471 static void am_destroy(const struct am_state
*state
)
473 struct strbuf sb
= STRBUF_INIT
;
475 strbuf_addstr(&sb
, state
->dir
);
476 remove_dir_recursively(&sb
, 0);
481 * Runs applypatch-msg hook. Returns its exit code.
483 static int run_applypatch_msg_hook(struct am_state
*state
)
489 if (!state
->no_verify
)
490 ret
= run_hooks_l("applypatch-msg", am_path(state
, "final-commit"), NULL
);
493 FREE_AND_NULL(state
->msg
);
494 if (read_commit_msg(state
) < 0)
495 die(_("'%s' was deleted by the applypatch-msg hook"),
496 am_path(state
, "final-commit"));
503 * Runs post-rewrite hook. Returns it exit code.
505 static int run_post_rewrite_hook(const struct am_state
*state
)
507 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
509 strvec_push(&opt
.args
, "rebase");
510 opt
.path_to_stdin
= am_path(state
, "rewritten");
512 return run_hooks_opt("post-rewrite", &opt
);
516 * Reads the state directory's "rewritten" file, and copies notes from the old
517 * commits listed in the file to their rewritten commits.
519 * Returns 0 on success, -1 on failure.
521 static int copy_notes_for_rebase(const struct am_state
*state
)
523 struct notes_rewrite_cfg
*c
;
524 struct strbuf sb
= STRBUF_INIT
;
525 const char *invalid_line
= _("Malformed input line: '%s'.");
526 const char *msg
= "Notes added by 'git rebase'";
530 assert(state
->rebasing
);
532 c
= init_copy_notes_for_rewrite("rebase");
536 fp
= xfopen(am_path(state
, "rewritten"), "r");
538 while (!strbuf_getline_lf(&sb
, fp
)) {
539 struct object_id from_obj
, to_obj
;
542 if (sb
.len
!= the_hash_algo
->hexsz
* 2 + 1) {
543 ret
= error(invalid_line
, sb
.buf
);
547 if (parse_oid_hex(sb
.buf
, &from_obj
, &p
)) {
548 ret
= error(invalid_line
, sb
.buf
);
553 ret
= error(invalid_line
, sb
.buf
);
557 if (get_oid_hex(p
+ 1, &to_obj
)) {
558 ret
= error(invalid_line
, sb
.buf
);
562 if (copy_note_for_rewrite(c
, &from_obj
, &to_obj
))
563 ret
= error(_("Failed to copy notes from '%s' to '%s'"),
564 oid_to_hex(&from_obj
), oid_to_hex(&to_obj
));
568 finish_copy_notes_for_rewrite(the_repository
, c
, msg
);
575 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
576 * non-indented lines and checking if they look like they begin with valid
577 * header field names.
579 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
581 static int is_mail(FILE *fp
)
583 const char *header_regex
= "^[!-9;-~]+:";
584 struct strbuf sb
= STRBUF_INIT
;
588 if (fseek(fp
, 0L, SEEK_SET
))
589 die_errno(_("fseek failed"));
591 if (regcomp(®ex
, header_regex
, REG_NOSUB
| REG_EXTENDED
))
592 die("invalid pattern: %s", header_regex
);
594 while (!strbuf_getline(&sb
, fp
)) {
596 break; /* End of header */
598 /* Ignore indented folded lines */
599 if (*sb
.buf
== '\t' || *sb
.buf
== ' ')
602 /* It's a header if it matches header_regex */
603 if (regexec(®ex
, sb
.buf
, 0, NULL
, 0)) {
616 * Attempts to detect the patch_format of the patches contained in `paths`,
617 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
620 static int detect_patch_format(const char **paths
)
622 enum patch_format ret
= PATCH_FORMAT_UNKNOWN
;
623 struct strbuf l1
= STRBUF_INIT
;
624 struct strbuf l2
= STRBUF_INIT
;
625 struct strbuf l3
= STRBUF_INIT
;
629 * We default to mbox format if input is from stdin and for directories
631 if (!*paths
|| !strcmp(*paths
, "-") || is_directory(*paths
))
632 return PATCH_FORMAT_MBOX
;
635 * Otherwise, check the first few lines of the first patch, starting
636 * from the first non-blank line, to try to detect its format.
639 fp
= xfopen(*paths
, "r");
641 while (!strbuf_getline(&l1
, fp
)) {
646 if (starts_with(l1
.buf
, "From ") || starts_with(l1
.buf
, "From: ")) {
647 ret
= PATCH_FORMAT_MBOX
;
651 if (starts_with(l1
.buf
, "# This series applies on GIT commit")) {
652 ret
= PATCH_FORMAT_STGIT_SERIES
;
656 if (!strcmp(l1
.buf
, "# HG changeset patch")) {
657 ret
= PATCH_FORMAT_HG
;
661 strbuf_getline(&l2
, fp
);
662 strbuf_getline(&l3
, fp
);
665 * If the second line is empty and the third is a From, Author or Date
666 * entry, this is likely an StGit patch.
668 if (l1
.len
&& !l2
.len
&&
669 (starts_with(l3
.buf
, "From:") ||
670 starts_with(l3
.buf
, "Author:") ||
671 starts_with(l3
.buf
, "Date:"))) {
672 ret
= PATCH_FORMAT_STGIT
;
676 if (l1
.len
&& is_mail(fp
)) {
677 ret
= PATCH_FORMAT_MBOX
;
690 * Splits out individual email patches from `paths`, where each path is either
691 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
693 static int split_mail_mbox(struct am_state
*state
, const char **paths
,
694 int keep_cr
, int mboxrd
)
696 struct child_process cp
= CHILD_PROCESS_INIT
;
697 struct strbuf last
= STRBUF_INIT
;
701 strvec_push(&cp
.args
, "mailsplit");
702 strvec_pushf(&cp
.args
, "-d%d", state
->prec
);
703 strvec_pushf(&cp
.args
, "-o%s", state
->dir
);
704 strvec_push(&cp
.args
, "-b");
706 strvec_push(&cp
.args
, "--keep-cr");
708 strvec_push(&cp
.args
, "--mboxrd");
709 strvec_push(&cp
.args
, "--");
710 strvec_pushv(&cp
.args
, paths
);
712 ret
= capture_command(&cp
, &last
, 8);
717 state
->last
= strtol(last
.buf
, NULL
, 10);
720 strbuf_release(&last
);
725 * Callback signature for split_mail_conv(). The foreign patch should be
726 * read from `in`, and the converted patch (in RFC2822 mail format) should be
727 * written to `out`. Return 0 on success, or -1 on failure.
729 typedef int (*mail_conv_fn
)(FILE *out
, FILE *in
, int keep_cr
);
732 * Calls `fn` for each file in `paths` to convert the foreign patch to the
733 * RFC2822 mail format suitable for parsing with git-mailinfo.
735 * Returns 0 on success, -1 on failure.
737 static int split_mail_conv(mail_conv_fn fn
, struct am_state
*state
,
738 const char **paths
, int keep_cr
)
740 static const char *stdin_only
[] = {"-", NULL
};
746 for (i
= 0; *paths
; paths
++, i
++) {
751 if (!strcmp(*paths
, "-"))
754 in
= fopen(*paths
, "r");
757 return error_errno(_("could not open '%s' for reading"),
760 mail
= mkpath("%s/%0*d", state
->dir
, state
->prec
, i
+ 1);
762 out
= fopen(mail
, "w");
766 return error_errno(_("could not open '%s' for writing"),
770 ret
= fn(out
, in
, keep_cr
);
777 return error(_("could not parse patch '%s'"), *paths
);
786 * A split_mail_conv() callback that converts an StGit patch to an RFC2822
787 * message suitable for parsing with git-mailinfo.
789 static int stgit_patch_to_mail(FILE *out
, FILE *in
, int keep_cr
)
791 struct strbuf sb
= STRBUF_INIT
;
792 int subject_printed
= 0;
794 while (!strbuf_getline_lf(&sb
, in
)) {
797 if (str_isspace(sb
.buf
))
799 else if (skip_prefix(sb
.buf
, "Author:", &str
))
800 fprintf(out
, "From:%s\n", str
);
801 else if (starts_with(sb
.buf
, "From") || starts_with(sb
.buf
, "Date"))
802 fprintf(out
, "%s\n", sb
.buf
);
803 else if (!subject_printed
) {
804 fprintf(out
, "Subject: %s\n", sb
.buf
);
807 fprintf(out
, "\n%s\n", sb
.buf
);
813 while (strbuf_fread(&sb
, 8192, in
) > 0) {
814 fwrite(sb
.buf
, 1, sb
.len
, out
);
823 * This function only supports a single StGit series file in `paths`.
825 * Given an StGit series file, converts the StGit patches in the series into
826 * RFC2822 messages suitable for parsing with git-mailinfo, and queues them in
827 * the state directory.
829 * Returns 0 on success, -1 on failure.
831 static int split_mail_stgit_series(struct am_state
*state
, const char **paths
,
834 const char *series_dir
;
835 char *series_dir_buf
;
837 struct strvec patches
= STRVEC_INIT
;
838 struct strbuf sb
= STRBUF_INIT
;
841 if (!paths
[0] || paths
[1])
842 return error(_("Only one StGIT patch series can be applied at once"));
844 series_dir_buf
= xstrdup(*paths
);
845 series_dir
= dirname(series_dir_buf
);
847 fp
= fopen(*paths
, "r");
849 return error_errno(_("could not open '%s' for reading"), *paths
);
851 while (!strbuf_getline_lf(&sb
, fp
)) {
853 continue; /* skip comment lines */
855 strvec_push(&patches
, mkpath("%s/%s", series_dir
, sb
.buf
));
860 free(series_dir_buf
);
862 ret
= split_mail_conv(stgit_patch_to_mail
, state
, patches
.v
, keep_cr
);
864 strvec_clear(&patches
);
869 * A split_patches_conv() callback that converts a mercurial patch to a RFC2822
870 * message suitable for parsing with git-mailinfo.
872 static int hg_patch_to_mail(FILE *out
, FILE *in
, int keep_cr
)
874 struct strbuf sb
= STRBUF_INIT
;
877 while (!strbuf_getline_lf(&sb
, in
)) {
880 if (skip_prefix(sb
.buf
, "# User ", &str
))
881 fprintf(out
, "From: %s\n", str
);
882 else if (skip_prefix(sb
.buf
, "# Date ", &str
)) {
883 timestamp_t timestamp
;
888 timestamp
= parse_timestamp(str
, &end
, 10);
890 rc
= error(_("invalid timestamp"));
894 if (!skip_prefix(end
, " ", &str
)) {
895 rc
= error(_("invalid Date line"));
900 tz
= strtol(str
, &end
, 10);
902 rc
= error(_("invalid timezone offset"));
907 rc
= error(_("invalid Date line"));
912 * mercurial's timezone is in seconds west of UTC,
913 * however git's timezone is in hours + minutes east of
916 tz2
= labs(tz
) / 3600 * 100 + labs(tz
) % 3600 / 60;
920 fprintf(out
, "Date: %s\n", show_date(timestamp
, tz2
, DATE_MODE(RFC2822
)));
921 } else if (starts_with(sb
.buf
, "# ")) {
924 fprintf(out
, "\n%s\n", sb
.buf
);
930 while (strbuf_fread(&sb
, 8192, in
) > 0) {
931 fwrite(sb
.buf
, 1, sb
.len
, out
);
940 * Splits a list of files/directories into individual email patches. Each path
941 * in `paths` must be a file/directory that is formatted according to
944 * Once split out, the individual email patches will be stored in the state
945 * directory, with each patch's filename being its index, padded to state->prec
948 * state->cur will be set to the index of the first mail, and state->last will
949 * be set to the index of the last mail.
951 * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1
952 * to disable this behavior, -1 to use the default configured setting.
954 * Returns 0 on success, -1 on failure.
956 static int split_mail(struct am_state
*state
, enum patch_format patch_format
,
957 const char **paths
, int keep_cr
)
961 git_config_get_bool("am.keepcr", &keep_cr
);
964 switch (patch_format
) {
965 case PATCH_FORMAT_MBOX
:
966 return split_mail_mbox(state
, paths
, keep_cr
, 0);
967 case PATCH_FORMAT_STGIT
:
968 return split_mail_conv(stgit_patch_to_mail
, state
, paths
, keep_cr
);
969 case PATCH_FORMAT_STGIT_SERIES
:
970 return split_mail_stgit_series(state
, paths
, keep_cr
);
971 case PATCH_FORMAT_HG
:
972 return split_mail_conv(hg_patch_to_mail
, state
, paths
, keep_cr
);
973 case PATCH_FORMAT_MBOXRD
:
974 return split_mail_mbox(state
, paths
, keep_cr
, 1);
976 BUG("invalid patch_format");
982 * Setup a new am session for applying patches
984 static void am_setup(struct am_state
*state
, enum patch_format patch_format
,
985 const char **paths
, int keep_cr
)
987 struct object_id curr_head
;
989 struct strbuf sb
= STRBUF_INIT
;
992 patch_format
= detect_patch_format(paths
);
995 fprintf_ln(stderr
, _("Patch format detection failed."));
999 if (mkdir(state
->dir
, 0777) < 0 && errno
!= EEXIST
)
1000 die_errno(_("failed to create directory '%s'"), state
->dir
);
1001 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
1003 if (split_mail(state
, patch_format
, paths
, keep_cr
) < 0) {
1005 die(_("Failed to split patches."));
1008 if (state
->rebasing
)
1009 state
->threeway
= 1;
1011 write_state_bool(state
, "threeway", state
->threeway
);
1012 write_state_bool(state
, "quiet", state
->quiet
);
1013 write_state_bool(state
, "sign", state
->signoff
);
1014 write_state_bool(state
, "utf8", state
->utf8
);
1016 if (state
->allow_rerere_autoupdate
)
1017 write_state_bool(state
, "rerere-autoupdate",
1018 state
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
);
1020 switch (state
->keep
) {
1027 case KEEP_NON_PATCH
:
1031 BUG("invalid value for state->keep");
1034 write_state_text(state
, "keep", str
);
1035 write_state_bool(state
, "messageid", state
->message_id
);
1037 switch (state
->scissors
) {
1038 case SCISSORS_UNSET
:
1041 case SCISSORS_FALSE
:
1048 BUG("invalid value for state->scissors");
1050 write_state_text(state
, "scissors", str
);
1052 switch (state
->quoted_cr
) {
1053 case quoted_cr_unset
:
1056 case quoted_cr_nowarn
:
1059 case quoted_cr_warn
:
1062 case quoted_cr_strip
:
1066 BUG("invalid value for state->quoted_cr");
1068 write_state_text(state
, "quoted-cr", str
);
1070 sq_quote_argv(&sb
, state
->git_apply_opts
.v
);
1071 write_state_text(state
, "apply-opt", sb
.buf
);
1073 if (state
->rebasing
)
1074 write_state_text(state
, "rebasing", "");
1076 write_state_text(state
, "applying", "");
1078 if (!repo_get_oid(the_repository
, "HEAD", &curr_head
)) {
1079 write_state_text(state
, "abort-safety", oid_to_hex(&curr_head
));
1080 if (!state
->rebasing
)
1081 update_ref("am", "ORIG_HEAD", &curr_head
, NULL
, 0,
1082 UPDATE_REFS_DIE_ON_ERR
);
1084 write_state_text(state
, "abort-safety", "");
1085 if (!state
->rebasing
)
1086 delete_ref(NULL
, "ORIG_HEAD", NULL
, 0);
1090 * NOTE: Since the "next" and "last" files determine if an am_state
1091 * session is in progress, they should be written last.
1094 write_state_count(state
, "next", state
->cur
);
1095 write_state_count(state
, "last", state
->last
);
1097 strbuf_release(&sb
);
1101 * Increments the patch pointer, and cleans am_state for the application of the
1104 static void am_next(struct am_state
*state
)
1106 struct object_id head
;
1108 FREE_AND_NULL(state
->author_name
);
1109 FREE_AND_NULL(state
->author_email
);
1110 FREE_AND_NULL(state
->author_date
);
1111 FREE_AND_NULL(state
->msg
);
1114 unlink(am_path(state
, "author-script"));
1115 unlink(am_path(state
, "final-commit"));
1117 oidclr(&state
->orig_commit
);
1118 unlink(am_path(state
, "original-commit"));
1119 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
1121 if (!repo_get_oid(the_repository
, "HEAD", &head
))
1122 write_state_text(state
, "abort-safety", oid_to_hex(&head
));
1124 write_state_text(state
, "abort-safety", "");
1127 write_state_count(state
, "next", state
->cur
);
1131 * Returns the filename of the current patch email.
1133 static const char *msgnum(const struct am_state
*state
)
1135 static struct strbuf sb
= STRBUF_INIT
;
1138 strbuf_addf(&sb
, "%0*d", state
->prec
, state
->cur
);
1144 * Dies with a user-friendly message on how to proceed after resolving the
1145 * problem. This message can be overridden with state->resolvemsg.
1147 static void NORETURN
die_user_resolve(const struct am_state
*state
)
1149 if (state
->resolvemsg
) {
1150 printf_ln("%s", state
->resolvemsg
);
1152 const char *cmdline
= state
->interactive
? "git am -i" : "git am";
1154 printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline
);
1155 printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline
);
1157 if (advice_enabled(ADVICE_AM_WORK_DIR
) &&
1158 is_empty_or_missing_file(am_path(state
, "patch")) &&
1159 !repo_index_has_changes(the_repository
, NULL
, NULL
))
1160 printf_ln(_("To record the empty patch as an empty commit, run \"%s --allow-empty\"."), cmdline
);
1162 printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline
);
1169 * Appends signoff to the "msg" field of the am_state.
1171 static void am_append_signoff(struct am_state
*state
)
1173 struct strbuf sb
= STRBUF_INIT
;
1175 strbuf_attach(&sb
, state
->msg
, state
->msg_len
, state
->msg_len
);
1176 append_signoff(&sb
, 0, 0);
1177 state
->msg
= strbuf_detach(&sb
, &state
->msg_len
);
1181 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
1182 * state->msg will be set to the patch message. state->author_name,
1183 * state->author_email and state->author_date will be set to the patch author's
1184 * name, email and date respectively. The patch body will be written to the
1185 * state directory's "patch" file.
1187 * Returns 1 if the patch should be skipped, 0 otherwise.
1189 static int parse_mail(struct am_state
*state
, const char *mail
)
1192 struct strbuf sb
= STRBUF_INIT
;
1193 struct strbuf msg
= STRBUF_INIT
;
1194 struct strbuf author_name
= STRBUF_INIT
;
1195 struct strbuf author_date
= STRBUF_INIT
;
1196 struct strbuf author_email
= STRBUF_INIT
;
1200 setup_mailinfo(&mi
);
1203 mi
.metainfo_charset
= get_commit_output_encoding();
1205 mi
.metainfo_charset
= NULL
;
1207 switch (state
->keep
) {
1211 mi
.keep_subject
= 1;
1213 case KEEP_NON_PATCH
:
1214 mi
.keep_non_patch_brackets_in_subject
= 1;
1217 BUG("invalid value for state->keep");
1220 if (state
->message_id
)
1221 mi
.add_message_id
= 1;
1223 switch (state
->scissors
) {
1224 case SCISSORS_UNSET
:
1226 case SCISSORS_FALSE
:
1227 mi
.use_scissors
= 0;
1230 mi
.use_scissors
= 1;
1233 BUG("invalid value for state->scissors");
1236 switch (state
->quoted_cr
) {
1237 case quoted_cr_unset
:
1239 case quoted_cr_nowarn
:
1240 case quoted_cr_warn
:
1241 case quoted_cr_strip
:
1242 mi
.quoted_cr
= state
->quoted_cr
;
1245 BUG("invalid value for state->quoted_cr");
1248 mi
.input
= xfopen(mail
, "r");
1249 mi
.output
= xfopen(am_path(state
, "info"), "w");
1250 if (mailinfo(&mi
, am_path(state
, "msg"), am_path(state
, "patch")))
1251 die("could not parse patch");
1256 if (mi
.format_flowed
)
1257 warning(_("Patch sent with format=flowed; "
1258 "space at the end of lines might be lost."));
1260 /* Extract message and author information */
1261 fp
= xfopen(am_path(state
, "info"), "r");
1262 while (!strbuf_getline_lf(&sb
, fp
)) {
1265 if (skip_prefix(sb
.buf
, "Subject: ", &x
)) {
1267 strbuf_addch(&msg
, '\n');
1268 strbuf_addstr(&msg
, x
);
1269 } else if (skip_prefix(sb
.buf
, "Author: ", &x
))
1270 strbuf_addstr(&author_name
, x
);
1271 else if (skip_prefix(sb
.buf
, "Email: ", &x
))
1272 strbuf_addstr(&author_email
, x
);
1273 else if (skip_prefix(sb
.buf
, "Date: ", &x
))
1274 strbuf_addstr(&author_date
, x
);
1278 /* Skip pine's internal folder data */
1279 if (!strcmp(author_name
.buf
, "Mail System Internal Data")) {
1284 strbuf_addstr(&msg
, "\n\n");
1285 strbuf_addbuf(&msg
, &mi
.log_message
);
1286 strbuf_stripspace(&msg
, 0);
1288 assert(!state
->author_name
);
1289 state
->author_name
= strbuf_detach(&author_name
, NULL
);
1291 assert(!state
->author_email
);
1292 state
->author_email
= strbuf_detach(&author_email
, NULL
);
1294 assert(!state
->author_date
);
1295 state
->author_date
= strbuf_detach(&author_date
, NULL
);
1297 assert(!state
->msg
);
1298 state
->msg
= strbuf_detach(&msg
, &state
->msg_len
);
1301 strbuf_release(&msg
);
1302 strbuf_release(&author_date
);
1303 strbuf_release(&author_email
);
1304 strbuf_release(&author_name
);
1305 strbuf_release(&sb
);
1306 clear_mailinfo(&mi
);
1311 * Sets commit_id to the commit hash where the mail was generated from.
1312 * Returns 0 on success, -1 on failure.
1314 static int get_mail_commit_oid(struct object_id
*commit_id
, const char *mail
)
1316 struct strbuf sb
= STRBUF_INIT
;
1317 FILE *fp
= xfopen(mail
, "r");
1321 if (strbuf_getline_lf(&sb
, fp
) ||
1322 !skip_prefix(sb
.buf
, "From ", &x
) ||
1323 get_oid_hex(x
, commit_id
) < 0)
1326 strbuf_release(&sb
);
1332 * Sets state->msg, state->author_name, state->author_email, state->author_date
1333 * to the commit's respective info.
1335 static void get_commit_info(struct am_state
*state
, struct commit
*commit
)
1337 const char *buffer
, *ident_line
, *msg
;
1339 struct ident_split id
;
1341 buffer
= repo_logmsg_reencode(the_repository
, commit
, NULL
,
1342 get_commit_output_encoding());
1344 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
1346 die(_("missing author line in commit %s"),
1347 oid_to_hex(&commit
->object
.oid
));
1348 if (split_ident_line(&id
, ident_line
, ident_len
) < 0)
1349 die(_("invalid ident line: %.*s"), (int)ident_len
, ident_line
);
1351 assert(!state
->author_name
);
1353 state
->author_name
=
1354 xmemdupz(id
.name_begin
, id
.name_end
- id
.name_begin
);
1356 state
->author_name
= xstrdup("");
1358 assert(!state
->author_email
);
1360 state
->author_email
=
1361 xmemdupz(id
.mail_begin
, id
.mail_end
- id
.mail_begin
);
1363 state
->author_email
= xstrdup("");
1365 assert(!state
->author_date
);
1366 state
->author_date
= xstrdup(show_ident_date(&id
, DATE_MODE(NORMAL
)));
1368 assert(!state
->msg
);
1369 msg
= strstr(buffer
, "\n\n");
1371 die(_("unable to parse commit %s"), oid_to_hex(&commit
->object
.oid
));
1372 state
->msg
= xstrdup(msg
+ 2);
1373 state
->msg_len
= strlen(state
->msg
);
1374 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
1378 * Writes `commit` as a patch to the state directory's "patch" file.
1380 static void write_commit_patch(const struct am_state
*state
, struct commit
*commit
)
1382 struct rev_info rev_info
;
1385 fp
= xfopen(am_path(state
, "patch"), "w");
1386 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1388 rev_info
.abbrev
= 0;
1389 rev_info
.disable_stdin
= 1;
1390 rev_info
.show_root_diff
= 1;
1391 rev_info
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1392 rev_info
.no_commit_id
= 1;
1393 rev_info
.diffopt
.flags
.binary
= 1;
1394 rev_info
.diffopt
.flags
.full_index
= 1;
1395 rev_info
.diffopt
.use_color
= 0;
1396 rev_info
.diffopt
.file
= fp
;
1397 rev_info
.diffopt
.close_file
= 1;
1398 add_pending_object(&rev_info
, &commit
->object
, "");
1399 diff_setup_done(&rev_info
.diffopt
);
1400 log_tree_commit(&rev_info
, commit
);
1401 release_revisions(&rev_info
);
1405 * Writes the diff of the index against HEAD as a patch to the state
1406 * directory's "patch" file.
1408 static void write_index_patch(const struct am_state
*state
)
1411 struct object_id head
;
1412 struct rev_info rev_info
;
1415 if (!repo_get_oid(the_repository
, "HEAD", &head
)) {
1416 struct commit
*commit
= lookup_commit_or_die(&head
, "HEAD");
1417 tree
= repo_get_commit_tree(the_repository
, commit
);
1419 tree
= lookup_tree(the_repository
,
1420 the_repository
->hash_algo
->empty_tree
);
1422 fp
= xfopen(am_path(state
, "patch"), "w");
1423 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1425 rev_info
.disable_stdin
= 1;
1426 rev_info
.no_commit_id
= 1;
1427 rev_info
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1428 rev_info
.diffopt
.use_color
= 0;
1429 rev_info
.diffopt
.file
= fp
;
1430 rev_info
.diffopt
.close_file
= 1;
1431 add_pending_object(&rev_info
, &tree
->object
, "");
1432 diff_setup_done(&rev_info
.diffopt
);
1433 run_diff_index(&rev_info
, 1);
1434 release_revisions(&rev_info
);
1438 * Like parse_mail(), but parses the mail by looking up its commit ID
1439 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
1442 * state->orig_commit will be set to the original commit ID.
1444 * Will always return 0 as the patch should never be skipped.
1446 static int parse_mail_rebase(struct am_state
*state
, const char *mail
)
1448 struct commit
*commit
;
1449 struct object_id commit_oid
;
1451 if (get_mail_commit_oid(&commit_oid
, mail
) < 0)
1452 die(_("could not parse %s"), mail
);
1454 commit
= lookup_commit_or_die(&commit_oid
, mail
);
1456 get_commit_info(state
, commit
);
1458 write_commit_patch(state
, commit
);
1460 oidcpy(&state
->orig_commit
, &commit_oid
);
1461 write_state_text(state
, "original-commit", oid_to_hex(&commit_oid
));
1462 update_ref("am", "REBASE_HEAD", &commit_oid
,
1463 NULL
, REF_NO_DEREF
, UPDATE_REFS_DIE_ON_ERR
);
1469 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
1470 * `index_file` is not NULL, the patch will be applied to that index.
1472 static int run_apply(const struct am_state
*state
, const char *index_file
)
1474 struct strvec apply_paths
= STRVEC_INIT
;
1475 struct strvec apply_opts
= STRVEC_INIT
;
1476 struct apply_state apply_state
;
1478 int force_apply
= 0;
1480 const char **apply_argv
;
1482 if (init_apply_state(&apply_state
, the_repository
, NULL
))
1483 BUG("init_apply_state() failed");
1485 strvec_push(&apply_opts
, "apply");
1486 strvec_pushv(&apply_opts
, state
->git_apply_opts
.v
);
1489 * Build a copy that apply_parse_options() can rearrange.
1490 * apply_opts.v keeps referencing the allocated strings for
1491 * strvec_clear() to release.
1493 DUP_ARRAY(apply_argv
, apply_opts
.v
, apply_opts
.nr
);
1495 opts_left
= apply_parse_options(apply_opts
.nr
, apply_argv
,
1496 &apply_state
, &force_apply
, &options
,
1500 die("unknown option passed through to git apply");
1503 apply_state
.index_file
= index_file
;
1504 apply_state
.cached
= 1;
1506 apply_state
.check_index
= 1;
1509 * If we are allowed to fall back on 3-way merge, don't give false
1510 * errors during the initial attempt.
1512 if (state
->threeway
&& !index_file
)
1513 apply_state
.apply_verbosity
= verbosity_silent
;
1515 if (check_apply_state(&apply_state
, force_apply
))
1516 BUG("check_apply_state() failed");
1518 strvec_push(&apply_paths
, am_path(state
, "patch"));
1520 res
= apply_all_patches(&apply_state
, apply_paths
.nr
, apply_paths
.v
, options
);
1522 strvec_clear(&apply_paths
);
1523 strvec_clear(&apply_opts
);
1524 clear_apply_state(&apply_state
);
1531 /* Reload index as apply_all_patches() will have modified it. */
1532 discard_index(&the_index
);
1533 read_index_from(&the_index
, index_file
, get_git_dir());
1540 * Builds an index that contains just the blobs needed for a 3way merge.
1542 static int build_fake_ancestor(const struct am_state
*state
, const char *index_file
)
1544 struct child_process cp
= CHILD_PROCESS_INIT
;
1547 strvec_push(&cp
.args
, "apply");
1548 strvec_pushv(&cp
.args
, state
->git_apply_opts
.v
);
1549 strvec_pushf(&cp
.args
, "--build-fake-ancestor=%s", index_file
);
1550 strvec_push(&cp
.args
, am_path(state
, "patch"));
1552 if (run_command(&cp
))
1559 * Attempt a threeway merge, using index_path as the temporary index.
1561 static int fall_back_threeway(const struct am_state
*state
, const char *index_path
)
1563 struct object_id orig_tree
, their_tree
, our_tree
;
1564 const struct object_id
*bases
[1] = { &orig_tree
};
1565 struct merge_options o
;
1566 struct commit
*result
;
1567 char *their_tree_name
;
1569 if (repo_get_oid(the_repository
, "HEAD", &our_tree
) < 0)
1570 oidcpy(&our_tree
, the_hash_algo
->empty_tree
);
1572 if (build_fake_ancestor(state
, index_path
))
1573 return error("could not build fake ancestor");
1575 discard_index(&the_index
);
1576 read_index_from(&the_index
, index_path
, get_git_dir());
1578 if (write_index_as_tree(&orig_tree
, &the_index
, index_path
, 0, NULL
))
1579 return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1581 say(state
, stdout
, _("Using index info to reconstruct a base tree..."));
1583 if (!state
->quiet
) {
1585 * List paths that needed 3-way fallback, so that the user can
1586 * review them with extra care to spot mismerges.
1588 struct rev_info rev_info
;
1590 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1591 rev_info
.diffopt
.output_format
= DIFF_FORMAT_NAME_STATUS
;
1592 rev_info
.diffopt
.filter
|= diff_filter_bit('A');
1593 rev_info
.diffopt
.filter
|= diff_filter_bit('M');
1594 add_pending_oid(&rev_info
, "HEAD", &our_tree
, 0);
1595 diff_setup_done(&rev_info
.diffopt
);
1596 run_diff_index(&rev_info
, 1);
1597 release_revisions(&rev_info
);
1600 if (run_apply(state
, index_path
))
1601 return error(_("Did you hand edit your patch?\n"
1602 "It does not apply to blobs recorded in its index."));
1604 if (write_index_as_tree(&their_tree
, &the_index
, index_path
, 0, NULL
))
1605 return error("could not write tree");
1607 say(state
, stdout
, _("Falling back to patching base and 3-way merge..."));
1609 discard_index(&the_index
);
1610 repo_read_index(the_repository
);
1613 * This is not so wrong. Depending on which base we picked, orig_tree
1614 * may be wildly different from ours, but their_tree has the same set of
1615 * wildly different changes in parts the patch did not touch, so
1616 * recursive ends up canceling them, saying that we reverted all those
1620 init_merge_options(&o
, the_repository
);
1623 their_tree_name
= xstrfmt("%.*s", linelen(state
->msg
), state
->msg
);
1624 o
.branch2
= their_tree_name
;
1625 o
.detect_directory_renames
= MERGE_DIRECTORY_RENAMES_NONE
;
1630 if (merge_recursive_generic(&o
, &our_tree
, &their_tree
, 1, bases
, &result
)) {
1631 repo_rerere(the_repository
, state
->allow_rerere_autoupdate
);
1632 free(their_tree_name
);
1633 return error(_("Failed to merge in the changes."));
1636 free(their_tree_name
);
1641 * Commits the current index with state->msg as the commit message and
1642 * state->author_name, state->author_email and state->author_date as the author
1645 static void do_commit(const struct am_state
*state
)
1647 struct object_id tree
, parent
, commit
;
1648 const struct object_id
*old_oid
;
1649 struct commit_list
*parents
= NULL
;
1650 const char *reflog_msg
, *author
, *committer
= NULL
;
1651 struct strbuf sb
= STRBUF_INIT
;
1653 if (!state
->no_verify
&& run_hooks("pre-applypatch"))
1656 if (write_index_as_tree(&tree
, &the_index
, get_index_file(), 0, NULL
))
1657 die(_("git write-tree failed to write a tree"));
1659 if (!repo_get_oid_commit(the_repository
, "HEAD", &parent
)) {
1661 commit_list_insert(lookup_commit(the_repository
, &parent
),
1665 say(state
, stderr
, _("applying to an empty history"));
1668 author
= fmt_ident(state
->author_name
, state
->author_email
,
1670 state
->ignore_date
? NULL
: state
->author_date
,
1673 if (state
->committer_date_is_author_date
)
1674 committer
= fmt_ident(getenv("GIT_COMMITTER_NAME"),
1675 getenv("GIT_COMMITTER_EMAIL"),
1676 WANT_COMMITTER_IDENT
,
1677 state
->ignore_date
? NULL
1678 : state
->author_date
,
1681 if (commit_tree_extended(state
->msg
, state
->msg_len
, &tree
, parents
,
1682 &commit
, author
, committer
, state
->sign_commit
,
1684 die(_("failed to write commit object"));
1686 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1690 strbuf_addf(&sb
, "%s: %.*s", reflog_msg
, linelen(state
->msg
),
1693 update_ref(sb
.buf
, "HEAD", &commit
, old_oid
, 0,
1694 UPDATE_REFS_DIE_ON_ERR
);
1696 if (state
->rebasing
) {
1697 FILE *fp
= xfopen(am_path(state
, "rewritten"), "a");
1699 assert(!is_null_oid(&state
->orig_commit
));
1700 fprintf(fp
, "%s ", oid_to_hex(&state
->orig_commit
));
1701 fprintf(fp
, "%s\n", oid_to_hex(&commit
));
1705 run_hooks("post-applypatch");
1707 strbuf_release(&sb
);
1711 * Validates the am_state for resuming -- the "msg" and authorship fields must
1714 static void validate_resume_state(const struct am_state
*state
)
1717 die(_("cannot resume: %s does not exist."),
1718 am_path(state
, "final-commit"));
1720 if (!state
->author_name
|| !state
->author_email
|| !state
->author_date
)
1721 die(_("cannot resume: %s does not exist."),
1722 am_path(state
, "author-script"));
1726 * Interactively prompt the user on whether the current patch should be
1729 * Returns 0 if the user chooses to apply the patch, 1 if the user chooses to
1732 static int do_interactive(struct am_state
*state
)
1739 puts(_("Commit Body is:"));
1740 puts("--------------------------");
1741 printf("%s", state
->msg
);
1742 puts("--------------------------");
1745 * TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
1746 * in your translation. The program will only accept English
1747 * input at this point.
1749 printf(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "));
1750 if (!fgets(reply
, sizeof(reply
), stdin
))
1751 die("unable to read from stdin; aborting");
1753 if (*reply
== 'y' || *reply
== 'Y') {
1755 } else if (*reply
== 'a' || *reply
== 'A') {
1756 state
->interactive
= 0;
1758 } else if (*reply
== 'n' || *reply
== 'N') {
1760 } else if (*reply
== 'e' || *reply
== 'E') {
1761 struct strbuf msg
= STRBUF_INIT
;
1763 if (!launch_editor(am_path(state
, "final-commit"), &msg
, NULL
)) {
1765 state
->msg
= strbuf_detach(&msg
, &state
->msg_len
);
1767 strbuf_release(&msg
);
1768 } else if (*reply
== 'v' || *reply
== 'V') {
1769 const char *pager
= git_pager(1);
1770 struct child_process cp
= CHILD_PROCESS_INIT
;
1774 prepare_pager_args(&cp
, pager
);
1775 strvec_push(&cp
.args
, am_path(state
, "patch"));
1782 * Applies all queued mail.
1784 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
1785 * well as the state directory's "patch" file is used as-is for applying the
1786 * patch and committing it.
1788 static void am_run(struct am_state
*state
, int resume
)
1790 struct strbuf sb
= STRBUF_INIT
;
1792 unlink(am_path(state
, "dirtyindex"));
1794 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
, 0, 0,
1795 NULL
, NULL
, NULL
) < 0)
1796 die(_("unable to write index file"));
1798 if (repo_index_has_changes(the_repository
, NULL
, &sb
)) {
1799 write_state_bool(state
, "dirtyindex", 1);
1800 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb
.buf
);
1803 strbuf_release(&sb
);
1805 while (state
->cur
<= state
->last
) {
1806 const char *mail
= am_path(state
, msgnum(state
));
1812 if (!file_exists(mail
))
1816 validate_resume_state(state
);
1820 if (state
->rebasing
)
1821 skip
= parse_mail_rebase(state
, mail
);
1823 skip
= parse_mail(state
, mail
);
1826 goto next
; /* mail should be skipped */
1829 am_append_signoff(state
);
1831 write_author_script(state
);
1832 write_commit_msg(state
);
1835 if (state
->interactive
&& do_interactive(state
))
1839 if (is_empty_or_missing_file(am_path(state
, "patch"))) {
1840 switch (state
->empty_type
) {
1841 case DROP_EMPTY_COMMIT
:
1842 say(state
, stdout
, _("Skipping: %.*s"), linelen(state
->msg
), state
->msg
);
1845 case KEEP_EMPTY_COMMIT
:
1847 say(state
, stdout
, _("Creating an empty commit: %.*s"),
1848 linelen(state
->msg
), state
->msg
);
1850 case STOP_ON_EMPTY_COMMIT
:
1851 printf_ln(_("Patch is empty."));
1852 die_user_resolve(state
);
1857 if (run_applypatch_msg_hook(state
))
1862 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1864 apply_status
= run_apply(state
, NULL
);
1866 if (apply_status
&& state
->threeway
) {
1867 struct strbuf sb
= STRBUF_INIT
;
1869 strbuf_addstr(&sb
, am_path(state
, "patch-merge-index"));
1870 apply_status
= fall_back_threeway(state
, sb
.buf
);
1871 strbuf_release(&sb
);
1874 * Applying the patch to an earlier tree and merging
1875 * the result may have produced the same tree as ours.
1877 if (!apply_status
&&
1878 !repo_index_has_changes(the_repository
, NULL
, NULL
)) {
1879 say(state
, stdout
, _("No changes -- Patch already applied."));
1885 printf_ln(_("Patch failed at %s %.*s"), msgnum(state
),
1886 linelen(state
->msg
), state
->msg
);
1888 if (advice_enabled(ADVICE_AM_WORK_DIR
))
1889 advise(_("Use 'git am --show-current-patch=diff' to see the failed patch"));
1891 die_user_resolve(state
);
1905 if (!is_empty_or_missing_file(am_path(state
, "rewritten"))) {
1906 assert(state
->rebasing
);
1907 copy_notes_for_rebase(state
);
1908 run_post_rewrite_hook(state
);
1912 * In rebasing mode, it's up to the caller to take care of
1915 if (!state
->rebasing
) {
1917 run_auto_maintenance(state
->quiet
);
1922 * Resume the current am session after patch application failure. The user did
1923 * all the hard work, and we do not have to do any patch application. Just
1924 * trust and commit what the user has in the index and working tree. If `allow_empty`
1925 * is true, commit as an empty commit when index has not changed and lacking a patch.
1927 static void am_resolve(struct am_state
*state
, int allow_empty
)
1929 validate_resume_state(state
);
1931 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1933 if (!repo_index_has_changes(the_repository
, NULL
, NULL
)) {
1934 if (allow_empty
&& is_empty_or_missing_file(am_path(state
, "patch"))) {
1935 printf_ln(_("No changes - recorded it as an empty commit."));
1937 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1938 "If there is nothing left to stage, chances are that something else\n"
1939 "already introduced the same changes; you might want to skip this patch."));
1940 die_user_resolve(state
);
1944 if (unmerged_index(&the_index
)) {
1945 printf_ln(_("You still have unmerged paths in your index.\n"
1946 "You should 'git add' each file with resolved conflicts to mark them as such.\n"
1947 "You might run `git rm` on a file to accept \"deleted by them\" for it."));
1948 die_user_resolve(state
);
1951 if (state
->interactive
) {
1952 write_index_patch(state
);
1953 if (do_interactive(state
))
1957 repo_rerere(the_repository
, 0);
1968 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1969 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1972 static int fast_forward_to(struct tree
*head
, struct tree
*remote
, int reset
)
1974 struct lock_file lock_file
= LOCK_INIT
;
1975 struct unpack_trees_options opts
;
1976 struct tree_desc t
[2];
1978 if (parse_tree(head
) || parse_tree(remote
))
1981 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
1983 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
1985 memset(&opts
, 0, sizeof(opts
));
1987 opts
.src_index
= &the_index
;
1988 opts
.dst_index
= &the_index
;
1991 opts
.reset
= reset
? UNPACK_RESET_PROTECT_UNTRACKED
: 0;
1992 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
1993 opts
.fn
= twoway_merge
;
1994 init_tree_desc(&t
[0], head
->buffer
, head
->size
);
1995 init_tree_desc(&t
[1], remote
->buffer
, remote
->size
);
1997 if (unpack_trees(2, t
, &opts
)) {
1998 rollback_lock_file(&lock_file
);
2002 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
2003 die(_("unable to write new index file"));
2009 * Merges a tree into the index. The index's stat info will take precedence
2010 * over the merged tree's. Returns 0 on success, -1 on failure.
2012 static int merge_tree(struct tree
*tree
)
2014 struct lock_file lock_file
= LOCK_INIT
;
2015 struct unpack_trees_options opts
;
2016 struct tree_desc t
[1];
2018 if (parse_tree(tree
))
2021 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
2023 memset(&opts
, 0, sizeof(opts
));
2025 opts
.src_index
= &the_index
;
2026 opts
.dst_index
= &the_index
;
2028 opts
.fn
= oneway_merge
;
2029 init_tree_desc(&t
[0], tree
->buffer
, tree
->size
);
2031 if (unpack_trees(1, t
, &opts
)) {
2032 rollback_lock_file(&lock_file
);
2036 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
2037 die(_("unable to write new index file"));
2043 * Clean the index without touching entries that are not modified between
2044 * `head` and `remote`.
2046 static int clean_index(const struct object_id
*head
, const struct object_id
*remote
)
2048 struct tree
*head_tree
, *remote_tree
, *index_tree
;
2049 struct object_id index
;
2051 head_tree
= parse_tree_indirect(head
);
2053 return error(_("Could not parse object '%s'."), oid_to_hex(head
));
2055 remote_tree
= parse_tree_indirect(remote
);
2057 return error(_("Could not parse object '%s'."), oid_to_hex(remote
));
2059 repo_read_index_unmerged(the_repository
);
2061 if (fast_forward_to(head_tree
, head_tree
, 1))
2064 if (write_index_as_tree(&index
, &the_index
, get_index_file(), 0, NULL
))
2067 index_tree
= parse_tree_indirect(&index
);
2069 return error(_("Could not parse object '%s'."), oid_to_hex(&index
));
2071 if (fast_forward_to(index_tree
, remote_tree
, 0))
2074 if (merge_tree(remote_tree
))
2077 remove_branch_state(the_repository
, 0);
2083 * Resets rerere's merge resolution metadata.
2085 static void am_rerere_clear(void)
2087 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
2088 rerere_clear(the_repository
, &merge_rr
);
2089 string_list_clear(&merge_rr
, 1);
2093 * Resume the current am session by skipping the current patch.
2095 static void am_skip(struct am_state
*state
)
2097 struct object_id head
;
2101 if (repo_get_oid(the_repository
, "HEAD", &head
))
2102 oidcpy(&head
, the_hash_algo
->empty_tree
);
2104 if (clean_index(&head
, &head
))
2105 die(_("failed to clean index"));
2107 if (state
->rebasing
) {
2108 FILE *fp
= xfopen(am_path(state
, "rewritten"), "a");
2110 assert(!is_null_oid(&state
->orig_commit
));
2111 fprintf(fp
, "%s ", oid_to_hex(&state
->orig_commit
));
2112 fprintf(fp
, "%s\n", oid_to_hex(&head
));
2122 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
2124 * It is not safe to reset HEAD when:
2125 * 1. git-am previously failed because the index was dirty.
2126 * 2. HEAD has moved since git-am previously failed.
2128 static int safe_to_abort(const struct am_state
*state
)
2130 struct strbuf sb
= STRBUF_INIT
;
2131 struct object_id abort_safety
, head
;
2133 if (file_exists(am_path(state
, "dirtyindex")))
2136 if (read_state_file(&sb
, state
, "abort-safety", 1) > 0) {
2137 if (get_oid_hex(sb
.buf
, &abort_safety
))
2138 die(_("could not parse %s"), am_path(state
, "abort-safety"));
2140 oidclr(&abort_safety
);
2141 strbuf_release(&sb
);
2143 if (repo_get_oid(the_repository
, "HEAD", &head
))
2146 if (oideq(&head
, &abort_safety
))
2149 warning(_("You seem to have moved HEAD since the last 'am' failure.\n"
2150 "Not rewinding to ORIG_HEAD"));
2156 * Aborts the current am session if it is safe to do so.
2158 static void am_abort(struct am_state
*state
)
2160 struct object_id curr_head
, orig_head
;
2161 int has_curr_head
, has_orig_head
;
2164 if (!safe_to_abort(state
)) {
2171 curr_branch
= resolve_refdup("HEAD", 0, &curr_head
, NULL
);
2172 has_curr_head
= curr_branch
&& !is_null_oid(&curr_head
);
2174 oidcpy(&curr_head
, the_hash_algo
->empty_tree
);
2176 has_orig_head
= !repo_get_oid(the_repository
, "ORIG_HEAD", &orig_head
);
2178 oidcpy(&orig_head
, the_hash_algo
->empty_tree
);
2180 if (clean_index(&curr_head
, &orig_head
))
2181 die(_("failed to clean index"));
2184 update_ref("am --abort", "HEAD", &orig_head
,
2185 has_curr_head
? &curr_head
: NULL
, 0,
2186 UPDATE_REFS_DIE_ON_ERR
);
2187 else if (curr_branch
)
2188 delete_ref(NULL
, curr_branch
, NULL
, REF_NO_DEREF
);
2194 static int show_patch(struct am_state
*state
, enum show_patch_type sub_mode
)
2196 struct strbuf sb
= STRBUF_INIT
;
2197 const char *patch_path
;
2200 if (!is_null_oid(&state
->orig_commit
)) {
2201 struct child_process cmd
= CHILD_PROCESS_INIT
;
2203 strvec_pushl(&cmd
.args
, "show", oid_to_hex(&state
->orig_commit
),
2206 return run_command(&cmd
);
2210 case SHOW_PATCH_RAW
:
2211 patch_path
= am_path(state
, msgnum(state
));
2213 case SHOW_PATCH_DIFF
:
2214 patch_path
= am_path(state
, "patch");
2217 BUG("invalid mode for --show-current-patch");
2220 len
= strbuf_read_file(&sb
, patch_path
, 0);
2222 die_errno(_("failed to read '%s'"), patch_path
);
2225 write_in_full(1, sb
.buf
, sb
.len
);
2226 strbuf_release(&sb
);
2231 * parse_options() callback that validates and sets opt->value to the
2232 * PATCH_FORMAT_* enum value corresponding to `arg`.
2234 static int parse_opt_patchformat(const struct option
*opt
, const char *arg
, int unset
)
2236 int *opt_value
= opt
->value
;
2239 *opt_value
= PATCH_FORMAT_UNKNOWN
;
2240 else if (!strcmp(arg
, "mbox"))
2241 *opt_value
= PATCH_FORMAT_MBOX
;
2242 else if (!strcmp(arg
, "stgit"))
2243 *opt_value
= PATCH_FORMAT_STGIT
;
2244 else if (!strcmp(arg
, "stgit-series"))
2245 *opt_value
= PATCH_FORMAT_STGIT_SERIES
;
2246 else if (!strcmp(arg
, "hg"))
2247 *opt_value
= PATCH_FORMAT_HG
;
2248 else if (!strcmp(arg
, "mboxrd"))
2249 *opt_value
= PATCH_FORMAT_MBOXRD
;
2251 * Please update $__git_patchformat in git-completion.bash
2252 * when you add new options
2255 return error(_("invalid value for '%s': '%s'"),
2256 "--patch-format", arg
);
2271 struct resume_mode
{
2272 enum resume_type mode
;
2273 enum show_patch_type sub_mode
;
2276 static int parse_opt_show_current_patch(const struct option
*opt
, const char *arg
, int unset
)
2278 int *opt_value
= opt
->value
;
2279 struct resume_mode
*resume
= container_of(opt_value
, struct resume_mode
, mode
);
2282 * Please update $__git_showcurrentpatch in git-completion.bash
2283 * when you add new options
2285 const char *valid_modes
[] = {
2286 [SHOW_PATCH_DIFF
] = "diff",
2287 [SHOW_PATCH_RAW
] = "raw"
2289 int new_value
= SHOW_PATCH_RAW
;
2291 BUG_ON_OPT_NEG(unset
);
2294 for (new_value
= 0; new_value
< ARRAY_SIZE(valid_modes
); new_value
++) {
2295 if (!strcmp(arg
, valid_modes
[new_value
]))
2298 if (new_value
>= ARRAY_SIZE(valid_modes
))
2299 return error(_("invalid value for '%s': '%s'"),
2300 "--show-current-patch", arg
);
2303 if (resume
->mode
== RESUME_SHOW_PATCH
&& new_value
!= resume
->sub_mode
)
2304 return error(_("options '%s=%s' and '%s=%s' "
2305 "cannot be used together"),
2306 "--show-current-patch", "--show-current-patch", arg
, valid_modes
[resume
->sub_mode
]);
2308 resume
->mode
= RESUME_SHOW_PATCH
;
2309 resume
->sub_mode
= new_value
;
2313 int cmd_am(int argc
, const char **argv
, const char *prefix
)
2315 struct am_state state
;
2318 int patch_format
= PATCH_FORMAT_UNKNOWN
;
2319 struct resume_mode resume
= { .mode
= RESUME_FALSE
};
2323 const char * const usage
[] = {
2324 N_("git am [<options>] [(<mbox> | <Maildir>)...]"),
2325 N_("git am [<options>] (--continue | --skip | --abort)"),
2329 struct option options
[] = {
2330 OPT_BOOL('i', "interactive", &state
.interactive
,
2331 N_("run interactively")),
2332 OPT_BOOL('n', "no-verify", &state
.no_verify
,
2333 N_("bypass pre-applypatch and applypatch-msg hooks")),
2334 OPT_HIDDEN_BOOL('b', "binary", &binary
,
2335 N_("historical option -- no-op")),
2336 OPT_BOOL('3', "3way", &state
.threeway
,
2337 N_("allow fall back on 3way merging if needed")),
2338 OPT__QUIET(&state
.quiet
, N_("be quiet")),
2339 OPT_SET_INT('s', "signoff", &state
.signoff
,
2340 N_("add a Signed-off-by trailer to the commit message"),
2342 OPT_BOOL('u', "utf8", &state
.utf8
,
2343 N_("recode into utf8 (default)")),
2344 OPT_SET_INT('k', "keep", &state
.keep
,
2345 N_("pass -k flag to git-mailinfo"), KEEP_TRUE
),
2346 OPT_SET_INT(0, "keep-non-patch", &state
.keep
,
2347 N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH
),
2348 OPT_BOOL('m', "message-id", &state
.message_id
,
2349 N_("pass -m flag to git-mailinfo")),
2350 OPT_SET_INT_F(0, "keep-cr", &keep_cr
,
2351 N_("pass --keep-cr flag to git-mailsplit for mbox format"),
2352 1, PARSE_OPT_NONEG
),
2353 OPT_SET_INT_F(0, "no-keep-cr", &keep_cr
,
2354 N_("do not pass --keep-cr flag to git-mailsplit independent of am.keepcr"),
2355 0, PARSE_OPT_NONEG
),
2356 OPT_BOOL('c', "scissors", &state
.scissors
,
2357 N_("strip everything before a scissors line")),
2358 OPT_CALLBACK_F(0, "quoted-cr", &state
.quoted_cr
, N_("action"),
2359 N_("pass it through git-mailinfo"),
2360 PARSE_OPT_NONEG
, am_option_parse_quoted_cr
),
2361 OPT_PASSTHRU_ARGV(0, "whitespace", &state
.git_apply_opts
, N_("action"),
2362 N_("pass it through git-apply"),
2364 OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state
.git_apply_opts
, NULL
,
2365 N_("pass it through git-apply"),
2367 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state
.git_apply_opts
, NULL
,
2368 N_("pass it through git-apply"),
2370 OPT_PASSTHRU_ARGV(0, "directory", &state
.git_apply_opts
, N_("root"),
2371 N_("pass it through git-apply"),
2373 OPT_PASSTHRU_ARGV(0, "exclude", &state
.git_apply_opts
, N_("path"),
2374 N_("pass it through git-apply"),
2376 OPT_PASSTHRU_ARGV(0, "include", &state
.git_apply_opts
, N_("path"),
2377 N_("pass it through git-apply"),
2379 OPT_PASSTHRU_ARGV('C', NULL
, &state
.git_apply_opts
, N_("n"),
2380 N_("pass it through git-apply"),
2382 OPT_PASSTHRU_ARGV('p', NULL
, &state
.git_apply_opts
, N_("num"),
2383 N_("pass it through git-apply"),
2385 OPT_CALLBACK(0, "patch-format", &patch_format
, N_("format"),
2386 N_("format the patch(es) are in"),
2387 parse_opt_patchformat
),
2388 OPT_PASSTHRU_ARGV(0, "reject", &state
.git_apply_opts
, NULL
,
2389 N_("pass it through git-apply"),
2391 OPT_STRING(0, "resolvemsg", &state
.resolvemsg
, NULL
,
2392 N_("override error message when patch failure occurs")),
2393 OPT_CMDMODE(0, "continue", &resume
.mode
,
2394 N_("continue applying patches after resolving a conflict"),
2396 OPT_CMDMODE('r', "resolved", &resume
.mode
,
2397 N_("synonyms for --continue"),
2399 OPT_CMDMODE(0, "skip", &resume
.mode
,
2400 N_("skip the current patch"),
2402 OPT_CMDMODE(0, "abort", &resume
.mode
,
2403 N_("restore the original branch and abort the patching operation"),
2405 OPT_CMDMODE(0, "quit", &resume
.mode
,
2406 N_("abort the patching operation but keep HEAD where it is"),
2408 { OPTION_CALLBACK
, 0, "show-current-patch", &resume
.mode
,
2410 N_("show the patch being applied"),
2411 PARSE_OPT_CMDMODE
| PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
| PARSE_OPT_LITERAL_ARGHELP
,
2412 parse_opt_show_current_patch
, RESUME_SHOW_PATCH
},
2413 OPT_CMDMODE(0, "allow-empty", &resume
.mode
,
2414 N_("record the empty patch as an empty commit"),
2415 RESUME_ALLOW_EMPTY
),
2416 OPT_BOOL(0, "committer-date-is-author-date",
2417 &state
.committer_date_is_author_date
,
2418 N_("lie about committer date")),
2419 OPT_BOOL(0, "ignore-date", &state
.ignore_date
,
2420 N_("use current timestamp for author date")),
2421 OPT_RERERE_AUTOUPDATE(&state
.allow_rerere_autoupdate
),
2422 { OPTION_STRING
, 'S', "gpg-sign", &state
.sign_commit
, N_("key-id"),
2423 N_("GPG-sign commits"),
2424 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
2425 OPT_CALLBACK_F(STOP_ON_EMPTY_COMMIT
, "empty", &state
.empty_type
, "{stop,drop,keep}",
2426 N_("how to handle empty patches"),
2427 PARSE_OPT_NONEG
, am_option_parse_empty
),
2428 OPT_HIDDEN_BOOL(0, "rebasing", &state
.rebasing
,
2429 N_("(internal use for git-rebase)")),
2433 if (argc
== 2 && !strcmp(argv
[1], "-h"))
2434 usage_with_options(usage
, options
);
2436 git_config(git_default_config
, NULL
);
2438 am_state_init(&state
);
2440 in_progress
= am_in_progress(&state
);
2444 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2447 fprintf_ln(stderr
, _("The -b/--binary option has been a no-op for long time, and\n"
2448 "it will be removed. Please do not use it anymore."));
2450 /* Ensure a valid committer ident can be constructed */
2451 git_committer_info(IDENT_STRICT
);
2453 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
2454 die(_("failed to read the index"));
2458 * Catch user error to feed us patches when there is a session
2461 * 1. mbox path(s) are provided on the command-line.
2462 * 2. stdin is not a tty: the user is trying to feed us a patch
2463 * from standard input. This is somewhat unreliable -- stdin
2464 * could be /dev/null for example and the caller did not
2465 * intend to feed us a patch but wanted to continue
2468 if (argc
|| (resume
.mode
== RESUME_FALSE
&& !isatty(0)))
2469 die(_("previous rebase directory %s still exists but mbox given."),
2472 if (resume
.mode
== RESUME_FALSE
)
2473 resume
.mode
= RESUME_APPLY
;
2475 if (state
.signoff
== SIGNOFF_EXPLICIT
)
2476 am_append_signoff(&state
);
2478 struct strvec paths
= STRVEC_INIT
;
2482 * Handle stray state directory in the independent-run case. In
2483 * the --rebasing case, it is up to the caller to take care of
2484 * stray directories.
2486 if (file_exists(state
.dir
) && !state
.rebasing
) {
2487 if (resume
.mode
== RESUME_ABORT
|| resume
.mode
== RESUME_QUIT
) {
2489 am_state_release(&state
);
2493 die(_("Stray %s directory found.\n"
2494 "Use \"git am --abort\" to remove it."),
2499 die(_("Resolve operation not in progress, we are not resuming."));
2501 for (i
= 0; i
< argc
; i
++) {
2502 if (is_absolute_path(argv
[i
]) || !prefix
)
2503 strvec_push(&paths
, argv
[i
]);
2505 strvec_push(&paths
, mkpath("%s/%s", prefix
, argv
[i
]));
2508 if (state
.interactive
&& !paths
.nr
)
2509 die(_("interactive mode requires patches on the command line"));
2511 am_setup(&state
, patch_format
, paths
.v
, keep_cr
);
2513 strvec_clear(&paths
);
2516 switch (resume
.mode
) {
2523 case RESUME_RESOLVED
:
2524 case RESUME_ALLOW_EMPTY
:
2525 am_resolve(&state
, resume
.mode
== RESUME_ALLOW_EMPTY
? 1 : 0);
2537 case RESUME_SHOW_PATCH
:
2538 ret
= show_patch(&state
, resume
.sub_mode
);
2541 BUG("invalid resume value");
2544 am_state_release(&state
);