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