4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
8 #define USE_THE_INDEX_COMPATIBILITY_MACROS
12 #include "cache-tree.h"
20 #include "wt-status.h"
21 #include "run-command.h"
26 #include "parse-options.h"
27 #include "string-list.h"
29 #include "unpack-trees.h"
31 #include "submodule.h"
32 #include "gpg-interface.h"
34 #include "sequencer.h"
37 #include "commit-reach.h"
38 #include "commit-graph.h"
40 static const char * const builtin_commit_usage
[] = {
41 N_("git commit [<options>] [--] <pathspec>..."),
45 static const char * const builtin_status_usage
[] = {
46 N_("git status [<options>] [--] <pathspec>..."),
50 static const char empty_amend_advice
[] =
51 N_("You asked to amend the most recent commit, but doing so would make\n"
52 "it empty. You can repeat your command with --allow-empty, or you can\n"
53 "remove the commit entirely with \"git reset HEAD^\".\n");
55 static const char empty_cherry_pick_advice
[] =
56 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
57 "If you wish to commit it anyway, use:\n"
59 " git commit --allow-empty\n"
62 static const char empty_rebase_pick_advice
[] =
63 N_("Otherwise, please use 'git rebase --skip'\n");
65 static const char empty_cherry_pick_advice_single
[] =
66 N_("Otherwise, please use 'git cherry-pick --skip'\n");
68 static const char empty_cherry_pick_advice_multi
[] =
71 " git cherry-pick --continue\n"
73 "to resume cherry-picking the remaining commits.\n"
74 "If you wish to skip this commit, use:\n"
76 " git cherry-pick --skip\n"
79 static const char *color_status_slots
[] = {
80 [WT_STATUS_HEADER
] = "header",
81 [WT_STATUS_UPDATED
] = "updated",
82 [WT_STATUS_CHANGED
] = "changed",
83 [WT_STATUS_UNTRACKED
] = "untracked",
84 [WT_STATUS_NOBRANCH
] = "noBranch",
85 [WT_STATUS_UNMERGED
] = "unmerged",
86 [WT_STATUS_LOCAL_BRANCH
] = "localBranch",
87 [WT_STATUS_REMOTE_BRANCH
] = "remoteBranch",
88 [WT_STATUS_ONBRANCH
] = "branch",
91 static const char *use_message_buffer
;
92 static struct lock_file index_lock
; /* real index */
93 static struct lock_file false_lock
; /* used only for partial commits */
100 static const char *logfile
, *force_author
;
101 static const char *template_file
;
103 * The _message variables are commit names from which to take
104 * the commit message and/or authorship.
106 static const char *author_message
, *author_message_buffer
;
107 static char *edit_message
, *use_message
;
108 static char *fixup_message
, *fixup_commit
, *squash_message
;
109 static const char *fixup_prefix
;
110 static int all
, also
, interactive
, patch_interactive
, only
, amend
, signoff
;
111 static int edit_flag
= -1; /* unspecified */
112 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
113 static int config_commit_verbose
= -1; /* unspecified */
114 static int no_post_rewrite
, allow_empty_message
, pathspec_file_nul
;
115 static char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
, *ignored_arg
;
116 static char *sign_commit
, *pathspec_from_file
;
117 static struct strvec trailer_args
= STRVEC_INIT
;
120 * The default commit message cleanup mode will remove the lines
121 * beginning with # (shell comments) and leading and trailing
122 * whitespaces (empty lines or containing only whitespaces)
123 * if editor is used, and only the whitespaces if the message
124 * is specified explicitly.
126 static enum commit_msg_cleanup_mode cleanup_mode
;
127 static const char *cleanup_arg
;
129 static enum commit_whence whence
;
130 static int use_editor
= 1, include_status
= 1;
131 static int have_option_m
;
132 static struct strbuf message
= STRBUF_INIT
;
134 static enum wt_status_format status_format
= STATUS_FORMAT_UNSPECIFIED
;
136 static int opt_pass_trailer(const struct option
*opt
, const char *arg
, int unset
)
138 BUG_ON_OPT_NEG(unset
);
140 strvec_pushl(&trailer_args
, "--trailer", arg
, NULL
);
144 static int opt_parse_porcelain(const struct option
*opt
, const char *arg
, int unset
)
146 enum wt_status_format
*value
= (enum wt_status_format
*)opt
->value
;
148 *value
= STATUS_FORMAT_NONE
;
150 *value
= STATUS_FORMAT_PORCELAIN
;
151 else if (!strcmp(arg
, "v1") || !strcmp(arg
, "1"))
152 *value
= STATUS_FORMAT_PORCELAIN
;
153 else if (!strcmp(arg
, "v2") || !strcmp(arg
, "2"))
154 *value
= STATUS_FORMAT_PORCELAIN_V2
;
156 die("unsupported porcelain version '%s'", arg
);
161 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
163 struct strbuf
*buf
= opt
->value
;
166 strbuf_setlen(buf
, 0);
170 strbuf_addch(buf
, '\n');
171 strbuf_addstr(buf
, arg
);
172 strbuf_complete_line(buf
);
177 static int opt_parse_rename_score(const struct option
*opt
, const char *arg
, int unset
)
179 const char **value
= opt
->value
;
181 BUG_ON_OPT_NEG(unset
);
183 if (arg
!= NULL
&& *arg
== '=')
190 static void determine_whence(struct wt_status
*s
)
192 if (file_exists(git_path_merge_head(the_repository
)))
194 else if (!sequencer_determine_whence(the_repository
, &whence
))
195 whence
= FROM_COMMIT
;
200 static void status_init_config(struct wt_status
*s
, config_fn_t fn
)
202 wt_status_prepare(the_repository
, s
);
203 init_diff_ui_defaults();
206 s
->hints
= advice_status_hints
; /* must come after git_config() */
209 static void rollback_index_files(void)
211 switch (commit_style
) {
213 break; /* nothing to do */
215 rollback_lock_file(&index_lock
);
218 rollback_lock_file(&index_lock
);
219 rollback_lock_file(&false_lock
);
224 static int commit_index_files(void)
228 switch (commit_style
) {
230 break; /* nothing to do */
232 err
= commit_lock_file(&index_lock
);
235 err
= commit_lock_file(&index_lock
);
236 rollback_lock_file(&false_lock
);
244 * Take a union of paths in the index and the named tree (typically, "HEAD"),
245 * and return the paths that match the given pattern in list.
247 static int list_paths(struct string_list
*list
, const char *with_tree
,
248 const struct pathspec
*pattern
)
256 m
= xcalloc(1, pattern
->nr
);
259 char *max_prefix
= common_prefix(pattern
);
260 overlay_tree_on_index(&the_index
, with_tree
, max_prefix
);
264 /* TODO: audit for interaction with sparse-index. */
265 ensure_full_index(&the_index
);
266 for (i
= 0; i
< active_nr
; i
++) {
267 const struct cache_entry
*ce
= active_cache
[i
];
268 struct string_list_item
*item
;
270 if (ce
->ce_flags
& CE_UPDATE
)
272 if (!ce_path_match(&the_index
, ce
, pattern
, m
))
274 item
= string_list_insert(list
, ce
->name
);
275 if (ce_skip_worktree(ce
))
276 item
->util
= item
; /* better a valid pointer than a fake one */
279 ret
= report_path_error(m
, pattern
);
284 static void add_remove_files(struct string_list
*list
)
287 for (i
= 0; i
< list
->nr
; i
++) {
289 struct string_list_item
*p
= &(list
->items
[i
]);
291 /* p->util is skip-worktree */
295 if (!lstat(p
->string
, &st
)) {
296 if (add_to_cache(p
->string
, &st
, 0))
297 die(_("updating files failed"));
299 remove_file_from_cache(p
->string
);
303 static void create_base_index(const struct commit
*current_head
)
306 struct unpack_trees_options opts
;
314 memset(&opts
, 0, sizeof(opts
));
318 opts
.src_index
= &the_index
;
319 opts
.dst_index
= &the_index
;
321 opts
.fn
= oneway_merge
;
322 tree
= parse_tree_indirect(¤t_head
->object
.oid
);
324 die(_("failed to unpack HEAD tree object"));
326 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
327 if (unpack_trees(1, &t
, &opts
))
328 exit(128); /* We've already reported the error, finish dying */
331 static void refresh_cache_or_die(int refresh_flags
)
334 * refresh_flags contains REFRESH_QUIET, so the only errors
335 * are for unmerged entries.
337 if (refresh_cache(refresh_flags
| REFRESH_IN_PORCELAIN
))
338 die_resolve_conflict("commit");
341 static const char *prepare_index(const char **argv
, const char *prefix
,
342 const struct commit
*current_head
, int is_status
)
344 struct string_list partial
= STRING_LIST_INIT_DUP
;
345 struct pathspec pathspec
;
346 int refresh_flags
= REFRESH_QUIET
;
350 refresh_flags
|= REFRESH_UNMERGED
;
351 parse_pathspec(&pathspec
, 0,
352 PATHSPEC_PREFER_FULL
,
355 if (pathspec_from_file
) {
357 die(_("--pathspec-from-file is incompatible with --interactive/--patch"));
360 die(_("--pathspec-from-file with -a does not make sense"));
363 die(_("--pathspec-from-file is incompatible with pathspec arguments"));
365 parse_pathspec_file(&pathspec
, 0,
366 PATHSPEC_PREFER_FULL
,
367 prefix
, pathspec_from_file
, pathspec_file_nul
);
368 } else if (pathspec_file_nul
) {
369 die(_("--pathspec-file-nul requires --pathspec-from-file"));
372 if (!pathspec
.nr
&& (also
|| (only
&& !allow_empty
&&
373 (!amend
|| (fixup_message
&& strcmp(fixup_prefix
, "amend"))))))
374 die(_("No paths with --include/--only does not make sense."));
376 if (read_cache_preload(&pathspec
) < 0)
377 die(_("index file corrupt"));
380 char *old_index_env
= NULL
, *old_repo_index_file
;
381 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
383 refresh_cache_or_die(refresh_flags
);
385 if (write_locked_index(&the_index
, &index_lock
, 0))
386 die(_("unable to create temporary index"));
388 old_repo_index_file
= the_repository
->index_file
;
389 the_repository
->index_file
=
390 (char *)get_lock_file_path(&index_lock
);
391 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
392 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
394 if (interactive_add(argv
, prefix
, patch_interactive
) != 0)
395 die(_("interactive add failed"));
397 the_repository
->index_file
= old_repo_index_file
;
398 if (old_index_env
&& *old_index_env
)
399 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
401 unsetenv(INDEX_ENVIRONMENT
);
402 FREE_AND_NULL(old_index_env
);
405 read_cache_from(get_lock_file_path(&index_lock
));
406 if (update_main_cache_tree(WRITE_TREE_SILENT
) == 0) {
407 if (reopen_lock_file(&index_lock
) < 0)
408 die(_("unable to write index file"));
409 if (write_locked_index(&the_index
, &index_lock
, 0))
410 die(_("unable to update temporary index"));
412 warning(_("Failed to update main cache tree"));
414 commit_style
= COMMIT_NORMAL
;
415 ret
= get_lock_file_path(&index_lock
);
420 * Non partial, non as-is commit.
422 * (1) get the real index;
423 * (2) update the_index as necessary;
424 * (3) write the_index out to the real index (still locked);
425 * (4) return the name of the locked index file.
427 * The caller should run hooks on the locked real index, and
428 * (A) if all goes well, commit the real index;
429 * (B) on failure, rollback the real index.
431 if (all
|| (also
&& pathspec
.nr
)) {
432 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
433 add_files_to_cache(also
? prefix
: NULL
, &pathspec
, 0);
434 refresh_cache_or_die(refresh_flags
);
435 update_main_cache_tree(WRITE_TREE_SILENT
);
436 if (write_locked_index(&the_index
, &index_lock
, 0))
437 die(_("unable to write new_index file"));
438 commit_style
= COMMIT_NORMAL
;
439 ret
= get_lock_file_path(&index_lock
);
446 * (1) return the name of the real index file.
448 * The caller should run hooks on the real index,
449 * and create commit from the_index.
450 * We still need to refresh the index here.
452 if (!only
&& !pathspec
.nr
) {
453 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
454 refresh_cache_or_die(refresh_flags
);
455 if (active_cache_changed
456 || !cache_tree_fully_valid(active_cache_tree
))
457 update_main_cache_tree(WRITE_TREE_SILENT
);
458 if (write_locked_index(&the_index
, &index_lock
,
459 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
460 die(_("unable to write new_index file"));
461 commit_style
= COMMIT_AS_IS
;
462 ret
= get_index_file();
469 * (0) find the set of affected paths;
470 * (1) get lock on the real index file;
471 * (2) update the_index with the given paths;
472 * (3) write the_index out to the real index (still locked);
473 * (4) get lock on the false index file;
474 * (5) reset the_index from HEAD;
475 * (6) update the_index the same way as (2);
476 * (7) write the_index out to the false index file;
477 * (8) return the name of the false index file (still locked);
479 * The caller should run hooks on the locked false index, and
480 * create commit from it. Then
481 * (A) if all goes well, commit the real index;
482 * (B) on failure, rollback the real index;
483 * In either case, rollback the false index.
485 commit_style
= COMMIT_PARTIAL
;
487 if (whence
!= FROM_COMMIT
) {
488 if (whence
== FROM_MERGE
)
489 die(_("cannot do a partial commit during a merge."));
490 else if (is_from_cherry_pick(whence
))
491 die(_("cannot do a partial commit during a cherry-pick."));
492 else if (is_from_rebase(whence
))
493 die(_("cannot do a partial commit during a rebase."));
496 if (list_paths(&partial
, !current_head
? NULL
: "HEAD", &pathspec
))
500 if (read_cache() < 0)
501 die(_("cannot read the index"));
503 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
504 add_remove_files(&partial
);
505 refresh_cache(REFRESH_QUIET
);
506 update_main_cache_tree(WRITE_TREE_SILENT
);
507 if (write_locked_index(&the_index
, &index_lock
, 0))
508 die(_("unable to write new_index file"));
510 hold_lock_file_for_update(&false_lock
,
511 git_path("next-index-%"PRIuMAX
,
512 (uintmax_t) getpid()),
515 create_base_index(current_head
);
516 add_remove_files(&partial
);
517 refresh_cache(REFRESH_QUIET
);
519 if (write_locked_index(&the_index
, &false_lock
, 0))
520 die(_("unable to write temporary index file"));
523 ret
= get_lock_file_path(&false_lock
);
524 read_cache_from(ret
);
526 string_list_clear(&partial
, 0);
527 clear_pathspec(&pathspec
);
531 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
534 struct object_id oid
;
536 if (s
->relative_paths
)
541 s
->reference
= "HEAD^1";
543 s
->verbose
= verbose
;
544 s
->index_file
= index_file
;
547 s
->is_initial
= get_oid(s
->reference
, &oid
) ? 1 : 0;
549 oidcpy(&s
->oid_commit
, &oid
);
550 s
->status_format
= status_format
;
551 s
->ignore_submodule_arg
= ignore_submodule_arg
;
553 wt_status_collect(s
);
555 wt_status_collect_free_buffers(s
);
557 return s
->committable
;
560 static int is_a_merge(const struct commit
*current_head
)
562 return !!(current_head
->parents
&& current_head
->parents
->next
);
565 static void assert_split_ident(struct ident_split
*id
, const struct strbuf
*buf
)
567 if (split_ident_line(id
, buf
->buf
, buf
->len
) || !id
->date_begin
)
568 BUG("unable to parse our own ident: %s", buf
->buf
);
571 static void export_one(const char *var
, const char *s
, const char *e
, int hack
)
573 struct strbuf buf
= STRBUF_INIT
;
575 strbuf_addch(&buf
, hack
);
576 strbuf_add(&buf
, s
, e
- s
);
577 setenv(var
, buf
.buf
, 1);
578 strbuf_release(&buf
);
581 static int parse_force_date(const char *in
, struct strbuf
*out
)
583 strbuf_addch(out
, '@');
585 if (parse_date(in
, out
) < 0) {
587 unsigned long t
= approxidate_careful(in
, &errors
);
590 strbuf_addf(out
, "%lu", t
);
596 static void set_ident_var(char **buf
, char *val
)
602 static void determine_author_info(struct strbuf
*author_ident
)
604 char *name
, *email
, *date
;
605 struct ident_split author
;
607 name
= xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
608 email
= xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
609 date
= xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
611 if (author_message
) {
612 struct ident_split ident
;
616 a
= find_commit_header(author_message_buffer
, "author", &len
);
618 die(_("commit '%s' lacks author header"), author_message
);
619 if (split_ident_line(&ident
, a
, len
) < 0)
620 die(_("commit '%s' has malformed author line"), author_message
);
622 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
623 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
625 if (ident
.date_begin
) {
626 struct strbuf date_buf
= STRBUF_INIT
;
627 strbuf_addch(&date_buf
, '@');
628 strbuf_add(&date_buf
, ident
.date_begin
, ident
.date_end
- ident
.date_begin
);
629 strbuf_addch(&date_buf
, ' ');
630 strbuf_add(&date_buf
, ident
.tz_begin
, ident
.tz_end
- ident
.tz_begin
);
631 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
636 struct ident_split ident
;
638 if (split_ident_line(&ident
, force_author
, strlen(force_author
)) < 0)
639 die(_("malformed --author parameter"));
640 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
641 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
645 struct strbuf date_buf
= STRBUF_INIT
;
646 if (parse_force_date(force_date
, &date_buf
))
647 die(_("invalid date format: %s"), force_date
);
648 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
651 strbuf_addstr(author_ident
, fmt_ident(name
, email
, WANT_AUTHOR_IDENT
, date
,
653 assert_split_ident(&author
, author_ident
);
654 export_one("GIT_AUTHOR_NAME", author
.name_begin
, author
.name_end
, 0);
655 export_one("GIT_AUTHOR_EMAIL", author
.mail_begin
, author
.mail_end
, 0);
656 export_one("GIT_AUTHOR_DATE", author
.date_begin
, author
.tz_end
, '@');
662 static int author_date_is_interesting(void)
664 return author_message
|| force_date
;
667 static void adjust_comment_line_char(const struct strbuf
*sb
)
669 char candidates
[] = "#;@!$%^&|:";
673 comment_line_char
= candidates
[0];
674 if (!memchr(sb
->buf
, comment_line_char
, sb
->len
))
678 candidate
= strchr(candidates
, *p
);
681 for (p
= sb
->buf
; *p
; p
++) {
682 if ((p
[0] == '\n' || p
[0] == '\r') && p
[1]) {
683 candidate
= strchr(candidates
, p
[1]);
689 for (p
= candidates
; *p
== ' '; p
++)
692 die(_("unable to select a comment character that is not used\n"
693 "in the current commit message"));
694 comment_line_char
= *p
;
697 static void prepare_amend_commit(struct commit
*commit
, struct strbuf
*sb
,
698 struct pretty_print_context
*ctx
)
700 const char *buffer
, *subject
, *fmt
;
702 buffer
= get_commit_buffer(commit
, NULL
);
703 find_commit_subject(buffer
, &subject
);
705 * If we amend the 'amend!' commit then we don't want to
706 * duplicate the subject line.
708 fmt
= starts_with(subject
, "amend!") ? "%b" : "%B";
709 format_commit_message(commit
, fmt
, sb
, ctx
);
710 unuse_commit_buffer(commit
, buffer
);
713 static int prepare_to_commit(const char *index_file
, const char *prefix
,
714 struct commit
*current_head
,
716 struct strbuf
*author_ident
)
719 struct strbuf committer_ident
= STRBUF_INIT
;
721 struct strbuf sb
= STRBUF_INIT
;
722 const char *hook_arg1
= NULL
;
723 const char *hook_arg2
= NULL
;
724 int clean_message_contents
= (cleanup_mode
!= COMMIT_MSG_CLEANUP_NONE
);
725 int old_display_comment_prefix
;
726 int merge_contains_scissors
= 0;
728 /* This checks and barfs if author is badly specified */
729 determine_author_info(author_ident
);
731 if (!no_verify
&& run_commit_hook(use_editor
, index_file
, "pre-commit", NULL
))
734 if (squash_message
) {
736 * Insert the proper subject line before other commit
737 * message options add their content.
739 if (use_message
&& !strcmp(use_message
, squash_message
))
740 strbuf_addstr(&sb
, "squash! ");
742 struct pretty_print_context ctx
= {0};
744 c
= lookup_commit_reference_by_name(squash_message
);
746 die(_("could not lookup commit %s"), squash_message
);
747 ctx
.output_encoding
= get_commit_output_encoding();
748 format_commit_message(c
, "squash! %s\n\n", &sb
,
753 if (have_option_m
&& !fixup_message
) {
754 strbuf_addbuf(&sb
, &message
);
755 hook_arg1
= "message";
756 } else if (logfile
&& !strcmp(logfile
, "-")) {
758 fprintf(stderr
, _("(reading log message from standard input)\n"));
759 if (strbuf_read(&sb
, 0, 0) < 0)
760 die_errno(_("could not read log from standard input"));
761 hook_arg1
= "message";
762 } else if (logfile
) {
763 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
764 die_errno(_("could not read log file '%s'"),
766 hook_arg1
= "message";
767 } else if (use_message
) {
769 buffer
= strstr(use_message_buffer
, "\n\n");
771 strbuf_addstr(&sb
, skip_blank_lines(buffer
+ 2));
772 hook_arg1
= "commit";
773 hook_arg2
= use_message
;
774 } else if (fixup_message
) {
775 struct pretty_print_context ctx
= {0};
776 struct commit
*commit
;
778 commit
= lookup_commit_reference_by_name(fixup_commit
);
780 die(_("could not lookup commit %s"), fixup_commit
);
781 ctx
.output_encoding
= get_commit_output_encoding();
782 fmt
= xstrfmt("%s! %%s\n\n", fixup_prefix
);
783 format_commit_message(commit
, fmt
, &sb
, &ctx
);
785 hook_arg1
= "message";
788 * Only `-m` commit message option is checked here, as
789 * it supports `--fixup` to append the commit message.
791 * The other commit message options `-c`/`-C`/`-F` are
792 * incompatible with all the forms of `--fixup` and
793 * have already errored out while parsing the `git commit`
796 if (have_option_m
&& !strcmp(fixup_prefix
, "fixup"))
797 strbuf_addbuf(&sb
, &message
);
799 if (!strcmp(fixup_prefix
, "amend")) {
801 die(_("cannot combine -m with --fixup:%s"), fixup_message
);
802 prepare_amend_commit(commit
, &sb
, &ctx
);
804 } else if (!stat(git_path_merge_msg(the_repository
), &statbuf
)) {
805 size_t merge_msg_start
;
808 * prepend SQUASH_MSG here if it exists and a
809 * "merge --squash" was originally performed
811 if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
812 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
813 die_errno(_("could not read SQUASH_MSG"));
814 hook_arg1
= "squash";
818 merge_msg_start
= sb
.len
;
819 if (strbuf_read_file(&sb
, git_path_merge_msg(the_repository
), 0) < 0)
820 die_errno(_("could not read MERGE_MSG"));
822 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
&&
823 wt_status_locate_end(sb
.buf
+ merge_msg_start
,
824 sb
.len
- merge_msg_start
) <
825 sb
.len
- merge_msg_start
)
826 merge_contains_scissors
= 1;
827 } else if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
828 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
829 die_errno(_("could not read SQUASH_MSG"));
830 hook_arg1
= "squash";
831 } else if (template_file
) {
832 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
833 die_errno(_("could not read '%s'"), template_file
);
834 hook_arg1
= "template";
835 clean_message_contents
= 0;
839 * The remaining cases don't modify the template message, but
840 * just set the argument(s) to the prepare-commit-msg hook.
842 else if (whence
== FROM_MERGE
)
844 else if (is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) {
845 hook_arg1
= "commit";
846 hook_arg2
= "CHERRY_PICK_HEAD";
849 if (squash_message
) {
851 * If squash_commit was used for the commit subject,
852 * then we're possibly hijacking other commit log options.
853 * Reset the hook args to tell the real story.
855 hook_arg1
= "message";
859 s
->fp
= fopen_for_writing(git_path_commit_editmsg());
861 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
863 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
864 old_display_comment_prefix
= s
->display_comment_prefix
;
865 s
->display_comment_prefix
= 1;
868 * Most hints are counter-productive when the commit has
873 if (clean_message_contents
)
874 strbuf_stripspace(&sb
, 0);
877 append_signoff(&sb
, ignore_non_trailer(sb
.buf
, sb
.len
), 0);
879 if (fwrite(sb
.buf
, 1, sb
.len
, s
->fp
) < sb
.len
)
880 die_errno(_("could not write commit template"));
882 if (auto_comment_line_char
)
883 adjust_comment_line_char(&sb
);
886 /* This checks if committer ident is explicitly given */
887 strbuf_addstr(&committer_ident
, git_committer_info(IDENT_STRICT
));
888 if (use_editor
&& include_status
) {
890 int saved_color_setting
;
891 struct ident_split ci
, ai
;
892 const char *hint_cleanup_all
= allow_empty_message
?
893 _("Please enter the commit message for your changes."
894 " Lines starting\nwith '%c' will be ignored.\n") :
895 _("Please enter the commit message for your changes."
896 " Lines starting\nwith '%c' will be ignored, and an empty"
897 " message aborts the commit.\n");
898 const char *hint_cleanup_space
= allow_empty_message
?
899 _("Please enter the commit message for your changes."
901 "with '%c' will be kept; you may remove them"
902 " yourself if you want to.\n") :
903 _("Please enter the commit message for your changes."
905 "with '%c' will be kept; you may remove them"
906 " yourself if you want to.\n"
907 "An empty message aborts the commit.\n");
908 if (whence
!= FROM_COMMIT
) {
909 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
&&
910 !merge_contains_scissors
)
911 wt_status_add_cut_line(s
->fp
);
914 whence
== FROM_MERGE
?
916 "It looks like you may be committing a merge.\n"
917 "If this is not correct, please run\n"
918 " git update-ref -d MERGE_HEAD\n"
919 "and try again.\n") :
921 "It looks like you may be committing a cherry-pick.\n"
922 "If this is not correct, please run\n"
923 " git update-ref -d CHERRY_PICK_HEAD\n"
924 "and try again.\n"));
927 fprintf(s
->fp
, "\n");
928 if (cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
)
929 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_all
, comment_line_char
);
930 else if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
931 if (whence
== FROM_COMMIT
&& !merge_contains_scissors
)
932 wt_status_add_cut_line(s
->fp
);
933 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
934 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_space
, comment_line_char
);
937 * These should never fail because they come from our own
938 * fmt_ident. They may fail the sane_ident test, but we know
939 * that the name and mail pointers will at least be valid,
940 * which is enough for our tests and printing here.
942 assert_split_ident(&ai
, author_ident
);
943 assert_split_ident(&ci
, &committer_ident
);
945 if (ident_cmp(&ai
, &ci
))
946 status_printf_ln(s
, GIT_COLOR_NORMAL
,
948 "Author: %.*s <%.*s>"),
949 ident_shown
++ ? "" : "\n",
950 (int)(ai
.name_end
- ai
.name_begin
), ai
.name_begin
,
951 (int)(ai
.mail_end
- ai
.mail_begin
), ai
.mail_begin
);
953 if (author_date_is_interesting())
954 status_printf_ln(s
, GIT_COLOR_NORMAL
,
957 ident_shown
++ ? "" : "\n",
958 show_ident_date(&ai
, DATE_MODE(NORMAL
)));
960 if (!committer_ident_sufficiently_given())
961 status_printf_ln(s
, GIT_COLOR_NORMAL
,
963 "Committer: %.*s <%.*s>"),
964 ident_shown
++ ? "" : "\n",
965 (int)(ci
.name_end
- ci
.name_begin
), ci
.name_begin
,
966 (int)(ci
.mail_end
- ci
.mail_begin
), ci
.mail_begin
);
968 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", ""); /* Add new line for clarity */
970 saved_color_setting
= s
->use_color
;
972 committable
= run_status(s
->fp
, index_file
, prefix
, 1, s
);
973 s
->use_color
= saved_color_setting
;
974 string_list_clear(&s
->change
, 1);
976 struct object_id oid
;
977 const char *parent
= "HEAD";
979 if (!active_nr
&& read_cache() < 0)
980 die(_("Cannot read index"));
985 if (get_oid(parent
, &oid
)) {
988 /* TODO: audit for interaction with sparse-index. */
989 ensure_full_index(&the_index
);
990 for (i
= 0; i
< active_nr
; i
++)
991 if (ce_intent_to_add(active_cache
[i
]))
993 committable
= active_nr
- ita_nr
> 0;
996 * Unless the user did explicitly request a submodule
997 * ignore mode by passing a command line option we do
998 * not ignore any changed submodule SHA-1s when
999 * comparing index and parent, no matter what is
1000 * configured. Otherwise we won't commit any
1001 * submodules which were manually staged, which would
1002 * be really confusing.
1004 struct diff_flags flags
= DIFF_FLAGS_INIT
;
1005 flags
.override_submodule_config
= 1;
1006 if (ignore_submodule_arg
&&
1007 !strcmp(ignore_submodule_arg
, "all"))
1008 flags
.ignore_submodules
= 1;
1009 committable
= index_differs_from(the_repository
,
1013 strbuf_release(&committer_ident
);
1017 if (trailer_args
.nr
) {
1018 struct child_process run_trailer
= CHILD_PROCESS_INIT
;
1020 strvec_pushl(&run_trailer
.args
, "interpret-trailers",
1021 "--in-place", git_path_commit_editmsg(), NULL
);
1022 strvec_pushv(&run_trailer
.args
, trailer_args
.v
);
1023 run_trailer
.git_cmd
= 1;
1024 if (run_command(&run_trailer
))
1025 die(_("unable to pass trailers to --trailers"));
1026 strvec_clear(&trailer_args
);
1030 * Reject an attempt to record a non-merge empty commit without
1031 * explicit --allow-empty. In the cherry-pick case, it may be
1032 * empty due to conflict resolution, which the user should okay.
1034 if (!committable
&& whence
!= FROM_MERGE
&& !allow_empty
&&
1035 !(amend
&& is_a_merge(current_head
))) {
1036 s
->hints
= advice_status_hints
;
1037 s
->display_comment_prefix
= old_display_comment_prefix
;
1038 run_status(stdout
, index_file
, prefix
, 0, s
);
1040 fputs(_(empty_amend_advice
), stderr
);
1041 else if (is_from_cherry_pick(whence
) ||
1042 whence
== FROM_REBASE_PICK
) {
1043 fputs(_(empty_cherry_pick_advice
), stderr
);
1044 if (whence
== FROM_CHERRY_PICK_SINGLE
)
1045 fputs(_(empty_cherry_pick_advice_single
), stderr
);
1046 else if (whence
== FROM_CHERRY_PICK_MULTI
)
1047 fputs(_(empty_cherry_pick_advice_multi
), stderr
);
1049 fputs(_(empty_rebase_pick_advice
), stderr
);
1054 if (!no_verify
&& find_hook("pre-commit")) {
1056 * Re-read the index as pre-commit hook could have updated it,
1057 * and write it out as a tree. We must do this before we invoke
1058 * the editor and after we invoke run_status above.
1062 read_cache_from(index_file
);
1064 if (update_main_cache_tree(0)) {
1065 error(_("Error building trees"));
1069 if (run_commit_hook(use_editor
, index_file
, "prepare-commit-msg",
1070 git_path_commit_editmsg(), hook_arg1
, hook_arg2
, NULL
))
1074 struct strvec env
= STRVEC_INIT
;
1076 strvec_pushf(&env
, "GIT_INDEX_FILE=%s", index_file
);
1077 if (launch_editor(git_path_commit_editmsg(), NULL
, env
.v
)) {
1079 _("Please supply the message using either -m or -F option.\n"));
1086 run_commit_hook(use_editor
, index_file
, "commit-msg", git_path_commit_editmsg(), NULL
)) {
1093 static const char *find_author_by_nickname(const char *name
)
1095 struct rev_info revs
;
1096 struct commit
*commit
;
1097 struct strbuf buf
= STRBUF_INIT
;
1098 struct string_list mailmap
= STRING_LIST_INIT_NODUP
;
1102 repo_init_revisions(the_repository
, &revs
, NULL
);
1103 strbuf_addf(&buf
, "--author=%s", name
);
1108 setup_revisions(ac
, av
, &revs
, NULL
);
1109 revs
.mailmap
= &mailmap
;
1110 read_mailmap(revs
.mailmap
);
1112 if (prepare_revision_walk(&revs
))
1113 die(_("revision walk setup failed"));
1114 commit
= get_revision(&revs
);
1116 struct pretty_print_context ctx
= {0};
1117 ctx
.date_mode
.type
= DATE_NORMAL
;
1118 strbuf_release(&buf
);
1119 format_commit_message(commit
, "%aN <%aE>", &buf
, &ctx
);
1120 clear_mailmap(&mailmap
);
1121 return strbuf_detach(&buf
, NULL
);
1123 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name
);
1126 static void handle_ignored_arg(struct wt_status
*s
)
1129 ; /* default already initialized */
1130 else if (!strcmp(ignored_arg
, "traditional"))
1131 s
->show_ignored_mode
= SHOW_TRADITIONAL_IGNORED
;
1132 else if (!strcmp(ignored_arg
, "no"))
1133 s
->show_ignored_mode
= SHOW_NO_IGNORED
;
1134 else if (!strcmp(ignored_arg
, "matching"))
1135 s
->show_ignored_mode
= SHOW_MATCHING_IGNORED
;
1137 die(_("Invalid ignored mode '%s'"), ignored_arg
);
1140 static void handle_untracked_files_arg(struct wt_status
*s
)
1142 if (!untracked_files_arg
)
1143 ; /* default already initialized */
1144 else if (!strcmp(untracked_files_arg
, "no"))
1145 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1146 else if (!strcmp(untracked_files_arg
, "normal"))
1147 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1148 else if (!strcmp(untracked_files_arg
, "all"))
1149 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1151 * Please update $__git_untracked_file_modes in
1152 * git-completion.bash when you add new options
1155 die(_("Invalid untracked files mode '%s'"), untracked_files_arg
);
1158 static const char *read_commit_message(const char *name
)
1160 const char *out_enc
;
1161 struct commit
*commit
;
1163 commit
= lookup_commit_reference_by_name(name
);
1165 die(_("could not lookup commit %s"), name
);
1166 out_enc
= get_commit_output_encoding();
1167 return logmsg_reencode(commit
, NULL
, out_enc
);
1171 * Enumerate what needs to be propagated when --porcelain
1172 * is not in effect here.
1174 static struct status_deferred_config
{
1175 enum wt_status_format status_format
;
1177 enum ahead_behind_flags ahead_behind
;
1178 } status_deferred_config
= {
1179 STATUS_FORMAT_UNSPECIFIED
,
1180 -1, /* unspecified */
1181 AHEAD_BEHIND_UNSPECIFIED
,
1184 static void finalize_deferred_config(struct wt_status
*s
)
1186 int use_deferred_config
= (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1187 status_format
!= STATUS_FORMAT_PORCELAIN_V2
&&
1188 !s
->null_termination
);
1190 if (s
->null_termination
) {
1191 if (status_format
== STATUS_FORMAT_NONE
||
1192 status_format
== STATUS_FORMAT_UNSPECIFIED
)
1193 status_format
= STATUS_FORMAT_PORCELAIN
;
1194 else if (status_format
== STATUS_FORMAT_LONG
)
1195 die(_("--long and -z are incompatible"));
1198 if (use_deferred_config
&& status_format
== STATUS_FORMAT_UNSPECIFIED
)
1199 status_format
= status_deferred_config
.status_format
;
1200 if (status_format
== STATUS_FORMAT_UNSPECIFIED
)
1201 status_format
= STATUS_FORMAT_NONE
;
1203 if (use_deferred_config
&& s
->show_branch
< 0)
1204 s
->show_branch
= status_deferred_config
.show_branch
;
1205 if (s
->show_branch
< 0)
1209 * If the user did not give a "--[no]-ahead-behind" command
1210 * line argument *AND* we will print in a human-readable format
1211 * (short, long etc.) then we inherit from the status.aheadbehind
1212 * config setting. In all other cases (and porcelain V[12] formats
1213 * in particular), we inherit _FULL for backwards compatibility.
1215 if (use_deferred_config
&&
1216 s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1217 s
->ahead_behind_flags
= status_deferred_config
.ahead_behind
;
1219 if (s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1220 s
->ahead_behind_flags
= AHEAD_BEHIND_FULL
;
1223 static void check_fixup_reword_options(int argc
, const char *argv
[]) {
1224 if (whence
!= FROM_COMMIT
) {
1225 if (whence
== FROM_MERGE
)
1226 die(_("You are in the middle of a merge -- cannot reword."));
1227 else if (is_from_cherry_pick(whence
))
1228 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1231 die(_("cannot combine reword option of --fixup with path '%s'"), *argv
);
1232 if (patch_interactive
|| interactive
|| all
|| also
|| only
)
1233 die(_("reword option of --fixup is mutually exclusive with --patch/--interactive/--all/--include/--only"));
1236 static int parse_and_validate_options(int argc
, const char *argv
[],
1237 const struct option
*options
,
1238 const char * const usage
[],
1240 struct commit
*current_head
,
1241 struct wt_status
*s
)
1245 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1246 finalize_deferred_config(s
);
1248 if (force_author
&& !strchr(force_author
, '>'))
1249 force_author
= find_author_by_nickname(force_author
);
1251 if (force_author
&& renew_authorship
)
1252 die(_("Using both --reset-author and --author does not make sense"));
1254 if (logfile
|| have_option_m
|| use_message
)
1257 /* Sanity check options */
1258 if (amend
&& !current_head
)
1259 die(_("You have nothing to amend."));
1260 if (amend
&& whence
!= FROM_COMMIT
) {
1261 if (whence
== FROM_MERGE
)
1262 die(_("You are in the middle of a merge -- cannot amend."));
1263 else if (is_from_cherry_pick(whence
))
1264 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1265 else if (whence
== FROM_REBASE_PICK
)
1266 die(_("You are in the middle of a rebase -- cannot amend."));
1268 if (fixup_message
&& squash_message
)
1269 die(_("Options --squash and --fixup cannot be used together"));
1279 die(_("Only one of -c/-C/-F/--fixup can be used."));
1280 if (have_option_m
&& (edit_message
|| use_message
|| logfile
))
1281 die((_("Option -m cannot be combined with -c/-C/-F.")));
1282 if (f
|| have_option_m
)
1283 template_file
= NULL
;
1285 use_message
= edit_message
;
1286 if (amend
&& !use_message
&& !fixup_message
)
1287 use_message
= "HEAD";
1288 if (!use_message
&& !is_from_cherry_pick(whence
) &&
1289 !is_from_rebase(whence
) && renew_authorship
)
1290 die(_("--reset-author can be used only with -C, -c or --amend."));
1292 use_message_buffer
= read_commit_message(use_message
);
1293 if (!renew_authorship
) {
1294 author_message
= use_message
;
1295 author_message_buffer
= use_message_buffer
;
1298 if ((is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) &&
1299 !renew_authorship
) {
1300 author_message
= "CHERRY_PICK_HEAD";
1301 author_message_buffer
= read_commit_message(author_message
);
1304 if (patch_interactive
)
1307 if (also
+ only
+ all
+ interactive
> 1)
1308 die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1310 if (fixup_message
) {
1312 * We limit --fixup's suboptions to only alpha characters.
1313 * If the first character after a run of alpha is colon,
1314 * then the part before the colon may be a known suboption
1315 * name like `amend` or `reword`, or a misspelt suboption
1316 * name. In either case, we treat it as
1317 * --fixup=<suboption>:<arg>.
1319 * Otherwise, we are dealing with --fixup=<commit>.
1321 char *p
= fixup_message
;
1324 if (p
> fixup_message
&& *p
== ':') {
1326 fixup_commit
= p
+ 1;
1327 if (!strcmp("amend", fixup_message
) ||
1328 !strcmp("reword", fixup_message
)) {
1329 fixup_prefix
= "amend";
1331 if (*fixup_message
== 'r') {
1332 check_fixup_reword_options(argc
, argv
);
1336 die(_("unknown option: --fixup=%s:%s"), fixup_message
, fixup_commit
);
1339 fixup_commit
= fixup_message
;
1340 fixup_prefix
= "fixup";
1346 use_editor
= edit_flag
;
1348 cleanup_mode
= get_cleanup_mode(cleanup_arg
, use_editor
);
1350 handle_untracked_files_arg(s
);
1352 if (all
&& argc
> 0)
1353 die(_("paths '%s ...' with -a does not make sense"),
1356 if (status_format
!= STATUS_FORMAT_NONE
)
1362 static int dry_run_commit(const char **argv
, const char *prefix
,
1363 const struct commit
*current_head
, struct wt_status
*s
)
1366 const char *index_file
;
1368 index_file
= prepare_index(argv
, prefix
, current_head
, 1);
1369 committable
= run_status(stdout
, index_file
, prefix
, 0, s
);
1370 rollback_index_files();
1372 return committable
? 0 : 1;
1375 define_list_config_array_extra(color_status_slots
, {"added"});
1377 static int parse_status_slot(const char *slot
)
1379 if (!strcasecmp(slot
, "added"))
1380 return WT_STATUS_UPDATED
;
1382 return LOOKUP_CONFIG(color_status_slots
, slot
);
1385 static int git_status_config(const char *k
, const char *v
, void *cb
)
1387 struct wt_status
*s
= cb
;
1388 const char *slot_name
;
1390 if (starts_with(k
, "column."))
1391 return git_column_config(k
, v
, "status", &s
->colopts
);
1392 if (!strcmp(k
, "status.submodulesummary")) {
1394 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
1395 if (is_bool
&& s
->submodule_summary
)
1396 s
->submodule_summary
= -1;
1399 if (!strcmp(k
, "status.short")) {
1400 if (git_config_bool(k
, v
))
1401 status_deferred_config
.status_format
= STATUS_FORMAT_SHORT
;
1403 status_deferred_config
.status_format
= STATUS_FORMAT_NONE
;
1406 if (!strcmp(k
, "status.branch")) {
1407 status_deferred_config
.show_branch
= git_config_bool(k
, v
);
1410 if (!strcmp(k
, "status.aheadbehind")) {
1411 status_deferred_config
.ahead_behind
= git_config_bool(k
, v
);
1414 if (!strcmp(k
, "status.showstash")) {
1415 s
->show_stash
= git_config_bool(k
, v
);
1418 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1419 s
->use_color
= git_config_colorbool(k
, v
);
1422 if (!strcmp(k
, "status.displaycommentprefix")) {
1423 s
->display_comment_prefix
= git_config_bool(k
, v
);
1426 if (skip_prefix(k
, "status.color.", &slot_name
) ||
1427 skip_prefix(k
, "color.status.", &slot_name
)) {
1428 int slot
= parse_status_slot(slot_name
);
1432 return config_error_nonbool(k
);
1433 return color_parse(v
, s
->color_palette
[slot
]);
1435 if (!strcmp(k
, "status.relativepaths")) {
1436 s
->relative_paths
= git_config_bool(k
, v
);
1439 if (!strcmp(k
, "status.showuntrackedfiles")) {
1441 return config_error_nonbool(k
);
1442 else if (!strcmp(v
, "no"))
1443 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1444 else if (!strcmp(v
, "normal"))
1445 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1446 else if (!strcmp(v
, "all"))
1447 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1449 return error(_("Invalid untracked files mode '%s'"), v
);
1452 if (!strcmp(k
, "diff.renamelimit")) {
1453 if (s
->rename_limit
== -1)
1454 s
->rename_limit
= git_config_int(k
, v
);
1457 if (!strcmp(k
, "status.renamelimit")) {
1458 s
->rename_limit
= git_config_int(k
, v
);
1461 if (!strcmp(k
, "diff.renames")) {
1462 if (s
->detect_rename
== -1)
1463 s
->detect_rename
= git_config_rename(k
, v
);
1466 if (!strcmp(k
, "status.renames")) {
1467 s
->detect_rename
= git_config_rename(k
, v
);
1470 return git_diff_ui_config(k
, v
, NULL
);
1473 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1475 static int no_renames
= -1;
1476 static const char *rename_score_arg
= (const char *)-1;
1477 static struct wt_status s
;
1478 unsigned int progress_flag
= 0;
1480 struct object_id oid
;
1481 static struct option builtin_status_options
[] = {
1482 OPT__VERBOSE(&verbose
, N_("be verbose")),
1483 OPT_SET_INT('s', "short", &status_format
,
1484 N_("show status concisely"), STATUS_FORMAT_SHORT
),
1485 OPT_BOOL('b', "branch", &s
.show_branch
,
1486 N_("show branch information")),
1487 OPT_BOOL(0, "show-stash", &s
.show_stash
,
1488 N_("show stash information")),
1489 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1490 N_("compute full ahead/behind values")),
1491 OPT_CALLBACK_F(0, "porcelain", &status_format
,
1492 N_("version"), N_("machine-readable output"),
1493 PARSE_OPT_OPTARG
, opt_parse_porcelain
),
1494 OPT_SET_INT(0, "long", &status_format
,
1495 N_("show status in long format (default)"),
1496 STATUS_FORMAT_LONG
),
1497 OPT_BOOL('z', "null", &s
.null_termination
,
1498 N_("terminate entries with NUL")),
1499 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1501 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1502 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1503 { OPTION_STRING
, 0, "ignored", &ignored_arg
,
1505 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1506 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"traditional" },
1507 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, N_("when"),
1508 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1509 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1510 OPT_COLUMN(0, "column", &s
.colopts
, N_("list untracked files in columns")),
1511 OPT_BOOL(0, "no-renames", &no_renames
, N_("do not detect renames")),
1512 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg
,
1513 N_("n"), N_("detect renames, optionally set similarity index"),
1514 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, opt_parse_rename_score
),
1518 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1519 usage_with_options(builtin_status_usage
, builtin_status_options
);
1521 prepare_repo_settings(the_repository
);
1522 the_repository
->settings
.command_requires_full_index
= 0;
1524 status_init_config(&s
, git_status_config
);
1525 argc
= parse_options(argc
, argv
, prefix
,
1526 builtin_status_options
,
1527 builtin_status_usage
, 0);
1528 finalize_colopts(&s
.colopts
, -1);
1529 finalize_deferred_config(&s
);
1531 handle_untracked_files_arg(&s
);
1532 handle_ignored_arg(&s
);
1534 if (s
.show_ignored_mode
== SHOW_MATCHING_IGNORED
&&
1535 s
.show_untracked_files
== SHOW_NO_UNTRACKED_FILES
)
1536 die(_("Unsupported combination of ignored and untracked-files arguments"));
1538 parse_pathspec(&s
.pathspec
, 0,
1539 PATHSPEC_PREFER_FULL
,
1542 if (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1543 status_format
!= STATUS_FORMAT_PORCELAIN_V2
)
1544 progress_flag
= REFRESH_PROGRESS
;
1545 repo_read_index(the_repository
);
1546 refresh_index(&the_index
,
1547 REFRESH_QUIET
|REFRESH_UNMERGED
|progress_flag
,
1548 &s
.pathspec
, NULL
, NULL
);
1550 if (use_optional_locks())
1551 fd
= hold_locked_index(&index_lock
, 0);
1555 s
.is_initial
= get_oid(s
.reference
, &oid
) ? 1 : 0;
1557 oidcpy(&s
.oid_commit
, &oid
);
1559 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1560 s
.status_format
= status_format
;
1561 s
.verbose
= verbose
;
1562 if (no_renames
!= -1)
1563 s
.detect_rename
= !no_renames
;
1564 if ((intptr_t)rename_score_arg
!= -1) {
1565 if (s
.detect_rename
< DIFF_DETECT_RENAME
)
1566 s
.detect_rename
= DIFF_DETECT_RENAME
;
1567 if (rename_score_arg
)
1568 s
.rename_score
= parse_rename_score(&rename_score_arg
);
1571 wt_status_collect(&s
);
1574 repo_update_index_if_able(the_repository
, &index_lock
);
1576 if (s
.relative_paths
)
1579 wt_status_print(&s
);
1580 wt_status_collect_free_buffers(&s
);
1585 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1587 struct wt_status
*s
= cb
;
1590 if (!strcmp(k
, "commit.template"))
1591 return git_config_pathname(&template_file
, k
, v
);
1592 if (!strcmp(k
, "commit.status")) {
1593 include_status
= git_config_bool(k
, v
);
1596 if (!strcmp(k
, "commit.cleanup"))
1597 return git_config_string(&cleanup_arg
, k
, v
);
1598 if (!strcmp(k
, "commit.gpgsign")) {
1599 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
1602 if (!strcmp(k
, "commit.verbose")) {
1604 config_commit_verbose
= git_config_bool_or_int(k
, v
, &is_bool
);
1608 status
= git_gpg_config(k
, v
, NULL
);
1611 return git_status_config(k
, v
, s
);
1614 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1616 static struct wt_status s
;
1617 static struct option builtin_commit_options
[] = {
1618 OPT__QUIET(&quiet
, N_("suppress summary after successful commit")),
1619 OPT__VERBOSE(&verbose
, N_("show diff in commit message template")),
1621 OPT_GROUP(N_("Commit message options")),
1622 OPT_FILENAME('F', "file", &logfile
, N_("read message from file")),
1623 OPT_STRING(0, "author", &force_author
, N_("author"), N_("override author for commit")),
1624 OPT_STRING(0, "date", &force_date
, N_("date"), N_("override date for commit")),
1625 OPT_CALLBACK('m', "message", &message
, N_("message"), N_("commit message"), opt_parse_m
),
1626 OPT_STRING('c', "reedit-message", &edit_message
, N_("commit"), N_("reuse and edit message from specified commit")),
1627 OPT_STRING('C', "reuse-message", &use_message
, N_("commit"), N_("reuse message from specified commit")),
1629 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1630 * and only translate <commit>.
1632 OPT_STRING(0, "fixup", &fixup_message
, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1633 OPT_STRING(0, "squash", &squash_message
, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1634 OPT_BOOL(0, "reset-author", &renew_authorship
, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1635 OPT_CALLBACK_F(0, "trailer", NULL
, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG
, opt_pass_trailer
),
1636 OPT_BOOL('s', "signoff", &signoff
, N_("add a Signed-off-by trailer")),
1637 OPT_FILENAME('t', "template", &template_file
, N_("use specified template file")),
1638 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of commit")),
1639 OPT_CLEANUP(&cleanup_arg
),
1640 OPT_BOOL(0, "status", &include_status
, N_("include status in commit message template")),
1641 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
1642 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1643 /* end commit message options */
1645 OPT_GROUP(N_("Commit contents options")),
1646 OPT_BOOL('a', "all", &all
, N_("commit all changed files")),
1647 OPT_BOOL('i', "include", &also
, N_("add specified files to index for commit")),
1648 OPT_BOOL(0, "interactive", &interactive
, N_("interactively add files")),
1649 OPT_BOOL('p', "patch", &patch_interactive
, N_("interactively add changes")),
1650 OPT_BOOL('o', "only", &only
, N_("commit only specified files")),
1651 OPT_BOOL('n', "no-verify", &no_verify
, N_("bypass pre-commit and commit-msg hooks")),
1652 OPT_BOOL(0, "dry-run", &dry_run
, N_("show what would be committed")),
1653 OPT_SET_INT(0, "short", &status_format
, N_("show status concisely"),
1654 STATUS_FORMAT_SHORT
),
1655 OPT_BOOL(0, "branch", &s
.show_branch
, N_("show branch information")),
1656 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1657 N_("compute full ahead/behind values")),
1658 OPT_SET_INT(0, "porcelain", &status_format
,
1659 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN
),
1660 OPT_SET_INT(0, "long", &status_format
,
1661 N_("show status in long format (default)"),
1662 STATUS_FORMAT_LONG
),
1663 OPT_BOOL('z', "null", &s
.null_termination
,
1664 N_("terminate entries with NUL")),
1665 OPT_BOOL(0, "amend", &amend
, N_("amend previous commit")),
1666 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite
, N_("bypass post-rewrite hook")),
1667 { 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" },
1668 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1669 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1670 /* end commit contents options */
1672 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty
,
1673 N_("ok to record an empty change")),
1674 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message
,
1675 N_("ok to record a change with an empty message")),
1680 struct strbuf sb
= STRBUF_INIT
;
1681 struct strbuf author_ident
= STRBUF_INIT
;
1682 const char *index_file
, *reflog_msg
;
1683 struct object_id oid
;
1684 struct commit_list
*parents
= NULL
;
1685 struct stat statbuf
;
1686 struct commit
*current_head
= NULL
;
1687 struct commit_extra_header
*extra
= NULL
;
1688 struct strbuf err
= STRBUF_INIT
;
1690 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1691 usage_with_options(builtin_commit_usage
, builtin_commit_options
);
1693 prepare_repo_settings(the_repository
);
1694 the_repository
->settings
.command_requires_full_index
= 0;
1696 status_init_config(&s
, git_commit_config
);
1697 s
.commit_template
= 1;
1698 status_format
= STATUS_FORMAT_NONE
; /* Ignore status.short */
1701 if (get_oid("HEAD", &oid
))
1702 current_head
= NULL
;
1704 current_head
= lookup_commit_or_die(&oid
, "HEAD");
1705 if (parse_commit(current_head
))
1706 die(_("could not parse HEAD commit"));
1708 verbose
= -1; /* unspecified */
1709 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_options
,
1710 builtin_commit_usage
,
1711 prefix
, current_head
, &s
);
1713 verbose
= (config_commit_verbose
< 0) ? 0 : config_commit_verbose
;
1716 return dry_run_commit(argv
, prefix
, current_head
, &s
);
1717 index_file
= prepare_index(argv
, prefix
, current_head
, 0);
1719 /* Set up everything for writing the commit object. This includes
1720 running hooks, writing the trees, and interacting with the user. */
1721 if (!prepare_to_commit(index_file
, prefix
,
1722 current_head
, &s
, &author_ident
)) {
1723 rollback_index_files();
1727 /* Determine parents */
1728 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1729 if (!current_head
) {
1731 reflog_msg
= "commit (initial)";
1734 reflog_msg
= "commit (amend)";
1735 parents
= copy_commit_list(current_head
->parents
);
1736 } else if (whence
== FROM_MERGE
) {
1737 struct strbuf m
= STRBUF_INIT
;
1739 int allow_fast_forward
= 1;
1740 struct commit_list
**pptr
= &parents
;
1743 reflog_msg
= "commit (merge)";
1744 pptr
= commit_list_append(current_head
, pptr
);
1745 fp
= xfopen(git_path_merge_head(the_repository
), "r");
1746 while (strbuf_getline_lf(&m
, fp
) != EOF
) {
1747 struct commit
*parent
;
1749 parent
= get_merge_parent(m
.buf
);
1751 die(_("Corrupt MERGE_HEAD file (%s)"), m
.buf
);
1752 pptr
= commit_list_append(parent
, pptr
);
1756 if (!stat(git_path_merge_mode(the_repository
), &statbuf
)) {
1757 if (strbuf_read_file(&sb
, git_path_merge_mode(the_repository
), 0) < 0)
1758 die_errno(_("could not read MERGE_MODE"));
1759 if (!strcmp(sb
.buf
, "no-ff"))
1760 allow_fast_forward
= 0;
1762 if (allow_fast_forward
)
1763 reduce_heads_replace(&parents
);
1766 reflog_msg
= is_from_cherry_pick(whence
)
1767 ? "commit (cherry-pick)"
1768 : is_from_rebase(whence
)
1771 commit_list_insert(current_head
, &parents
);
1774 /* Finally, get the commit message */
1776 if (strbuf_read_file(&sb
, git_path_commit_editmsg(), 0) < 0) {
1777 int saved_errno
= errno
;
1778 rollback_index_files();
1779 die(_("could not read commit message: %s"), strerror(saved_errno
));
1782 cleanup_message(&sb
, cleanup_mode
, verbose
);
1784 if (message_is_empty(&sb
, cleanup_mode
) && !allow_empty_message
) {
1785 rollback_index_files();
1786 fprintf(stderr
, _("Aborting commit due to empty commit message.\n"));
1789 if (template_untouched(&sb
, template_file
, cleanup_mode
) && !allow_empty_message
) {
1790 rollback_index_files();
1791 fprintf(stderr
, _("Aborting commit; you did not edit the message.\n"));
1795 if (fixup_message
&& starts_with(sb
.buf
, "amend! ") &&
1796 !allow_empty_message
) {
1797 struct strbuf body
= STRBUF_INIT
;
1798 size_t len
= commit_subject_length(sb
.buf
);
1799 strbuf_addstr(&body
, sb
.buf
+ len
);
1800 if (message_is_empty(&body
, cleanup_mode
)) {
1801 rollback_index_files();
1802 fprintf(stderr
, _("Aborting commit due to empty commit message body.\n"));
1805 strbuf_release(&body
);
1809 const char *exclude_gpgsig
[3] = { "gpgsig", "gpgsig-sha256", NULL
};
1810 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1812 struct commit_extra_header
**tail
= &extra
;
1813 append_merge_tag_headers(parents
, &tail
);
1816 if (commit_tree_extended(sb
.buf
, sb
.len
, &active_cache_tree
->oid
,
1817 parents
, &oid
, author_ident
.buf
, NULL
,
1818 sign_commit
, extra
)) {
1819 rollback_index_files();
1820 die(_("failed to write commit object"));
1822 strbuf_release(&author_ident
);
1823 free_commit_extra_headers(extra
);
1825 if (update_head_with_reflog(current_head
, &oid
, reflog_msg
, &sb
,
1827 rollback_index_files();
1831 sequencer_post_commit_cleanup(the_repository
, 0);
1832 unlink(git_path_merge_head(the_repository
));
1833 unlink(git_path_merge_msg(the_repository
));
1834 unlink(git_path_merge_mode(the_repository
));
1835 unlink(git_path_squash_msg(the_repository
));
1837 if (commit_index_files())
1838 die(_("repository has been updated, but unable to write\n"
1839 "new_index file. Check that disk is not full and quota is\n"
1840 "not exceeded, and then \"git restore --staged :/\" to recover."));
1842 git_test_write_commit_graph_or_die();
1844 repo_rerere(the_repository
, 0);
1845 run_auto_maintenance(quiet
);
1846 run_commit_hook(use_editor
, get_index_file(), "post-commit", NULL
);
1847 if (amend
&& !no_post_rewrite
) {
1848 commit_post_rewrite(the_repository
, current_head
, &oid
);
1851 unsigned int flags
= 0;
1854 flags
|= SUMMARY_INITIAL_COMMIT
;
1855 if (author_date_is_interesting())
1856 flags
|= SUMMARY_SHOW_AUTHOR_DATE
;
1857 print_commit_summary(the_repository
, prefix
,
1861 apply_autostash(git_path_merge_autostash(the_repository
));