4 * Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-commit.sh by Junio C Hamano and Linus Torvalds
8 #define USE_THE_INDEX_VARIABLE
12 #include "cache-tree.h"
21 #include "wt-status.h"
22 #include "run-command.h"
28 #include "parse-options.h"
29 #include "string-list.h"
31 #include "unpack-trees.h"
33 #include "submodule.h"
34 #include "gpg-interface.h"
36 #include "sequencer.h"
39 #include "commit-reach.h"
40 #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 *logfile
, *force_author
;
111 static const char *template_file
;
113 * The _message variables are commit names from which to take
114 * the commit message and/or authorship.
116 static const char *author_message
, *author_message_buffer
;
117 static char *edit_message
, *use_message
;
118 static char *fixup_message
, *fixup_commit
, *squash_message
;
119 static const char *fixup_prefix
;
120 static int all
, also
, interactive
, patch_interactive
, only
, amend
, signoff
;
121 static int edit_flag
= -1; /* unspecified */
122 static int quiet
, verbose
, no_verify
, allow_empty
, dry_run
, renew_authorship
;
123 static int config_commit_verbose
= -1; /* unspecified */
124 static int no_post_rewrite
, allow_empty_message
, pathspec_file_nul
;
125 static char *untracked_files_arg
, *force_date
, *ignore_submodule_arg
, *ignored_arg
;
126 static char *sign_commit
, *pathspec_from_file
;
127 static struct strvec trailer_args
= STRVEC_INIT
;
130 * The default commit message cleanup mode will remove the lines
131 * beginning with # (shell comments) and leading and trailing
132 * whitespaces (empty lines or containing only whitespaces)
133 * if editor is used, and only the whitespaces if the message
134 * is specified explicitly.
136 static enum commit_msg_cleanup_mode cleanup_mode
;
137 static const char *cleanup_arg
;
139 static enum commit_whence whence
;
140 static int use_editor
= 1, include_status
= 1;
141 static int have_option_m
;
142 static struct strbuf message
= STRBUF_INIT
;
144 static enum wt_status_format status_format
= STATUS_FORMAT_UNSPECIFIED
;
146 static int opt_pass_trailer(const struct option
*opt
, const char *arg
, int unset
)
148 BUG_ON_OPT_NEG(unset
);
150 strvec_pushl(opt
->value
, "--trailer", arg
, NULL
);
154 static int opt_parse_porcelain(const struct option
*opt
, const char *arg
, int unset
)
156 enum wt_status_format
*value
= (enum wt_status_format
*)opt
->value
;
158 *value
= STATUS_FORMAT_NONE
;
160 *value
= STATUS_FORMAT_PORCELAIN
;
161 else if (!strcmp(arg
, "v1") || !strcmp(arg
, "1"))
162 *value
= STATUS_FORMAT_PORCELAIN
;
163 else if (!strcmp(arg
, "v2") || !strcmp(arg
, "2"))
164 *value
= STATUS_FORMAT_PORCELAIN_V2
;
166 die("unsupported porcelain version '%s'", arg
);
171 static int opt_parse_m(const struct option
*opt
, const char *arg
, int unset
)
173 struct strbuf
*buf
= opt
->value
;
176 strbuf_setlen(buf
, 0);
180 strbuf_addch(buf
, '\n');
181 strbuf_addstr(buf
, arg
);
182 strbuf_complete_line(buf
);
187 static int opt_parse_rename_score(const struct option
*opt
, const char *arg
, int unset
)
189 const char **value
= opt
->value
;
191 BUG_ON_OPT_NEG(unset
);
193 if (arg
!= NULL
&& *arg
== '=')
200 static void determine_whence(struct wt_status
*s
)
202 if (file_exists(git_path_merge_head(the_repository
)))
204 else if (!sequencer_determine_whence(the_repository
, &whence
))
205 whence
= FROM_COMMIT
;
210 static void status_init_config(struct wt_status
*s
, config_fn_t fn
)
212 wt_status_prepare(the_repository
, s
);
213 init_diff_ui_defaults();
216 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
); /* must come after git_config() */
219 static void rollback_index_files(void)
221 switch (commit_style
) {
223 break; /* nothing to do */
225 rollback_lock_file(&index_lock
);
228 rollback_lock_file(&index_lock
);
229 rollback_lock_file(&false_lock
);
234 static int commit_index_files(void)
238 switch (commit_style
) {
240 break; /* nothing to do */
242 err
= commit_lock_file(&index_lock
);
245 err
= commit_lock_file(&index_lock
);
246 rollback_lock_file(&false_lock
);
254 * Take a union of paths in the index and the named tree (typically, "HEAD"),
255 * and return the paths that match the given pattern in list.
257 static int list_paths(struct string_list
*list
, const char *with_tree
,
258 const struct pathspec
*pattern
)
266 m
= xcalloc(1, pattern
->nr
);
269 char *max_prefix
= common_prefix(pattern
);
270 overlay_tree_on_index(&the_index
, with_tree
, max_prefix
);
274 /* TODO: audit for interaction with sparse-index. */
275 ensure_full_index(&the_index
);
276 for (i
= 0; i
< the_index
.cache_nr
; i
++) {
277 const struct cache_entry
*ce
= the_index
.cache
[i
];
278 struct string_list_item
*item
;
280 if (ce
->ce_flags
& CE_UPDATE
)
282 if (!ce_path_match(&the_index
, ce
, pattern
, m
))
284 item
= string_list_insert(list
, ce
->name
);
285 if (ce_skip_worktree(ce
))
286 item
->util
= item
; /* better a valid pointer than a fake one */
289 ret
= report_path_error(m
, pattern
);
294 static void add_remove_files(struct string_list
*list
)
297 for (i
= 0; i
< list
->nr
; i
++) {
299 struct string_list_item
*p
= &(list
->items
[i
]);
301 /* p->util is skip-worktree */
305 if (!lstat(p
->string
, &st
)) {
306 if (add_to_index(&the_index
, p
->string
, &st
, 0))
307 die(_("updating files failed"));
309 remove_file_from_index(&the_index
, p
->string
);
313 static void create_base_index(const struct commit
*current_head
)
316 struct unpack_trees_options opts
;
320 discard_index(&the_index
);
324 memset(&opts
, 0, sizeof(opts
));
328 opts
.src_index
= &the_index
;
329 opts
.dst_index
= &the_index
;
331 opts
.fn
= oneway_merge
;
332 tree
= parse_tree_indirect(¤t_head
->object
.oid
);
334 die(_("failed to unpack HEAD tree object"));
336 init_tree_desc(&t
, tree
->buffer
, tree
->size
);
337 if (unpack_trees(1, &t
, &opts
))
338 exit(128); /* We've already reported the error, finish dying */
341 static void refresh_cache_or_die(int refresh_flags
)
344 * refresh_flags contains REFRESH_QUIET, so the only errors
345 * are for unmerged entries.
347 if (refresh_index(&the_index
, refresh_flags
| REFRESH_IN_PORCELAIN
, NULL
, NULL
, NULL
))
348 die_resolve_conflict("commit");
351 static const char *prepare_index(const char **argv
, const char *prefix
,
352 const struct commit
*current_head
, int is_status
)
354 struct string_list partial
= STRING_LIST_INIT_DUP
;
355 struct pathspec pathspec
;
356 int refresh_flags
= REFRESH_QUIET
;
360 refresh_flags
|= REFRESH_UNMERGED
;
361 parse_pathspec(&pathspec
, 0,
362 PATHSPEC_PREFER_FULL
,
365 if (pathspec_from_file
) {
367 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
370 die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "-a");
373 die(_("'%s' and pathspec arguments cannot be used together"), "--pathspec-from-file");
375 parse_pathspec_file(&pathspec
, 0,
376 PATHSPEC_PREFER_FULL
,
377 prefix
, pathspec_from_file
, pathspec_file_nul
);
378 } else if (pathspec_file_nul
) {
379 die(_("the option '%s' requires '%s'"), "--pathspec-file-nul", "--pathspec-from-file");
382 if (!pathspec
.nr
&& (also
|| (only
&& !allow_empty
&&
383 (!amend
|| (fixup_message
&& strcmp(fixup_prefix
, "amend"))))))
384 die(_("No paths with --include/--only does not make sense."));
386 if (repo_read_index_preload(the_repository
, &pathspec
, 0) < 0)
387 die(_("index file corrupt"));
390 char *old_index_env
= NULL
, *old_repo_index_file
;
391 repo_hold_locked_index(the_repository
, &index_lock
,
394 refresh_cache_or_die(refresh_flags
);
396 if (write_locked_index(&the_index
, &index_lock
, 0))
397 die(_("unable to create temporary index"));
399 old_repo_index_file
= the_repository
->index_file
;
400 the_repository
->index_file
=
401 (char *)get_lock_file_path(&index_lock
);
402 old_index_env
= xstrdup_or_null(getenv(INDEX_ENVIRONMENT
));
403 setenv(INDEX_ENVIRONMENT
, the_repository
->index_file
, 1);
405 if (interactive_add(argv
, prefix
, patch_interactive
) != 0)
406 die(_("interactive add failed"));
408 the_repository
->index_file
= old_repo_index_file
;
409 if (old_index_env
&& *old_index_env
)
410 setenv(INDEX_ENVIRONMENT
, old_index_env
, 1);
412 unsetenv(INDEX_ENVIRONMENT
);
413 FREE_AND_NULL(old_index_env
);
415 discard_index(&the_index
);
416 read_index_from(&the_index
, get_lock_file_path(&index_lock
),
418 if (cache_tree_update(&the_index
, WRITE_TREE_SILENT
) == 0) {
419 if (reopen_lock_file(&index_lock
) < 0)
420 die(_("unable to write index file"));
421 if (write_locked_index(&the_index
, &index_lock
, 0))
422 die(_("unable to update temporary index"));
424 warning(_("Failed to update main cache tree"));
426 commit_style
= COMMIT_NORMAL
;
427 ret
= get_lock_file_path(&index_lock
);
432 * Non partial, non as-is commit.
434 * (1) get the real index;
435 * (2) update the_index as necessary;
436 * (3) write the_index out to the real index (still locked);
437 * (4) return the name of the locked index file.
439 * The caller should run hooks on the locked real index, and
440 * (A) if all goes well, commit the real index;
441 * (B) on failure, rollback the real index.
443 if (all
|| (also
&& pathspec
.nr
)) {
444 repo_hold_locked_index(the_repository
, &index_lock
,
446 add_files_to_cache(also
? prefix
: NULL
, &pathspec
, 0);
447 refresh_cache_or_die(refresh_flags
);
448 cache_tree_update(&the_index
, WRITE_TREE_SILENT
);
449 if (write_locked_index(&the_index
, &index_lock
, 0))
450 die(_("unable to write new_index file"));
451 commit_style
= COMMIT_NORMAL
;
452 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_index
.cache_changed
470 || !cache_tree_fully_valid(the_index
.cache_tree
))
471 cache_tree_update(&the_index
, WRITE_TREE_SILENT
);
472 if (write_locked_index(&the_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
= get_index_file();
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_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_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
520 cache_tree_update(&the_index
, WRITE_TREE_SILENT
);
521 if (write_locked_index(&the_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_index
, REFRESH_QUIET
, NULL
, NULL
, NULL
);
533 if (write_locked_index(&the_index
, &false_lock
, 0))
534 die(_("unable to write temporary index file"));
536 discard_index(&the_index
);
537 ret
= get_lock_file_path(&false_lock
);
538 read_index_from(&the_index
, ret
, get_git_dir());
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
= get_oid(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 comment_line_char
= candidates
[0];
688 if (!memchr(sb
->buf
, comment_line_char
, sb
->len
))
692 candidate
= strchr(candidates
, *p
);
695 for (p
= sb
->buf
; *p
; p
++) {
696 if ((p
[0] == '\n' || p
[0] == '\r') && p
[1]) {
697 candidate
= strchr(candidates
, p
[1]);
703 for (p
= candidates
; *p
== ' '; p
++)
706 die(_("unable to select a comment character that is not used\n"
707 "in the current commit message"));
708 comment_line_char
= *p
;
711 static void prepare_amend_commit(struct commit
*commit
, struct strbuf
*sb
,
712 struct pretty_print_context
*ctx
)
714 const char *buffer
, *subject
, *fmt
;
716 buffer
= get_commit_buffer(commit
, NULL
);
717 find_commit_subject(buffer
, &subject
);
719 * If we amend the 'amend!' commit then we don't want to
720 * duplicate the subject line.
722 fmt
= starts_with(subject
, "amend!") ? "%b" : "%B";
723 format_commit_message(commit
, fmt
, sb
, ctx
);
724 unuse_commit_buffer(commit
, buffer
);
727 static int prepare_to_commit(const char *index_file
, const char *prefix
,
728 struct commit
*current_head
,
730 struct strbuf
*author_ident
)
733 struct strbuf committer_ident
= STRBUF_INIT
;
735 struct strbuf sb
= STRBUF_INIT
;
736 const char *hook_arg1
= NULL
;
737 const char *hook_arg2
= NULL
;
738 int clean_message_contents
= (cleanup_mode
!= COMMIT_MSG_CLEANUP_NONE
);
739 int old_display_comment_prefix
;
740 int merge_contains_scissors
= 0;
743 /* This checks and barfs if author is badly specified */
744 determine_author_info(author_ident
);
746 if (!no_verify
&& run_commit_hook(use_editor
, index_file
, &invoked_hook
,
750 if (squash_message
) {
752 * Insert the proper subject line before other commit
753 * message options add their content.
755 if (use_message
&& !strcmp(use_message
, squash_message
))
756 strbuf_addstr(&sb
, "squash! ");
758 struct pretty_print_context ctx
= {0};
760 c
= lookup_commit_reference_by_name(squash_message
);
762 die(_("could not lookup commit %s"), squash_message
);
763 ctx
.output_encoding
= get_commit_output_encoding();
764 format_commit_message(c
, "squash! %s\n\n", &sb
,
769 if (have_option_m
&& !fixup_message
) {
770 strbuf_addbuf(&sb
, &message
);
771 hook_arg1
= "message";
772 } else if (logfile
&& !strcmp(logfile
, "-")) {
774 fprintf(stderr
, _("(reading log message from standard input)\n"));
775 if (strbuf_read(&sb
, 0, 0) < 0)
776 die_errno(_("could not read log from standard input"));
777 hook_arg1
= "message";
778 } else if (logfile
) {
779 if (strbuf_read_file(&sb
, logfile
, 0) < 0)
780 die_errno(_("could not read log file '%s'"),
782 hook_arg1
= "message";
783 } else if (use_message
) {
785 buffer
= strstr(use_message_buffer
, "\n\n");
787 strbuf_addstr(&sb
, skip_blank_lines(buffer
+ 2));
788 hook_arg1
= "commit";
789 hook_arg2
= use_message
;
790 } else if (fixup_message
) {
791 struct pretty_print_context ctx
= {0};
792 struct commit
*commit
;
794 commit
= lookup_commit_reference_by_name(fixup_commit
);
796 die(_("could not lookup commit %s"), fixup_commit
);
797 ctx
.output_encoding
= get_commit_output_encoding();
798 fmt
= xstrfmt("%s! %%s\n\n", fixup_prefix
);
799 format_commit_message(commit
, fmt
, &sb
, &ctx
);
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 merge_contains_scissors
= 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
, 0);
893 append_signoff(&sb
, ignore_non_trailer(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 '%c' will be ignored.\n") :
911 _("Please enter the commit message for your changes."
912 " Lines starting\nwith '%c' 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 '%c' 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 '%c' 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 !merge_contains_scissors
)
927 wt_status_add_cut_line(s
->fp
);
930 whence
== FROM_MERGE
?
932 "It looks like you may be committing a merge.\n"
933 "If this is not correct, please run\n"
934 " git update-ref -d MERGE_HEAD\n"
935 "and try again.\n") :
937 "It looks like you may be committing a cherry-pick.\n"
938 "If this is not correct, please run\n"
939 " git update-ref -d CHERRY_PICK_HEAD\n"
940 "and try again.\n"));
943 fprintf(s
->fp
, "\n");
944 if (cleanup_mode
== COMMIT_MSG_CLEANUP_ALL
)
945 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_all
, comment_line_char
);
946 else if (cleanup_mode
== COMMIT_MSG_CLEANUP_SCISSORS
) {
947 if (whence
== FROM_COMMIT
&& !merge_contains_scissors
)
948 wt_status_add_cut_line(s
->fp
);
949 } else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
950 status_printf(s
, GIT_COLOR_NORMAL
, hint_cleanup_space
, comment_line_char
);
953 * These should never fail because they come from our own
954 * fmt_ident. They may fail the sane_ident test, but we know
955 * that the name and mail pointers will at least be valid,
956 * which is enough for our tests and printing here.
958 assert_split_ident(&ai
, author_ident
);
959 assert_split_ident(&ci
, &committer_ident
);
961 if (ident_cmp(&ai
, &ci
))
962 status_printf_ln(s
, GIT_COLOR_NORMAL
,
964 "Author: %.*s <%.*s>"),
965 ident_shown
++ ? "" : "\n",
966 (int)(ai
.name_end
- ai
.name_begin
), ai
.name_begin
,
967 (int)(ai
.mail_end
- ai
.mail_begin
), ai
.mail_begin
);
969 if (author_date_is_interesting())
970 status_printf_ln(s
, GIT_COLOR_NORMAL
,
973 ident_shown
++ ? "" : "\n",
974 show_ident_date(&ai
, DATE_MODE(NORMAL
)));
976 if (!committer_ident_sufficiently_given())
977 status_printf_ln(s
, GIT_COLOR_NORMAL
,
979 "Committer: %.*s <%.*s>"),
980 ident_shown
++ ? "" : "\n",
981 (int)(ci
.name_end
- ci
.name_begin
), ci
.name_begin
,
982 (int)(ci
.mail_end
- ci
.mail_begin
), ci
.mail_begin
);
984 status_printf_ln(s
, GIT_COLOR_NORMAL
, "%s", ""); /* Add new line for clarity */
986 saved_color_setting
= s
->use_color
;
988 committable
= run_status(s
->fp
, index_file
, prefix
, 1, s
);
989 s
->use_color
= saved_color_setting
;
990 string_list_clear(&s
->change
, 1);
992 struct object_id oid
;
993 const char *parent
= "HEAD";
995 if (!the_index
.cache_nr
) {
996 discard_index(&the_index
);
997 if (repo_read_index(the_repository
) < 0)
998 die(_("Cannot read index"));
1004 if (get_oid(parent
, &oid
)) {
1007 /* TODO: audit for interaction with sparse-index. */
1008 ensure_full_index(&the_index
);
1009 for (i
= 0; i
< the_index
.cache_nr
; i
++)
1010 if (ce_intent_to_add(the_index
.cache
[i
]))
1012 committable
= the_index
.cache_nr
- ita_nr
> 0;
1015 * Unless the user did explicitly request a submodule
1016 * ignore mode by passing a command line option we do
1017 * not ignore any changed submodule SHA-1s when
1018 * comparing index and parent, no matter what is
1019 * configured. Otherwise we won't commit any
1020 * submodules which were manually staged, which would
1021 * be really confusing.
1023 struct diff_flags flags
= DIFF_FLAGS_INIT
;
1024 flags
.override_submodule_config
= 1;
1025 if (ignore_submodule_arg
&&
1026 !strcmp(ignore_submodule_arg
, "all"))
1027 flags
.ignore_submodules
= 1;
1028 committable
= index_differs_from(the_repository
,
1032 strbuf_release(&committer_ident
);
1036 if (trailer_args
.nr
) {
1037 struct child_process run_trailer
= CHILD_PROCESS_INIT
;
1039 strvec_pushl(&run_trailer
.args
, "interpret-trailers",
1040 "--in-place", git_path_commit_editmsg(), NULL
);
1041 strvec_pushv(&run_trailer
.args
, trailer_args
.v
);
1042 run_trailer
.git_cmd
= 1;
1043 if (run_command(&run_trailer
))
1044 die(_("unable to pass trailers to --trailers"));
1045 strvec_clear(&trailer_args
);
1049 * Reject an attempt to record a non-merge empty commit without
1050 * explicit --allow-empty. In the cherry-pick case, it may be
1051 * empty due to conflict resolution, which the user should okay.
1053 if (!committable
&& whence
!= FROM_MERGE
&& !allow_empty
&&
1054 !(amend
&& is_a_merge(current_head
))) {
1055 s
->hints
= advice_enabled(ADVICE_STATUS_HINTS
);
1056 s
->display_comment_prefix
= old_display_comment_prefix
;
1057 run_status(stdout
, index_file
, prefix
, 0, s
);
1059 fputs(_(empty_amend_advice
), stderr
);
1060 else if (is_from_cherry_pick(whence
) ||
1061 whence
== FROM_REBASE_PICK
) {
1062 fputs(_(empty_cherry_pick_advice
), stderr
);
1063 if (whence
== FROM_CHERRY_PICK_SINGLE
)
1064 fputs(_(empty_cherry_pick_advice_single
), stderr
);
1065 else if (whence
== FROM_CHERRY_PICK_MULTI
)
1066 fputs(_(empty_cherry_pick_advice_multi
), stderr
);
1068 fputs(_(empty_rebase_pick_advice
), stderr
);
1073 if (!no_verify
&& invoked_hook
) {
1075 * Re-read the index as the pre-commit-commit hook was invoked
1076 * and could have updated it. We must do this before we invoke
1077 * the editor and after we invoke run_status above.
1079 discard_index(&the_index
);
1081 read_index_from(&the_index
, index_file
, get_git_dir());
1083 if (cache_tree_update(&the_index
, 0)) {
1084 error(_("Error building trees"));
1088 if (run_commit_hook(use_editor
, index_file
, NULL
, "prepare-commit-msg",
1089 git_path_commit_editmsg(), hook_arg1
, hook_arg2
, NULL
))
1093 struct strvec env
= STRVEC_INIT
;
1095 strvec_pushf(&env
, "GIT_INDEX_FILE=%s", index_file
);
1096 if (launch_editor(git_path_commit_editmsg(), NULL
, env
.v
)) {
1098 _("Please supply the message using either -m or -F option.\n"));
1105 run_commit_hook(use_editor
, index_file
, NULL
, "commit-msg",
1106 git_path_commit_editmsg(), NULL
)) {
1113 static const char *find_author_by_nickname(const char *name
)
1115 struct rev_info revs
;
1116 struct commit
*commit
;
1117 struct strbuf buf
= STRBUF_INIT
;
1121 repo_init_revisions(the_repository
, &revs
, NULL
);
1122 strbuf_addf(&buf
, "--author=%s", name
);
1127 setup_revisions(ac
, av
, &revs
, NULL
);
1128 revs
.mailmap
= xmalloc(sizeof(struct string_list
));
1129 string_list_init_nodup(revs
.mailmap
);
1130 read_mailmap(revs
.mailmap
);
1132 if (prepare_revision_walk(&revs
))
1133 die(_("revision walk setup failed"));
1134 commit
= get_revision(&revs
);
1136 struct pretty_print_context ctx
= {0};
1137 ctx
.date_mode
.type
= DATE_NORMAL
;
1138 strbuf_release(&buf
);
1139 format_commit_message(commit
, "%aN <%aE>", &buf
, &ctx
);
1140 release_revisions(&revs
);
1141 return strbuf_detach(&buf
, NULL
);
1143 die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name
);
1146 static void handle_ignored_arg(struct wt_status
*s
)
1149 ; /* default already initialized */
1150 else if (!strcmp(ignored_arg
, "traditional"))
1151 s
->show_ignored_mode
= SHOW_TRADITIONAL_IGNORED
;
1152 else if (!strcmp(ignored_arg
, "no"))
1153 s
->show_ignored_mode
= SHOW_NO_IGNORED
;
1154 else if (!strcmp(ignored_arg
, "matching"))
1155 s
->show_ignored_mode
= SHOW_MATCHING_IGNORED
;
1157 die(_("Invalid ignored mode '%s'"), ignored_arg
);
1160 static void handle_untracked_files_arg(struct wt_status
*s
)
1162 if (!untracked_files_arg
)
1163 ; /* default already initialized */
1164 else if (!strcmp(untracked_files_arg
, "no"))
1165 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1166 else if (!strcmp(untracked_files_arg
, "normal"))
1167 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1168 else if (!strcmp(untracked_files_arg
, "all"))
1169 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1171 * Please update $__git_untracked_file_modes in
1172 * git-completion.bash when you add new options
1175 die(_("Invalid untracked files mode '%s'"), untracked_files_arg
);
1178 static const char *read_commit_message(const char *name
)
1180 const char *out_enc
;
1181 struct commit
*commit
;
1183 commit
= lookup_commit_reference_by_name(name
);
1185 die(_("could not lookup commit %s"), name
);
1186 out_enc
= get_commit_output_encoding();
1187 return logmsg_reencode(commit
, NULL
, out_enc
);
1191 * Enumerate what needs to be propagated when --porcelain
1192 * is not in effect here.
1194 static struct status_deferred_config
{
1195 enum wt_status_format status_format
;
1197 enum ahead_behind_flags ahead_behind
;
1198 } status_deferred_config
= {
1199 STATUS_FORMAT_UNSPECIFIED
,
1200 -1, /* unspecified */
1201 AHEAD_BEHIND_UNSPECIFIED
,
1204 static void finalize_deferred_config(struct wt_status
*s
)
1206 int use_deferred_config
= (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1207 status_format
!= STATUS_FORMAT_PORCELAIN_V2
&&
1208 !s
->null_termination
);
1210 if (s
->null_termination
) {
1211 if (status_format
== STATUS_FORMAT_NONE
||
1212 status_format
== STATUS_FORMAT_UNSPECIFIED
)
1213 status_format
= STATUS_FORMAT_PORCELAIN
;
1214 else if (status_format
== STATUS_FORMAT_LONG
)
1215 die(_("options '%s' and '%s' cannot be used together"), "--long", "-z");
1218 if (use_deferred_config
&& status_format
== STATUS_FORMAT_UNSPECIFIED
)
1219 status_format
= status_deferred_config
.status_format
;
1220 if (status_format
== STATUS_FORMAT_UNSPECIFIED
)
1221 status_format
= STATUS_FORMAT_NONE
;
1223 if (use_deferred_config
&& s
->show_branch
< 0)
1224 s
->show_branch
= status_deferred_config
.show_branch
;
1225 if (s
->show_branch
< 0)
1229 * If the user did not give a "--[no]-ahead-behind" command
1230 * line argument *AND* we will print in a human-readable format
1231 * (short, long etc.) then we inherit from the status.aheadbehind
1232 * config setting. In all other cases (and porcelain V[12] formats
1233 * in particular), we inherit _FULL for backwards compatibility.
1235 if (use_deferred_config
&&
1236 s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1237 s
->ahead_behind_flags
= status_deferred_config
.ahead_behind
;
1239 if (s
->ahead_behind_flags
== AHEAD_BEHIND_UNSPECIFIED
)
1240 s
->ahead_behind_flags
= AHEAD_BEHIND_FULL
;
1243 static void check_fixup_reword_options(int argc
, const char *argv
[]) {
1244 if (whence
!= FROM_COMMIT
) {
1245 if (whence
== FROM_MERGE
)
1246 die(_("You are in the middle of a merge -- cannot reword."));
1247 else if (is_from_cherry_pick(whence
))
1248 die(_("You are in the middle of a cherry-pick -- cannot reword."));
1251 die(_("reword option of '%s' and path '%s' cannot be used together"), "--fixup", *argv
);
1252 if (patch_interactive
|| interactive
|| all
|| also
|| only
)
1253 die(_("reword option of '%s' and '%s' cannot be used together"),
1254 "--fixup", "--patch/--interactive/--all/--include/--only");
1257 static int parse_and_validate_options(int argc
, const char *argv
[],
1258 const struct option
*options
,
1259 const char * const usage
[],
1261 struct commit
*current_head
,
1262 struct wt_status
*s
)
1264 argc
= parse_options(argc
, argv
, prefix
, options
, usage
, 0);
1265 finalize_deferred_config(s
);
1267 if (force_author
&& !strchr(force_author
, '>'))
1268 force_author
= find_author_by_nickname(force_author
);
1270 if (force_author
&& renew_authorship
)
1271 die(_("options '%s' and '%s' cannot be used together"), "--reset-author", "--author");
1273 if (logfile
|| have_option_m
|| use_message
)
1276 /* Sanity check options */
1277 if (amend
&& !current_head
)
1278 die(_("You have nothing to amend."));
1279 if (amend
&& whence
!= FROM_COMMIT
) {
1280 if (whence
== FROM_MERGE
)
1281 die(_("You are in the middle of a merge -- cannot amend."));
1282 else if (is_from_cherry_pick(whence
))
1283 die(_("You are in the middle of a cherry-pick -- cannot amend."));
1284 else if (whence
== FROM_REBASE_PICK
)
1285 die(_("You are in the middle of a rebase -- cannot amend."));
1287 if (fixup_message
&& squash_message
)
1288 die(_("options '%s' and '%s' cannot be used together"), "--squash", "--fixup");
1289 die_for_incompatible_opt4(!!use_message
, "-C",
1290 !!edit_message
, "-c",
1292 !!fixup_message
, "--fixup");
1293 die_for_incompatible_opt4(have_option_m
, "-m",
1294 !!edit_message
, "-c",
1295 !!use_message
, "-C",
1297 if (use_message
|| edit_message
|| logfile
||fixup_message
|| have_option_m
)
1298 template_file
= NULL
;
1300 use_message
= edit_message
;
1301 if (amend
&& !use_message
&& !fixup_message
)
1302 use_message
= "HEAD";
1303 if (!use_message
&& !is_from_cherry_pick(whence
) &&
1304 !is_from_rebase(whence
) && renew_authorship
)
1305 die(_("--reset-author can be used only with -C, -c or --amend."));
1307 use_message_buffer
= read_commit_message(use_message
);
1308 if (!renew_authorship
) {
1309 author_message
= use_message
;
1310 author_message_buffer
= use_message_buffer
;
1313 if ((is_from_cherry_pick(whence
) || whence
== FROM_REBASE_PICK
) &&
1314 !renew_authorship
) {
1315 author_message
= "CHERRY_PICK_HEAD";
1316 author_message_buffer
= read_commit_message(author_message
);
1319 if (patch_interactive
)
1322 die_for_incompatible_opt4(also
, "-i/--include",
1325 interactive
, "--interactive/-p/--patch");
1326 if (fixup_message
) {
1328 * We limit --fixup's suboptions to only alpha characters.
1329 * If the first character after a run of alpha is colon,
1330 * then the part before the colon may be a known suboption
1331 * name like `amend` or `reword`, or a misspelt suboption
1332 * name. In either case, we treat it as
1333 * --fixup=<suboption>:<arg>.
1335 * Otherwise, we are dealing with --fixup=<commit>.
1337 char *p
= fixup_message
;
1340 if (p
> fixup_message
&& *p
== ':') {
1342 fixup_commit
= p
+ 1;
1343 if (!strcmp("amend", fixup_message
) ||
1344 !strcmp("reword", fixup_message
)) {
1345 fixup_prefix
= "amend";
1347 if (*fixup_message
== 'r') {
1348 check_fixup_reword_options(argc
, argv
);
1352 die(_("unknown option: --fixup=%s:%s"), fixup_message
, fixup_commit
);
1355 fixup_commit
= fixup_message
;
1356 fixup_prefix
= "fixup";
1362 use_editor
= edit_flag
;
1364 cleanup_mode
= get_cleanup_mode(cleanup_arg
, use_editor
);
1366 handle_untracked_files_arg(s
);
1368 if (all
&& argc
> 0)
1369 die(_("paths '%s ...' with -a does not make sense"),
1372 if (status_format
!= STATUS_FORMAT_NONE
)
1378 static int dry_run_commit(const char **argv
, const char *prefix
,
1379 const struct commit
*current_head
, struct wt_status
*s
)
1382 const char *index_file
;
1384 index_file
= prepare_index(argv
, prefix
, current_head
, 1);
1385 committable
= run_status(stdout
, index_file
, prefix
, 0, s
);
1386 rollback_index_files();
1388 return committable
? 0 : 1;
1391 define_list_config_array_extra(color_status_slots
, {"added"});
1393 static int parse_status_slot(const char *slot
)
1395 if (!strcasecmp(slot
, "added"))
1396 return WT_STATUS_UPDATED
;
1398 return LOOKUP_CONFIG(color_status_slots
, slot
);
1401 static int git_status_config(const char *k
, const char *v
, void *cb
)
1403 struct wt_status
*s
= cb
;
1404 const char *slot_name
;
1406 if (starts_with(k
, "column."))
1407 return git_column_config(k
, v
, "status", &s
->colopts
);
1408 if (!strcmp(k
, "status.submodulesummary")) {
1410 s
->submodule_summary
= git_config_bool_or_int(k
, v
, &is_bool
);
1411 if (is_bool
&& s
->submodule_summary
)
1412 s
->submodule_summary
= -1;
1415 if (!strcmp(k
, "status.short")) {
1416 if (git_config_bool(k
, v
))
1417 status_deferred_config
.status_format
= STATUS_FORMAT_SHORT
;
1419 status_deferred_config
.status_format
= STATUS_FORMAT_NONE
;
1422 if (!strcmp(k
, "status.branch")) {
1423 status_deferred_config
.show_branch
= git_config_bool(k
, v
);
1426 if (!strcmp(k
, "status.aheadbehind")) {
1427 status_deferred_config
.ahead_behind
= git_config_bool(k
, v
);
1430 if (!strcmp(k
, "status.showstash")) {
1431 s
->show_stash
= git_config_bool(k
, v
);
1434 if (!strcmp(k
, "status.color") || !strcmp(k
, "color.status")) {
1435 s
->use_color
= git_config_colorbool(k
, v
);
1438 if (!strcmp(k
, "status.displaycommentprefix")) {
1439 s
->display_comment_prefix
= git_config_bool(k
, v
);
1442 if (skip_prefix(k
, "status.color.", &slot_name
) ||
1443 skip_prefix(k
, "color.status.", &slot_name
)) {
1444 int slot
= parse_status_slot(slot_name
);
1448 return config_error_nonbool(k
);
1449 return color_parse(v
, s
->color_palette
[slot
]);
1451 if (!strcmp(k
, "status.relativepaths")) {
1452 s
->relative_paths
= git_config_bool(k
, v
);
1455 if (!strcmp(k
, "status.showuntrackedfiles")) {
1457 return config_error_nonbool(k
);
1458 else if (!strcmp(v
, "no"))
1459 s
->show_untracked_files
= SHOW_NO_UNTRACKED_FILES
;
1460 else if (!strcmp(v
, "normal"))
1461 s
->show_untracked_files
= SHOW_NORMAL_UNTRACKED_FILES
;
1462 else if (!strcmp(v
, "all"))
1463 s
->show_untracked_files
= SHOW_ALL_UNTRACKED_FILES
;
1465 return error(_("Invalid untracked files mode '%s'"), v
);
1468 if (!strcmp(k
, "diff.renamelimit")) {
1469 if (s
->rename_limit
== -1)
1470 s
->rename_limit
= git_config_int(k
, v
);
1473 if (!strcmp(k
, "status.renamelimit")) {
1474 s
->rename_limit
= git_config_int(k
, v
);
1477 if (!strcmp(k
, "diff.renames")) {
1478 if (s
->detect_rename
== -1)
1479 s
->detect_rename
= git_config_rename(k
, v
);
1482 if (!strcmp(k
, "status.renames")) {
1483 s
->detect_rename
= git_config_rename(k
, v
);
1486 return git_diff_ui_config(k
, v
, NULL
);
1489 int cmd_status(int argc
, const char **argv
, const char *prefix
)
1491 static int no_renames
= -1;
1492 static const char *rename_score_arg
= (const char *)-1;
1493 static struct wt_status s
;
1494 unsigned int progress_flag
= 0;
1496 struct object_id oid
;
1497 static struct option builtin_status_options
[] = {
1498 OPT__VERBOSE(&verbose
, N_("be verbose")),
1499 OPT_SET_INT('s', "short", &status_format
,
1500 N_("show status concisely"), STATUS_FORMAT_SHORT
),
1501 OPT_BOOL('b', "branch", &s
.show_branch
,
1502 N_("show branch information")),
1503 OPT_BOOL(0, "show-stash", &s
.show_stash
,
1504 N_("show stash information")),
1505 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1506 N_("compute full ahead/behind values")),
1507 OPT_CALLBACK_F(0, "porcelain", &status_format
,
1508 N_("version"), N_("machine-readable output"),
1509 PARSE_OPT_OPTARG
, opt_parse_porcelain
),
1510 OPT_SET_INT(0, "long", &status_format
,
1511 N_("show status in long format (default)"),
1512 STATUS_FORMAT_LONG
),
1513 OPT_BOOL('z', "null", &s
.null_termination
,
1514 N_("terminate entries with NUL")),
1515 { OPTION_STRING
, 'u', "untracked-files", &untracked_files_arg
,
1517 N_("show untracked files, optional modes: all, normal, no. (Default: all)"),
1518 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1519 { OPTION_STRING
, 0, "ignored", &ignored_arg
,
1521 N_("show ignored files, optional modes: traditional, matching, no. (Default: traditional)"),
1522 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"traditional" },
1523 { OPTION_STRING
, 0, "ignore-submodules", &ignore_submodule_arg
, N_("when"),
1524 N_("ignore changes to submodules, optional when: all, dirty, untracked. (Default: all)"),
1525 PARSE_OPT_OPTARG
, NULL
, (intptr_t)"all" },
1526 OPT_COLUMN(0, "column", &s
.colopts
, N_("list untracked files in columns")),
1527 OPT_BOOL(0, "no-renames", &no_renames
, N_("do not detect renames")),
1528 OPT_CALLBACK_F('M', "find-renames", &rename_score_arg
,
1529 N_("n"), N_("detect renames, optionally set similarity index"),
1530 PARSE_OPT_OPTARG
| PARSE_OPT_NONEG
, opt_parse_rename_score
),
1534 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1535 usage_with_options(builtin_status_usage
, builtin_status_options
);
1537 prepare_repo_settings(the_repository
);
1538 the_repository
->settings
.command_requires_full_index
= 0;
1540 status_init_config(&s
, git_status_config
);
1541 argc
= parse_options(argc
, argv
, prefix
,
1542 builtin_status_options
,
1543 builtin_status_usage
, 0);
1544 finalize_colopts(&s
.colopts
, -1);
1545 finalize_deferred_config(&s
);
1547 handle_untracked_files_arg(&s
);
1548 handle_ignored_arg(&s
);
1550 if (s
.show_ignored_mode
== SHOW_MATCHING_IGNORED
&&
1551 s
.show_untracked_files
== SHOW_NO_UNTRACKED_FILES
)
1552 die(_("Unsupported combination of ignored and untracked-files arguments"));
1554 parse_pathspec(&s
.pathspec
, 0,
1555 PATHSPEC_PREFER_FULL
,
1558 if (status_format
!= STATUS_FORMAT_PORCELAIN
&&
1559 status_format
!= STATUS_FORMAT_PORCELAIN_V2
)
1560 progress_flag
= REFRESH_PROGRESS
;
1561 repo_read_index(the_repository
);
1562 refresh_index(&the_index
,
1563 REFRESH_QUIET
|REFRESH_UNMERGED
|progress_flag
,
1564 &s
.pathspec
, NULL
, NULL
);
1566 if (use_optional_locks())
1567 fd
= repo_hold_locked_index(the_repository
, &index_lock
, 0);
1571 s
.is_initial
= get_oid(s
.reference
, &oid
) ? 1 : 0;
1573 oidcpy(&s
.oid_commit
, &oid
);
1575 s
.ignore_submodule_arg
= ignore_submodule_arg
;
1576 s
.status_format
= status_format
;
1577 s
.verbose
= verbose
;
1578 if (no_renames
!= -1)
1579 s
.detect_rename
= !no_renames
;
1580 if ((intptr_t)rename_score_arg
!= -1) {
1581 if (s
.detect_rename
< DIFF_DETECT_RENAME
)
1582 s
.detect_rename
= DIFF_DETECT_RENAME
;
1583 if (rename_score_arg
)
1584 s
.rename_score
= parse_rename_score(&rename_score_arg
);
1587 wt_status_collect(&s
);
1590 repo_update_index_if_able(the_repository
, &index_lock
);
1592 if (s
.relative_paths
)
1595 wt_status_print(&s
);
1596 wt_status_collect_free_buffers(&s
);
1601 static int git_commit_config(const char *k
, const char *v
, void *cb
)
1603 struct wt_status
*s
= cb
;
1605 if (!strcmp(k
, "commit.template"))
1606 return git_config_pathname(&template_file
, k
, v
);
1607 if (!strcmp(k
, "commit.status")) {
1608 include_status
= git_config_bool(k
, v
);
1611 if (!strcmp(k
, "commit.cleanup"))
1612 return git_config_string(&cleanup_arg
, k
, v
);
1613 if (!strcmp(k
, "commit.gpgsign")) {
1614 sign_commit
= git_config_bool(k
, v
) ? "" : NULL
;
1617 if (!strcmp(k
, "commit.verbose")) {
1619 config_commit_verbose
= git_config_bool_or_int(k
, v
, &is_bool
);
1623 return git_status_config(k
, v
, s
);
1626 int cmd_commit(int argc
, const char **argv
, const char *prefix
)
1628 static struct wt_status s
;
1629 static struct option builtin_commit_options
[] = {
1630 OPT__QUIET(&quiet
, N_("suppress summary after successful commit")),
1631 OPT__VERBOSE(&verbose
, N_("show diff in commit message template")),
1633 OPT_GROUP(N_("Commit message options")),
1634 OPT_FILENAME('F', "file", &logfile
, N_("read message from file")),
1635 OPT_STRING(0, "author", &force_author
, N_("author"), N_("override author for commit")),
1636 OPT_STRING(0, "date", &force_date
, N_("date"), N_("override date for commit")),
1637 OPT_CALLBACK('m', "message", &message
, N_("message"), N_("commit message"), opt_parse_m
),
1638 OPT_STRING('c', "reedit-message", &edit_message
, N_("commit"), N_("reuse and edit message from specified commit")),
1639 OPT_STRING('C', "reuse-message", &use_message
, N_("commit"), N_("reuse message from specified commit")),
1641 * TRANSLATORS: Leave "[(amend|reword):]" as-is,
1642 * and only translate <commit>.
1644 OPT_STRING(0, "fixup", &fixup_message
, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")),
1645 OPT_STRING(0, "squash", &squash_message
, N_("commit"), N_("use autosquash formatted message to squash specified commit")),
1646 OPT_BOOL(0, "reset-author", &renew_authorship
, N_("the commit is authored by me now (used with -C/-c/--amend)")),
1647 OPT_CALLBACK_F(0, "trailer", &trailer_args
, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG
, opt_pass_trailer
),
1648 OPT_BOOL('s', "signoff", &signoff
, N_("add a Signed-off-by trailer")),
1649 OPT_FILENAME('t', "template", &template_file
, N_("use specified template file")),
1650 OPT_BOOL('e', "edit", &edit_flag
, N_("force edit of commit")),
1651 OPT_CLEANUP(&cleanup_arg
),
1652 OPT_BOOL(0, "status", &include_status
, N_("include status in commit message template")),
1653 { OPTION_STRING
, 'S', "gpg-sign", &sign_commit
, N_("key-id"),
1654 N_("GPG sign commit"), PARSE_OPT_OPTARG
, NULL
, (intptr_t) "" },
1655 /* end commit message options */
1657 OPT_GROUP(N_("Commit contents options")),
1658 OPT_BOOL('a', "all", &all
, N_("commit all changed files")),
1659 OPT_BOOL('i', "include", &also
, N_("add specified files to index for commit")),
1660 OPT_BOOL(0, "interactive", &interactive
, N_("interactively add files")),
1661 OPT_BOOL('p', "patch", &patch_interactive
, N_("interactively add changes")),
1662 OPT_BOOL('o', "only", &only
, N_("commit only specified files")),
1663 OPT_BOOL('n', "no-verify", &no_verify
, N_("bypass pre-commit and commit-msg hooks")),
1664 OPT_BOOL(0, "dry-run", &dry_run
, N_("show what would be committed")),
1665 OPT_SET_INT(0, "short", &status_format
, N_("show status concisely"),
1666 STATUS_FORMAT_SHORT
),
1667 OPT_BOOL(0, "branch", &s
.show_branch
, N_("show branch information")),
1668 OPT_BOOL(0, "ahead-behind", &s
.ahead_behind_flags
,
1669 N_("compute full ahead/behind values")),
1670 OPT_SET_INT(0, "porcelain", &status_format
,
1671 N_("machine-readable output"), STATUS_FORMAT_PORCELAIN
),
1672 OPT_SET_INT(0, "long", &status_format
,
1673 N_("show status in long format (default)"),
1674 STATUS_FORMAT_LONG
),
1675 OPT_BOOL('z', "null", &s
.null_termination
,
1676 N_("terminate entries with NUL")),
1677 OPT_BOOL(0, "amend", &amend
, N_("amend previous commit")),
1678 OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite
, N_("bypass post-rewrite hook")),
1679 { 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" },
1680 OPT_PATHSPEC_FROM_FILE(&pathspec_from_file
),
1681 OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul
),
1682 /* end commit contents options */
1684 OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty
,
1685 N_("ok to record an empty change")),
1686 OPT_HIDDEN_BOOL(0, "allow-empty-message", &allow_empty_message
,
1687 N_("ok to record a change with an empty message")),
1692 struct strbuf sb
= STRBUF_INIT
;
1693 struct strbuf author_ident
= STRBUF_INIT
;
1694 const char *index_file
, *reflog_msg
;
1695 struct object_id oid
;
1696 struct commit_list
*parents
= NULL
;
1697 struct stat statbuf
;
1698 struct commit
*current_head
= NULL
;
1699 struct commit_extra_header
*extra
= NULL
;
1700 struct strbuf err
= STRBUF_INIT
;
1703 if (argc
== 2 && !strcmp(argv
[1], "-h"))
1704 usage_with_options(builtin_commit_usage
, builtin_commit_options
);
1706 prepare_repo_settings(the_repository
);
1707 the_repository
->settings
.command_requires_full_index
= 0;
1709 status_init_config(&s
, git_commit_config
);
1710 s
.commit_template
= 1;
1711 status_format
= STATUS_FORMAT_NONE
; /* Ignore status.short */
1714 if (get_oid("HEAD", &oid
))
1715 current_head
= NULL
;
1717 current_head
= lookup_commit_or_die(&oid
, "HEAD");
1718 if (parse_commit(current_head
))
1719 die(_("could not parse HEAD commit"));
1721 verbose
= -1; /* unspecified */
1722 argc
= parse_and_validate_options(argc
, argv
, builtin_commit_options
,
1723 builtin_commit_usage
,
1724 prefix
, current_head
, &s
);
1726 verbose
= (config_commit_verbose
< 0) ? 0 : config_commit_verbose
;
1729 return dry_run_commit(argv
, prefix
, current_head
, &s
);
1730 index_file
= prepare_index(argv
, prefix
, current_head
, 0);
1732 /* Set up everything for writing the commit object. This includes
1733 running hooks, writing the trees, and interacting with the user. */
1734 if (!prepare_to_commit(index_file
, prefix
,
1735 current_head
, &s
, &author_ident
)) {
1737 rollback_index_files();
1741 /* Determine parents */
1742 reflog_msg
= getenv("GIT_REFLOG_ACTION");
1743 if (!current_head
) {
1745 reflog_msg
= "commit (initial)";
1748 reflog_msg
= "commit (amend)";
1749 parents
= copy_commit_list(current_head
->parents
);
1750 } else if (whence
== FROM_MERGE
) {
1751 struct strbuf m
= STRBUF_INIT
;
1753 int allow_fast_forward
= 1;
1754 struct commit_list
**pptr
= &parents
;
1757 reflog_msg
= "commit (merge)";
1758 pptr
= commit_list_append(current_head
, pptr
);
1759 fp
= xfopen(git_path_merge_head(the_repository
), "r");
1760 while (strbuf_getline_lf(&m
, fp
) != EOF
) {
1761 struct commit
*parent
;
1763 parent
= get_merge_parent(m
.buf
);
1765 die(_("Corrupt MERGE_HEAD file (%s)"), m
.buf
);
1766 pptr
= commit_list_append(parent
, pptr
);
1770 if (!stat(git_path_merge_mode(the_repository
), &statbuf
)) {
1771 if (strbuf_read_file(&sb
, git_path_merge_mode(the_repository
), 0) < 0)
1772 die_errno(_("could not read MERGE_MODE"));
1773 if (!strcmp(sb
.buf
, "no-ff"))
1774 allow_fast_forward
= 0;
1776 if (allow_fast_forward
)
1777 reduce_heads_replace(&parents
);
1780 reflog_msg
= is_from_cherry_pick(whence
)
1781 ? "commit (cherry-pick)"
1782 : is_from_rebase(whence
)
1785 commit_list_insert(current_head
, &parents
);
1788 /* Finally, get the commit message */
1790 if (strbuf_read_file(&sb
, git_path_commit_editmsg(), 0) < 0) {
1791 int saved_errno
= errno
;
1792 rollback_index_files();
1793 die(_("could not read commit message: %s"), strerror(saved_errno
));
1796 cleanup_message(&sb
, cleanup_mode
, verbose
);
1798 if (message_is_empty(&sb
, cleanup_mode
) && !allow_empty_message
) {
1799 rollback_index_files();
1800 fprintf(stderr
, _("Aborting commit due to empty commit message.\n"));
1803 if (template_untouched(&sb
, template_file
, cleanup_mode
) && !allow_empty_message
) {
1804 rollback_index_files();
1805 fprintf(stderr
, _("Aborting commit; you did not edit the message.\n"));
1809 if (fixup_message
&& starts_with(sb
.buf
, "amend! ") &&
1810 !allow_empty_message
) {
1811 struct strbuf body
= STRBUF_INIT
;
1812 size_t len
= commit_subject_length(sb
.buf
);
1813 strbuf_addstr(&body
, sb
.buf
+ len
);
1814 if (message_is_empty(&body
, cleanup_mode
)) {
1815 rollback_index_files();
1816 fprintf(stderr
, _("Aborting commit due to empty commit message body.\n"));
1819 strbuf_release(&body
);
1823 const char *exclude_gpgsig
[3] = { "gpgsig", "gpgsig-sha256", NULL
};
1824 extra
= read_commit_extra_headers(current_head
, exclude_gpgsig
);
1826 struct commit_extra_header
**tail
= &extra
;
1827 append_merge_tag_headers(parents
, &tail
);
1830 if (commit_tree_extended(sb
.buf
, sb
.len
, &the_index
.cache_tree
->oid
,
1831 parents
, &oid
, author_ident
.buf
, NULL
,
1832 sign_commit
, extra
)) {
1833 rollback_index_files();
1834 die(_("failed to write commit object"));
1836 free_commit_extra_headers(extra
);
1838 if (update_head_with_reflog(current_head
, &oid
, reflog_msg
, &sb
,
1840 rollback_index_files();
1844 sequencer_post_commit_cleanup(the_repository
, 0);
1845 unlink(git_path_merge_head(the_repository
));
1846 unlink(git_path_merge_msg(the_repository
));
1847 unlink(git_path_merge_mode(the_repository
));
1848 unlink(git_path_squash_msg(the_repository
));
1850 if (commit_index_files())
1851 die(_("repository has been updated, but unable to write\n"
1852 "new_index file. Check that disk is not full and quota is\n"
1853 "not exceeded, and then \"git restore --staged :/\" to recover."));
1855 git_test_write_commit_graph_or_die();
1857 repo_rerere(the_repository
, 0);
1858 run_auto_maintenance(quiet
);
1859 run_commit_hook(use_editor
, get_index_file(), NULL
, "post-commit",
1861 if (amend
&& !no_post_rewrite
) {
1862 commit_post_rewrite(the_repository
, current_head
, &oid
);
1865 unsigned int flags
= 0;
1868 flags
|= SUMMARY_INITIAL_COMMIT
;
1869 if (author_date_is_interesting())
1870 flags
|= SUMMARY_SHOW_AUTHOR_DATE
;
1871 print_commit_summary(the_repository
, prefix
,
1875 apply_autostash(git_path_merge_autostash(the_repository
));
1878 strbuf_release(&author_ident
);
1879 strbuf_release(&err
);
1880 strbuf_release(&sb
);