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