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