4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
10 #include "cache-tree.h"
18 #include "wt-status.h"
19 #include "run-command.h"
24 #include "parse-options.h"
25 #include "string-list.h"
27 #include "unpack-trees.h"
29 #include "submodule.h"
30 #include "gpg-interface.h"
32 #include "sequencer.h"
33 #include "notes-utils.h"
37 static const char * const builtin_commit_usage[] = {
38 N_("git commit [options] [--] <pathspec>..."),
42 static const char * const builtin_status_usage[] = {
43 N_("git status [options] [--] <pathspec>..."),
47 static const char implicit_ident_advice_noconfig[] =
48 N_("Your name and email address were configured automatically based\n"
49 "on your username and hostname. Please check that they are accurate.\n"
50 "You can suppress this message by setting them explicitly. Run the\n"
51 "following command and follow the instructions in your editor to edit\n"
52 "your configuration file:\n"
54 " git config --global --edit\n"
56 "After doing this, you may fix the identity used for this commit with:\n"
58 " git commit --amend --reset-author\n");
60 static const char implicit_ident_advice_config[] =
61 N_("Your name and email address were configured automatically based\n"
62 "on your username and hostname. Please check that they are accurate.\n"
63 "You can suppress this message by setting them explicitly:\n"
65 " git config --global user.name \"Your Name\"\n"
66 " git config --global user.email you@example.com\n"
68 "After doing this, you may fix the identity used for this commit with:\n"
70 " git commit --amend --reset-author\n");
72 static const char empty_amend_advice[] =
73 N_("You asked to amend the most recent commit, but doing so would make\n"
74 "it empty. You can repeat your command with --allow-empty, or you can\n"
75 "remove the commit entirely with \"git reset HEAD^\".\n");
77 static const char empty_cherry_pick_advice[] =
78 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
79 "If you wish to commit it anyway, use:\n"
81 " git commit --allow-empty\n"
84 static const char empty_cherry_pick_advice_single[] =
85 N_("Otherwise, please use 'git reset'\n");
87 static const char empty_cherry_pick_advice_multi[] =
88 N_("If you wish to skip this commit, use:\n"
92 "Then \"git cherry-pick --continue\" will resume cherry-picking\n"
93 "the remaining commits.\n");
95 static const char *use_message_buffer;
96 static const char commit_editmsg[] = "COMMIT_EDITMSG";
97 static struct lock_file index_lock; /* real index */
98 static struct lock_file false_lock; /* used only for partial commits */
105 static const char *logfile, *force_author;
106 static const char *template_file;
108 * The _message variables are commit names from which to take
109 * the commit message and/or authorship.
111 static const char *author_message, *author_message_buffer;
112 static char *edit_message, *use_message;
113 static char *fixup_message, *squash_message;
114 static int all, also, interactive, patch_interactive, only, amend, signoff;
115 static int edit_flag = -1; /* unspecified */
116 static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
117 static int no_post_rewrite, allow_empty_message;
118 static char *untracked_files_arg, *force_date, *ignore_submodule_arg;
119 static char *sign_commit;
122 * The default commit message cleanup mode will remove the lines
123 * beginning with # (shell comments) and leading and trailing
124 * whitespaces (empty lines or containing only whitespaces)
125 * if editor is used, and only the whitespaces if the message
126 * is specified explicitly.
134 static const char *cleanup_arg;
136 static enum commit_whence whence;
137 static int sequencer_in_use;
138 static int use_editor = 1, include_status = 1;
139 static int show_ignored_in_status, have_option_m;
140 static const char *only_include_assumed;
141 static struct strbuf message = STRBUF_INIT;
143 static enum status_format {
144 STATUS_FORMAT_NONE = 0,
147 STATUS_FORMAT_PORCELAIN,
149 STATUS_FORMAT_UNSPECIFIED
150 } status_format = STATUS_FORMAT_UNSPECIFIED;
152 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
154 struct strbuf *buf = opt->value;
157 strbuf_setlen(buf, 0);
161 strbuf_addch(buf, '\n');
162 strbuf_addstr(buf, arg);
163 strbuf_complete_line(buf);
168 static void determine_whence(struct wt_status *s)
170 if (file_exists(git_path("MERGE_HEAD")))
172 else if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
173 whence = FROM_CHERRY_PICK;
174 if (file_exists(git_path("sequencer")))
175 sequencer_in_use = 1;
178 whence = FROM_COMMIT;
183 static void status_init_config(struct wt_status *s, config_fn_t fn)
185 wt_status_prepare(s);
189 s->hints = advice_status_hints; /* must come after git_config() */
192 static void rollback_index_files(void)
194 switch (commit_style) {
196 break; /* nothing to do */
198 rollback_lock_file(&index_lock);
201 rollback_lock_file(&index_lock);
202 rollback_lock_file(&false_lock);
207 static int commit_index_files(void)
211 switch (commit_style) {
213 break; /* nothing to do */
215 err = commit_lock_file(&index_lock);
218 err = commit_lock_file(&index_lock);
219 rollback_lock_file(&false_lock);
227 * Take a union of paths in the index and the named tree (typically, "HEAD"),
228 * and return the paths that match the given pattern in list.
230 static int list_paths(struct string_list *list, const char *with_tree,
231 const char *prefix, const struct pathspec *pattern)
239 m = xcalloc(1, pattern->nr);
242 char *max_prefix = common_prefix(pattern);
243 overlay_tree_on_cache(with_tree, max_prefix ? max_prefix : prefix);
247 for (i = 0; i < active_nr; i++) {
248 const struct cache_entry *ce = active_cache[i];
249 struct string_list_item *item;
251 if (ce->ce_flags & CE_UPDATE)
253 if (!ce_path_match(ce, pattern, m))
255 item = string_list_insert(list, ce->name);
256 if (ce_skip_worktree(ce))
257 item->util = item; /* better a valid pointer than a fake one */
260 return report_path_error(m, pattern, prefix);
263 static void add_remove_files(struct string_list *list)
266 for (i = 0; i < list->nr; i++) {
268 struct string_list_item *p = &(list->items[i]);
270 /* p->util is skip-worktree */
274 if (!lstat(p->string, &st)) {
275 if (add_to_cache(p->string, &st, 0))
276 die(_("updating files failed"));
278 remove_file_from_cache(p->string);
282 static void create_base_index(const struct commit *current_head)
285 struct unpack_trees_options opts;
293 memset(&opts, 0, sizeof(opts));
297 opts.src_index = &the_index;
298 opts.dst_index = &the_index;
300 opts.fn = oneway_merge;
301 tree = parse_tree_indirect(current_head->object.sha1);
303 die(_("failed to unpack HEAD tree object"));
305 init_tree_desc(&t, tree->buffer, tree->size);
306 if (unpack_trees(1, &t, &opts))
307 exit(128); /* We've already reported the error, finish dying */
310 static void refresh_cache_or_die(int refresh_flags)
313 * refresh_flags contains REFRESH_QUIET, so the only errors
314 * are for unmerged entries.
316 if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
317 die_resolve_conflict("commit");
320 static const char *prepare_index(int argc, const char **argv, const char *prefix,
321 const struct commit *current_head, int is_status)
323 struct string_list partial;
324 struct pathspec pathspec;
325 int refresh_flags = REFRESH_QUIET;
328 refresh_flags |= REFRESH_UNMERGED;
329 parse_pathspec(&pathspec, 0,
330 PATHSPEC_PREFER_FULL,
333 if (read_cache_preload(&pathspec) < 0)
334 die(_("index file corrupt"));
337 char *old_index_env = NULL;
338 hold_locked_index(&index_lock, 1);
340 refresh_cache_or_die(refresh_flags);
342 if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
343 die(_("unable to create temporary index"));
345 old_index_env = getenv(INDEX_ENVIRONMENT);
346 setenv(INDEX_ENVIRONMENT, index_lock.filename.buf, 1);
348 if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
349 die(_("interactive add failed"));
351 if (old_index_env && *old_index_env)
352 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
354 unsetenv(INDEX_ENVIRONMENT);
357 read_cache_from(index_lock.filename.buf);
358 if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
359 if (reopen_lock_file(&index_lock) < 0)
360 die(_("unable to write index file"));
361 if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
362 die(_("unable to update temporary index"));
364 warning(_("Failed to update main cache tree"));
366 commit_style = COMMIT_NORMAL;
367 return index_lock.filename.buf;
371 * Non partial, non as-is commit.
373 * (1) get the real index;
374 * (2) update the_index as necessary;
375 * (3) write the_index out to the real index (still locked);
376 * (4) return the name of the locked index file.
378 * The caller should run hooks on the locked real index, and
379 * (A) if all goes well, commit the real index;
380 * (B) on failure, rollback the real index.
382 if (all || (also && pathspec.nr)) {
383 hold_locked_index(&index_lock, 1);
384 add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
385 refresh_cache_or_die(refresh_flags);
386 update_main_cache_tree(WRITE_TREE_SILENT);
387 if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
388 die(_("unable to write new_index file"));
389 commit_style = COMMIT_NORMAL;
390 return index_lock.filename.buf;
396 * (1) return the name of the real index file.
398 * The caller should run hooks on the real index,
399 * and create commit from the_index.
400 * We still need to refresh the index here.
402 if (!only && !pathspec.nr) {
403 hold_locked_index(&index_lock, 1);
404 refresh_cache_or_die(refresh_flags);
405 if (active_cache_changed
406 || !cache_tree_fully_valid(active_cache_tree)) {
407 update_main_cache_tree(WRITE_TREE_SILENT);
408 active_cache_changed = 1;
410 if (active_cache_changed) {
411 if (write_locked_index(&the_index, &index_lock,
413 die(_("unable to write new_index file"));
415 rollback_lock_file(&index_lock);
417 commit_style = COMMIT_AS_IS;
418 return get_index_file();
424 * (0) find the set of affected paths;
425 * (1) get lock on the real index file;
426 * (2) update the_index with the given paths;
427 * (3) write the_index out to the real index (still locked);
428 * (4) get lock on the false index file;
429 * (5) reset the_index from HEAD;
430 * (6) update the_index the same way as (2);
431 * (7) write the_index out to the false index file;
432 * (8) return the name of the false index file (still locked);
434 * The caller should run hooks on the locked false index, and
435 * create commit from it. Then
436 * (A) if all goes well, commit the real index;
437 * (B) on failure, rollback the real index;
438 * In either case, rollback the false index.
440 commit_style = COMMIT_PARTIAL;
442 if (whence != FROM_COMMIT) {
443 if (whence == FROM_MERGE)
444 die(_("cannot do a partial commit during a merge."));
445 else if (whence == FROM_CHERRY_PICK)
446 die(_("cannot do a partial commit during a cherry-pick."));
449 string_list_init(&partial, 1);
450 if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, &pathspec))
454 if (read_cache() < 0)
455 die(_("cannot read the index"));
457 hold_locked_index(&index_lock, 1);
458 add_remove_files(&partial);
459 refresh_cache(REFRESH_QUIET);
460 update_main_cache_tree(WRITE_TREE_SILENT);
461 if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
462 die(_("unable to write new_index file"));
464 hold_lock_file_for_update(&false_lock,
465 git_path("next-index-%"PRIuMAX,
466 (uintmax_t) getpid()),
469 create_base_index(current_head);
470 add_remove_files(&partial);
471 refresh_cache(REFRESH_QUIET);
473 if (write_locked_index(&the_index, &false_lock, CLOSE_LOCK))
474 die(_("unable to write temporary index file"));
477 read_cache_from(false_lock.filename.buf);
479 return false_lock.filename.buf;
482 static int run_status(FILE *fp, const char *index_file, const char *prefix, int nowarn,
485 unsigned char sha1[20];
487 if (s->relative_paths)
492 s->reference = "HEAD^1";
494 s->verbose = verbose;
495 s->index_file = index_file;
498 s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
500 wt_status_collect(s);
502 switch (status_format) {
503 case STATUS_FORMAT_SHORT:
504 wt_shortstatus_print(s);
506 case STATUS_FORMAT_PORCELAIN:
507 wt_porcelain_print(s);
509 case STATUS_FORMAT_UNSPECIFIED:
510 die("BUG: finalize_deferred_config() should have been called");
512 case STATUS_FORMAT_NONE:
513 case STATUS_FORMAT_LONG:
518 return s->commitable;
521 static int is_a_merge(const struct commit *current_head)
523 return !!(current_head->parents && current_head->parents->next);
526 static void export_one(const char *var, const char *s, const char *e, int hack)
528 struct strbuf buf = STRBUF_INIT;
530 strbuf_addch(&buf, hack);
531 strbuf_addf(&buf, "%.*s", (int)(e - s), s);
532 setenv(var, buf.buf, 1);
533 strbuf_release(&buf);
536 static int sane_ident_split(struct ident_split *person)
538 if (!person->name_begin || !person->name_end ||
539 person->name_begin == person->name_end)
540 return 0; /* no human readable name */
541 if (!person->mail_begin || !person->mail_end ||
542 person->mail_begin == person->mail_end)
543 return 0; /* no usable mail */
544 if (!person->date_begin || !person->date_end ||
545 !person->tz_begin || !person->tz_end)
550 static int parse_force_date(const char *in, struct strbuf *out)
552 strbuf_addch(out, '@');
554 if (parse_date(in, out) < 0) {
556 unsigned long t = approxidate_careful(in, &errors);
559 strbuf_addf(out, "%lu", t);
565 static void set_ident_var(char **buf, char *val)
571 static char *envdup(const char *var)
573 const char *val = getenv(var);
574 return val ? xstrdup(val) : NULL;
577 static void determine_author_info(struct strbuf *author_ident)
579 char *name, *email, *date;
580 struct ident_split author;
582 name = envdup("GIT_AUTHOR_NAME");
583 email = envdup("GIT_AUTHOR_EMAIL");
584 date = envdup("GIT_AUTHOR_DATE");
586 if (author_message) {
587 struct ident_split ident;
591 a = find_commit_header(author_message_buffer, "author", &len);
593 die(_("commit '%s' lacks author header"), author_message);
594 if (split_ident_line(&ident, a, len) < 0)
595 die(_("commit '%s' has malformed author line"), author_message);
597 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
598 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
600 if (ident.date_begin) {
601 struct strbuf date_buf = STRBUF_INIT;
602 strbuf_addch(&date_buf, '@');
603 strbuf_add(&date_buf, ident.date_begin, ident.date_end - ident.date_begin);
604 strbuf_addch(&date_buf, ' ');
605 strbuf_add(&date_buf, ident.tz_begin, ident.tz_end - ident.tz_begin);
606 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
611 struct ident_split ident;
613 if (split_ident_line(&ident, force_author, strlen(force_author)) < 0)
614 die(_("malformed --author parameter"));
615 set_ident_var(&name, xmemdupz(ident.name_begin, ident.name_end - ident.name_begin));
616 set_ident_var(&email, xmemdupz(ident.mail_begin, ident.mail_end - ident.mail_begin));
620 struct strbuf date_buf = STRBUF_INIT;
621 if (parse_force_date(force_date, &date_buf))
622 die(_("invalid date format: %s"), force_date);
623 set_ident_var(&date, strbuf_detach(&date_buf, NULL));
626 strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
627 if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
628 sane_ident_split(&author)) {
629 export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
630 export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
631 export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
639 static void split_ident_or_die(struct ident_split *id, const struct strbuf *buf)
641 if (split_ident_line(id, buf->buf, buf->len) ||
642 !sane_ident_split(id))
643 die(_("Malformed ident string: '%s'"), buf->buf);
646 static int author_date_is_interesting(void)
648 return author_message || force_date;
651 static void adjust_comment_line_char(const struct strbuf *sb)
653 char candidates[] = "#;@!$%^&|:";
657 comment_line_char = candidates[0];
658 if (!memchr(sb->buf, comment_line_char, sb->len))
662 candidate = strchr(candidates, *p);
665 for (p = sb->buf; *p; p++) {
666 if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
667 candidate = strchr(candidates, p[1]);
673 for (p = candidates; *p == ' '; p++)
676 die(_("unable to select a comment character that is not used\n"
677 "in the current commit message"));
678 comment_line_char = *p;
681 static int prepare_to_commit(const char *index_file, const char *prefix,
682 struct commit *current_head,
684 struct strbuf *author_ident)
687 struct strbuf committer_ident = STRBUF_INIT;
689 struct strbuf sb = STRBUF_INIT;
690 const char *hook_arg1 = NULL;
691 const char *hook_arg2 = NULL;
692 int clean_message_contents = (cleanup_mode != CLEANUP_NONE);
693 int old_display_comment_prefix;
695 /* This checks and barfs if author is badly specified */
696 determine_author_info(author_ident);
698 if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
701 if (squash_message) {
703 * Insert the proper subject line before other commit
704 * message options add their content.
706 if (use_message && !strcmp(use_message, squash_message))
707 strbuf_addstr(&sb, "squash! ");
709 struct pretty_print_context ctx = {0};
711 c = lookup_commit_reference_by_name(squash_message);
713 die(_("could not lookup commit %s"), squash_message);
714 ctx.output_encoding = get_commit_output_encoding();
715 format_commit_message(c, "squash! %s\n\n", &sb,
721 strbuf_addbuf(&sb, &message);
722 hook_arg1 = "message";
723 } else if (logfile && !strcmp(logfile, "-")) {
725 fprintf(stderr, _("(reading log message from standard input)\n"));
726 if (strbuf_read(&sb, 0, 0) < 0)
727 die_errno(_("could not read log from standard input"));
728 hook_arg1 = "message";
729 } else if (logfile) {
730 if (strbuf_read_file(&sb, logfile, 0) < 0)
731 die_errno(_("could not read log file '%s'"),
733 hook_arg1 = "message";
734 } else if (use_message) {
736 buffer = strstr(use_message_buffer, "\n\n");
738 strbuf_addstr(&sb, buffer + 2);
739 hook_arg1 = "commit";
740 hook_arg2 = use_message;
741 } else if (fixup_message) {
742 struct pretty_print_context ctx = {0};
743 struct commit *commit;
744 commit = lookup_commit_reference_by_name(fixup_message);
746 die(_("could not lookup commit %s"), fixup_message);
747 ctx.output_encoding = get_commit_output_encoding();
748 format_commit_message(commit, "fixup! %s\n\n",
750 hook_arg1 = "message";
751 } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
752 if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
753 die_errno(_("could not read MERGE_MSG"));
755 } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
756 if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
757 die_errno(_("could not read SQUASH_MSG"));
758 hook_arg1 = "squash";
759 } else if (template_file) {
760 if (strbuf_read_file(&sb, template_file, 0) < 0)
761 die_errno(_("could not read '%s'"), template_file);
762 hook_arg1 = "template";
763 clean_message_contents = 0;
767 * The remaining cases don't modify the template message, but
768 * just set the argument(s) to the prepare-commit-msg hook.
770 else if (whence == FROM_MERGE)
772 else if (whence == FROM_CHERRY_PICK) {
773 hook_arg1 = "commit";
774 hook_arg2 = "CHERRY_PICK_HEAD";
777 if (squash_message) {
779 * If squash_commit was used for the commit subject,
780 * then we're possibly hijacking other commit log options.
781 * Reset the hook args to tell the real story.
783 hook_arg1 = "message";
787 s->fp = fopen(git_path(commit_editmsg), "w");
789 die_errno(_("could not open '%s'"), git_path(commit_editmsg));
791 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
792 old_display_comment_prefix = s->display_comment_prefix;
793 s->display_comment_prefix = 1;
796 * Most hints are counter-productive when the commit has
801 if (clean_message_contents)
806 * See if we have a Conflicts: block at the end. If yes, count
807 * its size, so we can ignore it.
809 int ignore_footer = 0;
810 int i, eol, previous = 0;
813 for (i = 0; i < sb.len; i++) {
814 nl = memchr(sb.buf + i, '\n', sb.len - i);
819 if (starts_with(sb.buf + previous, "\nConflicts:\n")) {
820 ignore_footer = sb.len - previous;
828 append_signoff(&sb, ignore_footer, 0);
831 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
832 die_errno(_("could not write commit template"));
834 if (auto_comment_line_char)
835 adjust_comment_line_char(&sb);
838 /* This checks if committer ident is explicitly given */
839 strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
840 if (use_editor && include_status) {
842 int saved_color_setting;
843 struct ident_split ci, ai;
845 if (whence != FROM_COMMIT) {
846 if (cleanup_mode == CLEANUP_SCISSORS)
847 wt_status_add_cut_line(s->fp);
848 status_printf_ln(s, GIT_COLOR_NORMAL,
851 "It looks like you may be committing a merge.\n"
852 "If this is not correct, please remove the file\n"
856 "It looks like you may be committing a cherry-pick.\n"
857 "If this is not correct, please remove the file\n"
860 git_path(whence == FROM_MERGE
862 : "CHERRY_PICK_HEAD"));
865 fprintf(s->fp, "\n");
866 if (cleanup_mode == CLEANUP_ALL)
867 status_printf(s, GIT_COLOR_NORMAL,
868 _("Please enter the commit message for your changes."
869 " Lines starting\nwith '%c' will be ignored, and an empty"
870 " message aborts the commit.\n"), comment_line_char);
871 else if (cleanup_mode == CLEANUP_SCISSORS && whence == FROM_COMMIT)
872 wt_status_add_cut_line(s->fp);
873 else /* CLEANUP_SPACE, that is. */
874 status_printf(s, GIT_COLOR_NORMAL,
875 _("Please enter the commit message for your changes."
877 "with '%c' will be kept; you may remove them"
878 " yourself if you want to.\n"
879 "An empty message aborts the commit.\n"), comment_line_char);
880 if (only_include_assumed)
881 status_printf_ln(s, GIT_COLOR_NORMAL,
882 "%s", only_include_assumed);
884 split_ident_or_die(&ai, author_ident);
885 split_ident_or_die(&ci, &committer_ident);
887 if (ident_cmp(&ai, &ci))
888 status_printf_ln(s, GIT_COLOR_NORMAL,
890 "Author: %.*s <%.*s>"),
891 ident_shown++ ? "" : "\n",
892 (int)(ai.name_end - ai.name_begin), ai.name_begin,
893 (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
895 if (author_date_is_interesting())
896 status_printf_ln(s, GIT_COLOR_NORMAL,
899 ident_shown++ ? "" : "\n",
900 show_ident_date(&ai, DATE_NORMAL));
902 if (!committer_ident_sufficiently_given())
903 status_printf_ln(s, GIT_COLOR_NORMAL,
905 "Committer: %.*s <%.*s>"),
906 ident_shown++ ? "" : "\n",
907 (int)(ci.name_end - ci.name_begin), ci.name_begin,
908 (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
911 status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
913 saved_color_setting = s->use_color;
915 commitable = run_status(s->fp, index_file, prefix, 1, s);
916 s->use_color = saved_color_setting;
918 unsigned char sha1[20];
919 const char *parent = "HEAD";
921 if (!active_nr && read_cache() < 0)
922 die(_("Cannot read index"));
927 if (get_sha1(parent, sha1))
928 commitable = !!active_nr;
931 * Unless the user did explicitly request a submodule
932 * ignore mode by passing a command line option we do
933 * not ignore any changed submodule SHA-1s when
934 * comparing index and parent, no matter what is
935 * configured. Otherwise we won't commit any
936 * submodules which were manually staged, which would
937 * be really confusing.
939 int diff_flags = DIFF_OPT_OVERRIDE_SUBMODULE_CONFIG;
940 if (ignore_submodule_arg &&
941 !strcmp(ignore_submodule_arg, "all"))
942 diff_flags |= DIFF_OPT_IGNORE_SUBMODULES;
943 commitable = index_differs_from(parent, diff_flags);
946 strbuf_release(&committer_ident);
951 * Reject an attempt to record a non-merge empty commit without
952 * explicit --allow-empty. In the cherry-pick case, it may be
953 * empty due to conflict resolution, which the user should okay.
955 if (!commitable && whence != FROM_MERGE && !allow_empty &&
956 !(amend && is_a_merge(current_head))) {
957 s->display_comment_prefix = old_display_comment_prefix;
958 run_status(stdout, index_file, prefix, 0, s);
960 fputs(_(empty_amend_advice), stderr);
961 else if (whence == FROM_CHERRY_PICK) {
962 fputs(_(empty_cherry_pick_advice), stderr);
963 if (!sequencer_in_use)
964 fputs(_(empty_cherry_pick_advice_single), stderr);
966 fputs(_(empty_cherry_pick_advice_multi), stderr);
972 * Re-read the index as pre-commit hook could have updated it,
973 * and write it out as a tree. We must do this before we invoke
974 * the editor and after we invoke run_status above.
977 read_cache_from(index_file);
978 if (update_main_cache_tree(0)) {
979 error(_("Error building trees"));
983 if (run_commit_hook(use_editor, index_file, "prepare-commit-msg",
984 git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
988 char index[PATH_MAX];
989 const char *env[2] = { NULL };
991 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
992 if (launch_editor(git_path(commit_editmsg), NULL, env)) {
994 _("Please supply the message using either -m or -F option.\n"));
1000 run_commit_hook(use_editor, index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
1007 static int rest_is_empty(struct strbuf *sb, int start)
1012 /* Check if the rest is just whitespace and Signed-of-by's. */
1013 for (i = start; i < sb->len; i++) {
1014 nl = memchr(sb->buf + i, '\n', sb->len - i);
1020 if (strlen(sign_off_header) <= eol - i &&
1021 starts_with(sb->buf + i, sign_off_header)) {
1026 if (!isspace(sb->buf[i++]))
1034 * Find out if the message in the strbuf contains only whitespace and
1035 * Signed-off-by lines.
1037 static int message_is_empty(struct strbuf *sb)
1039 if (cleanup_mode == CLEANUP_NONE && sb->len)
1041 return rest_is_empty(sb, 0);
1045 * See if the user edited the message in the editor or left what
1046 * was in the template intact
1048 static int template_untouched(struct strbuf *sb)
1050 struct strbuf tmpl = STRBUF_INIT;
1053 if (cleanup_mode == CLEANUP_NONE && sb->len)
1056 if (!template_file || strbuf_read_file(&tmpl, template_file, 0) <= 0)
1059 stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
1060 if (!skip_prefix(sb->buf, tmpl.buf, &start))
1062 strbuf_release(&tmpl);
1063 return rest_is_empty(sb, start - sb->buf);
1066 static const char *find_author_by_nickname(const char *name)
1068 struct rev_info revs;
1069 struct commit *commit;
1070 struct strbuf buf = STRBUF_INIT;
1071 struct string_list mailmap = STRING_LIST_INIT_NODUP;
1075 init_revisions(&revs, NULL);
1076 strbuf_addf(&buf, "--author=%s", name);
1081 setup_revisions(ac, av, &revs, NULL);
1082 revs.mailmap = &mailmap;
1083 read_mailmap(revs.mailmap, NULL);
1085 if (prepare_revision_walk(&revs))
1086 die(_("revision walk setup failed"));
1087 commit = get_revision(&revs);
1089 struct pretty_print_context ctx = {0};
1090 ctx.date_mode = DATE_NORMAL;
1091 strbuf_release(&buf);
1092 format_commit_message(commit, "%aN <%aE>", &buf, &ctx);
1093 clear_mailmap(&mailmap);
1094 return strbuf_detach(&buf, NULL);
1096 die(_("No existing author found with '%s'"), name);
1100 static void handle_untracked_files_arg(struct wt_status *s)
1102 if (!untracked_files_arg)
1103 ; /* default already initialized */
1104 else if (!strcmp(untracked_files_arg, "no"))
1105 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1106 else if (!strcmp(untracked_files_arg, "normal"))
1107 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1108 else if (!strcmp(untracked_files_arg, "all"))
1109 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1111 die(_("Invalid untracked files mode '%s'"), untracked_files_arg);
1114 static const char *read_commit_message(const char *name)
1116 const char *out_enc;
1117 struct commit *commit;
1119 commit = lookup_commit_reference_by_name(name);
1121 die(_("could not lookup commit %s"), name);
1122 out_enc = get_commit_output_encoding();
1123 return logmsg_reencode(commit, NULL, out_enc);
1127 * Enumerate what needs to be propagated when --porcelain
1128 * is not in effect here.
1130 static struct status_deferred_config {
1131 enum status_format status_format;
1133 } status_deferred_config = {
1134 STATUS_FORMAT_UNSPECIFIED,
1135 -1 /* unspecified */
1138 static void finalize_deferred_config(struct wt_status *s)
1140 int use_deferred_config = (status_format != STATUS_FORMAT_PORCELAIN &&
1141 !s->null_termination);
1143 if (s->null_termination) {
1144 if (status_format == STATUS_FORMAT_NONE ||
1145 status_format == STATUS_FORMAT_UNSPECIFIED)
1146 status_format = STATUS_FORMAT_PORCELAIN;
1147 else if (status_format == STATUS_FORMAT_LONG)
1148 die(_("--long and -z are incompatible"));
1151 if (use_deferred_config && status_format == STATUS_FORMAT_UNSPECIFIED)
1152 status_format = status_deferred_config.status_format;
1153 if (status_format == STATUS_FORMAT_UNSPECIFIED)
1154 status_format = STATUS_FORMAT_NONE;
1156 if (use_deferred_config && s->show_branch < 0)
1157 s->show_branch = status_deferred_config.show_branch;
1158 if (s->show_branch < 0)
1162 static int parse_and_validate_options(int argc, const char *argv[],
1163 const struct option *options,
1164 const char * const usage[],
1166 struct commit *current_head,
1167 struct wt_status *s)
1171 argc = parse_options(argc, argv, prefix, options, usage, 0);
1172 finalize_deferred_config(s);
1174 if (force_author && !strchr(force_author, '>'))
1175 force_author = find_author_by_nickname(force_author);
1177 if (force_author && renew_authorship)
1178 die(_("Using both --reset-author and --author does not make sense"));
1180 if (logfile || have_option_m || use_message || fixup_message)
1183 use_editor = edit_flag;
1185 /* Sanity check options */
1186 if (amend && !current_head)
1187 die(_("You have nothing to amend."));
1188 if (amend && whence != FROM_COMMIT) {
1189 if (whence == FROM_MERGE)
1190 die(_("You are in the middle of a merge -- cannot amend."));
1191 else if (whence == FROM_CHERRY_PICK)
1192 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1194 if (fixup_message && squash_message)
1195 die(_("Options --squash and --fixup cannot be used together"));
1205 die(_("Only one of -c/-C/-F/--fixup can be used."));
1206 if (message.len && f > 0)
1207 die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
1208 if (f || message.len)
1209 template_file = NULL;
1211 use_message = edit_message;
1212 if (amend && !use_message && !fixup_message)
1213 use_message = "HEAD";
1214 if (!use_message && whence != FROM_CHERRY_PICK && renew_authorship)
1215 die(_("--reset-author can be used only with -C, -c or --amend."));
1217 use_message_buffer = read_commit_message(use_message);
1218 if (!renew_authorship) {
1219 author_message = use_message;
1220 author_message_buffer = use_message_buffer;
1223 if (whence == FROM_CHERRY_PICK && !renew_authorship) {
1224 author_message = "CHERRY_PICK_HEAD";
1225 author_message_buffer = read_commit_message(author_message);
1228 if (patch_interactive)
1231 if (also + only + all + interactive > 1)
1232 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1233 if (argc == 0 && (also || (only && !amend)))
1234 die(_("No paths with --include/--only does not make sense."));
1235 if (argc == 0 && only && amend)
1236 only_include_assumed = _("Clever... amending the last one with dirty index.");
1237 if (argc > 0 && !also && !only)
1238 only_include_assumed = _("Explicit paths specified without -i or -o; assuming --only paths...");
1239 if (!cleanup_arg || !strcmp(cleanup_arg, "default"))
1240 cleanup_mode = use_editor ? CLEANUP_ALL : CLEANUP_SPACE;
1241 else if (!strcmp(cleanup_arg, "verbatim"))
1242 cleanup_mode = CLEANUP_NONE;
1243 else if (!strcmp(cleanup_arg, "whitespace"))
1244 cleanup_mode = CLEANUP_SPACE;
1245 else if (!strcmp(cleanup_arg, "strip"))
1246 cleanup_mode = CLEANUP_ALL;
1247 else if (!strcmp(cleanup_arg, "scissors"))
1248 cleanup_mode = use_editor ? CLEANUP_SCISSORS : CLEANUP_SPACE;
1250 die(_("Invalid cleanup mode %s"), cleanup_arg);
1252 handle_untracked_files_arg(s);
1254 if (all && argc > 0)
1255 die(_("Paths with -a does not make sense."));
1257 if (status_format != STATUS_FORMAT_NONE)
1263 static int dry_run_commit(int argc, const char **argv, const char *prefix,
1264 const struct commit *current_head, struct wt_status *s)
1267 const char *index_file;
1269 index_file = prepare_index(argc, argv, prefix, current_head, 1);
1270 commitable = run_status(stdout, index_file, prefix, 0, s);
1271 rollback_index_files();
1273 return commitable ? 0 : 1;
1276 static int parse_status_slot(const char *slot)
1278 if (!strcasecmp(slot, "header"))
1279 return WT_STATUS_HEADER;
1280 if (!strcasecmp(slot, "branch"))
1281 return WT_STATUS_ONBRANCH;
1282 if (!strcasecmp(slot, "updated") || !strcasecmp(slot, "added"))
1283 return WT_STATUS_UPDATED;
1284 if (!strcasecmp(slot, "changed"))
1285 return WT_STATUS_CHANGED;
1286 if (!strcasecmp(slot, "untracked"))
1287 return WT_STATUS_UNTRACKED;
1288 if (!strcasecmp(slot, "nobranch"))
1289 return WT_STATUS_NOBRANCH;
1290 if (!strcasecmp(slot, "unmerged"))
1291 return WT_STATUS_UNMERGED;
1295 static int git_status_config(const char *k, const char *v, void *cb)
1297 struct wt_status *s = cb;
1298 const char *slot_name;
1300 if (starts_with(k, "column."))
1301 return git_column_config(k, v, "status", &s->colopts);
1302 if (!strcmp(k, "status.submodulesummary")) {
1304 s->submodule_summary = git_config_bool_or_int(k, v, &is_bool);
1305 if (is_bool && s->submodule_summary)
1306 s->submodule_summary = -1;
1309 if (!strcmp(k, "status.short")) {
1310 if (git_config_bool(k, v))
1311 status_deferred_config.status_format = STATUS_FORMAT_SHORT;
1313 status_deferred_config.status_format = STATUS_FORMAT_NONE;
1316 if (!strcmp(k, "status.branch")) {
1317 status_deferred_config.show_branch = git_config_bool(k, v);
1320 if (!strcmp(k, "status.color") || !strcmp(k, "color.status")) {
1321 s->use_color = git_config_colorbool(k, v);
1324 if (!strcmp(k, "status.displaycommentprefix")) {
1325 s->display_comment_prefix = git_config_bool(k, v);
1328 if (skip_prefix(k, "status.color.", &slot_name) ||
1329 skip_prefix(k, "color.status.", &slot_name)) {
1330 int slot = parse_status_slot(slot_name);
1334 return config_error_nonbool(k);
1335 return color_parse(v, s->color_palette[slot]);
1337 if (!strcmp(k, "status.relativepaths")) {
1338 s->relative_paths = git_config_bool(k, v);
1341 if (!strcmp(k, "status.showuntrackedfiles")) {
1343 return config_error_nonbool(k);
1344 else if (!strcmp(v, "no"))
1345 s->show_untracked_files = SHOW_NO_UNTRACKED_FILES;
1346 else if (!strcmp(v, "normal"))
1347 s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES;
1348 else if (!strcmp(v, "all"))
1349 s->show_untracked_files = SHOW_ALL_UNTRACKED_FILES;
1351 return error(_("Invalid untracked files mode '%s'"), v);
1354 return git_diff_ui_config(k, v, NULL);
1357 int cmd_status(int argc, const char **argv, const char *prefix)
1359 static struct wt_status s;
1361 unsigned char sha1[20];
1362 static struct option builtin_status_options[] = {
1363 OPT__VERBOSE(&verbose, N_("be verbose")),
1364 OPT_SET_INT('s', "short", &status_format,
1365 N_("show status concisely"), STATUS_FORMAT_SHORT),
1366 OPT_BOOL('b', "branch", &s.show_branch,
1367 N_("show branch information")),
1368 OPT_SET_INT(0, "porcelain", &status_format,
1369 N_("machine-readable output"),
1370 STATUS_FORMAT_PORCELAIN),
1371 OPT_SET_INT(0, "long", &status_format,
1372 N_("show status in long format (default)"),
1373 STATUS_FORMAT_LONG),
1374 OPT_BOOL('z', "null", &s.null_termination,
1375 N_("terminate entries with NUL")),
1376 { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg,
1378 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1379 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1380 OPT_BOOL(0, "ignored", &show_ignored_in_status,
1381 N_("show ignored files")),
1382 { OPTION_STRING, 0, "ignore-submodules", &ignore_submodule_arg, N_("when"),
1383 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1384 PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1385 OPT_COLUMN(0, "column", &s.colopts, N_("list untracked files in columns")),
1389 if (argc == 2 && !strcmp(argv[1], "-h"))
1390 usage_with_options(builtin_status_usage, builtin_status_options);
1392 status_init_config(&s, git_status_config);
1393 argc = parse_options(argc, argv, prefix,
1394 builtin_status_options,
1395 builtin_status_usage, 0);
1396 finalize_colopts(&s.colopts, -1);
1397 finalize_deferred_config(&s);
1399 handle_untracked_files_arg(&s);
1400 if (show_ignored_in_status)
1401 s.show_ignored_files = 1;
1402 parse_pathspec(&s.pathspec, 0,
1403 PATHSPEC_PREFER_FULL,
1406 read_cache_preload(&s.pathspec);
1407 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL);
1409 fd = hold_locked_index(&index_lock, 0);
1411 update_index_if_able(&the_index, &index_lock);
1413 s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
1414 s.ignore_submodule_arg = ignore_submodule_arg;
1415 wt_status_collect(&s);
1417 if (s.relative_paths)
1420 switch (status_format) {
1421 case STATUS_FORMAT_SHORT:
1422 wt_shortstatus_print(&s);
1424 case STATUS_FORMAT_PORCELAIN:
1425 wt_porcelain_print(&s);
1427 case STATUS_FORMAT_UNSPECIFIED:
1428 die("BUG: finalize_deferred_config() should have been called");
1430 case STATUS_FORMAT_NONE:
1431 case STATUS_FORMAT_LONG:
1432 s.verbose = verbose;
1433 s.ignore_submodule_arg = ignore_submodule_arg;
1434 wt_status_print(&s);
1440 static const char *implicit_ident_advice(void)
1442 char *user_config = NULL;
1443 char *xdg_config = NULL;
1446 home_config_paths(&user_config, &xdg_config, "config");
1447 config_exists = file_exists(user_config) || file_exists(xdg_config);
1452 return _(implicit_ident_advice_config);
1454 return _(implicit_ident_advice_noconfig);
1458 static void print_summary(const char *prefix, const unsigned char *sha1,
1461 struct rev_info rev;
1462 struct commit *commit;
1463 struct strbuf format = STRBUF_INIT;
1464 unsigned char junk_sha1[20];
1466 struct pretty_print_context pctx = {0};
1467 struct strbuf author_ident = STRBUF_INIT;
1468 struct strbuf committer_ident = STRBUF_INIT;
1470 commit = lookup_commit(sha1);
1472 die(_("couldn't look up newly created commit"));
1473 if (parse_commit(commit))
1474 die(_("could not parse newly created commit"));
1476 strbuf_addstr(&format, "format:%h] %s");
1478 format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
1479 format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
1480 if (strbuf_cmp(&author_ident, &committer_ident)) {
1481 strbuf_addstr(&format, "\n Author: ");
1482 strbuf_addbuf_percentquote(&format, &author_ident);
1484 if (author_date_is_interesting()) {
1485 struct strbuf date = STRBUF_INIT;
1486 format_commit_message(commit, "%ad", &date, &pctx);
1487 strbuf_addstr(&format, "\n Date: ");
1488 strbuf_addbuf_percentquote(&format, &date);
1489 strbuf_release(&date);
1491 if (!committer_ident_sufficiently_given()) {
1492 strbuf_addstr(&format, "\n Committer: ");
1493 strbuf_addbuf_percentquote(&format, &committer_ident);
1494 if (advice_implicit_identity) {
1495 strbuf_addch(&format, '\n');
1496 strbuf_addstr(&format, implicit_ident_advice());
1499 strbuf_release(&author_ident);
1500 strbuf_release(&committer_ident);
1502 init_revisions(&rev, prefix);
1503 setup_revisions(0, NULL, &rev, NULL);
1506 rev.diffopt.output_format =
1507 DIFF_FORMAT_SHORTSTAT | DIFF_FORMAT_SUMMARY;
1509 rev.verbose_header = 1;
1510 rev.show_root_diff = 1;
1511 get_commit_format(format.buf, &rev);
1512 rev.always_show_header = 0;
1513 rev.diffopt.detect_rename = 1;
1514 rev.diffopt.break_opt = 0;
1515 diff_setup_done(&rev.diffopt);
1517 head = resolve_ref_unsafe("HEAD", 0, junk_sha1, NULL);
1518 if (!strcmp(head, "HEAD"))
1519 head = _("detached HEAD");
1521 skip_prefix(head, "refs/heads/", &head);
1522 printf("[%s%s ", head, initial_commit ? _(" (root-commit)") : "");
1524 if (!log_tree_commit(&rev, commit)) {
1525 rev.always_show_header = 1;
1526 rev.use_terminator = 1;
1527 log_tree_commit(&rev, commit);
1530 strbuf_release(&format);
1533 static int git_commit_config(const char *k, const char *v, void *cb)
1535 struct wt_status *s = cb;
1538 if (!strcmp(k, "commit.template"))
1539 return git_config_pathname(&template_file, k, v);
1540 if (!strcmp(k, "commit.status")) {
1541 include_status = git_config_bool(k, v);
1544 if (!strcmp(k, "commit.cleanup"))
1545 return git_config_string(&cleanup_arg, k, v);
1546 if (!strcmp(k, "commit.gpgsign")) {
1547 sign_commit = git_config_bool(k, v) ? "" : NULL;
1551 status = git_gpg_config(k, v, NULL);
1554 return git_status_config(k, v, s);
1557 int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
1559 const char *hook_env[3] = { NULL };
1560 char index[PATH_MAX];
1564 snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
1565 hook_env[0] = index;
1568 * Let the hook know that no editor will be launched.
1570 if (!editor_is_used)
1571 hook_env[1] = "GIT_EDITOR=:";
1573 va_start(args, name);
1574 ret = run_hook_ve(hook_env, name, args);
1580 int cmd_commit(int argc, const char **argv, const char *prefix)
1582 static struct wt_status s;
1583 static struct option builtin_commit_options[] = {
1584 OPT__QUIET(&quiet, N_("suppress summary after successful commit")),
1585 OPT__VERBOSE(&verbose, N_("show diff in commit message template")),
1587 OPT_GROUP(N_("Commit message options")),
1588 OPT_FILENAME('F', "file", &logfile, N_("read message from file")),
1589 OPT_STRING(0, "author", &force_author, N_("author"), N_("override author for commit")),
1590 OPT_STRING(0, "date", &force_date, N_("date"), N_("override date for commit")),
1591 OPT_CALLBACK('m', "message", &message, N_("message"), N_("commit message"), opt_parse_m),
1592 OPT_STRING('c', "reedit-message", &edit_message, N_("commit"), N_("reuse and edit message from specified commit")),
1593 OPT_STRING('C', "reuse-message", &use_message, N_("commit"), N_("reuse message from specified commit")),
1594 OPT_STRING(0, "fixup", &fixup_message, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
1595 OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1596 OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1597 OPT_BOOL('s', "signoff", &signoff, N_("add Signed-off-by:")),
1598 OPT_FILENAME('t', "template", &template_file, N_("use specified template file")),
1599 OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")),
1600 OPT_STRING(0, "cleanup", &cleanup_arg, N_("default"), N_("how to strip spaces and #comments from message")),
1601 OPT_BOOL(0, "status", &include_status, N_("include status in commit message template")),
1602 { OPTION_STRING, 'S', "gpg-sign", &sign_commit, N_("key-id"),
1603 N_("GPG sign commit"), PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1604 /* end commit message options */
1606 OPT_GROUP(N_("Commit contents options")),
1607 OPT_BOOL('a', "all", &all, N_("commit all changed files")),
1608 OPT_BOOL('i', "include", &also, N_("add specified files to index for commit")),
1609 OPT_BOOL(0, "interactive", &interactive, N_("interactively add files")),
1610 OPT_BOOL('p', "patch", &patch_interactive, N_("interactively add changes")),
1611 OPT_BOOL('o', "only", &only, N_("commit only specified files")),
1612 OPT_BOOL('n', "no-verify", &no_verify, N_("bypass pre-commit hook")),
1613 OPT_BOOL(0, "dry-run", &dry_run, N_("show what would be committed")),
1614 OPT_SET_INT(0, "short", &status_format, N_("show status concisely"),
1615 STATUS_FORMAT_SHORT),
1616 OPT_BOOL(0, "branch", &s.show_branch, N_("show branch information")),
1617 OPT_SET_INT(0, "porcelain", &status_format,
1618 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN),
1619 OPT_SET_INT(0, "long", &status_format,
1620 N_("show status in long format (default)"),
1621 STATUS_FORMAT_LONG),
1622 OPT_BOOL('z', "null", &s.null_termination,
1623 N_("terminate entries with NUL")),
1624 OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
1625 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
1626 { 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" },
1627 /* end commit contents options */
1629 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,
1630 N_("ok to record an empty change")),
1631 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message,
1632 N_("ok to record a change with an empty message")),
1637 struct strbuf sb = STRBUF_INIT;
1638 struct strbuf author_ident = STRBUF_INIT;
1639 const char *index_file, *reflog_msg;
1641 unsigned char sha1[20];
1642 struct commit_list *parents = NULL, **pptr = &parents;
1643 struct stat statbuf;
1644 struct commit *current_head = NULL;
1645 struct commit_extra_header *extra = NULL;
1646 struct ref_transaction *transaction;
1647 struct strbuf err = STRBUF_INIT;
1649 if (argc == 2 && !strcmp(argv[1], "-h"))
1650 usage_with_options(builtin_commit_usage, builtin_commit_options);
1652 status_init_config(&s, git_commit_config);
1653 status_format = STATUS_FORMAT_NONE; /* Ignore status.short */
1656 if (get_sha1("HEAD", sha1))
1657 current_head = NULL;
1659 current_head = lookup_commit_or_die(sha1, "HEAD");
1660 if (parse_commit(current_head))
1661 die(_("could not parse HEAD commit"));
1663 argc = parse_and_validate_options(argc, argv, builtin_commit_options,
1664 builtin_commit_usage,
1665 prefix, current_head, &s);
1667 return dry_run_commit(argc, argv, prefix, current_head, &s);
1668 index_file = prepare_index(argc, argv, prefix, current_head, 0);
1670 /* Set up everything for writing the commit object. This includes
1671 running hooks, writing the trees, and interacting with the user. */
1672 if (!prepare_to_commit(index_file, prefix,
1673 current_head, &s, &author_ident)) {
1674 rollback_index_files();
1678 /* Determine parents */
1679 reflog_msg = getenv("GIT_REFLOG_ACTION");
1680 if (!current_head) {
1682 reflog_msg = "commit (initial)";
1684 struct commit_list *c;
1687 reflog_msg = "commit (amend)";
1688 for (c = current_head->parents; c; c = c->next)
1689 pptr = &commit_list_insert(c->item, pptr)->next;
1690 } else if (whence == FROM_MERGE) {
1691 struct strbuf m = STRBUF_INIT;
1693 int allow_fast_forward = 1;
1694 int reverse_parents = 0;
1697 reflog_msg = "commit (merge)";
1698 pptr = &commit_list_insert(current_head, pptr)->next;
1699 fp = fopen(git_path("MERGE_HEAD"), "r");
1701 die_errno(_("could not open '%s' for reading"),
1702 git_path("MERGE_HEAD"));
1703 while (strbuf_getline(&m, fp, '\n') != EOF) {
1704 struct commit *parent;
1706 parent = get_merge_parent(m.buf);
1708 die(_("Corrupt MERGE_HEAD file (%s)"), m.buf);
1709 pptr = &commit_list_insert(parent, pptr)->next;
1713 if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1714 if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
1715 die_errno(_("could not read MERGE_MODE"));
1716 if (strstr(sb.buf, "no-ff"))
1717 allow_fast_forward = 0;
1718 if (strstr(sb.buf, "reverse"))
1719 reverse_parents = 1;
1721 if (allow_fast_forward)
1722 parents = reduce_heads(parents);
1723 if (reverse_parents)
1724 parents = reverse_heads(parents);
1727 reflog_msg = (whence == FROM_CHERRY_PICK)
1728 ? "commit (cherry-pick)"
1730 pptr = &commit_list_insert(current_head, pptr)->next;
1733 /* Finally, get the commit message */
1735 if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
1736 int saved_errno = errno;
1737 rollback_index_files();
1738 die(_("could not read commit message: %s"), strerror(saved_errno));
1741 if (verbose || /* Truncate the message just before the diff, if any. */
1742 cleanup_mode == CLEANUP_SCISSORS)
1743 wt_status_truncate_message_at_cut_line(&sb);
1745 if (cleanup_mode != CLEANUP_NONE)
1746 stripspace(&sb, cleanup_mode == CLEANUP_ALL);
1747 if (template_untouched(&sb) && !allow_empty_message) {
1748 rollback_index_files();
1749 fprintf(stderr, _("Aborting commit; you did not edit the message.\n"));
1752 if (message_is_empty(&sb) && !allow_empty_message) {
1753 rollback_index_files();
1754 fprintf(stderr, _("Aborting commit due to empty commit message.\n"));
1759 const char *exclude_gpgsig[2] = { "gpgsig", NULL };
1760 extra = read_commit_extra_headers(current_head, exclude_gpgsig);
1762 struct commit_extra_header **tail = &extra;
1763 append_merge_tag_headers(parents, &tail);
1766 if (commit_tree_extended(sb.buf, sb.len, active_cache_tree->sha1,
1767 parents, sha1, author_ident.buf, sign_commit, extra)) {
1768 rollback_index_files();
1769 die(_("failed to write commit object"));
1771 strbuf_release(&author_ident);
1772 free_commit_extra_headers(extra);
1774 nl = strchr(sb.buf, '\n');
1776 strbuf_setlen(&sb, nl + 1 - sb.buf);
1778 strbuf_addch(&sb, '\n');
1779 strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
1780 strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
1782 transaction = ref_transaction_begin(&err);
1784 ref_transaction_update(transaction, "HEAD", sha1,
1786 ? current_head->object.sha1 : NULL,
1787 0, !!current_head, sb.buf, &err) ||
1788 ref_transaction_commit(transaction, &err)) {
1789 rollback_index_files();
1792 ref_transaction_free(transaction);
1794 unlink(git_path("CHERRY_PICK_HEAD"));
1795 unlink(git_path("REVERT_HEAD"));
1796 unlink(git_path("MERGE_HEAD"));
1797 unlink(git_path("MERGE_MSG"));
1798 unlink(git_path("MERGE_MODE"));
1799 unlink(git_path("SQUASH_MSG"));
1801 if (commit_index_files())
1802 die (_("Repository has been updated, but unable to write\n"
1803 "new_index file. Check that disk is not full and quota is\n"
1804 "not exceeded, and then \"git reset HEAD\" to recover."));
1807 run_commit_hook(use_editor, get_index_file(), "post-commit", NULL);
1808 if (amend && !no_post_rewrite) {
1809 struct rewritten rewrite;
1810 memset(&rewrite, 0, sizeof(rewrite));
1811 add_rewritten(&rewrite, current_head->object.sha1, sha1);
1812 copy_rewrite_notes(&rewrite, "amend", "Notes added by 'git commit --amend'");
1813 run_rewrite_hook(&rewrite, "amend");
1816 print_summary(prefix, sha1, !current_head);
1818 strbuf_release(&err);