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 static const char * const builtin_commit_usage
[] = {
32 "git commit [options] [--] <filepattern>...",
36 static const char * const builtin_status_usage
[] = {
37 "git status [options] [--] <filepattern>...",
41 static const char implicit_ident_advice
[] =
42 N_("Your name and email address were configured automatically based\n"
43 "on your username and hostname. Please check that they are accurate.\n"
44 "You can suppress this message by setting them explicitly:\n"
46 " git config --global user.name \"Your Name\"\n"
47 " git config --global user.email you@example.com\n"
49 "After doing this, you may fix the identity used for this commit with:\n"
51 " git commit --amend --reset-author\n");
53 static const char empty_amend_advice
[] =
54 N_("You asked to amend the most recent commit, but doing so would make\n"
55 "it empty. You can repeat your command with --allow-empty, or you can\n"
56 "remove the commit entirely with \"git reset HEAD^\".\n");
58 static const char empty_cherry_pick_advice
[] =
59 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
60 "If you wish to commit it anyway, use:\n"
62 " git commit --allow-empty\n"
64 "Otherwise, please use 'git reset'\n");
66 static const char *use_message_buffer
;
67 static const char commit_editmsg
[] = "COMMIT_EDITMSG";
68 static struct lock_file index_lock
; /* real index */
69 static struct lock_file false_lock
; /* used only for partial commits */
76 static const char *logfile
, *force_author
;
77 static const char *template_file
;
79 * The _message variables are commit names from which to take
80 * the commit message and/or authorship.
82 static const char *author_message
, *author_message_buffer
;
83 static char *edit_message
, *use_message
;
84 static char *fixup_message
, *squash_message
;
85 static int all
, also
, interactive
, patch_interactive
, only
, amend
, signoff
;
86 static int edit_flag
= -1; /* unspecified */
87 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
88 static int no_post_rewrite
, allow_empty_message
;
89 static char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
;
90 static char *sign_commit
;
93 * The default commit message cleanup mode will remove the lines
94 * beginning with # (shell comments) and leading and trailing
95 * whitespaces (empty lines or containing only whitespaces)
96 * if editor is used, and only the whitespaces if the message
97 * is specified explicitly.
104 static char *cleanup_arg
;
106 static enum commit_whence whence
;
107 static int use_editor
= 1, include_status
= 1;
108 static int show_ignored_in_status
;
109 static const char *only_include_assumed
;
110 static struct strbuf message
= STRBUF_INIT
;
112 static int null_termination
;
116 STATUS_FORMAT_PORCELAIN
117 } status_format
= STATUS_FORMAT_LONG
;
118 static int status_show_branch
;
120 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
122 struct strbuf
*buf
= opt
->value
;
124 strbuf_setlen(buf
, 0);
126 strbuf_addstr(buf
, arg
);
127 strbuf_addstr(buf
, "\n\n");
132 static void determine_whence(struct wt_status
*s
)
134 if (file_exists(git_path("MERGE_HEAD")))
136 else if (file_exists(git_path("CHERRY_PICK_HEAD")))
137 whence
= FROM_CHERRY_PICK
;
139 whence
= FROM_COMMIT
;
144 static const char *whence_s(void)
154 case FROM_CHERRY_PICK
:
155 s
= _("cherry-pick");
162 static void rollback_index_files(void)
164 switch (commit_style
) {
166 break; /* nothing to do */
168 rollback_lock_file(&index_lock
);
171 rollback_lock_file(&index_lock
);
172 rollback_lock_file(&false_lock
);
177 static int commit_index_files(void)
181 switch (commit_style
) {
183 break; /* nothing to do */
185 err
= commit_lock_file(&index_lock
);
188 err
= commit_lock_file(&index_lock
);
189 rollback_lock_file(&false_lock
);
197 * Take a union of paths in the index and the named tree (typically, "HEAD"),
198 * and return the paths that match the given pattern in list.
200 static int list_paths(struct string_list
*list
, const char *with_tree
,
201 const char *prefix
, const char **pattern
)
206 for (i
= 0; pattern
[i
]; i
++)
211 char *max_prefix
= common_prefix(pattern
);
212 overlay_tree_on_cache(with_tree
, max_prefix
? max_prefix
: prefix
);
216 for (i
= 0; i
< active_nr
; i
++) {
217 struct cache_entry
*ce
= active_cache
[i
];
218 struct string_list_item
*item
;
220 if (ce
->ce_flags
& CE_UPDATE
)
222 if (!match_pathspec(pattern
, ce
->name
, ce_namelen(ce
), 0, m
))
224 item
= string_list_insert(list
, ce
->name
);
225 if (ce_skip_worktree(ce
))
226 item
->util
= item
; /* better a valid pointer than a fake one */
229 return report_path_error(m
, pattern
, prefix
);
232 static void add_remove_files(struct string_list
*list
)
235 for (i
= 0; i
< list
->nr
; i
++) {
237 struct string_list_item
*p
= &(list
->items
[i
]);
239 /* p->util is skip-worktree */
243 if (!lstat(p
->string
, &st
)) {
244 if (add_to_cache(p
->string
, &st
, 0))
245 die(_("updating files failed"));
247 remove_file_from_cache(p
->string
);
251 static void create_base_index(const struct commit
*current_head
)
254 struct unpack_trees_options opts
;
262 memset(&opts
, 0, sizeof(opts
));
266 opts
.src_index
= &the_index
;
267 opts
.dst_index
= &the_index
;
269 opts
.fn
= oneway_merge
;
270 tree
= parse_tree_indirect(current_head
->object
.sha1
);
272 die(_("failed to unpack HEAD tree object"));
274 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
275 if (unpack_trees(1, &t
, &opts
))
276 exit(128); /* We've already reported the error, finish dying */
279 static void refresh_cache_or_die(int refresh_flags
)
282 * refresh_flags contains REFRESH_QUIET, so the only errors
283 * are for unmerged entries.
285 if (refresh_cache(refresh_flags
| REFRESH_IN_PORCELAIN
))
286 die_resolve_conflict("commit");
289 static char *prepare_index(int argc
, const char **argv
, const char *prefix
,
290 const struct commit
*current_head
, int is_status
)
293 struct string_list partial
;
294 const char **pathspec
= NULL
;
295 char *old_index_env
= NULL
;
296 int refresh_flags
= REFRESH_QUIET
;
299 refresh_flags
|= REFRESH_UNMERGED
;
302 pathspec
= get_pathspec(prefix
, argv
);
304 if (read_cache_preload(pathspec
) < 0)
305 die(_("index file corrupt"));
308 fd
= hold_locked_index(&index_lock
, 1);
310 refresh_cache_or_die(refresh_flags
);
312 if (write_cache(fd
, active_cache
, active_nr
) ||
313 close_lock_file(&index_lock
))
314 die(_("unable to create temporary index"));
316 old_index_env
= getenv(INDEX_ENVIRONMENT
);
317 setenv(INDEX_ENVIRONMENT
, index_lock
.filename
, 1);
319 if (interactive_add(argc
, argv
, prefix
, patch_interactive
) != 0)
320 die(_("interactive add failed"));
322 if (old_index_env
&& *old_index_env
)
323 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
325 unsetenv(INDEX_ENVIRONMENT
);
328 read_cache_from(index_lock
.filename
);
330 commit_style
= COMMIT_NORMAL
;
331 return index_lock
.filename
;
335 * Non partial, non as-is commit.
337 * (1) get the real index;
338 * (2) update the_index as necessary;
339 * (3) write the_index out to the real index (still locked);
340 * (4) return the name of the locked index file.
342 * The caller should run hooks on the locked real index, and
343 * (A) if all goes well, commit the real index;
344 * (B) on failure, rollback the real index.
346 if (all
|| (also
&& pathspec
&& *pathspec
)) {
347 fd
= hold_locked_index(&index_lock
, 1);
348 add_files_to_cache(also
? prefix
: NULL
, pathspec
, 0);
349 refresh_cache_or_die(refresh_flags
);
350 update_main_cache_tree(WRITE_TREE_SILENT
);
351 if (write_cache(fd
, active_cache
, active_nr
) ||
352 close_lock_file(&index_lock
))
353 die(_("unable to write new_index file"));
354 commit_style
= COMMIT_NORMAL
;
355 return index_lock
.filename
;
361 * (1) return the name of the real index file.
363 * The caller should run hooks on the real index,
364 * and create commit from the_index.
365 * We still need to refresh the index here.
367 if (!pathspec
|| !*pathspec
) {
368 fd
= hold_locked_index(&index_lock
, 1);
369 refresh_cache_or_die(refresh_flags
);
370 if (active_cache_changed
) {
371 update_main_cache_tree(WRITE_TREE_SILENT
);
372 if (write_cache(fd
, active_cache
, active_nr
) ||
373 commit_locked_index(&index_lock
))
374 die(_("unable to write new_index file"));
376 rollback_lock_file(&index_lock
);
378 commit_style
= COMMIT_AS_IS
;
379 return get_index_file();
385 * (0) find the set of affected paths;
386 * (1) get lock on the real index file;
387 * (2) update the_index with the given paths;
388 * (3) write the_index out to the real index (still locked);
389 * (4) get lock on the false index file;
390 * (5) reset the_index from HEAD;
391 * (6) update the_index the same way as (2);
392 * (7) write the_index out to the false index file;
393 * (8) return the name of the false index file (still locked);
395 * The caller should run hooks on the locked false index, and
396 * create commit from it. Then
397 * (A) if all goes well, commit the real index;
398 * (B) on failure, rollback the real index;
399 * In either case, rollback the false index.
401 commit_style
= COMMIT_PARTIAL
;
403 if (whence
!= FROM_COMMIT
)
404 die(_("cannot do a partial commit during a %s."), whence_s());
406 memset(&partial
, 0, sizeof(partial
));
407 partial
.strdup_strings
= 1;
408 if (list_paths(&partial
, !current_head
? NULL
: "HEAD", prefix
, pathspec
))
412 if (read_cache() < 0)
413 die(_("cannot read the index"));
415 fd
= hold_locked_index(&index_lock
, 1);
416 add_remove_files(&partial
);
417 refresh_cache(REFRESH_QUIET
);
418 if (write_cache(fd
, active_cache
, active_nr
) ||
419 close_lock_file(&index_lock
))
420 die(_("unable to write new_index file"));
422 fd
= hold_lock_file_for_update(&false_lock
,
423 git_path("next-index-%"PRIuMAX
,
424 (uintmax_t) getpid()),
427 create_base_index(current_head
);
428 add_remove_files(&partial
);
429 refresh_cache(REFRESH_QUIET
);
431 if (write_cache(fd
, active_cache
, active_nr
) ||
432 close_lock_file(&false_lock
))
433 die(_("unable to write temporary index file"));
436 read_cache_from(false_lock
.filename
);
438 return false_lock
.filename
;
441 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
444 unsigned char sha1
[20];
446 if (s
->relative_paths
)
451 s
->reference
= "HEAD^1";
453 s
->verbose
= verbose
;
454 s
->index_file
= index_file
;
457 s
->is_initial
= get_sha1(s
->reference
, sha1
) ? 1 : 0;
459 wt_status_collect(s
);
461 switch (status_format
) {
462 case STATUS_FORMAT_SHORT
:
463 wt_shortstatus_print(s
, null_termination
, status_show_branch
);
465 case STATUS_FORMAT_PORCELAIN
:
466 wt_porcelain_print(s
, null_termination
);
468 case STATUS_FORMAT_LONG
:
473 return s
->commitable
;
476 static int is_a_merge(const struct commit
*current_head
)
478 return !!(current_head
->parents
&& current_head
->parents
->next
);
481 static const char sign_off_header
[] = "Signed-off-by: ";
483 static void export_one(const char *var
, const char *s
, const char *e
, int hack
)
485 struct strbuf buf
= STRBUF_INIT
;
487 strbuf_addch(&buf
, hack
);
488 strbuf_addf(&buf
, "%.*s", (int)(e
- s
), s
);
489 setenv(var
, buf
.buf
, 1);
490 strbuf_release(&buf
);
493 static void determine_author_info(struct strbuf
*author_ident
)
495 char *name
, *email
, *date
;
496 struct ident_split author
;
498 name
= getenv("GIT_AUTHOR_NAME");
499 email
= getenv("GIT_AUTHOR_EMAIL");
500 date
= getenv("GIT_AUTHOR_DATE");
502 if (author_message
) {
503 const char *a
, *lb
, *rb
, *eol
;
506 a
= strstr(author_message_buffer
, "\nauthor ");
508 die(_("invalid commit: %s"), author_message
);
510 lb
= strchrnul(a
+ strlen("\nauthor "), '<');
511 rb
= strchrnul(lb
, '>');
512 eol
= strchrnul(rb
, '\n');
513 if (!*lb
|| !*rb
|| !*eol
)
514 die(_("invalid commit: %s"), author_message
);
516 if (lb
== a
+ strlen("\nauthor "))
517 /* \nauthor <foo@example.com> */
518 name
= xcalloc(1, 1);
520 name
= xmemdupz(a
+ strlen("\nauthor "),
522 (a
+ strlen("\nauthor "))));
523 email
= xmemdupz(lb
+ strlen("<"), rb
- (lb
+ strlen("<")));
524 date
= xmemdupz(rb
+ strlen("> "), eol
- (rb
+ strlen("> ")));
525 len
= eol
- (rb
+ strlen("> "));
526 date
= xmalloc(len
+ 2);
528 memcpy(date
+ 1, rb
+ strlen("> "), len
);
529 date
[len
+ 1] = '\0';
533 const char *lb
= strstr(force_author
, " <");
534 const char *rb
= strchr(force_author
, '>');
537 die(_("malformed --author parameter"));
538 name
= xstrndup(force_author
, lb
- force_author
);
539 email
= xstrndup(lb
+ 2, rb
- (lb
+ 2));
544 strbuf_addstr(author_ident
, fmt_ident(name
, email
, date
,
545 IDENT_ERROR_ON_NO_NAME
));
546 if (!split_ident_line(&author
, author_ident
->buf
, author_ident
->len
)) {
547 export_one("GIT_AUTHOR_NAME", author
.name_begin
, author
.name_end
, 0);
548 export_one("GIT_AUTHOR_EMAIL", author
.mail_begin
, author
.mail_end
, 0);
549 export_one("GIT_AUTHOR_DATE", author
.date_begin
, author
.tz_end
, '@');
553 static int ends_rfc2822_footer(struct strbuf
*sb
)
560 const char *buf
= sb
->buf
;
562 for (i
= len
- 1; i
> 0; i
--) {
563 if (hit
&& buf
[i
] == '\n')
565 hit
= (buf
[i
] == '\n');
568 while (i
< len
- 1 && buf
[i
] == '\n')
571 for (; i
< len
; i
= k
) {
572 for (k
= i
; k
< len
&& buf
[k
] != '\n'; k
++)
576 if ((buf
[k
] == ' ' || buf
[k
] == '\t') && !first
)
581 for (j
= 0; i
+ j
< len
; j
++) {
594 static char *cut_ident_timestamp_part(char *string
)
596 char *ket
= strrchr(string
, '>');
597 if (!ket
|| ket
[1] != ' ')
598 die(_("Malformed ident string: '%s'"), string
);
603 static int prepare_to_commit(const char *index_file
, const char *prefix
,
604 struct commit
*current_head
,
606 struct strbuf
*author_ident
)
609 struct strbuf committer_ident
= STRBUF_INIT
;
610 int commitable
, saved_color_setting
;
611 struct strbuf sb
= STRBUF_INIT
;
613 const char *hook_arg1
= NULL
;
614 const char *hook_arg2
= NULL
;
616 int clean_message_contents
= (cleanup_mode
!= CLEANUP_NONE
);
618 /* This checks and barfs if author is badly specified */
619 determine_author_info(author_ident
);
621 if (!no_verify
&& run_hook(index_file
, "pre-commit", NULL
))
624 if (squash_message
) {
626 * Insert the proper subject line before other commit
627 * message options add their content.
629 if (use_message
&& !strcmp(use_message
, squash_message
))
630 strbuf_addstr(&sb
, "squash! ");
632 struct pretty_print_context ctx
= {0};
634 c
= lookup_commit_reference_by_name(squash_message
);
636 die(_("could not lookup commit %s"), squash_message
);
637 ctx
.output_encoding
= get_commit_output_encoding();
638 format_commit_message(c
, "squash! %s\n\n", &sb
,
644 strbuf_addbuf(&sb
, &message
);
645 hook_arg1
= "message";
646 } else if (logfile
&& !strcmp(logfile
, "-")) {
648 fprintf(stderr
, _("(reading log message from standard input)\n"));
649 if (strbuf_read(&sb
, 0, 0) < 0)
650 die_errno(_("could not read log from standard input"));
651 hook_arg1
= "message";
652 } else if (logfile
) {
653 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
654 die_errno(_("could not read log file '%s'"),
656 hook_arg1
= "message";
657 } else if (use_message
) {
658 buffer
= strstr(use_message_buffer
, "\n\n");
659 if (!buffer
|| buffer
[2] == '\0')
660 die(_("commit has empty message"));
661 strbuf_add(&sb
, buffer
+ 2, strlen(buffer
+ 2));
662 hook_arg1
= "commit";
663 hook_arg2
= use_message
;
664 } else if (fixup_message
) {
665 struct pretty_print_context ctx
= {0};
666 struct commit
*commit
;
667 commit
= lookup_commit_reference_by_name(fixup_message
);
669 die(_("could not lookup commit %s"), fixup_message
);
670 ctx
.output_encoding
= get_commit_output_encoding();
671 format_commit_message(commit
, "fixup! %s\n\n",
673 hook_arg1
= "message";
674 } else if (!stat(git_path("MERGE_MSG"), &statbuf
)) {
675 if (strbuf_read_file(&sb
, git_path("MERGE_MSG"), 0) < 0)
676 die_errno(_("could not read MERGE_MSG"));
678 } else if (!stat(git_path("SQUASH_MSG"), &statbuf
)) {
679 if (strbuf_read_file(&sb
, git_path("SQUASH_MSG"), 0) < 0)
680 die_errno(_("could not read SQUASH_MSG"));
681 hook_arg1
= "squash";
682 } else if (template_file
) {
683 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
684 die_errno(_("could not read '%s'"), template_file
);
685 hook_arg1
= "template";
686 clean_message_contents
= 0;
690 * The remaining cases don't modify the template message, but
691 * just set the argument(s) to the prepare-commit-msg hook.
693 else if (whence
== FROM_MERGE
)
695 else if (whence
== FROM_CHERRY_PICK
) {
696 hook_arg1
= "commit";
697 hook_arg2
= "CHERRY_PICK_HEAD";
700 if (squash_message
) {
702 * If squash_commit was used for the commit subject,
703 * then we're possibly hijacking other commit log options.
704 * Reset the hook args to tell the real story.
706 hook_arg1
= "message";
710 s
->fp
= fopen(git_path(commit_editmsg
), "w");
712 die_errno(_("could not open '%s'"), git_path(commit_editmsg
));
714 if (clean_message_contents
)
718 struct strbuf sob
= STRBUF_INIT
;
721 strbuf_addstr(&sob
, sign_off_header
);
722 strbuf_addstr(&sob
, fmt_name(getenv("GIT_COMMITTER_NAME"),
723 getenv("GIT_COMMITTER_EMAIL")));
724 strbuf_addch(&sob
, '\n');
725 for (i
= sb
.len
- 1; i
> 0 && sb
.buf
[i
- 1] != '\n'; i
--)
727 if (prefixcmp(sb
.buf
+ i
, sob
.buf
)) {
728 if (!i
|| !ends_rfc2822_footer(&sb
))
729 strbuf_addch(&sb
, '\n');
730 strbuf_addbuf(&sb
, &sob
);
732 strbuf_release(&sob
);
735 if (fwrite(sb
.buf
, 1, sb
.len
, s
->fp
) < sb
.len
)
736 die_errno(_("could not write commit template"));
740 /* This checks if committer ident is explicitly given */
741 strbuf_addstr(&committer_ident
, git_committer_info(0));
742 if (use_editor
&& include_status
) {
743 char *ai_tmp
, *ci_tmp
;
744 if (whence
!= FROM_COMMIT
)
745 status_printf_ln(s
, GIT_COLOR_NORMAL
,
747 "It looks like you may be committing a %s.\n"
748 "If this is not correct, please remove the file\n"
753 git_path(whence
== FROM_MERGE
755 : "CHERRY_PICK_HEAD"));
757 fprintf(s
->fp
, "\n");
758 status_printf(s
, GIT_COLOR_NORMAL
,
759 _("Please enter the commit message for your changes."));
760 if (cleanup_mode
== CLEANUP_ALL
)
761 status_printf_more(s
, GIT_COLOR_NORMAL
,
762 _(" Lines starting\n"
763 "with '#' will be ignored, and an empty"
764 " message aborts the commit.\n"));
765 else /* CLEANUP_SPACE, that is. */
766 status_printf_more(s
, GIT_COLOR_NORMAL
,
767 _(" Lines starting\n"
768 "with '#' will be kept; you may remove them"
769 " yourself if you want to.\n"
770 "An empty message aborts the commit.\n"));
771 if (only_include_assumed
)
772 status_printf_ln(s
, GIT_COLOR_NORMAL
,
773 "%s", only_include_assumed
);
775 ai_tmp
= cut_ident_timestamp_part(author_ident
->buf
);
776 ci_tmp
= cut_ident_timestamp_part(committer_ident
.buf
);
777 if (strcmp(author_ident
->buf
, committer_ident
.buf
))
778 status_printf_ln(s
, GIT_COLOR_NORMAL
,
781 ident_shown
++ ? "" : "\n",
784 if (!user_ident_sufficiently_given())
785 status_printf_ln(s
, GIT_COLOR_NORMAL
,
788 ident_shown
++ ? "" : "\n",
789 committer_ident
.buf
);
792 status_printf_ln(s
, GIT_COLOR_NORMAL
, "");
794 saved_color_setting
= s
->use_color
;
796 commitable
= run_status(s
->fp
, index_file
, prefix
, 1, s
);
797 s
->use_color
= saved_color_setting
;
802 unsigned char sha1
[20];
803 const char *parent
= "HEAD";
805 if (!active_nr
&& read_cache() < 0)
806 die(_("Cannot read index"));
811 if (get_sha1(parent
, sha1
))
812 commitable
= !!active_nr
;
814 commitable
= index_differs_from(parent
, 0);
816 strbuf_release(&committer_ident
);
821 * Reject an attempt to record a non-merge empty commit without
822 * explicit --allow-empty. In the cherry-pick case, it may be
823 * empty due to conflict resolution, which the user should okay.
825 if (!commitable
&& whence
!= FROM_MERGE
&& !allow_empty
&&
826 !(amend
&& is_a_merge(current_head
))) {
827 run_status(stdout
, index_file
, prefix
, 0, s
);
829 fputs(_(empty_amend_advice
), stderr
);
830 else if (whence
== FROM_CHERRY_PICK
)
831 fputs(_(empty_cherry_pick_advice
), stderr
);
836 * Re-read the index as pre-commit hook could have updated it,
837 * and write it out as a tree. We must do this before we invoke
838 * the editor and after we invoke run_status above.
841 read_cache_from(index_file
);
842 if (update_main_cache_tree(0)) {
843 error(_("Error building trees"));
847 if (run_hook(index_file
, "prepare-commit-msg",
848 git_path(commit_editmsg
), hook_arg1
, hook_arg2
, NULL
))
852 char index
[PATH_MAX
];
853 const char *env
[2] = { NULL
};
855 snprintf(index
, sizeof(index
), "GIT_INDEX_FILE=%s", index_file
);
856 if (launch_editor(git_path(commit_editmsg
), NULL
, env
)) {
858 _("Please supply the message using either -m or -F option.\n"));
864 run_hook(index_file
, "commit-msg", git_path(commit_editmsg
), NULL
)) {
871 static int rest_is_empty(struct strbuf
*sb
, int start
)
876 /* Check if the rest is just whitespace and Signed-of-by's. */
877 for (i
= start
; i
< sb
->len
; i
++) {
878 nl
= memchr(sb
->buf
+ i
, '\n', sb
->len
- i
);
884 if (strlen(sign_off_header
) <= eol
- i
&&
885 !prefixcmp(sb
->buf
+ i
, sign_off_header
)) {
890 if (!isspace(sb
->buf
[i
++]))
898 * Find out if the message in the strbuf contains only whitespace and
899 * Signed-off-by lines.
901 static int message_is_empty(struct strbuf
*sb
)
903 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
905 return rest_is_empty(sb
, 0);
909 * See if the user edited the message in the editor or left what
910 * was in the template intact
912 static int template_untouched(struct strbuf
*sb
)
914 struct strbuf tmpl
= STRBUF_INIT
;
917 if (cleanup_mode
== CLEANUP_NONE
&& sb
->len
)
920 if (!template_file
|| strbuf_read_file(&tmpl
, template_file
, 0) <= 0)
923 stripspace(&tmpl
, cleanup_mode
== CLEANUP_ALL
);
924 start
= (char *)skip_prefix(sb
->buf
, tmpl
.buf
);
927 strbuf_release(&tmpl
);
928 return rest_is_empty(sb
, start
- sb
->buf
);
931 static const char *find_author_by_nickname(const char *name
)
933 struct rev_info revs
;
934 struct commit
*commit
;
935 struct strbuf buf
= STRBUF_INIT
;
939 init_revisions(&revs
, NULL
);
940 strbuf_addf(&buf
, "--author=%s", name
);
945 setup_revisions(ac
, av
, &revs
, NULL
);
946 prepare_revision_walk(&revs
);
947 commit
= get_revision(&revs
);
949 struct pretty_print_context ctx
= {0};
950 ctx
.date_mode
= DATE_NORMAL
;
951 strbuf_release(&buf
);
952 format_commit_message(commit
, "%an <%ae>", &buf
, &ctx
);
953 return strbuf_detach(&buf
, NULL
);
955 die(_("No existing author found with '%s'"), name
);
959 static void handle_untracked_files_arg(struct wt_status
*s
)
961 if (!untracked_files_arg
)
962 ; /* default already initialized */
963 else if (!strcmp(untracked_files_arg
, "no"))
964 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
965 else if (!strcmp(untracked_files_arg
, "normal"))
966 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
967 else if (!strcmp(untracked_files_arg
, "all"))
968 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
970 die(_("Invalid untracked files mode '%s'"), untracked_files_arg
);
973 static const char *read_commit_message(const char *name
)
975 const char *out_enc
, *out
;
976 struct commit
*commit
;
978 commit
= lookup_commit_reference_by_name(name
);
980 die(_("could not lookup commit %s"), name
);
981 out_enc
= get_commit_output_encoding();
982 out
= logmsg_reencode(commit
, out_enc
);
985 * If we failed to reencode the buffer, just copy it
986 * byte for byte so the user can try to fix it up.
987 * This also handles the case where input and output
988 * encodings are identical.
991 out
= xstrdup(commit
->buffer
);
995 static int parse_and_validate_options(int argc
, const char *argv
[],
996 const struct option
*options
,
997 const char * const usage
[],
999 struct commit
*current_head
,
1000 struct wt_status
*s
)
1004 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1006 if (force_author
&& !strchr(force_author
, '>'))
1007 force_author
= find_author_by_nickname(force_author
);
1009 if (force_author
&& renew_authorship
)
1010 die(_("Using both --reset-author and --author does not make sense"));
1012 if (logfile
|| message
.len
|| use_message
|| fixup_message
)
1015 use_editor
= edit_flag
;
1017 setenv("GIT_EDITOR", ":", 1);
1019 /* Sanity check options */
1020 if (amend
&& !current_head
)
1021 die(_("You have nothing to amend."));
1022 if (amend
&& whence
!= FROM_COMMIT
)
1023 die(_("You are in the middle of a %s -- cannot amend."), whence_s());
1024 if (fixup_message
&& squash_message
)
1025 die(_("Options --squash and --fixup cannot be used together"));
1035 die(_("Only one of -c/-C/-F/--fixup can be used."));
1036 if (message
.len
&& f
> 0)
1037 die((_("Option -m cannot be combined with -c/-C/-F/--fixup.")));
1038 if (f
|| message
.len
)
1039 template_file
= NULL
;
1041 use_message
= edit_message
;
1042 if (amend
&& !use_message
&& !fixup_message
)
1043 use_message
= "HEAD";
1044 if (!use_message
&& whence
!= FROM_CHERRY_PICK
&& renew_authorship
)
1045 die(_("--reset-author can be used only with -C, -c or --amend."));
1047 use_message_buffer
= read_commit_message(use_message
);
1048 if (!renew_authorship
) {
1049 author_message
= use_message
;
1050 author_message_buffer
= use_message_buffer
;
1053 if (whence
== FROM_CHERRY_PICK
&& !renew_authorship
) {
1054 author_message
= "CHERRY_PICK_HEAD";
1055 author_message_buffer
= read_commit_message(author_message
);
1058 if (patch_interactive
)
1061 if (!!also
+ !!only
+ !!all
+ !!interactive
> 1)
1062 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1063 if (argc
== 0 && (also
|| (only
&& !amend
)))
1064 die(_("No paths with --include/--only does not make sense."));
1065 if (argc
== 0 && only
&& amend
)
1066 only_include_assumed
= _("Clever... amending the last one with dirty index.");
1067 if (argc
> 0 && !also
&& !only
)
1068 only_include_assumed
= _("Explicit paths specified without -i nor -o; assuming --only paths...");
1069 if (!cleanup_arg
|| !strcmp(cleanup_arg
, "default"))
1070 cleanup_mode
= use_editor
? CLEANUP_ALL
: CLEANUP_SPACE
;
1071 else if (!strcmp(cleanup_arg
, "verbatim"))
1072 cleanup_mode
= CLEANUP_NONE
;
1073 else if (!strcmp(cleanup_arg
, "whitespace"))
1074 cleanup_mode
= CLEANUP_SPACE
;
1075 else if (!strcmp(cleanup_arg
, "strip"))
1076 cleanup_mode
= CLEANUP_ALL
;
1078 die(_("Invalid cleanup mode %s"), cleanup_arg
);
1080 handle_untracked_files_arg(s
);
1082 if (all
&& argc
> 0)
1083 die(_("Paths with -a does not make sense."));
1085 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
1086 status_format
= STATUS_FORMAT_PORCELAIN
;
1087 if (status_format
!= STATUS_FORMAT_LONG
)
1093 static int dry_run_commit(int argc
, const char **argv
, const char *prefix
,
1094 const struct commit
*current_head
, struct wt_status
*s
)
1097 const char *index_file
;
1099 index_file
= prepare_index(argc
, argv
, prefix
, current_head
, 1);
1100 commitable
= run_status(stdout
, index_file
, prefix
, 0, s
);
1101 rollback_index_files();
1103 return commitable
? 0 : 1;
1106 static int parse_status_slot(const char *var
, int offset
)
1108 if (!strcasecmp(var
+offset
, "header"))
1109 return WT_STATUS_HEADER
;
1110 if (!strcasecmp(var
+offset
, "branch"))
1111 return WT_STATUS_ONBRANCH
;
1112 if (!strcasecmp(var
+offset
, "updated")
1113 || !strcasecmp(var
+offset
, "added"))
1114 return WT_STATUS_UPDATED
;
1115 if (!strcasecmp(var
+offset
, "changed"))
1116 return WT_STATUS_CHANGED
;
1117 if (!strcasecmp(var
+offset
, "untracked"))
1118 return WT_STATUS_UNTRACKED
;
1119 if (!strcasecmp(var
+offset
, "nobranch"))
1120 return WT_STATUS_NOBRANCH
;
1121 if (!strcasecmp(var
+offset
, "unmerged"))
1122 return WT_STATUS_UNMERGED
;
1126 static int git_status_config(const char *k
, const char *v
, void *cb
)
1128 struct wt_status
*s
= cb
;
1130 if (!strcmp(k
, "status.submodulesummary")) {
1132 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
1133 if (is_bool
&& s
->submodule_summary
)
1134 s
->submodule_summary
= -1;
1137 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1138 s
->use_color
= git_config_colorbool(k
, v
);
1141 if (!prefixcmp(k
, "status.color.") || !prefixcmp(k
, "color.status.")) {
1142 int slot
= parse_status_slot(k
, 13);
1146 return config_error_nonbool(k
);
1147 color_parse(v
, k
, s
->color_palette
[slot
]);
1150 if (!strcmp(k
, "status.relativepaths")) {
1151 s
->relative_paths
= git_config_bool(k
, v
);
1154 if (!strcmp(k
, "status.showuntrackedfiles")) {
1156 return config_error_nonbool(k
);
1157 else if (!strcmp(v
, "no"))
1158 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1159 else if (!strcmp(v
, "normal"))
1160 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1161 else if (!strcmp(v
, "all"))
1162 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1164 return error(_("Invalid untracked files mode '%s'"), v
);
1167 return git_diff_ui_config(k
, v
, NULL
);
1170 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1172 static struct wt_status s
;
1174 unsigned char sha1
[20];
1175 static struct option builtin_status_options
[] = {
1176 OPT__VERBOSE(&verbose
, "be verbose"),
1177 OPT_SET_INT('s', "short", &status_format
,
1178 "show status concisely", STATUS_FORMAT_SHORT
),
1179 OPT_BOOLEAN('b', "branch", &status_show_branch
,
1180 "show branch information"),
1181 OPT_SET_INT(0, "porcelain", &status_format
,
1182 "machine-readable output",
1183 STATUS_FORMAT_PORCELAIN
),
1184 OPT_BOOLEAN('z', "null", &null_termination
,
1185 "terminate entries with NUL"),
1186 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1188 "show untracked files, optional modes: all, normal, no. (Default: all)",
1189 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1190 OPT_BOOLEAN(0, "ignored", &show_ignored_in_status
,
1191 "show ignored files"),
1192 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, "when",
1193 "ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)",
1194 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1198 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1199 usage_with_options(builtin_status_usage
, builtin_status_options
);
1201 wt_status_prepare(&s
);
1202 gitmodules_config();
1203 git_config(git_status_config
, &s
);
1204 determine_whence(&s
);
1205 argc
= parse_options(argc
, argv
, prefix
,
1206 builtin_status_options
,
1207 builtin_status_usage
, 0);
1209 if (null_termination
&& status_format
== STATUS_FORMAT_LONG
)
1210 status_format
= STATUS_FORMAT_PORCELAIN
;
1212 handle_untracked_files_arg(&s
);
1213 if (show_ignored_in_status
)
1214 s
.show_ignored_files
= 1;
1216 s
.pathspec
= get_pathspec(prefix
, argv
);
1218 read_cache_preload(s
.pathspec
);
1219 refresh_index(&the_index
, REFRESH_QUIET
|REFRESH_UNMERGED
, s
.pathspec
, NULL
, NULL
);
1221 fd
= hold_locked_index(&index_lock
, 0);
1223 update_index_if_able(&the_index
, &index_lock
);
1225 s
.is_initial
= get_sha1(s
.reference
, sha1
) ? 1 : 0;
1226 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1227 wt_status_collect(&s
);
1229 if (s
.relative_paths
)
1232 switch (status_format
) {
1233 case STATUS_FORMAT_SHORT
:
1234 wt_shortstatus_print(&s
, null_termination
, status_show_branch
);
1236 case STATUS_FORMAT_PORCELAIN
:
1237 wt_porcelain_print(&s
, null_termination
);
1239 case STATUS_FORMAT_LONG
:
1240 s
.verbose
= verbose
;
1241 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1242 wt_status_print(&s
);
1248 static void print_summary(const char *prefix
, const unsigned char *sha1
,
1251 struct rev_info rev
;
1252 struct commit
*commit
;
1253 struct strbuf format
= STRBUF_INIT
;
1254 unsigned char junk_sha1
[20];
1256 struct pretty_print_context pctx
= {0};
1257 struct strbuf author_ident
= STRBUF_INIT
;
1258 struct strbuf committer_ident
= STRBUF_INIT
;
1260 commit
= lookup_commit(sha1
);
1262 die(_("couldn't look up newly created commit"));
1263 if (!commit
|| parse_commit(commit
))
1264 die(_("could not parse newly created commit"));
1266 strbuf_addstr(&format
, "format:%h] %s");
1268 format_commit_message(commit
, "%an <%ae>", &author_ident
, &pctx
);
1269 format_commit_message(commit
, "%cn <%ce>", &committer_ident
, &pctx
);
1270 if (strbuf_cmp(&author_ident
, &committer_ident
)) {
1271 strbuf_addstr(&format
, "\n Author: ");
1272 strbuf_addbuf_percentquote(&format
, &author_ident
);
1274 if (!user_ident_sufficiently_given()) {
1275 strbuf_addstr(&format
, "\n Committer: ");
1276 strbuf_addbuf_percentquote(&format
, &committer_ident
);
1277 if (advice_implicit_identity
) {
1278 strbuf_addch(&format
, '\n');
1279 strbuf_addstr(&format
, _(implicit_ident_advice
));
1282 strbuf_release(&author_ident
);
1283 strbuf_release(&committer_ident
);
1285 init_revisions(&rev
, prefix
);
1286 setup_revisions(0, NULL
, &rev
, NULL
);
1289 rev
.diffopt
.output_format
=
1290 DIFF_FORMAT_SHORTSTAT
| DIFF_FORMAT_SUMMARY
;
1292 rev
.verbose_header
= 1;
1293 rev
.show_root_diff
= 1;
1294 get_commit_format(format
.buf
, &rev
);
1295 rev
.always_show_header
= 0;
1296 rev
.diffopt
.detect_rename
= 1;
1297 rev
.diffopt
.break_opt
= 0;
1298 diff_setup_done(&rev
.diffopt
);
1300 head
= resolve_ref_unsafe("HEAD", junk_sha1
, 0, NULL
);
1302 !prefixcmp(head
, "refs/heads/") ?
1304 !strcmp(head
, "HEAD") ?
1305 _("detached HEAD") :
1307 initial_commit
? _(" (root-commit)") : "");
1309 if (!log_tree_commit(&rev
, commit
)) {
1310 rev
.always_show_header
= 1;
1311 rev
.use_terminator
= 1;
1312 log_tree_commit(&rev
, commit
);
1315 strbuf_release(&format
);
1318 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1320 struct wt_status
*s
= cb
;
1323 if (!strcmp(k
, "commit.template"))
1324 return git_config_pathname(&template_file
, k
, v
);
1325 if (!strcmp(k
, "commit.status")) {
1326 include_status
= git_config_bool(k
, v
);
1330 status
= git_gpg_config(k
, v
, NULL
);
1333 return git_status_config(k
, v
, s
);
1336 static const char post_rewrite_hook
[] = "hooks/post-rewrite";
1338 static int run_rewrite_hook(const unsigned char *oldsha1
,
1339 const unsigned char *newsha1
)
1341 /* oldsha1 SP newsha1 LF NUL */
1342 static char buf
[2*40 + 3];
1343 struct child_process proc
;
1344 const char *argv
[3];
1348 if (access(git_path(post_rewrite_hook
), X_OK
) < 0)
1351 argv
[0] = git_path(post_rewrite_hook
);
1355 memset(&proc
, 0, sizeof(proc
));
1358 proc
.stdout_to_stderr
= 1;
1360 code
= start_command(&proc
);
1363 n
= snprintf(buf
, sizeof(buf
), "%s %s\n",
1364 sha1_to_hex(oldsha1
), sha1_to_hex(newsha1
));
1365 write_in_full(proc
.in
, buf
, n
);
1367 return finish_command(&proc
);
1370 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1372 static struct wt_status s
;
1373 static struct option builtin_commit_options
[] = {
1374 OPT__QUIET(&quiet
, "suppress summary after successful commit"),
1375 OPT__VERBOSE(&verbose
, "show diff in commit message template"),
1377 OPT_GROUP("Commit message options"),
1378 OPT_FILENAME('F', "file", &logfile
, "read message from file"),
1379 OPT_STRING(0, "author", &force_author
, "author", "override author for commit"),
1380 OPT_STRING(0, "date", &force_date
, "date", "override date for commit"),
1381 OPT_CALLBACK('m', "message", &message
, "message", "commit message", opt_parse_m
),
1382 OPT_STRING('c', "reedit-message", &edit_message
, "commit", "reuse and edit message from specified commit"),
1383 OPT_STRING('C', "reuse-message", &use_message
, "commit", "reuse message from specified commit"),
1384 OPT_STRING(0, "fixup", &fixup_message
, "commit", "use autosquash formatted message to fixup specified commit"),
1385 OPT_STRING(0, "squash", &squash_message
, "commit", "use autosquash formatted message to squash specified commit"),
1386 OPT_BOOLEAN(0, "reset-author", &renew_authorship
, "the commit is authored by me now (used with -C/-c/--amend)"),
1387 OPT_BOOLEAN('s', "signoff", &signoff
, "add Signed-off-by:"),
1388 OPT_FILENAME('t', "template", &template_file
, "use specified template file"),
1389 OPT_BOOL('e', "edit", &edit_flag
, "force edit of commit"),
1390 OPT_STRING(0, "cleanup", &cleanup_arg
, "default", "how to strip spaces and #comments from message"),
1391 OPT_BOOLEAN(0, "status", &include_status
, "include status in commit message template"),
1392 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, "key id",
1393 "GPG sign commit", PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1394 /* end commit message options */
1396 OPT_GROUP("Commit contents options"),
1397 OPT_BOOLEAN('a', "all", &all
, "commit all changed files"),
1398 OPT_BOOLEAN('i', "include", &also
, "add specified files to index for commit"),
1399 OPT_BOOLEAN(0, "interactive", &interactive
, "interactively add files"),
1400 OPT_BOOLEAN('p', "patch", &patch_interactive
, "interactively add changes"),
1401 OPT_BOOLEAN('o', "only", &only
, "commit only specified files"),
1402 OPT_BOOLEAN('n', "no-verify", &no_verify
, "bypass pre-commit hook"),
1403 OPT_BOOLEAN(0, "dry-run", &dry_run
, "show what would be committed"),
1404 OPT_SET_INT(0, "short", &status_format
, "show status concisely",
1405 STATUS_FORMAT_SHORT
),
1406 OPT_BOOLEAN(0, "branch", &status_show_branch
, "show branch information"),
1407 OPT_SET_INT(0, "porcelain", &status_format
,
1408 "machine-readable output", STATUS_FORMAT_PORCELAIN
),
1409 OPT_BOOLEAN('z', "null", &null_termination
,
1410 "terminate entries with NUL"),
1411 OPT_BOOLEAN(0, "amend", &amend
, "amend previous commit"),
1412 OPT_BOOLEAN(0, "no-post-rewrite", &no_post_rewrite
, "bypass post-rewrite hook"),
1413 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1414 /* end commit contents options */
1416 { OPTION_BOOLEAN
, 0, "allow-empty", &allow_empty
, NULL
,
1417 "ok to record an empty change",
1418 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
1419 { OPTION_BOOLEAN
, 0, "allow-empty-message", &allow_empty_message
, NULL
,
1420 "ok to record a change with an empty message",
1421 PARSE_OPT_NOARG
| PARSE_OPT_HIDDEN
},
1426 struct strbuf sb
= STRBUF_INIT
;
1427 struct strbuf author_ident
= STRBUF_INIT
;
1428 const char *index_file
, *reflog_msg
;
1430 unsigned char sha1
[20];
1431 struct ref_lock
*ref_lock
;
1432 struct commit_list
*parents
= NULL
, **pptr
= &parents
;
1433 struct stat statbuf
;
1434 int allow_fast_forward
= 1;
1435 struct commit
*current_head
= NULL
;
1436 struct commit_extra_header
*extra
= NULL
;
1438 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1439 usage_with_options(builtin_commit_usage
, builtin_commit_options
);
1441 wt_status_prepare(&s
);
1442 git_config(git_commit_config
, &s
);
1443 determine_whence(&s
);
1445 if (get_sha1("HEAD", sha1
))
1446 current_head
= NULL
;
1448 current_head
= lookup_commit_or_die(sha1
, "HEAD");
1449 if (!current_head
|| parse_commit(current_head
))
1450 die(_("could not parse HEAD commit"));
1452 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_options
,
1453 builtin_commit_usage
,
1454 prefix
, current_head
, &s
);
1456 return dry_run_commit(argc
, argv
, prefix
, current_head
, &s
);
1457 index_file
= prepare_index(argc
, argv
, prefix
, current_head
, 0);
1459 /* Set up everything for writing the commit object. This includes
1460 running hooks, writing the trees, and interacting with the user. */
1461 if (!prepare_to_commit(index_file
, prefix
,
1462 current_head
, &s
, &author_ident
)) {
1463 rollback_index_files();
1467 /* Determine parents */
1468 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1469 if (!current_head
) {
1471 reflog_msg
= "commit (initial)";
1473 struct commit_list
*c
;
1476 reflog_msg
= "commit (amend)";
1477 for (c
= current_head
->parents
; c
; c
= c
->next
)
1478 pptr
= &commit_list_insert(c
->item
, pptr
)->next
;
1479 } else if (whence
== FROM_MERGE
) {
1480 struct strbuf m
= STRBUF_INIT
;
1484 reflog_msg
= "commit (merge)";
1485 pptr
= &commit_list_insert(current_head
, pptr
)->next
;
1486 fp
= fopen(git_path("MERGE_HEAD"), "r");
1488 die_errno(_("could not open '%s' for reading"),
1489 git_path("MERGE_HEAD"));
1490 while (strbuf_getline(&m
, fp
, '\n') != EOF
) {
1491 struct commit
*parent
;
1493 parent
= get_merge_parent(m
.buf
);
1495 die(_("Corrupt MERGE_HEAD file (%s)"), m
.buf
);
1496 pptr
= &commit_list_insert(parent
, pptr
)->next
;
1500 if (!stat(git_path("MERGE_MODE"), &statbuf
)) {
1501 if (strbuf_read_file(&sb
, git_path("MERGE_MODE"), 0) < 0)
1502 die_errno(_("could not read MERGE_MODE"));
1503 if (!strcmp(sb
.buf
, "no-ff"))
1504 allow_fast_forward
= 0;
1506 if (allow_fast_forward
)
1507 parents
= reduce_heads(parents
);
1510 reflog_msg
= (whence
== FROM_CHERRY_PICK
)
1511 ? "commit (cherry-pick)"
1513 pptr
= &commit_list_insert(current_head
, pptr
)->next
;
1516 /* Finally, get the commit message */
1518 if (strbuf_read_file(&sb
, git_path(commit_editmsg
), 0) < 0) {
1519 int saved_errno
= errno
;
1520 rollback_index_files();
1521 die(_("could not read commit message: %s"), strerror(saved_errno
));
1524 /* Truncate the message just before the diff, if any. */
1526 p
= strstr(sb
.buf
, "\ndiff --git ");
1528 strbuf_setlen(&sb
, p
- sb
.buf
+ 1);
1531 if (cleanup_mode
!= CLEANUP_NONE
)
1532 stripspace(&sb
, cleanup_mode
== CLEANUP_ALL
);
1533 if (template_untouched(&sb
) && !allow_empty_message
) {
1534 rollback_index_files();
1535 fprintf(stderr
, _("Aborting commit; you did not edit the message.\n"));
1538 if (message_is_empty(&sb
) && !allow_empty_message
) {
1539 rollback_index_files();
1540 fprintf(stderr
, _("Aborting commit due to empty commit message.\n"));
1545 const char *exclude_gpgsig
[2] = { "gpgsig", NULL
};
1546 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1548 struct commit_extra_header
**tail
= &extra
;
1549 append_merge_tag_headers(parents
, &tail
);
1552 if (commit_tree_extended(&sb
, active_cache_tree
->sha1
, parents
, sha1
,
1553 author_ident
.buf
, sign_commit
, extra
)) {
1554 rollback_index_files();
1555 die(_("failed to write commit object"));
1557 strbuf_release(&author_ident
);
1558 free_commit_extra_headers(extra
);
1560 ref_lock
= lock_any_ref_for_update("HEAD",
1563 : current_head
->object
.sha1
,
1566 nl
= strchr(sb
.buf
, '\n');
1568 strbuf_setlen(&sb
, nl
+ 1 - sb
.buf
);
1570 strbuf_addch(&sb
, '\n');
1571 strbuf_insert(&sb
, 0, reflog_msg
, strlen(reflog_msg
));
1572 strbuf_insert(&sb
, strlen(reflog_msg
), ": ", 2);
1575 rollback_index_files();
1576 die(_("cannot lock HEAD ref"));
1578 if (write_ref_sha1(ref_lock
, sha1
, sb
.buf
) < 0) {
1579 rollback_index_files();
1580 die(_("cannot update HEAD ref"));
1583 unlink(git_path("CHERRY_PICK_HEAD"));
1584 unlink(git_path("REVERT_HEAD"));
1585 unlink(git_path("MERGE_HEAD"));
1586 unlink(git_path("MERGE_MSG"));
1587 unlink(git_path("MERGE_MODE"));
1588 unlink(git_path("SQUASH_MSG"));
1590 if (commit_index_files())
1591 die (_("Repository has been updated, but unable to write\n"
1592 "new_index file. Check that disk is not full or quota is\n"
1593 "not exceeded, and then \"git reset HEAD\" to recover."));
1596 run_hook(get_index_file(), "post-commit", NULL
);
1597 if (amend
&& !no_post_rewrite
) {
1598 struct notes_rewrite_cfg
*cfg
;
1599 cfg
= init_copy_notes_for_rewrite("amend");
1601 /* we are amending, so current_head is not NULL */
1602 copy_note_for_rewrite(cfg
, current_head
->object
.sha1
, sha1
);
1603 finish_copy_notes_for_rewrite(cfg
);
1605 run_rewrite_hook(current_head
->object
.sha1
, sha1
);
1608 print_summary(prefix
, sha1
, !current_head
);