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"
29 * Returns 1 if the file is empty or does not exist, 0 otherwise.
31 static int is_empty_file(const char *filename
)
35 if (stat(filename
, &st
) < 0) {
38 die_errno(_("could not stat %s"), filename
);
45 * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
47 static int strbuf_getline_crlf(struct strbuf
*sb
, FILE *fp
)
49 if (strbuf_getwholeline(sb
, fp
, '\n'))
51 if (sb
->buf
[sb
->len
- 1] == '\n') {
52 strbuf_setlen(sb
, sb
->len
- 1);
53 if (sb
->len
> 0 && sb
->buf
[sb
->len
- 1] == '\r')
54 strbuf_setlen(sb
, sb
->len
- 1);
60 * Returns the length of the first line of msg.
62 static int linelen(const char *msg
)
64 return strchrnul(msg
, '\n') - msg
;
68 PATCH_FORMAT_UNKNOWN
= 0,
74 KEEP_TRUE
, /* pass -k flag to git-mailinfo */
75 KEEP_NON_PATCH
/* pass -b flag to git-mailinfo */
80 SCISSORS_FALSE
= 0, /* pass --no-scissors to git-mailinfo */
81 SCISSORS_TRUE
/* pass --scissors to git-mailinfo */
85 /* state directory path */
88 /* current and last patch numbers, 1-indexed */
92 /* commit metadata and message */
99 /* when --rebasing, records the original commit the patch came from */
100 unsigned char orig_commit
[GIT_SHA1_RAWSZ
];
102 /* number of digits in patch filename */
105 /* various operating modes and command line options */
110 int keep
; /* enum keep_type */
112 int scissors
; /* enum scissors_type */
113 struct argv_array git_apply_opts
;
114 const char *resolvemsg
;
115 int committer_date_is_author_date
;
117 const char *sign_commit
;
122 * Initializes am_state with the default values. The state directory is set to
125 static void am_state_init(struct am_state
*state
, const char *dir
)
129 memset(state
, 0, sizeof(*state
));
132 state
->dir
= xstrdup(dir
);
138 git_config_get_bool("am.messageid", &state
->message_id
);
140 state
->scissors
= SCISSORS_UNSET
;
142 argv_array_init(&state
->git_apply_opts
);
144 if (!git_config_get_bool("commit.gpgsign", &gpgsign
))
145 state
->sign_commit
= gpgsign
? "" : NULL
;
149 * Releases memory allocated by an am_state.
151 static void am_state_release(struct am_state
*state
)
154 free(state
->author_name
);
155 free(state
->author_email
);
156 free(state
->author_date
);
158 argv_array_clear(&state
->git_apply_opts
);
162 * Returns path relative to the am_state directory.
164 static inline const char *am_path(const struct am_state
*state
, const char *path
)
166 return mkpath("%s/%s", state
->dir
, path
);
170 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
173 static void say(const struct am_state
*state
, FILE *fp
, const char *fmt
, ...)
179 vfprintf(fp
, fmt
, ap
);
186 * Returns 1 if there is an am session in progress, 0 otherwise.
188 static int am_in_progress(const struct am_state
*state
)
192 if (lstat(state
->dir
, &st
) < 0 || !S_ISDIR(st
.st_mode
))
194 if (lstat(am_path(state
, "last"), &st
) || !S_ISREG(st
.st_mode
))
196 if (lstat(am_path(state
, "next"), &st
) || !S_ISREG(st
.st_mode
))
202 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
203 * number of bytes read on success, -1 if the file does not exist. If `trim` is
204 * set, trailing whitespace will be removed.
206 static int read_state_file(struct strbuf
*sb
, const struct am_state
*state
,
207 const char *file
, int trim
)
211 if (strbuf_read_file(sb
, am_path(state
, file
), 0) >= 0) {
221 die_errno(_("could not read '%s'"), am_path(state
, file
));
225 * Reads a KEY=VALUE shell variable assignment from `fp`, returning the VALUE
226 * as a newly-allocated string. VALUE must be a quoted string, and the KEY must
227 * match `key`. Returns NULL on failure.
229 * This is used by read_author_script() to read the GIT_AUTHOR_* variables from
232 static char *read_shell_var(FILE *fp
, const char *key
)
234 struct strbuf sb
= STRBUF_INIT
;
237 if (strbuf_getline(&sb
, fp
, '\n'))
240 if (!skip_prefix(sb
.buf
, key
, &str
))
243 if (!skip_prefix(str
, "=", &str
))
246 strbuf_remove(&sb
, 0, str
- sb
.buf
);
248 str
= sq_dequote(sb
.buf
);
252 return strbuf_detach(&sb
, NULL
);
260 * Reads and parses the state directory's "author-script" file, and sets
261 * state->author_name, state->author_email and state->author_date accordingly.
262 * Returns 0 on success, -1 if the file could not be parsed.
264 * The author script is of the format:
266 * GIT_AUTHOR_NAME='$author_name'
267 * GIT_AUTHOR_EMAIL='$author_email'
268 * GIT_AUTHOR_DATE='$author_date'
270 * where $author_name, $author_email and $author_date are quoted. We are strict
271 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
272 * script, and thus if the file differs from what this function expects, it is
273 * better to bail out than to do something that the user does not expect.
275 static int read_author_script(struct am_state
*state
)
277 const char *filename
= am_path(state
, "author-script");
280 assert(!state
->author_name
);
281 assert(!state
->author_email
);
282 assert(!state
->author_date
);
284 fp
= fopen(filename
, "r");
288 die_errno(_("could not open '%s' for reading"), filename
);
291 state
->author_name
= read_shell_var(fp
, "GIT_AUTHOR_NAME");
292 if (!state
->author_name
) {
297 state
->author_email
= read_shell_var(fp
, "GIT_AUTHOR_EMAIL");
298 if (!state
->author_email
) {
303 state
->author_date
= read_shell_var(fp
, "GIT_AUTHOR_DATE");
304 if (!state
->author_date
) {
309 if (fgetc(fp
) != EOF
) {
319 * Saves state->author_name, state->author_email and state->author_date in the
320 * state directory's "author-script" file.
322 static void write_author_script(const struct am_state
*state
)
324 struct strbuf sb
= STRBUF_INIT
;
326 strbuf_addstr(&sb
, "GIT_AUTHOR_NAME=");
327 sq_quote_buf(&sb
, state
->author_name
);
328 strbuf_addch(&sb
, '\n');
330 strbuf_addstr(&sb
, "GIT_AUTHOR_EMAIL=");
331 sq_quote_buf(&sb
, state
->author_email
);
332 strbuf_addch(&sb
, '\n');
334 strbuf_addstr(&sb
, "GIT_AUTHOR_DATE=");
335 sq_quote_buf(&sb
, state
->author_date
);
336 strbuf_addch(&sb
, '\n');
338 write_file(am_path(state
, "author-script"), 1, "%s", sb
.buf
);
344 * Reads the commit message from the state directory's "final-commit" file,
345 * setting state->msg to its contents and state->msg_len to the length of its
348 * Returns 0 on success, -1 if the file does not exist.
350 static int read_commit_msg(struct am_state
*state
)
352 struct strbuf sb
= STRBUF_INIT
;
356 if (read_state_file(&sb
, state
, "final-commit", 0) < 0) {
361 state
->msg
= strbuf_detach(&sb
, &state
->msg_len
);
366 * Saves state->msg in the state directory's "final-commit" file.
368 static void write_commit_msg(const struct am_state
*state
)
371 const char *filename
= am_path(state
, "final-commit");
373 fd
= xopen(filename
, O_WRONLY
| O_CREAT
, 0666);
374 if (write_in_full(fd
, state
->msg
, state
->msg_len
) < 0)
375 die_errno(_("could not write to %s"), filename
);
380 * Loads state from disk.
382 static void am_load(struct am_state
*state
)
384 struct strbuf sb
= STRBUF_INIT
;
386 if (read_state_file(&sb
, state
, "next", 1) < 0)
387 die("BUG: state file 'next' does not exist");
388 state
->cur
= strtol(sb
.buf
, NULL
, 10);
390 if (read_state_file(&sb
, state
, "last", 1) < 0)
391 die("BUG: state file 'last' does not exist");
392 state
->last
= strtol(sb
.buf
, NULL
, 10);
394 if (read_author_script(state
) < 0)
395 die(_("could not parse author script"));
397 read_commit_msg(state
);
399 if (read_state_file(&sb
, state
, "original-commit", 1) < 0)
400 hashclr(state
->orig_commit
);
401 else if (get_sha1_hex(sb
.buf
, state
->orig_commit
) < 0)
402 die(_("could not parse %s"), am_path(state
, "original-commit"));
404 read_state_file(&sb
, state
, "threeway", 1);
405 state
->threeway
= !strcmp(sb
.buf
, "t");
407 read_state_file(&sb
, state
, "quiet", 1);
408 state
->quiet
= !strcmp(sb
.buf
, "t");
410 read_state_file(&sb
, state
, "sign", 1);
411 state
->signoff
= !strcmp(sb
.buf
, "t");
413 read_state_file(&sb
, state
, "utf8", 1);
414 state
->utf8
= !strcmp(sb
.buf
, "t");
416 read_state_file(&sb
, state
, "keep", 1);
417 if (!strcmp(sb
.buf
, "t"))
418 state
->keep
= KEEP_TRUE
;
419 else if (!strcmp(sb
.buf
, "b"))
420 state
->keep
= KEEP_NON_PATCH
;
422 state
->keep
= KEEP_FALSE
;
424 read_state_file(&sb
, state
, "messageid", 1);
425 state
->message_id
= !strcmp(sb
.buf
, "t");
427 read_state_file(&sb
, state
, "scissors", 1);
428 if (!strcmp(sb
.buf
, "t"))
429 state
->scissors
= SCISSORS_TRUE
;
430 else if (!strcmp(sb
.buf
, "f"))
431 state
->scissors
= SCISSORS_FALSE
;
433 state
->scissors
= SCISSORS_UNSET
;
435 read_state_file(&sb
, state
, "apply-opt", 1);
436 argv_array_clear(&state
->git_apply_opts
);
437 if (sq_dequote_to_argv_array(sb
.buf
, &state
->git_apply_opts
) < 0)
438 die(_("could not parse %s"), am_path(state
, "apply-opt"));
440 state
->rebasing
= !!file_exists(am_path(state
, "rebasing"));
446 * Removes the am_state directory, forcefully terminating the current am
449 static void am_destroy(const struct am_state
*state
)
451 struct strbuf sb
= STRBUF_INIT
;
453 strbuf_addstr(&sb
, state
->dir
);
454 remove_dir_recursively(&sb
, 0);
459 * Runs applypatch-msg hook. Returns its exit code.
461 static int run_applypatch_msg_hook(struct am_state
*state
)
466 ret
= run_hook_le(NULL
, "applypatch-msg", am_path(state
, "final-commit"), NULL
);
471 if (read_commit_msg(state
) < 0)
472 die(_("'%s' was deleted by the applypatch-msg hook"),
473 am_path(state
, "final-commit"));
480 * Runs post-rewrite hook. Returns it exit code.
482 static int run_post_rewrite_hook(const struct am_state
*state
)
484 struct child_process cp
= CHILD_PROCESS_INIT
;
485 const char *hook
= find_hook("post-rewrite");
491 argv_array_push(&cp
.args
, hook
);
492 argv_array_push(&cp
.args
, "rebase");
494 cp
.in
= xopen(am_path(state
, "rewritten"), O_RDONLY
);
495 cp
.stdout_to_stderr
= 1;
497 ret
= run_command(&cp
);
504 * Reads the state directory's "rewritten" file, and copies notes from the old
505 * commits listed in the file to their rewritten commits.
507 * Returns 0 on success, -1 on failure.
509 static int copy_notes_for_rebase(const struct am_state
*state
)
511 struct notes_rewrite_cfg
*c
;
512 struct strbuf sb
= STRBUF_INIT
;
513 const char *invalid_line
= _("Malformed input line: '%s'.");
514 const char *msg
= "Notes added by 'git rebase'";
518 assert(state
->rebasing
);
520 c
= init_copy_notes_for_rewrite("rebase");
524 fp
= xfopen(am_path(state
, "rewritten"), "r");
526 while (!strbuf_getline(&sb
, fp
, '\n')) {
527 unsigned char from_obj
[GIT_SHA1_RAWSZ
], to_obj
[GIT_SHA1_RAWSZ
];
529 if (sb
.len
!= GIT_SHA1_HEXSZ
* 2 + 1) {
530 ret
= error(invalid_line
, sb
.buf
);
534 if (get_sha1_hex(sb
.buf
, from_obj
)) {
535 ret
= error(invalid_line
, sb
.buf
);
539 if (sb
.buf
[GIT_SHA1_HEXSZ
] != ' ') {
540 ret
= error(invalid_line
, sb
.buf
);
544 if (get_sha1_hex(sb
.buf
+ GIT_SHA1_HEXSZ
+ 1, to_obj
)) {
545 ret
= error(invalid_line
, sb
.buf
);
549 if (copy_note_for_rewrite(c
, from_obj
, to_obj
))
550 ret
= error(_("Failed to copy notes from '%s' to '%s'"),
551 sha1_to_hex(from_obj
), sha1_to_hex(to_obj
));
555 finish_copy_notes_for_rewrite(c
, msg
);
562 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
563 * non-indented lines and checking if they look like they begin with valid
564 * header field names.
566 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
568 static int is_mail(FILE *fp
)
570 const char *header_regex
= "^[!-9;-~]+:";
571 struct strbuf sb
= STRBUF_INIT
;
575 if (fseek(fp
, 0L, SEEK_SET
))
576 die_errno(_("fseek failed"));
578 if (regcomp(®ex
, header_regex
, REG_NOSUB
| REG_EXTENDED
))
579 die("invalid pattern: %s", header_regex
);
581 while (!strbuf_getline_crlf(&sb
, fp
)) {
583 break; /* End of header */
585 /* Ignore indented folded lines */
586 if (*sb
.buf
== '\t' || *sb
.buf
== ' ')
589 /* It's a header if it matches header_regex */
590 if (regexec(®ex
, sb
.buf
, 0, NULL
, 0)) {
603 * Attempts to detect the patch_format of the patches contained in `paths`,
604 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
607 static int detect_patch_format(const char **paths
)
609 enum patch_format ret
= PATCH_FORMAT_UNKNOWN
;
610 struct strbuf l1
= STRBUF_INIT
;
614 * We default to mbox format if input is from stdin and for directories
616 if (!*paths
|| !strcmp(*paths
, "-") || is_directory(*paths
))
617 return PATCH_FORMAT_MBOX
;
620 * Otherwise, check the first few lines of the first patch, starting
621 * from the first non-blank line, to try to detect its format.
624 fp
= xfopen(*paths
, "r");
626 while (!strbuf_getline_crlf(&l1
, fp
)) {
631 if (starts_with(l1
.buf
, "From ") || starts_with(l1
.buf
, "From: ")) {
632 ret
= PATCH_FORMAT_MBOX
;
636 if (l1
.len
&& is_mail(fp
)) {
637 ret
= PATCH_FORMAT_MBOX
;
648 * Splits out individual email patches from `paths`, where each path is either
649 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
651 static int split_mail_mbox(struct am_state
*state
, const char **paths
, int keep_cr
)
653 struct child_process cp
= CHILD_PROCESS_INIT
;
654 struct strbuf last
= STRBUF_INIT
;
657 argv_array_push(&cp
.args
, "mailsplit");
658 argv_array_pushf(&cp
.args
, "-d%d", state
->prec
);
659 argv_array_pushf(&cp
.args
, "-o%s", state
->dir
);
660 argv_array_push(&cp
.args
, "-b");
662 argv_array_push(&cp
.args
, "--keep-cr");
663 argv_array_push(&cp
.args
, "--");
664 argv_array_pushv(&cp
.args
, paths
);
666 if (capture_command(&cp
, &last
, 8))
670 state
->last
= strtol(last
.buf
, NULL
, 10);
676 * Splits a list of files/directories into individual email patches. Each path
677 * in `paths` must be a file/directory that is formatted according to
680 * Once split out, the individual email patches will be stored in the state
681 * directory, with each patch's filename being its index, padded to state->prec
684 * state->cur will be set to the index of the first mail, and state->last will
685 * be set to the index of the last mail.
687 * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1
688 * to disable this behavior, -1 to use the default configured setting.
690 * Returns 0 on success, -1 on failure.
692 static int split_mail(struct am_state
*state
, enum patch_format patch_format
,
693 const char **paths
, int keep_cr
)
697 git_config_get_bool("am.keepcr", &keep_cr
);
700 switch (patch_format
) {
701 case PATCH_FORMAT_MBOX
:
702 return split_mail_mbox(state
, paths
, keep_cr
);
704 die("BUG: invalid patch_format");
710 * Setup a new am session for applying patches
712 static void am_setup(struct am_state
*state
, enum patch_format patch_format
,
713 const char **paths
, int keep_cr
)
715 unsigned char curr_head
[GIT_SHA1_RAWSZ
];
717 struct strbuf sb
= STRBUF_INIT
;
720 patch_format
= detect_patch_format(paths
);
723 fprintf_ln(stderr
, _("Patch format detection failed."));
727 if (mkdir(state
->dir
, 0777) < 0 && errno
!= EEXIST
)
728 die_errno(_("failed to create directory '%s'"), state
->dir
);
730 if (split_mail(state
, patch_format
, paths
, keep_cr
) < 0) {
732 die(_("Failed to split patches."));
738 write_file(am_path(state
, "threeway"), 1, state
->threeway
? "t" : "f");
740 write_file(am_path(state
, "quiet"), 1, state
->quiet
? "t" : "f");
742 write_file(am_path(state
, "sign"), 1, state
->signoff
? "t" : "f");
744 write_file(am_path(state
, "utf8"), 1, state
->utf8
? "t" : "f");
746 switch (state
->keep
) {
757 die("BUG: invalid value for state->keep");
760 write_file(am_path(state
, "keep"), 1, "%s", str
);
762 write_file(am_path(state
, "messageid"), 1, state
->message_id
? "t" : "f");
764 switch (state
->scissors
) {
775 die("BUG: invalid value for state->scissors");
778 write_file(am_path(state
, "scissors"), 1, "%s", str
);
780 sq_quote_argv(&sb
, state
->git_apply_opts
.argv
, 0);
781 write_file(am_path(state
, "apply-opt"), 1, "%s", sb
.buf
);
784 write_file(am_path(state
, "rebasing"), 1, "%s", "");
786 write_file(am_path(state
, "applying"), 1, "%s", "");
788 if (!get_sha1("HEAD", curr_head
)) {
789 write_file(am_path(state
, "abort-safety"), 1, "%s", sha1_to_hex(curr_head
));
790 if (!state
->rebasing
)
791 update_ref("am", "ORIG_HEAD", curr_head
, NULL
, 0,
792 UPDATE_REFS_DIE_ON_ERR
);
794 write_file(am_path(state
, "abort-safety"), 1, "%s", "");
795 if (!state
->rebasing
)
796 delete_ref("ORIG_HEAD", NULL
, 0);
800 * NOTE: Since the "next" and "last" files determine if an am_state
801 * session is in progress, they should be written last.
804 write_file(am_path(state
, "next"), 1, "%d", state
->cur
);
806 write_file(am_path(state
, "last"), 1, "%d", state
->last
);
812 * Increments the patch pointer, and cleans am_state for the application of the
815 static void am_next(struct am_state
*state
)
817 unsigned char head
[GIT_SHA1_RAWSZ
];
819 free(state
->author_name
);
820 state
->author_name
= NULL
;
822 free(state
->author_email
);
823 state
->author_email
= NULL
;
825 free(state
->author_date
);
826 state
->author_date
= NULL
;
832 unlink(am_path(state
, "author-script"));
833 unlink(am_path(state
, "final-commit"));
835 hashclr(state
->orig_commit
);
836 unlink(am_path(state
, "original-commit"));
838 if (!get_sha1("HEAD", head
))
839 write_file(am_path(state
, "abort-safety"), 1, "%s", sha1_to_hex(head
));
841 write_file(am_path(state
, "abort-safety"), 1, "%s", "");
844 write_file(am_path(state
, "next"), 1, "%d", state
->cur
);
848 * Returns the filename of the current patch email.
850 static const char *msgnum(const struct am_state
*state
)
852 static struct strbuf sb
= STRBUF_INIT
;
855 strbuf_addf(&sb
, "%0*d", state
->prec
, state
->cur
);
861 * Refresh and write index.
863 static void refresh_and_write_cache(void)
865 struct lock_file
*lock_file
= xcalloc(1, sizeof(struct lock_file
));
867 hold_locked_index(lock_file
, 1);
868 refresh_cache(REFRESH_QUIET
);
869 if (write_locked_index(&the_index
, lock_file
, COMMIT_LOCK
))
870 die(_("unable to write index file"));
874 * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
875 * branch, returns 1 if there are entries in the index, 0 otherwise. If an
876 * strbuf is provided, the space-separated list of files that differ will be
879 static int index_has_changes(struct strbuf
*sb
)
881 unsigned char head
[GIT_SHA1_RAWSZ
];
884 if (!get_sha1_tree("HEAD", head
)) {
885 struct diff_options opt
;
888 DIFF_OPT_SET(&opt
, EXIT_WITH_STATUS
);
890 DIFF_OPT_SET(&opt
, QUICK
);
891 do_diff_cache(head
, &opt
);
893 for (i
= 0; sb
&& i
< diff_queued_diff
.nr
; i
++) {
895 strbuf_addch(sb
, ' ');
896 strbuf_addstr(sb
, diff_queued_diff
.queue
[i
]->two
->path
);
899 return DIFF_OPT_TST(&opt
, HAS_CHANGES
) != 0;
901 for (i
= 0; sb
&& i
< active_nr
; i
++) {
903 strbuf_addch(sb
, ' ');
904 strbuf_addstr(sb
, active_cache
[i
]->name
);
911 * Dies with a user-friendly message on how to proceed after resolving the
912 * problem. This message can be overridden with state->resolvemsg.
914 static void NORETURN
die_user_resolve(const struct am_state
*state
)
916 if (state
->resolvemsg
) {
917 printf_ln("%s", state
->resolvemsg
);
919 const char *cmdline
= "git am";
921 printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline
);
922 printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline
);
923 printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline
);
930 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
931 * state->msg will be set to the patch message. state->author_name,
932 * state->author_email and state->author_date will be set to the patch author's
933 * name, email and date respectively. The patch body will be written to the
934 * state directory's "patch" file.
936 * Returns 1 if the patch should be skipped, 0 otherwise.
938 static int parse_mail(struct am_state
*state
, const char *mail
)
941 struct child_process cp
= CHILD_PROCESS_INIT
;
942 struct strbuf sb
= STRBUF_INIT
;
943 struct strbuf msg
= STRBUF_INIT
;
944 struct strbuf author_name
= STRBUF_INIT
;
945 struct strbuf author_date
= STRBUF_INIT
;
946 struct strbuf author_email
= STRBUF_INIT
;
950 cp
.in
= xopen(mail
, O_RDONLY
, 0);
951 cp
.out
= xopen(am_path(state
, "info"), O_WRONLY
| O_CREAT
, 0777);
953 argv_array_push(&cp
.args
, "mailinfo");
954 argv_array_push(&cp
.args
, state
->utf8
? "-u" : "-n");
956 switch (state
->keep
) {
960 argv_array_push(&cp
.args
, "-k");
963 argv_array_push(&cp
.args
, "-b");
966 die("BUG: invalid value for state->keep");
969 if (state
->message_id
)
970 argv_array_push(&cp
.args
, "-m");
972 switch (state
->scissors
) {
976 argv_array_push(&cp
.args
, "--no-scissors");
979 argv_array_push(&cp
.args
, "--scissors");
982 die("BUG: invalid value for state->scissors");
985 argv_array_push(&cp
.args
, am_path(state
, "msg"));
986 argv_array_push(&cp
.args
, am_path(state
, "patch"));
988 if (run_command(&cp
) < 0)
989 die("could not parse patch");
994 /* Extract message and author information */
995 fp
= xfopen(am_path(state
, "info"), "r");
996 while (!strbuf_getline(&sb
, fp
, '\n')) {
999 if (skip_prefix(sb
.buf
, "Subject: ", &x
)) {
1001 strbuf_addch(&msg
, '\n');
1002 strbuf_addstr(&msg
, x
);
1003 } else if (skip_prefix(sb
.buf
, "Author: ", &x
))
1004 strbuf_addstr(&author_name
, x
);
1005 else if (skip_prefix(sb
.buf
, "Email: ", &x
))
1006 strbuf_addstr(&author_email
, x
);
1007 else if (skip_prefix(sb
.buf
, "Date: ", &x
))
1008 strbuf_addstr(&author_date
, x
);
1012 /* Skip pine's internal folder data */
1013 if (!strcmp(author_name
.buf
, "Mail System Internal Data")) {
1018 if (is_empty_file(am_path(state
, "patch"))) {
1019 printf_ln(_("Patch is empty. Was it split wrong?"));
1020 die_user_resolve(state
);
1023 strbuf_addstr(&msg
, "\n\n");
1024 if (strbuf_read_file(&msg
, am_path(state
, "msg"), 0) < 0)
1025 die_errno(_("could not read '%s'"), am_path(state
, "msg"));
1026 stripspace(&msg
, 0);
1029 append_signoff(&msg
, 0, 0);
1031 assert(!state
->author_name
);
1032 state
->author_name
= strbuf_detach(&author_name
, NULL
);
1034 assert(!state
->author_email
);
1035 state
->author_email
= strbuf_detach(&author_email
, NULL
);
1037 assert(!state
->author_date
);
1038 state
->author_date
= strbuf_detach(&author_date
, NULL
);
1040 assert(!state
->msg
);
1041 state
->msg
= strbuf_detach(&msg
, &state
->msg_len
);
1044 strbuf_release(&msg
);
1045 strbuf_release(&author_date
);
1046 strbuf_release(&author_email
);
1047 strbuf_release(&author_name
);
1048 strbuf_release(&sb
);
1053 * Sets commit_id to the commit hash where the mail was generated from.
1054 * Returns 0 on success, -1 on failure.
1056 static int get_mail_commit_sha1(unsigned char *commit_id
, const char *mail
)
1058 struct strbuf sb
= STRBUF_INIT
;
1059 FILE *fp
= xfopen(mail
, "r");
1062 if (strbuf_getline(&sb
, fp
, '\n'))
1065 if (!skip_prefix(sb
.buf
, "From ", &x
))
1068 if (get_sha1_hex(x
, commit_id
) < 0)
1071 strbuf_release(&sb
);
1077 * Sets state->msg, state->author_name, state->author_email, state->author_date
1078 * to the commit's respective info.
1080 static void get_commit_info(struct am_state
*state
, struct commit
*commit
)
1082 const char *buffer
, *ident_line
, *author_date
, *msg
;
1084 struct ident_split ident_split
;
1085 struct strbuf sb
= STRBUF_INIT
;
1087 buffer
= logmsg_reencode(commit
, NULL
, get_commit_output_encoding());
1089 ident_line
= find_commit_header(buffer
, "author", &ident_len
);
1091 if (split_ident_line(&ident_split
, ident_line
, ident_len
) < 0) {
1092 strbuf_add(&sb
, ident_line
, ident_len
);
1093 die(_("invalid ident line: %s"), sb
.buf
);
1096 assert(!state
->author_name
);
1097 if (ident_split
.name_begin
) {
1098 strbuf_add(&sb
, ident_split
.name_begin
,
1099 ident_split
.name_end
- ident_split
.name_begin
);
1100 state
->author_name
= strbuf_detach(&sb
, NULL
);
1102 state
->author_name
= xstrdup("");
1104 assert(!state
->author_email
);
1105 if (ident_split
.mail_begin
) {
1106 strbuf_add(&sb
, ident_split
.mail_begin
,
1107 ident_split
.mail_end
- ident_split
.mail_begin
);
1108 state
->author_email
= strbuf_detach(&sb
, NULL
);
1110 state
->author_email
= xstrdup("");
1112 author_date
= show_ident_date(&ident_split
, DATE_MODE(NORMAL
));
1113 strbuf_addstr(&sb
, author_date
);
1114 assert(!state
->author_date
);
1115 state
->author_date
= strbuf_detach(&sb
, NULL
);
1117 assert(!state
->msg
);
1118 msg
= strstr(buffer
, "\n\n");
1120 die(_("unable to parse commit %s"), sha1_to_hex(commit
->object
.sha1
));
1121 state
->msg
= xstrdup(msg
+ 2);
1122 state
->msg_len
= strlen(state
->msg
);
1126 * Writes `commit` as a patch to the state directory's "patch" file.
1128 static void write_commit_patch(const struct am_state
*state
, struct commit
*commit
)
1130 struct rev_info rev_info
;
1133 fp
= xfopen(am_path(state
, "patch"), "w");
1134 init_revisions(&rev_info
, NULL
);
1136 rev_info
.abbrev
= 0;
1137 rev_info
.disable_stdin
= 1;
1138 rev_info
.show_root_diff
= 1;
1139 rev_info
.diffopt
.output_format
= DIFF_FORMAT_PATCH
;
1140 rev_info
.no_commit_id
= 1;
1141 DIFF_OPT_SET(&rev_info
.diffopt
, BINARY
);
1142 DIFF_OPT_SET(&rev_info
.diffopt
, FULL_INDEX
);
1143 rev_info
.diffopt
.use_color
= 0;
1144 rev_info
.diffopt
.file
= fp
;
1145 rev_info
.diffopt
.close_file
= 1;
1146 add_pending_object(&rev_info
, &commit
->object
, "");
1147 diff_setup_done(&rev_info
.diffopt
);
1148 log_tree_commit(&rev_info
, commit
);
1152 * Like parse_mail(), but parses the mail by looking up its commit ID
1153 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
1156 * state->orig_commit will be set to the original commit ID.
1158 * Will always return 0 as the patch should never be skipped.
1160 static int parse_mail_rebase(struct am_state
*state
, const char *mail
)
1162 struct commit
*commit
;
1163 unsigned char commit_sha1
[GIT_SHA1_RAWSZ
];
1165 if (get_mail_commit_sha1(commit_sha1
, mail
) < 0)
1166 die(_("could not parse %s"), mail
);
1168 commit
= lookup_commit_or_die(commit_sha1
, mail
);
1170 get_commit_info(state
, commit
);
1172 write_commit_patch(state
, commit
);
1174 hashcpy(state
->orig_commit
, commit_sha1
);
1175 write_file(am_path(state
, "original-commit"), 1, "%s",
1176 sha1_to_hex(commit_sha1
));
1182 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
1183 * `index_file` is not NULL, the patch will be applied to that index.
1185 static int run_apply(const struct am_state
*state
, const char *index_file
)
1187 struct child_process cp
= CHILD_PROCESS_INIT
;
1192 argv_array_pushf(&cp
.env_array
, "GIT_INDEX_FILE=%s", index_file
);
1195 * If we are allowed to fall back on 3-way merge, don't give false
1196 * errors during the initial attempt.
1198 if (state
->threeway
&& !index_file
) {
1203 argv_array_push(&cp
.args
, "apply");
1205 argv_array_pushv(&cp
.args
, state
->git_apply_opts
.argv
);
1208 argv_array_push(&cp
.args
, "--cached");
1210 argv_array_push(&cp
.args
, "--index");
1212 argv_array_push(&cp
.args
, am_path(state
, "patch"));
1214 if (run_command(&cp
))
1217 /* Reload index as git-apply will have modified it. */
1219 read_cache_from(index_file
? index_file
: get_index_file());
1225 * Builds an index that contains just the blobs needed for a 3way merge.
1227 static int build_fake_ancestor(const struct am_state
*state
, const char *index_file
)
1229 struct child_process cp
= CHILD_PROCESS_INIT
;
1232 argv_array_push(&cp
.args
, "apply");
1233 argv_array_pushv(&cp
.args
, state
->git_apply_opts
.argv
);
1234 argv_array_pushf(&cp
.args
, "--build-fake-ancestor=%s", index_file
);
1235 argv_array_push(&cp
.args
, am_path(state
, "patch"));
1237 if (run_command(&cp
))
1244 * Attempt a threeway merge, using index_path as the temporary index.
1246 static int fall_back_threeway(const struct am_state
*state
, const char *index_path
)
1248 unsigned char orig_tree
[GIT_SHA1_RAWSZ
], his_tree
[GIT_SHA1_RAWSZ
],
1249 our_tree
[GIT_SHA1_RAWSZ
];
1250 const unsigned char *bases
[1] = {orig_tree
};
1251 struct merge_options o
;
1252 struct commit
*result
;
1253 char *his_tree_name
;
1255 if (get_sha1("HEAD", our_tree
) < 0)
1256 hashcpy(our_tree
, EMPTY_TREE_SHA1_BIN
);
1258 if (build_fake_ancestor(state
, index_path
))
1259 return error("could not build fake ancestor");
1262 read_cache_from(index_path
);
1264 if (write_index_as_tree(orig_tree
, &the_index
, index_path
, 0, NULL
))
1265 return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1267 say(state
, stdout
, _("Using index info to reconstruct a base tree..."));
1269 if (!state
->quiet
) {
1271 * List paths that needed 3-way fallback, so that the user can
1272 * review them with extra care to spot mismerges.
1274 struct rev_info rev_info
;
1275 const char *diff_filter_str
= "--diff-filter=AM";
1277 init_revisions(&rev_info
, NULL
);
1278 rev_info
.diffopt
.output_format
= DIFF_FORMAT_NAME_STATUS
;
1279 diff_opt_parse(&rev_info
.diffopt
, &diff_filter_str
, 1);
1280 add_pending_sha1(&rev_info
, "HEAD", our_tree
, 0);
1281 diff_setup_done(&rev_info
.diffopt
);
1282 run_diff_index(&rev_info
, 1);
1285 if (run_apply(state
, index_path
))
1286 return error(_("Did you hand edit your patch?\n"
1287 "It does not apply to blobs recorded in its index."));
1289 if (write_index_as_tree(his_tree
, &the_index
, index_path
, 0, NULL
))
1290 return error("could not write tree");
1292 say(state
, stdout
, _("Falling back to patching base and 3-way merge..."));
1298 * This is not so wrong. Depending on which base we picked, orig_tree
1299 * may be wildly different from ours, but his_tree has the same set of
1300 * wildly different changes in parts the patch did not touch, so
1301 * recursive ends up canceling them, saying that we reverted all those
1305 init_merge_options(&o
);
1308 his_tree_name
= xstrfmt("%.*s", linelen(state
->msg
), state
->msg
);
1309 o
.branch2
= his_tree_name
;
1314 if (merge_recursive_generic(&o
, our_tree
, his_tree
, 1, bases
, &result
)) {
1315 free(his_tree_name
);
1316 return error(_("Failed to merge in the changes."));
1319 free(his_tree_name
);
1324 * Commits the current index with state->msg as the commit message and
1325 * state->author_name, state->author_email and state->author_date as the author
1328 static void do_commit(const struct am_state
*state
)
1330 unsigned char tree
[GIT_SHA1_RAWSZ
], parent
[GIT_SHA1_RAWSZ
],
1331 commit
[GIT_SHA1_RAWSZ
];
1333 struct commit_list
*parents
= NULL
;
1334 const char *reflog_msg
, *author
;
1335 struct strbuf sb
= STRBUF_INIT
;
1337 if (run_hook_le(NULL
, "pre-applypatch", NULL
))
1340 if (write_cache_as_tree(tree
, 0, NULL
))
1341 die(_("git write-tree failed to write a tree"));
1343 if (!get_sha1_commit("HEAD", parent
)) {
1345 commit_list_insert(lookup_commit(parent
), &parents
);
1348 say(state
, stderr
, _("applying to an empty history"));
1351 author
= fmt_ident(state
->author_name
, state
->author_email
,
1352 state
->ignore_date
? NULL
: state
->author_date
,
1355 if (state
->committer_date_is_author_date
)
1356 setenv("GIT_COMMITTER_DATE",
1357 state
->ignore_date
? "" : state
->author_date
, 1);
1359 if (commit_tree(state
->msg
, state
->msg_len
, tree
, parents
, commit
,
1360 author
, state
->sign_commit
))
1361 die(_("failed to write commit object"));
1363 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1367 strbuf_addf(&sb
, "%s: %.*s", reflog_msg
, linelen(state
->msg
),
1370 update_ref(sb
.buf
, "HEAD", commit
, ptr
, 0, UPDATE_REFS_DIE_ON_ERR
);
1372 if (state
->rebasing
) {
1373 FILE *fp
= xfopen(am_path(state
, "rewritten"), "a");
1375 assert(!is_null_sha1(state
->orig_commit
));
1376 fprintf(fp
, "%s ", sha1_to_hex(state
->orig_commit
));
1377 fprintf(fp
, "%s\n", sha1_to_hex(commit
));
1381 run_hook_le(NULL
, "post-applypatch", NULL
);
1383 strbuf_release(&sb
);
1387 * Validates the am_state for resuming -- the "msg" and authorship fields must
1390 static void validate_resume_state(const struct am_state
*state
)
1393 die(_("cannot resume: %s does not exist."),
1394 am_path(state
, "final-commit"));
1396 if (!state
->author_name
|| !state
->author_email
|| !state
->author_date
)
1397 die(_("cannot resume: %s does not exist."),
1398 am_path(state
, "author-script"));
1402 * Applies all queued mail.
1404 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
1405 * well as the state directory's "patch" file is used as-is for applying the
1406 * patch and committing it.
1408 static void am_run(struct am_state
*state
, int resume
)
1410 const char *argv_gc_auto
[] = {"gc", "--auto", NULL
};
1411 struct strbuf sb
= STRBUF_INIT
;
1413 unlink(am_path(state
, "dirtyindex"));
1415 refresh_and_write_cache();
1417 if (index_has_changes(&sb
)) {
1418 write_file(am_path(state
, "dirtyindex"), 1, "t");
1419 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb
.buf
);
1422 strbuf_release(&sb
);
1424 while (state
->cur
<= state
->last
) {
1425 const char *mail
= am_path(state
, msgnum(state
));
1428 if (!file_exists(mail
))
1432 validate_resume_state(state
);
1437 if (state
->rebasing
)
1438 skip
= parse_mail_rebase(state
, mail
);
1440 skip
= parse_mail(state
, mail
);
1443 goto next
; /* mail should be skipped */
1445 write_author_script(state
);
1446 write_commit_msg(state
);
1449 if (run_applypatch_msg_hook(state
))
1452 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1454 apply_status
= run_apply(state
, NULL
);
1456 if (apply_status
&& state
->threeway
) {
1457 struct strbuf sb
= STRBUF_INIT
;
1459 strbuf_addstr(&sb
, am_path(state
, "patch-merge-index"));
1460 apply_status
= fall_back_threeway(state
, sb
.buf
);
1461 strbuf_release(&sb
);
1464 * Applying the patch to an earlier tree and merging
1465 * the result may have produced the same tree as ours.
1467 if (!apply_status
&& !index_has_changes(NULL
)) {
1468 say(state
, stdout
, _("No changes -- Patch already applied."));
1474 int advice_amworkdir
= 1;
1476 printf_ln(_("Patch failed at %s %.*s"), msgnum(state
),
1477 linelen(state
->msg
), state
->msg
);
1479 git_config_get_bool("advice.amworkdir", &advice_amworkdir
);
1481 if (advice_amworkdir
)
1482 printf_ln(_("The copy of the patch that failed is found in: %s"),
1483 am_path(state
, "patch"));
1485 die_user_resolve(state
);
1494 if (!is_empty_file(am_path(state
, "rewritten"))) {
1495 assert(state
->rebasing
);
1496 copy_notes_for_rebase(state
);
1497 run_post_rewrite_hook(state
);
1501 * In rebasing mode, it's up to the caller to take care of
1504 if (!state
->rebasing
) {
1506 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);
1511 * Resume the current am session after patch application failure. The user did
1512 * all the hard work, and we do not have to do any patch application. Just
1513 * trust and commit what the user has in the index and working tree.
1515 static void am_resolve(struct am_state
*state
)
1517 validate_resume_state(state
);
1519 say(state
, stdout
, _("Applying: %.*s"), linelen(state
->msg
), state
->msg
);
1521 if (!index_has_changes(NULL
)) {
1522 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1523 "If there is nothing left to stage, chances are that something else\n"
1524 "already introduced the same changes; you might want to skip this patch."));
1525 die_user_resolve(state
);
1528 if (unmerged_cache()) {
1529 printf_ln(_("You still have unmerged paths in your index.\n"
1530 "Did you forget to use 'git add'?"));
1531 die_user_resolve(state
);
1541 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1542 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1545 static int fast_forward_to(struct tree
*head
, struct tree
*remote
, int reset
)
1547 struct lock_file
*lock_file
;
1548 struct unpack_trees_options opts
;
1549 struct tree_desc t
[2];
1551 if (parse_tree(head
) || parse_tree(remote
))
1554 lock_file
= xcalloc(1, sizeof(struct lock_file
));
1555 hold_locked_index(lock_file
, 1);
1557 refresh_cache(REFRESH_QUIET
);
1559 memset(&opts
, 0, sizeof(opts
));
1561 opts
.src_index
= &the_index
;
1562 opts
.dst_index
= &the_index
;
1566 opts
.fn
= twoway_merge
;
1567 init_tree_desc(&t
[0], head
->buffer
, head
->size
);
1568 init_tree_desc(&t
[1], remote
->buffer
, remote
->size
);
1570 if (unpack_trees(2, t
, &opts
)) {
1571 rollback_lock_file(lock_file
);
1575 if (write_locked_index(&the_index
, lock_file
, COMMIT_LOCK
))
1576 die(_("unable to write new index file"));
1582 * Clean the index without touching entries that are not modified between
1583 * `head` and `remote`.
1585 static int clean_index(const unsigned char *head
, const unsigned char *remote
)
1587 struct lock_file
*lock_file
;
1588 struct tree
*head_tree
, *remote_tree
, *index_tree
;
1589 unsigned char index
[GIT_SHA1_RAWSZ
];
1590 struct pathspec pathspec
;
1592 head_tree
= parse_tree_indirect(head
);
1594 return error(_("Could not parse object '%s'."), sha1_to_hex(head
));
1596 remote_tree
= parse_tree_indirect(remote
);
1598 return error(_("Could not parse object '%s'."), sha1_to_hex(remote
));
1600 read_cache_unmerged();
1602 if (fast_forward_to(head_tree
, head_tree
, 1))
1605 if (write_cache_as_tree(index
, 0, NULL
))
1608 index_tree
= parse_tree_indirect(index
);
1610 return error(_("Could not parse object '%s'."), sha1_to_hex(index
));
1612 if (fast_forward_to(index_tree
, remote_tree
, 0))
1615 memset(&pathspec
, 0, sizeof(pathspec
));
1617 lock_file
= xcalloc(1, sizeof(struct lock_file
));
1618 hold_locked_index(lock_file
, 1);
1620 if (read_tree(remote_tree
, 0, &pathspec
)) {
1621 rollback_lock_file(lock_file
);
1625 if (write_locked_index(&the_index
, lock_file
, COMMIT_LOCK
))
1626 die(_("unable to write new index file"));
1628 remove_branch_state();
1634 * Resume the current am session by skipping the current patch.
1636 static void am_skip(struct am_state
*state
)
1638 unsigned char head
[GIT_SHA1_RAWSZ
];
1640 if (get_sha1("HEAD", head
))
1641 hashcpy(head
, EMPTY_TREE_SHA1_BIN
);
1643 if (clean_index(head
, head
))
1644 die(_("failed to clean index"));
1651 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
1653 * It is not safe to reset HEAD when:
1654 * 1. git-am previously failed because the index was dirty.
1655 * 2. HEAD has moved since git-am previously failed.
1657 static int safe_to_abort(const struct am_state
*state
)
1659 struct strbuf sb
= STRBUF_INIT
;
1660 unsigned char abort_safety
[GIT_SHA1_RAWSZ
], head
[GIT_SHA1_RAWSZ
];
1662 if (file_exists(am_path(state
, "dirtyindex")))
1665 if (read_state_file(&sb
, state
, "abort-safety", 1) > 0) {
1666 if (get_sha1_hex(sb
.buf
, abort_safety
))
1667 die(_("could not parse %s"), am_path(state
, "abort_safety"));
1669 hashclr(abort_safety
);
1671 if (get_sha1("HEAD", head
))
1674 if (!hashcmp(head
, abort_safety
))
1677 error(_("You seem to have moved HEAD since the last 'am' failure.\n"
1678 "Not rewinding to ORIG_HEAD"));
1684 * Aborts the current am session if it is safe to do so.
1686 static void am_abort(struct am_state
*state
)
1688 unsigned char curr_head
[GIT_SHA1_RAWSZ
], orig_head
[GIT_SHA1_RAWSZ
];
1689 int has_curr_head
, has_orig_head
;
1692 if (!safe_to_abort(state
)) {
1697 curr_branch
= resolve_refdup("HEAD", 0, curr_head
, NULL
);
1698 has_curr_head
= !is_null_sha1(curr_head
);
1700 hashcpy(curr_head
, EMPTY_TREE_SHA1_BIN
);
1702 has_orig_head
= !get_sha1("ORIG_HEAD", orig_head
);
1704 hashcpy(orig_head
, EMPTY_TREE_SHA1_BIN
);
1706 clean_index(curr_head
, orig_head
);
1709 update_ref("am --abort", "HEAD", orig_head
,
1710 has_curr_head
? curr_head
: NULL
, 0,
1711 UPDATE_REFS_DIE_ON_ERR
);
1712 else if (curr_branch
)
1713 delete_ref(curr_branch
, NULL
, REF_NODEREF
);
1720 * parse_options() callback that validates and sets opt->value to the
1721 * PATCH_FORMAT_* enum value corresponding to `arg`.
1723 static int parse_opt_patchformat(const struct option
*opt
, const char *arg
, int unset
)
1725 int *opt_value
= opt
->value
;
1727 if (!strcmp(arg
, "mbox"))
1728 *opt_value
= PATCH_FORMAT_MBOX
;
1730 return error(_("Invalid value for --patch-format: %s"), arg
);
1742 int cmd_am(int argc
, const char **argv
, const char *prefix
)
1744 struct am_state state
;
1746 int patch_format
= PATCH_FORMAT_UNKNOWN
;
1747 enum resume_mode resume
= RESUME_FALSE
;
1749 const char * const usage
[] = {
1750 N_("git am [options] [(<mbox>|<Maildir>)...]"),
1751 N_("git am [options] (--continue | --skip | --abort)"),
1755 struct option options
[] = {
1756 OPT_BOOL('3', "3way", &state
.threeway
,
1757 N_("allow fall back on 3way merging if needed")),
1758 OPT__QUIET(&state
.quiet
, N_("be quiet")),
1759 OPT_BOOL('s', "signoff", &state
.signoff
,
1760 N_("add a Signed-off-by line to the commit message")),
1761 OPT_BOOL('u', "utf8", &state
.utf8
,
1762 N_("recode into utf8 (default)")),
1763 OPT_SET_INT('k', "keep", &state
.keep
,
1764 N_("pass -k flag to git-mailinfo"), KEEP_TRUE
),
1765 OPT_SET_INT(0, "keep-non-patch", &state
.keep
,
1766 N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH
),
1767 OPT_BOOL('m', "message-id", &state
.message_id
,
1768 N_("pass -m flag to git-mailinfo")),
1769 { OPTION_SET_INT
, 0, "keep-cr", &keep_cr
, NULL
,
1770 N_("pass --keep-cr flag to git-mailsplit for mbox format"),
1771 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 1},
1772 { OPTION_SET_INT
, 0, "no-keep-cr", &keep_cr
, NULL
,
1773 N_("do not pass --keep-cr flag to git-mailsplit independent of am.keepcr"),
1774 PARSE_OPT_NOARG
| PARSE_OPT_NONEG
, NULL
, 0},
1775 OPT_BOOL('c', "scissors", &state
.scissors
,
1776 N_("strip everything before a scissors line")),
1777 OPT_PASSTHRU_ARGV(0, "whitespace", &state
.git_apply_opts
, N_("action"),
1778 N_("pass it through git-apply"),
1780 OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state
.git_apply_opts
, NULL
,
1781 N_("pass it through git-apply"),
1783 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state
.git_apply_opts
, NULL
,
1784 N_("pass it through git-apply"),
1786 OPT_PASSTHRU_ARGV(0, "directory", &state
.git_apply_opts
, N_("root"),
1787 N_("pass it through git-apply"),
1789 OPT_PASSTHRU_ARGV(0, "exclude", &state
.git_apply_opts
, N_("path"),
1790 N_("pass it through git-apply"),
1792 OPT_PASSTHRU_ARGV(0, "include", &state
.git_apply_opts
, N_("path"),
1793 N_("pass it through git-apply"),
1795 OPT_PASSTHRU_ARGV('C', NULL
, &state
.git_apply_opts
, N_("n"),
1796 N_("pass it through git-apply"),
1798 OPT_PASSTHRU_ARGV('p', NULL
, &state
.git_apply_opts
, N_("num"),
1799 N_("pass it through git-apply"),
1801 OPT_CALLBACK(0, "patch-format", &patch_format
, N_("format"),
1802 N_("format the patch(es) are in"),
1803 parse_opt_patchformat
),
1804 OPT_PASSTHRU_ARGV(0, "reject", &state
.git_apply_opts
, NULL
,
1805 N_("pass it through git-apply"),
1807 OPT_STRING(0, "resolvemsg", &state
.resolvemsg
, NULL
,
1808 N_("override error message when patch failure occurs")),
1809 OPT_CMDMODE(0, "continue", &resume
,
1810 N_("continue applying patches after resolving a conflict"),
1812 OPT_CMDMODE('r', "resolved", &resume
,
1813 N_("synonyms for --continue"),
1815 OPT_CMDMODE(0, "skip", &resume
,
1816 N_("skip the current patch"),
1818 OPT_CMDMODE(0, "abort", &resume
,
1819 N_("restore the original branch and abort the patching operation."),
1821 OPT_BOOL(0, "committer-date-is-author-date",
1822 &state
.committer_date_is_author_date
,
1823 N_("lie about committer date")),
1824 OPT_BOOL(0, "ignore-date", &state
.ignore_date
,
1825 N_("use current timestamp for author date")),
1826 { OPTION_STRING
, 'S', "gpg-sign", &state
.sign_commit
, N_("key-id"),
1827 N_("GPG-sign commits"),
1828 PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1829 OPT_HIDDEN_BOOL(0, "rebasing", &state
.rebasing
,
1830 N_("(internal use for git-rebase)")),
1835 * NEEDSWORK: Once all the features of git-am.sh have been
1836 * re-implemented in builtin/am.c, this preamble can be removed.
1838 if (!getenv("_GIT_USE_BUILTIN_AM")) {
1839 const char *path
= mkpath("%s/git-am", git_exec_path());
1841 if (sane_execvp(path
, (char **)argv
) < 0)
1842 die_errno("could not exec %s", path
);
1844 prefix
= setup_git_directory();
1845 trace_repo_setup(prefix
);
1849 git_config(git_default_config
, NULL
);
1851 am_state_init(&state
, git_path("rebase-apply"));
1853 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1855 if (read_index_preload(&the_index
, NULL
) < 0)
1856 die(_("failed to read the index"));
1858 if (am_in_progress(&state
)) {
1860 * Catch user error to feed us patches when there is a session
1863 * 1. mbox path(s) are provided on the command-line.
1864 * 2. stdin is not a tty: the user is trying to feed us a patch
1865 * from standard input. This is somewhat unreliable -- stdin
1866 * could be /dev/null for example and the caller did not
1867 * intend to feed us a patch but wanted to continue
1870 if (argc
|| (resume
== RESUME_FALSE
&& !isatty(0)))
1871 die(_("previous rebase directory %s still exists but mbox given."),
1874 if (resume
== RESUME_FALSE
)
1875 resume
= RESUME_APPLY
;
1879 struct argv_array paths
= ARGV_ARRAY_INIT
;
1883 * Handle stray state directory in the independent-run case. In
1884 * the --rebasing case, it is up to the caller to take care of
1885 * stray directories.
1887 if (file_exists(state
.dir
) && !state
.rebasing
) {
1888 if (resume
== RESUME_ABORT
) {
1890 am_state_release(&state
);
1894 die(_("Stray %s directory found.\n"
1895 "Use \"git am --abort\" to remove it."),
1900 die(_("Resolve operation not in progress, we are not resuming."));
1902 for (i
= 0; i
< argc
; i
++) {
1903 if (is_absolute_path(argv
[i
]) || !prefix
)
1904 argv_array_push(&paths
, argv
[i
]);
1906 argv_array_push(&paths
, mkpath("%s/%s", prefix
, argv
[i
]));
1909 am_setup(&state
, patch_format
, paths
.argv
, keep_cr
);
1911 argv_array_clear(&paths
);
1921 case RESUME_RESOLVED
:
1931 die("BUG: invalid resume value");
1934 am_state_release(&state
);