builtin / am.con commit builtin-am: support and auto-detect StGit patches (5ae41c7)
   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#include "notes-utils.h"
  27#include "rerere.h"
  28
  29/**
  30 * Returns 1 if the file is empty or does not exist, 0 otherwise.
  31 */
  32static int is_empty_file(const char *filename)
  33{
  34        struct stat st;
  35
  36        if (stat(filename, &st) < 0) {
  37                if (errno == ENOENT)
  38                        return 1;
  39                die_errno(_("could not stat %s"), filename);
  40        }
  41
  42        return !st.st_size;
  43}
  44
  45/**
  46 * Like strbuf_getline(), but treats both '\n' and "\r\n" as line terminators.
  47 */
  48static int strbuf_getline_crlf(struct strbuf *sb, FILE *fp)
  49{
  50        if (strbuf_getwholeline(sb, fp, '\n'))
  51                return EOF;
  52        if (sb->buf[sb->len - 1] == '\n') {
  53                strbuf_setlen(sb, sb->len - 1);
  54                if (sb->len > 0 && sb->buf[sb->len - 1] == '\r')
  55                        strbuf_setlen(sb, sb->len - 1);
  56        }
  57        return 0;
  58}
  59
  60/**
  61 * Returns the length of the first line of msg.
  62 */
  63static int linelen(const char *msg)
  64{
  65        return strchrnul(msg, '\n') - msg;
  66}
  67
  68/**
  69 * Returns true if `str` consists of only whitespace, false otherwise.
  70 */
  71static int str_isspace(const char *str)
  72{
  73        for (; *str; str++)
  74                if (!isspace(*str))
  75                        return 0;
  76
  77        return 1;
  78}
  79
  80enum patch_format {
  81        PATCH_FORMAT_UNKNOWN = 0,
  82        PATCH_FORMAT_MBOX,
  83        PATCH_FORMAT_STGIT
  84};
  85
  86enum keep_type {
  87        KEEP_FALSE = 0,
  88        KEEP_TRUE,      /* pass -k flag to git-mailinfo */
  89        KEEP_NON_PATCH  /* pass -b flag to git-mailinfo */
  90};
  91
  92enum scissors_type {
  93        SCISSORS_UNSET = -1,
  94        SCISSORS_FALSE = 0,  /* pass --no-scissors to git-mailinfo */
  95        SCISSORS_TRUE        /* pass --scissors to git-mailinfo */
  96};
  97
  98struct am_state {
  99        /* state directory path */
 100        char *dir;
 101
 102        /* current and last patch numbers, 1-indexed */
 103        int cur;
 104        int last;
 105
 106        /* commit metadata and message */
 107        char *author_name;
 108        char *author_email;
 109        char *author_date;
 110        char *msg;
 111        size_t msg_len;
 112
 113        /* when --rebasing, records the original commit the patch came from */
 114        unsigned char orig_commit[GIT_SHA1_RAWSZ];
 115
 116        /* number of digits in patch filename */
 117        int prec;
 118
 119        /* various operating modes and command line options */
 120        int threeway;
 121        int quiet;
 122        int signoff;
 123        int utf8;
 124        int keep; /* enum keep_type */
 125        int message_id;
 126        int scissors; /* enum scissors_type */
 127        struct argv_array git_apply_opts;
 128        const char *resolvemsg;
 129        int committer_date_is_author_date;
 130        int ignore_date;
 131        int allow_rerere_autoupdate;
 132        const char *sign_commit;
 133        int rebasing;
 134};
 135
 136/**
 137 * Initializes am_state with the default values. The state directory is set to
 138 * dir.
 139 */
 140static void am_state_init(struct am_state *state, const char *dir)
 141{
 142        int gpgsign;
 143
 144        memset(state, 0, sizeof(*state));
 145
 146        assert(dir);
 147        state->dir = xstrdup(dir);
 148
 149        state->prec = 4;
 150
 151        state->utf8 = 1;
 152
 153        git_config_get_bool("am.messageid", &state->message_id);
 154
 155        state->scissors = SCISSORS_UNSET;
 156
 157        argv_array_init(&state->git_apply_opts);
 158
 159        if (!git_config_get_bool("commit.gpgsign", &gpgsign))
 160                state->sign_commit = gpgsign ? "" : NULL;
 161}
 162
 163/**
 164 * Releases memory allocated by an am_state.
 165 */
 166static void am_state_release(struct am_state *state)
 167{
 168        free(state->dir);
 169        free(state->author_name);
 170        free(state->author_email);
 171        free(state->author_date);
 172        free(state->msg);
 173        argv_array_clear(&state->git_apply_opts);
 174}
 175
 176/**
 177 * Returns path relative to the am_state directory.
 178 */
 179static inline const char *am_path(const struct am_state *state, const char *path)
 180{
 181        return mkpath("%s/%s", state->dir, path);
 182}
 183
 184/**
 185 * If state->quiet is false, calls fprintf(fp, fmt, ...), and appends a newline
 186 * at the end.
 187 */
 188static void say(const struct am_state *state, FILE *fp, const char *fmt, ...)
 189{
 190        va_list ap;
 191
 192        va_start(ap, fmt);
 193        if (!state->quiet) {
 194                vfprintf(fp, fmt, ap);
 195                putc('\n', fp);
 196        }
 197        va_end(ap);
 198}
 199
 200/**
 201 * Returns 1 if there is an am session in progress, 0 otherwise.
 202 */
 203static int am_in_progress(const struct am_state *state)
 204{
 205        struct stat st;
 206
 207        if (lstat(state->dir, &st) < 0 || !S_ISDIR(st.st_mode))
 208                return 0;
 209        if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode))
 210                return 0;
 211        if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode))
 212                return 0;
 213        return 1;
 214}
 215
 216/**
 217 * Reads the contents of `file` in the `state` directory into `sb`. Returns the
 218 * number of bytes read on success, -1 if the file does not exist. If `trim` is
 219 * set, trailing whitespace will be removed.
 220 */
 221static int read_state_file(struct strbuf *sb, const struct am_state *state,
 222                        const char *file, int trim)
 223{
 224        strbuf_reset(sb);
 225
 226        if (strbuf_read_file(sb, am_path(state, file), 0) >= 0) {
 227                if (trim)
 228                        strbuf_trim(sb);
 229
 230                return sb->len;
 231        }
 232
 233        if (errno == ENOENT)
 234                return -1;
 235
 236        die_errno(_("could not read '%s'"), am_path(state, file));
 237}
 238
 239/**
 240 * Reads a KEY=VALUE shell variable assignment from `fp`, returning the VALUE
 241 * as a newly-allocated string. VALUE must be a quoted string, and the KEY must
 242 * match `key`. Returns NULL on failure.
 243 *
 244 * This is used by read_author_script() to read the GIT_AUTHOR_* variables from
 245 * the author-script.
 246 */
 247static char *read_shell_var(FILE *fp, const char *key)
 248{
 249        struct strbuf sb = STRBUF_INIT;
 250        const char *str;
 251
 252        if (strbuf_getline(&sb, fp, '\n'))
 253                goto fail;
 254
 255        if (!skip_prefix(sb.buf, key, &str))
 256                goto fail;
 257
 258        if (!skip_prefix(str, "=", &str))
 259                goto fail;
 260
 261        strbuf_remove(&sb, 0, str - sb.buf);
 262
 263        str = sq_dequote(sb.buf);
 264        if (!str)
 265                goto fail;
 266
 267        return strbuf_detach(&sb, NULL);
 268
 269fail:
 270        strbuf_release(&sb);
 271        return NULL;
 272}
 273
 274/**
 275 * Reads and parses the state directory's "author-script" file, and sets
 276 * state->author_name, state->author_email and state->author_date accordingly.
 277 * Returns 0 on success, -1 if the file could not be parsed.
 278 *
 279 * The author script is of the format:
 280 *
 281 *      GIT_AUTHOR_NAME='$author_name'
 282 *      GIT_AUTHOR_EMAIL='$author_email'
 283 *      GIT_AUTHOR_DATE='$author_date'
 284 *
 285 * where $author_name, $author_email and $author_date are quoted. We are strict
 286 * with our parsing, as the file was meant to be eval'd in the old git-am.sh
 287 * script, and thus if the file differs from what this function expects, it is
 288 * better to bail out than to do something that the user does not expect.
 289 */
 290static int read_author_script(struct am_state *state)
 291{
 292        const char *filename = am_path(state, "author-script");
 293        FILE *fp;
 294
 295        assert(!state->author_name);
 296        assert(!state->author_email);
 297        assert(!state->author_date);
 298
 299        fp = fopen(filename, "r");
 300        if (!fp) {
 301                if (errno == ENOENT)
 302                        return 0;
 303                die_errno(_("could not open '%s' for reading"), filename);
 304        }
 305
 306        state->author_name = read_shell_var(fp, "GIT_AUTHOR_NAME");
 307        if (!state->author_name) {
 308                fclose(fp);
 309                return -1;
 310        }
 311
 312        state->author_email = read_shell_var(fp, "GIT_AUTHOR_EMAIL");
 313        if (!state->author_email) {
 314                fclose(fp);
 315                return -1;
 316        }
 317
 318        state->author_date = read_shell_var(fp, "GIT_AUTHOR_DATE");
 319        if (!state->author_date) {
 320                fclose(fp);
 321                return -1;
 322        }
 323
 324        if (fgetc(fp) != EOF) {
 325                fclose(fp);
 326                return -1;
 327        }
 328
 329        fclose(fp);
 330        return 0;
 331}
 332
 333/**
 334 * Saves state->author_name, state->author_email and state->author_date in the
 335 * state directory's "author-script" file.
 336 */
 337static void write_author_script(const struct am_state *state)
 338{
 339        struct strbuf sb = STRBUF_INIT;
 340
 341        strbuf_addstr(&sb, "GIT_AUTHOR_NAME=");
 342        sq_quote_buf(&sb, state->author_name);
 343        strbuf_addch(&sb, '\n');
 344
 345        strbuf_addstr(&sb, "GIT_AUTHOR_EMAIL=");
 346        sq_quote_buf(&sb, state->author_email);
 347        strbuf_addch(&sb, '\n');
 348
 349        strbuf_addstr(&sb, "GIT_AUTHOR_DATE=");
 350        sq_quote_buf(&sb, state->author_date);
 351        strbuf_addch(&sb, '\n');
 352
 353        write_file(am_path(state, "author-script"), 1, "%s", sb.buf);
 354
 355        strbuf_release(&sb);
 356}
 357
 358/**
 359 * Reads the commit message from the state directory's "final-commit" file,
 360 * setting state->msg to its contents and state->msg_len to the length of its
 361 * contents in bytes.
 362 *
 363 * Returns 0 on success, -1 if the file does not exist.
 364 */
 365static int read_commit_msg(struct am_state *state)
 366{
 367        struct strbuf sb = STRBUF_INIT;
 368
 369        assert(!state->msg);
 370
 371        if (read_state_file(&sb, state, "final-commit", 0) < 0) {
 372                strbuf_release(&sb);
 373                return -1;
 374        }
 375
 376        state->msg = strbuf_detach(&sb, &state->msg_len);
 377        return 0;
 378}
 379
 380/**
 381 * Saves state->msg in the state directory's "final-commit" file.
 382 */
 383static void write_commit_msg(const struct am_state *state)
 384{
 385        int fd;
 386        const char *filename = am_path(state, "final-commit");
 387
 388        fd = xopen(filename, O_WRONLY | O_CREAT, 0666);
 389        if (write_in_full(fd, state->msg, state->msg_len) < 0)
 390                die_errno(_("could not write to %s"), filename);
 391        close(fd);
 392}
 393
 394/**
 395 * Loads state from disk.
 396 */
 397static void am_load(struct am_state *state)
 398{
 399        struct strbuf sb = STRBUF_INIT;
 400
 401        if (read_state_file(&sb, state, "next", 1) < 0)
 402                die("BUG: state file 'next' does not exist");
 403        state->cur = strtol(sb.buf, NULL, 10);
 404
 405        if (read_state_file(&sb, state, "last", 1) < 0)
 406                die("BUG: state file 'last' does not exist");
 407        state->last = strtol(sb.buf, NULL, 10);
 408
 409        if (read_author_script(state) < 0)
 410                die(_("could not parse author script"));
 411
 412        read_commit_msg(state);
 413
 414        if (read_state_file(&sb, state, "original-commit", 1) < 0)
 415                hashclr(state->orig_commit);
 416        else if (get_sha1_hex(sb.buf, state->orig_commit) < 0)
 417                die(_("could not parse %s"), am_path(state, "original-commit"));
 418
 419        read_state_file(&sb, state, "threeway", 1);
 420        state->threeway = !strcmp(sb.buf, "t");
 421
 422        read_state_file(&sb, state, "quiet", 1);
 423        state->quiet = !strcmp(sb.buf, "t");
 424
 425        read_state_file(&sb, state, "sign", 1);
 426        state->signoff = !strcmp(sb.buf, "t");
 427
 428        read_state_file(&sb, state, "utf8", 1);
 429        state->utf8 = !strcmp(sb.buf, "t");
 430
 431        read_state_file(&sb, state, "keep", 1);
 432        if (!strcmp(sb.buf, "t"))
 433                state->keep = KEEP_TRUE;
 434        else if (!strcmp(sb.buf, "b"))
 435                state->keep = KEEP_NON_PATCH;
 436        else
 437                state->keep = KEEP_FALSE;
 438
 439        read_state_file(&sb, state, "messageid", 1);
 440        state->message_id = !strcmp(sb.buf, "t");
 441
 442        read_state_file(&sb, state, "scissors", 1);
 443        if (!strcmp(sb.buf, "t"))
 444                state->scissors = SCISSORS_TRUE;
 445        else if (!strcmp(sb.buf, "f"))
 446                state->scissors = SCISSORS_FALSE;
 447        else
 448                state->scissors = SCISSORS_UNSET;
 449
 450        read_state_file(&sb, state, "apply-opt", 1);
 451        argv_array_clear(&state->git_apply_opts);
 452        if (sq_dequote_to_argv_array(sb.buf, &state->git_apply_opts) < 0)
 453                die(_("could not parse %s"), am_path(state, "apply-opt"));
 454
 455        state->rebasing = !!file_exists(am_path(state, "rebasing"));
 456
 457        strbuf_release(&sb);
 458}
 459
 460/**
 461 * Removes the am_state directory, forcefully terminating the current am
 462 * session.
 463 */
 464static void am_destroy(const struct am_state *state)
 465{
 466        struct strbuf sb = STRBUF_INIT;
 467
 468        strbuf_addstr(&sb, state->dir);
 469        remove_dir_recursively(&sb, 0);
 470        strbuf_release(&sb);
 471}
 472
 473/**
 474 * Runs applypatch-msg hook. Returns its exit code.
 475 */
 476static int run_applypatch_msg_hook(struct am_state *state)
 477{
 478        int ret;
 479
 480        assert(state->msg);
 481        ret = run_hook_le(NULL, "applypatch-msg", am_path(state, "final-commit"), NULL);
 482
 483        if (!ret) {
 484                free(state->msg);
 485                state->msg = NULL;
 486                if (read_commit_msg(state) < 0)
 487                        die(_("'%s' was deleted by the applypatch-msg hook"),
 488                                am_path(state, "final-commit"));
 489        }
 490
 491        return ret;
 492}
 493
 494/**
 495 * Runs post-rewrite hook. Returns it exit code.
 496 */
 497static int run_post_rewrite_hook(const struct am_state *state)
 498{
 499        struct child_process cp = CHILD_PROCESS_INIT;
 500        const char *hook = find_hook("post-rewrite");
 501        int ret;
 502
 503        if (!hook)
 504                return 0;
 505
 506        argv_array_push(&cp.args, hook);
 507        argv_array_push(&cp.args, "rebase");
 508
 509        cp.in = xopen(am_path(state, "rewritten"), O_RDONLY);
 510        cp.stdout_to_stderr = 1;
 511
 512        ret = run_command(&cp);
 513
 514        close(cp.in);
 515        return ret;
 516}
 517
 518/**
 519 * Reads the state directory's "rewritten" file, and copies notes from the old
 520 * commits listed in the file to their rewritten commits.
 521 *
 522 * Returns 0 on success, -1 on failure.
 523 */
 524static int copy_notes_for_rebase(const struct am_state *state)
 525{
 526        struct notes_rewrite_cfg *c;
 527        struct strbuf sb = STRBUF_INIT;
 528        const char *invalid_line = _("Malformed input line: '%s'.");
 529        const char *msg = "Notes added by 'git rebase'";
 530        FILE *fp;
 531        int ret = 0;
 532
 533        assert(state->rebasing);
 534
 535        c = init_copy_notes_for_rewrite("rebase");
 536        if (!c)
 537                return 0;
 538
 539        fp = xfopen(am_path(state, "rewritten"), "r");
 540
 541        while (!strbuf_getline(&sb, fp, '\n')) {
 542                unsigned char from_obj[GIT_SHA1_RAWSZ], to_obj[GIT_SHA1_RAWSZ];
 543
 544                if (sb.len != GIT_SHA1_HEXSZ * 2 + 1) {
 545                        ret = error(invalid_line, sb.buf);
 546                        goto finish;
 547                }
 548
 549                if (get_sha1_hex(sb.buf, from_obj)) {
 550                        ret = error(invalid_line, sb.buf);
 551                        goto finish;
 552                }
 553
 554                if (sb.buf[GIT_SHA1_HEXSZ] != ' ') {
 555                        ret = error(invalid_line, sb.buf);
 556                        goto finish;
 557                }
 558
 559                if (get_sha1_hex(sb.buf + GIT_SHA1_HEXSZ + 1, to_obj)) {
 560                        ret = error(invalid_line, sb.buf);
 561                        goto finish;
 562                }
 563
 564                if (copy_note_for_rewrite(c, from_obj, to_obj))
 565                        ret = error(_("Failed to copy notes from '%s' to '%s'"),
 566                                        sha1_to_hex(from_obj), sha1_to_hex(to_obj));
 567        }
 568
 569finish:
 570        finish_copy_notes_for_rewrite(c, msg);
 571        fclose(fp);
 572        strbuf_release(&sb);
 573        return ret;
 574}
 575
 576/**
 577 * Determines if the file looks like a piece of RFC2822 mail by grabbing all
 578 * non-indented lines and checking if they look like they begin with valid
 579 * header field names.
 580 *
 581 * Returns 1 if the file looks like a piece of mail, 0 otherwise.
 582 */
 583static int is_mail(FILE *fp)
 584{
 585        const char *header_regex = "^[!-9;-~]+:";
 586        struct strbuf sb = STRBUF_INIT;
 587        regex_t regex;
 588        int ret = 1;
 589
 590        if (fseek(fp, 0L, SEEK_SET))
 591                die_errno(_("fseek failed"));
 592
 593        if (regcomp(&regex, header_regex, REG_NOSUB | REG_EXTENDED))
 594                die("invalid pattern: %s", header_regex);
 595
 596        while (!strbuf_getline_crlf(&sb, fp)) {
 597                if (!sb.len)
 598                        break; /* End of header */
 599
 600                /* Ignore indented folded lines */
 601                if (*sb.buf == '\t' || *sb.buf == ' ')
 602                        continue;
 603
 604                /* It's a header if it matches header_regex */
 605                if (regexec(&regex, sb.buf, 0, NULL, 0)) {
 606                        ret = 0;
 607                        goto done;
 608                }
 609        }
 610
 611done:
 612        regfree(&regex);
 613        strbuf_release(&sb);
 614        return ret;
 615}
 616
 617/**
 618 * Attempts to detect the patch_format of the patches contained in `paths`,
 619 * returning the PATCH_FORMAT_* enum value. Returns PATCH_FORMAT_UNKNOWN if
 620 * detection fails.
 621 */
 622static int detect_patch_format(const char **paths)
 623{
 624        enum patch_format ret = PATCH_FORMAT_UNKNOWN;
 625        struct strbuf l1 = STRBUF_INIT;
 626        struct strbuf l2 = STRBUF_INIT;
 627        struct strbuf l3 = STRBUF_INIT;
 628        FILE *fp;
 629
 630        /*
 631         * We default to mbox format if input is from stdin and for directories
 632         */
 633        if (!*paths || !strcmp(*paths, "-") || is_directory(*paths))
 634                return PATCH_FORMAT_MBOX;
 635
 636        /*
 637         * Otherwise, check the first few lines of the first patch, starting
 638         * from the first non-blank line, to try to detect its format.
 639         */
 640
 641        fp = xfopen(*paths, "r");
 642
 643        while (!strbuf_getline_crlf(&l1, fp)) {
 644                if (l1.len)
 645                        break;
 646        }
 647
 648        if (starts_with(l1.buf, "From ") || starts_with(l1.buf, "From: ")) {
 649                ret = PATCH_FORMAT_MBOX;
 650                goto done;
 651        }
 652
 653        strbuf_reset(&l2);
 654        strbuf_getline_crlf(&l2, fp);
 655        strbuf_reset(&l3);
 656        strbuf_getline_crlf(&l3, fp);
 657
 658        /*
 659         * If the second line is empty and the third is a From, Author or Date
 660         * entry, this is likely an StGit patch.
 661         */
 662        if (l1.len && !l2.len &&
 663                (starts_with(l3.buf, "From:") ||
 664                 starts_with(l3.buf, "Author:") ||
 665                 starts_with(l3.buf, "Date:"))) {
 666                ret = PATCH_FORMAT_STGIT;
 667                goto done;
 668        }
 669
 670        if (l1.len && is_mail(fp)) {
 671                ret = PATCH_FORMAT_MBOX;
 672                goto done;
 673        }
 674
 675done:
 676        fclose(fp);
 677        strbuf_release(&l1);
 678        return ret;
 679}
 680
 681/**
 682 * Splits out individual email patches from `paths`, where each path is either
 683 * a mbox file or a Maildir. Returns 0 on success, -1 on failure.
 684 */
 685static int split_mail_mbox(struct am_state *state, const char **paths, int keep_cr)
 686{
 687        struct child_process cp = CHILD_PROCESS_INIT;
 688        struct strbuf last = STRBUF_INIT;
 689
 690        cp.git_cmd = 1;
 691        argv_array_push(&cp.args, "mailsplit");
 692        argv_array_pushf(&cp.args, "-d%d", state->prec);
 693        argv_array_pushf(&cp.args, "-o%s", state->dir);
 694        argv_array_push(&cp.args, "-b");
 695        if (keep_cr)
 696                argv_array_push(&cp.args, "--keep-cr");
 697        argv_array_push(&cp.args, "--");
 698        argv_array_pushv(&cp.args, paths);
 699
 700        if (capture_command(&cp, &last, 8))
 701                return -1;
 702
 703        state->cur = 1;
 704        state->last = strtol(last.buf, NULL, 10);
 705
 706        return 0;
 707}
 708
 709/**
 710 * Callback signature for split_mail_conv(). The foreign patch should be
 711 * read from `in`, and the converted patch (in RFC2822 mail format) should be
 712 * written to `out`. Return 0 on success, or -1 on failure.
 713 */
 714typedef int (*mail_conv_fn)(FILE *out, FILE *in, int keep_cr);
 715
 716/**
 717 * Calls `fn` for each file in `paths` to convert the foreign patch to the
 718 * RFC2822 mail format suitable for parsing with git-mailinfo.
 719 *
 720 * Returns 0 on success, -1 on failure.
 721 */
 722static int split_mail_conv(mail_conv_fn fn, struct am_state *state,
 723                        const char **paths, int keep_cr)
 724{
 725        static const char *stdin_only[] = {"-", NULL};
 726        int i;
 727
 728        if (!*paths)
 729                paths = stdin_only;
 730
 731        for (i = 0; *paths; paths++, i++) {
 732                FILE *in, *out;
 733                const char *mail;
 734                int ret;
 735
 736                if (!strcmp(*paths, "-"))
 737                        in = stdin;
 738                else
 739                        in = fopen(*paths, "r");
 740
 741                if (!in)
 742                        return error(_("could not open '%s' for reading: %s"),
 743                                        *paths, strerror(errno));
 744
 745                mail = mkpath("%s/%0*d", state->dir, state->prec, i + 1);
 746
 747                out = fopen(mail, "w");
 748                if (!out)
 749                        return error(_("could not open '%s' for writing: %s"),
 750                                        mail, strerror(errno));
 751
 752                ret = fn(out, in, keep_cr);
 753
 754                fclose(out);
 755                fclose(in);
 756
 757                if (ret)
 758                        return error(_("could not parse patch '%s'"), *paths);
 759        }
 760
 761        state->cur = 1;
 762        state->last = i;
 763        return 0;
 764}
 765
 766/**
 767 * A split_mail_conv() callback that converts an StGit patch to an RFC2822
 768 * message suitable for parsing with git-mailinfo.
 769 */
 770static int stgit_patch_to_mail(FILE *out, FILE *in, int keep_cr)
 771{
 772        struct strbuf sb = STRBUF_INIT;
 773        int subject_printed = 0;
 774
 775        while (!strbuf_getline(&sb, in, '\n')) {
 776                const char *str;
 777
 778                if (str_isspace(sb.buf))
 779                        continue;
 780                else if (skip_prefix(sb.buf, "Author:", &str))
 781                        fprintf(out, "From:%s\n", str);
 782                else if (starts_with(sb.buf, "From") || starts_with(sb.buf, "Date"))
 783                        fprintf(out, "%s\n", sb.buf);
 784                else if (!subject_printed) {
 785                        fprintf(out, "Subject: %s\n", sb.buf);
 786                        subject_printed = 1;
 787                } else {
 788                        fprintf(out, "\n%s\n", sb.buf);
 789                        break;
 790                }
 791        }
 792
 793        strbuf_reset(&sb);
 794        while (strbuf_fread(&sb, 8192, in) > 0) {
 795                fwrite(sb.buf, 1, sb.len, out);
 796                strbuf_reset(&sb);
 797        }
 798
 799        strbuf_release(&sb);
 800        return 0;
 801}
 802
 803/**
 804 * Splits a list of files/directories into individual email patches. Each path
 805 * in `paths` must be a file/directory that is formatted according to
 806 * `patch_format`.
 807 *
 808 * Once split out, the individual email patches will be stored in the state
 809 * directory, with each patch's filename being its index, padded to state->prec
 810 * digits.
 811 *
 812 * state->cur will be set to the index of the first mail, and state->last will
 813 * be set to the index of the last mail.
 814 *
 815 * Set keep_cr to 0 to convert all lines ending with \r\n to end with \n, 1
 816 * to disable this behavior, -1 to use the default configured setting.
 817 *
 818 * Returns 0 on success, -1 on failure.
 819 */
 820static int split_mail(struct am_state *state, enum patch_format patch_format,
 821                        const char **paths, int keep_cr)
 822{
 823        if (keep_cr < 0) {
 824                keep_cr = 0;
 825                git_config_get_bool("am.keepcr", &keep_cr);
 826        }
 827
 828        switch (patch_format) {
 829        case PATCH_FORMAT_MBOX:
 830                return split_mail_mbox(state, paths, keep_cr);
 831        case PATCH_FORMAT_STGIT:
 832                return split_mail_conv(stgit_patch_to_mail, state, paths, keep_cr);
 833        default:
 834                die("BUG: invalid patch_format");
 835        }
 836        return -1;
 837}
 838
 839/**
 840 * Setup a new am session for applying patches
 841 */
 842static void am_setup(struct am_state *state, enum patch_format patch_format,
 843                        const char **paths, int keep_cr)
 844{
 845        unsigned char curr_head[GIT_SHA1_RAWSZ];
 846        const char *str;
 847        struct strbuf sb = STRBUF_INIT;
 848
 849        if (!patch_format)
 850                patch_format = detect_patch_format(paths);
 851
 852        if (!patch_format) {
 853                fprintf_ln(stderr, _("Patch format detection failed."));
 854                exit(128);
 855        }
 856
 857        if (mkdir(state->dir, 0777) < 0 && errno != EEXIST)
 858                die_errno(_("failed to create directory '%s'"), state->dir);
 859
 860        if (split_mail(state, patch_format, paths, keep_cr) < 0) {
 861                am_destroy(state);
 862                die(_("Failed to split patches."));
 863        }
 864
 865        if (state->rebasing)
 866                state->threeway = 1;
 867
 868        write_file(am_path(state, "threeway"), 1, state->threeway ? "t" : "f");
 869
 870        write_file(am_path(state, "quiet"), 1, state->quiet ? "t" : "f");
 871
 872        write_file(am_path(state, "sign"), 1, state->signoff ? "t" : "f");
 873
 874        write_file(am_path(state, "utf8"), 1, state->utf8 ? "t" : "f");
 875
 876        switch (state->keep) {
 877        case KEEP_FALSE:
 878                str = "f";
 879                break;
 880        case KEEP_TRUE:
 881                str = "t";
 882                break;
 883        case KEEP_NON_PATCH:
 884                str = "b";
 885                break;
 886        default:
 887                die("BUG: invalid value for state->keep");
 888        }
 889
 890        write_file(am_path(state, "keep"), 1, "%s", str);
 891
 892        write_file(am_path(state, "messageid"), 1, state->message_id ? "t" : "f");
 893
 894        switch (state->scissors) {
 895        case SCISSORS_UNSET:
 896                str = "";
 897                break;
 898        case SCISSORS_FALSE:
 899                str = "f";
 900                break;
 901        case SCISSORS_TRUE:
 902                str = "t";
 903                break;
 904        default:
 905                die("BUG: invalid value for state->scissors");
 906        }
 907
 908        write_file(am_path(state, "scissors"), 1, "%s", str);
 909
 910        sq_quote_argv(&sb, state->git_apply_opts.argv, 0);
 911        write_file(am_path(state, "apply-opt"), 1, "%s", sb.buf);
 912
 913        if (state->rebasing)
 914                write_file(am_path(state, "rebasing"), 1, "%s", "");
 915        else
 916                write_file(am_path(state, "applying"), 1, "%s", "");
 917
 918        if (!get_sha1("HEAD", curr_head)) {
 919                write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
 920                if (!state->rebasing)
 921                        update_ref("am", "ORIG_HEAD", curr_head, NULL, 0,
 922                                        UPDATE_REFS_DIE_ON_ERR);
 923        } else {
 924                write_file(am_path(state, "abort-safety"), 1, "%s", "");
 925                if (!state->rebasing)
 926                        delete_ref("ORIG_HEAD", NULL, 0);
 927        }
 928
 929        /*
 930         * NOTE: Since the "next" and "last" files determine if an am_state
 931         * session is in progress, they should be written last.
 932         */
 933
 934        write_file(am_path(state, "next"), 1, "%d", state->cur);
 935
 936        write_file(am_path(state, "last"), 1, "%d", state->last);
 937
 938        strbuf_release(&sb);
 939}
 940
 941/**
 942 * Increments the patch pointer, and cleans am_state for the application of the
 943 * next patch.
 944 */
 945static void am_next(struct am_state *state)
 946{
 947        unsigned char head[GIT_SHA1_RAWSZ];
 948
 949        free(state->author_name);
 950        state->author_name = NULL;
 951
 952        free(state->author_email);
 953        state->author_email = NULL;
 954
 955        free(state->author_date);
 956        state->author_date = NULL;
 957
 958        free(state->msg);
 959        state->msg = NULL;
 960        state->msg_len = 0;
 961
 962        unlink(am_path(state, "author-script"));
 963        unlink(am_path(state, "final-commit"));
 964
 965        hashclr(state->orig_commit);
 966        unlink(am_path(state, "original-commit"));
 967
 968        if (!get_sha1("HEAD", head))
 969                write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
 970        else
 971                write_file(am_path(state, "abort-safety"), 1, "%s", "");
 972
 973        state->cur++;
 974        write_file(am_path(state, "next"), 1, "%d", state->cur);
 975}
 976
 977/**
 978 * Returns the filename of the current patch email.
 979 */
 980static const char *msgnum(const struct am_state *state)
 981{
 982        static struct strbuf sb = STRBUF_INIT;
 983
 984        strbuf_reset(&sb);
 985        strbuf_addf(&sb, "%0*d", state->prec, state->cur);
 986
 987        return sb.buf;
 988}
 989
 990/**
 991 * Refresh and write index.
 992 */
 993static void refresh_and_write_cache(void)
 994{
 995        struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
 996
 997        hold_locked_index(lock_file, 1);
 998        refresh_cache(REFRESH_QUIET);
 999        if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1000                die(_("unable to write index file"));
1001}
1002
1003/**
1004 * Returns 1 if the index differs from HEAD, 0 otherwise. When on an unborn
1005 * branch, returns 1 if there are entries in the index, 0 otherwise. If an
1006 * strbuf is provided, the space-separated list of files that differ will be
1007 * appended to it.
1008 */
1009static int index_has_changes(struct strbuf *sb)
1010{
1011        unsigned char head[GIT_SHA1_RAWSZ];
1012        int i;
1013
1014        if (!get_sha1_tree("HEAD", head)) {
1015                struct diff_options opt;
1016
1017                diff_setup(&opt);
1018                DIFF_OPT_SET(&opt, EXIT_WITH_STATUS);
1019                if (!sb)
1020                        DIFF_OPT_SET(&opt, QUICK);
1021                do_diff_cache(head, &opt);
1022                diffcore_std(&opt);
1023                for (i = 0; sb && i < diff_queued_diff.nr; i++) {
1024                        if (i)
1025                                strbuf_addch(sb, ' ');
1026                        strbuf_addstr(sb, diff_queued_diff.queue[i]->two->path);
1027                }
1028                diff_flush(&opt);
1029                return DIFF_OPT_TST(&opt, HAS_CHANGES) != 0;
1030        } else {
1031                for (i = 0; sb && i < active_nr; i++) {
1032                        if (i)
1033                                strbuf_addch(sb, ' ');
1034                        strbuf_addstr(sb, active_cache[i]->name);
1035                }
1036                return !!active_nr;
1037        }
1038}
1039
1040/**
1041 * Dies with a user-friendly message on how to proceed after resolving the
1042 * problem. This message can be overridden with state->resolvemsg.
1043 */
1044static void NORETURN die_user_resolve(const struct am_state *state)
1045{
1046        if (state->resolvemsg) {
1047                printf_ln("%s", state->resolvemsg);
1048        } else {
1049                const char *cmdline = "git am";
1050
1051                printf_ln(_("When you have resolved this problem, run \"%s --continue\"."), cmdline);
1052                printf_ln(_("If you prefer to skip this patch, run \"%s --skip\" instead."), cmdline);
1053                printf_ln(_("To restore the original branch and stop patching, run \"%s --abort\"."), cmdline);
1054        }
1055
1056        exit(128);
1057}
1058
1059/**
1060 * Parses `mail` using git-mailinfo, extracting its patch and authorship info.
1061 * state->msg will be set to the patch message. state->author_name,
1062 * state->author_email and state->author_date will be set to the patch author's
1063 * name, email and date respectively. The patch body will be written to the
1064 * state directory's "patch" file.
1065 *
1066 * Returns 1 if the patch should be skipped, 0 otherwise.
1067 */
1068static int parse_mail(struct am_state *state, const char *mail)
1069{
1070        FILE *fp;
1071        struct child_process cp = CHILD_PROCESS_INIT;
1072        struct strbuf sb = STRBUF_INIT;
1073        struct strbuf msg = STRBUF_INIT;
1074        struct strbuf author_name = STRBUF_INIT;
1075        struct strbuf author_date = STRBUF_INIT;
1076        struct strbuf author_email = STRBUF_INIT;
1077        int ret = 0;
1078
1079        cp.git_cmd = 1;
1080        cp.in = xopen(mail, O_RDONLY, 0);
1081        cp.out = xopen(am_path(state, "info"), O_WRONLY | O_CREAT, 0777);
1082
1083        argv_array_push(&cp.args, "mailinfo");
1084        argv_array_push(&cp.args, state->utf8 ? "-u" : "-n");
1085
1086        switch (state->keep) {
1087        case KEEP_FALSE:
1088                break;
1089        case KEEP_TRUE:
1090                argv_array_push(&cp.args, "-k");
1091                break;
1092        case KEEP_NON_PATCH:
1093                argv_array_push(&cp.args, "-b");
1094                break;
1095        default:
1096                die("BUG: invalid value for state->keep");
1097        }
1098
1099        if (state->message_id)
1100                argv_array_push(&cp.args, "-m");
1101
1102        switch (state->scissors) {
1103        case SCISSORS_UNSET:
1104                break;
1105        case SCISSORS_FALSE:
1106                argv_array_push(&cp.args, "--no-scissors");
1107                break;
1108        case SCISSORS_TRUE:
1109                argv_array_push(&cp.args, "--scissors");
1110                break;
1111        default:
1112                die("BUG: invalid value for state->scissors");
1113        }
1114
1115        argv_array_push(&cp.args, am_path(state, "msg"));
1116        argv_array_push(&cp.args, am_path(state, "patch"));
1117
1118        if (run_command(&cp) < 0)
1119                die("could not parse patch");
1120
1121        close(cp.in);
1122        close(cp.out);
1123
1124        /* Extract message and author information */
1125        fp = xfopen(am_path(state, "info"), "r");
1126        while (!strbuf_getline(&sb, fp, '\n')) {
1127                const char *x;
1128
1129                if (skip_prefix(sb.buf, "Subject: ", &x)) {
1130                        if (msg.len)
1131                                strbuf_addch(&msg, '\n');
1132                        strbuf_addstr(&msg, x);
1133                } else if (skip_prefix(sb.buf, "Author: ", &x))
1134                        strbuf_addstr(&author_name, x);
1135                else if (skip_prefix(sb.buf, "Email: ", &x))
1136                        strbuf_addstr(&author_email, x);
1137                else if (skip_prefix(sb.buf, "Date: ", &x))
1138                        strbuf_addstr(&author_date, x);
1139        }
1140        fclose(fp);
1141
1142        /* Skip pine's internal folder data */
1143        if (!strcmp(author_name.buf, "Mail System Internal Data")) {
1144                ret = 1;
1145                goto finish;
1146        }
1147
1148        if (is_empty_file(am_path(state, "patch"))) {
1149                printf_ln(_("Patch is empty. Was it split wrong?"));
1150                die_user_resolve(state);
1151        }
1152
1153        strbuf_addstr(&msg, "\n\n");
1154        if (strbuf_read_file(&msg, am_path(state, "msg"), 0) < 0)
1155                die_errno(_("could not read '%s'"), am_path(state, "msg"));
1156        stripspace(&msg, 0);
1157
1158        if (state->signoff)
1159                append_signoff(&msg, 0, 0);
1160
1161        assert(!state->author_name);
1162        state->author_name = strbuf_detach(&author_name, NULL);
1163
1164        assert(!state->author_email);
1165        state->author_email = strbuf_detach(&author_email, NULL);
1166
1167        assert(!state->author_date);
1168        state->author_date = strbuf_detach(&author_date, NULL);
1169
1170        assert(!state->msg);
1171        state->msg = strbuf_detach(&msg, &state->msg_len);
1172
1173finish:
1174        strbuf_release(&msg);
1175        strbuf_release(&author_date);
1176        strbuf_release(&author_email);
1177        strbuf_release(&author_name);
1178        strbuf_release(&sb);
1179        return ret;
1180}
1181
1182/**
1183 * Sets commit_id to the commit hash where the mail was generated from.
1184 * Returns 0 on success, -1 on failure.
1185 */
1186static int get_mail_commit_sha1(unsigned char *commit_id, const char *mail)
1187{
1188        struct strbuf sb = STRBUF_INIT;
1189        FILE *fp = xfopen(mail, "r");
1190        const char *x;
1191
1192        if (strbuf_getline(&sb, fp, '\n'))
1193                return -1;
1194
1195        if (!skip_prefix(sb.buf, "From ", &x))
1196                return -1;
1197
1198        if (get_sha1_hex(x, commit_id) < 0)
1199                return -1;
1200
1201        strbuf_release(&sb);
1202        fclose(fp);
1203        return 0;
1204}
1205
1206/**
1207 * Sets state->msg, state->author_name, state->author_email, state->author_date
1208 * to the commit's respective info.
1209 */
1210static void get_commit_info(struct am_state *state, struct commit *commit)
1211{
1212        const char *buffer, *ident_line, *author_date, *msg;
1213        size_t ident_len;
1214        struct ident_split ident_split;
1215        struct strbuf sb = STRBUF_INIT;
1216
1217        buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding());
1218
1219        ident_line = find_commit_header(buffer, "author", &ident_len);
1220
1221        if (split_ident_line(&ident_split, ident_line, ident_len) < 0) {
1222                strbuf_add(&sb, ident_line, ident_len);
1223                die(_("invalid ident line: %s"), sb.buf);
1224        }
1225
1226        assert(!state->author_name);
1227        if (ident_split.name_begin) {
1228                strbuf_add(&sb, ident_split.name_begin,
1229                        ident_split.name_end - ident_split.name_begin);
1230                state->author_name = strbuf_detach(&sb, NULL);
1231        } else
1232                state->author_name = xstrdup("");
1233
1234        assert(!state->author_email);
1235        if (ident_split.mail_begin) {
1236                strbuf_add(&sb, ident_split.mail_begin,
1237                        ident_split.mail_end - ident_split.mail_begin);
1238                state->author_email = strbuf_detach(&sb, NULL);
1239        } else
1240                state->author_email = xstrdup("");
1241
1242        author_date = show_ident_date(&ident_split, DATE_MODE(NORMAL));
1243        strbuf_addstr(&sb, author_date);
1244        assert(!state->author_date);
1245        state->author_date = strbuf_detach(&sb, NULL);
1246
1247        assert(!state->msg);
1248        msg = strstr(buffer, "\n\n");
1249        if (!msg)
1250                die(_("unable to parse commit %s"), sha1_to_hex(commit->object.sha1));
1251        state->msg = xstrdup(msg + 2);
1252        state->msg_len = strlen(state->msg);
1253}
1254
1255/**
1256 * Writes `commit` as a patch to the state directory's "patch" file.
1257 */
1258static void write_commit_patch(const struct am_state *state, struct commit *commit)
1259{
1260        struct rev_info rev_info;
1261        FILE *fp;
1262
1263        fp = xfopen(am_path(state, "patch"), "w");
1264        init_revisions(&rev_info, NULL);
1265        rev_info.diff = 1;
1266        rev_info.abbrev = 0;
1267        rev_info.disable_stdin = 1;
1268        rev_info.show_root_diff = 1;
1269        rev_info.diffopt.output_format = DIFF_FORMAT_PATCH;
1270        rev_info.no_commit_id = 1;
1271        DIFF_OPT_SET(&rev_info.diffopt, BINARY);
1272        DIFF_OPT_SET(&rev_info.diffopt, FULL_INDEX);
1273        rev_info.diffopt.use_color = 0;
1274        rev_info.diffopt.file = fp;
1275        rev_info.diffopt.close_file = 1;
1276        add_pending_object(&rev_info, &commit->object, "");
1277        diff_setup_done(&rev_info.diffopt);
1278        log_tree_commit(&rev_info, commit);
1279}
1280
1281/**
1282 * Like parse_mail(), but parses the mail by looking up its commit ID
1283 * directly. This is used in --rebasing mode to bypass git-mailinfo's munging
1284 * of patches.
1285 *
1286 * state->orig_commit will be set to the original commit ID.
1287 *
1288 * Will always return 0 as the patch should never be skipped.
1289 */
1290static int parse_mail_rebase(struct am_state *state, const char *mail)
1291{
1292        struct commit *commit;
1293        unsigned char commit_sha1[GIT_SHA1_RAWSZ];
1294
1295        if (get_mail_commit_sha1(commit_sha1, mail) < 0)
1296                die(_("could not parse %s"), mail);
1297
1298        commit = lookup_commit_or_die(commit_sha1, mail);
1299
1300        get_commit_info(state, commit);
1301
1302        write_commit_patch(state, commit);
1303
1304        hashcpy(state->orig_commit, commit_sha1);
1305        write_file(am_path(state, "original-commit"), 1, "%s",
1306                        sha1_to_hex(commit_sha1));
1307
1308        return 0;
1309}
1310
1311/**
1312 * Applies current patch with git-apply. Returns 0 on success, -1 otherwise. If
1313 * `index_file` is not NULL, the patch will be applied to that index.
1314 */
1315static int run_apply(const struct am_state *state, const char *index_file)
1316{
1317        struct child_process cp = CHILD_PROCESS_INIT;
1318
1319        cp.git_cmd = 1;
1320
1321        if (index_file)
1322                argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
1323
1324        /*
1325         * If we are allowed to fall back on 3-way merge, don't give false
1326         * errors during the initial attempt.
1327         */
1328        if (state->threeway && !index_file) {
1329                cp.no_stdout = 1;
1330                cp.no_stderr = 1;
1331        }
1332
1333        argv_array_push(&cp.args, "apply");
1334
1335        argv_array_pushv(&cp.args, state->git_apply_opts.argv);
1336
1337        if (index_file)
1338                argv_array_push(&cp.args, "--cached");
1339        else
1340                argv_array_push(&cp.args, "--index");
1341
1342        argv_array_push(&cp.args, am_path(state, "patch"));
1343
1344        if (run_command(&cp))
1345                return -1;
1346
1347        /* Reload index as git-apply will have modified it. */
1348        discard_cache();
1349        read_cache_from(index_file ? index_file : get_index_file());
1350
1351        return 0;
1352}
1353
1354/**
1355 * Builds an index that contains just the blobs needed for a 3way merge.
1356 */
1357static int build_fake_ancestor(const struct am_state *state, const char *index_file)
1358{
1359        struct child_process cp = CHILD_PROCESS_INIT;
1360
1361        cp.git_cmd = 1;
1362        argv_array_push(&cp.args, "apply");
1363        argv_array_pushv(&cp.args, state->git_apply_opts.argv);
1364        argv_array_pushf(&cp.args, "--build-fake-ancestor=%s", index_file);
1365        argv_array_push(&cp.args, am_path(state, "patch"));
1366
1367        if (run_command(&cp))
1368                return -1;
1369
1370        return 0;
1371}
1372
1373/**
1374 * Attempt a threeway merge, using index_path as the temporary index.
1375 */
1376static int fall_back_threeway(const struct am_state *state, const char *index_path)
1377{
1378        unsigned char orig_tree[GIT_SHA1_RAWSZ], his_tree[GIT_SHA1_RAWSZ],
1379                      our_tree[GIT_SHA1_RAWSZ];
1380        const unsigned char *bases[1] = {orig_tree};
1381        struct merge_options o;
1382        struct commit *result;
1383        char *his_tree_name;
1384
1385        if (get_sha1("HEAD", our_tree) < 0)
1386                hashcpy(our_tree, EMPTY_TREE_SHA1_BIN);
1387
1388        if (build_fake_ancestor(state, index_path))
1389                return error("could not build fake ancestor");
1390
1391        discard_cache();
1392        read_cache_from(index_path);
1393
1394        if (write_index_as_tree(orig_tree, &the_index, index_path, 0, NULL))
1395                return error(_("Repository lacks necessary blobs to fall back on 3-way merge."));
1396
1397        say(state, stdout, _("Using index info to reconstruct a base tree..."));
1398
1399        if (!state->quiet) {
1400                /*
1401                 * List paths that needed 3-way fallback, so that the user can
1402                 * review them with extra care to spot mismerges.
1403                 */
1404                struct rev_info rev_info;
1405                const char *diff_filter_str = "--diff-filter=AM";
1406
1407                init_revisions(&rev_info, NULL);
1408                rev_info.diffopt.output_format = DIFF_FORMAT_NAME_STATUS;
1409                diff_opt_parse(&rev_info.diffopt, &diff_filter_str, 1);
1410                add_pending_sha1(&rev_info, "HEAD", our_tree, 0);
1411                diff_setup_done(&rev_info.diffopt);
1412                run_diff_index(&rev_info, 1);
1413        }
1414
1415        if (run_apply(state, index_path))
1416                return error(_("Did you hand edit your patch?\n"
1417                                "It does not apply to blobs recorded in its index."));
1418
1419        if (write_index_as_tree(his_tree, &the_index, index_path, 0, NULL))
1420                return error("could not write tree");
1421
1422        say(state, stdout, _("Falling back to patching base and 3-way merge..."));
1423
1424        discard_cache();
1425        read_cache();
1426
1427        /*
1428         * This is not so wrong. Depending on which base we picked, orig_tree
1429         * may be wildly different from ours, but his_tree has the same set of
1430         * wildly different changes in parts the patch did not touch, so
1431         * recursive ends up canceling them, saying that we reverted all those
1432         * changes.
1433         */
1434
1435        init_merge_options(&o);
1436
1437        o.branch1 = "HEAD";
1438        his_tree_name = xstrfmt("%.*s", linelen(state->msg), state->msg);
1439        o.branch2 = his_tree_name;
1440
1441        if (state->quiet)
1442                o.verbosity = 0;
1443
1444        if (merge_recursive_generic(&o, our_tree, his_tree, 1, bases, &result)) {
1445                rerere(state->allow_rerere_autoupdate);
1446                free(his_tree_name);
1447                return error(_("Failed to merge in the changes."));
1448        }
1449
1450        free(his_tree_name);
1451        return 0;
1452}
1453
1454/**
1455 * Commits the current index with state->msg as the commit message and
1456 * state->author_name, state->author_email and state->author_date as the author
1457 * information.
1458 */
1459static void do_commit(const struct am_state *state)
1460{
1461        unsigned char tree[GIT_SHA1_RAWSZ], parent[GIT_SHA1_RAWSZ],
1462                      commit[GIT_SHA1_RAWSZ];
1463        unsigned char *ptr;
1464        struct commit_list *parents = NULL;
1465        const char *reflog_msg, *author;
1466        struct strbuf sb = STRBUF_INIT;
1467
1468        if (run_hook_le(NULL, "pre-applypatch", NULL))
1469                exit(1);
1470
1471        if (write_cache_as_tree(tree, 0, NULL))
1472                die(_("git write-tree failed to write a tree"));
1473
1474        if (!get_sha1_commit("HEAD", parent)) {
1475                ptr = parent;
1476                commit_list_insert(lookup_commit(parent), &parents);
1477        } else {
1478                ptr = NULL;
1479                say(state, stderr, _("applying to an empty history"));
1480        }
1481
1482        author = fmt_ident(state->author_name, state->author_email,
1483                        state->ignore_date ? NULL : state->author_date,
1484                        IDENT_STRICT);
1485
1486        if (state->committer_date_is_author_date)
1487                setenv("GIT_COMMITTER_DATE",
1488                        state->ignore_date ? "" : state->author_date, 1);
1489
1490        if (commit_tree(state->msg, state->msg_len, tree, parents, commit,
1491                                author, state->sign_commit))
1492                die(_("failed to write commit object"));
1493
1494        reflog_msg = getenv("GIT_REFLOG_ACTION");
1495        if (!reflog_msg)
1496                reflog_msg = "am";
1497
1498        strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg),
1499                        state->msg);
1500
1501        update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR);
1502
1503        if (state->rebasing) {
1504                FILE *fp = xfopen(am_path(state, "rewritten"), "a");
1505
1506                assert(!is_null_sha1(state->orig_commit));
1507                fprintf(fp, "%s ", sha1_to_hex(state->orig_commit));
1508                fprintf(fp, "%s\n", sha1_to_hex(commit));
1509                fclose(fp);
1510        }
1511
1512        run_hook_le(NULL, "post-applypatch", NULL);
1513
1514        strbuf_release(&sb);
1515}
1516
1517/**
1518 * Validates the am_state for resuming -- the "msg" and authorship fields must
1519 * be filled up.
1520 */
1521static void validate_resume_state(const struct am_state *state)
1522{
1523        if (!state->msg)
1524                die(_("cannot resume: %s does not exist."),
1525                        am_path(state, "final-commit"));
1526
1527        if (!state->author_name || !state->author_email || !state->author_date)
1528                die(_("cannot resume: %s does not exist."),
1529                        am_path(state, "author-script"));
1530}
1531
1532/**
1533 * Applies all queued mail.
1534 *
1535 * If `resume` is true, we are "resuming". The "msg" and authorship fields, as
1536 * well as the state directory's "patch" file is used as-is for applying the
1537 * patch and committing it.
1538 */
1539static void am_run(struct am_state *state, int resume)
1540{
1541        const char *argv_gc_auto[] = {"gc", "--auto", NULL};
1542        struct strbuf sb = STRBUF_INIT;
1543
1544        unlink(am_path(state, "dirtyindex"));
1545
1546        refresh_and_write_cache();
1547
1548        if (index_has_changes(&sb)) {
1549                write_file(am_path(state, "dirtyindex"), 1, "t");
1550                die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
1551        }
1552
1553        strbuf_release(&sb);
1554
1555        while (state->cur <= state->last) {
1556                const char *mail = am_path(state, msgnum(state));
1557                int apply_status;
1558
1559                if (!file_exists(mail))
1560                        goto next;
1561
1562                if (resume) {
1563                        validate_resume_state(state);
1564                        resume = 0;
1565                } else {
1566                        int skip;
1567
1568                        if (state->rebasing)
1569                                skip = parse_mail_rebase(state, mail);
1570                        else
1571                                skip = parse_mail(state, mail);
1572
1573                        if (skip)
1574                                goto next; /* mail should be skipped */
1575
1576                        write_author_script(state);
1577                        write_commit_msg(state);
1578                }
1579
1580                if (run_applypatch_msg_hook(state))
1581                        exit(1);
1582
1583                say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1584
1585                apply_status = run_apply(state, NULL);
1586
1587                if (apply_status && state->threeway) {
1588                        struct strbuf sb = STRBUF_INIT;
1589
1590                        strbuf_addstr(&sb, am_path(state, "patch-merge-index"));
1591                        apply_status = fall_back_threeway(state, sb.buf);
1592                        strbuf_release(&sb);
1593
1594                        /*
1595                         * Applying the patch to an earlier tree and merging
1596                         * the result may have produced the same tree as ours.
1597                         */
1598                        if (!apply_status && !index_has_changes(NULL)) {
1599                                say(state, stdout, _("No changes -- Patch already applied."));
1600                                goto next;
1601                        }
1602                }
1603
1604                if (apply_status) {
1605                        int advice_amworkdir = 1;
1606
1607                        printf_ln(_("Patch failed at %s %.*s"), msgnum(state),
1608                                linelen(state->msg), state->msg);
1609
1610                        git_config_get_bool("advice.amworkdir", &advice_amworkdir);
1611
1612                        if (advice_amworkdir)
1613                                printf_ln(_("The copy of the patch that failed is found in: %s"),
1614                                                am_path(state, "patch"));
1615
1616                        die_user_resolve(state);
1617                }
1618
1619                do_commit(state);
1620
1621next:
1622                am_next(state);
1623        }
1624
1625        if (!is_empty_file(am_path(state, "rewritten"))) {
1626                assert(state->rebasing);
1627                copy_notes_for_rebase(state);
1628                run_post_rewrite_hook(state);
1629        }
1630
1631        /*
1632         * In rebasing mode, it's up to the caller to take care of
1633         * housekeeping.
1634         */
1635        if (!state->rebasing) {
1636                am_destroy(state);
1637                run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1638        }
1639}
1640
1641/**
1642 * Resume the current am session after patch application failure. The user did
1643 * all the hard work, and we do not have to do any patch application. Just
1644 * trust and commit what the user has in the index and working tree.
1645 */
1646static void am_resolve(struct am_state *state)
1647{
1648        validate_resume_state(state);
1649
1650        say(state, stdout, _("Applying: %.*s"), linelen(state->msg), state->msg);
1651
1652        if (!index_has_changes(NULL)) {
1653                printf_ln(_("No changes - did you forget to use 'git add'?\n"
1654                        "If there is nothing left to stage, chances are that something else\n"
1655                        "already introduced the same changes; you might want to skip this patch."));
1656                die_user_resolve(state);
1657        }
1658
1659        if (unmerged_cache()) {
1660                printf_ln(_("You still have unmerged paths in your index.\n"
1661                        "Did you forget to use 'git add'?"));
1662                die_user_resolve(state);
1663        }
1664
1665        rerere(0);
1666
1667        do_commit(state);
1668
1669        am_next(state);
1670        am_run(state, 0);
1671}
1672
1673/**
1674 * Performs a checkout fast-forward from `head` to `remote`. If `reset` is
1675 * true, any unmerged entries will be discarded. Returns 0 on success, -1 on
1676 * failure.
1677 */
1678static int fast_forward_to(struct tree *head, struct tree *remote, int reset)
1679{
1680        struct lock_file *lock_file;
1681        struct unpack_trees_options opts;
1682        struct tree_desc t[2];
1683
1684        if (parse_tree(head) || parse_tree(remote))
1685                return -1;
1686
1687        lock_file = xcalloc(1, sizeof(struct lock_file));
1688        hold_locked_index(lock_file, 1);
1689
1690        refresh_cache(REFRESH_QUIET);
1691
1692        memset(&opts, 0, sizeof(opts));
1693        opts.head_idx = 1;
1694        opts.src_index = &the_index;
1695        opts.dst_index = &the_index;
1696        opts.update = 1;
1697        opts.merge = 1;
1698        opts.reset = reset;
1699        opts.fn = twoway_merge;
1700        init_tree_desc(&t[0], head->buffer, head->size);
1701        init_tree_desc(&t[1], remote->buffer, remote->size);
1702
1703        if (unpack_trees(2, t, &opts)) {
1704                rollback_lock_file(lock_file);
1705                return -1;
1706        }
1707
1708        if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1709                die(_("unable to write new index file"));
1710
1711        return 0;
1712}
1713
1714/**
1715 * Clean the index without touching entries that are not modified between
1716 * `head` and `remote`.
1717 */
1718static int clean_index(const unsigned char *head, const unsigned char *remote)
1719{
1720        struct lock_file *lock_file;
1721        struct tree *head_tree, *remote_tree, *index_tree;
1722        unsigned char index[GIT_SHA1_RAWSZ];
1723        struct pathspec pathspec;
1724
1725        head_tree = parse_tree_indirect(head);
1726        if (!head_tree)
1727                return error(_("Could not parse object '%s'."), sha1_to_hex(head));
1728
1729        remote_tree = parse_tree_indirect(remote);
1730        if (!remote_tree)
1731                return error(_("Could not parse object '%s'."), sha1_to_hex(remote));
1732
1733        read_cache_unmerged();
1734
1735        if (fast_forward_to(head_tree, head_tree, 1))
1736                return -1;
1737
1738        if (write_cache_as_tree(index, 0, NULL))
1739                return -1;
1740
1741        index_tree = parse_tree_indirect(index);
1742        if (!index_tree)
1743                return error(_("Could not parse object '%s'."), sha1_to_hex(index));
1744
1745        if (fast_forward_to(index_tree, remote_tree, 0))
1746                return -1;
1747
1748        memset(&pathspec, 0, sizeof(pathspec));
1749
1750        lock_file = xcalloc(1, sizeof(struct lock_file));
1751        hold_locked_index(lock_file, 1);
1752
1753        if (read_tree(remote_tree, 0, &pathspec)) {
1754                rollback_lock_file(lock_file);
1755                return -1;
1756        }
1757
1758        if (write_locked_index(&the_index, lock_file, COMMIT_LOCK))
1759                die(_("unable to write new index file"));
1760
1761        remove_branch_state();
1762
1763        return 0;
1764}
1765
1766/**
1767 * Resets rerere's merge resolution metadata.
1768 */
1769static void am_rerere_clear(void)
1770{
1771        struct string_list merge_rr = STRING_LIST_INIT_DUP;
1772        int fd = setup_rerere(&merge_rr, 0);
1773
1774        if (fd < 0)
1775                return;
1776
1777        rerere_clear(&merge_rr);
1778        string_list_clear(&merge_rr, 1);
1779}
1780
1781/**
1782 * Resume the current am session by skipping the current patch.
1783 */
1784static void am_skip(struct am_state *state)
1785{
1786        unsigned char head[GIT_SHA1_RAWSZ];
1787
1788        am_rerere_clear();
1789
1790        if (get_sha1("HEAD", head))
1791                hashcpy(head, EMPTY_TREE_SHA1_BIN);
1792
1793        if (clean_index(head, head))
1794                die(_("failed to clean index"));
1795
1796        am_next(state);
1797        am_run(state, 0);
1798}
1799
1800/**
1801 * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
1802 *
1803 * It is not safe to reset HEAD when:
1804 * 1. git-am previously failed because the index was dirty.
1805 * 2. HEAD has moved since git-am previously failed.
1806 */
1807static int safe_to_abort(const struct am_state *state)
1808{
1809        struct strbuf sb = STRBUF_INIT;
1810        unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
1811
1812        if (file_exists(am_path(state, "dirtyindex")))
1813                return 0;
1814
1815        if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
1816                if (get_sha1_hex(sb.buf, abort_safety))
1817                        die(_("could not parse %s"), am_path(state, "abort_safety"));
1818        } else
1819                hashclr(abort_safety);
1820
1821        if (get_sha1("HEAD", head))
1822                hashclr(head);
1823
1824        if (!hashcmp(head, abort_safety))
1825                return 1;
1826
1827        error(_("You seem to have moved HEAD since the last 'am' failure.\n"
1828                "Not rewinding to ORIG_HEAD"));
1829
1830        return 0;
1831}
1832
1833/**
1834 * Aborts the current am session if it is safe to do so.
1835 */
1836static void am_abort(struct am_state *state)
1837{
1838        unsigned char curr_head[GIT_SHA1_RAWSZ], orig_head[GIT_SHA1_RAWSZ];
1839        int has_curr_head, has_orig_head;
1840        char *curr_branch;
1841
1842        if (!safe_to_abort(state)) {
1843                am_destroy(state);
1844                return;
1845        }
1846
1847        am_rerere_clear();
1848
1849        curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
1850        has_curr_head = !is_null_sha1(curr_head);
1851        if (!has_curr_head)
1852                hashcpy(curr_head, EMPTY_TREE_SHA1_BIN);
1853
1854        has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
1855        if (!has_orig_head)
1856                hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
1857
1858        clean_index(curr_head, orig_head);
1859
1860        if (has_orig_head)
1861                update_ref("am --abort", "HEAD", orig_head,
1862                                has_curr_head ? curr_head : NULL, 0,
1863                                UPDATE_REFS_DIE_ON_ERR);
1864        else if (curr_branch)
1865                delete_ref(curr_branch, NULL, REF_NODEREF);
1866
1867        free(curr_branch);
1868        am_destroy(state);
1869}
1870
1871/**
1872 * parse_options() callback that validates and sets opt->value to the
1873 * PATCH_FORMAT_* enum value corresponding to `arg`.
1874 */
1875static int parse_opt_patchformat(const struct option *opt, const char *arg, int unset)
1876{
1877        int *opt_value = opt->value;
1878
1879        if (!strcmp(arg, "mbox"))
1880                *opt_value = PATCH_FORMAT_MBOX;
1881        else if (!strcmp(arg, "stgit"))
1882                *opt_value = PATCH_FORMAT_STGIT;
1883        else
1884                return error(_("Invalid value for --patch-format: %s"), arg);
1885        return 0;
1886}
1887
1888enum resume_mode {
1889        RESUME_FALSE = 0,
1890        RESUME_APPLY,
1891        RESUME_RESOLVED,
1892        RESUME_SKIP,
1893        RESUME_ABORT
1894};
1895
1896int cmd_am(int argc, const char **argv, const char *prefix)
1897{
1898        struct am_state state;
1899        int keep_cr = -1;
1900        int patch_format = PATCH_FORMAT_UNKNOWN;
1901        enum resume_mode resume = RESUME_FALSE;
1902
1903        const char * const usage[] = {
1904                N_("git am [options] [(<mbox>|<Maildir>)...]"),
1905                N_("git am [options] (--continue | --skip | --abort)"),
1906                NULL
1907        };
1908
1909        struct option options[] = {
1910                OPT_BOOL('3', "3way", &state.threeway,
1911                        N_("allow fall back on 3way merging if needed")),
1912                OPT__QUIET(&state.quiet, N_("be quiet")),
1913                OPT_BOOL('s', "signoff", &state.signoff,
1914                        N_("add a Signed-off-by line to the commit message")),
1915                OPT_BOOL('u', "utf8", &state.utf8,
1916                        N_("recode into utf8 (default)")),
1917                OPT_SET_INT('k', "keep", &state.keep,
1918                        N_("pass -k flag to git-mailinfo"), KEEP_TRUE),
1919                OPT_SET_INT(0, "keep-non-patch", &state.keep,
1920                        N_("pass -b flag to git-mailinfo"), KEEP_NON_PATCH),
1921                OPT_BOOL('m', "message-id", &state.message_id,
1922                        N_("pass -m flag to git-mailinfo")),
1923                { OPTION_SET_INT, 0, "keep-cr", &keep_cr, NULL,
1924                  N_("pass --keep-cr flag to git-mailsplit for mbox format"),
1925                  PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1},
1926                { OPTION_SET_INT, 0, "no-keep-cr", &keep_cr, NULL,
1927                  N_("do not pass --keep-cr flag to git-mailsplit independent of am.keepcr"),
1928                  PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 0},
1929                OPT_BOOL('c', "scissors", &state.scissors,
1930                        N_("strip everything before a scissors line")),
1931                OPT_PASSTHRU_ARGV(0, "whitespace", &state.git_apply_opts, N_("action"),
1932                        N_("pass it through git-apply"),
1933                        0),
1934                OPT_PASSTHRU_ARGV(0, "ignore-space-change", &state.git_apply_opts, NULL,
1935                        N_("pass it through git-apply"),
1936                        PARSE_OPT_NOARG),
1937                OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &state.git_apply_opts, NULL,
1938                        N_("pass it through git-apply"),
1939                        PARSE_OPT_NOARG),
1940                OPT_PASSTHRU_ARGV(0, "directory", &state.git_apply_opts, N_("root"),
1941                        N_("pass it through git-apply"),
1942                        0),
1943                OPT_PASSTHRU_ARGV(0, "exclude", &state.git_apply_opts, N_("path"),
1944                        N_("pass it through git-apply"),
1945                        0),
1946                OPT_PASSTHRU_ARGV(0, "include", &state.git_apply_opts, N_("path"),
1947                        N_("pass it through git-apply"),
1948                        0),
1949                OPT_PASSTHRU_ARGV('C', NULL, &state.git_apply_opts, N_("n"),
1950                        N_("pass it through git-apply"),
1951                        0),
1952                OPT_PASSTHRU_ARGV('p', NULL, &state.git_apply_opts, N_("num"),
1953                        N_("pass it through git-apply"),
1954                        0),
1955                OPT_CALLBACK(0, "patch-format", &patch_format, N_("format"),
1956                        N_("format the patch(es) are in"),
1957                        parse_opt_patchformat),
1958                OPT_PASSTHRU_ARGV(0, "reject", &state.git_apply_opts, NULL,
1959                        N_("pass it through git-apply"),
1960                        PARSE_OPT_NOARG),
1961                OPT_STRING(0, "resolvemsg", &state.resolvemsg, NULL,
1962                        N_("override error message when patch failure occurs")),
1963                OPT_CMDMODE(0, "continue", &resume,
1964                        N_("continue applying patches after resolving a conflict"),
1965                        RESUME_RESOLVED),
1966                OPT_CMDMODE('r', "resolved", &resume,
1967                        N_("synonyms for --continue"),
1968                        RESUME_RESOLVED),
1969                OPT_CMDMODE(0, "skip", &resume,
1970                        N_("skip the current patch"),
1971                        RESUME_SKIP),
1972                OPT_CMDMODE(0, "abort", &resume,
1973                        N_("restore the original branch and abort the patching operation."),
1974                        RESUME_ABORT),
1975                OPT_BOOL(0, "committer-date-is-author-date",
1976                        &state.committer_date_is_author_date,
1977                        N_("lie about committer date")),
1978                OPT_BOOL(0, "ignore-date", &state.ignore_date,
1979                        N_("use current timestamp for author date")),
1980                OPT_RERERE_AUTOUPDATE(&state.allow_rerere_autoupdate),
1981                { OPTION_STRING, 'S', "gpg-sign", &state.sign_commit, N_("key-id"),
1982                  N_("GPG-sign commits"),
1983                  PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1984                OPT_HIDDEN_BOOL(0, "rebasing", &state.rebasing,
1985                        N_("(internal use for git-rebase)")),
1986                OPT_END()
1987        };
1988
1989        /*
1990         * NEEDSWORK: Once all the features of git-am.sh have been
1991         * re-implemented in builtin/am.c, this preamble can be removed.
1992         */
1993        if (!getenv("_GIT_USE_BUILTIN_AM")) {
1994                const char *path = mkpath("%s/git-am", git_exec_path());
1995
1996                if (sane_execvp(path, (char **)argv) < 0)
1997                        die_errno("could not exec %s", path);
1998        } else {
1999                prefix = setup_git_directory();
2000                trace_repo_setup(prefix);
2001                setup_work_tree();
2002        }
2003
2004        git_config(git_default_config, NULL);
2005
2006        am_state_init(&state, git_path("rebase-apply"));
2007
2008        argc = parse_options(argc, argv, prefix, options, usage, 0);
2009
2010        if (read_index_preload(&the_index, NULL) < 0)
2011                die(_("failed to read the index"));
2012
2013        if (am_in_progress(&state)) {
2014                /*
2015                 * Catch user error to feed us patches when there is a session
2016                 * in progress:
2017                 *
2018                 * 1. mbox path(s) are provided on the command-line.
2019                 * 2. stdin is not a tty: the user is trying to feed us a patch
2020                 *    from standard input. This is somewhat unreliable -- stdin
2021                 *    could be /dev/null for example and the caller did not
2022                 *    intend to feed us a patch but wanted to continue
2023                 *    unattended.
2024                 */
2025                if (argc || (resume == RESUME_FALSE && !isatty(0)))
2026                        die(_("previous rebase directory %s still exists but mbox given."),
2027                                state.dir);
2028
2029                if (resume == RESUME_FALSE)
2030                        resume = RESUME_APPLY;
2031
2032                am_load(&state);
2033        } else {
2034                struct argv_array paths = ARGV_ARRAY_INIT;
2035                int i;
2036
2037                /*
2038                 * Handle stray state directory in the independent-run case. In
2039                 * the --rebasing case, it is up to the caller to take care of
2040                 * stray directories.
2041                 */
2042                if (file_exists(state.dir) && !state.rebasing) {
2043                        if (resume == RESUME_ABORT) {
2044                                am_destroy(&state);
2045                                am_state_release(&state);
2046                                return 0;
2047                        }
2048
2049                        die(_("Stray %s directory found.\n"
2050                                "Use \"git am --abort\" to remove it."),
2051                                state.dir);
2052                }
2053
2054                if (resume)
2055                        die(_("Resolve operation not in progress, we are not resuming."));
2056
2057                for (i = 0; i < argc; i++) {
2058                        if (is_absolute_path(argv[i]) || !prefix)
2059                                argv_array_push(&paths, argv[i]);
2060                        else
2061                                argv_array_push(&paths, mkpath("%s/%s", prefix, argv[i]));
2062                }
2063
2064                am_setup(&state, patch_format, paths.argv, keep_cr);
2065
2066                argv_array_clear(&paths);
2067        }
2068
2069        switch (resume) {
2070        case RESUME_FALSE:
2071                am_run(&state, 0);
2072                break;
2073        case RESUME_APPLY:
2074                am_run(&state, 1);
2075                break;
2076        case RESUME_RESOLVED:
2077                am_resolve(&state);
2078                break;
2079        case RESUME_SKIP:
2080                am_skip(&state);
2081                break;
2082        case RESUME_ABORT:
2083                am_abort(&state);
2084                break;
2085        default:
2086                die("BUG: invalid resume value");
2087        }
2088
2089        am_state_release(&state);
2090
2091        return 0;
2092}