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"
17 #include "wt-status.h"
18 #include "run-command.h"
23 #include "parse-options.h"
24 #include "string-list.h"
26 #include "unpack-trees.h"
28 #include "submodule.h"
29 #include "gpg-interface.h"
31 #include "sequencer.h"
32 #include "notes-utils.h"
35 static const char * const builtin_commit_usage[] = {
36 N_("git commit [options] [--] <pathspec>..."),
40 static const char * const builtin_status_usage[] = {
41 N_("git status [options] [--] <pathspec>..."),
45 static const char implicit_ident_advice[] =
46 N_("Your name and email address were configured automatically based\n"
47 "on your username and hostname. Please check that they are accurate.\n"
48 "You can suppress this message by setting them explicitly:\n"
50 " git config --global user.name \"Your Name\"\n"
51 " git config --global user.email you@example.com\n"
53 "After doing this, you may fix the identity used for this commit with:\n"
55 " git commit --amend --reset-author\n");
57 static const char empty_amend_advice[] =
58 N_("You asked to amend the most recent commit, but doing so would make\n"
59 "it empty. You can repeat your command with --allow-empty, or you can\n"
60 "remove the commit entirely with \"git reset HEAD^\".\n");
62 static const char empty_cherry_pick_advice[] =
63 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
64 "If you wish to commit it anyway, use:\n"
66 " git commit --allow-empty\n"
69 static const char empty_cherry_pick_advice_single[] =
70 N_("Otherwise, please use 'git reset'\n");
72 static const char empty_cherry_pick_advice_multi[] =
73 N_("If you wish to skip this commit, use:\n"
77 "Then \"git cherry-pick --continue\" will resume cherry-picking\n"
78 "the remaining commits.\n");
80 static const char *use_message_buffer;
81 static const char commit_editmsg[] = "COMMIT_EDITMSG";
82 static struct lock_file index_lock; /* real index */
83 static struct lock_file false_lock; /* used only for partial commits */
90 static const char *logfile, *force_author;
91 static const char *template_file;
93 * The _message variables are commit names from which to take
94 * the commit message and/or authorship.
96 static const char *author_message, *author_message_buffer;
97 static char *edit_message, *use_message;
98 static char *fixup_message, *squash_message;
99 static int all, also, interactive, patch_interactive, only, amend, signoff;
100 static int edit_flag = -1; /* unspecified */
101 static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
102 static int no_post_rewrite, allow_empty_message;
103 static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
104 static char *sign_commit;
107 * The default commit message cleanup mode will remove the lines
108 * beginning with # (shell comments) and leading and trailing
109 * whitespaces (empty lines or containing only whitespaces)
110 * if editor is used, and only the whitespaces if the message
111 * is specified explicitly.
118 static const char *cleanup_arg;
120 static enum commit_whence whence;
121 static int sequencer_in_use;
122 static int use_editor = 1, include_status = 1;
123 static int show_ignored_in_status, have_option_m;
124 static const char *only_include_assumed;
125 static struct strbuf message = STRBUF_INIT;
127 static enum status_format {
128 STATUS_FORMAT_NONE = 0,
131 STATUS_FORMAT_PORCELAIN,
133 STATUS_FORMAT_UNSPECIFIED
134 } status_format = STATUS_FORMAT_UNSPECIFIED;
136 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
138 struct strbuf *buf = opt->value;
141 strbuf_setlen(buf, 0);
145 strbuf_addch(buf, '\n');
146 strbuf_addstr(buf, arg);
147 strbuf_complete_line(buf);
152 static void determine_whence(struct wt_status *s)
154 if (file_exists(git_path("MERGE_HEAD")))
156 else if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
157 whence = FROM_CHERRY_PICK;
158 if (file_exists(git_path("sequencer")))
159 sequencer_in_use = 1;
162 whence = FROM_COMMIT;
167 static void rollback_index_files(void)
169 switch (commit_style) {
171 break; /* nothing to do */
173 rollback_lock_file(&index_lock);
176 rollback_lock_file(&index_lock);
177 rollback_lock_file(&false_lock);
182 static int commit_index_files(void)
186 switch (commit_style) {
188 break; /* nothing to do */
190 err = commit_lock_file(&index_lock);
193 err = commit_lock_file(&index_lock);
194 rollback_lock_file(&false_lock);
202 * Take a union of paths in the index and the named tree (typically, "HEAD"),
203 * and return the paths that match the given pattern in list.
205 static int list_paths(struct string_list *list, const char *with_tree,
206 const char *prefix, const char **pattern)
214 for (i = 0; pattern[i]; i++)
219 char *max_prefix = common_prefix(pattern);
220 overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
224 for (i = 0; i < active_nr; i++) {
225 const struct cache_entry *ce = active_cache[i];
226 struct string_list_item *item;
228 if (ce->ce_flags & CE_UPDATE)
230 if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
232 item = string_list_insert(list, ce->name);
233 if (ce_skip_worktree(ce))
234 item->util = item; /* better a valid pointer than a fake one */
237 return report_path_error(m, pattern, prefix);
240 static void add_remove_files(struct string_list *list)
243 for (i = 0; i < list->nr; i++) {
245 struct string_list_item *p = &(list->items[i]);
247 /* p->util is skip-worktree */
251 if (!lstat(p->string, &st)) {
252 if (add_to_cache(p->string, &st, 0))
253 die(_("updating files failed"));
255 remove_file_from_cache(p->string);
259 static void create_base_index(const struct commit *current_head)
262 struct unpack_trees_options opts;
270 memset(&opts, 0, sizeof(opts));
274 opts.src_index = &the_index;
275 opts.dst_index = &the_index;
277 opts.fn = oneway_merge;
278 tree = parse_tree_indirect(current_head->object.sha1);
280 die(_("failed to unpack HEAD tree object"));
282 init_tree_desc(&t, tree->buffer, tree->size);
283 if (unpack_trees(1, &t, &opts))
284 exit(128); /* We've already reported the error, finish dying */
287 static void refresh_cache_or_die(int refresh_flags)
290 * refresh_flags contains REFRESH_QUIET, so the only errors
291 * are for unmerged entries.
293 if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
294 die_resolve_conflict("commit");
297 static char *prepare_index(int argc, const char **argv, const char *prefix,
298 const struct commit *current_head, int is_status)
301 struct string_list partial;
302 const char **pathspec = NULL;
303 char *old_index_env = NULL;
304 int refresh_flags = REFRESH_QUIET;
307 refresh_flags |= REFRESH_UNMERGED;
310 pathspec = get_pathspec(prefix, argv);
312 if (read_cache_preload(pathspec) < 0)
313 die(_("index file corrupt"));
316 fd = hold_locked_index(&index_lock, 1);
318 refresh_cache_or_die(refresh_flags);
320 if (write_cache(fd, active_cache, active_nr) ||
321 close_lock_file(&index_lock))
322 die(_("unable to create temporary index"));
324 old_index_env = getenv(INDEX_ENVIRONMENT);
325 setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
327 if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
328 die(_("interactive add failed"));
330 if (old_index_env && *old_index_env)
331 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
333 unsetenv(INDEX_ENVIRONMENT);
336 read_cache_from(index_lock.filename);
338 commit_style = COMMIT_NORMAL;
339 return index_lock.filename;
343 * Non partial, non as-is commit.
345 * (1) get the real index;
346 * (2) update the_index as necessary;
347 * (3) write the_index out to the real index (still locked);
348 * (4) return the name of the locked index file.
350 * The caller should run hooks on the locked real index, and
351 * (A) if all goes well, commit the real index;
352 * (B) on failure, rollback the real index.
354 if (all || (also && pathspec && *pathspec)) {
355 fd = hold_locked_index(&index_lock, 1);
356 add_files_to_cache(also ? prefix : NULL, pathspec, 0);
357 refresh_cache_or_die(refresh_flags);
358 update_main_cache_tree(WRITE_TREE_SILENT);
359 if (write_cache(fd, active_cache, active_nr) ||
360 close_lock_file(&index_lock))
361 die(_("unable to write new_index file"));
362 commit_style = COMMIT_NORMAL;
363 return index_lock.filename;
369 * (1) return the name of the real index file.
371 * The caller should run hooks on the real index,
372 * and create commit from the_index.
373 * We still need to refresh the index here.
375 if (!only && (!pathspec || !*pathspec)) {
376 fd = hold_locked_index(&index_lock, 1);
377 refresh_cache_or_die(refresh_flags);
378 if (active_cache_changed) {
379 update_main_cache_tree(WRITE_TREE_SILENT);
380 if (write_cache(fd, active_cache, active_nr) ||
381 commit_locked_index(&index_lock))
382 die(_("unable to write new_index file"));
384 rollback_lock_file(&index_lock);
386 commit_style = COMMIT_AS_IS;
387 return get_index_file();
393 * (0) find the set of affected paths;
394 * (1) get lock on the real index file;
395 * (2) update the_index with the given paths;
396 * (3) write the_index out to the real index (still locked);
397 * (4) get lock on the false index file;
398 * (5) reset the_index from HEAD;
399 * (6) update the_index the same way as (2);
400 * (7) write the_index out to the false index file;
401 * (8) return the name of the false index file (still locked);
403 * The caller should run hooks on the locked false index, and
404 * create commit from it. Then
405 * (A) if all goes well, commit the real index;
406 * (B) on failure, rollback the real index;
407 * In either case, rollback the false index.
409 commit_style = COMMIT_PARTIAL;
411 if (whence != FROM_COMMIT) {
412 if (whence == FROM_MERGE)
413 die(_("cannot do a partial commit during a merge."));
414 else if (whence == FROM_CHERRY_PICK)
415 die(_("cannot do a partial commit during a cherry-pick."));
418 memset(&partial, 0, sizeof(partial));
419 partial.strdup_strings = 1;
420 if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, pathspec))
424 if (read_cache() < 0)
425 die(_("cannot read the index"));
427 fd = hold_locked_index(&index_lock, 1);
428 add_remove_files(&partial);
429 refresh_cache(REFRESH_QUIET);
430 if (write_cache(fd, active_cache, active_nr) ||
431 close_lock_file(&index_lock))
432 die(_("unable to write new_index file"));
434 fd = hold_lock_file_for_update(&false_lock,
435 git_path("next-index-%"PRIuMAX,
436 (uintmax_t) getpid()),
439 create_base_index(current_head);
440 add_remove_files(&partial);
441 refresh_cache(REFRESH_QUIET);
443 if (write_cache(fd, active_cache, active_nr) ||
444 close_lock_file(&false_lock))
445 die(_("unable to write temporary index file"));
448 read_cache_from(false_lock.filename);
450 return false_lock.filename;
453 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
456 unsigned char sha1[20];
458 if (s->relative_paths)
463 s->reference = "HEAD^1";
465 s->verbose = verbose;
466 s->index_file = index_file;
469 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
471 wt_status_collect(s);
473 switch (status_format) {
474 case STATUS_FORMAT_SHORT:
475 wt_shortstatus_print(s);
477 case STATUS_FORMAT_PORCELAIN:
478 wt_porcelain_print(s);
480 case STATUS_FORMAT_UNSPECIFIED:
481 die("BUG: finalize_deferred_config() should have been called");
483 case STATUS_FORMAT_NONE:
484 case STATUS_FORMAT_LONG:
489 return s->commitable;
492 static int is_a_merge(const struct commit *current_head)
494 return !!(current_head->parents && current_head->parents->next);
497 static void export_one(const char *var, const char *s, const char *e, int hack)
499 struct strbuf buf = STRBUF_INIT;
501 strbuf_addch(&buf, hack);
502 strbuf_addf(&buf, "%.*s", (int)(e - s), s);
503 setenv(var, buf.buf, 1);
504 strbuf_release(&buf);
507 static int sane_ident_split(struct ident_split *person)
509 if (!person->name_begin || !person->name_end ||
510 person->name_begin == person->name_end)
511 return 0; /* no human readable name */
512 if (!person->mail_begin || !person->mail_end ||
513 person->mail_begin == person->mail_end)
514 return 0; /* no usable mail */
515 if (!person->date_begin || !person->date_end ||
516 !person->tz_begin || !person->tz_end)
521 static void determine_author_info(struct strbuf *author_ident)
523 char *name, *email, *date;
524 struct ident_split author;
526 name = getenv("GIT_AUTHOR_NAME");
527 email = getenv("GIT_AUTHOR_EMAIL");
528 date = getenv("GIT_AUTHOR_DATE");
530 if (author_message) {
531 const char *a, *lb, *rb, *eol;
534 a = strstr(author_message_buffer, "\nauthor ");
536 die(_("invalid commit: %s"), author_message);
538 lb = strchrnul(a + strlen("\nauthor "), '<');
539 rb = strchrnul(lb, '>');
540 eol = strchrnul(rb, '\n');
541 if (!*lb || !*rb || !*eol)
542 die(_("invalid commit: %s"), author_message);
544 if (lb == a + strlen("\nauthor "))
545 /* \nauthor <foo@example.com> */
546 name = xcalloc(1, 1);
548 name = xmemdupz(a + strlen("\nauthor "),
550 (a + strlen("\nauthor "))));
551 email = xmemdupz(lb + strlen("<"), rb - (lb + strlen("<")));
552 len = eol - (rb + strlen("> "));
553 date = xmalloc(len + 2);
555 memcpy(date + 1, rb + strlen("> "), len);
556 date[len + 1] = '\0';
560 const char *lb = strstr(force_author, " <");
561 const char *rb = strchr(force_author, '>');
564 die(_("malformed --author parameter"));
565 name = xstrndup(force_author, lb - force_author);
566 email = xstrndup(lb + 2, rb - (lb + 2));
571 strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
572 if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
573 sane_ident_split(&author)) {
574 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
575 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
576 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
580 static char *cut_ident_timestamp_part(char *string)
582 char *ket = strrchr(string, '>');
583 if (!ket || ket[1] != ' ')
584 die(_("Malformed ident string: '%s'"), string);
589 static int prepare_to_commit(const char *index_file, const char *prefix,
590 struct commit *current_head,
592 struct strbuf *author_ident)
595 struct strbuf committer_ident = STRBUF_INIT;
596 int commitable, saved_color_setting;
597 struct strbuf sb = STRBUF_INIT;
599 const char *hook_arg1 = NULL;
600 const char *hook_arg2 = NULL;
602 int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
604 /* This checks and barfs if author is badly specified */
605 determine_author_info(author_ident);
607 if (!no_verify && run_hook(index_file, "pre-commit", NULL))
610 if (squash_message) {
612 * Insert the proper subject line before other commit
613 * message options add their content.
615 if (use_message && !strcmp(use_message, squash_message))
616 strbuf_addstr(&sb, "squash! ");
618 struct pretty_print_context ctx = {0};
620 c = lookup_commit_reference_by_name(squash_message);
622 die(_("could not lookup commit %s"), squash_message);
623 ctx.output_encoding = get_commit_output_encoding();
624 format_commit_message(c, "squash! %s\n\n", &sb,
630 strbuf_addbuf(&sb, &message);
631 hook_arg1 = "message";
632 } else if (logfile && !strcmp(logfile, "-")) {
634 fprintf(stderr, _("(reading log message from standard input)\n"));
635 if (strbuf_read(&sb, 0, 0) < 0)
636 die_errno(_("could not read log from standard input"));
637 hook_arg1 = "message";
638 } else if (logfile) {
639 if (strbuf_read_file(&sb, logfile, 0) < 0)
640 die_errno(_("could not read log file '%s'"),
642 hook_arg1 = "message";
643 } else if (use_message) {
644 buffer = strstr(use_message_buffer, "\n\n");
645 if (!use_editor && (!buffer || buffer[2] == '\0'))
646 die(_("commit has empty message"));
647 strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
648 hook_arg1 = "commit";
649 hook_arg2 = use_message;
650 } else if (fixup_message) {
651 struct pretty_print_context ctx = {0};
652 struct commit *commit;
653 commit = lookup_commit_reference_by_name(fixup_message);
655 die(_("could not lookup commit %s"), fixup_message);
656 ctx.output_encoding = get_commit_output_encoding();
657 format_commit_message(commit, "fixup! %s\n\n",
659 hook_arg1 = "message";
660 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
661 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
662 die_errno(_("could not read MERGE_MSG"));
664 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
665 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
666 die_errno(_("could not read SQUASH_MSG"));
667 hook_arg1 = "squash";
668 } else if (template_file) {
669 if (strbuf_read_file(&sb, template_file, 0) < 0)
670 die_errno(_("could not read '%s'"), template_file);
671 hook_arg1 = "template";
672 clean_message_contents = 0;
676 * The remaining cases don't modify the template message, but
677 * just set the argument(s) to the prepare-commit-msg hook.
679 else if (whence == FROM_MERGE)
681 else if (whence == FROM_CHERRY_PICK) {
682 hook_arg1 = "commit";
683 hook_arg2 = "CHERRY_PICK_HEAD";
686 if (squash_message) {
688 * If squash_commit was used for the commit subject,
689 * then we're possibly hijacking other commit log options.
690 * Reset the hook args to tell the real story.
692 hook_arg1 = "message";
696 s->fp = fopen(git_path(commit_editmsg), "w");
698 die_errno(_("could not open '%s'"), git_path(commit_editmsg));
700 if (clean_message_contents)
705 * See if we have a Conflicts: block at the end. If yes, count
706 * its size, so we can ignore it.
708 int ignore_footer = 0;
709 int i, eol, previous = 0;
712 for (i = 0; i < sb.len; i++) {
713 nl = memchr(sb.buf + i, '\n', sb.len - i);
718 if (!prefixcmp(sb.buf + previous, "\nConflicts:\n")) {
719 ignore_footer = sb.len - previous;
727 append_signoff(&sb, ignore_footer, 0);
730 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
731 die_errno(_("could not write commit template"));
735 /* This checks if committer ident is explicitly given */
736 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
737 if (use_editor && include_status) {
738 char *ai_tmp, *ci_tmp;
739 if (whence != FROM_COMMIT)
740 status_printf_ln(s, GIT_COLOR_NORMAL,
743 "It looks like you may be committing a merge.\n"
744 "If this is not correct, please remove the file\n"
748 "It looks like you may be committing a cherry-pick.\n"
749 "If this is not correct, please remove the file\n"
752 git_path(whence == FROM_MERGE
754 : "CHERRY_PICK_HEAD"));
756 fprintf(s->fp, "\n");
757 if (cleanup_mode == CLEANUP_ALL)
758 status_printf(s, GIT_COLOR_NORMAL,
759 _("Please enter the commit message for your changes."
760 " Lines starting\nwith '%c' will be ignored, and an empty"
761 " message aborts the commit.\n"), comment_line_char);
762 else /* CLEANUP_SPACE, that is. */
763 status_printf(s, GIT_COLOR_NORMAL,
764 _("Please enter the commit message for your changes."
766 "with '%c' will be kept; you may remove them"
767 " yourself if you want to.\n"
768 "An empty message aborts the commit.\n"), comment_line_char);
769 if (only_include_assumed)
770 status_printf_ln(s, GIT_COLOR_NORMAL,
771 "%s", only_include_assumed);
773 ai_tmp = cut_ident_timestamp_part(author_ident->buf);
774 ci_tmp = cut_ident_timestamp_part(committer_ident.buf);
775 if (strcmp(author_ident->buf, committer_ident.buf))
776 status_printf_ln(s, GIT_COLOR_NORMAL,
779 ident_shown++ ? "" : "\n",
782 if (!committer_ident_sufficiently_given())
783 status_printf_ln(s, GIT_COLOR_NORMAL,
786 ident_shown++ ? "" : "\n",
787 committer_ident.buf);
790 status_printf_ln(s, GIT_COLOR_NORMAL, "");
792 saved_color_setting = s->use_color;
794 commitable = run_status(s->fp, index_file, prefix, 1, s);
795 s->use_color = saved_color_setting;
800 unsigned char sha1[20];
801 const char *parent = "HEAD";
803 if (!active_nr && read_cache() < 0)
804 die(_("Cannot read index"));
809 if (get_sha1(parent, sha1))
810 commitable = !!active_nr;
812 commitable = index_differs_from(parent, 0);
814 strbuf_release(&committer_ident);
819 * Reject an attempt to record a non-merge empty commit without
820 * explicit --allow-empty. In the cherry-pick case, it may be
821 * empty due to conflict resolution, which the user should okay.
823 if (!commitable && whence != FROM_MERGE && !allow_empty &&
824 !(amend && is_a_merge(current_head))) {
825 run_status(stdout, index_file, prefix, 0, s);
827 fputs(_(empty_amend_advice), stderr);
828 else if (whence == FROM_CHERRY_PICK) {
829 fputs(_(empty_cherry_pick_advice), stderr);
830 if (!sequencer_in_use)
831 fputs(_(empty_cherry_pick_advice_single), stderr);
833 fputs(_(empty_cherry_pick_advice_multi), stderr);
839 * Re-read the index as pre-commit hook could have updated it,
840 * and write it out as a tree. We must do this before we invoke
841 * the editor and after we invoke run_status above.
844 read_cache_from(index_file);
845 if (update_main_cache_tree(0)) {
846 error(_("Error building trees"));
850 if (run_hook(index_file, "prepare-commit-msg",
851 git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
855 char index[PATH_MAX];
856 const char *env[2] = { NULL };
858 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
859 if (launch_editor(git_path(commit_editmsg), NULL, env)) {
861 _("Please supply the message using either -m or -F option.\n"));
867 run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
874 static int rest_is_empty(struct strbuf *sb, int start)
879 /* Check if the rest is just whitespace and Signed-of-by's. */
880 for (i = start; i < sb->len; i++) {
881 nl = memchr(sb->buf + i, '\n', sb->len - i);
887 if (strlen(sign_off_header) <= eol - i &&
888 !prefixcmp(sb->buf + i, sign_off_header)) {
893 if (!isspace(sb->buf[i++]))
901 * Find out if the message in the strbuf contains only whitespace and
902 * Signed-off-by lines.
904 static int message_is_empty(struct strbuf *sb)
906 if (cleanup_mode == CLEANUP_NONE && sb->len)
908 return rest_is_empty(sb, 0);
912 * See if the user edited the message in the editor or left what
913 * was in the template intact
915 static int template_untouched(struct strbuf *sb)
917 struct strbuf tmpl = STRBUF_INIT;
920 if (cleanup_mode == CLEANUP_NONE && sb->len)
923 if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
926 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
927 start = (char *)skip_prefix(sb->buf, tmpl.buf);
930 strbuf_release(&tmpl);
931 return rest_is_empty(sb, start - sb->buf);
934 static const char *find_author_by_nickname(const char *name)
936 struct rev_info revs;
937 struct commit *commit;
938 struct strbuf buf = STRBUF_INIT;
939 struct string_list mailmap = STRING_LIST_INIT_NODUP;
943 init_revisions(&revs, NULL);
944 strbuf_addf(&buf, "--author=%s", name);
949 setup_revisions(ac, av, &revs, NULL);
950 revs.mailmap = &mailmap;
951 read_mailmap(revs.mailmap, NULL);
953 prepare_revision_walk(&revs);
954 commit = get_revision(&revs);
956 struct pretty_print_context ctx = {0};
957 ctx.date_mode = DATE_NORMAL;
958 strbuf_release(&buf);
959 format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
960 clear_mailmap(&mailmap);
961 return strbuf_detach(&buf, NULL);
963 die(_("No existing author found with '%s'"), name);
967 static void handle_untracked_files_arg(struct wt_status *s)
969 if (!untracked_files_arg)
970 ; /* default already initialized */
971 else if (!strcmp(untracked_files_arg, "no"))
972 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
973 else if (!strcmp(untracked_files_arg, "normal"))
974 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
975 else if (!strcmp(untracked_files_arg, "all"))
976 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
978 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
981 static const char *read_commit_message(const char *name)
984 struct commit *commit;
986 commit = lookup_commit_reference_by_name(name);
988 die(_("could not lookup commit %s"), name);
989 out_enc = get_commit_output_encoding();
990 return logmsg_reencode(commit, NULL, out_enc);
994 * Enumerate what needs to be propagated when --porcelain
995 * is not in effect here.
997 static struct status_deferred_config {
998 enum status_format status_format;
1000 } status_deferred_config = {
1001 STATUS_FORMAT_UNSPECIFIED,
1002 -1 /* unspecified */
1005 static void finalize_deferred_config(struct wt_status *s)
1007 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
1008 !s->null_termination);
1010 if (s->null_termination) {
1011 if (status_format == STATUS_FORMAT_NONE ||
1012 status_format == STATUS_FORMAT_UNSPECIFIED)
1013 status_format = STATUS_FORMAT_PORCELAIN;
1014 else if (status_format == STATUS_FORMAT_LONG)
1015 die(_("--long and -z are incompatible"));
1018 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1019 status_format = status_deferred_config.status_format;
1020 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1021 status_format = STATUS_FORMAT_NONE;
1023 if (use_deferred_config && s->show_branch < 0)
1024 s->show_branch = status_deferred_config.show_branch;
1025 if (s->show_branch < 0)
1029 static int parse_and_validate_options(int argc, const char *argv[],
1030 const struct option *options,
1031 const char * const usage[],
1033 struct commit *current_head,
1034 struct wt_status *s)
1038 argc = parse_options(argc, argv, prefix, options, usage, 0);
1039 finalize_deferred_config(s);
1041 if (force_author && !strchr(force_author, '>'))
1042 force_author = find_author_by_nickname(force_author);
1044 if (force_author && renew_authorship)
1045 die(_("Using both --reset-author and --author does not make sense"));
1047 if (logfile || have_option_m || use_message || fixup_message)
1050 use_editor = edit_flag;
1052 setenv("GIT_EDITOR", ":", 1);
1054 /* Sanity check options */
1055 if (amend && !current_head)
1056 die(_("You have nothing to amend."));
1057 if (amend && whence != FROM_COMMIT) {
1058 if (whence == FROM_MERGE)
1059 die(_("You are in the middle of a merge -- cannot amend."));
1060 else if (whence == FROM_CHERRY_PICK)
1061 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1063 if (fixup_message && squash_message)
1064 die(_("Options --squash and --fixup cannot be used together"));
1074 die(_("Only one of -c/-C/-F/--fixup can be used."));
1075 if (message.len && f > 0)
1076 die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
1077 if (f || message.len)
1078 template_file = NULL;
1080 use_message = edit_message;
1081 if (amend && !use_message && !fixup_message)
1082 use_message = "HEAD";
1083 if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship)
1084 die(_("--reset-author can be used only with -C, -c or --amend."));
1086 use_message_buffer = read_commit_message(use_message);
1087 if (!renew_authorship) {
1088 author_message = use_message;
1089 author_message_buffer = use_message_buffer;
1092 if (whence == FROM_CHERRY_PICK && !renew_authorship) {
1093 author_message = "CHERRY_PICK_HEAD";
1094 author_message_buffer = read_commit_message(author_message);
1097 if (patch_interactive)
1100 if (!!also + !!only + !!all + !!interactive > 1)
1101 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1102 if (argc == 0 && (also || (only && !amend)))
1103 die(_("No paths with --include/--only does not make sense."));
1104 if (argc == 0 && only && amend)
1105 only_include_assumed = _("Clever... amending the last one with dirty index.");
1106 if (argc > 0 && !also && !only)
1107 only_include_assumed = _("Explicit paths specified without -i nor -o; assuming --only paths...");
1108 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
1109 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
1110 else if (!strcmp(cleanup_arg, "verbatim"))
1111 cleanup_mode = CLEANUP_NONE;
1112 else if (!strcmp(cleanup_arg, "whitespace"))
1113 cleanup_mode = CLEANUP_SPACE;
1114 else if (!strcmp(cleanup_arg, "strip"))
1115 cleanup_mode = CLEANUP_ALL;
1117 die(_("Invalid cleanup mode %s"), cleanup_arg);
1119 handle_untracked_files_arg(s);
1121 if (all && argc > 0)
1122 die(_("Paths with -a does not make sense."));
1124 if (status_format != STATUS_FORMAT_NONE)
1130 static int dry_run_commit(int argc, const char **argv, const char *prefix,
1131 const struct commit *current_head, struct wt_status *s)
1134 const char *index_file;
1136 index_file = prepare_index(argc, argv, prefix, current_head, 1);
1137 commitable = run_status(stdout, index_file, prefix, 0, s);
1138 rollback_index_files();
1140 return commitable ? 0 : 1;
1143 static int parse_status_slot(const char *var, int offset)
1145 if (!strcasecmp(var+offset, "header"))
1146 return WT_STATUS_HEADER;
1147 if (!strcasecmp(var+offset, "branch"))
1148 return WT_STATUS_ONBRANCH;
1149 if (!strcasecmp(var+offset, "updated")
1150 || !strcasecmp(var+offset, "added"))
1151 return WT_STATUS_UPDATED;
1152 if (!strcasecmp(var+offset, "changed"))
1153 return WT_STATUS_CHANGED;
1154 if (!strcasecmp(var+offset, "untracked"))
1155 return WT_STATUS_UNTRACKED;
1156 if (!strcasecmp(var+offset, "nobranch"))
1157 return WT_STATUS_NOBRANCH;
1158 if (!strcasecmp(var+offset, "unmerged"))
1159 return WT_STATUS_UNMERGED;
1163 static int git_status_config(const char *k, const char *v, void *cb)
1165 struct wt_status *s = cb;
1167 if (!prefixcmp(k, "column."))
1168 return git_column_config(k, v, "status", &s->colopts);
1169 if (!strcmp(k, "status.submodulesummary")) {
1171 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1172 if (is_bool && s->submodule_summary)
1173 s->submodule_summary = -1;
1176 if (!strcmp(k, "status.short")) {
1177 if (git_config_bool(k, v))
1178 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
1180 status_deferred_config.status_format = STATUS_FORMAT_NONE;
1183 if (!strcmp(k, "status.branch")) {
1184 status_deferred_config.show_branch = git_config_bool(k, v);
1187 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1188 s->use_color = git_config_colorbool(k, v);
1191 if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
1192 int slot = parse_status_slot(k, 13);
1196 return config_error_nonbool(k);
1197 color_parse(v, k, s->color_palette[slot]);
1200 if (!strcmp(k, "status.relativepaths")) {
1201 s->relative_paths = git_config_bool(k, v);
1204 if (!strcmp(k, "status.showuntrackedfiles")) {
1206 return config_error_nonbool(k);
1207 else if (!strcmp(v, "no"))
1208 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1209 else if (!strcmp(v, "normal"))
1210 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1211 else if (!strcmp(v, "all"))
1212 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1214 return error(_("Invalid untracked files mode '%s'"), v);
1217 return git_diff_ui_config(k, v, NULL);
1220 int cmd_status(int argc, const char **argv, const char *prefix)
1222 static struct wt_status s;
1224 unsigned char sha1[20];
1225 static struct option builtin_status_options[] = {
1226 OPT__VERBOSE(&verbose, N_("be verbose")),
1227 OPT_SET_INT('s', "short", &status_format,
1228 N_("show status concisely"), STATUS_FORMAT_SHORT),
1229 OPT_BOOL('b', "branch", &s.show_branch,
1230 N_("show branch information")),
1231 OPT_SET_INT(0, "porcelain", &status_format,
1232 N_("machine-readable output"),
1233 STATUS_FORMAT_PORCELAIN),
1234 OPT_SET_INT(0, "long", &status_format,
1235 N_("show status in long format (default)"),
1236 STATUS_FORMAT_LONG),
1237 OPT_BOOLEAN('z', "null", &s.null_termination,
1238 N_("terminate entries with NUL")),
1239 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1241 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1242 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1243 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status,
1244 N_("show ignored files")),
1245 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1246 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1247 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1248 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
1252 if (argc == 2 && !strcmp(argv[1], "-h"))
1253 usage_with_options(builtin_status_usage, builtin_status_options);
1255 wt_status_prepare(&s);
1256 gitmodules_config();
1257 git_config(git_status_config, &s);
1258 determine_whence(&s);
1259 argc = parse_options(argc, argv, prefix,
1260 builtin_status_options,
1261 builtin_status_usage, 0);
1262 finalize_colopts(&s.colopts, -1);
1263 finalize_deferred_config(&s);
1265 handle_untracked_files_arg(&s);
1266 if (show_ignored_in_status)
1267 s.show_ignored_files = 1;
1269 s.pathspec = get_pathspec(prefix, argv);
1271 read_cache_preload(s.pathspec);
1272 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
1274 fd = hold_locked_index(&index_lock, 0);
1276 update_index_if_able(&the_index, &index_lock);
1278 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
1279 s.ignore_submodule_arg = ignore_submodule_arg;
1280 wt_status_collect(&s);
1282 if (s.relative_paths)
1285 switch (status_format) {
1286 case STATUS_FORMAT_SHORT:
1287 wt_shortstatus_print(&s);
1289 case STATUS_FORMAT_PORCELAIN:
1290 wt_porcelain_print(&s);
1292 case STATUS_FORMAT_UNSPECIFIED:
1293 die("BUG: finalize_deferred_config() should have been called");
1295 case STATUS_FORMAT_NONE:
1296 case STATUS_FORMAT_LONG:
1297 s.verbose = verbose;
1298 s.ignore_submodule_arg = ignore_submodule_arg;
1299 wt_status_print(&s);
1305 static void print_summary(const char *prefix, const unsigned char *sha1,
1308 struct rev_info rev;
1309 struct commit *commit;
1310 struct strbuf format = STRBUF_INIT;
1311 unsigned char junk_sha1[20];
1313 struct pretty_print_context pctx = {0};
1314 struct strbuf author_ident = STRBUF_INIT;
1315 struct strbuf committer_ident = STRBUF_INIT;
1317 commit = lookup_commit(sha1);
1319 die(_("couldn't look up newly created commit"));
1320 if (!commit || parse_commit(commit))
1321 die(_("could not parse newly created commit"));
1323 strbuf_addstr(&format, "format:%h] %s");
1325 format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
1326 format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
1327 if (strbuf_cmp(&author_ident, &committer_ident)) {
1328 strbuf_addstr(&format, "\n Author: ");
1329 strbuf_addbuf_percentquote(&format, &author_ident);
1331 if (!committer_ident_sufficiently_given()) {
1332 strbuf_addstr(&format, "\n Committer: ");
1333 strbuf_addbuf_percentquote(&format, &committer_ident);
1334 if (advice_implicit_identity) {
1335 strbuf_addch(&format, '\n');
1336 strbuf_addstr(&format, _(implicit_ident_advice));
1339 strbuf_release(&author_ident);
1340 strbuf_release(&committer_ident);
1342 init_revisions(&rev, prefix);
1343 setup_revisions(0, NULL, &rev, NULL);
1346 rev.diffopt.output_format =
1347 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1349 rev.verbose_header = 1;
1350 rev.show_root_diff = 1;
1351 get_commit_format(format.buf, &rev);
1352 rev.always_show_header = 0;
1353 rev.diffopt.detect_rename = 1;
1354 rev.diffopt.break_opt = 0;
1355 diff_setup_done(&rev.diffopt);
1357 head = resolve_ref_unsafe("HEAD", junk_sha1, 0, NULL);
1359 !prefixcmp(head, "refs/heads/") ?
1361 !strcmp(head, "HEAD") ?
1362 _("detached HEAD") :
1364 initial_commit ? _(" (root-commit)") : "");
1366 if (!log_tree_commit(&rev, commit)) {
1367 rev.always_show_header = 1;
1368 rev.use_terminator = 1;
1369 log_tree_commit(&rev, commit);
1372 strbuf_release(&format);
1375 static int git_commit_config(const char *k, const char *v, void *cb)
1377 struct wt_status *s = cb;
1380 if (!strcmp(k, "commit.template"))
1381 return git_config_pathname(&template_file, k, v);
1382 if (!strcmp(k, "commit.status")) {
1383 include_status = git_config_bool(k, v);
1386 if (!strcmp(k, "commit.cleanup"))
1387 return git_config_string(&cleanup_arg, k, v);
1389 status = git_gpg_config(k, v, NULL);
1392 return git_status_config(k, v, s);
1395 static int run_rewrite_hook(const unsigned char *oldsha1,
1396 const unsigned char *newsha1)
1398 /* oldsha1 SP newsha1 LF NUL */
1399 static char buf[2*40 + 3];
1400 struct child_process proc;
1401 const char *argv[3];
1405 argv[0] = find_hook("post-rewrite");
1412 memset(&proc, 0, sizeof(proc));
1415 proc.stdout_to_stderr = 1;
1417 code = start_command(&proc);
1420 n = snprintf(buf, sizeof(buf), "%s %s\n",
1421 sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
1422 write_in_full(proc.in, buf, n);
1424 return finish_command(&proc);
1427 int cmd_commit(int argc, const char **argv, const char *prefix)
1429 static struct wt_status s;
1430 static struct option builtin_commit_options[] = {
1431 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1432 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
1434 OPT_GROUP(N_("Commit message options")),
1435 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1436 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1437 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1438 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1439 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1440 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
1441 OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
1442 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1443 OPT_BOOLEAN(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1444 OPT_BOOLEAN('s', "signoff", &signoff, N_("add Signed-off-by:")),
1445 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1446 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
1447 OPT_STRING(0, "cleanup", &cleanup_arg, N_("default"), N_("how to strip spaces and #comments from message")),
1448 OPT_BOOLEAN(0, "status", &include_status, N_("include status in commit message template")),
1449 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key id"),
1450 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1451 /* end commit message options */
1453 OPT_GROUP(N_("Commit contents options")),
1454 OPT_BOOLEAN('a', "all", &all, N_("commit all changed files")),
1455 OPT_BOOLEAN('i', "include", &also, N_("add specified files to index for commit")),
1456 OPT_BOOLEAN(0, "interactive", &interactive, N_("interactively add files")),
1457 OPT_BOOLEAN('p', "patch", &patch_interactive, N_("interactively add changes")),
1458 OPT_BOOLEAN('o', "only", &only, N_("commit only specified files")),
1459 OPT_BOOLEAN('n', "no-verify", &no_verify, N_("bypass pre-commit hook")),
1460 OPT_BOOLEAN(0, "dry-run", &dry_run, N_("show what would be committed")),
1461 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
1462 STATUS_FORMAT_SHORT),
1463 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
1464 OPT_SET_INT(0, "porcelain", &status_format,
1465 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
1466 OPT_SET_INT(0, "long", &status_format,
1467 N_("show status in long format (default)"),
1468 STATUS_FORMAT_LONG),
1469 OPT_BOOLEAN('z', "null", &s.null_termination,
1470 N_("terminate entries with NUL")),
1471 OPT_BOOLEAN(0, "amend", &amend, N_("amend previous commit")),
1472 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
1473 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, N_("mode"), N_("show untracked files, optional modes: all, normal, no. (Default: all)"), PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1474 /* end commit contents options */
1476 { OPTION_BOOLEAN, 0, "allow-empty", &allow_empty, NULL,
1477 N_("ok to record an empty change"),
1478 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1479 { OPTION_BOOLEAN, 0, "allow-empty-message", &allow_empty_message, NULL,
1480 N_("ok to record a change with an empty message"),
1481 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
1486 struct strbuf sb = STRBUF_INIT;
1487 struct strbuf author_ident = STRBUF_INIT;
1488 const char *index_file, *reflog_msg;
1490 unsigned char sha1[20];
1491 struct ref_lock *ref_lock;
1492 struct commit_list *parents = NULL, **pptr = &parents;
1493 struct stat statbuf;
1494 int allow_fast_forward = 1;
1495 struct commit *current_head = NULL;
1496 struct commit_extra_header *extra = NULL;
1498 if (argc == 2 && !strcmp(argv[1], "-h"))
1499 usage_with_options(builtin_commit_usage, builtin_commit_options);
1501 wt_status_prepare(&s);
1502 gitmodules_config();
1503 git_config(git_commit_config, &s);
1504 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
1505 determine_whence(&s);
1508 if (get_sha1("HEAD", sha1))
1509 current_head = NULL;
1511 current_head = lookup_commit_or_die(sha1, "HEAD");
1512 if (!current_head || parse_commit(current_head))
1513 die(_("could not parse HEAD commit"));
1515 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1516 builtin_commit_usage,
1517 prefix, current_head, &s);
1519 return dry_run_commit(argc, argv, prefix, current_head, &s);
1520 index_file = prepare_index(argc, argv, prefix, current_head, 0);
1522 /* Set up everything for writing the commit object. This includes
1523 running hooks, writing the trees, and interacting with the user. */
1524 if (!prepare_to_commit(index_file, prefix,
1525 current_head, &s, &author_ident)) {
1526 rollback_index_files();
1530 /* Determine parents */
1531 reflog_msg = getenv("GIT_REFLOG_ACTION");
1532 if (!current_head) {
1534 reflog_msg = "commit (initial)";
1536 struct commit_list *c;
1539 reflog_msg = "commit (amend)";
1540 for (c = current_head->parents; c; c = c->next)
1541 pptr = &commit_list_insert(c->item, pptr)->next;
1542 } else if (whence == FROM_MERGE) {
1543 struct strbuf m = STRBUF_INIT;
1547 reflog_msg = "commit (merge)";
1548 pptr = &commit_list_insert(current_head, pptr)->next;
1549 fp = fopen(git_path("MERGE_HEAD"), "r");
1551 die_errno(_("could not open '%s' for reading"),
1552 git_path("MERGE_HEAD"));
1553 while (strbuf_getline(&m, fp, '\n') != EOF) {
1554 struct commit *parent;
1556 parent = get_merge_parent(m.buf);
1558 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
1559 pptr = &commit_list_insert(parent, pptr)->next;
1563 if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1564 if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
1565 die_errno(_("could not read MERGE_MODE"));
1566 if (!strcmp(sb.buf, "no-ff"))
1567 allow_fast_forward = 0;
1569 if (allow_fast_forward)
1570 parents = reduce_heads(parents);
1573 reflog_msg = (whence == FROM_CHERRY_PICK)
1574 ? "commit (cherry-pick)"
1576 pptr = &commit_list_insert(current_head, pptr)->next;
1579 /* Finally, get the commit message */
1581 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
1582 int saved_errno = errno;
1583 rollback_index_files();
1584 die(_("could not read commit message: %s"), strerror(saved_errno));
1587 /* Truncate the message just before the diff, if any. */
1589 p = strstr(sb.buf, "\ndiff --git ");
1591 strbuf_setlen(&sb, p - sb.buf + 1);
1594 if (cleanup_mode != CLEANUP_NONE)
1595 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1596 if (template_untouched(&sb) && !allow_empty_message) {
1597 rollback_index_files();
1598 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1601 if (message_is_empty(&sb) && !allow_empty_message) {
1602 rollback_index_files();
1603 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
1608 const char *exclude_gpgsig[2] = { "gpgsig", NULL };
1609 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
1611 struct commit_extra_header **tail = &extra;
1612 append_merge_tag_headers(parents, &tail);
1615 if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1,
1616 author_ident.buf, sign_commit, extra)) {
1617 rollback_index_files();
1618 die(_("failed to write commit object"));
1620 strbuf_release(&author_ident);
1621 free_commit_extra_headers(extra);
1623 ref_lock = lock_any_ref_for_update("HEAD",
1626 : current_head->object.sha1,
1629 nl = strchr(sb.buf, '\n');
1631 strbuf_setlen(&sb, nl + 1 - sb.buf);
1633 strbuf_addch(&sb, '\n');
1634 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1635 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
1638 rollback_index_files();
1639 die(_("cannot lock HEAD ref"));
1641 if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
1642 rollback_index_files();
1643 die(_("cannot update HEAD ref"));
1646 unlink(git_path("CHERRY_PICK_HEAD"));
1647 unlink(git_path("REVERT_HEAD"));
1648 unlink(git_path("MERGE_HEAD"));
1649 unlink(git_path("MERGE_MSG"));
1650 unlink(git_path("MERGE_MODE"));
1651 unlink(git_path("SQUASH_MSG"));
1653 if (commit_index_files())
1654 die (_("Repository has been updated, but unable to write\n"
1655 "new_index file. Check that disk is not full or quota is\n"
1656 "not exceeded, and then \"git reset HEAD\" to recover."));
1659 run_hook(get_index_file(), "post-commit", NULL);
1660 if (amend && !no_post_rewrite) {
1661 struct notes_rewrite_cfg *cfg;
1662 cfg = init_copy_notes_for_rewrite("amend");
1664 /* we are amending, so current_head is not NULL */
1665 copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
1666 finish_copy_notes_for_rewrite(cfg, "Notes added by 'git commit --amend'");
1668 run_rewrite_hook(current_head->object.sha1, sha1);
1671 print_summary(prefix, sha1, !current_head);