builtin-commit.con commit Merge branch 'jk/warn-author-committer-after-commit' (0877510)
   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
  29static const char * const builtin_commit_usage[] = {
  30        "git commit [options] [--] <filepattern>...",
  31        NULL
  32};
  33
  34static const char * const builtin_status_usage[] = {
  35        "git status [options] [--] <filepattern>...",
  36        NULL
  37};
  38
  39static const char implicit_ident_advice[] =
  40"Your name and email address were configured automatically based\n"
  41"on your username and hostname. Please check that they are accurate.\n"
  42"You can suppress this message by setting them explicitly:\n"
  43"\n"
  44"    git config --global user.name Your Name\n"
  45"    git config --global user.email you@example.com\n"
  46"\n"
  47"If the identity used for this commit is wrong, you can fix it with:\n"
  48"\n"
  49"    git commit --amend --author='Your Name <you@example.com>'\n";
  50
  51static unsigned char head_sha1[20];
  52
  53static char *use_message_buffer;
  54static const char commit_editmsg[] = "COMMIT_EDITMSG";
  55static struct lock_file index_lock; /* real index */
  56static struct lock_file false_lock; /* used only for partial commits */
  57static enum {
  58        COMMIT_AS_IS = 1,
  59        COMMIT_NORMAL,
  60        COMMIT_PARTIAL,
  61} commit_style;
  62
  63static const char *logfile, *force_author;
  64static const char *template_file;
  65static char *edit_message, *use_message;
  66static char *author_name, *author_email, *author_date;
  67static int all, edit_flag, also, interactive, only, amend, signoff;
  68static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
  69static char *untracked_files_arg, *force_date;
  70/*
  71 * The default commit message cleanup mode will remove the lines
  72 * beginning with # (shell comments) and leading and trailing
  73 * whitespaces (empty lines or containing only whitespaces)
  74 * if editor is used, and only the whitespaces if the message
  75 * is specified explicitly.
  76 */
  77static enum {
  78        CLEANUP_SPACE,
  79        CLEANUP_NONE,
  80        CLEANUP_ALL,
  81} cleanup_mode;
  82static char *cleanup_arg;
  83
  84static int use_editor = 1, initial_commit, in_merge, include_status = 1;
  85static const char *only_include_assumed;
  86static struct strbuf message;
  87
  88static int null_termination;
  89static enum {
  90        STATUS_FORMAT_LONG,
  91        STATUS_FORMAT_SHORT,
  92        STATUS_FORMAT_PORCELAIN,
  93} status_format = STATUS_FORMAT_LONG;
  94
  95static int opt_parse_m(const struct option *opt, const char *arg, int unset)
  96{
  97        struct strbuf *buf = opt->value;
  98        if (unset)
  99                strbuf_setlen(buf, 0);
 100        else {
 101                strbuf_addstr(buf, arg);
 102                strbuf_addstr(buf, "\n\n");
 103        }
 104        return 0;
 105}
 106
 107static struct option builtin_commit_options[] = {
 108        OPT__QUIET(&quiet),
 109        OPT__VERBOSE(&verbose),
 110
 111        OPT_GROUP("Commit message options"),
 112        OPT_FILENAME('F', "file", &logfile, "read log from file"),
 113        OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
 114        OPT_STRING(0, "date", &force_date, "DATE", "override date for commit"),
 115        OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
 116        OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit"),
 117        OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
 118        OPT_BOOLEAN(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"),
 119        OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
 120        OPT_FILENAME('t', "template", &template_file, "use specified template file"),
 121        OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
 122        OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
 123        OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
 124        /* end commit message options */
 125
 126        OPT_GROUP("Commit contents options"),
 127        OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
 128        OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
 129        OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
 130        OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
 131        OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
 132        OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
 133        OPT_SET_INT(0, "short", &status_format, "show status concisely",
 134                    STATUS_FORMAT_SHORT),
 135        OPT_SET_INT(0, "porcelain", &status_format,
 136                    "show porcelain output format", STATUS_FORMAT_PORCELAIN),
 137        OPT_BOOLEAN('z', "null", &null_termination,
 138                    "terminate entries with NUL"),
 139        OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
 140        { 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" },
 141        OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
 142        /* end commit contents options */
 143
 144        OPT_END()
 145};
 146
 147static void rollback_index_files(void)
 148{
 149        switch (commit_style) {
 150        case COMMIT_AS_IS:
 151                break; /* nothing to do */
 152        case COMMIT_NORMAL:
 153                rollback_lock_file(&index_lock);
 154                break;
 155        case COMMIT_PARTIAL:
 156                rollback_lock_file(&index_lock);
 157                rollback_lock_file(&false_lock);
 158                break;
 159        }
 160}
 161
 162static int commit_index_files(void)
 163{
 164        int err = 0;
 165
 166        switch (commit_style) {
 167        case COMMIT_AS_IS:
 168                break; /* nothing to do */
 169        case COMMIT_NORMAL:
 170                err = commit_lock_file(&index_lock);
 171                break;
 172        case COMMIT_PARTIAL:
 173                err = commit_lock_file(&index_lock);
 174                rollback_lock_file(&false_lock);
 175                break;
 176        }
 177
 178        return err;
 179}
 180
 181/*
 182 * Take a union of paths in the index and the named tree (typically, "HEAD"),
 183 * and return the paths that match the given pattern in list.
 184 */
 185static int list_paths(struct string_list *list, const char *with_tree,
 186                      const char *prefix, const char **pattern)
 187{
 188        int i;
 189        char *m;
 190
 191        for (i = 0; pattern[i]; i++)
 192                ;
 193        m = xcalloc(1, i);
 194
 195        if (with_tree)
 196                overlay_tree_on_cache(with_tree, prefix);
 197
 198        for (i = 0; i < active_nr; i++) {
 199                struct cache_entry *ce = active_cache[i];
 200                struct string_list_item *item;
 201
 202                if (ce->ce_flags & CE_UPDATE)
 203                        continue;
 204                if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
 205                        continue;
 206                item = string_list_insert(ce->name, list);
 207                if (ce_skip_worktree(ce))
 208                        item->util = item; /* better a valid pointer than a fake one */
 209        }
 210
 211        return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
 212}
 213
 214static void add_remove_files(struct string_list *list)
 215{
 216        int i;
 217        for (i = 0; i < list->nr; i++) {
 218                struct stat st;
 219                struct string_list_item *p = &(list->items[i]);
 220
 221                /* p->util is skip-worktree */
 222                if (p->util)
 223                        continue;
 224
 225                if (!lstat(p->string, &st)) {
 226                        if (add_to_cache(p->string, &st, 0))
 227                                die("updating files failed");
 228                } else
 229                        remove_file_from_cache(p->string);
 230        }
 231}
 232
 233static void create_base_index(void)
 234{
 235        struct tree *tree;
 236        struct unpack_trees_options opts;
 237        struct tree_desc t;
 238
 239        if (initial_commit) {
 240                discard_cache();
 241                return;
 242        }
 243
 244        memset(&opts, 0, sizeof(opts));
 245        opts.head_idx = 1;
 246        opts.index_only = 1;
 247        opts.merge = 1;
 248        opts.src_index = &the_index;
 249        opts.dst_index = &the_index;
 250
 251        opts.fn = oneway_merge;
 252        tree = parse_tree_indirect(head_sha1);
 253        if (!tree)
 254                die("failed to unpack HEAD tree object");
 255        parse_tree(tree);
 256        init_tree_desc(&t, tree->buffer, tree->size);
 257        if (unpack_trees(1, &t, &opts))
 258                exit(128); /* We've already reported the error, finish dying */
 259}
 260
 261static char *prepare_index(int argc, const char **argv, const char *prefix, int is_status)
 262{
 263        int fd;
 264        struct string_list partial;
 265        const char **pathspec = NULL;
 266        int refresh_flags = REFRESH_QUIET;
 267
 268        if (is_status)
 269                refresh_flags |= REFRESH_UNMERGED;
 270        if (interactive) {
 271                if (interactive_add(argc, argv, prefix) != 0)
 272                        die("interactive add failed");
 273                if (read_cache_preload(NULL) < 0)
 274                        die("index file corrupt");
 275                commit_style = COMMIT_AS_IS;
 276                return get_index_file();
 277        }
 278
 279        if (*argv)
 280                pathspec = get_pathspec(prefix, argv);
 281
 282        if (read_cache_preload(pathspec) < 0)
 283                die("index file corrupt");
 284
 285        /*
 286         * Non partial, non as-is commit.
 287         *
 288         * (1) get the real index;
 289         * (2) update the_index as necessary;
 290         * (3) write the_index out to the real index (still locked);
 291         * (4) return the name of the locked index file.
 292         *
 293         * The caller should run hooks on the locked real index, and
 294         * (A) if all goes well, commit the real index;
 295         * (B) on failure, rollback the real index.
 296         */
 297        if (all || (also && pathspec && *pathspec)) {
 298                int fd = hold_locked_index(&index_lock, 1);
 299                add_files_to_cache(also ? prefix : NULL, pathspec, 0);
 300                refresh_cache(refresh_flags);
 301                if (write_cache(fd, active_cache, active_nr) ||
 302                    close_lock_file(&index_lock))
 303                        die("unable to write new_index file");
 304                commit_style = COMMIT_NORMAL;
 305                return index_lock.filename;
 306        }
 307
 308        /*
 309         * As-is commit.
 310         *
 311         * (1) return the name of the real index file.
 312         *
 313         * The caller should run hooks on the real index, and run
 314         * hooks on the real index, and create commit from the_index.
 315         * We still need to refresh the index here.
 316         */
 317        if (!pathspec || !*pathspec) {
 318                fd = hold_locked_index(&index_lock, 1);
 319                refresh_cache(refresh_flags);
 320                if (write_cache(fd, active_cache, active_nr) ||
 321                    commit_locked_index(&index_lock))
 322                        die("unable to write new_index file");
 323                commit_style = COMMIT_AS_IS;
 324                return get_index_file();
 325        }
 326
 327        /*
 328         * A partial commit.
 329         *
 330         * (0) find the set of affected paths;
 331         * (1) get lock on the real index file;
 332         * (2) update the_index with the given paths;
 333         * (3) write the_index out to the real index (still locked);
 334         * (4) get lock on the false index file;
 335         * (5) reset the_index from HEAD;
 336         * (6) update the_index the same way as (2);
 337         * (7) write the_index out to the false index file;
 338         * (8) return the name of the false index file (still locked);
 339         *
 340         * The caller should run hooks on the locked false index, and
 341         * create commit from it.  Then
 342         * (A) if all goes well, commit the real index;
 343         * (B) on failure, rollback the real index;
 344         * In either case, rollback the false index.
 345         */
 346        commit_style = COMMIT_PARTIAL;
 347
 348        if (in_merge)
 349                die("cannot do a partial commit during a merge.");
 350
 351        memset(&partial, 0, sizeof(partial));
 352        partial.strdup_strings = 1;
 353        if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
 354                exit(1);
 355
 356        discard_cache();
 357        if (read_cache() < 0)
 358                die("cannot read the index");
 359
 360        fd = hold_locked_index(&index_lock, 1);
 361        add_remove_files(&partial);
 362        refresh_cache(REFRESH_QUIET);
 363        if (write_cache(fd, active_cache, active_nr) ||
 364            close_lock_file(&index_lock))
 365                die("unable to write new_index file");
 366
 367        fd = hold_lock_file_for_update(&false_lock,
 368                                       git_path("next-index-%"PRIuMAX,
 369                                                (uintmax_t) getpid()),
 370                                       LOCK_DIE_ON_ERROR);
 371
 372        create_base_index();
 373        add_remove_files(&partial);
 374        refresh_cache(REFRESH_QUIET);
 375
 376        if (write_cache(fd, active_cache, active_nr) ||
 377            close_lock_file(&false_lock))
 378                die("unable to write temporary index file");
 379
 380        discard_cache();
 381        read_cache_from(false_lock.filename);
 382
 383        return false_lock.filename;
 384}
 385
 386static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
 387                      struct wt_status *s)
 388{
 389        unsigned char sha1[20];
 390
 391        if (s->relative_paths)
 392                s->prefix = prefix;
 393
 394        if (amend) {
 395                s->amend = 1;
 396                s->reference = "HEAD^1";
 397        }
 398        s->verbose = verbose;
 399        s->index_file = index_file;
 400        s->fp = fp;
 401        s->nowarn = nowarn;
 402        s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
 403
 404        wt_status_collect(s);
 405
 406        switch (status_format) {
 407        case STATUS_FORMAT_SHORT:
 408                wt_shortstatus_print(s, null_termination);
 409                break;
 410        case STATUS_FORMAT_PORCELAIN:
 411                wt_porcelain_print(s, null_termination);
 412                break;
 413        case STATUS_FORMAT_LONG:
 414                wt_status_print(s);
 415                break;
 416        }
 417
 418        return s->commitable;
 419}
 420
 421static int is_a_merge(const unsigned char *sha1)
 422{
 423        struct commit *commit = lookup_commit(sha1);
 424        if (!commit || parse_commit(commit))
 425                die("could not parse HEAD commit");
 426        return !!(commit->parents && commit->parents->next);
 427}
 428
 429static const char sign_off_header[] = "Signed-off-by: ";
 430
 431static void determine_author_info(void)
 432{
 433        char *name, *email, *date;
 434
 435        name = getenv("GIT_AUTHOR_NAME");
 436        email = getenv("GIT_AUTHOR_EMAIL");
 437        date = getenv("GIT_AUTHOR_DATE");
 438
 439        if (use_message && !renew_authorship) {
 440                const char *a, *lb, *rb, *eol;
 441
 442                a = strstr(use_message_buffer, "\nauthor ");
 443                if (!a)
 444                        die("invalid commit: %s", use_message);
 445
 446                lb = strstr(a + 8, " <");
 447                rb = strstr(a + 8, "> ");
 448                eol = strchr(a + 8, '\n');
 449                if (!lb || !rb || !eol)
 450                        die("invalid commit: %s", use_message);
 451
 452                name = xstrndup(a + 8, lb - (a + 8));
 453                email = xstrndup(lb + 2, rb - (lb + 2));
 454                date = xstrndup(rb + 2, eol - (rb + 2));
 455        }
 456
 457        if (force_author) {
 458                const char *lb = strstr(force_author, " <");
 459                const char *rb = strchr(force_author, '>');
 460
 461                if (!lb || !rb)
 462                        die("malformed --author parameter");
 463                name = xstrndup(force_author, lb - force_author);
 464                email = xstrndup(lb + 2, rb - (lb + 2));
 465        }
 466
 467        if (force_date)
 468                date = force_date;
 469
 470        author_name = name;
 471        author_email = email;
 472        author_date = date;
 473}
 474
 475static int ends_rfc2822_footer(struct strbuf *sb)
 476{
 477        int ch;
 478        int hit = 0;
 479        int i, j, k;
 480        int len = sb->len;
 481        int first = 1;
 482        const char *buf = sb->buf;
 483
 484        for (i = len - 1; i > 0; i--) {
 485                if (hit && buf[i] == '\n')
 486                        break;
 487                hit = (buf[i] == '\n');
 488        }
 489
 490        while (i < len - 1 && buf[i] == '\n')
 491                i++;
 492
 493        for (; i < len; i = k) {
 494                for (k = i; k < len && buf[k] != '\n'; k++)
 495                        ; /* do nothing */
 496                k++;
 497
 498                if ((buf[k] == ' ' || buf[k] == '\t') && !first)
 499                        continue;
 500
 501                first = 0;
 502
 503                for (j = 0; i + j < len; j++) {
 504                        ch = buf[i + j];
 505                        if (ch == ':')
 506                                break;
 507                        if (isalnum(ch) ||
 508                            (ch == '-'))
 509                                continue;
 510                        return 0;
 511                }
 512        }
 513        return 1;
 514}
 515
 516static int prepare_to_commit(const char *index_file, const char *prefix,
 517                             struct wt_status *s)
 518{
 519        struct stat statbuf;
 520        int commitable, saved_color_setting;
 521        struct strbuf sb = STRBUF_INIT;
 522        char *buffer;
 523        FILE *fp;
 524        const char *hook_arg1 = NULL;
 525        const char *hook_arg2 = NULL;
 526        int ident_shown = 0;
 527
 528        if (!no_verify && run_hook(index_file, "pre-commit", NULL))
 529                return 0;
 530
 531        if (message.len) {
 532                strbuf_addbuf(&sb, &message);
 533                hook_arg1 = "message";
 534        } else if (logfile && !strcmp(logfile, "-")) {
 535                if (isatty(0))
 536                        fprintf(stderr, "(reading log message from standard input)\n");
 537                if (strbuf_read(&sb, 0, 0) < 0)
 538                        die_errno("could not read log from standard input");
 539                hook_arg1 = "message";
 540        } else if (logfile) {
 541                if (strbuf_read_file(&sb, logfile, 0) < 0)
 542                        die_errno("could not read log file '%s'",
 543                                  logfile);
 544                hook_arg1 = "message";
 545        } else if (use_message) {
 546                buffer = strstr(use_message_buffer, "\n\n");
 547                if (!buffer || buffer[2] == '\0')
 548                        die("commit has empty message");
 549                strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
 550                hook_arg1 = "commit";
 551                hook_arg2 = use_message;
 552        } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
 553                if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
 554                        die_errno("could not read MERGE_MSG");
 555                hook_arg1 = "merge";
 556        } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
 557                if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
 558                        die_errno("could not read SQUASH_MSG");
 559                hook_arg1 = "squash";
 560        } else if (template_file && !stat(template_file, &statbuf)) {
 561                if (strbuf_read_file(&sb, template_file, 0) < 0)
 562                        die_errno("could not read '%s'", template_file);
 563                hook_arg1 = "template";
 564        }
 565
 566        /*
 567         * This final case does not modify the template message,
 568         * it just sets the argument to the prepare-commit-msg hook.
 569         */
 570        else if (in_merge)
 571                hook_arg1 = "merge";
 572
 573        fp = fopen(git_path(commit_editmsg), "w");
 574        if (fp == NULL)
 575                die_errno("could not open '%s'", git_path(commit_editmsg));
 576
 577        if (cleanup_mode != CLEANUP_NONE)
 578                stripspace(&sb, 0);
 579
 580        if (signoff) {
 581                struct strbuf sob = STRBUF_INIT;
 582                int i;
 583
 584                strbuf_addstr(&sob, sign_off_header);
 585                strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
 586                                             getenv("GIT_COMMITTER_EMAIL")));
 587                strbuf_addch(&sob, '\n');
 588                for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
 589                        ; /* do nothing */
 590                if (prefixcmp(sb.buf + i, sob.buf)) {
 591                        if (!i || !ends_rfc2822_footer(&sb))
 592                                strbuf_addch(&sb, '\n');
 593                        strbuf_addbuf(&sb, &sob);
 594                }
 595                strbuf_release(&sob);
 596        }
 597
 598        if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
 599                die_errno("could not write commit template");
 600
 601        strbuf_release(&sb);
 602
 603        determine_author_info();
 604
 605        /* This checks if committer ident is explicitly given */
 606        git_committer_info(0);
 607        if (use_editor && include_status) {
 608                char *author_ident;
 609                const char *committer_ident;
 610
 611                if (in_merge)
 612                        fprintf(fp,
 613                                "#\n"
 614                                "# It looks like you may be committing a MERGE.\n"
 615                                "# If this is not correct, please remove the file\n"
 616                                "#      %s\n"
 617                                "# and try again.\n"
 618                                "#\n",
 619                                git_path("MERGE_HEAD"));
 620
 621                fprintf(fp,
 622                        "\n"
 623                        "# Please enter the commit message for your changes.");
 624                if (cleanup_mode == CLEANUP_ALL)
 625                        fprintf(fp,
 626                                " Lines starting\n"
 627                                "# with '#' will be ignored, and an empty"
 628                                " message aborts the commit.\n");
 629                else /* CLEANUP_SPACE, that is. */
 630                        fprintf(fp,
 631                                " Lines starting\n"
 632                                "# with '#' will be kept; you may remove them"
 633                                " yourself if you want to.\n"
 634                                "# An empty message aborts the commit.\n");
 635                if (only_include_assumed)
 636                        fprintf(fp, "# %s\n", only_include_assumed);
 637
 638                author_ident = xstrdup(fmt_name(author_name, author_email));
 639                committer_ident = fmt_name(getenv("GIT_COMMITTER_NAME"),
 640                                           getenv("GIT_COMMITTER_EMAIL"));
 641                if (strcmp(author_ident, committer_ident))
 642                        fprintf(fp,
 643                                "%s"
 644                                "# Author:    %s\n",
 645                                ident_shown++ ? "" : "#\n",
 646                                author_ident);
 647                free(author_ident);
 648
 649                if (!user_ident_sufficiently_given())
 650                        fprintf(fp,
 651                                "%s"
 652                                "# Committer: %s\n",
 653                                ident_shown++ ? "" : "#\n",
 654                                committer_ident);
 655
 656                if (ident_shown)
 657                        fprintf(fp, "#\n");
 658
 659                saved_color_setting = s->use_color;
 660                s->use_color = 0;
 661                commitable = run_status(fp, index_file, prefix, 1, s);
 662                s->use_color = saved_color_setting;
 663        } else {
 664                unsigned char sha1[20];
 665                const char *parent = "HEAD";
 666
 667                if (!active_nr && read_cache() < 0)
 668                        die("Cannot read index");
 669
 670                if (amend)
 671                        parent = "HEAD^1";
 672
 673                if (get_sha1(parent, sha1))
 674                        commitable = !!active_nr;
 675                else
 676                        commitable = index_differs_from(parent, 0);
 677        }
 678
 679        fclose(fp);
 680
 681        if (!commitable && !in_merge && !allow_empty &&
 682            !(amend && is_a_merge(head_sha1))) {
 683                run_status(stdout, index_file, prefix, 0, s);
 684                return 0;
 685        }
 686
 687        /*
 688         * Re-read the index as pre-commit hook could have updated it,
 689         * and write it out as a tree.  We must do this before we invoke
 690         * the editor and after we invoke run_status above.
 691         */
 692        discard_cache();
 693        read_cache_from(index_file);
 694        if (!active_cache_tree)
 695                active_cache_tree = cache_tree();
 696        if (cache_tree_update(active_cache_tree,
 697                              active_cache, active_nr, 0, 0) < 0) {
 698                error("Error building trees");
 699                return 0;
 700        }
 701
 702        if (run_hook(index_file, "prepare-commit-msg",
 703                     git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
 704                return 0;
 705
 706        if (use_editor) {
 707                char index[PATH_MAX];
 708                const char *env[2] = { index, NULL };
 709                snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
 710                if (launch_editor(git_path(commit_editmsg), NULL, env)) {
 711                        fprintf(stderr,
 712                        "Please supply the message using either -m or -F option.\n");
 713                        exit(1);
 714                }
 715        }
 716
 717        if (!no_verify &&
 718            run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
 719                return 0;
 720        }
 721
 722        return 1;
 723}
 724
 725/*
 726 * Find out if the message in the strbuf contains only whitespace and
 727 * Signed-off-by lines.
 728 */
 729static int message_is_empty(struct strbuf *sb)
 730{
 731        struct strbuf tmpl = STRBUF_INIT;
 732        const char *nl;
 733        int eol, i, start = 0;
 734
 735        if (cleanup_mode == CLEANUP_NONE && sb->len)
 736                return 0;
 737
 738        /* See if the template is just a prefix of the message. */
 739        if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
 740                stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
 741                if (start + tmpl.len <= sb->len &&
 742                    memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
 743                        start += tmpl.len;
 744        }
 745        strbuf_release(&tmpl);
 746
 747        /* Check if the rest is just whitespace and Signed-of-by's. */
 748        for (i = start; i < sb->len; i++) {
 749                nl = memchr(sb->buf + i, '\n', sb->len - i);
 750                if (nl)
 751                        eol = nl - sb->buf;
 752                else
 753                        eol = sb->len;
 754
 755                if (strlen(sign_off_header) <= eol - i &&
 756                    !prefixcmp(sb->buf + i, sign_off_header)) {
 757                        i = eol;
 758                        continue;
 759                }
 760                while (i < eol)
 761                        if (!isspace(sb->buf[i++]))
 762                                return 0;
 763        }
 764
 765        return 1;
 766}
 767
 768static const char *find_author_by_nickname(const char *name)
 769{
 770        struct rev_info revs;
 771        struct commit *commit;
 772        struct strbuf buf = STRBUF_INIT;
 773        const char *av[20];
 774        int ac = 0;
 775
 776        init_revisions(&revs, NULL);
 777        strbuf_addf(&buf, "--author=%s", name);
 778        av[++ac] = "--all";
 779        av[++ac] = "-i";
 780        av[++ac] = buf.buf;
 781        av[++ac] = NULL;
 782        setup_revisions(ac, av, &revs, NULL);
 783        prepare_revision_walk(&revs);
 784        commit = get_revision(&revs);
 785        if (commit) {
 786                struct pretty_print_context ctx = {0};
 787                ctx.date_mode = DATE_NORMAL;
 788                strbuf_release(&buf);
 789                format_commit_message(commit, "%an <%ae>", &buf, &ctx);
 790                return strbuf_detach(&buf, NULL);
 791        }
 792        die("No existing author found with '%s'", name);
 793}
 794
 795
 796static void handle_untracked_files_arg(struct wt_status *s)
 797{
 798        if (!untracked_files_arg)
 799                ; /* default already initialized */
 800        else if (!strcmp(untracked_files_arg, "no"))
 801                s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
 802        else if (!strcmp(untracked_files_arg, "normal"))
 803                s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
 804        else if (!strcmp(untracked_files_arg, "all"))
 805                s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
 806        else
 807                die("Invalid untracked files mode '%s'", untracked_files_arg);
 808}
 809
 810static int parse_and_validate_options(int argc, const char *argv[],
 811                                      const char * const usage[],
 812                                      const char *prefix,
 813                                      struct wt_status *s)
 814{
 815        int f = 0;
 816
 817        argc = parse_options(argc, argv, prefix, builtin_commit_options, usage,
 818                             0);
 819
 820        if (force_author && !strchr(force_author, '>'))
 821                force_author = find_author_by_nickname(force_author);
 822
 823        if (force_author && renew_authorship)
 824                die("Using both --reset-author and --author does not make sense");
 825
 826        if (logfile || message.len || use_message)
 827                use_editor = 0;
 828        if (edit_flag)
 829                use_editor = 1;
 830        if (!use_editor)
 831                setenv("GIT_EDITOR", ":", 1);
 832
 833        if (get_sha1("HEAD", head_sha1))
 834                initial_commit = 1;
 835
 836        /* Sanity check options */
 837        if (amend && initial_commit)
 838                die("You have nothing to amend.");
 839        if (amend && in_merge)
 840                die("You are in the middle of a merge -- cannot amend.");
 841
 842        if (use_message)
 843                f++;
 844        if (edit_message)
 845                f++;
 846        if (logfile)
 847                f++;
 848        if (f > 1)
 849                die("Only one of -c/-C/-F can be used.");
 850        if (message.len && f > 0)
 851                die("Option -m cannot be combined with -c/-C/-F.");
 852        if (edit_message)
 853                use_message = edit_message;
 854        if (amend && !use_message)
 855                use_message = "HEAD";
 856        if (!use_message && renew_authorship)
 857                die("--reset-author can be used only with -C, -c or --amend.");
 858        if (use_message) {
 859                unsigned char sha1[20];
 860                static char utf8[] = "UTF-8";
 861                const char *out_enc;
 862                char *enc, *end;
 863                struct commit *commit;
 864
 865                if (get_sha1(use_message, sha1))
 866                        die("could not lookup commit %s", use_message);
 867                commit = lookup_commit_reference(sha1);
 868                if (!commit || parse_commit(commit))
 869                        die("could not parse commit %s", use_message);
 870
 871                enc = strstr(commit->buffer, "\nencoding");
 872                if (enc) {
 873                        end = strchr(enc + 10, '\n');
 874                        enc = xstrndup(enc + 10, end - (enc + 10));
 875                } else {
 876                        enc = utf8;
 877                }
 878                out_enc = git_commit_encoding ? git_commit_encoding : utf8;
 879
 880                if (strcmp(out_enc, enc))
 881                        use_message_buffer =
 882                                reencode_string(commit->buffer, out_enc, enc);
 883
 884                /*
 885                 * If we failed to reencode the buffer, just copy it
 886                 * byte for byte so the user can try to fix it up.
 887                 * This also handles the case where input and output
 888                 * encodings are identical.
 889                 */
 890                if (use_message_buffer == NULL)
 891                        use_message_buffer = xstrdup(commit->buffer);
 892                if (enc != utf8)
 893                        free(enc);
 894        }
 895
 896        if (!!also + !!only + !!all + !!interactive > 1)
 897                die("Only one of --include/--only/--all/--interactive can be used.");
 898        if (argc == 0 && (also || (only && !amend)))
 899                die("No paths with --include/--only does not make sense.");
 900        if (argc == 0 && only && amend)
 901                only_include_assumed = "Clever... amending the last one with dirty index.";
 902        if (argc > 0 && !also && !only)
 903                only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
 904        if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
 905                cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
 906        else if (!strcmp(cleanup_arg, "verbatim"))
 907                cleanup_mode = CLEANUP_NONE;
 908        else if (!strcmp(cleanup_arg, "whitespace"))
 909                cleanup_mode = CLEANUP_SPACE;
 910        else if (!strcmp(cleanup_arg, "strip"))
 911                cleanup_mode = CLEANUP_ALL;
 912        else
 913                die("Invalid cleanup mode %s", cleanup_arg);
 914
 915        handle_untracked_files_arg(s);
 916
 917        if (all && argc > 0)
 918                die("Paths with -a does not make sense.");
 919        else if (interactive && argc > 0)
 920                die("Paths with --interactive does not make sense.");
 921
 922        if (null_termination && status_format == STATUS_FORMAT_LONG)
 923                status_format = STATUS_FORMAT_PORCELAIN;
 924        if (status_format != STATUS_FORMAT_LONG)
 925                dry_run = 1;
 926
 927        return argc;
 928}
 929
 930static int dry_run_commit(int argc, const char **argv, const char *prefix,
 931                          struct wt_status *s)
 932{
 933        int commitable;
 934        const char *index_file;
 935
 936        index_file = prepare_index(argc, argv, prefix, 1);
 937        commitable = run_status(stdout, index_file, prefix, 0, s);
 938        rollback_index_files();
 939
 940        return commitable ? 0 : 1;
 941}
 942
 943static int parse_status_slot(const char *var, int offset)
 944{
 945        if (!strcasecmp(var+offset, "header"))
 946                return WT_STATUS_HEADER;
 947        if (!strcasecmp(var+offset, "updated")
 948                || !strcasecmp(var+offset, "added"))
 949                return WT_STATUS_UPDATED;
 950        if (!strcasecmp(var+offset, "changed"))
 951                return WT_STATUS_CHANGED;
 952        if (!strcasecmp(var+offset, "untracked"))
 953                return WT_STATUS_UNTRACKED;
 954        if (!strcasecmp(var+offset, "nobranch"))
 955                return WT_STATUS_NOBRANCH;
 956        if (!strcasecmp(var+offset, "unmerged"))
 957                return WT_STATUS_UNMERGED;
 958        return -1;
 959}
 960
 961static int git_status_config(const char *k, const char *v, void *cb)
 962{
 963        struct wt_status *s = cb;
 964
 965        if (!strcmp(k, "status.submodulesummary")) {
 966                int is_bool;
 967                s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
 968                if (is_bool && s->submodule_summary)
 969                        s->submodule_summary = -1;
 970                return 0;
 971        }
 972        if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
 973                s->use_color = git_config_colorbool(k, v, -1);
 974                return 0;
 975        }
 976        if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
 977                int slot = parse_status_slot(k, 13);
 978                if (slot < 0)
 979                        return 0;
 980                if (!v)
 981                        return config_error_nonbool(k);
 982                color_parse(v, k, s->color_palette[slot]);
 983                return 0;
 984        }
 985        if (!strcmp(k, "status.relativepaths")) {
 986                s->relative_paths = git_config_bool(k, v);
 987                return 0;
 988        }
 989        if (!strcmp(k, "status.showuntrackedfiles")) {
 990                if (!v)
 991                        return config_error_nonbool(k);
 992                else if (!strcmp(v, "no"))
 993                        s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
 994                else if (!strcmp(v, "normal"))
 995                        s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
 996                else if (!strcmp(v, "all"))
 997                        s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
 998                else
 999                        return error("Invalid untracked files mode '%s'", v);
1000                return 0;
1001        }
1002        return git_diff_ui_config(k, v, NULL);
1003}
1004
1005int cmd_status(int argc, const char **argv, const char *prefix)
1006{
1007        struct wt_status s;
1008        unsigned char sha1[20];
1009        static struct option builtin_status_options[] = {
1010                OPT__VERBOSE(&verbose),
1011                OPT_SET_INT('s', "short", &status_format,
1012                            "show status concisely", STATUS_FORMAT_SHORT),
1013                OPT_SET_INT(0, "porcelain", &status_format,
1014                            "show porcelain output format",
1015                            STATUS_FORMAT_PORCELAIN),
1016                OPT_BOOLEAN('z', "null", &null_termination,
1017                            "terminate entries with NUL"),
1018                { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1019                  "mode",
1020                  "show untracked files, optional modes: all, normal, no. (Default: all)",
1021                  PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1022                OPT_END(),
1023        };
1024
1025        if (null_termination && status_format == STATUS_FORMAT_LONG)
1026                status_format = STATUS_FORMAT_PORCELAIN;
1027
1028        wt_status_prepare(&s);
1029        git_config(git_status_config, &s);
1030        in_merge = file_exists(git_path("MERGE_HEAD"));
1031        argc = parse_options(argc, argv, prefix,
1032                             builtin_status_options,
1033                             builtin_status_usage, 0);
1034        handle_untracked_files_arg(&s);
1035
1036        if (*argv)
1037                s.pathspec = get_pathspec(prefix, argv);
1038
1039        read_cache();
1040        refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
1041        s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
1042        s.in_merge = in_merge;
1043        wt_status_collect(&s);
1044
1045        if (s.relative_paths)
1046                s.prefix = prefix;
1047        if (s.use_color == -1)
1048                s.use_color = git_use_color_default;
1049        if (diff_use_color_default == -1)
1050                diff_use_color_default = git_use_color_default;
1051
1052        switch (status_format) {
1053        case STATUS_FORMAT_SHORT:
1054                wt_shortstatus_print(&s, null_termination);
1055                break;
1056        case STATUS_FORMAT_PORCELAIN:
1057                wt_porcelain_print(&s, null_termination);
1058                break;
1059        case STATUS_FORMAT_LONG:
1060                s.verbose = verbose;
1061                wt_status_print(&s);
1062                break;
1063        }
1064        return 0;
1065}
1066
1067static void print_summary(const char *prefix, const unsigned char *sha1)
1068{
1069        struct rev_info rev;
1070        struct commit *commit;
1071        struct strbuf format = STRBUF_INIT;
1072        unsigned char junk_sha1[20];
1073        const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
1074        struct pretty_print_context pctx = {0};
1075        struct strbuf author_ident = STRBUF_INIT;
1076        struct strbuf committer_ident = STRBUF_INIT;
1077
1078        commit = lookup_commit(sha1);
1079        if (!commit)
1080                die("couldn't look up newly created commit");
1081        if (!commit || parse_commit(commit))
1082                die("could not parse newly created commit");
1083
1084        strbuf_addstr(&format, "format:%h] %s");
1085
1086        format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
1087        format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
1088        if (strbuf_cmp(&author_ident, &committer_ident)) {
1089                strbuf_addstr(&format, "\n Author: ");
1090                strbuf_addbuf_percentquote(&format, &author_ident);
1091        }
1092        if (!user_ident_sufficiently_given()) {
1093                strbuf_addstr(&format, "\n Committer: ");
1094                strbuf_addbuf_percentquote(&format, &committer_ident);
1095                if (advice_implicit_identity) {
1096                        strbuf_addch(&format, '\n');
1097                        strbuf_addstr(&format, implicit_ident_advice);
1098                }
1099        }
1100        strbuf_release(&author_ident);
1101        strbuf_release(&committer_ident);
1102
1103        init_revisions(&rev, prefix);
1104        setup_revisions(0, NULL, &rev, NULL);
1105
1106        rev.abbrev = 0;
1107        rev.diff = 1;
1108        rev.diffopt.output_format =
1109                DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1110
1111        rev.verbose_header = 1;
1112        rev.show_root_diff = 1;
1113        get_commit_format(format.buf, &rev);
1114        rev.always_show_header = 0;
1115        rev.diffopt.detect_rename = 1;
1116        rev.diffopt.rename_limit = 100;
1117        rev.diffopt.break_opt = 0;
1118        diff_setup_done(&rev.diffopt);
1119
1120        printf("[%s%s ",
1121                !prefixcmp(head, "refs/heads/") ?
1122                        head + 11 :
1123                        !strcmp(head, "HEAD") ?
1124                                "detached HEAD" :
1125                                head,
1126                initial_commit ? " (root-commit)" : "");
1127
1128        if (!log_tree_commit(&rev, commit)) {
1129                struct pretty_print_context ctx = {0};
1130                struct strbuf buf = STRBUF_INIT;
1131                ctx.date_mode = DATE_NORMAL;
1132                format_commit_message(commit, format.buf + 7, &buf, &ctx);
1133                printf("%s\n", buf.buf);
1134                strbuf_release(&buf);
1135        }
1136        strbuf_release(&format);
1137}
1138
1139static int git_commit_config(const char *k, const char *v, void *cb)
1140{
1141        struct wt_status *s = cb;
1142
1143        if (!strcmp(k, "commit.template"))
1144                return git_config_pathname(&template_file, k, v);
1145        if (!strcmp(k, "commit.status")) {
1146                include_status = git_config_bool(k, v);
1147                return 0;
1148        }
1149
1150        return git_status_config(k, v, s);
1151}
1152
1153int cmd_commit(int argc, const char **argv, const char *prefix)
1154{
1155        struct strbuf sb = STRBUF_INIT;
1156        const char *index_file, *reflog_msg;
1157        char *nl, *p;
1158        unsigned char commit_sha1[20];
1159        struct ref_lock *ref_lock;
1160        struct commit_list *parents = NULL, **pptr = &parents;
1161        struct stat statbuf;
1162        int allow_fast_forward = 1;
1163        struct wt_status s;
1164
1165        wt_status_prepare(&s);
1166        git_config(git_commit_config, &s);
1167        in_merge = file_exists(git_path("MERGE_HEAD"));
1168        s.in_merge = in_merge;
1169
1170        if (s.use_color == -1)
1171                s.use_color = git_use_color_default;
1172        argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
1173                                          prefix, &s);
1174        if (dry_run) {
1175                if (diff_use_color_default == -1)
1176                        diff_use_color_default = git_use_color_default;
1177                return dry_run_commit(argc, argv, prefix, &s);
1178        }
1179        index_file = prepare_index(argc, argv, prefix, 0);
1180
1181        /* Set up everything for writing the commit object.  This includes
1182           running hooks, writing the trees, and interacting with the user.  */
1183        if (!prepare_to_commit(index_file, prefix, &s)) {
1184                rollback_index_files();
1185                return 1;
1186        }
1187
1188        /* Determine parents */
1189        if (initial_commit) {
1190                reflog_msg = "commit (initial)";
1191        } else if (amend) {
1192                struct commit_list *c;
1193                struct commit *commit;
1194
1195                reflog_msg = "commit (amend)";
1196                commit = lookup_commit(head_sha1);
1197                if (!commit || parse_commit(commit))
1198                        die("could not parse HEAD commit");
1199
1200                for (c = commit->parents; c; c = c->next)
1201                        pptr = &commit_list_insert(c->item, pptr)->next;
1202        } else if (in_merge) {
1203                struct strbuf m = STRBUF_INIT;
1204                FILE *fp;
1205
1206                reflog_msg = "commit (merge)";
1207                pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
1208                fp = fopen(git_path("MERGE_HEAD"), "r");
1209                if (fp == NULL)
1210                        die_errno("could not open '%s' for reading",
1211                                  git_path("MERGE_HEAD"));
1212                while (strbuf_getline(&m, fp, '\n') != EOF) {
1213                        unsigned char sha1[20];
1214                        if (get_sha1_hex(m.buf, sha1) < 0)
1215                                die("Corrupt MERGE_HEAD file (%s)", m.buf);
1216                        pptr = &commit_list_insert(lookup_commit(sha1), pptr)->next;
1217                }
1218                fclose(fp);
1219                strbuf_release(&m);
1220                if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1221                        if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
1222                                die_errno("could not read MERGE_MODE");
1223                        if (!strcmp(sb.buf, "no-ff"))
1224                                allow_fast_forward = 0;
1225                }
1226                if (allow_fast_forward)
1227                        parents = reduce_heads(parents);
1228        } else {
1229                reflog_msg = "commit";
1230                pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
1231        }
1232
1233        /* Finally, get the commit message */
1234        strbuf_reset(&sb);
1235        if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
1236                int saved_errno = errno;
1237                rollback_index_files();
1238                die("could not read commit message: %s", strerror(saved_errno));
1239        }
1240
1241        /* Truncate the message just before the diff, if any. */
1242        if (verbose) {
1243                p = strstr(sb.buf, "\ndiff --git ");
1244                if (p != NULL)
1245                        strbuf_setlen(&sb, p - sb.buf + 1);
1246        }
1247
1248        if (cleanup_mode != CLEANUP_NONE)
1249                stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1250        if (message_is_empty(&sb)) {
1251                rollback_index_files();
1252                fprintf(stderr, "Aborting commit due to empty commit message.\n");
1253                exit(1);
1254        }
1255
1256        if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1,
1257                        fmt_ident(author_name, author_email, author_date,
1258                                IDENT_ERROR_ON_NO_NAME))) {
1259                rollback_index_files();
1260                die("failed to write commit object");
1261        }
1262
1263        ref_lock = lock_any_ref_for_update("HEAD",
1264                                           initial_commit ? NULL : head_sha1,
1265                                           0);
1266
1267        nl = strchr(sb.buf, '\n');
1268        if (nl)
1269                strbuf_setlen(&sb, nl + 1 - sb.buf);
1270        else
1271                strbuf_addch(&sb, '\n');
1272        strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1273        strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
1274
1275        if (!ref_lock) {
1276                rollback_index_files();
1277                die("cannot lock HEAD ref");
1278        }
1279        if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
1280                rollback_index_files();
1281                die("cannot update HEAD ref");
1282        }
1283
1284        unlink(git_path("MERGE_HEAD"));
1285        unlink(git_path("MERGE_MSG"));
1286        unlink(git_path("MERGE_MODE"));
1287        unlink(git_path("SQUASH_MSG"));
1288
1289        if (commit_index_files())
1290                die ("Repository has been updated, but unable to write\n"
1291                     "new_index file. Check that disk is not full or quota is\n"
1292                     "not exceeded, and then \"git reset HEAD\" to recover.");
1293
1294        rerere(0);
1295        run_hook(get_index_file(), "post-commit", NULL);
1296        if (!quiet)
1297                print_summary(prefix, commit_sha1);
1298
1299        return 0;
1300}