in builtin/commit.c [743:1118]
static int prepare_to_commit(const char *index_file, const char *prefix,
struct commit *current_head,
struct wt_status *s,
struct strbuf *author_ident)
{
struct stat statbuf;
struct strbuf committer_ident = STRBUF_INIT;
int committable;
struct strbuf sb = STRBUF_INIT;
const char *hook_arg1 = NULL;
const char *hook_arg2 = NULL;
int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
int old_display_comment_prefix;
int invoked_hook;
/* This checks and barfs if author is badly specified */
determine_author_info(author_ident);
if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
"pre-commit", NULL))
return 0;
if (squash_message) {
/*
* Insert the proper subject line before other commit
* message options add their content.
*/
if (use_message && !strcmp(use_message, squash_message))
strbuf_addstr(&sb, "squash! ");
else {
struct pretty_print_context ctx = {0};
struct commit *c;
c = lookup_commit_reference_by_name(squash_message);
if (!c)
die(_("could not lookup commit '%s'"), squash_message);
ctx.output_encoding = get_commit_output_encoding();
repo_format_commit_message(the_repository, c,
"squash! %s\n\n", &sb,
&ctx);
}
}
if (have_option_m && !fixup_message) {
strbuf_addbuf(&sb, &message);
hook_arg1 = "message";
} else if (logfile && !strcmp(logfile, "-")) {
if (isatty(0))
fprintf(stderr, _("(reading log message from standard input)\n"));
if (strbuf_read(&sb, 0, 0) < 0)
die_errno(_("could not read log from standard input"));
hook_arg1 = "message";
} else if (logfile) {
if (strbuf_read_file(&sb, logfile, 0) < 0)
die_errno(_("could not read log file '%s'"),
logfile);
hook_arg1 = "message";
} else if (use_message) {
char *buffer;
buffer = strstr(use_message_buffer, "\n\n");
if (buffer)
strbuf_addstr(&sb, skip_blank_lines(buffer + 2));
hook_arg1 = "commit";
hook_arg2 = use_message;
} else if (fixup_message) {
struct pretty_print_context ctx = {0};
struct commit *commit;
char *fmt;
commit = lookup_commit_reference_by_name(fixup_commit);
if (!commit)
die(_("could not lookup commit '%s'"), fixup_commit);
ctx.output_encoding = get_commit_output_encoding();
fmt = xstrfmt("%s! %%s\n\n", fixup_prefix);
repo_format_commit_message(the_repository, commit, fmt, &sb,
&ctx);
free(fmt);
hook_arg1 = "message";
/*
* Only `-m` commit message option is checked here, as
* it supports `--fixup` to append the commit message.
*
* The other commit message options `-c`/`-C`/`-F` are
* incompatible with all the forms of `--fixup` and
* have already errored out while parsing the `git commit`
* options.
*/
if (have_option_m && !strcmp(fixup_prefix, "fixup"))
strbuf_addbuf(&sb, &message);
if (!strcmp(fixup_prefix, "amend")) {
if (have_option_m)
die(_("options '%s' and '%s:%s' cannot be used together"), "-m", "--fixup", fixup_message);
prepare_amend_commit(commit, &sb, &ctx);
}
} else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
size_t merge_msg_start;
/*
* prepend SQUASH_MSG here if it exists and a
* "merge --squash" was originally performed
*/
if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
die_errno(_("could not read SQUASH_MSG"));
hook_arg1 = "squash";
} else
hook_arg1 = "merge";
merge_msg_start = sb.len;
if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
die_errno(_("could not read MERGE_MSG"));
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS &&
wt_status_locate_end(sb.buf + merge_msg_start,
sb.len - merge_msg_start) <
sb.len - merge_msg_start)
s->added_cut_line = 1;
} else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
die_errno(_("could not read SQUASH_MSG"));
hook_arg1 = "squash";
} else if (template_file) {
if (strbuf_read_file(&sb, template_file, 0) < 0)
die_errno(_("could not read '%s'"), template_file);
hook_arg1 = "template";
clean_message_contents = 0;
}
/*
* The remaining cases don't modify the template message, but
* just set the argument(s) to the prepare-commit-msg hook.
*/
else if (whence == FROM_MERGE)
hook_arg1 = "merge";
else if (is_from_cherry_pick(whence) || whence == FROM_REBASE_PICK) {
hook_arg1 = "commit";
hook_arg2 = "CHERRY_PICK_HEAD";
}
if (squash_message) {
/*
* If squash_commit was used for the commit subject,
* then we're possibly hijacking other commit log options.
* Reset the hook args to tell the real story.
*/
hook_arg1 = "message";
hook_arg2 = "";
}
s->fp = fopen_for_writing(git_path_commit_editmsg());
if (!s->fp)
die_errno(_("could not open '%s'"), git_path_commit_editmsg());
/* Ignore status.displayCommentPrefix: we do need comments in COMMIT_EDITMSG. */
old_display_comment_prefix = s->display_comment_prefix;
s->display_comment_prefix = 1;
/*
* Most hints are counter-productive when the commit has
* already started.
*/
s->hints = 0;
if (clean_message_contents)
strbuf_stripspace(&sb, NULL);
if (signoff)
append_signoff(&sb, ignored_log_message_bytes(sb.buf, sb.len), 0);
if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
die_errno(_("could not write commit template"));
if (auto_comment_line_char)
adjust_comment_line_char(&sb);
strbuf_release(&sb);
/* This checks if committer ident is explicitly given */
strbuf_addstr(&committer_ident, git_committer_info(IDENT_STRICT));
if (use_editor && include_status) {
int ident_shown = 0;
int saved_color_setting;
struct ident_split ci, ai;
const char *hint_cleanup_all = allow_empty_message ?
_("Please enter the commit message for your changes."
" Lines starting\nwith '%s' will be ignored.\n") :
_("Please enter the commit message for your changes."
" Lines starting\nwith '%s' will be ignored, and an empty"
" message aborts the commit.\n");
const char *hint_cleanup_space = allow_empty_message ?
_("Please enter the commit message for your changes."
" Lines starting\n"
"with '%s' will be kept; you may remove them"
" yourself if you want to.\n") :
_("Please enter the commit message for your changes."
" Lines starting\n"
"with '%s' will be kept; you may remove them"
" yourself if you want to.\n"
"An empty message aborts the commit.\n");
if (whence != FROM_COMMIT) {
if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS)
wt_status_add_cut_line(s);
status_printf_ln(
s, GIT_COLOR_NORMAL,
whence == FROM_MERGE ?
_("\n"
"It looks like you may be committing a merge.\n"
"If this is not correct, please run\n"
" git update-ref -d MERGE_HEAD\n"
"and try again.\n") :
_("\n"
"It looks like you may be committing a cherry-pick.\n"
"If this is not correct, please run\n"
" git update-ref -d CHERRY_PICK_HEAD\n"
"and try again.\n"));
}
fprintf(s->fp, "\n");
if (cleanup_mode == COMMIT_MSG_CLEANUP_ALL)
status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_all, comment_line_str);
else if (cleanup_mode == COMMIT_MSG_CLEANUP_SCISSORS) {
if (whence == FROM_COMMIT)
wt_status_add_cut_line(s);
} else /* COMMIT_MSG_CLEANUP_SPACE, that is. */
status_printf(s, GIT_COLOR_NORMAL, hint_cleanup_space, comment_line_str);
/*
* These should never fail because they come from our own
* fmt_ident. They may fail the sane_ident test, but we know
* that the name and mail pointers will at least be valid,
* which is enough for our tests and printing here.
*/
assert_split_ident(&ai, author_ident);
assert_split_ident(&ci, &committer_ident);
if (ident_cmp(&ai, &ci))
status_printf_ln(s, GIT_COLOR_NORMAL,
_("%s"
"Author: %.*s <%.*s>"),
ident_shown++ ? "" : "\n",
(int)(ai.name_end - ai.name_begin), ai.name_begin,
(int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
if (author_date_is_interesting())
status_printf_ln(s, GIT_COLOR_NORMAL,
_("%s"
"Date: %s"),
ident_shown++ ? "" : "\n",
show_ident_date(&ai, DATE_MODE(NORMAL)));
if (!committer_ident_sufficiently_given())
status_printf_ln(s, GIT_COLOR_NORMAL,
_("%s"
"Committer: %.*s <%.*s>"),
ident_shown++ ? "" : "\n",
(int)(ci.name_end - ci.name_begin), ci.name_begin,
(int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
status_printf_ln(s, GIT_COLOR_NORMAL, "%s", ""); /* Add new line for clarity */
saved_color_setting = s->use_color;
s->use_color = 0;
committable = run_status(s->fp, index_file, prefix, 1, s);
s->use_color = saved_color_setting;
string_list_clear_func(&s->change, change_data_free);
} else {
struct object_id oid;
const char *parent = "HEAD";
if (!the_repository->index->initialized && repo_read_index(the_repository) < 0)
die(_("Cannot read index"));
if (amend)
parent = "HEAD^1";
if (repo_get_oid(the_repository, parent, &oid)) {
int i, ita_nr = 0;
/* TODO: audit for interaction with sparse-index. */
ensure_full_index(the_repository->index);
for (i = 0; i < the_repository->index->cache_nr; i++)
if (ce_intent_to_add(the_repository->index->cache[i]))
ita_nr++;
committable = the_repository->index->cache_nr - ita_nr > 0;
} else {
/*
* Unless the user did explicitly request a submodule
* ignore mode by passing a command line option we do
* not ignore any changed submodule SHA-1s when
* comparing index and parent, no matter what is
* configured. Otherwise we won't commit any
* submodules which were manually staged, which would
* be really confusing.
*/
struct diff_flags flags = DIFF_FLAGS_INIT;
flags.override_submodule_config = 1;
if (ignore_submodule_arg &&
!strcmp(ignore_submodule_arg, "all"))
flags.ignore_submodules = 1;
committable = index_differs_from(the_repository,
parent, &flags, 1);
}
}
strbuf_release(&committer_ident);
fclose(s->fp);
if (trailer_args.nr) {
if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args))
die(_("unable to pass trailers to --trailers"));
strvec_clear(&trailer_args);
}
/*
* Reject an attempt to record a non-merge empty commit without
* explicit --allow-empty. In the cherry-pick case, it may be
* empty due to conflict resolution, which the user should okay.
*/
if (!committable && whence != FROM_MERGE && !allow_empty &&
!(amend && is_a_merge(current_head))) {
s->hints = advice_enabled(ADVICE_STATUS_HINTS);
s->display_comment_prefix = old_display_comment_prefix;
run_status(stdout, index_file, prefix, 0, s);
if (amend)
fputs(_(empty_amend_advice), stderr);
else if (is_from_cherry_pick(whence) ||
whence == FROM_REBASE_PICK) {
fputs(_(empty_cherry_pick_advice), stderr);
if (whence == FROM_CHERRY_PICK_SINGLE)
fputs(_(empty_cherry_pick_advice_single), stderr);
else if (whence == FROM_CHERRY_PICK_MULTI)
fputs(_(empty_cherry_pick_advice_multi), stderr);
else
fputs(_(empty_rebase_pick_advice), stderr);
}
return 0;
}
if (!no_verify && invoked_hook) {
/*
* Re-read the index as the pre-commit-commit hook was invoked
* and could have updated it. We must do this before we invoke
* the editor and after we invoke run_status above.
*/
discard_index(the_repository->index);
}
read_index_from(the_repository->index, index_file, repo_get_git_dir(the_repository));
if (cache_tree_update(the_repository->index, 0)) {
error(_("Error building trees"));
return 0;
}
if (run_commit_hook(use_editor, index_file, NULL, "prepare-commit-msg",
git_path_commit_editmsg(), hook_arg1, hook_arg2, NULL))
return 0;
if (use_editor) {
struct strvec env = STRVEC_INIT;
strvec_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
if (launch_editor(git_path_commit_editmsg(), NULL, env.v)) {
fprintf(stderr,
_("Please supply the message using either -m or -F option.\n"));
exit(1);
}
strvec_clear(&env);
}
if (!no_verify &&
run_commit_hook(use_editor, index_file, NULL, "commit-msg",
git_path_commit_editmsg(), NULL)) {
return 0;
}
return 1;
}