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