Fix commit-msg hook to allow editing

The old git-commit.sh script allowed the commit-msg hook to not only
prevent a commit from proceding, but also to edit the commit message
on the fly and allow it to proceed. So here we teach builtin-commit
to do the same.

This is based on Wincent's patch, but redone with a clarified logic.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano 2007-12-08 23:23:20 -08:00
Родитель aa6da6cddb
Коммит 740001a578
2 изменённых файлов: 7 добавлений и 4 удалений

Просмотреть файл

@ -787,16 +787,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
char index[PATH_MAX]; char index[PATH_MAX];
const char *env[2] = { index, NULL }; const char *env[2] = { index, NULL };
snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file); snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
launch_editor(git_path(commit_editmsg), &sb, env); launch_editor(git_path(commit_editmsg), NULL, env);
} else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
rollback_index_files();
die("could not read commit message");
} }
if (!no_verify && if (!no_verify &&
run_hook(index_file, "commit-msg", git_path(commit_editmsg))) { run_hook(index_file, "commit-msg", git_path(commit_editmsg))) {
rollback_index_files(); rollback_index_files();
exit(1); exit(1);
} }
if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0) {
rollback_index_files();
die("could not read commit message");
}
/* Truncate the message just before the diff, if any. */ /* Truncate the message just before the diff, if any. */
p = strstr(sb.buf, "\ndiff --git a/"); p = strstr(sb.buf, "\ndiff --git a/");

Просмотреть файл

@ -53,6 +53,8 @@ void launch_editor(const char *path, struct strbuf *buffer, const char *const *e
die("There was a problem with the editor %s.", editor); die("There was a problem with the editor %s.", editor);
} }
if (!buffer)
return;
if (strbuf_read_file(buffer, path, 0) < 0) if (strbuf_read_file(buffer, path, 0) < 0)
die("could not read message file '%s': %s", die("could not read message file '%s': %s",
path, strerror(errno)); path, strerror(errno));