builtin-am: implement -k/--keep, --keep-non-patch
[git.git] / builtin / am.c
blob68dca2e8d3d2e7402d264dbc5e1b7d74ac685349
1 /*
2 * Builtin "git am"
4 * Based on git-am.sh by Junio C Hamano.
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "exec_cmd.h"
9 #include "parse-options.h"
10 #include "dir.h"
11 #include "run-command.h"
12 #include "quote.h"
13 #include "lockfile.h"
14 #include "cache-tree.h"
15 #include "refs.h"
16 #include "commit.h"
17 #include "diff.h"
18 #include "diffcore.h"
19 #include "unpack-trees.h"
20 #include "branch.h"
21 #include "sequencer.h"
22 #include "revision.h"
23 #include "merge-recursive.h"
24 #include "revision.h"
25 #include "log-tree.h"
27 /**
28 * Returns 1 if the file is empty or does not exist, 0 otherwise.
30 static int is_empty_file(const char *filename)
32 struct stat st;
34 if (stat(filename, &st) < 0) {
35 if (errno == ENOENT)
36 return 1;
37 die_errno(_("could not stat %s"), filename);
40 return !st.st_size;
43 /**
44 * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
46 static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
48 if (strbuf_getwholeline(sb, fp, '\n'))
49 return EOF;
50 if (sb->buf[sb->len - 1] == '\n') {
51 strbuf_setlen(sb, sb->len - 1);
52 if (sb->len > 0 && sb->buf[sb->len - 1] == '\r')
53 strbuf_setlen(sb, sb->len - 1);
55 return 0;
58 /**
59 * Returns the length of the first line of msg.
61 static int linelen(const char *msg)
63 return strchrnul(msg, '\n') - msg;
66 enum patch_format {
67 PATCH_FORMAT_UNKNOWN = 0,
68 PATCH_FORMAT_MBOX
71 enum keep_type {
72 KEEP_FALSE = 0,
73 KEEP_TRUE, /* pass -k flag to git-mailinfo */
74 KEEP_NON_PATCH /* pass -b flag to git-mailinfo */
77 struct am_state {
78 /* state directory path */
79 char *dir;
81 /* current and last patch numbers, 1-indexed */
82 int cur;
83 int last;
85 /* commit metadata and message */
86 char *author_name;
87 char *author_email;
88 char *author_date;
89 char *msg;
90 size_t msg_len;
92 /* number of digits in patch filename */
93 int prec;
95 /* various operating modes and command line options */
96 int threeway;
97 int quiet;
98 int signoff;
99 int utf8;
100 int keep; /* enum keep_type */
101 const char *resolvemsg;
102 int rebasing;
106 * Initializes am_state with the default values. The state directory is set to
107 * dir.
109 static void am_state_init(struct am_state *state, const char *dir)
111 memset(state, 0, sizeof(*state));
113 assert(dir);
114 state->dir = xstrdup(dir);
116 state->prec = 4;
118 state->utf8 = 1;
122 * Releases memory allocated by an am_state.
124 static void am_state_release(struct am_state *state)
126 free(state->dir);
127 free(state->author_name);
128 free(state->author_email);
129 free(state->author_date);
130 free(state->msg);
134 * Returns path relative to the am_state directory.
136 static inline const char *am_path(const struct am_state *state, const char *path)
138 return mkpath("%s/%s", state->dir, path);
142 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
143 * at the end.
145 static void say(const struct am_state *state, FILE *fp, const char *fmt, ...)
147 va_list ap;
149 va_start(ap, fmt);
150 if (!state->quiet) {
151 vfprintf(fp, fmt, ap);
152 putc('\n', fp);
154 va_end(ap);
158 * Returns 1 if there is an am session in progress, 0 otherwise.
160 static int am_in_progress(const struct am_state *state)
162 struct stat st;
164 if (lstat(state->dir, &st) < 0 || !S_ISDIR(st.st_mode))
165 return 0;
166 if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode))
167 return 0;
168 if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode))
169 return 0;
170 return 1;
174 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
175 * number of bytes read on success, -1 if the file does not exist. If `trim` is
176 * set, trailing whitespace will be removed.
178 static int read_state_file(struct strbuf *sb, const struct am_state *state,
179 const char *file, int trim)
181 strbuf_reset(sb);
183 if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) {
184 if (trim)
185 strbuf_trim(sb);
187 return sb->len;
190 if (errno == ENOENT)
191 return -1;
193 die_errno(_("could not read '%s'"), am_path(state, file));
197 * Reads a KEY=VALUE shell variable assignment from `fp`, returning the VALUE
198 * as a newly-allocated string. VALUE must be a quoted string, and the KEY must
199 * match `key`. Returns NULL on failure.
201 * This is used by read_author_script() to read the GIT_AUTHOR_* variables from
202 * the author-script.
204 static char *read_shell_var(FILE *fp, const char *key)
206 struct strbuf sb = STRBUF_INIT;
207 const char *str;
209 if (strbuf_getline(&sb, fp, '\n'))
210 goto fail;
212 if (!skip_prefix(sb.buf, key, &str))
213 goto fail;
215 if (!skip_prefix(str, "=", &str))
216 goto fail;
218 strbuf_remove(&sb, 0, str - sb.buf);
220 str = sq_dequote(sb.buf);
221 if (!str)
222 goto fail;
224 return strbuf_detach(&sb, NULL);
226 fail:
227 strbuf_release(&sb);
228 return NULL;
232 * Reads and parses the state directory's "author-script" file, and sets
233 * state->author_name, state->author_email and state->author_date accordingly.
234 * Returns 0 on success, -1 if the file could not be parsed.
236 * The author script is of the format:
238 * GIT_AUTHOR_NAME='$author_name'
239 * GIT_AUTHOR_EMAIL='$author_email'
240 * GIT_AUTHOR_DATE='$author_date'
242 * where $author_name, $author_email and $author_date are quoted. We are strict
243 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
244 * script, and thus if the file differs from what this function expects, it is
245 * better to bail out than to do something that the user does not expect.
247 static int read_author_script(struct am_state *state)
249 const char *filename = am_path(state, "author-script");
250 FILE *fp;
252 assert(!state->author_name);
253 assert(!state->author_email);
254 assert(!state->author_date);
256 fp = fopen(filename, "r");
257 if (!fp) {
258 if (errno == ENOENT)
259 return 0;
260 die_errno(_("could not open '%s' for reading"), filename);
263 state->author_name = read_shell_var(fp, "GIT_AUTHOR_NAME");
264 if (!state->author_name) {
265 fclose(fp);
266 return -1;
269 state->author_email = read_shell_var(fp, "GIT_AUTHOR_EMAIL");
270 if (!state->author_email) {
271 fclose(fp);
272 return -1;
275 state->author_date = read_shell_var(fp, "GIT_AUTHOR_DATE");
276 if (!state->author_date) {
277 fclose(fp);
278 return -1;
281 if (fgetc(fp) != EOF) {
282 fclose(fp);
283 return -1;
286 fclose(fp);
287 return 0;
291 * Saves state->author_name, state->author_email and state->author_date in the
292 * state directory's "author-script" file.
294 static void write_author_script(const struct am_state *state)
296 struct strbuf sb = STRBUF_INIT;
298 strbuf_addstr(&sb, "GIT_AUTHOR_NAME=");
299 sq_quote_buf(&sb, state->author_name);
300 strbuf_addch(&sb, '\n');
302 strbuf_addstr(&sb, "GIT_AUTHOR_EMAIL=");
303 sq_quote_buf(&sb, state->author_email);
304 strbuf_addch(&sb, '\n');
306 strbuf_addstr(&sb, "GIT_AUTHOR_DATE=");
307 sq_quote_buf(&sb, state->author_date);
308 strbuf_addch(&sb, '\n');
310 write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
312 strbuf_release(&sb);
316 * Reads the commit message from the state directory's "final-commit" file,
317 * setting state->msg to its contents and state->msg_len to the length of its
318 * contents in bytes.
320 * Returns 0 on success, -1 if the file does not exist.
322 static int read_commit_msg(struct am_state *state)
324 struct strbuf sb = STRBUF_INIT;
326 assert(!state->msg);
328 if (read_state_file(&sb, state, "final-commit", 0) < 0) {
329 strbuf_release(&sb);
330 return -1;
333 state->msg = strbuf_detach(&sb, &state->msg_len);
334 return 0;
338 * Saves state->msg in the state directory's "final-commit" file.
340 static void write_commit_msg(const struct am_state *state)
342 int fd;
343 const char *filename = am_path(state, "final-commit");
345 fd = xopen(filename, O_WRONLY | O_CREAT, 0666);
346 if (write_in_full(fd, state->msg, state->msg_len) < 0)
347 die_errno(_("could not write to %s"), filename);
348 close(fd);
352 * Loads state from disk.
354 static void am_load(struct am_state *state)
356 struct strbuf sb = STRBUF_INIT;
358 if (read_state_file(&sb, state, "next", 1) < 0)
359 die("BUG: state file 'next' does not exist");
360 state->cur = strtol(sb.buf, NULL, 10);
362 if (read_state_file(&sb, state, "last", 1) < 0)
363 die("BUG: state file 'last' does not exist");
364 state->last = strtol(sb.buf, NULL, 10);
366 if (read_author_script(state) < 0)
367 die(_("could not parse author script"));
369 read_commit_msg(state);
371 read_state_file(&sb, state, "threeway", 1);
372 state->threeway = !strcmp(sb.buf, "t");
374 read_state_file(&sb, state, "quiet", 1);
375 state->quiet = !strcmp(sb.buf, "t");
377 read_state_file(&sb, state, "sign", 1);
378 state->signoff = !strcmp(sb.buf, "t");
380 read_state_file(&sb, state, "utf8", 1);
381 state->utf8 = !strcmp(sb.buf, "t");
383 read_state_file(&sb, state, "keep", 1);
384 if (!strcmp(sb.buf, "t"))
385 state->keep = KEEP_TRUE;
386 else if (!strcmp(sb.buf, "b"))
387 state->keep = KEEP_NON_PATCH;
388 else
389 state->keep = KEEP_FALSE;
391 state->rebasing = !!file_exists(am_path(state, "rebasing"));
393 strbuf_release(&sb);
397 * Removes the am_state directory, forcefully terminating the current am
398 * session.
400 static void am_destroy(const struct am_state *state)
402 struct strbuf sb = STRBUF_INIT;
404 strbuf_addstr(&sb, state->dir);
405 remove_dir_recursively(&sb, 0);
406 strbuf_release(&sb);
410 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
411 * non-indented lines and checking if they look like they begin with valid
412 * header field names.
414 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
416 static int is_mail(FILE *fp)
418 const char *header_regex = "^[!-9;-~]+:";
419 struct strbuf sb = STRBUF_INIT;
420 regex_t regex;
421 int ret = 1;
423 if (fseek(fp, 0L, SEEK_SET))
424 die_errno(_("fseek failed"));
426 if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
427 die("invalid pattern: %s", header_regex);
429 while (!strbuf_getline_crlf(&sb, fp)) {
430 if (!sb.len)
431 break; /* End of header */
433 /* Ignore indented folded lines */
434 if (*sb.buf == '\t' || *sb.buf == ' ')
435 continue;
437 /* It's a header if it matches header_regex */
438 if (regexec(&regex, sb.buf, 0, NULL, 0)) {
439 ret = 0;
440 goto done;
444 done:
445 regfree(&regex);
446 strbuf_release(&sb);
447 return ret;
451 * Attempts to detect the patch_format of the patches contained in `paths`,
452 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
453 * detection fails.
455 static int detect_patch_format(const char **paths)
457 enum patch_format ret = PATCH_FORMAT_UNKNOWN;
458 struct strbuf l1 = STRBUF_INIT;
459 FILE *fp;
462 * We default to mbox format if input is from stdin and for directories
464 if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
465 return PATCH_FORMAT_MBOX;
468 * Otherwise, check the first few lines of the first patch, starting
469 * from the first non-blank line, to try to detect its format.
472 fp = xfopen(*paths, "r");
474 while (!strbuf_getline_crlf(&l1, fp)) {
475 if (l1.len)
476 break;
479 if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) {
480 ret = PATCH_FORMAT_MBOX;
481 goto done;
484 if (l1.len && is_mail(fp)) {
485 ret = PATCH_FORMAT_MBOX;
486 goto done;
489 done:
490 fclose(fp);
491 strbuf_release(&l1);
492 return ret;
496 * Splits out individual email patches from `paths`, where each path is either
497 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
499 static int split_mail_mbox(struct am_state *state, const char **paths)
501 struct child_process cp = CHILD_PROCESS_INIT;
502 struct strbuf last = STRBUF_INIT;
504 cp.git_cmd = 1;
505 argv_array_push(&cp.args, "mailsplit");
506 argv_array_pushf(&cp.args, "-d%d", state->prec);
507 argv_array_pushf(&cp.args, "-o%s", state->dir);
508 argv_array_push(&cp.args, "-b");
509 argv_array_push(&cp.args, "--");
510 argv_array_pushv(&cp.args, paths);
512 if (capture_command(&cp, &last, 8))
513 return -1;
515 state->cur = 1;
516 state->last = strtol(last.buf, NULL, 10);
518 return 0;
522 * Splits a list of files/directories into individual email patches. Each path
523 * in `paths` must be a file/directory that is formatted according to
524 * `patch_format`.
526 * Once split out, the individual email patches will be stored in the state
527 * directory, with each patch's filename being its index, padded to state->prec
528 * digits.
530 * state->cur will be set to the index of the first mail, and state->last will
531 * be set to the index of the last mail.
533 * Returns 0 on success, -1 on failure.
535 static int split_mail(struct am_state *state, enum patch_format patch_format,
536 const char **paths)
538 switch (patch_format) {
539 case PATCH_FORMAT_MBOX:
540 return split_mail_mbox(state, paths);
541 default:
542 die("BUG: invalid patch_format");
544 return -1;
548 * Setup a new am session for applying patches
550 static void am_setup(struct am_state *state, enum patch_format patch_format,
551 const char **paths)
553 unsigned char curr_head[GIT_SHA1_RAWSZ];
554 const char *str;
556 if (!patch_format)
557 patch_format = detect_patch_format(paths);
559 if (!patch_format) {
560 fprintf_ln(stderr, _("Patch format detection failed."));
561 exit(128);
564 if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
565 die_errno(_("failed to create directory '%s'"), state->dir);
567 if (split_mail(state, patch_format, paths) < 0) {
568 am_destroy(state);
569 die(_("Failed to split patches."));
572 if (state->rebasing)
573 state->threeway = 1;
575 write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
577 write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
579 write_file(am_path(state, "sign"), 1, state->signoff ? "t" : "f");
581 write_file(am_path(state, "utf8"), 1, state->utf8 ? "t" : "f");
583 switch (state->keep) {
584 case KEEP_FALSE:
585 str = "f";
586 break;
587 case KEEP_TRUE:
588 str = "t";
589 break;
590 case KEEP_NON_PATCH:
591 str = "b";
592 break;
593 default:
594 die("BUG: invalid value for state->keep");
597 write_file(am_path(state, "keep"), 1, "%s", str);
599 if (state->rebasing)
600 write_file(am_path(state, "rebasing"), 1, "%s", "");
601 else
602 write_file(am_path(state, "applying"), 1, "%s", "");
604 if (!get_sha1("HEAD", curr_head)) {
605 write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
606 if (!state->rebasing)
607 update_ref("am", "ORIG_HEAD", curr_head, NULL, 0,
608 UPDATE_REFS_DIE_ON_ERR);
609 } else {
610 write_file(am_path(state, "abort-safety"), 1, "%s", "");
611 if (!state->rebasing)
612 delete_ref("ORIG_HEAD", NULL, 0);
616 * NOTE: Since the "next" and "last" files determine if an am_state
617 * session is in progress, they should be written last.
620 write_file(am_path(state, "next"), 1, "%d", state->cur);
622 write_file(am_path(state, "last"), 1, "%d", state->last);
626 * Increments the patch pointer, and cleans am_state for the application of the
627 * next patch.
629 static void am_next(struct am_state *state)
631 unsigned char head[GIT_SHA1_RAWSZ];
633 free(state->author_name);
634 state->author_name = NULL;
636 free(state->author_email);
637 state->author_email = NULL;
639 free(state->author_date);
640 state->author_date = NULL;
642 free(state->msg);
643 state->msg = NULL;
644 state->msg_len = 0;
646 unlink(am_path(state, "author-script"));
647 unlink(am_path(state, "final-commit"));
649 if (!get_sha1("HEAD", head))
650 write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
651 else
652 write_file(am_path(state, "abort-safety"), 1, "%s", "");
654 state->cur++;
655 write_file(am_path(state, "next"), 1, "%d", state->cur);
659 * Returns the filename of the current patch email.
661 static const char *msgnum(const struct am_state *state)
663 static struct strbuf sb = STRBUF_INIT;
665 strbuf_reset(&sb);
666 strbuf_addf(&sb, "%0*d", state->prec, state->cur);
668 return sb.buf;
672 * Refresh and write index.
674 static void refresh_and_write_cache(void)
676 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
678 hold_locked_index(lock_file, 1);
679 refresh_cache(REFRESH_QUIET);
680 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
681 die(_("unable to write index file"));
685 * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
686 * branch, returns 1 if there are entries in the index, 0 otherwise. If an
687 * strbuf is provided, the space-separated list of files that differ will be
688 * appended to it.
690 static int index_has_changes(struct strbuf *sb)
692 unsigned char head[GIT_SHA1_RAWSZ];
693 int i;
695 if (!get_sha1_tree("HEAD", head)) {
696 struct diff_options opt;
698 diff_setup(&opt);
699 DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
700 if (!sb)
701 DIFF_OPT_SET(&opt, QUICK);
702 do_diff_cache(head, &opt);
703 diffcore_std(&opt);
704 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
705 if (i)
706 strbuf_addch(sb, ' ');
707 strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
709 diff_flush(&opt);
710 return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0;
711 } else {
712 for (i = 0; sb && i < active_nr; i++) {
713 if (i)
714 strbuf_addch(sb, ' ');
715 strbuf_addstr(sb, active_cache[i]->name);
717 return !!active_nr;
722 * Dies with a user-friendly message on how to proceed after resolving the
723 * problem. This message can be overridden with state->resolvemsg.
725 static void NORETURN die_user_resolve(const struct am_state *state)
727 if (state->resolvemsg) {
728 printf_ln("%s", state->resolvemsg);
729 } else {
730 const char *cmdline = "git am";
732 printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline);
733 printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline);
734 printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline);
737 exit(128);
741 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
742 * state->msg will be set to the patch message. state->author_name,
743 * state->author_email and state->author_date will be set to the patch author's
744 * name, email and date respectively. The patch body will be written to the
745 * state directory's "patch" file.
747 * Returns 1 if the patch should be skipped, 0 otherwise.
749 static int parse_mail(struct am_state *state, const char *mail)
751 FILE *fp;
752 struct child_process cp = CHILD_PROCESS_INIT;
753 struct strbuf sb = STRBUF_INIT;
754 struct strbuf msg = STRBUF_INIT;
755 struct strbuf author_name = STRBUF_INIT;
756 struct strbuf author_date = STRBUF_INIT;
757 struct strbuf author_email = STRBUF_INIT;
758 int ret = 0;
760 cp.git_cmd = 1;
761 cp.in = xopen(mail, O_RDONLY, 0);
762 cp.out = xopen(am_path(state, "info"), O_WRONLY | O_CREAT, 0777);
764 argv_array_push(&cp.args, "mailinfo");
765 argv_array_push(&cp.args, state->utf8 ? "-u" : "-n");
767 switch (state->keep) {
768 case KEEP_FALSE:
769 break;
770 case KEEP_TRUE:
771 argv_array_push(&cp.args, "-k");
772 break;
773 case KEEP_NON_PATCH:
774 argv_array_push(&cp.args, "-b");
775 break;
776 default:
777 die("BUG: invalid value for state->keep");
780 argv_array_push(&cp.args, am_path(state, "msg"));
781 argv_array_push(&cp.args, am_path(state, "patch"));
783 if (run_command(&cp) < 0)
784 die("could not parse patch");
786 close(cp.in);
787 close(cp.out);
789 /* Extract message and author information */
790 fp = xfopen(am_path(state, "info"), "r");
791 while (!strbuf_getline(&sb, fp, '\n')) {
792 const char *x;
794 if (skip_prefix(sb.buf, "Subject: ", &x)) {
795 if (msg.len)
796 strbuf_addch(&msg, '\n');
797 strbuf_addstr(&msg, x);
798 } else if (skip_prefix(sb.buf, "Author: ", &x))
799 strbuf_addstr(&author_name, x);
800 else if (skip_prefix(sb.buf, "Email: ", &x))
801 strbuf_addstr(&author_email, x);
802 else if (skip_prefix(sb.buf, "Date: ", &x))
803 strbuf_addstr(&author_date, x);
805 fclose(fp);
807 /* Skip pine's internal folder data */
808 if (!strcmp(author_name.buf, "Mail System Internal Data")) {
809 ret = 1;
810 goto finish;
813 if (is_empty_file(am_path(state, "patch"))) {
814 printf_ln(_("Patch is empty. Was it split wrong?"));
815 die_user_resolve(state);
818 strbuf_addstr(&msg, "\n\n");
819 if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
820 die_errno(_("could not read '%s'"), am_path(state, "msg"));
821 stripspace(&msg, 0);
823 if (state->signoff)
824 append_signoff(&msg, 0, 0);
826 assert(!state->author_name);
827 state->author_name = strbuf_detach(&author_name, NULL);
829 assert(!state->author_email);
830 state->author_email = strbuf_detach(&author_email, NULL);
832 assert(!state->author_date);
833 state->author_date = strbuf_detach(&author_date, NULL);
835 assert(!state->msg);
836 state->msg = strbuf_detach(&msg, &state->msg_len);
838 finish:
839 strbuf_release(&msg);
840 strbuf_release(&author_date);
841 strbuf_release(&author_email);
842 strbuf_release(&author_name);
843 strbuf_release(&sb);
844 return ret;
848 * Sets commit_id to the commit hash where the mail was generated from.
849 * Returns 0 on success, -1 on failure.
851 static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
853 struct strbuf sb = STRBUF_INIT;
854 FILE *fp = xfopen(mail, "r");
855 const char *x;
857 if (strbuf_getline(&sb, fp, '\n'))
858 return -1;
860 if (!skip_prefix(sb.buf, "From ", &x))
861 return -1;
863 if (get_sha1_hex(x, commit_id) < 0)
864 return -1;
866 strbuf_release(&sb);
867 fclose(fp);
868 return 0;
872 * Sets state->msg, state->author_name, state->author_email, state->author_date
873 * to the commit's respective info.
875 static void get_commit_info(struct am_state *state, struct commit *commit)
877 const char *buffer, *ident_line, *author_date, *msg;
878 size_t ident_len;
879 struct ident_split ident_split;
880 struct strbuf sb = STRBUF_INIT;
882 buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding());
884 ident_line = find_commit_header(buffer, "author", &ident_len);
886 if (split_ident_line(&ident_split, ident_line, ident_len) < 0) {
887 strbuf_add(&sb, ident_line, ident_len);
888 die(_("invalid ident line: %s"), sb.buf);
891 assert(!state->author_name);
892 if (ident_split.name_begin) {
893 strbuf_add(&sb, ident_split.name_begin,
894 ident_split.name_end - ident_split.name_begin);
895 state->author_name = strbuf_detach(&sb, NULL);
896 } else
897 state->author_name = xstrdup("");
899 assert(!state->author_email);
900 if (ident_split.mail_begin) {
901 strbuf_add(&sb, ident_split.mail_begin,
902 ident_split.mail_end - ident_split.mail_begin);
903 state->author_email = strbuf_detach(&sb, NULL);
904 } else
905 state->author_email = xstrdup("");
907 author_date = show_ident_date(&ident_split, DATE_MODE(NORMAL));
908 strbuf_addstr(&sb, author_date);
909 assert(!state->author_date);
910 state->author_date = strbuf_detach(&sb, NULL);
912 assert(!state->msg);
913 msg = strstr(buffer, "\n\n");
914 if (!msg)
915 die(_("unable to parse commit %s"), sha1_to_hex(commit->object.sha1));
916 state->msg = xstrdup(msg + 2);
917 state->msg_len = strlen(state->msg);
921 * Writes `commit` as a patch to the state directory's "patch" file.
923 static void write_commit_patch(const struct am_state *state, struct commit *commit)
925 struct rev_info rev_info;
926 FILE *fp;
928 fp = xfopen(am_path(state, "patch"), "w");
929 init_revisions(&rev_info, NULL);
930 rev_info.diff = 1;
931 rev_info.abbrev = 0;
932 rev_info.disable_stdin = 1;
933 rev_info.show_root_diff = 1;
934 rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
935 rev_info.no_commit_id = 1;
936 DIFF_OPT_SET(&rev_info.diffopt, BINARY);
937 DIFF_OPT_SET(&rev_info.diffopt, FULL_INDEX);
938 rev_info.diffopt.use_color = 0;
939 rev_info.diffopt.file = fp;
940 rev_info.diffopt.close_file = 1;
941 add_pending_object(&rev_info, &commit->object, "");
942 diff_setup_done(&rev_info.diffopt);
943 log_tree_commit(&rev_info, commit);
947 * Like parse_mail(), but parses the mail by looking up its commit ID
948 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
949 * of patches.
951 * Will always return 0 as the patch should never be skipped.
953 static int parse_mail_rebase(struct am_state *state, const char *mail)
955 struct commit *commit;
956 unsigned char commit_sha1[GIT_SHA1_RAWSZ];
958 if (get_mail_commit_sha1(commit_sha1, mail) < 0)
959 die(_("could not parse %s"), mail);
961 commit = lookup_commit_or_die(commit_sha1, mail);
963 get_commit_info(state, commit);
965 write_commit_patch(state, commit);
967 return 0;
971 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
972 * `index_file` is not NULL, the patch will be applied to that index.
974 static int run_apply(const struct am_state *state, const char *index_file)
976 struct child_process cp = CHILD_PROCESS_INIT;
978 cp.git_cmd = 1;
980 if (index_file)
981 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
984 * If we are allowed to fall back on 3-way merge, don't give false
985 * errors during the initial attempt.
987 if (state->threeway && !index_file) {
988 cp.no_stdout = 1;
989 cp.no_stderr = 1;
992 argv_array_push(&cp.args, "apply");
994 if (index_file)
995 argv_array_push(&cp.args, "--cached");
996 else
997 argv_array_push(&cp.args, "--index");
999 argv_array_push(&cp.args, am_path(state, "patch"));
1001 if (run_command(&cp))
1002 return -1;
1004 /* Reload index as git-apply will have modified it. */
1005 discard_cache();
1006 read_cache_from(index_file ? index_file : get_index_file());
1008 return 0;
1012 * Builds an index that contains just the blobs needed for a 3way merge.
1014 static int build_fake_ancestor(const struct am_state *state, const char *index_file)
1016 struct child_process cp = CHILD_PROCESS_INIT;
1018 cp.git_cmd = 1;
1019 argv_array_push(&cp.args, "apply");
1020 argv_array_pushf(&cp.args, "--build-fake-ancestor=%s", index_file);
1021 argv_array_push(&cp.args, am_path(state, "patch"));
1023 if (run_command(&cp))
1024 return -1;
1026 return 0;
1030 * Attempt a threeway merge, using index_path as the temporary index.
1032 static int fall_back_threeway(const struct am_state *state, const char *index_path)
1034 unsigned char orig_tree[GIT_SHA1_RAWSZ], his_tree[GIT_SHA1_RAWSZ],
1035 our_tree[GIT_SHA1_RAWSZ];
1036 const unsigned char *bases[1] = {orig_tree};
1037 struct merge_options o;
1038 struct commit *result;
1039 char *his_tree_name;
1041 if (get_sha1("HEAD", our_tree) < 0)
1042 hashcpy(our_tree, EMPTY_TREE_SHA1_BIN);
1044 if (build_fake_ancestor(state, index_path))
1045 return error("could not build fake ancestor");
1047 discard_cache();
1048 read_cache_from(index_path);
1050 if (write_index_as_tree(orig_tree, &the_index, index_path, 0, NULL))
1051 return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1053 say(state, stdout, _("Using index info to reconstruct a base tree..."));
1055 if (!state->quiet) {
1057 * List paths that needed 3-way fallback, so that the user can
1058 * review them with extra care to spot mismerges.
1060 struct rev_info rev_info;
1061 const char *diff_filter_str = "--diff-filter=AM";
1063 init_revisions(&rev_info, NULL);
1064 rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
1065 diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
1066 add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
1067 diff_setup_done(&rev_info.diffopt);
1068 run_diff_index(&rev_info, 1);
1071 if (run_apply(state, index_path))
1072 return error(_("Did you hand edit your patch?\n"
1073 "It does not apply to blobs recorded in its index."));
1075 if (write_index_as_tree(his_tree, &the_index, index_path, 0, NULL))
1076 return error("could not write tree");
1078 say(state, stdout, _("Falling back to patching base and 3-way merge..."));
1080 discard_cache();
1081 read_cache();
1084 * This is not so wrong. Depending on which base we picked, orig_tree
1085 * may be wildly different from ours, but his_tree has the same set of
1086 * wildly different changes in parts the patch did not touch, so
1087 * recursive ends up canceling them, saying that we reverted all those
1088 * changes.
1091 init_merge_options(&o);
1093 o.branch1 = "HEAD";
1094 his_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
1095 o.branch2 = his_tree_name;
1097 if (state->quiet)
1098 o.verbosity = 0;
1100 if (merge_recursive_generic(&o, our_tree, his_tree, 1, bases, &result)) {
1101 free(his_tree_name);
1102 return error(_("Failed to merge in the changes."));
1105 free(his_tree_name);
1106 return 0;
1110 * Commits the current index with state->msg as the commit message and
1111 * state->author_name, state->author_email and state->author_date as the author
1112 * information.
1114 static void do_commit(const struct am_state *state)
1116 unsigned char tree[GIT_SHA1_RAWSZ], parent[GIT_SHA1_RAWSZ],
1117 commit[GIT_SHA1_RAWSZ];
1118 unsigned char *ptr;
1119 struct commit_list *parents = NULL;
1120 const char *reflog_msg, *author;
1121 struct strbuf sb = STRBUF_INIT;
1123 if (write_cache_as_tree(tree, 0, NULL))
1124 die(_("git write-tree failed to write a tree"));
1126 if (!get_sha1_commit("HEAD", parent)) {
1127 ptr = parent;
1128 commit_list_insert(lookup_commit(parent), &parents);
1129 } else {
1130 ptr = NULL;
1131 say(state, stderr, _("applying to an empty history"));
1134 author = fmt_ident(state->author_name, state->author_email,
1135 state->author_date, IDENT_STRICT);
1137 if (commit_tree(state->msg, state->msg_len, tree, parents, commit,
1138 author, NULL))
1139 die(_("failed to write commit object"));
1141 reflog_msg = getenv("GIT_REFLOG_ACTION");
1142 if (!reflog_msg)
1143 reflog_msg = "am";
1145 strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
1146 state->msg);
1148 update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
1150 strbuf_release(&sb);
1154 * Validates the am_state for resuming -- the "msg" and authorship fields must
1155 * be filled up.
1157 static void validate_resume_state(const struct am_state *state)
1159 if (!state->msg)
1160 die(_("cannot resume: %s does not exist."),
1161 am_path(state, "final-commit"));
1163 if (!state->author_name || !state->author_email || !state->author_date)
1164 die(_("cannot resume: %s does not exist."),
1165 am_path(state, "author-script"));
1169 * Applies all queued mail.
1171 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
1172 * well as the state directory's "patch" file is used as-is for applying the
1173 * patch and committing it.
1175 static void am_run(struct am_state *state, int resume)
1177 const char *argv_gc_auto[] = {"gc", "--auto", NULL};
1178 struct strbuf sb = STRBUF_INIT;
1180 unlink(am_path(state, "dirtyindex"));
1182 refresh_and_write_cache();
1184 if (index_has_changes(&sb)) {
1185 write_file(am_path(state, "dirtyindex"), 1, "t");
1186 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
1189 strbuf_release(&sb);
1191 while (state->cur <= state->last) {
1192 const char *mail = am_path(state, msgnum(state));
1193 int apply_status;
1195 if (!file_exists(mail))
1196 goto next;
1198 if (resume) {
1199 validate_resume_state(state);
1200 resume = 0;
1201 } else {
1202 int skip;
1204 if (state->rebasing)
1205 skip = parse_mail_rebase(state, mail);
1206 else
1207 skip = parse_mail(state, mail);
1209 if (skip)
1210 goto next; /* mail should be skipped */
1212 write_author_script(state);
1213 write_commit_msg(state);
1216 say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1218 apply_status = run_apply(state, NULL);
1220 if (apply_status && state->threeway) {
1221 struct strbuf sb = STRBUF_INIT;
1223 strbuf_addstr(&sb, am_path(state, "patch-merge-index"));
1224 apply_status = fall_back_threeway(state, sb.buf);
1225 strbuf_release(&sb);
1228 * Applying the patch to an earlier tree and merging
1229 * the result may have produced the same tree as ours.
1231 if (!apply_status && !index_has_changes(NULL)) {
1232 say(state, stdout, _("No changes -- Patch already applied."));
1233 goto next;
1237 if (apply_status) {
1238 int advice_amworkdir = 1;
1240 printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
1241 linelen(state->msg), state->msg);
1243 git_config_get_bool("advice.amworkdir", &advice_amworkdir);
1245 if (advice_amworkdir)
1246 printf_ln(_("The copy of the patch that failed is found in: %s"),
1247 am_path(state, "patch"));
1249 die_user_resolve(state);
1252 do_commit(state);
1254 next:
1255 am_next(state);
1259 * In rebasing mode, it's up to the caller to take care of
1260 * housekeeping.
1262 if (!state->rebasing) {
1263 am_destroy(state);
1264 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1269 * Resume the current am session after patch application failure. The user did
1270 * all the hard work, and we do not have to do any patch application. Just
1271 * trust and commit what the user has in the index and working tree.
1273 static void am_resolve(struct am_state *state)
1275 validate_resume_state(state);
1277 say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1279 if (!index_has_changes(NULL)) {
1280 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1281 "If there is nothing left to stage, chances are that something else\n"
1282 "already introduced the same changes; you might want to skip this patch."));
1283 die_user_resolve(state);
1286 if (unmerged_cache()) {
1287 printf_ln(_("You still have unmerged paths in your index.\n"
1288 "Did you forget to use 'git add'?"));
1289 die_user_resolve(state);
1292 do_commit(state);
1294 am_next(state);
1295 am_run(state, 0);
1299 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1300 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1301 * failure.
1303 static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1305 struct lock_file *lock_file;
1306 struct unpack_trees_options opts;
1307 struct tree_desc t[2];
1309 if (parse_tree(head) || parse_tree(remote))
1310 return -1;
1312 lock_file = xcalloc(1, sizeof(struct lock_file));
1313 hold_locked_index(lock_file, 1);
1315 refresh_cache(REFRESH_QUIET);
1317 memset(&opts, 0, sizeof(opts));
1318 opts.head_idx = 1;
1319 opts.src_index = &the_index;
1320 opts.dst_index = &the_index;
1321 opts.update = 1;
1322 opts.merge = 1;
1323 opts.reset = reset;
1324 opts.fn = twoway_merge;
1325 init_tree_desc(&t[0], head->buffer, head->size);
1326 init_tree_desc(&t[1], remote->buffer, remote->size);
1328 if (unpack_trees(2, t, &opts)) {
1329 rollback_lock_file(lock_file);
1330 return -1;
1333 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1334 die(_("unable to write new index file"));
1336 return 0;
1340 * Clean the index without touching entries that are not modified between
1341 * `head` and `remote`.
1343 static int clean_index(const unsigned char *head, const unsigned char *remote)
1345 struct lock_file *lock_file;
1346 struct tree *head_tree, *remote_tree, *index_tree;
1347 unsigned char index[GIT_SHA1_RAWSZ];
1348 struct pathspec pathspec;
1350 head_tree = parse_tree_indirect(head);
1351 if (!head_tree)
1352 return error(_("Could not parse object '%s'."), sha1_to_hex(head));
1354 remote_tree = parse_tree_indirect(remote);
1355 if (!remote_tree)
1356 return error(_("Could not parse object '%s'."), sha1_to_hex(remote));
1358 read_cache_unmerged();
1360 if (fast_forward_to(head_tree, head_tree, 1))
1361 return -1;
1363 if (write_cache_as_tree(index, 0, NULL))
1364 return -1;
1366 index_tree = parse_tree_indirect(index);
1367 if (!index_tree)
1368 return error(_("Could not parse object '%s'."), sha1_to_hex(index));
1370 if (fast_forward_to(index_tree, remote_tree, 0))
1371 return -1;
1373 memset(&pathspec, 0, sizeof(pathspec));
1375 lock_file = xcalloc(1, sizeof(struct lock_file));
1376 hold_locked_index(lock_file, 1);
1378 if (read_tree(remote_tree, 0, &pathspec)) {
1379 rollback_lock_file(lock_file);
1380 return -1;
1383 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1384 die(_("unable to write new index file"));
1386 remove_branch_state();
1388 return 0;
1392 * Resume the current am session by skipping the current patch.
1394 static void am_skip(struct am_state *state)
1396 unsigned char head[GIT_SHA1_RAWSZ];
1398 if (get_sha1("HEAD", head))
1399 hashcpy(head, EMPTY_TREE_SHA1_BIN);
1401 if (clean_index(head, head))
1402 die(_("failed to clean index"));
1404 am_next(state);
1405 am_run(state, 0);
1409 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
1411 * It is not safe to reset HEAD when:
1412 * 1. git-am previously failed because the index was dirty.
1413 * 2. HEAD has moved since git-am previously failed.
1415 static int safe_to_abort(const struct am_state *state)
1417 struct strbuf sb = STRBUF_INIT;
1418 unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
1420 if (file_exists(am_path(state, "dirtyindex")))
1421 return 0;
1423 if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
1424 if (get_sha1_hex(sb.buf, abort_safety))
1425 die(_("could not parse %s"), am_path(state, "abort_safety"));
1426 } else
1427 hashclr(abort_safety);
1429 if (get_sha1("HEAD", head))
1430 hashclr(head);
1432 if (!hashcmp(head, abort_safety))
1433 return 1;
1435 error(_("You seem to have moved HEAD since the last 'am' failure.\n"
1436 "Not rewinding to ORIG_HEAD"));
1438 return 0;
1442 * Aborts the current am session if it is safe to do so.
1444 static void am_abort(struct am_state *state)
1446 unsigned char curr_head[GIT_SHA1_RAWSZ], orig_head[GIT_SHA1_RAWSZ];
1447 int has_curr_head, has_orig_head;
1448 char *curr_branch;
1450 if (!safe_to_abort(state)) {
1451 am_destroy(state);
1452 return;
1455 curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
1456 has_curr_head = !is_null_sha1(curr_head);
1457 if (!has_curr_head)
1458 hashcpy(curr_head, EMPTY_TREE_SHA1_BIN);
1460 has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
1461 if (!has_orig_head)
1462 hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
1464 clean_index(curr_head, orig_head);
1466 if (has_orig_head)
1467 update_ref("am --abort", "HEAD", orig_head,
1468 has_curr_head ? curr_head : NULL, 0,
1469 UPDATE_REFS_DIE_ON_ERR);
1470 else if (curr_branch)
1471 delete_ref(curr_branch, NULL, REF_NODEREF);
1473 free(curr_branch);
1474 am_destroy(state);
1478 * parse_options() callback that validates and sets opt->value to the
1479 * PATCH_FORMAT_* enum value corresponding to `arg`.
1481 static int parse_opt_patchformat(const struct option *opt, const char *arg, int unset)
1483 int *opt_value = opt->value;
1485 if (!strcmp(arg, "mbox"))
1486 *opt_value = PATCH_FORMAT_MBOX;
1487 else
1488 return error(_("Invalid value for --patch-format: %s"), arg);
1489 return 0;
1492 enum resume_mode {
1493 RESUME_FALSE = 0,
1494 RESUME_APPLY,
1495 RESUME_RESOLVED,
1496 RESUME_SKIP,
1497 RESUME_ABORT
1500 int cmd_am(int argc, const char **argv, const char *prefix)
1502 struct am_state state;
1503 int patch_format = PATCH_FORMAT_UNKNOWN;
1504 enum resume_mode resume = RESUME_FALSE;
1506 const char * const usage[] = {
1507 N_("git am [options] [(<mbox>|<Maildir>)...]"),
1508 N_("git am [options] (--continue | --skip | --abort)"),
1509 NULL
1512 struct option options[] = {
1513 OPT_BOOL('3', "3way", &state.threeway,
1514 N_("allow fall back on 3way merging if needed")),
1515 OPT__QUIET(&state.quiet, N_("be quiet")),
1516 OPT_BOOL('s', "signoff", &state.signoff,
1517 N_("add a Signed-off-by line to the commit message")),
1518 OPT_BOOL('u', "utf8", &state.utf8,
1519 N_("recode into utf8 (default)")),
1520 OPT_SET_INT('k', "keep", &state.keep,
1521 N_("pass -k flag to git-mailinfo"), KEEP_TRUE),
1522 OPT_SET_INT(0, "keep-non-patch", &state.keep,
1523 N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH),
1524 OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
1525 N_("format the patch(es) are in"),
1526 parse_opt_patchformat),
1527 OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
1528 N_("override error message when patch failure occurs")),
1529 OPT_CMDMODE(0, "continue", &resume,
1530 N_("continue applying patches after resolving a conflict"),
1531 RESUME_RESOLVED),
1532 OPT_CMDMODE('r', "resolved", &resume,
1533 N_("synonyms for --continue"),
1534 RESUME_RESOLVED),
1535 OPT_CMDMODE(0, "skip", &resume,
1536 N_("skip the current patch"),
1537 RESUME_SKIP),
1538 OPT_CMDMODE(0, "abort", &resume,
1539 N_("restore the original branch and abort the patching operation."),
1540 RESUME_ABORT),
1541 OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing,
1542 N_("(internal use for git-rebase)")),
1543 OPT_END()
1547 * NEEDSWORK: Once all the features of git-am.sh have been
1548 * re-implemented in builtin/am.c, this preamble can be removed.
1550 if (!getenv("_GIT_USE_BUILTIN_AM")) {
1551 const char *path = mkpath("%s/git-am", git_exec_path());
1553 if (sane_execvp(path, (char **)argv) < 0)
1554 die_errno("could not exec %s", path);
1555 } else {
1556 prefix = setup_git_directory();
1557 trace_repo_setup(prefix);
1558 setup_work_tree();
1561 git_config(git_default_config, NULL);
1563 am_state_init(&state, git_path("rebase-apply"));
1565 argc = parse_options(argc, argv, prefix, options, usage, 0);
1567 if (read_index_preload(&the_index, NULL) < 0)
1568 die(_("failed to read the index"));
1570 if (am_in_progress(&state)) {
1572 * Catch user error to feed us patches when there is a session
1573 * in progress:
1575 * 1. mbox path(s) are provided on the command-line.
1576 * 2. stdin is not a tty: the user is trying to feed us a patch
1577 * from standard input. This is somewhat unreliable -- stdin
1578 * could be /dev/null for example and the caller did not
1579 * intend to feed us a patch but wanted to continue
1580 * unattended.
1582 if (argc || (resume == RESUME_FALSE && !isatty(0)))
1583 die(_("previous rebase directory %s still exists but mbox given."),
1584 state.dir);
1586 if (resume == RESUME_FALSE)
1587 resume = RESUME_APPLY;
1589 am_load(&state);
1590 } else {
1591 struct argv_array paths = ARGV_ARRAY_INIT;
1592 int i;
1595 * Handle stray state directory in the independent-run case. In
1596 * the --rebasing case, it is up to the caller to take care of
1597 * stray directories.
1599 if (file_exists(state.dir) && !state.rebasing) {
1600 if (resume == RESUME_ABORT) {
1601 am_destroy(&state);
1602 am_state_release(&state);
1603 return 0;
1606 die(_("Stray %s directory found.\n"
1607 "Use \"git am --abort\" to remove it."),
1608 state.dir);
1611 if (resume)
1612 die(_("Resolve operation not in progress, we are not resuming."));
1614 for (i = 0; i < argc; i++) {
1615 if (is_absolute_path(argv[i]) || !prefix)
1616 argv_array_push(&paths, argv[i]);
1617 else
1618 argv_array_push(&paths, mkpath("%s/%s", prefix, argv[i]));
1621 am_setup(&state, patch_format, paths.argv);
1623 argv_array_clear(&paths);
1626 switch (resume) {
1627 case RESUME_FALSE:
1628 am_run(&state, 0);
1629 break;
1630 case RESUME_APPLY:
1631 am_run(&state, 1);
1632 break;
1633 case RESUME_RESOLVED:
1634 am_resolve(&state);
1635 break;
1636 case RESUME_SKIP:
1637 am_skip(&state);
1638 break;
1639 case RESUME_ABORT:
1640 am_abort(&state);
1641 break;
1642 default:
1643 die("BUG: invalid resume value");
1646 am_state_release(&state);
1648 return 0;