builtin-am: implement -u/--utf8
[git/mingw.git] / builtin / am.c
blob528b2c94f4ed316d0dce5416326e8ae26e00e0cc
1 /*
2 * Builtin "git am"
4 * Based on git-am.sh by Junio C Hamano.
5 */
6 #include "cache.h"
7 #include "builtin.h"
8 #include "exec_cmd.h"
9 #include "parse-options.h"
10 #include "dir.h"
11 #include "run-command.h"
12 #include "quote.h"
13 #include "lockfile.h"
14 #include "cache-tree.h"
15 #include "refs.h"
16 #include "commit.h"
17 #include "diff.h"
18 #include "diffcore.h"
19 #include "unpack-trees.h"
20 #include "branch.h"
21 #include "sequencer.h"
22 #include "revision.h"
23 #include "merge-recursive.h"
24 #include "revision.h"
25 #include "log-tree.h"
27 /**
28 * Returns 1 if the file is empty or does not exist, 0 otherwise.
30 static int is_empty_file(const char *filename)
32 struct stat st;
34 if (stat(filename, &st) < 0) {
35 if (errno == ENOENT)
36 return 1;
37 die_errno(_("could not stat %s"), filename);
40 return !st.st_size;
43 /**
44 * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
46 static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
48 if (strbuf_getwholeline(sb, fp, '\n'))
49 return EOF;
50 if (sb->buf[sb->len - 1] == '\n') {
51 strbuf_setlen(sb, sb->len - 1);
52 if (sb->len > 0 && sb->buf[sb->len - 1] == '\r')
53 strbuf_setlen(sb, sb->len - 1);
55 return 0;
58 /**
59 * Returns the length of the first line of msg.
61 static int linelen(const char *msg)
63 return strchrnul(msg, '\n') - msg;
66 enum patch_format {
67 PATCH_FORMAT_UNKNOWN = 0,
68 PATCH_FORMAT_MBOX
71 struct am_state {
72 /* state directory path */
73 char *dir;
75 /* current and last patch numbers, 1-indexed */
76 int cur;
77 int last;
79 /* commit metadata and message */
80 char *author_name;
81 char *author_email;
82 char *author_date;
83 char *msg;
84 size_t msg_len;
86 /* number of digits in patch filename */
87 int prec;
89 /* various operating modes and command line options */
90 int threeway;
91 int quiet;
92 int signoff;
93 int utf8;
94 const char *resolvemsg;
95 int rebasing;
98 /**
99 * Initializes am_state with the default values. The state directory is set to
100 * dir.
102 static void am_state_init(struct am_state *state, const char *dir)
104 memset(state, 0, sizeof(*state));
106 assert(dir);
107 state->dir = xstrdup(dir);
109 state->prec = 4;
111 state->utf8 = 1;
115 * Releases memory allocated by an am_state.
117 static void am_state_release(struct am_state *state)
119 free(state->dir);
120 free(state->author_name);
121 free(state->author_email);
122 free(state->author_date);
123 free(state->msg);
127 * Returns path relative to the am_state directory.
129 static inline const char *am_path(const struct am_state *state, const char *path)
131 return mkpath("%s/%s", state->dir, path);
135 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
136 * at the end.
138 static void say(const struct am_state *state, FILE *fp, const char *fmt, ...)
140 va_list ap;
142 va_start(ap, fmt);
143 if (!state->quiet) {
144 vfprintf(fp, fmt, ap);
145 putc('\n', fp);
147 va_end(ap);
151 * Returns 1 if there is an am session in progress, 0 otherwise.
153 static int am_in_progress(const struct am_state *state)
155 struct stat st;
157 if (lstat(state->dir, &st) < 0 || !S_ISDIR(st.st_mode))
158 return 0;
159 if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode))
160 return 0;
161 if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode))
162 return 0;
163 return 1;
167 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
168 * number of bytes read on success, -1 if the file does not exist. If `trim` is
169 * set, trailing whitespace will be removed.
171 static int read_state_file(struct strbuf *sb, const struct am_state *state,
172 const char *file, int trim)
174 strbuf_reset(sb);
176 if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) {
177 if (trim)
178 strbuf_trim(sb);
180 return sb->len;
183 if (errno == ENOENT)
184 return -1;
186 die_errno(_("could not read '%s'"), am_path(state, file));
190 * Reads a KEY=VALUE shell variable assignment from `fp`, returning the VALUE
191 * as a newly-allocated string. VALUE must be a quoted string, and the KEY must
192 * match `key`. Returns NULL on failure.
194 * This is used by read_author_script() to read the GIT_AUTHOR_* variables from
195 * the author-script.
197 static char *read_shell_var(FILE *fp, const char *key)
199 struct strbuf sb = STRBUF_INIT;
200 const char *str;
202 if (strbuf_getline(&sb, fp, '\n'))
203 goto fail;
205 if (!skip_prefix(sb.buf, key, &str))
206 goto fail;
208 if (!skip_prefix(str, "=", &str))
209 goto fail;
211 strbuf_remove(&sb, 0, str - sb.buf);
213 str = sq_dequote(sb.buf);
214 if (!str)
215 goto fail;
217 return strbuf_detach(&sb, NULL);
219 fail:
220 strbuf_release(&sb);
221 return NULL;
225 * Reads and parses the state directory's "author-script" file, and sets
226 * state->author_name, state->author_email and state->author_date accordingly.
227 * Returns 0 on success, -1 if the file could not be parsed.
229 * The author script is of the format:
231 * GIT_AUTHOR_NAME='$author_name'
232 * GIT_AUTHOR_EMAIL='$author_email'
233 * GIT_AUTHOR_DATE='$author_date'
235 * where $author_name, $author_email and $author_date are quoted. We are strict
236 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
237 * script, and thus if the file differs from what this function expects, it is
238 * better to bail out than to do something that the user does not expect.
240 static int read_author_script(struct am_state *state)
242 const char *filename = am_path(state, "author-script");
243 FILE *fp;
245 assert(!state->author_name);
246 assert(!state->author_email);
247 assert(!state->author_date);
249 fp = fopen(filename, "r");
250 if (!fp) {
251 if (errno == ENOENT)
252 return 0;
253 die_errno(_("could not open '%s' for reading"), filename);
256 state->author_name = read_shell_var(fp, "GIT_AUTHOR_NAME");
257 if (!state->author_name) {
258 fclose(fp);
259 return -1;
262 state->author_email = read_shell_var(fp, "GIT_AUTHOR_EMAIL");
263 if (!state->author_email) {
264 fclose(fp);
265 return -1;
268 state->author_date = read_shell_var(fp, "GIT_AUTHOR_DATE");
269 if (!state->author_date) {
270 fclose(fp);
271 return -1;
274 if (fgetc(fp) != EOF) {
275 fclose(fp);
276 return -1;
279 fclose(fp);
280 return 0;
284 * Saves state->author_name, state->author_email and state->author_date in the
285 * state directory's "author-script" file.
287 static void write_author_script(const struct am_state *state)
289 struct strbuf sb = STRBUF_INIT;
291 strbuf_addstr(&sb, "GIT_AUTHOR_NAME=");
292 sq_quote_buf(&sb, state->author_name);
293 strbuf_addch(&sb, '\n');
295 strbuf_addstr(&sb, "GIT_AUTHOR_EMAIL=");
296 sq_quote_buf(&sb, state->author_email);
297 strbuf_addch(&sb, '\n');
299 strbuf_addstr(&sb, "GIT_AUTHOR_DATE=");
300 sq_quote_buf(&sb, state->author_date);
301 strbuf_addch(&sb, '\n');
303 write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
305 strbuf_release(&sb);
309 * Reads the commit message from the state directory's "final-commit" file,
310 * setting state->msg to its contents and state->msg_len to the length of its
311 * contents in bytes.
313 * Returns 0 on success, -1 if the file does not exist.
315 static int read_commit_msg(struct am_state *state)
317 struct strbuf sb = STRBUF_INIT;
319 assert(!state->msg);
321 if (read_state_file(&sb, state, "final-commit", 0) < 0) {
322 strbuf_release(&sb);
323 return -1;
326 state->msg = strbuf_detach(&sb, &state->msg_len);
327 return 0;
331 * Saves state->msg in the state directory's "final-commit" file.
333 static void write_commit_msg(const struct am_state *state)
335 int fd;
336 const char *filename = am_path(state, "final-commit");
338 fd = xopen(filename, O_WRONLY | O_CREAT, 0666);
339 if (write_in_full(fd, state->msg, state->msg_len) < 0)
340 die_errno(_("could not write to %s"), filename);
341 close(fd);
345 * Loads state from disk.
347 static void am_load(struct am_state *state)
349 struct strbuf sb = STRBUF_INIT;
351 if (read_state_file(&sb, state, "next", 1) < 0)
352 die("BUG: state file 'next' does not exist");
353 state->cur = strtol(sb.buf, NULL, 10);
355 if (read_state_file(&sb, state, "last", 1) < 0)
356 die("BUG: state file 'last' does not exist");
357 state->last = strtol(sb.buf, NULL, 10);
359 if (read_author_script(state) < 0)
360 die(_("could not parse author script"));
362 read_commit_msg(state);
364 read_state_file(&sb, state, "threeway", 1);
365 state->threeway = !strcmp(sb.buf, "t");
367 read_state_file(&sb, state, "quiet", 1);
368 state->quiet = !strcmp(sb.buf, "t");
370 read_state_file(&sb, state, "sign", 1);
371 state->signoff = !strcmp(sb.buf, "t");
373 read_state_file(&sb, state, "utf8", 1);
374 state->utf8 = !strcmp(sb.buf, "t");
376 state->rebasing = !!file_exists(am_path(state, "rebasing"));
378 strbuf_release(&sb);
382 * Removes the am_state directory, forcefully terminating the current am
383 * session.
385 static void am_destroy(const struct am_state *state)
387 struct strbuf sb = STRBUF_INIT;
389 strbuf_addstr(&sb, state->dir);
390 remove_dir_recursively(&sb, 0);
391 strbuf_release(&sb);
395 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
396 * non-indented lines and checking if they look like they begin with valid
397 * header field names.
399 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
401 static int is_mail(FILE *fp)
403 const char *header_regex = "^[!-9;-~]+:";
404 struct strbuf sb = STRBUF_INIT;
405 regex_t regex;
406 int ret = 1;
408 if (fseek(fp, 0L, SEEK_SET))
409 die_errno(_("fseek failed"));
411 if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
412 die("invalid pattern: %s", header_regex);
414 while (!strbuf_getline_crlf(&sb, fp)) {
415 if (!sb.len)
416 break; /* End of header */
418 /* Ignore indented folded lines */
419 if (*sb.buf == '\t' || *sb.buf == ' ')
420 continue;
422 /* It's a header if it matches header_regex */
423 if (regexec(&regex, sb.buf, 0, NULL, 0)) {
424 ret = 0;
425 goto done;
429 done:
430 regfree(&regex);
431 strbuf_release(&sb);
432 return ret;
436 * Attempts to detect the patch_format of the patches contained in `paths`,
437 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
438 * detection fails.
440 static int detect_patch_format(const char **paths)
442 enum patch_format ret = PATCH_FORMAT_UNKNOWN;
443 struct strbuf l1 = STRBUF_INIT;
444 FILE *fp;
447 * We default to mbox format if input is from stdin and for directories
449 if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
450 return PATCH_FORMAT_MBOX;
453 * Otherwise, check the first few lines of the first patch, starting
454 * from the first non-blank line, to try to detect its format.
457 fp = xfopen(*paths, "r");
459 while (!strbuf_getline_crlf(&l1, fp)) {
460 if (l1.len)
461 break;
464 if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) {
465 ret = PATCH_FORMAT_MBOX;
466 goto done;
469 if (l1.len && is_mail(fp)) {
470 ret = PATCH_FORMAT_MBOX;
471 goto done;
474 done:
475 fclose(fp);
476 strbuf_release(&l1);
477 return ret;
481 * Splits out individual email patches from `paths`, where each path is either
482 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
484 static int split_mail_mbox(struct am_state *state, const char **paths)
486 struct child_process cp = CHILD_PROCESS_INIT;
487 struct strbuf last = STRBUF_INIT;
489 cp.git_cmd = 1;
490 argv_array_push(&cp.args, "mailsplit");
491 argv_array_pushf(&cp.args, "-d%d", state->prec);
492 argv_array_pushf(&cp.args, "-o%s", state->dir);
493 argv_array_push(&cp.args, "-b");
494 argv_array_push(&cp.args, "--");
495 argv_array_pushv(&cp.args, paths);
497 if (capture_command(&cp, &last, 8))
498 return -1;
500 state->cur = 1;
501 state->last = strtol(last.buf, NULL, 10);
503 return 0;
507 * Splits a list of files/directories into individual email patches. Each path
508 * in `paths` must be a file/directory that is formatted according to
509 * `patch_format`.
511 * Once split out, the individual email patches will be stored in the state
512 * directory, with each patch's filename being its index, padded to state->prec
513 * digits.
515 * state->cur will be set to the index of the first mail, and state->last will
516 * be set to the index of the last mail.
518 * Returns 0 on success, -1 on failure.
520 static int split_mail(struct am_state *state, enum patch_format patch_format,
521 const char **paths)
523 switch (patch_format) {
524 case PATCH_FORMAT_MBOX:
525 return split_mail_mbox(state, paths);
526 default:
527 die("BUG: invalid patch_format");
529 return -1;
533 * Setup a new am session for applying patches
535 static void am_setup(struct am_state *state, enum patch_format patch_format,
536 const char **paths)
538 unsigned char curr_head[GIT_SHA1_RAWSZ];
540 if (!patch_format)
541 patch_format = detect_patch_format(paths);
543 if (!patch_format) {
544 fprintf_ln(stderr, _("Patch format detection failed."));
545 exit(128);
548 if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
549 die_errno(_("failed to create directory '%s'"), state->dir);
551 if (split_mail(state, patch_format, paths) < 0) {
552 am_destroy(state);
553 die(_("Failed to split patches."));
556 if (state->rebasing)
557 state->threeway = 1;
559 write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
561 write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
563 write_file(am_path(state, "sign"), 1, state->signoff ? "t" : "f");
565 write_file(am_path(state, "utf8"), 1, state->utf8 ? "t" : "f");
567 if (state->rebasing)
568 write_file(am_path(state, "rebasing"), 1, "%s", "");
569 else
570 write_file(am_path(state, "applying"), 1, "%s", "");
572 if (!get_sha1("HEAD", curr_head)) {
573 write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
574 if (!state->rebasing)
575 update_ref("am", "ORIG_HEAD", curr_head, NULL, 0,
576 UPDATE_REFS_DIE_ON_ERR);
577 } else {
578 write_file(am_path(state, "abort-safety"), 1, "%s", "");
579 if (!state->rebasing)
580 delete_ref("ORIG_HEAD", NULL, 0);
584 * NOTE: Since the "next" and "last" files determine if an am_state
585 * session is in progress, they should be written last.
588 write_file(am_path(state, "next"), 1, "%d", state->cur);
590 write_file(am_path(state, "last"), 1, "%d", state->last);
594 * Increments the patch pointer, and cleans am_state for the application of the
595 * next patch.
597 static void am_next(struct am_state *state)
599 unsigned char head[GIT_SHA1_RAWSZ];
601 free(state->author_name);
602 state->author_name = NULL;
604 free(state->author_email);
605 state->author_email = NULL;
607 free(state->author_date);
608 state->author_date = NULL;
610 free(state->msg);
611 state->msg = NULL;
612 state->msg_len = 0;
614 unlink(am_path(state, "author-script"));
615 unlink(am_path(state, "final-commit"));
617 if (!get_sha1("HEAD", head))
618 write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
619 else
620 write_file(am_path(state, "abort-safety"), 1, "%s", "");
622 state->cur++;
623 write_file(am_path(state, "next"), 1, "%d", state->cur);
627 * Returns the filename of the current patch email.
629 static const char *msgnum(const struct am_state *state)
631 static struct strbuf sb = STRBUF_INIT;
633 strbuf_reset(&sb);
634 strbuf_addf(&sb, "%0*d", state->prec, state->cur);
636 return sb.buf;
640 * Refresh and write index.
642 static void refresh_and_write_cache(void)
644 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
646 hold_locked_index(lock_file, 1);
647 refresh_cache(REFRESH_QUIET);
648 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
649 die(_("unable to write index file"));
653 * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
654 * branch, returns 1 if there are entries in the index, 0 otherwise. If an
655 * strbuf is provided, the space-separated list of files that differ will be
656 * appended to it.
658 static int index_has_changes(struct strbuf *sb)
660 unsigned char head[GIT_SHA1_RAWSZ];
661 int i;
663 if (!get_sha1_tree("HEAD", head)) {
664 struct diff_options opt;
666 diff_setup(&opt);
667 DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
668 if (!sb)
669 DIFF_OPT_SET(&opt, QUICK);
670 do_diff_cache(head, &opt);
671 diffcore_std(&opt);
672 for (i = 0; sb && i < diff_queued_diff.nr; i++) {
673 if (i)
674 strbuf_addch(sb, ' ');
675 strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
677 diff_flush(&opt);
678 return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0;
679 } else {
680 for (i = 0; sb && i < active_nr; i++) {
681 if (i)
682 strbuf_addch(sb, ' ');
683 strbuf_addstr(sb, active_cache[i]->name);
685 return !!active_nr;
690 * Dies with a user-friendly message on how to proceed after resolving the
691 * problem. This message can be overridden with state->resolvemsg.
693 static void NORETURN die_user_resolve(const struct am_state *state)
695 if (state->resolvemsg) {
696 printf_ln("%s", state->resolvemsg);
697 } else {
698 const char *cmdline = "git am";
700 printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline);
701 printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline);
702 printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline);
705 exit(128);
709 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
710 * state->msg will be set to the patch message. state->author_name,
711 * state->author_email and state->author_date will be set to the patch author's
712 * name, email and date respectively. The patch body will be written to the
713 * state directory's "patch" file.
715 * Returns 1 if the patch should be skipped, 0 otherwise.
717 static int parse_mail(struct am_state *state, const char *mail)
719 FILE *fp;
720 struct child_process cp = CHILD_PROCESS_INIT;
721 struct strbuf sb = STRBUF_INIT;
722 struct strbuf msg = STRBUF_INIT;
723 struct strbuf author_name = STRBUF_INIT;
724 struct strbuf author_date = STRBUF_INIT;
725 struct strbuf author_email = STRBUF_INIT;
726 int ret = 0;
728 cp.git_cmd = 1;
729 cp.in = xopen(mail, O_RDONLY, 0);
730 cp.out = xopen(am_path(state, "info"), O_WRONLY | O_CREAT, 0777);
732 argv_array_push(&cp.args, "mailinfo");
733 argv_array_push(&cp.args, state->utf8 ? "-u" : "-n");
734 argv_array_push(&cp.args, am_path(state, "msg"));
735 argv_array_push(&cp.args, am_path(state, "patch"));
737 if (run_command(&cp) < 0)
738 die("could not parse patch");
740 close(cp.in);
741 close(cp.out);
743 /* Extract message and author information */
744 fp = xfopen(am_path(state, "info"), "r");
745 while (!strbuf_getline(&sb, fp, '\n')) {
746 const char *x;
748 if (skip_prefix(sb.buf, "Subject: ", &x)) {
749 if (msg.len)
750 strbuf_addch(&msg, '\n');
751 strbuf_addstr(&msg, x);
752 } else if (skip_prefix(sb.buf, "Author: ", &x))
753 strbuf_addstr(&author_name, x);
754 else if (skip_prefix(sb.buf, "Email: ", &x))
755 strbuf_addstr(&author_email, x);
756 else if (skip_prefix(sb.buf, "Date: ", &x))
757 strbuf_addstr(&author_date, x);
759 fclose(fp);
761 /* Skip pine's internal folder data */
762 if (!strcmp(author_name.buf, "Mail System Internal Data")) {
763 ret = 1;
764 goto finish;
767 if (is_empty_file(am_path(state, "patch"))) {
768 printf_ln(_("Patch is empty. Was it split wrong?"));
769 die_user_resolve(state);
772 strbuf_addstr(&msg, "\n\n");
773 if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
774 die_errno(_("could not read '%s'"), am_path(state, "msg"));
775 stripspace(&msg, 0);
777 if (state->signoff)
778 append_signoff(&msg, 0, 0);
780 assert(!state->author_name);
781 state->author_name = strbuf_detach(&author_name, NULL);
783 assert(!state->author_email);
784 state->author_email = strbuf_detach(&author_email, NULL);
786 assert(!state->author_date);
787 state->author_date = strbuf_detach(&author_date, NULL);
789 assert(!state->msg);
790 state->msg = strbuf_detach(&msg, &state->msg_len);
792 finish:
793 strbuf_release(&msg);
794 strbuf_release(&author_date);
795 strbuf_release(&author_email);
796 strbuf_release(&author_name);
797 strbuf_release(&sb);
798 return ret;
802 * Sets commit_id to the commit hash where the mail was generated from.
803 * Returns 0 on success, -1 on failure.
805 static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
807 struct strbuf sb = STRBUF_INIT;
808 FILE *fp = xfopen(mail, "r");
809 const char *x;
811 if (strbuf_getline(&sb, fp, '\n'))
812 return -1;
814 if (!skip_prefix(sb.buf, "From ", &x))
815 return -1;
817 if (get_sha1_hex(x, commit_id) < 0)
818 return -1;
820 strbuf_release(&sb);
821 fclose(fp);
822 return 0;
826 * Sets state->msg, state->author_name, state->author_email, state->author_date
827 * to the commit's respective info.
829 static void get_commit_info(struct am_state *state, struct commit *commit)
831 const char *buffer, *ident_line, *author_date, *msg;
832 size_t ident_len;
833 struct ident_split ident_split;
834 struct strbuf sb = STRBUF_INIT;
836 buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding());
838 ident_line = find_commit_header(buffer, "author", &ident_len);
840 if (split_ident_line(&ident_split, ident_line, ident_len) < 0) {
841 strbuf_add(&sb, ident_line, ident_len);
842 die(_("invalid ident line: %s"), sb.buf);
845 assert(!state->author_name);
846 if (ident_split.name_begin) {
847 strbuf_add(&sb, ident_split.name_begin,
848 ident_split.name_end - ident_split.name_begin);
849 state->author_name = strbuf_detach(&sb, NULL);
850 } else
851 state->author_name = xstrdup("");
853 assert(!state->author_email);
854 if (ident_split.mail_begin) {
855 strbuf_add(&sb, ident_split.mail_begin,
856 ident_split.mail_end - ident_split.mail_begin);
857 state->author_email = strbuf_detach(&sb, NULL);
858 } else
859 state->author_email = xstrdup("");
861 author_date = show_ident_date(&ident_split, DATE_MODE(NORMAL));
862 strbuf_addstr(&sb, author_date);
863 assert(!state->author_date);
864 state->author_date = strbuf_detach(&sb, NULL);
866 assert(!state->msg);
867 msg = strstr(buffer, "\n\n");
868 if (!msg)
869 die(_("unable to parse commit %s"), sha1_to_hex(commit->object.sha1));
870 state->msg = xstrdup(msg + 2);
871 state->msg_len = strlen(state->msg);
875 * Writes `commit` as a patch to the state directory's "patch" file.
877 static void write_commit_patch(const struct am_state *state, struct commit *commit)
879 struct rev_info rev_info;
880 FILE *fp;
882 fp = xfopen(am_path(state, "patch"), "w");
883 init_revisions(&rev_info, NULL);
884 rev_info.diff = 1;
885 rev_info.abbrev = 0;
886 rev_info.disable_stdin = 1;
887 rev_info.show_root_diff = 1;
888 rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
889 rev_info.no_commit_id = 1;
890 DIFF_OPT_SET(&rev_info.diffopt, BINARY);
891 DIFF_OPT_SET(&rev_info.diffopt, FULL_INDEX);
892 rev_info.diffopt.use_color = 0;
893 rev_info.diffopt.file = fp;
894 rev_info.diffopt.close_file = 1;
895 add_pending_object(&rev_info, &commit->object, "");
896 diff_setup_done(&rev_info.diffopt);
897 log_tree_commit(&rev_info, commit);
901 * Like parse_mail(), but parses the mail by looking up its commit ID
902 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
903 * of patches.
905 * Will always return 0 as the patch should never be skipped.
907 static int parse_mail_rebase(struct am_state *state, const char *mail)
909 struct commit *commit;
910 unsigned char commit_sha1[GIT_SHA1_RAWSZ];
912 if (get_mail_commit_sha1(commit_sha1, mail) < 0)
913 die(_("could not parse %s"), mail);
915 commit = lookup_commit_or_die(commit_sha1, mail);
917 get_commit_info(state, commit);
919 write_commit_patch(state, commit);
921 return 0;
925 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
926 * `index_file` is not NULL, the patch will be applied to that index.
928 static int run_apply(const struct am_state *state, const char *index_file)
930 struct child_process cp = CHILD_PROCESS_INIT;
932 cp.git_cmd = 1;
934 if (index_file)
935 argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
938 * If we are allowed to fall back on 3-way merge, don't give false
939 * errors during the initial attempt.
941 if (state->threeway && !index_file) {
942 cp.no_stdout = 1;
943 cp.no_stderr = 1;
946 argv_array_push(&cp.args, "apply");
948 if (index_file)
949 argv_array_push(&cp.args, "--cached");
950 else
951 argv_array_push(&cp.args, "--index");
953 argv_array_push(&cp.args, am_path(state, "patch"));
955 if (run_command(&cp))
956 return -1;
958 /* Reload index as git-apply will have modified it. */
959 discard_cache();
960 read_cache_from(index_file ? index_file : get_index_file());
962 return 0;
966 * Builds an index that contains just the blobs needed for a 3way merge.
968 static int build_fake_ancestor(const struct am_state *state, const char *index_file)
970 struct child_process cp = CHILD_PROCESS_INIT;
972 cp.git_cmd = 1;
973 argv_array_push(&cp.args, "apply");
974 argv_array_pushf(&cp.args, "--build-fake-ancestor=%s", index_file);
975 argv_array_push(&cp.args, am_path(state, "patch"));
977 if (run_command(&cp))
978 return -1;
980 return 0;
984 * Attempt a threeway merge, using index_path as the temporary index.
986 static int fall_back_threeway(const struct am_state *state, const char *index_path)
988 unsigned char orig_tree[GIT_SHA1_RAWSZ], his_tree[GIT_SHA1_RAWSZ],
989 our_tree[GIT_SHA1_RAWSZ];
990 const unsigned char *bases[1] = {orig_tree};
991 struct merge_options o;
992 struct commit *result;
993 char *his_tree_name;
995 if (get_sha1("HEAD", our_tree) < 0)
996 hashcpy(our_tree, EMPTY_TREE_SHA1_BIN);
998 if (build_fake_ancestor(state, index_path))
999 return error("could not build fake ancestor");
1001 discard_cache();
1002 read_cache_from(index_path);
1004 if (write_index_as_tree(orig_tree, &the_index, index_path, 0, NULL))
1005 return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1007 say(state, stdout, _("Using index info to reconstruct a base tree..."));
1009 if (!state->quiet) {
1011 * List paths that needed 3-way fallback, so that the user can
1012 * review them with extra care to spot mismerges.
1014 struct rev_info rev_info;
1015 const char *diff_filter_str = "--diff-filter=AM";
1017 init_revisions(&rev_info, NULL);
1018 rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
1019 diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
1020 add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
1021 diff_setup_done(&rev_info.diffopt);
1022 run_diff_index(&rev_info, 1);
1025 if (run_apply(state, index_path))
1026 return error(_("Did you hand edit your patch?\n"
1027 "It does not apply to blobs recorded in its index."));
1029 if (write_index_as_tree(his_tree, &the_index, index_path, 0, NULL))
1030 return error("could not write tree");
1032 say(state, stdout, _("Falling back to patching base and 3-way merge..."));
1034 discard_cache();
1035 read_cache();
1038 * This is not so wrong. Depending on which base we picked, orig_tree
1039 * may be wildly different from ours, but his_tree has the same set of
1040 * wildly different changes in parts the patch did not touch, so
1041 * recursive ends up canceling them, saying that we reverted all those
1042 * changes.
1045 init_merge_options(&o);
1047 o.branch1 = "HEAD";
1048 his_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
1049 o.branch2 = his_tree_name;
1051 if (state->quiet)
1052 o.verbosity = 0;
1054 if (merge_recursive_generic(&o, our_tree, his_tree, 1, bases, &result)) {
1055 free(his_tree_name);
1056 return error(_("Failed to merge in the changes."));
1059 free(his_tree_name);
1060 return 0;
1064 * Commits the current index with state->msg as the commit message and
1065 * state->author_name, state->author_email and state->author_date as the author
1066 * information.
1068 static void do_commit(const struct am_state *state)
1070 unsigned char tree[GIT_SHA1_RAWSZ], parent[GIT_SHA1_RAWSZ],
1071 commit[GIT_SHA1_RAWSZ];
1072 unsigned char *ptr;
1073 struct commit_list *parents = NULL;
1074 const char *reflog_msg, *author;
1075 struct strbuf sb = STRBUF_INIT;
1077 if (write_cache_as_tree(tree, 0, NULL))
1078 die(_("git write-tree failed to write a tree"));
1080 if (!get_sha1_commit("HEAD", parent)) {
1081 ptr = parent;
1082 commit_list_insert(lookup_commit(parent), &parents);
1083 } else {
1084 ptr = NULL;
1085 say(state, stderr, _("applying to an empty history"));
1088 author = fmt_ident(state->author_name, state->author_email,
1089 state->author_date, IDENT_STRICT);
1091 if (commit_tree(state->msg, state->msg_len, tree, parents, commit,
1092 author, NULL))
1093 die(_("failed to write commit object"));
1095 reflog_msg = getenv("GIT_REFLOG_ACTION");
1096 if (!reflog_msg)
1097 reflog_msg = "am";
1099 strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
1100 state->msg);
1102 update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
1104 strbuf_release(&sb);
1108 * Validates the am_state for resuming -- the "msg" and authorship fields must
1109 * be filled up.
1111 static void validate_resume_state(const struct am_state *state)
1113 if (!state->msg)
1114 die(_("cannot resume: %s does not exist."),
1115 am_path(state, "final-commit"));
1117 if (!state->author_name || !state->author_email || !state->author_date)
1118 die(_("cannot resume: %s does not exist."),
1119 am_path(state, "author-script"));
1123 * Applies all queued mail.
1125 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
1126 * well as the state directory's "patch" file is used as-is for applying the
1127 * patch and committing it.
1129 static void am_run(struct am_state *state, int resume)
1131 const char *argv_gc_auto[] = {"gc", "--auto", NULL};
1132 struct strbuf sb = STRBUF_INIT;
1134 unlink(am_path(state, "dirtyindex"));
1136 refresh_and_write_cache();
1138 if (index_has_changes(&sb)) {
1139 write_file(am_path(state, "dirtyindex"), 1, "t");
1140 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
1143 strbuf_release(&sb);
1145 while (state->cur <= state->last) {
1146 const char *mail = am_path(state, msgnum(state));
1147 int apply_status;
1149 if (!file_exists(mail))
1150 goto next;
1152 if (resume) {
1153 validate_resume_state(state);
1154 resume = 0;
1155 } else {
1156 int skip;
1158 if (state->rebasing)
1159 skip = parse_mail_rebase(state, mail);
1160 else
1161 skip = parse_mail(state, mail);
1163 if (skip)
1164 goto next; /* mail should be skipped */
1166 write_author_script(state);
1167 write_commit_msg(state);
1170 say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1172 apply_status = run_apply(state, NULL);
1174 if (apply_status && state->threeway) {
1175 struct strbuf sb = STRBUF_INIT;
1177 strbuf_addstr(&sb, am_path(state, "patch-merge-index"));
1178 apply_status = fall_back_threeway(state, sb.buf);
1179 strbuf_release(&sb);
1182 * Applying the patch to an earlier tree and merging
1183 * the result may have produced the same tree as ours.
1185 if (!apply_status && !index_has_changes(NULL)) {
1186 say(state, stdout, _("No changes -- Patch already applied."));
1187 goto next;
1191 if (apply_status) {
1192 int advice_amworkdir = 1;
1194 printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
1195 linelen(state->msg), state->msg);
1197 git_config_get_bool("advice.amworkdir", &advice_amworkdir);
1199 if (advice_amworkdir)
1200 printf_ln(_("The copy of the patch that failed is found in: %s"),
1201 am_path(state, "patch"));
1203 die_user_resolve(state);
1206 do_commit(state);
1208 next:
1209 am_next(state);
1213 * In rebasing mode, it's up to the caller to take care of
1214 * housekeeping.
1216 if (!state->rebasing) {
1217 am_destroy(state);
1218 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1223 * Resume the current am session after patch application failure. The user did
1224 * all the hard work, and we do not have to do any patch application. Just
1225 * trust and commit what the user has in the index and working tree.
1227 static void am_resolve(struct am_state *state)
1229 validate_resume_state(state);
1231 say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1233 if (!index_has_changes(NULL)) {
1234 printf_ln(_("No changes - did you forget to use 'git add'?\n"
1235 "If there is nothing left to stage, chances are that something else\n"
1236 "already introduced the same changes; you might want to skip this patch."));
1237 die_user_resolve(state);
1240 if (unmerged_cache()) {
1241 printf_ln(_("You still have unmerged paths in your index.\n"
1242 "Did you forget to use 'git add'?"));
1243 die_user_resolve(state);
1246 do_commit(state);
1248 am_next(state);
1249 am_run(state, 0);
1253 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1254 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1255 * failure.
1257 static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1259 struct lock_file *lock_file;
1260 struct unpack_trees_options opts;
1261 struct tree_desc t[2];
1263 if (parse_tree(head) || parse_tree(remote))
1264 return -1;
1266 lock_file = xcalloc(1, sizeof(struct lock_file));
1267 hold_locked_index(lock_file, 1);
1269 refresh_cache(REFRESH_QUIET);
1271 memset(&opts, 0, sizeof(opts));
1272 opts.head_idx = 1;
1273 opts.src_index = &the_index;
1274 opts.dst_index = &the_index;
1275 opts.update = 1;
1276 opts.merge = 1;
1277 opts.reset = reset;
1278 opts.fn = twoway_merge;
1279 init_tree_desc(&t[0], head->buffer, head->size);
1280 init_tree_desc(&t[1], remote->buffer, remote->size);
1282 if (unpack_trees(2, t, &opts)) {
1283 rollback_lock_file(lock_file);
1284 return -1;
1287 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1288 die(_("unable to write new index file"));
1290 return 0;
1294 * Clean the index without touching entries that are not modified between
1295 * `head` and `remote`.
1297 static int clean_index(const unsigned char *head, const unsigned char *remote)
1299 struct lock_file *lock_file;
1300 struct tree *head_tree, *remote_tree, *index_tree;
1301 unsigned char index[GIT_SHA1_RAWSZ];
1302 struct pathspec pathspec;
1304 head_tree = parse_tree_indirect(head);
1305 if (!head_tree)
1306 return error(_("Could not parse object '%s'."), sha1_to_hex(head));
1308 remote_tree = parse_tree_indirect(remote);
1309 if (!remote_tree)
1310 return error(_("Could not parse object '%s'."), sha1_to_hex(remote));
1312 read_cache_unmerged();
1314 if (fast_forward_to(head_tree, head_tree, 1))
1315 return -1;
1317 if (write_cache_as_tree(index, 0, NULL))
1318 return -1;
1320 index_tree = parse_tree_indirect(index);
1321 if (!index_tree)
1322 return error(_("Could not parse object '%s'."), sha1_to_hex(index));
1324 if (fast_forward_to(index_tree, remote_tree, 0))
1325 return -1;
1327 memset(&pathspec, 0, sizeof(pathspec));
1329 lock_file = xcalloc(1, sizeof(struct lock_file));
1330 hold_locked_index(lock_file, 1);
1332 if (read_tree(remote_tree, 0, &pathspec)) {
1333 rollback_lock_file(lock_file);
1334 return -1;
1337 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1338 die(_("unable to write new index file"));
1340 remove_branch_state();
1342 return 0;
1346 * Resume the current am session by skipping the current patch.
1348 static void am_skip(struct am_state *state)
1350 unsigned char head[GIT_SHA1_RAWSZ];
1352 if (get_sha1("HEAD", head))
1353 hashcpy(head, EMPTY_TREE_SHA1_BIN);
1355 if (clean_index(head, head))
1356 die(_("failed to clean index"));
1358 am_next(state);
1359 am_run(state, 0);
1363 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
1365 * It is not safe to reset HEAD when:
1366 * 1. git-am previously failed because the index was dirty.
1367 * 2. HEAD has moved since git-am previously failed.
1369 static int safe_to_abort(const struct am_state *state)
1371 struct strbuf sb = STRBUF_INIT;
1372 unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
1374 if (file_exists(am_path(state, "dirtyindex")))
1375 return 0;
1377 if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
1378 if (get_sha1_hex(sb.buf, abort_safety))
1379 die(_("could not parse %s"), am_path(state, "abort_safety"));
1380 } else
1381 hashclr(abort_safety);
1383 if (get_sha1("HEAD", head))
1384 hashclr(head);
1386 if (!hashcmp(head, abort_safety))
1387 return 1;
1389 error(_("You seem to have moved HEAD since the last 'am' failure.\n"
1390 "Not rewinding to ORIG_HEAD"));
1392 return 0;
1396 * Aborts the current am session if it is safe to do so.
1398 static void am_abort(struct am_state *state)
1400 unsigned char curr_head[GIT_SHA1_RAWSZ], orig_head[GIT_SHA1_RAWSZ];
1401 int has_curr_head, has_orig_head;
1402 char *curr_branch;
1404 if (!safe_to_abort(state)) {
1405 am_destroy(state);
1406 return;
1409 curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
1410 has_curr_head = !is_null_sha1(curr_head);
1411 if (!has_curr_head)
1412 hashcpy(curr_head, EMPTY_TREE_SHA1_BIN);
1414 has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
1415 if (!has_orig_head)
1416 hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
1418 clean_index(curr_head, orig_head);
1420 if (has_orig_head)
1421 update_ref("am --abort", "HEAD", orig_head,
1422 has_curr_head ? curr_head : NULL, 0,
1423 UPDATE_REFS_DIE_ON_ERR);
1424 else if (curr_branch)
1425 delete_ref(curr_branch, NULL, REF_NODEREF);
1427 free(curr_branch);
1428 am_destroy(state);
1432 * parse_options() callback that validates and sets opt->value to the
1433 * PATCH_FORMAT_* enum value corresponding to `arg`.
1435 static int parse_opt_patchformat(const struct option *opt, const char *arg, int unset)
1437 int *opt_value = opt->value;
1439 if (!strcmp(arg, "mbox"))
1440 *opt_value = PATCH_FORMAT_MBOX;
1441 else
1442 return error(_("Invalid value for --patch-format: %s"), arg);
1443 return 0;
1446 enum resume_mode {
1447 RESUME_FALSE = 0,
1448 RESUME_APPLY,
1449 RESUME_RESOLVED,
1450 RESUME_SKIP,
1451 RESUME_ABORT
1454 int cmd_am(int argc, const char **argv, const char *prefix)
1456 struct am_state state;
1457 int patch_format = PATCH_FORMAT_UNKNOWN;
1458 enum resume_mode resume = RESUME_FALSE;
1460 const char * const usage[] = {
1461 N_("git am [options] [(<mbox>|<Maildir>)...]"),
1462 N_("git am [options] (--continue | --skip | --abort)"),
1463 NULL
1466 struct option options[] = {
1467 OPT_BOOL('3', "3way", &state.threeway,
1468 N_("allow fall back on 3way merging if needed")),
1469 OPT__QUIET(&state.quiet, N_("be quiet")),
1470 OPT_BOOL('s', "signoff", &state.signoff,
1471 N_("add a Signed-off-by line to the commit message")),
1472 OPT_BOOL('u', "utf8", &state.utf8,
1473 N_("recode into utf8 (default)")),
1474 OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
1475 N_("format the patch(es) are in"),
1476 parse_opt_patchformat),
1477 OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
1478 N_("override error message when patch failure occurs")),
1479 OPT_CMDMODE(0, "continue", &resume,
1480 N_("continue applying patches after resolving a conflict"),
1481 RESUME_RESOLVED),
1482 OPT_CMDMODE('r', "resolved", &resume,
1483 N_("synonyms for --continue"),
1484 RESUME_RESOLVED),
1485 OPT_CMDMODE(0, "skip", &resume,
1486 N_("skip the current patch"),
1487 RESUME_SKIP),
1488 OPT_CMDMODE(0, "abort", &resume,
1489 N_("restore the original branch and abort the patching operation."),
1490 RESUME_ABORT),
1491 OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing,
1492 N_("(internal use for git-rebase)")),
1493 OPT_END()
1497 * NEEDSWORK: Once all the features of git-am.sh have been
1498 * re-implemented in builtin/am.c, this preamble can be removed.
1500 if (!getenv("_GIT_USE_BUILTIN_AM")) {
1501 const char *path = mkpath("%s/git-am", git_exec_path());
1503 if (sane_execvp(path, (char **)argv) < 0)
1504 die_errno("could not exec %s", path);
1505 } else {
1506 prefix = setup_git_directory();
1507 trace_repo_setup(prefix);
1508 setup_work_tree();
1511 git_config(git_default_config, NULL);
1513 am_state_init(&state, git_path("rebase-apply"));
1515 argc = parse_options(argc, argv, prefix, options, usage, 0);
1517 if (read_index_preload(&the_index, NULL) < 0)
1518 die(_("failed to read the index"));
1520 if (am_in_progress(&state)) {
1522 * Catch user error to feed us patches when there is a session
1523 * in progress:
1525 * 1. mbox path(s) are provided on the command-line.
1526 * 2. stdin is not a tty: the user is trying to feed us a patch
1527 * from standard input. This is somewhat unreliable -- stdin
1528 * could be /dev/null for example and the caller did not
1529 * intend to feed us a patch but wanted to continue
1530 * unattended.
1532 if (argc || (resume == RESUME_FALSE && !isatty(0)))
1533 die(_("previous rebase directory %s still exists but mbox given."),
1534 state.dir);
1536 if (resume == RESUME_FALSE)
1537 resume = RESUME_APPLY;
1539 am_load(&state);
1540 } else {
1541 struct argv_array paths = ARGV_ARRAY_INIT;
1542 int i;
1545 * Handle stray state directory in the independent-run case. In
1546 * the --rebasing case, it is up to the caller to take care of
1547 * stray directories.
1549 if (file_exists(state.dir) && !state.rebasing) {
1550 if (resume == RESUME_ABORT) {
1551 am_destroy(&state);
1552 am_state_release(&state);
1553 return 0;
1556 die(_("Stray %s directory found.\n"
1557 "Use \"git am --abort\" to remove it."),
1558 state.dir);
1561 if (resume)
1562 die(_("Resolve operation not in progress, we are not resuming."));
1564 for (i = 0; i < argc; i++) {
1565 if (is_absolute_path(argv[i]) || !prefix)
1566 argv_array_push(&paths, argv[i]);
1567 else
1568 argv_array_push(&paths, mkpath("%s/%s", prefix, argv[i]));
1571 am_setup(&state, patch_format, paths.argv);
1573 argv_array_clear(&paths);
1576 switch (resume) {
1577 case RESUME_FALSE:
1578 am_run(&state, 0);
1579 break;
1580 case RESUME_APPLY:
1581 am_run(&state, 1);
1582 break;
1583 case RESUME_RESOLVED:
1584 am_resolve(&state);
1585 break;
1586 case RESUME_SKIP:
1587 am_skip(&state);
1588 break;
1589 case RESUME_ABORT:
1590 am_abort(&state);
1591 break;
1592 default:
1593 die("BUG: invalid resume value");
1596 am_state_release(&state);
1598 return 0;