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 "preload-index.h"
32 #include "sequencer.h"
34 #include "merge-recursive.h"
36 #include "notes-utils.h"
41 #include "string-list.h"
45 #include "repository.h"
50 * Returns the length of the first line of msg.
52 static int linelen(const char *msg
)
54 return strchrnul(msg
, '\n') - msg
;
58 * Returns true if `str` consists of only whitespace, false otherwise.
60 static int str_isspace(const char *str
)
70 PATCH_FORMAT_UNKNOWN
= 0,
73 PATCH_FORMAT_STGIT_SERIES
,
80 KEEP_TRUE
, /* pass -k flag to git-mailinfo */
81 KEEP_NON_PATCH
/* pass -b flag to git-mailinfo */
86 SCISSORS_FALSE
= 0, /* pass --no-scissors to git-mailinfo */
87 SCISSORS_TRUE
/* pass --scissors to git-mailinfo */
93 SIGNOFF_EXPLICIT
/* --signoff was set on the command-line */
96 enum show_patch_type
{
102 STOP_ON_EMPTY_COMMIT
= 0, /* output errors and stop in the middle of an am session */
103 DROP_EMPTY_COMMIT
, /* skip with a notice message, unless "--quiet" has been passed */
104 KEEP_EMPTY_COMMIT
, /* keep recording as empty commits */
108 /* state directory path */
111 /* current and last patch numbers, 1-indexed */
115 /* commit metadata and message */
122 /* when --rebasing, records the original commit the patch came from */
123 struct object_id orig_commit
;
125 /* number of digits in patch filename */
128 /* various operating modes and command line options */
133 int signoff
; /* enum signoff_type */
135 int keep
; /* enum keep_type */
137 int scissors
; /* enum scissors_type */
138 int quoted_cr
; /* enum quoted_cr_action */
139 int empty_type
; /* enum empty_action */
140 struct strvec git_apply_opts
;
141 const char *resolvemsg
;
142 int committer_date_is_author_date
;
144 int allow_rerere_autoupdate
;
145 const char *sign_commit
;
150 * Initializes am_state with the default values.
152 static void am_state_init(struct am_state
*state
)
156 memset(state
, 0, sizeof(*state
));
158 state
->dir
= git_pathdup("rebase-apply");
162 git_config_get_bool("am.threeway", &state
->threeway
);
166 git_config_get_bool("am.messageid", &state
->message_id
);
168 state
->scissors
= SCISSORS_UNSET
;
169 state
->quoted_cr
= quoted_cr_unset
;
171 strvec_init(&state
->git_apply_opts
);
173 if (!git_config_get_bool("commit.gpgsign", &gpgsign
))
174 state
->sign_commit
= gpgsign
? "" : NULL
;
178 * Releases memory allocated by an am_state.
180 static void am_state_release(struct am_state
*state
)
183 free(state
->author_name
);
184 free(state
->author_email
);
185 free(state
->author_date
);
187 strvec_clear(&state
->git_apply_opts
);
190 static int am_option_parse_quoted_cr(const struct option
*opt
,
191 const char *arg
, int unset
)
193 BUG_ON_OPT_NEG(unset
);
195 if (mailinfo_parse_quoted_cr_action(arg
, opt
->value
) != 0)
196 return error(_("bad action '%s' for '%s'"), arg
, "--quoted-cr");
200 static int am_option_parse_empty(const struct option
*opt
,
201 const char *arg
, int unset
)
203 int *opt_value
= opt
->value
;
205 BUG_ON_OPT_NEG(unset
);
207 if (!strcmp(arg
, "stop"))
208 *opt_value
= STOP_ON_EMPTY_COMMIT
;
209 else if (!strcmp(arg
, "drop"))
210 *opt_value
= DROP_EMPTY_COMMIT
;
211 else if (!strcmp(arg
, "keep"))
212 *opt_value
= KEEP_EMPTY_COMMIT
;
214 return error(_("invalid value for '%s': '%s'"), "--empty", arg
);
220 * Returns path relative to the am_state directory.
222 static inline const char *am_path(const struct am_state
*state
, const char *path
)
224 return mkpath("%s/%s", state
->dir
, path
);
228 * For convenience to call write_file()
230 static void write_state_text(const struct am_state
*state
,
231 const char *name
, const char *string
)
233 write_file(am_path(state
, name
), "%s", string
);
236 static void write_state_count(const struct am_state
*state
,
237 const char *name
, int value
)
239 write_file(am_path(state
, name
), "%d", value
);
242 static void write_state_bool(const struct am_state
*state
,
243 const char *name
, int value
)
245 write_state_text(state
, name
, value
? "t" : "f");
249 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
252 __attribute__((format (printf
, 3, 4)))
253 static void say(const struct am_state
*state
, FILE *fp
, const char *fmt
, ...)
259 vfprintf(fp
, fmt
, ap
);
266 * Returns 1 if there is an am session in progress, 0 otherwise.
268 static int am_in_progress(const struct am_state
*state
)
272 if (lstat(state
->dir
, &st
) < 0 || !S_ISDIR(st
.st_mode
))
274 if (lstat(am_path(state
, "last"), &st
) || !S_ISREG(st
.st_mode
))
276 if (lstat(am_path(state
, "next"), &st
) || !S_ISREG(st
.st_mode
))
282 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
283 * number of bytes read on success, -1 if the file does not exist. If `trim` is
284 * set, trailing whitespace will be removed.
286 static int read_state_file(struct strbuf
*sb
, const struct am_state
*state
,
287 const char *file
, int trim
)
291 if (strbuf_read_file(sb
, am_path(state
, file
), 0) >= 0) {
301 die_errno(_("could not read '%s'"), am_path(state
, file
));
305 * Reads and parses the state directory's "author-script" file, and sets
306 * state->author_name, state->author_email and state->author_date accordingly.
307 * Returns 0 on success, -1 if the file could not be parsed.
309 * The author script is of the format:
311 * GIT_AUTHOR_NAME='$author_name'
312 * GIT_AUTHOR_EMAIL='$author_email'
313 * GIT_AUTHOR_DATE='$author_date'
315 * where $author_name, $author_email and $author_date are quoted. We are strict
316 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
317 * script, and thus if the file differs from what this function expects, it is
318 * better to bail out than to do something that the user does not expect.
320 static int read_am_author_script(struct am_state
*state
)
322 const char *filename
= am_path(state
, "author-script");
324 assert(!state
->author_name
);
325 assert(!state
->author_email
);
326 assert(!state
->author_date
);
328 return read_author_script(filename
, &state
->author_name
,
329 &state
->author_email
, &state
->author_date
, 1);
333 * Saves state->author_name, state->author_email and state->author_date in the
334 * state directory's "author-script" file.
336 static void write_author_script(const struct am_state
*state
)
338 struct strbuf sb
= STRBUF_INIT
;
340 strbuf_addstr(&sb
, "GIT_AUTHOR_NAME=");
341 sq_quote_buf(&sb
, state
->author_name
);
342 strbuf_addch(&sb
, '\n');
344 strbuf_addstr(&sb
, "GIT_AUTHOR_EMAIL=");
345 sq_quote_buf(&sb
, state
->author_email
);
346 strbuf_addch(&sb
, '\n');
348 strbuf_addstr(&sb
, "GIT_AUTHOR_DATE=");
349 sq_quote_buf(&sb
, state
->author_date
);
350 strbuf_addch(&sb
, '\n');
352 write_state_text(state
, "author-script", sb
.buf
);
358 * Reads the commit message from the state directory's "final-commit" file,
359 * setting state->msg to its contents and state->msg_len to the length of its
362 * Returns 0 on success, -1 if the file does not exist.
364 static int read_commit_msg(struct am_state
*state
)
366 struct strbuf sb
= STRBUF_INIT
;
370 if (read_state_file(&sb
, state
, "final-commit", 0) < 0) {
375 state
->msg
= strbuf_detach(&sb
, &state
->msg_len
);
380 * Saves state->msg in the state directory's "final-commit" file.
382 static void write_commit_msg(const struct am_state
*state
)
384 const char *filename
= am_path(state
, "final-commit");
385 write_file_buf(filename
, state
->msg
, state
->msg_len
);
389 * Loads state from disk.
391 static void am_load(struct am_state
*state
)
393 struct strbuf sb
= STRBUF_INIT
;
395 if (read_state_file(&sb
, state
, "next", 1) < 0)
396 BUG("state file 'next' does not exist");
397 state
->cur
= strtol(sb
.buf
, NULL
, 10);
399 if (read_state_file(&sb
, state
, "last", 1) < 0)
400 BUG("state file 'last' does not exist");
401 state
->last
= strtol(sb
.buf
, NULL
, 10);
403 if (read_am_author_script(state
) < 0)
404 die(_("could not parse author script"));
406 read_commit_msg(state
);
408 if (read_state_file(&sb
, state
, "original-commit", 1) < 0)
409 oidclr(&state
->orig_commit
);
410 else if (get_oid_hex(sb
.buf
, &state
->orig_commit
) < 0)
411 die(_("could not parse %s"), am_path(state
, "original-commit"));
413 read_state_file(&sb
, state
, "threeway", 1);
414 state
->threeway
= !strcmp(sb
.buf
, "t");
416 read_state_file(&sb
, state
, "quiet", 1);
417 state
->quiet
= !strcmp(sb
.buf
, "t");
419 read_state_file(&sb
, state
, "sign", 1);
420 state
->signoff
= !strcmp(sb
.buf
, "t");
422 read_state_file(&sb
, state
, "utf8", 1);
423 state
->utf8
= !strcmp(sb
.buf
, "t");
425 if (file_exists(am_path(state
, "rerere-autoupdate"))) {
426 read_state_file(&sb
, state
, "rerere-autoupdate", 1);
427 state
->allow_rerere_autoupdate
= strcmp(sb
.buf
, "t") ?
428 RERERE_NOAUTOUPDATE
: RERERE_AUTOUPDATE
;
430 state
->allow_rerere_autoupdate
= 0;
433 read_state_file(&sb
, state
, "keep", 1);
434 if (!strcmp(sb
.buf
, "t"))
435 state
->keep
= KEEP_TRUE
;
436 else if (!strcmp(sb
.buf
, "b"))
437 state
->keep
= KEEP_NON_PATCH
;
439 state
->keep
= KEEP_FALSE
;
441 read_state_file(&sb
, state
, "messageid", 1);
442 state
->message_id
= !strcmp(sb
.buf
, "t");
444 read_state_file(&sb
, state
, "scissors", 1);
445 if (!strcmp(sb
.buf
, "t"))
446 state
->scissors
= SCISSORS_TRUE
;
447 else if (!strcmp(sb
.buf
, "f"))
448 state
->scissors
= SCISSORS_FALSE
;
450 state
->scissors
= SCISSORS_UNSET
;
452 read_state_file(&sb
, state
, "quoted-cr", 1);
454 state
->quoted_cr
= quoted_cr_unset
;
455 else if (mailinfo_parse_quoted_cr_action(sb
.buf
, &state
->quoted_cr
) != 0)
456 die(_("could not parse %s"), am_path(state
, "quoted-cr"));
458 read_state_file(&sb
, state
, "apply-opt", 1);
459 strvec_clear(&state
->git_apply_opts
);
460 if (sq_dequote_to_strvec(sb
.buf
, &state
->git_apply_opts
) < 0)
461 die(_("could not parse %s"), am_path(state
, "apply-opt"));
463 state
->rebasing
= !!file_exists(am_path(state
, "rebasing"));
469 * Removes the am_state directory, forcefully terminating the current am
472 static void am_destroy(const struct am_state
*state
)
474 struct strbuf sb
= STRBUF_INIT
;
476 strbuf_addstr(&sb
, state
->dir
);
477 remove_dir_recursively(&sb
, 0);
482 * Runs applypatch-msg hook. Returns its exit code.
484 static int run_applypatch_msg_hook(struct am_state
*state
)
490 if (!state
->no_verify
)
491 ret
= run_hooks_l("applypatch-msg", am_path(state
, "final-commit"), NULL
);
494 FREE_AND_NULL(state
->msg
);
495 if (read_commit_msg(state
) < 0)
496 die(_("'%s' was deleted by the applypatch-msg hook"),
497 am_path(state
, "final-commit"));
504 * Runs post-rewrite hook. Returns it exit code.
506 static int run_post_rewrite_hook(const struct am_state
*state
)
508 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
510 strvec_push(&opt
.args
, "rebase");
511 opt
.path_to_stdin
= am_path(state
, "rewritten");
513 return run_hooks_opt("post-rewrite", &opt
);
517 * Reads the state directory's "rewritten" file, and copies notes from the old
518 * commits listed in the file to their rewritten commits.
520 * Returns 0 on success, -1 on failure.
522 static int copy_notes_for_rebase(const struct am_state
*state
)
524 struct notes_rewrite_cfg
*c
;
525 struct strbuf sb
= STRBUF_INIT
;
526 const char *invalid_line
= _("Malformed input line: '%s'.");
527 const char *msg
= "Notes added by 'git rebase'";
531 assert(state
->rebasing
);
533 c
= init_copy_notes_for_rewrite("rebase");
537 fp
= xfopen(am_path(state
, "rewritten"), "r");
539 while (!strbuf_getline_lf(&sb
, fp
)) {
540 struct object_id from_obj
, to_obj
;
543 if (sb
.len
!= the_hash_algo
->hexsz
* 2 + 1) {
544 ret
= error(invalid_line
, sb
.buf
);
548 if (parse_oid_hex(sb
.buf
, &from_obj
, &p
)) {
549 ret
= error(invalid_line
, sb
.buf
);
554 ret
= error(invalid_line
, sb
.buf
);
558 if (get_oid_hex(p
+ 1, &to_obj
)) {
559 ret
= error(invalid_line
, sb
.buf
);
563 if (copy_note_for_rewrite(c
, &from_obj
, &to_obj
))
564 ret
= error(_("Failed to copy notes from '%s' to '%s'"),
565 oid_to_hex(&from_obj
), oid_to_hex(&to_obj
));
569 finish_copy_notes_for_rewrite(the_repository
, c
, msg
);
576 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
577 * non-indented lines and checking if they look like they begin with valid
578 * header field names.
580 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
582 static int is_mail(FILE *fp
)
584 const char *header_regex
= "^[!-9;-~]+:";
585 struct strbuf sb
= STRBUF_INIT
;
589 if (fseek(fp
, 0L, SEEK_SET
))
590 die_errno(_("fseek failed"));
592 if (regcomp(®ex
, header_regex
, REG_NOSUB
| REG_EXTENDED
))
593 die("invalid pattern: %s", header_regex
);
595 while (!strbuf_getline(&sb
, fp
)) {
597 break; /* End of header */
599 /* Ignore indented folded lines */
600 if (*sb
.buf
== '\t' || *sb
.buf
== ' ')
603 /* It's a header if it matches header_regex */
604 if (regexec(®ex
, sb
.buf
, 0, NULL
, 0)) {
617 * Attempts to detect the patch_format of the patches contained in `paths`,
618 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
621 static int detect_patch_format(const char **paths
)
623 enum patch_format ret
= PATCH_FORMAT_UNKNOWN
;
624 struct strbuf l1
= STRBUF_INIT
;
625 struct strbuf l2
= STRBUF_INIT
;
626 struct strbuf l3
= STRBUF_INIT
;
630 * We default to mbox format if input is from stdin and for directories
632 if (!*paths
|| !strcmp(*paths
, "-") || is_directory(*paths
))
633 return PATCH_FORMAT_MBOX
;
636 * Otherwise, check the first few lines of the first patch, starting
637 * from the first non-blank line, to try to detect its format.
640 fp
= xfopen(*paths
, "r");
642 while (!strbuf_getline(&l1
, fp
)) {
647 if (starts_with(l1
.buf
, "From ") || starts_with(l1
.buf
, "From: ")) {
648 ret
= PATCH_FORMAT_MBOX
;
652 if (starts_with(l1
.buf
, "# This series applies on GIT commit")) {
653 ret
= PATCH_FORMAT_STGIT_SERIES
;
657 if (!strcmp(l1
.buf
, "# HG changeset patch")) {
658 ret
= PATCH_FORMAT_HG
;
662 strbuf_getline(&l2
, fp
);
663 strbuf_getline(&l3
, fp
);
666 * If the second line is empty and the third is a From, Author or Date
667 * entry, this is likely an StGit patch.
669 if (l1
.len
&& !l2
.len
&&
670 (starts_with(l3
.buf
, "From:") ||
671 starts_with(l3
.buf
, "Author:") ||
672 starts_with(l3
.buf
, "Date:"))) {
673 ret
= PATCH_FORMAT_STGIT
;
677 if (l1
.len
&& is_mail(fp
)) {
678 ret
= PATCH_FORMAT_MBOX
;
691 * Splits out individual email patches from `paths`, where each path is either
692 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
694 static int split_mail_mbox(struct am_state
*state
, const char **paths
,
695 int keep_cr
, int mboxrd
)
697 struct child_process cp
= CHILD_PROCESS_INIT
;
698 struct strbuf last
= STRBUF_INIT
;
702 strvec_push(&cp
.args
, "mailsplit");
703 strvec_pushf(&cp
.args
, "-d%d", state
->prec
);
704 strvec_pushf(&cp
.args
, "-o%s", state
->dir
);
705 strvec_push(&cp
.args
, "-b");
707 strvec_push(&cp
.args
, "--keep-cr");
709 strvec_push(&cp
.args
, "--mboxrd");
710 strvec_push(&cp
.args
, "--");
711 strvec_pushv(&cp
.args
, paths
);
713 ret
= capture_command(&cp
, &last
, 8);
718 state
->last
= strtol(last
.buf
, NULL
, 10);
721 strbuf_release(&last
);
726 * Callback signature for split_mail_conv(). The foreign patch should be
727 * read from `in`, and the converted patch (in RFC2822 mail format) should be
728 * written to `out`. Return 0 on success, or -1 on failure.
730 typedef int (*mail_conv_fn
)(FILE *out
, FILE *in
, int keep_cr
);
733 * Calls `fn` for each file in `paths` to convert the foreign patch to the
734 * RFC2822 mail format suitable for parsing with git-mailinfo.
736 * Returns 0 on success, -1 on failure.
738 static int split_mail_conv(mail_conv_fn fn
, struct am_state
*state
,
739 const char **paths
, int keep_cr
)
741 static const char *stdin_only
[] = {"-", NULL
};
747 for (i
= 0; *paths
; paths
++, i
++) {
752 if (!strcmp(*paths
, "-"))
755 in
= fopen(*paths
, "r");
758 return error_errno(_("could not open '%s' for reading"),
761 mail
= mkpath("%s/%0*d", state
->dir
, state
->prec
, i
+ 1);
763 out
= fopen(mail
, "w");
767 return error_errno(_("could not open '%s' for writing"),
771 ret
= fn(out
, in
, keep_cr
);
778 return error(_("could not parse patch '%s'"), *paths
);
787 * A split_mail_conv() callback that converts an StGit patch to an RFC2822
788 * message suitable for parsing with git-mailinfo.
790 static int stgit_patch_to_mail(FILE *out
, FILE *in
, int keep_cr
)
792 struct strbuf sb
= STRBUF_INIT
;
793 int subject_printed
= 0;
795 while (!strbuf_getline_lf(&sb
, in
)) {
798 if (str_isspace(sb
.buf
))
800 else if (skip_prefix(sb
.buf
, "Author:", &str
))
801 fprintf(out
, "From:%s\n", str
);
802 else if (starts_with(sb
.buf
, "From") || starts_with(sb
.buf
, "Date"))
803 fprintf(out
, "%s\n", sb
.buf
);
804 else if (!subject_printed
) {
805 fprintf(out
, "Subject: %s\n", sb
.buf
);
808 fprintf(out
, "\n%s\n", sb
.buf
);
814 while (strbuf_fread(&sb
, 8192, in
) > 0) {
815 fwrite(sb
.buf
, 1, sb
.len
, out
);
824 * This function only supports a single StGit series file in `paths`.
826 * Given an StGit series file, converts the StGit patches in the series into
827 * RFC2822 messages suitable for parsing with git-mailinfo, and queues them in
828 * the state directory.
830 * Returns 0 on success, -1 on failure.
832 static int split_mail_stgit_series(struct am_state
*state
, const char **paths
,
835 const char *series_dir
;
836 char *series_dir_buf
;
838 struct strvec patches
= STRVEC_INIT
;
839 struct strbuf sb
= STRBUF_INIT
;
842 if (!paths
[0] || paths
[1])
843 return error(_("Only one StGIT patch series can be applied at once"));
845 series_dir_buf
= xstrdup(*paths
);
846 series_dir
= dirname(series_dir_buf
);
848 fp
= fopen(*paths
, "r");
850 return error_errno(_("could not open '%s' for reading"), *paths
);
852 while (!strbuf_getline_lf(&sb
, fp
)) {
854 continue; /* skip comment lines */
856 strvec_push(&patches
, mkpath("%s/%s", series_dir
, sb
.buf
));
861 free(series_dir_buf
);
863 ret
= split_mail_conv(stgit_patch_to_mail
, state
, patches
.v
, keep_cr
);
865 strvec_clear(&patches
);
870 * A split_patches_conv() callback that converts a mercurial patch to a RFC2822
871 * message suitable for parsing with git-mailinfo.
873 static int hg_patch_to_mail(FILE *out
, FILE *in
, int keep_cr
)
875 struct strbuf sb
= STRBUF_INIT
;
878 while (!strbuf_getline_lf(&sb
, in
)) {
881 if (skip_prefix(sb
.buf
, "# User ", &str
))
882 fprintf(out
, "From: %s\n", str
);
883 else if (skip_prefix(sb
.buf
, "# Date ", &str
)) {
884 timestamp_t timestamp
;
889 timestamp
= parse_timestamp(str
, &end
, 10);
891 rc
= error(_("invalid timestamp"));
895 if (!skip_prefix(end
, " ", &str
)) {
896 rc
= error(_("invalid Date line"));
901 tz
= strtol(str
, &end
, 10);
903 rc
= error(_("invalid timezone offset"));
908 rc
= error(_("invalid Date line"));
913 * mercurial's timezone is in seconds west of UTC,
914 * however git's timezone is in hours + minutes east of
917 tz2
= labs(tz
) / 3600 * 100 + labs(tz
) % 3600 / 60;
921 fprintf(out
, "Date: %s\n", show_date(timestamp
, tz2
, DATE_MODE(RFC2822
)));
922 } else if (starts_with(sb
.buf
, "# ")) {
925 fprintf(out
, "\n%s\n", sb
.buf
);
931 while (strbuf_fread(&sb
, 8192, in
) > 0) {
932 fwrite(sb
.buf
, 1, sb
.len
, out
);
941 * Splits a list of files/directories into individual email patches. Each path
942 * in `paths` must be a file/directory that is formatted according to
945 * Once split out, the individual email patches will be stored in the state
946 * directory, with each patch's filename being its index, padded to state->prec
949 * state->cur will be set to the index of the first mail, and state->last will
950 * be set to the index of the last mail.
952 * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1
953 * to disable this behavior, -1 to use the default configured setting.
955 * Returns 0 on success, -1 on failure.
957 static int split_mail(struct am_state
*state
, enum patch_format patch_format
,
958 const char **paths
, int keep_cr
)
962 git_config_get_bool("am.keepcr", &keep_cr
);
965 switch (patch_format
) {
966 case PATCH_FORMAT_MBOX
:
967 return split_mail_mbox(state
, paths
, keep_cr
, 0);
968 case PATCH_FORMAT_STGIT
:
969 return split_mail_conv(stgit_patch_to_mail
, state
, paths
, keep_cr
);
970 case PATCH_FORMAT_STGIT_SERIES
:
971 return split_mail_stgit_series(state
, paths
, keep_cr
);
972 case PATCH_FORMAT_HG
:
973 return split_mail_conv(hg_patch_to_mail
, state
, paths
, keep_cr
);
974 case PATCH_FORMAT_MBOXRD
:
975 return split_mail_mbox(state
, paths
, keep_cr
, 1);
977 BUG("invalid patch_format");
983 * Setup a new am session for applying patches
985 static void am_setup(struct am_state
*state
, enum patch_format patch_format
,
986 const char **paths
, int keep_cr
)
988 struct object_id curr_head
;
990 struct strbuf sb
= STRBUF_INIT
;
993 patch_format
= detect_patch_format(paths
);
996 fprintf_ln(stderr
, _("Patch format detection failed."));
1000 if (mkdir(state
->dir
, 0777) < 0 && errno
!= EEXIST
)
1001 die_errno(_("failed to create directory '%s'"), state
->dir
);
1002 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
1004 if (split_mail(state
, patch_format
, paths
, keep_cr
) < 0) {
1006 die(_("Failed to split patches."));
1009 if (state
->rebasing
)
1010 state
->threeway
= 1;
1012 write_state_bool(state
, "threeway", state
->threeway
);
1013 write_state_bool(state
, "quiet", state
->quiet
);
1014 write_state_bool(state
, "sign", state
->signoff
);
1015 write_state_bool(state
, "utf8", state
->utf8
);
1017 if (state
->allow_rerere_autoupdate
)
1018 write_state_bool(state
, "rerere-autoupdate",
1019 state
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
);
1021 switch (state
->keep
) {
1028 case KEEP_NON_PATCH
:
1032 BUG("invalid value for state->keep");
1035 write_state_text(state
, "keep", str
);
1036 write_state_bool(state
, "messageid", state
->message_id
);
1038 switch (state
->scissors
) {
1039 case SCISSORS_UNSET
:
1042 case SCISSORS_FALSE
:
1049 BUG("invalid value for state->scissors");
1051 write_state_text(state
, "scissors", str
);
1053 switch (state
->quoted_cr
) {
1054 case quoted_cr_unset
:
1057 case quoted_cr_nowarn
:
1060 case quoted_cr_warn
:
1063 case quoted_cr_strip
:
1067 BUG("invalid value for state->quoted_cr");
1069 write_state_text(state
, "quoted-cr", str
);
1071 sq_quote_argv(&sb
, state
->git_apply_opts
.v
);
1072 write_state_text(state
, "apply-opt", sb
.buf
);
1074 if (state
->rebasing
)
1075 write_state_text(state
, "rebasing", "");
1077 write_state_text(state
, "applying", "");
1079 if (!repo_get_oid(the_repository
, "HEAD", &curr_head
)) {
1080 write_state_text(state
, "abort-safety", oid_to_hex(&curr_head
));
1081 if (!state
->rebasing
)
1082 update_ref("am", "ORIG_HEAD", &curr_head
, NULL
, 0,
1083 UPDATE_REFS_DIE_ON_ERR
);
1085 write_state_text(state
, "abort-safety", "");
1086 if (!state
->rebasing
)
1087 delete_ref(NULL
, "ORIG_HEAD", NULL
, 0);
1091 * NOTE: Since the "next" and "last" files determine if an am_state
1092 * session is in progress, they should be written last.
1095 write_state_count(state
, "next", state
->cur
);
1096 write_state_count(state
, "last", state
->last
);
1098 strbuf_release(&sb
);
1102 * Increments the patch pointer, and cleans am_state for the application of the
1105 static void am_next(struct am_state
*state
)
1107 struct object_id head
;
1109 FREE_AND_NULL(state
->author_name
);
1110 FREE_AND_NULL(state
->author_email
);
1111 FREE_AND_NULL(state
->author_date
);
1112 FREE_AND_NULL(state
->msg
);
1115 unlink(am_path(state
, "author-script"));
1116 unlink(am_path(state
, "final-commit"));
1118 oidclr(&state
->orig_commit
);
1119 unlink(am_path(state
, "original-commit"));
1120 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
1122 if (!repo_get_oid(the_repository
, "HEAD", &head
))
1123 write_state_text(state
, "abort-safety", oid_to_hex(&head
));
1125 write_state_text(state
, "abort-safety", "");
1128 write_state_count(state
, "next", state
->cur
);
1132 * Returns the filename of the current patch email.
1134 static const char *msgnum(const struct am_state
*state
)
1136 static struct strbuf sb
= STRBUF_INIT
;
1139 strbuf_addf(&sb
, "%0*d", state
->prec
, state
->cur
);
1145 * Dies with a user-friendly message on how to proceed after resolving the
1146 * problem. This message can be overridden with state->resolvemsg.
1148 static void NORETURN
die_user_resolve(const struct am_state
*state
)
1150 if (state
->resolvemsg
) {
1151 printf_ln("%s", state
->resolvemsg
);
1153 const char *cmdline
= state
->interactive
? "git am -i" : "git am";
1155 printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline
);
1156 printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline
);
1158 if (advice_enabled(ADVICE_AM_WORK_DIR
) &&
1159 is_empty_or_missing_file(am_path(state
, "patch")) &&
1160 !repo_index_has_changes(the_repository
, NULL
, NULL
))
1161 printf_ln(_("To record the empty patch as an empty commit, run \"%s --allow-empty\"."), cmdline
);
1163 printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline
);
1170 * Appends signoff to the "msg" field of the am_state.
1172 static void am_append_signoff(struct am_state
*state
)
1174 struct strbuf sb
= STRBUF_INIT
;
1176 strbuf_attach(&sb
, state
->msg
, state
->msg_len
, state
->msg_len
);
1177 append_signoff(&sb
, 0, 0);
1178 state
->msg
= strbuf_detach(&sb
, &state
->msg_len
);
1182 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
1183 * state->msg will be set to the patch message. state->author_name,
1184 * state->author_email and state->author_date will be set to the patch author's
1185 * name, email and date respectively. The patch body will be written to the
1186 * state directory's "patch" file.
1188 * Returns 1 if the patch should be skipped, 0 otherwise.
1190 static int parse_mail(struct am_state
*state
, const char *mail
)
1193 struct strbuf sb
= STRBUF_INIT
;
1194 struct strbuf msg
= STRBUF_INIT
;
1195 struct strbuf author_name
= STRBUF_INIT
;
1196 struct strbuf author_date
= STRBUF_INIT
;
1197 struct strbuf author_email
= STRBUF_INIT
;
1201 setup_mailinfo(&mi
);
1204 mi
.metainfo_charset
= get_commit_output_encoding();
1206 mi
.metainfo_charset
= NULL
;
1208 switch (state
->keep
) {
1212 mi
.keep_subject
= 1;
1214 case KEEP_NON_PATCH
:
1215 mi
.keep_non_patch_brackets_in_subject
= 1;
1218 BUG("invalid value for state->keep");
1221 if (state
->message_id
)
1222 mi
.add_message_id
= 1;
1224 switch (state
->scissors
) {
1225 case SCISSORS_UNSET
:
1227 case SCISSORS_FALSE
:
1228 mi
.use_scissors
= 0;
1231 mi
.use_scissors
= 1;
1234 BUG("invalid value for state->scissors");
1237 switch (state
->quoted_cr
) {
1238 case quoted_cr_unset
:
1240 case quoted_cr_nowarn
:
1241 case quoted_cr_warn
:
1242 case quoted_cr_strip
:
1243 mi
.quoted_cr
= state
->quoted_cr
;
1246 BUG("invalid value for state->quoted_cr");
1249 mi
.input
= xfopen(mail
, "r");
1250 mi
.output
= xfopen(am_path(state
, "info"), "w");
1251 if (mailinfo(&mi
, am_path(state
, "msg"), am_path(state
, "patch")))
1252 die("could not parse patch");
1257 if (mi
.format_flowed
)
1258 warning(_("Patch sent with format=flowed; "
1259 "space at the end of lines might be lost."));
1261 /* Extract message and author information */
1262 fp
= xfopen(am_path(state
, "info"), "r");
1263 while (!strbuf_getline_lf(&sb
, fp
)) {
1266 if (skip_prefix(sb
.buf
, "Subject: ", &x
)) {
1268 strbuf_addch(&msg
, '\n');
1269 strbuf_addstr(&msg
, x
);
1270 } else if (skip_prefix(sb
.buf
, "Author: ", &x
))
1271 strbuf_addstr(&author_name
, x
);
1272 else if (skip_prefix(sb
.buf
, "Email: ", &x
))
1273 strbuf_addstr(&author_email
, x
);
1274 else if (skip_prefix(sb
.buf
, "Date: ", &x
))
1275 strbuf_addstr(&author_date
, x
);
1279 /* Skip pine's internal folder data */
1280 if (!strcmp(author_name
.buf
, "Mail System Internal Data")) {
1285 strbuf_addstr(&msg
, "\n\n");
1286 strbuf_addbuf(&msg
, &mi
.log_message
);
1287 strbuf_stripspace(&msg
, '\0');
1289 assert(!state
->author_name
);
1290 state
->author_name
= strbuf_detach(&author_name
, NULL
);
1292 assert(!state
->author_email
);
1293 state
->author_email
= strbuf_detach(&author_email
, NULL
);
1295 assert(!state
->author_date
);
1296 state
->author_date
= strbuf_detach(&author_date
, NULL
);
1298 assert(!state
->msg
);
1299 state
->msg
= strbuf_detach(&msg
, &state
->msg_len
);
1302 strbuf_release(&msg
);
1303 strbuf_release(&author_date
);
1304 strbuf_release(&author_email
);
1305 strbuf_release(&author_name
);
1306 strbuf_release(&sb
);
1307 clear_mailinfo(&mi
);
1312 * Sets commit_id to the commit hash where the mail was generated from.
1313 * Returns 0 on success, -1 on failure.
1315 static int get_mail_commit_oid(struct object_id
*commit_id
, const char *mail
)
1317 struct strbuf sb
= STRBUF_INIT
;
1318 FILE *fp
= xfopen(mail
, "r");
1322 if (strbuf_getline_lf(&sb
, fp
) ||
1323 !skip_prefix(sb
.buf
, "From ", &x
) ||
1324 get_oid_hex(x
, commit_id
) < 0)
1327 strbuf_release(&sb
);
1333 * Sets state->msg, state->author_name, state->author_email, state->author_date
1334 * to the commit's respective info.
1336 static void get_commit_info(struct am_state
*state
, struct commit
*commit
)
1338 const char *buffer
, *ident_line
, *msg
;
1340 struct ident_split id
;
1342 buffer
= repo_logmsg_reencode(the_repository
, commit
, NULL
,
1343 get_commit_output_encoding());
1345 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
1347 die(_("missing author line in commit %s"),
1348 oid_to_hex(&commit
->object
.oid
));
1349 if (split_ident_line(&id
, ident_line
, ident_len
) < 0)
1350 die(_("invalid ident line: %.*s"), (int)ident_len
, ident_line
);
1352 assert(!state
->author_name
);
1354 state
->author_name
=
1355 xmemdupz(id
.name_begin
, id
.name_end
- id
.name_begin
);
1357 state
->author_name
= xstrdup("");
1359 assert(!state
->author_email
);
1361 state
->author_email
=
1362 xmemdupz(id
.mail_begin
, id
.mail_end
- id
.mail_begin
);
1364 state
->author_email
= xstrdup("");
1366 assert(!state
->author_date
);
1367 state
->author_date
= xstrdup(show_ident_date(&id
, DATE_MODE(NORMAL
)));
1369 assert(!state
->msg
);
1370 msg
= strstr(buffer
, "\n\n");
1372 die(_("unable to parse commit %s"), oid_to_hex(&commit
->object
.oid
));
1373 state
->msg
= xstrdup(msg
+ 2);
1374 state
->msg_len
= strlen(state
->msg
);
1375 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
1379 * Writes `commit` as a patch to the state directory's "patch" file.
1381 static void write_commit_patch(const struct am_state
*state
, struct commit
*commit
)
1383 struct rev_info rev_info
;
1386 fp
= xfopen(am_path(state
, "patch"), "w");
1387 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1389 rev_info
.abbrev
= 0;
1390 rev_info
.disable_stdin
= 1;
1391 rev_info
.show_root_diff
= 1;
1392 rev_info
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1393 rev_info
.no_commit_id
= 1;
1394 rev_info
.diffopt
.flags
.binary
= 1;
1395 rev_info
.diffopt
.flags
.full_index
= 1;
1396 rev_info
.diffopt
.use_color
= 0;
1397 rev_info
.diffopt
.file
= fp
;
1398 rev_info
.diffopt
.close_file
= 1;
1399 add_pending_object(&rev_info
, &commit
->object
, "");
1400 diff_setup_done(&rev_info
.diffopt
);
1401 log_tree_commit(&rev_info
, commit
);
1402 release_revisions(&rev_info
);
1406 * Writes the diff of the index against HEAD as a patch to the state
1407 * directory's "patch" file.
1409 static void write_index_patch(const struct am_state
*state
)
1412 struct object_id head
;
1413 struct rev_info rev_info
;
1416 if (!repo_get_oid(the_repository
, "HEAD", &head
)) {
1417 struct commit
*commit
= lookup_commit_or_die(&head
, "HEAD");
1418 tree
= repo_get_commit_tree(the_repository
, commit
);
1420 tree
= lookup_tree(the_repository
,
1421 the_repository
->hash_algo
->empty_tree
);
1423 fp
= xfopen(am_path(state
, "patch"), "w");
1424 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1426 rev_info
.disable_stdin
= 1;
1427 rev_info
.no_commit_id
= 1;
1428 rev_info
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1429 rev_info
.diffopt
.use_color
= 0;
1430 rev_info
.diffopt
.file
= fp
;
1431 rev_info
.diffopt
.close_file
= 1;
1432 add_pending_object(&rev_info
, &tree
->object
, "");
1433 diff_setup_done(&rev_info
.diffopt
);
1434 run_diff_index(&rev_info
, 1);
1435 release_revisions(&rev_info
);
1439 * Like parse_mail(), but parses the mail by looking up its commit ID
1440 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
1443 * state->orig_commit will be set to the original commit ID.
1445 * Will always return 0 as the patch should never be skipped.
1447 static int parse_mail_rebase(struct am_state
*state
, const char *mail
)
1449 struct commit
*commit
;
1450 struct object_id commit_oid
;
1452 if (get_mail_commit_oid(&commit_oid
, mail
) < 0)
1453 die(_("could not parse %s"), mail
);
1455 commit
= lookup_commit_or_die(&commit_oid
, mail
);
1457 get_commit_info(state
, commit
);
1459 write_commit_patch(state
, commit
);
1461 oidcpy(&state
->orig_commit
, &commit_oid
);
1462 write_state_text(state
, "original-commit", oid_to_hex(&commit_oid
));
1463 update_ref("am", "REBASE_HEAD", &commit_oid
,
1464 NULL
, REF_NO_DEREF
, UPDATE_REFS_DIE_ON_ERR
);
1470 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
1471 * `index_file` is not NULL, the patch will be applied to that index.
1473 static int run_apply(const struct am_state
*state
, const char *index_file
)
1475 struct strvec apply_paths
= STRVEC_INIT
;
1476 struct strvec apply_opts
= STRVEC_INIT
;
1477 struct apply_state apply_state
;
1479 int force_apply
= 0;
1481 const char **apply_argv
;
1483 if (init_apply_state(&apply_state
, the_repository
, NULL
))
1484 BUG("init_apply_state() failed");
1486 strvec_push(&apply_opts
, "apply");
1487 strvec_pushv(&apply_opts
, state
->git_apply_opts
.v
);
1490 * Build a copy that apply_parse_options() can rearrange.
1491 * apply_opts.v keeps referencing the allocated strings for
1492 * strvec_clear() to release.
1494 DUP_ARRAY(apply_argv
, apply_opts
.v
, apply_opts
.nr
);
1496 opts_left
= apply_parse_options(apply_opts
.nr
, apply_argv
,
1497 &apply_state
, &force_apply
, &options
,
1501 die("unknown option passed through to git apply");
1504 apply_state
.index_file
= index_file
;
1505 apply_state
.cached
= 1;
1507 apply_state
.check_index
= 1;
1510 * If we are allowed to fall back on 3-way merge, don't give false
1511 * errors during the initial attempt.
1513 if (state
->threeway
&& !index_file
)
1514 apply_state
.apply_verbosity
= verbosity_silent
;
1516 if (check_apply_state(&apply_state
, force_apply
))
1517 BUG("check_apply_state() failed");
1519 strvec_push(&apply_paths
, am_path(state
, "patch"));
1521 res
= apply_all_patches(&apply_state
, apply_paths
.nr
, apply_paths
.v
, options
);
1523 strvec_clear(&apply_paths
);
1524 strvec_clear(&apply_opts
);
1525 clear_apply_state(&apply_state
);
1532 /* Reload index as apply_all_patches() will have modified it. */
1533 discard_index(&the_index
);
1534 read_index_from(&the_index
, index_file
, get_git_dir());
1541 * Builds an index that contains just the blobs needed for a 3way merge.
1543 static int build_fake_ancestor(const struct am_state
*state
, const char *index_file
)
1545 struct child_process cp
= CHILD_PROCESS_INIT
;
1548 strvec_push(&cp
.args
, "apply");
1549 strvec_pushv(&cp
.args
, state
->git_apply_opts
.v
);
1550 strvec_pushf(&cp
.args
, "--build-fake-ancestor=%s", index_file
);
1551 strvec_push(&cp
.args
, am_path(state
, "patch"));
1553 if (run_command(&cp
))
1560 * Attempt a threeway merge, using index_path as the temporary index.
1562 static int fall_back_threeway(const struct am_state
*state
, const char *index_path
)
1564 struct object_id orig_tree
, their_tree
, our_tree
;
1565 const struct object_id
*bases
[1] = { &orig_tree
};
1566 struct merge_options o
;
1567 struct commit
*result
;
1568 char *their_tree_name
;
1570 if (repo_get_oid(the_repository
, "HEAD", &our_tree
) < 0)
1571 oidcpy(&our_tree
, the_hash_algo
->empty_tree
);
1573 if (build_fake_ancestor(state
, index_path
))
1574 return error("could not build fake ancestor");
1576 discard_index(&the_index
);
1577 read_index_from(&the_index
, index_path
, get_git_dir());
1579 if (write_index_as_tree(&orig_tree
, &the_index
, index_path
, 0, NULL
))
1580 return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1582 say(state
, stdout
, _("Using index info to reconstruct a base tree..."));
1584 if (!state
->quiet
) {
1586 * List paths that needed 3-way fallback, so that the user can
1587 * review them with extra care to spot mismerges.
1589 struct rev_info rev_info
;
1591 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1592 rev_info
.diffopt
.output_format
= DIFF_FORMAT_NAME_STATUS
;
1593 rev_info
.diffopt
.filter
|= diff_filter_bit('A');
1594 rev_info
.diffopt
.filter
|= diff_filter_bit('M');
1595 add_pending_oid(&rev_info
, "HEAD", &our_tree
, 0);
1596 diff_setup_done(&rev_info
.diffopt
);
1597 run_diff_index(&rev_info
, 1);
1598 release_revisions(&rev_info
);
1601 if (run_apply(state
, index_path
))
1602 return error(_("Did you hand edit your patch?\n"
1603 "It does not apply to blobs recorded in its index."));
1605 if (write_index_as_tree(&their_tree
, &the_index
, index_path
, 0, NULL
))
1606 return error("could not write tree");
1608 say(state
, stdout
, _("Falling back to patching base and 3-way merge..."));
1610 discard_index(&the_index
);
1611 repo_read_index(the_repository
);
1614 * This is not so wrong. Depending on which base we picked, orig_tree
1615 * may be wildly different from ours, but their_tree has the same set of
1616 * wildly different changes in parts the patch did not touch, so
1617 * recursive ends up canceling them, saying that we reverted all those
1621 init_merge_options(&o
, the_repository
);
1624 their_tree_name
= xstrfmt("%.*s", linelen(state
->msg
), state
->msg
);
1625 o
.branch2
= their_tree_name
;
1626 o
.detect_directory_renames
= MERGE_DIRECTORY_RENAMES_NONE
;
1631 if (merge_recursive_generic(&o
, &our_tree
, &their_tree
, 1, bases
, &result
)) {
1632 repo_rerere(the_repository
, state
->allow_rerere_autoupdate
);
1633 free(their_tree_name
);
1634 return error(_("Failed to merge in the changes."));
1637 free(their_tree_name
);
1642 * Commits the current index with state->msg as the commit message and
1643 * state->author_name, state->author_email and state->author_date as the author
1646 static void do_commit(const struct am_state
*state
)
1648 struct object_id tree
, parent
, commit
;
1649 const struct object_id
*old_oid
;
1650 struct commit_list
*parents
= NULL
;
1651 const char *reflog_msg
, *author
, *committer
= NULL
;
1652 struct strbuf sb
= STRBUF_INIT
;
1654 if (!state
->no_verify
&& run_hooks("pre-applypatch"))
1657 if (write_index_as_tree(&tree
, &the_index
, get_index_file(), 0, NULL
))
1658 die(_("git write-tree failed to write a tree"));
1660 if (!repo_get_oid_commit(the_repository
, "HEAD", &parent
)) {
1662 commit_list_insert(lookup_commit(the_repository
, &parent
),
1666 say(state
, stderr
, _("applying to an empty history"));
1669 author
= fmt_ident(state
->author_name
, state
->author_email
,
1671 state
->ignore_date
? NULL
: state
->author_date
,
1674 if (state
->committer_date_is_author_date
)
1675 committer
= fmt_ident(getenv("GIT_COMMITTER_NAME"),
1676 getenv("GIT_COMMITTER_EMAIL"),
1677 WANT_COMMITTER_IDENT
,
1678 state
->ignore_date
? NULL
1679 : state
->author_date
,
1682 if (commit_tree_extended(state
->msg
, state
->msg_len
, &tree
, parents
,
1683 &commit
, author
, committer
, state
->sign_commit
,
1685 die(_("failed to write commit object"));
1687 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1691 strbuf_addf(&sb
, "%s: %.*s", reflog_msg
, linelen(state
->msg
),
1694 update_ref(sb
.buf
, "HEAD", &commit
, old_oid
, 0,
1695 UPDATE_REFS_DIE_ON_ERR
);
1697 if (state
->rebasing
) {
1698 FILE *fp
= xfopen(am_path(state
, "rewritten"), "a");
1700 assert(!is_null_oid(&state
->orig_commit
));
1701 fprintf(fp
, "%s ", oid_to_hex(&state
->orig_commit
));
1702 fprintf(fp
, "%s\n", oid_to_hex(&commit
));
1706 run_hooks("post-applypatch");
1708 strbuf_release(&sb
);
1712 * Validates the am_state for resuming -- the "msg" and authorship fields must
1715 static void validate_resume_state(const struct am_state
*state
)
1718 die(_("cannot resume: %s does not exist."),
1719 am_path(state
, "final-commit"));
1721 if (!state
->author_name
|| !state
->author_email
|| !state
->author_date
)
1722 die(_("cannot resume: %s does not exist."),
1723 am_path(state
, "author-script"));
1727 * Interactively prompt the user on whether the current patch should be
1730 * Returns 0 if the user chooses to apply the patch, 1 if the user chooses to
1733 static int do_interactive(struct am_state
*state
)
1740 puts(_("Commit Body is:"));
1741 puts("--------------------------");
1742 printf("%s", state
->msg
);
1743 puts("--------------------------");
1746 * TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
1747 * in your translation. The program will only accept English
1748 * input at this point.
1750 printf(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "));
1751 if (!fgets(reply
, sizeof(reply
), stdin
))
1752 die("unable to read from stdin; aborting");
1754 if (*reply
== 'y' || *reply
== 'Y') {
1756 } else if (*reply
== 'a' || *reply
== 'A') {
1757 state
->interactive
= 0;
1759 } else if (*reply
== 'n' || *reply
== 'N') {
1761 } else if (*reply
== 'e' || *reply
== 'E') {
1762 struct strbuf msg
= STRBUF_INIT
;
1764 if (!launch_editor(am_path(state
, "final-commit"), &msg
, NULL
)) {
1766 state
->msg
= strbuf_detach(&msg
, &state
->msg_len
);
1768 strbuf_release(&msg
);
1769 } else if (*reply
== 'v' || *reply
== 'V') {
1770 const char *pager
= git_pager(1);
1771 struct child_process cp
= CHILD_PROCESS_INIT
;
1775 prepare_pager_args(&cp
, pager
);
1776 strvec_push(&cp
.args
, am_path(state
, "patch"));
1783 * Applies all queued mail.
1785 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
1786 * well as the state directory's "patch" file is used as-is for applying the
1787 * patch and committing it.
1789 static void am_run(struct am_state
*state
, int resume
)
1791 struct strbuf sb
= STRBUF_INIT
;
1793 unlink(am_path(state
, "dirtyindex"));
1795 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
, 0, 0,
1796 NULL
, NULL
, NULL
) < 0)
1797 die(_("unable to write index file"));
1799 if (repo_index_has_changes(the_repository
, NULL
, &sb
)) {
1800 write_state_bool(state
, "dirtyindex", 1);
1801 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb
.buf
);
1804 strbuf_release(&sb
);
1806 while (state
->cur
<= state
->last
) {
1807 const char *mail
= am_path(state
, msgnum(state
));
1813 if (!file_exists(mail
))
1817 validate_resume_state(state
);
1821 if (state
->rebasing
)
1822 skip
= parse_mail_rebase(state
, mail
);
1824 skip
= parse_mail(state
, mail
);
1827 goto next
; /* mail should be skipped */
1830 am_append_signoff(state
);
1832 write_author_script(state
);
1833 write_commit_msg(state
);
1836 if (state
->interactive
&& do_interactive(state
))
1840 if (is_empty_or_missing_file(am_path(state
, "patch"))) {
1841 switch (state
->empty_type
) {
1842 case DROP_EMPTY_COMMIT
:
1843 say(state
, stdout
, _("Skipping: %.*s"), linelen(state
->msg
), state
->msg
);
1846 case KEEP_EMPTY_COMMIT
:
1848 say(state
, stdout
, _("Creating an empty commit: %.*s"),
1849 linelen(state
->msg
), state
->msg
);
1851 case STOP_ON_EMPTY_COMMIT
:
1852 printf_ln(_("Patch is empty."));
1853 die_user_resolve(state
);
1858 if (run_applypatch_msg_hook(state
))
1863 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1865 apply_status
= run_apply(state
, NULL
);
1867 if (apply_status
&& state
->threeway
) {
1868 struct strbuf sb
= STRBUF_INIT
;
1870 strbuf_addstr(&sb
, am_path(state
, "patch-merge-index"));
1871 apply_status
= fall_back_threeway(state
, sb
.buf
);
1872 strbuf_release(&sb
);
1875 * Applying the patch to an earlier tree and merging
1876 * the result may have produced the same tree as ours.
1878 if (!apply_status
&&
1879 !repo_index_has_changes(the_repository
, NULL
, NULL
)) {
1880 say(state
, stdout
, _("No changes -- Patch already applied."));
1886 printf_ln(_("Patch failed at %s %.*s"), msgnum(state
),
1887 linelen(state
->msg
), state
->msg
);
1889 if (advice_enabled(ADVICE_AM_WORK_DIR
))
1890 advise(_("Use 'git am --show-current-patch=diff' to see the failed patch"));
1892 die_user_resolve(state
);
1906 if (!is_empty_or_missing_file(am_path(state
, "rewritten"))) {
1907 assert(state
->rebasing
);
1908 copy_notes_for_rebase(state
);
1909 run_post_rewrite_hook(state
);
1913 * In rebasing mode, it's up to the caller to take care of
1916 if (!state
->rebasing
) {
1918 run_auto_maintenance(state
->quiet
);
1923 * Resume the current am session after patch application failure. The user did
1924 * all the hard work, and we do not have to do any patch application. Just
1925 * trust and commit what the user has in the index and working tree. If `allow_empty`
1926 * is true, commit as an empty commit when index has not changed and lacking a patch.
1928 static void am_resolve(struct am_state
*state
, int allow_empty
)
1930 validate_resume_state(state
);
1932 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1934 if (!repo_index_has_changes(the_repository
, NULL
, NULL
)) {
1935 if (allow_empty
&& is_empty_or_missing_file(am_path(state
, "patch"))) {
1936 printf_ln(_("No changes - recorded it as an empty commit."));
1938 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1939 "If there is nothing left to stage, chances are that something else\n"
1940 "already introduced the same changes; you might want to skip this patch."));
1941 die_user_resolve(state
);
1945 if (unmerged_index(&the_index
)) {
1946 printf_ln(_("You still have unmerged paths in your index.\n"
1947 "You should 'git add' each file with resolved conflicts to mark them as such.\n"
1948 "You might run `git rm` on a file to accept \"deleted by them\" for it."));
1949 die_user_resolve(state
);
1952 if (state
->interactive
) {
1953 write_index_patch(state
);
1954 if (do_interactive(state
))
1958 repo_rerere(the_repository
, 0);
1969 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1970 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1973 static int fast_forward_to(struct tree
*head
, struct tree
*remote
, int reset
)
1975 struct lock_file lock_file
= LOCK_INIT
;
1976 struct unpack_trees_options opts
;
1977 struct tree_desc t
[2];
1979 if (parse_tree(head
) || parse_tree(remote
))
1982 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
1984 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
1986 memset(&opts
, 0, sizeof(opts
));
1988 opts
.src_index
= &the_index
;
1989 opts
.dst_index
= &the_index
;
1992 opts
.reset
= reset
? UNPACK_RESET_PROTECT_UNTRACKED
: 0;
1993 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
1994 opts
.fn
= twoway_merge
;
1995 init_tree_desc(&t
[0], head
->buffer
, head
->size
);
1996 init_tree_desc(&t
[1], remote
->buffer
, remote
->size
);
1998 if (unpack_trees(2, t
, &opts
)) {
1999 rollback_lock_file(&lock_file
);
2003 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
2004 die(_("unable to write new index file"));
2010 * Merges a tree into the index. The index's stat info will take precedence
2011 * over the merged tree's. Returns 0 on success, -1 on failure.
2013 static int merge_tree(struct tree
*tree
)
2015 struct lock_file lock_file
= LOCK_INIT
;
2016 struct unpack_trees_options opts
;
2017 struct tree_desc t
[1];
2019 if (parse_tree(tree
))
2022 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
2024 memset(&opts
, 0, sizeof(opts
));
2026 opts
.src_index
= &the_index
;
2027 opts
.dst_index
= &the_index
;
2029 opts
.fn
= oneway_merge
;
2030 init_tree_desc(&t
[0], tree
->buffer
, tree
->size
);
2032 if (unpack_trees(1, t
, &opts
)) {
2033 rollback_lock_file(&lock_file
);
2037 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
2038 die(_("unable to write new index file"));
2044 * Clean the index without touching entries that are not modified between
2045 * `head` and `remote`.
2047 static int clean_index(const struct object_id
*head
, const struct object_id
*remote
)
2049 struct tree
*head_tree
, *remote_tree
, *index_tree
;
2050 struct object_id index
;
2052 head_tree
= parse_tree_indirect(head
);
2054 return error(_("Could not parse object '%s'."), oid_to_hex(head
));
2056 remote_tree
= parse_tree_indirect(remote
);
2058 return error(_("Could not parse object '%s'."), oid_to_hex(remote
));
2060 repo_read_index_unmerged(the_repository
);
2062 if (fast_forward_to(head_tree
, head_tree
, 1))
2065 if (write_index_as_tree(&index
, &the_index
, get_index_file(), 0, NULL
))
2068 index_tree
= parse_tree_indirect(&index
);
2070 return error(_("Could not parse object '%s'."), oid_to_hex(&index
));
2072 if (fast_forward_to(index_tree
, remote_tree
, 0))
2075 if (merge_tree(remote_tree
))
2078 remove_branch_state(the_repository
, 0);
2084 * Resets rerere's merge resolution metadata.
2086 static void am_rerere_clear(void)
2088 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
2089 rerere_clear(the_repository
, &merge_rr
);
2090 string_list_clear(&merge_rr
, 1);
2094 * Resume the current am session by skipping the current patch.
2096 static void am_skip(struct am_state
*state
)
2098 struct object_id head
;
2102 if (repo_get_oid(the_repository
, "HEAD", &head
))
2103 oidcpy(&head
, the_hash_algo
->empty_tree
);
2105 if (clean_index(&head
, &head
))
2106 die(_("failed to clean index"));
2108 if (state
->rebasing
) {
2109 FILE *fp
= xfopen(am_path(state
, "rewritten"), "a");
2111 assert(!is_null_oid(&state
->orig_commit
));
2112 fprintf(fp
, "%s ", oid_to_hex(&state
->orig_commit
));
2113 fprintf(fp
, "%s\n", oid_to_hex(&head
));
2123 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
2125 * It is not safe to reset HEAD when:
2126 * 1. git-am previously failed because the index was dirty.
2127 * 2. HEAD has moved since git-am previously failed.
2129 static int safe_to_abort(const struct am_state
*state
)
2131 struct strbuf sb
= STRBUF_INIT
;
2132 struct object_id abort_safety
, head
;
2134 if (file_exists(am_path(state
, "dirtyindex")))
2137 if (read_state_file(&sb
, state
, "abort-safety", 1) > 0) {
2138 if (get_oid_hex(sb
.buf
, &abort_safety
))
2139 die(_("could not parse %s"), am_path(state
, "abort-safety"));
2141 oidclr(&abort_safety
);
2142 strbuf_release(&sb
);
2144 if (repo_get_oid(the_repository
, "HEAD", &head
))
2147 if (oideq(&head
, &abort_safety
))
2150 warning(_("You seem to have moved HEAD since the last 'am' failure.\n"
2151 "Not rewinding to ORIG_HEAD"));
2157 * Aborts the current am session if it is safe to do so.
2159 static void am_abort(struct am_state
*state
)
2161 struct object_id curr_head
, orig_head
;
2162 int has_curr_head
, has_orig_head
;
2165 if (!safe_to_abort(state
)) {
2172 curr_branch
= resolve_refdup("HEAD", 0, &curr_head
, NULL
);
2173 has_curr_head
= curr_branch
&& !is_null_oid(&curr_head
);
2175 oidcpy(&curr_head
, the_hash_algo
->empty_tree
);
2177 has_orig_head
= !repo_get_oid(the_repository
, "ORIG_HEAD", &orig_head
);
2179 oidcpy(&orig_head
, the_hash_algo
->empty_tree
);
2181 if (clean_index(&curr_head
, &orig_head
))
2182 die(_("failed to clean index"));
2185 update_ref("am --abort", "HEAD", &orig_head
,
2186 has_curr_head
? &curr_head
: NULL
, 0,
2187 UPDATE_REFS_DIE_ON_ERR
);
2188 else if (curr_branch
)
2189 delete_ref(NULL
, curr_branch
, NULL
, REF_NO_DEREF
);
2195 static int show_patch(struct am_state
*state
, enum show_patch_type sub_mode
)
2197 struct strbuf sb
= STRBUF_INIT
;
2198 const char *patch_path
;
2201 if (!is_null_oid(&state
->orig_commit
)) {
2202 struct child_process cmd
= CHILD_PROCESS_INIT
;
2204 strvec_pushl(&cmd
.args
, "show", oid_to_hex(&state
->orig_commit
),
2207 return run_command(&cmd
);
2211 case SHOW_PATCH_RAW
:
2212 patch_path
= am_path(state
, msgnum(state
));
2214 case SHOW_PATCH_DIFF
:
2215 patch_path
= am_path(state
, "patch");
2218 BUG("invalid mode for --show-current-patch");
2221 len
= strbuf_read_file(&sb
, patch_path
, 0);
2223 die_errno(_("failed to read '%s'"), patch_path
);
2226 write_in_full(1, sb
.buf
, sb
.len
);
2227 strbuf_release(&sb
);
2232 * parse_options() callback that validates and sets opt->value to the
2233 * PATCH_FORMAT_* enum value corresponding to `arg`.
2235 static int parse_opt_patchformat(const struct option
*opt
, const char *arg
, int unset
)
2237 int *opt_value
= opt
->value
;
2240 *opt_value
= PATCH_FORMAT_UNKNOWN
;
2241 else if (!strcmp(arg
, "mbox"))
2242 *opt_value
= PATCH_FORMAT_MBOX
;
2243 else if (!strcmp(arg
, "stgit"))
2244 *opt_value
= PATCH_FORMAT_STGIT
;
2245 else if (!strcmp(arg
, "stgit-series"))
2246 *opt_value
= PATCH_FORMAT_STGIT_SERIES
;
2247 else if (!strcmp(arg
, "hg"))
2248 *opt_value
= PATCH_FORMAT_HG
;
2249 else if (!strcmp(arg
, "mboxrd"))
2250 *opt_value
= PATCH_FORMAT_MBOXRD
;
2252 * Please update $__git_patchformat in git-completion.bash
2253 * when you add new options
2256 return error(_("invalid value for '%s': '%s'"),
2257 "--patch-format", arg
);
2272 struct resume_mode
{
2273 enum resume_type mode
;
2274 enum show_patch_type sub_mode
;
2277 static int parse_opt_show_current_patch(const struct option
*opt
, const char *arg
, int unset
)
2279 int *opt_value
= opt
->value
;
2280 struct resume_mode
*resume
= container_of(opt_value
, struct resume_mode
, mode
);
2283 * Please update $__git_showcurrentpatch in git-completion.bash
2284 * when you add new options
2286 const char *valid_modes
[] = {
2287 [SHOW_PATCH_DIFF
] = "diff",
2288 [SHOW_PATCH_RAW
] = "raw"
2290 int new_value
= SHOW_PATCH_RAW
;
2292 BUG_ON_OPT_NEG(unset
);
2295 for (new_value
= 0; new_value
< ARRAY_SIZE(valid_modes
); new_value
++) {
2296 if (!strcmp(arg
, valid_modes
[new_value
]))
2299 if (new_value
>= ARRAY_SIZE(valid_modes
))
2300 return error(_("invalid value for '%s': '%s'"),
2301 "--show-current-patch", arg
);
2304 if (resume
->mode
== RESUME_SHOW_PATCH
&& new_value
!= resume
->sub_mode
)
2305 return error(_("options '%s=%s' and '%s=%s' "
2306 "cannot be used together"),
2307 "--show-current-patch", "--show-current-patch", arg
, valid_modes
[resume
->sub_mode
]);
2309 resume
->mode
= RESUME_SHOW_PATCH
;
2310 resume
->sub_mode
= new_value
;
2314 int cmd_am(int argc
, const char **argv
, const char *prefix
)
2316 struct am_state state
;
2319 int patch_format
= PATCH_FORMAT_UNKNOWN
;
2320 struct resume_mode resume
= { .mode
= RESUME_FALSE
};
2324 const char * const usage
[] = {
2325 N_("git am [<options>] [(<mbox> | <Maildir>)...]"),
2326 N_("git am [<options>] (--continue | --skip | --abort)"),
2330 struct option options
[] = {
2331 OPT_BOOL('i', "interactive", &state
.interactive
,
2332 N_("run interactively")),
2333 OPT_BOOL('n', "no-verify", &state
.no_verify
,
2334 N_("bypass pre-applypatch and applypatch-msg hooks")),
2335 OPT_HIDDEN_BOOL('b', "binary", &binary
,
2336 N_("historical option -- no-op")),
2337 OPT_BOOL('3', "3way", &state
.threeway
,
2338 N_("allow fall back on 3way merging if needed")),
2339 OPT__QUIET(&state
.quiet
, N_("be quiet")),
2340 OPT_SET_INT('s', "signoff", &state
.signoff
,
2341 N_("add a Signed-off-by trailer to the commit message"),
2343 OPT_BOOL('u', "utf8", &state
.utf8
,
2344 N_("recode into utf8 (default)")),
2345 OPT_SET_INT('k', "keep", &state
.keep
,
2346 N_("pass -k flag to git-mailinfo"), KEEP_TRUE
),
2347 OPT_SET_INT(0, "keep-non-patch", &state
.keep
,
2348 N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH
),
2349 OPT_BOOL('m', "message-id", &state
.message_id
,
2350 N_("pass -m flag to git-mailinfo")),
2351 OPT_SET_INT_F(0, "keep-cr", &keep_cr
,
2352 N_("pass --keep-cr flag to git-mailsplit for mbox format"),
2353 1, PARSE_OPT_NONEG
),
2354 OPT_SET_INT_F(0, "no-keep-cr", &keep_cr
,
2355 N_("do not pass --keep-cr flag to git-mailsplit independent of am.keepcr"),
2356 0, PARSE_OPT_NONEG
),
2357 OPT_BOOL('c', "scissors", &state
.scissors
,
2358 N_("strip everything before a scissors line")),
2359 OPT_CALLBACK_F(0, "quoted-cr", &state
.quoted_cr
, N_("action"),
2360 N_("pass it through git-mailinfo"),
2361 PARSE_OPT_NONEG
, am_option_parse_quoted_cr
),
2362 OPT_PASSTHRU_ARGV(0, "whitespace", &state
.git_apply_opts
, N_("action"),
2363 N_("pass it through git-apply"),
2365 OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state
.git_apply_opts
, NULL
,
2366 N_("pass it through git-apply"),
2368 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state
.git_apply_opts
, NULL
,
2369 N_("pass it through git-apply"),
2371 OPT_PASSTHRU_ARGV(0, "directory", &state
.git_apply_opts
, N_("root"),
2372 N_("pass it through git-apply"),
2374 OPT_PASSTHRU_ARGV(0, "exclude", &state
.git_apply_opts
, N_("path"),
2375 N_("pass it through git-apply"),
2377 OPT_PASSTHRU_ARGV(0, "include", &state
.git_apply_opts
, N_("path"),
2378 N_("pass it through git-apply"),
2380 OPT_PASSTHRU_ARGV('C', NULL
, &state
.git_apply_opts
, N_("n"),
2381 N_("pass it through git-apply"),
2383 OPT_PASSTHRU_ARGV('p', NULL
, &state
.git_apply_opts
, N_("num"),
2384 N_("pass it through git-apply"),
2386 OPT_CALLBACK(0, "patch-format", &patch_format
, N_("format"),
2387 N_("format the patch(es) are in"),
2388 parse_opt_patchformat
),
2389 OPT_PASSTHRU_ARGV(0, "reject", &state
.git_apply_opts
, NULL
,
2390 N_("pass it through git-apply"),
2392 OPT_STRING(0, "resolvemsg", &state
.resolvemsg
, NULL
,
2393 N_("override error message when patch failure occurs")),
2394 OPT_CMDMODE(0, "continue", &resume
.mode
,
2395 N_("continue applying patches after resolving a conflict"),
2397 OPT_CMDMODE('r', "resolved", &resume
.mode
,
2398 N_("synonyms for --continue"),
2400 OPT_CMDMODE(0, "skip", &resume
.mode
,
2401 N_("skip the current patch"),
2403 OPT_CMDMODE(0, "abort", &resume
.mode
,
2404 N_("restore the original branch and abort the patching operation"),
2406 OPT_CMDMODE(0, "quit", &resume
.mode
,
2407 N_("abort the patching operation but keep HEAD where it is"),
2409 { OPTION_CALLBACK
, 0, "show-current-patch", &resume
.mode
,
2411 N_("show the patch being applied"),
2412 PARSE_OPT_CMDMODE
| PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
| PARSE_OPT_LITERAL_ARGHELP
,
2413 parse_opt_show_current_patch
, RESUME_SHOW_PATCH
},
2414 OPT_CMDMODE(0, "allow-empty", &resume
.mode
,
2415 N_("record the empty patch as an empty commit"),
2416 RESUME_ALLOW_EMPTY
),
2417 OPT_BOOL(0, "committer-date-is-author-date",
2418 &state
.committer_date_is_author_date
,
2419 N_("lie about committer date")),
2420 OPT_BOOL(0, "ignore-date", &state
.ignore_date
,
2421 N_("use current timestamp for author date")),
2422 OPT_RERERE_AUTOUPDATE(&state
.allow_rerere_autoupdate
),
2423 { OPTION_STRING
, 'S', "gpg-sign", &state
.sign_commit
, N_("key-id"),
2424 N_("GPG-sign commits"),
2425 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
2426 OPT_CALLBACK_F(STOP_ON_EMPTY_COMMIT
, "empty", &state
.empty_type
, "{stop,drop,keep}",
2427 N_("how to handle empty patches"),
2428 PARSE_OPT_NONEG
, am_option_parse_empty
),
2429 OPT_HIDDEN_BOOL(0, "rebasing", &state
.rebasing
,
2430 N_("(internal use for git-rebase)")),
2434 if (argc
== 2 && !strcmp(argv
[1], "-h"))
2435 usage_with_options(usage
, options
);
2437 git_config(git_default_config
, NULL
);
2439 am_state_init(&state
);
2441 in_progress
= am_in_progress(&state
);
2445 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2448 fprintf_ln(stderr
, _("The -b/--binary option has been a no-op for long time, and\n"
2449 "it will be removed. Please do not use it anymore."));
2451 /* Ensure a valid committer ident can be constructed */
2452 git_committer_info(IDENT_STRICT
);
2454 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
2455 die(_("failed to read the index"));
2459 * Catch user error to feed us patches when there is a session
2462 * 1. mbox path(s) are provided on the command-line.
2463 * 2. stdin is not a tty: the user is trying to feed us a patch
2464 * from standard input. This is somewhat unreliable -- stdin
2465 * could be /dev/null for example and the caller did not
2466 * intend to feed us a patch but wanted to continue
2469 if (argc
|| (resume
.mode
== RESUME_FALSE
&& !isatty(0)))
2470 die(_("previous rebase directory %s still exists but mbox given."),
2473 if (resume
.mode
== RESUME_FALSE
)
2474 resume
.mode
= RESUME_APPLY
;
2476 if (state
.signoff
== SIGNOFF_EXPLICIT
)
2477 am_append_signoff(&state
);
2479 struct strvec paths
= STRVEC_INIT
;
2483 * Handle stray state directory in the independent-run case. In
2484 * the --rebasing case, it is up to the caller to take care of
2485 * stray directories.
2487 if (file_exists(state
.dir
) && !state
.rebasing
) {
2488 if (resume
.mode
== RESUME_ABORT
|| resume
.mode
== RESUME_QUIT
) {
2490 am_state_release(&state
);
2494 die(_("Stray %s directory found.\n"
2495 "Use \"git am --abort\" to remove it."),
2500 die(_("Resolve operation not in progress, we are not resuming."));
2502 for (i
= 0; i
< argc
; i
++) {
2503 if (is_absolute_path(argv
[i
]) || !prefix
)
2504 strvec_push(&paths
, argv
[i
]);
2506 strvec_push(&paths
, mkpath("%s/%s", prefix
, argv
[i
]));
2509 if (state
.interactive
&& !paths
.nr
)
2510 die(_("interactive mode requires patches on the command line"));
2512 am_setup(&state
, patch_format
, paths
.v
, keep_cr
);
2514 strvec_clear(&paths
);
2517 switch (resume
.mode
) {
2524 case RESUME_RESOLVED
:
2525 case RESUME_ALLOW_EMPTY
:
2526 am_resolve(&state
, resume
.mode
== RESUME_ALLOW_EMPTY
? 1 : 0);
2538 case RESUME_SHOW_PATCH
:
2539 ret
= show_patch(&state
, resume
.sub_mode
);
2542 BUG("invalid resume value");
2545 am_state_release(&state
);