{"thread":{"id":"21817","subject":"[RFC/PATCHv9 00/11] git notes","startedAt":"2009-12-02T02:09:32Z","lastAt":"2009-12-03T03:45:55Z","messageCount":15,"participants":["Johan Herland","Shawn O. Pearce"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"128950","messageId":"1259719783-4674-1-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":null,"subject":"[RFC/PATCHv9 00/11] git notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:32Z","receivedAt":"2009-12-02T02:09:32Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"Hi,\n\nHere is the 9th iteration of the git-notes series. Changes in this\niteration are as follows:\n\nChanges to existing patches:\n\n- Rebased back onto the early part of the jh/notes series, as\n  suggested by Junio.\n\n- Minor style fixes to some of the patches.\n\n- The git-fast-import patch has been heavily rewritten after\n  suggestions from Shawn. The new patch is much less intrusive,\n  by way of NOT interfacing with the notes API, but doing the\n  fanout strategy directly in fast-import.c instead.\n\n- The new fast-import patch no longer depends on changes in the\n  notes API. The fast-import-related patches are therefore moved\n  to the head of the patch series, and the notes API changes that\n  are no longer necessary (but still worthwhile, IMHO) are moved\n  to the tail.\n\nIf Shawn is OK with the fast-import patch, I believe that at least\npatches #1 - #3 (and possibly #4 - #5) are ready for 'next'.\n\nPatches #6 - #11 drastically extend the notes API. Since there are\ncurrently no users of that API, and it has not been discussed much\non the list (although these patches have already been present in a\ncouple of iterations), I would still consider them RFC quality.\n\n\nTODO:\n- Builtin-ify git-notes shell script to take advantage of notes API\n- Garbage collect notes whose referenced object is unreachable (gc_notes())\n- Handle note objects that are not blobs, but trees\n\n\nHave fun! :)\n\n...Johan\n\n\nJohan Herland (11):\n  fast-import: Proper notes tree manipulation\n  Rename t9301 to t9350, to make room for more fast-import tests\n  Add more testcases to test fast-import of notes\n  Minor style fixes to notes.c\n  Notes API: get_commit_notes() -> format_note() + remove the commit restriction\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: 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: Allow multiple concurrent notes trees with new struct notes_tree\n  Refactor notes concatenation into a flexible interface for combining notes\n\n fast-import.c                                    |  141 +++++-\n notes.c                                          |  345 ++++++++++----\n notes.h                                          |  114 ++++-\n pretty.c                                         |    9 +-\n t/t9300-fast-import.sh                           |  156 ++++++-\n t/t9301-fast-import-notes.sh                     |  578 ++++++++++++++++++++++\n t/{t9301-fast-export.sh => t9350-fast-export.sh} |    0\n 7 files changed, 1218 insertions(+), 125 deletions(-)\n create mode 100755 t/t9301-fast-import-notes.sh\n rename t/{t9301-fast-export.sh => t9350-fast-export.sh} (100%)\n"},{"id":"128952","messageId":"1259719783-4674-2-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 01/11] fast-import: Proper notes tree manipulation","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:33Z","receivedAt":"2009-12-02T02:09:33Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"This patch teaches 'git fast-import' to automatically organize note objects\nin a fast-import stream into an appropriate fanout structure. The notes API\nin notes.h is NOT used to accomplish this, because trying to keep the\nfast-import and notes data structures in sync would yield a significantly\nlarger patch with higher complexity.\n\nNote objects are added to the fast-import tree structure with special mode\nbits set, so that they can be recognized and restructured on-demand. The\nspecial mode bits are ignored when generating the containing tree object,\nhence the special mode bits are never visible externally.\n\nThe code keeps track of the number of note objects per branch through a\nsimple counter, and if/when the number of notes warrant a different fanout\nlevel, the branch tree is traversed, renaming note objects into the\nlocation dictated by the new fanout level.\n\nSince note objects are stored in the same tree structure as other objects,\nthe unloading and reloading of a fast-import branches handle note objects\ntransparently.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n\nOn Thursday 26 November 2009, Shawn O. Pearce wrote:\n> Yea, I agree, I'm not happy with the amount of complex code added\n> to implement this.  But I can't say there's a better way to do it\n> and still reuse the notes code.  Maybe its just worth breaking away\n> from the notes code altogether?  fast-import also implements its\n> own pack formatting functions because reusing them from pack-objects\n> was just too ugly.\n\nOk, here is the promised rewrite that does it all within fast-import\ninstead of reusing the notes code. It's much smaller, both in size,\nand in the impact on the existing code. I'm certainly a lot happier\nwith this patch, than with the previous iteration.\n\n\nHave fun! :)\n\n...Johan\n\n\n fast-import.c          |  141 +++++++++++++++++++++++++++++++++++++++++---\n t/t9300-fast-import.sh |  156 ++++++++++++++++++++++++++++++++++++++++++++----\n 2 files changed, 276 insertions(+), 21 deletions(-)\n\ndiff --git a/fast-import.c b/fast-import.c\nindex b41d29f..b51ffbc 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -161,6 +161,7 @@ Format of STDIN stream:\n #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)\n #define DEPTH_BITS 13\n #define MAX_DEPTH ((1<<DEPTH_BITS)-1)\n+#define NOTE_MODE 0170000\n\n struct object_entry\n {\n@@ -245,6 +246,7 @@ struct branch\n \tconst char *name;\n \tstruct tree_entry branch_tree;\n \tuintmax_t last_commit;\n+\tunsigned int num_notes;\n \tunsigned active : 1;\n \tunsigned pack_id : PACK_ID_BITS;\n \tunsigned char sha1[20];\n@@ -693,6 +695,7 @@ static struct branch *new_branch(const char *name)\n \tb->table_next_branch = branch_table[hc];\n \tb->branch_tree.versions[0].mode = S_IFDIR;\n \tb->branch_tree.versions[1].mode = S_IFDIR;\n+\tb->num_notes = 0;\n \tb->active = 0;\n \tb->pack_id = MAX_PACK_ID;\n \tbranch_table[hc] = b;\n@@ -1306,10 +1309,12 @@ static void mktree(struct tree_content *t, int v, struct strbuf *b)\n \tstrbuf_grow(b, maxlen);\n \tfor (i = 0; i < t->entry_count; i++) {\n \t\tstruct tree_entry *e = t->entries[i];\n-\t\tif (!e->versions[v].mode)\n+\t\tunsigned int mode = (unsigned int) e->versions[v].mode;\n+\t\tif (!mode)\n \t\t\tcontinue;\n-\t\tstrbuf_addf(b, \"%o %s%c\", (unsigned int)e->versions[v].mode,\n-\t\t\t\t\te->name->str_dat, '\\0');\n+\t\telse if ((mode & NOTE_MODE) == NOTE_MODE)\n+\t\t\tmode = (mode & ~NOTE_MODE) | S_IFREG;\n+\t\tstrbuf_addf(b, \"%o %s%c\", mode, e->name->str_dat, '\\0');\n \t\tstrbuf_add(b, e->versions[v].sha1, 20);\n \t}\n }\n@@ -1860,6 +1865,115 @@ static void load_branch(struct branch *b)\n \t}\n }\n\n+static unsigned char convert_num_notes_to_fanout(unsigned int num_notes)\n+{\n+\tunsigned char fanout = 0;\n+\twhile ((num_notes >>= 8))\n+\t\tfanout++;\n+\treturn fanout;\n+}\n+\n+static void construct_path_with_fanout(const char *hex_sha1,\n+\t\tunsigned char fanout, char *path)\n+{\n+\tunsigned int i = 0, j = 0;\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+\tmemcpy(path + i, hex_sha1 + j, 40 - j);\n+\tpath[i + 40 - j] = '\\0';\n+}\n+\n+static int adjust_num_notes(struct tree_entry *root, const char *p,\n+\t\tconst unsigned char *sha1)\n+{\n+\t/* Return -1/0/1 indicating how storing sha1 at p affects #notes */\n+\tstruct tree_entry leaf;\n+\tint delete_note = is_null_sha1(sha1) ? 1 : 0;\n+\tint nonexisting_note = !(\n+\t\ttree_content_get(root, p, &leaf) &&\n+\t\t!is_null_sha1(leaf.versions[1].sha1) &&\n+\t\t(leaf.versions[1].mode & NOTE_MODE) == NOTE_MODE) ? 1 : 0;\n+\treturn nonexisting_note - delete_note;\n+}\n+\n+static unsigned int do_change_note_fanout(\n+\t\tstruct tree_entry *orig_root, struct tree_entry *root,\n+\t\tchar *hex_sha1, unsigned int hex_sha1_len,\n+\t\tchar *fullpath, unsigned int fullpath_len,\n+\t\tunsigned char fanout)\n+{\n+\tstruct tree_content *t = root->tree;\n+\tstruct tree_entry *e, leaf;\n+\tunsigned int i, tmp_hex_sha1_len, tmp_fullpath_len, num_notes = 0;\n+\tunsigned char sha1[20];\n+\tchar realpath[60];\n+\tint is_note;\n+\n+\tfor (i = 0; i < t->entry_count; i++) {\n+\t\te = t->entries[i];\n+\t\tis_note = (e->versions[1].mode & NOTE_MODE) == NOTE_MODE;\n+\t\ttmp_hex_sha1_len = hex_sha1_len + e->name->str_len;\n+\t\ttmp_fullpath_len = fullpath_len;\n+\n+\t\tif (tmp_hex_sha1_len <= 40 && e->name->str_len >= 2) {\n+\t\t\tmemcpy(hex_sha1 + hex_sha1_len, e->name->str_dat,\n+\t\t\t\te->name->str_len);\n+\t\t\tif (tmp_fullpath_len)\n+\t\t\t\tfullpath[tmp_fullpath_len++] = '/';\n+\t\t\tmemcpy(fullpath + tmp_fullpath_len, e->name->str_dat,\n+\t\t\t\te->name->str_len);\n+\t\t\ttmp_fullpath_len += e->name->str_len;\n+\t\t\tassert(tmp_fullpath_len < 60);\n+\t\t\tfullpath[tmp_fullpath_len] = '\\0';\n+\t\t} else {\n+\t\t\tassert(!is_note);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (is_note) {\n+\t\t\tnum_notes++;\n+\t\t\tassert(tmp_hex_sha1_len == 40);\n+\t\t\tif (get_sha1_hex(hex_sha1, sha1))\n+\t\t\t\tdie(\"Invalid SHA1 sum %.40s\", hex_sha1);\n+\t\t\tconstruct_path_with_fanout(hex_sha1, fanout, realpath);\n+\t\t\tif (!strcmp(fullpath, realpath))\n+\t\t\t\tcontinue; /* note is already in right place */\n+\n+\t\t\t/* Rename fullpath to realpath */\n+\t\t\tif (!tree_content_remove(orig_root, fullpath, &leaf))\n+\t\t\t\tdie(\"Failed to remove path %s\", fullpath);\n+\t\t\tif (!leaf.versions[1].mode)\n+\t\t\t\tdie(\"Path %s not in branch\", fullpath);\n+\t\t\ttree_content_set(orig_root, realpath,\n+\t\t\t\tleaf.versions[1].sha1,\n+\t\t\t\tleaf.versions[1].mode,\n+\t\t\t\tleaf.tree);\n+\t\t} else if (tmp_hex_sha1_len < 40 && S_ISDIR(e->versions[1].mode)) {\n+\t\t\t/* Found a subdir that may contain a note */\n+\t\t\tnum_notes += do_change_note_fanout(orig_root, e,\n+\t\t\t\thex_sha1, tmp_hex_sha1_len,\n+\t\t\t\tfullpath, tmp_fullpath_len, fanout);\n+\t\t}\n+\n+\t\t/* The above may have reallocated the current tree_content */\n+\t\tif (t != root->tree)\n+\t\t\tt = root->tree;\n+\t}\n+\treturn num_notes;\n+}\n+\n+static unsigned int change_note_fanout(struct tree_entry *root,\n+\t\tunsigned char fanout)\n+{\n+\tchar hex_sha1[40], path[60];\n+\treturn do_change_note_fanout(root, root, hex_sha1, 0, path, 0, fanout);\n+}\n+\n static void file_change_m(struct branch *b)\n {\n \tconst char *p = command_buf.buf + 2;\n@@ -2010,14 +2124,15 @@ static void file_change_cr(struct branch *b, int rename)\n \t\tleaf.tree);\n }\n\n-static void note_change_n(struct branch *b)\n+static void note_change_n(struct branch *b, unsigned char fanout)\n {\n \tconst char *p = command_buf.buf + 2;\n \tstatic struct strbuf uq = STRBUF_INIT;\n \tstruct object_entry *oe = oe;\n \tstruct branch *s;\n \tunsigned char sha1[20], commit_sha1[20];\n-\tuint16_t inline_data = 0;\n+\tchar path[60];\n+\tuint16_t inline_data = 0, mode;\n\n \t/* <dataref> or 'inline' */\n \tif (*p == ':') {\n@@ -2080,8 +2195,10 @@ static void note_change_n(struct branch *b)\n \t\t\t    typename(type), command_buf.buf);\n \t}\n\n-\ttree_content_set(&b->branch_tree, sha1_to_hex(commit_sha1), sha1,\n-\t\tS_IFREG | 0644, NULL);\n+\tconstruct_path_with_fanout(sha1_to_hex(commit_sha1), fanout, path);\n+\tb->num_notes += adjust_num_notes(&b->branch_tree, path, sha1);\n+\tmode = (is_null_sha1(sha1) ? S_IFREG : NOTE_MODE) | 0644;\n+\ttree_content_set(&b->branch_tree, path, sha1, mode, NULL);\n }\n\n static void file_change_deleteall(struct branch *b)\n@@ -2090,6 +2207,7 @@ static void file_change_deleteall(struct branch *b)\n \thashclr(b->branch_tree.versions[0].sha1);\n \thashclr(b->branch_tree.versions[1].sha1);\n \tload_tree(&b->branch_tree);\n+\tb->num_notes = 0;\n }\n\n static void parse_from_commit(struct branch *b, char *buf, unsigned long size)\n@@ -2213,6 +2331,7 @@ static void parse_new_commit(void)\n \tchar *committer = NULL;\n \tstruct hash_list *merge_list = NULL;\n \tunsigned int merge_count;\n+\tunsigned char prev_fanout, new_fanout;\n\n \t/* Obtain the branch name from the rest of our command */\n \tsp = strchr(command_buf.buf, ' ') + 1;\n@@ -2243,6 +2362,8 @@ static void parse_new_commit(void)\n \t\tload_branch(b);\n \t}\n\n+\tprev_fanout = convert_num_notes_to_fanout(b->num_notes);\n+\n \t/* file_change* */\n \twhile (command_buf.len > 0) {\n \t\tif (!prefixcmp(command_buf.buf, \"M \"))\n@@ -2254,7 +2375,7 @@ static void parse_new_commit(void)\n \t\telse if (!prefixcmp(command_buf.buf, \"C \"))\n \t\t\tfile_change_cr(b, 0);\n \t\telse if (!prefixcmp(command_buf.buf, \"N \"))\n-\t\t\tnote_change_n(b);\n+\t\t\tnote_change_n(b, prev_fanout);\n \t\telse if (!strcmp(\"deleteall\", command_buf.buf))\n \t\t\tfile_change_deleteall(b);\n \t\telse {\n@@ -2265,6 +2386,10 @@ static void parse_new_commit(void)\n \t\t\tbreak;\n \t}\n\n+\tnew_fanout = convert_num_notes_to_fanout(b->num_notes);\n+\tif (new_fanout != prev_fanout)\n+\t\tb->num_notes = change_note_fanout(&b->branch_tree, new_fanout);\n+\n \t/* build the tree and the commit */\n \tstore_tree(&b->branch_tree);\n \thashcpy(b->branch_tree.versions[0].sha1,\ndiff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh\nindex b49815d..bf8c509 100755\n--- a/t/t9300-fast-import.sh\n+++ b/t/t9300-fast-import.sh\n@@ -1092,9 +1092,12 @@ test_expect_success 'P: fail on blob mark in gitlink' '\n ### series Q (notes)\n ###\n\n-note1_data=\"Note for the first commit\"\n-note2_data=\"Note for the second commit\"\n-note3_data=\"Note for the third commit\"\n+note1_data=\"The first note for the first commit\"\n+note2_data=\"The first note for the second commit\"\n+note3_data=\"The first note for the third commit\"\n+note1b_data=\"The second note for the first commit\"\n+note1c_data=\"The third note for the first commit\"\n+note2b_data=\"The second note for the second commit\"\n\n test_tick\n cat >input <<INPUT_END\n@@ -1169,7 +1172,45 @@ data <<EOF\n $note3_data\n EOF\n\n+commit refs/notes/foobar\n+mark :10\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+notes (:10)\n+COMMIT\n+\n+N inline :3\n+data <<EOF\n+$note1b_data\n+EOF\n+\n+commit refs/notes/foobar2\n+mark :11\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+notes (:11)\n+COMMIT\n+\n+N inline :3\n+data <<EOF\n+$note1c_data\n+EOF\n+\n+commit refs/notes/foobar\n+mark :12\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+notes (:12)\n+COMMIT\n+\n+deleteall\n+N inline :5\n+data <<EOF\n+$note2b_data\n+EOF\n+\n INPUT_END\n+\n test_expect_success \\\n \t'Q: commit notes' \\\n \t'git fast-import <input &&\n@@ -1224,8 +1265,8 @@ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n notes (:9)\n EOF\n test_expect_success \\\n-\t'Q: verify notes commit' \\\n-\t'git cat-file commit refs/notes/foobar | sed 1d >actual &&\n+\t'Q: verify first notes commit' \\\n+\t'git cat-file commit refs/notes/foobar~2 | sed 1d >actual &&\n \ttest_cmp expect actual'\n\n cat >expect.unsorted <<EOF\n@@ -1235,23 +1276,112 @@ cat >expect.unsorted <<EOF\n EOF\n cat expect.unsorted | sort >expect\n test_expect_success \\\n-\t'Q: verify notes tree' \\\n-\t'git cat-file -p refs/notes/foobar^{tree} | sed \"s/ [0-9a-f]*\t/ /\" >actual &&\n+\t'Q: verify first notes tree' \\\n+\t'git cat-file -p refs/notes/foobar~2^{tree} | sed \"s/ [0-9a-f]*\t/ /\" >actual &&\n \t test_cmp expect actual'\n\n echo \"$note1_data\" >expect\n test_expect_success \\\n-\t'Q: verify note for first commit' \\\n-\t'git cat-file blob refs/notes/foobar:$commit1 >actual && test_cmp expect actual'\n+\t'Q: verify first note for first commit' \\\n+\t'git cat-file blob refs/notes/foobar~2:$commit1 >actual && test_cmp expect actual'\n\n echo \"$note2_data\" >expect\n test_expect_success \\\n-\t'Q: verify note for second commit' \\\n-\t'git cat-file blob refs/notes/foobar:$commit2 >actual && test_cmp expect actual'\n+\t'Q: verify first note for second commit' \\\n+\t'git cat-file blob refs/notes/foobar~2:$commit2 >actual && test_cmp expect actual'\n+\n+echo \"$note3_data\" >expect\n+test_expect_success \\\n+\t'Q: verify first note for third commit' \\\n+\t'git cat-file blob refs/notes/foobar~2:$commit3 >actual && test_cmp expect actual'\n+\n+cat >expect <<EOF\n+parent `git rev-parse --verify refs/notes/foobar~2`\n+author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+\n+notes (:10)\n+EOF\n+test_expect_success \\\n+\t'Q: verify second notes commit' \\\n+\t'git cat-file commit refs/notes/foobar^ | sed 1d >actual &&\n+\ttest_cmp expect actual'\n+\n+cat >expect.unsorted <<EOF\n+100644 blob $commit1\n+100644 blob $commit2\n+100644 blob $commit3\n+EOF\n+cat expect.unsorted | sort >expect\n+test_expect_success \\\n+\t'Q: verify second notes tree' \\\n+\t'git cat-file -p refs/notes/foobar^^{tree} | sed \"s/ [0-9a-f]*\t/ /\" >actual &&\n+\t test_cmp expect actual'\n+\n+echo \"$note1b_data\" >expect\n+test_expect_success \\\n+\t'Q: verify second note for first commit' \\\n+\t'git cat-file blob refs/notes/foobar^:$commit1 >actual && test_cmp expect actual'\n+\n+echo \"$note2_data\" >expect\n+test_expect_success \\\n+\t'Q: verify first note for second commit' \\\n+\t'git cat-file blob refs/notes/foobar^:$commit2 >actual && test_cmp expect actual'\n\n echo \"$note3_data\" >expect\n test_expect_success \\\n-\t'Q: verify note for third commit' \\\n-\t'git cat-file blob refs/notes/foobar:$commit3 >actual && test_cmp expect actual'\n+\t'Q: verify first note for third commit' \\\n+\t'git cat-file blob refs/notes/foobar^:$commit3 >actual && test_cmp expect actual'\n+\n+cat >expect <<EOF\n+author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+\n+notes (:11)\n+EOF\n+test_expect_success \\\n+\t'Q: verify third notes commit' \\\n+\t'git cat-file commit refs/notes/foobar2 | sed 1d >actual &&\n+\ttest_cmp expect actual'\n+\n+cat >expect.unsorted <<EOF\n+100644 blob $commit1\n+EOF\n+cat expect.unsorted | sort >expect\n+test_expect_success \\\n+\t'Q: verify third notes tree' \\\n+\t'git cat-file -p refs/notes/foobar2^{tree} | sed \"s/ [0-9a-f]*\t/ /\" >actual &&\n+\t test_cmp expect actual'\n+\n+echo \"$note1c_data\" >expect\n+test_expect_success \\\n+\t'Q: verify third note for first commit' \\\n+\t'git cat-file blob refs/notes/foobar2:$commit1 >actual && test_cmp expect actual'\n+\n+cat >expect <<EOF\n+parent `git rev-parse --verify refs/notes/foobar^`\n+author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+\n+notes (:12)\n+EOF\n+test_expect_success \\\n+\t'Q: verify fourth notes commit' \\\n+\t'git cat-file commit refs/notes/foobar | sed 1d >actual &&\n+\ttest_cmp expect actual'\n+\n+cat >expect.unsorted <<EOF\n+100644 blob $commit2\n+EOF\n+cat expect.unsorted | sort >expect\n+test_expect_success \\\n+\t'Q: verify fourth notes tree' \\\n+\t'git cat-file -p refs/notes/foobar^{tree} | sed \"s/ [0-9a-f]*\t/ /\" >actual &&\n+\t test_cmp expect actual'\n+\n+echo \"$note2b_data\" >expect\n+test_expect_success \\\n+\t'Q: verify second note for second commit' \\\n+\t'git cat-file blob refs/notes/foobar:$commit2 >actual && test_cmp expect actual'\n\n test_done\n--\n1.6.5.3.433.g11067\n"},{"id":"128951","messageId":"1259719783-4674-3-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 02/11] Rename t9301 to t9350, to make room for more fast-import tests","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:34Z","receivedAt":"2009-12-02T02:09:34Z","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/{t9301-fast-export.sh => t9350-fast-export.sh} |    0\n 1 files changed, 0 insertions(+), 0 deletions(-)\n rename t/{t9301-fast-export.sh => t9350-fast-export.sh} (100%)\n\ndiff --git a/t/t9301-fast-export.sh b/t/t9350-fast-export.sh\nsimilarity index 100%\nrename from t/t9301-fast-export.sh\nrename to t/t9350-fast-export.sh\n-- \n1.6.5.3.433.g11067\n"},{"id":"128954","messageId":"1259719783-4674-4-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 03/11] Add more testcases to test fast-import of notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:35Z","receivedAt":"2009-12-02T02:09:35Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"This patch adds testcases verifying correct behaviour in several scenarios\nregarding fast-import of notes:\n- using a mixture of 'N' and 'M' commands\n- updating existing notes\n- concatenation of notes\n- 'deleteall' also removes notes\n- fanout schemes is added/removed when needed\n- git-fast-import's branch unload/reload preserves notes\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n t/t9301-fast-import-notes.sh |  578 ++++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 578 insertions(+), 0 deletions(-)\n create mode 100755 t/t9301-fast-import-notes.sh\n\ndiff --git a/t/t9301-fast-import-notes.sh b/t/t9301-fast-import-notes.sh\nnew file mode 100755\nindex 0000000..5a08a76\n--- /dev/null\n+++ b/t/t9301-fast-import-notes.sh\n@@ -0,0 +1,578 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2009 Johan Herland\n+#\n+\n+test_description='test git fast-import of notes objects'\n+. ./test-lib.sh\n+\n+\n+test_tick\n+cat >input <<INPUT_END\n+commit refs/heads/master\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+first commit\n+COMMIT\n+\n+M 644 inline foo\n+data <<EOF\n+file foo in first commit\n+EOF\n+\n+M 755 inline bar\n+data <<EOF\n+file bar in first commit\n+EOF\n+\n+M 644 inline baz/xyzzy\n+data <<EOF\n+file baz/xyzzy in first commit\n+EOF\n+\n+commit refs/heads/master\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+second commit\n+COMMIT\n+\n+M 644 inline foo\n+data <<EOF\n+file foo in second commit\n+EOF\n+\n+M 755 inline baz/xyzzy\n+data <<EOF\n+file baz/xyzzy in second commit\n+EOF\n+\n+commit refs/heads/master\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+third commit\n+COMMIT\n+\n+M 644 inline foo\n+data <<EOF\n+file foo in third commit\n+EOF\n+\n+commit refs/heads/master\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+fourth commit\n+COMMIT\n+\n+M 755 inline bar\n+data <<EOF\n+file bar in fourth commit\n+EOF\n+\n+INPUT_END\n+\n+test_expect_success 'set up master branch' '\n+\n+\tgit fast-import <input &&\n+\tgit whatchanged master\n+'\n+\n+commit4=$(git rev-parse refs/heads/master)\n+commit3=$(git rev-parse \"$commit4^\")\n+commit2=$(git rev-parse \"$commit4~2\")\n+commit1=$(git rev-parse \"$commit4~3\")\n+\n+test_tick\n+cat >input <<INPUT_END\n+commit refs/notes/test\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+first notes commit\n+COMMIT\n+\n+M 644 inline $commit1\n+data <<EOF\n+first note for first commit\n+EOF\n+\n+M 755 inline $commit2\n+data <<EOF\n+first note for second commit\n+EOF\n+\n+INPUT_END\n+\n+cat >expect <<EXPECT_END\n+    fourth commit\n+    third commit\n+    second commit\n+    first note for second commit\n+    first commit\n+    first note for first commit\n+EXPECT_END\n+\n+test_expect_success 'add notes with simple M command' '\n+\n+\tgit fast-import <input &&\n+\tGIT_NOTES_REF=refs/notes/test git log | grep \"^    \" > actual &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+test_tick\n+cat >input <<INPUT_END\n+commit refs/notes/test\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+second notes commit\n+COMMIT\n+\n+from refs/notes/test^0\n+N inline $commit3\n+data <<EOF\n+first note for third commit\n+EOF\n+\n+N inline $commit4\n+data <<EOF\n+first note for fourth commit\n+EOF\n+\n+INPUT_END\n+\n+cat >expect <<EXPECT_END\n+    fourth commit\n+    first note for fourth commit\n+    third commit\n+    first note for third commit\n+    second commit\n+    first note for second commit\n+    first commit\n+    first note for first commit\n+EXPECT_END\n+\n+test_expect_success 'add notes with simple N command' '\n+\n+\tgit fast-import <input &&\n+\tGIT_NOTES_REF=refs/notes/test git log | grep \"^    \" > actual &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+test_tick\n+cat >input <<INPUT_END\n+commit refs/notes/test\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+third notes commit\n+COMMIT\n+\n+from refs/notes/test^0\n+N inline $commit1\n+data <<EOF\n+second note for first commit\n+EOF\n+\n+N inline $commit2\n+data <<EOF\n+second note for second commit\n+EOF\n+\n+N inline $commit3\n+data <<EOF\n+second note for third commit\n+EOF\n+\n+N inline $commit4\n+data <<EOF\n+second note for fourth commit\n+EOF\n+\n+INPUT_END\n+\n+cat >expect <<EXPECT_END\n+    fourth commit\n+    second note for fourth commit\n+    third commit\n+    second note for third commit\n+    second commit\n+    second note for second commit\n+    first commit\n+    second note for first commit\n+EXPECT_END\n+\n+test_expect_success 'update existing notes with N command' '\n+\n+\tgit fast-import <input &&\n+\tGIT_NOTES_REF=refs/notes/test git log | grep \"^    \" > actual &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+test_tick\n+cat >input <<INPUT_END\n+commit refs/notes/test\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+fourth notes commit\n+COMMIT\n+\n+from refs/notes/test^0\n+M 644 inline $(echo \"$commit3\" | sed \"s|^..|&/|\")\n+data <<EOF\n+prefix of note for third commit\n+EOF\n+\n+M 644 inline $(echo \"$commit4\" | sed \"s|^..|&/|\")\n+data <<EOF\n+prefix of note for fourth commit\n+EOF\n+\n+M 644 inline $(echo \"$commit4\" | sed \"s|^\\(..\\)\\(..\\)|\\1/\\2/|\")\n+data <<EOF\n+pre-prefix of note for fourth commit\n+EOF\n+\n+N inline $commit1\n+data <<EOF\n+third note for first commit\n+EOF\n+\n+N inline $commit2\n+data <<EOF\n+third note for second commit\n+EOF\n+\n+N inline $commit3\n+data <<EOF\n+third note for third commit\n+EOF\n+\n+N inline $commit4\n+data <<EOF\n+third note for fourth commit\n+EOF\n+\n+\n+INPUT_END\n+\n+cat >expect <<EXPECT_END\n+    fourth commit\n+    pre-prefix of note for fourth commit\n+    prefix of note for fourth commit\n+    third note for fourth commit\n+    third commit\n+    prefix of note for third commit\n+    third note for third commit\n+    second commit\n+    third note for second commit\n+    first commit\n+    third note for first commit\n+EXPECT_END\n+\n+test_expect_success 'add concatentation notes with M command' '\n+\n+\tgit fast-import <input &&\n+\tGIT_NOTES_REF=refs/notes/test git log | grep \"^    \" > actual &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+test_tick\n+cat >input <<INPUT_END\n+commit refs/notes/test\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+fifth notes commit\n+COMMIT\n+\n+from refs/notes/test^0\n+deleteall\n+\n+INPUT_END\n+\n+cat >expect <<EXPECT_END\n+    fourth commit\n+    third commit\n+    second commit\n+    first commit\n+EXPECT_END\n+\n+test_expect_success 'verify that deleteall also removes notes' '\n+\n+\tgit fast-import <input &&\n+\tGIT_NOTES_REF=refs/notes/test git log | grep \"^    \" > actual &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+test_tick\n+cat >input <<INPUT_END\n+commit refs/notes/test\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+sixth notes commit\n+COMMIT\n+\n+from refs/notes/test^0\n+M 644 inline $commit1\n+data <<EOF\n+third note for first commit\n+EOF\n+\n+M 644 inline $commit3\n+data <<EOF\n+third note for third commit\n+EOF\n+\n+N inline $commit1\n+data <<EOF\n+fourth note for first commit\n+EOF\n+\n+N inline $commit3\n+data <<EOF\n+fourth note for third commit\n+EOF\n+\n+INPUT_END\n+\n+cat >expect <<EXPECT_END\n+    fourth commit\n+    third commit\n+    fourth note for third commit\n+    second commit\n+    first commit\n+    fourth note for first commit\n+EXPECT_END\n+\n+test_expect_success 'verify that N commands override M commands' '\n+\n+\tgit fast-import <input &&\n+\tGIT_NOTES_REF=refs/notes/test git log | grep \"^    \" > actual &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+# Write fast-import commands to create the given number of commits\n+fast_import_commits () {\n+\tmy_ref=$1\n+\tmy_num_commits=$2\n+\tmy_append_to_file=$3\n+\tmy_i=0\n+\twhile test $my_i -lt $my_num_commits\n+\tdo\n+\t\tmy_i=$(($my_i + 1))\n+\t\ttest_tick\n+\t\tcat >>\"$my_append_to_file\" <<INPUT_END\n+commit $my_ref\n+mark :$my_i\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+commit #$my_i\n+COMMIT\n+\n+M 644 inline file\n+data <<EOF\n+file contents in commit #$my_i\n+EOF\n+\n+INPUT_END\n+\tdone\n+}\n+\n+# Write fast-import commands to create the given number of notes annotating\n+# the commits created by fast_import_commits()\n+fast_import_notes () {\n+\tmy_notes_ref=$1\n+\tmy_num_commits=$2\n+\tmy_append_to_file=$3\n+\tmy_note_append=$4\n+\ttest_tick\n+\tcat >>\"$my_append_to_file\" <<INPUT_END\n+commit $my_notes_ref\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+committing $my_num_commits notes\n+COMMIT\n+\n+INPUT_END\n+\n+\tmy_i=0\n+\twhile test $my_i -lt $my_num_commits\n+\tdo\n+\t\tmy_i=$(($my_i + 1))\n+\t\tcat >>\"$my_append_to_file\" <<INPUT_END\n+N inline :$my_i\n+data <<EOF\n+note for commit #$my_i$my_note_append\n+EOF\n+\n+INPUT_END\n+\tdone\n+}\n+\n+\n+rm input expect\n+num_commits=400\n+# Create lots of commits\n+fast_import_commits \"refs/heads/many_commits\" $num_commits input\n+# Create one note per above commit\n+fast_import_notes \"refs/notes/many_notes\" $num_commits input\n+# Finally create the expected output from all these notes and commits\n+i=$num_commits\n+while test $i -gt 0\n+do\n+\tcat >>expect <<EXPECT_END\n+    commit #$i\n+    note for commit #$i\n+EXPECT_END\n+\ti=$(($i - 1))\n+done\n+\n+test_expect_success 'add lots of commits and notes' '\n+\n+\tgit fast-import <input &&\n+\tGIT_NOTES_REF=refs/notes/many_notes git log refs/heads/many_commits |\n+\t    grep \"^    \" > actual &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+test_expect_success 'verify that lots of notes trigger a fanout scheme' '\n+\n+\t# None of the entries in the top-level notes tree should be a full SHA1\n+\tgit cat-file -p refs/notes/many_notes: |\n+\twhile read mode type sha1 path\n+\tdo\n+\t\tpath_len=$(expr length \"$path\") &&\n+\t\tif test $path_len -ge 40\n+\t\tthen\n+\t\t\treturn 1\n+\t\tfi\n+\tdone\n+\n+'\n+\n+commit1=$(git rev-parse \"refs/heads/many_commits\")\n+commit2=$(git rev-parse \"$commit1^\")\n+commit3=$(git rev-parse \"$commit2^\")\n+commit4=$(git rev-parse \"$commit3^\")\n+\n+test_tick\n+cat >input <<INPUT_END\n+commit refs/notes/many_notes\n+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE\n+data <<COMMIT\n+replacing many notes with few notes\n+COMMIT\n+\n+from refs/notes/many_notes^0\n+deleteall\n+N inline $commit1\n+data <<EOF\n+note for commit #$num_commits\n+EOF\n+\n+N inline $commit2\n+data <<EOF\n+note for commit #$(($num_commits - 1))\n+EOF\n+\n+N inline $commit3\n+data <<EOF\n+note for commit #$(($num_commits - 2))\n+EOF\n+\n+N inline $commit4\n+data <<EOF\n+note for commit #$(($num_commits - 3))\n+EOF\n+\n+INPUT_END\n+\n+i=$num_commits\n+rm expect\n+while test $i -gt 0\n+do\n+\tcat >>expect <<EXPECT_END\n+    commit #$i\n+EXPECT_END\n+\tif test $i -gt $(($num_commits - 4))\n+\tthen\n+\t\tcat >>expect <<EXPECT_END\n+    note for commit #$i\n+EXPECT_END\n+\tfi\n+\ti=$(($i - 1))\n+done\n+\n+test_expect_success 'remove lots of notes' '\n+\n+\tgit fast-import <input &&\n+\tGIT_NOTES_REF=refs/notes/many_notes git log refs/heads/many_commits |\n+\t    grep \"^    \" > actual &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+test_expect_success 'verify that removing notes trigger fanout consolidation' '\n+\n+\t# All entries in the top-level notes tree should be a full SHA1\n+\tgit cat-file -p refs/notes/many_notes: |\n+\twhile read mode type sha1 path\n+\tdo\n+\t\tpath_len=$(expr length \"$path\") &&\n+\t\tif test $path_len -ne 40\n+\t\tthen\n+\t\t\treturn 1\n+\t\tfi\n+\tdone\n+\n+'\n+\n+rm input expect\n+num_notes_refs=10\n+num_commits=16\n+some_commits=8\n+# Create commits\n+fast_import_commits \"refs/heads/more_commits\" $num_commits input\n+# Create one note per above commit per notes ref\n+i=0\n+while test $i -lt $num_notes_refs\n+do\n+\ti=$(($i + 1))\n+\tfast_import_notes \"refs/notes/more_notes_$i\" $num_commits input\n+done\n+# Trigger branch reloading in git-fast-import by repeating the note creation\n+i=0\n+while test $i -lt $num_notes_refs\n+do\n+\ti=$(($i + 1))\n+\tfast_import_notes \"refs/notes/more_notes_$i\" $some_commits input \" (2)\"\n+done\n+# Finally create the expected output from the notes in refs/notes/more_notes_1\n+i=$num_commits\n+while test $i -gt 0\n+do\n+\tnote_data=\"note for commit #$i\"\n+\tif test $i -le $some_commits\n+\tthen\n+\t\tnote_data=\"$note_data (2)\"\n+\tfi\n+\tcat >>expect <<EXPECT_END\n+    commit #$i\n+    $note_data\n+EXPECT_END\n+\ti=$(($i - 1))\n+done\n+\n+test_expect_success \"add notes to $num_commits commits in each of $num_notes_refs refs\" '\n+\n+\tgit fast-import --active-branches=5 <input &&\n+\tGIT_NOTES_REF=refs/notes/more_notes_1 git log refs/heads/more_commits |\n+\t    grep \"^    \" > actual &&\n+\ttest_cmp expect actual\n+\n+'\n+\n+test_done\n-- \n1.6.5.3.433.g11067\n"},{"id":"128960","messageId":"1259719783-4674-5-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 04/11] Minor style fixes to notes.c","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:36Z","receivedAt":"2009-12-02T02:09:36Z","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 |    6 +++---\n 1 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 50a4672..812aeb9 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -93,7 +93,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 +195,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 +257,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-- \n1.6.5.3.433.g11067\n"},{"id":"128959","messageId":"1259719783-4674-6-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 05/11] Notes API: get_commit_notes() -> format_note() + remove the commit restriction","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:37Z","receivedAt":"2009-12-02T02:09:37Z","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 812aeb9..dddca31 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 \"refs.h\"\n #include \"utf8.h\"\n@@ -25,10 +24,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@@ -211,7 +210,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@@ -299,7 +298,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@@ -312,23 +311,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\tl->key_sha1[19] = (unsigned char) len;\n@@ -342,12 +341,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@@ -355,9 +354,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@@ -370,7 +369,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@@ -389,7 +388,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 7f350bb..81791f5 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -703,8 +703,8 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,\n \t\tformat_decoration(sb, commit);\n \t\treturn 1;\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@@ -982,8 +982,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.5.3.433.g11067\n"},{"id":"128955","messageId":"1259719783-4674-7-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 06/11] Notes API: init_notes(): Initialize the notes tree from the given notes ref","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:38Z","receivedAt":"2009-12-02T02:09:38Z","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 |   27 ++++++++++++++++-----------\n notes.h |   20 ++++++++++++++++++++\n 2 files changed, 36 insertions(+), 11 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex dddca31..23e62dd 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -339,13 +339,25 @@ 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+\tassert(!initialized);\n+\tinitialized = 1;\n+\n+\tif (!notes_ref) {\n+\t\tconst char *env = getenv(GIT_NOTES_REF_ENVIRONMENT);\n+\t\tif (env)\n+\t\t\tnotes_ref = getenv(GIT_NOTES_REF_ENVIRONMENT);\n+\t\telse\n+\t\t\tnotes_ref = GIT_NOTES_DEFAULT_REF;\n+\t}\n+\n+\tif (flags & NOTES_INIT_EMPTY || !notes_ref ||\n+\t    read_ref(notes_ref, object_sha1) ||\n \t    get_tree_entry(object_sha1, \"\", sha1, &mode))\n \t\treturn;\n \n@@ -378,15 +390,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.5.3.433.g11067\n"},{"id":"128953","messageId":"1259719783-4674-8-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 07/11] Notes API: add_note(): Add note objects to the internal notes tree structure","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:39Z","receivedAt":"2009-12-02T02:09:39Z","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 23e62dd..3c8a6e0 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -366,6 +366,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.5.3.433.g11067\n"},{"id":"128956","messageId":"1259719783-4674-9-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 08/11] Notes API: get_note(): Return the note annotating the given object","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:40Z","receivedAt":"2009-12-02T02:09:40Z","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 3c8a6e0..6a36ff9 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -377,12 +377,13 @@ 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-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@@ -396,7 +397,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@@ -404,7 +405,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 5f22852..21a8930 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+/* 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.5.3.433.g11067\n"},{"id":"128957","messageId":"1259719783-4674-10-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 09/11] Notes API: for_each_note(): Traverse the entire notes tree with a callback","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:41Z","receivedAt":"2009-12-02T02:09:41Z","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 |  101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n notes.h |   17 ++++++++++\n 2 files changed, 118 insertions(+), 0 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 6a36ff9..0d8ff2c 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -339,6 +339,101 @@ 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 any of them are subtree entries, then\n+\t * there are likely plenty of notes below this level, so we return an\n+\t * incremented fanout immediately. Otherwise, we return an incremented\n+\t * fanout only if all of the entries at this level are int_nodes.\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\t\treturn fanout + 1;\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, each_note_fn fn, void *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(\n+\t\t\t\tCLR_PTR_TYPE(p), n + 1, fanout, fn, cb_data);\n+\t\t\tbreak;\n+\t\tcase PTR_TYPE_SUBTREE:\n+\t\t\t/* unpack subtree and resume traversal */\n+\t\t\tl = (struct leaf_node *) CLR_PTR_TYPE(p);\n+\t\t\ttree->a[i] = NULL;\n+\t\t\tload_subtree(l, tree, n);\n+\t\t\tfree(l);\n+\t\t\tgoto redo;\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@@ -386,6 +481,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(each_note_fn fn, void *cb_data)\n+{\n+\tassert(initialized);\n+\treturn for_each_note_helper(&root_node, 0, 0, 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 21a8930..f67bae8 100644\n--- a/notes.h\n+++ b/notes.h\n@@ -28,6 +28,23 @@ void add_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+ * 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().\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+ * - free_notes()\n+ */\n+typedef int each_note_fn(const unsigned char *object_sha1,\n+\t\tconst unsigned char *note_sha1, const char *note_tree_path,\n+\t\tvoid *cb_data);\n+int for_each_note(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.5.3.433.g11067\n"},{"id":"128958","messageId":"1259719783-4674-11-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 10/11] Notes API: Allow multiple concurrent notes trees with new struct notes_tree","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:42Z","receivedAt":"2009-12-02T02:09:42Z","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 interface with several different notes\ntrees simultaneously.\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, a fallback \"default\" notes\ntree (declared in notes.c) is used.\n\nSigned-off-by: Johan Herland <johan@herland.net>\n---\n notes.c  |   67 ++++++++++++++++++++++++++++++++++++++-----------------------\n notes.h  |   53 +++++++++++++++++++++++++++++++++++-------------\n pretty.c |    7 +++--\n 3 files changed, 84 insertions(+), 43 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 0d8ff2c..3f96ee6 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+static struct notes_tree default_tree;\n \n static void load_subtree(struct leaf_node *subtree, struct int_node *node,\n \t\tunsigned int n);\n@@ -434,14 +432,15 @@ redo:\n \treturn 0;\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_tree;\n+\tassert(!t->initialized);\n \n \tif (!notes_ref) {\n \t\tconst char *env = getenv(GIT_NOTES_REF_ENVIRONMENT);\n@@ -451,6 +450,10 @@ void init_notes(const char *notes_ref, int flags)\n \t\t\tnotes_ref = GIT_NOTES_DEFAULT_REF;\n \t}\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    get_tree_entry(object_sha1, \"\", sha1, &mode))\n@@ -458,44 +461,56 @@ 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_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-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_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(each_note_fn fn, void *cb_data)\n+int for_each_note(struct notes_tree *t, each_note_fn fn, void *cb_data)\n {\n-\tassert(initialized);\n-\treturn for_each_note_helper(&root_node, 0, 0, fn, cb_data);\n+\tif (!t)\n+\t\tt = &default_tree;\n+\tassert(t->initialized);\n+\treturn for_each_note_helper(t->root, 0, 0, fn, cb_data);\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_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@@ -503,10 +518,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_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 f67bae8..ea1235f 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+struct notes_tree {\n+\tstruct int_node *root;\n+\tchar *ref;\n+\tint initialized;\n+};\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,26 +25,32 @@\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-/* Add the given note object to the internal notes tree structure */\n-void add_note(const unsigned char *object_sha1,\n+/* Add the given note object to the given notes_tree structure */\n+void add_note(struct notes_tree *t, const unsigned char *object_sha1,\n \t\tconst unsigned char *note_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- * 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().\n@@ -43,10 +64,10 @@ 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, const char *note_tree_path,\n \t\tvoid *cb_data);\n-int for_each_note(each_note_fn fn, void *cb_data);\n+int for_each_note(struct notes_tree *t, each_note_fn fn, void *cb_data);\n \n-/* Free (and de-initialize) the internal notes tree structure */\n-void free_notes(void);\n+/* Free (and de-initialize) the give notes_tree structure */\n+void free_notes(struct notes_tree *t);\n \n /* Flags controlling how notes are formatted */\n #define NOTES_SHOW_HEADER 1\n@@ -55,12 +76,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 81791f5..047dbb8 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -703,8 +703,9 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,\n \t\tformat_decoration(sb, commit);\n \t\treturn 1;\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@@ -982,7 +983,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.5.3.433.g11067\n"},{"id":"128961","messageId":"1259719783-4674-12-git-send-email-johan@herland.net","threadId":"21817","inReplyTo":"1259719783-4674-1-git-send-email-johan@herland.net","subject":"[RFC/PATCHv9 11/11] Refactor notes concatenation into a flexible interface for combining notes","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T02:09:43Z","receivedAt":"2009-12-02T02:09:43Z","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 |  135 ++++++++++++++++++++++++++++++++++++---------------------------\n notes.h |   34 +++++++++++++++-\n 2 files changed, 109 insertions(+), 60 deletions(-)\n\ndiff --git a/notes.c b/notes.c\nindex 3f96ee6..14747bd 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -127,55 +127,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 +141,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 +163,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 +190,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 +200,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 /* Free the entire notes data contained in the given tree */\n@@ -331,7 +288,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\tcombine_notes_concatenate);\n \t\t}\n \t}\n \tfree(buf);\n@@ -432,7 +390,62 @@ redo:\n \treturn 0;\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, *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+\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+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@@ -450,8 +463,12 @@ void init_notes(struct notes_tree *t, const char *notes_ref, int flags)\n \t\t\tnotes_ref = GIT_NOTES_DEFAULT_REF;\n \t}\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@@ -465,17 +482,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_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 const unsigned char *get_note(struct notes_tree *t,\n@@ -521,7 +540,7 @@ void format_note(struct notes_tree *t, const unsigned char *object_sha1,\n \tif (!t)\n \t\tt = &default_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 ea1235f..047a282 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 are guaranteed to be non-NULL and different.\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 struct notes_tree {\n \tstruct int_node *root;\n \tchar *ref;\n+\tcombine_notes_fn *combine_notes;\n \tint initialized;\n };\n \n@@ -36,14 +61,19 @@ 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 /* Add the given note object to the given notes_tree structure */\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 /* Get the note object SHA1 containing the note data for the given object */\n const unsigned char *get_note(struct notes_tree *t,\n-- \n1.6.5.3.433.g11067\n"},{"id":"129063","messageId":"20091202203953.GE31648@spearce.org","threadId":"21817","inReplyTo":"1259719783-4674-2-git-send-email-johan@herland.net","subject":"Re: [RFC/PATCHv9 01/11] fast-import: Proper notes tree manipulation","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2009-12-02T20:39:53Z","receivedAt":"2009-12-02T20:39:53Z","isPatch":false,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"Johan Herland <johan@herland.net> wrote:\n> diff --git a/fast-import.c b/fast-import.c\n> index b41d29f..b51ffbc 100644\n> --- a/fast-import.c\n> +++ b/fast-import.c\n\nThis new version is much cleaner, thank you.\n\n> @@ -161,6 +161,7 @@ Format of STDIN stream:\n>  #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)\n>  #define DEPTH_BITS 13\n>  #define MAX_DEPTH ((1<<DEPTH_BITS)-1)\n> +#define NOTE_MODE 0170000\n\nBe consistent with S_IFGITLINK:\n\n#define S_IFNOTE 0170000\n#define S_ISNOTE(m) (((m) & S_IFMT) == S_IFNOTE)\n \n> @@ -245,6 +246,7 @@ struct branch\n>  \tconst char *name;\n>  \tstruct tree_entry branch_tree;\n>  \tuintmax_t last_commit;\n> +\tunsigned int num_notes;\n\nUse uintmax_t here?\n\n> +static unsigned int do_change_note_fanout(\n> +\t\tstruct tree_entry *orig_root, struct tree_entry *root,\n> +\t\tchar *hex_sha1, unsigned int hex_sha1_len,\n> +\t\tchar *fullpath, unsigned int fullpath_len,\n> +\t\tunsigned char fanout)\n> +{\n> +\tstruct tree_content *t = root->tree;\n> +\tstruct tree_entry *e, leaf;\n> +\tunsigned int i, tmp_hex_sha1_len, tmp_fullpath_len, num_notes = 0;\n> +\tunsigned char sha1[20];\n> +\tchar realpath[60];\n> +\tint is_note;\n> +\n> +\tfor (i = 0; i < t->entry_count; i++) {\n> +\t\te = t->entries[i];\n> +\t\tis_note = (e->versions[1].mode & NOTE_MODE) == NOTE_MODE;\n> +\t\ttmp_hex_sha1_len = hex_sha1_len + e->name->str_len;\n> +\t\ttmp_fullpath_len = fullpath_len;\n> +\n> +\t\tif (tmp_hex_sha1_len <= 40 && e->name->str_len >= 2) {\n> +\t\t\tmemcpy(hex_sha1 + hex_sha1_len, e->name->str_dat,\n> +\t\t\t\te->name->str_len);\n> +\t\t\tif (tmp_fullpath_len)\n> +\t\t\t\tfullpath[tmp_fullpath_len++] = '/';\n> +\t\t\tmemcpy(fullpath + tmp_fullpath_len, e->name->str_dat,\n> +\t\t\t\te->name->str_len);\n> +\t\t\ttmp_fullpath_len += e->name->str_len;\n> +\t\t\tassert(tmp_fullpath_len < 60);\n> +\t\t\tfullpath[tmp_fullpath_len] = '\\0';\n> +\t\t} else {\n> +\t\t\tassert(!is_note);\n> +\t\t\tcontinue;\n> +\t\t}\n\nAre we rejecting a mixed content-tree here?  I thought a notes\ntree was allowed to hold anything, e.g. isn't it ok to put a\n\".gitattributes\" file into a notes tree.\n\nI think we'd do better to have at the top of our loop:\n\n\tif (!is_note && !S_ISDIR(e->versions[1].mode))\n\t\tcontinue;\n\nso that we ignore non-notes and non-subdirectories which might\ncontain notes.\n\nAlso, fast-import never uses assert.  I'd prefer to die() because\nthen the recent command trace can go into the crash report.\nIt gives the user more context about what the hell just went wrong.\n\n> +\t\t/* The above may have reallocated the current tree_content */\n> +\t\tif (t != root->tree)\n> +\t\t\tt = root->tree;\n\nWhy bother with the condition?  Just do the assignment every time\nin the loop.\n\n> @@ -2080,8 +2195,10 @@ static void note_change_n(struct branch *b)\n>  \t\t\t    typename(type), command_buf.buf);\n>  \t}\n> \n> -\ttree_content_set(&b->branch_tree, sha1_to_hex(commit_sha1), sha1,\n> -\t\tS_IFREG | 0644, NULL);\n> +\tconstruct_path_with_fanout(sha1_to_hex(commit_sha1), fanout, path);\n> +\tb->num_notes += adjust_num_notes(&b->branch_tree, path, sha1);\n> +\tmode = (is_null_sha1(sha1) ? S_IFREG : NOTE_MODE) | 0644;\n> +\ttree_content_set(&b->branch_tree, path, sha1, mode, NULL);\n\nI wonder if it wouldn't be better to compute the fan out here on\neach put.  That way if an importer drives 2,000,000 notes at once\nto us in a single commit, we don't wind up with a flat 0 fan-out\ntree and trying to perform a linear insert on each one, but instead\nwill start to increase the fan out as the number of entries goes up.\n\nBasically, tree_content_set/remove are O(N) operations on N paths\nin the tree, because their structures aren't necessarily sorted.\nIIRC at one point in time I tried to do this with a binary search but\ngave up and just did it unsorted.  At least using the fan out here\nwould help partition the search space dramatically on large inserts.\n\n-- \nShawn.\n"},{"id":"129080","messageId":"200912022341.55474.johan@herland.net","threadId":"21817","inReplyTo":"20091202203953.GE31648@spearce.org","subject":"Re: [RFC/PATCHv9 01/11] fast-import: Proper notes tree manipulation","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-02T22:41:55Z","receivedAt":"2009-12-02T22:41:55Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"On Wednesday 02 December 2009, Shawn O. Pearce wrote:\n> Johan Herland <johan@herland.net> wrote:\n> > diff --git a/fast-import.c b/fast-import.c\n> > index b41d29f..b51ffbc 100644\n> > --- a/fast-import.c\n> > +++ b/fast-import.c\n> \n> This new version is much cleaner, thank you.\n\nThank you for the suggestions that made it so :)\n\nThe rest of your comments all make perfect sense. I'll whip up a replacement \npatch shortly.\n\n\nThanks for the review!\n\n...Johan\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"},{"id":"129095","messageId":"200912030445.55732.johan@herland.net","threadId":"21817","inReplyTo":"20091202203953.GE31648@spearce.org","subject":"Re: [RFC/PATCHv9 01/11] fast-import: Proper notes tree manipulation","fromName":"Johan Herland","fromEmail":"johan@herland.net","sentAt":"2009-12-03T03:45:55Z","receivedAt":"2009-12-03T03:45:55Z","isPatch":false,"sender":{"key":"johan@herland.net","avatar":"https://avatars.githubusercontent.com/u/547031?v=4"},"body":"(Oops. I forgot to answer a couple of your questions...)\n\nOn Wednesday 02 December 2009, Shawn O. Pearce wrote:\n> Johan Herland <johan@herland.net> wrote:\n> > +static unsigned int do_change_note_fanout(\n> > +\t\tstruct tree_entry *orig_root, struct tree_entry *root,\n> > +\t\tchar *hex_sha1, unsigned int hex_sha1_len,\n> > +\t\tchar *fullpath, unsigned int fullpath_len,\n> > +\t\tunsigned char fanout)\n> > +{\n> > +\tstruct tree_content *t = root->tree;\n> > +\tstruct tree_entry *e, leaf;\n> > +\tunsigned int i, tmp_hex_sha1_len, tmp_fullpath_len, num_notes = 0;\n> > +\tunsigned char sha1[20];\n> > +\tchar realpath[60];\n> > +\tint is_note;\n> > +\n> > +\tfor (i = 0; i < t->entry_count; i++) {\n> > +\t\te = t->entries[i];\n> > +\t\tis_note = (e->versions[1].mode & NOTE_MODE) == NOTE_MODE;\n> > +\t\ttmp_hex_sha1_len = hex_sha1_len + e->name->str_len;\n> > +\t\ttmp_fullpath_len = fullpath_len;\n> > +\n> > +\t\tif (tmp_hex_sha1_len <= 40 && e->name->str_len >= 2) {\n> > +\t\t\tmemcpy(hex_sha1 + hex_sha1_len, e->name->str_dat,\n> > +\t\t\t\te->name->str_len);\n> > +\t\t\tif (tmp_fullpath_len)\n> > +\t\t\t\tfullpath[tmp_fullpath_len++] = '/';\n> > +\t\t\tmemcpy(fullpath + tmp_fullpath_len, e->name->str_dat,\n> > +\t\t\t\te->name->str_len);\n> > +\t\t\ttmp_fullpath_len += e->name->str_len;\n> > +\t\t\tassert(tmp_fullpath_len < 60);\n> > +\t\t\tfullpath[tmp_fullpath_len] = '\\0';\n> > +\t\t} else {\n> > +\t\t\tassert(!is_note);\n> > +\t\t\tcontinue;\n> > +\t\t}\n> \n> Are we rejecting a mixed content-tree here?  I thought a notes\n> tree was allowed to hold anything, e.g. isn't it ok to put a\n> \".gitattributes\" file into a notes tree.\n> \n> I think we'd do better to have at the top of our loop:\n> \n> \tif (!is_note && !S_ISDIR(e->versions[1].mode))\n> \t\tcontinue;\n> \n> so that we ignore non-notes and non-subdirectories which might\n> contain notes.\n\nAFAICS, the current code does not reject \".gitattributes\", but instead \nsimply ignores it presence (i.e. does not change its fanout). However, it \ncertainly does some unnecessary work (setting up hex_sha1 and fullpath) \nwhich is ignored in the bottom part of the loop (where it fails both the \"if \n(is_note)\" and the following \"else if\").\n\nHowever, while adding a test to verify the handling of non-notes, I came \nacross an unrelated crasher in the notes.c code. Will send a fix for this \nASAP.\n\nIn any case, I think your proposed if-condition is better, and I will \nrewrite the code accordingly.\n\n> > @@ -2080,8 +2195,10 @@ static void note_change_n(struct branch *b)\n> >  \t\t\t    typename(type), command_buf.buf);\n> >  \t}\n> >\n> > -\ttree_content_set(&b->branch_tree, sha1_to_hex(commit_sha1), sha1,\n> > -\t\tS_IFREG | 0644, NULL);\n> > +\tconstruct_path_with_fanout(sha1_to_hex(commit_sha1), fanout, path);\n> > +\tb->num_notes += adjust_num_notes(&b->branch_tree, path, sha1);\n> > +\tmode = (is_null_sha1(sha1) ? S_IFREG : NOTE_MODE) | 0644;\n> > +\ttree_content_set(&b->branch_tree, path, sha1, mode, NULL);\n> \n> I wonder if it wouldn't be better to compute the fan out here on\n> each put.  That way if an importer drives 2,000,000 notes at once\n> to us in a single commit, we don't wind up with a flat 0 fan-out\n> tree and trying to perform a linear insert on each one, but instead\n> will start to increase the fan out as the number of entries goes up.\n> \n> Basically, tree_content_set/remove are O(N) operations on N paths\n> in the tree, because their structures aren't necessarily sorted.\n> IIRC at one point in time I tried to do this with a binary search but\n> gave up and just did it unsorted.  At least using the fan out here\n> would help partition the search space dramatically on large inserts.\n\nThis is a really good idea, but it's a bit more complicated than that:\nAn 'N' command may not only add new notes, but also rewrite existing notes, \nand when rewriting an existing note we must take care to _replace_ the old \nnote, and not merely adding a new note at a different fanout level. The \nabove code implicitly guarantees this, by calling note_change_n() with the \n'old' fanout level (which will cause the new note to overwrite the old note \nin-place).\n\nHowever, as long as we remember to check for (and remove, if found) the note \nat the old fanout level, we might still be able to place the new note at the \n_current_ fanout level [1], and thus avoid the worst case you describe \nabove. Hmm. I need to think some more about this...\n\n\nHave fun! :)\n\n...Johan\n\n\n[1] This is actually not _completely_ right: If you have several 'N' \ncommands in the same commit that act on the same note, but happen to do so \nin at least two different fanout levels, you will end up with two notes for \nthe same object (at least until do_change_note_fanout() arbitrarily \noverwrites one of them with the other). However, this might be such a far-\nfetched scenario, that we don't have to worry about it in practice.\n\n-- \nJohan Herland, <johan@herland.net>\nwww.herland.net\n"}]}