4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
9 #include "cache-tree.h"
17 #include "wt-status.h"
18 #include "run-command.h"
23 #include "parse-options.h"
24 #include "string-list.h"
26 #include "unpack-trees.h"
28 #include "submodule.h"
29 #include "gpg-interface.h"
31 #include "sequencer.h"
32 #include "notes-utils.h"
35 static const char * const builtin_commit_usage
[] = {
36 N_("git commit [options] [--] <pathspec>..."),
40 static const char * const builtin_status_usage
[] = {
41 N_("git status [options] [--] <pathspec>..."),
45 static const char implicit_ident_advice
[] =
46 N_("Your name and email address were configured automatically based\n"
47 "on your username and hostname. Please check that they are accurate.\n"
48 "You can suppress this message by setting them explicitly:\n"
50 " git config --global user.name \"Your Name\"\n"
51 " git config --global user.email you@example.com\n"
53 "After doing this, you may fix the identity used for this commit with:\n"
55 " git commit --amend --reset-author\n");
57 static const char empty_amend_advice
[] =
58 N_("You asked to amend the most recent commit, but doing so would make\n"
59 "it empty. You can repeat your command with --allow-empty, or you can\n"
60 "remove the commit entirely with \"git reset HEAD^\".\n");
62 static const char empty_cherry_pick_advice
[] =
63 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
64 "If you wish to commit it anyway, use:\n"
66 " git commit --allow-empty\n"
69 static const char empty_cherry_pick_advice_single
[] =
70 N_("Otherwise, please use 'git reset'\n");
72 static const char empty_cherry_pick_advice_multi
[] =
73 N_("If you wish to skip this commit, use:\n"
77 "Then \"git cherry-pick --continue\" will resume cherry-picking\n"
78 "the remaining commits.\n");
80 static const char *use_message_buffer
;
81 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
82 static struct lock_file index_lock
; /* real index */
83 static struct lock_file false_lock
; /* used only for partial commits */
90 static const char *logfile
, *force_author
;
91 static const char *template_file
;
93 * The _message variables are commit names from which to take
94 * the commit message and/or authorship.
96 static const char *author_message
, *author_message_buffer
;
97 static char *edit_message
, *use_message
;
98 static char *fixup_message
, *squash_message
;
99 static int all
, also
, interactive
, patch_interactive
, only
, amend
, signoff
;
100 static int edit_flag
= -1; /* unspecified */
101 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
102 static int no_post_rewrite
, allow_empty_message
;
103 static char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
;
104 static char *sign_commit
;
107 * The default commit message cleanup mode will remove the lines
108 * beginning with # (shell comments) and leading and trailing
109 * whitespaces (empty lines or containing only whitespaces)
110 * if editor is used, and only the whitespaces if the message
111 * is specified explicitly.
118 static const char *cleanup_arg
;
120 static enum commit_whence whence
;
121 static int sequencer_in_use
;
122 static int use_editor
= 1, include_status
= 1;
123 static int show_ignored_in_status
, have_option_m
;
124 static const char *only_include_assumed
;
125 static struct strbuf message
= STRBUF_INIT
;
127 static enum status_format
{
128 STATUS_FORMAT_NONE
= 0,
131 STATUS_FORMAT_PORCELAIN
,
133 STATUS_FORMAT_UNSPECIFIED
134 } status_format
= STATUS_FORMAT_UNSPECIFIED
;
136 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
138 struct strbuf
*buf
= opt
->value
;
141 strbuf_setlen(buf
, 0);
145 strbuf_addch(buf
, '\n');
146 strbuf_addstr(buf
, arg
);
147 strbuf_complete_line(buf
);
152 static void determine_whence(struct wt_status
*s
)
154 if (file_exists(git_path("MERGE_HEAD")))
156 else if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
157 whence
= FROM_CHERRY_PICK
;
158 if (file_exists(git_path("sequencer")))
159 sequencer_in_use
= 1;
162 whence
= FROM_COMMIT
;
167 static void rollback_index_files(void)
169 switch (commit_style
) {
171 break; /* nothing to do */
173 rollback_lock_file(&index_lock
);
176 rollback_lock_file(&index_lock
);
177 rollback_lock_file(&false_lock
);
182 static int commit_index_files(void)
186 switch (commit_style
) {
188 break; /* nothing to do */
190 err
= commit_lock_file(&index_lock
);
193 err
= commit_lock_file(&index_lock
);
194 rollback_lock_file(&false_lock
);
202 * Take a union of paths in the index and the named tree (typically, "HEAD"),
203 * and return the paths that match the given pattern in list.
205 static int list_paths(struct string_list
*list
, const char *with_tree
,
206 const char *prefix
, const struct pathspec
*pattern
)
214 m
= xcalloc(1, pattern
->nr
);
217 char *max_prefix
= common_prefix(pattern
);
218 overlay_tree_on_cache(with_tree
, max_prefix
? max_prefix
: prefix
);
222 for (i
= 0; i
< active_nr
; i
++) {
223 const struct cache_entry
*ce
= active_cache
[i
];
224 struct string_list_item
*item
;
226 if (ce
->ce_flags
& CE_UPDATE
)
228 if (!match_pathspec_depth(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
230 item
= string_list_insert(list
, ce
->name
);
231 if (ce_skip_worktree(ce
))
232 item
->util
= item
; /* better a valid pointer than a fake one */
235 return report_path_error(m
, pattern
, prefix
);
238 static void add_remove_files(struct string_list
*list
)
241 for (i
= 0; i
< list
->nr
; i
++) {
243 struct string_list_item
*p
= &(list
->items
[i
]);
245 /* p->util is skip-worktree */
249 if (!lstat(p
->string
, &st
)) {
250 if (add_to_cache(p
->string
, &st
, 0))
251 die(_("updating files failed"));
253 remove_file_from_cache(p
->string
);
257 static void create_base_index(const struct commit
*current_head
)
260 struct unpack_trees_options opts
;
268 memset(&opts
, 0, sizeof(opts
));
272 opts
.src_index
= &the_index
;
273 opts
.dst_index
= &the_index
;
275 opts
.fn
= oneway_merge
;
276 tree
= parse_tree_indirect(current_head
->object
.sha1
);
278 die(_("failed to unpack HEAD tree object"));
280 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
281 if (unpack_trees(1, &t
, &opts
))
282 exit(128); /* We've already reported the error, finish dying */
285 static void refresh_cache_or_die(int refresh_flags
)
288 * refresh_flags contains REFRESH_QUIET, so the only errors
289 * are for unmerged entries.
291 if (refresh_cache(refresh_flags
| REFRESH_IN_PORCELAIN
))
292 die_resolve_conflict("commit");
295 static char *prepare_index(int argc
, const char **argv
, const char *prefix
,
296 const struct commit
*current_head
, int is_status
)
299 struct string_list partial
;
300 struct pathspec pathspec
;
301 char *old_index_env
= NULL
;
302 int refresh_flags
= REFRESH_QUIET
;
305 refresh_flags
|= REFRESH_UNMERGED
;
306 parse_pathspec(&pathspec
, 0,
307 PATHSPEC_PREFER_FULL
,
310 if (read_cache_preload(&pathspec
) < 0)
311 die(_("index file corrupt"));
314 fd
= hold_locked_index(&index_lock
, 1);
316 refresh_cache_or_die(refresh_flags
);
318 if (write_cache(fd
, active_cache
, active_nr
) ||
319 close_lock_file(&index_lock
))
320 die(_("unable to create temporary index"));
322 old_index_env
= getenv(INDEX_ENVIRONMENT
);
323 setenv(INDEX_ENVIRONMENT
, index_lock
.filename
, 1);
325 if (interactive_add(argc
, argv
, prefix
, patch_interactive
) != 0)
326 die(_("interactive add failed"));
328 if (old_index_env
&& *old_index_env
)
329 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
331 unsetenv(INDEX_ENVIRONMENT
);
334 read_cache_from(index_lock
.filename
);
336 commit_style
= COMMIT_NORMAL
;
337 return index_lock
.filename
;
341 * Non partial, non as-is commit.
343 * (1) get the real index;
344 * (2) update the_index as necessary;
345 * (3) write the_index out to the real index (still locked);
346 * (4) return the name of the locked index file.
348 * The caller should run hooks on the locked real index, and
349 * (A) if all goes well, commit the real index;
350 * (B) on failure, rollback the real index.
352 if (all
|| (also
&& pathspec
.nr
)) {
353 fd
= hold_locked_index(&index_lock
, 1);
354 add_files_to_cache(also
? prefix
: NULL
, &pathspec
, 0);
355 refresh_cache_or_die(refresh_flags
);
356 update_main_cache_tree(WRITE_TREE_SILENT
);
357 if (write_cache(fd
, active_cache
, active_nr
) ||
358 close_lock_file(&index_lock
))
359 die(_("unable to write new_index file"));
360 commit_style
= COMMIT_NORMAL
;
361 return index_lock
.filename
;
367 * (1) return the name of the real index file.
369 * The caller should run hooks on the real index,
370 * and create commit from the_index.
371 * We still need to refresh the index here.
373 if (!only
&& !pathspec
.nr
) {
374 fd
= hold_locked_index(&index_lock
, 1);
375 refresh_cache_or_die(refresh_flags
);
376 if (active_cache_changed
) {
377 update_main_cache_tree(WRITE_TREE_SILENT
);
378 if (write_cache(fd
, active_cache
, active_nr
) ||
379 commit_locked_index(&index_lock
))
380 die(_("unable to write new_index file"));
382 rollback_lock_file(&index_lock
);
384 commit_style
= COMMIT_AS_IS
;
385 return get_index_file();
391 * (0) find the set of affected paths;
392 * (1) get lock on the real index file;
393 * (2) update the_index with the given paths;
394 * (3) write the_index out to the real index (still locked);
395 * (4) get lock on the false index file;
396 * (5) reset the_index from HEAD;
397 * (6) update the_index the same way as (2);
398 * (7) write the_index out to the false index file;
399 * (8) return the name of the false index file (still locked);
401 * The caller should run hooks on the locked false index, and
402 * create commit from it. Then
403 * (A) if all goes well, commit the real index;
404 * (B) on failure, rollback the real index;
405 * In either case, rollback the false index.
407 commit_style
= COMMIT_PARTIAL
;
409 if (whence
!= FROM_COMMIT
) {
410 if (whence
== FROM_MERGE
)
411 die(_("cannot do a partial commit during a merge."));
412 else if (whence
== FROM_CHERRY_PICK
)
413 die(_("cannot do a partial commit during a cherry-pick."));
416 memset(&partial
, 0, sizeof(partial
));
417 partial
.strdup_strings
= 1;
418 if (list_paths(&partial
, !current_head
? NULL
: "HEAD", prefix
, &pathspec
))
422 if (read_cache() < 0)
423 die(_("cannot read the index"));
425 fd
= hold_locked_index(&index_lock
, 1);
426 add_remove_files(&partial
);
427 refresh_cache(REFRESH_QUIET
);
428 if (write_cache(fd
, active_cache
, active_nr
) ||
429 close_lock_file(&index_lock
))
430 die(_("unable to write new_index file"));
432 fd
= hold_lock_file_for_update(&false_lock
,
433 git_path("next-index-%"PRIuMAX
,
434 (uintmax_t) getpid()),
437 create_base_index(current_head
);
438 add_remove_files(&partial
);
439 refresh_cache(REFRESH_QUIET
);
441 if (write_cache(fd
, active_cache
, active_nr
) ||
442 close_lock_file(&false_lock
))
443 die(_("unable to write temporary index file"));
446 read_cache_from(false_lock
.filename
);
448 return false_lock
.filename
;
451 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
454 unsigned char sha1
[20];
456 if (s
->relative_paths
)
461 s
->reference
= "HEAD^1";
463 s
->verbose
= verbose
;
464 s
->index_file
= index_file
;
467 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
469 wt_status_collect(s
);
471 switch (status_format
) {
472 case STATUS_FORMAT_SHORT
:
473 wt_shortstatus_print(s
);
475 case STATUS_FORMAT_PORCELAIN
:
476 wt_porcelain_print(s
);
478 case STATUS_FORMAT_UNSPECIFIED
:
479 die("BUG: finalize_deferred_config() should have been called");
481 case STATUS_FORMAT_NONE
:
482 case STATUS_FORMAT_LONG
:
487 return s
->commitable
;
490 static int is_a_merge(const struct commit
*current_head
)
492 return !!(current_head
->parents
&& current_head
->parents
->next
);
495 static void export_one(const char *var
, const char *s
, const char *e
, int hack
)
497 struct strbuf buf
= STRBUF_INIT
;
499 strbuf_addch(&buf
, hack
);
500 strbuf_addf(&buf
, "%.*s", (int)(e
- s
), s
);
501 setenv(var
, buf
.buf
, 1);
502 strbuf_release(&buf
);
505 static int sane_ident_split(struct ident_split
*person
)
507 if (!person
->name_begin
|| !person
->name_end
||
508 person
->name_begin
== person
->name_end
)
509 return 0; /* no human readable name */
510 if (!person
->mail_begin
|| !person
->mail_end
||
511 person
->mail_begin
== person
->mail_end
)
512 return 0; /* no usable mail */
513 if (!person
->date_begin
|| !person
->date_end
||
514 !person
->tz_begin
|| !person
->tz_end
)
519 static void determine_author_info(struct strbuf
*author_ident
)
521 char *name
, *email
, *date
;
522 struct ident_split author
;
524 name
= getenv("GIT_AUTHOR_NAME");
525 email
= getenv("GIT_AUTHOR_EMAIL");
526 date
= getenv("GIT_AUTHOR_DATE");
528 if (author_message
) {
529 const char *a
, *lb
, *rb
, *eol
;
532 a
= strstr(author_message_buffer
, "\nauthor ");
534 die(_("invalid commit: %s"), author_message
);
536 lb
= strchrnul(a
+ strlen("\nauthor "), '<');
537 rb
= strchrnul(lb
, '>');
538 eol
= strchrnul(rb
, '\n');
539 if (!*lb
|| !*rb
|| !*eol
)
540 die(_("invalid commit: %s"), author_message
);
542 if (lb
== a
+ strlen("\nauthor "))
543 /* \nauthor <foo@example.com> */
544 name
= xcalloc(1, 1);
546 name
= xmemdupz(a
+ strlen("\nauthor "),
548 (a
+ strlen("\nauthor "))));
549 email
= xmemdupz(lb
+ strlen("<"), rb
- (lb
+ strlen("<")));
550 len
= eol
- (rb
+ strlen("> "));
551 date
= xmalloc(len
+ 2);
553 memcpy(date
+ 1, rb
+ strlen("> "), len
);
554 date
[len
+ 1] = '\0';
558 const char *lb
= strstr(force_author
, " <");
559 const char *rb
= strchr(force_author
, '>');
562 die(_("malformed --author parameter"));
563 name
= xstrndup(force_author
, lb
- force_author
);
564 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
569 strbuf_addstr(author_ident
, fmt_ident(name
, email
, date
, IDENT_STRICT
));
570 if (!split_ident_line(&author
, author_ident
->buf
, author_ident
->len
) &&
571 sane_ident_split(&author
)) {
572 export_one("GIT_AUTHOR_NAME", author
.name_begin
, author
.name_end
, 0);
573 export_one("GIT_AUTHOR_EMAIL", author
.mail_begin
, author
.mail_end
, 0);
574 export_one("GIT_AUTHOR_DATE", author
.date_begin
, author
.tz_end
, '@');
578 static char *cut_ident_timestamp_part(char *string
)
580 char *ket
= strrchr(string
, '>');
581 if (!ket
|| ket
[1] != ' ')
582 die(_("Malformed ident string: '%s'"), string
);
587 static int prepare_to_commit(const char *index_file
, const char *prefix
,
588 struct commit
*current_head
,
590 struct strbuf
*author_ident
)
593 struct strbuf committer_ident
= STRBUF_INIT
;
594 int commitable
, saved_color_setting
;
595 struct strbuf sb
= STRBUF_INIT
;
597 const char *hook_arg1
= NULL
;
598 const char *hook_arg2
= NULL
;
600 int clean_message_contents
= (cleanup_mode
!= CLEANUP_NONE
);
602 /* This checks and barfs if author is badly specified */
603 determine_author_info(author_ident
);
605 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
608 if (squash_message
) {
610 * Insert the proper subject line before other commit
611 * message options add their content.
613 if (use_message
&& !strcmp(use_message
, squash_message
))
614 strbuf_addstr(&sb
, "squash! ");
616 struct pretty_print_context ctx
= {0};
618 c
= lookup_commit_reference_by_name(squash_message
);
620 die(_("could not lookup commit %s"), squash_message
);
621 ctx
.output_encoding
= get_commit_output_encoding();
622 format_commit_message(c
, "squash! %s\n\n", &sb
,
628 strbuf_addbuf(&sb
, &message
);
629 hook_arg1
= "message";
630 } else if (logfile
&& !strcmp(logfile
, "-")) {
632 fprintf(stderr
, _("(reading log message from standard input)\n"));
633 if (strbuf_read(&sb
, 0, 0) < 0)
634 die_errno(_("could not read log from standard input"));
635 hook_arg1
= "message";
636 } else if (logfile
) {
637 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
638 die_errno(_("could not read log file '%s'"),
640 hook_arg1
= "message";
641 } else if (use_message
) {
642 buffer
= strstr(use_message_buffer
, "\n\n");
643 if (!use_editor
&& (!buffer
|| buffer
[2] == '\0'))
644 die(_("commit has empty message"));
645 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
646 hook_arg1
= "commit";
647 hook_arg2
= use_message
;
648 } else if (fixup_message
) {
649 struct pretty_print_context ctx
= {0};
650 struct commit
*commit
;
651 commit
= lookup_commit_reference_by_name(fixup_message
);
653 die(_("could not lookup commit %s"), fixup_message
);
654 ctx
.output_encoding
= get_commit_output_encoding();
655 format_commit_message(commit
, "fixup! %s\n\n",
657 hook_arg1
= "message";
658 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
659 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
660 die_errno(_("could not read MERGE_MSG"));
662 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
663 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
664 die_errno(_("could not read SQUASH_MSG"));
665 hook_arg1
= "squash";
666 } else if (template_file
) {
667 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
668 die_errno(_("could not read '%s'"), template_file
);
669 hook_arg1
= "template";
670 clean_message_contents
= 0;
674 * The remaining cases don't modify the template message, but
675 * just set the argument(s) to the prepare-commit-msg hook.
677 else if (whence
== FROM_MERGE
)
679 else if (whence
== FROM_CHERRY_PICK
) {
680 hook_arg1
= "commit";
681 hook_arg2
= "CHERRY_PICK_HEAD";
684 if (squash_message
) {
686 * If squash_commit was used for the commit subject,
687 * then we're possibly hijacking other commit log options.
688 * Reset the hook args to tell the real story.
690 hook_arg1
= "message";
694 s
->fp
= fopen(git_path(commit_editmsg
), "w");
696 die_errno(_("could not open '%s'"), git_path(commit_editmsg
));
698 if (clean_message_contents
)
703 * See if we have a Conflicts: block at the end. If yes, count
704 * its size, so we can ignore it.
706 int ignore_footer
= 0;
707 int i
, eol
, previous
= 0;
710 for (i
= 0; i
< sb
.len
; i
++) {
711 nl
= memchr(sb
.buf
+ i
, '\n', sb
.len
- i
);
716 if (!prefixcmp(sb
.buf
+ previous
, "\nConflicts:\n")) {
717 ignore_footer
= sb
.len
- previous
;
725 append_signoff(&sb
, ignore_footer
, 0);
728 if (fwrite(sb
.buf
, 1, sb
.len
, s
->fp
) < sb
.len
)
729 die_errno(_("could not write commit template"));
733 /* This checks if committer ident is explicitly given */
734 strbuf_addstr(&committer_ident
, git_committer_info(IDENT_STRICT
));
735 if (use_editor
&& include_status
) {
736 char *ai_tmp
, *ci_tmp
;
737 if (whence
!= FROM_COMMIT
)
738 status_printf_ln(s
, GIT_COLOR_NORMAL
,
741 "It looks like you may be committing a merge.\n"
742 "If this is not correct, please remove the file\n"
746 "It looks like you may be committing a cherry-pick.\n"
747 "If this is not correct, please remove the file\n"
750 git_path(whence
== FROM_MERGE
752 : "CHERRY_PICK_HEAD"));
754 fprintf(s
->fp
, "\n");
755 if (cleanup_mode
== CLEANUP_ALL
)
756 status_printf(s
, GIT_COLOR_NORMAL
,
757 _("Please enter the commit message for your changes."
758 " Lines starting\nwith '%c' will be ignored, and an empty"
759 " message aborts the commit.\n"), comment_line_char
);
760 else /* CLEANUP_SPACE, that is. */
761 status_printf(s
, GIT_COLOR_NORMAL
,
762 _("Please enter the commit message for your changes."
764 "with '%c' will be kept; you may remove them"
765 " yourself if you want to.\n"
766 "An empty message aborts the commit.\n"), comment_line_char
);
767 if (only_include_assumed
)
768 status_printf_ln(s
, GIT_COLOR_NORMAL
,
769 "%s", only_include_assumed
);
771 ai_tmp
= cut_ident_timestamp_part(author_ident
->buf
);
772 ci_tmp
= cut_ident_timestamp_part(committer_ident
.buf
);
773 if (strcmp(author_ident
->buf
, committer_ident
.buf
))
774 status_printf_ln(s
, GIT_COLOR_NORMAL
,
777 ident_shown
++ ? "" : "\n",
780 if (!committer_ident_sufficiently_given())
781 status_printf_ln(s
, GIT_COLOR_NORMAL
,
784 ident_shown
++ ? "" : "\n",
785 committer_ident
.buf
);
788 status_printf_ln(s
, GIT_COLOR_NORMAL
, "");
790 saved_color_setting
= s
->use_color
;
792 commitable
= run_status(s
->fp
, index_file
, prefix
, 1, s
);
793 s
->use_color
= saved_color_setting
;
798 unsigned char sha1
[20];
799 const char *parent
= "HEAD";
801 if (!active_nr
&& read_cache() < 0)
802 die(_("Cannot read index"));
807 if (get_sha1(parent
, sha1
))
808 commitable
= !!active_nr
;
810 commitable
= index_differs_from(parent
, 0);
812 strbuf_release(&committer_ident
);
817 * Reject an attempt to record a non-merge empty commit without
818 * explicit --allow-empty. In the cherry-pick case, it may be
819 * empty due to conflict resolution, which the user should okay.
821 if (!commitable
&& whence
!= FROM_MERGE
&& !allow_empty
&&
822 !(amend
&& is_a_merge(current_head
))) {
823 run_status(stdout
, index_file
, prefix
, 0, s
);
825 fputs(_(empty_amend_advice
), stderr
);
826 else if (whence
== FROM_CHERRY_PICK
) {
827 fputs(_(empty_cherry_pick_advice
), stderr
);
828 if (!sequencer_in_use
)
829 fputs(_(empty_cherry_pick_advice_single
), stderr
);
831 fputs(_(empty_cherry_pick_advice_multi
), stderr
);
837 * Re-read the index as pre-commit hook could have updated it,
838 * and write it out as a tree. We must do this before we invoke
839 * the editor and after we invoke run_status above.
842 read_cache_from(index_file
);
843 if (update_main_cache_tree(0)) {
844 error(_("Error building trees"));
848 if (run_hook(index_file
, "prepare-commit-msg",
849 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
853 char index
[PATH_MAX
];
854 const char *env
[2] = { NULL
};
856 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
857 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
859 _("Please supply the message using either -m or -F option.\n"));
865 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
872 static int rest_is_empty(struct strbuf
*sb
, int start
)
877 /* Check if the rest is just whitespace and Signed-of-by's. */
878 for (i
= start
; i
< sb
->len
; i
++) {
879 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
885 if (strlen(sign_off_header
) <= eol
- i
&&
886 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
891 if (!isspace(sb
->buf
[i
++]))
899 * Find out if the message in the strbuf contains only whitespace and
900 * Signed-off-by lines.
902 static int message_is_empty(struct strbuf
*sb
)
904 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
906 return rest_is_empty(sb
, 0);
910 * See if the user edited the message in the editor or left what
911 * was in the template intact
913 static int template_untouched(struct strbuf
*sb
)
915 struct strbuf tmpl
= STRBUF_INIT
;
918 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
921 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
924 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
925 start
= (char *)skip_prefix(sb
->buf
, tmpl
.buf
);
928 strbuf_release(&tmpl
);
929 return rest_is_empty(sb
, start
- sb
->buf
);
932 static const char *find_author_by_nickname(const char *name
)
934 struct rev_info revs
;
935 struct commit
*commit
;
936 struct strbuf buf
= STRBUF_INIT
;
937 struct string_list mailmap
= STRING_LIST_INIT_NODUP
;
941 init_revisions(&revs
, NULL
);
942 strbuf_addf(&buf
, "--author=%s", name
);
947 setup_revisions(ac
, av
, &revs
, NULL
);
948 revs
.mailmap
= &mailmap
;
949 read_mailmap(revs
.mailmap
, NULL
);
951 prepare_revision_walk(&revs
);
952 commit
= get_revision(&revs
);
954 struct pretty_print_context ctx
= {0};
955 ctx
.date_mode
= DATE_NORMAL
;
956 strbuf_release(&buf
);
957 format_commit_message(commit
, "%aN <%aE>", &buf
, &ctx
);
958 clear_mailmap(&mailmap
);
959 return strbuf_detach(&buf
, NULL
);
961 die(_("No existing author found with '%s'"), name
);
965 static void handle_untracked_files_arg(struct wt_status
*s
)
967 if (!untracked_files_arg
)
968 ; /* default already initialized */
969 else if (!strcmp(untracked_files_arg
, "no"))
970 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
971 else if (!strcmp(untracked_files_arg
, "normal"))
972 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
973 else if (!strcmp(untracked_files_arg
, "all"))
974 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
976 die(_("Invalid untracked files mode '%s'"), untracked_files_arg
);
979 static const char *read_commit_message(const char *name
)
982 struct commit
*commit
;
984 commit
= lookup_commit_reference_by_name(name
);
986 die(_("could not lookup commit %s"), name
);
987 out_enc
= get_commit_output_encoding();
988 return logmsg_reencode(commit
, NULL
, out_enc
);
992 * Enumerate what needs to be propagated when --porcelain
993 * is not in effect here.
995 static struct status_deferred_config
{
996 enum status_format status_format
;
998 } status_deferred_config
= {
999 STATUS_FORMAT_UNSPECIFIED
,
1000 -1 /* unspecified */
1003 static void finalize_deferred_config(struct wt_status
*s
)
1005 int use_deferred_config
= (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1006 !s
->null_termination
);
1008 if (s
->null_termination
) {
1009 if (status_format
== STATUS_FORMAT_NONE
||
1010 status_format
== STATUS_FORMAT_UNSPECIFIED
)
1011 status_format
= STATUS_FORMAT_PORCELAIN
;
1012 else if (status_format
== STATUS_FORMAT_LONG
)
1013 die(_("--long and -z are incompatible"));
1016 if (use_deferred_config
&& status_format
== STATUS_FORMAT_UNSPECIFIED
)
1017 status_format
= status_deferred_config
.status_format
;
1018 if (status_format
== STATUS_FORMAT_UNSPECIFIED
)
1019 status_format
= STATUS_FORMAT_NONE
;
1021 if (use_deferred_config
&& s
->show_branch
< 0)
1022 s
->show_branch
= status_deferred_config
.show_branch
;
1023 if (s
->show_branch
< 0)
1027 static int parse_and_validate_options(int argc
, const char *argv
[],
1028 const struct option
*options
,
1029 const char * const usage
[],
1031 struct commit
*current_head
,
1032 struct wt_status
*s
)
1036 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1037 finalize_deferred_config(s
);
1039 if (force_author
&& !strchr(force_author
, '>'))
1040 force_author
= find_author_by_nickname(force_author
);
1042 if (force_author
&& renew_authorship
)
1043 die(_("Using both --reset-author and --author does not make sense"));
1045 if (logfile
|| have_option_m
|| use_message
|| fixup_message
)
1048 use_editor
= edit_flag
;
1050 setenv("GIT_EDITOR", ":", 1);
1052 /* Sanity check options */
1053 if (amend
&& !current_head
)
1054 die(_("You have nothing to amend."));
1055 if (amend
&& whence
!= FROM_COMMIT
) {
1056 if (whence
== FROM_MERGE
)
1057 die(_("You are in the middle of a merge -- cannot amend."));
1058 else if (whence
== FROM_CHERRY_PICK
)
1059 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1061 if (fixup_message
&& squash_message
)
1062 die(_("Options --squash and --fixup cannot be used together"));
1072 die(_("Only one of -c/-C/-F/--fixup can be used."));
1073 if (message
.len
&& f
> 0)
1074 die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
1075 if (f
|| message
.len
)
1076 template_file
= NULL
;
1078 use_message
= edit_message
;
1079 if (amend
&& !use_message
&& !fixup_message
)
1080 use_message
= "HEAD";
1081 if (!use_message
&& whence
!= FROM_CHERRY_PICK
&& renew_authorship
)
1082 die(_("--reset-author can be used only with -C, -c or --amend."));
1084 use_message_buffer
= read_commit_message(use_message
);
1085 if (!renew_authorship
) {
1086 author_message
= use_message
;
1087 author_message_buffer
= use_message_buffer
;
1090 if (whence
== FROM_CHERRY_PICK
&& !renew_authorship
) {
1091 author_message
= "CHERRY_PICK_HEAD";
1092 author_message_buffer
= read_commit_message(author_message
);
1095 if (patch_interactive
)
1098 if (also
+ only
+ all
+ interactive
> 1)
1099 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1100 if (argc
== 0 && (also
|| (only
&& !amend
)))
1101 die(_("No paths with --include/--only does not make sense."));
1102 if (argc
== 0 && only
&& amend
)
1103 only_include_assumed
= _("Clever... amending the last one with dirty index.");
1104 if (argc
> 0 && !also
&& !only
)
1105 only_include_assumed
= _("Explicit paths specified without -i nor -o; assuming --only paths...");
1106 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
1107 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
1108 else if (!strcmp(cleanup_arg
, "verbatim"))
1109 cleanup_mode
= CLEANUP_NONE
;
1110 else if (!strcmp(cleanup_arg
, "whitespace"))
1111 cleanup_mode
= CLEANUP_SPACE
;
1112 else if (!strcmp(cleanup_arg
, "strip"))
1113 cleanup_mode
= CLEANUP_ALL
;
1115 die(_("Invalid cleanup mode %s"), cleanup_arg
);
1117 handle_untracked_files_arg(s
);
1119 if (all
&& argc
> 0)
1120 die(_("Paths with -a does not make sense."));
1122 if (status_format
!= STATUS_FORMAT_NONE
)
1128 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
1129 const struct commit
*current_head
, struct wt_status
*s
)
1132 const char *index_file
;
1134 index_file
= prepare_index(argc
, argv
, prefix
, current_head
, 1);
1135 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
1136 rollback_index_files();
1138 return commitable
? 0 : 1;
1141 static int parse_status_slot(const char *var
, int offset
)
1143 if (!strcasecmp(var
+offset
, "header"))
1144 return WT_STATUS_HEADER
;
1145 if (!strcasecmp(var
+offset
, "branch"))
1146 return WT_STATUS_ONBRANCH
;
1147 if (!strcasecmp(var
+offset
, "updated")
1148 || !strcasecmp(var
+offset
, "added"))
1149 return WT_STATUS_UPDATED
;
1150 if (!strcasecmp(var
+offset
, "changed"))
1151 return WT_STATUS_CHANGED
;
1152 if (!strcasecmp(var
+offset
, "untracked"))
1153 return WT_STATUS_UNTRACKED
;
1154 if (!strcasecmp(var
+offset
, "nobranch"))
1155 return WT_STATUS_NOBRANCH
;
1156 if (!strcasecmp(var
+offset
, "unmerged"))
1157 return WT_STATUS_UNMERGED
;
1161 static int git_status_config(const char *k
, const char *v
, void *cb
)
1163 struct wt_status
*s
= cb
;
1165 if (!prefixcmp(k
, "column."))
1166 return git_column_config(k
, v
, "status", &s
->colopts
);
1167 if (!strcmp(k
, "status.submodulesummary")) {
1169 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
1170 if (is_bool
&& s
->submodule_summary
)
1171 s
->submodule_summary
= -1;
1174 if (!strcmp(k
, "status.short")) {
1175 if (git_config_bool(k
, v
))
1176 status_deferred_config
.status_format
= STATUS_FORMAT_SHORT
;
1178 status_deferred_config
.status_format
= STATUS_FORMAT_NONE
;
1181 if (!strcmp(k
, "status.branch")) {
1182 status_deferred_config
.show_branch
= git_config_bool(k
, v
);
1185 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1186 s
->use_color
= git_config_colorbool(k
, v
);
1189 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
1190 int slot
= parse_status_slot(k
, 13);
1194 return config_error_nonbool(k
);
1195 color_parse(v
, k
, s
->color_palette
[slot
]);
1198 if (!strcmp(k
, "status.relativepaths")) {
1199 s
->relative_paths
= git_config_bool(k
, v
);
1202 if (!strcmp(k
, "status.showuntrackedfiles")) {
1204 return config_error_nonbool(k
);
1205 else if (!strcmp(v
, "no"))
1206 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1207 else if (!strcmp(v
, "normal"))
1208 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1209 else if (!strcmp(v
, "all"))
1210 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1212 return error(_("Invalid untracked files mode '%s'"), v
);
1215 return git_diff_ui_config(k
, v
, NULL
);
1218 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1220 static struct wt_status s
;
1222 unsigned char sha1
[20];
1223 static struct option builtin_status_options
[] = {
1224 OPT__VERBOSE(&verbose
, N_("be verbose")),
1225 OPT_SET_INT('s', "short", &status_format
,
1226 N_("show status concisely"), STATUS_FORMAT_SHORT
),
1227 OPT_BOOL('b', "branch", &s
.show_branch
,
1228 N_("show branch information")),
1229 OPT_SET_INT(0, "porcelain", &status_format
,
1230 N_("machine-readable output"),
1231 STATUS_FORMAT_PORCELAIN
),
1232 OPT_SET_INT(0, "long", &status_format
,
1233 N_("show status in long format (default)"),
1234 STATUS_FORMAT_LONG
),
1235 OPT_BOOL('z', "null", &s
.null_termination
,
1236 N_("terminate entries with NUL")),
1237 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1239 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1240 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1241 OPT_BOOL(0, "ignored", &show_ignored_in_status
,
1242 N_("show ignored files")),
1243 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, N_("when"),
1244 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1245 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1246 OPT_COLUMN(0, "column", &s
.colopts
, N_("list untracked files in columns")),
1250 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1251 usage_with_options(builtin_status_usage
, builtin_status_options
);
1253 wt_status_prepare(&s
);
1254 gitmodules_config();
1255 git_config(git_status_config
, &s
);
1256 determine_whence(&s
);
1257 argc
= parse_options(argc
, argv
, prefix
,
1258 builtin_status_options
,
1259 builtin_status_usage
, 0);
1260 finalize_colopts(&s
.colopts
, -1);
1261 finalize_deferred_config(&s
);
1263 handle_untracked_files_arg(&s
);
1264 if (show_ignored_in_status
)
1265 s
.show_ignored_files
= 1;
1266 parse_pathspec(&s
.pathspec
, 0,
1267 PATHSPEC_PREFER_FULL
,
1270 read_cache_preload(&s
.pathspec
);
1271 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, &s
.pathspec
, NULL
, NULL
);
1273 fd
= hold_locked_index(&index_lock
, 0);
1275 update_index_if_able(&the_index
, &index_lock
);
1277 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
1278 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1279 wt_status_collect(&s
);
1281 if (s
.relative_paths
)
1284 switch (status_format
) {
1285 case STATUS_FORMAT_SHORT
:
1286 wt_shortstatus_print(&s
);
1288 case STATUS_FORMAT_PORCELAIN
:
1289 wt_porcelain_print(&s
);
1291 case STATUS_FORMAT_UNSPECIFIED
:
1292 die("BUG: finalize_deferred_config() should have been called");
1294 case STATUS_FORMAT_NONE
:
1295 case STATUS_FORMAT_LONG
:
1296 s
.verbose
= verbose
;
1297 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1298 wt_status_print(&s
);
1304 static void print_summary(const char *prefix
, const unsigned char *sha1
,
1307 struct rev_info rev
;
1308 struct commit
*commit
;
1309 struct strbuf format
= STRBUF_INIT
;
1310 unsigned char junk_sha1
[20];
1312 struct pretty_print_context pctx
= {0};
1313 struct strbuf author_ident
= STRBUF_INIT
;
1314 struct strbuf committer_ident
= STRBUF_INIT
;
1316 commit
= lookup_commit(sha1
);
1318 die(_("couldn't look up newly created commit"));
1319 if (!commit
|| parse_commit(commit
))
1320 die(_("could not parse newly created commit"));
1322 strbuf_addstr(&format
, "format:%h] %s");
1324 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1325 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1326 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1327 strbuf_addstr(&format
, "\n Author: ");
1328 strbuf_addbuf_percentquote(&format
, &author_ident
);
1330 if (!committer_ident_sufficiently_given()) {
1331 strbuf_addstr(&format
, "\n Committer: ");
1332 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1333 if (advice_implicit_identity
) {
1334 strbuf_addch(&format
, '\n');
1335 strbuf_addstr(&format
, _(implicit_ident_advice
));
1338 strbuf_release(&author_ident
);
1339 strbuf_release(&committer_ident
);
1341 init_revisions(&rev
, prefix
);
1342 setup_revisions(0, NULL
, &rev
, NULL
);
1345 rev
.diffopt
.output_format
=
1346 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1348 rev
.verbose_header
= 1;
1349 rev
.show_root_diff
= 1;
1350 get_commit_format(format
.buf
, &rev
);
1351 rev
.always_show_header
= 0;
1352 rev
.diffopt
.detect_rename
= 1;
1353 rev
.diffopt
.break_opt
= 0;
1354 diff_setup_done(&rev
.diffopt
);
1356 head
= resolve_ref_unsafe("HEAD", junk_sha1
, 0, NULL
);
1358 !prefixcmp(head
, "refs/heads/") ?
1360 !strcmp(head
, "HEAD") ?
1361 _("detached HEAD") :
1363 initial_commit
? _(" (root-commit)") : "");
1365 if (!log_tree_commit(&rev
, commit
)) {
1366 rev
.always_show_header
= 1;
1367 rev
.use_terminator
= 1;
1368 log_tree_commit(&rev
, commit
);
1371 strbuf_release(&format
);
1374 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1376 struct wt_status
*s
= cb
;
1379 if (!strcmp(k
, "commit.template"))
1380 return git_config_pathname(&template_file
, k
, v
);
1381 if (!strcmp(k
, "commit.status")) {
1382 include_status
= git_config_bool(k
, v
);
1385 if (!strcmp(k
, "commit.cleanup"))
1386 return git_config_string(&cleanup_arg
, k
, v
);
1388 status
= git_gpg_config(k
, v
, NULL
);
1391 return git_status_config(k
, v
, s
);
1394 static int run_rewrite_hook(const unsigned char *oldsha1
,
1395 const unsigned char *newsha1
)
1397 /* oldsha1 SP newsha1 LF NUL */
1398 static char buf
[2*40 + 3];
1399 struct child_process proc
;
1400 const char *argv
[3];
1404 argv
[0] = find_hook("post-rewrite");
1411 memset(&proc
, 0, sizeof(proc
));
1414 proc
.stdout_to_stderr
= 1;
1416 code
= start_command(&proc
);
1419 n
= snprintf(buf
, sizeof(buf
), "%s %s\n",
1420 sha1_to_hex(oldsha1
), sha1_to_hex(newsha1
));
1421 write_in_full(proc
.in
, buf
, n
);
1423 return finish_command(&proc
);
1426 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1428 static struct wt_status s
;
1429 static struct option builtin_commit_options
[] = {
1430 OPT__QUIET(&quiet
, N_("suppress summary after successful commit")),
1431 OPT__VERBOSE(&verbose
, N_("show diff in commit message template")),
1433 OPT_GROUP(N_("Commit message options")),
1434 OPT_FILENAME('F', "file", &logfile
, N_("read message from file")),
1435 OPT_STRING(0, "author", &force_author
, N_("author"), N_("override author for commit")),
1436 OPT_STRING(0, "date", &force_date
, N_("date"), N_("override date for commit")),
1437 OPT_CALLBACK('m', "message", &message
, N_("message"), N_("commit message"), opt_parse_m
),
1438 OPT_STRING('c', "reedit-message", &edit_message
, N_("commit"), N_("reuse and edit message from specified commit")),
1439 OPT_STRING('C', "reuse-message", &use_message
, N_("commit"), N_("reuse message from specified commit")),
1440 OPT_STRING(0, "fixup", &fixup_message
, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
1441 OPT_STRING(0, "squash", &squash_message
, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1442 OPT_BOOL(0, "reset-author", &renew_authorship
, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1443 OPT_BOOL('s', "signoff", &signoff
, N_("add Signed-off-by:")),
1444 OPT_FILENAME('t', "template", &template_file
, N_("use specified template file")),
1445 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of commit")),
1446 OPT_STRING(0, "cleanup", &cleanup_arg
, N_("default"), N_("how to strip spaces and #comments from message")),
1447 OPT_BOOL(0, "status", &include_status
, N_("include status in commit message template")),
1448 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key id"),
1449 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1450 /* end commit message options */
1452 OPT_GROUP(N_("Commit contents options")),
1453 OPT_BOOL('a', "all", &all
, N_("commit all changed files")),
1454 OPT_BOOL('i', "include", &also
, N_("add specified files to index for commit")),
1455 OPT_BOOL(0, "interactive", &interactive
, N_("interactively add files")),
1456 OPT_BOOL('p', "patch", &patch_interactive
, N_("interactively add changes")),
1457 OPT_BOOL('o', "only", &only
, N_("commit only specified files")),
1458 OPT_BOOL('n', "no-verify", &no_verify
, N_("bypass pre-commit hook")),
1459 OPT_BOOL(0, "dry-run", &dry_run
, N_("show what would be committed")),
1460 OPT_SET_INT(0, "short", &status_format
, N_("show status concisely"),
1461 STATUS_FORMAT_SHORT
),
1462 OPT_BOOL(0, "branch", &s
.show_branch
, N_("show branch information")),
1463 OPT_SET_INT(0, "porcelain", &status_format
,
1464 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN
),
1465 OPT_SET_INT(0, "long", &status_format
,
1466 N_("show status in long format (default)"),
1467 STATUS_FORMAT_LONG
),
1468 OPT_BOOL('z', "null", &s
.null_termination
,
1469 N_("terminate entries with NUL")),
1470 OPT_BOOL(0, "amend", &amend
, N_("amend previous commit")),
1471 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite
, N_("bypass post-rewrite hook")),
1472 { 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" },
1473 /* end commit contents options */
1475 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty
,
1476 N_("ok to record an empty change")),
1477 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message
,
1478 N_("ok to record a change with an empty message")),
1483 struct strbuf sb
= STRBUF_INIT
;
1484 struct strbuf author_ident
= STRBUF_INIT
;
1485 const char *index_file
, *reflog_msg
;
1487 unsigned char sha1
[20];
1488 struct ref_lock
*ref_lock
;
1489 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1490 struct stat statbuf
;
1491 int allow_fast_forward
= 1;
1492 struct commit
*current_head
= NULL
;
1493 struct commit_extra_header
*extra
= NULL
;
1495 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1496 usage_with_options(builtin_commit_usage
, builtin_commit_options
);
1498 wt_status_prepare(&s
);
1499 gitmodules_config();
1500 git_config(git_commit_config
, &s
);
1501 status_format
= STATUS_FORMAT_NONE
; /* Ignore status.short */
1502 determine_whence(&s
);
1505 if (get_sha1("HEAD", sha1
))
1506 current_head
= NULL
;
1508 current_head
= lookup_commit_or_die(sha1
, "HEAD");
1509 if (!current_head
|| parse_commit(current_head
))
1510 die(_("could not parse HEAD commit"));
1512 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_options
,
1513 builtin_commit_usage
,
1514 prefix
, current_head
, &s
);
1516 return dry_run_commit(argc
, argv
, prefix
, current_head
, &s
);
1517 index_file
= prepare_index(argc
, argv
, prefix
, current_head
, 0);
1519 /* Set up everything for writing the commit object. This includes
1520 running hooks, writing the trees, and interacting with the user. */
1521 if (!prepare_to_commit(index_file
, prefix
,
1522 current_head
, &s
, &author_ident
)) {
1523 rollback_index_files();
1527 /* Determine parents */
1528 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1529 if (!current_head
) {
1531 reflog_msg
= "commit (initial)";
1533 struct commit_list
*c
;
1536 reflog_msg
= "commit (amend)";
1537 for (c
= current_head
->parents
; c
; c
= c
->next
)
1538 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1539 } else if (whence
== FROM_MERGE
) {
1540 struct strbuf m
= STRBUF_INIT
;
1544 reflog_msg
= "commit (merge)";
1545 pptr
= &commit_list_insert(current_head
, pptr
)->next
;
1546 fp
= fopen(git_path("MERGE_HEAD"), "r");
1548 die_errno(_("could not open '%s' for reading"),
1549 git_path("MERGE_HEAD"));
1550 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1551 struct commit
*parent
;
1553 parent
= get_merge_parent(m
.buf
);
1555 die(_("Corrupt MERGE_HEAD file (%s)"), m
.buf
);
1556 pptr
= &commit_list_insert(parent
, pptr
)->next
;
1560 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1561 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1562 die_errno(_("could not read MERGE_MODE"));
1563 if (!strcmp(sb
.buf
, "no-ff"))
1564 allow_fast_forward
= 0;
1566 if (allow_fast_forward
)
1567 parents
= reduce_heads(parents
);
1570 reflog_msg
= (whence
== FROM_CHERRY_PICK
)
1571 ? "commit (cherry-pick)"
1573 pptr
= &commit_list_insert(current_head
, pptr
)->next
;
1576 /* Finally, get the commit message */
1578 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1579 int saved_errno
= errno
;
1580 rollback_index_files();
1581 die(_("could not read commit message: %s"), strerror(saved_errno
));
1584 /* Truncate the message just before the diff, if any. */
1586 p
= strstr(sb
.buf
, "\ndiff --git ");
1588 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1591 if (cleanup_mode
!= CLEANUP_NONE
)
1592 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1593 if (template_untouched(&sb
) && !allow_empty_message
) {
1594 rollback_index_files();
1595 fprintf(stderr
, _("Aborting commit; you did not edit the message.\n"));
1598 if (message_is_empty(&sb
) && !allow_empty_message
) {
1599 rollback_index_files();
1600 fprintf(stderr
, _("Aborting commit due to empty commit message.\n"));
1605 const char *exclude_gpgsig
[2] = { "gpgsig", NULL
};
1606 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1608 struct commit_extra_header
**tail
= &extra
;
1609 append_merge_tag_headers(parents
, &tail
);
1612 if (commit_tree_extended(&sb
, active_cache_tree
->sha1
, parents
, sha1
,
1613 author_ident
.buf
, sign_commit
, extra
)) {
1614 rollback_index_files();
1615 die(_("failed to write commit object"));
1617 strbuf_release(&author_ident
);
1618 free_commit_extra_headers(extra
);
1620 ref_lock
= lock_any_ref_for_update("HEAD",
1623 : current_head
->object
.sha1
,
1626 nl
= strchr(sb
.buf
, '\n');
1628 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1630 strbuf_addch(&sb
, '\n');
1631 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1632 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1635 rollback_index_files();
1636 die(_("cannot lock HEAD ref"));
1638 if (write_ref_sha1(ref_lock
, sha1
, sb
.buf
) < 0) {
1639 rollback_index_files();
1640 die(_("cannot update HEAD ref"));
1643 unlink(git_path("CHERRY_PICK_HEAD"));
1644 unlink(git_path("REVERT_HEAD"));
1645 unlink(git_path("MERGE_HEAD"));
1646 unlink(git_path("MERGE_MSG"));
1647 unlink(git_path("MERGE_MODE"));
1648 unlink(git_path("SQUASH_MSG"));
1650 if (commit_index_files())
1651 die (_("Repository has been updated, but unable to write\n"
1652 "new_index file. Check that disk is not full or quota is\n"
1653 "not exceeded, and then \"git reset HEAD\" to recover."));
1656 run_hook(get_index_file(), "post-commit", NULL
);
1657 if (amend
&& !no_post_rewrite
) {
1658 struct notes_rewrite_cfg
*cfg
;
1659 cfg
= init_copy_notes_for_rewrite("amend");
1661 /* we are amending, so current_head is not NULL */
1662 copy_note_for_rewrite(cfg
, current_head
->object
.sha1
, sha1
);
1663 finish_copy_notes_for_rewrite(cfg
, "Notes added by 'git commit --amend'");
1665 run_rewrite_hook(current_head
->object
.sha1
, sha1
);
1668 print_summary(prefix
, sha1
, !current_head
);