4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
7 #define USE_THE_REPOSITORY_VARIABLE
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 "repository.h"
30 #include "string-list.h"
32 #include "unpack-trees.h"
34 #include "sequencer.h"
35 #include "sparse-index.h"
38 #include "commit-reach.h"
39 #include "commit-graph.h"
43 static const char * const builtin_commit_usage
[] = {
44 N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n"
45 " [--dry-run] [(-c | -C | --squash) <commit> | --fixup [(amend|reword):]<commit>]\n"
46 " [-F <file> | -m <msg>] [--reset-author] [--allow-empty]\n"
47 " [--allow-empty-message] [--no-verify] [-e] [--author=<author>]\n"
48 " [--date=<date>] [--cleanup=<mode>] [--[no-]status]\n"
49 " [-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]\n"
50 " [(--trailer <token>[(=|:)<value>])...] [-S[<keyid>]]\n"
51 " [--] [<pathspec>...]"),
55 static const char * const builtin_status_usage
[] = {
56 N_("git status [<options>] [--] [<pathspec>...]"),
60 static const char empty_amend_advice
[] =
61 N_("You asked to amend the most recent commit, but doing so would make\n"
62 "it empty. You can repeat your command with --allow-empty, or you can\n"
63 "remove the commit entirely with \"git reset HEAD^\".\n");
65 static const char empty_cherry_pick_advice
[] =
66 N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\n"
67 "If you wish to commit it anyway, use:\n"
69 " git commit --allow-empty\n"
72 static const char empty_rebase_pick_advice
[] =
73 N_("Otherwise, please use 'git rebase --skip'\n");
75 static const char empty_cherry_pick_advice_single
[] =
76 N_("Otherwise, please use 'git cherry-pick --skip'\n");
78 static const char empty_cherry_pick_advice_multi
[] =
81 " git cherry-pick --continue\n"
83 "to resume cherry-picking the remaining commits.\n"
84 "If you wish to skip this commit, use:\n"
86 " git cherry-pick --skip\n"
89 static const char *color_status_slots
[] = {
90 [WT_STATUS_HEADER
] = "header",
91 [WT_STATUS_UPDATED
] = "updated",
92 [WT_STATUS_CHANGED
] = "changed",
93 [WT_STATUS_UNTRACKED
] = "untracked",
94 [WT_STATUS_NOBRANCH
] = "noBranch",
95 [WT_STATUS_UNMERGED
] = "unmerged",
96 [WT_STATUS_LOCAL_BRANCH
] = "localBranch",
97 [WT_STATUS_REMOTE_BRANCH
] = "remoteBranch",
98 [WT_STATUS_ONBRANCH
] = "branch",
101 static const char *use_message_buffer
;
102 static struct lock_file index_lock
; /* real index */
103 static struct lock_file false_lock
; /* used only for partial commits */
110 static const char *force_author
;
111 static char *logfile
;
112 static char *template_file
;
114 * The _message variables are commit names from which to take
115 * the commit message and/or authorship.
117 static const char *author_message
, *author_message_buffer
;
118 static const char *edit_message
, *use_message
;
119 static char *fixup_message
, *fixup_commit
, *squash_message
;
120 static const char *fixup_prefix
;
121 static int all
, also
, interactive
, patch_interactive
, only
, amend
, signoff
;
122 static int edit_flag
= -1; /* unspecified */
123 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
124 static int config_commit_verbose
= -1; /* unspecified */
125 static int no_post_rewrite
, allow_empty_message
, pathspec_file_nul
;
126 static const char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
, *ignored_arg
;
127 static const char *sign_commit
, *pathspec_from_file
;
128 static struct strvec trailer_args
= STRVEC_INIT
;
131 * The default commit message cleanup mode will remove the lines
132 * beginning with # (shell comments) and leading and trailing
133 * whitespaces (empty lines or containing only whitespaces)
134 * if editor is used, and only the whitespaces if the message
135 * is specified explicitly.
137 static enum commit_msg_cleanup_mode cleanup_mode
;
138 static char *cleanup_config
;
140 static enum commit_whence whence
;
141 static int use_editor
= 1, include_status
= 1;
142 static int have_option_m
;
143 static struct strbuf message
= STRBUF_INIT
;
145 static enum wt_status_format status_format
= STATUS_FORMAT_UNSPECIFIED
;
147 static int opt_parse_porcelain(const struct option
*opt
, const char *arg
, int unset
)
149 enum wt_status_format
*value
= (enum wt_status_format
*)opt
->value
;
151 *value
= STATUS_FORMAT_NONE
;
153 *value
= STATUS_FORMAT_PORCELAIN
;
154 else if (!strcmp(arg
, "v1") || !strcmp(arg
, "1"))
155 *value
= STATUS_FORMAT_PORCELAIN
;
156 else if (!strcmp(arg
, "v2") || !strcmp(arg
, "2"))
157 *value
= STATUS_FORMAT_PORCELAIN_V2
;
159 die("unsupported porcelain version '%s'", arg
);
164 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
166 struct strbuf
*buf
= opt
->value
;
169 strbuf_setlen(buf
, 0);
173 strbuf_addch(buf
, '\n');
174 strbuf_addstr(buf
, arg
);
175 strbuf_complete_line(buf
);
180 static int opt_parse_rename_score(const struct option
*opt
, const char *arg
, int unset
)
182 const char **value
= opt
->value
;
184 BUG_ON_OPT_NEG(unset
);
186 if (arg
!= NULL
&& *arg
== '=')
193 static void determine_whence(struct wt_status
*s
)
195 if (file_exists(git_path_merge_head(the_repository
)))
197 else if (!sequencer_determine_whence(the_repository
, &whence
))
198 whence
= FROM_COMMIT
;
203 static void status_init_config(struct wt_status
*s
, config_fn_t fn
)
205 wt_status_prepare(the_repository
, s
);
206 init_diff_ui_defaults();
209 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
); /* must come after git_config() */
212 static void rollback_index_files(void)
214 switch (commit_style
) {
216 break; /* nothing to do */
218 rollback_lock_file(&index_lock
);
221 rollback_lock_file(&index_lock
);
222 rollback_lock_file(&false_lock
);
227 static int commit_index_files(void)
231 switch (commit_style
) {
233 break; /* nothing to do */
235 err
= commit_lock_file(&index_lock
);
238 err
= commit_lock_file(&index_lock
);
239 rollback_lock_file(&false_lock
);
247 * Take a union of paths in the index and the named tree (typically, "HEAD"),
248 * and return the paths that match the given pattern in list.
250 static int list_paths(struct string_list
*list
, const char *with_tree
,
251 const struct pathspec
*pattern
)
259 m
= xcalloc(1, pattern
->nr
);
262 char *max_prefix
= common_prefix(pattern
);
263 overlay_tree_on_index(the_repository
->index
, with_tree
, max_prefix
);
267 /* TODO: audit for interaction with sparse-index. */
268 ensure_full_index(the_repository
->index
);
269 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++) {
270 const struct cache_entry
*ce
= the_repository
->index
->cache
[i
];
271 struct string_list_item
*item
;
273 if (ce
->ce_flags
& CE_UPDATE
)
275 if (!ce_path_match(the_repository
->index
, ce
, pattern
, m
))
277 item
= string_list_insert(list
, ce
->name
);
278 if (ce_skip_worktree(ce
))
279 item
->util
= item
; /* better a valid pointer than a fake one */
282 ret
= report_path_error(m
, pattern
);
287 static void add_remove_files(struct string_list
*list
)
290 for (i
= 0; i
< list
->nr
; i
++) {
292 struct string_list_item
*p
= &(list
->items
[i
]);
294 /* p->util is skip-worktree */
298 if (!lstat(p
->string
, &st
)) {
299 if (add_to_index(the_repository
->index
, p
->string
, &st
, 0))
300 die(_("updating files failed"));
302 remove_file_from_index(the_repository
->index
, p
->string
);
306 static void create_base_index(const struct commit
*current_head
)
309 struct unpack_trees_options opts
;
313 discard_index(the_repository
->index
);
317 memset(&opts
, 0, sizeof(opts
));
321 opts
.src_index
= the_repository
->index
;
322 opts
.dst_index
= the_repository
->index
;
324 opts
.fn
= oneway_merge
;
325 tree
= parse_tree_indirect(¤t_head
->object
.oid
);
327 die(_("failed to unpack HEAD tree object"));
328 if (parse_tree(tree
) < 0)
330 init_tree_desc(&t
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
331 if (unpack_trees(1, &t
, &opts
))
332 exit(128); /* We've already reported the error, finish dying */
335 static void refresh_cache_or_die(int refresh_flags
)
338 * refresh_flags contains REFRESH_QUIET, so the only errors
339 * are for unmerged entries.
341 if (refresh_index(the_repository
->index
, refresh_flags
| REFRESH_IN_PORCELAIN
, NULL
, NULL
, NULL
))
342 die_resolve_conflict("commit");
345 static const char *prepare_index(const char **argv
, const char *prefix
,
346 const struct commit
*current_head
, int is_status
)
348 struct string_list partial
= STRING_LIST_INIT_DUP
;
349 struct pathspec pathspec
;
350 int refresh_flags
= REFRESH_QUIET
;
354 refresh_flags
|= REFRESH_UNMERGED
;
355 parse_pathspec(&pathspec
, 0,
356 PATHSPEC_PREFER_FULL
,
359 if (pathspec_from_file
) {
361 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
364 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
367 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
369 parse_pathspec_file(&pathspec
, 0,
370 PATHSPEC_PREFER_FULL
,
371 prefix
, pathspec_from_file
, pathspec_file_nul
);
372 } else if (pathspec_file_nul
) {
373 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
376 if (!pathspec
.nr
&& (also
|| (only
&& !allow_empty
&&
377 (!amend
|| (fixup_message
&& strcmp(fixup_prefix
, "amend"))))))
378 die(_("No paths with --include/--only does not make sense."));
380 if (repo_read_index_preload(the_repository
, &pathspec
, 0) < 0)
381 die(_("index file corrupt"));
384 char *old_index_env
= NULL
, *old_repo_index_file
;
385 repo_hold_locked_index(the_repository
, &index_lock
,
388 refresh_cache_or_die(refresh_flags
);
390 if (write_locked_index(the_repository
->index
, &index_lock
, 0))
391 die(_("unable to create temporary index"));
393 old_repo_index_file
= the_repository
->index_file
;
394 the_repository
->index_file
=
395 (char *)get_lock_file_path(&index_lock
);
396 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
397 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
399 if (interactive_add(the_repository
, argv
, prefix
, patch_interactive
) != 0)
400 die(_("interactive add failed"));
402 the_repository
->index_file
= old_repo_index_file
;
403 if (old_index_env
&& *old_index_env
)
404 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
406 unsetenv(INDEX_ENVIRONMENT
);
407 FREE_AND_NULL(old_index_env
);
409 discard_index(the_repository
->index
);
410 read_index_from(the_repository
->index
, get_lock_file_path(&index_lock
),
411 repo_get_git_dir(the_repository
));
412 if (cache_tree_update(the_repository
->index
, WRITE_TREE_SILENT
) == 0) {
413 if (reopen_lock_file(&index_lock
) < 0)
414 die(_("unable to write index file"));
415 if (write_locked_index(the_repository
->index
, &index_lock
, 0))
416 die(_("unable to update temporary index"));
418 warning(_("Failed to update main cache tree"));
420 commit_style
= COMMIT_NORMAL
;
421 ret
= get_lock_file_path(&index_lock
);
426 * Non partial, non as-is commit.
428 * (1) get the real index;
429 * (2) update the_index as necessary;
430 * (3) write the_index out to the real index (still locked);
431 * (4) return the name of the locked index file.
433 * The caller should run hooks on the locked real index, and
434 * (A) if all goes well, commit the real index;
435 * (B) on failure, rollback the real index.
437 if (all
|| (also
&& pathspec
.nr
)) {
438 char *ps_matched
= xcalloc(pathspec
.nr
, 1);
439 repo_hold_locked_index(the_repository
, &index_lock
,
441 add_files_to_cache(the_repository
, also
? prefix
: NULL
,
442 &pathspec
, ps_matched
, 0, 0);
443 if (!all
&& report_path_error(ps_matched
, &pathspec
))
446 refresh_cache_or_die(refresh_flags
);
447 cache_tree_update(the_repository
->index
, WRITE_TREE_SILENT
);
448 if (write_locked_index(the_repository
->index
, &index_lock
, 0))
449 die(_("unable to write new index file"));
450 commit_style
= COMMIT_NORMAL
;
451 ret
= get_lock_file_path(&index_lock
);
459 * (1) return the name of the real index file.
461 * The caller should run hooks on the real index,
462 * and create commit from the_index.
463 * We still need to refresh the index here.
465 if (!only
&& !pathspec
.nr
) {
466 repo_hold_locked_index(the_repository
, &index_lock
,
468 refresh_cache_or_die(refresh_flags
);
469 if (the_repository
->index
->cache_changed
470 || !cache_tree_fully_valid(the_repository
->index
->cache_tree
))
471 cache_tree_update(the_repository
->index
, WRITE_TREE_SILENT
);
472 if (write_locked_index(the_repository
->index
, &index_lock
,
473 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
474 die(_("unable to write new index file"));
475 commit_style
= COMMIT_AS_IS
;
476 ret
= repo_get_index_file(the_repository
);
483 * (0) find the set of affected paths;
484 * (1) get lock on the real index file;
485 * (2) update the_index with the given paths;
486 * (3) write the_index out to the real index (still locked);
487 * (4) get lock on the false index file;
488 * (5) reset the_index from HEAD;
489 * (6) update the_index the same way as (2);
490 * (7) write the_index out to the false index file;
491 * (8) return the name of the false index file (still locked);
493 * The caller should run hooks on the locked false index, and
494 * create commit from it. Then
495 * (A) if all goes well, commit the real index;
496 * (B) on failure, rollback the real index;
497 * In either case, rollback the false index.
499 commit_style
= COMMIT_PARTIAL
;
501 if (whence
!= FROM_COMMIT
) {
502 if (whence
== FROM_MERGE
)
503 die(_("cannot do a partial commit during a merge."));
504 else if (is_from_cherry_pick(whence
))
505 die(_("cannot do a partial commit during a cherry-pick."));
506 else if (is_from_rebase(whence
))
507 die(_("cannot do a partial commit during a rebase."));
510 if (list_paths(&partial
, !current_head
? NULL
: "HEAD", &pathspec
))
513 discard_index(the_repository
->index
);
514 if (repo_read_index(the_repository
) < 0)
515 die(_("cannot read the index"));
517 repo_hold_locked_index(the_repository
, &index_lock
, LOCK_DIE_ON_ERROR
);
518 add_remove_files(&partial
);
519 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
520 cache_tree_update(the_repository
->index
, WRITE_TREE_SILENT
);
521 if (write_locked_index(the_repository
->index
, &index_lock
, 0))
522 die(_("unable to write new index file"));
524 hold_lock_file_for_update(&false_lock
,
525 git_path("next-index-%"PRIuMAX
,
526 (uintmax_t) getpid()),
529 create_base_index(current_head
);
530 add_remove_files(&partial
);
531 refresh_index(the_repository
->index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
533 if (write_locked_index(the_repository
->index
, &false_lock
, 0))
534 die(_("unable to write temporary index file"));
536 discard_index(the_repository
->index
);
537 ret
= get_lock_file_path(&false_lock
);
538 read_index_from(the_repository
->index
, ret
, repo_get_git_dir(the_repository
));
540 string_list_clear(&partial
, 0);
541 clear_pathspec(&pathspec
);
545 static int run_status(FILE *fp
, const char *index_file
, const char *prefix
, int nowarn
,
548 struct object_id oid
;
550 if (s
->relative_paths
)
555 s
->reference
= "HEAD^1";
557 s
->verbose
= verbose
;
558 s
->index_file
= index_file
;
561 s
->is_initial
= repo_get_oid(the_repository
, s
->reference
, &oid
) ? 1 : 0;
563 oidcpy(&s
->oid_commit
, &oid
);
564 s
->status_format
= status_format
;
565 s
->ignore_submodule_arg
= ignore_submodule_arg
;
567 wt_status_collect(s
);
569 wt_status_collect_free_buffers(s
);
571 return s
->committable
;
574 static int is_a_merge(const struct commit
*current_head
)
576 return !!(current_head
->parents
&& current_head
->parents
->next
);
579 static void assert_split_ident(struct ident_split
*id
, const struct strbuf
*buf
)
581 if (split_ident_line(id
, buf
->buf
, buf
->len
) || !id
->date_begin
)
582 BUG("unable to parse our own ident: %s", buf
->buf
);
585 static void export_one(const char *var
, const char *s
, const char *e
, int hack
)
587 struct strbuf buf
= STRBUF_INIT
;
589 strbuf_addch(&buf
, hack
);
590 strbuf_add(&buf
, s
, e
- s
);
591 setenv(var
, buf
.buf
, 1);
592 strbuf_release(&buf
);
595 static int parse_force_date(const char *in
, struct strbuf
*out
)
597 strbuf_addch(out
, '@');
599 if (parse_date(in
, out
) < 0) {
601 unsigned long t
= approxidate_careful(in
, &errors
);
604 strbuf_addf(out
, "%lu", t
);
610 static void set_ident_var(char **buf
, char *val
)
616 static void determine_author_info(struct strbuf
*author_ident
)
618 char *name
, *email
, *date
;
619 struct ident_split author
;
621 name
= xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
622 email
= xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
623 date
= xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
625 if (author_message
) {
626 struct ident_split ident
;
630 a
= find_commit_header(author_message_buffer
, "author", &len
);
632 die(_("commit '%s' lacks author header"), author_message
);
633 if (split_ident_line(&ident
, a
, len
) < 0)
634 die(_("commit '%s' has malformed author line"), author_message
);
636 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
637 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
639 if (ident
.date_begin
) {
640 struct strbuf date_buf
= STRBUF_INIT
;
641 strbuf_addch(&date_buf
, '@');
642 strbuf_add(&date_buf
, ident
.date_begin
, ident
.date_end
- ident
.date_begin
);
643 strbuf_addch(&date_buf
, ' ');
644 strbuf_add(&date_buf
, ident
.tz_begin
, ident
.tz_end
- ident
.tz_begin
);
645 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
650 struct ident_split ident
;
652 if (split_ident_line(&ident
, force_author
, strlen(force_author
)) < 0)
653 die(_("malformed --author parameter"));
654 set_ident_var(&name
, xmemdupz(ident
.name_begin
, ident
.name_end
- ident
.name_begin
));
655 set_ident_var(&email
, xmemdupz(ident
.mail_begin
, ident
.mail_end
- ident
.mail_begin
));
659 struct strbuf date_buf
= STRBUF_INIT
;
660 if (parse_force_date(force_date
, &date_buf
))
661 die(_("invalid date format: %s"), force_date
);
662 set_ident_var(&date
, strbuf_detach(&date_buf
, NULL
));
665 strbuf_addstr(author_ident
, fmt_ident(name
, email
, WANT_AUTHOR_IDENT
, date
,
667 assert_split_ident(&author
, author_ident
);
668 export_one("GIT_AUTHOR_NAME", author
.name_begin
, author
.name_end
, 0);
669 export_one("GIT_AUTHOR_EMAIL", author
.mail_begin
, author
.mail_end
, 0);
670 export_one("GIT_AUTHOR_DATE", author
.date_begin
, author
.tz_end
, '@');
676 static int author_date_is_interesting(void)
678 return author_message
|| force_date
;
681 static void adjust_comment_line_char(const struct strbuf
*sb
)
683 char candidates
[] = "#;@!$%^&|:";
687 if (!memchr(sb
->buf
, candidates
[0], sb
->len
)) {
688 free(comment_line_str_to_free
);
689 comment_line_str
= comment_line_str_to_free
=
690 xstrfmt("%c", candidates
[0]);
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 free(comment_line_str_to_free
);
712 comment_line_str
= comment_line_str_to_free
= xstrfmt("%c", *p
);
715 static void prepare_amend_commit(struct commit
*commit
, struct strbuf
*sb
,
716 struct pretty_print_context
*ctx
)
718 const char *buffer
, *subject
, *fmt
;
720 buffer
= repo_get_commit_buffer(the_repository
, commit
, NULL
);
721 find_commit_subject(buffer
, &subject
);
723 * If we amend the 'amend!' commit then we don't want to
724 * duplicate the subject line.
726 fmt
= starts_with(subject
, "amend!") ? "%b" : "%B";
727 repo_format_commit_message(the_repository
, commit
, fmt
, sb
, ctx
);
728 repo_unuse_commit_buffer(the_repository
, commit
, buffer
);
731 static void change_data_free(void *util
, const char *str UNUSED
)
733 struct wt_status_change_data
*d
= util
;
734 free(d
->rename_source
);
738 static int prepare_to_commit(const char *index_file
, const char *prefix
,
739 struct commit
*current_head
,
741 struct strbuf
*author_ident
)
744 struct strbuf committer_ident
= STRBUF_INIT
;
746 struct strbuf sb
= STRBUF_INIT
;
747 const char *hook_arg1
= NULL
;
748 const char *hook_arg2
= NULL
;
749 int clean_message_contents
= (cleanup_mode
!= COMMIT_MSG_CLEANUP_NONE
);
750 int old_display_comment_prefix
;
753 /* This checks and barfs if author is badly specified */
754 determine_author_info(author_ident
);
756 if (!no_verify
&& run_commit_hook(use_editor
, index_file
, &invoked_hook
,
760 if (squash_message
) {
762 * Insert the proper subject line before other commit
763 * message options add their content.
765 if (use_message
&& !strcmp(use_message
, squash_message
))
766 strbuf_addstr(&sb
, "squash! ");
768 struct pretty_print_context ctx
= {0};
770 c
= lookup_commit_reference_by_name(squash_message
);
772 die(_("could not lookup commit '%s'"), squash_message
);
773 ctx
.output_encoding
= get_commit_output_encoding();
774 repo_format_commit_message(the_repository
, c
,
775 "squash! %s\n\n", &sb
,
780 if (have_option_m
&& !fixup_message
) {
781 strbuf_addbuf(&sb
, &message
);
782 hook_arg1
= "message";
783 } else if (logfile
&& !strcmp(logfile
, "-")) {
785 fprintf(stderr
, _("(reading log message from standard input)\n"));
786 if (strbuf_read(&sb
, 0, 0) < 0)
787 die_errno(_("could not read log from standard input"));
788 hook_arg1
= "message";
789 } else if (logfile
) {
790 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
791 die_errno(_("could not read log file '%s'"),
793 hook_arg1
= "message";
794 } else if (use_message
) {
796 buffer
= strstr(use_message_buffer
, "\n\n");
798 strbuf_addstr(&sb
, skip_blank_lines(buffer
+ 2));
799 hook_arg1
= "commit";
800 hook_arg2
= use_message
;
801 } else if (fixup_message
) {
802 struct pretty_print_context ctx
= {0};
803 struct commit
*commit
;
805 commit
= lookup_commit_reference_by_name(fixup_commit
);
807 die(_("could not lookup commit '%s'"), fixup_commit
);
808 ctx
.output_encoding
= get_commit_output_encoding();
809 fmt
= xstrfmt("%s! %%s\n\n", fixup_prefix
);
810 repo_format_commit_message(the_repository
, commit
, fmt
, &sb
,
813 hook_arg1
= "message";
816 * Only `-m` commit message option is checked here, as
817 * it supports `--fixup` to append the commit message.
819 * The other commit message options `-c`/`-C`/`-F` are
820 * incompatible with all the forms of `--fixup` and
821 * have already errored out while parsing the `git commit`
824 if (have_option_m
&& !strcmp(fixup_prefix
, "fixup"))
825 strbuf_addbuf(&sb
, &message
);
827 if (!strcmp(fixup_prefix
, "amend")) {
829 die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message
);
830 prepare_amend_commit(commit
, &sb
, &ctx
);
832 } else if (!stat(git_path_merge_msg(the_repository
), &statbuf
)) {
833 size_t merge_msg_start
;
836 * prepend SQUASH_MSG here if it exists and a
837 * "merge --squash" was originally performed
839 if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
840 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
841 die_errno(_("could not read SQUASH_MSG"));
842 hook_arg1
= "squash";
846 merge_msg_start
= sb
.len
;
847 if (strbuf_read_file(&sb
, git_path_merge_msg(the_repository
), 0) < 0)
848 die_errno(_("could not read MERGE_MSG"));
850 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
&&
851 wt_status_locate_end(sb
.buf
+ merge_msg_start
,
852 sb
.len
- merge_msg_start
) <
853 sb
.len
- merge_msg_start
)
854 s
->added_cut_line
= 1;
855 } else if (!stat(git_path_squash_msg(the_repository
), &statbuf
)) {
856 if (strbuf_read_file(&sb
, git_path_squash_msg(the_repository
), 0) < 0)
857 die_errno(_("could not read SQUASH_MSG"));
858 hook_arg1
= "squash";
859 } else if (template_file
) {
860 if (strbuf_read_file(&sb
, template_file
, 0) < 0)
861 die_errno(_("could not read '%s'"), template_file
);
862 hook_arg1
= "template";
863 clean_message_contents
= 0;
867 * The remaining cases don't modify the template message, but
868 * just set the argument(s) to the prepare-commit-msg hook.
870 else if (whence
== FROM_MERGE
)
872 else if (is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) {
873 hook_arg1
= "commit";
874 hook_arg2
= "CHERRY_PICK_HEAD";
877 if (squash_message
) {
879 * If squash_commit was used for the commit subject,
880 * then we're possibly hijacking other commit log options.
881 * Reset the hook args to tell the real story.
883 hook_arg1
= "message";
887 s
->fp
= fopen_for_writing(git_path_commit_editmsg());
889 die_errno(_("could not open '%s'"), git_path_commit_editmsg());
891 /* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
892 old_display_comment_prefix
= s
->display_comment_prefix
;
893 s
->display_comment_prefix
= 1;
896 * Most hints are counter-productive when the commit has
901 if (clean_message_contents
)
902 strbuf_stripspace(&sb
, NULL
);
905 append_signoff(&sb
, ignored_log_message_bytes(sb
.buf
, sb
.len
), 0);
907 if (fwrite(sb
.buf
, 1, sb
.len
, s
->fp
) < sb
.len
)
908 die_errno(_("could not write commit template"));
910 if (auto_comment_line_char
)
911 adjust_comment_line_char(&sb
);
914 /* This checks if committer ident is explicitly given */
915 strbuf_addstr(&committer_ident
, git_committer_info(IDENT_STRICT
));
916 if (use_editor
&& include_status
) {
918 int saved_color_setting
;
919 struct ident_split ci
, ai
;
920 const char *hint_cleanup_all
= allow_empty_message
?
921 _("Please enter the commit message for your changes."
922 " Lines starting\nwith '%s' will be ignored.\n") :
923 _("Please enter the commit message for your changes."
924 " Lines starting\nwith '%s' will be ignored, and an empty"
925 " message aborts the commit.\n");
926 const char *hint_cleanup_space
= allow_empty_message
?
927 _("Please enter the commit message for your changes."
929 "with '%s' will be kept; you may remove them"
930 " yourself if you want to.\n") :
931 _("Please enter the commit message for your changes."
933 "with '%s' will be kept; you may remove them"
934 " yourself if you want to.\n"
935 "An empty message aborts the commit.\n");
936 if (whence
!= FROM_COMMIT
) {
937 if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
)
938 wt_status_add_cut_line(s
);
941 whence
== FROM_MERGE
?
943 "It looks like you may be committing a merge.\n"
944 "If this is not correct, please run\n"
945 " git update-ref -d MERGE_HEAD\n"
946 "and try again.\n") :
948 "It looks like you may be committing a cherry-pick.\n"
949 "If this is not correct, please run\n"
950 " git update-ref -d CHERRY_PICK_HEAD\n"
951 "and try again.\n"));
954 fprintf(s
->fp
, "\n");
955 if (cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
)
956 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_all
, comment_line_str
);
957 else if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
958 if (whence
== FROM_COMMIT
)
959 wt_status_add_cut_line(s
);
960 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
961 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_space
, comment_line_str
);
964 * These should never fail because they come from our own
965 * fmt_ident. They may fail the sane_ident test, but we know
966 * that the name and mail pointers will at least be valid,
967 * which is enough for our tests and printing here.
969 assert_split_ident(&ai
, author_ident
);
970 assert_split_ident(&ci
, &committer_ident
);
972 if (ident_cmp(&ai
, &ci
))
973 status_printf_ln(s
, GIT_COLOR_NORMAL
,
975 "Author: %.*s <%.*s>"),
976 ident_shown
++ ? "" : "\n",
977 (int)(ai
.name_end
- ai
.name_begin
), ai
.name_begin
,
978 (int)(ai
.mail_end
- ai
.mail_begin
), ai
.mail_begin
);
980 if (author_date_is_interesting())
981 status_printf_ln(s
, GIT_COLOR_NORMAL
,
984 ident_shown
++ ? "" : "\n",
985 show_ident_date(&ai
, DATE_MODE(NORMAL
)));
987 if (!committer_ident_sufficiently_given())
988 status_printf_ln(s
, GIT_COLOR_NORMAL
,
990 "Committer: %.*s <%.*s>"),
991 ident_shown
++ ? "" : "\n",
992 (int)(ci
.name_end
- ci
.name_begin
), ci
.name_begin
,
993 (int)(ci
.mail_end
- ci
.mail_begin
), ci
.mail_begin
);
995 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", ""); /* Add new line for clarity */
997 saved_color_setting
= s
->use_color
;
999 committable
= run_status(s
->fp
, index_file
, prefix
, 1, s
);
1000 s
->use_color
= saved_color_setting
;
1001 string_list_clear_func(&s
->change
, change_data_free
);
1003 struct object_id oid
;
1004 const char *parent
= "HEAD";
1006 if (!the_repository
->index
->initialized
&& repo_read_index(the_repository
) < 0)
1007 die(_("Cannot read index"));
1012 if (repo_get_oid(the_repository
, parent
, &oid
)) {
1015 /* TODO: audit for interaction with sparse-index. */
1016 ensure_full_index(the_repository
->index
);
1017 for (i
= 0; i
< the_repository
->index
->cache_nr
; i
++)
1018 if (ce_intent_to_add(the_repository
->index
->cache
[i
]))
1020 committable
= the_repository
->index
->cache_nr
- ita_nr
> 0;
1023 * Unless the user did explicitly request a submodule
1024 * ignore mode by passing a command line option we do
1025 * not ignore any changed submodule SHA-1s when
1026 * comparing index and parent, no matter what is
1027 * configured. Otherwise we won't commit any
1028 * submodules which were manually staged, which would
1029 * be really confusing.
1031 struct diff_flags flags
= DIFF_FLAGS_INIT
;
1032 flags
.override_submodule_config
= 1;
1033 if (ignore_submodule_arg
&&
1034 !strcmp(ignore_submodule_arg
, "all"))
1035 flags
.ignore_submodules
= 1;
1036 committable
= index_differs_from(the_repository
,
1040 strbuf_release(&committer_ident
);
1044 if (trailer_args
.nr
) {
1045 if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args
))
1046 die(_("unable to pass trailers to --trailers"));
1047 strvec_clear(&trailer_args
);
1051 * Reject an attempt to record a non-merge empty commit without
1052 * explicit --allow-empty. In the cherry-pick case, it may be
1053 * empty due to conflict resolution, which the user should okay.
1055 if (!committable
&& whence
!= FROM_MERGE
&& !allow_empty
&&
1056 !(amend
&& is_a_merge(current_head
))) {
1057 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
);
1058 s
->display_comment_prefix
= old_display_comment_prefix
;
1059 run_status(stdout
, index_file
, prefix
, 0, s
);
1061 fputs(_(empty_amend_advice
), stderr
);
1062 else if (is_from_cherry_pick(whence
) ||
1063 whence
== FROM_REBASE_PICK
) {
1064 fputs(_(empty_cherry_pick_advice
), stderr
);
1065 if (whence
== FROM_CHERRY_PICK_SINGLE
)
1066 fputs(_(empty_cherry_pick_advice_single
), stderr
);
1067 else if (whence
== FROM_CHERRY_PICK_MULTI
)
1068 fputs(_(empty_cherry_pick_advice_multi
), stderr
);
1070 fputs(_(empty_rebase_pick_advice
), stderr
);
1075 if (!no_verify
&& invoked_hook
) {
1077 * Re-read the index as the pre-commit-commit hook was invoked
1078 * and could have updated it. We must do this before we invoke
1079 * the editor and after we invoke run_status above.
1081 discard_index(the_repository
->index
);
1083 read_index_from(the_repository
->index
, index_file
, repo_get_git_dir(the_repository
));
1085 if (cache_tree_update(the_repository
->index
, 0)) {
1086 error(_("Error building trees"));
1090 if (run_commit_hook(use_editor
, index_file
, NULL
, "prepare-commit-msg",
1091 git_path_commit_editmsg(), hook_arg1
, hook_arg2
, NULL
))
1095 struct strvec env
= STRVEC_INIT
;
1097 strvec_pushf(&env
, "GIT_INDEX_FILE=%s", index_file
);
1098 if (launch_editor(git_path_commit_editmsg(), NULL
, env
.v
)) {
1100 _("Please supply the message using either -m or -F option.\n"));
1107 run_commit_hook(use_editor
, index_file
, NULL
, "commit-msg",
1108 git_path_commit_editmsg(), NULL
)) {
1115 static const char *find_author_by_nickname(const char *name
)
1117 struct rev_info revs
;
1118 struct commit
*commit
;
1119 struct strbuf buf
= STRBUF_INIT
;
1123 repo_init_revisions(the_repository
, &revs
, NULL
);
1124 strbuf_addf(&buf
, "--author=%s", name
);
1129 setup_revisions(ac
, av
, &revs
, NULL
);
1130 revs
.mailmap
= xmalloc(sizeof(struct string_list
));
1131 string_list_init_nodup(revs
.mailmap
);
1132 read_mailmap(revs
.mailmap
);
1134 if (prepare_revision_walk(&revs
))
1135 die(_("revision walk setup failed"));
1136 commit
= get_revision(&revs
);
1138 struct pretty_print_context ctx
= {0};
1139 ctx
.date_mode
.type
= DATE_NORMAL
;
1140 strbuf_release(&buf
);
1141 repo_format_commit_message(the_repository
, commit
,
1142 "%aN <%aE>", &buf
, &ctx
);
1143 release_revisions(&revs
);
1144 return strbuf_detach(&buf
, NULL
);
1146 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name
);
1149 static void handle_ignored_arg(struct wt_status
*s
)
1152 ; /* default already initialized */
1153 else if (!strcmp(ignored_arg
, "traditional"))
1154 s
->show_ignored_mode
= SHOW_TRADITIONAL_IGNORED
;
1155 else if (!strcmp(ignored_arg
, "no"))
1156 s
->show_ignored_mode
= SHOW_NO_IGNORED
;
1157 else if (!strcmp(ignored_arg
, "matching"))
1158 s
->show_ignored_mode
= SHOW_MATCHING_IGNORED
;
1160 die(_("Invalid ignored mode '%s'"), ignored_arg
);
1163 static enum untracked_status_type
parse_untracked_setting_name(const char *u
)
1166 * Please update $__git_untracked_file_modes in
1167 * git-completion.bash when you add new options
1169 switch (git_parse_maybe_bool(u
)) {
1180 if (!strcmp(u
, "no"))
1181 return SHOW_NO_UNTRACKED_FILES
;
1182 else if (!strcmp(u
, "normal"))
1183 return SHOW_NORMAL_UNTRACKED_FILES
;
1184 else if (!strcmp(u
, "all"))
1185 return SHOW_ALL_UNTRACKED_FILES
;
1187 return SHOW_UNTRACKED_FILES_ERROR
;
1190 static void handle_untracked_files_arg(struct wt_status
*s
)
1192 enum untracked_status_type u
;
1194 if (!untracked_files_arg
)
1195 return; /* default already initialized */
1197 u
= parse_untracked_setting_name(untracked_files_arg
);
1198 if (u
== SHOW_UNTRACKED_FILES_ERROR
)
1199 die(_("Invalid untracked files mode '%s'"),
1200 untracked_files_arg
);
1201 s
->show_untracked_files
= u
;
1204 static const char *read_commit_message(const char *name
)
1206 const char *out_enc
;
1207 struct commit
*commit
;
1209 commit
= lookup_commit_reference_by_name(name
);
1211 die(_("could not lookup commit '%s'"), name
);
1212 out_enc
= get_commit_output_encoding();
1213 return repo_logmsg_reencode(the_repository
, commit
, NULL
, out_enc
);
1217 * Enumerate what needs to be propagated when --porcelain
1218 * is not in effect here.
1220 static struct status_deferred_config
{
1221 enum wt_status_format status_format
;
1223 enum ahead_behind_flags ahead_behind
;
1224 } status_deferred_config
= {
1225 STATUS_FORMAT_UNSPECIFIED
,
1226 -1, /* unspecified */
1227 AHEAD_BEHIND_UNSPECIFIED
,
1230 static void finalize_deferred_config(struct wt_status
*s
)
1232 int use_deferred_config
= (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1233 status_format
!= STATUS_FORMAT_PORCELAIN_V2
&&
1234 !s
->null_termination
);
1236 if (s
->null_termination
) {
1237 if (status_format
== STATUS_FORMAT_NONE
||
1238 status_format
== STATUS_FORMAT_UNSPECIFIED
)
1239 status_format
= STATUS_FORMAT_PORCELAIN
;
1240 else if (status_format
== STATUS_FORMAT_LONG
)
1241 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
1244 if (use_deferred_config
&& status_format
== STATUS_FORMAT_UNSPECIFIED
)
1245 status_format
= status_deferred_config
.status_format
;
1246 if (status_format
== STATUS_FORMAT_UNSPECIFIED
)
1247 status_format
= STATUS_FORMAT_NONE
;
1249 if (use_deferred_config
&& s
->show_branch
< 0)
1250 s
->show_branch
= status_deferred_config
.show_branch
;
1251 if (s
->show_branch
< 0)
1255 * If the user did not give a "--[no]-ahead-behind" command
1256 * line argument *AND* we will print in a human-readable format
1257 * (short, long etc.) then we inherit from the status.aheadbehind
1258 * config setting. In all other cases (and porcelain V[12] formats
1259 * in particular), we inherit _FULL for backwards compatibility.
1261 if (use_deferred_config
&&
1262 s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1263 s
->ahead_behind_flags
= status_deferred_config
.ahead_behind
;
1265 if (s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1266 s
->ahead_behind_flags
= AHEAD_BEHIND_FULL
;
1269 static void check_fixup_reword_options(int argc
, const char *argv
[]) {
1270 if (whence
!= FROM_COMMIT
) {
1271 if (whence
== FROM_MERGE
)
1272 die(_("You are in the middle of a merge -- cannot reword."));
1273 else if (is_from_cherry_pick(whence
))
1274 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1277 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv
);
1278 if (patch_interactive
|| interactive
|| all
|| also
|| only
)
1279 die(_("reword option of '%s' and '%s' cannot be used together"),
1280 "--fixup", "--patch/--interactive/--all/--include/--only");
1283 static int parse_and_validate_options(int argc
, const char *argv
[],
1284 const struct option
*options
,
1285 const char * const usage
[],
1287 struct commit
*current_head
,
1288 struct wt_status
*s
)
1290 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1291 finalize_deferred_config(s
);
1293 if (force_author
&& !strchr(force_author
, '>'))
1294 force_author
= find_author_by_nickname(force_author
);
1296 if (force_author
&& renew_authorship
)
1297 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
1299 if (logfile
|| have_option_m
|| use_message
)
1302 /* Sanity check options */
1303 if (amend
&& !current_head
)
1304 die(_("You have nothing to amend."));
1305 if (amend
&& whence
!= FROM_COMMIT
) {
1306 if (whence
== FROM_MERGE
)
1307 die(_("You are in the middle of a merge -- cannot amend."));
1308 else if (is_from_cherry_pick(whence
))
1309 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1310 else if (whence
== FROM_REBASE_PICK
)
1311 die(_("You are in the middle of a rebase -- cannot amend."));
1313 if (fixup_message
&& squash_message
)
1314 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1315 die_for_incompatible_opt4(!!use_message
, "-C",
1316 !!edit_message
, "-c",
1318 !!fixup_message
, "--fixup");
1319 die_for_incompatible_opt4(have_option_m
, "-m",
1320 !!edit_message
, "-c",
1321 !!use_message
, "-C",
1323 if (use_message
|| edit_message
|| logfile
||fixup_message
|| have_option_m
)
1324 FREE_AND_NULL(template_file
);
1326 use_message
= edit_message
;
1327 if (amend
&& !use_message
&& !fixup_message
)
1328 use_message
= "HEAD";
1329 if (!use_message
&& !is_from_cherry_pick(whence
) &&
1330 !is_from_rebase(whence
) && renew_authorship
)
1331 die(_("--reset-author can be used only with -C, -c or --amend."));
1333 use_message_buffer
= read_commit_message(use_message
);
1334 if (!renew_authorship
) {
1335 author_message
= use_message
;
1336 author_message_buffer
= use_message_buffer
;
1339 if ((is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) &&
1340 !renew_authorship
) {
1341 author_message
= "CHERRY_PICK_HEAD";
1342 author_message_buffer
= read_commit_message(author_message
);
1345 if (patch_interactive
)
1348 die_for_incompatible_opt4(also
, "-i/--include",
1351 interactive
, "--interactive/-p/--patch");
1352 if (fixup_message
) {
1354 * We limit --fixup's suboptions to only alpha characters.
1355 * If the first character after a run of alpha is colon,
1356 * then the part before the colon may be a known suboption
1357 * name like `amend` or `reword`, or a misspelt suboption
1358 * name. In either case, we treat it as
1359 * --fixup=<suboption>:<arg>.
1361 * Otherwise, we are dealing with --fixup=<commit>.
1363 char *p
= fixup_message
;
1366 if (p
> fixup_message
&& *p
== ':') {
1368 fixup_commit
= p
+ 1;
1369 if (!strcmp("amend", fixup_message
) ||
1370 !strcmp("reword", fixup_message
)) {
1371 fixup_prefix
= "amend";
1373 if (*fixup_message
== 'r') {
1374 check_fixup_reword_options(argc
, argv
);
1378 die(_("unknown option: --fixup=%s:%s"), fixup_message
, fixup_commit
);
1381 fixup_commit
= fixup_message
;
1382 fixup_prefix
= "fixup";
1388 use_editor
= edit_flag
;
1390 handle_untracked_files_arg(s
);
1392 if (all
&& argc
> 0)
1393 die(_("paths '%s ...' with -a does not make sense"),
1396 if (status_format
!= STATUS_FORMAT_NONE
)
1402 static int dry_run_commit(const char **argv
, const char *prefix
,
1403 const struct commit
*current_head
, struct wt_status
*s
)
1406 const char *index_file
;
1408 index_file
= prepare_index(argv
, prefix
, current_head
, 1);
1409 committable
= run_status(stdout
, index_file
, prefix
, 0, s
);
1410 rollback_index_files();
1412 return committable
? 0 : 1;
1415 define_list_config_array_extra(color_status_slots
, {"added"});
1417 static int parse_status_slot(const char *slot
)
1419 if (!strcasecmp(slot
, "added"))
1420 return WT_STATUS_UPDATED
;
1422 return LOOKUP_CONFIG(color_status_slots
, slot
);
1425 static int git_status_config(const char *k
, const char *v
,
1426 const struct config_context
*ctx
, void *cb
)
1428 struct wt_status
*s
= cb
;
1429 const char *slot_name
;
1431 if (starts_with(k
, "column."))
1432 return git_column_config(k
, v
, "status", &s
->colopts
);
1433 if (!strcmp(k
, "status.submodulesummary")) {
1435 s
->submodule_summary
= git_config_bool_or_int(k
, v
, ctx
->kvi
,
1437 if (is_bool
&& s
->submodule_summary
)
1438 s
->submodule_summary
= -1;
1441 if (!strcmp(k
, "status.short")) {
1442 if (git_config_bool(k
, v
))
1443 status_deferred_config
.status_format
= STATUS_FORMAT_SHORT
;
1445 status_deferred_config
.status_format
= STATUS_FORMAT_NONE
;
1448 if (!strcmp(k
, "status.branch")) {
1449 status_deferred_config
.show_branch
= git_config_bool(k
, v
);
1452 if (!strcmp(k
, "status.aheadbehind")) {
1453 status_deferred_config
.ahead_behind
= git_config_bool(k
, v
);
1456 if (!strcmp(k
, "status.showstash")) {
1457 s
->show_stash
= git_config_bool(k
, v
);
1460 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1461 s
->use_color
= git_config_colorbool(k
, v
);
1464 if (!strcmp(k
, "status.displaycommentprefix")) {
1465 s
->display_comment_prefix
= git_config_bool(k
, v
);
1468 if (skip_prefix(k
, "status.color.", &slot_name
) ||
1469 skip_prefix(k
, "color.status.", &slot_name
)) {
1470 int slot
= parse_status_slot(slot_name
);
1474 return config_error_nonbool(k
);
1475 return color_parse(v
, s
->color_palette
[slot
]);
1477 if (!strcmp(k
, "status.relativepaths")) {
1478 s
->relative_paths
= git_config_bool(k
, v
);
1481 if (!strcmp(k
, "status.showuntrackedfiles")) {
1482 enum untracked_status_type u
;
1484 u
= parse_untracked_setting_name(v
);
1485 if (u
== SHOW_UNTRACKED_FILES_ERROR
)
1486 return error(_("Invalid untracked files mode '%s'"), v
);
1487 s
->show_untracked_files
= u
;
1490 if (!strcmp(k
, "diff.renamelimit")) {
1491 if (s
->rename_limit
== -1)
1492 s
->rename_limit
= git_config_int(k
, v
, ctx
->kvi
);
1495 if (!strcmp(k
, "status.renamelimit")) {
1496 s
->rename_limit
= git_config_int(k
, v
, ctx
->kvi
);
1499 if (!strcmp(k
, "diff.renames")) {
1500 if (s
->detect_rename
== -1)
1501 s
->detect_rename
= git_config_rename(k
, v
);
1504 if (!strcmp(k
, "status.renames")) {
1505 s
->detect_rename
= git_config_rename(k
, v
);
1508 return git_diff_ui_config(k
, v
, ctx
, NULL
);
1511 int cmd_status(int argc
,
1514 struct repository
*repo UNUSED
)
1516 static int no_renames
= -1;
1517 static const char *rename_score_arg
= (const char *)-1;
1518 static struct wt_status s
;
1519 unsigned int progress_flag
= 0;
1521 struct object_id oid
;
1522 static struct option builtin_status_options
[] = {
1523 OPT__VERBOSE(&verbose
, N_("be verbose")),
1524 OPT_SET_INT('s', "short", &status_format
,
1525 N_("show status concisely"), STATUS_FORMAT_SHORT
),
1526 OPT_BOOL('b', "branch", &s
.show_branch
,
1527 N_("show branch information")),
1528 OPT_BOOL(0, "show-stash", &s
.show_stash
,
1529 N_("show stash information")),
1530 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1531 N_("compute full ahead/behind values")),
1532 OPT_CALLBACK_F(0, "porcelain", &status_format
,
1533 N_("version"), N_("machine-readable output"),
1534 PARSE_OPT_OPTARG
, opt_parse_porcelain
),
1535 OPT_SET_INT(0, "long", &status_format
,
1536 N_("show status in long format (default)"),
1537 STATUS_FORMAT_LONG
),
1538 OPT_BOOL('z', "null", &s
.null_termination
,
1539 N_("terminate entries with NUL")),
1540 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1542 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1543 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1544 { OPTION_STRING
, 0, "ignored", &ignored_arg
,
1546 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1547 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"traditional" },
1548 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, N_("when"),
1549 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1550 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1551 OPT_COLUMN(0, "column", &s
.colopts
, N_("list untracked files in columns")),
1552 OPT_BOOL(0, "no-renames", &no_renames
, N_("do not detect renames")),
1553 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg
,
1554 N_("n"), N_("detect renames, optionally set similarity index"),
1555 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, opt_parse_rename_score
),
1559 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1560 usage_with_options(builtin_status_usage
, builtin_status_options
);
1562 prepare_repo_settings(the_repository
);
1563 the_repository
->settings
.command_requires_full_index
= 0;
1565 status_init_config(&s
, git_status_config
);
1566 argc
= parse_options(argc
, argv
, prefix
,
1567 builtin_status_options
,
1568 builtin_status_usage
, 0);
1569 finalize_colopts(&s
.colopts
, -1);
1570 finalize_deferred_config(&s
);
1572 handle_untracked_files_arg(&s
);
1573 handle_ignored_arg(&s
);
1575 if (s
.show_ignored_mode
== SHOW_MATCHING_IGNORED
&&
1576 s
.show_untracked_files
== SHOW_NO_UNTRACKED_FILES
)
1577 die(_("Unsupported combination of ignored and untracked-files arguments"));
1579 parse_pathspec(&s
.pathspec
, 0,
1580 PATHSPEC_PREFER_FULL
,
1583 if (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1584 status_format
!= STATUS_FORMAT_PORCELAIN_V2
)
1585 progress_flag
= REFRESH_PROGRESS
;
1586 repo_read_index(the_repository
);
1587 refresh_index(the_repository
->index
,
1588 REFRESH_QUIET
|REFRESH_UNMERGED
|progress_flag
,
1589 &s
.pathspec
, NULL
, NULL
);
1591 if (use_optional_locks())
1592 fd
= repo_hold_locked_index(the_repository
, &index_lock
, 0);
1596 s
.is_initial
= repo_get_oid(the_repository
, s
.reference
, &oid
) ? 1 : 0;
1598 oidcpy(&s
.oid_commit
, &oid
);
1600 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1601 s
.status_format
= status_format
;
1602 s
.verbose
= verbose
;
1603 if (no_renames
!= -1)
1604 s
.detect_rename
= !no_renames
;
1605 if ((intptr_t)rename_score_arg
!= -1) {
1606 if (s
.detect_rename
< DIFF_DETECT_RENAME
)
1607 s
.detect_rename
= DIFF_DETECT_RENAME
;
1608 if (rename_score_arg
)
1609 s
.rename_score
= parse_rename_score(&rename_score_arg
);
1612 wt_status_collect(&s
);
1615 repo_update_index_if_able(the_repository
, &index_lock
);
1617 if (s
.relative_paths
)
1620 wt_status_print(&s
);
1621 wt_status_collect_free_buffers(&s
);
1626 static int git_commit_config(const char *k
, const char *v
,
1627 const struct config_context
*ctx
, void *cb
)
1629 struct wt_status
*s
= cb
;
1631 if (!strcmp(k
, "commit.template"))
1632 return git_config_pathname(&template_file
, k
, v
);
1633 if (!strcmp(k
, "commit.status")) {
1634 include_status
= git_config_bool(k
, v
);
1637 if (!strcmp(k
, "commit.cleanup")) {
1638 FREE_AND_NULL(cleanup_config
);
1639 return git_config_string(&cleanup_config
, k
, v
);
1641 if (!strcmp(k
, "commit.gpgsign")) {
1642 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
1645 if (!strcmp(k
, "commit.verbose")) {
1647 config_commit_verbose
= git_config_bool_or_int(k
, v
, ctx
->kvi
,
1652 return git_status_config(k
, v
, ctx
, s
);
1655 int cmd_commit(int argc
,
1658 struct repository
*repo UNUSED
)
1660 static struct wt_status s
;
1661 static const char *cleanup_arg
= NULL
;
1662 static struct option builtin_commit_options
[] = {
1663 OPT__QUIET(&quiet
, N_("suppress summary after successful commit")),
1664 OPT__VERBOSE(&verbose
, N_("show diff in commit message template")),
1666 OPT_GROUP(N_("Commit message options")),
1667 OPT_FILENAME('F', "file", &logfile
, N_("read message from file")),
1668 OPT_STRING(0, "author", &force_author
, N_("author"), N_("override author for commit")),
1669 OPT_STRING(0, "date", &force_date
, N_("date"), N_("override date for commit")),
1670 OPT_CALLBACK('m', "message", &message
, N_("message"), N_("commit message"), opt_parse_m
),
1671 OPT_STRING('c', "reedit-message", &edit_message
, N_("commit"), N_("reuse and edit message from specified commit")),
1672 OPT_STRING('C', "reuse-message", &use_message
, N_("commit"), N_("reuse message from specified commit")),
1674 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1675 * and only translate <commit>.
1677 OPT_STRING(0, "fixup", &fixup_message
, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1678 OPT_STRING(0, "squash", &squash_message
, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1679 OPT_BOOL(0, "reset-author", &renew_authorship
, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1680 OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args
, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG
),
1681 OPT_BOOL('s', "signoff", &signoff
, N_("add a Signed-off-by trailer")),
1682 OPT_FILENAME('t', "template", &template_file
, N_("use specified template file")),
1683 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of commit")),
1684 OPT_CLEANUP(&cleanup_arg
),
1685 OPT_BOOL(0, "status", &include_status
, N_("include status in commit message template")),
1686 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
1687 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1688 /* end commit message options */
1690 OPT_GROUP(N_("Commit contents options")),
1691 OPT_BOOL('a', "all", &all
, N_("commit all changed files")),
1692 OPT_BOOL('i', "include", &also
, N_("add specified files to index for commit")),
1693 OPT_BOOL(0, "interactive", &interactive
, N_("interactively add files")),
1694 OPT_BOOL('p', "patch", &patch_interactive
, N_("interactively add changes")),
1695 OPT_BOOL('o', "only", &only
, N_("commit only specified files")),
1696 OPT_BOOL('n', "no-verify", &no_verify
, N_("bypass pre-commit and commit-msg hooks")),
1697 OPT_BOOL(0, "dry-run", &dry_run
, N_("show what would be committed")),
1698 OPT_SET_INT(0, "short", &status_format
, N_("show status concisely"),
1699 STATUS_FORMAT_SHORT
),
1700 OPT_BOOL(0, "branch", &s
.show_branch
, N_("show branch information")),
1701 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1702 N_("compute full ahead/behind values")),
1703 OPT_SET_INT(0, "porcelain", &status_format
,
1704 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN
),
1705 OPT_SET_INT(0, "long", &status_format
,
1706 N_("show status in long format (default)"),
1707 STATUS_FORMAT_LONG
),
1708 OPT_BOOL('z', "null", &s
.null_termination
,
1709 N_("terminate entries with NUL")),
1710 OPT_BOOL(0, "amend", &amend
, N_("amend previous commit")),
1711 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite
, N_("bypass post-rewrite hook")),
1712 { 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" },
1713 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1714 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1715 /* end commit contents options */
1717 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty
,
1718 N_("ok to record an empty change")),
1719 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message
,
1720 N_("ok to record a change with an empty message")),
1725 struct strbuf sb
= STRBUF_INIT
;
1726 struct strbuf author_ident
= STRBUF_INIT
;
1727 const char *index_file
, *reflog_msg
;
1728 struct object_id oid
;
1729 struct commit_list
*parents
= NULL
;
1730 struct stat statbuf
;
1731 struct commit
*current_head
= NULL
;
1732 struct commit_extra_header
*extra
= NULL
;
1733 struct strbuf err
= STRBUF_INIT
;
1736 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1737 usage_with_options(builtin_commit_usage
, builtin_commit_options
);
1739 prepare_repo_settings(the_repository
);
1740 the_repository
->settings
.command_requires_full_index
= 0;
1742 status_init_config(&s
, git_commit_config
);
1743 s
.commit_template
= 1;
1744 status_format
= STATUS_FORMAT_NONE
; /* Ignore status.short */
1747 if (repo_get_oid(the_repository
, "HEAD", &oid
))
1748 current_head
= NULL
;
1750 current_head
= lookup_commit_or_die(&oid
, "HEAD");
1751 if (repo_parse_commit(the_repository
, current_head
))
1752 die(_("could not parse HEAD commit"));
1754 verbose
= -1; /* unspecified */
1755 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_options
,
1756 builtin_commit_usage
,
1757 prefix
, current_head
, &s
);
1759 verbose
= (config_commit_verbose
< 0) ? 0 : config_commit_verbose
;
1762 free(cleanup_config
);
1763 cleanup_config
= xstrdup(cleanup_arg
);
1765 cleanup_mode
= get_cleanup_mode(cleanup_config
, use_editor
);
1768 return dry_run_commit(argv
, prefix
, current_head
, &s
);
1769 index_file
= prepare_index(argv
, prefix
, current_head
, 0);
1771 /* Set up everything for writing the commit object. This includes
1772 running hooks, writing the trees, and interacting with the user. */
1773 if (!prepare_to_commit(index_file
, prefix
,
1774 current_head
, &s
, &author_ident
)) {
1776 rollback_index_files();
1780 /* Determine parents */
1781 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1782 if (!current_head
) {
1784 reflog_msg
= "commit (initial)";
1787 reflog_msg
= "commit (amend)";
1788 parents
= copy_commit_list(current_head
->parents
);
1789 } else if (whence
== FROM_MERGE
) {
1790 struct strbuf m
= STRBUF_INIT
;
1792 int allow_fast_forward
= 1;
1793 struct commit_list
**pptr
= &parents
;
1796 reflog_msg
= "commit (merge)";
1797 pptr
= commit_list_append(current_head
, pptr
);
1798 fp
= xfopen(git_path_merge_head(the_repository
), "r");
1799 while (strbuf_getline_lf(&m
, fp
) != EOF
) {
1800 struct commit
*parent
;
1802 parent
= get_merge_parent(m
.buf
);
1804 die(_("Corrupt MERGE_HEAD file (%s)"), m
.buf
);
1805 pptr
= commit_list_append(parent
, pptr
);
1809 if (!stat(git_path_merge_mode(the_repository
), &statbuf
)) {
1810 if (strbuf_read_file(&sb
, git_path_merge_mode(the_repository
), 0) < 0)
1811 die_errno(_("could not read MERGE_MODE"));
1812 if (!strcmp(sb
.buf
, "no-ff"))
1813 allow_fast_forward
= 0;
1815 if (allow_fast_forward
)
1816 reduce_heads_replace(&parents
);
1819 reflog_msg
= is_from_cherry_pick(whence
)
1820 ? "commit (cherry-pick)"
1821 : is_from_rebase(whence
)
1824 commit_list_insert(current_head
, &parents
);
1827 /* Finally, get the commit message */
1829 if (strbuf_read_file(&sb
, git_path_commit_editmsg(), 0) < 0) {
1830 int saved_errno
= errno
;
1831 rollback_index_files();
1832 die(_("could not read commit message: %s"), strerror(saved_errno
));
1835 cleanup_message(&sb
, cleanup_mode
, verbose
);
1837 if (message_is_empty(&sb
, cleanup_mode
) && !allow_empty_message
) {
1838 rollback_index_files();
1839 fprintf(stderr
, _("Aborting commit due to empty commit message.\n"));
1842 if (template_untouched(&sb
, template_file
, cleanup_mode
) && !allow_empty_message
) {
1843 rollback_index_files();
1844 fprintf(stderr
, _("Aborting commit; you did not edit the message.\n"));
1848 if (fixup_message
&& starts_with(sb
.buf
, "amend! ") &&
1849 !allow_empty_message
) {
1850 struct strbuf body
= STRBUF_INIT
;
1851 size_t len
= commit_subject_length(sb
.buf
);
1852 strbuf_addstr(&body
, sb
.buf
+ len
);
1853 if (message_is_empty(&body
, cleanup_mode
)) {
1854 rollback_index_files();
1855 fprintf(stderr
, _("Aborting commit due to empty commit message body.\n"));
1858 strbuf_release(&body
);
1862 const char *exclude_gpgsig
[3] = { "gpgsig", "gpgsig-sha256", NULL
};
1863 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1865 struct commit_extra_header
**tail
= &extra
;
1866 append_merge_tag_headers(parents
, &tail
);
1869 if (commit_tree_extended(sb
.buf
, sb
.len
, &the_repository
->index
->cache_tree
->oid
,
1870 parents
, &oid
, author_ident
.buf
, NULL
,
1871 sign_commit
, extra
)) {
1872 rollback_index_files();
1873 die(_("failed to write commit object"));
1876 if (update_head_with_reflog(current_head
, &oid
, reflog_msg
, &sb
,
1878 rollback_index_files();
1882 sequencer_post_commit_cleanup(the_repository
, 0);
1883 unlink(git_path_merge_head(the_repository
));
1884 unlink(git_path_merge_msg(the_repository
));
1885 unlink(git_path_merge_mode(the_repository
));
1886 unlink(git_path_squash_msg(the_repository
));
1888 if (commit_index_files())
1889 die(_("repository has been updated, but unable to write\n"
1890 "new index file. Check that disk is not full and quota is\n"
1891 "not exceeded, and then \"git restore --staged :/\" to recover."));
1893 git_test_write_commit_graph_or_die();
1895 repo_rerere(the_repository
, 0);
1896 run_auto_maintenance(quiet
);
1897 run_commit_hook(use_editor
, repo_get_index_file(the_repository
),
1898 NULL
, "post-commit", NULL
);
1899 if (amend
&& !no_post_rewrite
) {
1900 commit_post_rewrite(the_repository
, current_head
, &oid
);
1903 unsigned int flags
= 0;
1906 flags
|= SUMMARY_INITIAL_COMMIT
;
1907 if (author_date_is_interesting())
1908 flags
|= SUMMARY_SHOW_AUTHOR_DATE
;
1909 print_commit_summary(the_repository
, prefix
,
1913 apply_autostash_ref(the_repository
, "MERGE_AUTOSTASH");
1916 free_commit_extra_headers(extra
);
1917 free_commit_list(parents
);
1918 strbuf_release(&author_ident
);
1919 strbuf_release(&err
);
1920 strbuf_release(&sb
);
1922 free(template_file
);