4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
8 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "cache-tree.h"
20 #include "wt-status.h"
21 #include "run-command.h"
26 #include "parse-options.h"
27 #include "string-list.h"
29 #include "unpack-trees.h"
31 #include "submodule.h"
32 #include "gpg-interface.h"
34 #include "sequencer.h"
37 #include "commit-reach.h"
38 #include "commit-graph.h"
40 static const char * const builtin_commit_usage[] = {
41 N_("git commit [<options>] [--] <pathspec>..."),
45 static const char * const builtin_status_usage[] = {
46 N_("git status [<options>] [--] <pathspec>..."),
50 static const char empty_amend_advice[] =
51 N_("You asked to amend the most recent commit, but doing so would make\n"
52 "it empty. You can repeat your command with --allow-empty, or you can\n"
53 "remove the commit entirely with \"git reset HEAD^\".\n");
55 static const char empty_cherry_pick_advice[] =
56 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
57 "If you wish to commit it anyway, use:\n"
59 " git commit --allow-empty\n"
62 static const char empty_cherry_pick_advice_single[] =
63 N_("Otherwise, please use 'git cherry-pick --skip'\n");
65 static const char empty_cherry_pick_advice_multi[] =
68 " git cherry-pick --continue\n"
70 "to resume cherry-picking the remaining commits.\n"
71 "If you wish to skip this commit, use:\n"
73 " git cherry-pick --skip\n"
76 static const char *color_status_slots[] = {
77 [WT_STATUS_HEADER] = "header",
78 [WT_STATUS_UPDATED] = "updated",
79 [WT_STATUS_CHANGED] = "changed",
80 [WT_STATUS_UNTRACKED] = "untracked",
81 [WT_STATUS_NOBRANCH] = "noBranch",
82 [WT_STATUS_UNMERGED] = "unmerged",
83 [WT_STATUS_LOCAL_BRANCH] = "localBranch",
84 [WT_STATUS_REMOTE_BRANCH] = "remoteBranch",
85 [WT_STATUS_ONBRANCH] = "branch",
88 static const char *use_message_buffer;
89 static struct lock_file index_lock; /* real index */
90 static struct lock_file false_lock; /* used only for partial commits */
97 static const char *logfile, *force_author;
98 static const char *template_file;
100 * The _message variables are commit names from which to take
101 * the commit message and/or authorship.
103 static const char *author_message, *author_message_buffer;
104 static char *edit_message, *use_message;
105 static char *fixup_message, *squash_message;
106 static int all, also, interactive, patch_interactive, only, amend, signoff;
107 static int edit_flag = -1; /* unspecified */
108 static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
109 static int config_commit_verbose = -1; /* unspecified */
110 static int no_post_rewrite, allow_empty_message;
111 static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
112 static char *sign_commit;
115 * The default commit message cleanup mode will remove the lines
116 * beginning with # (shell comments) and leading and trailing
117 * whitespaces (empty lines or containing only whitespaces)
118 * if editor is used, and only the whitespaces if the message
119 * is specified explicitly.
121 static enum commit_msg_cleanup_mode cleanup_mode;
122 static const char *cleanup_arg;
124 static enum commit_whence whence;
125 static int use_editor = 1, include_status = 1;
126 static int have_option_m;
127 static struct strbuf message = STRBUF_INIT;
129 static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED;
131 static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset)
133 enum wt_status_format *value = (enum wt_status_format *)opt->value;
135 *value = STATUS_FORMAT_NONE;
137 *value = STATUS_FORMAT_PORCELAIN;
138 else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
139 *value = STATUS_FORMAT_PORCELAIN;
140 else if (!strcmp(arg, "v2") || !strcmp(arg, "2"))
141 *value = STATUS_FORMAT_PORCELAIN_V2;
143 die("unsupported porcelain version '%s'", arg);
148 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
150 struct strbuf *buf = opt->value;
153 strbuf_setlen(buf, 0);
157 strbuf_addch(buf, '\n');
158 strbuf_addstr(buf, arg);
159 strbuf_complete_line(buf);
164 static int opt_parse_rename_score(const struct option *opt, const char *arg, int unset)
166 const char **value = opt->value;
168 BUG_ON_OPT_NEG(unset);
170 if (arg != NULL && *arg == '=')
177 static void determine_whence(struct wt_status *s)
179 if (file_exists(git_path_merge_head(the_repository)))
181 else if (!sequencer_determine_whence(the_repository, &whence))
182 whence = FROM_COMMIT;
187 static void status_init_config(struct wt_status *s, config_fn_t fn)
189 wt_status_prepare(the_repository, s);
190 init_diff_ui_defaults();
193 s->hints = advice_status_hints; /* must come after git_config() */
196 static void rollback_index_files(void)
198 switch (commit_style) {
200 break; /* nothing to do */
202 rollback_lock_file(&index_lock);
205 rollback_lock_file(&index_lock);
206 rollback_lock_file(&false_lock);
211 static int commit_index_files(void)
215 switch (commit_style) {
217 break; /* nothing to do */
219 err = commit_lock_file(&index_lock);
222 err = commit_lock_file(&index_lock);
223 rollback_lock_file(&false_lock);
231 * Take a union of paths in the index and the named tree (typically, "HEAD"),
232 * and return the paths that match the given pattern in list.
234 static int list_paths(struct string_list *list, const char *with_tree,
235 const struct pathspec *pattern)
243 m = xcalloc(1, pattern->nr);
246 char *max_prefix = common_prefix(pattern);
247 overlay_tree_on_index(&the_index, with_tree, max_prefix);
251 for (i = 0; i < active_nr; i++) {
252 const struct cache_entry *ce = active_cache[i];
253 struct string_list_item *item;
255 if (ce->ce_flags & CE_UPDATE)
257 if (!ce_path_match(&the_index, ce, pattern, m))
259 item = string_list_insert(list, ce->name);
260 if (ce_skip_worktree(ce))
261 item->util = item; /* better a valid pointer than a fake one */
264 ret = report_path_error(m, pattern);
269 static void add_remove_files(struct string_list *list)
272 for (i = 0; i < list->nr; i++) {
274 struct string_list_item *p = &(list->items[i]);
276 /* p->util is skip-worktree */
280 if (!lstat(p->string, &st)) {
281 if (add_to_cache(p->string, &st, 0))
282 die(_("updating files failed"));
284 remove_file_from_cache(p->string);
288 static void create_base_index(const struct commit *current_head)
291 struct unpack_trees_options opts;
299 memset(&opts, 0, sizeof(opts));
303 opts.src_index = &the_index;
304 opts.dst_index = &the_index;
306 opts.fn = oneway_merge;
307 tree = parse_tree_indirect(¤t_head->object.oid);
309 die(_("failed to unpack HEAD tree object"));
311 init_tree_desc(&t, tree->buffer, tree->size);
312 if (unpack_trees(1, &t, &opts))
313 exit(128); /* We've already reported the error, finish dying */
316 static void refresh_cache_or_die(int refresh_flags)
319 * refresh_flags contains REFRESH_QUIET, so the only errors
320 * are for unmerged entries.
322 if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
323 die_resolve_conflict("commit");
326 static const char *prepare_index(int argc, const char **argv, const char *prefix,
327 const struct commit *current_head, int is_status)
329 struct string_list partial = STRING_LIST_INIT_DUP;
330 struct pathspec pathspec;
331 int refresh_flags = REFRESH_QUIET;
335 refresh_flags |= REFRESH_UNMERGED;
336 parse_pathspec(&pathspec, 0,
337 PATHSPEC_PREFER_FULL,
340 if (read_cache_preload(&pathspec) < 0)
341 die(_("index file corrupt"));
344 char *old_index_env = NULL;
345 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
347 refresh_cache_or_die(refresh_flags);
349 if (write_locked_index(&the_index, &index_lock, 0))
350 die(_("unable to create temporary index"));
352 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
353 setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
355 if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
356 die(_("interactive add failed"));
358 if (old_index_env && *old_index_env)
359 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
361 unsetenv(INDEX_ENVIRONMENT);
362 FREE_AND_NULL(old_index_env);
365 read_cache_from(get_lock_file_path(&index_lock));
366 if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
367 if (reopen_lock_file(&index_lock) < 0)
368 die(_("unable to write index file"));
369 if (write_locked_index(&the_index, &index_lock, 0))
370 die(_("unable to update temporary index"));
372 warning(_("Failed to update main cache tree"));
374 commit_style = COMMIT_NORMAL;
375 ret = get_lock_file_path(&index_lock);
380 * Non partial, non as-is commit.
382 * (1) get the real index;
383 * (2) update the_index as necessary;
384 * (3) write the_index out to the real index (still locked);
385 * (4) return the name of the locked index file.
387 * The caller should run hooks on the locked real index, and
388 * (A) if all goes well, commit the real index;
389 * (B) on failure, rollback the real index.
391 if (all || (also && pathspec.nr)) {
392 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
393 add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
394 refresh_cache_or_die(refresh_flags);
395 update_main_cache_tree(WRITE_TREE_SILENT);
396 if (write_locked_index(&the_index, &index_lock, 0))
397 die(_("unable to write new_index file"));
398 commit_style = COMMIT_NORMAL;
399 ret = get_lock_file_path(&index_lock);
406 * (1) return the name of the real index file.
408 * The caller should run hooks on the real index,
409 * and create commit from the_index.
410 * We still need to refresh the index here.
412 if (!only && !pathspec.nr) {
413 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
414 refresh_cache_or_die(refresh_flags);
415 if (active_cache_changed
416 || !cache_tree_fully_valid(active_cache_tree))
417 update_main_cache_tree(WRITE_TREE_SILENT);
418 if (write_locked_index(&the_index, &index_lock,
419 COMMIT_LOCK | SKIP_IF_UNCHANGED))
420 die(_("unable to write new_index file"));
421 commit_style = COMMIT_AS_IS;
422 ret = get_index_file();
429 * (0) find the set of affected paths;
430 * (1) get lock on the real index file;
431 * (2) update the_index with the given paths;
432 * (3) write the_index out to the real index (still locked);
433 * (4) get lock on the false index file;
434 * (5) reset the_index from HEAD;
435 * (6) update the_index the same way as (2);
436 * (7) write the_index out to the false index file;
437 * (8) return the name of the false index file (still locked);
439 * The caller should run hooks on the locked false index, and
440 * create commit from it. Then
441 * (A) if all goes well, commit the real index;
442 * (B) on failure, rollback the real index;
443 * In either case, rollback the false index.
445 commit_style = COMMIT_PARTIAL;
447 if (whence != FROM_COMMIT) {
448 if (whence == FROM_MERGE)
449 die(_("cannot do a partial commit during a merge."));
450 else if (is_from_cherry_pick(whence))
451 die(_("cannot do a partial commit during a cherry-pick."));
454 if (list_paths(&partial, !current_head ? NULL : "HEAD", &pathspec))
458 if (read_cache() < 0)
459 die(_("cannot read the index"));
461 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
462 add_remove_files(&partial);
463 refresh_cache(REFRESH_QUIET);
464 update_main_cache_tree(WRITE_TREE_SILENT);
465 if (write_locked_index(&the_index, &index_lock, 0))
466 die(_("unable to write new_index file"));
468 hold_lock_file_for_update(&false_lock,
469 git_path("next-index-%"PRIuMAX,
470 (uintmax_t) getpid()),
473 create_base_index(current_head);
474 add_remove_files(&partial);
475 refresh_cache(REFRESH_QUIET);
477 if (write_locked_index(&the_index, &false_lock, 0))
478 die(_("unable to write temporary index file"));
481 ret = get_lock_file_path(&false_lock);
482 read_cache_from(ret);
484 string_list_clear(&partial, 0);
485 clear_pathspec(&pathspec);
489 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
492 struct object_id oid;
494 if (s->relative_paths)
499 s->reference = "HEAD^1";
501 s->verbose = verbose;
502 s->index_file = index_file;
505 s->is_initial = get_oid(s->reference, &oid) ? 1 : 0;
507 oidcpy(&s->oid_commit, &oid);
508 s->status_format = status_format;
509 s->ignore_submodule_arg = ignore_submodule_arg;
511 wt_status_collect(s);
513 wt_status_collect_free_buffers(s);
515 return s->committable;
518 static int is_a_merge(const struct commit *current_head)
520 return !!(current_head->parents && current_head->parents->next);
523 static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
525 if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
526 BUG("unable to parse our own ident: %s", buf->buf);
529 static void export_one(const char *var, const char *s, const char *e, int hack)
531 struct strbuf buf = STRBUF_INIT;
533 strbuf_addch(&buf, hack);
534 strbuf_addf(&buf, "%.*s", (int)(e - s), s);
535 setenv(var, buf.buf, 1);
536 strbuf_release(&buf);
539 static int parse_force_date(const char *in, struct strbuf *out)
541 strbuf_addch(out, '@');
543 if (parse_date(in, out) < 0) {
545 unsigned long t = approxidate_careful(in, &errors);
548 strbuf_addf(out, "%lu", t);
554 static void set_ident_var(char **buf, char *val)
560 static void determine_author_info(struct strbuf *author_ident)
562 char *name, *email, *date;
563 struct ident_split author;
565 name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
566 email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
567 date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
569 if (author_message) {
570 struct ident_split ident;
574 a = find_commit_header(author_message_buffer, "author", &len);
576 die(_("commit '%s' lacks author header"), author_message);
577 if (split_ident_line(&ident, a, len) < 0)
578 die(_("commit '%s' has malformed author line"), author_message);
580 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
581 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
583 if (ident.date_begin) {
584 struct strbuf date_buf = STRBUF_INIT;
585 strbuf_addch(&date_buf, '@');
586 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
587 strbuf_addch(&date_buf, ' ');
588 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
589 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
594 struct ident_split ident;
596 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
597 die(_("malformed --author parameter"));
598 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
599 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
603 struct strbuf date_buf = STRBUF_INIT;
604 if (parse_force_date(force_date, &date_buf))
605 die(_("invalid date format: %s"), force_date);
606 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
609 strbuf_addstr(author_ident, fmt_ident(name, email, WANT_AUTHOR_IDENT, date,
611 assert_split_ident(&author, author_ident);
612 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
613 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
614 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
620 static int author_date_is_interesting(void)
622 return author_message || force_date;
625 static void adjust_comment_line_char(const struct strbuf *sb)
627 char candidates[] = "#;@!$%^&|:";
631 comment_line_char = candidates[0];
632 if (!memchr(sb->buf, comment_line_char, sb->len))
636 candidate = strchr(candidates, *p);
639 for (p = sb->buf; *p; p++) {
640 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
641 candidate = strchr(candidates, p[1]);
647 for (p = candidates; *p == ' '; p++)
650 die(_("unable to select a comment character that is not used\n"
651 "in the current commit message"));
652 comment_line_char = *p;
655 static int prepare_to_commit(const char *index_file, const char *prefix,
656 struct commit *current_head,
658 struct strbuf *author_ident)
661 struct strbuf committer_ident = STRBUF_INIT;
663 struct strbuf sb = STRBUF_INIT;
664 const char *hook_arg1 = NULL;
665 const char *hook_arg2 = NULL;
666 int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
667 int old_display_comment_prefix;
668 int merge_contains_scissors = 0;
670 /* This checks and barfs if author is badly specified */
671 determine_author_info(author_ident);
673 if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
676 if (squash_message) {
678 * Insert the proper subject line before other commit
679 * message options add their content.
681 if (use_message && !strcmp(use_message, squash_message))
682 strbuf_addstr(&sb, "squash! ");
684 struct pretty_print_context ctx = {0};
686 c = lookup_commit_reference_by_name(squash_message);
688 die(_("could not lookup commit %s"), squash_message);
689 ctx.output_encoding = get_commit_output_encoding();
690 format_commit_message(c, "squash! %s\n\n", &sb,
695 if (have_option_m && !fixup_message) {
696 strbuf_addbuf(&sb, &message);
697 hook_arg1 = "message";
698 } else if (logfile && !strcmp(logfile, "-")) {
700 fprintf(stderr, _("(reading log message from standard input)\n"));
701 if (strbuf_read(&sb, 0, 0) < 0)
702 die_errno(_("could not read log from standard input"));
703 hook_arg1 = "message";
704 } else if (logfile) {
705 if (strbuf_read_file(&sb, logfile, 0) < 0)
706 die_errno(_("could not read log file '%s'"),
708 hook_arg1 = "message";
709 } else if (use_message) {
711 buffer = strstr(use_message_buffer, "\n\n");
713 strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
714 hook_arg1 = "commit";
715 hook_arg2 = use_message;
716 } else if (fixup_message) {
717 struct pretty_print_context ctx = {0};
718 struct commit *commit;
719 commit = lookup_commit_reference_by_name(fixup_message);
721 die(_("could not lookup commit %s"), fixup_message);
722 ctx.output_encoding = get_commit_output_encoding();
723 format_commit_message(commit, "fixup! %s\n\n",
726 strbuf_addbuf(&sb, &message);
727 hook_arg1 = "message";
728 } else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
729 size_t merge_msg_start;
732 * prepend SQUASH_MSG here if it exists and a
733 * "merge --squash" was originally performed
735 if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
736 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
737 die_errno(_("could not read SQUASH_MSG"));
738 hook_arg1 = "squash";
742 merge_msg_start = sb.len;
743 if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
744 die_errno(_("could not read MERGE_MSG"));
746 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
747 wt_status_locate_end(sb.buf + merge_msg_start,
748 sb.len - merge_msg_start) <
749 sb.len - merge_msg_start)
750 merge_contains_scissors = 1;
751 } else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
752 if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
753 die_errno(_("could not read SQUASH_MSG"));
754 hook_arg1 = "squash";
755 } else if (template_file) {
756 if (strbuf_read_file(&sb, template_file, 0) < 0)
757 die_errno(_("could not read '%s'"), template_file);
758 hook_arg1 = "template";
759 clean_message_contents = 0;
763 * The remaining cases don't modify the template message, but
764 * just set the argument(s) to the prepare-commit-msg hook.
766 else if (whence == FROM_MERGE)
768 else if (is_from_cherry_pick(whence)) {
769 hook_arg1 = "commit";
770 hook_arg2 = "CHERRY_PICK_HEAD";
773 if (squash_message) {
775 * If squash_commit was used for the commit subject,
776 * then we're possibly hijacking other commit log options.
777 * Reset the hook args to tell the real story.
779 hook_arg1 = "message";
783 s->fp = fopen_for_writing(git_path_commit_editmsg());
785 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
787 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
788 old_display_comment_prefix = s->display_comment_prefix;
789 s->display_comment_prefix = 1;
792 * Most hints are counter-productive when the commit has
797 if (clean_message_contents)
798 strbuf_stripspace(&sb, 0);
801 append_signoff(&sb, ignore_non_trailer(sb.buf, sb.len), 0);
803 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
804 die_errno(_("could not write commit template"));
806 if (auto_comment_line_char)
807 adjust_comment_line_char(&sb);
810 /* This checks if committer ident is explicitly given */
811 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
812 if (use_editor && include_status) {
814 int saved_color_setting;
815 struct ident_split ci, ai;
817 if (whence != FROM_COMMIT) {
818 if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
819 !merge_contains_scissors)
820 wt_status_add_cut_line(s->fp);
821 status_printf_ln(s, GIT_COLOR_NORMAL,
824 "It looks like you may be committing a merge.\n"
825 "If this is not correct, please remove the file\n"
829 "It looks like you may be committing a cherry-pick.\n"
830 "If this is not correct, please remove the file\n"
833 whence == FROM_MERGE ?
834 git_path_merge_head(the_repository) :
835 git_path_cherry_pick_head(the_repository));
838 fprintf(s->fp, "\n");
839 if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
840 status_printf(s, GIT_COLOR_NORMAL,
841 _("Please enter the commit message for your changes."
842 " Lines starting\nwith '%c' will be ignored, and an empty"
843 " message aborts the commit.\n"), comment_line_char);
844 else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
845 if (whence == FROM_COMMIT && !merge_contains_scissors)
846 wt_status_add_cut_line(s->fp);
847 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
848 status_printf(s, GIT_COLOR_NORMAL,
849 _("Please enter the commit message for your changes."
851 "with '%c' will be kept; you may remove them"
852 " yourself if you want to.\n"
853 "An empty message aborts the commit.\n"), comment_line_char);
856 * These should never fail because they come from our own
857 * fmt_ident. They may fail the sane_ident test, but we know
858 * that the name and mail pointers will at least be valid,
859 * which is enough for our tests and printing here.
861 assert_split_ident(&ai, author_ident);
862 assert_split_ident(&ci, &committer_ident);
864 if (ident_cmp(&ai, &ci))
865 status_printf_ln(s, GIT_COLOR_NORMAL,
867 "Author: %.*s <%.*s>"),
868 ident_shown++ ? "" : "\n",
869 (int)(ai.name_end - ai.name_begin), ai.name_begin,
870 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
872 if (author_date_is_interesting())
873 status_printf_ln(s, GIT_COLOR_NORMAL,
876 ident_shown++ ? "" : "\n",
877 show_ident_date(&ai, DATE_MODE(NORMAL)));
879 if (!committer_ident_sufficiently_given())
880 status_printf_ln(s, GIT_COLOR_NORMAL,
882 "Committer: %.*s <%.*s>"),
883 ident_shown++ ? "" : "\n",
884 (int)(ci.name_end - ci.name_begin), ci.name_begin,
885 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
887 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
889 saved_color_setting = s->use_color;
891 committable = run_status(s->fp, index_file, prefix, 1, s);
892 s->use_color = saved_color_setting;
893 string_list_clear(&s->change, 1);
895 struct object_id oid;
896 const char *parent = "HEAD";
898 if (!active_nr && read_cache() < 0)
899 die(_("Cannot read index"));
904 if (get_oid(parent, &oid)) {
907 for (i = 0; i < active_nr; i++)
908 if (ce_intent_to_add(active_cache[i]))
910 committable = active_nr - ita_nr > 0;
913 * Unless the user did explicitly request a submodule
914 * ignore mode by passing a command line option we do
915 * not ignore any changed submodule SHA-1s when
916 * comparing index and parent, no matter what is
917 * configured. Otherwise we won't commit any
918 * submodules which were manually staged, which would
919 * be really confusing.
921 struct diff_flags flags = DIFF_FLAGS_INIT;
922 flags.override_submodule_config = 1;
923 if (ignore_submodule_arg &&
924 !strcmp(ignore_submodule_arg, "all"))
925 flags.ignore_submodules = 1;
926 committable = index_differs_from(the_repository,
930 strbuf_release(&committer_ident);
935 * Reject an attempt to record a non-merge empty commit without
936 * explicit --allow-empty. In the cherry-pick case, it may be
937 * empty due to conflict resolution, which the user should okay.
939 if (!committable && whence != FROM_MERGE && !allow_empty &&
940 !(amend && is_a_merge(current_head))) {
941 s->display_comment_prefix = old_display_comment_prefix;
942 run_status(stdout, index_file, prefix, 0, s);
944 fputs(_(empty_amend_advice), stderr);
945 else if (is_from_cherry_pick(whence)) {
946 fputs(_(empty_cherry_pick_advice), stderr);
947 if (whence == FROM_CHERRY_PICK_SINGLE)
948 fputs(_(empty_cherry_pick_advice_single), stderr);
950 fputs(_(empty_cherry_pick_advice_multi), stderr);
955 if (!no_verify && find_hook("pre-commit")) {
957 * Re-read the index as pre-commit hook could have updated it,
958 * and write it out as a tree. We must do this before we invoke
959 * the editor and after we invoke run_status above.
963 read_cache_from(index_file);
965 if (update_main_cache_tree(0)) {
966 error(_("Error building trees"));
970 if (run_commit_hook(use_editor, index_file, "prepare-commit-msg",
971 git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
975 struct argv_array env = ARGV_ARRAY_INIT;
977 argv_array_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
978 if (launch_editor(git_path_commit_editmsg(), NULL, env.argv)) {
980 _("Please supply the message using either -m or -F option.\n"));
983 argv_array_clear(&env);
987 run_commit_hook(use_editor, index_file, "commit-msg", git_path_commit_editmsg(), NULL)) {
994 static const char *find_author_by_nickname(const char *name)
996 struct rev_info revs;
997 struct commit *commit;
998 struct strbuf buf = STRBUF_INIT;
999 struct string_list mailmap = STRING_LIST_INIT_NODUP;
1003 repo_init_revisions(the_repository, &revs, NULL);
1004 strbuf_addf(&buf, "--author=%s", name);
1009 setup_revisions(ac, av, &revs, NULL);
1010 revs.mailmap = &mailmap;
1011 read_mailmap(revs.mailmap, NULL);
1013 if (prepare_revision_walk(&revs))
1014 die(_("revision walk setup failed"));
1015 commit = get_revision(&revs);
1017 struct pretty_print_context ctx = {0};
1018 ctx.date_mode.type = DATE_NORMAL;
1019 strbuf_release(&buf);
1020 format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
1021 clear_mailmap(&mailmap);
1022 return strbuf_detach(&buf, NULL);
1024 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
1027 static void handle_ignored_arg(struct wt_status *s)
1030 ; /* default already initialized */
1031 else if (!strcmp(ignored_arg, "traditional"))
1032 s->show_ignored_mode = SHOW_TRADITIONAL_IGNORED;
1033 else if (!strcmp(ignored_arg, "no"))
1034 s->show_ignored_mode = SHOW_NO_IGNORED;
1035 else if (!strcmp(ignored_arg, "matching"))
1036 s->show_ignored_mode = SHOW_MATCHING_IGNORED;
1038 die(_("Invalid ignored mode '%s'"), ignored_arg);
1041 static void handle_untracked_files_arg(struct wt_status *s)
1043 if (!untracked_files_arg)
1044 ; /* default already initialized */
1045 else if (!strcmp(untracked_files_arg, "no"))
1046 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1047 else if (!strcmp(untracked_files_arg, "normal"))
1048 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1049 else if (!strcmp(untracked_files_arg, "all"))
1050 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1052 * Please update $__git_untracked_file_modes in
1053 * git-completion.bash when you add new options
1056 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
1059 static const char *read_commit_message(const char *name)
1061 const char *out_enc;
1062 struct commit *commit;
1064 commit = lookup_commit_reference_by_name(name);
1066 die(_("could not lookup commit %s"), name);
1067 out_enc = get_commit_output_encoding();
1068 return logmsg_reencode(commit, NULL, out_enc);
1072 * Enumerate what needs to be propagated when --porcelain
1073 * is not in effect here.
1075 static struct status_deferred_config {
1076 enum wt_status_format status_format;
1078 enum ahead_behind_flags ahead_behind;
1079 } status_deferred_config = {
1080 STATUS_FORMAT_UNSPECIFIED,
1081 -1, /* unspecified */
1082 AHEAD_BEHIND_UNSPECIFIED,
1085 static void finalize_deferred_config(struct wt_status *s)
1087 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
1088 status_format != STATUS_FORMAT_PORCELAIN_V2 &&
1089 !s->null_termination);
1091 if (s->null_termination) {
1092 if (status_format == STATUS_FORMAT_NONE ||
1093 status_format == STATUS_FORMAT_UNSPECIFIED)
1094 status_format = STATUS_FORMAT_PORCELAIN;
1095 else if (status_format == STATUS_FORMAT_LONG)
1096 die(_("--long and -z are incompatible"));
1099 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1100 status_format = status_deferred_config.status_format;
1101 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1102 status_format = STATUS_FORMAT_NONE;
1104 if (use_deferred_config && s->show_branch < 0)
1105 s->show_branch = status_deferred_config.show_branch;
1106 if (s->show_branch < 0)
1110 * If the user did not give a "--[no]-ahead-behind" command
1111 * line argument *AND* we will print in a human-readable format
1112 * (short, long etc.) then we inherit from the status.aheadbehind
1113 * config setting. In all other cases (and porcelain V[12] formats
1114 * in particular), we inherit _FULL for backwards compatibility.
1116 if (use_deferred_config &&
1117 s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1118 s->ahead_behind_flags = status_deferred_config.ahead_behind;
1120 if (s->ahead_behind_flags == AHEAD_BEHIND_UNSPECIFIED)
1121 s->ahead_behind_flags = AHEAD_BEHIND_FULL;
1124 static int parse_and_validate_options(int argc, const char *argv[],
1125 const struct option *options,
1126 const char * const usage[],
1128 struct commit *current_head,
1129 struct wt_status *s)
1133 argc = parse_options(argc, argv, prefix, options, usage, 0);
1134 finalize_deferred_config(s);
1136 if (force_author && !strchr(force_author, '>'))
1137 force_author = find_author_by_nickname(force_author);
1139 if (force_author && renew_authorship)
1140 die(_("Using both --reset-author and --author does not make sense"));
1142 if (logfile || have_option_m || use_message || fixup_message)
1145 use_editor = edit_flag;
1147 /* Sanity check options */
1148 if (amend && !current_head)
1149 die(_("You have nothing to amend."));
1150 if (amend && whence != FROM_COMMIT) {
1151 if (whence == FROM_MERGE)
1152 die(_("You are in the middle of a merge -- cannot amend."));
1153 else if (is_from_cherry_pick(whence))
1154 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1156 if (fixup_message && squash_message)
1157 die(_("Options --squash and --fixup cannot be used together"));
1167 die(_("Only one of -c/-C/-F/--fixup can be used."));
1168 if (have_option_m && (edit_message || use_message || logfile))
1169 die((_("Option -m cannot be combined with -c/-C/-F.")));
1170 if (f || have_option_m)
1171 template_file = NULL;
1173 use_message = edit_message;
1174 if (amend && !use_message && !fixup_message)
1175 use_message = "HEAD";
1176 if (!use_message && !is_from_cherry_pick(whence) && renew_authorship)
1177 die(_("--reset-author can be used only with -C, -c or --amend."));
1179 use_message_buffer = read_commit_message(use_message);
1180 if (!renew_authorship) {
1181 author_message = use_message;
1182 author_message_buffer = use_message_buffer;
1185 if (is_from_cherry_pick(whence) && !renew_authorship) {
1186 author_message = "CHERRY_PICK_HEAD";
1187 author_message_buffer = read_commit_message(author_message);
1190 if (patch_interactive)
1193 if (also + only + all + interactive > 1)
1194 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1195 if (argc == 0 && (also || (only && !amend && !allow_empty)))
1196 die(_("No paths with --include/--only does not make sense."));
1197 cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
1199 handle_untracked_files_arg(s);
1201 if (all && argc > 0)
1202 die(_("paths '%s ...' with -a does not make sense"),
1205 if (status_format != STATUS_FORMAT_NONE)
1211 static int dry_run_commit(int argc, const char **argv, const char *prefix,
1212 const struct commit *current_head, struct wt_status *s)
1215 const char *index_file;
1217 index_file = prepare_index(argc, argv, prefix, current_head, 1);
1218 committable = run_status(stdout, index_file, prefix, 0, s);
1219 rollback_index_files();
1221 return committable ? 0 : 1;
1224 define_list_config_array_extra(color_status_slots, {"added"});
1226 static int parse_status_slot(const char *slot)
1228 if (!strcasecmp(slot, "added"))
1229 return WT_STATUS_UPDATED;
1231 return LOOKUP_CONFIG(color_status_slots, slot);
1234 static int git_status_config(const char *k, const char *v, void *cb)
1236 struct wt_status *s = cb;
1237 const char *slot_name;
1239 if (starts_with(k, "column."))
1240 return git_column_config(k, v, "status", &s->colopts);
1241 if (!strcmp(k, "status.submodulesummary")) {
1243 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1244 if (is_bool && s->submodule_summary)
1245 s->submodule_summary = -1;
1248 if (!strcmp(k, "status.short")) {
1249 if (git_config_bool(k, v))
1250 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
1252 status_deferred_config.status_format = STATUS_FORMAT_NONE;
1255 if (!strcmp(k, "status.branch")) {
1256 status_deferred_config.show_branch = git_config_bool(k, v);
1259 if (!strcmp(k, "status.aheadbehind")) {
1260 status_deferred_config.ahead_behind = git_config_bool(k, v);
1263 if (!strcmp(k, "status.showstash")) {
1264 s->show_stash = git_config_bool(k, v);
1267 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1268 s->use_color = git_config_colorbool(k, v);
1271 if (!strcmp(k, "status.displaycommentprefix")) {
1272 s->display_comment_prefix = git_config_bool(k, v);
1275 if (skip_prefix(k, "status.color.", &slot_name) ||
1276 skip_prefix(k, "color.status.", &slot_name)) {
1277 int slot = parse_status_slot(slot_name);
1281 return config_error_nonbool(k);
1282 return color_parse(v, s->color_palette[slot]);
1284 if (!strcmp(k, "status.relativepaths")) {
1285 s->relative_paths = git_config_bool(k, v);
1288 if (!strcmp(k, "status.showuntrackedfiles")) {
1290 return config_error_nonbool(k);
1291 else if (!strcmp(v, "no"))
1292 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1293 else if (!strcmp(v, "normal"))
1294 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1295 else if (!strcmp(v, "all"))
1296 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1298 return error(_("Invalid untracked files mode '%s'"), v);
1301 if (!strcmp(k, "diff.renamelimit")) {
1302 if (s->rename_limit == -1)
1303 s->rename_limit = git_config_int(k, v);
1306 if (!strcmp(k, "status.renamelimit")) {
1307 s->rename_limit = git_config_int(k, v);
1310 if (!strcmp(k, "diff.renames")) {
1311 if (s->detect_rename == -1)
1312 s->detect_rename = git_config_rename(k, v);
1315 if (!strcmp(k, "status.renames")) {
1316 s->detect_rename = git_config_rename(k, v);
1319 return git_diff_ui_config(k, v, NULL);
1322 int cmd_status(int argc, const char **argv, const char *prefix)
1324 static int no_renames = -1;
1325 static const char *rename_score_arg = (const char *)-1;
1326 static struct wt_status s;
1327 unsigned int progress_flag = 0;
1329 struct object_id oid;
1330 static struct option builtin_status_options[] = {
1331 OPT__VERBOSE(&verbose, N_("be verbose")),
1332 OPT_SET_INT('s', "short", &status_format,
1333 N_("show status concisely"), STATUS_FORMAT_SHORT),
1334 OPT_BOOL('b', "branch", &s.show_branch,
1335 N_("show branch information")),
1336 OPT_BOOL(0, "show-stash", &s.show_stash,
1337 N_("show stash information")),
1338 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1339 N_("compute full ahead/behind values")),
1340 { OPTION_CALLBACK, 0, "porcelain", &status_format,
1341 N_("version"), N_("machine-readable output"),
1342 PARSE_OPT_OPTARG, opt_parse_porcelain },
1343 OPT_SET_INT(0, "long", &status_format,
1344 N_("show status in long format (default)"),
1345 STATUS_FORMAT_LONG),
1346 OPT_BOOL('z', "null", &s.null_termination,
1347 N_("terminate entries with NUL")),
1348 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1350 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1351 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1352 { OPTION_STRING, 0, "ignored", &ignored_arg,
1354 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1355 PARSE_OPT_OPTARG, NULL, (intptr_t)"traditional" },
1356 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1357 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1358 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1359 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
1360 OPT_BOOL(0, "no-renames", &no_renames, N_("do not detect renames")),
1361 { OPTION_CALLBACK, 'M', "find-renames", &rename_score_arg,
1362 N_("n"), N_("detect renames, optionally set similarity index"),
1363 PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_rename_score },
1367 if (argc == 2 && !strcmp(argv[1], "-h"))
1368 usage_with_options(builtin_status_usage, builtin_status_options);
1370 status_init_config(&s, git_status_config);
1371 argc = parse_options(argc, argv, prefix,
1372 builtin_status_options,
1373 builtin_status_usage, 0);
1374 finalize_colopts(&s.colopts, -1);
1375 finalize_deferred_config(&s);
1377 handle_untracked_files_arg(&s);
1378 handle_ignored_arg(&s);
1380 if (s.show_ignored_mode == SHOW_MATCHING_IGNORED &&
1381 s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
1382 die(_("Unsupported combination of ignored and untracked-files arguments"));
1384 parse_pathspec(&s.pathspec, 0,
1385 PATHSPEC_PREFER_FULL,
1388 if (status_format != STATUS_FORMAT_PORCELAIN &&
1389 status_format != STATUS_FORMAT_PORCELAIN_V2)
1390 progress_flag = REFRESH_PROGRESS;
1391 repo_read_index(the_repository);
1392 refresh_index(&the_index,
1393 REFRESH_QUIET|REFRESH_UNMERGED|progress_flag,
1394 &s.pathspec, NULL, NULL);
1396 if (use_optional_locks())
1397 fd = hold_locked_index(&index_lock, 0);
1401 s.is_initial = get_oid(s.reference, &oid) ? 1 : 0;
1403 oidcpy(&s.oid_commit, &oid);
1405 s.ignore_submodule_arg = ignore_submodule_arg;
1406 s.status_format = status_format;
1407 s.verbose = verbose;
1408 if (no_renames != -1)
1409 s.detect_rename = !no_renames;
1410 if ((intptr_t)rename_score_arg != -1) {
1411 if (s.detect_rename < DIFF_DETECT_RENAME)
1412 s.detect_rename = DIFF_DETECT_RENAME;
1413 if (rename_score_arg)
1414 s.rename_score = parse_rename_score(&rename_score_arg);
1417 wt_status_collect(&s);
1420 repo_update_index_if_able(the_repository, &index_lock);
1422 if (s.relative_paths)
1425 wt_status_print(&s);
1426 wt_status_collect_free_buffers(&s);
1431 static int git_commit_config(const char *k, const char *v, void *cb)
1433 struct wt_status *s = cb;
1436 if (!strcmp(k, "commit.template"))
1437 return git_config_pathname(&template_file, k, v);
1438 if (!strcmp(k, "commit.status")) {
1439 include_status = git_config_bool(k, v);
1442 if (!strcmp(k, "commit.cleanup"))
1443 return git_config_string(&cleanup_arg, k, v);
1444 if (!strcmp(k, "commit.gpgsign")) {
1445 sign_commit = git_config_bool(k, v) ? "" : NULL;
1448 if (!strcmp(k, "commit.verbose")) {
1450 config_commit_verbose = git_config_bool_or_int(k, v, &is_bool);
1454 status = git_gpg_config(k, v, NULL);
1457 return git_status_config(k, v, s);
1460 int cmd_commit(int argc, const char **argv, const char *prefix)
1462 const char *argv_gc_auto[] = {"gc", "--auto", NULL};
1463 static struct wt_status s;
1464 static struct option builtin_commit_options[] = {
1465 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1466 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
1468 OPT_GROUP(N_("Commit message options")),
1469 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1470 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1471 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1472 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1473 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1474 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
1475 OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
1476 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1477 OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1478 OPT_BOOL('s', "signoff", &signoff, N_("add Signed-off-by:")),
1479 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1480 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
1481 OPT_CLEANUP(&cleanup_arg),
1482 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
1483 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
1484 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1485 /* end commit message options */
1487 OPT_GROUP(N_("Commit contents options")),
1488 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1489 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1490 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1491 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1492 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
1493 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit and commit-msg hooks")),
1494 OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")),
1495 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
1496 STATUS_FORMAT_SHORT),
1497 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
1498 OPT_BOOL(0, "ahead-behind", &s.ahead_behind_flags,
1499 N_("compute full ahead/behind values")),
1500 OPT_SET_INT(0, "porcelain", &status_format,
1501 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
1502 OPT_SET_INT(0, "long", &status_format,
1503 N_("show status in long format (default)"),
1504 STATUS_FORMAT_LONG),
1505 OPT_BOOL('z', "null", &s.null_termination,
1506 N_("terminate entries with NUL")),
1507 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1508 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
1509 { 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" },
1510 /* end commit contents options */
1512 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1513 N_("ok to record an empty change")),
1514 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1515 N_("ok to record a change with an empty message")),
1520 struct strbuf sb = STRBUF_INIT;
1521 struct strbuf author_ident = STRBUF_INIT;
1522 const char *index_file, *reflog_msg;
1523 struct object_id oid;
1524 struct commit_list *parents = NULL;
1525 struct stat statbuf;
1526 struct commit *current_head = NULL;
1527 struct commit_extra_header *extra = NULL;
1528 struct strbuf err = STRBUF_INIT;
1530 if (argc == 2 && !strcmp(argv[1], "-h"))
1531 usage_with_options(builtin_commit_usage, builtin_commit_options);
1533 status_init_config(&s, git_commit_config);
1534 s.commit_template = 1;
1535 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
1538 if (get_oid("HEAD", &oid))
1539 current_head = NULL;
1541 current_head = lookup_commit_or_die(&oid, "HEAD");
1542 if (parse_commit(current_head))
1543 die(_("could not parse HEAD commit"));
1545 verbose = -1; /* unspecified */
1546 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1547 builtin_commit_usage,
1548 prefix, current_head, &s);
1550 verbose = (config_commit_verbose < 0) ? 0 : config_commit_verbose;
1553 return dry_run_commit(argc, argv, prefix, current_head, &s);
1554 index_file = prepare_index(argc, argv, prefix, current_head, 0);
1556 /* Set up everything for writing the commit object. This includes
1557 running hooks, writing the trees, and interacting with the user. */
1558 if (!prepare_to_commit(index_file, prefix,
1559 current_head, &s, &author_ident)) {
1560 rollback_index_files();
1564 /* Determine parents */
1565 reflog_msg = getenv("GIT_REFLOG_ACTION");
1566 if (!current_head) {
1568 reflog_msg = "commit (initial)";
1571 reflog_msg = "commit (amend)";
1572 parents = copy_commit_list(current_head->parents);
1573 } else if (whence == FROM_MERGE) {
1574 struct strbuf m = STRBUF_INIT;
1576 int allow_fast_forward = 1;
1577 struct commit_list **pptr = &parents;
1580 reflog_msg = "commit (merge)";
1581 pptr = commit_list_append(current_head, pptr);
1582 fp = xfopen(git_path_merge_head(the_repository), "r");
1583 while (strbuf_getline_lf(&m, fp) != EOF) {
1584 struct commit *parent;
1586 parent = get_merge_parent(m.buf);
1588 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
1589 pptr = commit_list_append(parent, pptr);
1593 if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1594 if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
1595 die_errno(_("could not read MERGE_MODE"));
1596 if (!strcmp(sb.buf, "no-ff"))
1597 allow_fast_forward = 0;
1599 if (allow_fast_forward)
1600 reduce_heads_replace(&parents);
1603 reflog_msg = is_from_cherry_pick(whence)
1604 ? "commit (cherry-pick)"
1606 commit_list_insert(current_head, &parents);
1609 /* Finally, get the commit message */
1611 if (strbuf_read_file(&sb, git_path_commit_editmsg(), 0) < 0) {
1612 int saved_errno = errno;
1613 rollback_index_files();
1614 die(_("could not read commit message: %s"), strerror(saved_errno));
1617 cleanup_message(&sb, cleanup_mode, verbose);
1619 if (message_is_empty(&sb, cleanup_mode) && !allow_empty_message) {
1620 rollback_index_files();
1621 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
1624 if (template_untouched(&sb, template_file, cleanup_mode) && !allow_empty_message) {
1625 rollback_index_files();
1626 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1631 const char *exclude_gpgsig[2] = { "gpgsig", NULL };
1632 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
1634 struct commit_extra_header **tail = &extra;
1635 append_merge_tag_headers(parents, &tail);
1638 if (commit_tree_extended(sb.buf, sb.len, &active_cache_tree->oid,
1639 parents, &oid, author_ident.buf, sign_commit,
1641 rollback_index_files();
1642 die(_("failed to write commit object"));
1644 strbuf_release(&author_ident);
1645 free_commit_extra_headers(extra);
1647 if (update_head_with_reflog(current_head, &oid, reflog_msg, &sb,
1649 rollback_index_files();
1653 sequencer_post_commit_cleanup(the_repository, 0);
1654 unlink(git_path_merge_head(the_repository));
1655 unlink(git_path_merge_msg(the_repository));
1656 unlink(git_path_merge_mode(the_repository));
1657 unlink(git_path_squash_msg(the_repository));
1659 if (commit_index_files())
1660 die(_("repository has been updated, but unable to write\n"
1661 "new_index file. Check that disk is not full and quota is\n"
1662 "not exceeded, and then \"git restore --staged :/\" to recover."));
1664 if (git_env_bool(GIT_TEST_COMMIT_GRAPH, 0) &&
1665 write_commit_graph_reachable(get_object_directory(), 0, NULL))
1668 repo_rerere(the_repository, 0);
1669 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
1670 run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
1671 if (amend && !no_post_rewrite) {
1672 commit_post_rewrite(the_repository, current_head, &oid);
1675 unsigned int flags = 0;
1678 flags |= SUMMARY_INITIAL_COMMIT;
1679 if (author_date_is_interesting())
1680 flags |= SUMMARY_SHOW_AUTHOR_DATE;
1681 print_commit_summary(the_repository, prefix,