@matheusbsbs / matheus-bagliano / commits / 7a85b848ad
Fork 0

merge-recur: use the unpack_trees() interface instead of exec()ing read-tree

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jul 30, 2006 at 20:27 UTC 7a85b848ad77c72c539c5887ead99a89140541da
1 file changed +52 -28
merge-recursive.c
+52 -28
@@ -19,7 +19,7 @@
19 #include "diffcore.h"
20 #include "run-command.h"
21 #include "tag.h"
22 -
22 +#include "unpack-trees.h"
23 #include "path-list.h"
24
25 /*
@@ -124,7 +124,7 @@ static int flush_cache(void)
124 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
125 int fd = hold_lock_file_for_update(lock, getenv("GIT_INDEX_FILE"));
126 if (fd < 0)
127 - die("could not lock %s", temporary_index_file);
127 + die("could not lock %s", lock->filename);
128 if (write_cache(fd, active_cache, active_nr) ||
129 close(fd) || commit_lock_file(lock))
130 die ("unable to write %s", getenv("GIT_INDEX_FILE"));
@@ -191,41 +191,59 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
191 */
192 static int index_only = 0;
193
194 -/*
195 - * TODO: this can be streamlined by refactoring builtin-read-tree.c
196 - */
197 -static int git_read_tree(const struct tree *tree)
194 +static int git_read_tree(struct tree *tree)
195 {
196 int rc;
200 - const char *argv[] = { "git-read-tree", NULL, NULL, };
197 + struct object_list *trees = NULL;
198 + struct unpack_trees_options opts;
199 +
200 if (cache_dirty)
201 die("read-tree with dirty cache");
203 - argv[1] = sha1_to_hex(tree->object.sha1);
204 - rc = run_command_v(2, argv);
205 - return rc < 0 ? -1: rc;
202 +
203 + memset(&opts, 0, sizeof(opts));
204 + object_list_append(&tree->object, &trees);
205 + rc = unpack_trees(trees, &opts);
206 + cache_tree_free(&active_cache_tree);
207 +
208 + if (rc == 0)
209 + cache_dirty = 1;
210 +
211 + return rc;
212 }
213
208 -/*
209 - * TODO: this can be streamlined by refactoring builtin-read-tree.c
210 - */
211 -static int git_merge_trees(const char *update_arg,
214 +static int git_merge_trees(int index_only,
215 struct tree *common,
216 struct tree *head,
217 struct tree *merge)
218 {
219 int rc;
217 - const char *argv[] = {
218 - "git-read-tree", NULL, "-m", NULL, NULL, NULL,
219 - NULL,
220 - };
221 - if (cache_dirty)
222 - flush_cache();
223 - argv[1] = update_arg;
224 - argv[3] = sha1_to_hex(common->object.sha1);
225 - argv[4] = sha1_to_hex(head->object.sha1);
226 - argv[5] = sha1_to_hex(merge->object.sha1);
227 - rc = run_command_v(6, argv);
228 - return rc < 0 ? -1: rc;
220 + struct object_list *trees = NULL;
221 + struct unpack_trees_options opts;
222 +
223 + if (!cache_dirty) {
224 + read_cache_from(getenv("GIT_INDEX_FILE"));
225 + cache_dirty = 1;
226 + }
227 +
228 + memset(&opts, 0, sizeof(opts));
229 + if (index_only)
230 + opts.index_only = 1;
231 + else
232 + opts.update = 1;
233 + opts.merge = 1;
234 + opts.head_idx = 2;
235 + opts.fn = threeway_merge;
236 +
237 + object_list_append(&common->object, &trees);
238 + object_list_append(&head->object, &trees);
239 + object_list_append(&merge->object, &trees);
240 +
241 + rc = unpack_trees(trees, &opts);
242 + cache_tree_free(&active_cache_tree);
243 +
244 + cache_dirty = 1;
245 +
246 + return rc;
247 }
248
249 /*
@@ -239,8 +257,14 @@ static struct tree *git_write_tree(void)
257 unsigned char sha1[20];
258 int ch;
259 unsigned i = 0;
242 - if (cache_dirty)
260 + if (cache_dirty) {
261 + for (i = 0; i < active_nr; i++) {
262 + struct cache_entry *ce = active_cache[i];
263 + if (ce_stage(ce))
264 + return NULL;
265 + }
266 flush_cache();
267 + }
268 fp = popen("git-write-tree 2>/dev/null", "r");
269 while ((ch = fgetc(fp)) != EOF)
270 if (i < sizeof(buf)-1 && ch >= '0' && ch <= 'f')
@@ -1121,7 +1145,7 @@ static int merge_trees(struct tree *head,
1145 return 1;
1146 }
1147
1124 - code = git_merge_trees(index_only ? "-i": "-u", common, head, merge);
1148 + code = git_merge_trees(index_only, common, head, merge);
1149
1150 if (code != 0)
1151 die("merging of trees %s and %s failed",