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"
27 #include "parse-options.h"
28 #include "string-list.h"
30 #include "unpack-trees.h"
32 #include "submodule.h"
33 #include "gpg-interface.h"
35 #include "sequencer.h"
38 #include "commit-reach.h"
39 #include "commit-graph.h"
42 static const char * const builtin_commit_usage
[] = {
43 N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
44 " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]\n"
45 " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n"
46 " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n"
47 " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n"
48 " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
49 " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n"
50 " [--] [<pathspec>...]"),
54 static const char * const builtin_status_usage
[] = {
55 N_("git status [<options>] [--] [<pathspec>...]"),
59 static const char empty_amend_advice
[] =
60 N_("You asked to amend the most recent commit, but doing so would make\n"
61 "it empty. You can repeat your command with --allow-empty, or you can\n"
62 "remove the commit entirely with \"git reset HEAD^\".\n");
64 static const char empty_cherry_pick_advice
[] =
65 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
66 "If you wish to commit it anyway, use:\n"
68 " git commit --allow-empty\n"
71 static const char empty_rebase_pick_advice
[] =
72 N_("Otherwise, please use 'git rebase --skip'\n");
74 static const char empty_cherry_pick_advice_single
[] =
75 N_("Otherwise, please use 'git cherry-pick --skip'\n");
77 static const char empty_cherry_pick_advice_multi
[] =
80 " git cherry-pick --continue\n"
82 "to resume cherry-picking the remaining commits.\n"
83 "If you wish to skip this commit, use:\n"
85 " git cherry-pick --skip\n"
88 static const char *color_status_slots
[] = {
89 [WT_STATUS_HEADER
] = "header",
90 [WT_STATUS_UPDATED
] = "updated",
91 [WT_STATUS_CHANGED
] = "changed",
92 [WT_STATUS_UNTRACKED
] = "untracked",
93 [WT_STATUS_NOBRANCH
] = "noBranch",
94 [WT_STATUS_UNMERGED
] = "unmerged",
95 [WT_STATUS_LOCAL_BRANCH
] = "localBranch",
96 [WT_STATUS_REMOTE_BRANCH
] = "remoteBranch",
97 [WT_STATUS_ONBRANCH
] = "branch",
100 static const char *use_message_buffer
;
101 static struct lock_file index_lock
; /* real index */
102 static struct lock_file false_lock
; /* used only for partial commits */
109 static const char *logfile
, *force_author
;
110 static const char *template_file
;
112 * The _message variables are commit names from which to take
113 * the commit message and/or authorship.
115 static const char *author_message
, *author_message_buffer
;
116 static char *edit_message
, *use_message
;
117 static char *fixup_message
, *fixup_commit
, *squash_message
;
118 static const char *fixup_prefix
;
119 static int all
, also
, interactive
, patch_interactive
, only
, amend
, signoff
;
120 static int edit_flag
= -1; /* unspecified */
121 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
122 static int config_commit_verbose
= -1; /* unspecified */
123 static int no_post_rewrite
, allow_empty_message
, pathspec_file_nul
;
124 static char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
, *ignored_arg
;
125 static char *sign_commit
, *pathspec_from_file
;
126 static struct strvec trailer_args
= STRVEC_INIT
;
129 * The default commit message cleanup mode will remove the lines
130 * beginning with # (shell comments) and leading and trailing
131 * whitespaces (empty lines or containing only whitespaces)
132 * if editor is used, and only the whitespaces if the message
133 * is specified explicitly.
135 static enum commit_msg_cleanup_mode cleanup_mode
;
136 static const char *cleanup_arg
;
138 static enum commit_whence whence
;
139 static int use_editor
= 1, include_status
= 1;
140 static int have_option_m
;
141 static struct strbuf message
= STRBUF_INIT
;
143 static enum wt_status_format status_format
= STATUS_FORMAT_UNSPECIFIED
;
145 static int opt_pass_trailer(const struct option
*opt
, const char *arg
, int unset
)
147 BUG_ON_OPT_NEG(unset
);
149 strvec_pushl(opt
->value
, "--trailer", arg
, NULL
);
153 static int opt_parse_porcelain(const struct option
*opt
, const char *arg
, int unset
)
155 enum wt_status_format
*value
= (enum wt_status_format
*)opt
->value
;
157 *value
= STATUS_FORMAT_NONE
;
159 *value
= STATUS_FORMAT_PORCELAIN
;
160 else if (!strcmp(arg
, "v1") || !strcmp(arg
, "1"))
161 *value
= STATUS_FORMAT_PORCELAIN
;
162 else if (!strcmp(arg
, "v2") || !strcmp(arg
, "2"))
163 *value
= STATUS_FORMAT_PORCELAIN_V2
;
165 die("unsupported porcelain version '%s'", arg
);
170 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
172 struct strbuf
*buf
= opt
->value
;
175 strbuf_setlen(buf
, 0);
179 strbuf_addch(buf
, '\n');
180 strbuf_addstr(buf
, arg
);
181 strbuf_complete_line(buf
);
186 static int opt_parse_rename_score(const struct option
*opt
, const char *arg
, int unset
)
188 const char **value
= opt
->value
;
190 BUG_ON_OPT_NEG(unset
);
192 if (arg
!= NULL
&& *arg
== '=')
199 static void determine_whence(struct wt_status
*s
)
201 if (file_exists(git_path_merge_head(the_repository
)))
203 else if (!sequencer_determine_whence(the_repository
, &whence
))
204 whence
= FROM_COMMIT
;
209 static void status_init_config(struct wt_status
*s
, config_fn_t fn
)
211 wt_status_prepare(the_repository
, s
);
212 init_diff_ui_defaults();
215 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
); /* must come after git_config() */
218 static void rollback_index_files(void)
220 switch (commit_style
) {
222 break; /* nothing to do */
224 rollback_lock_file(&index_lock
);
227 rollback_lock_file(&index_lock
);
228 rollback_lock_file(&false_lock
);
233 static int commit_index_files(void)
237 switch (commit_style
) {
239 break; /* nothing to do */
241 err
= commit_lock_file(&index_lock
);
244 err
= commit_lock_file(&index_lock
);
245 rollback_lock_file(&false_lock
);
253 * Take a union of paths in the index and the named tree (typically, "HEAD"),
254 * and return the paths that match the given pattern in list.
256 static int list_paths(struct string_list
*list
, const char *with_tree
,
257 const struct pathspec
*pattern
)
265 m
= xcalloc(1, pattern
->nr
);
268 char *max_prefix
= common_prefix(pattern
);
269 overlay_tree_on_index(&the_index
, with_tree
, max_prefix
);
273 /* TODO: audit for interaction with sparse-index. */
274 ensure_full_index(&the_index
);
275 for (i
= 0; i
< active_nr
; i
++) {
276 const struct cache_entry
*ce
= active_cache
[i
];
277 struct string_list_item
*item
;
279 if (ce
->ce_flags
& CE_UPDATE
)
281 if (!ce_path_match(&the_index
, ce
, pattern
, m
))
283 item
= string_list_insert(list
, ce
->name
);
284 if (ce_skip_worktree(ce
))
285 item
->util
= item
; /* better a valid pointer than a fake one */
288 ret
= report_path_error(m
, pattern
);
293 static void add_remove_files(struct string_list
*list
)
296 for (i
= 0; i
< list
->nr
; i
++) {
298 struct string_list_item
*p
= &(list
->items
[i
]);
300 /* p->util is skip-worktree */
304 if (!lstat(p
->string
, &st
)) {
305 if (add_to_cache(p
->string
, &st
, 0))
306 die(_("updating files failed"));
308 remove_file_from_cache(p
->string
);
312 static void create_base_index(const struct commit
*current_head
)
315 struct unpack_trees_options opts
;
323 memset(&opts
, 0, sizeof(opts
));
327 opts
.src_index
= &the_index
;
328 opts
.dst_index
= &the_index
;
330 opts
.fn
= oneway_merge
;
331 tree
= parse_tree_indirect(¤t_head
->object
.oid
);
333 die(_("failed to unpack HEAD tree object"));
335 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
336 if (unpack_trees(1, &t
, &opts
))
337 exit(128); /* We've already reported the error, finish dying */
340 static void refresh_cache_or_die(int refresh_flags
)
343 * refresh_flags contains REFRESH_QUIET, so the only errors
344 * are for unmerged entries.
346 if (refresh_cache(refresh_flags
| REFRESH_IN_PORCELAIN
))
347 die_resolve_conflict("commit");
350 static const char *prepare_index(const char **argv
, const char *prefix
,
351 const struct commit
*current_head
, int is_status
)
353 struct string_list partial
= STRING_LIST_INIT_DUP
;
354 struct pathspec pathspec
;
355 int refresh_flags
= REFRESH_QUIET
;
359 refresh_flags
|= REFRESH_UNMERGED
;
360 parse_pathspec(&pathspec
, 0,
361 PATHSPEC_PREFER_FULL
,
364 if (pathspec_from_file
) {
366 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
369 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
372 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
374 parse_pathspec_file(&pathspec
, 0,
375 PATHSPEC_PREFER_FULL
,
376 prefix
, pathspec_from_file
, pathspec_file_nul
);
377 } else if (pathspec_file_nul
) {
378 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
381 if (!pathspec
.nr
&& (also
|| (only
&& !allow_empty
&&
382 (!amend
|| (fixup_message
&& strcmp(fixup_prefix
, "amend"))))))
383 die(_("No paths with --include/--only does not make sense."));
385 if (read_cache_preload(&pathspec
) < 0)
386 die(_("index file corrupt"));
389 char *old_index_env
= NULL
, *old_repo_index_file
;
390 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
392 refresh_cache_or_die(refresh_flags
);
394 if (write_locked_index(&the_index
, &index_lock
, 0))
395 die(_("unable to create temporary index"));
397 old_repo_index_file
= the_repository
->index_file
;
398 the_repository
->index_file
=
399 (char *)get_lock_file_path(&index_lock
);
400 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
401 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
403 if (interactive_add(argv
, prefix
, patch_interactive
) != 0)
404 die(_("interactive add failed"));
406 the_repository
->index_file
= old_repo_index_file
;
407 if (old_index_env
&& *old_index_env
)
408 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
410 unsetenv(INDEX_ENVIRONMENT
);
411 FREE_AND_NULL(old_index_env
);
414 read_cache_from(get_lock_file_path(&index_lock
));
415 if (update_main_cache_tree(WRITE_TREE_SILENT
) == 0) {
416 if (reopen_lock_file(&index_lock
) < 0)
417 die(_("unable to write index file"));
418 if (write_locked_index(&the_index
, &index_lock
, 0))
419 die(_("unable to update temporary index"));
421 warning(_("Failed to update main cache tree"));
423 commit_style
= COMMIT_NORMAL
;
424 ret
= get_lock_file_path(&index_lock
);
429 * Non partial, non as-is commit.
431 * (1) get the real index;
432 * (2) update the_index as necessary;
433 * (3) write the_index out to the real index (still locked);
434 * (4) return the name of the locked index file.
436 * The caller should run hooks on the locked real index, and
437 * (A) if all goes well, commit the real index;
438 * (B) on failure, rollback the real index.
440 if (all
|| (also
&& pathspec
.nr
)) {
441 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
442 add_files_to_cache(also
? prefix
: NULL
, &pathspec
, 0);
443 refresh_cache_or_die(refresh_flags
);
444 update_main_cache_tree(WRITE_TREE_SILENT
);
445 if (write_locked_index(&the_index
, &index_lock
, 0))
446 die(_("unable to write new_index file"));
447 commit_style
= COMMIT_NORMAL
;
448 ret
= get_lock_file_path(&index_lock
);
455 * (1) return the name of the real index file.
457 * The caller should run hooks on the real index,
458 * and create commit from the_index.
459 * We still need to refresh the index here.
461 if (!only
&& !pathspec
.nr
) {
462 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
463 refresh_cache_or_die(refresh_flags
);
464 if (active_cache_changed
465 || !cache_tree_fully_valid(active_cache_tree
))
466 update_main_cache_tree(WRITE_TREE_SILENT
);
467 if (write_locked_index(&the_index
, &index_lock
,
468 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
469 die(_("unable to write new_index file"));
470 commit_style
= COMMIT_AS_IS
;
471 ret
= get_index_file();
478 * (0) find the set of affected paths;
479 * (1) get lock on the real index file;
480 * (2) update the_index with the given paths;
481 * (3) write the_index out to the real index (still locked);
482 * (4) get lock on the false index file;
483 * (5) reset the_index from HEAD;
484 * (6) update the_index the same way as (2);
485 * (7) write the_index out to the false index file;
486 * (8) return the name of the false index file (still locked);
488 * The caller should run hooks on the locked false index, and
489 * create commit from it. Then
490 * (A) if all goes well, commit the real index;
491 * (B) on failure, rollback the real index;
492 * In either case, rollback the false index.
494 commit_style
= COMMIT_PARTIAL
;
496 if (whence
!= FROM_COMMIT
) {
497 if (whence
== FROM_MERGE
)
498 die(_("cannot do a partial commit during a merge."));
499 else if (is_from_cherry_pick(whence
))
500 die(_("cannot do a partial commit during a cherry-pick."));
501 else if (is_from_rebase(whence
))
502 die(_("cannot do a partial commit during a rebase."));
505 if (list_paths(&partial
, !current_head
? NULL
: "HEAD", &pathspec
))
509 if (read_cache() < 0)
510 die(_("cannot read the index"));
512 hold_locked_index(&index_lock
, LOCK_DIE_ON_ERROR
);
513 add_remove_files(&partial
);
514 refresh_cache(REFRESH_QUIET
);
515 update_main_cache_tree(WRITE_TREE_SILENT
);
516 if (write_locked_index(&the_index
, &index_lock
, 0))
517 die(_("unable to write new_index file"));
519 hold_lock_file_for_update(&false_lock
,
520 git_path("next-index-%"PRIuMAX
,
521 (uintmax_t) getpid()),
524 create_base_index(current_head
);
525 add_remove_files(&partial
);
526 refresh_cache(REFRESH_QUIET
);
528 if (write_locked_index(&the_index
, &false_lock
, 0))
529 die(_("unable to write temporary index file"));
532 ret
= get_lock_file_path(&false_lock
);
533 read_cache_from(ret
);
535 string_list_clear(&partial
, 0);
536 clear_pathspec(&pathspec
);
540 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
543 struct object_id oid
;
545 if (s
->relative_paths
)
550 s
->reference
= "HEAD^1";
552 s
->verbose
= verbose
;
553 s
->index_file
= index_file
;
556 s
->is_initial
= get_oid(s
->reference
, &oid
) ? 1 : 0;
558 oidcpy(&s
->oid_commit
, &oid
);
559 s
->status_format
= status_format
;
560 s
->ignore_submodule_arg
= ignore_submodule_arg
;
562 wt_status_collect(s
);
564 wt_status_collect_free_buffers(s
);
566 return s
->committable
;
569 static int is_a_merge(const struct commit
*current_head
)
571 return !!(current_head
->parents
&& current_head
->parents
->next
);
574 static void assert_split_ident(struct ident_split
*id
, const struct strbuf
*buf
)
576 if (split_ident_line(id
, buf
->buf
, buf
->len
) || !id
->date_begin
)
577 BUG("unable to parse our own ident: %s", buf
->buf
);
580 static void export_one(const char *var
, const char *s
, const char *e
, int hack
)
582 struct strbuf buf
= STRBUF_INIT
;
584 strbuf_addch(&buf
, hack
);
585 strbuf_add(&buf
, s
, e
- s
);
586 setenv(var
, buf
.buf
, 1);
587 strbuf_release(&buf
);
590 static int parse_force_date(const char *in
, struct strbuf
*out
)
592 strbuf_addch(out
, '@');
594 if (parse_date(in
, out
) < 0) {
596 unsigned long t
= approxidate_careful(in
, &errors
);
599 strbuf_addf(out
, "%lu", t
);
605 static void set_ident_var(char **buf
, char *val
)
611 static void determine_author_info(struct strbuf
*author_ident
)
613 char *name
, *email
, *date
;
614 struct ident_split author
;
616 name
= xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
617 email
= xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
618 date
= xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
620 if (author_message
) {
621 struct ident_split ident
;
625 a
= find_commit_header(author_message_buffer
, "author", &len
);
627 die(_("commit '%s' lacks author header"), author_message
);
628 if (split_ident_line(&ident
, a
, len
) < 0)
629 die(_("commit '%s' has malformed author line"), author_message
);
631 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
632 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
634 if (ident
.date_begin
) {
635 struct strbuf date_buf
= STRBUF_INIT
;
636 strbuf_addch(&date_buf
, '@');
637 strbuf_add(&date_buf
, ident
.date_begin
, ident
.date_end
- ident
.date_begin
);
638 strbuf_addch(&date_buf
, ' ');
639 strbuf_add(&date_buf
, ident
.tz_begin
, ident
.tz_end
- ident
.tz_begin
);
640 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
645 struct ident_split ident
;
647 if (split_ident_line(&ident
, force_author
, strlen(force_author
)) < 0)
648 die(_("malformed --author parameter"));
649 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
650 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
654 struct strbuf date_buf
= STRBUF_INIT
;
655 if (parse_force_date(force_date
, &date_buf
))
656 die(_("invalid date format: %s"), force_date
);
657 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
660 strbuf_addstr(author_ident
, fmt_ident(name
, email
, WANT_AUTHOR_IDENT
, date
,
662 assert_split_ident(&author
, author_ident
);
663 export_one("GIT_AUTHOR_NAME", author
.name_begin
, author
.name_end
, 0);
664 export_one("GIT_AUTHOR_EMAIL", author
.mail_begin
, author
.mail_end
, 0);
665 export_one("GIT_AUTHOR_DATE", author
.date_begin
, author
.tz_end
, '@');
671 static int author_date_is_interesting(void)
673 return author_message
|| force_date
;
676 static void adjust_comment_line_char(const struct strbuf
*sb
)
678 char candidates
[] = "#;@!$%^&|:";
682 comment_line_char
= candidates
[0];
683 if (!memchr(sb
->buf
, comment_line_char
, sb
->len
))
687 candidate
= strchr(candidates
, *p
);
690 for (p
= sb
->buf
; *p
; p
++) {
691 if ((p
[0] == '\n' || p
[0] == '\r') && p
[1]) {
692 candidate
= strchr(candidates
, p
[1]);
698 for (p
= candidates
; *p
== ' '; p
++)
701 die(_("unable to select a comment character that is not used\n"
702 "in the current commit message"));
703 comment_line_char
= *p
;
706 static void prepare_amend_commit(struct commit
*commit
, struct strbuf
*sb
,
707 struct pretty_print_context
*ctx
)
709 const char *buffer
, *subject
, *fmt
;
711 buffer
= get_commit_buffer(commit
, NULL
);
712 find_commit_subject(buffer
, &subject
);
714 * If we amend the 'amend!' commit then we don't want to
715 * duplicate the subject line.
717 fmt
= starts_with(subject
, "amend!") ? "%b" : "%B";
718 format_commit_message(commit
, fmt
, sb
, ctx
);
719 unuse_commit_buffer(commit
, buffer
);
722 static int prepare_to_commit(const char *index_file
, const char *prefix
,
723 struct commit
*current_head
,
725 struct strbuf
*author_ident
)
728 struct strbuf committer_ident
= STRBUF_INIT
;
730 struct strbuf sb
= STRBUF_INIT
;
731 const char *hook_arg1
= NULL
;
732 const char *hook_arg2
= NULL
;
733 int clean_message_contents
= (cleanup_mode
!= COMMIT_MSG_CLEANUP_NONE
);
734 int old_display_comment_prefix
;
735 int merge_contains_scissors
= 0;
738 /* This checks and barfs if author is badly specified */
739 determine_author_info(author_ident
);
741 if (!no_verify
&& run_commit_hook(use_editor
, index_file
, &invoked_hook
,
745 if (squash_message
) {
747 * Insert the proper subject line before other commit
748 * message options add their content.
750 if (use_message
&& !strcmp(use_message
, squash_message
))
751 strbuf_addstr(&sb
, "squash! ");
753 struct pretty_print_context ctx
= {0};
755 c
= lookup_commit_reference_by_name(squash_message
);
757 die(_("could not lookup commit %s"), squash_message
);
758 ctx
.output_encoding
= get_commit_output_encoding();
759 format_commit_message(c
, "squash! %s\n\n", &sb
,
764 if (have_option_m
&& !fixup_message
) {
765 strbuf_addbuf(&sb
, &message
);
766 hook_arg1
= "message";
767 } else if (logfile
&& !strcmp(logfile
, "-")) {
769 fprintf(stderr
, _("(reading log message from standard input)\n"));
770 if (strbuf_read(&sb
, 0, 0) < 0)
771 die_errno(_("could not read log from standard input"));
772 hook_arg1
= "message";
773 } else if (logfile
) {
774 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
775 die_errno(_("could not read log file '%s'"),
777 hook_arg1
= "message";
778 } else if (use_message
) {
780 buffer
= strstr(use_message_buffer
, "\n\n");
782 strbuf_addstr(&sb
, skip_blank_lines(buffer
+ 2));
783 hook_arg1
= "commit";
784 hook_arg2
= use_message
;
785 } else if (fixup_message
) {
786 struct pretty_print_context ctx
= {0};
787 struct commit
*commit
;
789 commit
= lookup_commit_reference_by_name(fixup_commit
);
791 die(_("could not lookup commit %s"), fixup_commit
);
792 ctx
.output_encoding
= get_commit_output_encoding();
793 fmt
= xstrfmt("%s! %%s\n\n", fixup_prefix
);
794 format_commit_message(commit
, fmt
, &sb
, &ctx
);
796 hook_arg1
= "message";
799 * Only `-m` commit message option is checked here, as
800 * it supports `--fixup` to append the commit message.
802 * The other commit message options `-c`/`-C`/`-F` are
803 * incompatible with all the forms of `--fixup` and
804 * have already errored out while parsing the `git commit`
807 if (have_option_m
&& !strcmp(fixup_prefix
, "fixup"))
808 strbuf_addbuf(&sb
, &message
);
810 if (!strcmp(fixup_prefix
, "amend")) {
812 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message
);
813 prepare_amend_commit(commit
, &sb
, &ctx
);
815 } else if (!stat(git_path_merge_msg(the_repository
), &statbuf
)) {
816 size_t merge_msg_start
;
819 * prepend SQUASH_MSG here if it exists and a
820 * "merge --squash" was originally performed
822 if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
823 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
824 die_errno(_("could not read SQUASH_MSG"));
825 hook_arg1
= "squash";
829 merge_msg_start
= sb
.len
;
830 if (strbuf_read_file(&sb
, git_path_merge_msg(the_repository
), 0) < 0)
831 die_errno(_("could not read MERGE_MSG"));
833 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
&&
834 wt_status_locate_end(sb
.buf
+ merge_msg_start
,
835 sb
.len
- merge_msg_start
) <
836 sb
.len
- merge_msg_start
)
837 merge_contains_scissors
= 1;
838 } else if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
839 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
840 die_errno(_("could not read SQUASH_MSG"));
841 hook_arg1
= "squash";
842 } else if (template_file
) {
843 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
844 die_errno(_("could not read '%s'"), template_file
);
845 hook_arg1
= "template";
846 clean_message_contents
= 0;
850 * The remaining cases don't modify the template message, but
851 * just set the argument(s) to the prepare-commit-msg hook.
853 else if (whence
== FROM_MERGE
)
855 else if (is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) {
856 hook_arg1
= "commit";
857 hook_arg2
= "CHERRY_PICK_HEAD";
860 if (squash_message
) {
862 * If squash_commit was used for the commit subject,
863 * then we're possibly hijacking other commit log options.
864 * Reset the hook args to tell the real story.
866 hook_arg1
= "message";
870 s
->fp
= fopen_for_writing(git_path_commit_editmsg());
872 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
874 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
875 old_display_comment_prefix
= s
->display_comment_prefix
;
876 s
->display_comment_prefix
= 1;
879 * Most hints are counter-productive when the commit has
884 if (clean_message_contents
)
885 strbuf_stripspace(&sb
, 0);
888 append_signoff(&sb
, ignore_non_trailer(sb
.buf
, sb
.len
), 0);
890 if (fwrite(sb
.buf
, 1, sb
.len
, s
->fp
) < sb
.len
)
891 die_errno(_("could not write commit template"));
893 if (auto_comment_line_char
)
894 adjust_comment_line_char(&sb
);
897 /* This checks if committer ident is explicitly given */
898 strbuf_addstr(&committer_ident
, git_committer_info(IDENT_STRICT
));
899 if (use_editor
&& include_status
) {
901 int saved_color_setting
;
902 struct ident_split ci
, ai
;
903 const char *hint_cleanup_all
= allow_empty_message
?
904 _("Please enter the commit message for your changes."
905 " Lines starting\nwith '%c' will be ignored.\n") :
906 _("Please enter the commit message for your changes."
907 " Lines starting\nwith '%c' will be ignored, and an empty"
908 " message aborts the commit.\n");
909 const char *hint_cleanup_space
= allow_empty_message
?
910 _("Please enter the commit message for your changes."
912 "with '%c' will be kept; you may remove them"
913 " yourself if you want to.\n") :
914 _("Please enter the commit message for your changes."
916 "with '%c' will be kept; you may remove them"
917 " yourself if you want to.\n"
918 "An empty message aborts the commit.\n");
919 if (whence
!= FROM_COMMIT
) {
920 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
&&
921 !merge_contains_scissors
)
922 wt_status_add_cut_line(s
->fp
);
925 whence
== FROM_MERGE
?
927 "It looks like you may be committing a merge.\n"
928 "If this is not correct, please run\n"
929 " git update-ref -d MERGE_HEAD\n"
930 "and try again.\n") :
932 "It looks like you may be committing a cherry-pick.\n"
933 "If this is not correct, please run\n"
934 " git update-ref -d CHERRY_PICK_HEAD\n"
935 "and try again.\n"));
938 fprintf(s
->fp
, "\n");
939 if (cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
)
940 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_all
, comment_line_char
);
941 else if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
942 if (whence
== FROM_COMMIT
&& !merge_contains_scissors
)
943 wt_status_add_cut_line(s
->fp
);
944 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
945 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_space
, comment_line_char
);
948 * These should never fail because they come from our own
949 * fmt_ident. They may fail the sane_ident test, but we know
950 * that the name and mail pointers will at least be valid,
951 * which is enough for our tests and printing here.
953 assert_split_ident(&ai
, author_ident
);
954 assert_split_ident(&ci
, &committer_ident
);
956 if (ident_cmp(&ai
, &ci
))
957 status_printf_ln(s
, GIT_COLOR_NORMAL
,
959 "Author: %.*s <%.*s>"),
960 ident_shown
++ ? "" : "\n",
961 (int)(ai
.name_end
- ai
.name_begin
), ai
.name_begin
,
962 (int)(ai
.mail_end
- ai
.mail_begin
), ai
.mail_begin
);
964 if (author_date_is_interesting())
965 status_printf_ln(s
, GIT_COLOR_NORMAL
,
968 ident_shown
++ ? "" : "\n",
969 show_ident_date(&ai
, DATE_MODE(NORMAL
)));
971 if (!committer_ident_sufficiently_given())
972 status_printf_ln(s
, GIT_COLOR_NORMAL
,
974 "Committer: %.*s <%.*s>"),
975 ident_shown
++ ? "" : "\n",
976 (int)(ci
.name_end
- ci
.name_begin
), ci
.name_begin
,
977 (int)(ci
.mail_end
- ci
.mail_begin
), ci
.mail_begin
);
979 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", ""); /* Add new line for clarity */
981 saved_color_setting
= s
->use_color
;
983 committable
= run_status(s
->fp
, index_file
, prefix
, 1, s
);
984 s
->use_color
= saved_color_setting
;
985 string_list_clear(&s
->change
, 1);
987 struct object_id oid
;
988 const char *parent
= "HEAD";
990 if (!active_nr
&& read_cache() < 0)
991 die(_("Cannot read index"));
996 if (get_oid(parent
, &oid
)) {
999 /* TODO: audit for interaction with sparse-index. */
1000 ensure_full_index(&the_index
);
1001 for (i
= 0; i
< active_nr
; i
++)
1002 if (ce_intent_to_add(active_cache
[i
]))
1004 committable
= active_nr
- ita_nr
> 0;
1007 * Unless the user did explicitly request a submodule
1008 * ignore mode by passing a command line option we do
1009 * not ignore any changed submodule SHA-1s when
1010 * comparing index and parent, no matter what is
1011 * configured. Otherwise we won't commit any
1012 * submodules which were manually staged, which would
1013 * be really confusing.
1015 struct diff_flags flags
= DIFF_FLAGS_INIT
;
1016 flags
.override_submodule_config
= 1;
1017 if (ignore_submodule_arg
&&
1018 !strcmp(ignore_submodule_arg
, "all"))
1019 flags
.ignore_submodules
= 1;
1020 committable
= index_differs_from(the_repository
,
1024 strbuf_release(&committer_ident
);
1028 if (trailer_args
.nr
) {
1029 struct child_process run_trailer
= CHILD_PROCESS_INIT
;
1031 strvec_pushl(&run_trailer
.args
, "interpret-trailers",
1032 "--in-place", git_path_commit_editmsg(), NULL
);
1033 strvec_pushv(&run_trailer
.args
, trailer_args
.v
);
1034 run_trailer
.git_cmd
= 1;
1035 if (run_command(&run_trailer
))
1036 die(_("unable to pass trailers to --trailers"));
1037 strvec_clear(&trailer_args
);
1041 * Reject an attempt to record a non-merge empty commit without
1042 * explicit --allow-empty. In the cherry-pick case, it may be
1043 * empty due to conflict resolution, which the user should okay.
1045 if (!committable
&& whence
!= FROM_MERGE
&& !allow_empty
&&
1046 !(amend
&& is_a_merge(current_head
))) {
1047 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
);
1048 s
->display_comment_prefix
= old_display_comment_prefix
;
1049 run_status(stdout
, index_file
, prefix
, 0, s
);
1051 fputs(_(empty_amend_advice
), stderr
);
1052 else if (is_from_cherry_pick(whence
) ||
1053 whence
== FROM_REBASE_PICK
) {
1054 fputs(_(empty_cherry_pick_advice
), stderr
);
1055 if (whence
== FROM_CHERRY_PICK_SINGLE
)
1056 fputs(_(empty_cherry_pick_advice_single
), stderr
);
1057 else if (whence
== FROM_CHERRY_PICK_MULTI
)
1058 fputs(_(empty_cherry_pick_advice_multi
), stderr
);
1060 fputs(_(empty_rebase_pick_advice
), stderr
);
1065 if (!no_verify
&& invoked_hook
) {
1067 * Re-read the index as the pre-commit-commit hook was invoked
1068 * and could have updated it. We must do this before we invoke
1069 * the editor and after we invoke run_status above.
1073 read_cache_from(index_file
);
1075 if (update_main_cache_tree(0)) {
1076 error(_("Error building trees"));
1080 if (run_commit_hook(use_editor
, index_file
, NULL
, "prepare-commit-msg",
1081 git_path_commit_editmsg(), hook_arg1
, hook_arg2
, NULL
))
1085 struct strvec env
= STRVEC_INIT
;
1087 strvec_pushf(&env
, "GIT_INDEX_FILE=%s", index_file
);
1088 if (launch_editor(git_path_commit_editmsg(), NULL
, env
.v
)) {
1090 _("Please supply the message using either -m or -F option.\n"));
1097 run_commit_hook(use_editor
, index_file
, NULL
, "commit-msg",
1098 git_path_commit_editmsg(), NULL
)) {
1105 static const char *find_author_by_nickname(const char *name
)
1107 struct rev_info revs
;
1108 struct commit
*commit
;
1109 struct strbuf buf
= STRBUF_INIT
;
1113 repo_init_revisions(the_repository
, &revs
, NULL
);
1114 strbuf_addf(&buf
, "--author=%s", name
);
1119 setup_revisions(ac
, av
, &revs
, NULL
);
1120 revs
.mailmap
= xmalloc(sizeof(struct string_list
));
1121 string_list_init_nodup(revs
.mailmap
);
1122 read_mailmap(revs
.mailmap
);
1124 if (prepare_revision_walk(&revs
))
1125 die(_("revision walk setup failed"));
1126 commit
= get_revision(&revs
);
1128 struct pretty_print_context ctx
= {0};
1129 ctx
.date_mode
.type
= DATE_NORMAL
;
1130 strbuf_release(&buf
);
1131 format_commit_message(commit
, "%aN <%aE>", &buf
, &ctx
);
1132 release_revisions(&revs
);
1133 return strbuf_detach(&buf
, NULL
);
1135 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name
);
1138 static void handle_ignored_arg(struct wt_status
*s
)
1141 ; /* default already initialized */
1142 else if (!strcmp(ignored_arg
, "traditional"))
1143 s
->show_ignored_mode
= SHOW_TRADITIONAL_IGNORED
;
1144 else if (!strcmp(ignored_arg
, "no"))
1145 s
->show_ignored_mode
= SHOW_NO_IGNORED
;
1146 else if (!strcmp(ignored_arg
, "matching"))
1147 s
->show_ignored_mode
= SHOW_MATCHING_IGNORED
;
1149 die(_("Invalid ignored mode '%s'"), ignored_arg
);
1152 static void handle_untracked_files_arg(struct wt_status
*s
)
1154 if (!untracked_files_arg
)
1155 ; /* default already initialized */
1156 else if (!strcmp(untracked_files_arg
, "no"))
1157 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1158 else if (!strcmp(untracked_files_arg
, "normal"))
1159 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1160 else if (!strcmp(untracked_files_arg
, "all"))
1161 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1163 * Please update $__git_untracked_file_modes in
1164 * git-completion.bash when you add new options
1167 die(_("Invalid untracked files mode '%s'"), untracked_files_arg
);
1170 static const char *read_commit_message(const char *name
)
1172 const char *out_enc
;
1173 struct commit
*commit
;
1175 commit
= lookup_commit_reference_by_name(name
);
1177 die(_("could not lookup commit %s"), name
);
1178 out_enc
= get_commit_output_encoding();
1179 return logmsg_reencode(commit
, NULL
, out_enc
);
1183 * Enumerate what needs to be propagated when --porcelain
1184 * is not in effect here.
1186 static struct status_deferred_config
{
1187 enum wt_status_format status_format
;
1189 enum ahead_behind_flags ahead_behind
;
1190 } status_deferred_config
= {
1191 STATUS_FORMAT_UNSPECIFIED
,
1192 -1, /* unspecified */
1193 AHEAD_BEHIND_UNSPECIFIED
,
1196 static void finalize_deferred_config(struct wt_status
*s
)
1198 int use_deferred_config
= (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1199 status_format
!= STATUS_FORMAT_PORCELAIN_V2
&&
1200 !s
->null_termination
);
1202 if (s
->null_termination
) {
1203 if (status_format
== STATUS_FORMAT_NONE
||
1204 status_format
== STATUS_FORMAT_UNSPECIFIED
)
1205 status_format
= STATUS_FORMAT_PORCELAIN
;
1206 else if (status_format
== STATUS_FORMAT_LONG
)
1207 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
1210 if (use_deferred_config
&& status_format
== STATUS_FORMAT_UNSPECIFIED
)
1211 status_format
= status_deferred_config
.status_format
;
1212 if (status_format
== STATUS_FORMAT_UNSPECIFIED
)
1213 status_format
= STATUS_FORMAT_NONE
;
1215 if (use_deferred_config
&& s
->show_branch
< 0)
1216 s
->show_branch
= status_deferred_config
.show_branch
;
1217 if (s
->show_branch
< 0)
1221 * If the user did not give a "--[no]-ahead-behind" command
1222 * line argument *AND* we will print in a human-readable format
1223 * (short, long etc.) then we inherit from the status.aheadbehind
1224 * config setting. In all other cases (and porcelain V[12] formats
1225 * in particular), we inherit _FULL for backwards compatibility.
1227 if (use_deferred_config
&&
1228 s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1229 s
->ahead_behind_flags
= status_deferred_config
.ahead_behind
;
1231 if (s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1232 s
->ahead_behind_flags
= AHEAD_BEHIND_FULL
;
1235 static void check_fixup_reword_options(int argc
, const char *argv
[]) {
1236 if (whence
!= FROM_COMMIT
) {
1237 if (whence
== FROM_MERGE
)
1238 die(_("You are in the middle of a merge -- cannot reword."));
1239 else if (is_from_cherry_pick(whence
))
1240 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1243 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv
);
1244 if (patch_interactive
|| interactive
|| all
|| also
|| only
)
1245 die(_("reword option of '%s' and '%s' cannot be used together"),
1246 "--fixup", "--patch/--interactive/--all/--include/--only");
1249 static int parse_and_validate_options(int argc
, const char *argv
[],
1250 const struct option
*options
,
1251 const char * const usage
[],
1253 struct commit
*current_head
,
1254 struct wt_status
*s
)
1256 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1257 finalize_deferred_config(s
);
1259 if (force_author
&& !strchr(force_author
, '>'))
1260 force_author
= find_author_by_nickname(force_author
);
1262 if (force_author
&& renew_authorship
)
1263 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
1265 if (logfile
|| have_option_m
|| use_message
)
1268 /* Sanity check options */
1269 if (amend
&& !current_head
)
1270 die(_("You have nothing to amend."));
1271 if (amend
&& whence
!= FROM_COMMIT
) {
1272 if (whence
== FROM_MERGE
)
1273 die(_("You are in the middle of a merge -- cannot amend."));
1274 else if (is_from_cherry_pick(whence
))
1275 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1276 else if (whence
== FROM_REBASE_PICK
)
1277 die(_("You are in the middle of a rebase -- cannot amend."));
1279 if (fixup_message
&& squash_message
)
1280 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1281 die_for_incompatible_opt4(!!use_message
, "-C",
1282 !!edit_message
, "-c",
1284 !!fixup_message
, "--fixup");
1285 die_for_incompatible_opt4(have_option_m
, "-m",
1286 !!edit_message
, "-c",
1287 !!use_message
, "-C",
1289 if (use_message
|| edit_message
|| logfile
||fixup_message
|| have_option_m
)
1290 template_file
= NULL
;
1292 use_message
= edit_message
;
1293 if (amend
&& !use_message
&& !fixup_message
)
1294 use_message
= "HEAD";
1295 if (!use_message
&& !is_from_cherry_pick(whence
) &&
1296 !is_from_rebase(whence
) && renew_authorship
)
1297 die(_("--reset-author can be used only with -C, -c or --amend."));
1299 use_message_buffer
= read_commit_message(use_message
);
1300 if (!renew_authorship
) {
1301 author_message
= use_message
;
1302 author_message_buffer
= use_message_buffer
;
1305 if ((is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) &&
1306 !renew_authorship
) {
1307 author_message
= "CHERRY_PICK_HEAD";
1308 author_message_buffer
= read_commit_message(author_message
);
1311 if (patch_interactive
)
1314 die_for_incompatible_opt4(also
, "-i/--include",
1317 interactive
, "--interactive/-p/--patch");
1318 if (fixup_message
) {
1320 * We limit --fixup's suboptions to only alpha characters.
1321 * If the first character after a run of alpha is colon,
1322 * then the part before the colon may be a known suboption
1323 * name like `amend` or `reword`, or a misspelt suboption
1324 * name. In either case, we treat it as
1325 * --fixup=<suboption>:<arg>.
1327 * Otherwise, we are dealing with --fixup=<commit>.
1329 char *p
= fixup_message
;
1332 if (p
> fixup_message
&& *p
== ':') {
1334 fixup_commit
= p
+ 1;
1335 if (!strcmp("amend", fixup_message
) ||
1336 !strcmp("reword", fixup_message
)) {
1337 fixup_prefix
= "amend";
1339 if (*fixup_message
== 'r') {
1340 check_fixup_reword_options(argc
, argv
);
1344 die(_("unknown option: --fixup=%s:%s"), fixup_message
, fixup_commit
);
1347 fixup_commit
= fixup_message
;
1348 fixup_prefix
= "fixup";
1354 use_editor
= edit_flag
;
1356 cleanup_mode
= get_cleanup_mode(cleanup_arg
, use_editor
);
1358 handle_untracked_files_arg(s
);
1360 if (all
&& argc
> 0)
1361 die(_("paths '%s ...' with -a does not make sense"),
1364 if (status_format
!= STATUS_FORMAT_NONE
)
1370 static int dry_run_commit(const char **argv
, const char *prefix
,
1371 const struct commit
*current_head
, struct wt_status
*s
)
1374 const char *index_file
;
1376 index_file
= prepare_index(argv
, prefix
, current_head
, 1);
1377 committable
= run_status(stdout
, index_file
, prefix
, 0, s
);
1378 rollback_index_files();
1380 return committable
? 0 : 1;
1383 define_list_config_array_extra(color_status_slots
, {"added"});
1385 static int parse_status_slot(const char *slot
)
1387 if (!strcasecmp(slot
, "added"))
1388 return WT_STATUS_UPDATED
;
1390 return LOOKUP_CONFIG(color_status_slots
, slot
);
1393 static int git_status_config(const char *k
, const char *v
, void *cb
)
1395 struct wt_status
*s
= cb
;
1396 const char *slot_name
;
1398 if (starts_with(k
, "column."))
1399 return git_column_config(k
, v
, "status", &s
->colopts
);
1400 if (!strcmp(k
, "status.submodulesummary")) {
1402 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
1403 if (is_bool
&& s
->submodule_summary
)
1404 s
->submodule_summary
= -1;
1407 if (!strcmp(k
, "status.short")) {
1408 if (git_config_bool(k
, v
))
1409 status_deferred_config
.status_format
= STATUS_FORMAT_SHORT
;
1411 status_deferred_config
.status_format
= STATUS_FORMAT_NONE
;
1414 if (!strcmp(k
, "status.branch")) {
1415 status_deferred_config
.show_branch
= git_config_bool(k
, v
);
1418 if (!strcmp(k
, "status.aheadbehind")) {
1419 status_deferred_config
.ahead_behind
= git_config_bool(k
, v
);
1422 if (!strcmp(k
, "status.showstash")) {
1423 s
->show_stash
= git_config_bool(k
, v
);
1426 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1427 s
->use_color
= git_config_colorbool(k
, v
);
1430 if (!strcmp(k
, "status.displaycommentprefix")) {
1431 s
->display_comment_prefix
= git_config_bool(k
, v
);
1434 if (skip_prefix(k
, "status.color.", &slot_name
) ||
1435 skip_prefix(k
, "color.status.", &slot_name
)) {
1436 int slot
= parse_status_slot(slot_name
);
1440 return config_error_nonbool(k
);
1441 return color_parse(v
, s
->color_palette
[slot
]);
1443 if (!strcmp(k
, "status.relativepaths")) {
1444 s
->relative_paths
= git_config_bool(k
, v
);
1447 if (!strcmp(k
, "status.showuntrackedfiles")) {
1449 return config_error_nonbool(k
);
1450 else if (!strcmp(v
, "no"))
1451 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1452 else if (!strcmp(v
, "normal"))
1453 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1454 else if (!strcmp(v
, "all"))
1455 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1457 return error(_("Invalid untracked files mode '%s'"), v
);
1460 if (!strcmp(k
, "diff.renamelimit")) {
1461 if (s
->rename_limit
== -1)
1462 s
->rename_limit
= git_config_int(k
, v
);
1465 if (!strcmp(k
, "status.renamelimit")) {
1466 s
->rename_limit
= git_config_int(k
, v
);
1469 if (!strcmp(k
, "diff.renames")) {
1470 if (s
->detect_rename
== -1)
1471 s
->detect_rename
= git_config_rename(k
, v
);
1474 if (!strcmp(k
, "status.renames")) {
1475 s
->detect_rename
= git_config_rename(k
, v
);
1478 return git_diff_ui_config(k
, v
, NULL
);
1481 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1483 static int no_renames
= -1;
1484 static const char *rename_score_arg
= (const char *)-1;
1485 static struct wt_status s
;
1486 unsigned int progress_flag
= 0;
1488 struct object_id oid
;
1489 static struct option builtin_status_options
[] = {
1490 OPT__VERBOSE(&verbose
, N_("be verbose")),
1491 OPT_SET_INT('s', "short", &status_format
,
1492 N_("show status concisely"), STATUS_FORMAT_SHORT
),
1493 OPT_BOOL('b', "branch", &s
.show_branch
,
1494 N_("show branch information")),
1495 OPT_BOOL(0, "show-stash", &s
.show_stash
,
1496 N_("show stash information")),
1497 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1498 N_("compute full ahead/behind values")),
1499 OPT_CALLBACK_F(0, "porcelain", &status_format
,
1500 N_("version"), N_("machine-readable output"),
1501 PARSE_OPT_OPTARG
, opt_parse_porcelain
),
1502 OPT_SET_INT(0, "long", &status_format
,
1503 N_("show status in long format (default)"),
1504 STATUS_FORMAT_LONG
),
1505 OPT_BOOL('z', "null", &s
.null_termination
,
1506 N_("terminate entries with NUL")),
1507 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1509 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1510 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1511 { OPTION_STRING
, 0, "ignored", &ignored_arg
,
1513 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1514 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"traditional" },
1515 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, N_("when"),
1516 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1517 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1518 OPT_COLUMN(0, "column", &s
.colopts
, N_("list untracked files in columns")),
1519 OPT_BOOL(0, "no-renames", &no_renames
, N_("do not detect renames")),
1520 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg
,
1521 N_("n"), N_("detect renames, optionally set similarity index"),
1522 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, opt_parse_rename_score
),
1526 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1527 usage_with_options(builtin_status_usage
, builtin_status_options
);
1529 prepare_repo_settings(the_repository
);
1530 the_repository
->settings
.command_requires_full_index
= 0;
1532 status_init_config(&s
, git_status_config
);
1533 argc
= parse_options(argc
, argv
, prefix
,
1534 builtin_status_options
,
1535 builtin_status_usage
, 0);
1536 finalize_colopts(&s
.colopts
, -1);
1537 finalize_deferred_config(&s
);
1539 handle_untracked_files_arg(&s
);
1540 handle_ignored_arg(&s
);
1542 if (s
.show_ignored_mode
== SHOW_MATCHING_IGNORED
&&
1543 s
.show_untracked_files
== SHOW_NO_UNTRACKED_FILES
)
1544 die(_("Unsupported combination of ignored and untracked-files arguments"));
1546 parse_pathspec(&s
.pathspec
, 0,
1547 PATHSPEC_PREFER_FULL
,
1550 if (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1551 status_format
!= STATUS_FORMAT_PORCELAIN_V2
)
1552 progress_flag
= REFRESH_PROGRESS
;
1553 repo_read_index(the_repository
);
1554 refresh_index(&the_index
,
1555 REFRESH_QUIET
|REFRESH_UNMERGED
|progress_flag
,
1556 &s
.pathspec
, NULL
, NULL
);
1558 if (use_optional_locks())
1559 fd
= hold_locked_index(&index_lock
, 0);
1563 s
.is_initial
= get_oid(s
.reference
, &oid
) ? 1 : 0;
1565 oidcpy(&s
.oid_commit
, &oid
);
1567 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1568 s
.status_format
= status_format
;
1569 s
.verbose
= verbose
;
1570 if (no_renames
!= -1)
1571 s
.detect_rename
= !no_renames
;
1572 if ((intptr_t)rename_score_arg
!= -1) {
1573 if (s
.detect_rename
< DIFF_DETECT_RENAME
)
1574 s
.detect_rename
= DIFF_DETECT_RENAME
;
1575 if (rename_score_arg
)
1576 s
.rename_score
= parse_rename_score(&rename_score_arg
);
1579 wt_status_collect(&s
);
1582 repo_update_index_if_able(the_repository
, &index_lock
);
1584 if (s
.relative_paths
)
1587 wt_status_print(&s
);
1588 wt_status_collect_free_buffers(&s
);
1593 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1595 struct wt_status
*s
= cb
;
1598 if (!strcmp(k
, "commit.template"))
1599 return git_config_pathname(&template_file
, k
, v
);
1600 if (!strcmp(k
, "commit.status")) {
1601 include_status
= git_config_bool(k
, v
);
1604 if (!strcmp(k
, "commit.cleanup"))
1605 return git_config_string(&cleanup_arg
, k
, v
);
1606 if (!strcmp(k
, "commit.gpgsign")) {
1607 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
1610 if (!strcmp(k
, "commit.verbose")) {
1612 config_commit_verbose
= git_config_bool_or_int(k
, v
, &is_bool
);
1616 status
= git_gpg_config(k
, v
, NULL
);
1619 return git_status_config(k
, v
, s
);
1622 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1624 static struct wt_status s
;
1625 static struct option builtin_commit_options
[] = {
1626 OPT__QUIET(&quiet
, N_("suppress summary after successful commit")),
1627 OPT__VERBOSE(&verbose
, N_("show diff in commit message template")),
1629 OPT_GROUP(N_("Commit message options")),
1630 OPT_FILENAME('F', "file", &logfile
, N_("read message from file")),
1631 OPT_STRING(0, "author", &force_author
, N_("author"), N_("override author for commit")),
1632 OPT_STRING(0, "date", &force_date
, N_("date"), N_("override date for commit")),
1633 OPT_CALLBACK('m', "message", &message
, N_("message"), N_("commit message"), opt_parse_m
),
1634 OPT_STRING('c', "reedit-message", &edit_message
, N_("commit"), N_("reuse and edit message from specified commit")),
1635 OPT_STRING('C', "reuse-message", &use_message
, N_("commit"), N_("reuse message from specified commit")),
1637 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1638 * and only translate <commit>.
1640 OPT_STRING(0, "fixup", &fixup_message
, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1641 OPT_STRING(0, "squash", &squash_message
, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1642 OPT_BOOL(0, "reset-author", &renew_authorship
, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1643 OPT_CALLBACK_F(0, "trailer", &trailer_args
, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG
, opt_pass_trailer
),
1644 OPT_BOOL('s', "signoff", &signoff
, N_("add a Signed-off-by trailer")),
1645 OPT_FILENAME('t', "template", &template_file
, N_("use specified template file")),
1646 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of commit")),
1647 OPT_CLEANUP(&cleanup_arg
),
1648 OPT_BOOL(0, "status", &include_status
, N_("include status in commit message template")),
1649 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
1650 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1651 /* end commit message options */
1653 OPT_GROUP(N_("Commit contents options")),
1654 OPT_BOOL('a', "all", &all
, N_("commit all changed files")),
1655 OPT_BOOL('i', "include", &also
, N_("add specified files to index for commit")),
1656 OPT_BOOL(0, "interactive", &interactive
, N_("interactively add files")),
1657 OPT_BOOL('p', "patch", &patch_interactive
, N_("interactively add changes")),
1658 OPT_BOOL('o', "only", &only
, N_("commit only specified files")),
1659 OPT_BOOL('n', "no-verify", &no_verify
, N_("bypass pre-commit and commit-msg hooks")),
1660 OPT_BOOL(0, "dry-run", &dry_run
, N_("show what would be committed")),
1661 OPT_SET_INT(0, "short", &status_format
, N_("show status concisely"),
1662 STATUS_FORMAT_SHORT
),
1663 OPT_BOOL(0, "branch", &s
.show_branch
, N_("show branch information")),
1664 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1665 N_("compute full ahead/behind values")),
1666 OPT_SET_INT(0, "porcelain", &status_format
,
1667 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN
),
1668 OPT_SET_INT(0, "long", &status_format
,
1669 N_("show status in long format (default)"),
1670 STATUS_FORMAT_LONG
),
1671 OPT_BOOL('z', "null", &s
.null_termination
,
1672 N_("terminate entries with NUL")),
1673 OPT_BOOL(0, "amend", &amend
, N_("amend previous commit")),
1674 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite
, N_("bypass post-rewrite hook")),
1675 { 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" },
1676 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1677 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1678 /* end commit contents options */
1680 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty
,
1681 N_("ok to record an empty change")),
1682 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message
,
1683 N_("ok to record a change with an empty message")),
1688 struct strbuf sb
= STRBUF_INIT
;
1689 struct strbuf author_ident
= STRBUF_INIT
;
1690 const char *index_file
, *reflog_msg
;
1691 struct object_id oid
;
1692 struct commit_list
*parents
= NULL
;
1693 struct stat statbuf
;
1694 struct commit
*current_head
= NULL
;
1695 struct commit_extra_header
*extra
= NULL
;
1696 struct strbuf err
= STRBUF_INIT
;
1699 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1700 usage_with_options(builtin_commit_usage
, builtin_commit_options
);
1702 prepare_repo_settings(the_repository
);
1703 the_repository
->settings
.command_requires_full_index
= 0;
1705 status_init_config(&s
, git_commit_config
);
1706 s
.commit_template
= 1;
1707 status_format
= STATUS_FORMAT_NONE
; /* Ignore status.short */
1710 if (get_oid("HEAD", &oid
))
1711 current_head
= NULL
;
1713 current_head
= lookup_commit_or_die(&oid
, "HEAD");
1714 if (parse_commit(current_head
))
1715 die(_("could not parse HEAD commit"));
1717 verbose
= -1; /* unspecified */
1718 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_options
,
1719 builtin_commit_usage
,
1720 prefix
, current_head
, &s
);
1722 verbose
= (config_commit_verbose
< 0) ? 0 : config_commit_verbose
;
1725 return dry_run_commit(argv
, prefix
, current_head
, &s
);
1726 index_file
= prepare_index(argv
, prefix
, current_head
, 0);
1728 /* Set up everything for writing the commit object. This includes
1729 running hooks, writing the trees, and interacting with the user. */
1730 if (!prepare_to_commit(index_file
, prefix
,
1731 current_head
, &s
, &author_ident
)) {
1733 rollback_index_files();
1737 /* Determine parents */
1738 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1739 if (!current_head
) {
1741 reflog_msg
= "commit (initial)";
1744 reflog_msg
= "commit (amend)";
1745 parents
= copy_commit_list(current_head
->parents
);
1746 } else if (whence
== FROM_MERGE
) {
1747 struct strbuf m
= STRBUF_INIT
;
1749 int allow_fast_forward
= 1;
1750 struct commit_list
**pptr
= &parents
;
1753 reflog_msg
= "commit (merge)";
1754 pptr
= commit_list_append(current_head
, pptr
);
1755 fp
= xfopen(git_path_merge_head(the_repository
), "r");
1756 while (strbuf_getline_lf(&m
, fp
) != EOF
) {
1757 struct commit
*parent
;
1759 parent
= get_merge_parent(m
.buf
);
1761 die(_("Corrupt MERGE_HEAD file (%s)"), m
.buf
);
1762 pptr
= commit_list_append(parent
, pptr
);
1766 if (!stat(git_path_merge_mode(the_repository
), &statbuf
)) {
1767 if (strbuf_read_file(&sb
, git_path_merge_mode(the_repository
), 0) < 0)
1768 die_errno(_("could not read MERGE_MODE"));
1769 if (!strcmp(sb
.buf
, "no-ff"))
1770 allow_fast_forward
= 0;
1772 if (allow_fast_forward
)
1773 reduce_heads_replace(&parents
);
1776 reflog_msg
= is_from_cherry_pick(whence
)
1777 ? "commit (cherry-pick)"
1778 : is_from_rebase(whence
)
1781 commit_list_insert(current_head
, &parents
);
1784 /* Finally, get the commit message */
1786 if (strbuf_read_file(&sb
, git_path_commit_editmsg(), 0) < 0) {
1787 int saved_errno
= errno
;
1788 rollback_index_files();
1789 die(_("could not read commit message: %s"), strerror(saved_errno
));
1792 cleanup_message(&sb
, cleanup_mode
, verbose
);
1794 if (message_is_empty(&sb
, cleanup_mode
) && !allow_empty_message
) {
1795 rollback_index_files();
1796 fprintf(stderr
, _("Aborting commit due to empty commit message.\n"));
1799 if (template_untouched(&sb
, template_file
, cleanup_mode
) && !allow_empty_message
) {
1800 rollback_index_files();
1801 fprintf(stderr
, _("Aborting commit; you did not edit the message.\n"));
1805 if (fixup_message
&& starts_with(sb
.buf
, "amend! ") &&
1806 !allow_empty_message
) {
1807 struct strbuf body
= STRBUF_INIT
;
1808 size_t len
= commit_subject_length(sb
.buf
);
1809 strbuf_addstr(&body
, sb
.buf
+ len
);
1810 if (message_is_empty(&body
, cleanup_mode
)) {
1811 rollback_index_files();
1812 fprintf(stderr
, _("Aborting commit due to empty commit message body.\n"));
1815 strbuf_release(&body
);
1819 const char *exclude_gpgsig
[3] = { "gpgsig", "gpgsig-sha256", NULL
};
1820 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1822 struct commit_extra_header
**tail
= &extra
;
1823 append_merge_tag_headers(parents
, &tail
);
1826 if (commit_tree_extended(sb
.buf
, sb
.len
, &active_cache_tree
->oid
,
1827 parents
, &oid
, author_ident
.buf
, NULL
,
1828 sign_commit
, extra
)) {
1829 rollback_index_files();
1830 die(_("failed to write commit object"));
1832 free_commit_extra_headers(extra
);
1834 if (update_head_with_reflog(current_head
, &oid
, reflog_msg
, &sb
,
1836 rollback_index_files();
1840 sequencer_post_commit_cleanup(the_repository
, 0);
1841 unlink(git_path_merge_head(the_repository
));
1842 unlink(git_path_merge_msg(the_repository
));
1843 unlink(git_path_merge_mode(the_repository
));
1844 unlink(git_path_squash_msg(the_repository
));
1846 if (commit_index_files())
1847 die(_("repository has been updated, but unable to write\n"
1848 "new_index file. Check that disk is not full and quota is\n"
1849 "not exceeded, and then \"git restore --staged :/\" to recover."));
1851 git_test_write_commit_graph_or_die();
1853 repo_rerere(the_repository
, 0);
1854 run_auto_maintenance(quiet
);
1855 run_commit_hook(use_editor
, get_index_file(), NULL
, "post-commit",
1857 if (amend
&& !no_post_rewrite
) {
1858 commit_post_rewrite(the_repository
, current_head
, &oid
);
1861 unsigned int flags
= 0;
1864 flags
|= SUMMARY_INITIAL_COMMIT
;
1865 if (author_date_is_interesting())
1866 flags
|= SUMMARY_SHOW_AUTHOR_DATE
;
1867 print_commit_summary(the_repository
, prefix
,
1871 apply_autostash(git_path_merge_autostash(the_repository
));
1874 UNLEAK(author_ident
);