85cbeef0f4cebd4b7f737ba29fbc7602499f6239
   1/*
   2 * Builtin "git commit"
   3 *
   4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
   5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
   6 */
   7
   8#include "cache.h"
   9#include "cache-tree.h"
  10#include "color.h"
  11#include "dir.h"
  12#include "builtin.h"
  13#include "diff.h"
  14#include "diffcore.h"
  15#include "commit.h"
  16#include "revision.h"
  17#include "wt-status.h"
  18#include "run-command.h"
  19#include "refs.h"
  20#include "log-tree.h"
  21#include "strbuf.h"
  22#include "utf8.h"
  23#include "parse-options.h"
  24#include "string-list.h"
  25#include "rerere.h"
  26#include "unpack-trees.h"
  27#include "quote.h"
  28#include "submodule.h"
  29#include "gpg-interface.h"
  30
  31static const char * const builtin_commit_usage[] = {
  32        "git commit [options] [--] <filepattern>...",
  33        NULL
  34};
  35
  36static const char * const builtin_status_usage[] = {
  37        "git status [options] [--] <filepattern>...",
  38        NULL
  39};
  40
  41static const char implicit_ident_advice[] =
  42N_("Your name and email address were configured automatically based\n"
  43"on your username and hostname. Please check that they are accurate.\n"
  44"You can suppress this message by setting them explicitly:\n"
  45"\n"
  46"    git config --global user.name \"Your Name\"\n"
  47"    git config --global user.email you@example.com\n"
  48"\n"
  49"After doing this, you may fix the identity used for this commit with:\n"
  50"\n"
  51"    git commit --amend --reset-author\n");
  52
  53static const char empty_amend_advice[] =
  54N_("You asked to amend the most recent commit, but doing so would make\n"
  55"it empty. You can repeat your command with --allow-empty, or you can\n"
  56"remove the commit entirely with \"git reset HEAD^\".\n");
  57
  58static const char empty_cherry_pick_advice[] =
  59N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
  60"If you wish to commit it anyway, use:\n"
  61"\n"
  62"    git commit --allow-empty\n"
  63"\n"
  64"Otherwise, please use 'git reset'\n");
  65
  66static const char *use_message_buffer;
  67static const char commit_editmsg[] = "COMMIT_EDITMSG";
  68static struct lock_file index_lock; /* real index */
  69static struct lock_file false_lock; /* used only for partial commits */
  70static enum {
  71        COMMIT_AS_IS = 1,
  72        COMMIT_NORMAL,
  73        COMMIT_PARTIAL
  74} commit_style;
  75
  76static const char *logfile, *force_author;
  77static const char *template_file;
  78/*
  79 * The _message variables are commit names from which to take
  80 * the commit message and/or authorship.
  81 */
  82static const char *author_message, *author_message_buffer;
  83static char *edit_message, *use_message;
  84static char *fixup_message, *squash_message;
  85static int all, also, interactive, patch_interactive, only, amend, signoff;
  86static int edit_flag = -1; /* unspecified */
  87static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
  88static int no_post_rewrite, allow_empty_message;
  89static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
  90static char *sign_commit;
  91
  92/*
  93 * The default commit message cleanup mode will remove the lines
  94 * beginning with # (shell comments) and leading and trailing
  95 * whitespaces (empty lines or containing only whitespaces)
  96 * if editor is used, and only the whitespaces if the message
  97 * is specified explicitly.
  98 */
  99static enum {
 100        CLEANUP_SPACE,
 101        CLEANUP_NONE,
 102        CLEANUP_ALL
 103} cleanup_mode;
 104static char *cleanup_arg;
 105
 106static enum commit_whence whence;
 107static int use_editor = 1, include_status = 1;
 108static int show_ignored_in_status;
 109static const char *only_include_assumed;
 110static struct strbuf message = STRBUF_INIT;
 111
 112static enum {
 113        STATUS_FORMAT_LONG,
 114        STATUS_FORMAT_SHORT,
 115        STATUS_FORMAT_PORCELAIN
 116} status_format = STATUS_FORMAT_LONG;
 117static int status_show_branch;
 118
 119static int opt_parse_m(const struct option *opt, const char *arg, int unset)
 120{
 121        struct strbuf *buf = opt->value;
 122        if (unset)
 123                strbuf_setlen(buf, 0);
 124        else {
 125                strbuf_addstr(buf, arg);
 126                strbuf_addstr(buf, "\n\n");
 127        }
 128        return 0;
 129}
 130
 131static void determine_whence(struct wt_status *s)
 132{
 133        if (file_exists(git_path("MERGE_HEAD")))
 134                whence = FROM_MERGE;
 135        else if (file_exists(git_path("CHERRY_PICK_HEAD")))
 136                whence = FROM_CHERRY_PICK;
 137        else
 138                whence = FROM_COMMIT;
 139        if (s)
 140                s->whence = whence;
 141}
 142
 143static const char *whence_s(void)
 144{
 145        const char *s = "";
 146
 147        switch (whence) {
 148        case FROM_COMMIT:
 149                break;
 150        case FROM_MERGE:
 151                s = _("merge");
 152                break;
 153        case FROM_CHERRY_PICK:
 154                s = _("cherry-pick");
 155                break;
 156        }
 157
 158        return s;
 159}
 160
 161static void rollback_index_files(void)
 162{
 163        switch (commit_style) {
 164        case COMMIT_AS_IS:
 165                break; /* nothing to do */
 166        case COMMIT_NORMAL:
 167                rollback_lock_file(&index_lock);
 168                break;
 169        case COMMIT_PARTIAL:
 170                rollback_lock_file(&index_lock);
 171                rollback_lock_file(&false_lock);
 172                break;
 173        }
 174}
 175
 176static int commit_index_files(void)
 177{
 178        int err = 0;
 179
 180        switch (commit_style) {
 181        case COMMIT_AS_IS:
 182                break; /* nothing to do */
 183        case COMMIT_NORMAL:
 184                err = commit_lock_file(&index_lock);
 185                break;
 186        case COMMIT_PARTIAL:
 187                err = commit_lock_file(&index_lock);
 188                rollback_lock_file(&false_lock);
 189                break;
 190        }
 191
 192        return err;
 193}
 194
 195/*
 196 * Take a union of paths in the index and the named tree (typically, "HEAD"),
 197 * and return the paths that match the given pattern in list.
 198 */
 199static int list_paths(struct string_list *list, const char *with_tree,
 200                      const char *prefix, const char **pattern)
 201{
 202        int i;
 203        char *m;
 204
 205        for (i = 0; pattern[i]; i++)
 206                ;
 207        m = xcalloc(1, i);
 208
 209        if (with_tree) {
 210                char *max_prefix = common_prefix(pattern);
 211                overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
 212                free(max_prefix);
 213        }
 214
 215        for (i = 0; i < active_nr; i++) {
 216                struct cache_entry *ce = active_cache[i];
 217                struct string_list_item *item;
 218
 219                if (ce->ce_flags & CE_UPDATE)
 220                        continue;
 221                if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
 222                        continue;
 223                item = string_list_insert(list, ce->name);
 224                if (ce_skip_worktree(ce))
 225                        item->util = item; /* better a valid pointer than a fake one */
 226        }
 227
 228        return report_path_error(m, pattern, prefix);
 229}
 230
 231static void add_remove_files(struct string_list *list)
 232{
 233        int i;
 234        for (i = 0; i < list->nr; i++) {
 235                struct stat st;
 236                struct string_list_item *p = &(list->items[i]);
 237
 238                /* p->util is skip-worktree */
 239                if (p->util)
 240                        continue;
 241
 242                if (!lstat(p->string, &st)) {
 243                        if (add_to_cache(p->string, &st, 0))
 244                                die(_("updating files failed"));
 245                } else
 246                        remove_file_from_cache(p->string);
 247        }
 248}
 249
 250static void create_base_index(const struct commit *current_head)
 251{
 252        struct tree *tree;
 253        struct unpack_trees_options opts;
 254        struct tree_desc t;
 255
 256        if (!current_head) {
 257                discard_cache();
 258                return;
 259        }
 260
 261        memset(&opts, 0, sizeof(opts));
 262        opts.head_idx = 1;
 263        opts.index_only = 1;
 264        opts.merge = 1;
 265        opts.src_index = &the_index;
 266        opts.dst_index = &the_index;
 267
 268        opts.fn = oneway_merge;
 269        tree = parse_tree_indirect(current_head->object.sha1);
 270        if (!tree)
 271                die(_("failed to unpack HEAD tree object"));
 272        parse_tree(tree);
 273        init_tree_desc(&t, tree->buffer, tree->size);
 274        if (unpack_trees(1, &t, &opts))
 275                exit(128); /* We've already reported the error, finish dying */
 276}
 277
 278static void refresh_cache_or_die(int refresh_flags)
 279{
 280        /*
 281         * refresh_flags contains REFRESH_QUIET, so the only errors
 282         * are for unmerged entries.
 283         */
 284        if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
 285                die_resolve_conflict("commit");
 286}
 287
 288static char *prepare_index(int argc, const char **argv, const char *prefix,
 289                           const struct commit *current_head, int is_status)
 290{
 291        int fd;
 292        struct string_list partial;
 293        const char **pathspec = NULL;
 294        char *old_index_env = NULL;
 295        int refresh_flags = REFRESH_QUIET;
 296
 297        if (is_status)
 298                refresh_flags |= REFRESH_UNMERGED;
 299
 300        if (*argv)
 301                pathspec = get_pathspec(prefix, argv);
 302
 303        if (read_cache_preload(pathspec) < 0)
 304                die(_("index file corrupt"));
 305
 306        if (interactive) {
 307                fd = hold_locked_index(&index_lock, 1);
 308
 309                refresh_cache_or_die(refresh_flags);
 310
 311                if (write_cache(fd, active_cache, active_nr) ||
 312                    close_lock_file(&index_lock))
 313                        die(_("unable to create temporary index"));
 314
 315                old_index_env = getenv(INDEX_ENVIRONMENT);
 316                setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
 317
 318                if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
 319                        die(_("interactive add failed"));
 320
 321                if (old_index_env && *old_index_env)
 322                        setenv(INDEX_ENVIRONMENT, old_index_env, 1);
 323                else
 324                        unsetenv(INDEX_ENVIRONMENT);
 325
 326                discard_cache();
 327                read_cache_from(index_lock.filename);
 328
 329                commit_style = COMMIT_NORMAL;
 330                return index_lock.filename;
 331        }
 332
 333        /*
 334         * Non partial, non as-is commit.
 335         *
 336         * (1) get the real index;
 337         * (2) update the_index as necessary;
 338         * (3) write the_index out to the real index (still locked);
 339         * (4) return the name of the locked index file.
 340         *
 341         * The caller should run hooks on the locked real index, and
 342         * (A) if all goes well, commit the real index;
 343         * (B) on failure, rollback the real index.
 344         */
 345        if (all || (also && pathspec && *pathspec)) {
 346                fd = hold_locked_index(&index_lock, 1);
 347                add_files_to_cache(also ? prefix : NULL, pathspec, 0);
 348                refresh_cache_or_die(refresh_flags);
 349                update_main_cache_tree(WRITE_TREE_SILENT);
 350                if (write_cache(fd, active_cache, active_nr) ||
 351                    close_lock_file(&index_lock))
 352                        die(_("unable to write new_index file"));
 353                commit_style = COMMIT_NORMAL;
 354                return index_lock.filename;
 355        }
 356
 357        /*
 358         * As-is commit.
 359         *
 360         * (1) return the name of the real index file.
 361         *
 362         * The caller should run hooks on the real index,
 363         * and create commit from the_index.
 364         * We still need to refresh the index here.
 365         */
 366        if (!pathspec || !*pathspec) {
 367                fd = hold_locked_index(&index_lock, 1);
 368                refresh_cache_or_die(refresh_flags);
 369                if (active_cache_changed) {
 370                        update_main_cache_tree(WRITE_TREE_SILENT);
 371                        if (write_cache(fd, active_cache, active_nr) ||
 372                            commit_locked_index(&index_lock))
 373                                die(_("unable to write new_index file"));
 374                } else {
 375                        rollback_lock_file(&index_lock);
 376                }
 377                commit_style = COMMIT_AS_IS;
 378                return get_index_file();
 379        }
 380
 381        /*
 382         * A partial commit.
 383         *
 384         * (0) find the set of affected paths;
 385         * (1) get lock on the real index file;
 386         * (2) update the_index with the given paths;
 387         * (3) write the_index out to the real index (still locked);
 388         * (4) get lock on the false index file;
 389         * (5) reset the_index from HEAD;
 390         * (6) update the_index the same way as (2);
 391         * (7) write the_index out to the false index file;
 392         * (8) return the name of the false index file (still locked);
 393         *
 394         * The caller should run hooks on the locked false index, and
 395         * create commit from it.  Then
 396         * (A) if all goes well, commit the real index;
 397         * (B) on failure, rollback the real index;
 398         * In either case, rollback the false index.
 399         */
 400        commit_style = COMMIT_PARTIAL;
 401
 402        if (whence != FROM_COMMIT)
 403                die(_("cannot do a partial commit during a %s."), whence_s());
 404
 405        memset(&partial, 0, sizeof(partial));
 406        partial.strdup_strings = 1;
 407        if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, pathspec))
 408                exit(1);
 409
 410        discard_cache();
 411        if (read_cache() < 0)
 412                die(_("cannot read the index"));
 413
 414        fd = hold_locked_index(&index_lock, 1);
 415        add_remove_files(&partial);
 416        refresh_cache(REFRESH_QUIET);
 417        if (write_cache(fd, active_cache, active_nr) ||
 418            close_lock_file(&index_lock))
 419                die(_("unable to write new_index file"));
 420
 421        fd = hold_lock_file_for_update(&false_lock,
 422                                       git_path("next-index-%"PRIuMAX,
 423                                                (uintmax_t) getpid()),
 424                                       LOCK_DIE_ON_ERROR);
 425
 426        create_base_index(current_head);
 427        add_remove_files(&partial);
 428        refresh_cache(REFRESH_QUIET);
 429
 430        if (write_cache(fd, active_cache, active_nr) ||
 431            close_lock_file(&false_lock))
 432                die(_("unable to write temporary index file"));
 433
 434        discard_cache();
 435        read_cache_from(false_lock.filename);
 436
 437        return false_lock.filename;
 438}
 439
 440static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
 441                      struct wt_status *s)
 442{
 443        unsigned char sha1[20];
 444
 445        if (s->relative_paths)
 446                s->prefix = prefix;
 447
 448        if (amend) {
 449                s->amend = 1;
 450                s->reference = "HEAD^1";
 451        }
 452        s->verbose = verbose;
 453        s->index_file = index_file;
 454        s->fp = fp;
 455        s->nowarn = nowarn;
 456        s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
 457
 458        wt_status_collect(s);
 459
 460        switch (status_format) {
 461        case STATUS_FORMAT_SHORT:
 462                wt_shortstatus_print(s, status_show_branch);
 463                break;
 464        case STATUS_FORMAT_PORCELAIN:
 465                wt_porcelain_print(s);
 466                break;
 467        case STATUS_FORMAT_LONG:
 468                wt_status_print(s);
 469                break;
 470        }
 471
 472        return s->commitable;
 473}
 474
 475static int is_a_merge(const struct commit *current_head)
 476{
 477        return !!(current_head->parents && current_head->parents->next);
 478}
 479
 480static const char sign_off_header[] = "Signed-off-by: ";
 481
 482static void export_one(const char *var, const char *s, const char *e, int hack)
 483{
 484        struct strbuf buf = STRBUF_INIT;
 485        if (hack)
 486                strbuf_addch(&buf, hack);
 487        strbuf_addf(&buf, "%.*s", (int)(e - s), s);
 488        setenv(var, buf.buf, 1);
 489        strbuf_release(&buf);
 490}
 491
 492static void determine_author_info(struct strbuf *author_ident)
 493{
 494        char *name, *email, *date;
 495        struct ident_split author;
 496
 497        name = getenv("GIT_AUTHOR_NAME");
 498        email = getenv("GIT_AUTHOR_EMAIL");
 499        date = getenv("GIT_AUTHOR_DATE");
 500
 501        if (author_message) {
 502                const char *a, *lb, *rb, *eol;
 503                size_t len;
 504
 505                a = strstr(author_message_buffer, "\nauthor ");
 506                if (!a)
 507                        die(_("invalid commit: %s"), author_message);
 508
 509                lb = strchrnul(a + strlen("\nauthor "), '<');
 510                rb = strchrnul(lb, '>');
 511                eol = strchrnul(rb, '\n');
 512                if (!*lb || !*rb || !*eol)
 513                        die(_("invalid commit: %s"), author_message);
 514
 515                if (lb == a + strlen("\nauthor "))
 516                        /* \nauthor <foo@example.com> */
 517                        name = xcalloc(1, 1);
 518                else
 519                        name = xmemdupz(a + strlen("\nauthor "),
 520                                        (lb - strlen(" ") -
 521                                         (a + strlen("\nauthor "))));
 522                email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<")));
 523                date = xmemdupz(rb + strlen("> "), eol - (rb + strlen("> ")));
 524                len = eol - (rb + strlen("> "));
 525                date = xmalloc(len + 2);
 526                *date = '@';
 527                memcpy(date + 1, rb + strlen("> "), len);
 528                date[len + 1] = '\0';
 529        }
 530
 531        if (force_author) {
 532                const char *lb = strstr(force_author, " <");
 533                const char *rb = strchr(force_author, '>');
 534
 535                if (!lb || !rb)
 536                        die(_("malformed --author parameter"));
 537                name = xstrndup(force_author, lb - force_author);
 538                email = xstrndup(lb + 2, rb - (lb + 2));
 539        }
 540
 541        if (force_date)
 542                date = force_date;
 543        strbuf_addstr(author_ident, fmt_ident(name, email, date,
 544                                              IDENT_ERROR_ON_NO_NAME));
 545        if (!split_ident_line(&author, author_ident->buf, author_ident->len)) {
 546                export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
 547                export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
 548                export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
 549        }
 550}
 551
 552static int ends_rfc2822_footer(struct strbuf *sb)
 553{
 554        int ch;
 555        int hit = 0;
 556        int i, j, k;
 557        int len = sb->len;
 558        int first = 1;
 559        const char *buf = sb->buf;
 560
 561        for (i = len - 1; i > 0; i--) {
 562                if (hit && buf[i] == '\n')
 563                        break;
 564                hit = (buf[i] == '\n');
 565        }
 566
 567        while (i < len - 1 && buf[i] == '\n')
 568                i++;
 569
 570        for (; i < len; i = k) {
 571                for (k = i; k < len && buf[k] != '\n'; k++)
 572                        ; /* do nothing */
 573                k++;
 574
 575                if ((buf[k] == ' ' || buf[k] == '\t') && !first)
 576                        continue;
 577
 578                first = 0;
 579
 580                for (j = 0; i + j < len; j++) {
 581                        ch = buf[i + j];
 582                        if (ch == ':')
 583                                break;
 584                        if (isalnum(ch) ||
 585                            (ch == '-'))
 586                                continue;
 587                        return 0;
 588                }
 589        }
 590        return 1;
 591}
 592
 593static char *cut_ident_timestamp_part(char *string)
 594{
 595        char *ket = strrchr(string, '>');
 596        if (!ket || ket[1] != ' ')
 597                die(_("Malformed ident string: '%s'"), string);
 598        *++ket = '\0';
 599        return ket;
 600}
 601
 602static int prepare_to_commit(const char *index_file, const char *prefix,
 603                             struct commit *current_head,
 604                             struct wt_status *s,
 605                             struct strbuf *author_ident)
 606{
 607        struct stat statbuf;
 608        struct strbuf committer_ident = STRBUF_INIT;
 609        int commitable, saved_color_setting;
 610        struct strbuf sb = STRBUF_INIT;
 611        char *buffer;
 612        const char *hook_arg1 = NULL;
 613        const char *hook_arg2 = NULL;
 614        int ident_shown = 0;
 615        int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
 616
 617        /* This checks and barfs if author is badly specified */
 618        determine_author_info(author_ident);
 619
 620        if (!no_verify && run_hook(index_file, "pre-commit", NULL))
 621                return 0;
 622
 623        if (squash_message) {
 624                /*
 625                 * Insert the proper subject line before other commit
 626                 * message options add their content.
 627                 */
 628                if (use_message && !strcmp(use_message, squash_message))
 629                        strbuf_addstr(&sb, "squash! ");
 630                else {
 631                        struct pretty_print_context ctx = {0};
 632                        struct commit *c;
 633                        c = lookup_commit_reference_by_name(squash_message);
 634                        if (!c)
 635                                die(_("could not lookup commit %s"), squash_message);
 636                        ctx.output_encoding = get_commit_output_encoding();
 637                        format_commit_message(c, "squash! %s\n\n", &sb,
 638                                              &ctx);
 639                }
 640        }
 641
 642        if (message.len) {
 643                strbuf_addbuf(&sb, &message);
 644                hook_arg1 = "message";
 645        } else if (logfile && !strcmp(logfile, "-")) {
 646                if (isatty(0))
 647                        fprintf(stderr, _("(reading log message from standard input)\n"));
 648                if (strbuf_read(&sb, 0, 0) < 0)
 649                        die_errno(_("could not read log from standard input"));
 650                hook_arg1 = "message";
 651        } else if (logfile) {
 652                if (strbuf_read_file(&sb, logfile, 0) < 0)
 653                        die_errno(_("could not read log file '%s'"),
 654                                  logfile);
 655                hook_arg1 = "message";
 656        } else if (use_message) {
 657                buffer = strstr(use_message_buffer, "\n\n");
 658                if (!buffer || buffer[2] == '\0')
 659                        die(_("commit has empty message"));
 660                strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
 661                hook_arg1 = "commit";
 662                hook_arg2 = use_message;
 663        } else if (fixup_message) {
 664                struct pretty_print_context ctx = {0};
 665                struct commit *commit;
 666                commit = lookup_commit_reference_by_name(fixup_message);
 667                if (!commit)
 668                        die(_("could not lookup commit %s"), fixup_message);
 669                ctx.output_encoding = get_commit_output_encoding();
 670                format_commit_message(commit, "fixup! %s\n\n",
 671                                      &sb, &ctx);
 672                hook_arg1 = "message";
 673        } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
 674                if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
 675                        die_errno(_("could not read MERGE_MSG"));
 676                hook_arg1 = "merge";
 677        } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
 678                if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
 679                        die_errno(_("could not read SQUASH_MSG"));
 680                hook_arg1 = "squash";
 681        } else if (template_file) {
 682                if (strbuf_read_file(&sb, template_file, 0) < 0)
 683                        die_errno(_("could not read '%s'"), template_file);
 684                hook_arg1 = "template";
 685                clean_message_contents = 0;
 686        }
 687
 688        /*
 689         * The remaining cases don't modify the template message, but
 690         * just set the argument(s) to the prepare-commit-msg hook.
 691         */
 692        else if (whence == FROM_MERGE)
 693                hook_arg1 = "merge";
 694        else if (whence == FROM_CHERRY_PICK) {
 695                hook_arg1 = "commit";
 696                hook_arg2 = "CHERRY_PICK_HEAD";
 697        }
 698
 699        if (squash_message) {
 700                /*
 701                 * If squash_commit was used for the commit subject,
 702                 * then we're possibly hijacking other commit log options.
 703                 * Reset the hook args to tell the real story.
 704                 */
 705                hook_arg1 = "message";
 706                hook_arg2 = "";
 707        }
 708
 709        s->fp = fopen(git_path(commit_editmsg), "w");
 710        if (s->fp == NULL)
 711                die_errno(_("could not open '%s'"), git_path(commit_editmsg));
 712
 713        if (clean_message_contents)
 714                stripspace(&sb, 0);
 715
 716        if (signoff) {
 717                struct strbuf sob = STRBUF_INIT;
 718                int i;
 719
 720                strbuf_addstr(&sob, sign_off_header);
 721                strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
 722                                             getenv("GIT_COMMITTER_EMAIL")));
 723                strbuf_addch(&sob, '\n');
 724                for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
 725                        ; /* do nothing */
 726                if (prefixcmp(sb.buf + i, sob.buf)) {
 727                        if (!i || !ends_rfc2822_footer(&sb))
 728                                strbuf_addch(&sb, '\n');
 729                        strbuf_addbuf(&sb, &sob);
 730                }
 731                strbuf_release(&sob);
 732        }
 733
 734        if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
 735                die_errno(_("could not write commit template"));
 736
 737        strbuf_release(&sb);
 738
 739        /* This checks if committer ident is explicitly given */
 740        strbuf_addstr(&committer_ident, git_committer_info(0));
 741        if (use_editor && include_status) {
 742                char *ai_tmp, *ci_tmp;
 743                if (whence != FROM_COMMIT)
 744                        status_printf_ln(s, GIT_COLOR_NORMAL,
 745                                _("\n"
 746                                "It looks like you may be committing a %s.\n"
 747                                "If this is not correct, please remove the file\n"
 748                                "       %s\n"
 749                                "and try again.\n"
 750                                ""),
 751                                whence_s(),
 752                                git_path(whence == FROM_MERGE
 753                                         ? "MERGE_HEAD"
 754                                         : "CHERRY_PICK_HEAD"));
 755
 756                fprintf(s->fp, "\n");
 757                status_printf(s, GIT_COLOR_NORMAL,
 758                        _("Please enter the commit message for your changes."));
 759                if (cleanup_mode == CLEANUP_ALL)
 760                        status_printf_more(s, GIT_COLOR_NORMAL,
 761                                _(" Lines starting\n"
 762                                "with '#' will be ignored, and an empty"
 763                                " message aborts the commit.\n"));
 764                else /* CLEANUP_SPACE, that is. */
 765                        status_printf_more(s, GIT_COLOR_NORMAL,
 766                                _(" Lines starting\n"
 767                                "with '#' will be kept; you may remove them"
 768                                " yourself if you want to.\n"
 769                                "An empty message aborts the commit.\n"));
 770                if (only_include_assumed)
 771                        status_printf_ln(s, GIT_COLOR_NORMAL,
 772                                        "%s", only_include_assumed);
 773
 774                ai_tmp = cut_ident_timestamp_part(author_ident->buf);
 775                ci_tmp = cut_ident_timestamp_part(committer_ident.buf);
 776                if (strcmp(author_ident->buf, committer_ident.buf))
 777                        status_printf_ln(s, GIT_COLOR_NORMAL,
 778                                _("%s"
 779                                "Author:    %s"),
 780                                ident_shown++ ? "" : "\n",
 781                                author_ident->buf);
 782
 783                if (!user_ident_sufficiently_given())
 784                        status_printf_ln(s, GIT_COLOR_NORMAL,
 785                                _("%s"
 786                                "Committer: %s"),
 787                                ident_shown++ ? "" : "\n",
 788                                committer_ident.buf);
 789
 790                if (ident_shown)
 791                        status_printf_ln(s, GIT_COLOR_NORMAL, "");
 792
 793                saved_color_setting = s->use_color;
 794                s->use_color = 0;
 795                commitable = run_status(s->fp, index_file, prefix, 1, s);
 796                s->use_color = saved_color_setting;
 797
 798                *ai_tmp = ' ';
 799                *ci_tmp = ' ';
 800        } else {
 801                unsigned char sha1[20];
 802                const char *parent = "HEAD";
 803
 804                if (!active_nr && read_cache() < 0)
 805                        die(_("Cannot read index"));
 806
 807                if (amend)
 808                        parent = "HEAD^1";
 809
 810                if (get_sha1(parent, sha1))
 811                        commitable = !!active_nr;
 812                else
 813                        commitable = index_differs_from(parent, 0);
 814        }
 815        strbuf_release(&committer_ident);
 816
 817        fclose(s->fp);
 818
 819        /*
 820         * Reject an attempt to record a non-merge empty commit without
 821         * explicit --allow-empty. In the cherry-pick case, it may be
 822         * empty due to conflict resolution, which the user should okay.
 823         */
 824        if (!commitable && whence != FROM_MERGE && !allow_empty &&
 825            !(amend && is_a_merge(current_head))) {
 826                run_status(stdout, index_file, prefix, 0, s);
 827                if (amend)
 828                        fputs(_(empty_amend_advice), stderr);
 829                else if (whence == FROM_CHERRY_PICK)
 830                        fputs(_(empty_cherry_pick_advice), stderr);
 831                return 0;
 832        }
 833
 834        /*
 835         * Re-read the index as pre-commit hook could have updated it,
 836         * and write it out as a tree.  We must do this before we invoke
 837         * the editor and after we invoke run_status above.
 838         */
 839        discard_cache();
 840        read_cache_from(index_file);
 841        if (update_main_cache_tree(0)) {
 842                error(_("Error building trees"));
 843                return 0;
 844        }
 845
 846        if (run_hook(index_file, "prepare-commit-msg",
 847                     git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
 848                return 0;
 849
 850        if (use_editor) {
 851                char index[PATH_MAX];
 852                const char *env[2] = { NULL };
 853                env[0] =  index;
 854                snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
 855                if (launch_editor(git_path(commit_editmsg), NULL, env)) {
 856                        fprintf(stderr,
 857                        _("Please supply the message using either -m or -F option.\n"));
 858                        exit(1);
 859                }
 860        }
 861
 862        if (!no_verify &&
 863            run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
 864                return 0;
 865        }
 866
 867        return 1;
 868}
 869
 870static int rest_is_empty(struct strbuf *sb, int start)
 871{
 872        int i, eol;
 873        const char *nl;
 874
 875        /* Check if the rest is just whitespace and Signed-of-by's. */
 876        for (i = start; i < sb->len; i++) {
 877                nl = memchr(sb->buf + i, '\n', sb->len - i);
 878                if (nl)
 879                        eol = nl - sb->buf;
 880                else
 881                        eol = sb->len;
 882
 883                if (strlen(sign_off_header) <= eol - i &&
 884                    !prefixcmp(sb->buf + i, sign_off_header)) {
 885                        i = eol;
 886                        continue;
 887                }
 888                while (i < eol)
 889                        if (!isspace(sb->buf[i++]))
 890                                return 0;
 891        }
 892
 893        return 1;
 894}
 895
 896/*
 897 * Find out if the message in the strbuf contains only whitespace and
 898 * Signed-off-by lines.
 899 */
 900static int message_is_empty(struct strbuf *sb)
 901{
 902        if (cleanup_mode == CLEANUP_NONE && sb->len)
 903                return 0;
 904        return rest_is_empty(sb, 0);
 905}
 906
 907/*
 908 * See if the user edited the message in the editor or left what
 909 * was in the template intact
 910 */
 911static int template_untouched(struct strbuf *sb)
 912{
 913        struct strbuf tmpl = STRBUF_INIT;
 914        char *start;
 915
 916        if (cleanup_mode == CLEANUP_NONE && sb->len)
 917                return 0;
 918
 919        if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
 920                return 0;
 921
 922        stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
 923        start = (char *)skip_prefix(sb->buf, tmpl.buf);
 924        if (!start)
 925                start = sb->buf;
 926        strbuf_release(&tmpl);
 927        return rest_is_empty(sb, start - sb->buf);
 928}
 929
 930static const char *find_author_by_nickname(const char *name)
 931{
 932        struct rev_info revs;
 933        struct commit *commit;
 934        struct strbuf buf = STRBUF_INIT;
 935        const char *av[20];
 936        int ac = 0;
 937
 938        init_revisions(&revs, NULL);
 939        strbuf_addf(&buf, "--author=%s", name);
 940        av[++ac] = "--all";
 941        av[++ac] = "-i";
 942        av[++ac] = buf.buf;
 943        av[++ac] = NULL;
 944        setup_revisions(ac, av, &revs, NULL);
 945        prepare_revision_walk(&revs);
 946        commit = get_revision(&revs);
 947        if (commit) {
 948                struct pretty_print_context ctx = {0};
 949                ctx.date_mode = DATE_NORMAL;
 950                strbuf_release(&buf);
 951                format_commit_message(commit, "%an <%ae>", &buf, &ctx);
 952                return strbuf_detach(&buf, NULL);
 953        }
 954        die(_("No existing author found with '%s'"), name);
 955}
 956
 957
 958static void handle_untracked_files_arg(struct wt_status *s)
 959{
 960        if (!untracked_files_arg)
 961                ; /* default already initialized */
 962        else if (!strcmp(untracked_files_arg, "no"))
 963                s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
 964        else if (!strcmp(untracked_files_arg, "normal"))
 965                s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
 966        else if (!strcmp(untracked_files_arg, "all"))
 967                s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
 968        else
 969                die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
 970}
 971
 972static const char *read_commit_message(const char *name)
 973{
 974        const char *out_enc, *out;
 975        struct commit *commit;
 976
 977        commit = lookup_commit_reference_by_name(name);
 978        if (!commit)
 979                die(_("could not lookup commit %s"), name);
 980        out_enc = get_commit_output_encoding();
 981        out = logmsg_reencode(commit, out_enc);
 982
 983        /*
 984         * If we failed to reencode the buffer, just copy it
 985         * byte for byte so the user can try to fix it up.
 986         * This also handles the case where input and output
 987         * encodings are identical.
 988         */
 989        if (out == NULL)
 990                out = xstrdup(commit->buffer);
 991        return out;
 992}
 993
 994static int parse_and_validate_options(int argc, const char *argv[],
 995                                      const struct option *options,
 996                                      const char * const usage[],
 997                                      const char *prefix,
 998                                      struct commit *current_head,
 999                                      struct wt_status *s)
1000{
1001        int f = 0;
1002
1003        argc = parse_options(argc, argv, prefix, options, usage, 0);
1004
1005        if (force_author && !strchr(force_author, '>'))
1006                force_author = find_author_by_nickname(force_author);
1007
1008        if (force_author && renew_authorship)
1009                die(_("Using both --reset-author and --author does not make sense"));
1010
1011        if (logfile || message.len || use_message || fixup_message)
1012                use_editor = 0;
1013        if (0 <= edit_flag)
1014                use_editor = edit_flag;
1015        if (!use_editor)
1016                setenv("GIT_EDITOR", ":", 1);
1017
1018        /* Sanity check options */
1019        if (amend && !current_head)
1020                die(_("You have nothing to amend."));
1021        if (amend && whence != FROM_COMMIT)
1022                die(_("You are in the middle of a %s -- cannot amend."), whence_s());
1023        if (fixup_message && squash_message)
1024                die(_("Options --squash and --fixup cannot be used together"));
1025        if (use_message)
1026                f++;
1027        if (edit_message)
1028                f++;
1029        if (fixup_message)
1030                f++;
1031        if (logfile)
1032                f++;
1033        if (f > 1)
1034                die(_("Only one of -c/-C/-F/--fixup can be used."));
1035        if (message.len && f > 0)
1036                die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
1037        if (f || message.len)
1038                template_file = NULL;
1039        if (edit_message)
1040                use_message = edit_message;
1041        if (amend && !use_message && !fixup_message)
1042                use_message = "HEAD";
1043        if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship)
1044                die(_("--reset-author can be used only with -C, -c or --amend."));
1045        if (use_message) {
1046                use_message_buffer = read_commit_message(use_message);
1047                if (!renew_authorship) {
1048                        author_message = use_message;
1049                        author_message_buffer = use_message_buffer;
1050                }
1051        }
1052        if (whence == FROM_CHERRY_PICK && !renew_authorship) {
1053                author_message = "CHERRY_PICK_HEAD";
1054                author_message_buffer = read_commit_message(author_message);
1055        }
1056
1057        if (patch_interactive)
1058                interactive = 1;
1059
1060        if (!!also + !!only + !!all + !!interactive > 1)
1061                die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1062        if (argc == 0 && (also || (only && !amend)))
1063                die(_("No paths with --include/--only does not make sense."));
1064        if (argc == 0 && only && amend)
1065                only_include_assumed = _("Clever... amending the last one with dirty index.");
1066        if (argc > 0 && !also && !only)
1067                only_include_assumed = _("Explicit paths specified without -i nor -o; assuming --only paths...");
1068        if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
1069                cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
1070        else if (!strcmp(cleanup_arg, "verbatim"))
1071                cleanup_mode = CLEANUP_NONE;
1072        else if (!strcmp(cleanup_arg, "whitespace"))
1073                cleanup_mode = CLEANUP_SPACE;
1074        else if (!strcmp(cleanup_arg, "strip"))
1075                cleanup_mode = CLEANUP_ALL;
1076        else
1077                die(_("Invalid cleanup mode %s"), cleanup_arg);
1078
1079        handle_untracked_files_arg(s);
1080
1081        if (all && argc > 0)
1082                die(_("Paths with -a does not make sense."));
1083
1084        if (s->null_termination && status_format == STATUS_FORMAT_LONG)
1085                status_format = STATUS_FORMAT_PORCELAIN;
1086        if (status_format != STATUS_FORMAT_LONG)
1087                dry_run = 1;
1088
1089        return argc;
1090}
1091
1092static int dry_run_commit(int argc, const char **argv, const char *prefix,
1093                          const struct commit *current_head, struct wt_status *s)
1094{
1095        int commitable;
1096        const char *index_file;
1097
1098        index_file = prepare_index(argc, argv, prefix, current_head, 1);
1099        commitable = run_status(stdout, index_file, prefix, 0, s);
1100        rollback_index_files();
1101
1102        return commitable ? 0 : 1;
1103}
1104
1105static int parse_status_slot(const char *var, int offset)
1106{
1107        if (!strcasecmp(var+offset, "header"))
1108                return WT_STATUS_HEADER;
1109        if (!strcasecmp(var+offset, "branch"))
1110                return WT_STATUS_ONBRANCH;
1111        if (!strcasecmp(var+offset, "updated")
1112                || !strcasecmp(var+offset, "added"))
1113                return WT_STATUS_UPDATED;
1114        if (!strcasecmp(var+offset, "changed"))
1115                return WT_STATUS_CHANGED;
1116        if (!strcasecmp(var+offset, "untracked"))
1117                return WT_STATUS_UNTRACKED;
1118        if (!strcasecmp(var+offset, "nobranch"))
1119                return WT_STATUS_NOBRANCH;
1120        if (!strcasecmp(var+offset, "unmerged"))
1121                return WT_STATUS_UNMERGED;
1122        return -1;
1123}
1124
1125static int git_status_config(const char *k, const char *v, void *cb)
1126{
1127        struct wt_status *s = cb;
1128
1129        if (!strcmp(k, "status.submodulesummary")) {
1130                int is_bool;
1131                s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1132                if (is_bool && s->submodule_summary)
1133                        s->submodule_summary = -1;
1134                return 0;
1135        }
1136        if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1137                s->use_color = git_config_colorbool(k, v);
1138                return 0;
1139        }
1140        if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
1141                int slot = parse_status_slot(k, 13);
1142                if (slot < 0)
1143                        return 0;
1144                if (!v)
1145                        return config_error_nonbool(k);
1146                color_parse(v, k, s->color_palette[slot]);
1147                return 0;
1148        }
1149        if (!strcmp(k, "status.relativepaths")) {
1150                s->relative_paths = git_config_bool(k, v);
1151                return 0;
1152        }
1153        if (!strcmp(k, "status.showuntrackedfiles")) {
1154                if (!v)
1155                        return config_error_nonbool(k);
1156                else if (!strcmp(v, "no"))
1157                        s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1158                else if (!strcmp(v, "normal"))
1159                        s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1160                else if (!strcmp(v, "all"))
1161                        s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1162                else
1163                        return error(_("Invalid untracked files mode '%s'"), v);
1164                return 0;
1165        }
1166        return git_diff_ui_config(k, v, NULL);
1167}
1168
1169int cmd_status(int argc, const char **argv, const char *prefix)
1170{
1171        static struct wt_status s;
1172        int fd;
1173        unsigned char sha1[20];
1174        static struct option builtin_status_options[] = {
1175                OPT__VERBOSE(&verbose, "be verbose"),
1176                OPT_SET_INT('s', "short", &status_format,
1177                            "show status concisely", STATUS_FORMAT_SHORT),
1178                OPT_BOOLEAN('b', "branch", &status_show_branch,
1179                            "show branch information"),
1180                OPT_SET_INT(0, "porcelain", &status_format,
1181                            "machine-readable output",
1182                            STATUS_FORMAT_PORCELAIN),
1183                OPT_BOOLEAN('z', "null", &s.null_termination,
1184                            "terminate entries with NUL"),
1185                { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1186                  "mode",
1187                  "show untracked files, optional modes: all, normal, no. (Default: all)",
1188                  PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1189                OPT_BOOLEAN(0, "ignored", &show_ignored_in_status,
1190                            "show ignored files"),
1191                { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, "when",
1192                  "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)",
1193                  PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1194                OPT_END(),
1195        };
1196
1197        if (argc == 2 && !strcmp(argv[1], "-h"))
1198                usage_with_options(builtin_status_usage, builtin_status_options);
1199
1200        wt_status_prepare(&s);
1201        gitmodules_config();
1202        git_config(git_status_config, &s);
1203        determine_whence(&s);
1204        argc = parse_options(argc, argv, prefix,
1205                             builtin_status_options,
1206                             builtin_status_usage, 0);
1207
1208        if (s.null_termination && status_format == STATUS_FORMAT_LONG)
1209                status_format = STATUS_FORMAT_PORCELAIN;
1210
1211        handle_untracked_files_arg(&s);
1212        if (show_ignored_in_status)
1213                s.show_ignored_files = 1;
1214        if (*argv)
1215                s.pathspec = get_pathspec(prefix, argv);
1216
1217        read_cache_preload(s.pathspec);
1218        refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
1219
1220        fd = hold_locked_index(&index_lock, 0);
1221        if (0 <= fd)
1222                update_index_if_able(&the_index, &index_lock);
1223
1224        s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
1225        s.ignore_submodule_arg = ignore_submodule_arg;
1226        wt_status_collect(&s);
1227
1228        if (s.relative_paths)
1229                s.prefix = prefix;
1230
1231        switch (status_format) {
1232        case STATUS_FORMAT_SHORT:
1233                wt_shortstatus_print(&s, status_show_branch);
1234                break;
1235        case STATUS_FORMAT_PORCELAIN:
1236                wt_porcelain_print(&s);
1237                break;
1238        case STATUS_FORMAT_LONG:
1239                s.verbose = verbose;
1240                s.ignore_submodule_arg = ignore_submodule_arg;
1241                wt_status_print(&s);
1242                break;
1243        }
1244        return 0;
1245}
1246
1247static void print_summary(const char *prefix, const unsigned char *sha1,
1248                          int initial_commit)
1249{
1250        struct rev_info rev;
1251        struct commit *commit;
1252        struct strbuf format = STRBUF_INIT;
1253        unsigned char junk_sha1[20];
1254        const char *head;
1255        struct pretty_print_context pctx = {0};
1256        struct strbuf author_ident = STRBUF_INIT;
1257        struct strbuf committer_ident = STRBUF_INIT;
1258
1259        commit = lookup_commit(sha1);
1260        if (!commit)
1261                die(_("couldn't look up newly created commit"));
1262        if (!commit || parse_commit(commit))
1263                die(_("could not parse newly created commit"));
1264
1265        strbuf_addstr(&format, "format:%h] %s");
1266
1267        format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
1268        format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
1269        if (strbuf_cmp(&author_ident, &committer_ident)) {
1270                strbuf_addstr(&format, "\n Author: ");
1271                strbuf_addbuf_percentquote(&format, &author_ident);
1272        }
1273        if (!user_ident_sufficiently_given()) {
1274                strbuf_addstr(&format, "\n Committer: ");
1275                strbuf_addbuf_percentquote(&format, &committer_ident);
1276                if (advice_implicit_identity) {
1277                        strbuf_addch(&format, '\n');
1278                        strbuf_addstr(&format, _(implicit_ident_advice));
1279                }
1280        }
1281        strbuf_release(&author_ident);
1282        strbuf_release(&committer_ident);
1283
1284        init_revisions(&rev, prefix);
1285        setup_revisions(0, NULL, &rev, NULL);
1286
1287        rev.diff = 1;
1288        rev.diffopt.output_format =
1289                DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1290
1291        rev.verbose_header = 1;
1292        rev.show_root_diff = 1;
1293        get_commit_format(format.buf, &rev);
1294        rev.always_show_header = 0;
1295        rev.diffopt.detect_rename = 1;
1296        rev.diffopt.break_opt = 0;
1297        diff_setup_done(&rev.diffopt);
1298
1299        head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
1300        printf("[%s%s ",
1301                !prefixcmp(head, "refs/heads/") ?
1302                        head + 11 :
1303                        !strcmp(head, "HEAD") ?
1304                                _("detached HEAD") :
1305                                head,
1306                initial_commit ? _(" (root-commit)") : "");
1307
1308        if (!log_tree_commit(&rev, commit)) {
1309                rev.always_show_header = 1;
1310                rev.use_terminator = 1;
1311                log_tree_commit(&rev, commit);
1312        }
1313
1314        strbuf_release(&format);
1315}
1316
1317static int git_commit_config(const char *k, const char *v, void *cb)
1318{
1319        struct wt_status *s = cb;
1320        int status;
1321
1322        if (!strcmp(k, "commit.template"))
1323                return git_config_pathname(&template_file, k, v);
1324        if (!strcmp(k, "commit.status")) {
1325                include_status = git_config_bool(k, v);
1326                return 0;
1327        }
1328
1329        status = git_gpg_config(k, v, NULL);
1330        if (status)
1331                return status;
1332        return git_status_config(k, v, s);
1333}
1334
1335static const char post_rewrite_hook[] = "hooks/post-rewrite";
1336
1337static int run_rewrite_hook(const unsigned char *oldsha1,
1338                            const unsigned char *newsha1)
1339{
1340        /* oldsha1 SP newsha1 LF NUL */
1341        static char buf[2*40 + 3];
1342        struct child_process proc;
1343        const char *argv[3];
1344        int code;
1345        size_t n;
1346
1347        if (access(git_path(post_rewrite_hook), X_OK) < 0)
1348                return 0;
1349
1350        argv[0] = git_path(post_rewrite_hook);
1351        argv[1] = "amend";
1352        argv[2] = NULL;
1353
1354        memset(&proc, 0, sizeof(proc));
1355        proc.argv = argv;
1356        proc.in = -1;
1357        proc.stdout_to_stderr = 1;
1358
1359        code = start_command(&proc);
1360        if (code)
1361                return code;
1362        n = snprintf(buf, sizeof(buf), "%s %s\n",
1363                     sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
1364        write_in_full(proc.in, buf, n);
1365        close(proc.in);
1366        return finish_command(&proc);
1367}
1368
1369int cmd_commit(int argc, const char **argv, const char *prefix)
1370{
1371        static struct wt_status s;
1372        static struct option builtin_commit_options[] = {
1373                OPT__QUIET(&quiet, "suppress summary after successful commit"),
1374                OPT__VERBOSE(&verbose, "show diff in commit message template"),
1375
1376                OPT_GROUP("Commit message options"),
1377                OPT_FILENAME('F', "file", &logfile, "read message from file"),
1378                OPT_STRING(0, "author", &force_author, "author", "override author for commit"),
1379                OPT_STRING(0, "date", &force_date, "date", "override date for commit"),
1380                OPT_CALLBACK('m', "message", &message, "message", "commit message", opt_parse_m),
1381                OPT_STRING('c', "reedit-message", &edit_message, "commit", "reuse and edit message from specified commit"),
1382                OPT_STRING('C', "reuse-message", &use_message, "commit", "reuse message from specified commit"),
1383                OPT_STRING(0, "fixup", &fixup_message, "commit", "use autosquash formatted message to fixup specified commit"),
1384                OPT_STRING(0, "squash", &squash_message, "commit", "use autosquash formatted message to squash specified commit"),
1385                OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C/-c/--amend)"),
1386                OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
1387                OPT_FILENAME('t', "template", &template_file, "use specified template file"),
1388                OPT_BOOL('e', "edit", &edit_flag, "force edit of commit"),
1389                OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
1390                OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
1391                { OPTION_STRING, 'S', "gpg-sign", &sign_commit, "key id",
1392                  "GPG sign commit", PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1393                /* end commit message options */
1394
1395                OPT_GROUP("Commit contents options"),
1396                OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
1397                OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
1398                OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
1399                OPT_BOOLEAN('p', "patch", &patch_interactive, "interactively add changes"),
1400                OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
1401                OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
1402                OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
1403                OPT_SET_INT(0, "short", &status_format, "show status concisely",
1404                            STATUS_FORMAT_SHORT),
1405                OPT_BOOLEAN(0, "branch", &status_show_branch, "show branch information"),
1406                OPT_SET_INT(0, "porcelain", &status_format,
1407                            "machine-readable output", STATUS_FORMAT_PORCELAIN),
1408                OPT_BOOLEAN('z', "null", &s.null_termination,
1409                            "terminate entries with NUL"),
1410                OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
1411                OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, "bypass post-rewrite hook"),
1412                { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1413                /* end commit contents options */
1414
1415                { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
1416                  "ok to record an empty change",
1417                  PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1418                { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
1419                  "ok to record a change with an empty message",
1420                  PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1421
1422                OPT_END()
1423        };
1424
1425        struct strbuf sb = STRBUF_INIT;
1426        struct strbuf author_ident = STRBUF_INIT;
1427        const char *index_file, *reflog_msg;
1428        char *nl, *p;
1429        unsigned char sha1[20];
1430        struct ref_lock *ref_lock;
1431        struct commit_list *parents = NULL, **pptr = &parents;
1432        struct stat statbuf;
1433        int allow_fast_forward = 1;
1434        struct commit *current_head = NULL;
1435        struct commit_extra_header *extra = NULL;
1436
1437        if (argc == 2 && !strcmp(argv[1], "-h"))
1438                usage_with_options(builtin_commit_usage, builtin_commit_options);
1439
1440        wt_status_prepare(&s);
1441        git_config(git_commit_config, &s);
1442        determine_whence(&s);
1443
1444        if (get_sha1("HEAD", sha1))
1445                current_head = NULL;
1446        else {
1447                current_head = lookup_commit_or_die(sha1, "HEAD");
1448                if (!current_head || parse_commit(current_head))
1449                        die(_("could not parse HEAD commit"));
1450        }
1451        argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1452                                          builtin_commit_usage,
1453                                          prefix, current_head, &s);
1454        if (dry_run)
1455                return dry_run_commit(argc, argv, prefix, current_head, &s);
1456        index_file = prepare_index(argc, argv, prefix, current_head, 0);
1457
1458        /* Set up everything for writing the commit object.  This includes
1459           running hooks, writing the trees, and interacting with the user.  */
1460        if (!prepare_to_commit(index_file, prefix,
1461                               current_head, &s, &author_ident)) {
1462                rollback_index_files();
1463                return 1;
1464        }
1465
1466        /* Determine parents */
1467        reflog_msg = getenv("GIT_REFLOG_ACTION");
1468        if (!current_head) {
1469                if (!reflog_msg)
1470                        reflog_msg = "commit (initial)";
1471        } else if (amend) {
1472                struct commit_list *c;
1473
1474                if (!reflog_msg)
1475                        reflog_msg = "commit (amend)";
1476                for (c = current_head->parents; c; c = c->next)
1477                        pptr = &commit_list_insert(c->item, pptr)->next;
1478        } else if (whence == FROM_MERGE) {
1479                struct strbuf m = STRBUF_INIT;
1480                FILE *fp;
1481
1482                if (!reflog_msg)
1483                        reflog_msg = "commit (merge)";
1484                pptr = &commit_list_insert(current_head, pptr)->next;
1485                fp = fopen(git_path("MERGE_HEAD"), "r");
1486                if (fp == NULL)
1487                        die_errno(_("could not open '%s' for reading"),
1488                                  git_path("MERGE_HEAD"));
1489                while (strbuf_getline(&m, fp, '\n') != EOF) {
1490                        struct commit *parent;
1491
1492                        parent = get_merge_parent(m.buf);
1493                        if (!parent)
1494                                die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
1495                        pptr = &commit_list_insert(parent, pptr)->next;
1496                }
1497                fclose(fp);
1498                strbuf_release(&m);
1499                if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1500                        if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
1501                                die_errno(_("could not read MERGE_MODE"));
1502                        if (!strcmp(sb.buf, "no-ff"))
1503                                allow_fast_forward = 0;
1504                }
1505                if (allow_fast_forward)
1506                        parents = reduce_heads(parents);
1507        } else {
1508                if (!reflog_msg)
1509                        reflog_msg = (whence == FROM_CHERRY_PICK)
1510                                        ? "commit (cherry-pick)"
1511                                        : "commit";
1512                pptr = &commit_list_insert(current_head, pptr)->next;
1513        }
1514
1515        /* Finally, get the commit message */
1516        strbuf_reset(&sb);
1517        if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
1518                int saved_errno = errno;
1519                rollback_index_files();
1520                die(_("could not read commit message: %s"), strerror(saved_errno));
1521        }
1522
1523        /* Truncate the message just before the diff, if any. */
1524        if (verbose) {
1525                p = strstr(sb.buf, "\ndiff --git ");
1526                if (p != NULL)
1527                        strbuf_setlen(&sb, p - sb.buf + 1);
1528        }
1529
1530        if (cleanup_mode != CLEANUP_NONE)
1531                stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1532        if (template_untouched(&sb) && !allow_empty_message) {
1533                rollback_index_files();
1534                fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1535                exit(1);
1536        }
1537        if (message_is_empty(&sb) && !allow_empty_message) {
1538                rollback_index_files();
1539                fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
1540                exit(1);
1541        }
1542
1543        if (amend) {
1544                const char *exclude_gpgsig[2] = { "gpgsig", NULL };
1545                extra = read_commit_extra_headers(current_head, exclude_gpgsig);
1546        } else {
1547                struct commit_extra_header **tail = &extra;
1548                append_merge_tag_headers(parents, &tail);
1549        }
1550
1551        if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1,
1552                                 author_ident.buf, sign_commit, extra)) {
1553                rollback_index_files();
1554                die(_("failed to write commit object"));
1555        }
1556        strbuf_release(&author_ident);
1557        free_commit_extra_headers(extra);
1558
1559        ref_lock = lock_any_ref_for_update("HEAD",
1560                                           !current_head
1561                                           ? NULL
1562                                           : current_head->object.sha1,
1563                                           0);
1564
1565        nl = strchr(sb.buf, '\n');
1566        if (nl)
1567                strbuf_setlen(&sb, nl + 1 - sb.buf);
1568        else
1569                strbuf_addch(&sb, '\n');
1570        strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1571        strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
1572
1573        if (!ref_lock) {
1574                rollback_index_files();
1575                die(_("cannot lock HEAD ref"));
1576        }
1577        if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
1578                rollback_index_files();
1579                die(_("cannot update HEAD ref"));
1580        }
1581
1582        unlink(git_path("CHERRY_PICK_HEAD"));
1583        unlink(git_path("REVERT_HEAD"));
1584        unlink(git_path("MERGE_HEAD"));
1585        unlink(git_path("MERGE_MSG"));
1586        unlink(git_path("MERGE_MODE"));
1587        unlink(git_path("SQUASH_MSG"));
1588
1589        if (commit_index_files())
1590                die (_("Repository has been updated, but unable to write\n"
1591                     "new_index file. Check that disk is not full or quota is\n"
1592                     "not exceeded, and then \"git reset HEAD\" to recover."));
1593
1594        rerere(0);
1595        run_hook(get_index_file(), "post-commit", NULL);
1596        if (amend && !no_post_rewrite) {
1597                struct notes_rewrite_cfg *cfg;
1598                cfg = init_copy_notes_for_rewrite("amend");
1599                if (cfg) {
1600                        /* we are amending, so current_head is not NULL */
1601                        copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
1602                        finish_copy_notes_for_rewrite(cfg);
1603                }
1604                run_rewrite_hook(current_head->object.sha1, sha1);
1605        }
1606        if (!quiet)
1607                print_summary(prefix, sha1, !current_head);
1608
1609        return 0;
1610}