builtin / am.con commit builtin-am: implement -u/--utf8 (ef7ee16)
   1/*
   2 * Builtin "git am"
   3 *
   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"
  26
  27/**
  28 * Returns 1 if the file is empty or does not exist, 0 otherwise.
  29 */
  30static int is_empty_file(const char *filename)
  31{
  32        struct stat st;
  33
  34        if (stat(filename, &st) < 0) {
  35                if (errno == ENOENT)
  36                        return 1;
  37                die_errno(_("could not stat %s"), filename);
  38        }
  39
  40        return !st.st_size;
  41}
  42
  43/**
  44 * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
  45 */
  46static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
  47{
  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);
  54        }
  55        return 0;
  56}
  57
  58/**
  59 * Returns the length of the first line of msg.
  60 */
  61static int linelen(const char *msg)
  62{
  63        return strchrnul(msg, '\n') - msg;
  64}
  65
  66enum patch_format {
  67        PATCH_FORMAT_UNKNOWN = 0,
  68        PATCH_FORMAT_MBOX
  69};
  70
  71struct am_state {
  72        /* state directory path */
  73        char *dir;
  74
  75        /* current and last patch numbers, 1-indexed */
  76        int cur;
  77        int last;
  78
  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;
  85
  86        /* number of digits in patch filename */
  87        int prec;
  88
  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;
  96};
  97
  98/**
  99 * Initializes am_state with the default values. The state directory is set to
 100 * dir.
 101 */
 102static void am_state_init(struct am_state *state, const char *dir)
 103{
 104        memset(state, 0, sizeof(*state));
 105
 106        assert(dir);
 107        state->dir = xstrdup(dir);
 108
 109        state->prec = 4;
 110
 111        state->utf8 = 1;
 112}
 113
 114/**
 115 * Releases memory allocated by an am_state.
 116 */
 117static void am_state_release(struct am_state *state)
 118{
 119        free(state->dir);
 120        free(state->author_name);
 121        free(state->author_email);
 122        free(state->author_date);
 123        free(state->msg);
 124}
 125
 126/**
 127 * Returns path relative to the am_state directory.
 128 */
 129static inline const char *am_path(const struct am_state *state, const char *path)
 130{
 131        return mkpath("%s/%s", state->dir, path);
 132}
 133
 134/**
 135 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
 136 * at the end.
 137 */
 138static void say(const struct am_state *state, FILE *fp, const char *fmt, ...)
 139{
 140        va_list ap;
 141
 142        va_start(ap, fmt);
 143        if (!state->quiet) {
 144                vfprintf(fp, fmt, ap);
 145                putc('\n', fp);
 146        }
 147        va_end(ap);
 148}
 149
 150/**
 151 * Returns 1 if there is an am session in progress, 0 otherwise.
 152 */
 153static int am_in_progress(const struct am_state *state)
 154{
 155        struct stat st;
 156
 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;
 164}
 165
 166/**
 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.
 170 */
 171static int read_state_file(struct strbuf *sb, const struct am_state *state,
 172                        const char *file, int trim)
 173{
 174        strbuf_reset(sb);
 175
 176        if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) {
 177                if (trim)
 178                        strbuf_trim(sb);
 179
 180                return sb->len;
 181        }
 182
 183        if (errno == ENOENT)
 184                return -1;
 185
 186        die_errno(_("could not read '%s'"), am_path(state, file));
 187}
 188
 189/**
 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.
 193 *
 194 * This is used by read_author_script() to read the GIT_AUTHOR_* variables from
 195 * the author-script.
 196 */
 197static char *read_shell_var(FILE *fp, const char *key)
 198{
 199        struct strbuf sb = STRBUF_INIT;
 200        const char *str;
 201
 202        if (strbuf_getline(&sb, fp, '\n'))
 203                goto fail;
 204
 205        if (!skip_prefix(sb.buf, key, &str))
 206                goto fail;
 207
 208        if (!skip_prefix(str, "=", &str))
 209                goto fail;
 210
 211        strbuf_remove(&sb, 0, str - sb.buf);
 212
 213        str = sq_dequote(sb.buf);
 214        if (!str)
 215                goto fail;
 216
 217        return strbuf_detach(&sb, NULL);
 218
 219fail:
 220        strbuf_release(&sb);
 221        return NULL;
 222}
 223
 224/**
 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.
 228 *
 229 * The author script is of the format:
 230 *
 231 *      GIT_AUTHOR_NAME='$author_name'
 232 *      GIT_AUTHOR_EMAIL='$author_email'
 233 *      GIT_AUTHOR_DATE='$author_date'
 234 *
 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.
 239 */
 240static int read_author_script(struct am_state *state)
 241{
 242        const char *filename = am_path(state, "author-script");
 243        FILE *fp;
 244
 245        assert(!state->author_name);
 246        assert(!state->author_email);
 247        assert(!state->author_date);
 248
 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);
 254        }
 255
 256        state->author_name = read_shell_var(fp, "GIT_AUTHOR_NAME");
 257        if (!state->author_name) {
 258                fclose(fp);
 259                return -1;
 260        }
 261
 262        state->author_email = read_shell_var(fp, "GIT_AUTHOR_EMAIL");
 263        if (!state->author_email) {
 264                fclose(fp);
 265                return -1;
 266        }
 267
 268        state->author_date = read_shell_var(fp, "GIT_AUTHOR_DATE");
 269        if (!state->author_date) {
 270                fclose(fp);
 271                return -1;
 272        }
 273
 274        if (fgetc(fp) != EOF) {
 275                fclose(fp);
 276                return -1;
 277        }
 278
 279        fclose(fp);
 280        return 0;
 281}
 282
 283/**
 284 * Saves state->author_name, state->author_email and state->author_date in the
 285 * state directory's "author-script" file.
 286 */
 287static void write_author_script(const struct am_state *state)
 288{
 289        struct strbuf sb = STRBUF_INIT;
 290
 291        strbuf_addstr(&sb, "GIT_AUTHOR_NAME=");
 292        sq_quote_buf(&sb, state->author_name);
 293        strbuf_addch(&sb, '\n');
 294
 295        strbuf_addstr(&sb, "GIT_AUTHOR_EMAIL=");
 296        sq_quote_buf(&sb, state->author_email);
 297        strbuf_addch(&sb, '\n');
 298
 299        strbuf_addstr(&sb, "GIT_AUTHOR_DATE=");
 300        sq_quote_buf(&sb, state->author_date);
 301        strbuf_addch(&sb, '\n');
 302
 303        write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
 304
 305        strbuf_release(&sb);
 306}
 307
 308/**
 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.
 312 *
 313 * Returns 0 on success, -1 if the file does not exist.
 314 */
 315static int read_commit_msg(struct am_state *state)
 316{
 317        struct strbuf sb = STRBUF_INIT;
 318
 319        assert(!state->msg);
 320
 321        if (read_state_file(&sb, state, "final-commit", 0) < 0) {
 322                strbuf_release(&sb);
 323                return -1;
 324        }
 325
 326        state->msg = strbuf_detach(&sb, &state->msg_len);
 327        return 0;
 328}
 329
 330/**
 331 * Saves state->msg in the state directory's "final-commit" file.
 332 */
 333static void write_commit_msg(const struct am_state *state)
 334{
 335        int fd;
 336        const char *filename = am_path(state, "final-commit");
 337
 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);
 342}
 343
 344/**
 345 * Loads state from disk.
 346 */
 347static void am_load(struct am_state *state)
 348{
 349        struct strbuf sb = STRBUF_INIT;
 350
 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);
 354
 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);
 358
 359        if (read_author_script(state) < 0)
 360                die(_("could not parse author script"));
 361
 362        read_commit_msg(state);
 363
 364        read_state_file(&sb, state, "threeway", 1);
 365        state->threeway = !strcmp(sb.buf, "t");
 366
 367        read_state_file(&sb, state, "quiet", 1);
 368        state->quiet = !strcmp(sb.buf, "t");
 369
 370        read_state_file(&sb, state, "sign", 1);
 371        state->signoff = !strcmp(sb.buf, "t");
 372
 373        read_state_file(&sb, state, "utf8", 1);
 374        state->utf8 = !strcmp(sb.buf, "t");
 375
 376        state->rebasing = !!file_exists(am_path(state, "rebasing"));
 377
 378        strbuf_release(&sb);
 379}
 380
 381/**
 382 * Removes the am_state directory, forcefully terminating the current am
 383 * session.
 384 */
 385static void am_destroy(const struct am_state *state)
 386{
 387        struct strbuf sb = STRBUF_INIT;
 388
 389        strbuf_addstr(&sb, state->dir);
 390        remove_dir_recursively(&sb, 0);
 391        strbuf_release(&sb);
 392}
 393
 394/**
 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.
 398 *
 399 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
 400 */
 401static int is_mail(FILE *fp)
 402{
 403        const char *header_regex = "^[!-9;-~]+:";
 404        struct strbuf sb = STRBUF_INIT;
 405        regex_t regex;
 406        int ret = 1;
 407
 408        if (fseek(fp, 0L, SEEK_SET))
 409                die_errno(_("fseek failed"));
 410
 411        if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
 412                die("invalid pattern: %s", header_regex);
 413
 414        while (!strbuf_getline_crlf(&sb, fp)) {
 415                if (!sb.len)
 416                        break; /* End of header */
 417
 418                /* Ignore indented folded lines */
 419                if (*sb.buf == '\t' || *sb.buf == ' ')
 420                        continue;
 421
 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;
 426                }
 427        }
 428
 429done:
 430        regfree(&regex);
 431        strbuf_release(&sb);
 432        return ret;
 433}
 434
 435/**
 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.
 439 */
 440static int detect_patch_format(const char **paths)
 441{
 442        enum patch_format ret = PATCH_FORMAT_UNKNOWN;
 443        struct strbuf l1 = STRBUF_INIT;
 444        FILE *fp;
 445
 446        /*
 447         * We default to mbox format if input is from stdin and for directories
 448         */
 449        if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
 450                return PATCH_FORMAT_MBOX;
 451
 452        /*
 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.
 455         */
 456
 457        fp = xfopen(*paths, "r");
 458
 459        while (!strbuf_getline_crlf(&l1, fp)) {
 460                if (l1.len)
 461                        break;
 462        }
 463
 464        if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) {
 465                ret = PATCH_FORMAT_MBOX;
 466                goto done;
 467        }
 468
 469        if (l1.len && is_mail(fp)) {
 470                ret = PATCH_FORMAT_MBOX;
 471                goto done;
 472        }
 473
 474done:
 475        fclose(fp);
 476        strbuf_release(&l1);
 477        return ret;
 478}
 479
 480/**
 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.
 483 */
 484static int split_mail_mbox(struct am_state *state, const char **paths)
 485{
 486        struct child_process cp = CHILD_PROCESS_INIT;
 487        struct strbuf last = STRBUF_INIT;
 488
 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);
 496
 497        if (capture_command(&cp, &last, 8))
 498                return -1;
 499
 500        state->cur = 1;
 501        state->last = strtol(last.buf, NULL, 10);
 502
 503        return 0;
 504}
 505
 506/**
 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`.
 510 *
 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.
 514 *
 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.
 517 *
 518 * Returns 0 on success, -1 on failure.
 519 */
 520static int split_mail(struct am_state *state, enum patch_format patch_format,
 521                        const char **paths)
 522{
 523        switch (patch_format) {
 524        case PATCH_FORMAT_MBOX:
 525                return split_mail_mbox(state, paths);
 526        default:
 527                die("BUG: invalid patch_format");
 528        }
 529        return -1;
 530}
 531
 532/**
 533 * Setup a new am session for applying patches
 534 */
 535static void am_setup(struct am_state *state, enum patch_format patch_format,
 536                        const char **paths)
 537{
 538        unsigned char curr_head[GIT_SHA1_RAWSZ];
 539
 540        if (!patch_format)
 541                patch_format = detect_patch_format(paths);
 542
 543        if (!patch_format) {
 544                fprintf_ln(stderr, _("Patch format detection failed."));
 545                exit(128);
 546        }
 547
 548        if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
 549                die_errno(_("failed to create directory '%s'"), state->dir);
 550
 551        if (split_mail(state, patch_format, paths) < 0) {
 552                am_destroy(state);
 553                die(_("Failed to split patches."));
 554        }
 555
 556        if (state->rebasing)
 557                state->threeway = 1;
 558
 559        write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
 560
 561        write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
 562
 563        write_file(am_path(state, "sign"), 1, state->signoff ? "t" : "f");
 564
 565        write_file(am_path(state, "utf8"), 1, state->utf8 ? "t" : "f");
 566
 567        if (state->rebasing)
 568                write_file(am_path(state, "rebasing"), 1, "%s", "");
 569        else
 570                write_file(am_path(state, "applying"), 1, "%s", "");
 571
 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);
 581        }
 582
 583        /*
 584         * NOTE: Since the "next" and "last" files determine if an am_state
 585         * session is in progress, they should be written last.
 586         */
 587
 588        write_file(am_path(state, "next"), 1, "%d", state->cur);
 589
 590        write_file(am_path(state, "last"), 1, "%d", state->last);
 591}
 592
 593/**
 594 * Increments the patch pointer, and cleans am_state for the application of the
 595 * next patch.
 596 */
 597static void am_next(struct am_state *state)
 598{
 599        unsigned char head[GIT_SHA1_RAWSZ];
 600
 601        free(state->author_name);
 602        state->author_name = NULL;
 603
 604        free(state->author_email);
 605        state->author_email = NULL;
 606
 607        free(state->author_date);
 608        state->author_date = NULL;
 609
 610        free(state->msg);
 611        state->msg = NULL;
 612        state->msg_len = 0;
 613
 614        unlink(am_path(state, "author-script"));
 615        unlink(am_path(state, "final-commit"));
 616
 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", "");
 621
 622        state->cur++;
 623        write_file(am_path(state, "next"), 1, "%d", state->cur);
 624}
 625
 626/**
 627 * Returns the filename of the current patch email.
 628 */
 629static const char *msgnum(const struct am_state *state)
 630{
 631        static struct strbuf sb = STRBUF_INIT;
 632
 633        strbuf_reset(&sb);
 634        strbuf_addf(&sb, "%0*d", state->prec, state->cur);
 635
 636        return sb.buf;
 637}
 638
 639/**
 640 * Refresh and write index.
 641 */
 642static void refresh_and_write_cache(void)
 643{
 644        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 645
 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"));
 650}
 651
 652/**
 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.
 657 */
 658static int index_has_changes(struct strbuf *sb)
 659{
 660        unsigned char head[GIT_SHA1_RAWSZ];
 661        int i;
 662
 663        if (!get_sha1_tree("HEAD", head)) {
 664                struct diff_options opt;
 665
 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);
 676                }
 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);
 684                }
 685                return !!active_nr;
 686        }
 687}
 688
 689/**
 690 * Dies with a user-friendly message on how to proceed after resolving the
 691 * problem. This message can be overridden with state->resolvemsg.
 692 */
 693static void NORETURN die_user_resolve(const struct am_state *state)
 694{
 695        if (state->resolvemsg) {
 696                printf_ln("%s", state->resolvemsg);
 697        } else {
 698                const char *cmdline = "git am";
 699
 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);
 703        }
 704
 705        exit(128);
 706}
 707
 708/**
 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.
 714 *
 715 * Returns 1 if the patch should be skipped, 0 otherwise.
 716 */
 717static int parse_mail(struct am_state *state, const char *mail)
 718{
 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;
 727
 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);
 731
 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"));
 736
 737        if (run_command(&cp) < 0)
 738                die("could not parse patch");
 739
 740        close(cp.in);
 741        close(cp.out);
 742
 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;
 747
 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);
 758        }
 759        fclose(fp);
 760
 761        /* Skip pine's internal folder data */
 762        if (!strcmp(author_name.buf, "Mail System Internal Data")) {
 763                ret = 1;
 764                goto finish;
 765        }
 766
 767        if (is_empty_file(am_path(state, "patch"))) {
 768                printf_ln(_("Patch is empty. Was it split wrong?"));
 769                die_user_resolve(state);
 770        }
 771
 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);
 776
 777        if (state->signoff)
 778                append_signoff(&msg, 0, 0);
 779
 780        assert(!state->author_name);
 781        state->author_name = strbuf_detach(&author_name, NULL);
 782
 783        assert(!state->author_email);
 784        state->author_email = strbuf_detach(&author_email, NULL);
 785
 786        assert(!state->author_date);
 787        state->author_date = strbuf_detach(&author_date, NULL);
 788
 789        assert(!state->msg);
 790        state->msg = strbuf_detach(&msg, &state->msg_len);
 791
 792finish:
 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;
 799}
 800
 801/**
 802 * Sets commit_id to the commit hash where the mail was generated from.
 803 * Returns 0 on success, -1 on failure.
 804 */
 805static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
 806{
 807        struct strbuf sb = STRBUF_INIT;
 808        FILE *fp = xfopen(mail, "r");
 809        const char *x;
 810
 811        if (strbuf_getline(&sb, fp, '\n'))
 812                return -1;
 813
 814        if (!skip_prefix(sb.buf, "From ", &x))
 815                return -1;
 816
 817        if (get_sha1_hex(x, commit_id) < 0)
 818                return -1;
 819
 820        strbuf_release(&sb);
 821        fclose(fp);
 822        return 0;
 823}
 824
 825/**
 826 * Sets state->msg, state->author_name, state->author_email, state->author_date
 827 * to the commit's respective info.
 828 */
 829static void get_commit_info(struct am_state *state, struct commit *commit)
 830{
 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;
 835
 836        buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding());
 837
 838        ident_line = find_commit_header(buffer, "author", &ident_len);
 839
 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);
 843        }
 844
 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("");
 852
 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("");
 860
 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);
 865
 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);
 872}
 873
 874/**
 875 * Writes `commit` as a patch to the state directory's "patch" file.
 876 */
 877static void write_commit_patch(const struct am_state *state, struct commit *commit)
 878{
 879        struct rev_info rev_info;
 880        FILE *fp;
 881
 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);
 898}
 899
 900/**
 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.
 904 *
 905 * Will always return 0 as the patch should never be skipped.
 906 */
 907static int parse_mail_rebase(struct am_state *state, const char *mail)
 908{
 909        struct commit *commit;
 910        unsigned char commit_sha1[GIT_SHA1_RAWSZ];
 911
 912        if (get_mail_commit_sha1(commit_sha1, mail) < 0)
 913                die(_("could not parse %s"), mail);
 914
 915        commit = lookup_commit_or_die(commit_sha1, mail);
 916
 917        get_commit_info(state, commit);
 918
 919        write_commit_patch(state, commit);
 920
 921        return 0;
 922}
 923
 924/**
 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.
 927 */
 928static int run_apply(const struct am_state *state, const char *index_file)
 929{
 930        struct child_process cp = CHILD_PROCESS_INIT;
 931
 932        cp.git_cmd = 1;
 933
 934        if (index_file)
 935                argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
 936
 937        /*
 938         * If we are allowed to fall back on 3-way merge, don't give false
 939         * errors during the initial attempt.
 940         */
 941        if (state->threeway && !index_file) {
 942                cp.no_stdout = 1;
 943                cp.no_stderr = 1;
 944        }
 945
 946        argv_array_push(&cp.args, "apply");
 947
 948        if (index_file)
 949                argv_array_push(&cp.args, "--cached");
 950        else
 951                argv_array_push(&cp.args, "--index");
 952
 953        argv_array_push(&cp.args, am_path(state, "patch"));
 954
 955        if (run_command(&cp))
 956                return -1;
 957
 958        /* Reload index as git-apply will have modified it. */
 959        discard_cache();
 960        read_cache_from(index_file ? index_file : get_index_file());
 961
 962        return 0;
 963}
 964
 965/**
 966 * Builds an index that contains just the blobs needed for a 3way merge.
 967 */
 968static int build_fake_ancestor(const struct am_state *state, const char *index_file)
 969{
 970        struct child_process cp = CHILD_PROCESS_INIT;
 971
 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"));
 976
 977        if (run_command(&cp))
 978                return -1;
 979
 980        return 0;
 981}
 982
 983/**
 984 * Attempt a threeway merge, using index_path as the temporary index.
 985 */
 986static int fall_back_threeway(const struct am_state *state, const char *index_path)
 987{
 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;
 994
 995        if (get_sha1("HEAD", our_tree) < 0)
 996                hashcpy(our_tree, EMPTY_TREE_SHA1_BIN);
 997
 998        if (build_fake_ancestor(state, index_path))
 999                return error("could not build fake ancestor");
1000
1001        discard_cache();
1002        read_cache_from(index_path);
1003
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."));
1006
1007        say(state, stdout, _("Using index info to reconstruct a base tree..."));
1008
1009        if (!state->quiet) {
1010                /*
1011                 * List paths that needed 3-way fallback, so that the user can
1012                 * review them with extra care to spot mismerges.
1013                 */
1014                struct rev_info rev_info;
1015                const char *diff_filter_str = "--diff-filter=AM";
1016
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);
1023        }
1024
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."));
1028
1029        if (write_index_as_tree(his_tree, &the_index, index_path, 0, NULL))
1030                return error("could not write tree");
1031
1032        say(state, stdout, _("Falling back to patching base and 3-way merge..."));
1033
1034        discard_cache();
1035        read_cache();
1036
1037        /*
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.
1043         */
1044
1045        init_merge_options(&o);
1046
1047        o.branch1 = "HEAD";
1048        his_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
1049        o.branch2 = his_tree_name;
1050
1051        if (state->quiet)
1052                o.verbosity = 0;
1053
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."));
1057        }
1058
1059        free(his_tree_name);
1060        return 0;
1061}
1062
1063/**
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.
1067 */
1068static void do_commit(const struct am_state *state)
1069{
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;
1076
1077        if (write_cache_as_tree(tree, 0, NULL))
1078                die(_("git write-tree failed to write a tree"));
1079
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"));
1086        }
1087
1088        author = fmt_ident(state->author_name, state->author_email,
1089                        state->author_date, IDENT_STRICT);
1090
1091        if (commit_tree(state->msg, state->msg_len, tree, parents, commit,
1092                                author, NULL))
1093                die(_("failed to write commit object"));
1094
1095        reflog_msg = getenv("GIT_REFLOG_ACTION");
1096        if (!reflog_msg)
1097                reflog_msg = "am";
1098
1099        strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
1100                        state->msg);
1101
1102        update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
1103
1104        strbuf_release(&sb);
1105}
1106
1107/**
1108 * Validates the am_state for resuming -- the "msg" and authorship fields must
1109 * be filled up.
1110 */
1111static void validate_resume_state(const struct am_state *state)
1112{
1113        if (!state->msg)
1114                die(_("cannot resume: %s does not exist."),
1115                        am_path(state, "final-commit"));
1116
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"));
1120}
1121
1122/**
1123 * Applies all queued mail.
1124 *
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.
1128 */
1129static void am_run(struct am_state *state, int resume)
1130{
1131        const char *argv_gc_auto[] = {"gc", "--auto", NULL};
1132        struct strbuf sb = STRBUF_INIT;
1133
1134        unlink(am_path(state, "dirtyindex"));
1135
1136        refresh_and_write_cache();
1137
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);
1141        }
1142
1143        strbuf_release(&sb);
1144
1145        while (state->cur <= state->last) {
1146                const char *mail = am_path(state, msgnum(state));
1147                int apply_status;
1148
1149                if (!file_exists(mail))
1150                        goto next;
1151
1152                if (resume) {
1153                        validate_resume_state(state);
1154                        resume = 0;
1155                } else {
1156                        int skip;
1157
1158                        if (state->rebasing)
1159                                skip = parse_mail_rebase(state, mail);
1160                        else
1161                                skip = parse_mail(state, mail);
1162
1163                        if (skip)
1164                                goto next; /* mail should be skipped */
1165
1166                        write_author_script(state);
1167                        write_commit_msg(state);
1168                }
1169
1170                say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1171
1172                apply_status = run_apply(state, NULL);
1173
1174                if (apply_status && state->threeway) {
1175                        struct strbuf sb = STRBUF_INIT;
1176
1177                        strbuf_addstr(&sb, am_path(state, "patch-merge-index"));
1178                        apply_status = fall_back_threeway(state, sb.buf);
1179                        strbuf_release(&sb);
1180
1181                        /*
1182                         * Applying the patch to an earlier tree and merging
1183                         * the result may have produced the same tree as ours.
1184                         */
1185                        if (!apply_status && !index_has_changes(NULL)) {
1186                                say(state, stdout, _("No changes -- Patch already applied."));
1187                                goto next;
1188                        }
1189                }
1190
1191                if (apply_status) {
1192                        int advice_amworkdir = 1;
1193
1194                        printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
1195                                linelen(state->msg), state->msg);
1196
1197                        git_config_get_bool("advice.amworkdir", &advice_amworkdir);
1198
1199                        if (advice_amworkdir)
1200                                printf_ln(_("The copy of the patch that failed is found in: %s"),
1201                                                am_path(state, "patch"));
1202
1203                        die_user_resolve(state);
1204                }
1205
1206                do_commit(state);
1207
1208next:
1209                am_next(state);
1210        }
1211
1212        /*
1213         * In rebasing mode, it's up to the caller to take care of
1214         * housekeeping.
1215         */
1216        if (!state->rebasing) {
1217                am_destroy(state);
1218                run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1219        }
1220}
1221
1222/**
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.
1226 */
1227static void am_resolve(struct am_state *state)
1228{
1229        validate_resume_state(state);
1230
1231        say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1232
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);
1238        }
1239
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);
1244        }
1245
1246        do_commit(state);
1247
1248        am_next(state);
1249        am_run(state, 0);
1250}
1251
1252/**
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.
1256 */
1257static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1258{
1259        struct lock_file *lock_file;
1260        struct unpack_trees_options opts;
1261        struct tree_desc t[2];
1262
1263        if (parse_tree(head) || parse_tree(remote))
1264                return -1;
1265
1266        lock_file = xcalloc(1, sizeof(struct lock_file));
1267        hold_locked_index(lock_file, 1);
1268
1269        refresh_cache(REFRESH_QUIET);
1270
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);
1281
1282        if (unpack_trees(2, t, &opts)) {
1283                rollback_lock_file(lock_file);
1284                return -1;
1285        }
1286
1287        if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1288                die(_("unable to write new index file"));
1289
1290        return 0;
1291}
1292
1293/**
1294 * Clean the index without touching entries that are not modified between
1295 * `head` and `remote`.
1296 */
1297static int clean_index(const unsigned char *head, const unsigned char *remote)
1298{
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;
1303
1304        head_tree = parse_tree_indirect(head);
1305        if (!head_tree)
1306                return error(_("Could not parse object '%s'."), sha1_to_hex(head));
1307
1308        remote_tree = parse_tree_indirect(remote);
1309        if (!remote_tree)
1310                return error(_("Could not parse object '%s'."), sha1_to_hex(remote));
1311
1312        read_cache_unmerged();
1313
1314        if (fast_forward_to(head_tree, head_tree, 1))
1315                return -1;
1316
1317        if (write_cache_as_tree(index, 0, NULL))
1318                return -1;
1319
1320        index_tree = parse_tree_indirect(index);
1321        if (!index_tree)
1322                return error(_("Could not parse object '%s'."), sha1_to_hex(index));
1323
1324        if (fast_forward_to(index_tree, remote_tree, 0))
1325                return -1;
1326
1327        memset(&pathspec, 0, sizeof(pathspec));
1328
1329        lock_file = xcalloc(1, sizeof(struct lock_file));
1330        hold_locked_index(lock_file, 1);
1331
1332        if (read_tree(remote_tree, 0, &pathspec)) {
1333                rollback_lock_file(lock_file);
1334                return -1;
1335        }
1336
1337        if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1338                die(_("unable to write new index file"));
1339
1340        remove_branch_state();
1341
1342        return 0;
1343}
1344
1345/**
1346 * Resume the current am session by skipping the current patch.
1347 */
1348static void am_skip(struct am_state *state)
1349{
1350        unsigned char head[GIT_SHA1_RAWSZ];
1351
1352        if (get_sha1("HEAD", head))
1353                hashcpy(head, EMPTY_TREE_SHA1_BIN);
1354
1355        if (clean_index(head, head))
1356                die(_("failed to clean index"));
1357
1358        am_next(state);
1359        am_run(state, 0);
1360}
1361
1362/**
1363 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
1364 *
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.
1368 */
1369static int safe_to_abort(const struct am_state *state)
1370{
1371        struct strbuf sb = STRBUF_INIT;
1372        unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
1373
1374        if (file_exists(am_path(state, "dirtyindex")))
1375                return 0;
1376
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);
1382
1383        if (get_sha1("HEAD", head))
1384                hashclr(head);
1385
1386        if (!hashcmp(head, abort_safety))
1387                return 1;
1388
1389        error(_("You seem to have moved HEAD since the last 'am' failure.\n"
1390                "Not rewinding to ORIG_HEAD"));
1391
1392        return 0;
1393}
1394
1395/**
1396 * Aborts the current am session if it is safe to do so.
1397 */
1398static void am_abort(struct am_state *state)
1399{
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;
1403
1404        if (!safe_to_abort(state)) {
1405                am_destroy(state);
1406                return;
1407        }
1408
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);
1413
1414        has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
1415        if (!has_orig_head)
1416                hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
1417
1418        clean_index(curr_head, orig_head);
1419
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);
1426
1427        free(curr_branch);
1428        am_destroy(state);
1429}
1430
1431/**
1432 * parse_options() callback that validates and sets opt->value to the
1433 * PATCH_FORMAT_* enum value corresponding to `arg`.
1434 */
1435static int parse_opt_patchformat(const struct option *opt, const char *arg, int unset)
1436{
1437        int *opt_value = opt->value;
1438
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;
1444}
1445
1446enum resume_mode {
1447        RESUME_FALSE = 0,
1448        RESUME_APPLY,
1449        RESUME_RESOLVED,
1450        RESUME_SKIP,
1451        RESUME_ABORT
1452};
1453
1454int cmd_am(int argc, const char **argv, const char *prefix)
1455{
1456        struct am_state state;
1457        int patch_format = PATCH_FORMAT_UNKNOWN;
1458        enum resume_mode resume = RESUME_FALSE;
1459
1460        const char * const usage[] = {
1461                N_("git am [options] [(<mbox>|<Maildir>)...]"),
1462                N_("git am [options] (--continue | --skip | --abort)"),
1463                NULL
1464        };
1465
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()
1494        };
1495
1496        /*
1497         * NEEDSWORK: Once all the features of git-am.sh have been
1498         * re-implemented in builtin/am.c, this preamble can be removed.
1499         */
1500        if (!getenv("_GIT_USE_BUILTIN_AM")) {
1501                const char *path = mkpath("%s/git-am", git_exec_path());
1502
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();
1509        }
1510
1511        git_config(git_default_config, NULL);
1512
1513        am_state_init(&state, git_path("rebase-apply"));
1514
1515        argc = parse_options(argc, argv, prefix, options, usage, 0);
1516
1517        if (read_index_preload(&the_index, NULL) < 0)
1518                die(_("failed to read the index"));
1519
1520        if (am_in_progress(&state)) {
1521                /*
1522                 * Catch user error to feed us patches when there is a session
1523                 * in progress:
1524                 *
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.
1531                 */
1532                if (argc || (resume == RESUME_FALSE && !isatty(0)))
1533                        die(_("previous rebase directory %s still exists but mbox given."),
1534                                state.dir);
1535
1536                if (resume == RESUME_FALSE)
1537                        resume = RESUME_APPLY;
1538
1539                am_load(&state);
1540        } else {
1541                struct argv_array paths = ARGV_ARRAY_INIT;
1542                int i;
1543
1544                /*
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.
1548                 */
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;
1554                        }
1555
1556                        die(_("Stray %s directory found.\n"
1557                                "Use \"git am --abort\" to remove it."),
1558                                state.dir);
1559                }
1560
1561                if (resume)
1562                        die(_("Resolve operation not in progress, we are not resuming."));
1563
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]));
1569                }
1570
1571                am_setup(&state, patch_format, paths.argv);
1572
1573                argv_array_clear(&paths);
1574        }
1575
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");
1594        }
1595
1596        am_state_release(&state);
1597
1598        return 0;
1599}