{"thread":{"id":"5048","subject":"[PATCH 3/3] merge-recur: use the unpack_trees() interface instead of exec()ing read-tree","startedAt":"2006-07-30T18:27:10Z","lastAt":"2006-07-30T18:27:10Z","messageCount":1,"participants":["Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"24382","messageId":"Pine.LNX.4.63.0607302026380.29667@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"5048","inReplyTo":null,"subject":"[PATCH 3/3] merge-recur: use the unpack_trees() interface instead of exec()ing read-tree","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2006-07-30T18:27:10Z","receivedAt":"2006-07-30T18:27:10Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n merge-recursive.c |   77 +++++++++++++++++++++++++++++++++++------------------\n 1 files changed, 51 insertions(+), 26 deletions(-)\n\ndiff --git a/merge-recursive.c b/merge-recursive.c\nindex 5375a1b..3cdea81 100644\n--- a/merge-recursive.c\n+++ b/merge-recursive.c\n@@ -19,7 +19,7 @@ #include \"diff.h\"\n #include \"diffcore.h\"\n #include \"run-command.h\"\n #include \"tag.h\"\n-\n+#include \"unpack-trees.h\"\n #include \"path-list.h\"\n \n /*\n@@ -124,7 +124,7 @@ static int flush_cache(void)\n \tstruct lock_file *lock = xcalloc(1, sizeof(struct lock_file));\n \tint fd = hold_lock_file_for_update(lock, getenv(\"GIT_INDEX_FILE\"));\n \tif (fd < 0)\n-\t\tdie(\"could not lock %s\", temporary_index_file);\n+\t\tdie(\"could not lock %s\", lock->filename);\n \tif (write_cache(fd, active_cache, active_nr) ||\n \t\t\tclose(fd) || commit_lock_file(lock))\n \t\tdie (\"unable to write %s\", getenv(\"GIT_INDEX_FILE\"));\n@@ -191,41 +191,60 @@ static int add_cacheinfo(unsigned int mo\n  */\n static int index_only = 0;\n \n-/*\n- * TODO: this can be streamlined by refactoring builtin-read-tree.c\n- */\n-static int git_read_tree(const struct tree *tree)\n+static int git_read_tree(struct tree *tree)\n {\n \tint rc;\n-\tconst char *argv[] = { \"git-read-tree\", NULL, NULL, };\n+\tstruct object_list *trees = NULL;\n+\tstruct unpack_trees_options opts;\n+\n \tif (cache_dirty)\n \t\tdie(\"read-tree with dirty cache\");\n-\targv[1] = sha1_to_hex(tree->object.sha1);\n-\trc = run_command_v(2, argv);\n-\treturn rc < 0 ? -1: rc;\n+\n+\tmemset(&opts, 0, sizeof(opts));\n+\tobject_list_append(&tree->object, &trees);\n+\trc = unpack_trees(trees, &opts);\n+\tcache_tree_free(&active_cache_tree);\n+\n+\tif (rc == 0)\n+\t\tcache_dirty = 1;\n+\n+\treturn rc;\n }\n \n-/*\n- * TODO: this can be streamlined by refactoring builtin-read-tree.c\n- */\n-static int git_merge_trees(const char *update_arg,\n+static int git_merge_trees(int index_only,\n \t\t\t   struct tree *common,\n \t\t\t   struct tree *head,\n \t\t\t   struct tree *merge)\n {\n \tint rc;\n-\tconst char *argv[] = {\n-\t\t\"git-read-tree\", NULL, \"-m\", NULL, NULL, NULL,\n-\t\tNULL,\n-\t};\n+\tstruct object_list *trees = NULL;\n+\tstruct unpack_trees_options opts;\n+\n+\t/* TODO: if (!cache_dirty) read */\n \tif (cache_dirty)\n \t\tflush_cache();\n-\targv[1] = update_arg;\n-\targv[3] = sha1_to_hex(common->object.sha1);\n-\targv[4] = sha1_to_hex(head->object.sha1);\n-\targv[5] = sha1_to_hex(merge->object.sha1);\n-\trc = run_command_v(6, argv);\n-\treturn rc < 0 ? -1: rc;\n+\n+\tread_cache_from(getenv(\"GIT_INDEX_FILE\"));\n+\n+\tmemset(&opts, 0, sizeof(opts));\n+\tif (index_only)\n+\t\topts.index_only = 1;\n+\telse\n+\t\topts.update = 1;\n+\topts.merge = 1;\n+\topts.head_idx = 2;\n+\topts.fn = threeway_merge;\n+\n+\tobject_list_append(&common->object, &trees);\n+\tobject_list_append(&head->object, &trees);\n+\tobject_list_append(&merge->object, &trees);\n+\n+\trc = unpack_trees(trees, &opts);\n+\tcache_tree_free(&active_cache_tree);\n+\n+\tcache_dirty = 1;\n+\n+\treturn rc;\n }\n \n /*\n@@ -239,8 +258,14 @@ static struct tree *git_write_tree(void)\n \tunsigned char sha1[20];\n \tint ch;\n \tunsigned i = 0;\n-\tif (cache_dirty)\n+\tif (cache_dirty) {\n+\t\tfor (i = 0; i < active_nr; i++) {\n+\t\t\tstruct cache_entry *ce = active_cache[i];\n+\t\t\tif (ce_stage(ce))\n+\t\t\t\treturn NULL;\n+\t\t}\n \t\tflush_cache();\n+\t}\n \tfp = popen(\"git-write-tree 2>/dev/null\", \"r\");\n \twhile ((ch = fgetc(fp)) != EOF)\n \t\tif (i < sizeof(buf)-1 && ch >= '0' && ch <= 'f')\n@@ -1121,7 +1146,7 @@ static int merge_trees(struct tree *head\n \t\treturn 1;\n \t}\n \n-\tcode = git_merge_trees(index_only ? \"-i\": \"-u\", common, head, merge);\n+\tcode = git_merge_trees(index_only, common, head, merge);\n \n \tif (code != 0)\n \t\tdie(\"merging of trees %s and %s failed\",\n-- \n1.4.2.rc2.g76b2-dirty\n"}]}