Merge branch 'nd/war-on-nul-in-commit'

* nd/war-on-nul-in-commit:
  commit_tree(): refuse commit messages that contain NULs
  Convert commit_tree() to take strbuf as message
  merge: abort if fails to commit

Conflicts:
	builtin/commit.c
	commit.c
	commit.h
This commit is contained in:
Junio C Hamano 2011-12-22 11:27:26 -08:00
commit f35ccd9be2
10 changed files with 33 additions and 17 deletions

View File

@ -98,7 +98,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
die_errno("git commit-tree: failed to read"); die_errno("git commit-tree: failed to read");
} }
if (commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) { if (commit_tree(&buffer, tree_sha1, parents, commit_sha1, NULL)) {
strbuf_release(&buffer); strbuf_release(&buffer);
return 1; return 1;
} }

View File

@ -1492,7 +1492,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
append_merge_tag_headers(parents, &tail); append_merge_tag_headers(parents, &tail);
} }
if (commit_tree_extended(sb.buf, active_cache_tree->sha1, parents, sha1, if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1,
author_ident.buf, extra)) { author_ident.buf, extra)) {
rollback_index_files(); rollback_index_files();
die(_("failed to write commit object")); die(_("failed to write commit object"));

View File

@ -910,7 +910,8 @@ static int merge_trivial(struct commit *head)
parent->next->item = remoteheads->item; parent->next->item = remoteheads->item;
parent->next->next = NULL; parent->next->next = NULL;
prepare_to_commit(); prepare_to_commit();
commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL); if (commit_tree(&merge_msg, result_tree, parent, result_commit, NULL))
die(_("failed to write commit object"));
finish(head, result_commit, "In-index merge"); finish(head, result_commit, "In-index merge");
drop_save(); drop_save();
return 0; return 0;
@ -941,7 +942,8 @@ static int finish_automerge(struct commit *head,
strbuf_addch(&merge_msg, '\n'); strbuf_addch(&merge_msg, '\n');
prepare_to_commit(); prepare_to_commit();
free_commit_list(remoteheads); free_commit_list(remoteheads);
commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL); if (commit_tree(&merge_msg, result_tree, parents, result_commit, NULL))
die(_("failed to write commit object"));
strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy); strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
finish(head, result_commit, buf.buf); finish(head, result_commit, buf.buf);
strbuf_release(&buf); strbuf_release(&buf);

View File

@ -301,12 +301,12 @@ void commit_notes(struct notes_tree *t, const char *msg)
return; /* don't have to commit an unchanged tree */ return; /* don't have to commit an unchanged tree */
/* Prepare commit message and reflog message */ /* Prepare commit message and reflog message */
strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
strbuf_addstr(&buf, msg); strbuf_addstr(&buf, msg);
if (buf.buf[buf.len - 1] != '\n') if (buf.buf[buf.len - 1] != '\n')
strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */ strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
create_notes_commit(t, NULL, buf.buf + 7, commit_sha1); create_notes_commit(t, NULL, &buf, commit_sha1);
strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR); update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
strbuf_release(&buf); strbuf_release(&buf);

View File

@ -973,7 +973,7 @@ void free_commit_extra_headers(struct commit_extra_header *extra)
} }
} }
int commit_tree(const char *msg, unsigned char *tree, int commit_tree(const struct strbuf *msg, unsigned char *tree,
struct commit_list *parents, unsigned char *ret, struct commit_list *parents, unsigned char *ret,
const char *author) const char *author)
{ {
@ -991,7 +991,7 @@ static const char commit_utf8_warn[] =
"You may want to amend it after fixing the message, or set the config\n" "You may want to amend it after fixing the message, or set the config\n"
"variable i18n.commitencoding to the encoding your project uses.\n"; "variable i18n.commitencoding to the encoding your project uses.\n";
int commit_tree_extended(const char *msg, unsigned char *tree, int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
struct commit_list *parents, unsigned char *ret, struct commit_list *parents, unsigned char *ret,
const char *author, struct commit_extra_header *extra) const char *author, struct commit_extra_header *extra)
{ {
@ -1001,6 +1001,9 @@ int commit_tree_extended(const char *msg, unsigned char *tree,
assert_sha1_type(tree, OBJ_TREE); assert_sha1_type(tree, OBJ_TREE);
if (memchr(msg->buf, '\0', msg->len))
return error("a NUL byte in commit log message not allowed.");
/* Not having i18n.commitencoding is the same as having utf-8 */ /* Not having i18n.commitencoding is the same as having utf-8 */
encoding_is_utf8 = is_encoding_utf8(git_commit_encoding); encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
@ -1037,7 +1040,7 @@ int commit_tree_extended(const char *msg, unsigned char *tree,
strbuf_addch(&buffer, '\n'); strbuf_addch(&buffer, '\n');
/* And add the comment */ /* And add the comment */
strbuf_addstr(&buffer, msg); strbuf_addbuf(&buffer, msg);
/* And check the encoding */ /* And check the encoding */
if (encoding_is_utf8 && !is_utf8(buffer.buf)) if (encoding_is_utf8 && !is_utf8(buffer.buf))

View File

@ -191,11 +191,11 @@ struct commit_extra_header {
extern void append_merge_tag_headers(struct commit_list *parents, extern void append_merge_tag_headers(struct commit_list *parents,
struct commit_extra_header ***tail); struct commit_extra_header ***tail);
extern int commit_tree(const char *msg, unsigned char *tree, extern int commit_tree(const struct strbuf *msg, unsigned char *tree,
struct commit_list *parents, unsigned char *ret, struct commit_list *parents, unsigned char *ret,
const char *author); const char *author);
extern int commit_tree_extended(const char *msg, unsigned char *tree, extern int commit_tree_extended(const struct strbuf *msg, unsigned char *tree,
struct commit_list *parents, unsigned char *ret, struct commit_list *parents, unsigned char *ret,
const char *author, const char *author,
struct commit_extra_header *); struct commit_extra_header *);

View File

@ -48,6 +48,7 @@ int notes_cache_write(struct notes_cache *c)
{ {
unsigned char tree_sha1[20]; unsigned char tree_sha1[20];
unsigned char commit_sha1[20]; unsigned char commit_sha1[20];
struct strbuf msg = STRBUF_INIT;
if (!c || !c->tree.initialized || !c->tree.ref || !*c->tree.ref) if (!c || !c->tree.initialized || !c->tree.ref || !*c->tree.ref)
return -1; return -1;
@ -56,7 +57,9 @@ int notes_cache_write(struct notes_cache *c)
if (write_notes_tree(&c->tree, tree_sha1)) if (write_notes_tree(&c->tree, tree_sha1))
return -1; return -1;
if (commit_tree(c->validity, tree_sha1, NULL, commit_sha1, NULL) < 0) strbuf_attach(&msg, c->validity,
strlen(c->validity), strlen(c->validity) + 1);
if (commit_tree(&msg, tree_sha1, NULL, commit_sha1, NULL) < 0)
return -1; return -1;
if (update_ref("update notes cache", c->tree.ref, commit_sha1, NULL, if (update_ref("update notes cache", c->tree.ref, commit_sha1, NULL,
0, QUIET_ON_ERR) < 0) 0, QUIET_ON_ERR) < 0)

View File

@ -530,7 +530,7 @@ static int merge_from_diffs(struct notes_merge_options *o,
} }
void create_notes_commit(struct notes_tree *t, struct commit_list *parents, void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
const char *msg, unsigned char *result_sha1) const struct strbuf *msg, unsigned char *result_sha1)
{ {
unsigned char tree_sha1[20]; unsigned char tree_sha1[20];
@ -668,7 +668,7 @@ int notes_merge(struct notes_merge_options *o,
struct commit_list *parents = NULL; struct commit_list *parents = NULL;
commit_list_insert(remote, &parents); /* LIFO order */ commit_list_insert(remote, &parents); /* LIFO order */
commit_list_insert(local, &parents); commit_list_insert(local, &parents);
create_notes_commit(local_tree, parents, o->commit_msg.buf, create_notes_commit(local_tree, parents, &o->commit_msg,
result_sha1); result_sha1);
} }
@ -695,7 +695,8 @@ int notes_merge_commit(struct notes_merge_options *o,
struct dir_struct dir; struct dir_struct dir;
char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/")); char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/"));
int path_len = strlen(path), i; int path_len = strlen(path), i;
const char *msg = strstr(partial_commit->buffer, "\n\n"); char *msg = strstr(partial_commit->buffer, "\n\n");
struct strbuf sb_msg = STRBUF_INIT;
if (o->verbosity >= 3) if (o->verbosity >= 3)
printf("Committing notes in notes merge worktree at %.*s\n", printf("Committing notes in notes merge worktree at %.*s\n",
@ -733,7 +734,8 @@ int notes_merge_commit(struct notes_merge_options *o,
sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1)); sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1));
} }
create_notes_commit(partial_tree, partial_commit->parents, msg, strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1);
create_notes_commit(partial_tree, partial_commit->parents, &sb_msg,
result_sha1); result_sha1);
if (o->verbosity >= 4) if (o->verbosity >= 4)
printf("Finalized notes merge commit: %s\n", printf("Finalized notes merge commit: %s\n",

View File

@ -37,7 +37,7 @@ void init_notes_merge_options(struct notes_merge_options *o);
* The resulting commit SHA1 is stored in result_sha1. * The resulting commit SHA1 is stored in result_sha1.
*/ */
void create_notes_commit(struct notes_tree *t, struct commit_list *parents, void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
const char *msg, unsigned char *result_sha1); const struct strbuf *msg, unsigned char *result_sha1);
/* /*
* Merge notes from o->remote_ref into o->local_ref * Merge notes from o->remote_ref into o->local_ref

View File

@ -34,6 +34,12 @@ test_expect_success 'no encoding header for base case' '
test z = "z$E" test z = "z$E"
' '
test_expect_failure 'UTF-16 refused because of NULs' '
echo UTF-16 >F &&
git commit -a -F "$TEST_DIRECTORY"/t3900/UTF-16.txt
'
for H in ISO8859-1 eucJP ISO-2022-JP for H in ISO8859-1 eucJP ISO-2022-JP
do do
test_expect_success "$H setup" ' test_expect_success "$H setup" '