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