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"
34 static const char * const builtin_commit_usage
[] = {
35 N_("git commit [options] [--] <pathspec>..."),
39 static const char * const builtin_status_usage
[] = {
40 N_("git status [options] [--] <pathspec>..."),
44 static const char implicit_ident_advice
[] =
45 N_("Your name and email address were configured automatically based\n"
46 "on your username and hostname. Please check that they are accurate.\n"
47 "You can suppress this message by setting them explicitly:\n"
49 " git config --global user.name \"Your Name\"\n"
50 " git config --global user.email you@example.com\n"
52 "After doing this, you may fix the identity used for this commit with:\n"
54 " git commit --amend --reset-author\n");
56 static const char empty_amend_advice
[] =
57 N_("You asked to amend the most recent commit, but doing so would make\n"
58 "it empty. You can repeat your command with --allow-empty, or you can\n"
59 "remove the commit entirely with \"git reset HEAD^\".\n");
61 static const char empty_cherry_pick_advice
[] =
62 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
63 "If you wish to commit it anyway, use:\n"
65 " git commit --allow-empty\n"
67 "Otherwise, please use 'git reset'\n");
69 static const char *use_message_buffer
;
70 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
71 static struct lock_file index_lock
; /* real index */
72 static struct lock_file false_lock
; /* used only for partial commits */
79 static const char *logfile
, *force_author
;
80 static const char *template_file
;
82 * The _message variables are commit names from which to take
83 * the commit message and/or authorship.
85 static const char *author_message
, *author_message_buffer
;
86 static char *edit_message
, *use_message
;
87 static char *fixup_message
, *squash_message
;
88 static int all
, also
, interactive
, patch_interactive
, only
, amend
, signoff
;
89 static int edit_flag
= -1; /* unspecified */
90 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
91 static int no_post_rewrite
, allow_empty_message
;
92 static char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
;
93 static char *sign_commit
;
96 * The default commit message cleanup mode will remove the lines
97 * beginning with # (shell comments) and leading and trailing
98 * whitespaces (empty lines or containing only whitespaces)
99 * if editor is used, and only the whitespaces if the message
100 * is specified explicitly.
107 static const char *cleanup_arg
;
109 static enum commit_whence whence
;
110 static int use_editor
= 1, include_status
= 1;
111 static int show_ignored_in_status
, have_option_m
;
112 static const char *only_include_assumed
;
113 static struct strbuf message
= STRBUF_INIT
;
116 STATUS_FORMAT_NONE
= 0,
119 STATUS_FORMAT_PORCELAIN
122 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
124 struct strbuf
*buf
= opt
->value
;
127 strbuf_setlen(buf
, 0);
131 strbuf_addch(buf
, '\n');
132 strbuf_addstr(buf
, arg
);
133 strbuf_complete_line(buf
);
138 static void determine_whence(struct wt_status
*s
)
140 if (file_exists(git_path("MERGE_HEAD")))
142 else if (file_exists(git_path("CHERRY_PICK_HEAD")))
143 whence
= FROM_CHERRY_PICK
;
145 whence
= FROM_COMMIT
;
150 static void rollback_index_files(void)
152 switch (commit_style
) {
154 break; /* nothing to do */
156 rollback_lock_file(&index_lock
);
159 rollback_lock_file(&index_lock
);
160 rollback_lock_file(&false_lock
);
165 static int commit_index_files(void)
169 switch (commit_style
) {
171 break; /* nothing to do */
173 err
= commit_lock_file(&index_lock
);
176 err
= commit_lock_file(&index_lock
);
177 rollback_lock_file(&false_lock
);
185 * Take a union of paths in the index and the named tree (typically, "HEAD"),
186 * and return the paths that match the given pattern in list.
188 static int list_paths(struct string_list
*list
, const char *with_tree
,
189 const char *prefix
, const char **pattern
)
197 for (i
= 0; pattern
[i
]; i
++)
202 char *max_prefix
= common_prefix(pattern
);
203 overlay_tree_on_cache(with_tree
, max_prefix
? max_prefix
: prefix
);
207 for (i
= 0; i
< active_nr
; i
++) {
208 struct cache_entry
*ce
= active_cache
[i
];
209 struct string_list_item
*item
;
211 if (ce
->ce_flags
& CE_UPDATE
)
213 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
215 item
= string_list_insert(list
, ce
->name
);
216 if (ce_skip_worktree(ce
))
217 item
->util
= item
; /* better a valid pointer than a fake one */
220 return report_path_error(m
, pattern
, prefix
);
223 static void add_remove_files(struct string_list
*list
)
226 for (i
= 0; i
< list
->nr
; i
++) {
228 struct string_list_item
*p
= &(list
->items
[i
]);
230 /* p->util is skip-worktree */
234 if (!lstat(p
->string
, &st
)) {
235 if (add_to_cache(p
->string
, &st
, 0))
236 die(_("updating files failed"));
238 remove_file_from_cache(p
->string
);
242 static void create_base_index(const struct commit
*current_head
)
245 struct unpack_trees_options opts
;
253 memset(&opts
, 0, sizeof(opts
));
257 opts
.src_index
= &the_index
;
258 opts
.dst_index
= &the_index
;
260 opts
.fn
= oneway_merge
;
261 tree
= parse_tree_indirect(current_head
->object
.sha1
);
263 die(_("failed to unpack HEAD tree object"));
265 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
266 if (unpack_trees(1, &t
, &opts
))
267 exit(128); /* We've already reported the error, finish dying */
270 static void refresh_cache_or_die(int refresh_flags
)
273 * refresh_flags contains REFRESH_QUIET, so the only errors
274 * are for unmerged entries.
276 if (refresh_cache(refresh_flags
| REFRESH_IN_PORCELAIN
))
277 die_resolve_conflict("commit");
280 static char *prepare_index(int argc
, const char **argv
, const char *prefix
,
281 const struct commit
*current_head
, int is_status
)
284 struct string_list partial
;
285 const char **pathspec
= NULL
;
286 char *old_index_env
= NULL
;
287 int refresh_flags
= REFRESH_QUIET
;
290 refresh_flags
|= REFRESH_UNMERGED
;
293 pathspec
= get_pathspec(prefix
, argv
);
295 if (read_cache_preload(pathspec
) < 0)
296 die(_("index file corrupt"));
299 fd
= hold_locked_index(&index_lock
, 1);
301 refresh_cache_or_die(refresh_flags
);
303 if (write_cache(fd
, active_cache
, active_nr
) ||
304 close_lock_file(&index_lock
))
305 die(_("unable to create temporary index"));
307 old_index_env
= getenv(INDEX_ENVIRONMENT
);
308 setenv(INDEX_ENVIRONMENT
, index_lock
.filename
, 1);
310 if (interactive_add(argc
, argv
, prefix
, patch_interactive
) != 0)
311 die(_("interactive add failed"));
313 if (old_index_env
&& *old_index_env
)
314 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
316 unsetenv(INDEX_ENVIRONMENT
);
319 read_cache_from(index_lock
.filename
);
321 commit_style
= COMMIT_NORMAL
;
322 return index_lock
.filename
;
326 * Non partial, non as-is commit.
328 * (1) get the real index;
329 * (2) update the_index as necessary;
330 * (3) write the_index out to the real index (still locked);
331 * (4) return the name of the locked index file.
333 * The caller should run hooks on the locked real index, and
334 * (A) if all goes well, commit the real index;
335 * (B) on failure, rollback the real index.
337 if (all
|| (also
&& pathspec
&& *pathspec
)) {
338 fd
= hold_locked_index(&index_lock
, 1);
339 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
340 refresh_cache_or_die(refresh_flags
);
341 update_main_cache_tree(WRITE_TREE_SILENT
);
342 if (write_cache(fd
, active_cache
, active_nr
) ||
343 close_lock_file(&index_lock
))
344 die(_("unable to write new_index file"));
345 commit_style
= COMMIT_NORMAL
;
346 return index_lock
.filename
;
352 * (1) return the name of the real index file.
354 * The caller should run hooks on the real index,
355 * and create commit from the_index.
356 * We still need to refresh the index here.
358 if (!only
&& (!pathspec
|| !*pathspec
)) {
359 fd
= hold_locked_index(&index_lock
, 1);
360 refresh_cache_or_die(refresh_flags
);
361 if (active_cache_changed
) {
362 update_main_cache_tree(WRITE_TREE_SILENT
);
363 if (write_cache(fd
, active_cache
, active_nr
) ||
364 commit_locked_index(&index_lock
))
365 die(_("unable to write new_index file"));
367 rollback_lock_file(&index_lock
);
369 commit_style
= COMMIT_AS_IS
;
370 return get_index_file();
376 * (0) find the set of affected paths;
377 * (1) get lock on the real index file;
378 * (2) update the_index with the given paths;
379 * (3) write the_index out to the real index (still locked);
380 * (4) get lock on the false index file;
381 * (5) reset the_index from HEAD;
382 * (6) update the_index the same way as (2);
383 * (7) write the_index out to the false index file;
384 * (8) return the name of the false index file (still locked);
386 * The caller should run hooks on the locked false index, and
387 * create commit from it. Then
388 * (A) if all goes well, commit the real index;
389 * (B) on failure, rollback the real index;
390 * In either case, rollback the false index.
392 commit_style
= COMMIT_PARTIAL
;
394 if (whence
!= FROM_COMMIT
) {
395 if (whence
== FROM_MERGE
)
396 die(_("cannot do a partial commit during a merge."));
397 else if (whence
== FROM_CHERRY_PICK
)
398 die(_("cannot do a partial commit during a cherry-pick."));
401 memset(&partial
, 0, sizeof(partial
));
402 partial
.strdup_strings
= 1;
403 if (list_paths(&partial
, !current_head
? NULL
: "HEAD", prefix
, pathspec
))
407 if (read_cache() < 0)
408 die(_("cannot read the index"));
410 fd
= hold_locked_index(&index_lock
, 1);
411 add_remove_files(&partial
);
412 refresh_cache(REFRESH_QUIET
);
413 if (write_cache(fd
, active_cache
, active_nr
) ||
414 close_lock_file(&index_lock
))
415 die(_("unable to write new_index file"));
417 fd
= hold_lock_file_for_update(&false_lock
,
418 git_path("next-index-%"PRIuMAX
,
419 (uintmax_t) getpid()),
422 create_base_index(current_head
);
423 add_remove_files(&partial
);
424 refresh_cache(REFRESH_QUIET
);
426 if (write_cache(fd
, active_cache
, active_nr
) ||
427 close_lock_file(&false_lock
))
428 die(_("unable to write temporary index file"));
431 read_cache_from(false_lock
.filename
);
433 return false_lock
.filename
;
436 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
439 unsigned char sha1
[20];
441 if (s
->relative_paths
)
446 s
->reference
= "HEAD^1";
448 s
->verbose
= verbose
;
449 s
->index_file
= index_file
;
452 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
454 wt_status_collect(s
);
456 switch (status_format
) {
457 case STATUS_FORMAT_SHORT
:
458 wt_shortstatus_print(s
);
460 case STATUS_FORMAT_PORCELAIN
:
461 wt_porcelain_print(s
);
463 case STATUS_FORMAT_NONE
:
464 case STATUS_FORMAT_LONG
:
469 return s
->commitable
;
472 static int is_a_merge(const struct commit
*current_head
)
474 return !!(current_head
->parents
&& current_head
->parents
->next
);
477 static void export_one(const char *var
, const char *s
, const char *e
, int hack
)
479 struct strbuf buf
= STRBUF_INIT
;
481 strbuf_addch(&buf
, hack
);
482 strbuf_addf(&buf
, "%.*s", (int)(e
- s
), s
);
483 setenv(var
, buf
.buf
, 1);
484 strbuf_release(&buf
);
487 static int sane_ident_split(struct ident_split
*person
)
489 if (!person
->name_begin
|| !person
->name_end
||
490 person
->name_begin
== person
->name_end
)
491 return 0; /* no human readable name */
492 if (!person
->mail_begin
|| !person
->mail_end
||
493 person
->mail_begin
== person
->mail_end
)
494 return 0; /* no usable mail */
495 if (!person
->date_begin
|| !person
->date_end
||
496 !person
->tz_begin
|| !person
->tz_end
)
501 static void determine_author_info(struct strbuf
*author_ident
)
503 char *name
, *email
, *date
;
504 struct ident_split author
;
506 name
= getenv("GIT_AUTHOR_NAME");
507 email
= getenv("GIT_AUTHOR_EMAIL");
508 date
= getenv("GIT_AUTHOR_DATE");
510 if (author_message
) {
511 const char *a
, *lb
, *rb
, *eol
;
514 a
= strstr(author_message_buffer
, "\nauthor ");
516 die(_("invalid commit: %s"), author_message
);
518 lb
= strchrnul(a
+ strlen("\nauthor "), '<');
519 rb
= strchrnul(lb
, '>');
520 eol
= strchrnul(rb
, '\n');
521 if (!*lb
|| !*rb
|| !*eol
)
522 die(_("invalid commit: %s"), author_message
);
524 if (lb
== a
+ strlen("\nauthor "))
525 /* \nauthor <foo@example.com> */
526 name
= xcalloc(1, 1);
528 name
= xmemdupz(a
+ strlen("\nauthor "),
530 (a
+ strlen("\nauthor "))));
531 email
= xmemdupz(lb
+ strlen("<"), rb
- (lb
+ strlen("<")));
532 date
= xmemdupz(rb
+ strlen("> "), eol
- (rb
+ strlen("> ")));
533 len
= eol
- (rb
+ strlen("> "));
534 date
= xmalloc(len
+ 2);
536 memcpy(date
+ 1, rb
+ strlen("> "), len
);
537 date
[len
+ 1] = '\0';
541 const char *lb
= strstr(force_author
, " <");
542 const char *rb
= strchr(force_author
, '>');
545 die(_("malformed --author parameter"));
546 name
= xstrndup(force_author
, lb
- force_author
);
547 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
552 strbuf_addstr(author_ident
, fmt_ident(name
, email
, date
, IDENT_STRICT
));
553 if (!split_ident_line(&author
, author_ident
->buf
, author_ident
->len
) &&
554 sane_ident_split(&author
)) {
555 export_one("GIT_AUTHOR_NAME", author
.name_begin
, author
.name_end
, 0);
556 export_one("GIT_AUTHOR_EMAIL", author
.mail_begin
, author
.mail_end
, 0);
557 export_one("GIT_AUTHOR_DATE", author
.date_begin
, author
.tz_end
, '@');
561 static char *cut_ident_timestamp_part(char *string
)
563 char *ket
= strrchr(string
, '>');
564 if (!ket
|| ket
[1] != ' ')
565 die(_("Malformed ident string: '%s'"), string
);
570 static int prepare_to_commit(const char *index_file
, const char *prefix
,
571 struct commit
*current_head
,
573 struct strbuf
*author_ident
)
576 struct strbuf committer_ident
= STRBUF_INIT
;
577 int commitable
, saved_color_setting
;
578 struct strbuf sb
= STRBUF_INIT
;
580 const char *hook_arg1
= NULL
;
581 const char *hook_arg2
= NULL
;
583 int clean_message_contents
= (cleanup_mode
!= CLEANUP_NONE
);
585 /* This checks and barfs if author is badly specified */
586 determine_author_info(author_ident
);
588 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
591 if (squash_message
) {
593 * Insert the proper subject line before other commit
594 * message options add their content.
596 if (use_message
&& !strcmp(use_message
, squash_message
))
597 strbuf_addstr(&sb
, "squash! ");
599 struct pretty_print_context ctx
= {0};
601 c
= lookup_commit_reference_by_name(squash_message
);
603 die(_("could not lookup commit %s"), squash_message
);
604 ctx
.output_encoding
= get_commit_output_encoding();
605 format_commit_message(c
, "squash! %s\n\n", &sb
,
611 strbuf_addbuf(&sb
, &message
);
612 hook_arg1
= "message";
613 } else if (logfile
&& !strcmp(logfile
, "-")) {
615 fprintf(stderr
, _("(reading log message from standard input)\n"));
616 if (strbuf_read(&sb
, 0, 0) < 0)
617 die_errno(_("could not read log from standard input"));
618 hook_arg1
= "message";
619 } else if (logfile
) {
620 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
621 die_errno(_("could not read log file '%s'"),
623 hook_arg1
= "message";
624 } else if (use_message
) {
625 buffer
= strstr(use_message_buffer
, "\n\n");
626 if (!use_editor
&& (!buffer
|| buffer
[2] == '\0'))
627 die(_("commit has empty message"));
628 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
629 hook_arg1
= "commit";
630 hook_arg2
= use_message
;
631 } else if (fixup_message
) {
632 struct pretty_print_context ctx
= {0};
633 struct commit
*commit
;
634 commit
= lookup_commit_reference_by_name(fixup_message
);
636 die(_("could not lookup commit %s"), fixup_message
);
637 ctx
.output_encoding
= get_commit_output_encoding();
638 format_commit_message(commit
, "fixup! %s\n\n",
640 hook_arg1
= "message";
641 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
642 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
643 die_errno(_("could not read MERGE_MSG"));
645 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
646 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
647 die_errno(_("could not read SQUASH_MSG"));
648 hook_arg1
= "squash";
649 } else if (template_file
) {
650 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
651 die_errno(_("could not read '%s'"), template_file
);
652 hook_arg1
= "template";
653 clean_message_contents
= 0;
657 * The remaining cases don't modify the template message, but
658 * just set the argument(s) to the prepare-commit-msg hook.
660 else if (whence
== FROM_MERGE
)
662 else if (whence
== FROM_CHERRY_PICK
) {
663 hook_arg1
= "commit";
664 hook_arg2
= "CHERRY_PICK_HEAD";
667 if (squash_message
) {
669 * If squash_commit was used for the commit subject,
670 * then we're possibly hijacking other commit log options.
671 * Reset the hook args to tell the real story.
673 hook_arg1
= "message";
677 s
->fp
= fopen(git_path(commit_editmsg
), "w");
679 die_errno(_("could not open '%s'"), git_path(commit_editmsg
));
681 if (clean_message_contents
)
686 * See if we have a Conflicts: block at the end. If yes, count
687 * its size, so we can ignore it.
689 int ignore_footer
= 0;
690 int i
, eol
, previous
= 0;
693 for (i
= 0; i
< sb
.len
; i
++) {
694 nl
= memchr(sb
.buf
+ i
, '\n', sb
.len
- i
);
699 if (!prefixcmp(sb
.buf
+ previous
, "\nConflicts:\n")) {
700 ignore_footer
= sb
.len
- previous
;
708 append_signoff(&sb
, ignore_footer
, 0);
711 if (fwrite(sb
.buf
, 1, sb
.len
, s
->fp
) < sb
.len
)
712 die_errno(_("could not write commit template"));
716 /* This checks if committer ident is explicitly given */
717 strbuf_addstr(&committer_ident
, git_committer_info(IDENT_STRICT
));
718 if (use_editor
&& include_status
) {
719 char *ai_tmp
, *ci_tmp
;
720 if (whence
!= FROM_COMMIT
)
721 status_printf_ln(s
, GIT_COLOR_NORMAL
,
724 "It looks like you may be committing a merge.\n"
725 "If this is not correct, please remove the file\n"
729 "It looks like you may be committing a cherry-pick.\n"
730 "If this is not correct, please remove the file\n"
733 git_path(whence
== FROM_MERGE
735 : "CHERRY_PICK_HEAD"));
737 fprintf(s
->fp
, "\n");
738 if (cleanup_mode
== CLEANUP_ALL
)
739 status_printf(s
, GIT_COLOR_NORMAL
,
740 _("Please enter the commit message for your changes."
741 " Lines starting\nwith '%c' will be ignored, and an empty"
742 " message aborts the commit.\n"), comment_line_char
);
743 else /* CLEANUP_SPACE, that is. */
744 status_printf(s
, GIT_COLOR_NORMAL
,
745 _("Please enter the commit message for your changes."
747 "with '%c' will be kept; you may remove them"
748 " yourself if you want to.\n"
749 "An empty message aborts the commit.\n"), comment_line_char
);
750 if (only_include_assumed
)
751 status_printf_ln(s
, GIT_COLOR_NORMAL
,
752 "%s", only_include_assumed
);
754 ai_tmp
= cut_ident_timestamp_part(author_ident
->buf
);
755 ci_tmp
= cut_ident_timestamp_part(committer_ident
.buf
);
756 if (strcmp(author_ident
->buf
, committer_ident
.buf
))
757 status_printf_ln(s
, GIT_COLOR_NORMAL
,
760 ident_shown
++ ? "" : "\n",
763 if (!committer_ident_sufficiently_given())
764 status_printf_ln(s
, GIT_COLOR_NORMAL
,
767 ident_shown
++ ? "" : "\n",
768 committer_ident
.buf
);
771 status_printf_ln(s
, GIT_COLOR_NORMAL
, "");
773 saved_color_setting
= s
->use_color
;
775 commitable
= run_status(s
->fp
, index_file
, prefix
, 1, s
);
776 s
->use_color
= saved_color_setting
;
781 unsigned char sha1
[20];
782 const char *parent
= "HEAD";
784 if (!active_nr
&& read_cache() < 0)
785 die(_("Cannot read index"));
790 if (get_sha1(parent
, sha1
))
791 commitable
= !!active_nr
;
793 commitable
= index_differs_from(parent
, 0);
795 strbuf_release(&committer_ident
);
800 * Reject an attempt to record a non-merge empty commit without
801 * explicit --allow-empty. In the cherry-pick case, it may be
802 * empty due to conflict resolution, which the user should okay.
804 if (!commitable
&& whence
!= FROM_MERGE
&& !allow_empty
&&
805 !(amend
&& is_a_merge(current_head
))) {
806 run_status(stdout
, index_file
, prefix
, 0, s
);
808 fputs(_(empty_amend_advice
), stderr
);
809 else if (whence
== FROM_CHERRY_PICK
)
810 fputs(_(empty_cherry_pick_advice
), stderr
);
815 * Re-read the index as pre-commit hook could have updated it,
816 * and write it out as a tree. We must do this before we invoke
817 * the editor and after we invoke run_status above.
820 read_cache_from(index_file
);
821 if (update_main_cache_tree(0)) {
822 error(_("Error building trees"));
826 if (run_hook(index_file
, "prepare-commit-msg",
827 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
831 char index
[PATH_MAX
];
832 const char *env
[2] = { NULL
};
834 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
835 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
837 _("Please supply the message using either -m or -F option.\n"));
843 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
850 static int rest_is_empty(struct strbuf
*sb
, int start
)
855 /* Check if the rest is just whitespace and Signed-of-by's. */
856 for (i
= start
; i
< sb
->len
; i
++) {
857 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
863 if (strlen(sign_off_header
) <= eol
- i
&&
864 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
869 if (!isspace(sb
->buf
[i
++]))
877 * Find out if the message in the strbuf contains only whitespace and
878 * Signed-off-by lines.
880 static int message_is_empty(struct strbuf
*sb
)
882 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
884 return rest_is_empty(sb
, 0);
888 * See if the user edited the message in the editor or left what
889 * was in the template intact
891 static int template_untouched(struct strbuf
*sb
)
893 struct strbuf tmpl
= STRBUF_INIT
;
896 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
899 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
902 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
903 start
= (char *)skip_prefix(sb
->buf
, tmpl
.buf
);
906 strbuf_release(&tmpl
);
907 return rest_is_empty(sb
, start
- sb
->buf
);
910 static const char *find_author_by_nickname(const char *name
)
912 struct rev_info revs
;
913 struct commit
*commit
;
914 struct strbuf buf
= STRBUF_INIT
;
918 init_revisions(&revs
, NULL
);
919 strbuf_addf(&buf
, "--author=%s", name
);
924 setup_revisions(ac
, av
, &revs
, NULL
);
925 prepare_revision_walk(&revs
);
926 commit
= get_revision(&revs
);
928 struct pretty_print_context ctx
= {0};
929 ctx
.date_mode
= DATE_NORMAL
;
930 strbuf_release(&buf
);
931 format_commit_message(commit
, "%an <%ae>", &buf
, &ctx
);
932 return strbuf_detach(&buf
, NULL
);
934 die(_("No existing author found with '%s'"), name
);
938 static void handle_untracked_files_arg(struct wt_status
*s
)
940 if (!untracked_files_arg
)
941 ; /* default already initialized */
942 else if (!strcmp(untracked_files_arg
, "no"))
943 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
944 else if (!strcmp(untracked_files_arg
, "normal"))
945 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
946 else if (!strcmp(untracked_files_arg
, "all"))
947 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
949 die(_("Invalid untracked files mode '%s'"), untracked_files_arg
);
952 static const char *read_commit_message(const char *name
)
955 struct commit
*commit
;
957 commit
= lookup_commit_reference_by_name(name
);
959 die(_("could not lookup commit %s"), name
);
960 out_enc
= get_commit_output_encoding();
961 return logmsg_reencode(commit
, NULL
, out_enc
);
964 static int parse_and_validate_options(int argc
, const char *argv
[],
965 const struct option
*options
,
966 const char * const usage
[],
968 struct commit
*current_head
,
973 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
975 if (force_author
&& !strchr(force_author
, '>'))
976 force_author
= find_author_by_nickname(force_author
);
978 if (force_author
&& renew_authorship
)
979 die(_("Using both --reset-author and --author does not make sense"));
981 if (logfile
|| have_option_m
|| use_message
|| fixup_message
)
984 use_editor
= edit_flag
;
986 setenv("GIT_EDITOR", ":", 1);
988 /* Sanity check options */
989 if (amend
&& !current_head
)
990 die(_("You have nothing to amend."));
991 if (amend
&& whence
!= FROM_COMMIT
) {
992 if (whence
== FROM_MERGE
)
993 die(_("You are in the middle of a merge -- cannot amend."));
994 else if (whence
== FROM_CHERRY_PICK
)
995 die(_("You are in the middle of a cherry-pick -- cannot amend."));
997 if (fixup_message
&& squash_message
)
998 die(_("Options --squash and --fixup cannot be used together"));
1008 die(_("Only one of -c/-C/-F/--fixup can be used."));
1009 if (message
.len
&& f
> 0)
1010 die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
1011 if (f
|| message
.len
)
1012 template_file
= NULL
;
1014 use_message
= edit_message
;
1015 if (amend
&& !use_message
&& !fixup_message
)
1016 use_message
= "HEAD";
1017 if (!use_message
&& whence
!= FROM_CHERRY_PICK
&& renew_authorship
)
1018 die(_("--reset-author can be used only with -C, -c or --amend."));
1020 use_message_buffer
= read_commit_message(use_message
);
1021 if (!renew_authorship
) {
1022 author_message
= use_message
;
1023 author_message_buffer
= use_message_buffer
;
1026 if (whence
== FROM_CHERRY_PICK
&& !renew_authorship
) {
1027 author_message
= "CHERRY_PICK_HEAD";
1028 author_message_buffer
= read_commit_message(author_message
);
1031 if (patch_interactive
)
1034 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
1035 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1036 if (argc
== 0 && (also
|| (only
&& !amend
)))
1037 die(_("No paths with --include/--only does not make sense."));
1038 if (argc
== 0 && only
&& amend
)
1039 only_include_assumed
= _("Clever... amending the last one with dirty index.");
1040 if (argc
> 0 && !also
&& !only
)
1041 only_include_assumed
= _("Explicit paths specified without -i nor -o; assuming --only paths...");
1042 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
1043 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
1044 else if (!strcmp(cleanup_arg
, "verbatim"))
1045 cleanup_mode
= CLEANUP_NONE
;
1046 else if (!strcmp(cleanup_arg
, "whitespace"))
1047 cleanup_mode
= CLEANUP_SPACE
;
1048 else if (!strcmp(cleanup_arg
, "strip"))
1049 cleanup_mode
= CLEANUP_ALL
;
1051 die(_("Invalid cleanup mode %s"), cleanup_arg
);
1053 handle_untracked_files_arg(s
);
1055 if (all
&& argc
> 0)
1056 die(_("Paths with -a does not make sense."));
1058 if (s
->null_termination
) {
1059 if (status_format
== STATUS_FORMAT_NONE
)
1060 status_format
= STATUS_FORMAT_PORCELAIN
;
1061 else if (status_format
== STATUS_FORMAT_LONG
)
1062 die(_("--long and -z are incompatible"));
1064 if (status_format
!= STATUS_FORMAT_NONE
)
1070 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
1071 const struct commit
*current_head
, struct wt_status
*s
)
1074 const char *index_file
;
1076 index_file
= prepare_index(argc
, argv
, prefix
, current_head
, 1);
1077 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
1078 rollback_index_files();
1080 return commitable
? 0 : 1;
1083 static int parse_status_slot(const char *var
, int offset
)
1085 if (!strcasecmp(var
+offset
, "header"))
1086 return WT_STATUS_HEADER
;
1087 if (!strcasecmp(var
+offset
, "branch"))
1088 return WT_STATUS_ONBRANCH
;
1089 if (!strcasecmp(var
+offset
, "updated")
1090 || !strcasecmp(var
+offset
, "added"))
1091 return WT_STATUS_UPDATED
;
1092 if (!strcasecmp(var
+offset
, "changed"))
1093 return WT_STATUS_CHANGED
;
1094 if (!strcasecmp(var
+offset
, "untracked"))
1095 return WT_STATUS_UNTRACKED
;
1096 if (!strcasecmp(var
+offset
, "nobranch"))
1097 return WT_STATUS_NOBRANCH
;
1098 if (!strcasecmp(var
+offset
, "unmerged"))
1099 return WT_STATUS_UNMERGED
;
1103 static int git_status_config(const char *k
, const char *v
, void *cb
)
1105 struct wt_status
*s
= cb
;
1107 if (!prefixcmp(k
, "column."))
1108 return git_column_config(k
, v
, "status", &s
->colopts
);
1109 if (!strcmp(k
, "status.submodulesummary")) {
1111 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
1112 if (is_bool
&& s
->submodule_summary
)
1113 s
->submodule_summary
= -1;
1116 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1117 s
->use_color
= git_config_colorbool(k
, v
);
1120 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
1121 int slot
= parse_status_slot(k
, 13);
1125 return config_error_nonbool(k
);
1126 color_parse(v
, k
, s
->color_palette
[slot
]);
1129 if (!strcmp(k
, "status.relativepaths")) {
1130 s
->relative_paths
= git_config_bool(k
, v
);
1133 if (!strcmp(k
, "status.showuntrackedfiles")) {
1135 return config_error_nonbool(k
);
1136 else if (!strcmp(v
, "no"))
1137 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1138 else if (!strcmp(v
, "normal"))
1139 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1140 else if (!strcmp(v
, "all"))
1141 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1143 return error(_("Invalid untracked files mode '%s'"), v
);
1146 return git_diff_ui_config(k
, v
, NULL
);
1149 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1151 static struct wt_status s
;
1153 unsigned char sha1
[20];
1154 static struct option builtin_status_options
[] = {
1155 OPT__VERBOSE(&verbose
, N_("be verbose")),
1156 OPT_SET_INT('s', "short", &status_format
,
1157 N_("show status concisely"), STATUS_FORMAT_SHORT
),
1158 OPT_BOOLEAN('b', "branch", &s
.show_branch
,
1159 N_("show branch information")),
1160 OPT_SET_INT(0, "porcelain", &status_format
,
1161 N_("machine-readable output"),
1162 STATUS_FORMAT_PORCELAIN
),
1163 OPT_SET_INT(0, "long", &status_format
,
1164 N_("show status in long format (default)"),
1165 STATUS_FORMAT_LONG
),
1166 OPT_BOOLEAN('z', "null", &s
.null_termination
,
1167 N_("terminate entries with NUL")),
1168 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1170 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1171 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1172 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status
,
1173 N_("show ignored files")),
1174 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, N_("when"),
1175 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1176 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1177 OPT_COLUMN(0, "column", &s
.colopts
, N_("list untracked files in columns")),
1181 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1182 usage_with_options(builtin_status_usage
, builtin_status_options
);
1184 wt_status_prepare(&s
);
1185 gitmodules_config();
1186 git_config(git_status_config
, &s
);
1187 determine_whence(&s
);
1188 argc
= parse_options(argc
, argv
, prefix
,
1189 builtin_status_options
,
1190 builtin_status_usage
, 0);
1191 finalize_colopts(&s
.colopts
, -1);
1193 if (s
.null_termination
) {
1194 if (status_format
== STATUS_FORMAT_NONE
)
1195 status_format
= STATUS_FORMAT_PORCELAIN
;
1196 else if (status_format
== STATUS_FORMAT_LONG
)
1197 die(_("--long and -z are incompatible"));
1200 handle_untracked_files_arg(&s
);
1201 if (show_ignored_in_status
)
1202 s
.show_ignored_files
= 1;
1204 s
.pathspec
= get_pathspec(prefix
, argv
);
1206 read_cache_preload(s
.pathspec
);
1207 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, s
.pathspec
, NULL
, NULL
);
1209 fd
= hold_locked_index(&index_lock
, 0);
1211 update_index_if_able(&the_index
, &index_lock
);
1213 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
1214 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1215 wt_status_collect(&s
);
1217 if (s
.relative_paths
)
1220 switch (status_format
) {
1221 case STATUS_FORMAT_SHORT
:
1222 wt_shortstatus_print(&s
);
1224 case STATUS_FORMAT_PORCELAIN
:
1225 wt_porcelain_print(&s
);
1227 case STATUS_FORMAT_NONE
:
1228 case STATUS_FORMAT_LONG
:
1229 s
.verbose
= verbose
;
1230 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1231 wt_status_print(&s
);
1237 static void print_summary(const char *prefix
, const unsigned char *sha1
,
1240 struct rev_info rev
;
1241 struct commit
*commit
;
1242 struct strbuf format
= STRBUF_INIT
;
1243 unsigned char junk_sha1
[20];
1245 struct pretty_print_context pctx
= {0};
1246 struct strbuf author_ident
= STRBUF_INIT
;
1247 struct strbuf committer_ident
= STRBUF_INIT
;
1249 commit
= lookup_commit(sha1
);
1251 die(_("couldn't look up newly created commit"));
1252 if (!commit
|| parse_commit(commit
))
1253 die(_("could not parse newly created commit"));
1255 strbuf_addstr(&format
, "format:%h] %s");
1257 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1258 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1259 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1260 strbuf_addstr(&format
, "\n Author: ");
1261 strbuf_addbuf_percentquote(&format
, &author_ident
);
1263 if (!committer_ident_sufficiently_given()) {
1264 strbuf_addstr(&format
, "\n Committer: ");
1265 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1266 if (advice_implicit_identity
) {
1267 strbuf_addch(&format
, '\n');
1268 strbuf_addstr(&format
, _(implicit_ident_advice
));
1271 strbuf_release(&author_ident
);
1272 strbuf_release(&committer_ident
);
1274 init_revisions(&rev
, prefix
);
1275 setup_revisions(0, NULL
, &rev
, NULL
);
1278 rev
.diffopt
.output_format
=
1279 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1281 rev
.verbose_header
= 1;
1282 rev
.show_root_diff
= 1;
1283 get_commit_format(format
.buf
, &rev
);
1284 rev
.always_show_header
= 0;
1285 rev
.diffopt
.detect_rename
= 1;
1286 rev
.diffopt
.break_opt
= 0;
1287 diff_setup_done(&rev
.diffopt
);
1289 head
= resolve_ref_unsafe("HEAD", junk_sha1
, 0, NULL
);
1291 !prefixcmp(head
, "refs/heads/") ?
1293 !strcmp(head
, "HEAD") ?
1294 _("detached HEAD") :
1296 initial_commit
? _(" (root-commit)") : "");
1298 if (!log_tree_commit(&rev
, commit
)) {
1299 rev
.always_show_header
= 1;
1300 rev
.use_terminator
= 1;
1301 log_tree_commit(&rev
, commit
);
1304 strbuf_release(&format
);
1307 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1309 struct wt_status
*s
= cb
;
1312 if (!strcmp(k
, "commit.template"))
1313 return git_config_pathname(&template_file
, k
, v
);
1314 if (!strcmp(k
, "commit.status")) {
1315 include_status
= git_config_bool(k
, v
);
1318 if (!strcmp(k
, "commit.cleanup"))
1319 return git_config_string(&cleanup_arg
, k
, v
);
1321 status
= git_gpg_config(k
, v
, NULL
);
1324 return git_status_config(k
, v
, s
);
1327 static int run_rewrite_hook(const unsigned char *oldsha1
,
1328 const unsigned char *newsha1
)
1330 /* oldsha1 SP newsha1 LF NUL */
1331 static char buf
[2*40 + 3];
1332 struct child_process proc
;
1333 const char *argv
[3];
1337 argv
[0] = find_hook("post-rewrite");
1344 memset(&proc
, 0, sizeof(proc
));
1347 proc
.stdout_to_stderr
= 1;
1349 code
= start_command(&proc
);
1352 n
= snprintf(buf
, sizeof(buf
), "%s %s\n",
1353 sha1_to_hex(oldsha1
), sha1_to_hex(newsha1
));
1354 write_in_full(proc
.in
, buf
, n
);
1356 return finish_command(&proc
);
1359 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1361 static struct wt_status s
;
1362 static struct option builtin_commit_options
[] = {
1363 OPT__QUIET(&quiet
, N_("suppress summary after successful commit")),
1364 OPT__VERBOSE(&verbose
, N_("show diff in commit message template")),
1366 OPT_GROUP(N_("Commit message options")),
1367 OPT_FILENAME('F', "file", &logfile
, N_("read message from file")),
1368 OPT_STRING(0, "author", &force_author
, N_("author"), N_("override author for commit")),
1369 OPT_STRING(0, "date", &force_date
, N_("date"), N_("override date for commit")),
1370 OPT_CALLBACK('m', "message", &message
, N_("message"), N_("commit message"), opt_parse_m
),
1371 OPT_STRING('c', "reedit-message", &edit_message
, N_("commit"), N_("reuse and edit message from specified commit")),
1372 OPT_STRING('C', "reuse-message", &use_message
, N_("commit"), N_("reuse message from specified commit")),
1373 OPT_STRING(0, "fixup", &fixup_message
, N_("commit"), N_("use autosquash formatted message to fixup specified commit")),
1374 OPT_STRING(0, "squash", &squash_message
, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1375 OPT_BOOLEAN(0, "reset-author", &renew_authorship
, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1376 OPT_BOOLEAN('s', "signoff", &signoff
, N_("add Signed-off-by:")),
1377 OPT_FILENAME('t', "template", &template_file
, N_("use specified template file")),
1378 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of commit")),
1379 OPT_STRING(0, "cleanup", &cleanup_arg
, N_("default"), N_("how to strip spaces and #comments from message")),
1380 OPT_BOOLEAN(0, "status", &include_status
, N_("include status in commit message template")),
1381 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key id"),
1382 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1383 /* end commit message options */
1385 OPT_GROUP(N_("Commit contents options")),
1386 OPT_BOOLEAN('a', "all", &all
, N_("commit all changed files")),
1387 OPT_BOOLEAN('i', "include", &also
, N_("add specified files to index for commit")),
1388 OPT_BOOLEAN(0, "interactive", &interactive
, N_("interactively add files")),
1389 OPT_BOOLEAN('p', "patch", &patch_interactive
, N_("interactively add changes")),
1390 OPT_BOOLEAN('o', "only", &only
, N_("commit only specified files")),
1391 OPT_BOOLEAN('n', "no-verify", &no_verify
, N_("bypass pre-commit hook")),
1392 OPT_BOOLEAN(0, "dry-run", &dry_run
, N_("show what would be committed")),
1393 OPT_SET_INT(0, "short", &status_format
, N_("show status concisely"),
1394 STATUS_FORMAT_SHORT
),
1395 OPT_BOOLEAN(0, "branch", &s
.show_branch
, N_("show branch information")),
1396 OPT_SET_INT(0, "porcelain", &status_format
,
1397 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN
),
1398 OPT_SET_INT(0, "long", &status_format
,
1399 N_("show status in long format (default)"),
1400 STATUS_FORMAT_LONG
),
1401 OPT_BOOLEAN('z', "null", &s
.null_termination
,
1402 N_("terminate entries with NUL")),
1403 OPT_BOOLEAN(0, "amend", &amend
, N_("amend previous commit")),
1404 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite
, N_("bypass post-rewrite hook")),
1405 { 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" },
1406 /* end commit contents options */
1408 { OPTION_BOOLEAN
, 0, "allow-empty", &allow_empty
, NULL
,
1409 N_("ok to record an empty change"),
1410 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
1411 { OPTION_BOOLEAN
, 0, "allow-empty-message", &allow_empty_message
, NULL
,
1412 N_("ok to record a change with an empty message"),
1413 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
1418 struct strbuf sb
= STRBUF_INIT
;
1419 struct strbuf author_ident
= STRBUF_INIT
;
1420 const char *index_file
, *reflog_msg
;
1422 unsigned char sha1
[20];
1423 struct ref_lock
*ref_lock
;
1424 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1425 struct stat statbuf
;
1426 int allow_fast_forward
= 1;
1427 struct commit
*current_head
= NULL
;
1428 struct commit_extra_header
*extra
= NULL
;
1430 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1431 usage_with_options(builtin_commit_usage
, builtin_commit_options
);
1433 wt_status_prepare(&s
);
1434 gitmodules_config();
1435 git_config(git_commit_config
, &s
);
1436 determine_whence(&s
);
1439 if (get_sha1("HEAD", sha1
))
1440 current_head
= NULL
;
1442 current_head
= lookup_commit_or_die(sha1
, "HEAD");
1443 if (!current_head
|| parse_commit(current_head
))
1444 die(_("could not parse HEAD commit"));
1446 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_options
,
1447 builtin_commit_usage
,
1448 prefix
, current_head
, &s
);
1450 return dry_run_commit(argc
, argv
, prefix
, current_head
, &s
);
1451 index_file
= prepare_index(argc
, argv
, prefix
, current_head
, 0);
1453 /* Set up everything for writing the commit object. This includes
1454 running hooks, writing the trees, and interacting with the user. */
1455 if (!prepare_to_commit(index_file
, prefix
,
1456 current_head
, &s
, &author_ident
)) {
1457 rollback_index_files();
1461 /* Determine parents */
1462 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1463 if (!current_head
) {
1465 reflog_msg
= "commit (initial)";
1467 struct commit_list
*c
;
1470 reflog_msg
= "commit (amend)";
1471 for (c
= current_head
->parents
; c
; c
= c
->next
)
1472 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1473 } else if (whence
== FROM_MERGE
) {
1474 struct strbuf m
= STRBUF_INIT
;
1478 reflog_msg
= "commit (merge)";
1479 pptr
= &commit_list_insert(current_head
, pptr
)->next
;
1480 fp
= fopen(git_path("MERGE_HEAD"), "r");
1482 die_errno(_("could not open '%s' for reading"),
1483 git_path("MERGE_HEAD"));
1484 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1485 struct commit
*parent
;
1487 parent
= get_merge_parent(m
.buf
);
1489 die(_("Corrupt MERGE_HEAD file (%s)"), m
.buf
);
1490 pptr
= &commit_list_insert(parent
, pptr
)->next
;
1494 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1495 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1496 die_errno(_("could not read MERGE_MODE"));
1497 if (!strcmp(sb
.buf
, "no-ff"))
1498 allow_fast_forward
= 0;
1500 if (allow_fast_forward
)
1501 parents
= reduce_heads(parents
);
1504 reflog_msg
= (whence
== FROM_CHERRY_PICK
)
1505 ? "commit (cherry-pick)"
1507 pptr
= &commit_list_insert(current_head
, pptr
)->next
;
1510 /* Finally, get the commit message */
1512 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1513 int saved_errno
= errno
;
1514 rollback_index_files();
1515 die(_("could not read commit message: %s"), strerror(saved_errno
));
1518 /* Truncate the message just before the diff, if any. */
1520 p
= strstr(sb
.buf
, "\ndiff --git ");
1522 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1525 if (cleanup_mode
!= CLEANUP_NONE
)
1526 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1527 if (template_untouched(&sb
) && !allow_empty_message
) {
1528 rollback_index_files();
1529 fprintf(stderr
, _("Aborting commit; you did not edit the message.\n"));
1532 if (message_is_empty(&sb
) && !allow_empty_message
) {
1533 rollback_index_files();
1534 fprintf(stderr
, _("Aborting commit due to empty commit message.\n"));
1539 const char *exclude_gpgsig
[2] = { "gpgsig", NULL
};
1540 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1542 struct commit_extra_header
**tail
= &extra
;
1543 append_merge_tag_headers(parents
, &tail
);
1546 if (commit_tree_extended(&sb
, active_cache_tree
->sha1
, parents
, sha1
,
1547 author_ident
.buf
, sign_commit
, extra
)) {
1548 rollback_index_files();
1549 die(_("failed to write commit object"));
1551 strbuf_release(&author_ident
);
1552 free_commit_extra_headers(extra
);
1554 ref_lock
= lock_any_ref_for_update("HEAD",
1557 : current_head
->object
.sha1
,
1560 nl
= strchr(sb
.buf
, '\n');
1562 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1564 strbuf_addch(&sb
, '\n');
1565 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1566 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1569 rollback_index_files();
1570 die(_("cannot lock HEAD ref"));
1572 if (write_ref_sha1(ref_lock
, sha1
, sb
.buf
) < 0) {
1573 rollback_index_files();
1574 die(_("cannot update HEAD ref"));
1577 unlink(git_path("CHERRY_PICK_HEAD"));
1578 unlink(git_path("REVERT_HEAD"));
1579 unlink(git_path("MERGE_HEAD"));
1580 unlink(git_path("MERGE_MSG"));
1581 unlink(git_path("MERGE_MODE"));
1582 unlink(git_path("SQUASH_MSG"));
1584 if (commit_index_files())
1585 die (_("Repository has been updated, but unable to write\n"
1586 "new_index file. Check that disk is not full or quota is\n"
1587 "not exceeded, and then \"git reset HEAD\" to recover."));
1590 run_hook(get_index_file(), "post-commit", NULL
);
1591 if (amend
&& !no_post_rewrite
) {
1592 struct notes_rewrite_cfg
*cfg
;
1593 cfg
= init_copy_notes_for_rewrite("amend");
1595 /* we are amending, so current_head is not NULL */
1596 copy_note_for_rewrite(cfg
, current_head
->object
.sha1
, sha1
);
1597 finish_copy_notes_for_rewrite(cfg
, "Notes added by 'git commit --amend'");
1599 run_rewrite_hook(current_head
->object
.sha1
, sha1
);
1602 print_summary(prefix
, sha1
, !current_head
);