{"thread":{"id":"217","subject":"[PATCH 09/19] migrate read-tree.c to the new cache api's","startedAt":"2005-04-21T18:36:44Z","lastAt":"2005-04-21T18:36:44Z","messageCount":1,"participants":["Brad Roberts"],"isPatch":true,"patchVersion":1,"patchTotal":19},"messages":[{"id":"1166","messageId":"200504211836.j3LIaiXh027569@gameboy2.puremagic.com","threadId":"217","inReplyTo":null,"subject":"[PATCH 09/19] migrate read-tree.c to the new cache api's","fromName":"Brad Roberts","fromEmail":"braddr@gameboy2.puremagic.com","sentAt":"2005-04-21T18:36:44Z","receivedAt":"2005-04-21T18:36:44Z","isPatch":true,"sender":{"key":"braddr@gameboy2.puremagic.com","avatar":null},"body":"tree 7a3cab4437a849857cc899017b97eea1787a6ce1\nparent 099367f98cc063c33733d15c7a2d9737bea853d9\nauthor Brad Roberts <braddr@puremagic.com> 1114077044 -0700\ncommitter Brad Roberts <braddr@gameboy2.puremagic.com> 1114077044 -0700\n\n[PATCH] migrate read-tree.c to the new cache api's\n\nSigned-off-by: Brad Roberts <braddr@puremagic.com>\n---\n\n read-tree.c |   52 +++++++++++++++++++++++++++++++---------------------\n 1 files changed, 31 insertions(+), 21 deletions(-)\n\nIndex: read-tree.c\n===================================================================\n--- 099367f98cc063c33733d15c7a2d9737bea853d9:1/read-tree.c  (mode:100644 sha1:4ad48f5c409ead69407d2b5feab4466cdcb499f8)\n+++ 50a6596bf7f51ecd598cd02d9c44379a9b92044a:1/read-tree.c  (mode:100644 sha1:ad9128f26613a82361475516dd0f2b470f4ce4b3)\n@@ -146,26 +146,30 @@\n \treturn NULL;\n }\n \n-static void trivially_merge_cache(struct cache_entry **src, int nr)\n+/* rather than doing the 'right' thing of deleting entries as we merge,\n+ * walk dst through the cache, overwriting entries as we go and at the\n+ * end truncate the size of the cache */\n+static void trivially_merge_cache()\n {\n \tstatic struct cache_entry null_entry;\n-\tstruct cache_entry **dst = src;\n \tstruct cache_entry *old = &null_entry;\n+\tint src = 0, dst = 0, nr = get_num_cache_entries();\n \n-\twhile (nr) {\n+\twhile (src < nr) {\n \t\tstruct cache_entry *ce, *result;\n \n-\t\tce = src[0];\n+\t\tce = get_cache_entry(src);\n \n \t\t/* We throw away original cache entries except for the stat information */\n \t\tif (!ce_stage(ce)) {\n \t\t\told = ce;\n \t\t\tsrc++;\n-\t\t\tnr--;\n-\t\t\tactive_nr--;\n \t\t\tcontinue;\n \t\t}\n-\t\tif (nr > 2 && (result = merge_entries(ce, src[1], src[2])) != NULL) {\n+\t\tif ((src < (nr - 2)) &&\n+\t\t    (result = merge_entries(ce,\n+\t\t\t\t\t    get_cache_entry(src + 1),\n+\t\t\t\t\t    get_cache_entry(src + 2))) != NULL) {\n \t\t\t/*\n \t\t\t * See if we can re-use the old CE directly?\n \t\t\t * That way we get the uptodate stat info.\n@@ -175,40 +179,46 @@\n \t\t\tce = result;\n \t\t\tce->ce_flags &= ~htons(CE_STAGEMASK);\n \t\t\tsrc += 2;\n-\t\t\tnr -= 2;\n-\t\t\tactive_nr -= 2;\n \t\t}\n-\t\t*dst++ = ce;\n+\t\tset_cache_entry(ce, dst);\n+\t\tdst++;\n \t\tsrc++;\n+\t}\n+\t/* this could be replaced by a truncate api */\n+\twhile (nr > dst) {\n \t\tnr--;\n+\t\tremove_cache_entry_at(nr);\n \t}\n }\n \n-static void merge_stat_info(struct cache_entry **src, int nr)\n+static void merge_stat_info()\n {\n \tstatic struct cache_entry null_entry;\n-\tstruct cache_entry **dst = src;\n \tstruct cache_entry *old = &null_entry;\n+\tint src = 0, dst = 0, nr = get_num_cache_entries();\n \n-\twhile (nr) {\n+\twhile (src < nr) {\n \t\tstruct cache_entry *ce;\n \n-\t\tce = src[0];\n+\t\tce = get_cache_entry(src);\n \n \t\t/* We throw away original cache entries except for the stat information */\n \t\tif (!ce_stage(ce)) {\n \t\t\told = ce;\n \t\t\tsrc++;\n-\t\t\tnr--;\n-\t\t\tactive_nr--;\n \t\t\tcontinue;\n \t\t}\n \t\tif (path_matches(ce, old) && same(ce, old))\n \t\t\t*ce = *old;\n \t\tce->ce_flags &= ~htons(CE_STAGEMASK);\n-\t\t*dst++ = ce;\n+\t\tset_cache_entry(ce, dst);\n+\t\tdst++;\n \t\tsrc++;\n+\t}\n+\t/* this could be replaced by a truncate api */\n+\twhile (nr > dst) {\n \t\tnr--;\n+\t\tremove_cache_entry_at(nr);\n \t}\n }\n \n@@ -233,8 +243,8 @@\n \t\t\tif (stage)\n \t\t\t\tusage(\"-m needs to come first\");\n \t\t\tread_cache();\n-\t\t\tfor (i = 0; i < active_nr; i++) {\n-\t\t\t\tif (ce_stage(active_cache[i]))\n+\t\t\tfor (i = 0; i < get_num_cache_entries(); i++) {\n+\t\t\t\tif (ce_stage(get_cache_entry(i)))\n \t\t\t\t\tusage(\"you need to resolve your current index first\");\n \t\t\t}\n \t\t\tstage = 1;\n@@ -252,10 +262,10 @@\n \tif (merge) {\n \t\tswitch (stage) {\n \t\tcase 4:\t/* Three-way merge */\n-\t\t\ttrivially_merge_cache(active_cache, active_nr);\n+\t\t\ttrivially_merge_cache();\n \t\t\tbreak;\n \t\tcase 2:\t/* Just read a tree, merge with old cache contents */\n-\t\t\tmerge_stat_info(active_cache, active_nr);\n+\t\t\tmerge_stat_info();\n \t\t\tbreak;\n \t\tdefault:\n \t\t\tdie(\"just how do you expect me to merge %d trees?\", stage-1);\n\n"}]}