4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
9 #include "cache-tree.h"
16 #include "wt-status.h"
17 #include "run-command.h"
22 #include "parse-options.h"
23 #include "path-list.h"
24 #include "unpack-trees.h"
26 static const char * const builtin_commit_usage[] = {
27 "git-commit [options] [--] <filepattern>...",
31 static const char * const builtin_status_usage[] = {
32 "git-status [options] [--] <filepattern>...",
36 static unsigned char head_sha1[20], merge_head_sha1[20];
37 static char *use_message_buffer;
38 static const char commit_editmsg[] = "COMMIT_EDITMSG";
39 static struct lock_file index_lock; /* real index */
40 static struct lock_file false_lock; /* used only for partial commits */
47 static char *logfile, *force_author, *template_file;
48 static char *edit_message, *use_message;
49 static int all, edit_flag, also, interactive, only, amend, signoff;
50 static int quiet, verbose, untracked_files, no_verify, allow_empty;
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.
63 static char *cleanup_arg;
65 static int use_editor = 1, initial_commit, in_merge;
66 const char *only_include_assumed;
67 struct strbuf message;
69 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
71 struct strbuf *buf = opt->value;
73 strbuf_setlen(buf, 0);
75 strbuf_addstr(buf, arg);
76 strbuf_addch(buf, '\n');
77 strbuf_addch(buf, '\n');
82 static struct option builtin_commit_options[] = {
84 OPT__VERBOSE(&verbose),
85 OPT_GROUP("Commit message options"),
87 OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
88 OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
89 OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
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"),
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"),
104 OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
105 OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
110 static void rollback_index_files(void)
112 switch (commit_style) {
114 break; /* nothing to do */
116 rollback_lock_file(&index_lock);
119 rollback_lock_file(&index_lock);
120 rollback_lock_file(&false_lock);
125 static void commit_index_files(void)
127 switch (commit_style) {
129 break; /* nothing to do */
131 commit_lock_file(&index_lock);
134 commit_lock_file(&index_lock);
135 rollback_lock_file(&false_lock);
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.
144 static int list_paths(struct path_list *list, const char *with_tree,
145 const char *prefix, const char **pattern)
150 for (i = 0; pattern[i]; i++)
155 overlay_tree_on_cache(with_tree, prefix);
157 for (i = 0; i < active_nr; i++) {
158 struct cache_entry *ce = active_cache[i];
159 if (ce->ce_flags & CE_UPDATE)
161 if (!pathspec_match(pattern, m, ce->name, 0))
163 path_list_insert(ce->name, list);
166 return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
169 static void add_remove_files(struct path_list *list)
172 for (i = 0; i < list->nr; i++) {
173 struct path_list_item *p = &(list->items[i]);
174 if (file_exists(p->path))
175 add_file_to_cache(p->path, 0);
177 remove_file_from_cache(p->path);
181 static void create_base_index(void)
184 struct unpack_trees_options opts;
187 if (initial_commit) {
192 memset(&opts, 0, sizeof(opts));
197 opts.fn = oneway_merge;
198 tree = parse_tree_indirect(head_sha1);
200 die("failed to unpack HEAD tree object");
202 init_tree_desc(&t, tree->buffer, tree->size);
203 if (unpack_trees(1, &t, &opts))
204 exit(128); /* We've already reported the error, finish dying */
207 static char *prepare_index(int argc, const char **argv, const char *prefix)
210 struct path_list partial;
211 const char **pathspec = NULL;
214 interactive_add(argc, argv, prefix);
215 commit_style = COMMIT_AS_IS;
216 return get_index_file();
219 if (read_cache() < 0)
220 die("index file corrupt");
223 pathspec = get_pathspec(prefix, argv);
226 * Non partial, non as-is commit.
228 * (1) get the real index;
229 * (2) update the_index as necessary;
230 * (3) write the_index out to the real index (still locked);
231 * (4) return the name of the locked index file.
233 * The caller should run hooks on the locked real index, and
234 * (A) if all goes well, commit the real index;
235 * (B) on failure, rollback the real index.
237 if (all || (also && pathspec && *pathspec)) {
238 int fd = hold_locked_index(&index_lock, 1);
239 add_files_to_cache(0, also ? prefix : NULL, pathspec);
240 refresh_cache(REFRESH_QUIET);
241 if (write_cache(fd, active_cache, active_nr) ||
242 close_lock_file(&index_lock))
243 die("unable to write new_index file");
244 commit_style = COMMIT_NORMAL;
245 return index_lock.filename;
251 * (1) return the name of the real index file.
253 * The caller should run hooks on the real index, and run
254 * hooks on the real index, and create commit from the_index.
255 * We still need to refresh the index here.
257 if (!pathspec || !*pathspec) {
258 fd = hold_locked_index(&index_lock, 1);
259 refresh_cache(REFRESH_QUIET);
260 if (write_cache(fd, active_cache, active_nr) ||
261 commit_locked_index(&index_lock))
262 die("unable to write new_index file");
263 commit_style = COMMIT_AS_IS;
264 return get_index_file();
270 * (0) find the set of affected paths;
271 * (1) get lock on the real index file;
272 * (2) update the_index with the given paths;
273 * (3) write the_index out to the real index (still locked);
274 * (4) get lock on the false index file;
275 * (5) reset the_index from HEAD;
276 * (6) update the_index the same way as (2);
277 * (7) write the_index out to the false index file;
278 * (8) return the name of the false index file (still locked);
280 * The caller should run hooks on the locked false index, and
281 * create commit from it. Then
282 * (A) if all goes well, commit the real index;
283 * (B) on failure, rollback the real index;
284 * In either case, rollback the false index.
286 commit_style = COMMIT_PARTIAL;
288 if (file_exists(git_path("MERGE_HEAD")))
289 die("cannot do a partial commit during a merge.");
291 memset(&partial, 0, sizeof(partial));
292 partial.strdup_paths = 1;
293 if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
297 if (read_cache() < 0)
298 die("cannot read the index");
300 fd = hold_locked_index(&index_lock, 1);
301 add_remove_files(&partial);
302 refresh_cache(REFRESH_QUIET);
303 if (write_cache(fd, active_cache, active_nr) ||
304 close_lock_file(&index_lock))
305 die("unable to write new_index file");
307 fd = hold_lock_file_for_update(&false_lock,
308 git_path("next-index-%d", getpid()), 1);
311 add_remove_files(&partial);
312 refresh_cache(REFRESH_QUIET);
314 if (write_cache(fd, active_cache, active_nr) ||
315 close_lock_file(&false_lock))
316 die("unable to write temporary index file");
317 return false_lock.filename;
320 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn)
324 wt_status_prepare(&s);
325 if (wt_status_relative_paths)
330 s.reference = "HEAD^1";
333 s.untracked = untracked_files;
334 s.index_file = index_file;
343 static const char sign_off_header[] = "Signed-off-by: ";
345 static int prepare_log_message(const char *index_file, const char *prefix)
348 int commitable, saved_color_setting;
355 strbuf_addbuf(&sb, &message);
356 } else if (logfile && !strcmp(logfile, "-")) {
358 fprintf(stderr, "(reading log message from standard input)\n");
359 if (strbuf_read(&sb, 0, 0) < 0)
360 die("could not read log from standard input");
361 } else if (logfile) {
362 if (strbuf_read_file(&sb, logfile, 0) < 0)
363 die("could not read log file '%s': %s",
364 logfile, strerror(errno));
365 } else if (use_message) {
366 buffer = strstr(use_message_buffer, "\n\n");
367 if (!buffer || buffer[2] == '\0')
368 die("commit has empty message");
369 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
370 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
371 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
372 die("could not read MERGE_MSG: %s", strerror(errno));
373 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
374 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
375 die("could not read SQUASH_MSG: %s", strerror(errno));
376 } else if (template_file && !stat(template_file, &statbuf)) {
377 if (strbuf_read_file(&sb, template_file, 0) < 0)
378 die("could not read %s: %s",
379 template_file, strerror(errno));
382 fp = fopen(git_path(commit_editmsg), "w");
384 die("could not open %s", git_path(commit_editmsg));
386 if (cleanup_mode != CLEANUP_NONE)
393 strbuf_init(&sob, 0);
394 strbuf_addstr(&sob, sign_off_header);
395 strbuf_addstr(&sob, fmt_name(getenv("GIT_COMMITTER_NAME"),
396 getenv("GIT_COMMITTER_EMAIL")));
397 strbuf_addch(&sob, '\n');
398 for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
400 if (prefixcmp(sb.buf + i, sob.buf)) {
401 if (prefixcmp(sb.buf + i, sign_off_header))
402 strbuf_addch(&sb, '\n');
403 strbuf_addbuf(&sb, &sob);
405 strbuf_release(&sob);
408 if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
409 die("could not write commit template: %s", strerror(errno));
415 unsigned char sha1[20];
416 const char *parent = "HEAD";
420 if (!active_nr && read_cache() < 0)
421 die("Cannot read index");
426 if (get_sha1(parent, sha1))
429 init_revisions(&rev, "");
431 setup_revisions(0, NULL, &rev, parent);
432 DIFF_OPT_SET(&rev.diffopt, QUIET);
433 DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
434 run_diff_index(&rev, 1 /* cached */);
436 return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
442 "# It looks like you may be committing a MERGE.\n"
443 "# If this is not correct, please remove the file\n"
447 git_path("MERGE_HEAD"));
451 "# Please enter the commit message for your changes.\n"
452 "# (Comment lines starting with '#' will ");
453 if (cleanup_mode == CLEANUP_ALL)
454 fprintf(fp, "not be included)\n");
455 else /* CLEANUP_SPACE, that is. */
456 fprintf(fp, "be kept.\n"
457 "# You can remove them yourself if you want to)\n");
458 if (only_include_assumed)
459 fprintf(fp, "# %s\n", only_include_assumed);
461 saved_color_setting = wt_status_use_color;
462 wt_status_use_color = 0;
463 commitable = run_status(fp, index_file, prefix, 1);
464 wt_status_use_color = saved_color_setting;
472 * Find out if the message starting at position 'start' in the strbuf
473 * contains only whitespace and Signed-off-by lines.
475 static int message_is_empty(struct strbuf *sb, int start)
481 if (cleanup_mode == CLEANUP_NONE && sb->len)
484 /* See if the template is just a prefix of the message. */
485 strbuf_init(&tmpl, 0);
486 if (template_file && strbuf_read_file(&tmpl, template_file, 0) > 0) {
487 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
488 if (start + tmpl.len <= sb->len &&
489 memcmp(tmpl.buf, sb->buf + start, tmpl.len) == 0)
492 strbuf_release(&tmpl);
494 /* Check if the rest is just whitespace and Signed-of-by's. */
495 for (i = start; i < sb->len; i++) {
496 nl = memchr(sb->buf + i, '\n', sb->len - i);
502 if (strlen(sign_off_header) <= eol - i &&
503 !prefixcmp(sb->buf + i, sign_off_header)) {
508 if (!isspace(sb->buf[i++]))
515 static void determine_author_info(struct strbuf *sb)
517 char *name, *email, *date;
519 name = getenv("GIT_AUTHOR_NAME");
520 email = getenv("GIT_AUTHOR_EMAIL");
521 date = getenv("GIT_AUTHOR_DATE");
524 const char *a, *lb, *rb, *eol;
526 a = strstr(use_message_buffer, "\nauthor ");
528 die("invalid commit: %s", use_message);
530 lb = strstr(a + 8, " <");
531 rb = strstr(a + 8, "> ");
532 eol = strchr(a + 8, '\n');
533 if (!lb || !rb || !eol)
534 die("invalid commit: %s", use_message);
536 name = xstrndup(a + 8, lb - (a + 8));
537 email = xstrndup(lb + 2, rb - (lb + 2));
538 date = xstrndup(rb + 2, eol - (rb + 2));
542 const char *lb = strstr(force_author, " <");
543 const char *rb = strchr(force_author, '>');
546 die("malformed --author parameter");
547 name = xstrndup(force_author, lb - force_author);
548 email = xstrndup(lb + 2, rb - (lb + 2));
551 strbuf_addf(sb, "author %s\n", fmt_ident(name, email, date, IDENT_ERROR_ON_NO_NAME));
554 static int parse_and_validate_options(int argc, const char *argv[],
555 const char * const usage[])
559 argc = parse_options(argc, argv, builtin_commit_options, usage, 0);
561 if (logfile || message.len || use_message)
566 if (get_sha1("HEAD", head_sha1))
569 if (!get_sha1("MERGE_HEAD", merge_head_sha1))
572 /* Sanity check options */
573 if (amend && initial_commit)
574 die("You have nothing to amend.");
575 if (amend && in_merge)
576 die("You are in the middle of a merge -- cannot amend.");
585 die("Only one of -c/-C/-F can be used.");
586 if (message.len && f > 0)
587 die("Option -m cannot be combined with -c/-C/-F.");
589 use_message = edit_message;
590 if (amend && !use_message)
591 use_message = "HEAD";
593 unsigned char sha1[20];
594 static char utf8[] = "UTF-8";
597 struct commit *commit;
599 if (get_sha1(use_message, sha1))
600 die("could not lookup commit %s", use_message);
601 commit = lookup_commit(sha1);
602 if (!commit || parse_commit(commit))
603 die("could not parse commit %s", use_message);
605 enc = strstr(commit->buffer, "\nencoding");
607 end = strchr(enc + 10, '\n');
608 enc = xstrndup(enc + 10, end - (enc + 10));
612 out_enc = git_commit_encoding ? git_commit_encoding : utf8;
614 if (strcmp(out_enc, enc))
616 reencode_string(commit->buffer, out_enc, enc);
619 * If we failed to reencode the buffer, just copy it
620 * byte for byte so the user can try to fix it up.
621 * This also handles the case where input and output
622 * encodings are identical.
624 if (use_message_buffer == NULL)
625 use_message_buffer = xstrdup(commit->buffer);
630 if (!!also + !!only + !!all + !!interactive > 1)
631 die("Only one of --include/--only/--all/--interactive can be used.");
632 if (argc == 0 && (also || (only && !amend)))
633 die("No paths with --include/--only does not make sense.");
634 if (argc == 0 && only && amend)
635 only_include_assumed = "Clever... amending the last one with dirty index.";
636 if (argc > 0 && !also && !only) {
637 only_include_assumed = "Explicit paths specified without -i nor -o; assuming --only paths...";
640 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
641 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
642 else if (!strcmp(cleanup_arg, "verbatim"))
643 cleanup_mode = CLEANUP_NONE;
644 else if (!strcmp(cleanup_arg, "whitespace"))
645 cleanup_mode = CLEANUP_SPACE;
646 else if (!strcmp(cleanup_arg, "strip"))
647 cleanup_mode = CLEANUP_ALL;
649 die("Invalid cleanup mode %s", cleanup_arg);
652 die("Paths with -a does not make sense.");
653 else if (interactive && argc > 0)
654 die("Paths with --interactive does not make sense.");
659 int cmd_status(int argc, const char **argv, const char *prefix)
661 const char *index_file;
664 git_config(git_status_config);
666 argc = parse_and_validate_options(argc, argv, builtin_status_usage);
668 index_file = prepare_index(argc, argv, prefix);
670 commitable = run_status(stdout, index_file, prefix, 0);
672 rollback_index_files();
674 return commitable ? 0 : 1;
677 static int run_hook(const char *index_file, const char *name, const char *arg)
679 struct child_process hook;
680 const char *argv[3], *env[2];
681 char index[PATH_MAX];
683 argv[0] = git_path("hooks/%s", name);
686 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
690 if (access(argv[0], X_OK) < 0)
693 memset(&hook, 0, sizeof(hook));
696 hook.stdout_to_stderr = 1;
699 return run_command(&hook);
702 static void print_summary(const char *prefix, const unsigned char *sha1)
705 struct commit *commit;
707 commit = lookup_commit(sha1);
709 die("couldn't look up newly created commit");
710 if (!commit || parse_commit(commit))
711 die("could not parse newly created commit");
713 init_revisions(&rev, prefix);
714 setup_revisions(0, NULL, &rev, NULL);
718 rev.diffopt.output_format =
719 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
721 rev.verbose_header = 1;
722 rev.show_root_diff = 1;
723 rev.commit_format = get_commit_format("format:%h: %s");
724 rev.always_show_header = 0;
725 rev.diffopt.detect_rename = 1;
726 rev.diffopt.rename_limit = 100;
727 rev.diffopt.break_opt = 0;
728 diff_setup_done(&rev.diffopt);
730 printf("Created %scommit ", initial_commit ? "initial " : "");
732 if (!log_tree_commit(&rev, commit)) {
733 struct strbuf buf = STRBUF_INIT;
734 format_commit_message(commit, "%h: %s", &buf);
735 printf("%s\n", buf.buf);
736 strbuf_release(&buf);
740 int git_commit_config(const char *k, const char *v)
742 if (!strcmp(k, "commit.template")) {
743 template_file = xstrdup(v);
747 return git_status_config(k, v);
750 static int is_a_merge(const unsigned char *sha1)
752 struct commit *commit = lookup_commit(sha1);
753 if (!commit || parse_commit(commit))
754 die("could not parse HEAD commit");
755 return !!(commit->parents && commit->parents->next);
758 static const char commit_utf8_warn[] =
759 "Warning: commit message does not conform to UTF-8.\n"
760 "You may want to amend it after fixing the message, or set the config\n"
761 "variable i18n.commitencoding to the encoding your project uses.\n";
763 static void add_parent(struct strbuf *sb, const unsigned char *sha1)
765 struct object *obj = parse_object(sha1);
766 const char *parent = sha1_to_hex(sha1);
768 die("Unable to find commit parent %s", parent);
769 if (obj->type != OBJ_COMMIT)
770 die("Parent %s isn't a proper commit", parent);
771 strbuf_addf(sb, "parent %s\n", parent);
774 int cmd_commit(int argc, const char **argv, const char *prefix)
778 const char *index_file, *reflog_msg;
780 unsigned char commit_sha1[20];
781 struct ref_lock *ref_lock;
783 git_config(git_commit_config);
785 argc = parse_and_validate_options(argc, argv, builtin_commit_usage);
787 index_file = prepare_index(argc, argv, prefix);
789 if (!no_verify && run_hook(index_file, "pre-commit", NULL)) {
790 rollback_index_files();
794 if (!prepare_log_message(index_file, prefix) && !in_merge &&
795 !allow_empty && !(amend && is_a_merge(head_sha1))) {
796 run_status(stdout, index_file, prefix, 0);
797 rollback_index_files();
798 unlink(commit_editmsg);
803 * Re-read the index as pre-commit hook could have updated it,
804 * and write it out as a tree.
807 read_cache_from(index_file);
808 if (!active_cache_tree)
809 active_cache_tree = cache_tree();
810 if (cache_tree_update(active_cache_tree,
811 active_cache, active_nr, 0, 0) < 0) {
812 rollback_index_files();
813 die("Error building trees");
820 strbuf_addf(&sb, "tree %s\n",
821 sha1_to_hex(active_cache_tree->sha1));
823 /* Determine parents */
824 if (initial_commit) {
825 reflog_msg = "commit (initial)";
827 struct commit_list *c;
828 struct commit *commit;
830 reflog_msg = "commit (amend)";
831 commit = lookup_commit(head_sha1);
832 if (!commit || parse_commit(commit))
833 die("could not parse HEAD commit");
835 for (c = commit->parents; c; c = c->next)
836 add_parent(&sb, c->item->object.sha1);
837 } else if (in_merge) {
841 reflog_msg = "commit (merge)";
842 add_parent(&sb, head_sha1);
844 fp = fopen(git_path("MERGE_HEAD"), "r");
846 die("could not open %s for reading: %s",
847 git_path("MERGE_HEAD"), strerror(errno));
848 while (strbuf_getline(&m, fp, '\n') != EOF) {
849 unsigned char sha1[20];
850 if (get_sha1_hex(m.buf, sha1) < 0)
851 die("Corrupt MERGE_HEAD file (%s)", m.buf);
852 add_parent(&sb, sha1);
857 reflog_msg = "commit";
858 strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
861 determine_author_info(&sb);
862 strbuf_addf(&sb, "committer %s\n", git_committer_info(IDENT_ERROR_ON_NO_NAME));
863 if (!is_encoding_utf8(git_commit_encoding))
864 strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
865 strbuf_addch(&sb, '\n');
867 /* Get the commit message and validate it */
870 char index[PATH_MAX];
871 const char *env[2] = { index, NULL };
872 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
873 launch_editor(git_path(commit_editmsg), NULL, env);
876 run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
877 rollback_index_files();
880 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
881 rollback_index_files();
882 die("could not read commit message");
885 /* Truncate the message just before the diff, if any. */
886 p = strstr(sb.buf, "\ndiff --git a/");
888 strbuf_setlen(&sb, p - sb.buf + 1);
890 if (cleanup_mode != CLEANUP_NONE)
891 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
892 if (sb.len < header_len || message_is_empty(&sb, header_len)) {
893 rollback_index_files();
894 die("no commit message? aborting commit.");
896 strbuf_addch(&sb, '\0');
897 if (is_encoding_utf8(git_commit_encoding) && !is_utf8(sb.buf))
898 fprintf(stderr, commit_utf8_warn);
900 if (write_sha1_file(sb.buf, sb.len - 1, commit_type, commit_sha1)) {
901 rollback_index_files();
902 die("failed to write commit object");
905 ref_lock = lock_any_ref_for_update("HEAD",
906 initial_commit ? NULL : head_sha1,
909 nl = strchr(sb.buf + header_len, '\n');
911 strbuf_setlen(&sb, nl + 1 - sb.buf);
913 strbuf_addch(&sb, '\n');
914 strbuf_remove(&sb, 0, header_len);
915 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
916 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
919 rollback_index_files();
920 die("cannot lock HEAD ref");
922 if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
923 rollback_index_files();
924 die("cannot update HEAD ref");
927 unlink(git_path("MERGE_HEAD"));
928 unlink(git_path("MERGE_MSG"));
930 commit_index_files();
933 run_hook(get_index_file(), "post-commit", NULL);
935 print_summary(prefix, commit_sha1);