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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Dec 15, 2011 at 20:47 UTC
13f8b72d8c8e24215a3dd7771592b30083f1c740
9 files changed
+20
-15
builtin/commit-tree.c
+1
-1
@@ -56,7 +56,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
56
if (strbuf_read(&buffer, 0, 0) < 0)
57
die_errno("git commit-tree: failed to read");
58
59
- if (commit_tree(buffer.buf, tree_sha1, parents, commit_sha1, NULL)) {
59
+ if (commit_tree(&buffer, tree_sha1, parents, commit_sha1, NULL)) {
60
strbuf_release(&buffer);
61
return 1;
62
}
builtin/commit.c
+1
-1
@@ -1483,7 +1483,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1483
exit(1);
1484
}
1485
1486
- if (commit_tree(sb.buf, active_cache_tree->sha1, parents, sha1,
1486
+ if (commit_tree(&sb, active_cache_tree->sha1, parents, sha1,
1487
author_ident.buf)) {
1488
rollback_index_files();
1489
die(_("failed to write commit object"));
builtin/merge.c
+2
-2
@@ -913,7 +913,7 @@ static int merge_trivial(struct commit *head)
913
parent->next->item = remoteheads->item;
914
parent->next->next = NULL;
915
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))
917
die(_("failed to write commit object"));
918
finish(head, result_commit, "In-index merge");
919
drop_save();
@@ -945,7 +945,7 @@ static int finish_automerge(struct commit *head,
945
strbuf_addch(&merge_msg, '\n');
946
prepare_to_commit();
947
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))
949
die(_("failed to write commit object"));
950
strbuf_addf(&buf, "Merge made by the '%s' strategy.", wt_strategy);
951
finish(head, result_commit, buf.buf);
builtin/notes.c
+2
-2
@@ -301,12 +301,12 @@ void commit_notes(struct notes_tree *t, const char *msg)
301
return; /* don't have to commit an unchanged tree */
302
303
/* Prepare commit message and reflog message */
304
- strbuf_addstr(&buf, "notes: "); /* commit message starts at index 7 */
304
strbuf_addstr(&buf, msg);
305
if (buf.buf[buf.len - 1] != '\n')
306
strbuf_addch(&buf, '\n'); /* Make sure msg ends with newline */
307
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 */
310
update_ref(buf.buf, t->ref, commit_sha1, NULL, 0, DIE_ON_ERR);
311
312
strbuf_release(&buf);
commit.c
+2
-2
@@ -845,7 +845,7 @@ static const char commit_utf8_warn[] =
845
"You may want to amend it after fixing the message, or set the config\n"
846
"variable i18n.commitencoding to the encoding your project uses.\n";
847
848
-int commit_tree(const char *msg, unsigned char *tree,
848
+int commit_tree(const struct strbuf *msg, unsigned char *tree,
849
struct commit_list *parents, unsigned char *ret,
850
const char *author)
851
{
@@ -884,7 +884,7 @@ int commit_tree(const char *msg, unsigned char *tree,
884
strbuf_addch(&buffer, '\n');
885
886
/* And add the comment */
887
- strbuf_addstr(&buffer, msg);
887
+ strbuf_addbuf(&buffer, msg);
888
889
/* And check the encoding */
890
if (encoding_is_utf8 && !is_utf8(buffer.buf))
commit.h
+1
-1
@@ -181,7 +181,7 @@ static inline int single_parent(struct commit *commit)
181
182
struct commit_list *reduce_heads(struct commit_list *heads);
183
184
-extern int commit_tree(const char *msg, unsigned char *tree,
184
+extern int commit_tree(const struct strbuf *msg, unsigned char *tree,
185
struct commit_list *parents, unsigned char *ret,
186
const char *author);
187
notes-cache.c
+4
-1
@@ -48,6 +48,7 @@ int notes_cache_write(struct notes_cache *c)
48
{
49
unsigned char tree_sha1[20];
50
unsigned char commit_sha1[20];
51
+ struct strbuf msg = STRBUF_INIT;
52
53
if (!c || !c->tree.initialized || !c->tree.ref || !*c->tree.ref)
54
return -1;
@@ -56,7 +57,9 @@ int notes_cache_write(struct notes_cache *c)
57
58
if (write_notes_tree(&c->tree, tree_sha1))
59
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)
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
+6
-4
@@ -530,7 +530,7 @@ static int merge_from_diffs(struct notes_merge_options *o,
530
}
531
532
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)
534
{
535
unsigned char tree_sha1[20];
536
@@ -668,7 +668,7 @@ int notes_merge(struct notes_merge_options *o,
668
struct commit_list *parents = NULL;
669
commit_list_insert(remote, &parents); /* LIFO order */
670
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,
672
result_sha1);
673
}
674
@@ -695,7 +695,8 @@ int notes_merge_commit(struct notes_merge_options *o,
695
struct dir_struct dir;
696
char *path = xstrdup(git_path(NOTES_MERGE_WORKTREE "/"));
697
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;
700
701
if (o->verbosity >= 3)
702
printf("Committing notes in notes merge worktree at %.*s\n",
@@ -733,7 +734,8 @@ int notes_merge_commit(struct notes_merge_options *o,
734
sha1_to_hex(obj_sha1), sha1_to_hex(blob_sha1));
735
}
736
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,
739
result_sha1);
740
if (o->verbosity >= 4)
741
printf("Finalized notes merge commit: %s\n",
notes-merge.h
+1
-1
@@ -37,7 +37,7 @@ void init_notes_merge_options(struct notes_merge_options *o);
37
* The resulting commit SHA1 is stored in result_sha1.
38
*/
39
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);
41
42
/*
43
* Merge notes from o->remote_ref into o->local_ref