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>

Jeff King committed Jun 10, 2014 at 17:36 UTC 3ffefb54c0515308ceafb6ba071567d9fd379498
9 files changed +29 -22
builtin/commit-tree.c
+2 -2
@@ -123,8 +123,8 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
123 die_errno("git commit-tree: failed to read");
124 }
125
126 - if (commit_tree(&buffer, tree_sha1, parents, commit_sha1,
127 - NULL, sign_commit)) {
126 + if (commit_tree(buffer.buf, buffer.len, tree_sha1, parents,
127 + commit_sha1, NULL, sign_commit)) {
128 strbuf_release(&buffer);
129 return 1;
130 }
builtin/commit.c
+2 -2
@@ -1659,8 +1659,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1659 append_merge_tag_headers(parents, &tail);
1660 }
1661
1662 - if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1,
1663 - author_ident.buf, sign_commit, extra)) {
1662 + if (commit_tree_extended(sb.buf, sb.len, active_cache_tree->sha1,
1663 + parents, sha1, author_ident.buf, sign_commit, extra)) {
1664 rollback_index_files();
1665 die(_("failed to write commit object"));
1666 }
builtin/merge.c
+4 -4
@@ -852,8 +852,8 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
852 parent->next->item = remoteheads->item;
853 parent->next->next = NULL;
854 prepare_to_commit(remoteheads);
855 - if (commit_tree(&merge_msg, result_tree, parent, result_commit, NULL,
856 - sign_commit))
855 + if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parent,
856 + result_commit, NULL, sign_commit))
857 die(_("failed to write commit object"));
858 finish(head, remoteheads, result_commit, "In-index merge");
859 drop_save();
@@ -877,8 +877,8 @@ static int finish_automerge(struct commit *head,
877 commit_list_insert(head, &parents);
878 strbuf_addch(&merge_msg, '\n');
879 prepare_to_commit(remoteheads);
880 - if (commit_tree(&merge_msg, result_tree, parents, result_commit,
881 - NULL, sign_commit))
880 + if (commit_tree(merge_msg.buf, merge_msg.len, result_tree, parents,
881 + result_commit, NULL, sign_commit))
882 die(_("failed to write commit object"));
883 strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
884 finish(head, remoteheads, result_commit, buf.buf);
commit.c
+7 -5
@@ -1344,7 +1344,8 @@ void free_commit_extra_headers(struct commit_extra_header *extra)
1344 }
1345 }
1346
1347 -int commit_tree(const struct strbuf *msg, const unsigned char *tree,
1347 +int commit_tree(const char *msg, size_t msg_len,
1348 + const unsigned char *tree,
1349 struct commit_list *parents, unsigned char *ret,
1350 const char *author, const char *sign_commit)
1351 {
@@ -1352,7 +1353,7 @@ int commit_tree(const struct strbuf *msg, const unsigned char *tree,
1353 int result;
1354
1355 append_merge_tag_headers(parents, &tail);
1355 - result = commit_tree_extended(msg, tree, parents, ret,
1356 + result = commit_tree_extended(msg, msg_len, tree, parents, ret,
1357 author, sign_commit, extra);
1358 free_commit_extra_headers(extra);
1359 return result;
@@ -1473,7 +1474,8 @@ static const char commit_utf8_warn[] =
1474 "You may want to amend it after fixing the message, or set the config\n"
1475 "variable i18n.commitencoding to the encoding your project uses.\n";
1476
1476 -int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
1477 +int commit_tree_extended(const char *msg, size_t msg_len,
1478 + const unsigned char *tree,
1479 struct commit_list *parents, unsigned char *ret,
1480 const char *author, const char *sign_commit,
1481 struct commit_extra_header *extra)
@@ -1484,7 +1486,7 @@ int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
1486
1487 assert_sha1_type(tree, OBJ_TREE);
1488
1487 - if (memchr(msg->buf, '\0', msg->len))
1489 + if (memchr(msg, '\0', msg_len))
1490 return error("a NUL byte in commit log message not allowed.");
1491
1492 /* 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,
1525 strbuf_addch(&buffer, '\n');
1526
1527 /* And add the comment */
1526 - strbuf_addbuf(&buffer, msg);
1528 + strbuf_add(&buffer, msg, msg_len);
1529
1530 /* And check the encoding */
1531 if (encoding_is_utf8 && !verify_utf8(&buffer))
commit.h
+4 -2
@@ -261,11 +261,13 @@ struct commit_extra_header {
261 extern void append_merge_tag_headers(struct commit_list *parents,
262 struct commit_extra_header ***tail);
263
264 -extern int commit_tree(const struct strbuf *msg, const unsigned char *tree,
264 +extern int commit_tree(const char *msg, size_t msg_len,
265 + const unsigned char *tree,
266 struct commit_list *parents, unsigned char *ret,
267 const char *author, const char *sign_commit);
268
268 -extern int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
269 +extern int commit_tree_extended(const char *msg, size_t msg_len,
270 + const unsigned char *tree,
271 struct commit_list *parents, unsigned char *ret,
272 const char *author, const char *sign_commit,
273 struct commit_extra_header *);
notes-cache.c
+1 -1
@@ -59,7 +59,7 @@ int notes_cache_write(struct notes_cache *c)
59 return -1;
60 strbuf_attach(&msg, c->validity,
61 strlen(c->validity), strlen(c->validity) + 1);
62 - if (commit_tree(&msg, tree_sha1, NULL, commit_sha1, NULL, NULL) < 0)
62 + if (commit_tree(msg.buf, msg.len, tree_sha1, NULL, commit_sha1, NULL, NULL) < 0)
63 return -1;
64 if (update_ref("update notes cache", c->tree.ref, commit_sha1, NULL,
65 0, QUIET_ON_ERR) < 0)
notes-merge.c
+4 -2
@@ -644,7 +644,8 @@ int notes_merge(struct notes_merge_options *o,
644 struct commit_list *parents = NULL;
645 commit_list_insert(remote, &parents); /* LIFO order */
646 commit_list_insert(local, &parents);
647 - create_notes_commit(local_tree, parents, &o->commit_msg,
647 + create_notes_commit(local_tree, parents,
648 + o->commit_msg.buf, o->commit_msg.len,
649 result_sha1);
650 }
651
@@ -720,7 +721,8 @@ int notes_merge_commit(struct notes_merge_options *o,
721 }
722
723 strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1);
723 - create_notes_commit(partial_tree, partial_commit->parents, &sb_msg,
724 + create_notes_commit(partial_tree, partial_commit->parents,
725 + sb_msg.buf, sb_msg.len,
726 result_sha1);
727 if (o->verbosity >= 4)
728 printf("Finalized notes merge commit: %s\n",
notes-utils.c
+4 -3
@@ -4,7 +4,8 @@
4 #include "notes-utils.h"
5
6 void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
7 - const struct strbuf *msg, unsigned char *result_sha1)
7 + const char *msg, size_t msg_len,
8 + unsigned char *result_sha1)
9 {
10 unsigned char tree_sha1[20];
11
@@ -25,7 +26,7 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
26 /* else: t->ref points to nothing, assume root/orphan commit */
27 }
28
28 - if (commit_tree(msg, tree_sha1, parents, result_sha1, NULL, NULL))
29 + if (commit_tree(msg, msg_len, tree_sha1, parents, result_sha1, NULL, NULL))
30 die("Failed to commit notes tree to database");
31 }
32
@@ -46,7 +47,7 @@ void commit_notes(struct notes_tree *t, const char *msg)
47 if (buf.buf[buf.len - 1] != '\n')
48 strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
49
49 - create_notes_commit(t, NULL, &buf, commit_sha1);
50 + create_notes_commit(t, NULL, buf.buf, buf.len, commit_sha1);
51 strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
52 update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
53
notes-utils.h
+1 -1
@@ -15,7 +15,7 @@
15 * The resulting commit SHA1 is stored in result_sha1.
16 */
17 void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
18 - const struct strbuf *msg, unsigned char *result_sha1);
18 + const char *msg, size_t msg_len, unsigned char *result_sha1);
19
20 void commit_notes(struct notes_tree *t, const char *msg);
21