builtin-am: implement committing applied patch
[git/mingw.git] / builtin / am.c
bloba2811b687a76bfc0f6829ee781270fd446618028
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"
18 /**
19 * Returns 1 if the file is empty or does not exist, 0 otherwise.
21 static int is_empty_file(const char *filename)
23 struct stat st;
25 if (stat(filename, &st) < 0) {
26 if (errno == ENOENT)
27 return 1;
28 die_errno(_("could not stat %s"), filename);
31 return !st.st_size;
34 /**
35 * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
37 static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
39 if (strbuf_getwholeline(sb, fp, '\n'))
40 return EOF;
41 if (sb->buf[sb->len - 1] == '\n') {
42 strbuf_setlen(sb, sb->len - 1);
43 if (sb->len > 0 && sb->buf[sb->len - 1] == '\r')
44 strbuf_setlen(sb, sb->len - 1);
46 return 0;
49 /**
50 * Returns the length of the first line of msg.
52 static int linelen(const char *msg)
54 return strchrnul(msg, '\n') - msg;
57 enum patch_format {
58 PATCH_FORMAT_UNKNOWN = 0,
59 PATCH_FORMAT_MBOX
62 struct am_state {
63 /* state directory path */
64 char *dir;
66 /* current and last patch numbers, 1-indexed */
67 int cur;
68 int last;
70 /* commit metadata and message */
71 char *author_name;
72 char *author_email;
73 char *author_date;
74 char *msg;
75 size_t msg_len;
77 /* number of digits in patch filename */
78 int prec;
81 /**
82 * Initializes am_state with the default values. The state directory is set to
83 * dir.
85 static void am_state_init(struct am_state *state, const char *dir)
87 memset(state, 0, sizeof(*state));
89 assert(dir);
90 state->dir = xstrdup(dir);
92 state->prec = 4;
95 /**
96 * Releases memory allocated by an am_state.
98 static void am_state_release(struct am_state *state)
100 free(state->dir);
101 free(state->author_name);
102 free(state->author_email);
103 free(state->author_date);
104 free(state->msg);
108 * Returns path relative to the am_state directory.
110 static inline const char *am_path(const struct am_state *state, const char *path)
112 return mkpath("%s/%s", state->dir, path);
116 * Returns 1 if there is an am session in progress, 0 otherwise.
118 static int am_in_progress(const struct am_state *state)
120 struct stat st;
122 if (lstat(state->dir, &st) < 0 || !S_ISDIR(st.st_mode))
123 return 0;
124 if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode))
125 return 0;
126 if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode))
127 return 0;
128 return 1;
132 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
133 * number of bytes read on success, -1 if the file does not exist. If `trim` is
134 * set, trailing whitespace will be removed.
136 static int read_state_file(struct strbuf *sb, const struct am_state *state,
137 const char *file, int trim)
139 strbuf_reset(sb);
141 if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) {
142 if (trim)
143 strbuf_trim(sb);
145 return sb->len;
148 if (errno == ENOENT)
149 return -1;
151 die_errno(_("could not read '%s'"), am_path(state, file));
155 * Reads a KEY=VALUE shell variable assignment from `fp`, returning the VALUE
156 * as a newly-allocated string. VALUE must be a quoted string, and the KEY must
157 * match `key`. Returns NULL on failure.
159 * This is used by read_author_script() to read the GIT_AUTHOR_* variables from
160 * the author-script.
162 static char *read_shell_var(FILE *fp, const char *key)
164 struct strbuf sb = STRBUF_INIT;
165 const char *str;
167 if (strbuf_getline(&sb, fp, '\n'))
168 goto fail;
170 if (!skip_prefix(sb.buf, key, &str))
171 goto fail;
173 if (!skip_prefix(str, "=", &str))
174 goto fail;
176 strbuf_remove(&sb, 0, str - sb.buf);
178 str = sq_dequote(sb.buf);
179 if (!str)
180 goto fail;
182 return strbuf_detach(&sb, NULL);
184 fail:
185 strbuf_release(&sb);
186 return NULL;
190 * Reads and parses the state directory's "author-script" file, and sets
191 * state->author_name, state->author_email and state->author_date accordingly.
192 * Returns 0 on success, -1 if the file could not be parsed.
194 * The author script is of the format:
196 * GIT_AUTHOR_NAME='$author_name'
197 * GIT_AUTHOR_EMAIL='$author_email'
198 * GIT_AUTHOR_DATE='$author_date'
200 * where $author_name, $author_email and $author_date are quoted. We are strict
201 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
202 * script, and thus if the file differs from what this function expects, it is
203 * better to bail out than to do something that the user does not expect.
205 static int read_author_script(struct am_state *state)
207 const char *filename = am_path(state, "author-script");
208 FILE *fp;
210 assert(!state->author_name);
211 assert(!state->author_email);
212 assert(!state->author_date);
214 fp = fopen(filename, "r");
215 if (!fp) {
216 if (errno == ENOENT)
217 return 0;
218 die_errno(_("could not open '%s' for reading"), filename);
221 state->author_name = read_shell_var(fp, "GIT_AUTHOR_NAME");
222 if (!state->author_name) {
223 fclose(fp);
224 return -1;
227 state->author_email = read_shell_var(fp, "GIT_AUTHOR_EMAIL");
228 if (!state->author_email) {
229 fclose(fp);
230 return -1;
233 state->author_date = read_shell_var(fp, "GIT_AUTHOR_DATE");
234 if (!state->author_date) {
235 fclose(fp);
236 return -1;
239 if (fgetc(fp) != EOF) {
240 fclose(fp);
241 return -1;
244 fclose(fp);
245 return 0;
249 * Saves state->author_name, state->author_email and state->author_date in the
250 * state directory's "author-script" file.
252 static void write_author_script(const struct am_state *state)
254 struct strbuf sb = STRBUF_INIT;
256 strbuf_addstr(&sb, "GIT_AUTHOR_NAME=");
257 sq_quote_buf(&sb, state->author_name);
258 strbuf_addch(&sb, '\n');
260 strbuf_addstr(&sb, "GIT_AUTHOR_EMAIL=");
261 sq_quote_buf(&sb, state->author_email);
262 strbuf_addch(&sb, '\n');
264 strbuf_addstr(&sb, "GIT_AUTHOR_DATE=");
265 sq_quote_buf(&sb, state->author_date);
266 strbuf_addch(&sb, '\n');
268 write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
270 strbuf_release(&sb);
274 * Reads the commit message from the state directory's "final-commit" file,
275 * setting state->msg to its contents and state->msg_len to the length of its
276 * contents in bytes.
278 * Returns 0 on success, -1 if the file does not exist.
280 static int read_commit_msg(struct am_state *state)
282 struct strbuf sb = STRBUF_INIT;
284 assert(!state->msg);
286 if (read_state_file(&sb, state, "final-commit", 0) < 0) {
287 strbuf_release(&sb);
288 return -1;
291 state->msg = strbuf_detach(&sb, &state->msg_len);
292 return 0;
296 * Saves state->msg in the state directory's "final-commit" file.
298 static void write_commit_msg(const struct am_state *state)
300 int fd;
301 const char *filename = am_path(state, "final-commit");
303 fd = xopen(filename, O_WRONLY | O_CREAT, 0666);
304 if (write_in_full(fd, state->msg, state->msg_len) < 0)
305 die_errno(_("could not write to %s"), filename);
306 close(fd);
310 * Loads state from disk.
312 static void am_load(struct am_state *state)
314 struct strbuf sb = STRBUF_INIT;
316 if (read_state_file(&sb, state, "next", 1) < 0)
317 die("BUG: state file 'next' does not exist");
318 state->cur = strtol(sb.buf, NULL, 10);
320 if (read_state_file(&sb, state, "last", 1) < 0)
321 die("BUG: state file 'last' does not exist");
322 state->last = strtol(sb.buf, NULL, 10);
324 if (read_author_script(state) < 0)
325 die(_("could not parse author script"));
327 read_commit_msg(state);
329 strbuf_release(&sb);
333 * Removes the am_state directory, forcefully terminating the current am
334 * session.
336 static void am_destroy(const struct am_state *state)
338 struct strbuf sb = STRBUF_INIT;
340 strbuf_addstr(&sb, state->dir);
341 remove_dir_recursively(&sb, 0);
342 strbuf_release(&sb);
346 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
347 * non-indented lines and checking if they look like they begin with valid
348 * header field names.
350 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
352 static int is_mail(FILE *fp)
354 const char *header_regex = "^[!-9;-~]+:";
355 struct strbuf sb = STRBUF_INIT;
356 regex_t regex;
357 int ret = 1;
359 if (fseek(fp, 0L, SEEK_SET))
360 die_errno(_("fseek failed"));
362 if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
363 die("invalid pattern: %s", header_regex);
365 while (!strbuf_getline_crlf(&sb, fp)) {
366 if (!sb.len)
367 break; /* End of header */
369 /* Ignore indented folded lines */
370 if (*sb.buf == '\t' || *sb.buf == ' ')
371 continue;
373 /* It's a header if it matches header_regex */
374 if (regexec(&regex, sb.buf, 0, NULL, 0)) {
375 ret = 0;
376 goto done;
380 done:
381 regfree(&regex);
382 strbuf_release(&sb);
383 return ret;
387 * Attempts to detect the patch_format of the patches contained in `paths`,
388 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
389 * detection fails.
391 static int detect_patch_format(const char **paths)
393 enum patch_format ret = PATCH_FORMAT_UNKNOWN;
394 struct strbuf l1 = STRBUF_INIT;
395 FILE *fp;
398 * We default to mbox format if input is from stdin and for directories
400 if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
401 return PATCH_FORMAT_MBOX;
404 * Otherwise, check the first few lines of the first patch, starting
405 * from the first non-blank line, to try to detect its format.
408 fp = xfopen(*paths, "r");
410 while (!strbuf_getline_crlf(&l1, fp)) {
411 if (l1.len)
412 break;
415 if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) {
416 ret = PATCH_FORMAT_MBOX;
417 goto done;
420 if (l1.len && is_mail(fp)) {
421 ret = PATCH_FORMAT_MBOX;
422 goto done;
425 done:
426 fclose(fp);
427 strbuf_release(&l1);
428 return ret;
432 * Splits out individual email patches from `paths`, where each path is either
433 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
435 static int split_mail_mbox(struct am_state *state, const char **paths)
437 struct child_process cp = CHILD_PROCESS_INIT;
438 struct strbuf last = STRBUF_INIT;
440 cp.git_cmd = 1;
441 argv_array_push(&cp.args, "mailsplit");
442 argv_array_pushf(&cp.args, "-d%d", state->prec);
443 argv_array_pushf(&cp.args, "-o%s", state->dir);
444 argv_array_push(&cp.args, "-b");
445 argv_array_push(&cp.args, "--");
446 argv_array_pushv(&cp.args, paths);
448 if (capture_command(&cp, &last, 8))
449 return -1;
451 state->cur = 1;
452 state->last = strtol(last.buf, NULL, 10);
454 return 0;
458 * Splits a list of files/directories into individual email patches. Each path
459 * in `paths` must be a file/directory that is formatted according to
460 * `patch_format`.
462 * Once split out, the individual email patches will be stored in the state
463 * directory, with each patch's filename being its index, padded to state->prec
464 * digits.
466 * state->cur will be set to the index of the first mail, and state->last will
467 * be set to the index of the last mail.
469 * Returns 0 on success, -1 on failure.
471 static int split_mail(struct am_state *state, enum patch_format patch_format,
472 const char **paths)
474 switch (patch_format) {
475 case PATCH_FORMAT_MBOX:
476 return split_mail_mbox(state, paths);
477 default:
478 die("BUG: invalid patch_format");
480 return -1;
484 * Setup a new am session for applying patches
486 static void am_setup(struct am_state *state, enum patch_format patch_format,
487 const char **paths)
489 if (!patch_format)
490 patch_format = detect_patch_format(paths);
492 if (!patch_format) {
493 fprintf_ln(stderr, _("Patch format detection failed."));
494 exit(128);
497 if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
498 die_errno(_("failed to create directory '%s'"), state->dir);
500 if (split_mail(state, patch_format, paths) < 0) {
501 am_destroy(state);
502 die(_("Failed to split patches."));
506 * NOTE: Since the "next" and "last" files determine if an am_state
507 * session is in progress, they should be written last.
510 write_file(am_path(state, "next"), 1, "%d", state->cur);
512 write_file(am_path(state, "last"), 1, "%d", state->last);
516 * Increments the patch pointer, and cleans am_state for the application of the
517 * next patch.
519 static void am_next(struct am_state *state)
521 free(state->author_name);
522 state->author_name = NULL;
524 free(state->author_email);
525 state->author_email = NULL;
527 free(state->author_date);
528 state->author_date = NULL;
530 free(state->msg);
531 state->msg = NULL;
532 state->msg_len = 0;
534 unlink(am_path(state, "author-script"));
535 unlink(am_path(state, "final-commit"));
537 state->cur++;
538 write_file(am_path(state, "next"), 1, "%d", state->cur);
542 * Returns the filename of the current patch email.
544 static const char *msgnum(const struct am_state *state)
546 static struct strbuf sb = STRBUF_INIT;
548 strbuf_reset(&sb);
549 strbuf_addf(&sb, "%0*d", state->prec, state->cur);
551 return sb.buf;
555 * Refresh and write index.
557 static void refresh_and_write_cache(void)
559 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
561 hold_locked_index(lock_file, 1);
562 refresh_cache(REFRESH_QUIET);
563 if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
564 die(_("unable to write index file"));
568 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
569 * state->msg will be set to the patch message. state->author_name,
570 * state->author_email and state->author_date will be set to the patch author's
571 * name, email and date respectively. The patch body will be written to the
572 * state directory's "patch" file.
574 * Returns 1 if the patch should be skipped, 0 otherwise.
576 static int parse_mail(struct am_state *state, const char *mail)
578 FILE *fp;
579 struct child_process cp = CHILD_PROCESS_INIT;
580 struct strbuf sb = STRBUF_INIT;
581 struct strbuf msg = STRBUF_INIT;
582 struct strbuf author_name = STRBUF_INIT;
583 struct strbuf author_date = STRBUF_INIT;
584 struct strbuf author_email = STRBUF_INIT;
585 int ret = 0;
587 cp.git_cmd = 1;
588 cp.in = xopen(mail, O_RDONLY, 0);
589 cp.out = xopen(am_path(state, "info"), O_WRONLY | O_CREAT, 0777);
591 argv_array_push(&cp.args, "mailinfo");
592 argv_array_push(&cp.args, am_path(state, "msg"));
593 argv_array_push(&cp.args, am_path(state, "patch"));
595 if (run_command(&cp) < 0)
596 die("could not parse patch");
598 close(cp.in);
599 close(cp.out);
601 /* Extract message and author information */
602 fp = xfopen(am_path(state, "info"), "r");
603 while (!strbuf_getline(&sb, fp, '\n')) {
604 const char *x;
606 if (skip_prefix(sb.buf, "Subject: ", &x)) {
607 if (msg.len)
608 strbuf_addch(&msg, '\n');
609 strbuf_addstr(&msg, x);
610 } else if (skip_prefix(sb.buf, "Author: ", &x))
611 strbuf_addstr(&author_name, x);
612 else if (skip_prefix(sb.buf, "Email: ", &x))
613 strbuf_addstr(&author_email, x);
614 else if (skip_prefix(sb.buf, "Date: ", &x))
615 strbuf_addstr(&author_date, x);
617 fclose(fp);
619 /* Skip pine's internal folder data */
620 if (!strcmp(author_name.buf, "Mail System Internal Data")) {
621 ret = 1;
622 goto finish;
625 if (is_empty_file(am_path(state, "patch"))) {
626 printf_ln(_("Patch is empty. Was it split wrong?"));
627 exit(128);
630 strbuf_addstr(&msg, "\n\n");
631 if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
632 die_errno(_("could not read '%s'"), am_path(state, "msg"));
633 stripspace(&msg, 0);
635 assert(!state->author_name);
636 state->author_name = strbuf_detach(&author_name, NULL);
638 assert(!state->author_email);
639 state->author_email = strbuf_detach(&author_email, NULL);
641 assert(!state->author_date);
642 state->author_date = strbuf_detach(&author_date, NULL);
644 assert(!state->msg);
645 state->msg = strbuf_detach(&msg, &state->msg_len);
647 finish:
648 strbuf_release(&msg);
649 strbuf_release(&author_date);
650 strbuf_release(&author_email);
651 strbuf_release(&author_name);
652 strbuf_release(&sb);
653 return ret;
657 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise.
659 static int run_apply(const struct am_state *state)
661 struct child_process cp = CHILD_PROCESS_INIT;
663 cp.git_cmd = 1;
665 argv_array_push(&cp.args, "apply");
666 argv_array_push(&cp.args, "--index");
667 argv_array_push(&cp.args, am_path(state, "patch"));
669 if (run_command(&cp))
670 return -1;
672 /* Reload index as git-apply will have modified it. */
673 discard_cache();
674 read_cache();
676 return 0;
680 * Commits the current index with state->msg as the commit message and
681 * state->author_name, state->author_email and state->author_date as the author
682 * information.
684 static void do_commit(const struct am_state *state)
686 unsigned char tree[GIT_SHA1_RAWSZ], parent[GIT_SHA1_RAWSZ],
687 commit[GIT_SHA1_RAWSZ];
688 unsigned char *ptr;
689 struct commit_list *parents = NULL;
690 const char *reflog_msg, *author;
691 struct strbuf sb = STRBUF_INIT;
693 if (write_cache_as_tree(tree, 0, NULL))
694 die(_("git write-tree failed to write a tree"));
696 if (!get_sha1_commit("HEAD", parent)) {
697 ptr = parent;
698 commit_list_insert(lookup_commit(parent), &parents);
699 } else {
700 ptr = NULL;
701 fprintf_ln(stderr, _("applying to an empty history"));
704 author = fmt_ident(state->author_name, state->author_email,
705 state->author_date, IDENT_STRICT);
707 if (commit_tree(state->msg, state->msg_len, tree, parents, commit,
708 author, NULL))
709 die(_("failed to write commit object"));
711 reflog_msg = getenv("GIT_REFLOG_ACTION");
712 if (!reflog_msg)
713 reflog_msg = "am";
715 strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
716 state->msg);
718 update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
720 strbuf_release(&sb);
724 * Applies all queued mail.
726 static void am_run(struct am_state *state)
728 const char *argv_gc_auto[] = {"gc", "--auto", NULL};
730 refresh_and_write_cache();
732 while (state->cur <= state->last) {
733 const char *mail = am_path(state, msgnum(state));
735 if (!file_exists(mail))
736 goto next;
738 if (parse_mail(state, mail))
739 goto next; /* mail should be skipped */
741 write_author_script(state);
742 write_commit_msg(state);
744 printf_ln(_("Applying: %.*s"), linelen(state->msg), state->msg);
746 if (run_apply(state) < 0) {
747 int advice_amworkdir = 1;
749 printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
750 linelen(state->msg), state->msg);
752 git_config_get_bool("advice.amworkdir", &advice_amworkdir);
754 if (advice_amworkdir)
755 printf_ln(_("The copy of the patch that failed is found in: %s"),
756 am_path(state, "patch"));
758 exit(128);
761 do_commit(state);
763 next:
764 am_next(state);
767 am_destroy(state);
768 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
772 * parse_options() callback that validates and sets opt->value to the
773 * PATCH_FORMAT_* enum value corresponding to `arg`.
775 static int parse_opt_patchformat(const struct option *opt, const char *arg, int unset)
777 int *opt_value = opt->value;
779 if (!strcmp(arg, "mbox"))
780 *opt_value = PATCH_FORMAT_MBOX;
781 else
782 return error(_("Invalid value for --patch-format: %s"), arg);
783 return 0;
786 int cmd_am(int argc, const char **argv, const char *prefix)
788 struct am_state state;
789 int patch_format = PATCH_FORMAT_UNKNOWN;
791 const char * const usage[] = {
792 N_("git am [options] [(<mbox>|<Maildir>)...]"),
793 NULL
796 struct option options[] = {
797 OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
798 N_("format the patch(es) are in"),
799 parse_opt_patchformat),
800 OPT_END()
804 * NEEDSWORK: Once all the features of git-am.sh have been
805 * re-implemented in builtin/am.c, this preamble can be removed.
807 if (!getenv("_GIT_USE_BUILTIN_AM")) {
808 const char *path = mkpath("%s/git-am", git_exec_path());
810 if (sane_execvp(path, (char **)argv) < 0)
811 die_errno("could not exec %s", path);
812 } else {
813 prefix = setup_git_directory();
814 trace_repo_setup(prefix);
815 setup_work_tree();
818 git_config(git_default_config, NULL);
820 am_state_init(&state, git_path("rebase-apply"));
822 argc = parse_options(argc, argv, prefix, options, usage, 0);
824 if (read_index_preload(&the_index, NULL) < 0)
825 die(_("failed to read the index"));
827 if (am_in_progress(&state))
828 am_load(&state);
829 else {
830 struct argv_array paths = ARGV_ARRAY_INIT;
831 int i;
833 for (i = 0; i < argc; i++) {
834 if (is_absolute_path(argv[i]) || !prefix)
835 argv_array_push(&paths, argv[i]);
836 else
837 argv_array_push(&paths, mkpath("%s/%s", prefix, argv[i]));
840 am_setup(&state, patch_format, paths.argv);
842 argv_array_clear(&paths);
845 am_run(&state);
847 am_state_release(&state);
849 return 0;