a5d5e8c665cface79bde405b5c7a7159b32a37f7
   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
  25/**
  26 * Returns 1 if the file is empty or does not exist, 0 otherwise.
  27 */
  28static int is_empty_file(const char *filename)
  29{
  30        struct stat st;
  31
  32        if (stat(filename, &st) < 0) {
  33                if (errno == ENOENT)
  34                        return 1;
  35                die_errno(_("could not stat %s"), filename);
  36        }
  37
  38        return !st.st_size;
  39}
  40
  41/**
  42 * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
  43 */
  44static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
  45{
  46        if (strbuf_getwholeline(sb, fp, '\n'))
  47                return EOF;
  48        if (sb->buf[sb->len - 1] == '\n') {
  49                strbuf_setlen(sb, sb->len - 1);
  50                if (sb->len > 0 && sb->buf[sb->len - 1] == '\r')
  51                        strbuf_setlen(sb, sb->len - 1);
  52        }
  53        return 0;
  54}
  55
  56/**
  57 * Returns the length of the first line of msg.
  58 */
  59static int linelen(const char *msg)
  60{
  61        return strchrnul(msg, '\n') - msg;
  62}
  63
  64enum patch_format {
  65        PATCH_FORMAT_UNKNOWN = 0,
  66        PATCH_FORMAT_MBOX
  67};
  68
  69struct am_state {
  70        /* state directory path */
  71        char *dir;
  72
  73        /* current and last patch numbers, 1-indexed */
  74        int cur;
  75        int last;
  76
  77        /* commit metadata and message */
  78        char *author_name;
  79        char *author_email;
  80        char *author_date;
  81        char *msg;
  82        size_t msg_len;
  83
  84        /* number of digits in patch filename */
  85        int prec;
  86
  87        /* various operating modes and command line options */
  88        int threeway;
  89        int quiet;
  90        int signoff;
  91        const char *resolvemsg;
  92};
  93
  94/**
  95 * Initializes am_state with the default values. The state directory is set to
  96 * dir.
  97 */
  98static void am_state_init(struct am_state *state, const char *dir)
  99{
 100        memset(state, 0, sizeof(*state));
 101
 102        assert(dir);
 103        state->dir = xstrdup(dir);
 104
 105        state->prec = 4;
 106}
 107
 108/**
 109 * Releases memory allocated by an am_state.
 110 */
 111static void am_state_release(struct am_state *state)
 112{
 113        free(state->dir);
 114        free(state->author_name);
 115        free(state->author_email);
 116        free(state->author_date);
 117        free(state->msg);
 118}
 119
 120/**
 121 * Returns path relative to the am_state directory.
 122 */
 123static inline const char *am_path(const struct am_state *state, const char *path)
 124{
 125        return mkpath("%s/%s", state->dir, path);
 126}
 127
 128/**
 129 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
 130 * at the end.
 131 */
 132static void say(const struct am_state *state, FILE *fp, const char *fmt, ...)
 133{
 134        va_list ap;
 135
 136        va_start(ap, fmt);
 137        if (!state->quiet) {
 138                vfprintf(fp, fmt, ap);
 139                putc('\n', fp);
 140        }
 141        va_end(ap);
 142}
 143
 144/**
 145 * Returns 1 if there is an am session in progress, 0 otherwise.
 146 */
 147static int am_in_progress(const struct am_state *state)
 148{
 149        struct stat st;
 150
 151        if (lstat(state->dir, &st) < 0 || !S_ISDIR(st.st_mode))
 152                return 0;
 153        if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode))
 154                return 0;
 155        if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode))
 156                return 0;
 157        return 1;
 158}
 159
 160/**
 161 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
 162 * number of bytes read on success, -1 if the file does not exist. If `trim` is
 163 * set, trailing whitespace will be removed.
 164 */
 165static int read_state_file(struct strbuf *sb, const struct am_state *state,
 166                        const char *file, int trim)
 167{
 168        strbuf_reset(sb);
 169
 170        if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) {
 171                if (trim)
 172                        strbuf_trim(sb);
 173
 174                return sb->len;
 175        }
 176
 177        if (errno == ENOENT)
 178                return -1;
 179
 180        die_errno(_("could not read '%s'"), am_path(state, file));
 181}
 182
 183/**
 184 * Reads a KEY=VALUE shell variable assignment from `fp`, returning the VALUE
 185 * as a newly-allocated string. VALUE must be a quoted string, and the KEY must
 186 * match `key`. Returns NULL on failure.
 187 *
 188 * This is used by read_author_script() to read the GIT_AUTHOR_* variables from
 189 * the author-script.
 190 */
 191static char *read_shell_var(FILE *fp, const char *key)
 192{
 193        struct strbuf sb = STRBUF_INIT;
 194        const char *str;
 195
 196        if (strbuf_getline(&sb, fp, '\n'))
 197                goto fail;
 198
 199        if (!skip_prefix(sb.buf, key, &str))
 200                goto fail;
 201
 202        if (!skip_prefix(str, "=", &str))
 203                goto fail;
 204
 205        strbuf_remove(&sb, 0, str - sb.buf);
 206
 207        str = sq_dequote(sb.buf);
 208        if (!str)
 209                goto fail;
 210
 211        return strbuf_detach(&sb, NULL);
 212
 213fail:
 214        strbuf_release(&sb);
 215        return NULL;
 216}
 217
 218/**
 219 * Reads and parses the state directory's "author-script" file, and sets
 220 * state->author_name, state->author_email and state->author_date accordingly.
 221 * Returns 0 on success, -1 if the file could not be parsed.
 222 *
 223 * The author script is of the format:
 224 *
 225 *      GIT_AUTHOR_NAME='$author_name'
 226 *      GIT_AUTHOR_EMAIL='$author_email'
 227 *      GIT_AUTHOR_DATE='$author_date'
 228 *
 229 * where $author_name, $author_email and $author_date are quoted. We are strict
 230 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
 231 * script, and thus if the file differs from what this function expects, it is
 232 * better to bail out than to do something that the user does not expect.
 233 */
 234static int read_author_script(struct am_state *state)
 235{
 236        const char *filename = am_path(state, "author-script");
 237        FILE *fp;
 238
 239        assert(!state->author_name);
 240        assert(!state->author_email);
 241        assert(!state->author_date);
 242
 243        fp = fopen(filename, "r");
 244        if (!fp) {
 245                if (errno == ENOENT)
 246                        return 0;
 247                die_errno(_("could not open '%s' for reading"), filename);
 248        }
 249
 250        state->author_name = read_shell_var(fp, "GIT_AUTHOR_NAME");
 251        if (!state->author_name) {
 252                fclose(fp);
 253                return -1;
 254        }
 255
 256        state->author_email = read_shell_var(fp, "GIT_AUTHOR_EMAIL");
 257        if (!state->author_email) {
 258                fclose(fp);
 259                return -1;
 260        }
 261
 262        state->author_date = read_shell_var(fp, "GIT_AUTHOR_DATE");
 263        if (!state->author_date) {
 264                fclose(fp);
 265                return -1;
 266        }
 267
 268        if (fgetc(fp) != EOF) {
 269                fclose(fp);
 270                return -1;
 271        }
 272
 273        fclose(fp);
 274        return 0;
 275}
 276
 277/**
 278 * Saves state->author_name, state->author_email and state->author_date in the
 279 * state directory's "author-script" file.
 280 */
 281static void write_author_script(const struct am_state *state)
 282{
 283        struct strbuf sb = STRBUF_INIT;
 284
 285        strbuf_addstr(&sb, "GIT_AUTHOR_NAME=");
 286        sq_quote_buf(&sb, state->author_name);
 287        strbuf_addch(&sb, '\n');
 288
 289        strbuf_addstr(&sb, "GIT_AUTHOR_EMAIL=");
 290        sq_quote_buf(&sb, state->author_email);
 291        strbuf_addch(&sb, '\n');
 292
 293        strbuf_addstr(&sb, "GIT_AUTHOR_DATE=");
 294        sq_quote_buf(&sb, state->author_date);
 295        strbuf_addch(&sb, '\n');
 296
 297        write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
 298
 299        strbuf_release(&sb);
 300}
 301
 302/**
 303 * Reads the commit message from the state directory's "final-commit" file,
 304 * setting state->msg to its contents and state->msg_len to the length of its
 305 * contents in bytes.
 306 *
 307 * Returns 0 on success, -1 if the file does not exist.
 308 */
 309static int read_commit_msg(struct am_state *state)
 310{
 311        struct strbuf sb = STRBUF_INIT;
 312
 313        assert(!state->msg);
 314
 315        if (read_state_file(&sb, state, "final-commit", 0) < 0) {
 316                strbuf_release(&sb);
 317                return -1;
 318        }
 319
 320        state->msg = strbuf_detach(&sb, &state->msg_len);
 321        return 0;
 322}
 323
 324/**
 325 * Saves state->msg in the state directory's "final-commit" file.
 326 */
 327static void write_commit_msg(const struct am_state *state)
 328{
 329        int fd;
 330        const char *filename = am_path(state, "final-commit");
 331
 332        fd = xopen(filename, O_WRONLY | O_CREAT, 0666);
 333        if (write_in_full(fd, state->msg, state->msg_len) < 0)
 334                die_errno(_("could not write to %s"), filename);
 335        close(fd);
 336}
 337
 338/**
 339 * Loads state from disk.
 340 */
 341static void am_load(struct am_state *state)
 342{
 343        struct strbuf sb = STRBUF_INIT;
 344
 345        if (read_state_file(&sb, state, "next", 1) < 0)
 346                die("BUG: state file 'next' does not exist");
 347        state->cur = strtol(sb.buf, NULL, 10);
 348
 349        if (read_state_file(&sb, state, "last", 1) < 0)
 350                die("BUG: state file 'last' does not exist");
 351        state->last = strtol(sb.buf, NULL, 10);
 352
 353        if (read_author_script(state) < 0)
 354                die(_("could not parse author script"));
 355
 356        read_commit_msg(state);
 357
 358        read_state_file(&sb, state, "threeway", 1);
 359        state->threeway = !strcmp(sb.buf, "t");
 360
 361        read_state_file(&sb, state, "quiet", 1);
 362        state->quiet = !strcmp(sb.buf, "t");
 363
 364        read_state_file(&sb, state, "sign", 1);
 365        state->signoff = !strcmp(sb.buf, "t");
 366
 367        strbuf_release(&sb);
 368}
 369
 370/**
 371 * Removes the am_state directory, forcefully terminating the current am
 372 * session.
 373 */
 374static void am_destroy(const struct am_state *state)
 375{
 376        struct strbuf sb = STRBUF_INIT;
 377
 378        strbuf_addstr(&sb, state->dir);
 379        remove_dir_recursively(&sb, 0);
 380        strbuf_release(&sb);
 381}
 382
 383/**
 384 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
 385 * non-indented lines and checking if they look like they begin with valid
 386 * header field names.
 387 *
 388 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
 389 */
 390static int is_mail(FILE *fp)
 391{
 392        const char *header_regex = "^[!-9;-~]+:";
 393        struct strbuf sb = STRBUF_INIT;
 394        regex_t regex;
 395        int ret = 1;
 396
 397        if (fseek(fp, 0L, SEEK_SET))
 398                die_errno(_("fseek failed"));
 399
 400        if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
 401                die("invalid pattern: %s", header_regex);
 402
 403        while (!strbuf_getline_crlf(&sb, fp)) {
 404                if (!sb.len)
 405                        break; /* End of header */
 406
 407                /* Ignore indented folded lines */
 408                if (*sb.buf == '\t' || *sb.buf == ' ')
 409                        continue;
 410
 411                /* It's a header if it matches header_regex */
 412                if (regexec(&regex, sb.buf, 0, NULL, 0)) {
 413                        ret = 0;
 414                        goto done;
 415                }
 416        }
 417
 418done:
 419        regfree(&regex);
 420        strbuf_release(&sb);
 421        return ret;
 422}
 423
 424/**
 425 * Attempts to detect the patch_format of the patches contained in `paths`,
 426 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
 427 * detection fails.
 428 */
 429static int detect_patch_format(const char **paths)
 430{
 431        enum patch_format ret = PATCH_FORMAT_UNKNOWN;
 432        struct strbuf l1 = STRBUF_INIT;
 433        FILE *fp;
 434
 435        /*
 436         * We default to mbox format if input is from stdin and for directories
 437         */
 438        if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
 439                return PATCH_FORMAT_MBOX;
 440
 441        /*
 442         * Otherwise, check the first few lines of the first patch, starting
 443         * from the first non-blank line, to try to detect its format.
 444         */
 445
 446        fp = xfopen(*paths, "r");
 447
 448        while (!strbuf_getline_crlf(&l1, fp)) {
 449                if (l1.len)
 450                        break;
 451        }
 452
 453        if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) {
 454                ret = PATCH_FORMAT_MBOX;
 455                goto done;
 456        }
 457
 458        if (l1.len && is_mail(fp)) {
 459                ret = PATCH_FORMAT_MBOX;
 460                goto done;
 461        }
 462
 463done:
 464        fclose(fp);
 465        strbuf_release(&l1);
 466        return ret;
 467}
 468
 469/**
 470 * Splits out individual email patches from `paths`, where each path is either
 471 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
 472 */
 473static int split_mail_mbox(struct am_state *state, const char **paths)
 474{
 475        struct child_process cp = CHILD_PROCESS_INIT;
 476        struct strbuf last = STRBUF_INIT;
 477
 478        cp.git_cmd = 1;
 479        argv_array_push(&cp.args, "mailsplit");
 480        argv_array_pushf(&cp.args, "-d%d", state->prec);
 481        argv_array_pushf(&cp.args, "-o%s", state->dir);
 482        argv_array_push(&cp.args, "-b");
 483        argv_array_push(&cp.args, "--");
 484        argv_array_pushv(&cp.args, paths);
 485
 486        if (capture_command(&cp, &last, 8))
 487                return -1;
 488
 489        state->cur = 1;
 490        state->last = strtol(last.buf, NULL, 10);
 491
 492        return 0;
 493}
 494
 495/**
 496 * Splits a list of files/directories into individual email patches. Each path
 497 * in `paths` must be a file/directory that is formatted according to
 498 * `patch_format`.
 499 *
 500 * Once split out, the individual email patches will be stored in the state
 501 * directory, with each patch's filename being its index, padded to state->prec
 502 * digits.
 503 *
 504 * state->cur will be set to the index of the first mail, and state->last will
 505 * be set to the index of the last mail.
 506 *
 507 * Returns 0 on success, -1 on failure.
 508 */
 509static int split_mail(struct am_state *state, enum patch_format patch_format,
 510                        const char **paths)
 511{
 512        switch (patch_format) {
 513        case PATCH_FORMAT_MBOX:
 514                return split_mail_mbox(state, paths);
 515        default:
 516                die("BUG: invalid patch_format");
 517        }
 518        return -1;
 519}
 520
 521/**
 522 * Setup a new am session for applying patches
 523 */
 524static void am_setup(struct am_state *state, enum patch_format patch_format,
 525                        const char **paths)
 526{
 527        unsigned char curr_head[GIT_SHA1_RAWSZ];
 528
 529        if (!patch_format)
 530                patch_format = detect_patch_format(paths);
 531
 532        if (!patch_format) {
 533                fprintf_ln(stderr, _("Patch format detection failed."));
 534                exit(128);
 535        }
 536
 537        if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
 538                die_errno(_("failed to create directory '%s'"), state->dir);
 539
 540        if (split_mail(state, patch_format, paths) < 0) {
 541                am_destroy(state);
 542                die(_("Failed to split patches."));
 543        }
 544
 545        write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
 546
 547        write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
 548
 549        write_file(am_path(state, "sign"), 1, state->signoff ? "t" : "f");
 550
 551        if (!get_sha1("HEAD", curr_head)) {
 552                write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
 553                update_ref("am", "ORIG_HEAD", curr_head, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
 554        } else {
 555                write_file(am_path(state, "abort-safety"), 1, "%s", "");
 556                delete_ref("ORIG_HEAD", NULL, 0);
 557        }
 558
 559        /*
 560         * NOTE: Since the "next" and "last" files determine if an am_state
 561         * session is in progress, they should be written last.
 562         */
 563
 564        write_file(am_path(state, "next"), 1, "%d", state->cur);
 565
 566        write_file(am_path(state, "last"), 1, "%d", state->last);
 567}
 568
 569/**
 570 * Increments the patch pointer, and cleans am_state for the application of the
 571 * next patch.
 572 */
 573static void am_next(struct am_state *state)
 574{
 575        unsigned char head[GIT_SHA1_RAWSZ];
 576
 577        free(state->author_name);
 578        state->author_name = NULL;
 579
 580        free(state->author_email);
 581        state->author_email = NULL;
 582
 583        free(state->author_date);
 584        state->author_date = NULL;
 585
 586        free(state->msg);
 587        state->msg = NULL;
 588        state->msg_len = 0;
 589
 590        unlink(am_path(state, "author-script"));
 591        unlink(am_path(state, "final-commit"));
 592
 593        if (!get_sha1("HEAD", head))
 594                write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
 595        else
 596                write_file(am_path(state, "abort-safety"), 1, "%s", "");
 597
 598        state->cur++;
 599        write_file(am_path(state, "next"), 1, "%d", state->cur);
 600}
 601
 602/**
 603 * Returns the filename of the current patch email.
 604 */
 605static const char *msgnum(const struct am_state *state)
 606{
 607        static struct strbuf sb = STRBUF_INIT;
 608
 609        strbuf_reset(&sb);
 610        strbuf_addf(&sb, "%0*d", state->prec, state->cur);
 611
 612        return sb.buf;
 613}
 614
 615/**
 616 * Refresh and write index.
 617 */
 618static void refresh_and_write_cache(void)
 619{
 620        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 621
 622        hold_locked_index(lock_file, 1);
 623        refresh_cache(REFRESH_QUIET);
 624        if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
 625                die(_("unable to write index file"));
 626}
 627
 628/**
 629 * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
 630 * branch, returns 1 if there are entries in the index, 0 otherwise. If an
 631 * strbuf is provided, the space-separated list of files that differ will be
 632 * appended to it.
 633 */
 634static int index_has_changes(struct strbuf *sb)
 635{
 636        unsigned char head[GIT_SHA1_RAWSZ];
 637        int i;
 638
 639        if (!get_sha1_tree("HEAD", head)) {
 640                struct diff_options opt;
 641
 642                diff_setup(&opt);
 643                DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
 644                if (!sb)
 645                        DIFF_OPT_SET(&opt, QUICK);
 646                do_diff_cache(head, &opt);
 647                diffcore_std(&opt);
 648                for (i = 0; sb && i < diff_queued_diff.nr; i++) {
 649                        if (i)
 650                                strbuf_addch(sb, ' ');
 651                        strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
 652                }
 653                diff_flush(&opt);
 654                return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0;
 655        } else {
 656                for (i = 0; sb && i < active_nr; i++) {
 657                        if (i)
 658                                strbuf_addch(sb, ' ');
 659                        strbuf_addstr(sb, active_cache[i]->name);
 660                }
 661                return !!active_nr;
 662        }
 663}
 664
 665/**
 666 * Dies with a user-friendly message on how to proceed after resolving the
 667 * problem. This message can be overridden with state->resolvemsg.
 668 */
 669static void NORETURN die_user_resolve(const struct am_state *state)
 670{
 671        if (state->resolvemsg) {
 672                printf_ln("%s", state->resolvemsg);
 673        } else {
 674                const char *cmdline = "git am";
 675
 676                printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline);
 677                printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline);
 678                printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline);
 679        }
 680
 681        exit(128);
 682}
 683
 684/**
 685 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
 686 * state->msg will be set to the patch message. state->author_name,
 687 * state->author_email and state->author_date will be set to the patch author's
 688 * name, email and date respectively. The patch body will be written to the
 689 * state directory's "patch" file.
 690 *
 691 * Returns 1 if the patch should be skipped, 0 otherwise.
 692 */
 693static int parse_mail(struct am_state *state, const char *mail)
 694{
 695        FILE *fp;
 696        struct child_process cp = CHILD_PROCESS_INIT;
 697        struct strbuf sb = STRBUF_INIT;
 698        struct strbuf msg = STRBUF_INIT;
 699        struct strbuf author_name = STRBUF_INIT;
 700        struct strbuf author_date = STRBUF_INIT;
 701        struct strbuf author_email = STRBUF_INIT;
 702        int ret = 0;
 703
 704        cp.git_cmd = 1;
 705        cp.in = xopen(mail, O_RDONLY, 0);
 706        cp.out = xopen(am_path(state, "info"), O_WRONLY | O_CREAT, 0777);
 707
 708        argv_array_push(&cp.args, "mailinfo");
 709        argv_array_push(&cp.args, am_path(state, "msg"));
 710        argv_array_push(&cp.args, am_path(state, "patch"));
 711
 712        if (run_command(&cp) < 0)
 713                die("could not parse patch");
 714
 715        close(cp.in);
 716        close(cp.out);
 717
 718        /* Extract message and author information */
 719        fp = xfopen(am_path(state, "info"), "r");
 720        while (!strbuf_getline(&sb, fp, '\n')) {
 721                const char *x;
 722
 723                if (skip_prefix(sb.buf, "Subject: ", &x)) {
 724                        if (msg.len)
 725                                strbuf_addch(&msg, '\n');
 726                        strbuf_addstr(&msg, x);
 727                } else if (skip_prefix(sb.buf, "Author: ", &x))
 728                        strbuf_addstr(&author_name, x);
 729                else if (skip_prefix(sb.buf, "Email: ", &x))
 730                        strbuf_addstr(&author_email, x);
 731                else if (skip_prefix(sb.buf, "Date: ", &x))
 732                        strbuf_addstr(&author_date, x);
 733        }
 734        fclose(fp);
 735
 736        /* Skip pine's internal folder data */
 737        if (!strcmp(author_name.buf, "Mail System Internal Data")) {
 738                ret = 1;
 739                goto finish;
 740        }
 741
 742        if (is_empty_file(am_path(state, "patch"))) {
 743                printf_ln(_("Patch is empty. Was it split wrong?"));
 744                die_user_resolve(state);
 745        }
 746
 747        strbuf_addstr(&msg, "\n\n");
 748        if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
 749                die_errno(_("could not read '%s'"), am_path(state, "msg"));
 750        stripspace(&msg, 0);
 751
 752        if (state->signoff)
 753                append_signoff(&msg, 0, 0);
 754
 755        assert(!state->author_name);
 756        state->author_name = strbuf_detach(&author_name, NULL);
 757
 758        assert(!state->author_email);
 759        state->author_email = strbuf_detach(&author_email, NULL);
 760
 761        assert(!state->author_date);
 762        state->author_date = strbuf_detach(&author_date, NULL);
 763
 764        assert(!state->msg);
 765        state->msg = strbuf_detach(&msg, &state->msg_len);
 766
 767finish:
 768        strbuf_release(&msg);
 769        strbuf_release(&author_date);
 770        strbuf_release(&author_email);
 771        strbuf_release(&author_name);
 772        strbuf_release(&sb);
 773        return ret;
 774}
 775
 776/**
 777 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
 778 * `index_file` is not NULL, the patch will be applied to that index.
 779 */
 780static int run_apply(const struct am_state *state, const char *index_file)
 781{
 782        struct child_process cp = CHILD_PROCESS_INIT;
 783
 784        cp.git_cmd = 1;
 785
 786        if (index_file)
 787                argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
 788
 789        /*
 790         * If we are allowed to fall back on 3-way merge, don't give false
 791         * errors during the initial attempt.
 792         */
 793        if (state->threeway && !index_file) {
 794                cp.no_stdout = 1;
 795                cp.no_stderr = 1;
 796        }
 797
 798        argv_array_push(&cp.args, "apply");
 799
 800        if (index_file)
 801                argv_array_push(&cp.args, "--cached");
 802        else
 803                argv_array_push(&cp.args, "--index");
 804
 805        argv_array_push(&cp.args, am_path(state, "patch"));
 806
 807        if (run_command(&cp))
 808                return -1;
 809
 810        /* Reload index as git-apply will have modified it. */
 811        discard_cache();
 812        read_cache_from(index_file ? index_file : get_index_file());
 813
 814        return 0;
 815}
 816
 817/**
 818 * Builds an index that contains just the blobs needed for a 3way merge.
 819 */
 820static int build_fake_ancestor(const struct am_state *state, const char *index_file)
 821{
 822        struct child_process cp = CHILD_PROCESS_INIT;
 823
 824        cp.git_cmd = 1;
 825        argv_array_push(&cp.args, "apply");
 826        argv_array_pushf(&cp.args, "--build-fake-ancestor=%s", index_file);
 827        argv_array_push(&cp.args, am_path(state, "patch"));
 828
 829        if (run_command(&cp))
 830                return -1;
 831
 832        return 0;
 833}
 834
 835/**
 836 * Attempt a threeway merge, using index_path as the temporary index.
 837 */
 838static int fall_back_threeway(const struct am_state *state, const char *index_path)
 839{
 840        unsigned char orig_tree[GIT_SHA1_RAWSZ], his_tree[GIT_SHA1_RAWSZ],
 841                      our_tree[GIT_SHA1_RAWSZ];
 842        const unsigned char *bases[1] = {orig_tree};
 843        struct merge_options o;
 844        struct commit *result;
 845        char *his_tree_name;
 846
 847        if (get_sha1("HEAD", our_tree) < 0)
 848                hashcpy(our_tree, EMPTY_TREE_SHA1_BIN);
 849
 850        if (build_fake_ancestor(state, index_path))
 851                return error("could not build fake ancestor");
 852
 853        discard_cache();
 854        read_cache_from(index_path);
 855
 856        if (write_index_as_tree(orig_tree, &the_index, index_path, 0, NULL))
 857                return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
 858
 859        say(state, stdout, _("Using index info to reconstruct a base tree..."));
 860
 861        if (!state->quiet) {
 862                /*
 863                 * List paths that needed 3-way fallback, so that the user can
 864                 * review them with extra care to spot mismerges.
 865                 */
 866                struct rev_info rev_info;
 867                const char *diff_filter_str = "--diff-filter=AM";
 868
 869                init_revisions(&rev_info, NULL);
 870                rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
 871                diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
 872                add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
 873                diff_setup_done(&rev_info.diffopt);
 874                run_diff_index(&rev_info, 1);
 875        }
 876
 877        if (run_apply(state, index_path))
 878                return error(_("Did you hand edit your patch?\n"
 879                                "It does not apply to blobs recorded in its index."));
 880
 881        if (write_index_as_tree(his_tree, &the_index, index_path, 0, NULL))
 882                return error("could not write tree");
 883
 884        say(state, stdout, _("Falling back to patching base and 3-way merge..."));
 885
 886        discard_cache();
 887        read_cache();
 888
 889        /*
 890         * This is not so wrong. Depending on which base we picked, orig_tree
 891         * may be wildly different from ours, but his_tree has the same set of
 892         * wildly different changes in parts the patch did not touch, so
 893         * recursive ends up canceling them, saying that we reverted all those
 894         * changes.
 895         */
 896
 897        init_merge_options(&o);
 898
 899        o.branch1 = "HEAD";
 900        his_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
 901        o.branch2 = his_tree_name;
 902
 903        if (state->quiet)
 904                o.verbosity = 0;
 905
 906        if (merge_recursive_generic(&o, our_tree, his_tree, 1, bases, &result)) {
 907                free(his_tree_name);
 908                return error(_("Failed to merge in the changes."));
 909        }
 910
 911        free(his_tree_name);
 912        return 0;
 913}
 914
 915/**
 916 * Commits the current index with state->msg as the commit message and
 917 * state->author_name, state->author_email and state->author_date as the author
 918 * information.
 919 */
 920static void do_commit(const struct am_state *state)
 921{
 922        unsigned char tree[GIT_SHA1_RAWSZ], parent[GIT_SHA1_RAWSZ],
 923                      commit[GIT_SHA1_RAWSZ];
 924        unsigned char *ptr;
 925        struct commit_list *parents = NULL;
 926        const char *reflog_msg, *author;
 927        struct strbuf sb = STRBUF_INIT;
 928
 929        if (write_cache_as_tree(tree, 0, NULL))
 930                die(_("git write-tree failed to write a tree"));
 931
 932        if (!get_sha1_commit("HEAD", parent)) {
 933                ptr = parent;
 934                commit_list_insert(lookup_commit(parent), &parents);
 935        } else {
 936                ptr = NULL;
 937                say(state, stderr, _("applying to an empty history"));
 938        }
 939
 940        author = fmt_ident(state->author_name, state->author_email,
 941                        state->author_date, IDENT_STRICT);
 942
 943        if (commit_tree(state->msg, state->msg_len, tree, parents, commit,
 944                                author, NULL))
 945                die(_("failed to write commit object"));
 946
 947        reflog_msg = getenv("GIT_REFLOG_ACTION");
 948        if (!reflog_msg)
 949                reflog_msg = "am";
 950
 951        strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
 952                        state->msg);
 953
 954        update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
 955
 956        strbuf_release(&sb);
 957}
 958
 959/**
 960 * Validates the am_state for resuming -- the "msg" and authorship fields must
 961 * be filled up.
 962 */
 963static void validate_resume_state(const struct am_state *state)
 964{
 965        if (!state->msg)
 966                die(_("cannot resume: %s does not exist."),
 967                        am_path(state, "final-commit"));
 968
 969        if (!state->author_name || !state->author_email || !state->author_date)
 970                die(_("cannot resume: %s does not exist."),
 971                        am_path(state, "author-script"));
 972}
 973
 974/**
 975 * Applies all queued mail.
 976 *
 977 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
 978 * well as the state directory's "patch" file is used as-is for applying the
 979 * patch and committing it.
 980 */
 981static void am_run(struct am_state *state, int resume)
 982{
 983        const char *argv_gc_auto[] = {"gc", "--auto", NULL};
 984        struct strbuf sb = STRBUF_INIT;
 985
 986        unlink(am_path(state, "dirtyindex"));
 987
 988        refresh_and_write_cache();
 989
 990        if (index_has_changes(&sb)) {
 991                write_file(am_path(state, "dirtyindex"), 1, "t");
 992                die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
 993        }
 994
 995        strbuf_release(&sb);
 996
 997        while (state->cur <= state->last) {
 998                const char *mail = am_path(state, msgnum(state));
 999                int apply_status;
1000
1001                if (!file_exists(mail))
1002                        goto next;
1003
1004                if (resume) {
1005                        validate_resume_state(state);
1006                        resume = 0;
1007                } else {
1008                        if (parse_mail(state, mail))
1009                                goto next; /* mail should be skipped */
1010
1011                        write_author_script(state);
1012                        write_commit_msg(state);
1013                }
1014
1015                say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1016
1017                apply_status = run_apply(state, NULL);
1018
1019                if (apply_status && state->threeway) {
1020                        struct strbuf sb = STRBUF_INIT;
1021
1022                        strbuf_addstr(&sb, am_path(state, "patch-merge-index"));
1023                        apply_status = fall_back_threeway(state, sb.buf);
1024                        strbuf_release(&sb);
1025
1026                        /*
1027                         * Applying the patch to an earlier tree and merging
1028                         * the result may have produced the same tree as ours.
1029                         */
1030                        if (!apply_status && !index_has_changes(NULL)) {
1031                                say(state, stdout, _("No changes -- Patch already applied."));
1032                                goto next;
1033                        }
1034                }
1035
1036                if (apply_status) {
1037                        int advice_amworkdir = 1;
1038
1039                        printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
1040                                linelen(state->msg), state->msg);
1041
1042                        git_config_get_bool("advice.amworkdir", &advice_amworkdir);
1043
1044                        if (advice_amworkdir)
1045                                printf_ln(_("The copy of the patch that failed is found in: %s"),
1046                                                am_path(state, "patch"));
1047
1048                        die_user_resolve(state);
1049                }
1050
1051                do_commit(state);
1052
1053next:
1054                am_next(state);
1055        }
1056
1057        am_destroy(state);
1058        run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1059}
1060
1061/**
1062 * Resume the current am session after patch application failure. The user did
1063 * all the hard work, and we do not have to do any patch application. Just
1064 * trust and commit what the user has in the index and working tree.
1065 */
1066static void am_resolve(struct am_state *state)
1067{
1068        validate_resume_state(state);
1069
1070        say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1071
1072        if (!index_has_changes(NULL)) {
1073                printf_ln(_("No changes - did you forget to use 'git add'?\n"
1074                        "If there is nothing left to stage, chances are that something else\n"
1075                        "already introduced the same changes; you might want to skip this patch."));
1076                die_user_resolve(state);
1077        }
1078
1079        if (unmerged_cache()) {
1080                printf_ln(_("You still have unmerged paths in your index.\n"
1081                        "Did you forget to use 'git add'?"));
1082                die_user_resolve(state);
1083        }
1084
1085        do_commit(state);
1086
1087        am_next(state);
1088        am_run(state, 0);
1089}
1090
1091/**
1092 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1093 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1094 * failure.
1095 */
1096static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1097{
1098        struct lock_file *lock_file;
1099        struct unpack_trees_options opts;
1100        struct tree_desc t[2];
1101
1102        if (parse_tree(head) || parse_tree(remote))
1103                return -1;
1104
1105        lock_file = xcalloc(1, sizeof(struct lock_file));
1106        hold_locked_index(lock_file, 1);
1107
1108        refresh_cache(REFRESH_QUIET);
1109
1110        memset(&opts, 0, sizeof(opts));
1111        opts.head_idx = 1;
1112        opts.src_index = &the_index;
1113        opts.dst_index = &the_index;
1114        opts.update = 1;
1115        opts.merge = 1;
1116        opts.reset = reset;
1117        opts.fn = twoway_merge;
1118        init_tree_desc(&t[0], head->buffer, head->size);
1119        init_tree_desc(&t[1], remote->buffer, remote->size);
1120
1121        if (unpack_trees(2, t, &opts)) {
1122                rollback_lock_file(lock_file);
1123                return -1;
1124        }
1125
1126        if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1127                die(_("unable to write new index file"));
1128
1129        return 0;
1130}
1131
1132/**
1133 * Clean the index without touching entries that are not modified between
1134 * `head` and `remote`.
1135 */
1136static int clean_index(const unsigned char *head, const unsigned char *remote)
1137{
1138        struct lock_file *lock_file;
1139        struct tree *head_tree, *remote_tree, *index_tree;
1140        unsigned char index[GIT_SHA1_RAWSZ];
1141        struct pathspec pathspec;
1142
1143        head_tree = parse_tree_indirect(head);
1144        if (!head_tree)
1145                return error(_("Could not parse object '%s'."), sha1_to_hex(head));
1146
1147        remote_tree = parse_tree_indirect(remote);
1148        if (!remote_tree)
1149                return error(_("Could not parse object '%s'."), sha1_to_hex(remote));
1150
1151        read_cache_unmerged();
1152
1153        if (fast_forward_to(head_tree, head_tree, 1))
1154                return -1;
1155
1156        if (write_cache_as_tree(index, 0, NULL))
1157                return -1;
1158
1159        index_tree = parse_tree_indirect(index);
1160        if (!index_tree)
1161                return error(_("Could not parse object '%s'."), sha1_to_hex(index));
1162
1163        if (fast_forward_to(index_tree, remote_tree, 0))
1164                return -1;
1165
1166        memset(&pathspec, 0, sizeof(pathspec));
1167
1168        lock_file = xcalloc(1, sizeof(struct lock_file));
1169        hold_locked_index(lock_file, 1);
1170
1171        if (read_tree(remote_tree, 0, &pathspec)) {
1172                rollback_lock_file(lock_file);
1173                return -1;
1174        }
1175
1176        if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1177                die(_("unable to write new index file"));
1178
1179        remove_branch_state();
1180
1181        return 0;
1182}
1183
1184/**
1185 * Resume the current am session by skipping the current patch.
1186 */
1187static void am_skip(struct am_state *state)
1188{
1189        unsigned char head[GIT_SHA1_RAWSZ];
1190
1191        if (get_sha1("HEAD", head))
1192                hashcpy(head, EMPTY_TREE_SHA1_BIN);
1193
1194        if (clean_index(head, head))
1195                die(_("failed to clean index"));
1196
1197        am_next(state);
1198        am_run(state, 0);
1199}
1200
1201/**
1202 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
1203 *
1204 * It is not safe to reset HEAD when:
1205 * 1. git-am previously failed because the index was dirty.
1206 * 2. HEAD has moved since git-am previously failed.
1207 */
1208static int safe_to_abort(const struct am_state *state)
1209{
1210        struct strbuf sb = STRBUF_INIT;
1211        unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
1212
1213        if (file_exists(am_path(state, "dirtyindex")))
1214                return 0;
1215
1216        if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
1217                if (get_sha1_hex(sb.buf, abort_safety))
1218                        die(_("could not parse %s"), am_path(state, "abort_safety"));
1219        } else
1220                hashclr(abort_safety);
1221
1222        if (get_sha1("HEAD", head))
1223                hashclr(head);
1224
1225        if (!hashcmp(head, abort_safety))
1226                return 1;
1227
1228        error(_("You seem to have moved HEAD since the last 'am' failure.\n"
1229                "Not rewinding to ORIG_HEAD"));
1230
1231        return 0;
1232}
1233
1234/**
1235 * Aborts the current am session if it is safe to do so.
1236 */
1237static void am_abort(struct am_state *state)
1238{
1239        unsigned char curr_head[GIT_SHA1_RAWSZ], orig_head[GIT_SHA1_RAWSZ];
1240        int has_curr_head, has_orig_head;
1241        char *curr_branch;
1242
1243        if (!safe_to_abort(state)) {
1244                am_destroy(state);
1245                return;
1246        }
1247
1248        curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
1249        has_curr_head = !is_null_sha1(curr_head);
1250        if (!has_curr_head)
1251                hashcpy(curr_head, EMPTY_TREE_SHA1_BIN);
1252
1253        has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
1254        if (!has_orig_head)
1255                hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
1256
1257        clean_index(curr_head, orig_head);
1258
1259        if (has_orig_head)
1260                update_ref("am --abort", "HEAD", orig_head,
1261                                has_curr_head ? curr_head : NULL, 0,
1262                                UPDATE_REFS_DIE_ON_ERR);
1263        else if (curr_branch)
1264                delete_ref(curr_branch, NULL, REF_NODEREF);
1265
1266        free(curr_branch);
1267        am_destroy(state);
1268}
1269
1270/**
1271 * parse_options() callback that validates and sets opt->value to the
1272 * PATCH_FORMAT_* enum value corresponding to `arg`.
1273 */
1274static int parse_opt_patchformat(const struct option *opt, const char *arg, int unset)
1275{
1276        int *opt_value = opt->value;
1277
1278        if (!strcmp(arg, "mbox"))
1279                *opt_value = PATCH_FORMAT_MBOX;
1280        else
1281                return error(_("Invalid value for --patch-format: %s"), arg);
1282        return 0;
1283}
1284
1285enum resume_mode {
1286        RESUME_FALSE = 0,
1287        RESUME_APPLY,
1288        RESUME_RESOLVED,
1289        RESUME_SKIP,
1290        RESUME_ABORT
1291};
1292
1293int cmd_am(int argc, const char **argv, const char *prefix)
1294{
1295        struct am_state state;
1296        int patch_format = PATCH_FORMAT_UNKNOWN;
1297        enum resume_mode resume = RESUME_FALSE;
1298
1299        const char * const usage[] = {
1300                N_("git am [options] [(<mbox>|<Maildir>)...]"),
1301                N_("git am [options] (--continue | --skip | --abort)"),
1302                NULL
1303        };
1304
1305        struct option options[] = {
1306                OPT_BOOL('3', "3way", &state.threeway,
1307                        N_("allow fall back on 3way merging if needed")),
1308                OPT__QUIET(&state.quiet, N_("be quiet")),
1309                OPT_BOOL('s', "signoff", &state.signoff,
1310                        N_("add a Signed-off-by line to the commit message")),
1311                OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
1312                        N_("format the patch(es) are in"),
1313                        parse_opt_patchformat),
1314                OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
1315                        N_("override error message when patch failure occurs")),
1316                OPT_CMDMODE(0, "continue", &resume,
1317                        N_("continue applying patches after resolving a conflict"),
1318                        RESUME_RESOLVED),
1319                OPT_CMDMODE('r', "resolved", &resume,
1320                        N_("synonyms for --continue"),
1321                        RESUME_RESOLVED),
1322                OPT_CMDMODE(0, "skip", &resume,
1323                        N_("skip the current patch"),
1324                        RESUME_SKIP),
1325                OPT_CMDMODE(0, "abort", &resume,
1326                        N_("restore the original branch and abort the patching operation."),
1327                        RESUME_ABORT),
1328                OPT_END()
1329        };
1330
1331        /*
1332         * NEEDSWORK: Once all the features of git-am.sh have been
1333         * re-implemented in builtin/am.c, this preamble can be removed.
1334         */
1335        if (!getenv("_GIT_USE_BUILTIN_AM")) {
1336                const char *path = mkpath("%s/git-am", git_exec_path());
1337
1338                if (sane_execvp(path, (char **)argv) < 0)
1339                        die_errno("could not exec %s", path);
1340        } else {
1341                prefix = setup_git_directory();
1342                trace_repo_setup(prefix);
1343                setup_work_tree();
1344        }
1345
1346        git_config(git_default_config, NULL);
1347
1348        am_state_init(&state, git_path("rebase-apply"));
1349
1350        argc = parse_options(argc, argv, prefix, options, usage, 0);
1351
1352        if (read_index_preload(&the_index, NULL) < 0)
1353                die(_("failed to read the index"));
1354
1355        if (am_in_progress(&state)) {
1356                /*
1357                 * Catch user error to feed us patches when there is a session
1358                 * in progress:
1359                 *
1360                 * 1. mbox path(s) are provided on the command-line.
1361                 * 2. stdin is not a tty: the user is trying to feed us a patch
1362                 *    from standard input. This is somewhat unreliable -- stdin
1363                 *    could be /dev/null for example and the caller did not
1364                 *    intend to feed us a patch but wanted to continue
1365                 *    unattended.
1366                 */
1367                if (argc || (resume == RESUME_FALSE && !isatty(0)))
1368                        die(_("previous rebase directory %s still exists but mbox given."),
1369                                state.dir);
1370
1371                if (resume == RESUME_FALSE)
1372                        resume = RESUME_APPLY;
1373
1374                am_load(&state);
1375        } else {
1376                struct argv_array paths = ARGV_ARRAY_INIT;
1377                int i;
1378
1379                if (resume)
1380                        die(_("Resolve operation not in progress, we are not resuming."));
1381
1382                for (i = 0; i < argc; i++) {
1383                        if (is_absolute_path(argv[i]) || !prefix)
1384                                argv_array_push(&paths, argv[i]);
1385                        else
1386                                argv_array_push(&paths, mkpath("%s/%s", prefix, argv[i]));
1387                }
1388
1389                am_setup(&state, patch_format, paths.argv);
1390
1391                argv_array_clear(&paths);
1392        }
1393
1394        switch (resume) {
1395        case RESUME_FALSE:
1396                am_run(&state, 0);
1397                break;
1398        case RESUME_APPLY:
1399                am_run(&state, 1);
1400                break;
1401        case RESUME_RESOLVED:
1402                am_resolve(&state);
1403                break;
1404        case RESUME_SKIP:
1405                am_skip(&state);
1406                break;
1407        case RESUME_ABORT:
1408                am_abort(&state);
1409                break;
1410        default:
1411                die("BUG: invalid resume value");
1412        }
1413
1414        am_state_release(&state);
1415
1416        return 0;
1417}