4 * Based on git-am.sh by Junio C Hamano.
6 #define USE_THE_INDEX_VARIABLE
11 #include "environment.h"
15 #include "parse-options.h"
17 #include "run-command.h"
22 #include "cache-tree.h"
27 #include "unpack-trees.h"
29 #include "sequencer.h"
31 #include "merge-recursive.h"
33 #include "notes-utils.h"
38 #include "string-list.h"
40 #include "repository.h"
45 * Returns the length of the first line of msg.
47 static int linelen(const char *msg
)
49 return strchrnul(msg
, '\n') - msg
;
53 * Returns true if `str` consists of only whitespace, false otherwise.
55 static int str_isspace(const char *str
)
65 PATCH_FORMAT_UNKNOWN
= 0,
68 PATCH_FORMAT_STGIT_SERIES
,
75 KEEP_TRUE
, /* pass -k flag to git-mailinfo */
76 KEEP_NON_PATCH
/* pass -b flag to git-mailinfo */
81 SCISSORS_FALSE
= 0, /* pass --no-scissors to git-mailinfo */
82 SCISSORS_TRUE
/* pass --scissors to git-mailinfo */
88 SIGNOFF_EXPLICIT
/* --signoff was set on the command-line */
91 enum show_patch_type
{
97 STOP_ON_EMPTY_COMMIT
= 0, /* output errors and stop in the middle of an am session */
98 DROP_EMPTY_COMMIT
, /* skip with a notice message, unless "--quiet" has been passed */
99 KEEP_EMPTY_COMMIT
, /* keep recording as empty commits */
103 /* state directory path */
106 /* current and last patch numbers, 1-indexed */
110 /* commit metadata and message */
117 /* when --rebasing, records the original commit the patch came from */
118 struct object_id orig_commit
;
120 /* number of digits in patch filename */
123 /* various operating modes and command line options */
128 int signoff
; /* enum signoff_type */
130 int keep
; /* enum keep_type */
132 int scissors
; /* enum scissors_type */
133 int quoted_cr
; /* enum quoted_cr_action */
134 int empty_type
; /* enum empty_action */
135 struct strvec git_apply_opts
;
136 const char *resolvemsg
;
137 int committer_date_is_author_date
;
139 int allow_rerere_autoupdate
;
140 const char *sign_commit
;
145 * Initializes am_state with the default values.
147 static void am_state_init(struct am_state
*state
)
151 memset(state
, 0, sizeof(*state
));
153 state
->dir
= git_pathdup("rebase-apply");
157 git_config_get_bool("am.threeway", &state
->threeway
);
161 git_config_get_bool("am.messageid", &state
->message_id
);
163 state
->scissors
= SCISSORS_UNSET
;
164 state
->quoted_cr
= quoted_cr_unset
;
166 strvec_init(&state
->git_apply_opts
);
168 if (!git_config_get_bool("commit.gpgsign", &gpgsign
))
169 state
->sign_commit
= gpgsign
? "" : NULL
;
173 * Releases memory allocated by an am_state.
175 static void am_state_release(struct am_state
*state
)
178 free(state
->author_name
);
179 free(state
->author_email
);
180 free(state
->author_date
);
182 strvec_clear(&state
->git_apply_opts
);
185 static int am_option_parse_quoted_cr(const struct option
*opt
,
186 const char *arg
, int unset
)
188 BUG_ON_OPT_NEG(unset
);
190 if (mailinfo_parse_quoted_cr_action(arg
, opt
->value
) != 0)
191 return error(_("bad action '%s' for '%s'"), arg
, "--quoted-cr");
195 static int am_option_parse_empty(const struct option
*opt
,
196 const char *arg
, int unset
)
198 int *opt_value
= opt
->value
;
200 BUG_ON_OPT_NEG(unset
);
202 if (!strcmp(arg
, "stop"))
203 *opt_value
= STOP_ON_EMPTY_COMMIT
;
204 else if (!strcmp(arg
, "drop"))
205 *opt_value
= DROP_EMPTY_COMMIT
;
206 else if (!strcmp(arg
, "keep"))
207 *opt_value
= KEEP_EMPTY_COMMIT
;
209 return error(_("invalid value for '%s': '%s'"), "--empty", arg
);
215 * Returns path relative to the am_state directory.
217 static inline const char *am_path(const struct am_state
*state
, const char *path
)
219 return mkpath("%s/%s", state
->dir
, path
);
223 * For convenience to call write_file()
225 static void write_state_text(const struct am_state
*state
,
226 const char *name
, const char *string
)
228 write_file(am_path(state
, name
), "%s", string
);
231 static void write_state_count(const struct am_state
*state
,
232 const char *name
, int value
)
234 write_file(am_path(state
, name
), "%d", value
);
237 static void write_state_bool(const struct am_state
*state
,
238 const char *name
, int value
)
240 write_state_text(state
, name
, value
? "t" : "f");
244 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
247 __attribute__((format (printf
, 3, 4)))
248 static void say(const struct am_state
*state
, FILE *fp
, const char *fmt
, ...)
254 vfprintf(fp
, fmt
, ap
);
261 * Returns 1 if there is an am session in progress, 0 otherwise.
263 static int am_in_progress(const struct am_state
*state
)
267 if (lstat(state
->dir
, &st
) < 0 || !S_ISDIR(st
.st_mode
))
269 if (lstat(am_path(state
, "last"), &st
) || !S_ISREG(st
.st_mode
))
271 if (lstat(am_path(state
, "next"), &st
) || !S_ISREG(st
.st_mode
))
277 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
278 * number of bytes read on success, -1 if the file does not exist. If `trim` is
279 * set, trailing whitespace will be removed.
281 static int read_state_file(struct strbuf
*sb
, const struct am_state
*state
,
282 const char *file
, int trim
)
286 if (strbuf_read_file(sb
, am_path(state
, file
), 0) >= 0) {
296 die_errno(_("could not read '%s'"), am_path(state
, file
));
300 * Reads and parses the state directory's "author-script" file, and sets
301 * state->author_name, state->author_email and state->author_date accordingly.
302 * Returns 0 on success, -1 if the file could not be parsed.
304 * The author script is of the format:
306 * GIT_AUTHOR_NAME='$author_name'
307 * GIT_AUTHOR_EMAIL='$author_email'
308 * GIT_AUTHOR_DATE='$author_date'
310 * where $author_name, $author_email and $author_date are quoted. We are strict
311 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
312 * script, and thus if the file differs from what this function expects, it is
313 * better to bail out than to do something that the user does not expect.
315 static int read_am_author_script(struct am_state
*state
)
317 const char *filename
= am_path(state
, "author-script");
319 assert(!state
->author_name
);
320 assert(!state
->author_email
);
321 assert(!state
->author_date
);
323 return read_author_script(filename
, &state
->author_name
,
324 &state
->author_email
, &state
->author_date
, 1);
328 * Saves state->author_name, state->author_email and state->author_date in the
329 * state directory's "author-script" file.
331 static void write_author_script(const struct am_state
*state
)
333 struct strbuf sb
= STRBUF_INIT
;
335 strbuf_addstr(&sb
, "GIT_AUTHOR_NAME=");
336 sq_quote_buf(&sb
, state
->author_name
);
337 strbuf_addch(&sb
, '\n');
339 strbuf_addstr(&sb
, "GIT_AUTHOR_EMAIL=");
340 sq_quote_buf(&sb
, state
->author_email
);
341 strbuf_addch(&sb
, '\n');
343 strbuf_addstr(&sb
, "GIT_AUTHOR_DATE=");
344 sq_quote_buf(&sb
, state
->author_date
);
345 strbuf_addch(&sb
, '\n');
347 write_state_text(state
, "author-script", sb
.buf
);
353 * Reads the commit message from the state directory's "final-commit" file,
354 * setting state->msg to its contents and state->msg_len to the length of its
357 * Returns 0 on success, -1 if the file does not exist.
359 static int read_commit_msg(struct am_state
*state
)
361 struct strbuf sb
= STRBUF_INIT
;
365 if (read_state_file(&sb
, state
, "final-commit", 0) < 0) {
370 state
->msg
= strbuf_detach(&sb
, &state
->msg_len
);
375 * Saves state->msg in the state directory's "final-commit" file.
377 static void write_commit_msg(const struct am_state
*state
)
379 const char *filename
= am_path(state
, "final-commit");
380 write_file_buf(filename
, state
->msg
, state
->msg_len
);
384 * Loads state from disk.
386 static void am_load(struct am_state
*state
)
388 struct strbuf sb
= STRBUF_INIT
;
390 if (read_state_file(&sb
, state
, "next", 1) < 0)
391 BUG("state file 'next' does not exist");
392 state
->cur
= strtol(sb
.buf
, NULL
, 10);
394 if (read_state_file(&sb
, state
, "last", 1) < 0)
395 BUG("state file 'last' does not exist");
396 state
->last
= strtol(sb
.buf
, NULL
, 10);
398 if (read_am_author_script(state
) < 0)
399 die(_("could not parse author script"));
401 read_commit_msg(state
);
403 if (read_state_file(&sb
, state
, "original-commit", 1) < 0)
404 oidclr(&state
->orig_commit
);
405 else if (get_oid_hex(sb
.buf
, &state
->orig_commit
) < 0)
406 die(_("could not parse %s"), am_path(state
, "original-commit"));
408 read_state_file(&sb
, state
, "threeway", 1);
409 state
->threeway
= !strcmp(sb
.buf
, "t");
411 read_state_file(&sb
, state
, "quiet", 1);
412 state
->quiet
= !strcmp(sb
.buf
, "t");
414 read_state_file(&sb
, state
, "sign", 1);
415 state
->signoff
= !strcmp(sb
.buf
, "t");
417 read_state_file(&sb
, state
, "utf8", 1);
418 state
->utf8
= !strcmp(sb
.buf
, "t");
420 if (file_exists(am_path(state
, "rerere-autoupdate"))) {
421 read_state_file(&sb
, state
, "rerere-autoupdate", 1);
422 state
->allow_rerere_autoupdate
= strcmp(sb
.buf
, "t") ?
423 RERERE_NOAUTOUPDATE
: RERERE_AUTOUPDATE
;
425 state
->allow_rerere_autoupdate
= 0;
428 read_state_file(&sb
, state
, "keep", 1);
429 if (!strcmp(sb
.buf
, "t"))
430 state
->keep
= KEEP_TRUE
;
431 else if (!strcmp(sb
.buf
, "b"))
432 state
->keep
= KEEP_NON_PATCH
;
434 state
->keep
= KEEP_FALSE
;
436 read_state_file(&sb
, state
, "messageid", 1);
437 state
->message_id
= !strcmp(sb
.buf
, "t");
439 read_state_file(&sb
, state
, "scissors", 1);
440 if (!strcmp(sb
.buf
, "t"))
441 state
->scissors
= SCISSORS_TRUE
;
442 else if (!strcmp(sb
.buf
, "f"))
443 state
->scissors
= SCISSORS_FALSE
;
445 state
->scissors
= SCISSORS_UNSET
;
447 read_state_file(&sb
, state
, "quoted-cr", 1);
449 state
->quoted_cr
= quoted_cr_unset
;
450 else if (mailinfo_parse_quoted_cr_action(sb
.buf
, &state
->quoted_cr
) != 0)
451 die(_("could not parse %s"), am_path(state
, "quoted-cr"));
453 read_state_file(&sb
, state
, "apply-opt", 1);
454 strvec_clear(&state
->git_apply_opts
);
455 if (sq_dequote_to_strvec(sb
.buf
, &state
->git_apply_opts
) < 0)
456 die(_("could not parse %s"), am_path(state
, "apply-opt"));
458 state
->rebasing
= !!file_exists(am_path(state
, "rebasing"));
464 * Removes the am_state directory, forcefully terminating the current am
467 static void am_destroy(const struct am_state
*state
)
469 struct strbuf sb
= STRBUF_INIT
;
471 strbuf_addstr(&sb
, state
->dir
);
472 remove_dir_recursively(&sb
, 0);
477 * Runs applypatch-msg hook. Returns its exit code.
479 static int run_applypatch_msg_hook(struct am_state
*state
)
485 if (!state
->no_verify
)
486 ret
= run_hooks_l("applypatch-msg", am_path(state
, "final-commit"), NULL
);
489 FREE_AND_NULL(state
->msg
);
490 if (read_commit_msg(state
) < 0)
491 die(_("'%s' was deleted by the applypatch-msg hook"),
492 am_path(state
, "final-commit"));
499 * Runs post-rewrite hook. Returns it exit code.
501 static int run_post_rewrite_hook(const struct am_state
*state
)
503 struct run_hooks_opt opt
= RUN_HOOKS_OPT_INIT
;
505 strvec_push(&opt
.args
, "rebase");
506 opt
.path_to_stdin
= am_path(state
, "rewritten");
508 return run_hooks_opt("post-rewrite", &opt
);
512 * Reads the state directory's "rewritten" file, and copies notes from the old
513 * commits listed in the file to their rewritten commits.
515 * Returns 0 on success, -1 on failure.
517 static int copy_notes_for_rebase(const struct am_state
*state
)
519 struct notes_rewrite_cfg
*c
;
520 struct strbuf sb
= STRBUF_INIT
;
521 const char *invalid_line
= _("Malformed input line: '%s'.");
522 const char *msg
= "Notes added by 'git rebase'";
526 assert(state
->rebasing
);
528 c
= init_copy_notes_for_rewrite("rebase");
532 fp
= xfopen(am_path(state
, "rewritten"), "r");
534 while (!strbuf_getline_lf(&sb
, fp
)) {
535 struct object_id from_obj
, to_obj
;
538 if (sb
.len
!= the_hash_algo
->hexsz
* 2 + 1) {
539 ret
= error(invalid_line
, sb
.buf
);
543 if (parse_oid_hex(sb
.buf
, &from_obj
, &p
)) {
544 ret
= error(invalid_line
, sb
.buf
);
549 ret
= error(invalid_line
, sb
.buf
);
553 if (get_oid_hex(p
+ 1, &to_obj
)) {
554 ret
= error(invalid_line
, sb
.buf
);
558 if (copy_note_for_rewrite(c
, &from_obj
, &to_obj
))
559 ret
= error(_("Failed to copy notes from '%s' to '%s'"),
560 oid_to_hex(&from_obj
), oid_to_hex(&to_obj
));
564 finish_copy_notes_for_rewrite(the_repository
, c
, msg
);
571 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
572 * non-indented lines and checking if they look like they begin with valid
573 * header field names.
575 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
577 static int is_mail(FILE *fp
)
579 const char *header_regex
= "^[!-9;-~]+:";
580 struct strbuf sb
= STRBUF_INIT
;
584 if (fseek(fp
, 0L, SEEK_SET
))
585 die_errno(_("fseek failed"));
587 if (regcomp(®ex
, header_regex
, REG_NOSUB
| REG_EXTENDED
))
588 die("invalid pattern: %s", header_regex
);
590 while (!strbuf_getline(&sb
, fp
)) {
592 break; /* End of header */
594 /* Ignore indented folded lines */
595 if (*sb
.buf
== '\t' || *sb
.buf
== ' ')
598 /* It's a header if it matches header_regex */
599 if (regexec(®ex
, sb
.buf
, 0, NULL
, 0)) {
612 * Attempts to detect the patch_format of the patches contained in `paths`,
613 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
616 static int detect_patch_format(const char **paths
)
618 enum patch_format ret
= PATCH_FORMAT_UNKNOWN
;
619 struct strbuf l1
= STRBUF_INIT
;
620 struct strbuf l2
= STRBUF_INIT
;
621 struct strbuf l3
= STRBUF_INIT
;
625 * We default to mbox format if input is from stdin and for directories
627 if (!*paths
|| !strcmp(*paths
, "-") || is_directory(*paths
))
628 return PATCH_FORMAT_MBOX
;
631 * Otherwise, check the first few lines of the first patch, starting
632 * from the first non-blank line, to try to detect its format.
635 fp
= xfopen(*paths
, "r");
637 while (!strbuf_getline(&l1
, fp
)) {
642 if (starts_with(l1
.buf
, "From ") || starts_with(l1
.buf
, "From: ")) {
643 ret
= PATCH_FORMAT_MBOX
;
647 if (starts_with(l1
.buf
, "# This series applies on GIT commit")) {
648 ret
= PATCH_FORMAT_STGIT_SERIES
;
652 if (!strcmp(l1
.buf
, "# HG changeset patch")) {
653 ret
= PATCH_FORMAT_HG
;
657 strbuf_getline(&l2
, fp
);
658 strbuf_getline(&l3
, fp
);
661 * If the second line is empty and the third is a From, Author or Date
662 * entry, this is likely an StGit patch.
664 if (l1
.len
&& !l2
.len
&&
665 (starts_with(l3
.buf
, "From:") ||
666 starts_with(l3
.buf
, "Author:") ||
667 starts_with(l3
.buf
, "Date:"))) {
668 ret
= PATCH_FORMAT_STGIT
;
672 if (l1
.len
&& is_mail(fp
)) {
673 ret
= PATCH_FORMAT_MBOX
;
686 * Splits out individual email patches from `paths`, where each path is either
687 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
689 static int split_mail_mbox(struct am_state
*state
, const char **paths
,
690 int keep_cr
, int mboxrd
)
692 struct child_process cp
= CHILD_PROCESS_INIT
;
693 struct strbuf last
= STRBUF_INIT
;
697 strvec_push(&cp
.args
, "mailsplit");
698 strvec_pushf(&cp
.args
, "-d%d", state
->prec
);
699 strvec_pushf(&cp
.args
, "-o%s", state
->dir
);
700 strvec_push(&cp
.args
, "-b");
702 strvec_push(&cp
.args
, "--keep-cr");
704 strvec_push(&cp
.args
, "--mboxrd");
705 strvec_push(&cp
.args
, "--");
706 strvec_pushv(&cp
.args
, paths
);
708 ret
= capture_command(&cp
, &last
, 8);
713 state
->last
= strtol(last
.buf
, NULL
, 10);
716 strbuf_release(&last
);
721 * Callback signature for split_mail_conv(). The foreign patch should be
722 * read from `in`, and the converted patch (in RFC2822 mail format) should be
723 * written to `out`. Return 0 on success, or -1 on failure.
725 typedef int (*mail_conv_fn
)(FILE *out
, FILE *in
, int keep_cr
);
728 * Calls `fn` for each file in `paths` to convert the foreign patch to the
729 * RFC2822 mail format suitable for parsing with git-mailinfo.
731 * Returns 0 on success, -1 on failure.
733 static int split_mail_conv(mail_conv_fn fn
, struct am_state
*state
,
734 const char **paths
, int keep_cr
)
736 static const char *stdin_only
[] = {"-", NULL
};
742 for (i
= 0; *paths
; paths
++, i
++) {
747 if (!strcmp(*paths
, "-"))
750 in
= fopen(*paths
, "r");
753 return error_errno(_("could not open '%s' for reading"),
756 mail
= mkpath("%s/%0*d", state
->dir
, state
->prec
, i
+ 1);
758 out
= fopen(mail
, "w");
762 return error_errno(_("could not open '%s' for writing"),
766 ret
= fn(out
, in
, keep_cr
);
773 return error(_("could not parse patch '%s'"), *paths
);
782 * A split_mail_conv() callback that converts an StGit patch to an RFC2822
783 * message suitable for parsing with git-mailinfo.
785 static int stgit_patch_to_mail(FILE *out
, FILE *in
, int keep_cr
)
787 struct strbuf sb
= STRBUF_INIT
;
788 int subject_printed
= 0;
790 while (!strbuf_getline_lf(&sb
, in
)) {
793 if (str_isspace(sb
.buf
))
795 else if (skip_prefix(sb
.buf
, "Author:", &str
))
796 fprintf(out
, "From:%s\n", str
);
797 else if (starts_with(sb
.buf
, "From") || starts_with(sb
.buf
, "Date"))
798 fprintf(out
, "%s\n", sb
.buf
);
799 else if (!subject_printed
) {
800 fprintf(out
, "Subject: %s\n", sb
.buf
);
803 fprintf(out
, "\n%s\n", sb
.buf
);
809 while (strbuf_fread(&sb
, 8192, in
) > 0) {
810 fwrite(sb
.buf
, 1, sb
.len
, out
);
819 * This function only supports a single StGit series file in `paths`.
821 * Given an StGit series file, converts the StGit patches in the series into
822 * RFC2822 messages suitable for parsing with git-mailinfo, and queues them in
823 * the state directory.
825 * Returns 0 on success, -1 on failure.
827 static int split_mail_stgit_series(struct am_state
*state
, const char **paths
,
830 const char *series_dir
;
831 char *series_dir_buf
;
833 struct strvec patches
= STRVEC_INIT
;
834 struct strbuf sb
= STRBUF_INIT
;
837 if (!paths
[0] || paths
[1])
838 return error(_("Only one StGIT patch series can be applied at once"));
840 series_dir_buf
= xstrdup(*paths
);
841 series_dir
= dirname(series_dir_buf
);
843 fp
= fopen(*paths
, "r");
845 return error_errno(_("could not open '%s' for reading"), *paths
);
847 while (!strbuf_getline_lf(&sb
, fp
)) {
849 continue; /* skip comment lines */
851 strvec_push(&patches
, mkpath("%s/%s", series_dir
, sb
.buf
));
856 free(series_dir_buf
);
858 ret
= split_mail_conv(stgit_patch_to_mail
, state
, patches
.v
, keep_cr
);
860 strvec_clear(&patches
);
865 * A split_patches_conv() callback that converts a mercurial patch to a RFC2822
866 * message suitable for parsing with git-mailinfo.
868 static int hg_patch_to_mail(FILE *out
, FILE *in
, int keep_cr
)
870 struct strbuf sb
= STRBUF_INIT
;
873 while (!strbuf_getline_lf(&sb
, in
)) {
876 if (skip_prefix(sb
.buf
, "# User ", &str
))
877 fprintf(out
, "From: %s\n", str
);
878 else if (skip_prefix(sb
.buf
, "# Date ", &str
)) {
879 timestamp_t timestamp
;
884 timestamp
= parse_timestamp(str
, &end
, 10);
886 rc
= error(_("invalid timestamp"));
890 if (!skip_prefix(end
, " ", &str
)) {
891 rc
= error(_("invalid Date line"));
896 tz
= strtol(str
, &end
, 10);
898 rc
= error(_("invalid timezone offset"));
903 rc
= error(_("invalid Date line"));
908 * mercurial's timezone is in seconds west of UTC,
909 * however git's timezone is in hours + minutes east of
912 tz2
= labs(tz
) / 3600 * 100 + labs(tz
) % 3600 / 60;
916 fprintf(out
, "Date: %s\n", show_date(timestamp
, tz2
, DATE_MODE(RFC2822
)));
917 } else if (starts_with(sb
.buf
, "# ")) {
920 fprintf(out
, "\n%s\n", sb
.buf
);
926 while (strbuf_fread(&sb
, 8192, in
) > 0) {
927 fwrite(sb
.buf
, 1, sb
.len
, out
);
936 * Splits a list of files/directories into individual email patches. Each path
937 * in `paths` must be a file/directory that is formatted according to
940 * Once split out, the individual email patches will be stored in the state
941 * directory, with each patch's filename being its index, padded to state->prec
944 * state->cur will be set to the index of the first mail, and state->last will
945 * be set to the index of the last mail.
947 * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1
948 * to disable this behavior, -1 to use the default configured setting.
950 * Returns 0 on success, -1 on failure.
952 static int split_mail(struct am_state
*state
, enum patch_format patch_format
,
953 const char **paths
, int keep_cr
)
957 git_config_get_bool("am.keepcr", &keep_cr
);
960 switch (patch_format
) {
961 case PATCH_FORMAT_MBOX
:
962 return split_mail_mbox(state
, paths
, keep_cr
, 0);
963 case PATCH_FORMAT_STGIT
:
964 return split_mail_conv(stgit_patch_to_mail
, state
, paths
, keep_cr
);
965 case PATCH_FORMAT_STGIT_SERIES
:
966 return split_mail_stgit_series(state
, paths
, keep_cr
);
967 case PATCH_FORMAT_HG
:
968 return split_mail_conv(hg_patch_to_mail
, state
, paths
, keep_cr
);
969 case PATCH_FORMAT_MBOXRD
:
970 return split_mail_mbox(state
, paths
, keep_cr
, 1);
972 BUG("invalid patch_format");
978 * Setup a new am session for applying patches
980 static void am_setup(struct am_state
*state
, enum patch_format patch_format
,
981 const char **paths
, int keep_cr
)
983 struct object_id curr_head
;
985 struct strbuf sb
= STRBUF_INIT
;
988 patch_format
= detect_patch_format(paths
);
991 fprintf_ln(stderr
, _("Patch format detection failed."));
995 if (mkdir(state
->dir
, 0777) < 0 && errno
!= EEXIST
)
996 die_errno(_("failed to create directory '%s'"), state
->dir
);
997 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
999 if (split_mail(state
, patch_format
, paths
, keep_cr
) < 0) {
1001 die(_("Failed to split patches."));
1004 if (state
->rebasing
)
1005 state
->threeway
= 1;
1007 write_state_bool(state
, "threeway", state
->threeway
);
1008 write_state_bool(state
, "quiet", state
->quiet
);
1009 write_state_bool(state
, "sign", state
->signoff
);
1010 write_state_bool(state
, "utf8", state
->utf8
);
1012 if (state
->allow_rerere_autoupdate
)
1013 write_state_bool(state
, "rerere-autoupdate",
1014 state
->allow_rerere_autoupdate
== RERERE_AUTOUPDATE
);
1016 switch (state
->keep
) {
1023 case KEEP_NON_PATCH
:
1027 BUG("invalid value for state->keep");
1030 write_state_text(state
, "keep", str
);
1031 write_state_bool(state
, "messageid", state
->message_id
);
1033 switch (state
->scissors
) {
1034 case SCISSORS_UNSET
:
1037 case SCISSORS_FALSE
:
1044 BUG("invalid value for state->scissors");
1046 write_state_text(state
, "scissors", str
);
1048 switch (state
->quoted_cr
) {
1049 case quoted_cr_unset
:
1052 case quoted_cr_nowarn
:
1055 case quoted_cr_warn
:
1058 case quoted_cr_strip
:
1062 BUG("invalid value for state->quoted_cr");
1064 write_state_text(state
, "quoted-cr", str
);
1066 sq_quote_argv(&sb
, state
->git_apply_opts
.v
);
1067 write_state_text(state
, "apply-opt", sb
.buf
);
1069 if (state
->rebasing
)
1070 write_state_text(state
, "rebasing", "");
1072 write_state_text(state
, "applying", "");
1074 if (!get_oid("HEAD", &curr_head
)) {
1075 write_state_text(state
, "abort-safety", oid_to_hex(&curr_head
));
1076 if (!state
->rebasing
)
1077 update_ref("am", "ORIG_HEAD", &curr_head
, NULL
, 0,
1078 UPDATE_REFS_DIE_ON_ERR
);
1080 write_state_text(state
, "abort-safety", "");
1081 if (!state
->rebasing
)
1082 delete_ref(NULL
, "ORIG_HEAD", NULL
, 0);
1086 * NOTE: Since the "next" and "last" files determine if an am_state
1087 * session is in progress, they should be written last.
1090 write_state_count(state
, "next", state
->cur
);
1091 write_state_count(state
, "last", state
->last
);
1093 strbuf_release(&sb
);
1097 * Increments the patch pointer, and cleans am_state for the application of the
1100 static void am_next(struct am_state
*state
)
1102 struct object_id head
;
1104 FREE_AND_NULL(state
->author_name
);
1105 FREE_AND_NULL(state
->author_email
);
1106 FREE_AND_NULL(state
->author_date
);
1107 FREE_AND_NULL(state
->msg
);
1110 unlink(am_path(state
, "author-script"));
1111 unlink(am_path(state
, "final-commit"));
1113 oidclr(&state
->orig_commit
);
1114 unlink(am_path(state
, "original-commit"));
1115 delete_ref(NULL
, "REBASE_HEAD", NULL
, REF_NO_DEREF
);
1117 if (!get_oid("HEAD", &head
))
1118 write_state_text(state
, "abort-safety", oid_to_hex(&head
));
1120 write_state_text(state
, "abort-safety", "");
1123 write_state_count(state
, "next", state
->cur
);
1127 * Returns the filename of the current patch email.
1129 static const char *msgnum(const struct am_state
*state
)
1131 static struct strbuf sb
= STRBUF_INIT
;
1134 strbuf_addf(&sb
, "%0*d", state
->prec
, state
->cur
);
1140 * Dies with a user-friendly message on how to proceed after resolving the
1141 * problem. This message can be overridden with state->resolvemsg.
1143 static void NORETURN
die_user_resolve(const struct am_state
*state
)
1145 if (state
->resolvemsg
) {
1146 printf_ln("%s", state
->resolvemsg
);
1148 const char *cmdline
= state
->interactive
? "git am -i" : "git am";
1150 printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline
);
1151 printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline
);
1153 if (advice_enabled(ADVICE_AM_WORK_DIR
) &&
1154 is_empty_or_missing_file(am_path(state
, "patch")) &&
1155 !repo_index_has_changes(the_repository
, NULL
, NULL
))
1156 printf_ln(_("To record the empty patch as an empty commit, run \"%s --allow-empty\"."), cmdline
);
1158 printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline
);
1165 * Appends signoff to the "msg" field of the am_state.
1167 static void am_append_signoff(struct am_state
*state
)
1169 struct strbuf sb
= STRBUF_INIT
;
1171 strbuf_attach(&sb
, state
->msg
, state
->msg_len
, state
->msg_len
);
1172 append_signoff(&sb
, 0, 0);
1173 state
->msg
= strbuf_detach(&sb
, &state
->msg_len
);
1177 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
1178 * state->msg will be set to the patch message. state->author_name,
1179 * state->author_email and state->author_date will be set to the patch author's
1180 * name, email and date respectively. The patch body will be written to the
1181 * state directory's "patch" file.
1183 * Returns 1 if the patch should be skipped, 0 otherwise.
1185 static int parse_mail(struct am_state
*state
, const char *mail
)
1188 struct strbuf sb
= STRBUF_INIT
;
1189 struct strbuf msg
= STRBUF_INIT
;
1190 struct strbuf author_name
= STRBUF_INIT
;
1191 struct strbuf author_date
= STRBUF_INIT
;
1192 struct strbuf author_email
= STRBUF_INIT
;
1196 setup_mailinfo(&mi
);
1199 mi
.metainfo_charset
= get_commit_output_encoding();
1201 mi
.metainfo_charset
= NULL
;
1203 switch (state
->keep
) {
1207 mi
.keep_subject
= 1;
1209 case KEEP_NON_PATCH
:
1210 mi
.keep_non_patch_brackets_in_subject
= 1;
1213 BUG("invalid value for state->keep");
1216 if (state
->message_id
)
1217 mi
.add_message_id
= 1;
1219 switch (state
->scissors
) {
1220 case SCISSORS_UNSET
:
1222 case SCISSORS_FALSE
:
1223 mi
.use_scissors
= 0;
1226 mi
.use_scissors
= 1;
1229 BUG("invalid value for state->scissors");
1232 switch (state
->quoted_cr
) {
1233 case quoted_cr_unset
:
1235 case quoted_cr_nowarn
:
1236 case quoted_cr_warn
:
1237 case quoted_cr_strip
:
1238 mi
.quoted_cr
= state
->quoted_cr
;
1241 BUG("invalid value for state->quoted_cr");
1244 mi
.input
= xfopen(mail
, "r");
1245 mi
.output
= xfopen(am_path(state
, "info"), "w");
1246 if (mailinfo(&mi
, am_path(state
, "msg"), am_path(state
, "patch")))
1247 die("could not parse patch");
1252 if (mi
.format_flowed
)
1253 warning(_("Patch sent with format=flowed; "
1254 "space at the end of lines might be lost."));
1256 /* Extract message and author information */
1257 fp
= xfopen(am_path(state
, "info"), "r");
1258 while (!strbuf_getline_lf(&sb
, fp
)) {
1261 if (skip_prefix(sb
.buf
, "Subject: ", &x
)) {
1263 strbuf_addch(&msg
, '\n');
1264 strbuf_addstr(&msg
, x
);
1265 } else if (skip_prefix(sb
.buf
, "Author: ", &x
))
1266 strbuf_addstr(&author_name
, x
);
1267 else if (skip_prefix(sb
.buf
, "Email: ", &x
))
1268 strbuf_addstr(&author_email
, x
);
1269 else if (skip_prefix(sb
.buf
, "Date: ", &x
))
1270 strbuf_addstr(&author_date
, x
);
1274 /* Skip pine's internal folder data */
1275 if (!strcmp(author_name
.buf
, "Mail System Internal Data")) {
1280 strbuf_addstr(&msg
, "\n\n");
1281 strbuf_addbuf(&msg
, &mi
.log_message
);
1282 strbuf_stripspace(&msg
, 0);
1284 assert(!state
->author_name
);
1285 state
->author_name
= strbuf_detach(&author_name
, NULL
);
1287 assert(!state
->author_email
);
1288 state
->author_email
= strbuf_detach(&author_email
, NULL
);
1290 assert(!state
->author_date
);
1291 state
->author_date
= strbuf_detach(&author_date
, NULL
);
1293 assert(!state
->msg
);
1294 state
->msg
= strbuf_detach(&msg
, &state
->msg_len
);
1297 strbuf_release(&msg
);
1298 strbuf_release(&author_date
);
1299 strbuf_release(&author_email
);
1300 strbuf_release(&author_name
);
1301 strbuf_release(&sb
);
1302 clear_mailinfo(&mi
);
1307 * Sets commit_id to the commit hash where the mail was generated from.
1308 * Returns 0 on success, -1 on failure.
1310 static int get_mail_commit_oid(struct object_id
*commit_id
, const char *mail
)
1312 struct strbuf sb
= STRBUF_INIT
;
1313 FILE *fp
= xfopen(mail
, "r");
1317 if (strbuf_getline_lf(&sb
, fp
) ||
1318 !skip_prefix(sb
.buf
, "From ", &x
) ||
1319 get_oid_hex(x
, commit_id
) < 0)
1322 strbuf_release(&sb
);
1328 * Sets state->msg, state->author_name, state->author_email, state->author_date
1329 * to the commit's respective info.
1331 static void get_commit_info(struct am_state
*state
, struct commit
*commit
)
1333 const char *buffer
, *ident_line
, *msg
;
1335 struct ident_split id
;
1337 buffer
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
1339 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
1341 die(_("missing author line in commit %s"),
1342 oid_to_hex(&commit
->object
.oid
));
1343 if (split_ident_line(&id
, ident_line
, ident_len
) < 0)
1344 die(_("invalid ident line: %.*s"), (int)ident_len
, ident_line
);
1346 assert(!state
->author_name
);
1348 state
->author_name
=
1349 xmemdupz(id
.name_begin
, id
.name_end
- id
.name_begin
);
1351 state
->author_name
= xstrdup("");
1353 assert(!state
->author_email
);
1355 state
->author_email
=
1356 xmemdupz(id
.mail_begin
, id
.mail_end
- id
.mail_begin
);
1358 state
->author_email
= xstrdup("");
1360 assert(!state
->author_date
);
1361 state
->author_date
= xstrdup(show_ident_date(&id
, DATE_MODE(NORMAL
)));
1363 assert(!state
->msg
);
1364 msg
= strstr(buffer
, "\n\n");
1366 die(_("unable to parse commit %s"), oid_to_hex(&commit
->object
.oid
));
1367 state
->msg
= xstrdup(msg
+ 2);
1368 state
->msg_len
= strlen(state
->msg
);
1369 unuse_commit_buffer(commit
, buffer
);
1373 * Writes `commit` as a patch to the state directory's "patch" file.
1375 static void write_commit_patch(const struct am_state
*state
, struct commit
*commit
)
1377 struct rev_info rev_info
;
1380 fp
= xfopen(am_path(state
, "patch"), "w");
1381 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1383 rev_info
.abbrev
= 0;
1384 rev_info
.disable_stdin
= 1;
1385 rev_info
.show_root_diff
= 1;
1386 rev_info
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1387 rev_info
.no_commit_id
= 1;
1388 rev_info
.diffopt
.flags
.binary
= 1;
1389 rev_info
.diffopt
.flags
.full_index
= 1;
1390 rev_info
.diffopt
.use_color
= 0;
1391 rev_info
.diffopt
.file
= fp
;
1392 rev_info
.diffopt
.close_file
= 1;
1393 add_pending_object(&rev_info
, &commit
->object
, "");
1394 diff_setup_done(&rev_info
.diffopt
);
1395 log_tree_commit(&rev_info
, commit
);
1396 release_revisions(&rev_info
);
1400 * Writes the diff of the index against HEAD as a patch to the state
1401 * directory's "patch" file.
1403 static void write_index_patch(const struct am_state
*state
)
1406 struct object_id head
;
1407 struct rev_info rev_info
;
1410 if (!get_oid("HEAD", &head
)) {
1411 struct commit
*commit
= lookup_commit_or_die(&head
, "HEAD");
1412 tree
= get_commit_tree(commit
);
1414 tree
= lookup_tree(the_repository
,
1415 the_repository
->hash_algo
->empty_tree
);
1417 fp
= xfopen(am_path(state
, "patch"), "w");
1418 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1420 rev_info
.disable_stdin
= 1;
1421 rev_info
.no_commit_id
= 1;
1422 rev_info
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1423 rev_info
.diffopt
.use_color
= 0;
1424 rev_info
.diffopt
.file
= fp
;
1425 rev_info
.diffopt
.close_file
= 1;
1426 add_pending_object(&rev_info
, &tree
->object
, "");
1427 diff_setup_done(&rev_info
.diffopt
);
1428 run_diff_index(&rev_info
, 1);
1429 release_revisions(&rev_info
);
1433 * Like parse_mail(), but parses the mail by looking up its commit ID
1434 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
1437 * state->orig_commit will be set to the original commit ID.
1439 * Will always return 0 as the patch should never be skipped.
1441 static int parse_mail_rebase(struct am_state
*state
, const char *mail
)
1443 struct commit
*commit
;
1444 struct object_id commit_oid
;
1446 if (get_mail_commit_oid(&commit_oid
, mail
) < 0)
1447 die(_("could not parse %s"), mail
);
1449 commit
= lookup_commit_or_die(&commit_oid
, mail
);
1451 get_commit_info(state
, commit
);
1453 write_commit_patch(state
, commit
);
1455 oidcpy(&state
->orig_commit
, &commit_oid
);
1456 write_state_text(state
, "original-commit", oid_to_hex(&commit_oid
));
1457 update_ref("am", "REBASE_HEAD", &commit_oid
,
1458 NULL
, REF_NO_DEREF
, UPDATE_REFS_DIE_ON_ERR
);
1464 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
1465 * `index_file` is not NULL, the patch will be applied to that index.
1467 static int run_apply(const struct am_state
*state
, const char *index_file
)
1469 struct strvec apply_paths
= STRVEC_INIT
;
1470 struct strvec apply_opts
= STRVEC_INIT
;
1471 struct apply_state apply_state
;
1473 int force_apply
= 0;
1475 const char **apply_argv
;
1477 if (init_apply_state(&apply_state
, the_repository
, NULL
))
1478 BUG("init_apply_state() failed");
1480 strvec_push(&apply_opts
, "apply");
1481 strvec_pushv(&apply_opts
, state
->git_apply_opts
.v
);
1484 * Build a copy that apply_parse_options() can rearrange.
1485 * apply_opts.v keeps referencing the allocated strings for
1486 * strvec_clear() to release.
1488 DUP_ARRAY(apply_argv
, apply_opts
.v
, apply_opts
.nr
);
1490 opts_left
= apply_parse_options(apply_opts
.nr
, apply_argv
,
1491 &apply_state
, &force_apply
, &options
,
1495 die("unknown option passed through to git apply");
1498 apply_state
.index_file
= index_file
;
1499 apply_state
.cached
= 1;
1501 apply_state
.check_index
= 1;
1504 * If we are allowed to fall back on 3-way merge, don't give false
1505 * errors during the initial attempt.
1507 if (state
->threeway
&& !index_file
)
1508 apply_state
.apply_verbosity
= verbosity_silent
;
1510 if (check_apply_state(&apply_state
, force_apply
))
1511 BUG("check_apply_state() failed");
1513 strvec_push(&apply_paths
, am_path(state
, "patch"));
1515 res
= apply_all_patches(&apply_state
, apply_paths
.nr
, apply_paths
.v
, options
);
1517 strvec_clear(&apply_paths
);
1518 strvec_clear(&apply_opts
);
1519 clear_apply_state(&apply_state
);
1526 /* Reload index as apply_all_patches() will have modified it. */
1527 discard_index(&the_index
);
1528 read_index_from(&the_index
, index_file
, get_git_dir());
1535 * Builds an index that contains just the blobs needed for a 3way merge.
1537 static int build_fake_ancestor(const struct am_state
*state
, const char *index_file
)
1539 struct child_process cp
= CHILD_PROCESS_INIT
;
1542 strvec_push(&cp
.args
, "apply");
1543 strvec_pushv(&cp
.args
, state
->git_apply_opts
.v
);
1544 strvec_pushf(&cp
.args
, "--build-fake-ancestor=%s", index_file
);
1545 strvec_push(&cp
.args
, am_path(state
, "patch"));
1547 if (run_command(&cp
))
1554 * Attempt a threeway merge, using index_path as the temporary index.
1556 static int fall_back_threeway(const struct am_state
*state
, const char *index_path
)
1558 struct object_id orig_tree
, their_tree
, our_tree
;
1559 const struct object_id
*bases
[1] = { &orig_tree
};
1560 struct merge_options o
;
1561 struct commit
*result
;
1562 char *their_tree_name
;
1564 if (get_oid("HEAD", &our_tree
) < 0)
1565 oidcpy(&our_tree
, the_hash_algo
->empty_tree
);
1567 if (build_fake_ancestor(state
, index_path
))
1568 return error("could not build fake ancestor");
1570 discard_index(&the_index
);
1571 read_index_from(&the_index
, index_path
, get_git_dir());
1573 if (write_index_as_tree(&orig_tree
, &the_index
, index_path
, 0, NULL
))
1574 return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1576 say(state
, stdout
, _("Using index info to reconstruct a base tree..."));
1578 if (!state
->quiet
) {
1580 * List paths that needed 3-way fallback, so that the user can
1581 * review them with extra care to spot mismerges.
1583 struct rev_info rev_info
;
1585 repo_init_revisions(the_repository
, &rev_info
, NULL
);
1586 rev_info
.diffopt
.output_format
= DIFF_FORMAT_NAME_STATUS
;
1587 rev_info
.diffopt
.filter
|= diff_filter_bit('A');
1588 rev_info
.diffopt
.filter
|= diff_filter_bit('M');
1589 add_pending_oid(&rev_info
, "HEAD", &our_tree
, 0);
1590 diff_setup_done(&rev_info
.diffopt
);
1591 run_diff_index(&rev_info
, 1);
1592 release_revisions(&rev_info
);
1595 if (run_apply(state
, index_path
))
1596 return error(_("Did you hand edit your patch?\n"
1597 "It does not apply to blobs recorded in its index."));
1599 if (write_index_as_tree(&their_tree
, &the_index
, index_path
, 0, NULL
))
1600 return error("could not write tree");
1602 say(state
, stdout
, _("Falling back to patching base and 3-way merge..."));
1604 discard_index(&the_index
);
1605 repo_read_index(the_repository
);
1608 * This is not so wrong. Depending on which base we picked, orig_tree
1609 * may be wildly different from ours, but their_tree has the same set of
1610 * wildly different changes in parts the patch did not touch, so
1611 * recursive ends up canceling them, saying that we reverted all those
1615 init_merge_options(&o
, the_repository
);
1618 their_tree_name
= xstrfmt("%.*s", linelen(state
->msg
), state
->msg
);
1619 o
.branch2
= their_tree_name
;
1620 o
.detect_directory_renames
= MERGE_DIRECTORY_RENAMES_NONE
;
1625 if (merge_recursive_generic(&o
, &our_tree
, &their_tree
, 1, bases
, &result
)) {
1626 repo_rerere(the_repository
, state
->allow_rerere_autoupdate
);
1627 free(their_tree_name
);
1628 return error(_("Failed to merge in the changes."));
1631 free(their_tree_name
);
1636 * Commits the current index with state->msg as the commit message and
1637 * state->author_name, state->author_email and state->author_date as the author
1640 static void do_commit(const struct am_state
*state
)
1642 struct object_id tree
, parent
, commit
;
1643 const struct object_id
*old_oid
;
1644 struct commit_list
*parents
= NULL
;
1645 const char *reflog_msg
, *author
, *committer
= NULL
;
1646 struct strbuf sb
= STRBUF_INIT
;
1648 if (!state
->no_verify
&& run_hooks("pre-applypatch"))
1651 if (write_index_as_tree(&tree
, &the_index
, get_index_file(), 0, NULL
))
1652 die(_("git write-tree failed to write a tree"));
1654 if (!get_oid_commit("HEAD", &parent
)) {
1656 commit_list_insert(lookup_commit(the_repository
, &parent
),
1660 say(state
, stderr
, _("applying to an empty history"));
1663 author
= fmt_ident(state
->author_name
, state
->author_email
,
1665 state
->ignore_date
? NULL
: state
->author_date
,
1668 if (state
->committer_date_is_author_date
)
1669 committer
= fmt_ident(getenv("GIT_COMMITTER_NAME"),
1670 getenv("GIT_COMMITTER_EMAIL"),
1671 WANT_COMMITTER_IDENT
,
1672 state
->ignore_date
? NULL
1673 : state
->author_date
,
1676 if (commit_tree_extended(state
->msg
, state
->msg_len
, &tree
, parents
,
1677 &commit
, author
, committer
, state
->sign_commit
,
1679 die(_("failed to write commit object"));
1681 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1685 strbuf_addf(&sb
, "%s: %.*s", reflog_msg
, linelen(state
->msg
),
1688 update_ref(sb
.buf
, "HEAD", &commit
, old_oid
, 0,
1689 UPDATE_REFS_DIE_ON_ERR
);
1691 if (state
->rebasing
) {
1692 FILE *fp
= xfopen(am_path(state
, "rewritten"), "a");
1694 assert(!is_null_oid(&state
->orig_commit
));
1695 fprintf(fp
, "%s ", oid_to_hex(&state
->orig_commit
));
1696 fprintf(fp
, "%s\n", oid_to_hex(&commit
));
1700 run_hooks("post-applypatch");
1702 strbuf_release(&sb
);
1706 * Validates the am_state for resuming -- the "msg" and authorship fields must
1709 static void validate_resume_state(const struct am_state
*state
)
1712 die(_("cannot resume: %s does not exist."),
1713 am_path(state
, "final-commit"));
1715 if (!state
->author_name
|| !state
->author_email
|| !state
->author_date
)
1716 die(_("cannot resume: %s does not exist."),
1717 am_path(state
, "author-script"));
1721 * Interactively prompt the user on whether the current patch should be
1724 * Returns 0 if the user chooses to apply the patch, 1 if the user chooses to
1727 static int do_interactive(struct am_state
*state
)
1734 puts(_("Commit Body is:"));
1735 puts("--------------------------");
1736 printf("%s", state
->msg
);
1737 puts("--------------------------");
1740 * TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
1741 * in your translation. The program will only accept English
1742 * input at this point.
1744 printf(_("Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all: "));
1745 if (!fgets(reply
, sizeof(reply
), stdin
))
1746 die("unable to read from stdin; aborting");
1748 if (*reply
== 'y' || *reply
== 'Y') {
1750 } else if (*reply
== 'a' || *reply
== 'A') {
1751 state
->interactive
= 0;
1753 } else if (*reply
== 'n' || *reply
== 'N') {
1755 } else if (*reply
== 'e' || *reply
== 'E') {
1756 struct strbuf msg
= STRBUF_INIT
;
1758 if (!launch_editor(am_path(state
, "final-commit"), &msg
, NULL
)) {
1760 state
->msg
= strbuf_detach(&msg
, &state
->msg_len
);
1762 strbuf_release(&msg
);
1763 } else if (*reply
== 'v' || *reply
== 'V') {
1764 const char *pager
= git_pager(1);
1765 struct child_process cp
= CHILD_PROCESS_INIT
;
1769 prepare_pager_args(&cp
, pager
);
1770 strvec_push(&cp
.args
, am_path(state
, "patch"));
1777 * Applies all queued mail.
1779 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
1780 * well as the state directory's "patch" file is used as-is for applying the
1781 * patch and committing it.
1783 static void am_run(struct am_state
*state
, int resume
)
1785 struct strbuf sb
= STRBUF_INIT
;
1787 unlink(am_path(state
, "dirtyindex"));
1789 if (repo_refresh_and_write_index(the_repository
, REFRESH_QUIET
, 0, 0,
1790 NULL
, NULL
, NULL
) < 0)
1791 die(_("unable to write index file"));
1793 if (repo_index_has_changes(the_repository
, NULL
, &sb
)) {
1794 write_state_bool(state
, "dirtyindex", 1);
1795 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb
.buf
);
1798 strbuf_release(&sb
);
1800 while (state
->cur
<= state
->last
) {
1801 const char *mail
= am_path(state
, msgnum(state
));
1807 if (!file_exists(mail
))
1811 validate_resume_state(state
);
1815 if (state
->rebasing
)
1816 skip
= parse_mail_rebase(state
, mail
);
1818 skip
= parse_mail(state
, mail
);
1821 goto next
; /* mail should be skipped */
1824 am_append_signoff(state
);
1826 write_author_script(state
);
1827 write_commit_msg(state
);
1830 if (state
->interactive
&& do_interactive(state
))
1834 if (is_empty_or_missing_file(am_path(state
, "patch"))) {
1835 switch (state
->empty_type
) {
1836 case DROP_EMPTY_COMMIT
:
1837 say(state
, stdout
, _("Skipping: %.*s"), linelen(state
->msg
), state
->msg
);
1840 case KEEP_EMPTY_COMMIT
:
1842 say(state
, stdout
, _("Creating an empty commit: %.*s"),
1843 linelen(state
->msg
), state
->msg
);
1845 case STOP_ON_EMPTY_COMMIT
:
1846 printf_ln(_("Patch is empty."));
1847 die_user_resolve(state
);
1852 if (run_applypatch_msg_hook(state
))
1857 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1859 apply_status
= run_apply(state
, NULL
);
1861 if (apply_status
&& state
->threeway
) {
1862 struct strbuf sb
= STRBUF_INIT
;
1864 strbuf_addstr(&sb
, am_path(state
, "patch-merge-index"));
1865 apply_status
= fall_back_threeway(state
, sb
.buf
);
1866 strbuf_release(&sb
);
1869 * Applying the patch to an earlier tree and merging
1870 * the result may have produced the same tree as ours.
1872 if (!apply_status
&&
1873 !repo_index_has_changes(the_repository
, NULL
, NULL
)) {
1874 say(state
, stdout
, _("No changes -- Patch already applied."));
1880 printf_ln(_("Patch failed at %s %.*s"), msgnum(state
),
1881 linelen(state
->msg
), state
->msg
);
1883 if (advice_enabled(ADVICE_AM_WORK_DIR
))
1884 advise(_("Use 'git am --show-current-patch=diff' to see the failed patch"));
1886 die_user_resolve(state
);
1900 if (!is_empty_or_missing_file(am_path(state
, "rewritten"))) {
1901 assert(state
->rebasing
);
1902 copy_notes_for_rebase(state
);
1903 run_post_rewrite_hook(state
);
1907 * In rebasing mode, it's up to the caller to take care of
1910 if (!state
->rebasing
) {
1912 run_auto_maintenance(state
->quiet
);
1917 * Resume the current am session after patch application failure. The user did
1918 * all the hard work, and we do not have to do any patch application. Just
1919 * trust and commit what the user has in the index and working tree. If `allow_empty`
1920 * is true, commit as an empty commit when index has not changed and lacking a patch.
1922 static void am_resolve(struct am_state
*state
, int allow_empty
)
1924 validate_resume_state(state
);
1926 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1928 if (!repo_index_has_changes(the_repository
, NULL
, NULL
)) {
1929 if (allow_empty
&& is_empty_or_missing_file(am_path(state
, "patch"))) {
1930 printf_ln(_("No changes - recorded it as an empty commit."));
1932 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1933 "If there is nothing left to stage, chances are that something else\n"
1934 "already introduced the same changes; you might want to skip this patch."));
1935 die_user_resolve(state
);
1939 if (unmerged_index(&the_index
)) {
1940 printf_ln(_("You still have unmerged paths in your index.\n"
1941 "You should 'git add' each file with resolved conflicts to mark them as such.\n"
1942 "You might run `git rm` on a file to accept \"deleted by them\" for it."));
1943 die_user_resolve(state
);
1946 if (state
->interactive
) {
1947 write_index_patch(state
);
1948 if (do_interactive(state
))
1952 repo_rerere(the_repository
, 0);
1963 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1964 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1967 static int fast_forward_to(struct tree
*head
, struct tree
*remote
, int reset
)
1969 struct lock_file lock_file
= LOCK_INIT
;
1970 struct unpack_trees_options opts
;
1971 struct tree_desc t
[2];
1973 if (parse_tree(head
) || parse_tree(remote
))
1976 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
1978 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
1980 memset(&opts
, 0, sizeof(opts
));
1982 opts
.src_index
= &the_index
;
1983 opts
.dst_index
= &the_index
;
1986 opts
.reset
= reset
? UNPACK_RESET_PROTECT_UNTRACKED
: 0;
1987 opts
.preserve_ignored
= 0; /* FIXME: !overwrite_ignore */
1988 opts
.fn
= twoway_merge
;
1989 init_tree_desc(&t
[0], head
->buffer
, head
->size
);
1990 init_tree_desc(&t
[1], remote
->buffer
, remote
->size
);
1992 if (unpack_trees(2, t
, &opts
)) {
1993 rollback_lock_file(&lock_file
);
1997 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
1998 die(_("unable to write new index file"));
2004 * Merges a tree into the index. The index's stat info will take precedence
2005 * over the merged tree's. Returns 0 on success, -1 on failure.
2007 static int merge_tree(struct tree
*tree
)
2009 struct lock_file lock_file
= LOCK_INIT
;
2010 struct unpack_trees_options opts
;
2011 struct tree_desc t
[1];
2013 if (parse_tree(tree
))
2016 repo_hold_locked_index(the_repository
, &lock_file
, LOCK_DIE_ON_ERROR
);
2018 memset(&opts
, 0, sizeof(opts
));
2020 opts
.src_index
= &the_index
;
2021 opts
.dst_index
= &the_index
;
2023 opts
.fn
= oneway_merge
;
2024 init_tree_desc(&t
[0], tree
->buffer
, tree
->size
);
2026 if (unpack_trees(1, t
, &opts
)) {
2027 rollback_lock_file(&lock_file
);
2031 if (write_locked_index(&the_index
, &lock_file
, COMMIT_LOCK
))
2032 die(_("unable to write new index file"));
2038 * Clean the index without touching entries that are not modified between
2039 * `head` and `remote`.
2041 static int clean_index(const struct object_id
*head
, const struct object_id
*remote
)
2043 struct tree
*head_tree
, *remote_tree
, *index_tree
;
2044 struct object_id index
;
2046 head_tree
= parse_tree_indirect(head
);
2048 return error(_("Could not parse object '%s'."), oid_to_hex(head
));
2050 remote_tree
= parse_tree_indirect(remote
);
2052 return error(_("Could not parse object '%s'."), oid_to_hex(remote
));
2054 repo_read_index_unmerged(the_repository
);
2056 if (fast_forward_to(head_tree
, head_tree
, 1))
2059 if (write_index_as_tree(&index
, &the_index
, get_index_file(), 0, NULL
))
2062 index_tree
= parse_tree_indirect(&index
);
2064 return error(_("Could not parse object '%s'."), oid_to_hex(&index
));
2066 if (fast_forward_to(index_tree
, remote_tree
, 0))
2069 if (merge_tree(remote_tree
))
2072 remove_branch_state(the_repository
, 0);
2078 * Resets rerere's merge resolution metadata.
2080 static void am_rerere_clear(void)
2082 struct string_list merge_rr
= STRING_LIST_INIT_DUP
;
2083 rerere_clear(the_repository
, &merge_rr
);
2084 string_list_clear(&merge_rr
, 1);
2088 * Resume the current am session by skipping the current patch.
2090 static void am_skip(struct am_state
*state
)
2092 struct object_id head
;
2096 if (get_oid("HEAD", &head
))
2097 oidcpy(&head
, the_hash_algo
->empty_tree
);
2099 if (clean_index(&head
, &head
))
2100 die(_("failed to clean index"));
2102 if (state
->rebasing
) {
2103 FILE *fp
= xfopen(am_path(state
, "rewritten"), "a");
2105 assert(!is_null_oid(&state
->orig_commit
));
2106 fprintf(fp
, "%s ", oid_to_hex(&state
->orig_commit
));
2107 fprintf(fp
, "%s\n", oid_to_hex(&head
));
2117 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
2119 * It is not safe to reset HEAD when:
2120 * 1. git-am previously failed because the index was dirty.
2121 * 2. HEAD has moved since git-am previously failed.
2123 static int safe_to_abort(const struct am_state
*state
)
2125 struct strbuf sb
= STRBUF_INIT
;
2126 struct object_id abort_safety
, head
;
2128 if (file_exists(am_path(state
, "dirtyindex")))
2131 if (read_state_file(&sb
, state
, "abort-safety", 1) > 0) {
2132 if (get_oid_hex(sb
.buf
, &abort_safety
))
2133 die(_("could not parse %s"), am_path(state
, "abort-safety"));
2135 oidclr(&abort_safety
);
2136 strbuf_release(&sb
);
2138 if (get_oid("HEAD", &head
))
2141 if (oideq(&head
, &abort_safety
))
2144 warning(_("You seem to have moved HEAD since the last 'am' failure.\n"
2145 "Not rewinding to ORIG_HEAD"));
2151 * Aborts the current am session if it is safe to do so.
2153 static void am_abort(struct am_state
*state
)
2155 struct object_id curr_head
, orig_head
;
2156 int has_curr_head
, has_orig_head
;
2159 if (!safe_to_abort(state
)) {
2166 curr_branch
= resolve_refdup("HEAD", 0, &curr_head
, NULL
);
2167 has_curr_head
= curr_branch
&& !is_null_oid(&curr_head
);
2169 oidcpy(&curr_head
, the_hash_algo
->empty_tree
);
2171 has_orig_head
= !get_oid("ORIG_HEAD", &orig_head
);
2173 oidcpy(&orig_head
, the_hash_algo
->empty_tree
);
2175 if (clean_index(&curr_head
, &orig_head
))
2176 die(_("failed to clean index"));
2179 update_ref("am --abort", "HEAD", &orig_head
,
2180 has_curr_head
? &curr_head
: NULL
, 0,
2181 UPDATE_REFS_DIE_ON_ERR
);
2182 else if (curr_branch
)
2183 delete_ref(NULL
, curr_branch
, NULL
, REF_NO_DEREF
);
2189 static int show_patch(struct am_state
*state
, enum show_patch_type sub_mode
)
2191 struct strbuf sb
= STRBUF_INIT
;
2192 const char *patch_path
;
2195 if (!is_null_oid(&state
->orig_commit
)) {
2196 struct child_process cmd
= CHILD_PROCESS_INIT
;
2198 strvec_pushl(&cmd
.args
, "show", oid_to_hex(&state
->orig_commit
),
2201 return run_command(&cmd
);
2205 case SHOW_PATCH_RAW
:
2206 patch_path
= am_path(state
, msgnum(state
));
2208 case SHOW_PATCH_DIFF
:
2209 patch_path
= am_path(state
, "patch");
2212 BUG("invalid mode for --show-current-patch");
2215 len
= strbuf_read_file(&sb
, patch_path
, 0);
2217 die_errno(_("failed to read '%s'"), patch_path
);
2220 write_in_full(1, sb
.buf
, sb
.len
);
2221 strbuf_release(&sb
);
2226 * parse_options() callback that validates and sets opt->value to the
2227 * PATCH_FORMAT_* enum value corresponding to `arg`.
2229 static int parse_opt_patchformat(const struct option
*opt
, const char *arg
, int unset
)
2231 int *opt_value
= opt
->value
;
2234 *opt_value
= PATCH_FORMAT_UNKNOWN
;
2235 else if (!strcmp(arg
, "mbox"))
2236 *opt_value
= PATCH_FORMAT_MBOX
;
2237 else if (!strcmp(arg
, "stgit"))
2238 *opt_value
= PATCH_FORMAT_STGIT
;
2239 else if (!strcmp(arg
, "stgit-series"))
2240 *opt_value
= PATCH_FORMAT_STGIT_SERIES
;
2241 else if (!strcmp(arg
, "hg"))
2242 *opt_value
= PATCH_FORMAT_HG
;
2243 else if (!strcmp(arg
, "mboxrd"))
2244 *opt_value
= PATCH_FORMAT_MBOXRD
;
2246 * Please update $__git_patchformat in git-completion.bash
2247 * when you add new options
2250 return error(_("invalid value for '%s': '%s'"),
2251 "--patch-format", arg
);
2266 struct resume_mode
{
2267 enum resume_type mode
;
2268 enum show_patch_type sub_mode
;
2271 static int parse_opt_show_current_patch(const struct option
*opt
, const char *arg
, int unset
)
2273 int *opt_value
= opt
->value
;
2274 struct resume_mode
*resume
= container_of(opt_value
, struct resume_mode
, mode
);
2277 * Please update $__git_showcurrentpatch in git-completion.bash
2278 * when you add new options
2280 const char *valid_modes
[] = {
2281 [SHOW_PATCH_DIFF
] = "diff",
2282 [SHOW_PATCH_RAW
] = "raw"
2284 int new_value
= SHOW_PATCH_RAW
;
2286 BUG_ON_OPT_NEG(unset
);
2289 for (new_value
= 0; new_value
< ARRAY_SIZE(valid_modes
); new_value
++) {
2290 if (!strcmp(arg
, valid_modes
[new_value
]))
2293 if (new_value
>= ARRAY_SIZE(valid_modes
))
2294 return error(_("invalid value for '%s': '%s'"),
2295 "--show-current-patch", arg
);
2298 if (resume
->mode
== RESUME_SHOW_PATCH
&& new_value
!= resume
->sub_mode
)
2299 return error(_("options '%s=%s' and '%s=%s' "
2300 "cannot be used together"),
2301 "--show-current-patch", "--show-current-patch", arg
, valid_modes
[resume
->sub_mode
]);
2303 resume
->mode
= RESUME_SHOW_PATCH
;
2304 resume
->sub_mode
= new_value
;
2308 int cmd_am(int argc
, const char **argv
, const char *prefix
)
2310 struct am_state state
;
2313 int patch_format
= PATCH_FORMAT_UNKNOWN
;
2314 struct resume_mode resume
= { .mode
= RESUME_FALSE
};
2318 const char * const usage
[] = {
2319 N_("git am [<options>] [(<mbox> | <Maildir>)...]"),
2320 N_("git am [<options>] (--continue | --skip | --abort)"),
2324 struct option options
[] = {
2325 OPT_BOOL('i', "interactive", &state
.interactive
,
2326 N_("run interactively")),
2327 OPT_BOOL('n', "no-verify", &state
.no_verify
,
2328 N_("bypass pre-applypatch and applypatch-msg hooks")),
2329 OPT_HIDDEN_BOOL('b', "binary", &binary
,
2330 N_("historical option -- no-op")),
2331 OPT_BOOL('3', "3way", &state
.threeway
,
2332 N_("allow fall back on 3way merging if needed")),
2333 OPT__QUIET(&state
.quiet
, N_("be quiet")),
2334 OPT_SET_INT('s', "signoff", &state
.signoff
,
2335 N_("add a Signed-off-by trailer to the commit message"),
2337 OPT_BOOL('u', "utf8", &state
.utf8
,
2338 N_("recode into utf8 (default)")),
2339 OPT_SET_INT('k', "keep", &state
.keep
,
2340 N_("pass -k flag to git-mailinfo"), KEEP_TRUE
),
2341 OPT_SET_INT(0, "keep-non-patch", &state
.keep
,
2342 N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH
),
2343 OPT_BOOL('m', "message-id", &state
.message_id
,
2344 N_("pass -m flag to git-mailinfo")),
2345 OPT_SET_INT_F(0, "keep-cr", &keep_cr
,
2346 N_("pass --keep-cr flag to git-mailsplit for mbox format"),
2347 1, PARSE_OPT_NONEG
),
2348 OPT_SET_INT_F(0, "no-keep-cr", &keep_cr
,
2349 N_("do not pass --keep-cr flag to git-mailsplit independent of am.keepcr"),
2350 0, PARSE_OPT_NONEG
),
2351 OPT_BOOL('c', "scissors", &state
.scissors
,
2352 N_("strip everything before a scissors line")),
2353 OPT_CALLBACK_F(0, "quoted-cr", &state
.quoted_cr
, N_("action"),
2354 N_("pass it through git-mailinfo"),
2355 PARSE_OPT_NONEG
, am_option_parse_quoted_cr
),
2356 OPT_PASSTHRU_ARGV(0, "whitespace", &state
.git_apply_opts
, N_("action"),
2357 N_("pass it through git-apply"),
2359 OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state
.git_apply_opts
, NULL
,
2360 N_("pass it through git-apply"),
2362 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state
.git_apply_opts
, NULL
,
2363 N_("pass it through git-apply"),
2365 OPT_PASSTHRU_ARGV(0, "directory", &state
.git_apply_opts
, N_("root"),
2366 N_("pass it through git-apply"),
2368 OPT_PASSTHRU_ARGV(0, "exclude", &state
.git_apply_opts
, N_("path"),
2369 N_("pass it through git-apply"),
2371 OPT_PASSTHRU_ARGV(0, "include", &state
.git_apply_opts
, N_("path"),
2372 N_("pass it through git-apply"),
2374 OPT_PASSTHRU_ARGV('C', NULL
, &state
.git_apply_opts
, N_("n"),
2375 N_("pass it through git-apply"),
2377 OPT_PASSTHRU_ARGV('p', NULL
, &state
.git_apply_opts
, N_("num"),
2378 N_("pass it through git-apply"),
2380 OPT_CALLBACK(0, "patch-format", &patch_format
, N_("format"),
2381 N_("format the patch(es) are in"),
2382 parse_opt_patchformat
),
2383 OPT_PASSTHRU_ARGV(0, "reject", &state
.git_apply_opts
, NULL
,
2384 N_("pass it through git-apply"),
2386 OPT_STRING(0, "resolvemsg", &state
.resolvemsg
, NULL
,
2387 N_("override error message when patch failure occurs")),
2388 OPT_CMDMODE(0, "continue", &resume
.mode
,
2389 N_("continue applying patches after resolving a conflict"),
2391 OPT_CMDMODE('r', "resolved", &resume
.mode
,
2392 N_("synonyms for --continue"),
2394 OPT_CMDMODE(0, "skip", &resume
.mode
,
2395 N_("skip the current patch"),
2397 OPT_CMDMODE(0, "abort", &resume
.mode
,
2398 N_("restore the original branch and abort the patching operation"),
2400 OPT_CMDMODE(0, "quit", &resume
.mode
,
2401 N_("abort the patching operation but keep HEAD where it is"),
2403 { OPTION_CALLBACK
, 0, "show-current-patch", &resume
.mode
,
2405 N_("show the patch being applied"),
2406 PARSE_OPT_CMDMODE
| PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
| PARSE_OPT_LITERAL_ARGHELP
,
2407 parse_opt_show_current_patch
, RESUME_SHOW_PATCH
},
2408 OPT_CMDMODE(0, "allow-empty", &resume
.mode
,
2409 N_("record the empty patch as an empty commit"),
2410 RESUME_ALLOW_EMPTY
),
2411 OPT_BOOL(0, "committer-date-is-author-date",
2412 &state
.committer_date_is_author_date
,
2413 N_("lie about committer date")),
2414 OPT_BOOL(0, "ignore-date", &state
.ignore_date
,
2415 N_("use current timestamp for author date")),
2416 OPT_RERERE_AUTOUPDATE(&state
.allow_rerere_autoupdate
),
2417 { OPTION_STRING
, 'S', "gpg-sign", &state
.sign_commit
, N_("key-id"),
2418 N_("GPG-sign commits"),
2419 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
2420 OPT_CALLBACK_F(STOP_ON_EMPTY_COMMIT
, "empty", &state
.empty_type
, "{stop,drop,keep}",
2421 N_("how to handle empty patches"),
2422 PARSE_OPT_NONEG
, am_option_parse_empty
),
2423 OPT_HIDDEN_BOOL(0, "rebasing", &state
.rebasing
,
2424 N_("(internal use for git-rebase)")),
2428 if (argc
== 2 && !strcmp(argv
[1], "-h"))
2429 usage_with_options(usage
, options
);
2431 git_config(git_default_config
, NULL
);
2433 am_state_init(&state
);
2435 in_progress
= am_in_progress(&state
);
2439 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
2442 fprintf_ln(stderr
, _("The -b/--binary option has been a no-op for long time, and\n"
2443 "it will be removed. Please do not use it anymore."));
2445 /* Ensure a valid committer ident can be constructed */
2446 git_committer_info(IDENT_STRICT
);
2448 if (repo_read_index_preload(the_repository
, NULL
, 0) < 0)
2449 die(_("failed to read the index"));
2453 * Catch user error to feed us patches when there is a session
2456 * 1. mbox path(s) are provided on the command-line.
2457 * 2. stdin is not a tty: the user is trying to feed us a patch
2458 * from standard input. This is somewhat unreliable -- stdin
2459 * could be /dev/null for example and the caller did not
2460 * intend to feed us a patch but wanted to continue
2463 if (argc
|| (resume
.mode
== RESUME_FALSE
&& !isatty(0)))
2464 die(_("previous rebase directory %s still exists but mbox given."),
2467 if (resume
.mode
== RESUME_FALSE
)
2468 resume
.mode
= RESUME_APPLY
;
2470 if (state
.signoff
== SIGNOFF_EXPLICIT
)
2471 am_append_signoff(&state
);
2473 struct strvec paths
= STRVEC_INIT
;
2477 * Handle stray state directory in the independent-run case. In
2478 * the --rebasing case, it is up to the caller to take care of
2479 * stray directories.
2481 if (file_exists(state
.dir
) && !state
.rebasing
) {
2482 if (resume
.mode
== RESUME_ABORT
|| resume
.mode
== RESUME_QUIT
) {
2484 am_state_release(&state
);
2488 die(_("Stray %s directory found.\n"
2489 "Use \"git am --abort\" to remove it."),
2494 die(_("Resolve operation not in progress, we are not resuming."));
2496 for (i
= 0; i
< argc
; i
++) {
2497 if (is_absolute_path(argv
[i
]) || !prefix
)
2498 strvec_push(&paths
, argv
[i
]);
2500 strvec_push(&paths
, mkpath("%s/%s", prefix
, argv
[i
]));
2503 if (state
.interactive
&& !paths
.nr
)
2504 die(_("interactive mode requires patches on the command line"));
2506 am_setup(&state
, patch_format
, paths
.v
, keep_cr
);
2508 strvec_clear(&paths
);
2511 switch (resume
.mode
) {
2518 case RESUME_RESOLVED
:
2519 case RESUME_ALLOW_EMPTY
:
2520 am_resolve(&state
, resume
.mode
== RESUME_ALLOW_EMPTY
? 1 : 0);
2532 case RESUME_SHOW_PATCH
:
2533 ret
= show_patch(&state
, resume
.sub_mode
);
2536 BUG("invalid resume value");
2539 am_state_release(&state
);