git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 1/4] notes: preserve object type given by "add -C"

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 11, 2012, 01:25 UTC
Message-ID
<1336699506-28388-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1336658701-9004-1-git-send-email-pclouds@gmail.com>

_Automatically_ converting a non-blob object to a blob is wrong. Either this way, or reject non-blob objects upfront.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/notes.c  |    8 +++++---
 t/t3301-notes.sh |   10 ++++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)
diff --git a/builtin/notes.c b/builtin/notes.c
index 3644d14..9840269 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -98,6 +98,7 @@ struct msg_arg {
 	int given;
 	int use_editor;
 	struct strbuf buf;
+	enum object_type type;
 };
 
 static int list_each_note(const unsigned char *object_sha1,
@@ -211,7 +212,7 @@ static void create_note(const unsigned char *object, struct msg_arg *msg,
 			sha1_to_hex(object));
 		hashclr(result);
 	} else {
-		if (write_sha1_file(msg->buf.buf, msg->buf.len, blob_type, result)) {
+		if (write_sha1_file(msg->buf.buf, msg->buf.len, typename(msg->type), result)) {
 			error(_("unable to write note object"));
 			if (path)
 				error(_("The note contents has been left in %s"),
@@ -278,6 +279,7 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)
 	free(buf);
 
 	msg->given = 1;
+	msg->type = type;
 	return 0;
 }
 
@@ -529,7 +531,7 @@ static int add(int argc, const char **argv, const char *prefix)
 	unsigned char object[20], new_note[20];
 	char logmsg[100];
 	const unsigned char *note;
-	struct msg_arg msg = { 0, 0, STRBUF_INIT };
+	struct msg_arg msg = { 0, 0, STRBUF_INIT, OBJ_BLOB };
 	struct option options[] = {
 		{ OPTION_CALLBACK, 'm', "message", &msg, "msg",
 			"note contents as a string", PARSE_OPT_NONEG,
@@ -686,7 +688,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
 	const unsigned char *note;
 	char logmsg[100];
 	const char * const *usage;
-	struct msg_arg msg = { 0, 0, STRBUF_INIT };
+	struct msg_arg msg = { 0, 0, STRBUF_INIT, OBJ_BLOB };
 	struct option options[] = {
 		{ OPTION_CALLBACK, 'm', "message", &msg, "msg",
 			"note contents as a string", PARSE_OPT_NONEG,
diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh
index 16de05a..d3fd341 100755
--- a/t/t3301-notes.sh
+++ b/t/t3301-notes.sh
@@ -1222,4 +1222,14 @@ test_expect_success 'git notes get-ref (--ref)' '
 	test "$(GIT_NOTES_REF=refs/notes/bar git notes --ref=baz get-ref)" = "refs/notes/baz"
 '
 
+test_expect_success 'add -C happily takes object of any kind' '
+	git notes add -f -C HEAD^{tree}
+'
+
+test_expect_success 'non-blobs notes are shown in human-readable form' '
+	git notes show HEAD >actual &&
+	git show `git rev-parse HEAD^{tree}` >expected &&
+	test_cmp expected actual
+'
+
 test_done
-- 
1.7.3.1.256.g2539c.dirty
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 22 of 29 in “notes: do not accept non-blobs as new notes”
  1. notes: do not accept non-blobs as new notesNguyễn Thái Ngọc Duy, May 8, 2012
  2. Jeff KingMay 8, 2012
  3. Junio C HamanoMay 8, 2012
  4. Nguyen Thai Ngoc DuyMay 9, 2012
  5. Jeff KingMay 9, 2012
  6. Junio C HamanoMay 9, 2012
  7. Jeff KingMay 9, 2012
  8. 0/4 git-notes ui fixes regarding non-blobs notesNguyễn Thái Ngọc Duy, May 10, 2012
  9. 1/4 notes: preserve object type given by "add -C"Nguyễn Thái Ngọc Duy, May 10, 2012
  10. 2/4 notes: "add -c" refuses to open an editor with non-blobsNguyễn Thái Ngọc Duy, May 10, 2012
  11. Johannes SixtMay 10, 2012
  12. Nguyen Thai Ngoc DuyMay 11, 2012
  13. 3/4 notes: refuse to edit non-blobsNguyễn Thái Ngọc Duy, May 10, 2012
  14. 4/4 notes: refuse to append to non-blob notesNguyễn Thái Ngọc Duy, May 10, 2012
  15. 4/4 notes: only append a blob to a blobNguyễn Thái Ngọc Duy, May 10, 2012
  16. Jeff KingMay 10, 2012
  17. Nguyen Thai Ngoc DuyMay 10, 2012
  18. Jeff KingMay 10, 2012
  19. Junio C HamanoMay 11, 2012
  20. Jeff KingMay 10, 2012
  21. 0/4 non-blob notes fixesNguyễn Thái Ngọc Duy, May 11, 2012
  22. 1/4 notes: preserve object type given by "add -C"Nguyễn Thái Ngọc Duy, May 11, 2012
  23. Junio C HamanoMay 11, 2012
  24. Nguyen Thai Ngoc DuyMay 12, 2012
  25. Junio C HamanoMay 12, 2012
  26. Nguyen Thai Ngoc DuyMay 12, 2012
  27. 2/4 notes: "add -c" refuses to open an editor with non-blobsNguyễn Thái Ngọc Duy, May 11, 2012
  28. 3/4 notes: refuse to edit non-blobsNguyễn Thái Ngọc Duy, May 11, 2012
  29. 4/4 notes: only allow to append a blob to a blobNguyễn Thái Ngọc Duy, May 11, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.