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