Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

0 notes on commit 13f8b72

Please sign in to comment.
Something went wrong with that request. Please try again.