4 * Based on git-am.sh by Junio C Hamano.
6 #define USE_THE_INDEX_VARIABLE
12 #include "environment.h"
16 #include "parse-options.h"
18 #include "run-command.h"
23 #include "cache-tree.h"
28 #include "unpack-trees.h"
30 #include "object-name.h"
31 #include "sequencer.h"
33 #include "merge-recursive.h"
35 #include "notes-utils.h"
40 #include "string-list.h"
42 #include "repository.h"
47 * Returns the length of the first line of msg.
49 static int linelen(const char *msg
)
51 return strchrnul(msg
, '\n') - msg
;
55 * Returns true if `str` consists of only whitespace, false otherwise.
57 static int str_isspace(const char *str
)
67 PATCH_FORMAT_UNKNOWN
= 0,
70 PATCH_FORMAT_STGIT_SERIES
,
77 KEEP_TRUE
, /* pass -k flag to git-mailinfo */
78 KEEP_NON_PATCH
/* pass -b flag to git-mailinfo */
83 SCISSORS_FALSE
= 0, /* pass --no-scissors to git-mailinfo */
84 SCISSORS_TRUE
/* pass --scissors to git-mailinfo */
90 SIGNOFF_EXPLICIT
/* --signoff was set on the command-line */
93 enum show_patch_type
{
99 STOP_ON_EMPTY_COMMIT
= 0, /* output errors and stop in the middle of an am session */
100 DROP_EMPTY_COMMIT
, /* skip with a notice message, unless "--quiet" has been passed */
101 KEEP_EMPTY_COMMIT
, /* keep recording as empty commits */
105 /* state directory path */
108 /* current and last patch numbers, 1-indexed */
112 /* commit metadata and message */
119 /* when --rebasing, records the original commit the patch came from */
120 struct object_id orig_commit
;
122 /* number of digits in patch filename */
125 /* various operating modes and command line options */
130 int signoff
; /* enum signoff_type */
132 int keep
; /* enum keep_type */
134 int scissors
; /* enum scissors_type */
135 int quoted_cr
; /* enum quoted_cr_action */
136 int empty_type
; /* enum empty_action */
137 struct strvec git_apply_opts
;
138 const char *resolvemsg
;
139 int committer_date_is_author_date
;
141 int allow_rerere_autoupdate
;
142 const char *sign_commit
;
147 * Initializes am_state with the default values.
149 static void am_state_init(struct am_state
*state
)
153 memset(state
, 0, sizeof(*state
));
155 state
->dir
= git_pathdup("rebase-apply");
159 git_config_get_bool("am.threeway", &state
->threeway
);
163 git_config_get_bool("am.messageid", &state
->message_id
);
165 state
->scissors
= SCISSORS_UNSET
;
166 state
->quoted_cr
= quoted_cr_unset
;
168 strvec_init(&state
->git_apply_opts
);
170 if (!git_config_get_bool("commit.gpgsign", &gpgsign
))
171 state
->sign_commit
= gpgsign
? "" : NULL
;
175 * Releases memory allocated by an am_state.
177 static void am_state_release(struct am_state
*state
)
180 free(state
->author_name
);
181 free(state
->author_email
);
182 free(state
->author_date
);
184 strvec_clear(&state
->git_apply_opts
);
187 static int am_option_parse_quoted_cr(const struct option
*opt
,
188 const char *arg
, int unset
)
190 BUG_ON_OPT_NEG(unset
);
192 if (mailinfo_parse_quoted_cr_action(arg
, opt
->value
) != 0)
193 return error(_("bad action '%s' for '%s'"), arg
, "--quoted-cr");
197 static int am_option_parse_empty(const struct option
*opt
,
198 const char *arg
, int unset
)
200 int *opt_value
= opt
->value
;
202 BUG_ON_OPT_NEG(unset
);
204 if (!strcmp(arg
, "stop"))
205 *opt_value
= STOP_ON_EMPTY_COMMIT
;
206 else if (!strcmp(arg
, "drop"))
207 *opt_value
= DROP_EMPTY_COMMIT
;
208 else if (!strcmp(arg
, "keep"))
209 *opt_value
= KEEP_EMPTY_COMMIT
;
211 return error(_("invalid value for '%s': '%s'"), "--empty", arg
);
217 * Returns path relative to the am_state directory.
219 static inline const char *am_path(const struct am_state
*state
, const char *path
)
221 return mkpath("%s/%s", state
->dir
, path
);
225 * For convenience to call write_file()
227 static void write_state_text(const struct am_state
*state
,
228 const char *name
, const char *string
)
230 write_file(am_path(state
, name
), "%s", string
);
233 static void write_state_count(const struct am_state
*state
,
234 const char *name
, int value
)
236 write_file(am_path(state
, name
), "%d", value
);
239 static void write_state_bool(const struct am_state
*state
,
240 const char *name
, int value
)
242 write_state_text(state
, name
, value
? "t" : "f");
246 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
249 __attribute__((format (printf
, 3, 4)))
250 static void say(const struct am_state
*state
, FILE *fp
, const char *fmt
, ...)
256 vfprintf(fp
, fmt
, ap
);
263 * Returns 1 if there is an am session in progress, 0 otherwise.
265 static int am_in_progress(const struct am_state
*state
)
269 if (lstat(state
->dir
, &st
) < 0 || !S_ISDIR(st
.st_mode
))
271 if (lstat(am_path(state
, "last"), &st
) || !S_ISREG(st
.st_mode
))
273 if (lstat(am_path(state
, "next"), &st
) || !S_ISREG(st
.st_mode
))
279 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
280 * number of bytes read on success, -1 if the file does not exist. If `trim` is
281 * set, trailing whitespace will be removed.
283 static int read_state_file(struct strbuf
*sb
, const struct am_state
*state
,
284 const char *file
, int trim
)
288 if (strbuf_read_file(sb
, am_path(state
, file
), 0) >= 0) {
298 die_errno(_("could not read '%s'"), am_path(state
, file
));
302 * Reads and parses the state directory's "author-script" file, and sets
303 * state->author_name, state->author_email and state->author_date accordingly.
304 * Returns 0 on success, -1 if the file could not be parsed.
306 * The author script is of the format:
308 * GIT_AUTHOR_NAME='$author_name'
309 * GIT_AUTHOR_EMAIL='$author_email'
310 * GIT_AUTHOR_DATE='$author_date'
312 * where $author_name, $author_email and $author_date are quoted. We are strict
313 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
314 * script, and thus if the file differs from what this function expects, it is
315 * better to bail out than to do something that the user does not expect.
317 static int read_am_author_script(struct am_state
*state
)
319 const char *filename
= am_path(state
, "author-script");
321 assert(!state
->author_name
);
322 assert(!state
->author_email
);
323 assert(!state
->author_date
);
325 return read_author_script(filename
, &state
->author_name
,
326 &state
->author_email
, &state
->author_date
, 1);
330 * Saves state->author_name, state->author_email and state->author_date in the
331 * state directory's "author-script" file.
333 static void write_author_script(const struct am_state
*state
)
335 struct strbuf sb
= STRBUF_INIT
;
337 strbuf_addstr(&sb
, "GIT_AUTHOR_NAME=");
338 sq_quote_buf(&sb
, state
->author_name
);
339 strbuf_addch(&sb
, '\n');
341 strbuf_addstr(&sb
, "GIT_AUTHOR_EMAIL=");
342 sq_quote_buf(&sb
, state
->author_email
);
343 strbuf_addch(&sb
, '\n');
345 strbuf_addstr(&sb
, "GIT_AUTHOR_DATE=");
346 sq_quote_buf(&sb
, state
->author_date
);
347 strbuf_addch(&sb
, '\n');
349 write_state_text(state
, "author-script", sb
.buf
);
355 * Reads the commit message from the state directory's "final-commit" file,
356 * setting state->msg to its contents and state->msg_len to the length of its
359 * Returns 0 on success, -1 if the file does not exist.
361 static int read_commit_msg(struct am_state
*state
)
363 struct strbuf sb
= STRBUF_INIT
;
367 if (read_state_file(&sb
, state
, "final-commit", 0) < 0) {
372 state
->msg
= strbuf_detach(&sb
, &state
->msg_len
);
377 * Saves state->msg in the state directory's "final-commit" file.
379 static void write_commit_msg(const struct am_state
*state
)
381 const char *filename
= am_path(state
, "final-commit");
382 write_file_buf(filename
, state
->msg
, state
->msg_len
);
386 * Loads state from disk.
388 static void am_load(struct am_state
*state
)
390 struct strbuf sb
= STRBUF_INIT
;
392 if (read_state_file(&sb
, state
, "next", 1) < 0)
393 BUG("state file 'next' does not exist");
394 state
->cur
= strtol(sb
.buf
, NULL
, 10);
396 if (read_state_file(&sb
, state
, "last", 1) < 0)
397 BUG("state file 'last' does not exist");
398 state
->last
= strtol(sb
.buf
, NULL
, 10);
400 if (read_am_author_script(state
) < 0)
401 die(_("could not parse author script"));
403 read_commit_msg(state
);
405 if (read_state_file(&sb
, state
, "original-commit", 1) < 0)
406 oidclr(&state
->orig_commit
);
407 else if (get_oid_hex(sb
.buf
, &state
->orig_commit
) < 0)
408 die(_("could not parse %s"), am_path(state
, "original-commit"));
410 read_state_file(&sb
, state
, "threeway", 1);
411 state
->threeway
= !strcmp(sb
.buf
, "t");
413 read_state_file(&sb
, state
, "quiet", 1);
414 state
->quiet
= !strcmp(sb
.buf
, "t");
416 read_state_file(&sb
, state
, "sign", 1);
417 state
->signoff
= !strcmp(sb
.buf
, "t");
419 read_state_file(&sb
, state
, "utf8", 1);
420 state
->utf8
= !strcmp(sb
.buf
, "t");
422 if (file_exists(am_path(state
, "rerere-autoupdate"))) {
423 read_state_file(&sb
, state
, "rerere-autoupdate", 1);
424 state
->allow_rerere_autoupdate
= strcmp(sb
.buf
, "t") ?
425 RERERE_NOAUTOUPDATE
: RERERE_AUTOUPDATE
;
427 state
->allow_rerere_autoupdate
= 0;
430 read_state_file(&sb
, state
, "keep", 1);
431 if (!strcmp(sb
.buf
, "t"))
432 state
->keep
= KEEP_TRUE
;
433 else if (!strcmp(sb
.buf
, "b"))
434 state
->keep
= KEEP_NON_PATCH
;
436 state
->keep
= KEEP_FALSE
;
438 read_state_file(&sb
, state
, "messageid", 1);
439 state
->message_id
= !strcmp(sb
.buf
, "t");
441 read_state_file(&sb
, state
, "scissors", 1);
442 if (!strcmp(sb
.buf
, "t"))
443 state
->scissors
= SCISSORS_TRUE
;
444 else if (!strcmp(sb
.buf
, "f"))
445 state
->scissors
= SCISSORS_FALSE
;
447 state
->scissors
= SCISSORS_UNSET
;
449 read_state_file(&sb
, state
, "quoted-cr", 1);
451 state
->quoted_cr
= quoted_cr_unset
;
452 else if (mailinfo_parse_quoted_cr_action(sb
.buf
, &state
->quoted_cr
) != 0)
453 die(_("could not parse %s"), am_path(state
, "quoted-cr"));
455 read_state_file(&sb
, state
, "apply-opt", 1);
456 strvec_clear(&state
->git_apply_opts
);
457 if (sq_dequote_to_strvec(sb
.buf
, &state
->git_apply_opts
) < 0)
458 die(_("could not parse %s"), am_path(state
, "apply-opt"));
460 state
->rebasing
= !!file_exists(am_path(state
, "rebasing"));
466 * Removes the am_state directory, forcefully terminating the current am
469 static void am_destroy(const struct am_state
*state
)
471 struct strbuf sb
= STRBUF_INIT
;
473 strbuf_addstr(&sb
, state
->dir
);
474 remove_dir_recursively(&sb
, 0);
479 * Runs applypatch-msg hook. Returns its exit code.
481 static int run_applypatch_msg_hook(struct am_state
*state
)
487 if (!state
->no_verify
)
488 ret
= run_hooks_l("applypatch-msg", am_path(state
, "final-commit"), NULL
);
491 FREE_AND_NULL(state
->msg
);
492 if (read_commit_msg(state
) < 0)
493 die(_("'%s' was deleted by the applypatch-msg hook"),
494 am_path(state
, "final-commit"));
501 * Runs post-rewrite hook. Returns it exit code.
503 static int run_post_rewrite_hook(const struct am_state
*state
)
505 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
507 strvec_push(&opt
.args
, "rebase");
508 opt
.path_to_stdin
= am_path(state
, "rewritten");
510 return run_hooks_opt("post-rewrite", &opt
);
514 * Reads the state directory's "rewritten" file, and copies notes from the old
515 * commits listed in the file to their rewritten commits.
517 * Returns 0 on success, -1 on failure.
519 static int copy_notes_for_rebase(const struct am_state
*state
)
521 struct notes_rewrite_cfg
*c
;
522 struct strbuf sb
= STRBUF_INIT
;
523 const char *invalid_line
= _("Malformed input line: '%s'.");
524 const char *msg
= "Notes added by 'git rebase'";
528 assert(state
->rebasing
);
530 c
= init_copy_notes_for_rewrite("rebase");
534 fp
= xfopen(am_path(state
, "rewritten"), "r");
536 while (!strbuf_getline_lf(&sb
, fp
)) {
537 struct object_id from_obj
, to_obj
;
540 if (sb
.len
!= the_hash_algo
->hexsz
* 2 + 1) {
541 ret
= error(invalid_line
, sb
.buf
);
545 if (parse_oid_hex(sb
.buf
, &from_obj
, &p
)) {
546 ret
= error(invalid_line
, sb
.buf
);
551 ret
= error(invalid_line
, sb
.buf
);
555 if (get_oid_hex(p
+ 1, &to_obj
)) {
556 ret
= error(invalid_line
, sb
.buf
);
560 if (copy_note_for_rewrite(c
, &from_obj
, &to_obj
))
561 ret
= error(_("Failed to copy notes from '%s' to '%s'"),
562 oid_to_hex(&from_obj
), oid_to_hex(&to_obj
));
566 finish_copy_notes_for_rewrite(the_repository
, c
, msg
);
573 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
574 * non-indented lines and checking if they look like they begin with valid
575 * header field names.
577 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
579 static int is_mail(FILE *fp
)
581 const char *header_regex
= "^[!-9;-~]+:";
582 struct strbuf sb
= STRBUF_INIT
;
586 if (fseek(fp
, 0L, SEEK_SET
))
587 die_errno(_("fseek failed"));
589 if (regcomp(®ex
, header_regex
, REG_NOSUB
| REG_EXTENDED
))
590 die("invalid pattern: %s", header_regex
);
592 while (!strbuf_getline(&sb
, fp
)) {
594 break; /* End of header */
596 /* Ignore indented folded lines */
597 if (*sb
.buf
== '\t' || *sb
.buf
== ' ')
600 /* It's a header if it matches header_regex */
601 if (regexec(®ex
, sb
.buf
, 0, NULL
, 0)) {
614 * Attempts to detect the patch_format of the patches contained in `paths`,
615 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
618 static int detect_patch_format(const char **paths
)
620 enum patch_format ret
= PATCH_FORMAT_UNKNOWN
;
621 struct strbuf l1
= STRBUF_INIT
;
622 struct strbuf l2
= STRBUF_INIT
;
623 struct strbuf l3
= STRBUF_INIT
;
627 * We default to mbox format if input is from stdin and for directories
629 if (!*paths
|| !strcmp(*paths
, "-") || is_directory(*paths
))
630 return PATCH_FORMAT_MBOX
;
633 * Otherwise, check the first few lines of the first patch, starting
634 * from the first non-blank line, to try to detect its format.
637 fp
= xfopen(*paths
, "r");
639 while (!strbuf_getline(&l1
, fp
)) {
644 if (starts_with(l1
.buf
, "From ") || starts_with(l1
.buf
, "From: ")) {
645 ret
= PATCH_FORMAT_MBOX
;
649 if (starts_with(l1
.buf
, "# This series applies on GIT commit")) {
650 ret
= PATCH_FORMAT_STGIT_SERIES
;
654 if (!strcmp(l1
.buf
, "# HG changeset patch")) {
655 ret
= PATCH_FORMAT_HG
;
659 strbuf_getline(&l2
, fp
);
660 strbuf_getline(&l3
, fp
);
663 * If the second line is empty and the third is a From, Author or Date
664 * entry, this is likely an StGit patch.
666 if (l1
.len
&& !l2
.len
&&
667 (starts_with(l3
.buf
, "From:") ||
668 starts_with(l3
.buf
, "Author:") ||
669 starts_with(l3
.buf
, "Date:"))) {
670 ret
= PATCH_FORMAT_STGIT
;
674 if (l1
.len
&& is_mail(fp
)) {
675 ret
= PATCH_FORMAT_MBOX
;
688 * Splits out individual email patches from `paths`, where each path is either
689 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
691 static int split_mail_mbox(struct am_state
*state
, const char **paths
,
692 int keep_cr
, int mboxrd
)
694 struct child_process cp
= CHILD_PROCESS_INIT
;
695 struct strbuf last
= STRBUF_INIT
;
699 strvec_push(&cp
.args
, "mailsplit");
700 strvec_pushf(&cp
.args
, "-d%d", state
->prec
);
701 strvec_pushf(&cp
.args
, "-o%s", state
->dir
);
702 strvec_push(&cp
.args
, "-b");
704 strvec_push(&cp
.args
, "--keep-cr");
706 strvec_push(&cp
.args
, "--mboxrd");
707 strvec_push(&cp
.args
, "--");
708 strvec_pushv(&cp
.args
, paths
);
710 ret
= capture_command(&cp
, &last
, 8);
715 state
->last
= strtol(last
.buf
, NULL
, 10);
718 strbuf_release(&last
);
723 * Callback signature for split_mail_conv(). The foreign patch should be
724 * read from `in`, and the converted patch (in RFC2822 mail format) should be
725 * written to `out`. Return 0 on success, or -1 on failure.
727 typedef int (*mail_conv_fn
)(FILE *out
, FILE *in
, int keep_cr
);
730 * Calls `fn` for each file in `paths` to convert the foreign patch to the
731 * RFC2822 mail format suitable for parsing with git-mailinfo.
733 * Returns 0 on success, -1 on failure.
735 static int split_mail_conv(mail_conv_fn fn
, struct am_state
*state
,
736 const char **paths
, int keep_cr
)
738 static const char *stdin_only
[] = {"-", NULL
};
744 for (i
= 0; *paths
; paths
++, i
++) {
749 if (!strcmp(*paths
, "-"))
752 in
= fopen(*paths
, "r");
755 return error_errno(_("could not open '%s' for reading"),
758 mail
= mkpath("%s/%0*d", state
->dir
, state
->prec
, i
+ 1);
760 out
= fopen(mail
, "w");
764 return error_errno(_("could not open '%s' for writing"),
768 ret
= fn(out
, in
, keep_cr
);
775 return error(_("could not parse patch '%s'"), *paths
);
784 * A split_mail_conv() callback that converts an StGit patch to an RFC2822
785 * message suitable for parsing with git-mailinfo.
787 static int stgit_patch_to_mail(FILE *out
, FILE *in
, int keep_cr
)
789 struct strbuf sb
= STRBUF_INIT
;
790 int subject_printed
= 0;
792 while (!strbuf_getline_lf(&sb
, in
)) {
795 if (str_isspace(sb
.buf
))
797 else if (skip_prefix(sb
.buf
, "Author:", &str
))
798 fprintf(out
, "From:%s\n", str
);
799 else if (starts_with(sb
.buf
, "From") || starts_with(sb
.buf
, "Date"))
800 fprintf(out
, "%s\n", sb
.buf
);
801 else if (!subject_printed
) {
802 fprintf(out
, "Subject: %s\n", sb
.buf
);
805 fprintf(out
, "\n%s\n", sb
.buf
);
811 while (strbuf_fread(&sb
, 8192, in
) > 0) {
812 fwrite(sb
.buf
, 1, sb
.len
, out
);
821 * This function only supports a single StGit series file in `paths`.
823 * Given an StGit series file, converts the StGit patches in the series into
824 * RFC2822 messages suitable for parsing with git-mailinfo, and queues them in
825 * the state directory.
827 * Returns 0 on success, -1 on failure.
829 static int split_mail_stgit_series(struct am_state
*state
, const char **paths
,
832 const char *series_dir
;
833 char *series_dir_buf
;
835 struct strvec patches
= STRVEC_INIT
;
836 struct strbuf sb
= STRBUF_INIT
;
839 if (!paths
[0] || paths
[1])
840 return error(_("Only one StGIT patch series can be applied at once"));
842 series_dir_buf
= xstrdup(*paths
);
843 series_dir
= dirname(series_dir_buf
);
845 fp
= fopen(*paths
, "r");
847 return error_errno(_("could not open '%s' for reading"), *paths
);
849 while (!strbuf_getline_lf(&sb
, fp
)) {
851 continue; /* skip comment lines */
853 strvec_push(&patches
, mkpath("%s/%s", series_dir
, sb
.buf
));
858 free(series_dir_buf
);
860 ret
= split_mail_conv(stgit_patch_to_mail
, state
, patches
.v
, keep_cr
);
862 strvec_clear(&patches
);
867 * A split_patches_conv() callback that converts a mercurial patch to a RFC2822
868 * message suitable for parsing with git-mailinfo.
870 static int hg_patch_to_mail(FILE *out
, FILE *in
, int keep_cr
)
872 struct strbuf sb
= STRBUF_INIT
;
875 while (!strbuf_getline_lf(&sb
, in
)) {
878 if (skip_prefix(sb
.buf
, "# User ", &str
))
879 fprintf(out
, "From: %s\n", str
);
880 else if (skip_prefix(sb
.buf
, "# Date ", &str
)) {
881 timestamp_t timestamp
;
886 timestamp
= parse_timestamp(str
, &end
, 10);
888 rc
= error(_("invalid timestamp"));
892 if (!skip_prefix(end
, " ", &str
)) {
893 rc
= error(_("invalid Date line"));
898 tz
= strtol(str
, &end
, 10);
900 rc
= error(_("invalid timezone offset"));
905 rc
= error(_("invalid Date line"));
910 * mercurial's timezone is in seconds west of UTC,
911 * however git's timezone is in hours + minutes east of
914 tz2
= labs(tz
) / 3600 * 100 + labs(tz
) % 3600 / 60;
918 fprintf(out
, "Date: %s\n", show_date(timestamp
, tz2
, DATE_MODE(RFC2822
)));
919 } else if (starts_with(sb
.buf
, "# ")) {
922 fprintf(out
, "\n%s\n", sb
.buf
);
928 while (strbuf_fread(&sb
, 8192, in
) > 0) {
929 fwrite(sb
.buf
, 1, sb
.len
, out
);
938 * Splits a list of files/directories into individual email patches. Each path
939 * in `paths` must be a file/directory that is formatted according to
942 * Once split out, the individual email patches will be stored in the state
943 * directory, with each patch's filename being its index, padded to state->prec
946 * state->cur will be set to the index of the first mail, and state->last will
947 * be set to the index of the last mail.
949 * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1
950 * to disable this behavior, -1 to use the default configured setting.
952 * Returns 0 on success, -1 on failure.
954 static int split_mail(struct am_state
*state
, enum patch_format patch_format
,
955 const char **paths
, int keep_cr
)
959 git_config_get_bool("am.keepcr", &keep_cr
);
962 switch (patch_format
) {
963 case PATCH_FORMAT_MBOX
:
964 return split_mail_mbox(state
, paths
, keep_cr
, 0);
965 case PATCH_FORMAT_STGIT
:
966 return split_mail_conv(stgit_patch_to_mail
, state
, paths
, keep_cr
);
967 case PATCH_FORMAT_STGIT_SERIES
:
968 return split_mail_stgit_series(state
, paths
, keep_cr
);
969 case PATCH_FORMAT_HG
:
970 return split_mail_conv(hg_patch_to_mail
, state
, paths
, keep_cr
);
971 case PATCH_FORMAT_MBOXRD
:
972 return split_mail_mbox(state
, paths
, keep_cr
, 1);
974 BUG("invalid patch_format");
980 * Setup a new am session for applying patches
982 static void am_setup(struct am_state
*state
, enum patch_format patch_format
,
983 const char **paths
, int keep_cr
)
985 struct object_id curr_head
;
987 struct strbuf sb
= STRBUF_INIT
;
990 patch_format
= detect_patch_format(paths
);
993 fprintf_ln(stderr
, _("Patch format detection failed."));
997 if (mkdir(state
->dir
, 0777) < 0 && errno
!= EEXIST
)
998 die_errno(_("failed to create directory '%s'"), state
->dir
);
999 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
1001 if (split_mail(state
, patch_format
, paths
, keep_cr
) < 0) {
1003 die(_("Failed to split patches."));
1006 if (state
->rebasing
)
1007 state
->threeway
= 1;
1009 write_state_bool(state
, "threeway", state
->threeway
);
1010 write_state_bool(state
, "quiet", state
->quiet
);
1011 write_state_bool(state
, "sign", state
->signoff
);
1012 write_state_bool(state
, "utf8", state
->utf8
);
1014 if (state
->allow_rerere_autoupdate
)
1015 write_state_bool(state
, "rerere-autoupdate",
1016 state
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
);
1018 switch (state
->keep
) {
1025 case KEEP_NON_PATCH
:
1029 BUG("invalid value for state->keep");
1032 write_state_text(state
, "keep", str
);
1033 write_state_bool(state
, "messageid", state
->message_id
);
1035 switch (state
->scissors
) {
1036 case SCISSORS_UNSET
:
1039 case SCISSORS_FALSE
:
1046 BUG("invalid value for state->scissors");
1048 write_state_text(state
, "scissors", str
);
1050 switch (state
->quoted_cr
) {
1051 case quoted_cr_unset
:
1054 case quoted_cr_nowarn
:
1057 case quoted_cr_warn
:
1060 case quoted_cr_strip
:
1064 BUG("invalid value for state->quoted_cr");
1066 write_state_text(state
, "quoted-cr", str
);
1068 sq_quote_argv(&sb
, state
->git_apply_opts
.v
);
1069 write_state_text(state
, "apply-opt", sb
.buf
);
1071 if (state
->rebasing
)
1072 write_state_text(state
, "rebasing", "");
1074 write_state_text(state
, "applying", "");
1076 if (!repo_get_oid(the_repository
, "HEAD", &curr_head
)) {
1077 write_state_text(state
, "abort-safety", oid_to_hex(&curr_head
));
1078 if (!state
->rebasing
)
1079 update_ref("am", "ORIG_HEAD", &curr_head
, NULL
, 0,
1080 UPDATE_REFS_DIE_ON_ERR
);
1082 write_state_text(state
, "abort-safety", "");
1083 if (!state
->rebasing
)
1084 delete_ref(NULL
, "ORIG_HEAD", NULL
, 0);
1088 * NOTE: Since the "next" and "last" files determine if an am_state
1089 * session is in progress, they should be written last.
1092 write_state_count(state
, "next", state
->cur
);
1093 write_state_count(state
, "last", state
->last
);
1095 strbuf_release(&sb
);
1099 * Increments the patch pointer, and cleans am_state for the application of the
1102 static void am_next(struct am_state
*state
)
1104 struct object_id head
;
1106 FREE_AND_NULL(state
->author_name
);
1107 FREE_AND_NULL(state
->author_email
);
1108 FREE_AND_NULL(state
->author_date
);
1109 FREE_AND_NULL(state
->msg
);
1112 unlink(am_path(state
, "author-script"));
1113 unlink(am_path(state
, "final-commit"));
1115 oidclr(&state
->orig_commit
);
1116 unlink(am_path(state
, "original-commit"));
1117 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
1119 if (!repo_get_oid(the_repository
, "HEAD", &head
))
1120 write_state_text(state
, "abort-safety", oid_to_hex(&head
));
1122 write_state_text(state
, "abort-safety", "");
1125 write_state_count(state
, "next", state
->cur
);
1129 * Returns the filename of the current patch email.
1131 static const char *msgnum(const struct am_state
*state
)
1133 static struct strbuf sb
= STRBUF_INIT
;
1136 strbuf_addf(&sb
, "%0*d", state
->prec
, state
->cur
);
1142 * Dies with a user-friendly message on how to proceed after resolving the
1143 * problem. This message can be overridden with state->resolvemsg.
1145 static void NORETURN
die_user_resolve(const struct am_state
*state
)
1147 if (state
->resolvemsg
) {
1148 printf_ln("%s", state
->resolvemsg
);
1150 const char *cmdline
= state
->interactive
? "git am -i" : "git am";
1152 printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline
);
1153 printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline
);
1155 if (advice_enabled(ADVICE_AM_WORK_DIR
) &&
1156 is_empty_or_missing_file(am_path(state
, "patch")) &&
1157 !repo_index_has_changes(the_repository
, NULL
, NULL
))
1158 printf_ln(_("To record the empty patch as an empty commit, run \"%s --allow-empty\"."), cmdline
);
1160 printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline
);
1167 * Appends signoff to the "msg" field of the am_state.
1169 static void am_append_signoff(struct am_state
*state
)
1171 struct strbuf sb
= STRBUF_INIT
;
1173 strbuf_attach(&sb
, state
->msg
, state
->msg_len
, state
->msg_len
);
1174 append_signoff(&sb
, 0, 0);
1175 state
->msg
= strbuf_detach(&sb
, &state
->msg_len
);
1179 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
1180 * state->msg will be set to the patch message. state->author_name,
1181 * state->author_email and state->author_date will be set to the patch author's
1182 * name, email and date respectively. The patch body will be written to the
1183 * state directory's "patch" file.
1185 * Returns 1 if the patch should be skipped, 0 otherwise.
1187 static int parse_mail(struct am_state
*state
, const char *mail
)
1190 struct strbuf sb
= STRBUF_INIT
;
1191 struct strbuf msg
= STRBUF_INIT
;
1192 struct strbuf author_name
= STRBUF_INIT
;
1193 struct strbuf author_date
= STRBUF_INIT
;
1194 struct strbuf author_email
= STRBUF_INIT
;
1198 setup_mailinfo(&mi
);
1201 mi
.metainfo_charset
= get_commit_output_encoding();
1203 mi
.metainfo_charset
= NULL
;
1205 switch (state
->keep
) {
1209 mi
.keep_subject
= 1;
1211 case KEEP_NON_PATCH
:
1212 mi
.keep_non_patch_brackets_in_subject
= 1;
1215 BUG("invalid value for state->keep");
1218 if (state
->message_id
)
1219 mi
.add_message_id
= 1;
1221 switch (state
->scissors
) {
1222 case SCISSORS_UNSET
:
1224 case SCISSORS_FALSE
:
1225 mi
.use_scissors
= 0;
1228 mi
.use_scissors
= 1;
1231 BUG("invalid value for state->scissors");
1234 switch (state
->quoted_cr
) {
1235 case quoted_cr_unset
:
1237 case quoted_cr_nowarn
:
1238 case quoted_cr_warn
:
1239 case quoted_cr_strip
:
1240 mi
.quoted_cr
= state
->quoted_cr
;
1243 BUG("invalid value for state->quoted_cr");
1246 mi
.input
= xfopen(mail
, "r");
1247 mi
.output
= xfopen(am_path(state
, "info"), "w");
1248 if (mailinfo(&mi
, am_path(state
, "msg"), am_path(state
, "patch")))
1249 die("could not parse patch");
1254 if (mi
.format_flowed
)
1255 warning(_("Patch sent with format=flowed; "
1256 "space at the end of lines might be lost."));
1258 /* Extract message and author information */
1259 fp
= xfopen(am_path(state
, "info"), "r");
1260 while (!strbuf_getline_lf(&sb
, fp
)) {
1263 if (skip_prefix(sb
.buf
, "Subject: ", &x
)) {
1265 strbuf_addch(&msg
, '\n');
1266 strbuf_addstr(&msg
, x
);
1267 } else if (skip_prefix(sb
.buf
, "Author: ", &x
))
1268 strbuf_addstr(&author_name
, x
);
1269 else if (skip_prefix(sb
.buf
, "Email: ", &x
))
1270 strbuf_addstr(&author_email
, x
);
1271 else if (skip_prefix(sb
.buf
, "Date: ", &x
))
1272 strbuf_addstr(&author_date
, x
);
1276 /* Skip pine's internal folder data */
1277 if (!strcmp(author_name
.buf
, "Mail System Internal Data")) {
1282 strbuf_addstr(&msg
, "\n\n");
1283 strbuf_addbuf(&msg
, &mi
.log_message
);
1284 strbuf_stripspace(&msg
, 0);
1286 assert(!state
->author_name
);
1287 state
->author_name
= strbuf_detach(&author_name
, NULL
);
1289 assert(!state
->author_email
);
1290 state
->author_email
= strbuf_detach(&author_email
, NULL
);
1292 assert(!state
->author_date
);
1293 state
->author_date
= strbuf_detach(&author_date
, NULL
);
1295 assert(!state
->msg
);
1296 state
->msg
= strbuf_detach(&msg
, &state
->msg_len
);
1299 strbuf_release(&msg
);
1300 strbuf_release(&author_date
);
1301 strbuf_release(&author_email
);
1302 strbuf_release(&author_name
);
1303 strbuf_release(&sb
);
1304 clear_mailinfo(&mi
);
1309 * Sets commit_id to the commit hash where the mail was generated from.
1310 * Returns 0 on success, -1 on failure.
1312 static int get_mail_commit_oid(struct object_id
*commit_id
, const char *mail
)
1314 struct strbuf sb
= STRBUF_INIT
;
1315 FILE *fp
= xfopen(mail
, "r");
1319 if (strbuf_getline_lf(&sb
, fp
) ||
1320 !skip_prefix(sb
.buf
, "From ", &x
) ||
1321 get_oid_hex(x
, commit_id
) < 0)
1324 strbuf_release(&sb
);
1330 * Sets state->msg, state->author_name, state->author_email, state->author_date
1331 * to the commit's respective info.
1333 static void get_commit_info(struct am_state
*state
, struct commit
*commit
)
1335 const char *buffer
, *ident_line
, *msg
;
1337 struct ident_split id
;
1339 buffer
= repo_logmsg_reencode(the_repository
, commit
, NULL
,
1340 get_commit_output_encoding());
1342 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
1344 die(_("missing author line in commit %s"),
1345 oid_to_hex(&commit
->object
.oid
));
1346 if (split_ident_line(&id
, ident_line
, ident_len
) < 0)
1347 die(_("invalid ident line: %.*s"), (int)ident_len
, ident_line
);
1349 assert(!state
->author_name
);
1351 state
->author_name
=
1352 xmemdupz(id
.name_begin
, id
.name_end
- id
.name_begin
);
1354 state
->author_name
= xstrdup("");
1356 assert(!state
->author_email
);
1358 state
->author_email
=
1359 xmemdupz(id
.mail_begin
, id
.mail_end
- id
.mail_begin
);
1361 state
->author_email
= xstrdup("");
1363 assert(!state
->author_date
);
1364 state
->author_date
= xstrdup(show_ident_date(&id
, DATE_MODE(NORMAL
)));
1366 assert(!state
->msg
);
1367 msg
= strstr(buffer
, "\n\n");
1369 die(_("unable to parse commit %s"), oid_to_hex(&commit
->object
.oid
));
1370 state
->msg
= xstrdup(msg
+ 2);
1371 state
->msg_len
= strlen(state
->msg
);
1372 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
1376 * Writes `commit` as a patch to the state directory's "patch" file.
1378 static void write_commit_patch(const struct am_state
*state
, struct commit
*commit
)
1380 struct rev_info rev_info
;
1383 fp
= xfopen(am_path(state
, "patch"), "w");
1384 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1386 rev_info
.abbrev
= 0;
1387 rev_info
.disable_stdin
= 1;
1388 rev_info
.show_root_diff
= 1;
1389 rev_info
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1390 rev_info
.no_commit_id
= 1;
1391 rev_info
.diffopt
.flags
.binary
= 1;
1392 rev_info
.diffopt
.flags
.full_index
= 1;
1393 rev_info
.diffopt
.use_color
= 0;
1394 rev_info
.diffopt
.file
= fp
;
1395 rev_info
.diffopt
.close_file
= 1;
1396 add_pending_object(&rev_info
, &commit
->object
, "");
1397 diff_setup_done(&rev_info
.diffopt
);
1398 log_tree_commit(&rev_info
, commit
);
1399 release_revisions(&rev_info
);
1403 * Writes the diff of the index against HEAD as a patch to the state
1404 * directory's "patch" file.
1406 static void write_index_patch(const struct am_state
*state
)
1409 struct object_id head
;
1410 struct rev_info rev_info
;
1413 if (!repo_get_oid(the_repository
, "HEAD", &head
)) {
1414 struct commit
*commit
= lookup_commit_or_die(&head
, "HEAD");
1415 tree
= repo_get_commit_tree(the_repository
, commit
);
1417 tree
= lookup_tree(the_repository
,
1418 the_repository
->hash_algo
->empty_tree
);
1420 fp
= xfopen(am_path(state
, "patch"), "w");
1421 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1423 rev_info
.disable_stdin
= 1;
1424 rev_info
.no_commit_id
= 1;
1425 rev_info
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1426 rev_info
.diffopt
.use_color
= 0;
1427 rev_info
.diffopt
.file
= fp
;
1428 rev_info
.diffopt
.close_file
= 1;
1429 add_pending_object(&rev_info
, &tree
->object
, "");
1430 diff_setup_done(&rev_info
.diffopt
);
1431 run_diff_index(&rev_info
, 1);
1432 release_revisions(&rev_info
);
1436 * Like parse_mail(), but parses the mail by looking up its commit ID
1437 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
1440 * state->orig_commit will be set to the original commit ID.
1442 * Will always return 0 as the patch should never be skipped.
1444 static int parse_mail_rebase(struct am_state
*state
, const char *mail
)
1446 struct commit
*commit
;
1447 struct object_id commit_oid
;
1449 if (get_mail_commit_oid(&commit_oid
, mail
) < 0)
1450 die(_("could not parse %s"), mail
);
1452 commit
= lookup_commit_or_die(&commit_oid
, mail
);
1454 get_commit_info(state
, commit
);
1456 write_commit_patch(state
, commit
);
1458 oidcpy(&state
->orig_commit
, &commit_oid
);
1459 write_state_text(state
, "original-commit", oid_to_hex(&commit_oid
));
1460 update_ref("am", "REBASE_HEAD", &commit_oid
,
1461 NULL
, REF_NO_DEREF
, UPDATE_REFS_DIE_ON_ERR
);
1467 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
1468 * `index_file` is not NULL, the patch will be applied to that index.
1470 static int run_apply(const struct am_state
*state
, const char *index_file
)
1472 struct strvec apply_paths
= STRVEC_INIT
;
1473 struct strvec apply_opts
= STRVEC_INIT
;
1474 struct apply_state apply_state
;
1476 int force_apply
= 0;
1478 const char **apply_argv
;
1480 if (init_apply_state(&apply_state
, the_repository
, NULL
))
1481 BUG("init_apply_state() failed");
1483 strvec_push(&apply_opts
, "apply");
1484 strvec_pushv(&apply_opts
, state
->git_apply_opts
.v
);
1487 * Build a copy that apply_parse_options() can rearrange.
1488 * apply_opts.v keeps referencing the allocated strings for
1489 * strvec_clear() to release.
1491 DUP_ARRAY(apply_argv
, apply_opts
.v
, apply_opts
.nr
);
1493 opts_left
= apply_parse_options(apply_opts
.nr
, apply_argv
,
1494 &apply_state
, &force_apply
, &options
,
1498 die("unknown option passed through to git apply");
1501 apply_state
.index_file
= index_file
;
1502 apply_state
.cached
= 1;
1504 apply_state
.check_index
= 1;
1507 * If we are allowed to fall back on 3-way merge, don't give false
1508 * errors during the initial attempt.
1510 if (state
->threeway
&& !index_file
)
1511 apply_state
.apply_verbosity
= verbosity_silent
;
1513 if (check_apply_state(&apply_state
, force_apply
))
1514 BUG("check_apply_state() failed");
1516 strvec_push(&apply_paths
, am_path(state
, "patch"));
1518 res
= apply_all_patches(&apply_state
, apply_paths
.nr
, apply_paths
.v
, options
);
1520 strvec_clear(&apply_paths
);
1521 strvec_clear(&apply_opts
);
1522 clear_apply_state(&apply_state
);
1529 /* Reload index as apply_all_patches() will have modified it. */
1530 discard_index(&the_index
);
1531 read_index_from(&the_index
, index_file
, get_git_dir());
1538 * Builds an index that contains just the blobs needed for a 3way merge.
1540 static int build_fake_ancestor(const struct am_state
*state
, const char *index_file
)
1542 struct child_process cp
= CHILD_PROCESS_INIT
;
1545 strvec_push(&cp
.args
, "apply");
1546 strvec_pushv(&cp
.args
, state
->git_apply_opts
.v
);
1547 strvec_pushf(&cp
.args
, "--build-fake-ancestor=%s", index_file
);
1548 strvec_push(&cp
.args
, am_path(state
, "patch"));
1550 if (run_command(&cp
))
1557 * Attempt a threeway merge, using index_path as the temporary index.
1559 static int fall_back_threeway(const struct am_state
*state
, const char *index_path
)
1561 struct object_id orig_tree
, their_tree
, our_tree
;
1562 const struct object_id
*bases
[1] = { &orig_tree
};
1563 struct merge_options o
;
1564 struct commit
*result
;
1565 char *their_tree_name
;
1567 if (repo_get_oid(the_repository
, "HEAD", &our_tree
) < 0)
1568 oidcpy(&our_tree
, the_hash_algo
->empty_tree
);
1570 if (build_fake_ancestor(state
, index_path
))
1571 return error("could not build fake ancestor");
1573 discard_index(&the_index
);
1574 read_index_from(&the_index
, index_path
, get_git_dir());
1576 if (write_index_as_tree(&orig_tree
, &the_index
, index_path
, 0, NULL
))
1577 return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1579 say(state
, stdout
, _("Using index info to reconstruct a base tree..."));
1581 if (!state
->quiet
) {
1583 * List paths that needed 3-way fallback, so that the user can
1584 * review them with extra care to spot mismerges.
1586 struct rev_info rev_info
;
1588 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1589 rev_info
.diffopt
.output_format
= DIFF_FORMAT_NAME_STATUS
;
1590 rev_info
.diffopt
.filter
|= diff_filter_bit('A');
1591 rev_info
.diffopt
.filter
|= diff_filter_bit('M');
1592 add_pending_oid(&rev_info
, "HEAD", &our_tree
, 0);
1593 diff_setup_done(&rev_info
.diffopt
);
1594 run_diff_index(&rev_info
, 1);
1595 release_revisions(&rev_info
);
1598 if (run_apply(state
, index_path
))
1599 return error(_("Did you hand edit your patch?\n"
1600 "It does not apply to blobs recorded in its index."));
1602 if (write_index_as_tree(&their_tree
, &the_index
, index_path
, 0, NULL
))
1603 return error("could not write tree");
1605 say(state
, stdout
, _("Falling back to patching base and 3-way merge..."));
1607 discard_index(&the_index
);
1608 repo_read_index(the_repository
);
1611 * This is not so wrong. Depending on which base we picked, orig_tree
1612 * may be wildly different from ours, but their_tree has the same set of
1613 * wildly different changes in parts the patch did not touch, so
1614 * recursive ends up canceling them, saying that we reverted all those
1618 init_merge_options(&o
, the_repository
);
1621 their_tree_name
= xstrfmt("%.*s", linelen(state
->msg
), state
->msg
);
1622 o
.branch2
= their_tree_name
;
1623 o
.detect_directory_renames
= MERGE_DIRECTORY_RENAMES_NONE
;
1628 if (merge_recursive_generic(&o
, &our_tree
, &their_tree
, 1, bases
, &result
)) {
1629 repo_rerere(the_repository
, state
->allow_rerere_autoupdate
);
1630 free(their_tree_name
);
1631 return error(_("Failed to merge in the changes."));
1634 free(their_tree_name
);
1639 * Commits the current index with state->msg as the commit message and
1640 * state->author_name, state->author_email and state->author_date as the author
1643 static void do_commit(const struct am_state
*state
)
1645 struct object_id tree
, parent
, commit
;
1646 const struct object_id
*old_oid
;
1647 struct commit_list
*parents
= NULL
;
1648 const char *reflog_msg
, *author
, *committer
= NULL
;
1649 struct strbuf sb
= STRBUF_INIT
;
1651 if (!state
->no_verify
&& run_hooks("pre-applypatch"))
1654 if (write_index_as_tree(&tree
, &the_index
, get_index_file(), 0, NULL
))
1655 die(_("git write-tree failed to write a tree"));
1657 if (!repo_get_oid_commit(the_repository
, "HEAD", &parent
)) {
1659 commit_list_insert(lookup_commit(the_repository
, &parent
),
1663 say(state
, stderr
, _("applying to an empty history"));
1666 author
= fmt_ident(state
->author_name
, state
->author_email
,
1668 state
->ignore_date
? NULL
: state
->author_date
,
1671 if (state
->committer_date_is_author_date
)
1672 committer
= fmt_ident(getenv("GIT_COMMITTER_NAME"),
1673 getenv("GIT_COMMITTER_EMAIL"),
1674 WANT_COMMITTER_IDENT
,
1675 state
->ignore_date
? NULL
1676 : state
->author_date
,
1679 if (commit_tree_extended(state
->msg
, state
->msg_len
, &tree
, parents
,
1680 &commit
, author
, committer
, state
->sign_commit
,
1682 die(_("failed to write commit object"));
1684 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1688 strbuf_addf(&sb
, "%s: %.*s", reflog_msg
, linelen(state
->msg
),
1691 update_ref(sb
.buf
, "HEAD", &commit
, old_oid
, 0,
1692 UPDATE_REFS_DIE_ON_ERR
);
1694 if (state
->rebasing
) {
1695 FILE *fp
= xfopen(am_path(state
, "rewritten"), "a");
1697 assert(!is_null_oid(&state
->orig_commit
));
1698 fprintf(fp
, "%s ", oid_to_hex(&state
->orig_commit
));
1699 fprintf(fp
, "%s\n", oid_to_hex(&commit
));
1703 run_hooks("post-applypatch");
1705 strbuf_release(&sb
);
1709 * Validates the am_state for resuming -- the "msg" and authorship fields must
1712 static void validate_resume_state(const struct am_state
*state
)
1715 die(_("cannot resume: %s does not exist."),
1716 am_path(state
, "final-commit"));
1718 if (!state
->author_name
|| !state
->author_email
|| !state
->author_date
)
1719 die(_("cannot resume: %s does not exist."),
1720 am_path(state
, "author-script"));
1724 * Interactively prompt the user on whether the current patch should be
1727 * Returns 0 if the user chooses to apply the patch, 1 if the user chooses to
1730 static int do_interactive(struct am_state
*state
)
1737 puts(_("Commit Body is:"));
1738 puts("--------------------------");
1739 printf("%s", state
->msg
);
1740 puts("--------------------------");
1743 * TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
1744 * in your translation. The program will only accept English
1745 * input at this point.
1747 printf(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "));
1748 if (!fgets(reply
, sizeof(reply
), stdin
))
1749 die("unable to read from stdin; aborting");
1751 if (*reply
== 'y' || *reply
== 'Y') {
1753 } else if (*reply
== 'a' || *reply
== 'A') {
1754 state
->interactive
= 0;
1756 } else if (*reply
== 'n' || *reply
== 'N') {
1758 } else if (*reply
== 'e' || *reply
== 'E') {
1759 struct strbuf msg
= STRBUF_INIT
;
1761 if (!launch_editor(am_path(state
, "final-commit"), &msg
, NULL
)) {
1763 state
->msg
= strbuf_detach(&msg
, &state
->msg_len
);
1765 strbuf_release(&msg
);
1766 } else if (*reply
== 'v' || *reply
== 'V') {
1767 const char *pager
= git_pager(1);
1768 struct child_process cp
= CHILD_PROCESS_INIT
;
1772 prepare_pager_args(&cp
, pager
);
1773 strvec_push(&cp
.args
, am_path(state
, "patch"));
1780 * Applies all queued mail.
1782 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
1783 * well as the state directory's "patch" file is used as-is for applying the
1784 * patch and committing it.
1786 static void am_run(struct am_state
*state
, int resume
)
1788 struct strbuf sb
= STRBUF_INIT
;
1790 unlink(am_path(state
, "dirtyindex"));
1792 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
, 0, 0,
1793 NULL
, NULL
, NULL
) < 0)
1794 die(_("unable to write index file"));
1796 if (repo_index_has_changes(the_repository
, NULL
, &sb
)) {
1797 write_state_bool(state
, "dirtyindex", 1);
1798 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb
.buf
);
1801 strbuf_release(&sb
);
1803 while (state
->cur
<= state
->last
) {
1804 const char *mail
= am_path(state
, msgnum(state
));
1810 if (!file_exists(mail
))
1814 validate_resume_state(state
);
1818 if (state
->rebasing
)
1819 skip
= parse_mail_rebase(state
, mail
);
1821 skip
= parse_mail(state
, mail
);
1824 goto next
; /* mail should be skipped */
1827 am_append_signoff(state
);
1829 write_author_script(state
);
1830 write_commit_msg(state
);
1833 if (state
->interactive
&& do_interactive(state
))
1837 if (is_empty_or_missing_file(am_path(state
, "patch"))) {
1838 switch (state
->empty_type
) {
1839 case DROP_EMPTY_COMMIT
:
1840 say(state
, stdout
, _("Skipping: %.*s"), linelen(state
->msg
), state
->msg
);
1843 case KEEP_EMPTY_COMMIT
:
1845 say(state
, stdout
, _("Creating an empty commit: %.*s"),
1846 linelen(state
->msg
), state
->msg
);
1848 case STOP_ON_EMPTY_COMMIT
:
1849 printf_ln(_("Patch is empty."));
1850 die_user_resolve(state
);
1855 if (run_applypatch_msg_hook(state
))
1860 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1862 apply_status
= run_apply(state
, NULL
);
1864 if (apply_status
&& state
->threeway
) {
1865 struct strbuf sb
= STRBUF_INIT
;
1867 strbuf_addstr(&sb
, am_path(state
, "patch-merge-index"));
1868 apply_status
= fall_back_threeway(state
, sb
.buf
);
1869 strbuf_release(&sb
);
1872 * Applying the patch to an earlier tree and merging
1873 * the result may have produced the same tree as ours.
1875 if (!apply_status
&&
1876 !repo_index_has_changes(the_repository
, NULL
, NULL
)) {
1877 say(state
, stdout
, _("No changes -- Patch already applied."));
1883 printf_ln(_("Patch failed at %s %.*s"), msgnum(state
),
1884 linelen(state
->msg
), state
->msg
);
1886 if (advice_enabled(ADVICE_AM_WORK_DIR
))
1887 advise(_("Use 'git am --show-current-patch=diff' to see the failed patch"));
1889 die_user_resolve(state
);
1903 if (!is_empty_or_missing_file(am_path(state
, "rewritten"))) {
1904 assert(state
->rebasing
);
1905 copy_notes_for_rebase(state
);
1906 run_post_rewrite_hook(state
);
1910 * In rebasing mode, it's up to the caller to take care of
1913 if (!state
->rebasing
) {
1915 run_auto_maintenance(state
->quiet
);
1920 * Resume the current am session after patch application failure. The user did
1921 * all the hard work, and we do not have to do any patch application. Just
1922 * trust and commit what the user has in the index and working tree. If `allow_empty`
1923 * is true, commit as an empty commit when index has not changed and lacking a patch.
1925 static void am_resolve(struct am_state
*state
, int allow_empty
)
1927 validate_resume_state(state
);
1929 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1931 if (!repo_index_has_changes(the_repository
, NULL
, NULL
)) {
1932 if (allow_empty
&& is_empty_or_missing_file(am_path(state
, "patch"))) {
1933 printf_ln(_("No changes - recorded it as an empty commit."));
1935 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1936 "If there is nothing left to stage, chances are that something else\n"
1937 "already introduced the same changes; you might want to skip this patch."));
1938 die_user_resolve(state
);
1942 if (unmerged_index(&the_index
)) {
1943 printf_ln(_("You still have unmerged paths in your index.\n"
1944 "You should 'git add' each file with resolved conflicts to mark them as such.\n"
1945 "You might run `git rm` on a file to accept \"deleted by them\" for it."));
1946 die_user_resolve(state
);
1949 if (state
->interactive
) {
1950 write_index_patch(state
);
1951 if (do_interactive(state
))
1955 repo_rerere(the_repository
, 0);
1966 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1967 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1970 static int fast_forward_to(struct tree
*head
, struct tree
*remote
, int reset
)
1972 struct lock_file lock_file
= LOCK_INIT
;
1973 struct unpack_trees_options opts
;
1974 struct tree_desc t
[2];
1976 if (parse_tree(head
) || parse_tree(remote
))
1979 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
1981 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
1983 memset(&opts
, 0, sizeof(opts
));
1985 opts
.src_index
= &the_index
;
1986 opts
.dst_index
= &the_index
;
1989 opts
.reset
= reset
? UNPACK_RESET_PROTECT_UNTRACKED
: 0;
1990 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
1991 opts
.fn
= twoway_merge
;
1992 init_tree_desc(&t
[0], head
->buffer
, head
->size
);
1993 init_tree_desc(&t
[1], remote
->buffer
, remote
->size
);
1995 if (unpack_trees(2, t
, &opts
)) {
1996 rollback_lock_file(&lock_file
);
2000 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
2001 die(_("unable to write new index file"));
2007 * Merges a tree into the index. The index's stat info will take precedence
2008 * over the merged tree's. Returns 0 on success, -1 on failure.
2010 static int merge_tree(struct tree
*tree
)
2012 struct lock_file lock_file
= LOCK_INIT
;
2013 struct unpack_trees_options opts
;
2014 struct tree_desc t
[1];
2016 if (parse_tree(tree
))
2019 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
2021 memset(&opts
, 0, sizeof(opts
));
2023 opts
.src_index
= &the_index
;
2024 opts
.dst_index
= &the_index
;
2026 opts
.fn
= oneway_merge
;
2027 init_tree_desc(&t
[0], tree
->buffer
, tree
->size
);
2029 if (unpack_trees(1, t
, &opts
)) {
2030 rollback_lock_file(&lock_file
);
2034 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
2035 die(_("unable to write new index file"));
2041 * Clean the index without touching entries that are not modified between
2042 * `head` and `remote`.
2044 static int clean_index(const struct object_id
*head
, const struct object_id
*remote
)
2046 struct tree
*head_tree
, *remote_tree
, *index_tree
;
2047 struct object_id index
;
2049 head_tree
= parse_tree_indirect(head
);
2051 return error(_("Could not parse object '%s'."), oid_to_hex(head
));
2053 remote_tree
= parse_tree_indirect(remote
);
2055 return error(_("Could not parse object '%s'."), oid_to_hex(remote
));
2057 repo_read_index_unmerged(the_repository
);
2059 if (fast_forward_to(head_tree
, head_tree
, 1))
2062 if (write_index_as_tree(&index
, &the_index
, get_index_file(), 0, NULL
))
2065 index_tree
= parse_tree_indirect(&index
);
2067 return error(_("Could not parse object '%s'."), oid_to_hex(&index
));
2069 if (fast_forward_to(index_tree
, remote_tree
, 0))
2072 if (merge_tree(remote_tree
))
2075 remove_branch_state(the_repository
, 0);
2081 * Resets rerere's merge resolution metadata.
2083 static void am_rerere_clear(void)
2085 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
2086 rerere_clear(the_repository
, &merge_rr
);
2087 string_list_clear(&merge_rr
, 1);
2091 * Resume the current am session by skipping the current patch.
2093 static void am_skip(struct am_state
*state
)
2095 struct object_id head
;
2099 if (repo_get_oid(the_repository
, "HEAD", &head
))
2100 oidcpy(&head
, the_hash_algo
->empty_tree
);
2102 if (clean_index(&head
, &head
))
2103 die(_("failed to clean index"));
2105 if (state
->rebasing
) {
2106 FILE *fp
= xfopen(am_path(state
, "rewritten"), "a");
2108 assert(!is_null_oid(&state
->orig_commit
));
2109 fprintf(fp
, "%s ", oid_to_hex(&state
->orig_commit
));
2110 fprintf(fp
, "%s\n", oid_to_hex(&head
));
2120 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
2122 * It is not safe to reset HEAD when:
2123 * 1. git-am previously failed because the index was dirty.
2124 * 2. HEAD has moved since git-am previously failed.
2126 static int safe_to_abort(const struct am_state
*state
)
2128 struct strbuf sb
= STRBUF_INIT
;
2129 struct object_id abort_safety
, head
;
2131 if (file_exists(am_path(state
, "dirtyindex")))
2134 if (read_state_file(&sb
, state
, "abort-safety", 1) > 0) {
2135 if (get_oid_hex(sb
.buf
, &abort_safety
))
2136 die(_("could not parse %s"), am_path(state
, "abort-safety"));
2138 oidclr(&abort_safety
);
2139 strbuf_release(&sb
);
2141 if (repo_get_oid(the_repository
, "HEAD", &head
))
2144 if (oideq(&head
, &abort_safety
))
2147 warning(_("You seem to have moved HEAD since the last 'am' failure.\n"
2148 "Not rewinding to ORIG_HEAD"));
2154 * Aborts the current am session if it is safe to do so.
2156 static void am_abort(struct am_state
*state
)
2158 struct object_id curr_head
, orig_head
;
2159 int has_curr_head
, has_orig_head
;
2162 if (!safe_to_abort(state
)) {
2169 curr_branch
= resolve_refdup("HEAD", 0, &curr_head
, NULL
);
2170 has_curr_head
= curr_branch
&& !is_null_oid(&curr_head
);
2172 oidcpy(&curr_head
, the_hash_algo
->empty_tree
);
2174 has_orig_head
= !repo_get_oid(the_repository
, "ORIG_HEAD", &orig_head
);
2176 oidcpy(&orig_head
, the_hash_algo
->empty_tree
);
2178 if (clean_index(&curr_head
, &orig_head
))
2179 die(_("failed to clean index"));
2182 update_ref("am --abort", "HEAD", &orig_head
,
2183 has_curr_head
? &curr_head
: NULL
, 0,
2184 UPDATE_REFS_DIE_ON_ERR
);
2185 else if (curr_branch
)
2186 delete_ref(NULL
, curr_branch
, NULL
, REF_NO_DEREF
);
2192 static int show_patch(struct am_state
*state
, enum show_patch_type sub_mode
)
2194 struct strbuf sb
= STRBUF_INIT
;
2195 const char *patch_path
;
2198 if (!is_null_oid(&state
->orig_commit
)) {
2199 struct child_process cmd
= CHILD_PROCESS_INIT
;
2201 strvec_pushl(&cmd
.args
, "show", oid_to_hex(&state
->orig_commit
),
2204 return run_command(&cmd
);
2208 case SHOW_PATCH_RAW
:
2209 patch_path
= am_path(state
, msgnum(state
));
2211 case SHOW_PATCH_DIFF
:
2212 patch_path
= am_path(state
, "patch");
2215 BUG("invalid mode for --show-current-patch");
2218 len
= strbuf_read_file(&sb
, patch_path
, 0);
2220 die_errno(_("failed to read '%s'"), patch_path
);
2223 write_in_full(1, sb
.buf
, sb
.len
);
2224 strbuf_release(&sb
);
2229 * parse_options() callback that validates and sets opt->value to the
2230 * PATCH_FORMAT_* enum value corresponding to `arg`.
2232 static int parse_opt_patchformat(const struct option
*opt
, const char *arg
, int unset
)
2234 int *opt_value
= opt
->value
;
2237 *opt_value
= PATCH_FORMAT_UNKNOWN
;
2238 else if (!strcmp(arg
, "mbox"))
2239 *opt_value
= PATCH_FORMAT_MBOX
;
2240 else if (!strcmp(arg
, "stgit"))
2241 *opt_value
= PATCH_FORMAT_STGIT
;
2242 else if (!strcmp(arg
, "stgit-series"))
2243 *opt_value
= PATCH_FORMAT_STGIT_SERIES
;
2244 else if (!strcmp(arg
, "hg"))
2245 *opt_value
= PATCH_FORMAT_HG
;
2246 else if (!strcmp(arg
, "mboxrd"))
2247 *opt_value
= PATCH_FORMAT_MBOXRD
;
2249 * Please update $__git_patchformat in git-completion.bash
2250 * when you add new options
2253 return error(_("invalid value for '%s': '%s'"),
2254 "--patch-format", arg
);
2269 struct resume_mode
{
2270 enum resume_type mode
;
2271 enum show_patch_type sub_mode
;
2274 static int parse_opt_show_current_patch(const struct option
*opt
, const char *arg
, int unset
)
2276 int *opt_value
= opt
->value
;
2277 struct resume_mode
*resume
= container_of(opt_value
, struct resume_mode
, mode
);
2280 * Please update $__git_showcurrentpatch in git-completion.bash
2281 * when you add new options
2283 const char *valid_modes
[] = {
2284 [SHOW_PATCH_DIFF
] = "diff",
2285 [SHOW_PATCH_RAW
] = "raw"
2287 int new_value
= SHOW_PATCH_RAW
;
2289 BUG_ON_OPT_NEG(unset
);
2292 for (new_value
= 0; new_value
< ARRAY_SIZE(valid_modes
); new_value
++) {
2293 if (!strcmp(arg
, valid_modes
[new_value
]))
2296 if (new_value
>= ARRAY_SIZE(valid_modes
))
2297 return error(_("invalid value for '%s': '%s'"),
2298 "--show-current-patch", arg
);
2301 if (resume
->mode
== RESUME_SHOW_PATCH
&& new_value
!= resume
->sub_mode
)
2302 return error(_("options '%s=%s' and '%s=%s' "
2303 "cannot be used together"),
2304 "--show-current-patch", "--show-current-patch", arg
, valid_modes
[resume
->sub_mode
]);
2306 resume
->mode
= RESUME_SHOW_PATCH
;
2307 resume
->sub_mode
= new_value
;
2311 int cmd_am(int argc
, const char **argv
, const char *prefix
)
2313 struct am_state state
;
2316 int patch_format
= PATCH_FORMAT_UNKNOWN
;
2317 struct resume_mode resume
= { .mode
= RESUME_FALSE
};
2321 const char * const usage
[] = {
2322 N_("git am [<options>] [(<mbox> | <Maildir>)...]"),
2323 N_("git am [<options>] (--continue | --skip | --abort)"),
2327 struct option options
[] = {
2328 OPT_BOOL('i', "interactive", &state
.interactive
,
2329 N_("run interactively")),
2330 OPT_BOOL('n', "no-verify", &state
.no_verify
,
2331 N_("bypass pre-applypatch and applypatch-msg hooks")),
2332 OPT_HIDDEN_BOOL('b', "binary", &binary
,
2333 N_("historical option -- no-op")),
2334 OPT_BOOL('3', "3way", &state
.threeway
,
2335 N_("allow fall back on 3way merging if needed")),
2336 OPT__QUIET(&state
.quiet
, N_("be quiet")),
2337 OPT_SET_INT('s', "signoff", &state
.signoff
,
2338 N_("add a Signed-off-by trailer to the commit message"),
2340 OPT_BOOL('u', "utf8", &state
.utf8
,
2341 N_("recode into utf8 (default)")),
2342 OPT_SET_INT('k', "keep", &state
.keep
,
2343 N_("pass -k flag to git-mailinfo"), KEEP_TRUE
),
2344 OPT_SET_INT(0, "keep-non-patch", &state
.keep
,
2345 N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH
),
2346 OPT_BOOL('m', "message-id", &state
.message_id
,
2347 N_("pass -m flag to git-mailinfo")),
2348 OPT_SET_INT_F(0, "keep-cr", &keep_cr
,
2349 N_("pass --keep-cr flag to git-mailsplit for mbox format"),
2350 1, PARSE_OPT_NONEG
),
2351 OPT_SET_INT_F(0, "no-keep-cr", &keep_cr
,
2352 N_("do not pass --keep-cr flag to git-mailsplit independent of am.keepcr"),
2353 0, PARSE_OPT_NONEG
),
2354 OPT_BOOL('c', "scissors", &state
.scissors
,
2355 N_("strip everything before a scissors line")),
2356 OPT_CALLBACK_F(0, "quoted-cr", &state
.quoted_cr
, N_("action"),
2357 N_("pass it through git-mailinfo"),
2358 PARSE_OPT_NONEG
, am_option_parse_quoted_cr
),
2359 OPT_PASSTHRU_ARGV(0, "whitespace", &state
.git_apply_opts
, N_("action"),
2360 N_("pass it through git-apply"),
2362 OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state
.git_apply_opts
, NULL
,
2363 N_("pass it through git-apply"),
2365 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state
.git_apply_opts
, NULL
,
2366 N_("pass it through git-apply"),
2368 OPT_PASSTHRU_ARGV(0, "directory", &state
.git_apply_opts
, N_("root"),
2369 N_("pass it through git-apply"),
2371 OPT_PASSTHRU_ARGV(0, "exclude", &state
.git_apply_opts
, N_("path"),
2372 N_("pass it through git-apply"),
2374 OPT_PASSTHRU_ARGV(0, "include", &state
.git_apply_opts
, N_("path"),
2375 N_("pass it through git-apply"),
2377 OPT_PASSTHRU_ARGV('C', NULL
, &state
.git_apply_opts
, N_("n"),
2378 N_("pass it through git-apply"),
2380 OPT_PASSTHRU_ARGV('p', NULL
, &state
.git_apply_opts
, N_("num"),
2381 N_("pass it through git-apply"),
2383 OPT_CALLBACK(0, "patch-format", &patch_format
, N_("format"),
2384 N_("format the patch(es) are in"),
2385 parse_opt_patchformat
),
2386 OPT_PASSTHRU_ARGV(0, "reject", &state
.git_apply_opts
, NULL
,
2387 N_("pass it through git-apply"),
2389 OPT_STRING(0, "resolvemsg", &state
.resolvemsg
, NULL
,
2390 N_("override error message when patch failure occurs")),
2391 OPT_CMDMODE(0, "continue", &resume
.mode
,
2392 N_("continue applying patches after resolving a conflict"),
2394 OPT_CMDMODE('r', "resolved", &resume
.mode
,
2395 N_("synonyms for --continue"),
2397 OPT_CMDMODE(0, "skip", &resume
.mode
,
2398 N_("skip the current patch"),
2400 OPT_CMDMODE(0, "abort", &resume
.mode
,
2401 N_("restore the original branch and abort the patching operation"),
2403 OPT_CMDMODE(0, "quit", &resume
.mode
,
2404 N_("abort the patching operation but keep HEAD where it is"),
2406 { OPTION_CALLBACK
, 0, "show-current-patch", &resume
.mode
,
2408 N_("show the patch being applied"),
2409 PARSE_OPT_CMDMODE
| PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
| PARSE_OPT_LITERAL_ARGHELP
,
2410 parse_opt_show_current_patch
, RESUME_SHOW_PATCH
},
2411 OPT_CMDMODE(0, "allow-empty", &resume
.mode
,
2412 N_("record the empty patch as an empty commit"),
2413 RESUME_ALLOW_EMPTY
),
2414 OPT_BOOL(0, "committer-date-is-author-date",
2415 &state
.committer_date_is_author_date
,
2416 N_("lie about committer date")),
2417 OPT_BOOL(0, "ignore-date", &state
.ignore_date
,
2418 N_("use current timestamp for author date")),
2419 OPT_RERERE_AUTOUPDATE(&state
.allow_rerere_autoupdate
),
2420 { OPTION_STRING
, 'S', "gpg-sign", &state
.sign_commit
, N_("key-id"),
2421 N_("GPG-sign commits"),
2422 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
2423 OPT_CALLBACK_F(STOP_ON_EMPTY_COMMIT
, "empty", &state
.empty_type
, "{stop,drop,keep}",
2424 N_("how to handle empty patches"),
2425 PARSE_OPT_NONEG
, am_option_parse_empty
),
2426 OPT_HIDDEN_BOOL(0, "rebasing", &state
.rebasing
,
2427 N_("(internal use for git-rebase)")),
2431 if (argc
== 2 && !strcmp(argv
[1], "-h"))
2432 usage_with_options(usage
, options
);
2434 git_config(git_default_config
, NULL
);
2436 am_state_init(&state
);
2438 in_progress
= am_in_progress(&state
);
2442 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2445 fprintf_ln(stderr
, _("The -b/--binary option has been a no-op for long time, and\n"
2446 "it will be removed. Please do not use it anymore."));
2448 /* Ensure a valid committer ident can be constructed */
2449 git_committer_info(IDENT_STRICT
);
2451 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
2452 die(_("failed to read the index"));
2456 * Catch user error to feed us patches when there is a session
2459 * 1. mbox path(s) are provided on the command-line.
2460 * 2. stdin is not a tty: the user is trying to feed us a patch
2461 * from standard input. This is somewhat unreliable -- stdin
2462 * could be /dev/null for example and the caller did not
2463 * intend to feed us a patch but wanted to continue
2466 if (argc
|| (resume
.mode
== RESUME_FALSE
&& !isatty(0)))
2467 die(_("previous rebase directory %s still exists but mbox given."),
2470 if (resume
.mode
== RESUME_FALSE
)
2471 resume
.mode
= RESUME_APPLY
;
2473 if (state
.signoff
== SIGNOFF_EXPLICIT
)
2474 am_append_signoff(&state
);
2476 struct strvec paths
= STRVEC_INIT
;
2480 * Handle stray state directory in the independent-run case. In
2481 * the --rebasing case, it is up to the caller to take care of
2482 * stray directories.
2484 if (file_exists(state
.dir
) && !state
.rebasing
) {
2485 if (resume
.mode
== RESUME_ABORT
|| resume
.mode
== RESUME_QUIT
) {
2487 am_state_release(&state
);
2491 die(_("Stray %s directory found.\n"
2492 "Use \"git am --abort\" to remove it."),
2497 die(_("Resolve operation not in progress, we are not resuming."));
2499 for (i
= 0; i
< argc
; i
++) {
2500 if (is_absolute_path(argv
[i
]) || !prefix
)
2501 strvec_push(&paths
, argv
[i
]);
2503 strvec_push(&paths
, mkpath("%s/%s", prefix
, argv
[i
]));
2506 if (state
.interactive
&& !paths
.nr
)
2507 die(_("interactive mode requires patches on the command line"));
2509 am_setup(&state
, patch_format
, paths
.v
, keep_cr
);
2511 strvec_clear(&paths
);
2514 switch (resume
.mode
) {
2521 case RESUME_RESOLVED
:
2522 case RESUME_ALLOW_EMPTY
:
2523 am_resolve(&state
, resume
.mode
== RESUME_ALLOW_EMPTY
? 1 : 0);
2535 case RESUME_SHOW_PATCH
:
2536 ret
= show_patch(&state
, resume
.sub_mode
);
2539 BUG("invalid resume value");
2542 am_state_release(&state
);