]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-commit.c
Make default pre-commit hook less noisy
[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"
2888605c 10#include "dir.h"
f5bbc322
KH
11#include "builtin.h"
12#include "diff.h"
13#include "diffcore.h"
14#include "commit.h"
15#include "revision.h"
16#include "wt-status.h"
17#include "run-command.h"
18#include "refs.h"
19#include "log-tree.h"
20#include "strbuf.h"
21#include "utf8.h"
22#include "parse-options.h"
2888605c 23#include "path-list.h"
fa9dcf80 24#include "unpack-trees.h"
f5bbc322
KH
25
26static const char * const builtin_commit_usage[] = {
27 "git-commit [options] [--] <filepattern>...",
28 NULL
29};
30
2f02b25f
SB
31static const char * const builtin_status_usage[] = {
32 "git-status [options] [--] <filepattern>...",
33 NULL
34};
35
f5bbc322
KH
36static unsigned char head_sha1[20], merge_head_sha1[20];
37static char *use_message_buffer;
38static const char commit_editmsg[] = "COMMIT_EDITMSG";
2888605c
JH
39static struct lock_file index_lock; /* real index */
40static struct lock_file false_lock; /* used only for partial commits */
41static enum {
42 COMMIT_AS_IS = 1,
43 COMMIT_NORMAL,
44 COMMIT_PARTIAL,
45} commit_style;
f5bbc322 46
f9568530 47static char *logfile, *force_author, *template_file;
f5bbc322
KH
48static char *edit_message, *use_message;
49static int all, edit_flag, also, interactive, only, amend, signoff;
5241b6bf 50static int quiet, verbose, untracked_files, no_verify, allow_empty;
5f065737
AR
51/*
52 * The default commit message cleanup mode will remove the lines
53 * beginning with # (shell comments) and leading and trailing
54 * whitespaces (empty lines or containing only whitespaces)
55 * if editor is used, and only the whitespaces if the message
56 * is specified explicitly.
57 */
58static enum {
59 CLEANUP_SPACE,
60 CLEANUP_NONE,
61 CLEANUP_ALL,
62} cleanup_mode;
63static char *cleanup_arg;
f5bbc322 64
4803466f 65static int use_editor = 1, initial_commit, in_merge;
f5bbc322 66const char *only_include_assumed;
f9568530
JS
67struct strbuf message;
68
69static int opt_parse_m(const struct option *opt, const char *arg, int unset)
70{
71 struct strbuf *buf = opt->value;
72 if (unset)
73 strbuf_setlen(buf, 0);
74 else {
75 strbuf_addstr(buf, arg);
76 strbuf_addch(buf, '\n');
77 strbuf_addch(buf, '\n');
78 }
79 return 0;
80}
f5bbc322
KH
81
82static struct option builtin_commit_options[] = {
83 OPT__QUIET(&quiet),
84 OPT__VERBOSE(&verbose),
85 OPT_GROUP("Commit message options"),
86
87 OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
88 OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
f9568530 89 OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
f5bbc322
KH
90 OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "),
91 OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
92 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by: header"),
93 OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"),
94 OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
95
96 OPT_GROUP("Commit contents options"),
97 OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
98 OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
99 OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
100 OPT_BOOLEAN('o', "only", &only, ""),
101 OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
102 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
103 OPT_BOOLEAN(0, "untracked-files", &untracked_files, "show all untracked files"),
5241b6bf 104 OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
5f065737 105 OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
f5bbc322
KH
106
107 OPT_END()
108};
109
2888605c
JH
110static void rollback_index_files(void)
111{
112 switch (commit_style) {
113 case COMMIT_AS_IS:
114 break; /* nothing to do */
115 case COMMIT_NORMAL:
116 rollback_lock_file(&index_lock);
117 break;
118 case COMMIT_PARTIAL:
119 rollback_lock_file(&index_lock);
120 rollback_lock_file(&false_lock);
121 break;
122 }
123}
124
125static void commit_index_files(void)
126{
127 switch (commit_style) {
128 case COMMIT_AS_IS:
129 break; /* nothing to do */
130 case COMMIT_NORMAL:
131 commit_lock_file(&index_lock);
132 break;
133 case COMMIT_PARTIAL:
134 commit_lock_file(&index_lock);
135 rollback_lock_file(&false_lock);
136 break;
137 }
138}
139
140/*
141 * Take a union of paths in the index and the named tree (typically, "HEAD"),
142 * and return the paths that match the given pattern in list.
143 */
144static int list_paths(struct path_list *list, const char *with_tree,
145 const char *prefix, const char **pattern)
146{
147 int i;
148 char *m;
149
150 for (i = 0; pattern[i]; i++)
151 ;
152 m = xcalloc(1, i);
153
154 if (with_tree)
155 overlay_tree_on_cache(with_tree, prefix);
156
157 for (i = 0; i < active_nr; i++) {
158 struct cache_entry *ce = active_cache[i];
2888605c
JH
159 if (!pathspec_match(pattern, m, ce->name, 0))
160 continue;
161 path_list_insert(ce->name, list);
162 }
163
164 return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
165}
166
167static void add_remove_files(struct path_list *list)
168{
169 int i;
170 for (i = 0; i < list->nr; i++) {
171 struct path_list_item *p = &(list->items[i]);
172 if (file_exists(p->path))
173 add_file_to_cache(p->path, 0);
174 else
175 remove_file_from_cache(p->path);
176 }
177}
178
fa9dcf80
LT
179static void create_base_index(void)
180{
181 struct tree *tree;
182 struct unpack_trees_options opts;
183 struct tree_desc t;
184
185 if (initial_commit) {
186 discard_cache();
187 return;
188 }
189
190 memset(&opts, 0, sizeof(opts));
191 opts.head_idx = 1;
192 opts.index_only = 1;
193 opts.merge = 1;
194
195 opts.fn = oneway_merge;
196 tree = parse_tree_indirect(head_sha1);
197 if (!tree)
198 die("failed to unpack HEAD tree object");
199 parse_tree(tree);
200 init_tree_desc(&t, tree->buffer, tree->size);
201 unpack_trees(1, &t, &opts);
202}
203
f64fe7b4 204static char *prepare_index(int argc, const char **argv, const char *prefix)
f5bbc322
KH
205{
206 int fd;
2888605c
JH
207 struct path_list partial;
208 const char **pathspec = NULL;
f5bbc322
KH
209
210 if (interactive) {
3f061887 211 interactive_add(argc, argv, prefix);
2888605c 212 commit_style = COMMIT_AS_IS;
f5bbc322
KH
213 return get_index_file();
214 }
215
f5bbc322
KH
216 if (read_cache() < 0)
217 die("index file corrupt");
218
f64fe7b4
JH
219 if (*argv)
220 pathspec = get_pathspec(prefix, argv);
2888605c
JH
221
222 /*
223 * Non partial, non as-is commit.
224 *
225 * (1) get the real index;
226 * (2) update the_index as necessary;
227 * (3) write the_index out to the real index (still locked);
228 * (4) return the name of the locked index file.
229 *
230 * The caller should run hooks on the locked real index, and
231 * (A) if all goes well, commit the real index;
232 * (B) on failure, rollback the real index.
233 */
234 if (all || (also && pathspec && *pathspec)) {
235 int fd = hold_locked_index(&index_lock, 1);
236 add_files_to_cache(0, also ? prefix : NULL, pathspec);
d37d3203 237 refresh_cache(REFRESH_QUIET);
f5bbc322
KH
238 if (write_cache(fd, active_cache, active_nr) || close(fd))
239 die("unable to write new_index file");
2888605c
JH
240 commit_style = COMMIT_NORMAL;
241 return index_lock.filename;
f5bbc322
KH
242 }
243
2888605c
JH
244 /*
245 * As-is commit.
246 *
247 * (1) return the name of the real index file.
248 *
249 * The caller should run hooks on the real index, and run
250 * hooks on the real index, and create commit from the_index.
251 * We still need to refresh the index here.
252 */
253 if (!pathspec || !*pathspec) {
254 fd = hold_locked_index(&index_lock, 1);
255 refresh_cache(REFRESH_QUIET);
256 if (write_cache(fd, active_cache, active_nr) ||
257 close(fd) || commit_locked_index(&index_lock))
258 die("unable to write new_index file");
259 commit_style = COMMIT_AS_IS;
f5bbc322
KH
260 return get_index_file();
261 }
262
2888605c
JH
263 /*
264 * A partial commit.
265 *
266 * (0) find the set of affected paths;
267 * (1) get lock on the real index file;
268 * (2) update the_index with the given paths;
269 * (3) write the_index out to the real index (still locked);
270 * (4) get lock on the false index file;
271 * (5) reset the_index from HEAD;
272 * (6) update the_index the same way as (2);
273 * (7) write the_index out to the false index file;
274 * (8) return the name of the false index file (still locked);
275 *
276 * The caller should run hooks on the locked false index, and
277 * create commit from it. Then
278 * (A) if all goes well, commit the real index;
279 * (B) on failure, rollback the real index;
280 * In either case, rollback the false index.
281 */
282 commit_style = COMMIT_PARTIAL;
283
284 if (file_exists(git_path("MERGE_HEAD")))
285 die("cannot do a partial commit during a merge.");
286
287 memset(&partial, 0, sizeof(partial));
288 partial.strdup_paths = 1;
289 if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
290 exit(1);
291
292 discard_cache();
293 if (read_cache() < 0)
294 die("cannot read the index");
295
296 fd = hold_locked_index(&index_lock, 1);
297 add_remove_files(&partial);
ef12b50d 298 refresh_cache(REFRESH_QUIET);
f5bbc322
KH
299 if (write_cache(fd, active_cache, active_nr) || close(fd))
300 die("unable to write new_index file");
301
2888605c
JH
302 fd = hold_lock_file_for_update(&false_lock,
303 git_path("next-index-%d", getpid()), 1);
fa9dcf80
LT
304
305 create_base_index();
2888605c 306 add_remove_files(&partial);
d37d3203 307 refresh_cache(REFRESH_QUIET);
f5bbc322 308
2888605c
JH
309 if (write_cache(fd, active_cache, active_nr) || close(fd))
310 die("unable to write temporary index file");
311 return false_lock.filename;
f5bbc322
KH
312}
313
37d07f8f 314static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn)
f5bbc322
KH
315{
316 struct wt_status s;
317
318 wt_status_prepare(&s);
46f721c8
JK
319 if (wt_status_relative_paths)
320 s.prefix = prefix;
f5bbc322
KH
321
322 if (amend) {
323 s.amend = 1;
324 s.reference = "HEAD^1";
325 }
326 s.verbose = verbose;
327 s.untracked = untracked_files;
328 s.index_file = index_file;
329 s.fp = fp;
37d07f8f 330 s.nowarn = nowarn;
f5bbc322
KH
331
332 wt_status_print(&s);
333
334 return s.commitable;
335}
336
337static const char sign_off_header[] = "Signed-off-by: ";
338
367c9886 339static int prepare_log_message(const char *index_file, const char *prefix)
f5bbc322
KH
340{
341 struct stat statbuf;
bc5d248a 342 int commitable, saved_color_setting;
f5bbc322
KH
343 struct strbuf sb;
344 char *buffer;
345 FILE *fp;
346
347 strbuf_init(&sb, 0);
f9568530
JS
348 if (message.len) {
349 strbuf_addbuf(&sb, &message);
f5bbc322
KH
350 } else if (logfile && !strcmp(logfile, "-")) {
351 if (isatty(0))
352 fprintf(stderr, "(reading log message from standard input)\n");
353 if (strbuf_read(&sb, 0, 0) < 0)
354 die("could not read log from standard input");
355 } else if (logfile) {
356 if (strbuf_read_file(&sb, logfile, 0) < 0)
357 die("could not read log file '%s': %s",
358 logfile, strerror(errno));
359 } else if (use_message) {
360 buffer = strstr(use_message_buffer, "\n\n");
361 if (!buffer || buffer[2] == '\0')
362 die("commit has empty message");
363 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
364 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
365 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
366 die("could not read MERGE_MSG: %s", strerror(errno));
367 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
368 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
369 die("could not read SQUASH_MSG: %s", strerror(errno));
370 } else if (template_file && !stat(template_file, &statbuf)) {
371 if (strbuf_read_file(&sb, template_file, 0) < 0)
372 die("could not read %s: %s",
373 template_file, strerror(errno));
374 }
375
376 fp = fopen(git_path(commit_editmsg), "w");
377 if (fp == NULL)
b5b644a9 378 die("could not open %s", git_path(commit_editmsg));
f5bbc322 379
5f065737
AR
380 if (cleanup_mode != CLEANUP_NONE)
381 stripspace(&sb, 0);
f5bbc322
KH
382
383 if (signoff) {
13208572
JS
384 struct strbuf sob;
385 int i;
386
387 strbuf_init(&sob, 0);
388 strbuf_addstr(&sob, sign_off_header);
d9ccfe77
JH
389 strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
390 getenv("GIT_COMMITTER_EMAIL")));
13208572 391 strbuf_addch(&sob, '\n');
13208572
JS
392 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
393 ; /* do nothing */
2150554b
JS
394 if (prefixcmp(sb.buf + i, sob.buf)) {
395 if (prefixcmp(sb.buf + i, sign_off_header))
396 strbuf_addch(&sb, '\n');
13208572 397 strbuf_addbuf(&sb, &sob);
2150554b 398 }
13208572 399 strbuf_release(&sob);
f5bbc322
KH
400 }
401
13208572 402 if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
b5b644a9 403 die("could not write commit template: %s", strerror(errno));
13208572 404
f5bbc322
KH
405 strbuf_release(&sb);
406
4803466f 407 if (!use_editor) {
7168624c 408 struct rev_info rev;
d616a239 409 unsigned char sha1[20];
fbcf1184 410 const char *parent = "HEAD";
7168624c
AR
411
412 fclose(fp);
413
414 if (!active_nr && read_cache() < 0)
415 die("Cannot read index");
416
fbcf1184
JH
417 if (amend)
418 parent = "HEAD^1";
419
d616a239
JH
420 if (get_sha1(parent, sha1))
421 return !!active_nr;
422
7168624c
AR
423 init_revisions(&rev, "");
424 rev.abbrev = 0;
fbcf1184 425 setup_revisions(0, NULL, &rev, parent);
7168624c
AR
426 DIFF_OPT_SET(&rev.diffopt, QUIET);
427 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
428 run_diff_index(&rev, 1 /* cached */);
429
430 return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
431 }
432
4803466f 433 if (in_merge)
f5bbc322
KH
434 fprintf(fp,
435 "#\n"
436 "# It looks like you may be committing a MERGE.\n"
437 "# If this is not correct, please remove the file\n"
438 "# %s\n"
439 "# and try again.\n"
440 "#\n",
441 git_path("MERGE_HEAD"));
442
443 fprintf(fp,
444 "\n"
445 "# Please enter the commit message for your changes.\n"
5f065737
AR
446 "# (Comment lines starting with '#' will ");
447 if (cleanup_mode == CLEANUP_ALL)
448 fprintf(fp, "not be included)\n");
449 else /* CLEANUP_SPACE, that is. */
450 fprintf(fp, "be kept.\n"
451 "# You can remove them yourself if you want to)\n");
f5bbc322
KH
452 if (only_include_assumed)
453 fprintf(fp, "# %s\n", only_include_assumed);
454
bc5d248a
JH
455 saved_color_setting = wt_status_use_color;
456 wt_status_use_color = 0;
37d07f8f 457 commitable = run_status(fp, index_file, prefix, 1);
bc5d248a 458 wt_status_use_color = saved_color_setting;
f5bbc322
KH
459
460 fclose(fp);
461
462 return commitable;
463}
464
465/*
466 * Find out if the message starting at position 'start' in the strbuf
467 * contains only whitespace and Signed-off-by lines.
468 */
469static int message_is_empty(struct strbuf *sb, int start)
470{
471 struct strbuf tmpl;
472 const char *nl;
473 int eol, i;
474
5f065737
AR
475 if (cleanup_mode == CLEANUP_NONE && sb->len)
476 return 0;
477
f5bbc322
KH
478 /* See if the template is just a prefix of the message. */
479 strbuf_init(&tmpl, 0);
480 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
5f065737 481 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
f5bbc322
KH
482 if (start + tmpl.len <= sb->len &&
483 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
484 start += tmpl.len;
485 }
486 strbuf_release(&tmpl);
487
488 /* Check if the rest is just whitespace and Signed-of-by's. */
489 for (i = start; i < sb->len; i++) {
490 nl = memchr(sb->buf + i, '\n', sb->len - i);
491 if (nl)
492 eol = nl - sb->buf;
493 else
494 eol = sb->len;
495
496 if (strlen(sign_off_header) <= eol - i &&
497 !prefixcmp(sb->buf + i, sign_off_header)) {
498 i = eol;
499 continue;
500 }
501 while (i < eol)
502 if (!isspace(sb->buf[i++]))
503 return 0;
504 }
505
506 return 1;
507}
508
509static void determine_author_info(struct strbuf *sb)
510{
511 char *name, *email, *date;
512
513 name = getenv("GIT_AUTHOR_NAME");
514 email = getenv("GIT_AUTHOR_EMAIL");
515 date = getenv("GIT_AUTHOR_DATE");
516
517 if (use_message) {
518 const char *a, *lb, *rb, *eol;
519
520 a = strstr(use_message_buffer, "\nauthor ");
521 if (!a)
b5b644a9 522 die("invalid commit: %s", use_message);
f5bbc322
KH
523
524 lb = strstr(a + 8, " <");
525 rb = strstr(a + 8, "> ");
526 eol = strchr(a + 8, '\n');
527 if (!lb || !rb || !eol)
b5b644a9 528 die("invalid commit: %s", use_message);
f5bbc322
KH
529
530 name = xstrndup(a + 8, lb - (a + 8));
531 email = xstrndup(lb + 2, rb - (lb + 2));
532 date = xstrndup(rb + 2, eol - (rb + 2));
533 }
534
535 if (force_author) {
536 const char *lb = strstr(force_author, " <");
537 const char *rb = strchr(force_author, '>');
538
539 if (!lb || !rb)
b5b644a9 540 die("malformed --author parameter");
f5bbc322
KH
541 name = xstrndup(force_author, lb - force_author);
542 email = xstrndup(lb + 2, rb - (lb + 2));
543 }
544
774751a8 545 strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME));
f5bbc322
KH
546}
547
2f02b25f
SB
548static int parse_and_validate_options(int argc, const char *argv[],
549 const char * const usage[])
f5bbc322
KH
550{
551 int f = 0;
552
2f02b25f 553 argc = parse_options(argc, argv, builtin_commit_options, usage, 0);
f5bbc322 554
f9568530 555 if (logfile || message.len || use_message)
4803466f 556 use_editor = 0;
f5bbc322 557 if (edit_flag)
4803466f 558 use_editor = 1;
f5bbc322
KH
559
560 if (get_sha1("HEAD", head_sha1))
561 initial_commit = 1;
562
563 if (!get_sha1("MERGE_HEAD", merge_head_sha1))
564 in_merge = 1;
565
566 /* Sanity check options */
567 if (amend && initial_commit)
568 die("You have nothing to amend.");
569 if (amend && in_merge)
b5b644a9 570 die("You are in the middle of a merge -- cannot amend.");
f5bbc322
KH
571
572 if (use_message)
573 f++;
574 if (edit_message)
575 f++;
576 if (logfile)
577 f++;
578 if (f > 1)
579 die("Only one of -c/-C/-F can be used.");
f9568530 580 if (message.len && f > 0)
f5bbc322
KH
581 die("Option -m cannot be combined with -c/-C/-F.");
582 if (edit_message)
583 use_message = edit_message;
1eb1e9ee 584 if (amend && !use_message)
f5bbc322
KH
585 use_message = "HEAD";
586 if (use_message) {
587 unsigned char sha1[20];
588 static char utf8[] = "UTF-8";
589 const char *out_enc;
590 char *enc, *end;
591 struct commit *commit;
592
593 if (get_sha1(use_message, sha1))
594 die("could not lookup commit %s", use_message);
595 commit = lookup_commit(sha1);
596 if (!commit || parse_commit(commit))
597 die("could not parse commit %s", use_message);
598
599 enc = strstr(commit->buffer, "\nencoding");
600 if (enc) {
601 end = strchr(enc + 10, '\n');
602 enc = xstrndup(enc + 10, end - (enc + 10));
603 } else {
604 enc = utf8;
605 }
606 out_enc = git_commit_encoding ? git_commit_encoding : utf8;
607
608 if (strcmp(out_enc, enc))
609 use_message_buffer =
610 reencode_string(commit->buffer, out_enc, enc);
611
612 /*
613 * If we failed to reencode the buffer, just copy it
614 * byte for byte so the user can try to fix it up.
615 * This also handles the case where input and output
616 * encodings are identical.
617 */
618 if (use_message_buffer == NULL)
619 use_message_buffer = xstrdup(commit->buffer);
620 if (enc != utf8)
621 free(enc);
622 }
623
624 if (!!also + !!only + !!all + !!interactive > 1)
625 die("Only one of --include/--only/--all/--interactive can be used.");
626 if (argc == 0 && (also || (only && !amend)))
627 die("No paths with --include/--only does not make sense.");
628 if (argc == 0 && only && amend)
629 only_include_assumed = "Clever... amending the last one with dirty index.";
630 if (argc > 0 && !also && !only) {
631 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
632 also = 0;
633 }
5f065737
AR
634 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
635 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
636 else if (!strcmp(cleanup_arg, "verbatim"))
637 cleanup_mode = CLEANUP_NONE;
638 else if (!strcmp(cleanup_arg, "whitespace"))
639 cleanup_mode = CLEANUP_SPACE;
640 else if (!strcmp(cleanup_arg, "strip"))
641 cleanup_mode = CLEANUP_ALL;
642 else
643 die("Invalid cleanup mode %s", cleanup_arg);
f5bbc322
KH
644
645 if (all && argc > 0)
646 die("Paths with -a does not make sense.");
647 else if (interactive && argc > 0)
648 die("Paths with --interactive does not make sense.");
649
650 return argc;
651}
652
653int cmd_status(int argc, const char **argv, const char *prefix)
654{
655 const char *index_file;
656 int commitable;
657
658 git_config(git_status_config);
659
2f02b25f 660 argc = parse_and_validate_options(argc, argv, builtin_status_usage);
f5bbc322 661
f64fe7b4 662 index_file = prepare_index(argc, argv, prefix);
f5bbc322 663
37d07f8f 664 commitable = run_status(stdout, index_file, prefix, 0);
f5bbc322 665
2888605c 666 rollback_index_files();
f5bbc322
KH
667
668 return commitable ? 0 : 1;
669}
670
671static int run_hook(const char *index_file, const char *name, const char *arg)
672{
673 struct child_process hook;
674 const char *argv[3], *env[2];
675 char index[PATH_MAX];
676
677 argv[0] = git_path("hooks/%s", name);
678 argv[1] = arg;
679 argv[2] = NULL;
680 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
681 env[0] = index;
682 env[1] = NULL;
683
684 if (access(argv[0], X_OK) < 0)
685 return 0;
686
687 memset(&hook, 0, sizeof(hook));
688 hook.argv = argv;
689 hook.no_stdin = 1;
690 hook.stdout_to_stderr = 1;
691 hook.env = env;
692
693 return run_command(&hook);
694}
695
696static void print_summary(const char *prefix, const unsigned char *sha1)
697{
698 struct rev_info rev;
699 struct commit *commit;
700
701 commit = lookup_commit(sha1);
702 if (!commit)
b5b644a9 703 die("couldn't look up newly created commit");
f5bbc322
KH
704 if (!commit || parse_commit(commit))
705 die("could not parse newly created commit");
706
707 init_revisions(&rev, prefix);
708 setup_revisions(0, NULL, &rev, NULL);
709
710 rev.abbrev = 0;
711 rev.diff = 1;
712 rev.diffopt.output_format =
713 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
714
715 rev.verbose_header = 1;
716 rev.show_root_diff = 1;
717 rev.commit_format = get_commit_format("format:%h: %s");
bf82a150 718 rev.always_show_header = 0;
3eb2a15e
JH
719 rev.diffopt.detect_rename = 1;
720 rev.diffopt.rename_limit = 100;
721 rev.diffopt.break_opt = 0;
15964563 722 diff_setup_done(&rev.diffopt);
f5bbc322
KH
723
724 printf("Created %scommit ", initial_commit ? "initial " : "");
725
bf82a150
JH
726 if (!log_tree_commit(&rev, commit)) {
727 struct strbuf buf = STRBUF_INIT;
728 format_commit_message(commit, "%h: %s", &buf);
729 printf("%s\n", buf.buf);
730 strbuf_release(&buf);
731 }
f5bbc322
KH
732}
733
734int git_commit_config(const char *k, const char *v)
735{
736 if (!strcmp(k, "commit.template")) {
737 template_file = xstrdup(v);
738 return 0;
739 }
740
741 return git_status_config(k, v);
742}
743
9663c3bc
JH
744static int is_a_merge(const unsigned char *sha1)
745{
746 struct commit *commit = lookup_commit(sha1);
747 if (!commit || parse_commit(commit))
748 die("could not parse HEAD commit");
749 return !!(commit->parents && commit->parents->next);
750}
751
f5bbc322
KH
752static const char commit_utf8_warn[] =
753"Warning: commit message does not conform to UTF-8.\n"
754"You may want to amend it after fixing the message, or set the config\n"
755"variable i18n.commitencoding to the encoding your project uses.\n";
756
757int cmd_commit(int argc, const char **argv, const char *prefix)
758{
18abc2db 759 int header_len;
f5bbc322
KH
760 struct strbuf sb;
761 const char *index_file, *reflog_msg;
99a12694 762 char *nl, *p;
f5bbc322
KH
763 unsigned char commit_sha1[20];
764 struct ref_lock *ref_lock;
765
766 git_config(git_commit_config);
767
2f02b25f 768 argc = parse_and_validate_options(argc, argv, builtin_commit_usage);
f5bbc322 769
f64fe7b4 770 index_file = prepare_index(argc, argv, prefix);
f5bbc322 771
2888605c
JH
772 if (!no_verify && run_hook(index_file, "pre-commit", NULL)) {
773 rollback_index_files();
774 return 1;
775 }
f5bbc322 776
9663c3bc 777 if (!prepare_log_message(index_file, prefix) && !in_merge &&
5241b6bf 778 !allow_empty && !(amend && is_a_merge(head_sha1))) {
37d07f8f 779 run_status(stdout, index_file, prefix, 0);
2888605c 780 rollback_index_files();
f5bbc322
KH
781 unlink(commit_editmsg);
782 return 1;
783 }
784
2888605c
JH
785 /*
786 * Re-read the index as pre-commit hook could have updated it,
787 * and write it out as a tree.
788 */
789 discard_cache();
f5bbc322 790 read_cache_from(index_file);
2888605c
JH
791 if (!active_cache_tree)
792 active_cache_tree = cache_tree();
f5bbc322 793 if (cache_tree_update(active_cache_tree,
2888605c
JH
794 active_cache, active_nr, 0, 0) < 0) {
795 rollback_index_files();
f5bbc322 796 die("Error building trees");
2888605c
JH
797 }
798
799 /*
800 * The commit object
801 */
802 strbuf_init(&sb, 0);
f5bbc322
KH
803 strbuf_addf(&sb, "tree %s\n",
804 sha1_to_hex(active_cache_tree->sha1));
805
806 /* Determine parents */
807 if (initial_commit) {
808 reflog_msg = "commit (initial)";
f5bbc322
KH
809 } else if (amend) {
810 struct commit_list *c;
811 struct commit *commit;
812
813 reflog_msg = "commit (amend)";
814 commit = lookup_commit(head_sha1);
815 if (!commit || parse_commit(commit))
816 die("could not parse HEAD commit");
817
818 for (c = commit->parents; c; c = c->next)
819 strbuf_addf(&sb, "parent %s\n",
820 sha1_to_hex(c->item->object.sha1));
821 } else if (in_merge) {
822 struct strbuf m;
823 FILE *fp;
824
825 reflog_msg = "commit (merge)";
826 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
827 strbuf_init(&m, 0);
828 fp = fopen(git_path("MERGE_HEAD"), "r");
829 if (fp == NULL)
830 die("could not open %s for reading: %s",
831 git_path("MERGE_HEAD"), strerror(errno));
832 while (strbuf_getline(&m, fp, '\n') != EOF)
833 strbuf_addf(&sb, "parent %s\n", m.buf);
834 fclose(fp);
835 strbuf_release(&m);
836 } else {
837 reflog_msg = "commit";
838 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
839 }
840
841 determine_author_info(&sb);
774751a8 842 strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
f5bbc322
KH
843 if (!is_encoding_utf8(git_commit_encoding))
844 strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
845 strbuf_addch(&sb, '\n');
846
847 /* Get the commit message and validate it */
848 header_len = sb.len;
4803466f 849 if (use_editor) {
8babab95
PH
850 char index[PATH_MAX];
851 const char *env[2] = { index, NULL };
852 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
740001a5 853 launch_editor(git_path(commit_editmsg), NULL, env);
2888605c 854 }
6b95655d
WC
855 if (!no_verify &&
856 run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
2888605c 857 rollback_index_files();
f5bbc322 858 exit(1);
2888605c 859 }
740001a5
JH
860 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
861 rollback_index_files();
862 die("could not read commit message");
863 }
99a12694
KH
864
865 /* Truncate the message just before the diff, if any. */
866 p = strstr(sb.buf, "\ndiff --git a/");
867 if (p != NULL)
a98b8191 868 strbuf_setlen(&sb, p - sb.buf + 1);
99a12694 869
5f065737
AR
870 if (cleanup_mode != CLEANUP_NONE)
871 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
2888605c
JH
872 if (sb.len < header_len || message_is_empty(&sb, header_len)) {
873 rollback_index_files();
b5b644a9 874 die("no commit message? aborting commit.");
2888605c 875 }
f5bbc322
KH
876 strbuf_addch(&sb, '\0');
877 if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
878 fprintf(stderr, commit_utf8_warn);
879
2888605c
JH
880 if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) {
881 rollback_index_files();
f5bbc322 882 die("failed to write commit object");
2888605c 883 }
f5bbc322
KH
884
885 ref_lock = lock_any_ref_for_update("HEAD",
886 initial_commit ? NULL : head_sha1,
887 0);
888
889 nl = strchr(sb.buf + header_len, '\n');
741707b1
JS
890 if (nl)
891 strbuf_setlen(&sb, nl + 1 - sb.buf);
892 else
893 strbuf_addch(&sb, '\n');
894 strbuf_remove(&sb, 0, header_len);
895 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
896 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
f5bbc322 897
2888605c
JH
898 if (!ref_lock) {
899 rollback_index_files();
f5bbc322 900 die("cannot lock HEAD ref");
2888605c
JH
901 }
902 if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
903 rollback_index_files();
f5bbc322 904 die("cannot update HEAD ref");
2888605c 905 }
f5bbc322
KH
906
907 unlink(git_path("MERGE_HEAD"));
908 unlink(git_path("MERGE_MSG"));
909
2888605c 910 commit_index_files();
f5bbc322
KH
911
912 rerere();
2888605c 913 run_hook(get_index_file(), "post-commit", NULL);
f5bbc322
KH
914 if (!quiet)
915 print_summary(prefix, commit_sha1);
916
917 return 0;
918}