Skip to content

Commit 3ffefb5

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bce14aa commit 3ffefb5

File tree

9 files changed

+29
-22
lines changed

9 files changed

+29
-22
lines changed

builtin/commit-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
123123
die_errno("git commit-tree: failed to read");
124124
}
125125

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)) {
128128
strbuf_release(&buffer);
129129
return 1;
130130
}

builtin/commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,8 +1659,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16591659
append_merge_tag_headers(parents, &tail);
16601660
}
16611661

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)) {
16641664
rollback_index_files();
16651665
die(_("failed to write commit object"));
16661666
}

builtin/merge.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,8 @@ static int merge_trivial(struct commit *head, struct commit_list *remoteheads)
852852
parent->next->item = remoteheads->item;
853853
parent->next->next = NULL;
854854
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))
857857
die(_("failed to write commit object"));
858858
finish(head, remoteheads, result_commit, "In-index merge");
859859
drop_save();
@@ -877,8 +877,8 @@ static int finish_automerge(struct commit *head,
877877
commit_list_insert(head, &parents);
878878
strbuf_addch(&merge_msg, '\n');
879879
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))
882882
die(_("failed to write commit object"));
883883
strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
884884
finish(head, remoteheads, result_commit, buf.buf);

commit.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,15 +1344,16 @@ void free_commit_extra_headers(struct commit_extra_header *extra)
13441344
}
13451345
}
13461346

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,
13481349
struct commit_list *parents, unsigned char *ret,
13491350
const char *author, const char *sign_commit)
13501351
{
13511352
struct commit_extra_header *extra = NULL, **tail = &extra;
13521353
int result;
13531354

13541355
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,
13561357
author, sign_commit, extra);
13571358
free_commit_extra_headers(extra);
13581359
return result;
@@ -1473,7 +1474,8 @@ static const char commit_utf8_warn[] =
14731474
"You may want to amend it after fixing the message, or set the config\n"
14741475
"variable i18n.commitencoding to the encoding your project uses.\n";
14751476

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,
14771479
struct commit_list *parents, unsigned char *ret,
14781480
const char *author, const char *sign_commit,
14791481
struct commit_extra_header *extra)
@@ -1484,7 +1486,7 @@ int commit_tree_extended(const struct strbuf *msg, const unsigned char *tree,
14841486

14851487
assert_sha1_type(tree, OBJ_TREE);
14861488

1487-
if (memchr(msg->buf, '\0', msg->len))
1489+
if (memchr(msg, '\0', msg_len))
14881490
return error("a NUL byte in commit log message not allowed.");
14891491

14901492
/* 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,
15231525
strbuf_addch(&buffer, '\n');
15241526

15251527
/* And add the comment */
1526-
strbuf_addbuf(&buffer, msg);
1528+
strbuf_add(&buffer, msg, msg_len);
15271529

15281530
/* And check the encoding */
15291531
if (encoding_is_utf8 && !verify_utf8(&buffer))

commit.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,13 @@ struct commit_extra_header {
261261
extern void append_merge_tag_headers(struct commit_list *parents,
262262
struct commit_extra_header ***tail);
263263

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,
265266
struct commit_list *parents, unsigned char *ret,
266267
const char *author, const char *sign_commit);
267268

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,
269271
struct commit_list *parents, unsigned char *ret,
270272
const char *author, const char *sign_commit,
271273
struct commit_extra_header *);

notes-cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int notes_cache_write(struct notes_cache *c)
5959
return -1;
6060
strbuf_attach(&msg, c->validity,
6161
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)
6363
return -1;
6464
if (update_ref("update notes cache", c->tree.ref, commit_sha1, NULL,
6565
0, QUIET_ON_ERR) < 0)

notes-merge.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ int notes_merge(struct notes_merge_options *o,
644644
struct commit_list *parents = NULL;
645645
commit_list_insert(remote, &parents); /* LIFO order */
646646
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,
648649
result_sha1);
649650
}
650651

@@ -720,7 +721,8 @@ int notes_merge_commit(struct notes_merge_options *o,
720721
}
721722

722723
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,
724726
result_sha1);
725727
if (o->verbosity >= 4)
726728
printf("Finalized notes merge commit: %s\n",

notes-utils.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include "notes-utils.h"
55

66
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)
89
{
910
unsigned char tree_sha1[20];
1011

@@ -25,7 +26,7 @@ void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
2526
/* else: t->ref points to nothing, assume root/orphan commit */
2627
}
2728

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))
2930
die("Failed to commit notes tree to database");
3031
}
3132

@@ -46,7 +47,7 @@ void commit_notes(struct notes_tree *t, const char *msg)
4647
if (buf.buf[buf.len - 1] != '\n')
4748
strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
4849

49-
create_notes_commit(t, NULL, &buf, commit_sha1);
50+
create_notes_commit(t, NULL, buf.buf, buf.len, commit_sha1);
5051
strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
5152
update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
5253

notes-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* The resulting commit SHA1 is stored in result_sha1.
1616
*/
1717
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);
1919

2020
void commit_notes(struct notes_tree *t, const char *msg);
2121

0 commit comments

Comments
 (0)