4 * Based on git-am.sh by Junio C Hamano.
9 #include "parse-options.h"
11 #include "run-command.h"
14 #include "cache-tree.h"
19 #include "unpack-trees.h"
21 #include "sequencer.h"
23 #include "merge-recursive.h"
26 #include "notes-utils.h"
30 * Returns 1 if the file is empty or does not exist, 0 otherwise.
32 static int is_empty_file(const char *filename)
36 if (stat(filename, &st) < 0) {
39 die_errno(_("could not stat %s"), filename);
46 * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
48 static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
50 if (strbuf_getwholeline(sb, fp, '\n'))
52 if (sb->buf[sb->len - 1] == '\n') {
53 strbuf_setlen(sb, sb->len - 1);
54 if (sb->len > 0 && sb->buf[sb->len - 1] == '\r')
55 strbuf_setlen(sb, sb->len - 1);
61 * Returns the length of the first line of msg.
63 static int linelen(const char *msg)
65 return strchrnul(msg, '\n') - msg;
69 * Returns true if `str` consists of only whitespace, false otherwise.
71 static int str_isspace(const char *str)
81 PATCH_FORMAT_UNKNOWN = 0,
88 KEEP_TRUE, /* pass -k flag to git-mailinfo */
89 KEEP_NON_PATCH /* pass -b flag to git-mailinfo */
94 SCISSORS_FALSE = 0, /* pass --no-scissors to git-mailinfo */
95 SCISSORS_TRUE /* pass --scissors to git-mailinfo */
99 /* state directory path */
102 /* current and last patch numbers, 1-indexed */
106 /* commit metadata and message */
113 /* when --rebasing, records the original commit the patch came from */
114 unsigned char orig_commit[GIT_SHA1_RAWSZ];
116 /* number of digits in patch filename */
119 /* various operating modes and command line options */
124 int keep; /* enum keep_type */
126 int scissors; /* enum scissors_type */
127 struct argv_array git_apply_opts;
128 const char *resolvemsg;
129 int committer_date_is_author_date;
131 int allow_rerere_autoupdate;
132 const char *sign_commit;
137 * Initializes am_state with the default values. The state directory is set to
140 static void am_state_init(struct am_state *state, const char *dir)
144 memset(state, 0, sizeof(*state));
147 state->dir = xstrdup(dir);
153 git_config_get_bool("am.messageid", &state->message_id);
155 state->scissors = SCISSORS_UNSET;
157 argv_array_init(&state->git_apply_opts);
159 if (!git_config_get_bool("commit.gpgsign", &gpgsign))
160 state->sign_commit = gpgsign ? "" : NULL;
164 * Releases memory allocated by an am_state.
166 static void am_state_release(struct am_state *state)
169 free(state->author_name);
170 free(state->author_email);
171 free(state->author_date);
173 argv_array_clear(&state->git_apply_opts);
177 * Returns path relative to the am_state directory.
179 static inline const char *am_path(const struct am_state *state, const char *path)
181 return mkpath("%s/%s", state->dir, path);
185 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
188 static void say(const struct am_state *state, FILE *fp, const char *fmt, ...)
194 vfprintf(fp, fmt, ap);
201 * Returns 1 if there is an am session in progress, 0 otherwise.
203 static int am_in_progress(const struct am_state *state)
207 if (lstat(state->dir, &st) < 0 || !S_ISDIR(st.st_mode))
209 if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode))
211 if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode))
217 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
218 * number of bytes read on success, -1 if the file does not exist. If `trim` is
219 * set, trailing whitespace will be removed.
221 static int read_state_file(struct strbuf *sb, const struct am_state *state,
222 const char *file, int trim)
226 if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) {
236 die_errno(_("could not read '%s'"), am_path(state, file));
240 * Reads a KEY=VALUE shell variable assignment from `fp`, returning the VALUE
241 * as a newly-allocated string. VALUE must be a quoted string, and the KEY must
242 * match `key`. Returns NULL on failure.
244 * This is used by read_author_script() to read the GIT_AUTHOR_* variables from
247 static char *read_shell_var(FILE *fp, const char *key)
249 struct strbuf sb = STRBUF_INIT;
252 if (strbuf_getline(&sb, fp, '\n'))
255 if (!skip_prefix(sb.buf, key, &str))
258 if (!skip_prefix(str, "=", &str))
261 strbuf_remove(&sb, 0, str - sb.buf);
263 str = sq_dequote(sb.buf);
267 return strbuf_detach(&sb, NULL);
275 * Reads and parses the state directory's "author-script" file, and sets
276 * state->author_name, state->author_email and state->author_date accordingly.
277 * Returns 0 on success, -1 if the file could not be parsed.
279 * The author script is of the format:
281 * GIT_AUTHOR_NAME='$author_name'
282 * GIT_AUTHOR_EMAIL='$author_email'
283 * GIT_AUTHOR_DATE='$author_date'
285 * where $author_name, $author_email and $author_date are quoted. We are strict
286 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
287 * script, and thus if the file differs from what this function expects, it is
288 * better to bail out than to do something that the user does not expect.
290 static int read_author_script(struct am_state *state)
292 const char *filename = am_path(state, "author-script");
295 assert(!state->author_name);
296 assert(!state->author_email);
297 assert(!state->author_date);
299 fp = fopen(filename, "r");
303 die_errno(_("could not open '%s' for reading"), filename);
306 state->author_name = read_shell_var(fp, "GIT_AUTHOR_NAME");
307 if (!state->author_name) {
312 state->author_email = read_shell_var(fp, "GIT_AUTHOR_EMAIL");
313 if (!state->author_email) {
318 state->author_date = read_shell_var(fp, "GIT_AUTHOR_DATE");
319 if (!state->author_date) {
324 if (fgetc(fp) != EOF) {
334 * Saves state->author_name, state->author_email and state->author_date in the
335 * state directory's "author-script" file.
337 static void write_author_script(const struct am_state *state)
339 struct strbuf sb = STRBUF_INIT;
341 strbuf_addstr(&sb, "GIT_AUTHOR_NAME=");
342 sq_quote_buf(&sb, state->author_name);
343 strbuf_addch(&sb, '\n');
345 strbuf_addstr(&sb, "GIT_AUTHOR_EMAIL=");
346 sq_quote_buf(&sb, state->author_email);
347 strbuf_addch(&sb, '\n');
349 strbuf_addstr(&sb, "GIT_AUTHOR_DATE=");
350 sq_quote_buf(&sb, state->author_date);
351 strbuf_addch(&sb, '\n');
353 write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
359 * Reads the commit message from the state directory's "final-commit" file,
360 * setting state->msg to its contents and state->msg_len to the length of its
363 * Returns 0 on success, -1 if the file does not exist.
365 static int read_commit_msg(struct am_state *state)
367 struct strbuf sb = STRBUF_INIT;
371 if (read_state_file(&sb, state, "final-commit", 0) < 0) {
376 state->msg = strbuf_detach(&sb, &state->msg_len);
381 * Saves state->msg in the state directory's "final-commit" file.
383 static void write_commit_msg(const struct am_state *state)
386 const char *filename = am_path(state, "final-commit");
388 fd = xopen(filename, O_WRONLY | O_CREAT, 0666);
389 if (write_in_full(fd, state->msg, state->msg_len) < 0)
390 die_errno(_("could not write to %s"), filename);
395 * Loads state from disk.
397 static void am_load(struct am_state *state)
399 struct strbuf sb = STRBUF_INIT;
401 if (read_state_file(&sb, state, "next", 1) < 0)
402 die("BUG: state file 'next' does not exist");
403 state->cur = strtol(sb.buf, NULL, 10);
405 if (read_state_file(&sb, state, "last", 1) < 0)
406 die("BUG: state file 'last' does not exist");
407 state->last = strtol(sb.buf, NULL, 10);
409 if (read_author_script(state) < 0)
410 die(_("could not parse author script"));
412 read_commit_msg(state);
414 if (read_state_file(&sb, state, "original-commit", 1) < 0)
415 hashclr(state->orig_commit);
416 else if (get_sha1_hex(sb.buf, state->orig_commit) < 0)
417 die(_("could not parse %s"), am_path(state, "original-commit"));
419 read_state_file(&sb, state, "threeway", 1);
420 state->threeway = !strcmp(sb.buf, "t");
422 read_state_file(&sb, state, "quiet", 1);
423 state->quiet = !strcmp(sb.buf, "t");
425 read_state_file(&sb, state, "sign", 1);
426 state->signoff = !strcmp(sb.buf, "t");
428 read_state_file(&sb, state, "utf8", 1);
429 state->utf8 = !strcmp(sb.buf, "t");
431 read_state_file(&sb, state, "keep", 1);
432 if (!strcmp(sb.buf, "t"))
433 state->keep = KEEP_TRUE;
434 else if (!strcmp(sb.buf, "b"))
435 state->keep = KEEP_NON_PATCH;
437 state->keep = KEEP_FALSE;
439 read_state_file(&sb, state, "messageid", 1);
440 state->message_id = !strcmp(sb.buf, "t");
442 read_state_file(&sb, state, "scissors", 1);
443 if (!strcmp(sb.buf, "t"))
444 state->scissors = SCISSORS_TRUE;
445 else if (!strcmp(sb.buf, "f"))
446 state->scissors = SCISSORS_FALSE;
448 state->scissors = SCISSORS_UNSET;
450 read_state_file(&sb, state, "apply-opt", 1);
451 argv_array_clear(&state->git_apply_opts);
452 if (sq_dequote_to_argv_array(sb.buf, &state->git_apply_opts) < 0)
453 die(_("could not parse %s"), am_path(state, "apply-opt"));
455 state->rebasing = !!file_exists(am_path(state, "rebasing"));
461 * Removes the am_state directory, forcefully terminating the current am
464 static void am_destroy(const struct am_state *state)
466 struct strbuf sb = STRBUF_INIT;
468 strbuf_addstr(&sb, state->dir);
469 remove_dir_recursively(&sb, 0);
474 * Runs applypatch-msg hook. Returns its exit code.
476 static int run_applypatch_msg_hook(struct am_state *state)
481 ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
486 if (read_commit_msg(state) < 0)
487 die(_("'%s' was deleted by the applypatch-msg hook"),
488 am_path(state, "final-commit"));
495 * Runs post-rewrite hook. Returns it exit code.
497 static int run_post_rewrite_hook(const struct am_state *state)
499 struct child_process cp = CHILD_PROCESS_INIT;
500 const char *hook = find_hook("post-rewrite");
506 argv_array_push(&cp.args, hook);
507 argv_array_push(&cp.args, "rebase");
509 cp.in = xopen(am_path(state, "rewritten"), O_RDONLY);
510 cp.stdout_to_stderr = 1;
512 ret = run_command(&cp);
519 * Reads the state directory's "rewritten" file, and copies notes from the old
520 * commits listed in the file to their rewritten commits.
522 * Returns 0 on success, -1 on failure.
524 static int copy_notes_for_rebase(const struct am_state *state)
526 struct notes_rewrite_cfg *c;
527 struct strbuf sb = STRBUF_INIT;
528 const char *invalid_line = _("Malformed input line: '%s'.");
529 const char *msg = "Notes added by 'git rebase'";
533 assert(state->rebasing);
535 c = init_copy_notes_for_rewrite("rebase");
539 fp = xfopen(am_path(state, "rewritten"), "r");
541 while (!strbuf_getline(&sb, fp, '\n')) {
542 unsigned char from_obj[GIT_SHA1_RAWSZ], to_obj[GIT_SHA1_RAWSZ];
544 if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
545 ret = error(invalid_line, sb.buf);
549 if (get_sha1_hex(sb.buf, from_obj)) {
550 ret = error(invalid_line, sb.buf);
554 if (sb.buf[GIT_SHA1_HEXSZ] != ' ') {
555 ret = error(invalid_line, sb.buf);
559 if (get_sha1_hex(sb.buf + GIT_SHA1_HEXSZ + 1, to_obj)) {
560 ret = error(invalid_line, sb.buf);
564 if (copy_note_for_rewrite(c, from_obj, to_obj))
565 ret = error(_("Failed to copy notes from '%s' to '%s'"),
566 sha1_to_hex(from_obj), sha1_to_hex(to_obj));
570 finish_copy_notes_for_rewrite(c, msg);
577 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
578 * non-indented lines and checking if they look like they begin with valid
579 * header field names.
581 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
583 static int is_mail(FILE *fp)
585 const char *header_regex = "^[!-9;-~]+:";
586 struct strbuf sb = STRBUF_INIT;
590 if (fseek(fp, 0L, SEEK_SET))
591 die_errno(_("fseek failed"));
593 if (regcomp(®ex, header_regex, REG_NOSUB | REG_EXTENDED))
594 die("invalid pattern: %s", header_regex);
596 while (!strbuf_getline_crlf(&sb, fp)) {
598 break; /* End of header */
600 /* Ignore indented folded lines */
601 if (*sb.buf == '\t' || *sb.buf == ' ')
604 /* It's a header if it matches header_regex */
605 if (regexec(®ex, sb.buf, 0, NULL, 0)) {
618 * Attempts to detect the patch_format of the patches contained in `paths`,
619 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
622 static int detect_patch_format(const char **paths)
624 enum patch_format ret = PATCH_FORMAT_UNKNOWN;
625 struct strbuf l1 = STRBUF_INIT;
626 struct strbuf l2 = STRBUF_INIT;
627 struct strbuf l3 = STRBUF_INIT;
631 * We default to mbox format if input is from stdin and for directories
633 if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
634 return PATCH_FORMAT_MBOX;
637 * Otherwise, check the first few lines of the first patch, starting
638 * from the first non-blank line, to try to detect its format.
641 fp = xfopen(*paths, "r");
643 while (!strbuf_getline_crlf(&l1, fp)) {
648 if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) {
649 ret = PATCH_FORMAT_MBOX;
654 strbuf_getline_crlf(&l2, fp);
656 strbuf_getline_crlf(&l3, fp);
659 * If the second line is empty and the third is a From, Author or Date
660 * entry, this is likely an StGit patch.
662 if (l1.len && !l2.len &&
663 (starts_with(l3.buf, "From:") ||
664 starts_with(l3.buf, "Author:") ||
665 starts_with(l3.buf, "Date:"))) {
666 ret = PATCH_FORMAT_STGIT;
670 if (l1.len && is_mail(fp)) {
671 ret = PATCH_FORMAT_MBOX;
682 * Splits out individual email patches from `paths`, where each path is either
683 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
685 static int split_mail_mbox(struct am_state *state, const char **paths, int keep_cr)
687 struct child_process cp = CHILD_PROCESS_INIT;
688 struct strbuf last = STRBUF_INIT;
691 argv_array_push(&cp.args, "mailsplit");
692 argv_array_pushf(&cp.args, "-d%d", state->prec);
693 argv_array_pushf(&cp.args, "-o%s", state->dir);
694 argv_array_push(&cp.args, "-b");
696 argv_array_push(&cp.args, "--keep-cr");
697 argv_array_push(&cp.args, "--");
698 argv_array_pushv(&cp.args, paths);
700 if (capture_command(&cp, &last, 8))
704 state->last = strtol(last.buf, NULL, 10);
710 * Callback signature for split_mail_conv(). The foreign patch should be
711 * read from `in`, and the converted patch (in RFC2822 mail format) should be
712 * written to `out`. Return 0 on success, or -1 on failure.
714 typedef int (*mail_conv_fn)(FILE *out, FILE *in, int keep_cr);
717 * Calls `fn` for each file in `paths` to convert the foreign patch to the
718 * RFC2822 mail format suitable for parsing with git-mailinfo.
720 * Returns 0 on success, -1 on failure.
722 static int split_mail_conv(mail_conv_fn fn, struct am_state *state,
723 const char **paths, int keep_cr)
725 static const char *stdin_only[] = {"-", NULL};
731 for (i = 0; *paths; paths++, i++) {
736 if (!strcmp(*paths, "-"))
739 in = fopen(*paths, "r");
742 return error(_("could not open '%s' for reading: %s"),
743 *paths, strerror(errno));
745 mail = mkpath("%s/%0*d", state->dir, state->prec, i + 1);
747 out = fopen(mail, "w");
749 return error(_("could not open '%s' for writing: %s"),
750 mail, strerror(errno));
752 ret = fn(out, in, keep_cr);
758 return error(_("could not parse patch '%s'"), *paths);
767 * A split_mail_conv() callback that converts an StGit patch to an RFC2822
768 * message suitable for parsing with git-mailinfo.
770 static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr)
772 struct strbuf sb = STRBUF_INIT;
773 int subject_printed = 0;
775 while (!strbuf_getline(&sb, in, '\n')) {
778 if (str_isspace(sb.buf))
780 else if (skip_prefix(sb.buf, "Author:", &str))
781 fprintf(out, "From:%s\n", str);
782 else if (starts_with(sb.buf, "From") || starts_with(sb.buf, "Date"))
783 fprintf(out, "%s\n", sb.buf);
784 else if (!subject_printed) {
785 fprintf(out, "Subject: %s\n", sb.buf);
788 fprintf(out, "\n%s\n", sb.buf);
794 while (strbuf_fread(&sb, 8192, in) > 0) {
795 fwrite(sb.buf, 1, sb.len, out);
804 * Splits a list of files/directories into individual email patches. Each path
805 * in `paths` must be a file/directory that is formatted according to
808 * Once split out, the individual email patches will be stored in the state
809 * directory, with each patch's filename being its index, padded to state->prec
812 * state->cur will be set to the index of the first mail, and state->last will
813 * be set to the index of the last mail.
815 * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1
816 * to disable this behavior, -1 to use the default configured setting.
818 * Returns 0 on success, -1 on failure.
820 static int split_mail(struct am_state *state, enum patch_format patch_format,
821 const char **paths, int keep_cr)
825 git_config_get_bool("am.keepcr", &keep_cr);
828 switch (patch_format) {
829 case PATCH_FORMAT_MBOX:
830 return split_mail_mbox(state, paths, keep_cr);
831 case PATCH_FORMAT_STGIT:
832 return split_mail_conv(stgit_patch_to_mail, state, paths, keep_cr);
834 die("BUG: invalid patch_format");
840 * Setup a new am session for applying patches
842 static void am_setup(struct am_state *state, enum patch_format patch_format,
843 const char **paths, int keep_cr)
845 unsigned char curr_head[GIT_SHA1_RAWSZ];
847 struct strbuf sb = STRBUF_INIT;
850 patch_format = detect_patch_format(paths);
853 fprintf_ln(stderr, _("Patch format detection failed."));
857 if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
858 die_errno(_("failed to create directory '%s'"), state->dir);
860 if (split_mail(state, patch_format, paths, keep_cr) < 0) {
862 die(_("Failed to split patches."));
868 write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
870 write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
872 write_file(am_path(state, "sign"), 1, state->signoff ? "t" : "f");
874 write_file(am_path(state, "utf8"), 1, state->utf8 ? "t" : "f");
876 switch (state->keep) {
887 die("BUG: invalid value for state->keep");
890 write_file(am_path(state, "keep"), 1, "%s", str);
892 write_file(am_path(state, "messageid"), 1, state->message_id ? "t" : "f");
894 switch (state->scissors) {
905 die("BUG: invalid value for state->scissors");
908 write_file(am_path(state, "scissors"), 1, "%s", str);
910 sq_quote_argv(&sb, state->git_apply_opts.argv, 0);
911 write_file(am_path(state, "apply-opt"), 1, "%s", sb.buf);
914 write_file(am_path(state, "rebasing"), 1, "%s", "");
916 write_file(am_path(state, "applying"), 1, "%s", "");
918 if (!get_sha1("HEAD", curr_head)) {
919 write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
920 if (!state->rebasing)
921 update_ref("am", "ORIG_HEAD", curr_head, NULL, 0,
922 UPDATE_REFS_DIE_ON_ERR);
924 write_file(am_path(state, "abort-safety"), 1, "%s", "");
925 if (!state->rebasing)
926 delete_ref("ORIG_HEAD", NULL, 0);
930 * NOTE: Since the "next" and "last" files determine if an am_state
931 * session is in progress, they should be written last.
934 write_file(am_path(state, "next"), 1, "%d", state->cur);
936 write_file(am_path(state, "last"), 1, "%d", state->last);
942 * Increments the patch pointer, and cleans am_state for the application of the
945 static void am_next(struct am_state *state)
947 unsigned char head[GIT_SHA1_RAWSZ];
949 free(state->author_name);
950 state->author_name = NULL;
952 free(state->author_email);
953 state->author_email = NULL;
955 free(state->author_date);
956 state->author_date = NULL;
962 unlink(am_path(state, "author-script"));
963 unlink(am_path(state, "final-commit"));
965 hashclr(state->orig_commit);
966 unlink(am_path(state, "original-commit"));
968 if (!get_sha1("HEAD", head))
969 write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
971 write_file(am_path(state, "abort-safety"), 1, "%s", "");
974 write_file(am_path(state, "next"), 1, "%d", state->cur);
978 * Returns the filename of the current patch email.
980 static const char *msgnum(const struct am_state *state)
982 static struct strbuf sb = STRBUF_INIT;
985 strbuf_addf(&sb, "%0*d", state->prec, state->cur);
991 * Refresh and write index.
993 static void refresh_and_write_cache(void)
995 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
997 hold_locked_index(lock_file, 1);
998 refresh_cache(REFRESH_QUIET);
999 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1000 die(_("unable to write index file"));
1004 * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
1005 * branch, returns 1 if there are entries in the index, 0 otherwise. If an
1006 * strbuf is provided, the space-separated list of files that differ will be
1009 static int index_has_changes(struct strbuf *sb)
1011 unsigned char head[GIT_SHA1_RAWSZ];
1014 if (!get_sha1_tree("HEAD", head)) {
1015 struct diff_options opt;
1018 DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
1020 DIFF_OPT_SET(&opt, QUICK);
1021 do_diff_cache(head, &opt);
1023 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
1025 strbuf_addch(sb, ' ');
1026 strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
1029 return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0;
1031 for (i = 0; sb && i < active_nr; i++) {
1033 strbuf_addch(sb, ' ');
1034 strbuf_addstr(sb, active_cache[i]->name);
1041 * Dies with a user-friendly message on how to proceed after resolving the
1042 * problem. This message can be overridden with state->resolvemsg.
1044 static void NORETURN die_user_resolve(const struct am_state *state)
1046 if (state->resolvemsg) {
1047 printf_ln("%s", state->resolvemsg);
1049 const char *cmdline = "git am";
1051 printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline);
1052 printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline);
1053 printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline);
1060 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
1061 * state->msg will be set to the patch message. state->author_name,
1062 * state->author_email and state->author_date will be set to the patch author's
1063 * name, email and date respectively. The patch body will be written to the
1064 * state directory's "patch" file.
1066 * Returns 1 if the patch should be skipped, 0 otherwise.
1068 static int parse_mail(struct am_state *state, const char *mail)
1071 struct child_process cp = CHILD_PROCESS_INIT;
1072 struct strbuf sb = STRBUF_INIT;
1073 struct strbuf msg = STRBUF_INIT;
1074 struct strbuf author_name = STRBUF_INIT;
1075 struct strbuf author_date = STRBUF_INIT;
1076 struct strbuf author_email = STRBUF_INIT;
1080 cp.in = xopen(mail, O_RDONLY, 0);
1081 cp.out = xopen(am_path(state, "info"), O_WRONLY | O_CREAT, 0777);
1083 argv_array_push(&cp.args, "mailinfo");
1084 argv_array_push(&cp.args, state->utf8 ? "-u" : "-n");
1086 switch (state->keep) {
1090 argv_array_push(&cp.args, "-k");
1092 case KEEP_NON_PATCH:
1093 argv_array_push(&cp.args, "-b");
1096 die("BUG: invalid value for state->keep");
1099 if (state->message_id)
1100 argv_array_push(&cp.args, "-m");
1102 switch (state->scissors) {
1103 case SCISSORS_UNSET:
1105 case SCISSORS_FALSE:
1106 argv_array_push(&cp.args, "--no-scissors");
1109 argv_array_push(&cp.args, "--scissors");
1112 die("BUG: invalid value for state->scissors");
1115 argv_array_push(&cp.args, am_path(state, "msg"));
1116 argv_array_push(&cp.args, am_path(state, "patch"));
1118 if (run_command(&cp) < 0)
1119 die("could not parse patch");
1124 /* Extract message and author information */
1125 fp = xfopen(am_path(state, "info"), "r");
1126 while (!strbuf_getline(&sb, fp, '\n')) {
1129 if (skip_prefix(sb.buf, "Subject: ", &x)) {
1131 strbuf_addch(&msg, '\n');
1132 strbuf_addstr(&msg, x);
1133 } else if (skip_prefix(sb.buf, "Author: ", &x))
1134 strbuf_addstr(&author_name, x);
1135 else if (skip_prefix(sb.buf, "Email: ", &x))
1136 strbuf_addstr(&author_email, x);
1137 else if (skip_prefix(sb.buf, "Date: ", &x))
1138 strbuf_addstr(&author_date, x);
1142 /* Skip pine's internal folder data */
1143 if (!strcmp(author_name.buf, "Mail System Internal Data")) {
1148 if (is_empty_file(am_path(state, "patch"))) {
1149 printf_ln(_("Patch is empty. Was it split wrong?"));
1150 die_user_resolve(state);
1153 strbuf_addstr(&msg, "\n\n");
1154 if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
1155 die_errno(_("could not read '%s'"), am_path(state, "msg"));
1156 stripspace(&msg, 0);
1159 append_signoff(&msg, 0, 0);
1161 assert(!state->author_name);
1162 state->author_name = strbuf_detach(&author_name, NULL);
1164 assert(!state->author_email);
1165 state->author_email = strbuf_detach(&author_email, NULL);
1167 assert(!state->author_date);
1168 state->author_date = strbuf_detach(&author_date, NULL);
1170 assert(!state->msg);
1171 state->msg = strbuf_detach(&msg, &state->msg_len);
1174 strbuf_release(&msg);
1175 strbuf_release(&author_date);
1176 strbuf_release(&author_email);
1177 strbuf_release(&author_name);
1178 strbuf_release(&sb);
1183 * Sets commit_id to the commit hash where the mail was generated from.
1184 * Returns 0 on success, -1 on failure.
1186 static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
1188 struct strbuf sb = STRBUF_INIT;
1189 FILE *fp = xfopen(mail, "r");
1192 if (strbuf_getline(&sb, fp, '\n'))
1195 if (!skip_prefix(sb.buf, "From ", &x))
1198 if (get_sha1_hex(x, commit_id) < 0)
1201 strbuf_release(&sb);
1207 * Sets state->msg, state->author_name, state->author_email, state->author_date
1208 * to the commit's respective info.
1210 static void get_commit_info(struct am_state *state, struct commit *commit)
1212 const char *buffer, *ident_line, *author_date, *msg;
1214 struct ident_split ident_split;
1215 struct strbuf sb = STRBUF_INIT;
1217 buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding());
1219 ident_line = find_commit_header(buffer, "author", &ident_len);
1221 if (split_ident_line(&ident_split, ident_line, ident_len) < 0) {
1222 strbuf_add(&sb, ident_line, ident_len);
1223 die(_("invalid ident line: %s"), sb.buf);
1226 assert(!state->author_name);
1227 if (ident_split.name_begin) {
1228 strbuf_add(&sb, ident_split.name_begin,
1229 ident_split.name_end - ident_split.name_begin);
1230 state->author_name = strbuf_detach(&sb, NULL);
1232 state->author_name = xstrdup("");
1234 assert(!state->author_email);
1235 if (ident_split.mail_begin) {
1236 strbuf_add(&sb, ident_split.mail_begin,
1237 ident_split.mail_end - ident_split.mail_begin);
1238 state->author_email = strbuf_detach(&sb, NULL);
1240 state->author_email = xstrdup("");
1242 author_date = show_ident_date(&ident_split, DATE_MODE(NORMAL));
1243 strbuf_addstr(&sb, author_date);
1244 assert(!state->author_date);
1245 state->author_date = strbuf_detach(&sb, NULL);
1247 assert(!state->msg);
1248 msg = strstr(buffer, "\n\n");
1250 die(_("unable to parse commit %s"), sha1_to_hex(commit->object.sha1));
1251 state->msg = xstrdup(msg + 2);
1252 state->msg_len = strlen(state->msg);
1256 * Writes `commit` as a patch to the state directory's "patch" file.
1258 static void write_commit_patch(const struct am_state *state, struct commit *commit)
1260 struct rev_info rev_info;
1263 fp = xfopen(am_path(state, "patch"), "w");
1264 init_revisions(&rev_info, NULL);
1266 rev_info.abbrev = 0;
1267 rev_info.disable_stdin = 1;
1268 rev_info.show_root_diff = 1;
1269 rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
1270 rev_info.no_commit_id = 1;
1271 DIFF_OPT_SET(&rev_info.diffopt, BINARY);
1272 DIFF_OPT_SET(&rev_info.diffopt, FULL_INDEX);
1273 rev_info.diffopt.use_color = 0;
1274 rev_info.diffopt.file = fp;
1275 rev_info.diffopt.close_file = 1;
1276 add_pending_object(&rev_info, &commit->object, "");
1277 diff_setup_done(&rev_info.diffopt);
1278 log_tree_commit(&rev_info, commit);
1282 * Like parse_mail(), but parses the mail by looking up its commit ID
1283 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
1286 * state->orig_commit will be set to the original commit ID.
1288 * Will always return 0 as the patch should never be skipped.
1290 static int parse_mail_rebase(struct am_state *state, const char *mail)
1292 struct commit *commit;
1293 unsigned char commit_sha1[GIT_SHA1_RAWSZ];
1295 if (get_mail_commit_sha1(commit_sha1, mail) < 0)
1296 die(_("could not parse %s"), mail);
1298 commit = lookup_commit_or_die(commit_sha1, mail);
1300 get_commit_info(state, commit);
1302 write_commit_patch(state, commit);
1304 hashcpy(state->orig_commit, commit_sha1);
1305 write_file(am_path(state, "original-commit"), 1, "%s",
1306 sha1_to_hex(commit_sha1));
1312 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
1313 * `index_file` is not NULL, the patch will be applied to that index.
1315 static int run_apply(const struct am_state *state, const char *index_file)
1317 struct child_process cp = CHILD_PROCESS_INIT;
1322 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
1325 * If we are allowed to fall back on 3-way merge, don't give false
1326 * errors during the initial attempt.
1328 if (state->threeway && !index_file) {
1333 argv_array_push(&cp.args, "apply");
1335 argv_array_pushv(&cp.args, state->git_apply_opts.argv);
1338 argv_array_push(&cp.args, "--cached");
1340 argv_array_push(&cp.args, "--index");
1342 argv_array_push(&cp.args, am_path(state, "patch"));
1344 if (run_command(&cp))
1347 /* Reload index as git-apply will have modified it. */
1349 read_cache_from(index_file ? index_file : get_index_file());
1355 * Builds an index that contains just the blobs needed for a 3way merge.
1357 static int build_fake_ancestor(const struct am_state *state, const char *index_file)
1359 struct child_process cp = CHILD_PROCESS_INIT;
1362 argv_array_push(&cp.args, "apply");
1363 argv_array_pushv(&cp.args, state->git_apply_opts.argv);
1364 argv_array_pushf(&cp.args, "--build-fake-ancestor=%s", index_file);
1365 argv_array_push(&cp.args, am_path(state, "patch"));
1367 if (run_command(&cp))
1374 * Attempt a threeway merge, using index_path as the temporary index.
1376 static int fall_back_threeway(const struct am_state *state, const char *index_path)
1378 unsigned char orig_tree[GIT_SHA1_RAWSZ], his_tree[GIT_SHA1_RAWSZ],
1379 our_tree[GIT_SHA1_RAWSZ];
1380 const unsigned char *bases[1] = {orig_tree};
1381 struct merge_options o;
1382 struct commit *result;
1383 char *his_tree_name;
1385 if (get_sha1("HEAD", our_tree) < 0)
1386 hashcpy(our_tree, EMPTY_TREE_SHA1_BIN);
1388 if (build_fake_ancestor(state, index_path))
1389 return error("could not build fake ancestor");
1392 read_cache_from(index_path);
1394 if (write_index_as_tree(orig_tree, &the_index, index_path, 0, NULL))
1395 return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1397 say(state, stdout, _("Using index info to reconstruct a base tree..."));
1399 if (!state->quiet) {
1401 * List paths that needed 3-way fallback, so that the user can
1402 * review them with extra care to spot mismerges.
1404 struct rev_info rev_info;
1405 const char *diff_filter_str = "--diff-filter=AM";
1407 init_revisions(&rev_info, NULL);
1408 rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
1409 diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
1410 add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
1411 diff_setup_done(&rev_info.diffopt);
1412 run_diff_index(&rev_info, 1);
1415 if (run_apply(state, index_path))
1416 return error(_("Did you hand edit your patch?\n"
1417 "It does not apply to blobs recorded in its index."));
1419 if (write_index_as_tree(his_tree, &the_index, index_path, 0, NULL))
1420 return error("could not write tree");
1422 say(state, stdout, _("Falling back to patching base and 3-way merge..."));
1428 * This is not so wrong. Depending on which base we picked, orig_tree
1429 * may be wildly different from ours, but his_tree has the same set of
1430 * wildly different changes in parts the patch did not touch, so
1431 * recursive ends up canceling them, saying that we reverted all those
1435 init_merge_options(&o);
1438 his_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
1439 o.branch2 = his_tree_name;
1444 if (merge_recursive_generic(&o, our_tree, his_tree, 1, bases, &result)) {
1445 rerere(state->allow_rerere_autoupdate);
1446 free(his_tree_name);
1447 return error(_("Failed to merge in the changes."));
1450 free(his_tree_name);
1455 * Commits the current index with state->msg as the commit message and
1456 * state->author_name, state->author_email and state->author_date as the author
1459 static void do_commit(const struct am_state *state)
1461 unsigned char tree[GIT_SHA1_RAWSZ], parent[GIT_SHA1_RAWSZ],
1462 commit[GIT_SHA1_RAWSZ];
1464 struct commit_list *parents = NULL;
1465 const char *reflog_msg, *author;
1466 struct strbuf sb = STRBUF_INIT;
1468 if (run_hook_le(NULL, "pre-applypatch", NULL))
1471 if (write_cache_as_tree(tree, 0, NULL))
1472 die(_("git write-tree failed to write a tree"));
1474 if (!get_sha1_commit("HEAD", parent)) {
1476 commit_list_insert(lookup_commit(parent), &parents);
1479 say(state, stderr, _("applying to an empty history"));
1482 author = fmt_ident(state->author_name, state->author_email,
1483 state->ignore_date ? NULL : state->author_date,
1486 if (state->committer_date_is_author_date)
1487 setenv("GIT_COMMITTER_DATE",
1488 state->ignore_date ? "" : state->author_date, 1);
1490 if (commit_tree(state->msg, state->msg_len, tree, parents, commit,
1491 author, state->sign_commit))
1492 die(_("failed to write commit object"));
1494 reflog_msg = getenv("GIT_REFLOG_ACTION");
1498 strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
1501 update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
1503 if (state->rebasing) {
1504 FILE *fp = xfopen(am_path(state, "rewritten"), "a");
1506 assert(!is_null_sha1(state->orig_commit));
1507 fprintf(fp, "%s ", sha1_to_hex(state->orig_commit));
1508 fprintf(fp, "%s\n", sha1_to_hex(commit));
1512 run_hook_le(NULL, "post-applypatch", NULL);
1514 strbuf_release(&sb);
1518 * Validates the am_state for resuming -- the "msg" and authorship fields must
1521 static void validate_resume_state(const struct am_state *state)
1524 die(_("cannot resume: %s does not exist."),
1525 am_path(state, "final-commit"));
1527 if (!state->author_name || !state->author_email || !state->author_date)
1528 die(_("cannot resume: %s does not exist."),
1529 am_path(state, "author-script"));
1533 * Applies all queued mail.
1535 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
1536 * well as the state directory's "patch" file is used as-is for applying the
1537 * patch and committing it.
1539 static void am_run(struct am_state *state, int resume)
1541 const char *argv_gc_auto[] = {"gc", "--auto", NULL};
1542 struct strbuf sb = STRBUF_INIT;
1544 unlink(am_path(state, "dirtyindex"));
1546 refresh_and_write_cache();
1548 if (index_has_changes(&sb)) {
1549 write_file(am_path(state, "dirtyindex"), 1, "t");
1550 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
1553 strbuf_release(&sb);
1555 while (state->cur <= state->last) {
1556 const char *mail = am_path(state, msgnum(state));
1559 if (!file_exists(mail))
1563 validate_resume_state(state);
1568 if (state->rebasing)
1569 skip = parse_mail_rebase(state, mail);
1571 skip = parse_mail(state, mail);
1574 goto next; /* mail should be skipped */
1576 write_author_script(state);
1577 write_commit_msg(state);
1580 if (run_applypatch_msg_hook(state))
1583 say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1585 apply_status = run_apply(state, NULL);
1587 if (apply_status && state->threeway) {
1588 struct strbuf sb = STRBUF_INIT;
1590 strbuf_addstr(&sb, am_path(state, "patch-merge-index"));
1591 apply_status = fall_back_threeway(state, sb.buf);
1592 strbuf_release(&sb);
1595 * Applying the patch to an earlier tree and merging
1596 * the result may have produced the same tree as ours.
1598 if (!apply_status && !index_has_changes(NULL)) {
1599 say(state, stdout, _("No changes -- Patch already applied."));
1605 int advice_amworkdir = 1;
1607 printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
1608 linelen(state->msg), state->msg);
1610 git_config_get_bool("advice.amworkdir", &advice_amworkdir);
1612 if (advice_amworkdir)
1613 printf_ln(_("The copy of the patch that failed is found in: %s"),
1614 am_path(state, "patch"));
1616 die_user_resolve(state);
1625 if (!is_empty_file(am_path(state, "rewritten"))) {
1626 assert(state->rebasing);
1627 copy_notes_for_rebase(state);
1628 run_post_rewrite_hook(state);
1632 * In rebasing mode, it's up to the caller to take care of
1635 if (!state->rebasing) {
1637 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1642 * Resume the current am session after patch application failure. The user did
1643 * all the hard work, and we do not have to do any patch application. Just
1644 * trust and commit what the user has in the index and working tree.
1646 static void am_resolve(struct am_state *state)
1648 validate_resume_state(state);
1650 say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1652 if (!index_has_changes(NULL)) {
1653 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1654 "If there is nothing left to stage, chances are that something else\n"
1655 "already introduced the same changes; you might want to skip this patch."));
1656 die_user_resolve(state);
1659 if (unmerged_cache()) {
1660 printf_ln(_("You still have unmerged paths in your index.\n"
1661 "Did you forget to use 'git add'?"));
1662 die_user_resolve(state);
1674 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1675 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1678 static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1680 struct lock_file *lock_file;
1681 struct unpack_trees_options opts;
1682 struct tree_desc t[2];
1684 if (parse_tree(head) || parse_tree(remote))
1687 lock_file = xcalloc(1, sizeof(struct lock_file));
1688 hold_locked_index(lock_file, 1);
1690 refresh_cache(REFRESH_QUIET);
1692 memset(&opts, 0, sizeof(opts));
1694 opts.src_index = &the_index;
1695 opts.dst_index = &the_index;
1699 opts.fn = twoway_merge;
1700 init_tree_desc(&t[0], head->buffer, head->size);
1701 init_tree_desc(&t[1], remote->buffer, remote->size);
1703 if (unpack_trees(2, t, &opts)) {
1704 rollback_lock_file(lock_file);
1708 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1709 die(_("unable to write new index file"));
1715 * Clean the index without touching entries that are not modified between
1716 * `head` and `remote`.
1718 static int clean_index(const unsigned char *head, const unsigned char *remote)
1720 struct lock_file *lock_file;
1721 struct tree *head_tree, *remote_tree, *index_tree;
1722 unsigned char index[GIT_SHA1_RAWSZ];
1723 struct pathspec pathspec;
1725 head_tree = parse_tree_indirect(head);
1727 return error(_("Could not parse object '%s'."), sha1_to_hex(head));
1729 remote_tree = parse_tree_indirect(remote);
1731 return error(_("Could not parse object '%s'."), sha1_to_hex(remote));
1733 read_cache_unmerged();
1735 if (fast_forward_to(head_tree, head_tree, 1))
1738 if (write_cache_as_tree(index, 0, NULL))
1741 index_tree = parse_tree_indirect(index);
1743 return error(_("Could not parse object '%s'."), sha1_to_hex(index));
1745 if (fast_forward_to(index_tree, remote_tree, 0))
1748 memset(&pathspec, 0, sizeof(pathspec));
1750 lock_file = xcalloc(1, sizeof(struct lock_file));
1751 hold_locked_index(lock_file, 1);
1753 if (read_tree(remote_tree, 0, &pathspec)) {
1754 rollback_lock_file(lock_file);
1758 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1759 die(_("unable to write new index file"));
1761 remove_branch_state();
1767 * Resets rerere's merge resolution metadata.
1769 static void am_rerere_clear(void)
1771 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1772 int fd = setup_rerere(&merge_rr, 0);
1777 rerere_clear(&merge_rr);
1778 string_list_clear(&merge_rr, 1);
1782 * Resume the current am session by skipping the current patch.
1784 static void am_skip(struct am_state *state)
1786 unsigned char head[GIT_SHA1_RAWSZ];
1790 if (get_sha1("HEAD", head))
1791 hashcpy(head, EMPTY_TREE_SHA1_BIN);
1793 if (clean_index(head, head))
1794 die(_("failed to clean index"));
1801 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
1803 * It is not safe to reset HEAD when:
1804 * 1. git-am previously failed because the index was dirty.
1805 * 2. HEAD has moved since git-am previously failed.
1807 static int safe_to_abort(const struct am_state *state)
1809 struct strbuf sb = STRBUF_INIT;
1810 unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
1812 if (file_exists(am_path(state, "dirtyindex")))
1815 if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
1816 if (get_sha1_hex(sb.buf, abort_safety))
1817 die(_("could not parse %s"), am_path(state, "abort_safety"));
1819 hashclr(abort_safety);
1821 if (get_sha1("HEAD", head))
1824 if (!hashcmp(head, abort_safety))
1827 error(_("You seem to have moved HEAD since the last 'am' failure.\n"
1828 "Not rewinding to ORIG_HEAD"));
1834 * Aborts the current am session if it is safe to do so.
1836 static void am_abort(struct am_state *state)
1838 unsigned char curr_head[GIT_SHA1_RAWSZ], orig_head[GIT_SHA1_RAWSZ];
1839 int has_curr_head, has_orig_head;
1842 if (!safe_to_abort(state)) {
1849 curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
1850 has_curr_head = !is_null_sha1(curr_head);
1852 hashcpy(curr_head, EMPTY_TREE_SHA1_BIN);
1854 has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
1856 hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
1858 clean_index(curr_head, orig_head);
1861 update_ref("am --abort", "HEAD", orig_head,
1862 has_curr_head ? curr_head : NULL, 0,
1863 UPDATE_REFS_DIE_ON_ERR);
1864 else if (curr_branch)
1865 delete_ref(curr_branch, NULL, REF_NODEREF);
1872 * parse_options() callback that validates and sets opt->value to the
1873 * PATCH_FORMAT_* enum value corresponding to `arg`.
1875 static int parse_opt_patchformat(const struct option *opt, const char *arg, int unset)
1877 int *opt_value = opt->value;
1879 if (!strcmp(arg, "mbox"))
1880 *opt_value = PATCH_FORMAT_MBOX;
1881 else if (!strcmp(arg, "stgit"))
1882 *opt_value = PATCH_FORMAT_STGIT;
1884 return error(_("Invalid value for --patch-format: %s"), arg);
1896 int cmd_am(int argc, const char **argv, const char *prefix)
1898 struct am_state state;
1900 int patch_format = PATCH_FORMAT_UNKNOWN;
1901 enum resume_mode resume = RESUME_FALSE;
1903 const char * const usage[] = {
1904 N_("git am [options] [(<mbox>|<Maildir>)...]"),
1905 N_("git am [options] (--continue | --skip | --abort)"),
1909 struct option options[] = {
1910 OPT_BOOL('3', "3way", &state.threeway,
1911 N_("allow fall back on 3way merging if needed")),
1912 OPT__QUIET(&state.quiet, N_("be quiet")),
1913 OPT_BOOL('s', "signoff", &state.signoff,
1914 N_("add a Signed-off-by line to the commit message")),
1915 OPT_BOOL('u', "utf8", &state.utf8,
1916 N_("recode into utf8 (default)")),
1917 OPT_SET_INT('k', "keep", &state.keep,
1918 N_("pass -k flag to git-mailinfo"), KEEP_TRUE),
1919 OPT_SET_INT(0, "keep-non-patch", &state.keep,
1920 N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH),
1921 OPT_BOOL('m', "message-id", &state.message_id,
1922 N_("pass -m flag to git-mailinfo")),
1923 { OPTION_SET_INT, 0, "keep-cr", &keep_cr, NULL,
1924 N_("pass --keep-cr flag to git-mailsplit for mbox format"),
1925 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
1926 { OPTION_SET_INT, 0, "no-keep-cr", &keep_cr, NULL,
1927 N_("do not pass --keep-cr flag to git-mailsplit independent of am.keepcr"),
1928 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 0},
1929 OPT_BOOL('c', "scissors", &state.scissors,
1930 N_("strip everything before a scissors line")),
1931 OPT_PASSTHRU_ARGV(0, "whitespace", &state.git_apply_opts, N_("action"),
1932 N_("pass it through git-apply"),
1934 OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state.git_apply_opts, NULL,
1935 N_("pass it through git-apply"),
1937 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state.git_apply_opts, NULL,
1938 N_("pass it through git-apply"),
1940 OPT_PASSTHRU_ARGV(0, "directory", &state.git_apply_opts, N_("root"),
1941 N_("pass it through git-apply"),
1943 OPT_PASSTHRU_ARGV(0, "exclude", &state.git_apply_opts, N_("path"),
1944 N_("pass it through git-apply"),
1946 OPT_PASSTHRU_ARGV(0, "include", &state.git_apply_opts, N_("path"),
1947 N_("pass it through git-apply"),
1949 OPT_PASSTHRU_ARGV('C', NULL, &state.git_apply_opts, N_("n"),
1950 N_("pass it through git-apply"),
1952 OPT_PASSTHRU_ARGV('p', NULL, &state.git_apply_opts, N_("num"),
1953 N_("pass it through git-apply"),
1955 OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
1956 N_("format the patch(es) are in"),
1957 parse_opt_patchformat),
1958 OPT_PASSTHRU_ARGV(0, "reject", &state.git_apply_opts, NULL,
1959 N_("pass it through git-apply"),
1961 OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
1962 N_("override error message when patch failure occurs")),
1963 OPT_CMDMODE(0, "continue", &resume,
1964 N_("continue applying patches after resolving a conflict"),
1966 OPT_CMDMODE('r', "resolved", &resume,
1967 N_("synonyms for --continue"),
1969 OPT_CMDMODE(0, "skip", &resume,
1970 N_("skip the current patch"),
1972 OPT_CMDMODE(0, "abort", &resume,
1973 N_("restore the original branch and abort the patching operation."),
1975 OPT_BOOL(0, "committer-date-is-author-date",
1976 &state.committer_date_is_author_date,
1977 N_("lie about committer date")),
1978 OPT_BOOL(0, "ignore-date", &state.ignore_date,
1979 N_("use current timestamp for author date")),
1980 OPT_RERERE_AUTOUPDATE(&state.allow_rerere_autoupdate),
1981 { OPTION_STRING, 'S', "gpg-sign", &state.sign_commit, N_("key-id"),
1982 N_("GPG-sign commits"),
1983 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1984 OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing,
1985 N_("(internal use for git-rebase)")),
1990 * NEEDSWORK: Once all the features of git-am.sh have been
1991 * re-implemented in builtin/am.c, this preamble can be removed.
1993 if (!getenv("_GIT_USE_BUILTIN_AM")) {
1994 const char *path = mkpath("%s/git-am", git_exec_path());
1996 if (sane_execvp(path, (char **)argv) < 0)
1997 die_errno("could not exec %s", path);
1999 prefix = setup_git_directory();
2000 trace_repo_setup(prefix);
2004 git_config(git_default_config, NULL);
2006 am_state_init(&state, git_path("rebase-apply"));
2008 argc = parse_options(argc, argv, prefix, options, usage, 0);
2010 if (read_index_preload(&the_index, NULL) < 0)
2011 die(_("failed to read the index"));
2013 if (am_in_progress(&state)) {
2015 * Catch user error to feed us patches when there is a session
2018 * 1. mbox path(s) are provided on the command-line.
2019 * 2. stdin is not a tty: the user is trying to feed us a patch
2020 * from standard input. This is somewhat unreliable -- stdin
2021 * could be /dev/null for example and the caller did not
2022 * intend to feed us a patch but wanted to continue
2025 if (argc || (resume == RESUME_FALSE && !isatty(0)))
2026 die(_("previous rebase directory %s still exists but mbox given."),
2029 if (resume == RESUME_FALSE)
2030 resume = RESUME_APPLY;
2034 struct argv_array paths = ARGV_ARRAY_INIT;
2038 * Handle stray state directory in the independent-run case. In
2039 * the --rebasing case, it is up to the caller to take care of
2040 * stray directories.
2042 if (file_exists(state.dir) && !state.rebasing) {
2043 if (resume == RESUME_ABORT) {
2045 am_state_release(&state);
2049 die(_("Stray %s directory found.\n"
2050 "Use \"git am --abort\" to remove it."),
2055 die(_("Resolve operation not in progress, we are not resuming."));
2057 for (i = 0; i < argc; i++) {
2058 if (is_absolute_path(argv[i]) || !prefix)
2059 argv_array_push(&paths, argv[i]);
2061 argv_array_push(&paths, mkpath("%s/%s", prefix, argv[i]));
2064 am_setup(&state, patch_format, paths.argv, keep_cr);
2066 argv_array_clear(&paths);
2076 case RESUME_RESOLVED:
2086 die("BUG: invalid resume value");
2089 am_state_release(&state);