commit_tree: take a pointer/len pair rather than a const strbuf

While strbufs are pretty common throughout our code, it is
more flexible for functions to take a pointer/len pair than
a strbuf. It's easy to turn a strbuf into such a pair (by
dereferencing its members), but less easy to go the other
way (you can strbuf_attach, but that has implications about
memory ownership).

This patch teaches commit_tree (and its associated callers
and sub-functions) to take such a pair for the commit
message rather than a strbuf.  This makes passing the buffer
around slightly more verbose, but means we can get rid of
some dangerous strbuf_attach calls in the next patch.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2014-06-10 17:36:52 -04:00 committed by Junio C Hamano
parent bce14aa132
commit 3ffefb54c0
9 changed files with 29 additions and 22 deletions

View File

@ -123,8 +123,8 @@ 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, tree_sha1, parents, commit_sha1, if (commit_tree(buffer.buf, buffer.len, tree_sha1, parents,
NULL, sign_commit)) { commit_sha1, NULL, sign_commit)) {
strbuf_release(&buffer); strbuf_release(&buffer);
return 1; return 1;
} }

View File

@ -1659,8 +1659,8 @@ 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, active_cache_tree->sha1, parents, sha1, if (commit_tree_extended(sb.buf, sb.len, active_cache_tree->sha1,
author_ident.buf, sign_commit, extra)) { parents, sha1, author_ident.buf, sign_commit, extra)) {
rollback_index_files(); rollback_index_files();
die(_("failed to write commit object")); die(_("failed to write commit object"));
} }

View File

@ -852,8 +852,8 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
parent->next->item = remoteheads->item; parent->next->item = remoteheads->item;
parent->next->next = NULL; parent->next->next = NULL;
prepare_to_commit(remoteheads); prepare_to_commit(remoteheads);
if (commit_tree(&merge_msg, result_tree, parent, result_commit, NULL, if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parent,
sign_commit)) result_commit, NULL, sign_commit))
die(_("failed to write commit object")); die(_("failed to write commit object"));
finish(head, remoteheads, result_commit, "In-index merge"); finish(head, remoteheads, result_commit, "In-index merge");
drop_save(); drop_save();
@ -877,8 +877,8 @@ static int finish_automerge(struct commit *head,
commit_list_insert(head, &parents); commit_list_insert(head, &parents);
strbuf_addch(&merge_msg, '\n'); strbuf_addch(&merge_msg, '\n');
prepare_to_commit(remoteheads); prepare_to_commit(remoteheads);
if (commit_tree(&merge_msg, result_tree, parents, result_commit, if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
NULL, sign_commit)) result_commit, NULL, sign_commit))
die(_("failed to write commit object")); 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, remoteheads, result_commit, buf.buf); finish(head, remoteheads, result_commit, buf.buf);

View File

@ -1344,7 +1344,8 @@ void free_commit_extra_headers(struct commit_extra_header *extra)
} }
} }
int commit_tree(const struct strbuf *msg, const unsigned char *tree, int commit_tree(const char *msg, size_t msg_len,
const unsigned char *tree,
struct commit_list *parents, unsigned char *ret, struct commit_list *parents, unsigned char *ret,
const char *author, const char *sign_commit) const char *author, const char *sign_commit)
{ {
@ -1352,7 +1353,7 @@ int commit_tree(const struct strbuf *msg, const unsigned char *tree,
int result; int result;
append_merge_tag_headers(parents, &tail); append_merge_tag_headers(parents, &tail);
result = commit_tree_extended(msg, tree, parents, ret, result = commit_tree_extended(msg, msg_len, tree, parents, ret,
author, sign_commit, extra); author, sign_commit, extra);
free_commit_extra_headers(extra); free_commit_extra_headers(extra);
return result; return result;
@ -1473,7 +1474,8 @@ 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 struct strbuf *msg, const unsigned char *tree, int commit_tree_extended(const char *msg, size_t msg_len,
const unsigned char *tree,
struct commit_list *parents, unsigned char *ret, struct commit_list *parents, unsigned char *ret,
const char *author, const char *sign_commit, const char *author, const char *sign_commit,
struct commit_extra_header *extra) struct commit_extra_header *extra)
@ -1484,7 +1486,7 @@ int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
assert_sha1_type(tree, OBJ_TREE); assert_sha1_type(tree, OBJ_TREE);
if (memchr(msg->buf, '\0', msg->len)) if (memchr(msg, '\0', msg_len))
return error("a NUL byte in commit log message not allowed."); 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 */
@ -1523,7 +1525,7 @@ int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
strbuf_addch(&buffer, '\n'); strbuf_addch(&buffer, '\n');
/* And add the comment */ /* And add the comment */
strbuf_addbuf(&buffer, msg); strbuf_add(&buffer, msg, msg_len);
/* And check the encoding */ /* And check the encoding */
if (encoding_is_utf8 && !verify_utf8(&buffer)) if (encoding_is_utf8 && !verify_utf8(&buffer))

View File

@ -261,11 +261,13 @@ 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 struct strbuf *msg, const unsigned char *tree, extern int commit_tree(const char *msg, size_t msg_len,
const unsigned char *tree,
struct commit_list *parents, unsigned char *ret, struct commit_list *parents, unsigned char *ret,
const char *author, const char *sign_commit); const char *author, const char *sign_commit);
extern int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree, extern int commit_tree_extended(const char *msg, size_t msg_len,
const unsigned char *tree,
struct commit_list *parents, unsigned char *ret, struct commit_list *parents, unsigned char *ret,
const char *author, const char *sign_commit, const char *author, const char *sign_commit,
struct commit_extra_header *); struct commit_extra_header *);

View File

@ -59,7 +59,7 @@ int notes_cache_write(struct notes_cache *c)
return -1; return -1;
strbuf_attach(&msg, c->validity, strbuf_attach(&msg, c->validity,
strlen(c->validity), strlen(c->validity) + 1); strlen(c->validity), strlen(c->validity) + 1);
if (commit_tree(&msg, tree_sha1, NULL, commit_sha1, NULL, NULL) < 0) if (commit_tree(msg.buf, msg.len, tree_sha1, NULL, commit_sha1, NULL, 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

@ -644,7 +644,8 @@ 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, create_notes_commit(local_tree, parents,
o->commit_msg.buf, o->commit_msg.len,
result_sha1); result_sha1);
} }
@ -720,7 +721,8 @@ int notes_merge_commit(struct notes_merge_options *o,
} }
strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1); strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1);
create_notes_commit(partial_tree, partial_commit->parents, &sb_msg, create_notes_commit(partial_tree, partial_commit->parents,
sb_msg.buf, sb_msg.len,
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

@ -4,7 +4,8 @@
#include "notes-utils.h" #include "notes-utils.h"
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 struct strbuf *msg, unsigned char *result_sha1) const char *msg, size_t msg_len,
unsigned char *result_sha1)
{ {
unsigned char tree_sha1[20]; unsigned char tree_sha1[20];
@ -25,7 +26,7 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
/* else: t->ref points to nothing, assume root/orphan commit */ /* else: t->ref points to nothing, assume root/orphan commit */
} }
if (commit_tree(msg, tree_sha1, parents, result_sha1, NULL, NULL)) if (commit_tree(msg, msg_len, tree_sha1, parents, result_sha1, NULL, NULL))
die("Failed to commit notes tree to database"); die("Failed to commit notes tree to database");
} }
@ -46,7 +47,7 @@ void commit_notes(struct notes_tree *t, const char *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, commit_sha1); create_notes_commit(t, NULL, buf.buf, buf.len, commit_sha1);
strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */ 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);

View File

@ -15,7 +15,7 @@
* 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 struct strbuf *msg, unsigned char *result_sha1); const char *msg, size_t msg_len, unsigned char *result_sha1);
void commit_notes(struct notes_tree *t, const char *msg); void commit_notes(struct notes_tree *t, const char *msg);