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_VARIABLE
13 #include "cache-tree.h"
16 #include "environment.h"
23 #include "wt-status.h"
24 #include "run-command.h"
30 #include "object-name.h"
31 #include "parse-options.h"
32 #include "string-list.h"
34 #include "unpack-trees.h"
36 #include "submodule.h"
37 #include "gpg-interface.h"
39 #include "sequencer.h"
42 #include "commit-reach.h"
43 #include "commit-graph.h"
46 static const char * const builtin_commit_usage
[] = {
47 N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
48 " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>)]\n"
49 " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n"
50 " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n"
51 " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n"
52 " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
53 " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n"
54 " [--] [<pathspec>...]"),
58 static const char * const builtin_status_usage
[] = {
59 N_("git status [<options>] [--] [<pathspec>...]"),
63 static const char empty_amend_advice
[] =
64 N_("You asked to amend the most recent commit, but doing so would make\n"
65 "it empty. You can repeat your command with --allow-empty, or you can\n"
66 "remove the commit entirely with \"git reset HEAD^\".\n");
68 static const char empty_cherry_pick_advice
[] =
69 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
70 "If you wish to commit it anyway, use:\n"
72 " git commit --allow-empty\n"
75 static const char empty_rebase_pick_advice
[] =
76 N_("Otherwise, please use 'git rebase --skip'\n");
78 static const char empty_cherry_pick_advice_single
[] =
79 N_("Otherwise, please use 'git cherry-pick --skip'\n");
81 static const char empty_cherry_pick_advice_multi
[] =
84 " git cherry-pick --continue\n"
86 "to resume cherry-picking the remaining commits.\n"
87 "If you wish to skip this commit, use:\n"
89 " git cherry-pick --skip\n"
92 static const char *color_status_slots
[] = {
93 [WT_STATUS_HEADER
] = "header",
94 [WT_STATUS_UPDATED
] = "updated",
95 [WT_STATUS_CHANGED
] = "changed",
96 [WT_STATUS_UNTRACKED
] = "untracked",
97 [WT_STATUS_NOBRANCH
] = "noBranch",
98 [WT_STATUS_UNMERGED
] = "unmerged",
99 [WT_STATUS_LOCAL_BRANCH
] = "localBranch",
100 [WT_STATUS_REMOTE_BRANCH
] = "remoteBranch",
101 [WT_STATUS_ONBRANCH
] = "branch",
104 static const char *use_message_buffer
;
105 static struct lock_file index_lock
; /* real index */
106 static struct lock_file false_lock
; /* used only for partial commits */
113 static const char *logfile
, *force_author
;
114 static const char *template_file
;
116 * The _message variables are commit names from which to take
117 * the commit message and/or authorship.
119 static const char *author_message
, *author_message_buffer
;
120 static char *edit_message
, *use_message
;
121 static char *fixup_message
, *fixup_commit
, *squash_message
;
122 static const char *fixup_prefix
;
123 static int all
, also
, interactive
, patch_interactive
, only
, amend
, signoff
;
124 static int edit_flag
= -1; /* unspecified */
125 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
126 static int config_commit_verbose
= -1; /* unspecified */
127 static int no_post_rewrite
, allow_empty_message
, pathspec_file_nul
;
128 static char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
, *ignored_arg
;
129 static char *sign_commit
, *pathspec_from_file
;
130 static struct strvec trailer_args
= STRVEC_INIT
;
133 * The default commit message cleanup mode will remove the lines
134 * beginning with # (shell comments) and leading and trailing
135 * whitespaces (empty lines or containing only whitespaces)
136 * if editor is used, and only the whitespaces if the message
137 * is specified explicitly.
139 static enum commit_msg_cleanup_mode cleanup_mode
;
140 static const char *cleanup_arg
;
142 static enum commit_whence whence
;
143 static int use_editor
= 1, include_status
= 1;
144 static int have_option_m
;
145 static struct strbuf message
= STRBUF_INIT
;
147 static enum wt_status_format status_format
= STATUS_FORMAT_UNSPECIFIED
;
149 static int opt_pass_trailer(const struct option
*opt
, const char *arg
, int unset
)
151 BUG_ON_OPT_NEG(unset
);
153 strvec_pushl(opt
->value
, "--trailer", arg
, NULL
);
157 static int opt_parse_porcelain(const struct option
*opt
, const char *arg
, int unset
)
159 enum wt_status_format
*value
= (enum wt_status_format
*)opt
->value
;
161 *value
= STATUS_FORMAT_NONE
;
163 *value
= STATUS_FORMAT_PORCELAIN
;
164 else if (!strcmp(arg
, "v1") || !strcmp(arg
, "1"))
165 *value
= STATUS_FORMAT_PORCELAIN
;
166 else if (!strcmp(arg
, "v2") || !strcmp(arg
, "2"))
167 *value
= STATUS_FORMAT_PORCELAIN_V2
;
169 die("unsupported porcelain version '%s'", arg
);
174 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
176 struct strbuf
*buf
= opt
->value
;
179 strbuf_setlen(buf
, 0);
183 strbuf_addch(buf
, '\n');
184 strbuf_addstr(buf
, arg
);
185 strbuf_complete_line(buf
);
190 static int opt_parse_rename_score(const struct option
*opt
, const char *arg
, int unset
)
192 const char **value
= opt
->value
;
194 BUG_ON_OPT_NEG(unset
);
196 if (arg
!= NULL
&& *arg
== '=')
203 static void determine_whence(struct wt_status
*s
)
205 if (file_exists(git_path_merge_head(the_repository
)))
207 else if (!sequencer_determine_whence(the_repository
, &whence
))
208 whence
= FROM_COMMIT
;
213 static void status_init_config(struct wt_status
*s
, config_fn_t fn
)
215 wt_status_prepare(the_repository
, s
);
216 init_diff_ui_defaults();
219 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
); /* must come after git_config() */
222 static void rollback_index_files(void)
224 switch (commit_style
) {
226 break; /* nothing to do */
228 rollback_lock_file(&index_lock
);
231 rollback_lock_file(&index_lock
);
232 rollback_lock_file(&false_lock
);
237 static int commit_index_files(void)
241 switch (commit_style
) {
243 break; /* nothing to do */
245 err
= commit_lock_file(&index_lock
);
248 err
= commit_lock_file(&index_lock
);
249 rollback_lock_file(&false_lock
);
257 * Take a union of paths in the index and the named tree (typically, "HEAD"),
258 * and return the paths that match the given pattern in list.
260 static int list_paths(struct string_list
*list
, const char *with_tree
,
261 const struct pathspec
*pattern
)
269 m
= xcalloc(1, pattern
->nr
);
272 char *max_prefix
= common_prefix(pattern
);
273 overlay_tree_on_index(&the_index
, with_tree
, max_prefix
);
277 /* TODO: audit for interaction with sparse-index. */
278 ensure_full_index(&the_index
);
279 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
280 const struct cache_entry
*ce
= the_index
.cache
[i
];
281 struct string_list_item
*item
;
283 if (ce
->ce_flags
& CE_UPDATE
)
285 if (!ce_path_match(&the_index
, ce
, pattern
, m
))
287 item
= string_list_insert(list
, ce
->name
);
288 if (ce_skip_worktree(ce
))
289 item
->util
= item
; /* better a valid pointer than a fake one */
292 ret
= report_path_error(m
, pattern
);
297 static void add_remove_files(struct string_list
*list
)
300 for (i
= 0; i
< list
->nr
; i
++) {
302 struct string_list_item
*p
= &(list
->items
[i
]);
304 /* p->util is skip-worktree */
308 if (!lstat(p
->string
, &st
)) {
309 if (add_to_index(&the_index
, p
->string
, &st
, 0))
310 die(_("updating files failed"));
312 remove_file_from_index(&the_index
, p
->string
);
316 static void create_base_index(const struct commit
*current_head
)
319 struct unpack_trees_options opts
;
323 discard_index(&the_index
);
327 memset(&opts
, 0, sizeof(opts
));
331 opts
.src_index
= &the_index
;
332 opts
.dst_index
= &the_index
;
334 opts
.fn
= oneway_merge
;
335 tree
= parse_tree_indirect(¤t_head
->object
.oid
);
337 die(_("failed to unpack HEAD tree object"));
339 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
340 if (unpack_trees(1, &t
, &opts
))
341 exit(128); /* We've already reported the error, finish dying */
344 static void refresh_cache_or_die(int refresh_flags
)
347 * refresh_flags contains REFRESH_QUIET, so the only errors
348 * are for unmerged entries.
350 if (refresh_index(&the_index
, refresh_flags
| REFRESH_IN_PORCELAIN
, NULL
, NULL
, NULL
))
351 die_resolve_conflict("commit");
354 static const char *prepare_index(const char **argv
, const char *prefix
,
355 const struct commit
*current_head
, int is_status
)
357 struct string_list partial
= STRING_LIST_INIT_DUP
;
358 struct pathspec pathspec
;
359 int refresh_flags
= REFRESH_QUIET
;
363 refresh_flags
|= REFRESH_UNMERGED
;
364 parse_pathspec(&pathspec
, 0,
365 PATHSPEC_PREFER_FULL
,
368 if (pathspec_from_file
) {
370 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
373 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
376 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
378 parse_pathspec_file(&pathspec
, 0,
379 PATHSPEC_PREFER_FULL
,
380 prefix
, pathspec_from_file
, pathspec_file_nul
);
381 } else if (pathspec_file_nul
) {
382 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
385 if (!pathspec
.nr
&& (also
|| (only
&& !allow_empty
&&
386 (!amend
|| (fixup_message
&& strcmp(fixup_prefix
, "amend"))))))
387 die(_("No paths with --include/--only does not make sense."));
389 if (repo_read_index_preload(the_repository
, &pathspec
, 0) < 0)
390 die(_("index file corrupt"));
393 char *old_index_env
= NULL
, *old_repo_index_file
;
394 repo_hold_locked_index(the_repository
, &index_lock
,
397 refresh_cache_or_die(refresh_flags
);
399 if (write_locked_index(&the_index
, &index_lock
, 0))
400 die(_("unable to create temporary index"));
402 old_repo_index_file
= the_repository
->index_file
;
403 the_repository
->index_file
=
404 (char *)get_lock_file_path(&index_lock
);
405 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
406 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
408 if (interactive_add(argv
, prefix
, patch_interactive
) != 0)
409 die(_("interactive add failed"));
411 the_repository
->index_file
= old_repo_index_file
;
412 if (old_index_env
&& *old_index_env
)
413 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
415 unsetenv(INDEX_ENVIRONMENT
);
416 FREE_AND_NULL(old_index_env
);
418 discard_index(&the_index
);
419 read_index_from(&the_index
, get_lock_file_path(&index_lock
),
421 if (cache_tree_update(&the_index
, WRITE_TREE_SILENT
) == 0) {
422 if (reopen_lock_file(&index_lock
) < 0)
423 die(_("unable to write index file"));
424 if (write_locked_index(&the_index
, &index_lock
, 0))
425 die(_("unable to update temporary index"));
427 warning(_("Failed to update main cache tree"));
429 commit_style
= COMMIT_NORMAL
;
430 ret
= get_lock_file_path(&index_lock
);
435 * Non partial, non as-is commit.
437 * (1) get the real index;
438 * (2) update the_index as necessary;
439 * (3) write the_index out to the real index (still locked);
440 * (4) return the name of the locked index file.
442 * The caller should run hooks on the locked real index, and
443 * (A) if all goes well, commit the real index;
444 * (B) on failure, rollback the real index.
446 if (all
|| (also
&& pathspec
.nr
)) {
447 repo_hold_locked_index(the_repository
, &index_lock
,
449 add_files_to_cache(also
? prefix
: NULL
, &pathspec
, 0);
450 refresh_cache_or_die(refresh_flags
);
451 cache_tree_update(&the_index
, WRITE_TREE_SILENT
);
452 if (write_locked_index(&the_index
, &index_lock
, 0))
453 die(_("unable to write new_index file"));
454 commit_style
= COMMIT_NORMAL
;
455 ret
= get_lock_file_path(&index_lock
);
462 * (1) return the name of the real index file.
464 * The caller should run hooks on the real index,
465 * and create commit from the_index.
466 * We still need to refresh the index here.
468 if (!only
&& !pathspec
.nr
) {
469 repo_hold_locked_index(the_repository
, &index_lock
,
471 refresh_cache_or_die(refresh_flags
);
472 if (the_index
.cache_changed
473 || !cache_tree_fully_valid(the_index
.cache_tree
))
474 cache_tree_update(&the_index
, WRITE_TREE_SILENT
);
475 if (write_locked_index(&the_index
, &index_lock
,
476 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
477 die(_("unable to write new_index file"));
478 commit_style
= COMMIT_AS_IS
;
479 ret
= get_index_file();
486 * (0) find the set of affected paths;
487 * (1) get lock on the real index file;
488 * (2) update the_index with the given paths;
489 * (3) write the_index out to the real index (still locked);
490 * (4) get lock on the false index file;
491 * (5) reset the_index from HEAD;
492 * (6) update the_index the same way as (2);
493 * (7) write the_index out to the false index file;
494 * (8) return the name of the false index file (still locked);
496 * The caller should run hooks on the locked false index, and
497 * create commit from it. Then
498 * (A) if all goes well, commit the real index;
499 * (B) on failure, rollback the real index;
500 * In either case, rollback the false index.
502 commit_style
= COMMIT_PARTIAL
;
504 if (whence
!= FROM_COMMIT
) {
505 if (whence
== FROM_MERGE
)
506 die(_("cannot do a partial commit during a merge."));
507 else if (is_from_cherry_pick(whence
))
508 die(_("cannot do a partial commit during a cherry-pick."));
509 else if (is_from_rebase(whence
))
510 die(_("cannot do a partial commit during a rebase."));
513 if (list_paths(&partial
, !current_head
? NULL
: "HEAD", &pathspec
))
516 discard_index(&the_index
);
517 if (repo_read_index(the_repository
) < 0)
518 die(_("cannot read the index"));
520 repo_hold_locked_index(the_repository
, &index_lock
, LOCK_DIE_ON_ERROR
);
521 add_remove_files(&partial
);
522 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
523 cache_tree_update(&the_index
, WRITE_TREE_SILENT
);
524 if (write_locked_index(&the_index
, &index_lock
, 0))
525 die(_("unable to write new_index file"));
527 hold_lock_file_for_update(&false_lock
,
528 git_path("next-index-%"PRIuMAX
,
529 (uintmax_t) getpid()),
532 create_base_index(current_head
);
533 add_remove_files(&partial
);
534 refresh_index(&the_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
536 if (write_locked_index(&the_index
, &false_lock
, 0))
537 die(_("unable to write temporary index file"));
539 discard_index(&the_index
);
540 ret
= get_lock_file_path(&false_lock
);
541 read_index_from(&the_index
, ret
, get_git_dir());
543 string_list_clear(&partial
, 0);
544 clear_pathspec(&pathspec
);
548 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
551 struct object_id oid
;
553 if (s
->relative_paths
)
558 s
->reference
= "HEAD^1";
560 s
->verbose
= verbose
;
561 s
->index_file
= index_file
;
564 s
->is_initial
= repo_get_oid(the_repository
, s
->reference
, &oid
) ? 1 : 0;
566 oidcpy(&s
->oid_commit
, &oid
);
567 s
->status_format
= status_format
;
568 s
->ignore_submodule_arg
= ignore_submodule_arg
;
570 wt_status_collect(s
);
572 wt_status_collect_free_buffers(s
);
574 return s
->committable
;
577 static int is_a_merge(const struct commit
*current_head
)
579 return !!(current_head
->parents
&& current_head
->parents
->next
);
582 static void assert_split_ident(struct ident_split
*id
, const struct strbuf
*buf
)
584 if (split_ident_line(id
, buf
->buf
, buf
->len
) || !id
->date_begin
)
585 BUG("unable to parse our own ident: %s", buf
->buf
);
588 static void export_one(const char *var
, const char *s
, const char *e
, int hack
)
590 struct strbuf buf
= STRBUF_INIT
;
592 strbuf_addch(&buf
, hack
);
593 strbuf_add(&buf
, s
, e
- s
);
594 setenv(var
, buf
.buf
, 1);
595 strbuf_release(&buf
);
598 static int parse_force_date(const char *in
, struct strbuf
*out
)
600 strbuf_addch(out
, '@');
602 if (parse_date(in
, out
) < 0) {
604 unsigned long t
= approxidate_careful(in
, &errors
);
607 strbuf_addf(out
, "%lu", t
);
613 static void set_ident_var(char **buf
, char *val
)
619 static void determine_author_info(struct strbuf
*author_ident
)
621 char *name
, *email
, *date
;
622 struct ident_split author
;
624 name
= xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
625 email
= xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
626 date
= xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
628 if (author_message
) {
629 struct ident_split ident
;
633 a
= find_commit_header(author_message_buffer
, "author", &len
);
635 die(_("commit '%s' lacks author header"), author_message
);
636 if (split_ident_line(&ident
, a
, len
) < 0)
637 die(_("commit '%s' has malformed author line"), author_message
);
639 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
640 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
642 if (ident
.date_begin
) {
643 struct strbuf date_buf
= STRBUF_INIT
;
644 strbuf_addch(&date_buf
, '@');
645 strbuf_add(&date_buf
, ident
.date_begin
, ident
.date_end
- ident
.date_begin
);
646 strbuf_addch(&date_buf
, ' ');
647 strbuf_add(&date_buf
, ident
.tz_begin
, ident
.tz_end
- ident
.tz_begin
);
648 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
653 struct ident_split ident
;
655 if (split_ident_line(&ident
, force_author
, strlen(force_author
)) < 0)
656 die(_("malformed --author parameter"));
657 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
658 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
662 struct strbuf date_buf
= STRBUF_INIT
;
663 if (parse_force_date(force_date
, &date_buf
))
664 die(_("invalid date format: %s"), force_date
);
665 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
668 strbuf_addstr(author_ident
, fmt_ident(name
, email
, WANT_AUTHOR_IDENT
, date
,
670 assert_split_ident(&author
, author_ident
);
671 export_one("GIT_AUTHOR_NAME", author
.name_begin
, author
.name_end
, 0);
672 export_one("GIT_AUTHOR_EMAIL", author
.mail_begin
, author
.mail_end
, 0);
673 export_one("GIT_AUTHOR_DATE", author
.date_begin
, author
.tz_end
, '@');
679 static int author_date_is_interesting(void)
681 return author_message
|| force_date
;
684 static void adjust_comment_line_char(const struct strbuf
*sb
)
686 char candidates
[] = "#;@!$%^&|:";
690 comment_line_char
= candidates
[0];
691 if (!memchr(sb
->buf
, comment_line_char
, sb
->len
))
695 candidate
= strchr(candidates
, *p
);
698 for (p
= sb
->buf
; *p
; p
++) {
699 if ((p
[0] == '\n' || p
[0] == '\r') && p
[1]) {
700 candidate
= strchr(candidates
, p
[1]);
706 for (p
= candidates
; *p
== ' '; p
++)
709 die(_("unable to select a comment character that is not used\n"
710 "in the current commit message"));
711 comment_line_char
= *p
;
714 static void prepare_amend_commit(struct commit
*commit
, struct strbuf
*sb
,
715 struct pretty_print_context
*ctx
)
717 const char *buffer
, *subject
, *fmt
;
719 buffer
= repo_get_commit_buffer(the_repository
, commit
, NULL
);
720 find_commit_subject(buffer
, &subject
);
722 * If we amend the 'amend!' commit then we don't want to
723 * duplicate the subject line.
725 fmt
= starts_with(subject
, "amend!") ? "%b" : "%B";
726 repo_format_commit_message(the_repository
, commit
, fmt
, sb
, ctx
);
727 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
730 static int prepare_to_commit(const char *index_file
, const char *prefix
,
731 struct commit
*current_head
,
733 struct strbuf
*author_ident
)
736 struct strbuf committer_ident
= STRBUF_INIT
;
738 struct strbuf sb
= STRBUF_INIT
;
739 const char *hook_arg1
= NULL
;
740 const char *hook_arg2
= NULL
;
741 int clean_message_contents
= (cleanup_mode
!= COMMIT_MSG_CLEANUP_NONE
);
742 int old_display_comment_prefix
;
743 int merge_contains_scissors
= 0;
746 /* This checks and barfs if author is badly specified */
747 determine_author_info(author_ident
);
749 if (!no_verify
&& run_commit_hook(use_editor
, index_file
, &invoked_hook
,
753 if (squash_message
) {
755 * Insert the proper subject line before other commit
756 * message options add their content.
758 if (use_message
&& !strcmp(use_message
, squash_message
))
759 strbuf_addstr(&sb
, "squash! ");
761 struct pretty_print_context ctx
= {0};
763 c
= lookup_commit_reference_by_name(squash_message
);
765 die(_("could not lookup commit %s"), squash_message
);
766 ctx
.output_encoding
= get_commit_output_encoding();
767 repo_format_commit_message(the_repository
, c
,
768 "squash! %s\n\n", &sb
,
773 if (have_option_m
&& !fixup_message
) {
774 strbuf_addbuf(&sb
, &message
);
775 hook_arg1
= "message";
776 } else if (logfile
&& !strcmp(logfile
, "-")) {
778 fprintf(stderr
, _("(reading log message from standard input)\n"));
779 if (strbuf_read(&sb
, 0, 0) < 0)
780 die_errno(_("could not read log from standard input"));
781 hook_arg1
= "message";
782 } else if (logfile
) {
783 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
784 die_errno(_("could not read log file '%s'"),
786 hook_arg1
= "message";
787 } else if (use_message
) {
789 buffer
= strstr(use_message_buffer
, "\n\n");
791 strbuf_addstr(&sb
, skip_blank_lines(buffer
+ 2));
792 hook_arg1
= "commit";
793 hook_arg2
= use_message
;
794 } else if (fixup_message
) {
795 struct pretty_print_context ctx
= {0};
796 struct commit
*commit
;
798 commit
= lookup_commit_reference_by_name(fixup_commit
);
800 die(_("could not lookup commit %s"), fixup_commit
);
801 ctx
.output_encoding
= get_commit_output_encoding();
802 fmt
= xstrfmt("%s! %%s\n\n", fixup_prefix
);
803 repo_format_commit_message(the_repository
, commit
, fmt
, &sb
,
806 hook_arg1
= "message";
809 * Only `-m` commit message option is checked here, as
810 * it supports `--fixup` to append the commit message.
812 * The other commit message options `-c`/`-C`/`-F` are
813 * incompatible with all the forms of `--fixup` and
814 * have already errored out while parsing the `git commit`
817 if (have_option_m
&& !strcmp(fixup_prefix
, "fixup"))
818 strbuf_addbuf(&sb
, &message
);
820 if (!strcmp(fixup_prefix
, "amend")) {
822 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message
);
823 prepare_amend_commit(commit
, &sb
, &ctx
);
825 } else if (!stat(git_path_merge_msg(the_repository
), &statbuf
)) {
826 size_t merge_msg_start
;
829 * prepend SQUASH_MSG here if it exists and a
830 * "merge --squash" was originally performed
832 if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
833 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
834 die_errno(_("could not read SQUASH_MSG"));
835 hook_arg1
= "squash";
839 merge_msg_start
= sb
.len
;
840 if (strbuf_read_file(&sb
, git_path_merge_msg(the_repository
), 0) < 0)
841 die_errno(_("could not read MERGE_MSG"));
843 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
&&
844 wt_status_locate_end(sb
.buf
+ merge_msg_start
,
845 sb
.len
- merge_msg_start
) <
846 sb
.len
- merge_msg_start
)
847 merge_contains_scissors
= 1;
848 } else if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
849 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
850 die_errno(_("could not read SQUASH_MSG"));
851 hook_arg1
= "squash";
852 } else if (template_file
) {
853 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
854 die_errno(_("could not read '%s'"), template_file
);
855 hook_arg1
= "template";
856 clean_message_contents
= 0;
860 * The remaining cases don't modify the template message, but
861 * just set the argument(s) to the prepare-commit-msg hook.
863 else if (whence
== FROM_MERGE
)
865 else if (is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) {
866 hook_arg1
= "commit";
867 hook_arg2
= "CHERRY_PICK_HEAD";
870 if (squash_message
) {
872 * If squash_commit was used for the commit subject,
873 * then we're possibly hijacking other commit log options.
874 * Reset the hook args to tell the real story.
876 hook_arg1
= "message";
880 s
->fp
= fopen_for_writing(git_path_commit_editmsg());
882 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
884 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
885 old_display_comment_prefix
= s
->display_comment_prefix
;
886 s
->display_comment_prefix
= 1;
889 * Most hints are counter-productive when the commit has
894 if (clean_message_contents
)
895 strbuf_stripspace(&sb
, 0);
898 append_signoff(&sb
, ignore_non_trailer(sb
.buf
, sb
.len
), 0);
900 if (fwrite(sb
.buf
, 1, sb
.len
, s
->fp
) < sb
.len
)
901 die_errno(_("could not write commit template"));
903 if (auto_comment_line_char
)
904 adjust_comment_line_char(&sb
);
907 /* This checks if committer ident is explicitly given */
908 strbuf_addstr(&committer_ident
, git_committer_info(IDENT_STRICT
));
909 if (use_editor
&& include_status
) {
911 int saved_color_setting
;
912 struct ident_split ci
, ai
;
913 const char *hint_cleanup_all
= allow_empty_message
?
914 _("Please enter the commit message for your changes."
915 " Lines starting\nwith '%c' will be ignored.\n") :
916 _("Please enter the commit message for your changes."
917 " Lines starting\nwith '%c' will be ignored, and an empty"
918 " message aborts the commit.\n");
919 const char *hint_cleanup_space
= allow_empty_message
?
920 _("Please enter the commit message for your changes."
922 "with '%c' will be kept; you may remove them"
923 " yourself if you want to.\n") :
924 _("Please enter the commit message for your changes."
926 "with '%c' will be kept; you may remove them"
927 " yourself if you want to.\n"
928 "An empty message aborts the commit.\n");
929 if (whence
!= FROM_COMMIT
) {
930 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
&&
931 !merge_contains_scissors
)
932 wt_status_add_cut_line(s
->fp
);
935 whence
== FROM_MERGE
?
937 "It looks like you may be committing a merge.\n"
938 "If this is not correct, please run\n"
939 " git update-ref -d MERGE_HEAD\n"
940 "and try again.\n") :
942 "It looks like you may be committing a cherry-pick.\n"
943 "If this is not correct, please run\n"
944 " git update-ref -d CHERRY_PICK_HEAD\n"
945 "and try again.\n"));
948 fprintf(s
->fp
, "\n");
949 if (cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
)
950 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_all
, comment_line_char
);
951 else if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
952 if (whence
== FROM_COMMIT
&& !merge_contains_scissors
)
953 wt_status_add_cut_line(s
->fp
);
954 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
955 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_space
, comment_line_char
);
958 * These should never fail because they come from our own
959 * fmt_ident. They may fail the sane_ident test, but we know
960 * that the name and mail pointers will at least be valid,
961 * which is enough for our tests and printing here.
963 assert_split_ident(&ai
, author_ident
);
964 assert_split_ident(&ci
, &committer_ident
);
966 if (ident_cmp(&ai
, &ci
))
967 status_printf_ln(s
, GIT_COLOR_NORMAL
,
969 "Author: %.*s <%.*s>"),
970 ident_shown
++ ? "" : "\n",
971 (int)(ai
.name_end
- ai
.name_begin
), ai
.name_begin
,
972 (int)(ai
.mail_end
- ai
.mail_begin
), ai
.mail_begin
);
974 if (author_date_is_interesting())
975 status_printf_ln(s
, GIT_COLOR_NORMAL
,
978 ident_shown
++ ? "" : "\n",
979 show_ident_date(&ai
, DATE_MODE(NORMAL
)));
981 if (!committer_ident_sufficiently_given())
982 status_printf_ln(s
, GIT_COLOR_NORMAL
,
984 "Committer: %.*s <%.*s>"),
985 ident_shown
++ ? "" : "\n",
986 (int)(ci
.name_end
- ci
.name_begin
), ci
.name_begin
,
987 (int)(ci
.mail_end
- ci
.mail_begin
), ci
.mail_begin
);
989 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", ""); /* Add new line for clarity */
991 saved_color_setting
= s
->use_color
;
993 committable
= run_status(s
->fp
, index_file
, prefix
, 1, s
);
994 s
->use_color
= saved_color_setting
;
995 string_list_clear(&s
->change
, 1);
997 struct object_id oid
;
998 const char *parent
= "HEAD";
1000 if (!the_index
.cache_nr
) {
1001 discard_index(&the_index
);
1002 if (repo_read_index(the_repository
) < 0)
1003 die(_("Cannot read index"));
1009 if (repo_get_oid(the_repository
, parent
, &oid
)) {
1012 /* TODO: audit for interaction with sparse-index. */
1013 ensure_full_index(&the_index
);
1014 for (i
= 0; i
< the_index
.cache_nr
; i
++)
1015 if (ce_intent_to_add(the_index
.cache
[i
]))
1017 committable
= the_index
.cache_nr
- ita_nr
> 0;
1020 * Unless the user did explicitly request a submodule
1021 * ignore mode by passing a command line option we do
1022 * not ignore any changed submodule SHA-1s when
1023 * comparing index and parent, no matter what is
1024 * configured. Otherwise we won't commit any
1025 * submodules which were manually staged, which would
1026 * be really confusing.
1028 struct diff_flags flags
= DIFF_FLAGS_INIT
;
1029 flags
.override_submodule_config
= 1;
1030 if (ignore_submodule_arg
&&
1031 !strcmp(ignore_submodule_arg
, "all"))
1032 flags
.ignore_submodules
= 1;
1033 committable
= index_differs_from(the_repository
,
1037 strbuf_release(&committer_ident
);
1041 if (trailer_args
.nr
) {
1042 struct child_process run_trailer
= CHILD_PROCESS_INIT
;
1044 strvec_pushl(&run_trailer
.args
, "interpret-trailers",
1045 "--in-place", git_path_commit_editmsg(), NULL
);
1046 strvec_pushv(&run_trailer
.args
, trailer_args
.v
);
1047 run_trailer
.git_cmd
= 1;
1048 if (run_command(&run_trailer
))
1049 die(_("unable to pass trailers to --trailers"));
1050 strvec_clear(&trailer_args
);
1054 * Reject an attempt to record a non-merge empty commit without
1055 * explicit --allow-empty. In the cherry-pick case, it may be
1056 * empty due to conflict resolution, which the user should okay.
1058 if (!committable
&& whence
!= FROM_MERGE
&& !allow_empty
&&
1059 !(amend
&& is_a_merge(current_head
))) {
1060 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
);
1061 s
->display_comment_prefix
= old_display_comment_prefix
;
1062 run_status(stdout
, index_file
, prefix
, 0, s
);
1064 fputs(_(empty_amend_advice
), stderr
);
1065 else if (is_from_cherry_pick(whence
) ||
1066 whence
== FROM_REBASE_PICK
) {
1067 fputs(_(empty_cherry_pick_advice
), stderr
);
1068 if (whence
== FROM_CHERRY_PICK_SINGLE
)
1069 fputs(_(empty_cherry_pick_advice_single
), stderr
);
1070 else if (whence
== FROM_CHERRY_PICK_MULTI
)
1071 fputs(_(empty_cherry_pick_advice_multi
), stderr
);
1073 fputs(_(empty_rebase_pick_advice
), stderr
);
1078 if (!no_verify
&& invoked_hook
) {
1080 * Re-read the index as the pre-commit-commit hook was invoked
1081 * and could have updated it. We must do this before we invoke
1082 * the editor and after we invoke run_status above.
1084 discard_index(&the_index
);
1086 read_index_from(&the_index
, index_file
, get_git_dir());
1088 if (cache_tree_update(&the_index
, 0)) {
1089 error(_("Error building trees"));
1093 if (run_commit_hook(use_editor
, index_file
, NULL
, "prepare-commit-msg",
1094 git_path_commit_editmsg(), hook_arg1
, hook_arg2
, NULL
))
1098 struct strvec env
= STRVEC_INIT
;
1100 strvec_pushf(&env
, "GIT_INDEX_FILE=%s", index_file
);
1101 if (launch_editor(git_path_commit_editmsg(), NULL
, env
.v
)) {
1103 _("Please supply the message using either -m or -F option.\n"));
1110 run_commit_hook(use_editor
, index_file
, NULL
, "commit-msg",
1111 git_path_commit_editmsg(), NULL
)) {
1118 static const char *find_author_by_nickname(const char *name
)
1120 struct rev_info revs
;
1121 struct commit
*commit
;
1122 struct strbuf buf
= STRBUF_INIT
;
1126 repo_init_revisions(the_repository
, &revs
, NULL
);
1127 strbuf_addf(&buf
, "--author=%s", name
);
1132 setup_revisions(ac
, av
, &revs
, NULL
);
1133 revs
.mailmap
= xmalloc(sizeof(struct string_list
));
1134 string_list_init_nodup(revs
.mailmap
);
1135 read_mailmap(revs
.mailmap
);
1137 if (prepare_revision_walk(&revs
))
1138 die(_("revision walk setup failed"));
1139 commit
= get_revision(&revs
);
1141 struct pretty_print_context ctx
= {0};
1142 ctx
.date_mode
.type
= DATE_NORMAL
;
1143 strbuf_release(&buf
);
1144 repo_format_commit_message(the_repository
, commit
,
1145 "%aN <%aE>", &buf
, &ctx
);
1146 release_revisions(&revs
);
1147 return strbuf_detach(&buf
, NULL
);
1149 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name
);
1152 static void handle_ignored_arg(struct wt_status
*s
)
1155 ; /* default already initialized */
1156 else if (!strcmp(ignored_arg
, "traditional"))
1157 s
->show_ignored_mode
= SHOW_TRADITIONAL_IGNORED
;
1158 else if (!strcmp(ignored_arg
, "no"))
1159 s
->show_ignored_mode
= SHOW_NO_IGNORED
;
1160 else if (!strcmp(ignored_arg
, "matching"))
1161 s
->show_ignored_mode
= SHOW_MATCHING_IGNORED
;
1163 die(_("Invalid ignored mode '%s'"), ignored_arg
);
1166 static void handle_untracked_files_arg(struct wt_status
*s
)
1168 if (!untracked_files_arg
)
1169 ; /* default already initialized */
1170 else if (!strcmp(untracked_files_arg
, "no"))
1171 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1172 else if (!strcmp(untracked_files_arg
, "normal"))
1173 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1174 else if (!strcmp(untracked_files_arg
, "all"))
1175 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1177 * Please update $__git_untracked_file_modes in
1178 * git-completion.bash when you add new options
1181 die(_("Invalid untracked files mode '%s'"), untracked_files_arg
);
1184 static const char *read_commit_message(const char *name
)
1186 const char *out_enc
;
1187 struct commit
*commit
;
1189 commit
= lookup_commit_reference_by_name(name
);
1191 die(_("could not lookup commit %s"), name
);
1192 out_enc
= get_commit_output_encoding();
1193 return repo_logmsg_reencode(the_repository
, commit
, NULL
, out_enc
);
1197 * Enumerate what needs to be propagated when --porcelain
1198 * is not in effect here.
1200 static struct status_deferred_config
{
1201 enum wt_status_format status_format
;
1203 enum ahead_behind_flags ahead_behind
;
1204 } status_deferred_config
= {
1205 STATUS_FORMAT_UNSPECIFIED
,
1206 -1, /* unspecified */
1207 AHEAD_BEHIND_UNSPECIFIED
,
1210 static void finalize_deferred_config(struct wt_status
*s
)
1212 int use_deferred_config
= (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1213 status_format
!= STATUS_FORMAT_PORCELAIN_V2
&&
1214 !s
->null_termination
);
1216 if (s
->null_termination
) {
1217 if (status_format
== STATUS_FORMAT_NONE
||
1218 status_format
== STATUS_FORMAT_UNSPECIFIED
)
1219 status_format
= STATUS_FORMAT_PORCELAIN
;
1220 else if (status_format
== STATUS_FORMAT_LONG
)
1221 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
1224 if (use_deferred_config
&& status_format
== STATUS_FORMAT_UNSPECIFIED
)
1225 status_format
= status_deferred_config
.status_format
;
1226 if (status_format
== STATUS_FORMAT_UNSPECIFIED
)
1227 status_format
= STATUS_FORMAT_NONE
;
1229 if (use_deferred_config
&& s
->show_branch
< 0)
1230 s
->show_branch
= status_deferred_config
.show_branch
;
1231 if (s
->show_branch
< 0)
1235 * If the user did not give a "--[no]-ahead-behind" command
1236 * line argument *AND* we will print in a human-readable format
1237 * (short, long etc.) then we inherit from the status.aheadbehind
1238 * config setting. In all other cases (and porcelain V[12] formats
1239 * in particular), we inherit _FULL for backwards compatibility.
1241 if (use_deferred_config
&&
1242 s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1243 s
->ahead_behind_flags
= status_deferred_config
.ahead_behind
;
1245 if (s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1246 s
->ahead_behind_flags
= AHEAD_BEHIND_FULL
;
1249 static void check_fixup_reword_options(int argc
, const char *argv
[]) {
1250 if (whence
!= FROM_COMMIT
) {
1251 if (whence
== FROM_MERGE
)
1252 die(_("You are in the middle of a merge -- cannot reword."));
1253 else if (is_from_cherry_pick(whence
))
1254 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1257 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv
);
1258 if (patch_interactive
|| interactive
|| all
|| also
|| only
)
1259 die(_("reword option of '%s' and '%s' cannot be used together"),
1260 "--fixup", "--patch/--interactive/--all/--include/--only");
1263 static int parse_and_validate_options(int argc
, const char *argv
[],
1264 const struct option
*options
,
1265 const char * const usage
[],
1267 struct commit
*current_head
,
1268 struct wt_status
*s
)
1270 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1271 finalize_deferred_config(s
);
1273 if (force_author
&& !strchr(force_author
, '>'))
1274 force_author
= find_author_by_nickname(force_author
);
1276 if (force_author
&& renew_authorship
)
1277 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
1279 if (logfile
|| have_option_m
|| use_message
)
1282 /* Sanity check options */
1283 if (amend
&& !current_head
)
1284 die(_("You have nothing to amend."));
1285 if (amend
&& whence
!= FROM_COMMIT
) {
1286 if (whence
== FROM_MERGE
)
1287 die(_("You are in the middle of a merge -- cannot amend."));
1288 else if (is_from_cherry_pick(whence
))
1289 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1290 else if (whence
== FROM_REBASE_PICK
)
1291 die(_("You are in the middle of a rebase -- cannot amend."));
1293 if (fixup_message
&& squash_message
)
1294 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1295 die_for_incompatible_opt4(!!use_message
, "-C",
1296 !!edit_message
, "-c",
1298 !!fixup_message
, "--fixup");
1299 die_for_incompatible_opt4(have_option_m
, "-m",
1300 !!edit_message
, "-c",
1301 !!use_message
, "-C",
1303 if (use_message
|| edit_message
|| logfile
||fixup_message
|| have_option_m
)
1304 template_file
= NULL
;
1306 use_message
= edit_message
;
1307 if (amend
&& !use_message
&& !fixup_message
)
1308 use_message
= "HEAD";
1309 if (!use_message
&& !is_from_cherry_pick(whence
) &&
1310 !is_from_rebase(whence
) && renew_authorship
)
1311 die(_("--reset-author can be used only with -C, -c or --amend."));
1313 use_message_buffer
= read_commit_message(use_message
);
1314 if (!renew_authorship
) {
1315 author_message
= use_message
;
1316 author_message_buffer
= use_message_buffer
;
1319 if ((is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) &&
1320 !renew_authorship
) {
1321 author_message
= "CHERRY_PICK_HEAD";
1322 author_message_buffer
= read_commit_message(author_message
);
1325 if (patch_interactive
)
1328 die_for_incompatible_opt4(also
, "-i/--include",
1331 interactive
, "--interactive/-p/--patch");
1332 if (fixup_message
) {
1334 * We limit --fixup's suboptions to only alpha characters.
1335 * If the first character after a run of alpha is colon,
1336 * then the part before the colon may be a known suboption
1337 * name like `amend` or `reword`, or a misspelt suboption
1338 * name. In either case, we treat it as
1339 * --fixup=<suboption>:<arg>.
1341 * Otherwise, we are dealing with --fixup=<commit>.
1343 char *p
= fixup_message
;
1346 if (p
> fixup_message
&& *p
== ':') {
1348 fixup_commit
= p
+ 1;
1349 if (!strcmp("amend", fixup_message
) ||
1350 !strcmp("reword", fixup_message
)) {
1351 fixup_prefix
= "amend";
1353 if (*fixup_message
== 'r') {
1354 check_fixup_reword_options(argc
, argv
);
1358 die(_("unknown option: --fixup=%s:%s"), fixup_message
, fixup_commit
);
1361 fixup_commit
= fixup_message
;
1362 fixup_prefix
= "fixup";
1368 use_editor
= edit_flag
;
1370 cleanup_mode
= get_cleanup_mode(cleanup_arg
, use_editor
);
1372 handle_untracked_files_arg(s
);
1374 if (all
&& argc
> 0)
1375 die(_("paths '%s ...' with -a does not make sense"),
1378 if (status_format
!= STATUS_FORMAT_NONE
)
1384 static int dry_run_commit(const char **argv
, const char *prefix
,
1385 const struct commit
*current_head
, struct wt_status
*s
)
1388 const char *index_file
;
1390 index_file
= prepare_index(argv
, prefix
, current_head
, 1);
1391 committable
= run_status(stdout
, index_file
, prefix
, 0, s
);
1392 rollback_index_files();
1394 return committable
? 0 : 1;
1397 define_list_config_array_extra(color_status_slots
, {"added"});
1399 static int parse_status_slot(const char *slot
)
1401 if (!strcasecmp(slot
, "added"))
1402 return WT_STATUS_UPDATED
;
1404 return LOOKUP_CONFIG(color_status_slots
, slot
);
1407 static int git_status_config(const char *k
, const char *v
, void *cb
)
1409 struct wt_status
*s
= cb
;
1410 const char *slot_name
;
1412 if (starts_with(k
, "column."))
1413 return git_column_config(k
, v
, "status", &s
->colopts
);
1414 if (!strcmp(k
, "status.submodulesummary")) {
1416 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
1417 if (is_bool
&& s
->submodule_summary
)
1418 s
->submodule_summary
= -1;
1421 if (!strcmp(k
, "status.short")) {
1422 if (git_config_bool(k
, v
))
1423 status_deferred_config
.status_format
= STATUS_FORMAT_SHORT
;
1425 status_deferred_config
.status_format
= STATUS_FORMAT_NONE
;
1428 if (!strcmp(k
, "status.branch")) {
1429 status_deferred_config
.show_branch
= git_config_bool(k
, v
);
1432 if (!strcmp(k
, "status.aheadbehind")) {
1433 status_deferred_config
.ahead_behind
= git_config_bool(k
, v
);
1436 if (!strcmp(k
, "status.showstash")) {
1437 s
->show_stash
= git_config_bool(k
, v
);
1440 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1441 s
->use_color
= git_config_colorbool(k
, v
);
1444 if (!strcmp(k
, "status.displaycommentprefix")) {
1445 s
->display_comment_prefix
= git_config_bool(k
, v
);
1448 if (skip_prefix(k
, "status.color.", &slot_name
) ||
1449 skip_prefix(k
, "color.status.", &slot_name
)) {
1450 int slot
= parse_status_slot(slot_name
);
1454 return config_error_nonbool(k
);
1455 return color_parse(v
, s
->color_palette
[slot
]);
1457 if (!strcmp(k
, "status.relativepaths")) {
1458 s
->relative_paths
= git_config_bool(k
, v
);
1461 if (!strcmp(k
, "status.showuntrackedfiles")) {
1463 return config_error_nonbool(k
);
1464 else if (!strcmp(v
, "no"))
1465 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1466 else if (!strcmp(v
, "normal"))
1467 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1468 else if (!strcmp(v
, "all"))
1469 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1471 return error(_("Invalid untracked files mode '%s'"), v
);
1474 if (!strcmp(k
, "diff.renamelimit")) {
1475 if (s
->rename_limit
== -1)
1476 s
->rename_limit
= git_config_int(k
, v
);
1479 if (!strcmp(k
, "status.renamelimit")) {
1480 s
->rename_limit
= git_config_int(k
, v
);
1483 if (!strcmp(k
, "diff.renames")) {
1484 if (s
->detect_rename
== -1)
1485 s
->detect_rename
= git_config_rename(k
, v
);
1488 if (!strcmp(k
, "status.renames")) {
1489 s
->detect_rename
= git_config_rename(k
, v
);
1492 return git_diff_ui_config(k
, v
, NULL
);
1495 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1497 static int no_renames
= -1;
1498 static const char *rename_score_arg
= (const char *)-1;
1499 static struct wt_status s
;
1500 unsigned int progress_flag
= 0;
1502 struct object_id oid
;
1503 static struct option builtin_status_options
[] = {
1504 OPT__VERBOSE(&verbose
, N_("be verbose")),
1505 OPT_SET_INT('s', "short", &status_format
,
1506 N_("show status concisely"), STATUS_FORMAT_SHORT
),
1507 OPT_BOOL('b', "branch", &s
.show_branch
,
1508 N_("show branch information")),
1509 OPT_BOOL(0, "show-stash", &s
.show_stash
,
1510 N_("show stash information")),
1511 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1512 N_("compute full ahead/behind values")),
1513 OPT_CALLBACK_F(0, "porcelain", &status_format
,
1514 N_("version"), N_("machine-readable output"),
1515 PARSE_OPT_OPTARG
, opt_parse_porcelain
),
1516 OPT_SET_INT(0, "long", &status_format
,
1517 N_("show status in long format (default)"),
1518 STATUS_FORMAT_LONG
),
1519 OPT_BOOL('z', "null", &s
.null_termination
,
1520 N_("terminate entries with NUL")),
1521 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1523 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1524 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1525 { OPTION_STRING
, 0, "ignored", &ignored_arg
,
1527 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1528 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"traditional" },
1529 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, N_("when"),
1530 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1531 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1532 OPT_COLUMN(0, "column", &s
.colopts
, N_("list untracked files in columns")),
1533 OPT_BOOL(0, "no-renames", &no_renames
, N_("do not detect renames")),
1534 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg
,
1535 N_("n"), N_("detect renames, optionally set similarity index"),
1536 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, opt_parse_rename_score
),
1540 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1541 usage_with_options(builtin_status_usage
, builtin_status_options
);
1543 prepare_repo_settings(the_repository
);
1544 the_repository
->settings
.command_requires_full_index
= 0;
1546 status_init_config(&s
, git_status_config
);
1547 argc
= parse_options(argc
, argv
, prefix
,
1548 builtin_status_options
,
1549 builtin_status_usage
, 0);
1550 finalize_colopts(&s
.colopts
, -1);
1551 finalize_deferred_config(&s
);
1553 handle_untracked_files_arg(&s
);
1554 handle_ignored_arg(&s
);
1556 if (s
.show_ignored_mode
== SHOW_MATCHING_IGNORED
&&
1557 s
.show_untracked_files
== SHOW_NO_UNTRACKED_FILES
)
1558 die(_("Unsupported combination of ignored and untracked-files arguments"));
1560 parse_pathspec(&s
.pathspec
, 0,
1561 PATHSPEC_PREFER_FULL
,
1564 if (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1565 status_format
!= STATUS_FORMAT_PORCELAIN_V2
)
1566 progress_flag
= REFRESH_PROGRESS
;
1567 repo_read_index(the_repository
);
1568 refresh_index(&the_index
,
1569 REFRESH_QUIET
|REFRESH_UNMERGED
|progress_flag
,
1570 &s
.pathspec
, NULL
, NULL
);
1572 if (use_optional_locks())
1573 fd
= repo_hold_locked_index(the_repository
, &index_lock
, 0);
1577 s
.is_initial
= repo_get_oid(the_repository
, s
.reference
, &oid
) ? 1 : 0;
1579 oidcpy(&s
.oid_commit
, &oid
);
1581 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1582 s
.status_format
= status_format
;
1583 s
.verbose
= verbose
;
1584 if (no_renames
!= -1)
1585 s
.detect_rename
= !no_renames
;
1586 if ((intptr_t)rename_score_arg
!= -1) {
1587 if (s
.detect_rename
< DIFF_DETECT_RENAME
)
1588 s
.detect_rename
= DIFF_DETECT_RENAME
;
1589 if (rename_score_arg
)
1590 s
.rename_score
= parse_rename_score(&rename_score_arg
);
1593 wt_status_collect(&s
);
1596 repo_update_index_if_able(the_repository
, &index_lock
);
1598 if (s
.relative_paths
)
1601 wt_status_print(&s
);
1602 wt_status_collect_free_buffers(&s
);
1607 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1609 struct wt_status
*s
= cb
;
1611 if (!strcmp(k
, "commit.template"))
1612 return git_config_pathname(&template_file
, k
, v
);
1613 if (!strcmp(k
, "commit.status")) {
1614 include_status
= git_config_bool(k
, v
);
1617 if (!strcmp(k
, "commit.cleanup"))
1618 return git_config_string(&cleanup_arg
, k
, v
);
1619 if (!strcmp(k
, "commit.gpgsign")) {
1620 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
1623 if (!strcmp(k
, "commit.verbose")) {
1625 config_commit_verbose
= git_config_bool_or_int(k
, v
, &is_bool
);
1629 return git_status_config(k
, v
, s
);
1632 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1634 static struct wt_status s
;
1635 static struct option builtin_commit_options
[] = {
1636 OPT__QUIET(&quiet
, N_("suppress summary after successful commit")),
1637 OPT__VERBOSE(&verbose
, N_("show diff in commit message template")),
1639 OPT_GROUP(N_("Commit message options")),
1640 OPT_FILENAME('F', "file", &logfile
, N_("read message from file")),
1641 OPT_STRING(0, "author", &force_author
, N_("author"), N_("override author for commit")),
1642 OPT_STRING(0, "date", &force_date
, N_("date"), N_("override date for commit")),
1643 OPT_CALLBACK('m', "message", &message
, N_("message"), N_("commit message"), opt_parse_m
),
1644 OPT_STRING('c', "reedit-message", &edit_message
, N_("commit"), N_("reuse and edit message from specified commit")),
1645 OPT_STRING('C', "reuse-message", &use_message
, N_("commit"), N_("reuse message from specified commit")),
1647 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1648 * and only translate <commit>.
1650 OPT_STRING(0, "fixup", &fixup_message
, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1651 OPT_STRING(0, "squash", &squash_message
, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1652 OPT_BOOL(0, "reset-author", &renew_authorship
, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1653 OPT_CALLBACK_F(0, "trailer", &trailer_args
, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG
, opt_pass_trailer
),
1654 OPT_BOOL('s', "signoff", &signoff
, N_("add a Signed-off-by trailer")),
1655 OPT_FILENAME('t', "template", &template_file
, N_("use specified template file")),
1656 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of commit")),
1657 OPT_CLEANUP(&cleanup_arg
),
1658 OPT_BOOL(0, "status", &include_status
, N_("include status in commit message template")),
1659 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
1660 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1661 /* end commit message options */
1663 OPT_GROUP(N_("Commit contents options")),
1664 OPT_BOOL('a', "all", &all
, N_("commit all changed files")),
1665 OPT_BOOL('i', "include", &also
, N_("add specified files to index for commit")),
1666 OPT_BOOL(0, "interactive", &interactive
, N_("interactively add files")),
1667 OPT_BOOL('p', "patch", &patch_interactive
, N_("interactively add changes")),
1668 OPT_BOOL('o', "only", &only
, N_("commit only specified files")),
1669 OPT_BOOL('n', "no-verify", &no_verify
, N_("bypass pre-commit and commit-msg hooks")),
1670 OPT_BOOL(0, "dry-run", &dry_run
, N_("show what would be committed")),
1671 OPT_SET_INT(0, "short", &status_format
, N_("show status concisely"),
1672 STATUS_FORMAT_SHORT
),
1673 OPT_BOOL(0, "branch", &s
.show_branch
, N_("show branch information")),
1674 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1675 N_("compute full ahead/behind values")),
1676 OPT_SET_INT(0, "porcelain", &status_format
,
1677 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN
),
1678 OPT_SET_INT(0, "long", &status_format
,
1679 N_("show status in long format (default)"),
1680 STATUS_FORMAT_LONG
),
1681 OPT_BOOL('z', "null", &s
.null_termination
,
1682 N_("terminate entries with NUL")),
1683 OPT_BOOL(0, "amend", &amend
, N_("amend previous commit")),
1684 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite
, N_("bypass post-rewrite hook")),
1685 { 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" },
1686 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1687 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1688 /* end commit contents options */
1690 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty
,
1691 N_("ok to record an empty change")),
1692 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message
,
1693 N_("ok to record a change with an empty message")),
1698 struct strbuf sb
= STRBUF_INIT
;
1699 struct strbuf author_ident
= STRBUF_INIT
;
1700 const char *index_file
, *reflog_msg
;
1701 struct object_id oid
;
1702 struct commit_list
*parents
= NULL
;
1703 struct stat statbuf
;
1704 struct commit
*current_head
= NULL
;
1705 struct commit_extra_header
*extra
= NULL
;
1706 struct strbuf err
= STRBUF_INIT
;
1709 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1710 usage_with_options(builtin_commit_usage
, builtin_commit_options
);
1712 prepare_repo_settings(the_repository
);
1713 the_repository
->settings
.command_requires_full_index
= 0;
1715 status_init_config(&s
, git_commit_config
);
1716 s
.commit_template
= 1;
1717 status_format
= STATUS_FORMAT_NONE
; /* Ignore status.short */
1720 if (repo_get_oid(the_repository
, "HEAD", &oid
))
1721 current_head
= NULL
;
1723 current_head
= lookup_commit_or_die(&oid
, "HEAD");
1724 if (repo_parse_commit(the_repository
, current_head
))
1725 die(_("could not parse HEAD commit"));
1727 verbose
= -1; /* unspecified */
1728 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_options
,
1729 builtin_commit_usage
,
1730 prefix
, current_head
, &s
);
1732 verbose
= (config_commit_verbose
< 0) ? 0 : config_commit_verbose
;
1735 return dry_run_commit(argv
, prefix
, current_head
, &s
);
1736 index_file
= prepare_index(argv
, prefix
, current_head
, 0);
1738 /* Set up everything for writing the commit object. This includes
1739 running hooks, writing the trees, and interacting with the user. */
1740 if (!prepare_to_commit(index_file
, prefix
,
1741 current_head
, &s
, &author_ident
)) {
1743 rollback_index_files();
1747 /* Determine parents */
1748 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1749 if (!current_head
) {
1751 reflog_msg
= "commit (initial)";
1754 reflog_msg
= "commit (amend)";
1755 parents
= copy_commit_list(current_head
->parents
);
1756 } else if (whence
== FROM_MERGE
) {
1757 struct strbuf m
= STRBUF_INIT
;
1759 int allow_fast_forward
= 1;
1760 struct commit_list
**pptr
= &parents
;
1763 reflog_msg
= "commit (merge)";
1764 pptr
= commit_list_append(current_head
, pptr
);
1765 fp
= xfopen(git_path_merge_head(the_repository
), "r");
1766 while (strbuf_getline_lf(&m
, fp
) != EOF
) {
1767 struct commit
*parent
;
1769 parent
= get_merge_parent(m
.buf
);
1771 die(_("Corrupt MERGE_HEAD file (%s)"), m
.buf
);
1772 pptr
= commit_list_append(parent
, pptr
);
1776 if (!stat(git_path_merge_mode(the_repository
), &statbuf
)) {
1777 if (strbuf_read_file(&sb
, git_path_merge_mode(the_repository
), 0) < 0)
1778 die_errno(_("could not read MERGE_MODE"));
1779 if (!strcmp(sb
.buf
, "no-ff"))
1780 allow_fast_forward
= 0;
1782 if (allow_fast_forward
)
1783 reduce_heads_replace(&parents
);
1786 reflog_msg
= is_from_cherry_pick(whence
)
1787 ? "commit (cherry-pick)"
1788 : is_from_rebase(whence
)
1791 commit_list_insert(current_head
, &parents
);
1794 /* Finally, get the commit message */
1796 if (strbuf_read_file(&sb
, git_path_commit_editmsg(), 0) < 0) {
1797 int saved_errno
= errno
;
1798 rollback_index_files();
1799 die(_("could not read commit message: %s"), strerror(saved_errno
));
1802 cleanup_message(&sb
, cleanup_mode
, verbose
);
1804 if (message_is_empty(&sb
, cleanup_mode
) && !allow_empty_message
) {
1805 rollback_index_files();
1806 fprintf(stderr
, _("Aborting commit due to empty commit message.\n"));
1809 if (template_untouched(&sb
, template_file
, cleanup_mode
) && !allow_empty_message
) {
1810 rollback_index_files();
1811 fprintf(stderr
, _("Aborting commit; you did not edit the message.\n"));
1815 if (fixup_message
&& starts_with(sb
.buf
, "amend! ") &&
1816 !allow_empty_message
) {
1817 struct strbuf body
= STRBUF_INIT
;
1818 size_t len
= commit_subject_length(sb
.buf
);
1819 strbuf_addstr(&body
, sb
.buf
+ len
);
1820 if (message_is_empty(&body
, cleanup_mode
)) {
1821 rollback_index_files();
1822 fprintf(stderr
, _("Aborting commit due to empty commit message body.\n"));
1825 strbuf_release(&body
);
1829 const char *exclude_gpgsig
[3] = { "gpgsig", "gpgsig-sha256", NULL
};
1830 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1832 struct commit_extra_header
**tail
= &extra
;
1833 append_merge_tag_headers(parents
, &tail
);
1836 if (commit_tree_extended(sb
.buf
, sb
.len
, &the_index
.cache_tree
->oid
,
1837 parents
, &oid
, author_ident
.buf
, NULL
,
1838 sign_commit
, extra
)) {
1839 rollback_index_files();
1840 die(_("failed to write commit object"));
1842 free_commit_extra_headers(extra
);
1844 if (update_head_with_reflog(current_head
, &oid
, reflog_msg
, &sb
,
1846 rollback_index_files();
1850 sequencer_post_commit_cleanup(the_repository
, 0);
1851 unlink(git_path_merge_head(the_repository
));
1852 unlink(git_path_merge_msg(the_repository
));
1853 unlink(git_path_merge_mode(the_repository
));
1854 unlink(git_path_squash_msg(the_repository
));
1856 if (commit_index_files())
1857 die(_("repository has been updated, but unable to write\n"
1858 "new_index file. Check that disk is not full and quota is\n"
1859 "not exceeded, and then \"git restore --staged :/\" to recover."));
1861 git_test_write_commit_graph_or_die();
1863 repo_rerere(the_repository
, 0);
1864 run_auto_maintenance(quiet
);
1865 run_commit_hook(use_editor
, get_index_file(), NULL
, "post-commit",
1867 if (amend
&& !no_post_rewrite
) {
1868 commit_post_rewrite(the_repository
, current_head
, &oid
);
1871 unsigned int flags
= 0;
1874 flags
|= SUMMARY_INITIAL_COMMIT
;
1875 if (author_date_is_interesting())
1876 flags
|= SUMMARY_SHOW_AUTHOR_DATE
;
1877 print_commit_summary(the_repository
, prefix
,
1881 apply_autostash(git_path_merge_autostash(the_repository
));
1884 strbuf_release(&author_ident
);
1885 strbuf_release(&err
);
1886 strbuf_release(&sb
);