{"thread":{"id":"22262","subject":"[PATCHv11 00/20] git notes","startedAt":"2010-01-17T21:04:17Z","lastAt":"2010-01-27T12:02:17Z","messageCount":26,"participants":["Johan Herland","Stephen Boyd","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"131968","messageId":"1263762277-31419-1-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":null,"subject":"[PATCHv11 00/20] git notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:17Z","receivedAt":"2010-01-17T21:04:17Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Hi,\n\nHere is the 11th iteration of the git-notes series. Changes in this\niteration are as follows:\n\nChanges to existing patches:\n\n- Rebased onto a8b59ef... (Add more testcases to test fast-import of notes)\n  which has now been merged to 'next', which now incorporates the initial 3\n  of the 11 patches from the previous iteration.\n\n- Patch #1 (was #4) has received minor additional non-functional fixes.\n\n- Patch #4 (was #6) has received two fixes to init_notes():\n  - Fail loudly if get_tree_entry() fails on the latest notes commit.\n  - Don't ignore the core.notesRef config variable.\n\n- Patch #8 (was #9) revamps for_each_note():\n  - Add a 'flags' parameter for adjusting the notes tree traversal.\n  - Make sure too-deep fanout levels are consolidated.\n  - Minor change to calculation of optimal fanout.\n\n- Patch #10 (was #10) is adjusted to handle the addition of remove_note()\n  and write_notes_tree() (patches #6 and #9).\n\n- Patch #11 (was #11) has received some fixes:\n  - Use blob_type from blob.h instead of \"blob\".\n\n\nNew patches:\n\n- Patch #3 adds tests to make sure $GIT_NOTES_REF and core.notesRef\n  are handled correctly.\n\n- Patch #6 adds remove_note() for removing notes from notes tree.\n\n- Patch #9 adds write_notes_tree() for saving the notes tree to tree\n  objects on disk.\n\n- Patch #12 is the builtin-ification of the \"git notes\" command.\n\n- Patches #13 and #14 adds tests for aspects of the notes code that was\n  not easily testable before the builtin-ification of \"git notes\".\n\n- Patch #15 teaches the notes code to properly preserve non-note entries\n  in the notes tree across a read/update/write-cycle.\n\n- Patches #16 - #18 handles deletion of notes from the notes tree,\n  including a test for verifying that repeatedly removing notes\n  eventually consolidates the notes tree structure.\n\n- Patches #19 and #20 adds gc_notes() for garbage-collecting notes that\n  annotate non-existing commits, and teaches the \"--notes\" option to\n  \"git gc\".\n\n\nTODO:\n- Handle note objects that are not blobs, but trees\n(- Rewrite fast-import notes code to use new notes API with non-note support)\n\n\nHave fun! :)\n\n...Johan\n\n\nJohan Herland (20):\n  Minor non-functional fixes to notes.c\n  Notes API: get_commit_notes() -> format_note() + remove the commit restriction\n  Add tests for checking correct handling of $GIT_NOTES_REF and core.notesRef\n  Notes API: init_notes(): Initialize the notes tree from the given notes ref\n  Notes API: add_note(): Add note objects to the internal notes tree structure\n  Notes API: remove_note(): Remove note objects from the notes tree structure\n  Notes API: get_note(): Return the note annotating the given object\n  Notes API: for_each_note(): Traverse the entire notes tree with a callback\n  Notes API: write_notes_tree(): Store the notes tree in the database\n  Notes API: Allow multiple concurrent notes trees with new struct notes_tree\n  Refactor notes concatenation into a flexible interface for combining notes\n  Builtin-ify git-notes\n  t3301: Verify successful annotation of non-commits\n  t3305: Verify that adding many notes with git-notes triggers increased fanout\n  Teach notes code to properly preserve non-notes in the notes tree\n  Teach builtin-notes to remove empty notes\n  builtin-notes: Teach -d option for deleting existing notes\n  t3305: Verify that removing notes triggers automatic fanout consolidation\n  Notes API: gc_notes(): Prune notes that belong to non-existing objects\n  builtin-gc: Teach the new --notes option to garbage-collect notes\n\n Documentation/git-gc.txt                      |    5 +-\n Documentation/git-notes.txt                   |   19 +-\n Makefile                                      |    2 +-\n builtin-gc.c                                  |   13 +\n builtin-notes.c                               |  251 ++++++++\n builtin.h                                     |    3 +\n git-notes.sh => contrib/examples/git-notes.sh |    0\n git.c                                         |    1 +\n notes.c                                       |  841 +++++++++++++++++++++----\n notes.h                                       |  192 ++++++-\n pretty.c                                      |    9 +-\n t/t3301-notes.sh                              |  189 +++++-\n t/t3303-notes-subtrees.sh                     |   28 +-\n t/t3304-notes-mixed.sh                        |   36 +-\n t/t3305-notes-fanout.sh                       |   95 +++\n t/t3306-notes-gc.sh                           |  106 ++++\n 16 files changed, 1622 insertions(+), 168 deletions(-)\n create mode 100644 builtin-notes.c\n rename git-notes.sh => contrib/examples/git-notes.sh (100%)\n create mode 100755 t/t3305-notes-fanout.sh\n create mode 100755 t/t3306-notes-gc.sh\n"},{"id":"131976","messageId":"1263762277-31419-2-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 01/20] Minor non-functional fixes to notes.c","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:18Z","receivedAt":"2010-01-17T21:04:18Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Signed-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |    9 ++++-----\n 1 files changed, 4 insertions(+), 5 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 023adce..47e38a1 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -1,7 +1,6 @@\n #include \"cache.h\"\n #include \"commit.h\"\n #include \"notes.h\"\n-#include \"refs.h\"\n #include \"utf8.h\"\n #include \"strbuf.h\"\n #include \"tree-walk.h\"\n@@ -93,7 +92,7 @@ static void **note_tree_search(struct int_node **tree,\n \n \ti = GET_NIBBLE(*n, key_sha1);\n \tp = (*tree)->a[i];\n-\tswitch(GET_PTR_TYPE(p)) {\n+\tswitch (GET_PTR_TYPE(p)) {\n \tcase PTR_TYPE_INTERNAL:\n \t\t*tree = CLR_PTR_TYPE(p);\n \t\t(*n)++;\n@@ -195,7 +194,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,\n \n \tassert(GET_PTR_TYPE(entry) == 0); /* no type bits set */\n \tl = (struct leaf_node *) CLR_PTR_TYPE(*p);\n-\tswitch(GET_PTR_TYPE(*p)) {\n+\tswitch (GET_PTR_TYPE(*p)) {\n \tcase PTR_TYPE_NULL:\n \t\tassert(!*p);\n \t\t*p = SET_PTR_TYPE(entry, type);\n@@ -257,7 +256,7 @@ static void note_tree_free(struct int_node *tree)\n \tunsigned int i;\n \tfor (i = 0; i < 16; i++) {\n \t\tvoid *p = tree->a[i];\n-\t\tswitch(GET_PTR_TYPE(p)) {\n+\t\tswitch (GET_PTR_TYPE(p)) {\n \t\tcase PTR_TYPE_INTERNAL:\n \t\t\tnote_tree_free(CLR_PTR_TYPE(p));\n \t\t\t/* fall through */\n@@ -274,7 +273,7 @@ static void note_tree_free(struct int_node *tree)\n  * - hex_len  - Length of above segment. Must be multiple of 2 between 0 and 40\n  * - sha1     - Partial SHA1 value is written here\n  * - sha1_len - Max #bytes to store in sha1, Must be >= hex_len / 2, and < 20\n- * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format).\n+ * Returns -1 on error (invalid arguments or invalid SHA1 (not in hex format)).\n  * Otherwise, returns number of bytes written to sha1 (i.e. hex_len / 2).\n  * Pads sha1 with NULs up to sha1_len (not included in returned length).\n  */\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131975","messageId":"1263762277-31419-3-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 02/20] Notes API: get_commit_notes() -> format_note() + remove the commit restriction","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:19Z","receivedAt":"2010-01-17T21:04:19Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"There is really no reason why only commit objects can be annotated. By\nchanging the struct commit parameter to get_commit_notes() into a sha1 we\ngain the ability to annotate any object type. To reflect this in the function\nnaming as well, we rename get_commit_notes() to format_note().\n\nThis patch also fixes comments and variable names throughout notes.c as a\nconsequence of the removal of the unnecessary 'commit' restriction.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c  |   33 ++++++++++++++++-----------------\n notes.h  |   11 ++++++++++-\n pretty.c |    8 ++++----\n 3 files changed, 30 insertions(+), 22 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 47e38a1..4ee4fec 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -1,5 +1,4 @@\n #include \"cache.h\"\n-#include \"commit.h\"\n #include \"notes.h\"\n #include \"utf8.h\"\n #include \"strbuf.h\"\n@@ -24,10 +23,10 @@ struct int_node {\n /*\n  * Leaf nodes come in two variants, note entries and subtree entries,\n  * distinguished by the LSb of the leaf node pointer (see above).\n- * As a note entry, the key is the SHA1 of the referenced commit, and the\n+ * As a note entry, the key is the SHA1 of the referenced object, and the\n  * value is the SHA1 of the note object.\n  * As a subtree entry, the key is the prefix SHA1 (w/trailing NULs) of the\n- * referenced commit, using the last byte of the key to store the length of\n+ * referenced object, using the last byte of the key to store the length of\n  * the prefix. The value is the SHA1 of the tree object containing the notes\n  * subtree.\n  */\n@@ -210,7 +209,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,\n \t\t\t\tif (concatenate_notes(l->val_sha1,\n \t\t\t\t\t\tentry->val_sha1))\n \t\t\t\t\tdie(\"failed to concatenate note %s \"\n-\t\t\t\t\t    \"into note %s for commit %s\",\n+\t\t\t\t\t    \"into note %s for object %s\",\n \t\t\t\t\t    sha1_to_hex(entry->val_sha1),\n \t\t\t\t\t    sha1_to_hex(l->val_sha1),\n \t\t\t\t\t    sha1_to_hex(l->key_sha1));\n@@ -298,7 +297,7 @@ static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,\n static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n \t\tunsigned int n)\n {\n-\tunsigned char commit_sha1[20];\n+\tunsigned char object_sha1[20];\n \tunsigned int prefix_len;\n \tvoid *buf;\n \tstruct tree_desc desc;\n@@ -311,23 +310,23 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n \n \tprefix_len = subtree->key_sha1[19];\n \tassert(prefix_len * 2 >= n);\n-\tmemcpy(commit_sha1, subtree->key_sha1, prefix_len);\n+\tmemcpy(object_sha1, subtree->key_sha1, prefix_len);\n \twhile (tree_entry(&desc, &entry)) {\n \t\tint len = get_sha1_hex_segment(entry.path, strlen(entry.path),\n-\t\t\t\tcommit_sha1 + prefix_len, 20 - prefix_len);\n+\t\t\t\tobject_sha1 + prefix_len, 20 - prefix_len);\n \t\tif (len < 0)\n \t\t\tcontinue; /* entry.path is not a SHA1 sum. Skip */\n \t\tlen += prefix_len;\n \n \t\t/*\n-\t\t * If commit SHA1 is complete (len == 20), assume note object\n-\t\t * If commit SHA1 is incomplete (len < 20), assume note subtree\n+\t\t * If object SHA1 is complete (len == 20), assume note object\n+\t\t * If object SHA1 is incomplete (len < 20), assume note subtree\n \t\t */\n \t\tif (len <= 20) {\n \t\t\tunsigned char type = PTR_TYPE_NOTE;\n \t\t\tstruct leaf_node *l = (struct leaf_node *)\n \t\t\t\txcalloc(sizeof(struct leaf_node), 1);\n-\t\t\thashcpy(l->key_sha1, commit_sha1);\n+\t\t\thashcpy(l->key_sha1, object_sha1);\n \t\t\thashcpy(l->val_sha1, entry.sha1);\n \t\t\tif (len < 20) {\n \t\t\t\tif (!S_ISDIR(entry.mode))\n@@ -343,12 +342,12 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n \n static void initialize_notes(const char *notes_ref_name)\n {\n-\tunsigned char sha1[20], commit_sha1[20];\n+\tunsigned char sha1[20], object_sha1[20];\n \tunsigned mode;\n \tstruct leaf_node root_tree;\n \n-\tif (!notes_ref_name || read_ref(notes_ref_name, commit_sha1) ||\n-\t    get_tree_entry(commit_sha1, \"\", sha1, &mode))\n+\tif (!notes_ref_name || read_ref(notes_ref_name, object_sha1) ||\n+\t    get_tree_entry(object_sha1, \"\", sha1, &mode))\n \t\treturn;\n \n \thashclr(root_tree.key_sha1);\n@@ -356,9 +355,9 @@ static void initialize_notes(const char *notes_ref_name)\n \tload_subtree(&root_tree, &root_node, 0);\n }\n \n-static unsigned char *lookup_notes(const unsigned char *commit_sha1)\n+static unsigned char *lookup_notes(const unsigned char *object_sha1)\n {\n-\tstruct leaf_node *found = note_tree_find(&root_node, 0, commit_sha1);\n+\tstruct leaf_node *found = note_tree_find(&root_node, 0, object_sha1);\n \tif (found)\n \t\treturn found->val_sha1;\n \treturn NULL;\n@@ -371,7 +370,7 @@ void free_notes(void)\n \tinitialized = 0;\n }\n \n-void get_commit_notes(const struct commit *commit, struct strbuf *sb,\n+void format_note(const unsigned char *object_sha1, struct strbuf *sb,\n \t\tconst char *output_encoding, int flags)\n {\n \tstatic const char utf8[] = \"utf-8\";\n@@ -390,7 +389,7 @@ void get_commit_notes(const struct commit *commit, struct strbuf *sb,\n \t\tinitialized = 1;\n \t}\n \n-\tsha1 = lookup_notes(commit->object.sha1);\n+\tsha1 = lookup_notes(object_sha1);\n \tif (!sha1)\n \t\treturn;\n \ndiff --git a/notes.h b/notes.h\nindex a1421e3..d745ed1 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -4,10 +4,19 @@\n /* Free (and de-initialize) the internal notes tree structure */\n void free_notes(void);\n \n+/* Flags controlling how notes are formatted */\n #define NOTES_SHOW_HEADER 1\n #define NOTES_INDENT 2\n \n-void get_commit_notes(const struct commit *commit, struct strbuf *sb,\n+/*\n+ * Fill the given strbuf with the notes associated with the given object.\n+ *\n+ * If the internal notes structure is not initialized, it will be auto-\n+ * initialized to the default value (see documentation for init_notes() above).\n+ *\n+ * 'flags' is a bitwise combination of the above formatting flags.\n+ */\n+void format_note(const unsigned char *object_sha1, struct strbuf *sb,\n \t\tconst char *output_encoding, int flags);\n \n #endif\ndiff --git a/pretty.c b/pretty.c\nindex 8f5bd1a..fe77090 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -775,8 +775,8 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,\n \t\t}\n \t\treturn 0;\t/* unknown %g placeholder */\n \tcase 'N':\n-\t\tget_commit_notes(commit, sb, git_log_output_encoding ?\n-\t\t\t     git_log_output_encoding : git_commit_encoding, 0);\n+\t\tformat_note(commit->object.sha1, sb, git_log_output_encoding ?\n+\t\t\t    git_log_output_encoding : git_commit_encoding, 0);\n \t\treturn 1;\n \t}\n \n@@ -1095,8 +1095,8 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,\n \t\tstrbuf_addch(sb, '\\n');\n \n \tif (fmt != CMIT_FMT_ONELINE)\n-\t\tget_commit_notes(commit, sb, encoding,\n-\t\t\t\t NOTES_SHOW_HEADER | NOTES_INDENT);\n+\t\tformat_note(commit->object.sha1, sb, encoding,\n+\t\t\t    NOTES_SHOW_HEADER | NOTES_INDENT);\n \n \tfree(reencoded);\n }\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131970","messageId":"1263762277-31419-4-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 03/20] Add tests for checking correct handling of $GIT_NOTES_REF and core.notesRef","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:20Z","receivedAt":"2010-01-17T21:04:20Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Signed-off-by: Johan Herland <johan@herland.net>\n---\n t/t3301-notes.sh |   48 ++++++++++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 48 insertions(+), 0 deletions(-)\n\ndiff --git a/t/t3301-notes.sh b/t/t3301-notes.sh\nindex 1e34f48..3985d2a 100755\n--- a/t/t3301-notes.sh\n+++ b/t/t3301-notes.sh\n@@ -147,4 +147,52 @@ test_expect_success 'show -m and -F notes' '\n \ttest_cmp expect-m-and-F output\n '\n \n+test_expect_success 'create other note on a different notes ref (setup)' '\n+\t: > a5 &&\n+\tgit add a5 &&\n+\ttest_tick &&\n+\tgit commit -m 5th &&\n+\tGIT_NOTES_REF=\"refs/notes/other\" git notes edit -m \"other note\"\n+'\n+\n+cat > expect-other << EOF\n+commit bd1753200303d0a0344be813e504253b3d98e74d\n+Author: A U Thor <author@example.com>\n+Date:   Thu Apr 7 15:17:13 2005 -0700\n+\n+    5th\n+\n+Notes:\n+    other note\n+EOF\n+\n+cat > expect-not-other << EOF\n+commit bd1753200303d0a0344be813e504253b3d98e74d\n+Author: A U Thor <author@example.com>\n+Date:   Thu Apr 7 15:17:13 2005 -0700\n+\n+    5th\n+EOF\n+\n+test_expect_success 'Do not show note on other ref by default' '\n+\tgit log -1 > output &&\n+\ttest_cmp expect-not-other output\n+'\n+\n+test_expect_success 'Do show note when ref is given in GIT_NOTES_REF' '\n+\tGIT_NOTES_REF=\"refs/notes/other\" git log -1 > output &&\n+\ttest_cmp expect-other output\n+'\n+\n+test_expect_success 'Do show note when ref is given in core.notesRef config' '\n+\tgit config core.notesRef \"refs/notes/other\" &&\n+\tgit log -1 > output &&\n+\ttest_cmp expect-other output\n+'\n+\n+test_expect_success 'Do not show note when core.notesRef is overridden' '\n+\tGIT_NOTES_REF=\"refs/notes/wrong\" git log -1 > output &&\n+\ttest_cmp expect-not-other output\n+'\n+\n test_done\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131969","messageId":"1263762277-31419-5-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 04/20] Notes API: init_notes(): Initialize the notes tree from the given notes ref","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:21Z","receivedAt":"2010-01-17T21:04:21Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Created by a simple refactoring of initialize_notes().\n\nAlso add a new 'flags' parameter, which is a bitwise combination of notes\ninitialization flags. For now, there is only one flag - NOTES_INIT_EMPTY -\nwhich indicates that the notes tree should not auto-load the contents of\nthe given (or default) notes ref, but rather should leave the notes tree\ninitialized to an empty state. This will become useful in the future when\nmanipulating the notes tree through the notes API.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |   30 ++++++++++++++++++------------\n notes.h |   20 ++++++++++++++++++++\n 2 files changed, 38 insertions(+), 12 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 4ee4fec..3f4ae35 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -340,15 +340,28 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n \tfree(buf);\n }\n \n-static void initialize_notes(const char *notes_ref_name)\n+void init_notes(const char *notes_ref, int flags)\n {\n \tunsigned char sha1[20], object_sha1[20];\n \tunsigned mode;\n \tstruct leaf_node root_tree;\n \n-\tif (!notes_ref_name || read_ref(notes_ref_name, object_sha1) ||\n-\t    get_tree_entry(object_sha1, \"\", sha1, &mode))\n+\tassert(!initialized);\n+\tinitialized = 1;\n+\n+\tif (!notes_ref)\n+\t\tnotes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);\n+\tif (!notes_ref)\n+\t\tnotes_ref = notes_ref_name; /* value of core.notesRef config */\n+\tif (!notes_ref)\n+\t\tnotes_ref = GIT_NOTES_DEFAULT_REF;\n+\n+\tif (flags & NOTES_INIT_EMPTY || !notes_ref ||\n+\t    read_ref(notes_ref, object_sha1))\n \t\treturn;\n+\tif (get_tree_entry(object_sha1, \"\", sha1, &mode))\n+\t\tdie(\"Failed to read notes tree referenced by %s (%s)\",\n+\t\t    notes_ref, object_sha1);\n \n \thashclr(root_tree.key_sha1);\n \thashcpy(root_tree.val_sha1, sha1);\n@@ -379,15 +392,8 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb,\n \tunsigned long linelen, msglen;\n \tenum object_type type;\n \n-\tif (!initialized) {\n-\t\tconst char *env = getenv(GIT_NOTES_REF_ENVIRONMENT);\n-\t\tif (env)\n-\t\t\tnotes_ref_name = getenv(GIT_NOTES_REF_ENVIRONMENT);\n-\t\telse if (!notes_ref_name)\n-\t\t\tnotes_ref_name = GIT_NOTES_DEFAULT_REF;\n-\t\tinitialize_notes(notes_ref_name);\n-\t\tinitialized = 1;\n-\t}\n+\tif (!initialized)\n+\t\tinit_notes(NULL, 0);\n \n \tsha1 = lookup_notes(object_sha1);\n \tif (!sha1)\ndiff --git a/notes.h b/notes.h\nindex d745ed1..6b52799 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -1,6 +1,26 @@\n #ifndef NOTES_H\n #define NOTES_H\n \n+/*\n+ * Flags controlling behaviour of notes tree initialization\n+ *\n+ * Default behaviour is to initialize the notes tree from the tree object\n+ * specified by the given (or default) notes ref.\n+ */\n+#define NOTES_INIT_EMPTY 1\n+\n+/*\n+ * Initialize internal notes tree structure with the notes tree at the given\n+ * ref. If given ref is NULL, the value of the $GIT_NOTES_REF environment\n+ * variable is used, and if that is missing, the default notes ref is used\n+ * (\"refs/notes/commits\").\n+ *\n+ * If you need to re-intialize the internal notes tree structure (e.g. loading\n+ * from a different notes ref), please first de-initialize the current notes\n+ * tree by calling free_notes().\n+ */\n+void init_notes(const char *notes_ref, int flags);\n+\n /* Free (and de-initialize) the internal notes tree structure */\n void free_notes(void);\n \n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131988","messageId":"1263762277-31419-6-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 05/20] Notes API: add_note(): Add note objects to the internal notes tree structure","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:22Z","receivedAt":"2010-01-17T21:04:22Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Signed-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |   11 +++++++++++\n notes.h |    4 ++++\n 2 files changed, 15 insertions(+), 0 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 3f4ae35..2c0d14e 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -368,6 +368,17 @@ void init_notes(const char *notes_ref, int flags)\n \tload_subtree(&root_tree, &root_node, 0);\n }\n \n+void add_note(const unsigned char *object_sha1, const unsigned char *note_sha1)\n+{\n+\tstruct leaf_node *l;\n+\n+\tassert(initialized);\n+\tl = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));\n+\thashcpy(l->key_sha1, object_sha1);\n+\thashcpy(l->val_sha1, note_sha1);\n+\tnote_tree_insert(&root_node, 0, l, PTR_TYPE_NOTE);\n+}\n+\n static unsigned char *lookup_notes(const unsigned char *object_sha1)\n {\n \tstruct leaf_node *found = note_tree_find(&root_node, 0, object_sha1);\ndiff --git a/notes.h b/notes.h\nindex 6b52799..5f22852 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -21,6 +21,10 @@\n  */\n void init_notes(const char *notes_ref, int flags);\n \n+/* Add the given note object to the internal notes tree structure */\n+void add_note(const unsigned char *object_sha1,\n+\t\tconst unsigned char *note_sha1);\n+\n /* Free (and de-initialize) the internal notes tree structure */\n void free_notes(void);\n \n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131984","messageId":"1263762277-31419-7-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 06/20] Notes API: remove_note(): Remove note objects from the notes tree structure","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:23Z","receivedAt":"2010-01-17T21:04:23Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"This includes adding internal functions for maintaining a healthy notes tree\nstructure after removing individual notes.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |   85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-\n notes.h |    3 ++\n 2 files changed, 87 insertions(+), 1 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 2c0d14e..2e82d71 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -44,7 +44,7 @@ struct leaf_node {\n #define CLR_PTR_TYPE(ptr)       ((void *) ((uintptr_t) (ptr) & ~3))\n #define SET_PTR_TYPE(ptr, type) ((void *) ((uintptr_t) (ptr) | (type)))\n \n-#define GET_NIBBLE(n, sha1) (((sha1[n >> 1]) >> ((~n & 0x01) << 2)) & 0x0f)\n+#define GET_NIBBLE(n, sha1) (((sha1[(n) >> 1]) >> ((~(n) & 0x01) << 2)) & 0x0f)\n \n #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \\\n \t(memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))\n@@ -249,6 +249,79 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,\n \tnote_tree_insert(new_node, n + 1, entry, type);\n }\n \n+/*\n+ * How to consolidate an int_node:\n+ * If there are > 1 non-NULL entries, give up and return non-zero.\n+ * Otherwise replace the int_node at the given index in the given parent node\n+ * with the only entry (or a NULL entry if no entries) from the given tree,\n+ * and return 0.\n+ */\n+static int note_tree_consolidate(struct int_node *tree,\n+\tstruct int_node *parent, unsigned char index)\n+{\n+\tunsigned int i;\n+\tvoid *p = NULL;\n+\n+\tassert(tree && parent);\n+\tassert(CLR_PTR_TYPE(parent->a[index]) == tree);\n+\n+\tfor (i = 0; i < 16; i++) {\n+\t\tif (GET_PTR_TYPE(tree->a[i]) != PTR_TYPE_NULL) {\n+\t\t\tif (p) /* more than one entry */\n+\t\t\t\treturn -2;\n+\t\t\tp = tree->a[i];\n+\t\t}\n+\t}\n+\n+\t/* replace tree with p in parent[index] */\n+\tparent->a[index] = p;\n+\tfree(tree);\n+\treturn 0;\n+}\n+\n+/*\n+ * To remove a leaf_node:\n+ * Search to the tree location appropriate for the given leaf_node's key:\n+ * - If location does not hold a matching entry, abort and do nothing.\n+ * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).\n+ * - Consolidate int_nodes repeatedly, while walking up the tree towards root.\n+ */\n+static void note_tree_remove(struct int_node *tree, unsigned char n,\n+\t\tstruct leaf_node *entry)\n+{\n+\tstruct leaf_node *l;\n+\tstruct int_node *parent_stack[20];\n+\tunsigned char i, j;\n+\tvoid **p = note_tree_search(&tree, &n, entry->key_sha1);\n+\n+\tassert(GET_PTR_TYPE(entry) == 0); /* no type bits set */\n+\tif (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)\n+\t\treturn; /* type mismatch, nothing to remove */\n+\tl = (struct leaf_node *) CLR_PTR_TYPE(*p);\n+\tif (hashcmp(l->key_sha1, entry->key_sha1))\n+\t\treturn; /* key mismatch, nothing to remove */\n+\n+\t/* we have found a matching entry */\n+\tfree(l);\n+\t*p = SET_PTR_TYPE(NULL, PTR_TYPE_NULL);\n+\n+\t/* consolidate this tree level, and parent levels, if possible */\n+\tif (!n)\n+\t\treturn; /* cannot consolidate top level */\n+\t/* first, build stack of ancestors between root and current node */\n+\tparent_stack[0] = &root_node;\n+\tfor (i = 0; i < n; i++) {\n+\t\tj = GET_NIBBLE(i, entry->key_sha1);\n+\t\tparent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);\n+\t}\n+\tassert(i == n && parent_stack[i] == tree);\n+\t/* next, unwind stack until note_tree_consolidate() is done */\n+\twhile (i > 0 &&\n+\t       !note_tree_consolidate(parent_stack[i], parent_stack[i - 1],\n+\t\t\t\t      GET_NIBBLE(i - 1, entry->key_sha1)))\n+\t\ti--;\n+}\n+\n /* Free the entire notes data contained in the given tree */\n static void note_tree_free(struct int_node *tree)\n {\n@@ -379,6 +452,16 @@ void add_note(const unsigned char *object_sha1, const unsigned char *note_sha1)\n \tnote_tree_insert(&root_node, 0, l, PTR_TYPE_NOTE);\n }\n \n+void remove_note(const unsigned char *object_sha1)\n+{\n+\tstruct leaf_node l;\n+\n+\tassert(initialized);\n+\thashcpy(l.key_sha1, object_sha1);\n+\thashclr(l.val_sha1);\n+\treturn note_tree_remove(&root_node, 0, &l);\n+}\n+\n static unsigned char *lookup_notes(const unsigned char *object_sha1)\n {\n \tstruct leaf_node *found = note_tree_find(&root_node, 0, object_sha1);\ndiff --git a/notes.h b/notes.h\nindex 5f22852..9e66855 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -25,6 +25,9 @@ void init_notes(const char *notes_ref, int flags);\n void add_note(const unsigned char *object_sha1,\n \t\tconst unsigned char *note_sha1);\n \n+/* Remove the given note object from the internal notes tree structure */\n+void remove_note(const unsigned char *object_sha1);\n+\n /* Free (and de-initialize) the internal notes tree structure */\n void free_notes(void);\n \n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131972","messageId":"1263762277-31419-8-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 07/20] Notes API: get_note(): Return the note annotating the given object","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:24Z","receivedAt":"2010-01-17T21:04:24Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Created by a simple cleanup and rename of lookup_notes().\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |   15 ++++++++-------\n notes.h |    3 +++\n 2 files changed, 11 insertions(+), 7 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 2e82d71..a0a85b4 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -462,12 +462,13 @@ void remove_note(const unsigned char *object_sha1)\n \treturn note_tree_remove(&root_node, 0, &l);\n }\n \n-static unsigned char *lookup_notes(const unsigned char *object_sha1)\n+const unsigned char *get_note(const unsigned char *object_sha1)\n {\n-\tstruct leaf_node *found = note_tree_find(&root_node, 0, object_sha1);\n-\tif (found)\n-\t\treturn found->val_sha1;\n-\treturn NULL;\n+\tstruct leaf_node *found;\n+\n+\tassert(initialized);\n+\tfound = note_tree_find(&root_node, 0, object_sha1);\n+\treturn found ? found->val_sha1 : NULL;\n }\n \n void free_notes(void)\n@@ -481,7 +482,7 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb,\n \t\tconst char *output_encoding, int flags)\n {\n \tstatic const char utf8[] = \"utf-8\";\n-\tunsigned char *sha1;\n+\tconst unsigned char *sha1;\n \tchar *msg, *msg_p;\n \tunsigned long linelen, msglen;\n \tenum object_type type;\n@@ -489,7 +490,7 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb,\n \tif (!initialized)\n \t\tinit_notes(NULL, 0);\n \n-\tsha1 = lookup_notes(object_sha1);\n+\tsha1 = get_note(object_sha1);\n \tif (!sha1)\n \t\treturn;\n \ndiff --git a/notes.h b/notes.h\nindex 9e66855..c0714f4 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -28,6 +28,9 @@ void add_note(const unsigned char *object_sha1,\n /* Remove the given note object from the internal notes tree structure */\n void remove_note(const unsigned char *object_sha1);\n \n+/* Get the note object SHA1 containing the note data for the given object */\n+const unsigned char *get_note(const unsigned char *object_sha1);\n+\n /* Free (and de-initialize) the internal notes tree structure */\n void free_notes(void);\n \n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131977","messageId":"1263762277-31419-9-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 08/20] Notes API: for_each_note(): Traverse the entire notes tree with a callback","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:25Z","receivedAt":"2010-01-17T21:04:25Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"This includes a first attempt at creating an optimal fanout scheme (which\nis calculated on-the-fly, while traversing).\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |  133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n notes.h |   47 ++++++++++++++++++++++\n 2 files changed, 180 insertions(+), 0 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex a0a85b4..eabd6f3 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -413,6 +413,133 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n \tfree(buf);\n }\n \n+/*\n+ * Determine optimal on-disk fanout for this part of the notes tree\n+ *\n+ * Given a (sub)tree and the level in the internal tree structure, determine\n+ * whether or not the given existing fanout should be expanded for this\n+ * (sub)tree.\n+ *\n+ * Values of the 'fanout' variable:\n+ * - 0: No fanout (all notes are stored directly in the root notes tree)\n+ * - 1: 2/38 fanout\n+ * - 2: 2/2/36 fanout\n+ * - 3: 2/2/2/34 fanout\n+ * etc.\n+ */\n+static unsigned char determine_fanout(struct int_node *tree, unsigned char n,\n+\t\tunsigned char fanout)\n+{\n+\t/*\n+\t * The following is a simple heuristic that works well in practice:\n+\t * For each even-numbered 16-tree level (remember that each on-disk\n+\t * fanout level corresponds to _two_ 16-tree levels), peek at all 16\n+\t * entries at that tree level. If all of them are either int_nodes or\n+\t * subtree entries, then there are likely plenty of notes below this\n+\t * level, so we return an incremented fanout.\n+\t */\n+\tunsigned int i;\n+\tif ((n % 2) || (n > 2 * fanout))\n+\t\treturn fanout;\n+\tfor (i = 0; i < 16; i++) {\n+\t\tswitch (GET_PTR_TYPE(tree->a[i])) {\n+\t\tcase PTR_TYPE_SUBTREE:\n+\t\tcase PTR_TYPE_INTERNAL:\n+\t\t\tcontinue;\n+\t\tdefault:\n+\t\t\treturn fanout;\n+\t\t}\n+\t}\n+\treturn fanout + 1;\n+}\n+\n+static void construct_path_with_fanout(const unsigned char *sha1,\n+\t\tunsigned char fanout, char *path)\n+{\n+\tunsigned int i = 0, j = 0;\n+\tconst char *hex_sha1 = sha1_to_hex(sha1);\n+\tassert(fanout < 20);\n+\twhile (fanout) {\n+\t\tpath[i++] = hex_sha1[j++];\n+\t\tpath[i++] = hex_sha1[j++];\n+\t\tpath[i++] = '/';\n+\t\tfanout--;\n+\t}\n+\tstrcpy(path + i, hex_sha1 + j);\n+}\n+\n+static int for_each_note_helper(struct int_node *tree, unsigned char n,\n+\t\tunsigned char fanout, int flags, each_note_fn fn,\n+\t\tvoid *cb_data)\n+{\n+\tunsigned int i;\n+\tvoid *p;\n+\tint ret = 0;\n+\tstruct leaf_node *l;\n+\tstatic char path[40 + 19 + 1];  /* hex SHA1 + 19 * '/' + NUL */\n+\n+\tfanout = determine_fanout(tree, n, fanout);\n+\tfor (i = 0; i < 16; i++) {\n+redo:\n+\t\tp = tree->a[i];\n+\t\tswitch (GET_PTR_TYPE(p)) {\n+\t\tcase PTR_TYPE_INTERNAL:\n+\t\t\t/* recurse into int_node */\n+\t\t\tret = for_each_note_helper(CLR_PTR_TYPE(p), n + 1,\n+\t\t\t\tfanout, flags, fn, cb_data);\n+\t\t\tbreak;\n+\t\tcase PTR_TYPE_SUBTREE:\n+\t\t\tl = (struct leaf_node *) CLR_PTR_TYPE(p);\n+\t\t\t/*\n+\t\t\t * Subtree entries in the note tree represent parts of\n+\t\t\t * the note tree that have not yet been explored. There\n+\t\t\t * is a direct relationship between subtree entries at\n+\t\t\t * level 'n' in the tree, and the 'fanout' variable:\n+\t\t\t * Subtree entries at level 'n <= 2 * fanout' should be\n+\t\t\t * preserved, since they correspond exactly to a fanout\n+\t\t\t * directory in the on-disk structure. However, subtree\n+\t\t\t * entries at level 'n > 2 * fanout' should NOT be\n+\t\t\t * preserved, but rather consolidated into the above\n+\t\t\t * notes tree level. We achieve this by unconditionally\n+\t\t\t * unpacking subtree entries that exist below the\n+\t\t\t * threshold level at 'n = 2 * fanout'.\n+\t\t\t */\n+\t\t\tif (n <= 2 * fanout &&\n+\t\t\t    flags & FOR_EACH_NOTE_YIELD_SUBTREES) {\n+\t\t\t\t/* invoke callback with subtree */\n+\t\t\t\tunsigned int path_len =\n+\t\t\t\t\tl->key_sha1[19] * 2 + fanout;\n+\t\t\t\tassert(path_len < 40 + 19);\n+\t\t\t\tconstruct_path_with_fanout(l->key_sha1, fanout,\n+\t\t\t\t\t\t\t   path);\n+\t\t\t\t/* Create trailing slash, if needed */\n+\t\t\t\tif (path[path_len - 1] != '/')\n+\t\t\t\t\tpath[path_len++] = '/';\n+\t\t\t\tpath[path_len] = '\\0';\n+\t\t\t\tret = fn(l->key_sha1, l->val_sha1, path,\n+\t\t\t\t\t cb_data);\n+\t\t\t}\n+\t\t\tif (n > fanout * 2 ||\n+\t\t\t    !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {\n+\t\t\t\t/* unpack subtree and resume traversal */\n+\t\t\t\ttree->a[i] = NULL;\n+\t\t\t\tload_subtree(l, tree, n);\n+\t\t\t\tfree(l);\n+\t\t\t\tgoto redo;\n+\t\t\t}\n+\t\t\tbreak;\n+\t\tcase PTR_TYPE_NOTE:\n+\t\t\tl = (struct leaf_node *) CLR_PTR_TYPE(p);\n+\t\t\tconstruct_path_with_fanout(l->key_sha1, fanout, path);\n+\t\t\tret = fn(l->key_sha1, l->val_sha1, path, cb_data);\n+\t\t\tbreak;\n+\t\t}\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t}\n+\treturn 0;\n+}\n+\n void init_notes(const char *notes_ref, int flags)\n {\n \tunsigned char sha1[20], object_sha1[20];\n@@ -471,6 +598,12 @@ const unsigned char *get_note(const unsigned char *object_sha1)\n \treturn found ? found->val_sha1 : NULL;\n }\n \n+int for_each_note(int flags, each_note_fn fn, void *cb_data)\n+{\n+\tassert(initialized);\n+\treturn for_each_note_helper(&root_node, 0, 0, flags, fn, cb_data);\n+}\n+\n void free_notes(void)\n {\n \tnote_tree_free(&root_node);\ndiff --git a/notes.h b/notes.h\nindex c0714f4..c319fd8 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -31,6 +31,53 @@ void remove_note(const unsigned char *object_sha1);\n /* Get the note object SHA1 containing the note data for the given object */\n const unsigned char *get_note(const unsigned char *object_sha1);\n \n+/*\n+ * Flags controlling behaviour of for_each_note()\n+ *\n+ * Default behaviour of for_each_note() is to traverse every single note object\n+ * in the notes tree, unpacking subtree entries along the way.\n+ * The following flags can be used to alter the default behaviour:\n+ *\n+ * - DONT_UNPACK_SUBTREES causes for_each_note() NOT to unpack and recurse into\n+ *   subtree entries while traversing the notes tree. This causes notes within\n+ *   those subtrees NOT to be passed to the callback. Use this flag if you\n+ *   don't want to traverse _all_ notes, but only want to traverse the parts\n+ *   of the notes tree that have already been unpacked (this includes at least\n+ *   all notes that have been added/changed).\n+ *\n+ * - YIELD_SUBTREES causes any subtree entries that are encountered to be\n+ *   passed to the callback, before recursing into them. Subtree entries are\n+ *   not note objects, but represent intermediate directories in the notes\n+ *   tree. When passed to the callback, subtree entries will have a trailing\n+ *   slash in their path, which the callback may use to differentiate between\n+ *   note entries and subtree entries. Note that already-unpacked subtree\n+ *   entries are not part of the notes tree, and will therefore not be yielded.\n+ *   If this flag is used together with DONT_UNPACK_SUBTREES, for_each_note()\n+ *   will yield the subtree entry, but not recurse into it.\n+ */\n+#define FOR_EACH_NOTE_DONT_UNPACK_SUBTREES 1\n+#define FOR_EACH_NOTE_YIELD_SUBTREES 2\n+\n+/*\n+ * Invoke the specified callback function for each note\n+ *\n+ * If the callback returns nonzero, the note walk is aborted, and the return\n+ * value from the callback is returned from for_each_note(). Hence, a zero\n+ * return value from for_each_note() indicates that all notes were walked\n+ * successfully.\n+ *\n+ * IMPORTANT: The callback function is NOT allowed to change the notes tree.\n+ * In other words, the following functions can NOT be invoked (on the current\n+ * notes tree) from within the callback:\n+ * - add_note()\n+ * - remove_note()\n+ * - free_notes()\n+ */\n+typedef int each_note_fn(const unsigned char *object_sha1,\n+\t\tconst unsigned char *note_sha1, char *note_path,\n+\t\tvoid *cb_data);\n+int for_each_note(int flags, each_note_fn fn, void *cb_data);\n+\n /* Free (and de-initialize) the internal notes tree structure */\n void free_notes(void);\n \n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131979","messageId":"1263762277-31419-10-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 09/20] Notes API: write_notes_tree(): Store the notes tree in the database","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:26Z","receivedAt":"2010-01-17T21:04:26Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Uses for_each_note() to traverse the notes tree, and produces tree\nobjects on the fly representing the \"on-disk\" version of the notes\ntree with appropriate fanout.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |  143 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n notes.h |   38 +++++++++++++++-\n 2 files changed, 178 insertions(+), 3 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex eabd6f3..159e2c6 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -1,5 +1,6 @@\n #include \"cache.h\"\n #include \"notes.h\"\n+#include \"tree.h\"\n #include \"utf8.h\"\n #include \"strbuf.h\"\n #include \"tree-walk.h\"\n@@ -540,6 +541,124 @@ redo:\n \treturn 0;\n }\n \n+struct tree_write_stack {\n+\tstruct tree_write_stack *next;\n+\tstruct strbuf buf;\n+\tchar path[2]; /* path to subtree in next, if any */\n+};\n+\n+static inline int matches_tree_write_stack(struct tree_write_stack *tws,\n+\t\tconst char *full_path)\n+{\n+\treturn  full_path[0] == tws->path[0] &&\n+\t\tfull_path[1] == tws->path[1] &&\n+\t\tfull_path[2] == '/';\n+}\n+\n+static void write_tree_entry(struct strbuf *buf, unsigned int mode,\n+\t\tconst char *path, unsigned int path_len, const\n+\t\tunsigned char *sha1)\n+{\n+\t\tstrbuf_addf(buf, \"%06o %.*s%c\", mode, path_len, path, '\\0');\n+\t\tstrbuf_add(buf, sha1, 20);\n+}\n+\n+static void tree_write_stack_init_subtree(struct tree_write_stack *tws,\n+\t\tconst char *path) {\n+\tstruct tree_write_stack *n;\n+\tassert(!tws->next);\n+\tassert(tws->path[0] == '\\0' && tws->path[1] == '\\0');\n+\tn = (struct tree_write_stack *)\n+\t\txmalloc(sizeof(struct tree_write_stack));\n+\tn->next = NULL;\n+\tstrbuf_init(&n->buf, 256 * (32 + 40)); /* assume 256 entries per tree */\n+\tn->path[0] = n->path[1] = '\\0';\n+\ttws->next = n;\n+\ttws->path[0] = path[0];\n+\ttws->path[1] = path[1];\n+}\n+\n+static int tree_write_stack_finish_subtree(struct tree_write_stack *tws) {\n+\tint ret;\n+\tstruct tree_write_stack *n = tws->next;\n+\tunsigned char s[20];\n+\tif (n) {\n+\t\tret = tree_write_stack_finish_subtree(n);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t\tret = write_sha1_file(n->buf.buf, n->buf.len, tree_type, s);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t\tstrbuf_release(&n->buf);\n+\t\tfree(n);\n+\t\ttws->next = NULL;\n+\t\twrite_tree_entry(&tws->buf, 040000, tws->path, 2, s);\n+\t\ttws->path[0] = tws->path[1] = '\\0';\n+\t}\n+\treturn 0;\n+}\n+\n+static int write_each_note_helper(struct tree_write_stack *tws,\n+\t\tconst char *path, unsigned int mode,\n+\t\tconst unsigned char *sha1)\n+{\n+\tsize_t path_len = strlen(path);\n+\tunsigned int n = 0;\n+\tint ret;\n+\n+\t/* Determine common part of tree write stack */\n+\twhile (tws && 3 * n < path_len &&\n+\t       matches_tree_write_stack(tws, path + 3 * n)) {\n+\t\tn++;\n+\t\ttws = tws->next;\n+\t}\n+\n+\t/* tws point to last matching tree_write_stack entry */\n+\tret = tree_write_stack_finish_subtree(tws);\n+\tif (ret)\n+\t\treturn ret;\n+\n+\t/* Start subtrees needed to satisfy path */\n+\twhile (3 * n + 2 < path_len && path[3 * n + 2] == '/') {\n+\t\ttree_write_stack_init_subtree(tws, path + 3 * n);\n+\t\tn++;\n+\t\ttws = tws->next;\n+\t}\n+\n+\t/* There should be no more directory components in the given path */\n+\tassert(memchr(path + 3 * n, '/', path_len - (3 * n)) == NULL);\n+\n+\t/* Finally add given entry to the current tree object */\n+\twrite_tree_entry(&tws->buf, mode, path + 3 * n, path_len - (3 * n),\n+\t\t\t sha1);\n+\n+\treturn 0;\n+}\n+\n+struct write_each_note_data {\n+\tstruct tree_write_stack *root;\n+};\n+\n+static int write_each_note(const unsigned char *object_sha1,\n+\t\tconst unsigned char *note_sha1, char *note_path,\n+\t\tvoid *cb_data)\n+{\n+\tstruct write_each_note_data *d =\n+\t\t(struct write_each_note_data *) cb_data;\n+\tsize_t note_path_len = strlen(note_path);\n+\tunsigned int mode = 0100644;\n+\n+\tif (note_path[note_path_len - 1] == '/') {\n+\t\t/* subtree entry */\n+\t\tnote_path_len--;\n+\t\tnote_path[note_path_len] = '\\0';\n+\t\tmode = 040000;\n+\t}\n+\tassert(note_path_len <= 40 + 19);\n+\n+\treturn write_each_note_helper(d->root, note_path, mode, note_sha1);\n+}\n+\n void init_notes(const char *notes_ref, int flags)\n {\n \tunsigned char sha1[20], object_sha1[20];\n@@ -604,6 +723,30 @@ int for_each_note(int flags, each_note_fn fn, void *cb_data)\n \treturn for_each_note_helper(&root_node, 0, 0, flags, fn, cb_data);\n }\n \n+int write_notes_tree(unsigned char *result)\n+{\n+\tstruct tree_write_stack root;\n+\tstruct write_each_note_data cb_data;\n+\tint ret;\n+\n+\tassert(initialized);\n+\n+\t/* Prepare for traversal of current notes tree */\n+\troot.next = NULL; /* last forward entry in list is grounded */\n+\tstrbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */\n+\troot.path[0] = root.path[1] = '\\0';\n+\tcb_data.root = &root;\n+\n+\t/* Write tree objects representing current notes tree */\n+\tret = for_each_note(FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |\n+\t\t\t\tFOR_EACH_NOTE_YIELD_SUBTREES,\n+\t\t\twrite_each_note, &cb_data) ||\n+\t\ttree_write_stack_finish_subtree(&root) ||\n+\t\twrite_sha1_file(root.buf.buf, root.buf.len, tree_type, result);\n+\tstrbuf_release(&root.buf);\n+\treturn ret;\n+}\n+\n void free_notes(void)\n {\n \tnote_tree_free(&root_node);\ndiff --git a/notes.h b/notes.h\nindex c319fd8..ebc301c 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -21,11 +21,23 @@\n  */\n void init_notes(const char *notes_ref, int flags);\n \n-/* Add the given note object to the internal notes tree structure */\n+/*\n+ * Add the given note object to the internal notes tree structure\n+ *\n+ * IMPORTANT: The changes made by add_note() to the internal notes tree structure\n+ * are not persistent until a subsequent call to write_notes_tree() returns\n+ * zero.\n+ */\n void add_note(const unsigned char *object_sha1,\n \t\tconst unsigned char *note_sha1);\n \n-/* Remove the given note object from the internal notes tree structure */\n+/*\n+ * Remove the given note object from the internal notes tree structure\n+ *\n+ * IMPORTANT: The changes made by remove_note() to the internal notes tree\n+ * structure are not persistent until a subsequent call to write_notes_tree()\n+ * returns zero.\n+ */\n void remove_note(const unsigned char *object_sha1);\n \n /* Get the note object SHA1 containing the note data for the given object */\n@@ -78,7 +90,27 @@ typedef int each_note_fn(const unsigned char *object_sha1,\n \t\tvoid *cb_data);\n int for_each_note(int flags, each_note_fn fn, void *cb_data);\n \n-/* Free (and de-initialize) the internal notes tree structure */\n+/*\n+ * Write the internal notes tree structure to the object database\n+ *\n+ * Creates a new tree object encapsulating the current state of the\n+ * internal notes tree, and stores its SHA1 into the 'result' argument.\n+ *\n+ * Returns zero on success, non-zero on failure.\n+ *\n+ * IMPORTANT: Changes made to the internal notes tree structure are not\n+ * persistent until this function has returned zero. Please also remember\n+ * to create a corresponding commit object, and update the appropriate\n+ * notes ref.\n+ */\n+int write_notes_tree(unsigned char *result);\n+\n+/*\n+ * Free (and de-initialize) the internal notes tree structure\n+ *\n+ * IMPORTANT: Changes made to the notes tree since the last, successful\n+ * call to write_notes_tree() will be lost.\n+ */\n void free_notes(void);\n \n /* Flags controlling how notes are formatted */\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131971","messageId":"1263762277-31419-11-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 10/20] Notes API: Allow multiple concurrent notes trees with new struct notes_tree","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:27Z","receivedAt":"2010-01-17T21:04:27Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"The new struct notes_tree encapsulates access to a specific notes tree.\nIt is provided to allow callers to make use of several different notes trees\nsimultaneously.\n\nA struct notes_tree * parameter is added to every function in the notes API.\nIn all cases, NULL can be passed, in which case the fallback \"default\" notes\ntree (default_notes_tree) is used.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c  |   90 ++++++++++++++++++++++++++++++++++++++-----------------------\n notes.h  |   81 +++++++++++++++++++++++++++++++++++--------------------\n pretty.c |    7 +++--\n 3 files changed, 112 insertions(+), 66 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 159e2c6..1cac4ef 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -50,9 +50,7 @@ struct leaf_node {\n #define SUBTREE_SHA1_PREFIXCMP(key_sha1, subtree_sha1) \\\n \t(memcmp(key_sha1, subtree_sha1, subtree_sha1[19]))\n \n-static struct int_node root_node;\n-\n-static int initialized;\n+struct notes_tree default_notes_tree;\n \n static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n \t\tunsigned int n);\n@@ -287,8 +285,8 @@ static int note_tree_consolidate(struct int_node *tree,\n  * - Replace the matching leaf_node with a NULL entry (and free the leaf_node).\n  * - Consolidate int_nodes repeatedly, while walking up the tree towards root.\n  */\n-static void note_tree_remove(struct int_node *tree, unsigned char n,\n-\t\tstruct leaf_node *entry)\n+static void note_tree_remove(struct notes_tree *t, struct int_node *tree,\n+\t\tunsigned char n, struct leaf_node *entry)\n {\n \tstruct leaf_node *l;\n \tstruct int_node *parent_stack[20];\n@@ -310,7 +308,7 @@ static void note_tree_remove(struct int_node *tree, unsigned char n,\n \tif (!n)\n \t\treturn; /* cannot consolidate top level */\n \t/* first, build stack of ancestors between root and current node */\n-\tparent_stack[0] = &root_node;\n+\tparent_stack[0] = t->root;\n \tfor (i = 0; i < n; i++) {\n \t\tj = GET_NIBBLE(i, entry->key_sha1);\n \t\tparent_stack[i + 1] = CLR_PTR_TYPE(parent_stack[i]->a[j]);\n@@ -659,14 +657,15 @@ static int write_each_note(const unsigned char *object_sha1,\n \treturn write_each_note_helper(d->root, note_path, mode, note_sha1);\n }\n \n-void init_notes(const char *notes_ref, int flags)\n+void init_notes(struct notes_tree *t, const char *notes_ref, int flags)\n {\n \tunsigned char sha1[20], object_sha1[20];\n \tunsigned mode;\n \tstruct leaf_node root_tree;\n \n-\tassert(!initialized);\n-\tinitialized = 1;\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tassert(!t->initialized);\n \n \tif (!notes_ref)\n \t\tnotes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);\n@@ -675,6 +674,10 @@ void init_notes(const char *notes_ref, int flags)\n \tif (!notes_ref)\n \t\tnotes_ref = GIT_NOTES_DEFAULT_REF;\n \n+\tt->root = (struct int_node *) xcalloc(sizeof(struct int_node), 1);\n+\tt->ref = notes_ref ? xstrdup(notes_ref) : NULL;\n+\tt->initialized = 1;\n+\n \tif (flags & NOTES_INIT_EMPTY || !notes_ref ||\n \t    read_ref(notes_ref, object_sha1))\n \t\treturn;\n@@ -684,52 +687,65 @@ void init_notes(const char *notes_ref, int flags)\n \n \thashclr(root_tree.key_sha1);\n \thashcpy(root_tree.val_sha1, sha1);\n-\tload_subtree(&root_tree, &root_node, 0);\n+\tload_subtree(&root_tree, t->root, 0);\n }\n \n-void add_note(const unsigned char *object_sha1, const unsigned char *note_sha1)\n+void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n+\t\tconst unsigned char *note_sha1)\n {\n \tstruct leaf_node *l;\n \n-\tassert(initialized);\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tassert(t->initialized);\n \tl = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));\n \thashcpy(l->key_sha1, object_sha1);\n \thashcpy(l->val_sha1, note_sha1);\n-\tnote_tree_insert(&root_node, 0, l, PTR_TYPE_NOTE);\n+\tnote_tree_insert(t->root, 0, l, PTR_TYPE_NOTE);\n }\n \n-void remove_note(const unsigned char *object_sha1)\n+void remove_note(struct notes_tree *t, const unsigned char *object_sha1)\n {\n \tstruct leaf_node l;\n \n-\tassert(initialized);\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tassert(t->initialized);\n \thashcpy(l.key_sha1, object_sha1);\n \thashclr(l.val_sha1);\n-\treturn note_tree_remove(&root_node, 0, &l);\n+\treturn note_tree_remove(t, t->root, 0, &l);\n }\n \n-const unsigned char *get_note(const unsigned char *object_sha1)\n+const unsigned char *get_note(struct notes_tree *t,\n+\t\tconst unsigned char *object_sha1)\n {\n \tstruct leaf_node *found;\n \n-\tassert(initialized);\n-\tfound = note_tree_find(&root_node, 0, object_sha1);\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tassert(t->initialized);\n+\tfound = note_tree_find(t->root, 0, object_sha1);\n \treturn found ? found->val_sha1 : NULL;\n }\n \n-int for_each_note(int flags, each_note_fn fn, void *cb_data)\n+int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,\n+\t\tvoid *cb_data)\n {\n-\tassert(initialized);\n-\treturn for_each_note_helper(&root_node, 0, 0, flags, fn, cb_data);\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tassert(t->initialized);\n+\treturn for_each_note_helper(t->root, 0, 0, flags, fn, cb_data);\n }\n \n-int write_notes_tree(unsigned char *result)\n+int write_notes_tree(struct notes_tree *t, unsigned char *result)\n {\n \tstruct tree_write_stack root;\n \tstruct write_each_note_data cb_data;\n \tint ret;\n \n-\tassert(initialized);\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tassert(t->initialized);\n \n \t/* Prepare for traversal of current notes tree */\n \troot.next = NULL; /* last forward entry in list is grounded */\n@@ -738,7 +754,7 @@ int write_notes_tree(unsigned char *result)\n \tcb_data.root = &root;\n \n \t/* Write tree objects representing current notes tree */\n-\tret = for_each_note(FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |\n+\tret = for_each_note(t, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |\n \t\t\t\tFOR_EACH_NOTE_YIELD_SUBTREES,\n \t\t\twrite_each_note, &cb_data) ||\n \t\ttree_write_stack_finish_subtree(&root) ||\n@@ -747,15 +763,19 @@ int write_notes_tree(unsigned char *result)\n \treturn ret;\n }\n \n-void free_notes(void)\n+void free_notes(struct notes_tree *t)\n {\n-\tnote_tree_free(&root_node);\n-\tmemset(&root_node, 0, sizeof(struct int_node));\n-\tinitialized = 0;\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tif (t->root)\n+\t\tnote_tree_free(t->root);\n+\tfree(t->root);\n+\tfree(t->ref);\n+\tmemset(t, 0, sizeof(struct notes_tree));\n }\n \n-void format_note(const unsigned char *object_sha1, struct strbuf *sb,\n-\t\tconst char *output_encoding, int flags)\n+void format_note(struct notes_tree *t, const unsigned char *object_sha1,\n+\t\tstruct strbuf *sb, const char *output_encoding, int flags)\n {\n \tstatic const char utf8[] = \"utf-8\";\n \tconst unsigned char *sha1;\n@@ -763,10 +783,12 @@ void format_note(const unsigned char *object_sha1, struct strbuf *sb,\n \tunsigned long linelen, msglen;\n \tenum object_type type;\n \n-\tif (!initialized)\n-\t\tinit_notes(NULL, 0);\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tif (!t->initialized)\n+\t\tinit_notes(t, NULL, 0);\n \n-\tsha1 = get_note(object_sha1);\n+\tsha1 = get_note(t, object_sha1);\n \tif (!sha1)\n \t\treturn;\n \ndiff --git a/notes.h b/notes.h\nindex ebc301c..a599a0d 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -2,6 +2,21 @@\n #define NOTES_H\n \n /*\n+ * Notes tree object\n+ *\n+ * Encapsulates the internal notes tree structure associated with a notes ref.\n+ * Whenever a struct notes_tree pointer is required below, you may pass NULL in\n+ * order to use the default/internal notes tree. E.g. you only need to pass a\n+ * non-NULL value if you need to refer to several different notes trees\n+ * simultaneously.\n+ */\n+extern struct notes_tree {\n+\tstruct int_node *root;\n+\tchar *ref;\n+\tint initialized;\n+} default_notes_tree;\n+\n+/*\n  * Flags controlling behaviour of notes tree initialization\n  *\n  * Default behaviour is to initialize the notes tree from the tree object\n@@ -10,44 +25,50 @@\n #define NOTES_INIT_EMPTY 1\n \n /*\n- * Initialize internal notes tree structure with the notes tree at the given\n+ * Initialize the given notes_tree with the notes tree structure at the given\n  * ref. If given ref is NULL, the value of the $GIT_NOTES_REF environment\n  * variable is used, and if that is missing, the default notes ref is used\n  * (\"refs/notes/commits\").\n  *\n- * If you need to re-intialize the internal notes tree structure (e.g. loading\n- * from a different notes ref), please first de-initialize the current notes\n- * tree by calling free_notes().\n+ * If you need to re-intialize a notes_tree structure (e.g. when switching from\n+ * one notes ref to another), you must first de-initialize the notes_tree\n+ * structure by calling free_notes(struct notes_tree *).\n+ *\n+ * If you pass t == NULL, the default internal notes_tree will be initialized.\n+ *\n+ * Precondition: The notes_tree structure is zeroed (this can be achieved with\n+ * memset(t, 0, sizeof(struct notes_tree)))\n  */\n-void init_notes(const char *notes_ref, int flags);\n+void init_notes(struct notes_tree *t, const char *notes_ref, int flags);\n \n /*\n- * Add the given note object to the internal notes tree structure\n+ * Add the given note object to the given notes_tree structure\n  *\n- * IMPORTANT: The changes made by add_note() to the internal notes tree structure\n+ * IMPORTANT: The changes made by add_note() to the given notes_tree structure\n  * are not persistent until a subsequent call to write_notes_tree() returns\n  * zero.\n  */\n-void add_note(const unsigned char *object_sha1,\n+void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n \t\tconst unsigned char *note_sha1);\n \n /*\n- * Remove the given note object from the internal notes tree structure\n+ * Remove the given note object from the given notes_tree structure\n  *\n- * IMPORTANT: The changes made by remove_note() to the internal notes tree\n+ * IMPORTANT: The changes made by remove_note() to the given notes_tree\n  * structure are not persistent until a subsequent call to write_notes_tree()\n  * returns zero.\n  */\n-void remove_note(const unsigned char *object_sha1);\n+void remove_note(struct notes_tree *t, const unsigned char *object_sha1);\n \n /* Get the note object SHA1 containing the note data for the given object */\n-const unsigned char *get_note(const unsigned char *object_sha1);\n+const unsigned char *get_note(struct notes_tree *t,\n+\t\tconst unsigned char *object_sha1);\n \n /*\n  * Flags controlling behaviour of for_each_note()\n  *\n  * Default behaviour of for_each_note() is to traverse every single note object\n- * in the notes tree, unpacking subtree entries along the way.\n+ * in the given notes tree, unpacking subtree entries along the way.\n  * The following flags can be used to alter the default behaviour:\n  *\n  * - DONT_UNPACK_SUBTREES causes for_each_note() NOT to unpack and recurse into\n@@ -71,7 +92,7 @@ const unsigned char *get_note(const unsigned char *object_sha1);\n #define FOR_EACH_NOTE_YIELD_SUBTREES 2\n \n /*\n- * Invoke the specified callback function for each note\n+ * Invoke the specified callback function for each note in the given notes_tree\n  *\n  * If the callback returns nonzero, the note walk is aborted, and the return\n  * value from the callback is returned from for_each_note(). Hence, a zero\n@@ -88,30 +109,30 @@ const unsigned char *get_note(const unsigned char *object_sha1);\n typedef int each_note_fn(const unsigned char *object_sha1,\n \t\tconst unsigned char *note_sha1, char *note_path,\n \t\tvoid *cb_data);\n-int for_each_note(int flags, each_note_fn fn, void *cb_data);\n+int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,\n+\t\tvoid *cb_data);\n \n /*\n- * Write the internal notes tree structure to the object database\n+ * Write the given notes_tree structure to the object database\n  *\n- * Creates a new tree object encapsulating the current state of the\n- * internal notes tree, and stores its SHA1 into the 'result' argument.\n+ * Creates a new tree object encapsulating the current state of the given\n+ * notes_tree, and stores its SHA1 into the 'result' argument.\n  *\n  * Returns zero on success, non-zero on failure.\n  *\n- * IMPORTANT: Changes made to the internal notes tree structure are not\n- * persistent until this function has returned zero. Please also remember\n- * to create a corresponding commit object, and update the appropriate\n- * notes ref.\n+ * IMPORTANT: Changes made to the given notes_tree are not persistent until\n+ * this function has returned zero. Please also remember to create a\n+ * corresponding commit object, and update the appropriate notes ref.\n  */\n-int write_notes_tree(unsigned char *result);\n+int write_notes_tree(struct notes_tree *t, unsigned char *result);\n \n /*\n- * Free (and de-initialize) the internal notes tree structure\n+ * Free (and de-initialize) the given notes_tree structure\n  *\n- * IMPORTANT: Changes made to the notes tree since the last, successful\n+ * IMPORTANT: Changes made to the given notes_tree since the last, successful\n  * call to write_notes_tree() will be lost.\n  */\n-void free_notes(void);\n+void free_notes(struct notes_tree *t);\n \n /* Flags controlling how notes are formatted */\n #define NOTES_SHOW_HEADER 1\n@@ -120,12 +141,14 @@ void free_notes(void);\n /*\n  * Fill the given strbuf with the notes associated with the given object.\n  *\n- * If the internal notes structure is not initialized, it will be auto-\n+ * If the given notes_tree structure is not initialized, it will be auto-\n  * initialized to the default value (see documentation for init_notes() above).\n+ * If the given notes_tree is NULL, the internal/default notes_tree will be\n+ * used instead.\n  *\n  * 'flags' is a bitwise combination of the above formatting flags.\n  */\n-void format_note(const unsigned char *object_sha1, struct strbuf *sb,\n-\t\tconst char *output_encoding, int flags);\n+void format_note(struct notes_tree *t, const unsigned char *object_sha1,\n+\t\tstruct strbuf *sb, const char *output_encoding, int flags);\n \n #endif\ndiff --git a/pretty.c b/pretty.c\nindex fe77090..b4882cc 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -775,8 +775,9 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,\n \t\t}\n \t\treturn 0;\t/* unknown %g placeholder */\n \tcase 'N':\n-\t\tformat_note(commit->object.sha1, sb, git_log_output_encoding ?\n-\t\t\t    git_log_output_encoding : git_commit_encoding, 0);\n+\t\tformat_note(NULL, commit->object.sha1, sb,\n+\t\t\t    git_log_output_encoding ? git_log_output_encoding\n+\t\t\t\t\t\t    : git_commit_encoding, 0);\n \t\treturn 1;\n \t}\n \n@@ -1095,7 +1096,7 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,\n \t\tstrbuf_addch(sb, '\\n');\n \n \tif (fmt != CMIT_FMT_ONELINE)\n-\t\tformat_note(commit->object.sha1, sb, encoding,\n+\t\tformat_note(NULL, commit->object.sha1, sb, encoding,\n \t\t\t    NOTES_SHOW_HEADER | NOTES_INDENT);\n \n \tfree(reencoded);\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131973","messageId":"1263762277-31419-12-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 11/20] Refactor notes concatenation into a flexible interface for combining notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:28Z","receivedAt":"2010-01-17T21:04:28Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"When adding a note to an object that already has an existing note, the\ncurrent solution is to concatenate the contents of the two notes. However,\nthe caller may instead wish to _overwrite_ the existing note with the new\nnote, or maybe even _ignore_ the new note, and keep the existing one. There\nmight also be other ways of combining notes that are only known to the\ncaller.\n\nTherefore, instead of unconditionally concatenating notes, we let the caller\nspecify how to combine notes, by passing in a pointer to a function for\ncombining notes. The caller may choose to implement its own function for\nnotes combining, but normally one of the following three conveniently\nsupplied notes combination functions will be sufficient:\n\n- combine_notes_concatenate() combines the two notes by appending the\n  contents of the new note to the contents of the existing note.\n\n- combine_notes_overwrite() replaces the existing note with the new note.\n\n- combine_notes_ignore() keeps the existing note, and ignores the new note.\n\nA combine_notes function can be passed to init_notes() to choose a default\ncombine_notes function for that notes tree. If NULL is given, the notes tree\nfalls back to combine_notes_concatenate() as the ultimate default.\n\nA combine_notes function can also be passed directly to add_note(), to\ncontrol the notes combining behaviour for a note addition in particular.\nIf NULL is passed, the combine_notes function registered for the given\nnotes tree is used.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |  138 ++++++++++++++++++++++++++++++++++++--------------------------\n notes.h |   34 +++++++++++++++-\n 2 files changed, 112 insertions(+), 60 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 1cac4ef..a40bc11 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -1,5 +1,6 @@\n #include \"cache.h\"\n #include \"notes.h\"\n+#include \"blob.h\"\n #include \"tree.h\"\n #include \"utf8.h\"\n #include \"strbuf.h\"\n@@ -127,55 +128,12 @@ static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n,\n \treturn NULL;\n }\n \n-/* Create a new blob object by concatenating the two given blob objects */\n-static int concatenate_notes(unsigned char *cur_sha1,\n-\t\tconst unsigned char *new_sha1)\n-{\n-\tchar *cur_msg, *new_msg, *buf;\n-\tunsigned long cur_len, new_len, buf_len;\n-\tenum object_type cur_type, new_type;\n-\tint ret;\n-\n-\t/* read in both note blob objects */\n-\tnew_msg = read_sha1_file(new_sha1, &new_type, &new_len);\n-\tif (!new_msg || !new_len || new_type != OBJ_BLOB) {\n-\t\tfree(new_msg);\n-\t\treturn 0;\n-\t}\n-\tcur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len);\n-\tif (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {\n-\t\tfree(cur_msg);\n-\t\tfree(new_msg);\n-\t\thashcpy(cur_sha1, new_sha1);\n-\t\treturn 0;\n-\t}\n-\n-\t/* we will separate the notes by a newline anyway */\n-\tif (cur_msg[cur_len - 1] == '\\n')\n-\t\tcur_len--;\n-\n-\t/* concatenate cur_msg and new_msg into buf */\n-\tbuf_len = cur_len + 1 + new_len;\n-\tbuf = (char *) xmalloc(buf_len);\n-\tmemcpy(buf, cur_msg, cur_len);\n-\tbuf[cur_len] = '\\n';\n-\tmemcpy(buf + cur_len + 1, new_msg, new_len);\n-\n-\tfree(cur_msg);\n-\tfree(new_msg);\n-\n-\t/* create a new blob object from buf */\n-\tret = write_sha1_file(buf, buf_len, \"blob\", cur_sha1);\n-\tfree(buf);\n-\treturn ret;\n-}\n-\n /*\n  * To insert a leaf_node:\n  * Search to the tree location appropriate for the given leaf_node's key:\n  * - If location is unused (NULL), store the tweaked pointer directly there\n  * - If location holds a note entry that matches the note-to-be-inserted, then\n- *   concatenate the two notes.\n+ *   combine the two notes (by calling the given combine_notes function).\n  * - If location holds a note entry that matches the subtree-to-be-inserted,\n  *   then unpack the subtree-to-be-inserted into the location.\n  * - If location holds a matching subtree entry, unpack the subtree at that\n@@ -184,7 +142,8 @@ static int concatenate_notes(unsigned char *cur_sha1,\n  *   node-to-be-inserted, and store the new int_node into the location.\n  */\n static void note_tree_insert(struct int_node *tree, unsigned char n,\n-\t\tstruct leaf_node *entry, unsigned char type)\n+\t\tstruct leaf_node *entry, unsigned char type,\n+\t\tcombine_notes_fn combine_notes)\n {\n \tstruct int_node *new_node;\n \tstruct leaf_node *l;\n@@ -205,12 +164,11 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,\n \t\t\t\tif (!hashcmp(l->val_sha1, entry->val_sha1))\n \t\t\t\t\treturn;\n \n-\t\t\t\tif (concatenate_notes(l->val_sha1,\n-\t\t\t\t\t\tentry->val_sha1))\n-\t\t\t\t\tdie(\"failed to concatenate note %s \"\n-\t\t\t\t\t    \"into note %s for object %s\",\n-\t\t\t\t\t    sha1_to_hex(entry->val_sha1),\n+\t\t\t\tif (combine_notes(l->val_sha1, entry->val_sha1))\n+\t\t\t\t\tdie(\"failed to combine notes %s and %s\"\n+\t\t\t\t\t    \" for object %s\",\n \t\t\t\t\t    sha1_to_hex(l->val_sha1),\n+\t\t\t\t\t    sha1_to_hex(entry->val_sha1),\n \t\t\t\t\t    sha1_to_hex(l->key_sha1));\n \t\t\t\tfree(entry);\n \t\t\t\treturn;\n@@ -233,7 +191,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,\n \t\t\t*p = NULL;\n \t\t\tload_subtree(l, tree, n);\n \t\t\tfree(l);\n-\t\t\tnote_tree_insert(tree, n, entry, type);\n+\t\t\tnote_tree_insert(tree, n, entry, type, combine_notes);\n \t\t\treturn;\n \t\t}\n \t\tbreak;\n@@ -243,9 +201,9 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,\n \tassert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||\n \t       GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);\n \tnew_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1);\n-\tnote_tree_insert(new_node, n + 1, l, GET_PTR_TYPE(*p));\n+\tnote_tree_insert(new_node, n + 1, l, GET_PTR_TYPE(*p), combine_notes);\n \t*p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);\n-\tnote_tree_insert(new_node, n + 1, entry, type);\n+\tnote_tree_insert(new_node, n + 1, entry, type, combine_notes);\n }\n \n /*\n@@ -406,7 +364,8 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n \t\t\t\tl->key_sha1[19] = (unsigned char) len;\n \t\t\t\ttype = PTR_TYPE_SUBTREE;\n \t\t\t}\n-\t\t\tnote_tree_insert(node, n, l, type);\n+\t\t\tnote_tree_insert(node, n, l, type,\n+\t\t\t\t\t combine_notes_concatenate);\n \t\t}\n \t}\n \tfree(buf);\n@@ -657,7 +616,64 @@ static int write_each_note(const unsigned char *object_sha1,\n \treturn write_each_note_helper(d->root, note_path, mode, note_sha1);\n }\n \n-void init_notes(struct notes_tree *t, const char *notes_ref, int flags)\n+int combine_notes_concatenate(unsigned char *cur_sha1,\n+\t\tconst unsigned char *new_sha1)\n+{\n+\tchar *cur_msg = NULL, *new_msg = NULL, *buf;\n+\tunsigned long cur_len, new_len, buf_len;\n+\tenum object_type cur_type, new_type;\n+\tint ret;\n+\n+\t/* read in both note blob objects */\n+\tif (!is_null_sha1(new_sha1))\n+\t\tnew_msg = read_sha1_file(new_sha1, &new_type, &new_len);\n+\tif (!new_msg || !new_len || new_type != OBJ_BLOB) {\n+\t\tfree(new_msg);\n+\t\treturn 0;\n+\t}\n+\tif (!is_null_sha1(cur_sha1))\n+\t\tcur_msg = read_sha1_file(cur_sha1, &cur_type, &cur_len);\n+\tif (!cur_msg || !cur_len || cur_type != OBJ_BLOB) {\n+\t\tfree(cur_msg);\n+\t\tfree(new_msg);\n+\t\thashcpy(cur_sha1, new_sha1);\n+\t\treturn 0;\n+\t}\n+\n+\t/* we will separate the notes by a newline anyway */\n+\tif (cur_msg[cur_len - 1] == '\\n')\n+\t\tcur_len--;\n+\n+\t/* concatenate cur_msg and new_msg into buf */\n+\tbuf_len = cur_len + 1 + new_len;\n+\tbuf = (char *) xmalloc(buf_len);\n+\tmemcpy(buf, cur_msg, cur_len);\n+\tbuf[cur_len] = '\\n';\n+\tmemcpy(buf + cur_len + 1, new_msg, new_len);\n+\tfree(cur_msg);\n+\tfree(new_msg);\n+\n+\t/* create a new blob object from buf */\n+\tret = write_sha1_file(buf, buf_len, blob_type, cur_sha1);\n+\tfree(buf);\n+\treturn ret;\n+}\n+\n+int combine_notes_overwrite(unsigned char *cur_sha1,\n+\t\tconst unsigned char *new_sha1)\n+{\n+\thashcpy(cur_sha1, new_sha1);\n+\treturn 0;\n+}\n+\n+int combine_notes_ignore(unsigned char *cur_sha1,\n+\t\tconst unsigned char *new_sha1)\n+{\n+\treturn 0;\n+}\n+\n+void init_notes(struct notes_tree *t, const char *notes_ref,\n+\t\tcombine_notes_fn combine_notes, int flags)\n {\n \tunsigned char sha1[20], object_sha1[20];\n \tunsigned mode;\n@@ -674,8 +690,12 @@ void init_notes(struct notes_tree *t, const char *notes_ref, int flags)\n \tif (!notes_ref)\n \t\tnotes_ref = GIT_NOTES_DEFAULT_REF;\n \n+\tif (!combine_notes)\n+\t\tcombine_notes = combine_notes_concatenate;\n+\n \tt->root = (struct int_node *) xcalloc(sizeof(struct int_node), 1);\n \tt->ref = notes_ref ? xstrdup(notes_ref) : NULL;\n+\tt->combine_notes = combine_notes;\n \tt->initialized = 1;\n \n \tif (flags & NOTES_INIT_EMPTY || !notes_ref ||\n@@ -691,17 +711,19 @@ void init_notes(struct notes_tree *t, const char *notes_ref, int flags)\n }\n \n void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n-\t\tconst unsigned char *note_sha1)\n+\t\tconst unsigned char *note_sha1, combine_notes_fn combine_notes)\n {\n \tstruct leaf_node *l;\n \n \tif (!t)\n \t\tt = &default_notes_tree;\n \tassert(t->initialized);\n+\tif (!combine_notes)\n+\t\tcombine_notes = t->combine_notes;\n \tl = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));\n \thashcpy(l->key_sha1, object_sha1);\n \thashcpy(l->val_sha1, note_sha1);\n-\tnote_tree_insert(t->root, 0, l, PTR_TYPE_NOTE);\n+\tnote_tree_insert(t->root, 0, l, PTR_TYPE_NOTE, combine_notes);\n }\n \n void remove_note(struct notes_tree *t, const unsigned char *object_sha1)\n@@ -786,7 +808,7 @@ void format_note(struct notes_tree *t, const unsigned char *object_sha1,\n \tif (!t)\n \t\tt = &default_notes_tree;\n \tif (!t->initialized)\n-\t\tinit_notes(t, NULL, 0);\n+\t\tinit_notes(t, NULL, NULL, 0);\n \n \tsha1 = get_note(t, object_sha1);\n \tif (!sha1)\ndiff --git a/notes.h b/notes.h\nindex a599a0d..5c49465 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -2,6 +2,30 @@\n #define NOTES_H\n \n /*\n+ * Function type for combining two notes annotating the same object.\n+ *\n+ * When adding a new note annotating the same object as an existing note, it is\n+ * up to the caller to decide how to combine the two notes. The decision is\n+ * made by passing in a function of the following form. The function accepts\n+ * two SHA1s -- of the existing note and the new note, respectively. The\n+ * function then combines the notes in whatever way it sees fit, and writes the\n+ * resulting SHA1 into the first SHA1 argument (cur_sha1). A non-zero return\n+ * value indicates failure.\n+ *\n+ * The two given SHA1s must both be non-NULL and different from each other.\n+ *\n+ * The default combine_notes function (you get this when passing NULL) is\n+ * combine_notes_concatenate(), which appends the contents of the new note to\n+ * the contents of the existing note.\n+ */\n+typedef int combine_notes_fn(unsigned char *cur_sha1, const unsigned char *new_sha1);\n+\n+/* Common notes combinators */\n+int combine_notes_concatenate(unsigned char *cur_sha1, const unsigned char *new_sha1);\n+int combine_notes_overwrite(unsigned char *cur_sha1, const unsigned char *new_sha1);\n+int combine_notes_ignore(unsigned char *cur_sha1, const unsigned char *new_sha1);\n+\n+/*\n  * Notes tree object\n  *\n  * Encapsulates the internal notes tree structure associated with a notes ref.\n@@ -13,6 +37,7 @@\n extern struct notes_tree {\n \tstruct int_node *root;\n \tchar *ref;\n+\tcombine_notes_fn *combine_notes;\n \tint initialized;\n } default_notes_tree;\n \n@@ -36,10 +61,15 @@ extern struct notes_tree {\n  *\n  * If you pass t == NULL, the default internal notes_tree will be initialized.\n  *\n+ * The combine_notes function that is passed becomes the default combine_notes\n+ * function for the given notes_tree. If NULL is passed, the default\n+ * combine_notes function is combine_notes_concatenate().\n+ *\n  * Precondition: The notes_tree structure is zeroed (this can be achieved with\n  * memset(t, 0, sizeof(struct notes_tree)))\n  */\n-void init_notes(struct notes_tree *t, const char *notes_ref, int flags);\n+void init_notes(struct notes_tree *t, const char *notes_ref,\n+\t\tcombine_notes_fn combine_notes, int flags);\n \n /*\n  * Add the given note object to the given notes_tree structure\n@@ -49,7 +79,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref, int flags);\n  * zero.\n  */\n void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n-\t\tconst unsigned char *note_sha1);\n+\t\tconst unsigned char *note_sha1, combine_notes_fn combine_notes);\n \n /*\n  * Remove the given note object from the given notes_tree structure\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131980","messageId":"1263762277-31419-13-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 12/20] Builtin-ify git-notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:29Z","receivedAt":"2010-01-17T21:04:29Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"The builtin-ification includes some minor behavioural changes to the\ncommand-line interface: It is no longer allowed to mix the -m and -F\narguments, and it is not allowed to use multiple -F options.\n\nAs part of the builtin-ification, we add the commit_notes() function\nto the builtin API. This function (together with the notes.h API) can\nbe easily used from other builtins to manipulate the notes tree.\n\nAlso includes needed changes to t3301.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n Documentation/git-notes.txt                   |    6 +-\n Makefile                                      |    2 +-\n builtin-notes.c                               |  240 +++++++++++++++++++++++++\n builtin.h                                     |    3 +\n git-notes.sh => contrib/examples/git-notes.sh |    0\n git.c                                         |    1 +\n t/t3301-notes.sh                              |   84 ++++++---\n 7 files changed, 306 insertions(+), 30 deletions(-)\n create mode 100644 builtin-notes.c\n rename git-notes.sh => contrib/examples/git-notes.sh (100%)\n\ndiff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt\nindex 94cceb1..bb73b02 100644\n--- a/Documentation/git-notes.txt\n+++ b/Documentation/git-notes.txt\n@@ -37,14 +37,12 @@ OPTIONS\n -------\n -m <msg>::\n \tUse the given note message (instead of prompting).\n-\tIf multiple `-m` (or `-F`) options are given, their\n-\tvalues are concatenated as separate paragraphs.\n+\tIf multiple `-m` options are given, their values\n+\tare concatenated as separate paragraphs.\n \n -F <file>::\n \tTake the note message from the given file.  Use '-' to\n \tread the note message from the standard input.\n-\tIf multiple `-F` (or `-m`) options are given, their\n-\tvalues are concatenated as separate paragraphs.\n \n \n Author\ndiff --git a/Makefile b/Makefile\nindex 4a1e5bc..b3f0c0c 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -354,7 +354,6 @@ SCRIPT_SH += git-merge-one-file.sh\n SCRIPT_SH += git-merge-resolve.sh\n SCRIPT_SH += git-mergetool.sh\n SCRIPT_SH += git-mergetool--lib.sh\n-SCRIPT_SH += git-notes.sh\n SCRIPT_SH += git-parse-remote.sh\n SCRIPT_SH += git-pull.sh\n SCRIPT_SH += git-quiltimport.sh\n@@ -658,6 +657,7 @@ BUILTIN_OBJS += builtin-merge-recursive.o\n BUILTIN_OBJS += builtin-mktree.o\n BUILTIN_OBJS += builtin-mv.o\n BUILTIN_OBJS += builtin-name-rev.o\n+BUILTIN_OBJS += builtin-notes.o\n BUILTIN_OBJS += builtin-pack-objects.o\n BUILTIN_OBJS += builtin-pack-refs.o\n BUILTIN_OBJS += builtin-prune-packed.o\ndiff --git a/builtin-notes.c b/builtin-notes.c\nnew file mode 100644\nindex 0000000..a534ece\n--- /dev/null\n+++ b/builtin-notes.c\n@@ -0,0 +1,240 @@\n+/*\n+ * Builtin \"git notes\"\n+ *\n+ * Copyright (c) 2010 Johan Herland <johan@herland.net>\n+ *\n+ * Based on git-notes.sh by Johannes Schindelin,\n+ * and builtin-tag.c by Kristian Høgsberg and Carlos Rica.\n+ */\n+\n+#include \"cache.h\"\n+#include \"builtin.h\"\n+#include \"notes.h\"\n+#include \"blob.h\"\n+#include \"commit.h\"\n+#include \"refs.h\"\n+#include \"exec_cmd.h\"\n+#include \"parse-options.h\"\n+\n+static const char * const git_notes_usage[] = {\n+\t\"git notes edit [-m <msg>|-F <file>] [<object>]\",\n+\t\"git notes show [<object>]\",\n+\tNULL\n+};\n+\n+static const char note_template[] =\n+\t\"\\n\"\n+\t\"#\\n\"\n+\t\"# Write/edit the note contents\\n\"\n+\t\"#\\n\";\n+\n+static void write_note_data(int fd, const unsigned char *sha1)\n+{\n+\tunsigned long size;\n+\tenum object_type type;\n+\tchar *buf = read_sha1_file(sha1, &type, &size);\n+\tif (buf) {\n+\t\tif (size)\n+\t\t\twrite_or_die(fd, buf, size);\n+\t\tfree(buf);\n+\t}\n+}\n+\n+static void create_note(const unsigned char *object,\n+\t\t\tstruct strbuf *buf,\n+\t\t\tint skip_editor,\n+\t\t\tconst unsigned char *prev,\n+\t\t\tunsigned char *result)\n+{\n+\tchar *path = NULL;\n+\n+\tif (!skip_editor) {\n+\t\tint fd;\n+\n+\t\t/* write the template message before editing: */\n+\t\tpath = git_pathdup(\"NOTES_EDITMSG\");\n+\t\tfd = open(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);\n+\t\tif (fd < 0)\n+\t\t\tdie_errno(\"could not create file '%s'\", path);\n+\n+\t\tif (prev)\n+\t\t\twrite_note_data(fd, prev);\n+\t\twrite_or_die(fd, note_template, strlen(note_template));\n+\n+\t\tclose(fd);\n+\n+\t\tif (launch_editor(path, buf, NULL)) {\n+\t\t\tfprintf(stderr, \"Please supply the note contents using\"\n+\t\t\t\t\" either -m or -F option.\\n\");\n+\t\t\texit(1);\n+\t\t}\n+\t}\n+\n+\tstripspace(buf, 1);\n+\n+\tif (!skip_editor && !buf->len) {\n+\t\tfprintf(stderr, \"Removing note for object %s\\n\",\n+\t\t\tsha1_to_hex(object));\n+\t\thashclr(result);\n+\t}\n+\telse {\n+\t\tif (write_sha1_file(buf->buf, buf->len, blob_type, result)) {\n+\t\t\terror(\"unable to write note object\");\n+\t\t\tif (path)\n+\t\t\t\terror(\"The note contents has been left in %s\",\n+\t\t\t\t      path);\n+\t\t\texit(128);\n+\t\t}\n+\t}\n+\n+\tif (path) {\n+\t\tunlink_or_warn(path);\n+\t\tfree(path);\n+\t}\n+}\n+\n+struct msg_arg {\n+\tint given;\n+\tstruct strbuf buf;\n+};\n+\n+static int parse_msg_arg(const struct option *opt, const char *arg, int unset)\n+{\n+\tstruct msg_arg *msg = opt->value;\n+\n+\tif (!arg)\n+\t\treturn -1;\n+\tif (msg->buf.len)\n+\t\tstrbuf_addstr(&(msg->buf), \"\\n\\n\");\n+\tstrbuf_addstr(&(msg->buf), arg);\n+\tmsg->given = 1;\n+\treturn 0;\n+}\n+\n+int commit_notes(struct notes_tree *t, const char *msg)\n+{\n+\tstruct commit_list *parent;\n+\tunsigned char tree_sha1[20], prev_commit[20], new_commit[20];\n+\tstruct strbuf buf = STRBUF_INIT;\n+\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tif (!t->initialized || !t->ref || !*t->ref)\n+\t\tdie(\"Cannot commit uninitialized/unreferenced notes tree\");\n+\n+\t/* Prepare commit message and reflog message */\n+\tstrbuf_addstr(&buf, \"notes: \"); /* commit message starts at index 7 */\n+\tstrbuf_addstr(&buf, msg);\n+\tif (buf.buf[buf.len - 1] != '\\n')\n+\t\tstrbuf_addch(&buf, '\\n'); /* Make sure msg ends with newline */\n+\n+\t/* Convert notes tree to tree object */\n+\tif (write_notes_tree(t, tree_sha1))\n+\t\tdie(\"Failed to write current notes tree to database\");\n+\n+\t/* Create new commit for the tree object */\n+\tif (!read_ref(t->ref, prev_commit)) { /* retrieve parent commit */\n+\t\tparent = xmalloc(sizeof(*parent));\n+\t\tparent->item = lookup_commit(prev_commit);\n+\t\tparent->next =NULL;\n+\t} else {\n+\t\thashclr(prev_commit);\n+\t\tparent = NULL;\n+\t}\n+\tif (commit_tree(buf.buf + 7, tree_sha1, parent, new_commit, NULL))\n+\t\tdie(\"Failed to commit notes tree to database\");\n+\n+\t/* Update notes ref with new commit */\n+\tupdate_ref(buf.buf, t->ref, new_commit, prev_commit, 0, DIE_ON_ERR);\n+\n+\tstrbuf_release(&buf);\n+\treturn 0;\n+}\n+\n+int cmd_notes(int argc, const char **argv, const char *prefix)\n+{\n+\tstruct strbuf buf = STRBUF_INIT;\n+\tstruct notes_tree *t;\n+\tunsigned char object[20], new_note[20];\n+\tconst unsigned char *note;\n+\tconst char *object_ref;\n+\tint edit = 0, show = 0;\n+\tconst char *msgfile = NULL;\n+\tstruct msg_arg msg = { 0, STRBUF_INIT };\n+\tstruct option options[] = {\n+\t\tOPT_GROUP(\"Notes edit options\"),\n+\t\tOPT_CALLBACK('m', NULL, &msg, \"msg\",\n+\t\t\t     \"note contents as a string\", parse_msg_arg),\n+\t\tOPT_FILENAME('F', NULL, &msgfile, \"note contents in a file\"),\n+\t\tOPT_END()\n+\t};\n+\n+\tgit_config(git_default_config, NULL);\n+\n+\targc = parse_options(argc, argv, prefix, options, git_notes_usage, 0);\n+\n+\tif (argc && !strcmp(argv[0], \"edit\"))\n+\t\tedit = 1;\n+\telse if (argc && !strcmp(argv[0], \"show\"))\n+\t\tshow = 1;\n+\n+\tif (edit + show != 1)\n+\t\tusage_with_options(git_notes_usage, options);\n+\n+\tobject_ref = argc == 2 ? argv[1] : \"HEAD\";\n+\tif (argc > 2) {\n+\t\terror(\"too many parameters\");\n+\t\tusage_with_options(git_notes_usage, options);\n+\t}\n+\n+\tif (get_sha1(object_ref, object))\n+\t\tdie(\"Failed to resolve '%s' as a valid ref.\", object_ref);\n+\n+\tinit_notes(NULL, NULL, NULL, 0);\n+\tt = &default_notes_tree;\n+\n+\tif (prefixcmp(t->ref, \"refs/notes/\"))\n+\t\tdie(\"Refusing to %s notes in %s (outside of refs/notes/)\",\n+\t\t    edit ? \"edit\" : \"show\", t->ref);\n+\n+\tnote = get_note(t, object);\n+\n+\t/* show command */\n+\n+\tif (show && !note) {\n+\t\terror(\"No note found for object %s.\", sha1_to_hex(object));\n+\t\treturn 1;\n+\t} else if (show) {\n+\t\tconst char *show_args[3] = {\"show\", sha1_to_hex(note), NULL};\n+\t\treturn execv_git_cmd(show_args);\n+\t}\n+\n+\t/* edit command */\n+\n+\tif (msg.given || msgfile) {\n+\t\tif (msg.given && msgfile) {\n+\t\t\terror(\"mixing -m and -F options is not allowed.\");\n+\t\t\tusage_with_options(git_notes_usage, options);\n+\t\t}\n+\t\tif (msg.given)\n+\t\t\tstrbuf_addbuf(&buf, &(msg.buf));\n+\t\telse {\n+\t\t\tif (!strcmp(msgfile, \"-\")) {\n+\t\t\t\tif (strbuf_read(&buf, 0, 1024) < 0)\n+\t\t\t\t\tdie_errno(\"cannot read '%s'\", msgfile);\n+\t\t\t} else {\n+\t\t\t\tif (strbuf_read_file(&buf, msgfile, 1024) < 0)\n+\t\t\t\t\tdie_errno(\"could not open or read '%s'\",\n+\t\t\t\t\t\tmsgfile);\n+\t\t\t}\n+\t\t}\n+\t}\n+\n+\tcreate_note(object, &buf, msg.given || msgfile, note, new_note);\n+\tadd_note(t, object, new_note, combine_notes_overwrite);\n+\tcommit_notes(t, \"Note added by 'git notes edit'\");\n+\n+\tfree_notes(t);\n+\tstrbuf_release(&buf);\n+\treturn 0;\n+}\ndiff --git a/builtin.h b/builtin.h\nindex c3f83c0..67e30a2 100644\n--- a/builtin.h\n+++ b/builtin.h\n@@ -5,6 +5,7 @@\n #include \"strbuf.h\"\n #include \"cache.h\"\n #include \"commit.h\"\n+#include \"notes.h\"\n \n extern const char git_version_string[];\n extern const char git_usage_string[];\n@@ -18,6 +19,7 @@ extern int fmt_merge_msg(int merge_summary, struct strbuf *in,\n extern int commit_tree(const char *msg, unsigned char *tree,\n \t\tstruct commit_list *parents, unsigned char *ret,\n \t\tconst char *author);\n+extern int commit_notes(struct notes_tree *t, const char *msg);\n extern int check_pager_config(const char *cmd);\n \n extern int cmd_add(int argc, const char **argv, const char *prefix);\n@@ -73,6 +75,7 @@ extern int cmd_merge_recursive(int argc, const char **argv, const char *prefix);\n extern int cmd_mktree(int argc, const char **argv, const char *prefix);\n extern int cmd_mv(int argc, const char **argv, const char *prefix);\n extern int cmd_name_rev(int argc, const char **argv, const char *prefix);\n+extern int cmd_notes(int argc, const char **argv, const char *prefix);\n extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);\n extern int cmd_pickaxe(int argc, const char **argv, const char *prefix);\n extern int cmd_prune(int argc, const char **argv, const char *prefix);\ndiff --git a/git-notes.sh b/contrib/examples/git-notes.sh\nsimilarity index 100%\nrename from git-notes.sh\nrename to contrib/examples/git-notes.sh\ndiff --git a/git.c b/git.c\nindex 11544cd..b4b118e 100644\n--- a/git.c\n+++ b/git.c\n@@ -336,6 +336,7 @@ static void handle_internal_command(int argc, const char **argv)\n \t\t{ \"mktree\", cmd_mktree, RUN_SETUP },\n \t\t{ \"mv\", cmd_mv, RUN_SETUP | NEED_WORK_TREE },\n \t\t{ \"name-rev\", cmd_name_rev, RUN_SETUP },\n+\t\t{ \"notes\", cmd_notes, RUN_SETUP },\n \t\t{ \"pack-objects\", cmd_pack_objects, RUN_SETUP },\n \t\t{ \"peek-remote\", cmd_ls_remote },\n \t\t{ \"pickaxe\", cmd_blame, RUN_SETUP },\ndiff --git a/t/t3301-notes.sh b/t/t3301-notes.sh\nindex 3985d2a..c6031b1 100755\n--- a/t/t3301-notes.sh\n+++ b/t/t3301-notes.sh\n@@ -12,8 +12,8 @@ echo \"$MSG\" > \"$1\"\n echo \"$MSG\" >& 2\n EOF\n chmod a+x fake_editor.sh\n-VISUAL=./fake_editor.sh\n-export VISUAL\n+GIT_EDITOR=./fake_editor.sh\n+export GIT_EDITOR\n \n test_expect_success 'cannot annotate non-existing HEAD' '\n \t(MSG=3 && export MSG && test_must_fail git notes edit)\n@@ -56,8 +56,17 @@ test_expect_success 'handle empty notes gracefully' '\n \n test_expect_success 'create notes' '\n \tgit config core.notesRef refs/notes/commits &&\n+\tMSG=b0 git notes edit &&\n+\ttest ! -f .git/NOTES_EDITMSG &&\n+\ttest 1 = $(git ls-tree refs/notes/commits | wc -l) &&\n+\ttest b0 = $(git notes show) &&\n+\tgit show HEAD^ &&\n+\ttest_must_fail git notes show HEAD^\n+'\n+\n+test_expect_success 'edit existing notes' '\n \tMSG=b1 git notes edit &&\n-\ttest ! -f .git/new-notes &&\n+\ttest ! -f .git/NOTES_EDITMSG &&\n \ttest 1 = $(git ls-tree refs/notes/commits | wc -l) &&\n \ttest b1 = $(git notes show) &&\n \tgit show HEAD^ &&\n@@ -110,19 +119,16 @@ test_expect_success 'show multi-line notes' '\n \tgit log -2 > output &&\n \ttest_cmp expect-multiline output\n '\n-test_expect_success 'create -m and -F notes (setup)' '\n+test_expect_success 'create -F notes (setup)' '\n \t: > a4 &&\n \tgit add a4 &&\n \ttest_tick &&\n \tgit commit -m 4th &&\n \techo \"xyzzy\" > note5 &&\n-\tgit notes edit -m spam -F note5 -m \"foo\n-bar\n-baz\"\n+\tgit notes edit -F note5\n '\n \n-whitespace=\"    \"\n-cat > expect-m-and-F << EOF\n+cat > expect-F << EOF\n commit 15023535574ded8b1a89052b32673f84cf9582b8\n Author: A U Thor <author@example.com>\n Date:   Thu Apr 7 15:16:13 2005 -0700\n@@ -130,32 +136,29 @@ Date:   Thu Apr 7 15:16:13 2005 -0700\n     4th\n \n Notes:\n-    spam\n-$whitespace\n     xyzzy\n-$whitespace\n-    foo\n-    bar\n-    baz\n EOF\n \n-printf \"\\n\" >> expect-m-and-F\n-cat expect-multiline >> expect-m-and-F\n+printf \"\\n\" >> expect-F\n+cat expect-multiline >> expect-F\n \n-test_expect_success 'show -m and -F notes' '\n+test_expect_success 'show -F notes' '\n \tgit log -3 > output &&\n-\ttest_cmp expect-m-and-F output\n+\ttest_cmp expect-F output\n '\n \n-test_expect_success 'create other note on a different notes ref (setup)' '\n+test_expect_success 'create -m notes (setup)' '\n \t: > a5 &&\n \tgit add a5 &&\n \ttest_tick &&\n \tgit commit -m 5th &&\n-\tGIT_NOTES_REF=\"refs/notes/other\" git notes edit -m \"other note\"\n+\tgit notes edit -m spam -m \"foo\n+bar\n+baz\"\n '\n \n-cat > expect-other << EOF\n+whitespace=\"    \"\n+cat > expect-m << EOF\n commit bd1753200303d0a0344be813e504253b3d98e74d\n Author: A U Thor <author@example.com>\n Date:   Thu Apr 7 15:17:13 2005 -0700\n@@ -163,15 +166,46 @@ Date:   Thu Apr 7 15:17:13 2005 -0700\n     5th\n \n Notes:\n+    spam\n+$whitespace\n+    foo\n+    bar\n+    baz\n+EOF\n+\n+printf \"\\n\" >> expect-m\n+cat expect-F >> expect-m\n+\n+test_expect_success 'show -m notes' '\n+\tgit log -4 > output &&\n+\ttest_cmp expect-m output\n+'\n+\n+test_expect_success 'create other note on a different notes ref (setup)' '\n+\t: > a6 &&\n+\tgit add a6 &&\n+\ttest_tick &&\n+\tgit commit -m 6th &&\n+\tGIT_NOTES_REF=\"refs/notes/other\" git notes edit -m \"other note\"\n+'\n+\n+cat > expect-other << EOF\n+commit 387a89921c73d7ed72cd94d179c1c7048ca47756\n+Author: A U Thor <author@example.com>\n+Date:   Thu Apr 7 15:18:13 2005 -0700\n+\n+    6th\n+\n+Notes:\n     other note\n EOF\n \n cat > expect-not-other << EOF\n-commit bd1753200303d0a0344be813e504253b3d98e74d\n+commit 387a89921c73d7ed72cd94d179c1c7048ca47756\n Author: A U Thor <author@example.com>\n-Date:   Thu Apr 7 15:17:13 2005 -0700\n+Date:   Thu Apr 7 15:18:13 2005 -0700\n \n-    5th\n+    6th\n EOF\n \n test_expect_success 'Do not show note on other ref by default' '\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131974","messageId":"1263762277-31419-14-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 13/20] t3301: Verify successful annotation of non-commits","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:30Z","receivedAt":"2010-01-17T21:04:30Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Adds a testcase verifying that git-notes works successfully on\ntree, blob, and tag objects.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n t/t3301-notes.sh |   17 +++++++++++++++++\n 1 files changed, 17 insertions(+), 0 deletions(-)\n\ndiff --git a/t/t3301-notes.sh b/t/t3301-notes.sh\nindex c6031b1..0881db6 100755\n--- a/t/t3301-notes.sh\n+++ b/t/t3301-notes.sh\n@@ -229,4 +229,21 @@ test_expect_success 'Do not show note when core.notesRef is overridden' '\n \ttest_cmp expect-not-other output\n '\n \n+test_expect_success 'Allow notes on non-commits (trees, blobs, tags)' '\n+\techo \"Note on a tree\" > expect\n+\tgit notes edit -m \"Note on a tree\" HEAD: &&\n+\tgit notes show HEAD: > actual &&\n+\ttest_cmp expect actual &&\n+\techo \"Note on a blob\" > expect\n+\tfilename=$(git ls-tree --name-only HEAD | head -n1) &&\n+\tgit notes edit -m \"Note on a blob\" HEAD:$filename &&\n+\tgit notes show HEAD:$filename > actual &&\n+\ttest_cmp expect actual &&\n+\techo \"Note on a tag\" > expect\n+\tgit tag -a -m \"This is an annotated tag\" foobar HEAD^ &&\n+\tgit notes edit -m \"Note on a tag\" foobar &&\n+\tgit notes show foobar > actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131982","messageId":"1263762277-31419-15-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 14/20] t3305: Verify that adding many notes with git-notes triggers increased fanout","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:31Z","receivedAt":"2010-01-17T21:04:31Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Add a test verifying that the notes code automatically restructures the\nnotes tree into a deeper fanout level, when many notes are added with\n\"git notes\".\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n t/t3305-notes-fanout.sh |   50 +++++++++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 50 insertions(+), 0 deletions(-)\n create mode 100755 t/t3305-notes-fanout.sh\n\ndiff --git a/t/t3305-notes-fanout.sh b/t/t3305-notes-fanout.sh\nnew file mode 100755\nindex 0000000..823b0ff\n--- /dev/null\n+++ b/t/t3305-notes-fanout.sh\n@@ -0,0 +1,50 @@\n+#!/bin/sh\n+\n+test_description='Test that adding many notes triggers automatic fanout restructuring'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'creating many notes with git-notes' '\n+\tnum_notes=300 &&\n+\ti=0 &&\n+\twhile test $i -lt $num_notes\n+\tdo\n+\t\ti=$(($i + 1)) &&\n+\t\ttest_tick &&\n+\t\techo \"file for commit #$i\" > file &&\n+\t\tgit add file &&\n+\t\tgit commit -q -m \"commit #$i\" &&\n+\t\tgit notes edit -m \"note #$i\" || return 1\n+\tdone\n+'\n+\n+test_expect_success 'many notes created correctly with git-notes' '\n+\tgit log | grep \"^    \" > output &&\n+\ti=300 &&\n+\twhile test $i -gt 0\n+\tdo\n+\t\techo \"    commit #$i\" &&\n+\t\techo \"    note #$i\" &&\n+\t\ti=$(($i - 1));\n+\tdone > expect &&\n+\ttest_cmp expect output\n+'\n+\n+test_expect_success 'many notes created with git-notes triggers fanout' '\n+\t# Expect entire notes tree to have a fanout == 1\n+\tgit ls-tree -r --name-only refs/notes/commits |\n+\twhile read path\n+\tdo\n+\t\tcase \"$path\" in\n+\t\t??/??????????????????????????????????????)\n+\t\t\t: true\n+\t\t\t;;\n+\t\t*)\n+\t\t\techo \"Invalid path \\\"$path\\\"\" &&\n+\t\t\treturn 1\n+\t\t\t;;\n+\t\tesac\n+\tdone\n+'\n+\n+test_done\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131981","messageId":"1263762277-31419-16-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 15/20] Teach notes code to properly preserve non-notes in the notes tree","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:32Z","receivedAt":"2010-01-17T21:04:32Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"The note tree structure allows for non-note entries to coexist with note\nentries in a notes tree. Although we certainly expect there to be very\nfew non-notes in a notes tree, we should still support them to a certain\ndegree.\n\nThis patch teaches the notes code to preserve non-notes when updating the\nnotes tree with write_notes_tree(). Non-notes are not affected by fanout\nrestructuring.\n\nFor non-notes to be handled correctly, we can no longer allow subtree\nentries that do not match the fanout structure produced by the notes code\nitself. This means that fanouts like 4/36, 6/34, 8/32, 4/4/32, etc. are\nno longer recognized as note subtrees; only 2-based fanouts are allowed\n(2/38, 2/2/36, 2/2/2/34, etc.). Since the notes code has never at any point\n_produced_ non-2-based fanouts, it is highly unlikely that this change will\ncause problems for anyone.\n\nThe patch also adds some tests verifying the correct handling of non-notes\nin a notes tree.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c                   |  219 +++++++++++++++++++++++++++++++++++++--------\n notes.h                   |    1 +\n t/t3303-notes-subtrees.sh |   28 ++++---\n t/t3304-notes-mixed.sh    |   36 +++++++-\n 4 files changed, 233 insertions(+), 51 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex a40bc11..4f260a5 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -37,6 +37,21 @@ struct leaf_node {\n \tunsigned char val_sha1[20];\n };\n \n+/*\n+ * A notes tree may contain entries that are not notes, and that do not follow\n+ * the naming conventions of notes. There are typically none/few of these, but\n+ * we still need to keep track of them. Keep a simple linked list sorted alpha-\n+ * betically on the non-note path. The list is populated when parsing tree\n+ * objects in load_subtree(), and the non-notes are correctly written back into\n+ * the tree objects produced by write_notes_tree().\n+ */\n+struct non_note {\n+\tstruct non_note *next; /* grounded (last->next == NULL) */\n+\tchar *path;\n+\tunsigned int mode;\n+\tunsigned char sha1[20];\n+};\n+\n #define PTR_TYPE_NULL     0\n #define PTR_TYPE_INTERNAL 1\n #define PTR_TYPE_NOTE     2\n@@ -53,8 +68,8 @@ struct leaf_node {\n \n struct notes_tree default_notes_tree;\n \n-static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n-\t\tunsigned int n);\n+static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,\n+\t\tstruct int_node *node, unsigned int n);\n \n /*\n  * Search the tree until the appropriate location for the given key is found:\n@@ -71,7 +86,7 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n  *      - an unused leaf node (NULL)\n  *      In any case, set *tree and *n, and return pointer to the tree location.\n  */\n-static void **note_tree_search(struct int_node **tree,\n+static void **note_tree_search(struct notes_tree *t, struct int_node **tree,\n \t\tunsigned char *n, const unsigned char *key_sha1)\n {\n \tstruct leaf_node *l;\n@@ -83,9 +98,9 @@ static void **note_tree_search(struct int_node **tree,\n \t\tif (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {\n \t\t\t/* unpack tree and resume search */\n \t\t\t(*tree)->a[0] = NULL;\n-\t\t\tload_subtree(l, *tree, *n);\n+\t\t\tload_subtree(t, l, *tree, *n);\n \t\t\tfree(l);\n-\t\t\treturn note_tree_search(tree, n, key_sha1);\n+\t\t\treturn note_tree_search(t, tree, n, key_sha1);\n \t\t}\n \t}\n \n@@ -95,15 +110,15 @@ static void **note_tree_search(struct int_node **tree,\n \tcase PTR_TYPE_INTERNAL:\n \t\t*tree = CLR_PTR_TYPE(p);\n \t\t(*n)++;\n-\t\treturn note_tree_search(tree, n, key_sha1);\n+\t\treturn note_tree_search(t, tree, n, key_sha1);\n \tcase PTR_TYPE_SUBTREE:\n \t\tl = (struct leaf_node *) CLR_PTR_TYPE(p);\n \t\tif (!SUBTREE_SHA1_PREFIXCMP(key_sha1, l->key_sha1)) {\n \t\t\t/* unpack tree and resume search */\n \t\t\t(*tree)->a[i] = NULL;\n-\t\t\tload_subtree(l, *tree, *n);\n+\t\t\tload_subtree(t, l, *tree, *n);\n \t\t\tfree(l);\n-\t\t\treturn note_tree_search(tree, n, key_sha1);\n+\t\t\treturn note_tree_search(t, tree, n, key_sha1);\n \t\t}\n \t\t/* fall through */\n \tdefault:\n@@ -116,10 +131,11 @@ static void **note_tree_search(struct int_node **tree,\n  * Search to the tree location appropriate for the given key:\n  * If a note entry with matching key, return the note entry, else return NULL.\n  */\n-static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n,\n+static struct leaf_node *note_tree_find(struct notes_tree *t,\n+\t\tstruct int_node *tree, unsigned char n,\n \t\tconst unsigned char *key_sha1)\n {\n-\tvoid **p = note_tree_search(&tree, &n, key_sha1);\n+\tvoid **p = note_tree_search(t, &tree, &n, key_sha1);\n \tif (GET_PTR_TYPE(*p) == PTR_TYPE_NOTE) {\n \t\tstruct leaf_node *l = (struct leaf_node *) CLR_PTR_TYPE(*p);\n \t\tif (!hashcmp(key_sha1, l->key_sha1))\n@@ -141,13 +157,13 @@ static struct leaf_node *note_tree_find(struct int_node *tree, unsigned char n,\n  * - Else, create a new int_node, holding both the node-at-location and the\n  *   node-to-be-inserted, and store the new int_node into the location.\n  */\n-static void note_tree_insert(struct int_node *tree, unsigned char n,\n-\t\tstruct leaf_node *entry, unsigned char type,\n+static void note_tree_insert(struct notes_tree *t, struct int_node *tree,\n+\t\tunsigned char n, struct leaf_node *entry, unsigned char type,\n \t\tcombine_notes_fn combine_notes)\n {\n \tstruct int_node *new_node;\n \tstruct leaf_node *l;\n-\tvoid **p = note_tree_search(&tree, &n, entry->key_sha1);\n+\tvoid **p = note_tree_search(t, &tree, &n, entry->key_sha1);\n \n \tassert(GET_PTR_TYPE(entry) == 0); /* no type bits set */\n \tl = (struct leaf_node *) CLR_PTR_TYPE(*p);\n@@ -178,7 +194,7 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,\n \t\t\tif (!SUBTREE_SHA1_PREFIXCMP(l->key_sha1,\n \t\t\t\t\t\t    entry->key_sha1)) {\n \t\t\t\t/* unpack 'entry' */\n-\t\t\t\tload_subtree(entry, tree, n);\n+\t\t\t\tload_subtree(t, entry, tree, n);\n \t\t\t\tfree(entry);\n \t\t\t\treturn;\n \t\t\t}\n@@ -189,9 +205,10 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,\n \t\tif (!SUBTREE_SHA1_PREFIXCMP(entry->key_sha1, l->key_sha1)) {\n \t\t\t/* unpack 'l' and restart insert */\n \t\t\t*p = NULL;\n-\t\t\tload_subtree(l, tree, n);\n+\t\t\tload_subtree(t, l, tree, n);\n \t\t\tfree(l);\n-\t\t\tnote_tree_insert(tree, n, entry, type, combine_notes);\n+\t\t\tnote_tree_insert(t, tree, n, entry, type,\n+\t\t\t\t\t combine_notes);\n \t\t\treturn;\n \t\t}\n \t\tbreak;\n@@ -201,9 +218,10 @@ static void note_tree_insert(struct int_node *tree, unsigned char n,\n \tassert(GET_PTR_TYPE(*p) == PTR_TYPE_NOTE ||\n \t       GET_PTR_TYPE(*p) == PTR_TYPE_SUBTREE);\n \tnew_node = (struct int_node *) xcalloc(sizeof(struct int_node), 1);\n-\tnote_tree_insert(new_node, n + 1, l, GET_PTR_TYPE(*p), combine_notes);\n+\tnote_tree_insert(t, new_node, n + 1, l, GET_PTR_TYPE(*p),\n+\t\t\t combine_notes);\n \t*p = SET_PTR_TYPE(new_node, PTR_TYPE_INTERNAL);\n-\tnote_tree_insert(new_node, n + 1, entry, type, combine_notes);\n+\tnote_tree_insert(t, new_node, n + 1, entry, type, combine_notes);\n }\n \n /*\n@@ -249,7 +267,7 @@ static void note_tree_remove(struct notes_tree *t, struct int_node *tree,\n \tstruct leaf_node *l;\n \tstruct int_node *parent_stack[20];\n \tunsigned char i, j;\n-\tvoid **p = note_tree_search(&tree, &n, entry->key_sha1);\n+\tvoid **p = note_tree_search(t, &tree, &n, entry->key_sha1);\n \n \tassert(GET_PTR_TYPE(entry) == 0); /* no type bits set */\n \tif (GET_PTR_TYPE(*p) != PTR_TYPE_NOTE)\n@@ -324,14 +342,67 @@ static int get_sha1_hex_segment(const char *hex, unsigned int hex_len,\n \treturn len;\n }\n \n-static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n-\t\tunsigned int n)\n+static int non_note_cmp(const struct non_note *a, const struct non_note *b)\n+{\n+\treturn strcmp(a->path, b->path);\n+}\n+\n+static void add_non_note(struct notes_tree *t, const char *path,\n+\t\tunsigned int mode, const unsigned char *sha1)\n+{\n+\tstruct non_note *p = t->prev_non_note, *n;\n+\tn = (struct non_note *) xmalloc(sizeof(struct non_note));\n+\tn->next = NULL;\n+\tn->path = xstrdup(path);\n+\tn->mode = mode;\n+\thashcpy(n->sha1, sha1);\n+\tt->prev_non_note = n;\n+\n+\tif (!t->first_non_note) {\n+\t\tt->first_non_note = n;\n+\t\treturn;\n+\t}\n+\n+\tif (non_note_cmp(p, n) < 0)\n+\t\t; /* do nothing  */\n+\telse if (non_note_cmp(t->first_non_note, n) <= 0)\n+\t\tp = t->first_non_note;\n+\telse {\n+\t\t/* n sorts before t->first_non_note */\n+\t\tn->next = t->first_non_note;\n+\t\tt->first_non_note = n;\n+\t\treturn;\n+\t}\n+\n+\t/* n sorts equal or after p */\n+\twhile (p->next && non_note_cmp(p->next, n) <= 0)\n+\t\tp = p->next;\n+\n+\tif (non_note_cmp(p, n) == 0) { /* n ~= p; overwrite p with n */\n+\t\tassert(strcmp(p->path, n->path) == 0);\n+\t\tp->mode = n->mode;\n+\t\thashcpy(p->sha1, n->sha1);\n+\t\tfree(n);\n+\t\tt->prev_non_note = p;\n+\t\treturn;\n+\t}\n+\n+\t/* n sorts between p and p->next */\n+\tn->next = p->next;\n+\tp->next = n;\n+}\n+\n+static void load_subtree(struct notes_tree *t, struct leaf_node *subtree,\n+\t\tstruct int_node *node, unsigned int n)\n {\n \tunsigned char object_sha1[20];\n \tunsigned int prefix_len;\n \tvoid *buf;\n \tstruct tree_desc desc;\n \tstruct name_entry entry;\n+\tint len, path_len;\n+\tunsigned char type;\n+\tstruct leaf_node *l;\n \n \tbuf = fill_tree_descriptor(&desc, subtree->val_sha1);\n \tif (!buf)\n@@ -342,31 +413,68 @@ static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n \tassert(prefix_len * 2 >= n);\n \tmemcpy(object_sha1, subtree->key_sha1, prefix_len);\n \twhile (tree_entry(&desc, &entry)) {\n-\t\tint len = get_sha1_hex_segment(entry.path, strlen(entry.path),\n+\t\tpath_len = strlen(entry.path);\n+\t\tlen = get_sha1_hex_segment(entry.path, path_len,\n \t\t\t\tobject_sha1 + prefix_len, 20 - prefix_len);\n \t\tif (len < 0)\n-\t\t\tcontinue; /* entry.path is not a SHA1 sum. Skip */\n+\t\t\tgoto handle_non_note; /* entry.path is not a SHA1 */\n \t\tlen += prefix_len;\n \n \t\t/*\n \t\t * If object SHA1 is complete (len == 20), assume note object\n-\t\t * If object SHA1 is incomplete (len < 20), assume note subtree\n+\t\t * If object SHA1 is incomplete (len < 20), and current\n+\t\t * component consists of 2 hex chars, assume note subtree\n \t\t */\n \t\tif (len <= 20) {\n-\t\t\tunsigned char type = PTR_TYPE_NOTE;\n-\t\t\tstruct leaf_node *l = (struct leaf_node *)\n+\t\t\ttype = PTR_TYPE_NOTE;\n+\t\t\tl = (struct leaf_node *)\n \t\t\t\txcalloc(sizeof(struct leaf_node), 1);\n \t\t\thashcpy(l->key_sha1, object_sha1);\n \t\t\thashcpy(l->val_sha1, entry.sha1);\n \t\t\tif (len < 20) {\n-\t\t\t\tif (!S_ISDIR(entry.mode))\n-\t\t\t\t\tcontinue; /* entry cannot be subtree */\n+\t\t\t\tif (!S_ISDIR(entry.mode) || path_len != 2)\n+\t\t\t\t\tgoto handle_non_note; /* not subtree */\n \t\t\t\tl->key_sha1[19] = (unsigned char) len;\n \t\t\t\ttype = PTR_TYPE_SUBTREE;\n \t\t\t}\n-\t\t\tnote_tree_insert(node, n, l, type,\n+\t\t\tnote_tree_insert(t, node, n, l, type,\n \t\t\t\t\t combine_notes_concatenate);\n \t\t}\n+\t\tcontinue;\n+\n+handle_non_note:\n+\t\t/*\n+\t\t * Determine full path for this non-note entry:\n+\t\t * The filename is already found in entry.path, but the\n+\t\t * directory part of the path must be deduced from the subtree\n+\t\t * containing this entry. We assume here that the overall notes\n+\t\t * tree follows a strict byte-based progressive fanout\n+\t\t * structure (i.e. using 2/38, 2/2/36, etc. fanouts, and not\n+\t\t * e.g. 4/36 fanout). This means that if a non-note is found at\n+\t\t * path \"dead/beef\", the following code will register it as\n+\t\t * being found on \"de/ad/beef\".\n+\t\t * On the other hand, if you use such non-obvious non-note\n+\t\t * paths in the middle of a notes tree, you deserve what's\n+\t\t * coming to you ;). Note that for non-notes that are not\n+\t\t * SHA1-like at the top level, there will be no problems.\n+\t\t *\n+\t\t * To conclude, it is strongly advised to make sure non-notes\n+\t\t * have at least one non-hex character in the top-level path\n+\t\t * component.\n+\t\t */\n+\t\t{\n+\t\t\tchar non_note_path[PATH_MAX];\n+\t\t\tchar *p = non_note_path;\n+\t\t\tconst char *q = sha1_to_hex(subtree->key_sha1);\n+\t\t\tint i;\n+\t\t\tfor (i = 0; i < prefix_len; i++) {\n+\t\t\t\t*p++ = *q++;\n+\t\t\t\t*p++ = *q++;\n+\t\t\t\t*p++ = '/';\n+\t\t\t}\n+\t\t\tstrcpy(p, entry.path);\n+\t\t\tadd_non_note(t, non_note_path, entry.mode, entry.sha1);\n+\t\t}\n \t}\n \tfree(buf);\n }\n@@ -426,9 +534,9 @@ static void construct_path_with_fanout(const unsigned char *sha1,\n \tstrcpy(path + i, hex_sha1 + j);\n }\n \n-static int for_each_note_helper(struct int_node *tree, unsigned char n,\n-\t\tunsigned char fanout, int flags, each_note_fn fn,\n-\t\tvoid *cb_data)\n+static int for_each_note_helper(struct notes_tree *t, struct int_node *tree,\n+\t\tunsigned char n, unsigned char fanout, int flags,\n+\t\teach_note_fn fn, void *cb_data)\n {\n \tunsigned int i;\n \tvoid *p;\n@@ -443,7 +551,7 @@ redo:\n \t\tswitch (GET_PTR_TYPE(p)) {\n \t\tcase PTR_TYPE_INTERNAL:\n \t\t\t/* recurse into int_node */\n-\t\t\tret = for_each_note_helper(CLR_PTR_TYPE(p), n + 1,\n+\t\t\tret = for_each_note_helper(t, CLR_PTR_TYPE(p), n + 1,\n \t\t\t\tfanout, flags, fn, cb_data);\n \t\t\tbreak;\n \t\tcase PTR_TYPE_SUBTREE:\n@@ -481,7 +589,7 @@ redo:\n \t\t\t    !(flags & FOR_EACH_NOTE_DONT_UNPACK_SUBTREES)) {\n \t\t\t\t/* unpack subtree and resume traversal */\n \t\t\t\ttree->a[i] = NULL;\n-\t\t\t\tload_subtree(l, tree, n);\n+\t\t\t\tload_subtree(t, l, tree, n);\n \t\t\t\tfree(l);\n \t\t\t\tgoto redo;\n \t\t\t}\n@@ -594,8 +702,29 @@ static int write_each_note_helper(struct tree_write_stack *tws,\n \n struct write_each_note_data {\n \tstruct tree_write_stack *root;\n+\tstruct non_note *next_non_note;\n };\n \n+static int write_each_non_note_until(const char *note_path,\n+\t\tstruct write_each_note_data *d)\n+{\n+\tstruct non_note *n = d->next_non_note;\n+\tint cmp, ret;\n+\twhile (n && (!note_path || (cmp = strcmp(n->path, note_path)) <= 0)) {\n+\t\tif (note_path && cmp == 0)\n+\t\t\t; /* do nothing, prefer note to non-note */\n+\t\telse {\n+\t\t\tret = write_each_note_helper(d->root, n->path, n->mode,\n+\t\t\t\t\t\t     n->sha1);\n+\t\t\tif (ret)\n+\t\t\t\treturn ret;\n+\t\t}\n+\t\tn = n->next;\n+\t}\n+\td->next_non_note = n;\n+\treturn 0;\n+}\n+\n static int write_each_note(const unsigned char *object_sha1,\n \t\tconst unsigned char *note_sha1, char *note_path,\n \t\tvoid *cb_data)\n@@ -613,7 +742,9 @@ static int write_each_note(const unsigned char *object_sha1,\n \t}\n \tassert(note_path_len <= 40 + 19);\n \n-\treturn write_each_note_helper(d->root, note_path, mode, note_sha1);\n+\t/* Weave non-note entries into note entries */\n+\treturn  write_each_non_note_until(note_path, d) ||\n+\t\twrite_each_note_helper(d->root, note_path, mode, note_sha1);\n }\n \n int combine_notes_concatenate(unsigned char *cur_sha1,\n@@ -694,6 +825,8 @@ void init_notes(struct notes_tree *t, const char *notes_ref,\n \t\tcombine_notes = combine_notes_concatenate;\n \n \tt->root = (struct int_node *) xcalloc(sizeof(struct int_node), 1);\n+\tt->first_non_note = NULL;\n+\tt->prev_non_note = NULL;\n \tt->ref = notes_ref ? xstrdup(notes_ref) : NULL;\n \tt->combine_notes = combine_notes;\n \tt->initialized = 1;\n@@ -707,7 +840,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref,\n \n \thashclr(root_tree.key_sha1);\n \thashcpy(root_tree.val_sha1, sha1);\n-\tload_subtree(&root_tree, t->root, 0);\n+\tload_subtree(t, &root_tree, t->root, 0);\n }\n \n void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n@@ -723,7 +856,7 @@ void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n \tl = (struct leaf_node *) xmalloc(sizeof(struct leaf_node));\n \thashcpy(l->key_sha1, object_sha1);\n \thashcpy(l->val_sha1, note_sha1);\n-\tnote_tree_insert(t->root, 0, l, PTR_TYPE_NOTE, combine_notes);\n+\tnote_tree_insert(t, t->root, 0, l, PTR_TYPE_NOTE, combine_notes);\n }\n \n void remove_note(struct notes_tree *t, const unsigned char *object_sha1)\n@@ -746,7 +879,7 @@ const unsigned char *get_note(struct notes_tree *t,\n \tif (!t)\n \t\tt = &default_notes_tree;\n \tassert(t->initialized);\n-\tfound = note_tree_find(t->root, 0, object_sha1);\n+\tfound = note_tree_find(t, t->root, 0, object_sha1);\n \treturn found ? found->val_sha1 : NULL;\n }\n \n@@ -756,7 +889,7 @@ int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,\n \tif (!t)\n \t\tt = &default_notes_tree;\n \tassert(t->initialized);\n-\treturn for_each_note_helper(t->root, 0, 0, flags, fn, cb_data);\n+\treturn for_each_note_helper(t, t->root, 0, 0, flags, fn, cb_data);\n }\n \n int write_notes_tree(struct notes_tree *t, unsigned char *result)\n@@ -774,11 +907,13 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result)\n \tstrbuf_init(&root.buf, 256 * (32 + 40)); /* assume 256 entries */\n \troot.path[0] = root.path[1] = '\\0';\n \tcb_data.root = &root;\n+\tcb_data.next_non_note = t->first_non_note;\n \n \t/* Write tree objects representing current notes tree */\n \tret = for_each_note(t, FOR_EACH_NOTE_DONT_UNPACK_SUBTREES |\n \t\t\t\tFOR_EACH_NOTE_YIELD_SUBTREES,\n \t\t\twrite_each_note, &cb_data) ||\n+\t\twrite_each_non_note_until(NULL, &cb_data) ||\n \t\ttree_write_stack_finish_subtree(&root) ||\n \t\twrite_sha1_file(root.buf.buf, root.buf.len, tree_type, result);\n \tstrbuf_release(&root.buf);\n@@ -792,6 +927,12 @@ void free_notes(struct notes_tree *t)\n \tif (t->root)\n \t\tnote_tree_free(t->root);\n \tfree(t->root);\n+\twhile (t->first_non_note) {\n+\t\tt->prev_non_note = t->first_non_note->next;\n+\t\tfree(t->first_non_note->path);\n+\t\tfree(t->first_non_note);\n+\t\tt->first_non_note = t->prev_non_note;\n+\t}\n \tfree(t->ref);\n \tmemset(t, 0, sizeof(struct notes_tree));\n }\ndiff --git a/notes.h b/notes.h\nindex 5c49465..f25643e 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -36,6 +36,7 @@ int combine_notes_ignore(unsigned char *cur_sha1, const unsigned char *new_sha1)\n  */\n extern struct notes_tree {\n \tstruct int_node *root;\n+\tstruct non_note *first_non_note, *prev_non_note;\n \tchar *ref;\n \tcombine_notes_fn *combine_notes;\n \tint initialized;\ndiff --git a/t/t3303-notes-subtrees.sh b/t/t3303-notes-subtrees.sh\nindex edc4bc8..75ec187 100755\n--- a/t/t3303-notes-subtrees.sh\n+++ b/t/t3303-notes-subtrees.sh\n@@ -95,12 +95,12 @@ INPUT_END\n test_expect_success 'test notes in 2/38-fanout' 'test_sha1_based \"s|^..|&/|\"'\n test_expect_success 'verify notes in 2/38-fanout' 'verify_notes'\n \n-test_expect_success 'test notes in 4/36-fanout' 'test_sha1_based \"s|^....|&/|\"'\n-test_expect_success 'verify notes in 4/36-fanout' 'verify_notes'\n-\n test_expect_success 'test notes in 2/2/36-fanout' 'test_sha1_based \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\"'\n test_expect_success 'verify notes in 2/2/36-fanout' 'verify_notes'\n \n+test_expect_success 'test notes in 2/2/2/34-fanout' 'test_sha1_based \"s|^\\(..\\)\\(..\\)\\(..\\)|\\1/\\2/\\3/|\"'\n+test_expect_success 'verify notes in 2/2/2/34-fanout' 'verify_notes'\n+\n test_same_notes () {\n \t(\n \t\tstart_note_commit &&\n@@ -128,14 +128,17 @@ INPUT_END\n \tgit fast-import --quiet\n }\n \n-test_expect_success 'test same notes in 4/36-fanout and 2/38-fanout' 'test_same_notes \"s|^..|&/|\" \"s|^....|&/|\"'\n-test_expect_success 'verify same notes in 4/36-fanout and 2/38-fanout' 'verify_notes'\n+test_expect_success 'test same notes in no fanout and 2/38-fanout' 'test_same_notes \"s|^..|&/|\" \"\"'\n+test_expect_success 'verify same notes in no fanout and 2/38-fanout' 'verify_notes'\n+\n+test_expect_success 'test same notes in no fanout and 2/2/36-fanout' 'test_same_notes \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\" \"\"'\n+test_expect_success 'verify same notes in no fanout and 2/2/36-fanout' 'verify_notes'\n \n test_expect_success 'test same notes in 2/38-fanout and 2/2/36-fanout' 'test_same_notes \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\" \"s|^..|&/|\"'\n test_expect_success 'verify same notes in 2/38-fanout and 2/2/36-fanout' 'verify_notes'\n \n-test_expect_success 'test same notes in 4/36-fanout and 2/2/36-fanout' 'test_same_notes \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\" \"s|^....|&/|\"'\n-test_expect_success 'verify same notes in 4/36-fanout and 2/2/36-fanout' 'verify_notes'\n+test_expect_success 'test same notes in 2/2/2/34-fanout and 2/2/36-fanout' 'test_same_notes \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\" \"s|^\\(..\\)\\(..\\)\\(..\\)|\\1/\\2/\\3/|\"'\n+test_expect_success 'verify same notes in 2/2/2/34-fanout and 2/2/36-fanout' 'verify_notes'\n \n test_concatenated_notes () {\n \t(\n@@ -176,13 +179,16 @@ verify_concatenated_notes () {\n     test_cmp expect output\n }\n \n-test_expect_success 'test notes in 4/36-fanout concatenated with 2/38-fanout' 'test_concatenated_notes \"s|^..|&/|\" \"s|^....|&/|\"'\n-test_expect_success 'verify notes in 4/36-fanout concatenated with 2/38-fanout' 'verify_concatenated_notes'\n+test_expect_success 'test notes in no fanout concatenated with 2/38-fanout' 'test_concatenated_notes \"s|^..|&/|\" \"\"'\n+test_expect_success 'verify notes in no fanout concatenated with 2/38-fanout' 'verify_concatenated_notes'\n+\n+test_expect_success 'test notes in no fanout concatenated with 2/2/36-fanout' 'test_concatenated_notes \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\" \"\"'\n+test_expect_success 'verify notes in no fanout concatenated with 2/2/36-fanout' 'verify_concatenated_notes'\n \n test_expect_success 'test notes in 2/38-fanout concatenated with 2/2/36-fanout' 'test_concatenated_notes \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\" \"s|^..|&/|\"'\n test_expect_success 'verify notes in 2/38-fanout concatenated with 2/2/36-fanout' 'verify_concatenated_notes'\n \n-test_expect_success 'test notes in 4/36-fanout concatenated with 2/2/36-fanout' 'test_concatenated_notes \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\" \"s|^....|&/|\"'\n-test_expect_success 'verify notes in 4/36-fanout concatenated with 2/2/36-fanout' 'verify_concatenated_notes'\n+test_expect_success 'test notes in 2/2/36-fanout concatenated with 2/2/2/34-fanout' 'test_concatenated_notes \"s|^\\(..\\)\\(..\\)\\(..\\)|\\1/\\2/\\3/|\" \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\"'\n+test_expect_success 'verify notes in 2/2/36-fanout concatenated with 2/2/2/34-fanout' 'verify_concatenated_notes'\n \n test_done\ndiff --git a/t/t3304-notes-mixed.sh b/t/t3304-notes-mixed.sh\nindex 256687f..c975a6d 100755\n--- a/t/t3304-notes-mixed.sh\n+++ b/t/t3304-notes-mixed.sh\n@@ -131,6 +131,17 @@ data <<EOF\n another non-note with SHA1-like name\n EOF\n \n+M 644 inline de/adbeefdeadbeefdeadbeefdeadbeefdeadbeef\n+data <<EOF\n+This is actually a valid note, albeit to a non-existing object.\n+It is needed in order to trigger the \"mishandling\" of the dead/beef non-note.\n+EOF\n+\n+M 644 inline dead/beef\n+data <<EOF\n+yet another non-note with SHA1-like name\n+EOF\n+\n INPUT_END\n \tgit fast-import --quiet <input &&\n \tgit config core.notesRef refs/notes/commits\n@@ -158,6 +169,9 @@ EXPECT_END\n cat >expect_nn3 <<EXPECT_END\n another non-note with SHA1-like name\n EXPECT_END\n+cat >expect_nn4 <<EXPECT_END\n+yet another non-note with SHA1-like name\n+EXPECT_END\n \n test_expect_success \"verify contents of non-notes\" '\n \n@@ -166,7 +180,27 @@ test_expect_success \"verify contents of non-notes\" '\n \tgit cat-file -p refs/notes/commits:deadbeef > actual_nn2 &&\n \ttest_cmp expect_nn2 actual_nn2 &&\n \tgit cat-file -p refs/notes/commits:de/adbeef > actual_nn3 &&\n-\ttest_cmp expect_nn3 actual_nn3\n+\ttest_cmp expect_nn3 actual_nn3 &&\n+\tgit cat-file -p refs/notes/commits:dead/beef > actual_nn4 &&\n+\ttest_cmp expect_nn4 actual_nn4\n+'\n+\n+test_expect_success \"git-notes preserves non-notes\" '\n+\n+\ttest_tick &&\n+\tgit notes edit -m \"foo bar\"\n+'\n+\n+test_expect_success \"verify contents of non-notes after git-notes\" '\n+\n+\tgit cat-file -p refs/notes/commits:foobar/non-note.txt > actual_nn1 &&\n+\ttest_cmp expect_nn1 actual_nn1 &&\n+\tgit cat-file -p refs/notes/commits:deadbeef > actual_nn2 &&\n+\ttest_cmp expect_nn2 actual_nn2 &&\n+\tgit cat-file -p refs/notes/commits:de/adbeef > actual_nn3 &&\n+\ttest_cmp expect_nn3 actual_nn3 &&\n+\tgit cat-file -p refs/notes/commits:dead/beef > actual_nn4 &&\n+\ttest_cmp expect_nn4 actual_nn4\n '\n \n test_done\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131983","messageId":"1263762277-31419-17-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 16/20] Teach builtin-notes to remove empty notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:33Z","receivedAt":"2010-01-17T21:04:33Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"When the result of editing a note is an empty string, the associated note\nentry should be deleted from the notes tree.\n\nThis allows deleting notes by invoking either \"git notes -m ''\" or\n\"git notes -F /dev/null\".\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n builtin-notes.c  |   15 +++++++++++----\n t/t3301-notes.sh |   31 +++++++++++++++++++++++++++++++\n 2 files changed, 42 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin-notes.c b/builtin-notes.c\nindex a534ece..0cce608 100644\n--- a/builtin-notes.c\n+++ b/builtin-notes.c\n@@ -72,7 +72,7 @@ static void create_note(const unsigned char *object,\n \n \tstripspace(buf, 1);\n \n-\tif (!skip_editor && !buf->len) {\n+\tif (!buf->len) {\n \t\tfprintf(stderr, \"Removing note for object %s\\n\",\n \t\t\tsha1_to_hex(object));\n \t\thashclr(result);\n@@ -157,7 +157,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)\n \tstruct notes_tree *t;\n \tunsigned char object[20], new_note[20];\n \tconst unsigned char *note;\n-\tconst char *object_ref;\n+\tconst char *object_ref, *logmsg;\n+\n \tint edit = 0, show = 0;\n \tconst char *msgfile = NULL;\n \tstruct msg_arg msg = { 0, STRBUF_INIT };\n@@ -231,8 +232,14 @@ int cmd_notes(int argc, const char **argv, const char *prefix)\n \t}\n \n \tcreate_note(object, &buf, msg.given || msgfile, note, new_note);\n-\tadd_note(t, object, new_note, combine_notes_overwrite);\n-\tcommit_notes(t, \"Note added by 'git notes edit'\");\n+\tif (is_null_sha1(new_note)) {\n+\t\tremove_note(t, object);\n+\t\tlogmsg = \"Note removed by 'git notes edit'\";\n+\t} else {\n+\t\tadd_note(t, object, new_note, combine_notes_overwrite);\n+\t\tlogmsg = \"Note added by 'git notes edit'\";\n+\t}\n+\tcommit_notes(t, logmsg);\n \n \tfree_notes(t);\n \tstrbuf_release(&buf);\ndiff --git a/t/t3301-notes.sh b/t/t3301-notes.sh\nindex 0881db6..404a94b 100755\n--- a/t/t3301-notes.sh\n+++ b/t/t3301-notes.sh\n@@ -181,6 +181,37 @@ test_expect_success 'show -m notes' '\n \ttest_cmp expect-m output\n '\n \n+test_expect_success 'remove note with -F /dev/null (setup)' '\n+\tgit notes edit -F /dev/null\n+'\n+\n+cat > expect-rm-F << EOF\n+commit bd1753200303d0a0344be813e504253b3d98e74d\n+Author: A U Thor <author@example.com>\n+Date:   Thu Apr 7 15:17:13 2005 -0700\n+\n+    5th\n+EOF\n+\n+printf \"\\n\" >> expect-rm-F\n+cat expect-F >> expect-rm-F\n+\n+test_expect_success 'verify note removal with -F /dev/null' '\n+\tgit log -4 > output &&\n+\ttest_cmp expect-rm-F output &&\n+\t! git notes show\n+'\n+\n+test_expect_success 'do not create empty note with -m \"\" (setup)' '\n+\tgit notes edit -m \"\"\n+'\n+\n+test_expect_success 'verify non-creation of note with -m \"\"' '\n+\tgit log -4 > output &&\n+\ttest_cmp expect-rm-F output &&\n+\t! git notes show\n+'\n+\n test_expect_success 'create other note on a different notes ref (setup)' '\n \t: > a6 &&\n \tgit add a6 &&\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131978","messageId":"1263762277-31419-18-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 17/20] builtin-notes: Teach -d option for deleting existing notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:34Z","receivedAt":"2010-01-17T21:04:34Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Using -d is equivalent to specifying an empty note message. This option is\nprovided largely for consistency with other git commands, such as git tag\nand git branch.\n\nThe patch includes tests verifying the correct behaviour of the new option.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n Documentation/git-notes.txt |   13 ++++++++-----\n builtin-notes.c             |   16 ++++++++++------\n t/t3301-notes.sh            |   27 +++++++++++++++++++++++++++\n 3 files changed, 45 insertions(+), 11 deletions(-)\n\ndiff --git a/Documentation/git-notes.txt b/Documentation/git-notes.txt\nindex bb73b02..bac34bb 100644\n--- a/Documentation/git-notes.txt\n+++ b/Documentation/git-notes.txt\n@@ -8,14 +8,14 @@ git-notes - Add/inspect commit notes\n SYNOPSIS\n --------\n [verse]\n-'git-notes' (edit [-F <file> | -m <msg>] | show) [commit]\n+'git-notes' (edit [-F <file> | -m <msg> | -d] | show) [commit]\n \n DESCRIPTION\n -----------\n-This command allows you to add notes to commit messages, without\n-changing the commit.  To discern these notes from the message stored\n-in the commit object, the notes are indented like the message, after\n-an unindented line saying \"Notes:\".\n+This command allows you to add/remove notes to/from commit messages,\n+without changing the commit.  To discern these notes from the message\n+stored in the commit object, the notes are indented like the message,\n+after an unindented line saying \"Notes:\".\n \n To disable commit notes, you have to set the config variable\n core.notesRef to the empty string.  Alternatively, you can set it\n@@ -44,6 +44,9 @@ OPTIONS\n \tTake the note message from the given file.  Use '-' to\n \tread the note message from the standard input.\n \n+-d::\n+\tRemove the note. This is equivalent to specifying an\n+\tempty note message.\n \n Author\n ------\ndiff --git a/builtin-notes.c b/builtin-notes.c\nindex 0cce608..a166ad0 100644\n--- a/builtin-notes.c\n+++ b/builtin-notes.c\n@@ -17,7 +17,7 @@\n #include \"parse-options.h\"\n \n static const char * const git_notes_usage[] = {\n-\t\"git notes edit [-m <msg>|-F <file>] [<object>]\",\n+\t\"git notes edit [-m <msg> | -F <file> | -d] [<object>]\",\n \t\"git notes show [<object>]\",\n \tNULL\n };\n@@ -159,7 +159,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)\n \tconst unsigned char *note;\n \tconst char *object_ref, *logmsg;\n \n-\tint edit = 0, show = 0;\n+\tint edit = 0, show = 0, delete = 0;\n \tconst char *msgfile = NULL;\n \tstruct msg_arg msg = { 0, STRBUF_INIT };\n \tstruct option options[] = {\n@@ -167,6 +167,7 @@ int cmd_notes(int argc, const char **argv, const char *prefix)\n \t\tOPT_CALLBACK('m', NULL, &msg, \"msg\",\n \t\t\t     \"note contents as a string\", parse_msg_arg),\n \t\tOPT_FILENAME('F', NULL, &msgfile, \"note contents in a file\"),\n+\t\tOPT_BOOLEAN('d', NULL, &delete, \"delete existing note\"),\n \t\tOPT_END()\n \t};\n \n@@ -212,13 +213,15 @@ int cmd_notes(int argc, const char **argv, const char *prefix)\n \n \t/* edit command */\n \n-\tif (msg.given || msgfile) {\n-\t\tif (msg.given && msgfile) {\n-\t\t\terror(\"mixing -m and -F options is not allowed.\");\n+\tif (msg.given || msgfile || delete) {\n+\t\tif (msg.given + delete + (msgfile ? 1 : 0) > 1) {\n+\t\t\terror(\"mixing -m/-F/-d options is not allowed.\");\n \t\t\tusage_with_options(git_notes_usage, options);\n \t\t}\n \t\tif (msg.given)\n \t\t\tstrbuf_addbuf(&buf, &(msg.buf));\n+\t\telse if (delete)\n+\t\t\tstrbuf_reset(&buf);\n \t\telse {\n \t\t\tif (!strcmp(msgfile, \"-\")) {\n \t\t\t\tif (strbuf_read(&buf, 0, 1024) < 0)\n@@ -231,7 +234,8 @@ int cmd_notes(int argc, const char **argv, const char *prefix)\n \t\t}\n \t}\n \n-\tcreate_note(object, &buf, msg.given || msgfile, note, new_note);\n+\tcreate_note(object, &buf, msg.given || msgfile || delete, note,\n+\t\t    new_note);\n \tif (is_null_sha1(new_note)) {\n \t\tremove_note(t, object);\n \t\tlogmsg = \"Note removed by 'git notes edit'\";\ndiff --git a/t/t3301-notes.sh b/t/t3301-notes.sh\nindex 404a94b..8617c2f 100755\n--- a/t/t3301-notes.sh\n+++ b/t/t3301-notes.sh\n@@ -212,6 +212,33 @@ test_expect_success 'verify non-creation of note with -m \"\"' '\n \t! git notes show\n '\n \n+test_expect_success 'remove note with -d (setup)' '\n+\tgit notes edit -d HEAD^\n+'\n+\n+cat > expect-rm-d << EOF\n+commit bd1753200303d0a0344be813e504253b3d98e74d\n+Author: A U Thor <author@example.com>\n+Date:   Thu Apr 7 15:17:13 2005 -0700\n+\n+    5th\n+\n+commit 15023535574ded8b1a89052b32673f84cf9582b8\n+Author: A U Thor <author@example.com>\n+Date:   Thu Apr 7 15:16:13 2005 -0700\n+\n+    4th\n+EOF\n+\n+printf \"\\n\" >> expect-rm-d\n+cat expect-multiline >> expect-rm-d\n+\n+test_expect_success 'verify note removal with -d' '\n+\tgit log -4 > output &&\n+\ttest_cmp expect-rm-d output &&\n+\t! git notes show HEAD^\n+'\n+\n test_expect_success 'create other note on a different notes ref (setup)' '\n \t: > a6 &&\n \tgit add a6 &&\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131986","messageId":"1263762277-31419-19-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 18/20] t3305: Verify that removing notes triggers automatic fanout consolidation","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:35Z","receivedAt":"2010-01-17T21:04:35Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Signed-off-by: Johan Herland <johan@herland.net>\n---\n t/t3305-notes-fanout.sh |   47 ++++++++++++++++++++++++++++++++++++++++++++++-\n 1 files changed, 46 insertions(+), 1 deletions(-)\n\ndiff --git a/t/t3305-notes-fanout.sh b/t/t3305-notes-fanout.sh\nindex 823b0ff..687a46a 100755\n--- a/t/t3305-notes-fanout.sh\n+++ b/t/t3305-notes-fanout.sh\n@@ -1,6 +1,6 @@\n #!/bin/sh\n \n-test_description='Test that adding many notes triggers automatic fanout restructuring'\n+test_description='Test that adding/removing many notes triggers automatic fanout restructuring'\n \n . ./test-lib.sh\n \n@@ -47,4 +47,49 @@ test_expect_success 'many notes created with git-notes triggers fanout' '\n \tdone\n '\n \n+test_expect_success 'deleting most notes with git-notes' '\n+\tnum_notes=250 &&\n+\ti=0 &&\n+\tgit rev-list HEAD |\n+\twhile read sha1\n+\tdo\n+\t\ti=$(($i + 1)) &&\n+\t\tif test $i -gt $num_notes\n+\t\tthen\n+\t\t\tbreak\n+\t\tfi &&\n+\t\ttest_tick &&\n+\t\tgit notes edit -d \"$sha1\"\n+\tdone\n+'\n+\n+test_expect_success 'most notes deleted correctly with git-notes' '\n+\tgit log HEAD~250 | grep \"^    \" > output &&\n+\ti=50 &&\n+\twhile test $i -gt 0\n+\tdo\n+\t\techo \"    commit #$i\" &&\n+\t\techo \"    note #$i\" &&\n+\t\ti=$(($i - 1));\n+\tdone > expect &&\n+\ttest_cmp expect output\n+'\n+\n+test_expect_success 'deleting most notes triggers fanout consolidation' '\n+\t# Expect entire notes tree to have a fanout == 0\n+\tgit ls-tree -r --name-only refs/notes/commits |\n+\twhile read path\n+\tdo\n+\t\tcase \"$path\" in\n+\t\t????????????????????????????????????????)\n+\t\t\t: true\n+\t\t\t;;\n+\t\t*)\n+\t\t\techo \"Invalid path \\\"$path\\\"\" &&\n+\t\t\treturn 1\n+\t\t\t;;\n+\t\tesac\n+\tdone\n+'\n+\n test_done\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131987","messageId":"1263762277-31419-20-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 19/20] Notes API: gc_notes(): Prune notes that belong to non-existing objects","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:36Z","receivedAt":"2010-01-17T21:04:36Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"When an object is garbage collected by Git, any notes that annotate that\nobject is not automatically garbage-collected, since all notes are trivially\nreachable from a notes ref. In order to remove notes for non-existing objects,\nwe therefore need to add functionality for traversing the notes tree and\nexplicitly removing references to notes that annotate non-reachable objects.\nThus the notes objects themselves also become unreachable, and are removed\nby a later GC.\n\ngc_notes() performs this traversal (by using for_each_note() internally), and\nremoves the notes in question from the notes tree.\n\nNote that the effect of gc_notes() is not persistent unless a subsequent call\nto write_notes_tree() is made.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c |   39 +++++++++++++++++++++++++++++++++++++++\n notes.h |   12 ++++++++++++\n 2 files changed, 51 insertions(+), 0 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 4f260a5..880a306 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -747,6 +747,29 @@ static int write_each_note(const unsigned char *object_sha1,\n \t\twrite_each_note_helper(d->root, note_path, mode, note_sha1);\n }\n \n+struct note_delete_list {\n+\tstruct note_delete_list *next;\n+\tconst unsigned char *sha1;\n+};\n+\n+static int gc_notes_helper(const unsigned char *object_sha1,\n+\t\tconst unsigned char *note_sha1, char *note_path,\n+\t\tvoid *cb_data)\n+{\n+\tstruct note_delete_list **l = (struct note_delete_list **) cb_data;\n+\tstruct note_delete_list *n;\n+\n+\tif (has_sha1_file(object_sha1))\n+\t\treturn 0; /* nothing to do for this note */\n+\n+\t/* failed to find object => GC this note */\n+\tn = (struct note_delete_list *) xmalloc(sizeof(*n));\n+\tn->next = *l;\n+\tn->sha1 = object_sha1;\n+\t*l = n;\n+\treturn 0;\n+}\n+\n int combine_notes_concatenate(unsigned char *cur_sha1,\n \t\tconst unsigned char *new_sha1)\n {\n@@ -920,6 +943,22 @@ int write_notes_tree(struct notes_tree *t, unsigned char *result)\n \treturn ret;\n }\n \n+void gc_notes(struct notes_tree *t)\n+{\n+\tstruct note_delete_list *l = NULL;\n+\n+\tif (!t)\n+\t\tt = &default_notes_tree;\n+\tassert(t->initialized);\n+\n+\tfor_each_note(t, 0, gc_notes_helper, &l);\n+\n+\twhile (l) {\n+\t\tremove_note(t, l->sha1);\n+\t\tl = l->next;\n+\t}\n+}\n+\n void free_notes(struct notes_tree *t)\n {\n \tif (!t)\ndiff --git a/notes.h b/notes.h\nindex f25643e..71d3fab 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -158,6 +158,18 @@ int for_each_note(struct notes_tree *t, int flags, each_note_fn fn,\n int write_notes_tree(struct notes_tree *t, unsigned char *result);\n \n /*\n+ * Remove all notes annotating non-existing objects from the given notes tree\n+ *\n+ * All notes in the given notes_tree that are associated with objects that no\n+ * longer exist in the database, are removed from the notes tree.\n+ *\n+ * IMPORTANT: The changes made by gc_notes() to the given notes_tree structure\n+ * are not persistent until a subsequent call to write_notes_tree() returns\n+ * zero.\n+ */\n+void gc_notes(struct notes_tree *t);\n+\n+/*\n  * Free (and de-initialize) the given notes_tree structure\n  *\n  * IMPORTANT: Changes made to the given notes_tree since the last, successful\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"131985","messageId":"1263762277-31419-21-git-send-email-johan@herland.net","threadId":"22262","inReplyTo":"1263762277-31419-1-git-send-email-johan@herland.net","subject":"[PATCHv11 20/20] builtin-gc: Teach the new --notes option to garbage-collect notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-17T21:04:37Z","receivedAt":"2010-01-17T21:04:37Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"The new --notes option triggers a call to gc_notes(), which removes note\nobjects that reference non-existing objects.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n Documentation/git-gc.txt |    5 ++-\n builtin-gc.c             |   13 ++++++\n t/t3306-notes-gc.sh      |  106 ++++++++++++++++++++++++++++++++++++++++++++++\n 3 files changed, 123 insertions(+), 1 deletions(-)\n create mode 100755 t/t3306-notes-gc.sh\n\ndiff --git a/Documentation/git-gc.txt b/Documentation/git-gc.txt\nindex 4cd9cdf..b3d1107 100644\n--- a/Documentation/git-gc.txt\n+++ b/Documentation/git-gc.txt\n@@ -8,7 +8,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository\n \n SYNOPSIS\n --------\n-'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]\n+'git gc' [--aggressive] [--auto] [--quiet] [--notes] [--prune=<date> | --no-prune]\n \n DESCRIPTION\n -----------\n@@ -70,6 +70,9 @@ automatic consolidation of packs.\n --quiet::\n \tSuppress all progress reports.\n \n+--notes::\n+\tPrune note objects where the referenced object no longer exists.\n+\n Configuration\n -------------\n \ndiff --git a/builtin-gc.c b/builtin-gc.c\nindex 093517e..b051bc8 100644\n--- a/builtin-gc.c\n+++ b/builtin-gc.c\n@@ -12,6 +12,7 @@\n \n #include \"builtin.h\"\n #include \"cache.h\"\n+#include \"notes.h\"\n #include \"parse-options.h\"\n #include \"run-command.h\"\n \n@@ -177,7 +178,9 @@ int cmd_gc(int argc, const char **argv, const char *prefix)\n \tint aggressive = 0;\n \tint auto_gc = 0;\n \tint quiet = 0;\n+\tint notes = 0;\n \tchar buf[80];\n+\tstruct notes_tree *t;\n \n \tstruct option builtin_gc_options[] = {\n \t\t{ OPTION_STRING, 0, \"prune\", &prune_expire, \"date\",\n@@ -186,6 +189,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)\n \t\tOPT_BOOLEAN(0, \"aggressive\", &aggressive, \"be more thorough (increased runtime)\"),\n \t\tOPT_BOOLEAN(0, \"auto\", &auto_gc, \"enable auto-gc mode\"),\n \t\tOPT_BOOLEAN('q', \"quiet\", &quiet, \"suppress progress reports\"),\n+\t\tOPT_BOOLEAN(0, \"notes\", &notes, \"delete orphaned note objects\"),\n \t\tOPT_END()\n \t};\n \n@@ -235,6 +239,15 @@ int cmd_gc(int argc, const char **argv, const char *prefix)\n \tif (run_command_v_opt(argv_reflog, RUN_GIT_CMD))\n \t\treturn error(FAILED_RUN, argv_reflog[0]);\n \n+\tif (notes) {\n+\t\tinit_notes(NULL, NULL, NULL, 0);\n+\t\tt = &default_notes_tree;\n+\t\tgc_notes(t);\n+\t\tif (commit_notes(t, \"Notes tree cleaned up by 'git gc --notes'\"))\n+\t\t\tdie(\"Failed to garbage-collect notes tree @ '%s'\", t->ref);\n+\t\tfree_notes(t);\n+\t}\n+\n \tif (run_command_v_opt(argv_repack, RUN_GIT_CMD))\n \t\treturn error(FAILED_RUN, argv_repack[0]);\n \ndiff --git a/t/t3306-notes-gc.sh b/t/t3306-notes-gc.sh\nnew file mode 100755\nindex 0000000..1d8233d\n--- /dev/null\n+++ b/t/t3306-notes-gc.sh\n@@ -0,0 +1,106 @@\n+#!/bin/sh\n+\n+test_description='Test git gc --notes'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup: create a few commits with notes' '\n+\n+\t: > file1 &&\n+\tgit add file1 &&\n+\ttest_tick &&\n+\tgit commit -m 1st &&\n+\tgit notes edit -m \"Note #1\" &&\n+\t: > file2 &&\n+\tgit add file2 &&\n+\ttest_tick &&\n+\tgit commit -m 2nd &&\n+\tgit notes edit -m \"Note #2\" &&\n+\t: > file3 &&\n+\tgit add file3 &&\n+\ttest_tick &&\n+\tgit commit -m 3rd &&\n+\tgit notes edit -m \"Note #3\"\n+'\n+\n+cat > expect <<END_OF_LOG\n+commit 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29\n+Author: A U Thor <author@example.com>\n+Date:   Thu Apr 7 15:15:13 2005 -0700\n+\n+    3rd\n+\n+Notes:\n+    Note #3\n+\n+commit 08341ad9e94faa089d60fd3f523affb25c6da189\n+Author: A U Thor <author@example.com>\n+Date:   Thu Apr 7 15:14:13 2005 -0700\n+\n+    2nd\n+\n+Notes:\n+    Note #2\n+\n+commit ab5f302035f2e7aaf04265f08b42034c23256e1f\n+Author: A U Thor <author@example.com>\n+Date:   Thu Apr 7 15:13:13 2005 -0700\n+\n+    1st\n+\n+Notes:\n+    Note #1\n+END_OF_LOG\n+\n+test_expect_success 'verify commits and notes' '\n+\n+\tgit log > actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'remove some commits' '\n+\n+\tgit reset --hard HEAD~2 &&\n+\tgit reflog expire --expire=now HEAD &&\n+\tgit gc --prune=now\n+'\n+\n+test_expect_success 'verify that commits are gone' '\n+\n+\t! git cat-file -p 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&\n+\t! git cat-file -p 08341ad9e94faa089d60fd3f523affb25c6da189 &&\n+\tgit cat-file -p ab5f302035f2e7aaf04265f08b42034c23256e1f\n+'\n+\n+test_expect_success 'verify that notes are still present' '\n+\n+\tgit notes show 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&\n+\tgit notes show 08341ad9e94faa089d60fd3f523affb25c6da189 &&\n+\tgit notes show ab5f302035f2e7aaf04265f08b42034c23256e1f\n+'\n+\n+test_expect_success 'garbage-collect (without --notes)' '\n+\n+\tgit gc --prune=now --aggressive\n+'\n+\n+test_expect_success 'verify that notes are still present' '\n+\n+\tgit notes show 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&\n+\tgit notes show 08341ad9e94faa089d60fd3f523affb25c6da189 &&\n+\tgit notes show ab5f302035f2e7aaf04265f08b42034c23256e1f\n+'\n+\n+test_expect_success 'garbage-collect notes' '\n+\n+\tgit gc --notes --prune=now --aggressive\n+'\n+\n+test_expect_success 'verify that notes are gone' '\n+\n+\t! git notes show 5ee1c35e83ea47cd3cc4f8cbee0568915fbbbd29 &&\n+\t! git notes show 08341ad9e94faa089d60fd3f523affb25c6da189 &&\n+\tgit notes show ab5f302035f2e7aaf04265f08b42034c23256e1f\n+'\n+\n+test_done\n-- \n1.6.6.rc1.321.g0496e\n"},{"id":"132325","messageId":"780e0a6b1001211028t35a19e2la6bb92f8ba94a689@mail.gmail.com","threadId":"22262","inReplyTo":"1263762277-31419-13-git-send-email-johan@herland.net","subject":"Re: [PATCHv11 12/20] Builtin-ify git-notes","fromName":"Stephen Boyd","fromEmail":"bebarino@gmail.com","sentAt":"2010-01-21T18:28:33Z","receivedAt":"2010-01-21T18:28:33Z","isPatch":false,"sender":{"key":"bebarino@gmail.com","avatar":"https://avatars.githubusercontent.com/u/38832?v=4"},"body":"On Sun, Jan 17, 2010 at 1:04 PM, Johan Herland <johan@herland.net> wrote:\n> +\n> +               if (launch_editor(path, buf, NULL)) {\n> +                       fprintf(stderr, \"Please supply the note contents using\"\n> +                               \" either -m or -F option.\\n\");\n> +                       exit(1);\n\ndie()?\n"},{"id":"132327","messageId":"780e0a6b1001211127u6304546ej4e5bffbceca12e0b@mail.gmail.com","threadId":"22262","inReplyTo":"1263762277-31419-21-git-send-email-johan@herland.net","subject":"Re: [PATCHv11 20/20] builtin-gc: Teach the new --notes option to garbage-collect notes","fromName":"Stephen Boyd","fromEmail":"bebarino@gmail.com","sentAt":"2010-01-21T19:27:27Z","receivedAt":"2010-01-21T19:27:27Z","isPatch":false,"sender":{"key":"bebarino@gmail.com","avatar":"https://avatars.githubusercontent.com/u/38832?v=4"},"body":"On Sun, Jan 17, 2010 at 1:04 PM, Johan Herland <johan@herland.net> wrote:\n> The new --notes option triggers a call to gc_notes(), which removes note\n> objects that reference non-existing objects.\n>\n\nShouldn't notes be unconditionally garbage collected? Or maybe there's\na reason why notes should be treated differently that isn't written\nhere.\n"},{"id":"132329","messageId":"7vtyufp6r9.fsf@alter.siamese.dyndns.org","threadId":"22262","inReplyTo":"780e0a6b1001211127u6304546ej4e5bffbceca12e0b@mail.gmail.com","subject":"Re: [PATCHv11 20/20] builtin-gc: Teach the new --notes option to garbage-collect notes","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2010-01-21T20:01:14Z","receivedAt":"2010-01-21T20:01:14Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Stephen Boyd <bebarino@gmail.com> writes:\n\n> On Sun, Jan 17, 2010 at 1:04 PM, Johan Herland <johan@herland.net> wrote:\n>> The new --notes option triggers a call to gc_notes(), which removes note\n>> objects that reference non-existing objects.\n>>\n>\n> Shouldn't notes be unconditionally garbage collected? Or maybe there's\n> a reason why notes should be treated differently that isn't written\n> here.\n\nA few thoughts, inspired by this patch, but on other things around\n\"notes\":\n\n - This is more about pruning notes regarding objects that are no longer\n   availabe from _the tip_ of the notes tree.  It doesn't run (and I am\n   not suggesting to make it it to run) filter-branch to eradicate all\n   such stale notes from the notes commit ancestry, so in that sense this\n   is not really a garbage collection.\n\n - We would want to have \"notes prune\" subcommand that lets us do the\n   pruning without all the other \"gc\" operation.  \"git gc\" would of course\n   call the same underlying code \"notes prune\" would use if we want to\n   be able to trigger \"notes prune\" from it.\n\n - Because there is no public interface to list objects that are annotated\n   with the most recent notes tree, the only thing this pruning affects is\n   the look-up overhead of \"log --show-notes\".  As such, it might be\n   better to later add tree rebalancing in \"notes prune\" and at that\n   point, it will become even less about garbage collection and more about\n   performance (\"notes optimize?\").\n\n - Do we want to have a public interface to list objects that are\n   annotated with notes?  \"git notes\" probably deserves a bit more\n   subcommands other than \"(edit|show)\" to help users, e.g. \"list\" and\n   \"remove\".\n\n - If this were \"notes optimize\" (this is just me thinking aloud), perhaps\n   we would want to do this at some key places (e.g. when you\n   automatically rebalance the tree).\n"},{"id":"132662","messageId":"201001260309.15363.johan@herland.net","threadId":"22262","inReplyTo":"780e0a6b1001211028t35a19e2la6bb92f8ba94a689@mail.gmail.com","subject":"Re: [PATCHv11 12/20] Builtin-ify git-notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-26T02:09:15Z","receivedAt":"2010-01-26T02:09:15Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Thursday 21 January 2010, Stephen Boyd wrote:\n> On Sun, Jan 17, 2010 at 1:04 PM, Johan Herland <johan@herland.net> wrote:\n> > +\n> > +               if (launch_editor(path, buf, NULL)) {\n> > +                       fprintf(stderr, \"Please supply the note\n> > contents using\" +                               \" either -m or -F\n> > option.\\n\"); +                       exit(1);\n> \n> die()?\n\nYes, thanks. Will be part of the next iteration.\n\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"132795","messageId":"201001271302.17368.johan@herland.net","threadId":"22262","inReplyTo":"7vtyufp6r9.fsf@alter.siamese.dyndns.org","subject":"Re: [PATCHv11 20/20] builtin-gc: Teach the new --notes option to garbage-collect notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2010-01-27T12:02:17Z","receivedAt":"2010-01-27T12:02:17Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Thursday 21 January 2010, Junio C Hamano wrote:\n> Stephen Boyd <bebarino@gmail.com> writes:\n> > On Sun, Jan 17, 2010 at 1:04 PM, Johan Herland wrote:\n> >> The new --notes option triggers a call to gc_notes(), which removes\n> >> note objects that reference non-existing objects.\n> >\n> > Shouldn't notes be unconditionally garbage collected? Or maybe there's\n> > a reason why notes should be treated differently that isn't written\n> > here.\n> \n> A few thoughts, inspired by this patch, but on other things around\n> \"notes\":\n> \n>  - This is more about pruning notes regarding objects that are no longer\n>    availabe from _the tip_ of the notes tree.  It doesn't run (and I am\n>    not suggesting to make it it to run) filter-branch to eradicate all\n>    such stale notes from the notes commit ancestry, so in that sense this\n>    is not really a garbage collection.\n\nI've removed this patch from the next iteration of the series...\n\n>  - We would want to have \"notes prune\" subcommand that lets us do the\n>    pruning without all the other \"gc\" operation.  \"git gc\" would of\n>  course call the same underlying code \"notes prune\" would use if we want\n>  to be able to trigger \"notes prune\" from it.\n\n...and replaced it with \"git notes prune\".\n\n>  - Because there is no public interface to list objects that are\n>  annotated with the most recent notes tree, the only thing this pruning\n>  affects is the look-up overhead of \"log --show-notes\".  As such, it\n>  might be better to later add tree rebalancing in \"notes prune\" and at\n>  that point, it will become even less about garbage collection and more\n>  about performance (\"notes optimize?\").\n> \n>  - Do we want to have a public interface to list objects that are\n>    annotated with notes?  \"git notes\" probably deserves a bit more\n>    subcommands other than \"(edit|show)\" to help users, e.g. \"list\" and\n>    \"remove\".\n\nAdded in the next iteration of the series.\n\n>  - If this were \"notes optimize\" (this is just me thinking aloud),\n>  perhaps we would want to do this at some key places (e.g. when you\n>    automatically rebalance the tree).\n\nThere is currently no explicit rebalancing of the tree. The only thing \"git \nnotes prune\" does, is call remove_note() for all notes referencing \nunreachable objects. Instead the tree is always kept in \"balance\" by \nremove_note() calling note_tree_consolidate() for the part of the tree \naffected by the removed note.\n\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"}]}