4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
12 #include "cache-tree.h"
16 #include "environment.h"
21 #include "wt-status.h"
22 #include "run-command.h"
24 #include "object-name.h"
25 #include "parse-options.h"
27 #include "preload-index.h"
28 #include "read-cache.h"
29 #include "string-list.h"
31 #include "unpack-trees.h"
33 #include "sequencer.h"
34 #include "sparse-index.h"
37 #include "commit-reach.h"
38 #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 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 const 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 const char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
, *ignored_arg
;
125 static const 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 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_parse_porcelain(const struct option
*opt
, const char *arg
, int unset
)
147 enum wt_status_format
*value
= (enum wt_status_format
*)opt
->value
;
149 *value
= STATUS_FORMAT_NONE
;
151 *value
= STATUS_FORMAT_PORCELAIN
;
152 else if (!strcmp(arg
, "v1") || !strcmp(arg
, "1"))
153 *value
= STATUS_FORMAT_PORCELAIN
;
154 else if (!strcmp(arg
, "v2") || !strcmp(arg
, "2"))
155 *value
= STATUS_FORMAT_PORCELAIN_V2
;
157 die("unsupported porcelain version '%s'", arg
);
162 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
164 struct strbuf
*buf
= opt
->value
;
167 strbuf_setlen(buf
, 0);
171 strbuf_addch(buf
, '\n');
172 strbuf_addstr(buf
, arg
);
173 strbuf_complete_line(buf
);
178 static int opt_parse_rename_score(const struct option
*opt
, const char *arg
, int unset
)
180 const char **value
= opt
->value
;
182 BUG_ON_OPT_NEG(unset
);
184 if (arg
!= NULL
&& *arg
== '=')
191 static void determine_whence(struct wt_status
*s
)
193 if (file_exists(git_path_merge_head(the_repository
)))
195 else if (!sequencer_determine_whence(the_repository
, &whence
))
196 whence
= FROM_COMMIT
;
201 static void status_init_config(struct wt_status
*s
, config_fn_t fn
)
203 wt_status_prepare(the_repository
, s
);
204 init_diff_ui_defaults();
207 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
); /* must come after git_config() */
210 static void rollback_index_files(void)
212 switch (commit_style
) {
214 break; /* nothing to do */
216 rollback_lock_file(&index_lock
);
219 rollback_lock_file(&index_lock
);
220 rollback_lock_file(&false_lock
);
225 static int commit_index_files(void)
229 switch (commit_style
) {
231 break; /* nothing to do */
233 err
= commit_lock_file(&index_lock
);
236 err
= commit_lock_file(&index_lock
);
237 rollback_lock_file(&false_lock
);
245 * Take a union of paths in the index and the named tree (typically, "HEAD"),
246 * and return the paths that match the given pattern in list.
248 static int list_paths(struct string_list
*list
, const char *with_tree
,
249 const struct pathspec
*pattern
)
257 m
= xcalloc(1, pattern
->nr
);
260 char *max_prefix
= common_prefix(pattern
);
261 overlay_tree_on_index(the_repository
->index
, with_tree
, max_prefix
);
265 /* TODO: audit for interaction with sparse-index. */
266 ensure_full_index(the_repository
->index
);
267 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
268 const struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
269 struct string_list_item
*item
;
271 if (ce
->ce_flags
& CE_UPDATE
)
273 if (!ce_path_match(the_repository
->index
, ce
, pattern
, m
))
275 item
= string_list_insert(list
, ce
->name
);
276 if (ce_skip_worktree(ce
))
277 item
->util
= item
; /* better a valid pointer than a fake one */
280 ret
= report_path_error(m
, pattern
);
285 static void add_remove_files(struct string_list
*list
)
288 for (i
= 0; i
< list
->nr
; i
++) {
290 struct string_list_item
*p
= &(list
->items
[i
]);
292 /* p->util is skip-worktree */
296 if (!lstat(p
->string
, &st
)) {
297 if (add_to_index(the_repository
->index
, p
->string
, &st
, 0))
298 die(_("updating files failed"));
300 remove_file_from_index(the_repository
->index
, p
->string
);
304 static void create_base_index(const struct commit
*current_head
)
307 struct unpack_trees_options opts
;
311 discard_index(the_repository
->index
);
315 memset(&opts
, 0, sizeof(opts
));
319 opts
.src_index
= the_repository
->index
;
320 opts
.dst_index
= the_repository
->index
;
322 opts
.fn
= oneway_merge
;
323 tree
= parse_tree_indirect(¤t_head
->object
.oid
);
325 die(_("failed to unpack HEAD tree object"));
326 if (parse_tree(tree
) < 0)
328 init_tree_desc(&t
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
329 if (unpack_trees(1, &t
, &opts
))
330 exit(128); /* We've already reported the error, finish dying */
333 static void refresh_cache_or_die(int refresh_flags
)
336 * refresh_flags contains REFRESH_QUIET, so the only errors
337 * are for unmerged entries.
339 if (refresh_index(the_repository
->index
, refresh_flags
| REFRESH_IN_PORCELAIN
, NULL
, NULL
, NULL
))
340 die_resolve_conflict("commit");
343 static const char *prepare_index(const char **argv
, const char *prefix
,
344 const struct commit
*current_head
, int is_status
)
346 struct string_list partial
= STRING_LIST_INIT_DUP
;
347 struct pathspec pathspec
;
348 int refresh_flags
= REFRESH_QUIET
;
352 refresh_flags
|= REFRESH_UNMERGED
;
353 parse_pathspec(&pathspec
, 0,
354 PATHSPEC_PREFER_FULL
,
357 if (pathspec_from_file
) {
359 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
362 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
365 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
367 parse_pathspec_file(&pathspec
, 0,
368 PATHSPEC_PREFER_FULL
,
369 prefix
, pathspec_from_file
, pathspec_file_nul
);
370 } else if (pathspec_file_nul
) {
371 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
374 if (!pathspec
.nr
&& (also
|| (only
&& !allow_empty
&&
375 (!amend
|| (fixup_message
&& strcmp(fixup_prefix
, "amend"))))))
376 die(_("No paths with --include/--only does not make sense."));
378 if (repo_read_index_preload(the_repository
, &pathspec
, 0) < 0)
379 die(_("index file corrupt"));
382 char *old_index_env
= NULL
, *old_repo_index_file
;
383 repo_hold_locked_index(the_repository
, &index_lock
,
386 refresh_cache_or_die(refresh_flags
);
388 if (write_locked_index(the_repository
->index
, &index_lock
, 0))
389 die(_("unable to create temporary index"));
391 old_repo_index_file
= the_repository
->index_file
;
392 the_repository
->index_file
=
393 (char *)get_lock_file_path(&index_lock
);
394 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
395 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
397 if (interactive_add(argv
, prefix
, patch_interactive
) != 0)
398 die(_("interactive add failed"));
400 the_repository
->index_file
= old_repo_index_file
;
401 if (old_index_env
&& *old_index_env
)
402 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
404 unsetenv(INDEX_ENVIRONMENT
);
405 FREE_AND_NULL(old_index_env
);
407 discard_index(the_repository
->index
);
408 read_index_from(the_repository
->index
, get_lock_file_path(&index_lock
),
410 if (cache_tree_update(the_repository
->index
, WRITE_TREE_SILENT
) == 0) {
411 if (reopen_lock_file(&index_lock
) < 0)
412 die(_("unable to write index file"));
413 if (write_locked_index(the_repository
->index
, &index_lock
, 0))
414 die(_("unable to update temporary index"));
416 warning(_("Failed to update main cache tree"));
418 commit_style
= COMMIT_NORMAL
;
419 ret
= get_lock_file_path(&index_lock
);
424 * Non partial, non as-is commit.
426 * (1) get the real index;
427 * (2) update the_index as necessary;
428 * (3) write the_index out to the real index (still locked);
429 * (4) return the name of the locked index file.
431 * The caller should run hooks on the locked real index, and
432 * (A) if all goes well, commit the real index;
433 * (B) on failure, rollback the real index.
435 if (all
|| (also
&& pathspec
.nr
)) {
436 char *ps_matched
= xcalloc(pathspec
.nr
, 1);
437 repo_hold_locked_index(the_repository
, &index_lock
,
439 add_files_to_cache(the_repository
, also
? prefix
: NULL
,
440 &pathspec
, ps_matched
, 0, 0);
441 if (!all
&& report_path_error(ps_matched
, &pathspec
))
444 refresh_cache_or_die(refresh_flags
);
445 cache_tree_update(the_repository
->index
, WRITE_TREE_SILENT
);
446 if (write_locked_index(the_repository
->index
, &index_lock
, 0))
447 die(_("unable to write new index file"));
448 commit_style
= COMMIT_NORMAL
;
449 ret
= get_lock_file_path(&index_lock
);
457 * (1) return the name of the real index file.
459 * The caller should run hooks on the real index,
460 * and create commit from the_index.
461 * We still need to refresh the index here.
463 if (!only
&& !pathspec
.nr
) {
464 repo_hold_locked_index(the_repository
, &index_lock
,
466 refresh_cache_or_die(refresh_flags
);
467 if (the_repository
->index
->cache_changed
468 || !cache_tree_fully_valid(the_repository
->index
->cache_tree
))
469 cache_tree_update(the_repository
->index
, WRITE_TREE_SILENT
);
470 if (write_locked_index(the_repository
->index
, &index_lock
,
471 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
472 die(_("unable to write new index file"));
473 commit_style
= COMMIT_AS_IS
;
474 ret
= get_index_file();
481 * (0) find the set of affected paths;
482 * (1) get lock on the real index file;
483 * (2) update the_index with the given paths;
484 * (3) write the_index out to the real index (still locked);
485 * (4) get lock on the false index file;
486 * (5) reset the_index from HEAD;
487 * (6) update the_index the same way as (2);
488 * (7) write the_index out to the false index file;
489 * (8) return the name of the false index file (still locked);
491 * The caller should run hooks on the locked false index, and
492 * create commit from it. Then
493 * (A) if all goes well, commit the real index;
494 * (B) on failure, rollback the real index;
495 * In either case, rollback the false index.
497 commit_style
= COMMIT_PARTIAL
;
499 if (whence
!= FROM_COMMIT
) {
500 if (whence
== FROM_MERGE
)
501 die(_("cannot do a partial commit during a merge."));
502 else if (is_from_cherry_pick(whence
))
503 die(_("cannot do a partial commit during a cherry-pick."));
504 else if (is_from_rebase(whence
))
505 die(_("cannot do a partial commit during a rebase."));
508 if (list_paths(&partial
, !current_head
? NULL
: "HEAD", &pathspec
))
511 discard_index(the_repository
->index
);
512 if (repo_read_index(the_repository
) < 0)
513 die(_("cannot read the index"));
515 repo_hold_locked_index(the_repository
, &index_lock
, LOCK_DIE_ON_ERROR
);
516 add_remove_files(&partial
);
517 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
518 cache_tree_update(the_repository
->index
, WRITE_TREE_SILENT
);
519 if (write_locked_index(the_repository
->index
, &index_lock
, 0))
520 die(_("unable to write new index file"));
522 hold_lock_file_for_update(&false_lock
,
523 git_path("next-index-%"PRIuMAX
,
524 (uintmax_t) getpid()),
527 create_base_index(current_head
);
528 add_remove_files(&partial
);
529 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
531 if (write_locked_index(the_repository
->index
, &false_lock
, 0))
532 die(_("unable to write temporary index file"));
534 discard_index(the_repository
->index
);
535 ret
= get_lock_file_path(&false_lock
);
536 read_index_from(the_repository
->index
, ret
, get_git_dir());
538 string_list_clear(&partial
, 0);
539 clear_pathspec(&pathspec
);
543 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
546 struct object_id oid
;
548 if (s
->relative_paths
)
553 s
->reference
= "HEAD^1";
555 s
->verbose
= verbose
;
556 s
->index_file
= index_file
;
559 s
->is_initial
= repo_get_oid(the_repository
, s
->reference
, &oid
) ? 1 : 0;
561 oidcpy(&s
->oid_commit
, &oid
);
562 s
->status_format
= status_format
;
563 s
->ignore_submodule_arg
= ignore_submodule_arg
;
565 wt_status_collect(s
);
567 wt_status_collect_free_buffers(s
);
569 return s
->committable
;
572 static int is_a_merge(const struct commit
*current_head
)
574 return !!(current_head
->parents
&& current_head
->parents
->next
);
577 static void assert_split_ident(struct ident_split
*id
, const struct strbuf
*buf
)
579 if (split_ident_line(id
, buf
->buf
, buf
->len
) || !id
->date_begin
)
580 BUG("unable to parse our own ident: %s", buf
->buf
);
583 static void export_one(const char *var
, const char *s
, const char *e
, int hack
)
585 struct strbuf buf
= STRBUF_INIT
;
587 strbuf_addch(&buf
, hack
);
588 strbuf_add(&buf
, s
, e
- s
);
589 setenv(var
, buf
.buf
, 1);
590 strbuf_release(&buf
);
593 static int parse_force_date(const char *in
, struct strbuf
*out
)
595 strbuf_addch(out
, '@');
597 if (parse_date(in
, out
) < 0) {
599 unsigned long t
= approxidate_careful(in
, &errors
);
602 strbuf_addf(out
, "%lu", t
);
608 static void set_ident_var(char **buf
, char *val
)
614 static void determine_author_info(struct strbuf
*author_ident
)
616 char *name
, *email
, *date
;
617 struct ident_split author
;
619 name
= xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
620 email
= xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
621 date
= xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
623 if (author_message
) {
624 struct ident_split ident
;
628 a
= find_commit_header(author_message_buffer
, "author", &len
);
630 die(_("commit '%s' lacks author header"), author_message
);
631 if (split_ident_line(&ident
, a
, len
) < 0)
632 die(_("commit '%s' has malformed author line"), author_message
);
634 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
635 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
637 if (ident
.date_begin
) {
638 struct strbuf date_buf
= STRBUF_INIT
;
639 strbuf_addch(&date_buf
, '@');
640 strbuf_add(&date_buf
, ident
.date_begin
, ident
.date_end
- ident
.date_begin
);
641 strbuf_addch(&date_buf
, ' ');
642 strbuf_add(&date_buf
, ident
.tz_begin
, ident
.tz_end
- ident
.tz_begin
);
643 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
648 struct ident_split ident
;
650 if (split_ident_line(&ident
, force_author
, strlen(force_author
)) < 0)
651 die(_("malformed --author parameter"));
652 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
653 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
657 struct strbuf date_buf
= STRBUF_INIT
;
658 if (parse_force_date(force_date
, &date_buf
))
659 die(_("invalid date format: %s"), force_date
);
660 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
663 strbuf_addstr(author_ident
, fmt_ident(name
, email
, WANT_AUTHOR_IDENT
, date
,
665 assert_split_ident(&author
, author_ident
);
666 export_one("GIT_AUTHOR_NAME", author
.name_begin
, author
.name_end
, 0);
667 export_one("GIT_AUTHOR_EMAIL", author
.mail_begin
, author
.mail_end
, 0);
668 export_one("GIT_AUTHOR_DATE", author
.date_begin
, author
.tz_end
, '@');
674 static int author_date_is_interesting(void)
676 return author_message
|| force_date
;
679 static void adjust_comment_line_char(const struct strbuf
*sb
)
681 char candidates
[] = "#;@!$%^&|:";
685 if (!memchr(sb
->buf
, candidates
[0], sb
->len
)) {
686 comment_line_str
= xstrfmt("%c", candidates
[0]);
691 candidate
= strchr(candidates
, *p
);
694 for (p
= sb
->buf
; *p
; p
++) {
695 if ((p
[0] == '\n' || p
[0] == '\r') && p
[1]) {
696 candidate
= strchr(candidates
, p
[1]);
702 for (p
= candidates
; *p
== ' '; p
++)
705 die(_("unable to select a comment character that is not used\n"
706 "in the current commit message"));
707 comment_line_str
= xstrfmt("%c", *p
);
710 static void prepare_amend_commit(struct commit
*commit
, struct strbuf
*sb
,
711 struct pretty_print_context
*ctx
)
713 const char *buffer
, *subject
, *fmt
;
715 buffer
= repo_get_commit_buffer(the_repository
, commit
, NULL
);
716 find_commit_subject(buffer
, &subject
);
718 * If we amend the 'amend!' commit then we don't want to
719 * duplicate the subject line.
721 fmt
= starts_with(subject
, "amend!") ? "%b" : "%B";
722 repo_format_commit_message(the_repository
, commit
, fmt
, sb
, ctx
);
723 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
726 static int prepare_to_commit(const char *index_file
, const char *prefix
,
727 struct commit
*current_head
,
729 struct strbuf
*author_ident
)
732 struct strbuf committer_ident
= STRBUF_INIT
;
734 struct strbuf sb
= STRBUF_INIT
;
735 const char *hook_arg1
= NULL
;
736 const char *hook_arg2
= NULL
;
737 int clean_message_contents
= (cleanup_mode
!= COMMIT_MSG_CLEANUP_NONE
);
738 int old_display_comment_prefix
;
741 /* This checks and barfs if author is badly specified */
742 determine_author_info(author_ident
);
744 if (!no_verify
&& run_commit_hook(use_editor
, index_file
, &invoked_hook
,
748 if (squash_message
) {
750 * Insert the proper subject line before other commit
751 * message options add their content.
753 if (use_message
&& !strcmp(use_message
, squash_message
))
754 strbuf_addstr(&sb
, "squash! ");
756 struct pretty_print_context ctx
= {0};
758 c
= lookup_commit_reference_by_name(squash_message
);
760 die(_("could not lookup commit '%s'"), squash_message
);
761 ctx
.output_encoding
= get_commit_output_encoding();
762 repo_format_commit_message(the_repository
, c
,
763 "squash! %s\n\n", &sb
,
768 if (have_option_m
&& !fixup_message
) {
769 strbuf_addbuf(&sb
, &message
);
770 hook_arg1
= "message";
771 } else if (logfile
&& !strcmp(logfile
, "-")) {
773 fprintf(stderr
, _("(reading log message from standard input)\n"));
774 if (strbuf_read(&sb
, 0, 0) < 0)
775 die_errno(_("could not read log from standard input"));
776 hook_arg1
= "message";
777 } else if (logfile
) {
778 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
779 die_errno(_("could not read log file '%s'"),
781 hook_arg1
= "message";
782 } else if (use_message
) {
784 buffer
= strstr(use_message_buffer
, "\n\n");
786 strbuf_addstr(&sb
, skip_blank_lines(buffer
+ 2));
787 hook_arg1
= "commit";
788 hook_arg2
= use_message
;
789 } else if (fixup_message
) {
790 struct pretty_print_context ctx
= {0};
791 struct commit
*commit
;
793 commit
= lookup_commit_reference_by_name(fixup_commit
);
795 die(_("could not lookup commit '%s'"), fixup_commit
);
796 ctx
.output_encoding
= get_commit_output_encoding();
797 fmt
= xstrfmt("%s! %%s\n\n", fixup_prefix
);
798 repo_format_commit_message(the_repository
, commit
, fmt
, &sb
,
801 hook_arg1
= "message";
804 * Only `-m` commit message option is checked here, as
805 * it supports `--fixup` to append the commit message.
807 * The other commit message options `-c`/`-C`/`-F` are
808 * incompatible with all the forms of `--fixup` and
809 * have already errored out while parsing the `git commit`
812 if (have_option_m
&& !strcmp(fixup_prefix
, "fixup"))
813 strbuf_addbuf(&sb
, &message
);
815 if (!strcmp(fixup_prefix
, "amend")) {
817 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message
);
818 prepare_amend_commit(commit
, &sb
, &ctx
);
820 } else if (!stat(git_path_merge_msg(the_repository
), &statbuf
)) {
821 size_t merge_msg_start
;
824 * prepend SQUASH_MSG here if it exists and a
825 * "merge --squash" was originally performed
827 if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
828 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
829 die_errno(_("could not read SQUASH_MSG"));
830 hook_arg1
= "squash";
834 merge_msg_start
= sb
.len
;
835 if (strbuf_read_file(&sb
, git_path_merge_msg(the_repository
), 0) < 0)
836 die_errno(_("could not read MERGE_MSG"));
838 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
&&
839 wt_status_locate_end(sb
.buf
+ merge_msg_start
,
840 sb
.len
- merge_msg_start
) <
841 sb
.len
- merge_msg_start
)
842 s
->added_cut_line
= 1;
843 } else if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
844 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
845 die_errno(_("could not read SQUASH_MSG"));
846 hook_arg1
= "squash";
847 } else if (template_file
) {
848 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
849 die_errno(_("could not read '%s'"), template_file
);
850 hook_arg1
= "template";
851 clean_message_contents
= 0;
855 * The remaining cases don't modify the template message, but
856 * just set the argument(s) to the prepare-commit-msg hook.
858 else if (whence
== FROM_MERGE
)
860 else if (is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) {
861 hook_arg1
= "commit";
862 hook_arg2
= "CHERRY_PICK_HEAD";
865 if (squash_message
) {
867 * If squash_commit was used for the commit subject,
868 * then we're possibly hijacking other commit log options.
869 * Reset the hook args to tell the real story.
871 hook_arg1
= "message";
875 s
->fp
= fopen_for_writing(git_path_commit_editmsg());
877 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
879 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
880 old_display_comment_prefix
= s
->display_comment_prefix
;
881 s
->display_comment_prefix
= 1;
884 * Most hints are counter-productive when the commit has
889 if (clean_message_contents
)
890 strbuf_stripspace(&sb
, NULL
);
893 append_signoff(&sb
, ignored_log_message_bytes(sb
.buf
, sb
.len
), 0);
895 if (fwrite(sb
.buf
, 1, sb
.len
, s
->fp
) < sb
.len
)
896 die_errno(_("could not write commit template"));
898 if (auto_comment_line_char
)
899 adjust_comment_line_char(&sb
);
902 /* This checks if committer ident is explicitly given */
903 strbuf_addstr(&committer_ident
, git_committer_info(IDENT_STRICT
));
904 if (use_editor
&& include_status
) {
906 int saved_color_setting
;
907 struct ident_split ci
, ai
;
908 const char *hint_cleanup_all
= allow_empty_message
?
909 _("Please enter the commit message for your changes."
910 " Lines starting\nwith '%s' will be ignored.\n") :
911 _("Please enter the commit message for your changes."
912 " Lines starting\nwith '%s' will be ignored, and an empty"
913 " message aborts the commit.\n");
914 const char *hint_cleanup_space
= allow_empty_message
?
915 _("Please enter the commit message for your changes."
917 "with '%s' will be kept; you may remove them"
918 " yourself if you want to.\n") :
919 _("Please enter the commit message for your changes."
921 "with '%s' will be kept; you may remove them"
922 " yourself if you want to.\n"
923 "An empty message aborts the commit.\n");
924 if (whence
!= FROM_COMMIT
) {
925 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
926 wt_status_add_cut_line(s
);
929 whence
== FROM_MERGE
?
931 "It looks like you may be committing a merge.\n"
932 "If this is not correct, please run\n"
933 " git update-ref -d MERGE_HEAD\n"
934 "and try again.\n") :
936 "It looks like you may be committing a cherry-pick.\n"
937 "If this is not correct, please run\n"
938 " git update-ref -d CHERRY_PICK_HEAD\n"
939 "and try again.\n"));
942 fprintf(s
->fp
, "\n");
943 if (cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
)
944 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_all
, comment_line_str
);
945 else if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
946 if (whence
== FROM_COMMIT
)
947 wt_status_add_cut_line(s
);
948 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
949 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_space
, comment_line_str
);
952 * These should never fail because they come from our own
953 * fmt_ident. They may fail the sane_ident test, but we know
954 * that the name and mail pointers will at least be valid,
955 * which is enough for our tests and printing here.
957 assert_split_ident(&ai
, author_ident
);
958 assert_split_ident(&ci
, &committer_ident
);
960 if (ident_cmp(&ai
, &ci
))
961 status_printf_ln(s
, GIT_COLOR_NORMAL
,
963 "Author: %.*s <%.*s>"),
964 ident_shown
++ ? "" : "\n",
965 (int)(ai
.name_end
- ai
.name_begin
), ai
.name_begin
,
966 (int)(ai
.mail_end
- ai
.mail_begin
), ai
.mail_begin
);
968 if (author_date_is_interesting())
969 status_printf_ln(s
, GIT_COLOR_NORMAL
,
972 ident_shown
++ ? "" : "\n",
973 show_ident_date(&ai
, DATE_MODE(NORMAL
)));
975 if (!committer_ident_sufficiently_given())
976 status_printf_ln(s
, GIT_COLOR_NORMAL
,
978 "Committer: %.*s <%.*s>"),
979 ident_shown
++ ? "" : "\n",
980 (int)(ci
.name_end
- ci
.name_begin
), ci
.name_begin
,
981 (int)(ci
.mail_end
- ci
.mail_begin
), ci
.mail_begin
);
983 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", ""); /* Add new line for clarity */
985 saved_color_setting
= s
->use_color
;
987 committable
= run_status(s
->fp
, index_file
, prefix
, 1, s
);
988 s
->use_color
= saved_color_setting
;
989 string_list_clear(&s
->change
, 1);
991 struct object_id oid
;
992 const char *parent
= "HEAD";
994 if (!the_repository
->index
->initialized
&& repo_read_index(the_repository
) < 0)
995 die(_("Cannot read index"));
1000 if (repo_get_oid(the_repository
, parent
, &oid
)) {
1003 /* TODO: audit for interaction with sparse-index. */
1004 ensure_full_index(the_repository
->index
);
1005 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++)
1006 if (ce_intent_to_add(the_repository
->index
->cache
[i
]))
1008 committable
= the_repository
->index
->cache_nr
- ita_nr
> 0;
1011 * Unless the user did explicitly request a submodule
1012 * ignore mode by passing a command line option we do
1013 * not ignore any changed submodule SHA-1s when
1014 * comparing index and parent, no matter what is
1015 * configured. Otherwise we won't commit any
1016 * submodules which were manually staged, which would
1017 * be really confusing.
1019 struct diff_flags flags
= DIFF_FLAGS_INIT
;
1020 flags
.override_submodule_config
= 1;
1021 if (ignore_submodule_arg
&&
1022 !strcmp(ignore_submodule_arg
, "all"))
1023 flags
.ignore_submodules
= 1;
1024 committable
= index_differs_from(the_repository
,
1028 strbuf_release(&committer_ident
);
1032 if (trailer_args
.nr
) {
1033 if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args
))
1034 die(_("unable to pass trailers to --trailers"));
1035 strvec_clear(&trailer_args
);
1039 * Reject an attempt to record a non-merge empty commit without
1040 * explicit --allow-empty. In the cherry-pick case, it may be
1041 * empty due to conflict resolution, which the user should okay.
1043 if (!committable
&& whence
!= FROM_MERGE
&& !allow_empty
&&
1044 !(amend
&& is_a_merge(current_head
))) {
1045 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
);
1046 s
->display_comment_prefix
= old_display_comment_prefix
;
1047 run_status(stdout
, index_file
, prefix
, 0, s
);
1049 fputs(_(empty_amend_advice
), stderr
);
1050 else if (is_from_cherry_pick(whence
) ||
1051 whence
== FROM_REBASE_PICK
) {
1052 fputs(_(empty_cherry_pick_advice
), stderr
);
1053 if (whence
== FROM_CHERRY_PICK_SINGLE
)
1054 fputs(_(empty_cherry_pick_advice_single
), stderr
);
1055 else if (whence
== FROM_CHERRY_PICK_MULTI
)
1056 fputs(_(empty_cherry_pick_advice_multi
), stderr
);
1058 fputs(_(empty_rebase_pick_advice
), stderr
);
1063 if (!no_verify
&& invoked_hook
) {
1065 * Re-read the index as the pre-commit-commit hook was invoked
1066 * and could have updated it. We must do this before we invoke
1067 * the editor and after we invoke run_status above.
1069 discard_index(the_repository
->index
);
1071 read_index_from(the_repository
->index
, index_file
, get_git_dir());
1073 if (cache_tree_update(the_repository
->index
, 0)) {
1074 error(_("Error building trees"));
1078 if (run_commit_hook(use_editor
, index_file
, NULL
, "prepare-commit-msg",
1079 git_path_commit_editmsg(), hook_arg1
, hook_arg2
, NULL
))
1083 struct strvec env
= STRVEC_INIT
;
1085 strvec_pushf(&env
, "GIT_INDEX_FILE=%s", index_file
);
1086 if (launch_editor(git_path_commit_editmsg(), NULL
, env
.v
)) {
1088 _("Please supply the message using either -m or -F option.\n"));
1095 run_commit_hook(use_editor
, index_file
, NULL
, "commit-msg",
1096 git_path_commit_editmsg(), NULL
)) {
1103 static const char *find_author_by_nickname(const char *name
)
1105 struct rev_info revs
;
1106 struct commit
*commit
;
1107 struct strbuf buf
= STRBUF_INIT
;
1111 repo_init_revisions(the_repository
, &revs
, NULL
);
1112 strbuf_addf(&buf
, "--author=%s", name
);
1117 setup_revisions(ac
, av
, &revs
, NULL
);
1118 revs
.mailmap
= xmalloc(sizeof(struct string_list
));
1119 string_list_init_nodup(revs
.mailmap
);
1120 read_mailmap(revs
.mailmap
);
1122 if (prepare_revision_walk(&revs
))
1123 die(_("revision walk setup failed"));
1124 commit
= get_revision(&revs
);
1126 struct pretty_print_context ctx
= {0};
1127 ctx
.date_mode
.type
= DATE_NORMAL
;
1128 strbuf_release(&buf
);
1129 repo_format_commit_message(the_repository
, commit
,
1130 "%aN <%aE>", &buf
, &ctx
);
1131 release_revisions(&revs
);
1132 return strbuf_detach(&buf
, NULL
);
1134 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name
);
1137 static void handle_ignored_arg(struct wt_status
*s
)
1140 ; /* default already initialized */
1141 else if (!strcmp(ignored_arg
, "traditional"))
1142 s
->show_ignored_mode
= SHOW_TRADITIONAL_IGNORED
;
1143 else if (!strcmp(ignored_arg
, "no"))
1144 s
->show_ignored_mode
= SHOW_NO_IGNORED
;
1145 else if (!strcmp(ignored_arg
, "matching"))
1146 s
->show_ignored_mode
= SHOW_MATCHING_IGNORED
;
1148 die(_("Invalid ignored mode '%s'"), ignored_arg
);
1151 static enum untracked_status_type
parse_untracked_setting_name(const char *u
)
1154 * Please update $__git_untracked_file_modes in
1155 * git-completion.bash when you add new options
1157 switch (git_parse_maybe_bool(u
)) {
1168 if (!strcmp(u
, "no"))
1169 return SHOW_NO_UNTRACKED_FILES
;
1170 else if (!strcmp(u
, "normal"))
1171 return SHOW_NORMAL_UNTRACKED_FILES
;
1172 else if (!strcmp(u
, "all"))
1173 return SHOW_ALL_UNTRACKED_FILES
;
1175 return SHOW_UNTRACKED_FILES_ERROR
;
1178 static void handle_untracked_files_arg(struct wt_status
*s
)
1180 enum untracked_status_type u
;
1182 if (!untracked_files_arg
)
1183 return; /* default already initialized */
1185 u
= parse_untracked_setting_name(untracked_files_arg
);
1186 if (u
== SHOW_UNTRACKED_FILES_ERROR
)
1187 die(_("Invalid untracked files mode '%s'"),
1188 untracked_files_arg
);
1189 s
->show_untracked_files
= u
;
1192 static const char *read_commit_message(const char *name
)
1194 const char *out_enc
;
1195 struct commit
*commit
;
1197 commit
= lookup_commit_reference_by_name(name
);
1199 die(_("could not lookup commit '%s'"), name
);
1200 out_enc
= get_commit_output_encoding();
1201 return repo_logmsg_reencode(the_repository
, commit
, NULL
, out_enc
);
1205 * Enumerate what needs to be propagated when --porcelain
1206 * is not in effect here.
1208 static struct status_deferred_config
{
1209 enum wt_status_format status_format
;
1211 enum ahead_behind_flags ahead_behind
;
1212 } status_deferred_config
= {
1213 STATUS_FORMAT_UNSPECIFIED
,
1214 -1, /* unspecified */
1215 AHEAD_BEHIND_UNSPECIFIED
,
1218 static void finalize_deferred_config(struct wt_status
*s
)
1220 int use_deferred_config
= (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1221 status_format
!= STATUS_FORMAT_PORCELAIN_V2
&&
1222 !s
->null_termination
);
1224 if (s
->null_termination
) {
1225 if (status_format
== STATUS_FORMAT_NONE
||
1226 status_format
== STATUS_FORMAT_UNSPECIFIED
)
1227 status_format
= STATUS_FORMAT_PORCELAIN
;
1228 else if (status_format
== STATUS_FORMAT_LONG
)
1229 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
1232 if (use_deferred_config
&& status_format
== STATUS_FORMAT_UNSPECIFIED
)
1233 status_format
= status_deferred_config
.status_format
;
1234 if (status_format
== STATUS_FORMAT_UNSPECIFIED
)
1235 status_format
= STATUS_FORMAT_NONE
;
1237 if (use_deferred_config
&& s
->show_branch
< 0)
1238 s
->show_branch
= status_deferred_config
.show_branch
;
1239 if (s
->show_branch
< 0)
1243 * If the user did not give a "--[no]-ahead-behind" command
1244 * line argument *AND* we will print in a human-readable format
1245 * (short, long etc.) then we inherit from the status.aheadbehind
1246 * config setting. In all other cases (and porcelain V[12] formats
1247 * in particular), we inherit _FULL for backwards compatibility.
1249 if (use_deferred_config
&&
1250 s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1251 s
->ahead_behind_flags
= status_deferred_config
.ahead_behind
;
1253 if (s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1254 s
->ahead_behind_flags
= AHEAD_BEHIND_FULL
;
1257 static void check_fixup_reword_options(int argc
, const char *argv
[]) {
1258 if (whence
!= FROM_COMMIT
) {
1259 if (whence
== FROM_MERGE
)
1260 die(_("You are in the middle of a merge -- cannot reword."));
1261 else if (is_from_cherry_pick(whence
))
1262 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1265 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv
);
1266 if (patch_interactive
|| interactive
|| all
|| also
|| only
)
1267 die(_("reword option of '%s' and '%s' cannot be used together"),
1268 "--fixup", "--patch/--interactive/--all/--include/--only");
1271 static int parse_and_validate_options(int argc
, const char *argv
[],
1272 const struct option
*options
,
1273 const char * const usage
[],
1275 struct commit
*current_head
,
1276 struct wt_status
*s
)
1278 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1279 finalize_deferred_config(s
);
1281 if (force_author
&& !strchr(force_author
, '>'))
1282 force_author
= find_author_by_nickname(force_author
);
1284 if (force_author
&& renew_authorship
)
1285 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
1287 if (logfile
|| have_option_m
|| use_message
)
1290 /* Sanity check options */
1291 if (amend
&& !current_head
)
1292 die(_("You have nothing to amend."));
1293 if (amend
&& whence
!= FROM_COMMIT
) {
1294 if (whence
== FROM_MERGE
)
1295 die(_("You are in the middle of a merge -- cannot amend."));
1296 else if (is_from_cherry_pick(whence
))
1297 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1298 else if (whence
== FROM_REBASE_PICK
)
1299 die(_("You are in the middle of a rebase -- cannot amend."));
1301 if (fixup_message
&& squash_message
)
1302 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1303 die_for_incompatible_opt4(!!use_message
, "-C",
1304 !!edit_message
, "-c",
1306 !!fixup_message
, "--fixup");
1307 die_for_incompatible_opt4(have_option_m
, "-m",
1308 !!edit_message
, "-c",
1309 !!use_message
, "-C",
1311 if (use_message
|| edit_message
|| logfile
||fixup_message
|| have_option_m
)
1312 template_file
= NULL
;
1314 use_message
= edit_message
;
1315 if (amend
&& !use_message
&& !fixup_message
)
1316 use_message
= "HEAD";
1317 if (!use_message
&& !is_from_cherry_pick(whence
) &&
1318 !is_from_rebase(whence
) && renew_authorship
)
1319 die(_("--reset-author can be used only with -C, -c or --amend."));
1321 use_message_buffer
= read_commit_message(use_message
);
1322 if (!renew_authorship
) {
1323 author_message
= use_message
;
1324 author_message_buffer
= use_message_buffer
;
1327 if ((is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) &&
1328 !renew_authorship
) {
1329 author_message
= "CHERRY_PICK_HEAD";
1330 author_message_buffer
= read_commit_message(author_message
);
1333 if (patch_interactive
)
1336 die_for_incompatible_opt4(also
, "-i/--include",
1339 interactive
, "--interactive/-p/--patch");
1340 if (fixup_message
) {
1342 * We limit --fixup's suboptions to only alpha characters.
1343 * If the first character after a run of alpha is colon,
1344 * then the part before the colon may be a known suboption
1345 * name like `amend` or `reword`, or a misspelt suboption
1346 * name. In either case, we treat it as
1347 * --fixup=<suboption>:<arg>.
1349 * Otherwise, we are dealing with --fixup=<commit>.
1351 char *p
= fixup_message
;
1354 if (p
> fixup_message
&& *p
== ':') {
1356 fixup_commit
= p
+ 1;
1357 if (!strcmp("amend", fixup_message
) ||
1358 !strcmp("reword", fixup_message
)) {
1359 fixup_prefix
= "amend";
1361 if (*fixup_message
== 'r') {
1362 check_fixup_reword_options(argc
, argv
);
1366 die(_("unknown option: --fixup=%s:%s"), fixup_message
, fixup_commit
);
1369 fixup_commit
= fixup_message
;
1370 fixup_prefix
= "fixup";
1376 use_editor
= edit_flag
;
1378 cleanup_mode
= get_cleanup_mode(cleanup_arg
, use_editor
);
1380 handle_untracked_files_arg(s
);
1382 if (all
&& argc
> 0)
1383 die(_("paths '%s ...' with -a does not make sense"),
1386 if (status_format
!= STATUS_FORMAT_NONE
)
1392 static int dry_run_commit(const char **argv
, const char *prefix
,
1393 const struct commit
*current_head
, struct wt_status
*s
)
1396 const char *index_file
;
1398 index_file
= prepare_index(argv
, prefix
, current_head
, 1);
1399 committable
= run_status(stdout
, index_file
, prefix
, 0, s
);
1400 rollback_index_files();
1402 return committable
? 0 : 1;
1405 define_list_config_array_extra(color_status_slots
, {"added"});
1407 static int parse_status_slot(const char *slot
)
1409 if (!strcasecmp(slot
, "added"))
1410 return WT_STATUS_UPDATED
;
1412 return LOOKUP_CONFIG(color_status_slots
, slot
);
1415 static int git_status_config(const char *k
, const char *v
,
1416 const struct config_context
*ctx
, void *cb
)
1418 struct wt_status
*s
= cb
;
1419 const char *slot_name
;
1421 if (starts_with(k
, "column."))
1422 return git_column_config(k
, v
, "status", &s
->colopts
);
1423 if (!strcmp(k
, "status.submodulesummary")) {
1425 s
->submodule_summary
= git_config_bool_or_int(k
, v
, ctx
->kvi
,
1427 if (is_bool
&& s
->submodule_summary
)
1428 s
->submodule_summary
= -1;
1431 if (!strcmp(k
, "status.short")) {
1432 if (git_config_bool(k
, v
))
1433 status_deferred_config
.status_format
= STATUS_FORMAT_SHORT
;
1435 status_deferred_config
.status_format
= STATUS_FORMAT_NONE
;
1438 if (!strcmp(k
, "status.branch")) {
1439 status_deferred_config
.show_branch
= git_config_bool(k
, v
);
1442 if (!strcmp(k
, "status.aheadbehind")) {
1443 status_deferred_config
.ahead_behind
= git_config_bool(k
, v
);
1446 if (!strcmp(k
, "status.showstash")) {
1447 s
->show_stash
= git_config_bool(k
, v
);
1450 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1451 s
->use_color
= git_config_colorbool(k
, v
);
1454 if (!strcmp(k
, "status.displaycommentprefix")) {
1455 s
->display_comment_prefix
= git_config_bool(k
, v
);
1458 if (skip_prefix(k
, "status.color.", &slot_name
) ||
1459 skip_prefix(k
, "color.status.", &slot_name
)) {
1460 int slot
= parse_status_slot(slot_name
);
1464 return config_error_nonbool(k
);
1465 return color_parse(v
, s
->color_palette
[slot
]);
1467 if (!strcmp(k
, "status.relativepaths")) {
1468 s
->relative_paths
= git_config_bool(k
, v
);
1471 if (!strcmp(k
, "status.showuntrackedfiles")) {
1472 enum untracked_status_type u
;
1474 u
= parse_untracked_setting_name(v
);
1475 if (u
== SHOW_UNTRACKED_FILES_ERROR
)
1476 return error(_("Invalid untracked files mode '%s'"), v
);
1477 s
->show_untracked_files
= u
;
1480 if (!strcmp(k
, "diff.renamelimit")) {
1481 if (s
->rename_limit
== -1)
1482 s
->rename_limit
= git_config_int(k
, v
, ctx
->kvi
);
1485 if (!strcmp(k
, "status.renamelimit")) {
1486 s
->rename_limit
= git_config_int(k
, v
, ctx
->kvi
);
1489 if (!strcmp(k
, "diff.renames")) {
1490 if (s
->detect_rename
== -1)
1491 s
->detect_rename
= git_config_rename(k
, v
);
1494 if (!strcmp(k
, "status.renames")) {
1495 s
->detect_rename
= git_config_rename(k
, v
);
1498 return git_diff_ui_config(k
, v
, ctx
, NULL
);
1501 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1503 static int no_renames
= -1;
1504 static const char *rename_score_arg
= (const char *)-1;
1505 static struct wt_status s
;
1506 unsigned int progress_flag
= 0;
1508 struct object_id oid
;
1509 static struct option builtin_status_options
[] = {
1510 OPT__VERBOSE(&verbose
, N_("be verbose")),
1511 OPT_SET_INT('s', "short", &status_format
,
1512 N_("show status concisely"), STATUS_FORMAT_SHORT
),
1513 OPT_BOOL('b', "branch", &s
.show_branch
,
1514 N_("show branch information")),
1515 OPT_BOOL(0, "show-stash", &s
.show_stash
,
1516 N_("show stash information")),
1517 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1518 N_("compute full ahead/behind values")),
1519 OPT_CALLBACK_F(0, "porcelain", &status_format
,
1520 N_("version"), N_("machine-readable output"),
1521 PARSE_OPT_OPTARG
, opt_parse_porcelain
),
1522 OPT_SET_INT(0, "long", &status_format
,
1523 N_("show status in long format (default)"),
1524 STATUS_FORMAT_LONG
),
1525 OPT_BOOL('z', "null", &s
.null_termination
,
1526 N_("terminate entries with NUL")),
1527 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1529 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1530 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1531 { OPTION_STRING
, 0, "ignored", &ignored_arg
,
1533 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1534 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"traditional" },
1535 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, N_("when"),
1536 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1537 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1538 OPT_COLUMN(0, "column", &s
.colopts
, N_("list untracked files in columns")),
1539 OPT_BOOL(0, "no-renames", &no_renames
, N_("do not detect renames")),
1540 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg
,
1541 N_("n"), N_("detect renames, optionally set similarity index"),
1542 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, opt_parse_rename_score
),
1546 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1547 usage_with_options(builtin_status_usage
, builtin_status_options
);
1549 prepare_repo_settings(the_repository
);
1550 the_repository
->settings
.command_requires_full_index
= 0;
1552 status_init_config(&s
, git_status_config
);
1553 argc
= parse_options(argc
, argv
, prefix
,
1554 builtin_status_options
,
1555 builtin_status_usage
, 0);
1556 finalize_colopts(&s
.colopts
, -1);
1557 finalize_deferred_config(&s
);
1559 handle_untracked_files_arg(&s
);
1560 handle_ignored_arg(&s
);
1562 if (s
.show_ignored_mode
== SHOW_MATCHING_IGNORED
&&
1563 s
.show_untracked_files
== SHOW_NO_UNTRACKED_FILES
)
1564 die(_("Unsupported combination of ignored and untracked-files arguments"));
1566 parse_pathspec(&s
.pathspec
, 0,
1567 PATHSPEC_PREFER_FULL
,
1570 if (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1571 status_format
!= STATUS_FORMAT_PORCELAIN_V2
)
1572 progress_flag
= REFRESH_PROGRESS
;
1573 repo_read_index(the_repository
);
1574 refresh_index(the_repository
->index
,
1575 REFRESH_QUIET
|REFRESH_UNMERGED
|progress_flag
,
1576 &s
.pathspec
, NULL
, NULL
);
1578 if (use_optional_locks())
1579 fd
= repo_hold_locked_index(the_repository
, &index_lock
, 0);
1583 s
.is_initial
= repo_get_oid(the_repository
, s
.reference
, &oid
) ? 1 : 0;
1585 oidcpy(&s
.oid_commit
, &oid
);
1587 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1588 s
.status_format
= status_format
;
1589 s
.verbose
= verbose
;
1590 if (no_renames
!= -1)
1591 s
.detect_rename
= !no_renames
;
1592 if ((intptr_t)rename_score_arg
!= -1) {
1593 if (s
.detect_rename
< DIFF_DETECT_RENAME
)
1594 s
.detect_rename
= DIFF_DETECT_RENAME
;
1595 if (rename_score_arg
)
1596 s
.rename_score
= parse_rename_score(&rename_score_arg
);
1599 wt_status_collect(&s
);
1602 repo_update_index_if_able(the_repository
, &index_lock
);
1604 if (s
.relative_paths
)
1607 wt_status_print(&s
);
1608 wt_status_collect_free_buffers(&s
);
1613 static int git_commit_config(const char *k
, const char *v
,
1614 const struct config_context
*ctx
, void *cb
)
1616 struct wt_status
*s
= cb
;
1618 if (!strcmp(k
, "commit.template"))
1619 return git_config_pathname(&template_file
, k
, v
);
1620 if (!strcmp(k
, "commit.status")) {
1621 include_status
= git_config_bool(k
, v
);
1624 if (!strcmp(k
, "commit.cleanup"))
1625 return git_config_string(&cleanup_arg
, k
, v
);
1626 if (!strcmp(k
, "commit.gpgsign")) {
1627 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
1630 if (!strcmp(k
, "commit.verbose")) {
1632 config_commit_verbose
= git_config_bool_or_int(k
, v
, ctx
->kvi
,
1637 return git_status_config(k
, v
, ctx
, s
);
1640 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1642 static struct wt_status s
;
1643 static struct option builtin_commit_options
[] = {
1644 OPT__QUIET(&quiet
, N_("suppress summary after successful commit")),
1645 OPT__VERBOSE(&verbose
, N_("show diff in commit message template")),
1647 OPT_GROUP(N_("Commit message options")),
1648 OPT_FILENAME('F', "file", &logfile
, N_("read message from file")),
1649 OPT_STRING(0, "author", &force_author
, N_("author"), N_("override author for commit")),
1650 OPT_STRING(0, "date", &force_date
, N_("date"), N_("override date for commit")),
1651 OPT_CALLBACK('m', "message", &message
, N_("message"), N_("commit message"), opt_parse_m
),
1652 OPT_STRING('c', "reedit-message", &edit_message
, N_("commit"), N_("reuse and edit message from specified commit")),
1653 OPT_STRING('C', "reuse-message", &use_message
, N_("commit"), N_("reuse message from specified commit")),
1655 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1656 * and only translate <commit>.
1658 OPT_STRING(0, "fixup", &fixup_message
, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1659 OPT_STRING(0, "squash", &squash_message
, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1660 OPT_BOOL(0, "reset-author", &renew_authorship
, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1661 OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args
, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG
),
1662 OPT_BOOL('s', "signoff", &signoff
, N_("add a Signed-off-by trailer")),
1663 OPT_FILENAME('t', "template", &template_file
, N_("use specified template file")),
1664 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of commit")),
1665 OPT_CLEANUP(&cleanup_arg
),
1666 OPT_BOOL(0, "status", &include_status
, N_("include status in commit message template")),
1667 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
1668 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1669 /* end commit message options */
1671 OPT_GROUP(N_("Commit contents options")),
1672 OPT_BOOL('a', "all", &all
, N_("commit all changed files")),
1673 OPT_BOOL('i', "include", &also
, N_("add specified files to index for commit")),
1674 OPT_BOOL(0, "interactive", &interactive
, N_("interactively add files")),
1675 OPT_BOOL('p', "patch", &patch_interactive
, N_("interactively add changes")),
1676 OPT_BOOL('o', "only", &only
, N_("commit only specified files")),
1677 OPT_BOOL('n', "no-verify", &no_verify
, N_("bypass pre-commit and commit-msg hooks")),
1678 OPT_BOOL(0, "dry-run", &dry_run
, N_("show what would be committed")),
1679 OPT_SET_INT(0, "short", &status_format
, N_("show status concisely"),
1680 STATUS_FORMAT_SHORT
),
1681 OPT_BOOL(0, "branch", &s
.show_branch
, N_("show branch information")),
1682 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1683 N_("compute full ahead/behind values")),
1684 OPT_SET_INT(0, "porcelain", &status_format
,
1685 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN
),
1686 OPT_SET_INT(0, "long", &status_format
,
1687 N_("show status in long format (default)"),
1688 STATUS_FORMAT_LONG
),
1689 OPT_BOOL('z', "null", &s
.null_termination
,
1690 N_("terminate entries with NUL")),
1691 OPT_BOOL(0, "amend", &amend
, N_("amend previous commit")),
1692 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite
, N_("bypass post-rewrite hook")),
1693 { 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" },
1694 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1695 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1696 /* end commit contents options */
1698 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty
,
1699 N_("ok to record an empty change")),
1700 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message
,
1701 N_("ok to record a change with an empty message")),
1706 struct strbuf sb
= STRBUF_INIT
;
1707 struct strbuf author_ident
= STRBUF_INIT
;
1708 const char *index_file
, *reflog_msg
;
1709 struct object_id oid
;
1710 struct commit_list
*parents
= NULL
;
1711 struct stat statbuf
;
1712 struct commit
*current_head
= NULL
;
1713 struct commit_extra_header
*extra
= NULL
;
1714 struct strbuf err
= STRBUF_INIT
;
1717 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1718 usage_with_options(builtin_commit_usage
, builtin_commit_options
);
1720 prepare_repo_settings(the_repository
);
1721 the_repository
->settings
.command_requires_full_index
= 0;
1723 status_init_config(&s
, git_commit_config
);
1724 s
.commit_template
= 1;
1725 status_format
= STATUS_FORMAT_NONE
; /* Ignore status.short */
1728 if (repo_get_oid(the_repository
, "HEAD", &oid
))
1729 current_head
= NULL
;
1731 current_head
= lookup_commit_or_die(&oid
, "HEAD");
1732 if (repo_parse_commit(the_repository
, current_head
))
1733 die(_("could not parse HEAD commit"));
1735 verbose
= -1; /* unspecified */
1736 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_options
,
1737 builtin_commit_usage
,
1738 prefix
, current_head
, &s
);
1740 verbose
= (config_commit_verbose
< 0) ? 0 : config_commit_verbose
;
1743 return dry_run_commit(argv
, prefix
, current_head
, &s
);
1744 index_file
= prepare_index(argv
, prefix
, current_head
, 0);
1746 /* Set up everything for writing the commit object. This includes
1747 running hooks, writing the trees, and interacting with the user. */
1748 if (!prepare_to_commit(index_file
, prefix
,
1749 current_head
, &s
, &author_ident
)) {
1751 rollback_index_files();
1755 /* Determine parents */
1756 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1757 if (!current_head
) {
1759 reflog_msg
= "commit (initial)";
1762 reflog_msg
= "commit (amend)";
1763 parents
= copy_commit_list(current_head
->parents
);
1764 } else if (whence
== FROM_MERGE
) {
1765 struct strbuf m
= STRBUF_INIT
;
1767 int allow_fast_forward
= 1;
1768 struct commit_list
**pptr
= &parents
;
1771 reflog_msg
= "commit (merge)";
1772 pptr
= commit_list_append(current_head
, pptr
);
1773 fp
= xfopen(git_path_merge_head(the_repository
), "r");
1774 while (strbuf_getline_lf(&m
, fp
) != EOF
) {
1775 struct commit
*parent
;
1777 parent
= get_merge_parent(m
.buf
);
1779 die(_("Corrupt MERGE_HEAD file (%s)"), m
.buf
);
1780 pptr
= commit_list_append(parent
, pptr
);
1784 if (!stat(git_path_merge_mode(the_repository
), &statbuf
)) {
1785 if (strbuf_read_file(&sb
, git_path_merge_mode(the_repository
), 0) < 0)
1786 die_errno(_("could not read MERGE_MODE"));
1787 if (!strcmp(sb
.buf
, "no-ff"))
1788 allow_fast_forward
= 0;
1790 if (allow_fast_forward
)
1791 reduce_heads_replace(&parents
);
1794 reflog_msg
= is_from_cherry_pick(whence
)
1795 ? "commit (cherry-pick)"
1796 : is_from_rebase(whence
)
1799 commit_list_insert(current_head
, &parents
);
1802 /* Finally, get the commit message */
1804 if (strbuf_read_file(&sb
, git_path_commit_editmsg(), 0) < 0) {
1805 int saved_errno
= errno
;
1806 rollback_index_files();
1807 die(_("could not read commit message: %s"), strerror(saved_errno
));
1810 cleanup_message(&sb
, cleanup_mode
, verbose
);
1812 if (message_is_empty(&sb
, cleanup_mode
) && !allow_empty_message
) {
1813 rollback_index_files();
1814 fprintf(stderr
, _("Aborting commit due to empty commit message.\n"));
1817 if (template_untouched(&sb
, template_file
, cleanup_mode
) && !allow_empty_message
) {
1818 rollback_index_files();
1819 fprintf(stderr
, _("Aborting commit; you did not edit the message.\n"));
1823 if (fixup_message
&& starts_with(sb
.buf
, "amend! ") &&
1824 !allow_empty_message
) {
1825 struct strbuf body
= STRBUF_INIT
;
1826 size_t len
= commit_subject_length(sb
.buf
);
1827 strbuf_addstr(&body
, sb
.buf
+ len
);
1828 if (message_is_empty(&body
, cleanup_mode
)) {
1829 rollback_index_files();
1830 fprintf(stderr
, _("Aborting commit due to empty commit message body.\n"));
1833 strbuf_release(&body
);
1837 const char *exclude_gpgsig
[3] = { "gpgsig", "gpgsig-sha256", NULL
};
1838 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1840 struct commit_extra_header
**tail
= &extra
;
1841 append_merge_tag_headers(parents
, &tail
);
1844 if (commit_tree_extended(sb
.buf
, sb
.len
, &the_repository
->index
->cache_tree
->oid
,
1845 parents
, &oid
, author_ident
.buf
, NULL
,
1846 sign_commit
, extra
)) {
1847 rollback_index_files();
1848 die(_("failed to write commit object"));
1850 free_commit_extra_headers(extra
);
1852 if (update_head_with_reflog(current_head
, &oid
, reflog_msg
, &sb
,
1854 rollback_index_files();
1858 sequencer_post_commit_cleanup(the_repository
, 0);
1859 unlink(git_path_merge_head(the_repository
));
1860 unlink(git_path_merge_msg(the_repository
));
1861 unlink(git_path_merge_mode(the_repository
));
1862 unlink(git_path_squash_msg(the_repository
));
1864 if (commit_index_files())
1865 die(_("repository has been updated, but unable to write\n"
1866 "new index file. Check that disk is not full and quota is\n"
1867 "not exceeded, and then \"git restore --staged :/\" to recover."));
1869 git_test_write_commit_graph_or_die();
1871 repo_rerere(the_repository
, 0);
1872 run_auto_maintenance(quiet
);
1873 run_commit_hook(use_editor
, get_index_file(), NULL
, "post-commit",
1875 if (amend
&& !no_post_rewrite
) {
1876 commit_post_rewrite(the_repository
, current_head
, &oid
);
1879 unsigned int flags
= 0;
1882 flags
|= SUMMARY_INITIAL_COMMIT
;
1883 if (author_date_is_interesting())
1884 flags
|= SUMMARY_SHOW_AUTHOR_DATE
;
1885 print_commit_summary(the_repository
, prefix
,
1889 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1892 strbuf_release(&author_ident
);
1893 strbuf_release(&err
);
1894 strbuf_release(&sb
);