Documentation: config: add 'help.*' and 'instaweb.*' variables.
[git/dscho.git] / builtin-commit.c
blob73f1e3576a019d33ee7034d46a4fe4a2d7ab3f7b
1 /*
2 * Builtin "git commit"
4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
6 */
8 #include "cache.h"
9 #include "cache-tree.h"
10 #include "dir.h"
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"
23 #include "path-list.h"
25 static const char * const builtin_commit_usage[] = {
26 "git-commit [options] [--] <filepattern>...",
27 NULL
30 static const char * const builtin_status_usage[] = {
31 "git-status [options] [--] <filepattern>...",
32 NULL
35 static unsigned char head_sha1[20], merge_head_sha1[20];
36 static char *use_message_buffer;
37 static const char commit_editmsg[] = "COMMIT_EDITMSG";
38 static struct lock_file index_lock; /* real index */
39 static struct lock_file false_lock; /* used only for partial commits */
40 static enum {
41 COMMIT_AS_IS = 1,
42 COMMIT_NORMAL,
43 COMMIT_PARTIAL,
44 } commit_style;
46 static char *logfile, *force_author, *template_file;
47 static char *edit_message, *use_message;
48 static int all, edit_flag, also, interactive, only, amend, signoff;
49 static int quiet, verbose, untracked_files, no_verify, allow_empty;
51 * The default commit message cleanup mode will remove the lines
52 * beginning with # (shell comments) and leading and trailing
53 * whitespaces (empty lines or containing only whitespaces)
54 * if editor is used, and only the whitespaces if the message
55 * is specified explicitly.
57 static enum {
58 CLEANUP_SPACE,
59 CLEANUP_NONE,
60 CLEANUP_ALL,
61 } cleanup_mode;
62 static char *cleanup_arg;
64 static int use_editor = 1, initial_commit, in_merge;
65 const char *only_include_assumed;
66 struct strbuf message;
68 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
70 struct strbuf *buf = opt->value;
71 if (unset)
72 strbuf_setlen(buf, 0);
73 else {
74 strbuf_addstr(buf, arg);
75 strbuf_addch(buf, '\n');
76 strbuf_addch(buf, '\n');
78 return 0;
81 static struct option builtin_commit_options[] = {
82 OPT__QUIET(&quiet),
83 OPT__VERBOSE(&verbose),
84 OPT_GROUP("Commit message options"),
86 OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
87 OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
88 OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
89 OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "),
90 OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
91 OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by: header"),
92 OPT_STRING('t', "template", &template_file, "FILE", "use specified template file"),
93 OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
95 OPT_GROUP("Commit contents options"),
96 OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
97 OPT_BOOLEAN('i', "include", &also, "add specified files to index for commit"),
98 OPT_BOOLEAN(0, "interactive", &interactive, "interactively add files"),
99 OPT_BOOLEAN('o', "only", &only, ""),
100 OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
101 OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
102 OPT_BOOLEAN(0, "untracked-files", &untracked_files, "show all untracked files"),
103 OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
104 OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
106 OPT_END()
109 static void rollback_index_files(void)
111 switch (commit_style) {
112 case COMMIT_AS_IS:
113 break; /* nothing to do */
114 case COMMIT_NORMAL:
115 rollback_lock_file(&index_lock);
116 break;
117 case COMMIT_PARTIAL:
118 rollback_lock_file(&index_lock);
119 rollback_lock_file(&false_lock);
120 break;
124 static void commit_index_files(void)
126 switch (commit_style) {
127 case COMMIT_AS_IS:
128 break; /* nothing to do */
129 case COMMIT_NORMAL:
130 commit_lock_file(&index_lock);
131 break;
132 case COMMIT_PARTIAL:
133 commit_lock_file(&index_lock);
134 rollback_lock_file(&false_lock);
135 break;
140 * Take a union of paths in the index and the named tree (typically, "HEAD"),
141 * and return the paths that match the given pattern in list.
143 static int list_paths(struct path_list *list, const char *with_tree,
144 const char *prefix, const char **pattern)
146 int i;
147 char *m;
149 for (i = 0; pattern[i]; i++)
151 m = xcalloc(1, i);
153 if (with_tree)
154 overlay_tree_on_cache(with_tree, prefix);
156 for (i = 0; i < active_nr; i++) {
157 struct cache_entry *ce = active_cache[i];
158 if (ce->ce_flags & htons(CE_UPDATE))
159 continue;
160 if (!pathspec_match(pattern, m, ce->name, 0))
161 continue;
162 path_list_insert(ce->name, list);
165 return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
168 static void add_remove_files(struct path_list *list)
170 int i;
171 for (i = 0; i < list->nr; i++) {
172 struct path_list_item *p = &(list->items[i]);
173 if (file_exists(p->path))
174 add_file_to_cache(p->path, 0);
175 else
176 remove_file_from_cache(p->path);
180 static char *prepare_index(int argc, const char **argv, const char *prefix)
182 int fd;
183 struct tree *tree;
184 struct path_list partial;
185 const char **pathspec = NULL;
187 if (interactive) {
188 interactive_add(argc, argv, prefix);
189 commit_style = COMMIT_AS_IS;
190 return get_index_file();
193 if (read_cache() < 0)
194 die("index file corrupt");
196 if (*argv)
197 pathspec = get_pathspec(prefix, argv);
200 * Non partial, non as-is commit.
202 * (1) get the real index;
203 * (2) update the_index as necessary;
204 * (3) write the_index out to the real index (still locked);
205 * (4) return the name of the locked index file.
207 * The caller should run hooks on the locked real index, and
208 * (A) if all goes well, commit the real index;
209 * (B) on failure, rollback the real index.
211 if (all || (also && pathspec && *pathspec)) {
212 int fd = hold_locked_index(&index_lock, 1);
213 add_files_to_cache(0, also ? prefix : NULL, pathspec);
214 refresh_cache(REFRESH_QUIET);
215 if (write_cache(fd, active_cache, active_nr) || close(fd))
216 die("unable to write new_index file");
217 commit_style = COMMIT_NORMAL;
218 return index_lock.filename;
222 * As-is commit.
224 * (1) return the name of the real index file.
226 * The caller should run hooks on the real index, and run
227 * hooks on the real index, and create commit from the_index.
228 * We still need to refresh the index here.
230 if (!pathspec || !*pathspec) {
231 fd = hold_locked_index(&index_lock, 1);
232 refresh_cache(REFRESH_QUIET);
233 if (write_cache(fd, active_cache, active_nr) ||
234 close(fd) || commit_locked_index(&index_lock))
235 die("unable to write new_index file");
236 commit_style = COMMIT_AS_IS;
237 return get_index_file();
241 * A partial commit.
243 * (0) find the set of affected paths;
244 * (1) get lock on the real index file;
245 * (2) update the_index with the given paths;
246 * (3) write the_index out to the real index (still locked);
247 * (4) get lock on the false index file;
248 * (5) reset the_index from HEAD;
249 * (6) update the_index the same way as (2);
250 * (7) write the_index out to the false index file;
251 * (8) return the name of the false index file (still locked);
253 * The caller should run hooks on the locked false index, and
254 * create commit from it. Then
255 * (A) if all goes well, commit the real index;
256 * (B) on failure, rollback the real index;
257 * In either case, rollback the false index.
259 commit_style = COMMIT_PARTIAL;
261 if (file_exists(git_path("MERGE_HEAD")))
262 die("cannot do a partial commit during a merge.");
264 memset(&partial, 0, sizeof(partial));
265 partial.strdup_paths = 1;
266 if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
267 exit(1);
269 discard_cache();
270 if (read_cache() < 0)
271 die("cannot read the index");
273 fd = hold_locked_index(&index_lock, 1);
274 add_remove_files(&partial);
275 refresh_cache(REFRESH_QUIET);
276 if (write_cache(fd, active_cache, active_nr) || close(fd))
277 die("unable to write new_index file");
279 fd = hold_lock_file_for_update(&false_lock,
280 git_path("next-index-%d", getpid()), 1);
281 discard_cache();
282 if (!initial_commit) {
283 tree = parse_tree_indirect(head_sha1);
284 if (!tree)
285 die("failed to unpack HEAD tree object");
286 if (read_tree(tree, 0, NULL))
287 die("failed to read HEAD tree object");
289 add_remove_files(&partial);
290 refresh_cache(REFRESH_QUIET);
292 if (write_cache(fd, active_cache, active_nr) || close(fd))
293 die("unable to write temporary index file");
294 return false_lock.filename;
297 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn)
299 struct wt_status s;
301 wt_status_prepare(&s);
302 if (wt_status_relative_paths)
303 s.prefix = prefix;
305 if (amend) {
306 s.amend = 1;
307 s.reference = "HEAD^1";
309 s.verbose = verbose;
310 s.untracked = untracked_files;
311 s.index_file = index_file;
312 s.fp = fp;
313 s.nowarn = nowarn;
315 wt_status_print(&s);
317 return s.commitable;
320 static const char sign_off_header[] = "Signed-off-by: ";
322 static int prepare_log_message(const char *index_file, const char *prefix)
324 struct stat statbuf;
325 int commitable, saved_color_setting;
326 struct strbuf sb;
327 char *buffer;
328 FILE *fp;
330 strbuf_init(&sb, 0);
331 if (message.len) {
332 strbuf_addbuf(&sb, &message);
333 } else if (logfile && !strcmp(logfile, "-")) {
334 if (isatty(0))
335 fprintf(stderr, "(reading log message from standard input)\n");
336 if (strbuf_read(&sb, 0, 0) < 0)
337 die("could not read log from standard input");
338 } else if (logfile) {
339 if (strbuf_read_file(&sb, logfile, 0) < 0)
340 die("could not read log file '%s': %s",
341 logfile, strerror(errno));
342 } else if (use_message) {
343 buffer = strstr(use_message_buffer, "\n\n");
344 if (!buffer || buffer[2] == '\0')
345 die("commit has empty message");
346 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
347 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
348 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
349 die("could not read MERGE_MSG: %s", strerror(errno));
350 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
351 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
352 die("could not read SQUASH_MSG: %s", strerror(errno));
353 } else if (template_file && !stat(template_file, &statbuf)) {
354 if (strbuf_read_file(&sb, template_file, 0) < 0)
355 die("could not read %s: %s",
356 template_file, strerror(errno));
359 fp = fopen(git_path(commit_editmsg), "w");
360 if (fp == NULL)
361 die("could not open %s", git_path(commit_editmsg));
363 if (cleanup_mode != CLEANUP_NONE)
364 stripspace(&sb, 0);
366 if (signoff) {
367 struct strbuf sob;
368 int i;
370 strbuf_init(&sob, 0);
371 strbuf_addstr(&sob, sign_off_header);
372 strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
373 getenv("GIT_COMMITTER_EMAIL")));
374 strbuf_addch(&sob, '\n');
375 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
376 ; /* do nothing */
377 if (prefixcmp(sb.buf + i, sob.buf)) {
378 if (prefixcmp(sb.buf + i, sign_off_header))
379 strbuf_addch(&sb, '\n');
380 strbuf_addbuf(&sb, &sob);
382 strbuf_release(&sob);
385 if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
386 die("could not write commit template: %s", strerror(errno));
388 strbuf_release(&sb);
390 if (!use_editor) {
391 struct rev_info rev;
392 unsigned char sha1[20];
393 const char *parent = "HEAD";
395 fclose(fp);
397 if (!active_nr && read_cache() < 0)
398 die("Cannot read index");
400 if (amend)
401 parent = "HEAD^1";
403 if (get_sha1(parent, sha1))
404 return !!active_nr;
406 init_revisions(&rev, "");
407 rev.abbrev = 0;
408 setup_revisions(0, NULL, &rev, parent);
409 DIFF_OPT_SET(&rev.diffopt, QUIET);
410 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
411 run_diff_index(&rev, 1 /* cached */);
413 return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
416 if (in_merge)
417 fprintf(fp,
418 "#\n"
419 "# It looks like you may be committing a MERGE.\n"
420 "# If this is not correct, please remove the file\n"
421 "# %s\n"
422 "# and try again.\n"
423 "#\n",
424 git_path("MERGE_HEAD"));
426 fprintf(fp,
427 "\n"
428 "# Please enter the commit message for your changes.\n"
429 "# (Comment lines starting with '#' will ");
430 if (cleanup_mode == CLEANUP_ALL)
431 fprintf(fp, "not be included)\n");
432 else /* CLEANUP_SPACE, that is. */
433 fprintf(fp, "be kept.\n"
434 "# You can remove them yourself if you want to)\n");
435 if (only_include_assumed)
436 fprintf(fp, "# %s\n", only_include_assumed);
438 saved_color_setting = wt_status_use_color;
439 wt_status_use_color = 0;
440 commitable = run_status(fp, index_file, prefix, 1);
441 wt_status_use_color = saved_color_setting;
443 fclose(fp);
445 return commitable;
449 * Find out if the message starting at position 'start' in the strbuf
450 * contains only whitespace and Signed-off-by lines.
452 static int message_is_empty(struct strbuf *sb, int start)
454 struct strbuf tmpl;
455 const char *nl;
456 int eol, i;
458 if (cleanup_mode == CLEANUP_NONE && sb->len)
459 return 0;
461 /* See if the template is just a prefix of the message. */
462 strbuf_init(&tmpl, 0);
463 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
464 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
465 if (start + tmpl.len <= sb->len &&
466 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
467 start += tmpl.len;
469 strbuf_release(&tmpl);
471 /* Check if the rest is just whitespace and Signed-of-by's. */
472 for (i = start; i < sb->len; i++) {
473 nl = memchr(sb->buf + i, '\n', sb->len - i);
474 if (nl)
475 eol = nl - sb->buf;
476 else
477 eol = sb->len;
479 if (strlen(sign_off_header) <= eol - i &&
480 !prefixcmp(sb->buf + i, sign_off_header)) {
481 i = eol;
482 continue;
484 while (i < eol)
485 if (!isspace(sb->buf[i++]))
486 return 0;
489 return 1;
492 static void determine_author_info(struct strbuf *sb)
494 char *name, *email, *date;
496 name = getenv("GIT_AUTHOR_NAME");
497 email = getenv("GIT_AUTHOR_EMAIL");
498 date = getenv("GIT_AUTHOR_DATE");
500 if (use_message) {
501 const char *a, *lb, *rb, *eol;
503 a = strstr(use_message_buffer, "\nauthor ");
504 if (!a)
505 die("invalid commit: %s", use_message);
507 lb = strstr(a + 8, " <");
508 rb = strstr(a + 8, "> ");
509 eol = strchr(a + 8, '\n');
510 if (!lb || !rb || !eol)
511 die("invalid commit: %s", use_message);
513 name = xstrndup(a + 8, lb - (a + 8));
514 email = xstrndup(lb + 2, rb - (lb + 2));
515 date = xstrndup(rb + 2, eol - (rb + 2));
518 if (force_author) {
519 const char *lb = strstr(force_author, " <");
520 const char *rb = strchr(force_author, '>');
522 if (!lb || !rb)
523 die("malformed --author parameter");
524 name = xstrndup(force_author, lb - force_author);
525 email = xstrndup(lb + 2, rb - (lb + 2));
528 strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME));
531 static int parse_and_validate_options(int argc, const char *argv[],
532 const char * const usage[])
534 int f = 0;
536 argc = parse_options(argc, argv, builtin_commit_options, usage, 0);
538 if (logfile || message.len || use_message)
539 use_editor = 0;
540 if (edit_flag)
541 use_editor = 1;
543 if (get_sha1("HEAD", head_sha1))
544 initial_commit = 1;
546 if (!get_sha1("MERGE_HEAD", merge_head_sha1))
547 in_merge = 1;
549 /* Sanity check options */
550 if (amend && initial_commit)
551 die("You have nothing to amend.");
552 if (amend && in_merge)
553 die("You are in the middle of a merge -- cannot amend.");
555 if (use_message)
556 f++;
557 if (edit_message)
558 f++;
559 if (logfile)
560 f++;
561 if (f > 1)
562 die("Only one of -c/-C/-F can be used.");
563 if (message.len && f > 0)
564 die("Option -m cannot be combined with -c/-C/-F.");
565 if (edit_message)
566 use_message = edit_message;
567 if (amend && !use_message)
568 use_message = "HEAD";
569 if (use_message) {
570 unsigned char sha1[20];
571 static char utf8[] = "UTF-8";
572 const char *out_enc;
573 char *enc, *end;
574 struct commit *commit;
576 if (get_sha1(use_message, sha1))
577 die("could not lookup commit %s", use_message);
578 commit = lookup_commit(sha1);
579 if (!commit || parse_commit(commit))
580 die("could not parse commit %s", use_message);
582 enc = strstr(commit->buffer, "\nencoding");
583 if (enc) {
584 end = strchr(enc + 10, '\n');
585 enc = xstrndup(enc + 10, end - (enc + 10));
586 } else {
587 enc = utf8;
589 out_enc = git_commit_encoding ? git_commit_encoding : utf8;
591 if (strcmp(out_enc, enc))
592 use_message_buffer =
593 reencode_string(commit->buffer, out_enc, enc);
596 * If we failed to reencode the buffer, just copy it
597 * byte for byte so the user can try to fix it up.
598 * This also handles the case where input and output
599 * encodings are identical.
601 if (use_message_buffer == NULL)
602 use_message_buffer = xstrdup(commit->buffer);
603 if (enc != utf8)
604 free(enc);
607 if (!!also + !!only + !!all + !!interactive > 1)
608 die("Only one of --include/--only/--all/--interactive can be used.");
609 if (argc == 0 && (also || (only && !amend)))
610 die("No paths with --include/--only does not make sense.");
611 if (argc == 0 && only && amend)
612 only_include_assumed = "Clever... amending the last one with dirty index.";
613 if (argc > 0 && !also && !only) {
614 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
615 also = 0;
617 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
618 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
619 else if (!strcmp(cleanup_arg, "verbatim"))
620 cleanup_mode = CLEANUP_NONE;
621 else if (!strcmp(cleanup_arg, "whitespace"))
622 cleanup_mode = CLEANUP_SPACE;
623 else if (!strcmp(cleanup_arg, "strip"))
624 cleanup_mode = CLEANUP_ALL;
625 else
626 die("Invalid cleanup mode %s", cleanup_arg);
628 if (all && argc > 0)
629 die("Paths with -a does not make sense.");
630 else if (interactive && argc > 0)
631 die("Paths with --interactive does not make sense.");
633 return argc;
636 int cmd_status(int argc, const char **argv, const char *prefix)
638 const char *index_file;
639 int commitable;
641 git_config(git_status_config);
643 argc = parse_and_validate_options(argc, argv, builtin_status_usage);
645 index_file = prepare_index(argc, argv, prefix);
647 commitable = run_status(stdout, index_file, prefix, 0);
649 rollback_index_files();
651 return commitable ? 0 : 1;
654 static int run_hook(const char *index_file, const char *name, const char *arg)
656 struct child_process hook;
657 const char *argv[3], *env[2];
658 char index[PATH_MAX];
660 argv[0] = git_path("hooks/%s", name);
661 argv[1] = arg;
662 argv[2] = NULL;
663 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
664 env[0] = index;
665 env[1] = NULL;
667 if (access(argv[0], X_OK) < 0)
668 return 0;
670 memset(&hook, 0, sizeof(hook));
671 hook.argv = argv;
672 hook.no_stdin = 1;
673 hook.stdout_to_stderr = 1;
674 hook.env = env;
676 return run_command(&hook);
679 static void print_summary(const char *prefix, const unsigned char *sha1)
681 struct rev_info rev;
682 struct commit *commit;
684 commit = lookup_commit(sha1);
685 if (!commit)
686 die("couldn't look up newly created commit");
687 if (!commit || parse_commit(commit))
688 die("could not parse newly created commit");
690 init_revisions(&rev, prefix);
691 setup_revisions(0, NULL, &rev, NULL);
693 rev.abbrev = 0;
694 rev.diff = 1;
695 rev.diffopt.output_format =
696 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
698 rev.verbose_header = 1;
699 rev.show_root_diff = 1;
700 rev.commit_format = get_commit_format("format:%h: %s");
701 rev.always_show_header = 0;
702 rev.diffopt.detect_rename = 1;
703 rev.diffopt.rename_limit = 100;
704 rev.diffopt.break_opt = 0;
705 diff_setup_done(&rev.diffopt);
707 printf("Created %scommit ", initial_commit ? "initial " : "");
709 if (!log_tree_commit(&rev, commit)) {
710 struct strbuf buf = STRBUF_INIT;
711 format_commit_message(commit, "%h: %s", &buf);
712 printf("%s\n", buf.buf);
713 strbuf_release(&buf);
717 int git_commit_config(const char *k, const char *v)
719 if (!strcmp(k, "commit.template")) {
720 template_file = xstrdup(v);
721 return 0;
724 return git_status_config(k, v);
727 static int is_a_merge(const unsigned char *sha1)
729 struct commit *commit = lookup_commit(sha1);
730 if (!commit || parse_commit(commit))
731 die("could not parse HEAD commit");
732 return !!(commit->parents && commit->parents->next);
735 static const char commit_utf8_warn[] =
736 "Warning: commit message does not conform to UTF-8.\n"
737 "You may want to amend it after fixing the message, or set the config\n"
738 "variable i18n.commitencoding to the encoding your project uses.\n";
740 int cmd_commit(int argc, const char **argv, const char *prefix)
742 int header_len;
743 struct strbuf sb;
744 const char *index_file, *reflog_msg;
745 char *nl, *p;
746 unsigned char commit_sha1[20];
747 struct ref_lock *ref_lock;
749 git_config(git_commit_config);
751 argc = parse_and_validate_options(argc, argv, builtin_commit_usage);
753 index_file = prepare_index(argc, argv, prefix);
755 if (!no_verify && run_hook(index_file, "pre-commit", NULL)) {
756 rollback_index_files();
757 return 1;
760 if (!prepare_log_message(index_file, prefix) && !in_merge &&
761 !allow_empty && !(amend && is_a_merge(head_sha1))) {
762 run_status(stdout, index_file, prefix, 0);
763 rollback_index_files();
764 unlink(commit_editmsg);
765 return 1;
769 * Re-read the index as pre-commit hook could have updated it,
770 * and write it out as a tree.
772 discard_cache();
773 read_cache_from(index_file);
774 if (!active_cache_tree)
775 active_cache_tree = cache_tree();
776 if (cache_tree_update(active_cache_tree,
777 active_cache, active_nr, 0, 0) < 0) {
778 rollback_index_files();
779 die("Error building trees");
783 * The commit object
785 strbuf_init(&sb, 0);
786 strbuf_addf(&sb, "tree %s\n",
787 sha1_to_hex(active_cache_tree->sha1));
789 /* Determine parents */
790 if (initial_commit) {
791 reflog_msg = "commit (initial)";
792 } else if (amend) {
793 struct commit_list *c;
794 struct commit *commit;
796 reflog_msg = "commit (amend)";
797 commit = lookup_commit(head_sha1);
798 if (!commit || parse_commit(commit))
799 die("could not parse HEAD commit");
801 for (c = commit->parents; c; c = c->next)
802 strbuf_addf(&sb, "parent %s\n",
803 sha1_to_hex(c->item->object.sha1));
804 } else if (in_merge) {
805 struct strbuf m;
806 FILE *fp;
808 reflog_msg = "commit (merge)";
809 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
810 strbuf_init(&m, 0);
811 fp = fopen(git_path("MERGE_HEAD"), "r");
812 if (fp == NULL)
813 die("could not open %s for reading: %s",
814 git_path("MERGE_HEAD"), strerror(errno));
815 while (strbuf_getline(&m, fp, '\n') != EOF)
816 strbuf_addf(&sb, "parent %s\n", m.buf);
817 fclose(fp);
818 strbuf_release(&m);
819 } else {
820 reflog_msg = "commit";
821 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
824 determine_author_info(&sb);
825 strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
826 if (!is_encoding_utf8(git_commit_encoding))
827 strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
828 strbuf_addch(&sb, '\n');
830 /* Get the commit message and validate it */
831 header_len = sb.len;
832 if (use_editor) {
833 char index[PATH_MAX];
834 const char *env[2] = { index, NULL };
835 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
836 launch_editor(git_path(commit_editmsg), NULL, env);
838 if (!no_verify &&
839 run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
840 rollback_index_files();
841 exit(1);
843 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
844 rollback_index_files();
845 die("could not read commit message");
848 /* Truncate the message just before the diff, if any. */
849 p = strstr(sb.buf, "\ndiff --git a/");
850 if (p != NULL)
851 strbuf_setlen(&sb, p - sb.buf + 1);
853 if (cleanup_mode != CLEANUP_NONE)
854 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
855 if (sb.len < header_len || message_is_empty(&sb, header_len)) {
856 rollback_index_files();
857 die("no commit message? aborting commit.");
859 strbuf_addch(&sb, '\0');
860 if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
861 fprintf(stderr, commit_utf8_warn);
863 if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) {
864 rollback_index_files();
865 die("failed to write commit object");
868 ref_lock = lock_any_ref_for_update("HEAD",
869 initial_commit ? NULL : head_sha1,
872 nl = strchr(sb.buf + header_len, '\n');
873 if (nl)
874 strbuf_setlen(&sb, nl + 1 - sb.buf);
875 else
876 strbuf_addch(&sb, '\n');
877 strbuf_remove(&sb, 0, header_len);
878 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
879 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
881 if (!ref_lock) {
882 rollback_index_files();
883 die("cannot lock HEAD ref");
885 if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
886 rollback_index_files();
887 die("cannot update HEAD ref");
890 unlink(git_path("MERGE_HEAD"));
891 unlink(git_path("MERGE_MSG"));
893 commit_index_files();
895 rerere();
896 run_hook(get_index_file(), "post-commit", NULL);
897 if (!quiet)
898 print_summary(prefix, commit_sha1);
900 return 0;