Subject: [PATCH 4/4] notes: only append a blob to a blob

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Thu, 10 May 2012 21:43:35 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 On Thu, May 10, 2012 at 9:29 PM, Jeff King <[email-masked]> wrote:
 > It's a little odd that we rewrite the object in the "-C" case at all; we
 > should never even need to open the object. I guess it is just to make
 > the code paths between "-c" and "-C" simpler.

 Yeah. It made me look again to see if that was true, and I found that
 my last patch was flawed. Reading object content in memory in "add
 -C" is nonsense, not so much in "append -C".

 builtin/notes.c  |   18 +++++++++++++++++-
 t/t3301-notes.sh |    6 ++++++
 2 files changed, 23 insertions(+), 1 deletions(-)

diff --git a/builtin/notes.c b/builtin/notes.c
index 44fb8b6..595bcc8 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -162,6 +162,18 @@ static void write_commented_object(int fd, const unsigned char *object)
 		    sha1_to_hex(object));
 }
 
+static const char *type_name(enum object_type type)
+{
+	switch (type) {
+	case OBJ_BLOB: return _("a blob");
+	case OBJ_TAG: return _("a tag");
+	case OBJ_COMMIT: return _("a commit");
+	case OBJ_TREE: return _("a tree");
+	default:
+		die("BUG: put a new string for type %d here", type);
+	}
+}
+
 static void create_note(const unsigned char *object, struct msg_arg *msg,
 			int append_only, const unsigned char *prev,
 			unsigned char *result)
@@ -204,8 +216,12 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
 		strbuf_grow(&(msg->buf), size + 1);
 		if (msg->buf.len && prev_buf && size)
 			strbuf_insert(&(msg->buf), 0, "\n", 1);
-		if (prev_buf && size)
+		if (prev_buf && size) {
+			if (type != OBJ_BLOB || msg->type != OBJ_BLOB)
+				die(_("cannot append %s to %s"),
+				    type_name(type), type_name(msg->type));
 			strbuf_insert(&(msg->buf), 0, prev_buf, size);
+		}
 		free(prev_buf);
 	}
 
diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh
index 9104bf0..9b17e56 100755
--- a/t/t3301-notes.sh
+++ b/t/t3301-notes.sh
@@ -1240,4 +1240,10 @@ test_expect_success 'cannot edit non-blob notes' '
 	EDITOR=cat test_must_fail git notes edit
 '
 
+test_expect_success 'refuse to concatenate two notes of different type' '
+	EDITOR=cat test_must_fail git notes append -m foo &&
+	git notes add -f -m foo &&
+	EDITOR=cat test_must_fail git notes append -C HEAD^{tree}
+'
+
 test_done
-- 
1.7.8.36.g69ee2