@ -377,9 +377,17 @@ static int run_hook(const char *index_file, const char *name, ...)
@@ -377,9 +377,17 @@ static int run_hook(const char *index_file, const char *name, ...)
return run_command(&hook);
}
static int is_a_merge(const unsigned char *sha1)
{
struct commit *commit = lookup_commit(sha1);
if (!commit || parse_commit(commit))
die("could not parse HEAD commit");
return !!(commit->parents && commit->parents->next);
}
static const char sign_off_header[] = "Signed-off-by: ";
static int prepare_log_message(const char *index_file, const char *prefix)
static int prepare_to_commit(const char *index_file, const char *prefix)
{
struct stat statbuf;
int commitable, saved_color_setting;
@ -387,6 +395,9 @@ static int prepare_log_message(const char *index_file, const char *prefix)
@@ -387,6 +395,9 @@ static int prepare_log_message(const char *index_file, const char *prefix)
char *buffer;
FILE *fp;
if (!no_verify && run_hook(index_file, "pre-commit", NULL))
return 0;
strbuf_init(&sb, 0);
if (message.len) {
strbuf_addbuf(&sb, &message);
@ -447,32 +458,7 @@ static int prepare_log_message(const char *index_file, const char *prefix)
@@ -447,32 +458,7 @@ static int prepare_log_message(const char *index_file, const char *prefix)
strbuf_release(&sb);
if (!use_editor) {
struct rev_info rev;
unsigned char sha1[20];
const char *parent = "HEAD";
fclose(fp);
if (!active_nr && read_cache() < 0)
die("Cannot read index");
if (amend)
parent = "HEAD^1";
if (get_sha1(parent, sha1))
return !!active_nr;
init_revisions(&rev, "");
rev.abbrev = 0;
setup_revisions(0, NULL, &rev, parent);
DIFF_OPT_SET(&rev.diffopt, QUIET);
DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
run_diff_index(&rev, 1 /* cached */);
return !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
}
if (use_editor) {
if (in_merge)
fprintf(fp,
"#\n"
@ -499,10 +485,68 @@ static int prepare_log_message(const char *index_file, const char *prefix)
@@ -499,10 +485,68 @@ static int prepare_log_message(const char *index_file, const char *prefix)
wt_status_use_color = 0;
commitable = run_status(fp, index_file, prefix, 1);
wt_status_use_color = saved_color_setting;
} else {
struct rev_info rev;
unsigned char sha1[20];
const char *parent = "HEAD";
if (!active_nr && read_cache() < 0)
die("Cannot read index");
if (amend)
parent = "HEAD^1";
if (get_sha1(parent, sha1))
commitable = !!active_nr;
else {
init_revisions(&rev, "");
rev.abbrev = 0;
setup_revisions(0, NULL, &rev, parent);
DIFF_OPT_SET(&rev.diffopt, QUIET);
DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
run_diff_index(&rev, 1 /* cached */);
commitable = !!DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES);
}
}
fclose(fp);
return commitable;
if (!commitable && !in_merge && !allow_empty &&
!(amend && is_a_merge(head_sha1))) {
run_status(stdout, index_file, prefix, 0);
unlink(commit_editmsg);
return 0;
}
/*
* Re-read the index as pre-commit hook could have updated it,
* and write it out as a tree. We must do this before we invoke
* the editor and after we invoke run_status above.
*/
discard_cache();
read_cache_from(index_file);
if (!active_cache_tree)
active_cache_tree = cache_tree();
if (cache_tree_update(active_cache_tree,
active_cache, active_nr, 0, 0) < 0) {
error("Error building trees");
return 0;
}
if (use_editor) {
char index[PATH_MAX];
const char *env[2] = { index, NULL };
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
launch_editor(git_path(commit_editmsg), NULL, env);
}
if (!no_verify &&
run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
return 0;
}
return 1;
}
/*
@ -761,14 +805,6 @@ int git_commit_config(const char *k, const char *v)
@@ -761,14 +805,6 @@ int git_commit_config(const char *k, const char *v)
return git_status_config(k, v);
}
static int is_a_merge(const unsigned char *sha1)
{
struct commit *commit = lookup_commit(sha1);
if (!commit || parse_commit(commit))
die("could not parse HEAD commit");
return !!(commit->parents && commit->parents->next);
}
static const char commit_utf8_warn[] =
"Warning: commit message does not conform to UTF-8.\n"
"You may want to amend it after fixing the message, or set the config\n"
@ -800,33 +836,13 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
@@ -800,33 +836,13 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
index_file = prepare_index(argc, argv, prefix);
if (!no_verify && run_hook(index_file, "pre-commit", NULL)) {
rollback_index_files();
return 1;
}
if (!prepare_log_message(index_file, prefix) && !in_merge &&
!allow_empty && !(amend && is_a_merge(head_sha1))) {
run_status(stdout, index_file, prefix, 0);
/* Set up everything for writing the commit object. This includes
running hooks, writing the trees, and interacting with the user. */
if (!prepare_to_commit(index_file, prefix)) {
rollback_index_files();
unlink(commit_editmsg);
return 1;
}
/*
* Re-read the index as pre-commit hook could have updated it,
* and write it out as a tree.
*/
discard_cache();
read_cache_from(index_file);
if (!active_cache_tree)
active_cache_tree = cache_tree();
if (cache_tree_update(active_cache_tree,
active_cache, active_nr, 0, 0) < 0) {
rollback_index_files();
die("Error building trees");
}
/*
* The commit object
*/
@ -878,19 +894,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
@@ -878,19 +894,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
strbuf_addf(&sb, "encoding %s\n", git_commit_encoding);
strbuf_addch(&sb, '\n');
/* Get the commit message and validate it */
/* Finally, get the commit message */
header_len = sb.len;
if (use_editor) {
char index[PATH_MAX];
const char *env[2] = { index, NULL };
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
launch_editor(git_path(commit_editmsg), NULL, env);
}
if (!no_verify &&
run_hook(index_file, "commit-msg", git_path(commit_editmsg), NULL)) {
rollback_index_files();
exit(1);
}
if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
rollback_index_files();
die("could not read commit message");