Skip to content

Commit 13f8b72

Browse files
pcloudsgitster
authored andcommitted
Convert commit_tree() to take strbuf as message
There wan't a way for commit_tree() to notice if the message the caller prepared contained a NUL byte, as it did not take the length of the message as a parameter. Use a pointer to a strbuf instead, so that we can either choose to allow low-level plumbing commands to make commits that contain NUL byte in its message, or forbid NUL everywhere by adding the check in commit_tree(), in later patches. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6b3c4c0 commit 13f8b72

File tree

9 files changed

+20
-15
lines changed

9 files changed

+20
-15
lines changed

builtin/commit-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
5656
if (strbuf_read(&buffer, 0, 0) < 0)
5757
die_errno("git commit-tree: failed to read");
5858

59-
if (commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) {
59+
if (commit_tree(&buffer, tree_sha1, parents, commit_sha1, NULL)) {
6060
strbuf_release(&buffer);
6161
return 1;
6262
}

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1483,7 +1483,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14831483
exit(1);
14841484
}
14851485

1486-
if (commit_tree(sb.buf, active_cache_tree->sha1, parents, sha1,
1486+
if (commit_tree(&sb, active_cache_tree->sha1, parents, sha1,
14871487
author_ident.buf)) {
14881488
rollback_index_files();
14891489
die(_("failed to write commit object"));

builtin/merge.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ static int merge_trivial(struct commit *head)
913913
parent->next->item = remoteheads->item;
914914
parent->next->next = NULL;
915915
prepare_to_commit();
916-
if (commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL))
916+
if (commit_tree(&merge_msg, result_tree, parent, result_commit, NULL))
917917
die(_("failed to write commit object"));
918918
finish(head, result_commit, "In-index merge");
919919
drop_save();
@@ -945,7 +945,7 @@ static int finish_automerge(struct commit *head,
945945
strbuf_addch(&merge_msg, '\n');
946946
prepare_to_commit();
947947
free_commit_list(remoteheads);
948-
if (commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL))
948+
if (commit_tree(&merge_msg, result_tree, parents, result_commit, NULL))
949949
die(_("failed to write commit object"));
950950
strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
951951
finish(head, result_commit, buf.buf);

builtin/notes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,12 +301,12 @@ void commit_notes(struct notes_tree *t, const char *msg)
301301
return; /* don't have to commit an unchanged tree */
302302

303303
/* Prepare commit message and reflog message */
304-
strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
305304
strbuf_addstr(&buf, msg);
306305
if (buf.buf[buf.len - 1] != '\n')
307306
strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
308307

309-
create_notes_commit(t, NULL, buf.buf + 7, commit_sha1);
308+
create_notes_commit(t, NULL, &buf, commit_sha1);
309+
strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
310310
update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
311311

312312
strbuf_release(&buf);

commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ static const char commit_utf8_warn[] =
845845
"You may want to amend it after fixing the message, or set the config\n"
846846
"variable i18n.commitencoding to the encoding your project uses.\n";
847847

848-
int commit_tree(const char *msg, unsigned char *tree,
848+
int commit_tree(const struct strbuf *msg, unsigned char *tree,
849849
struct commit_list *parents, unsigned char *ret,
850850
const char *author)
851851
{
@@ -884,7 +884,7 @@ int commit_tree(const char *msg, unsigned char *tree,
884884
strbuf_addch(&buffer, '\n');
885885

886886
/* And add the comment */
887-
strbuf_addstr(&buffer, msg);
887+
strbuf_addbuf(&buffer, msg);
888888

889889
/* And check the encoding */
890890
if (encoding_is_utf8 && !is_utf8(buffer.buf))

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ static inline int single_parent(struct commit *commit)
181181

182182
struct commit_list *reduce_heads(struct commit_list *heads);
183183

184-
extern int commit_tree(const char *msg, unsigned char *tree,
184+
extern int commit_tree(const struct strbuf *msg, unsigned char *tree,
185185
struct commit_list *parents, unsigned char *ret,
186186
const char *author);
187187

notes-cache.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ int notes_cache_write(struct notes_cache *c)
4848
{
4949
unsigned char tree_sha1[20];
5050
unsigned char commit_sha1[20];
51+
struct strbuf msg = STRBUF_INIT;
5152

5253
if (!c || !c->tree.initialized || !c->tree.ref || !*c->tree.ref)
5354
return -1;
@@ -56,7 +57,9 @@ int notes_cache_write(struct notes_cache *c)
5657

5758
if (write_notes_tree(&c->tree, tree_sha1))
5859
return -1;
59-
if (commit_tree(c->validity, tree_sha1, NULL, commit_sha1, NULL) < 0)
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) < 0)
6063
return -1;
6164
if (update_ref("update notes cache", c->tree.ref, commit_sha1, NULL,
6265
0, QUIET_ON_ERR) < 0)

notes-merge.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ static int merge_from_diffs(struct notes_merge_options *o,
530530
}
531531

532532
void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
533-
const char *msg, unsigned char *result_sha1)
533+
const struct strbuf *msg, unsigned char *result_sha1)
534534
{
535535
unsigned char tree_sha1[20];
536536

@@ -668,7 +668,7 @@ int notes_merge(struct notes_merge_options *o,
668668
struct commit_list *parents = NULL;
669669
commit_list_insert(remote, &parents); /* LIFO order */
670670
commit_list_insert(local, &parents);
671-
create_notes_commit(local_tree, parents, o->commit_msg.buf,
671+
create_notes_commit(local_tree, parents, &o->commit_msg,
672672
result_sha1);
673673
}
674674

@@ -695,7 +695,8 @@ int notes_merge_commit(struct notes_merge_options *o,
695695
struct dir_struct dir;
696696
char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/"));
697697
int path_len = strlen(path), i;
698-
const char *msg = strstr(partial_commit->buffer, "\n\n");
698+
char *msg = strstr(partial_commit->buffer, "\n\n");
699+
struct strbuf sb_msg = STRBUF_INIT;
699700

700701
if (o->verbosity >= 3)
701702
printf("Committing notes in notes merge worktree at %.*s\n",
@@ -733,7 +734,8 @@ int notes_merge_commit(struct notes_merge_options *o,
733734
sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1));
734735
}
735736

736-
create_notes_commit(partial_tree, partial_commit->parents, msg,
737+
strbuf_attach(&sb_msg, msg, strlen(msg), strlen(msg) + 1);
738+
create_notes_commit(partial_tree, partial_commit->parents, &sb_msg,
737739
result_sha1);
738740
if (o->verbosity >= 4)
739741
printf("Finalized notes merge commit: %s\n",

notes-merge.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void init_notes_merge_options(struct notes_merge_options *o);
3737
* The resulting commit SHA1 is stored in result_sha1.
3838
*/
3939
void create_notes_commit(struct notes_tree *t, struct commit_list *parents,
40-
const char *msg, unsigned char *result_sha1);
40+
const struct strbuf *msg, unsigned char *result_sha1);
4141

4242
/*
4343
* Merge notes from o->remote_ref into o->local_ref

0 commit comments

Comments
 (0)