@matheusbsbs / matheus-bagliano / commits / 3af244caa8
Fork 0

Cumulative update of merge-recursive in C

This contains mainly three sorts of fixes: - get rid of small wrapper functions - reuse the diff_filespec structure when sha1, mode & path are needed - Junio's pedantic updates Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jul 27, 2006 at 13:17 UTC 3af244caa8297793f29d7422bb19d9da1bf07b5e
1 file changed +364 -526
merge-recursive.c
+364 -526
@@ -25,9 +25,9 @@
25 /*#define DEBUG*/
26
27 #ifdef DEBUG
28 -#define debug(args, ...) fprintf(stderr, args, ## __VA_ARGS__)
28 +#define debug(...) fprintf(stderr, __VA_ARGS__)
29 #else
30 -#define debug(args, ...)
30 +#define debug(...) do { ; /* nothing */ } while (0)
31 #endif
32
33 #ifdef DEBUG
@@ -61,13 +61,14 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
61 fputc('\n', stderr);
62 }
63
64 -static void ls_files() {
64 +static void ls_files(void) {
65 int i;
66 for (i = 0; i < active_nr; i++) {
67 struct cache_entry *ce = active_cache[i];
68 show_ce_entry("", ce);
69 }
70 fprintf(stderr, "---\n");
71 + if (0) ls_files(); /* avoid "unused" warning */
72 }
73 #endif
74
@@ -76,41 +77,6 @@ static void ls_files() {
77 * - (const char *)commit->util set to the name, and
78 * - *(int *)commit->object.sha1 set to the virtual id.
79 */
79 -static const char *commit_title(struct commit *commit, int *len)
80 -{
81 - const char *s = "(null commit)";
82 - *len = strlen(s);
83 -
84 - if ( commit->util ) {
85 - s = commit->util;
86 - *len = strlen(s);
87 - } else {
88 - if ( parse_commit(commit) != 0 ) {
89 - s = "(bad commit)";
90 - *len = strlen(s);
91 - } else {
92 - s = commit->buffer;
93 - char prev = '\0';
94 - while ( *s ) {
95 - if ( '\n' == prev && '\n' == *s ) {
96 - ++s;
97 - break;
98 - }
99 - prev = *s++;
100 - }
101 - *len = 0;
102 - while ( s[*len] && '\n' != s[*len] )
103 - ++(*len);
104 - }
105 - }
106 - return s;
107 -}
108 -
109 -static const char *commit_hex_sha1(const struct commit *commit)
110 -{
111 - return commit->util ? "virtual" : commit ?
112 - sha1_to_hex(commit->object.sha1) : "undefined";
113 -}
80
81 static unsigned commit_list_count(const struct commit_list *l)
82 {
@@ -136,41 +102,11 @@ static struct commit *make_virtual_commit(struct tree *tree, const char *comment
102 */
103 static int sha_eq(const unsigned char *a, const unsigned char *b)
104 {
139 - if ( !a && !b )
105 + if (!a && !b)
106 return 2;
107 return a && b && memcmp(a, b, 20) == 0;
108 }
109
144 -static void memswp(void *p1, void *p2, unsigned n)
145 -{
146 - unsigned char *a = p1, *b = p2;
147 - while ( n-- ) {
148 - *a ^= *b;
149 - *b ^= *a;
150 - *a ^= *b;
151 - ++a;
152 - ++b;
153 - }
154 -}
155 -
156 -/*
157 - * TODO: we should convert the merge_result users to
158 - * int blabla(..., struct commit **result)
159 - * like everywhere else in git.
160 - * Same goes for merge_tree_result and merge_file_info.
161 - */
162 -struct merge_result
163 -{
164 - struct commit *commit;
165 - unsigned clean:1;
166 -};
167 -
168 -struct merge_tree_result
169 -{
170 - struct tree *tree;
171 - unsigned clean:1;
172 -};
173 -
110 /*
111 * TODO: check if we can just reuse the active_cache structure: it is already
112 * sorted (by name, stage).
@@ -195,7 +131,7 @@ static void output(const char *fmt, ...)
131 {
132 va_list args;
133 int i;
198 - for ( i = output_indent; i--; )
134 + for (i = output_indent; i--;)
135 fputs(" ", stdout);
136 va_start(args, fmt);
137 vfprintf(stdout, fmt, args);
@@ -203,11 +139,37 @@ static void output(const char *fmt, ...)
139 fputc('\n', stdout);
140 }
141
142 +static void output_commit_title(struct commit *commit)
143 +{
144 + int i;
145 + for (i = output_indent; i--;)
146 + fputs(" ", stdout);
147 + if (commit->util)
148 + printf("virtual %s\n", (char *)commit->util);
149 + else {
150 + printf("%s ", sha1_to_hex(commit->object.sha1));
151 + if (parse_commit(commit) != 0)
152 + printf("(bad commit)\n");
153 + else {
154 + const char *s;
155 + int len;
156 + for (s = commit->buffer; *s; s++)
157 + if (*s == '\n' && s[1] == '\n') {
158 + s += 2;
159 + break;
160 + }
161 + for (len = 0; s[len] && '\n' != s[len]; len++)
162 + ; /* do nothing */
163 + printf("%.*s\n", len, s);
164 + }
165 + }
166 +}
167 +
168 static const char *original_index_file;
169 static const char *temporary_index_file;
170 static int cache_dirty = 0;
171
210 -static int flush_cache()
172 +static int flush_cache(void)
173 {
174 /* flush temporary index */
175 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
@@ -290,11 +252,12 @@ static int git_read_tree(const struct tree *tree)
252 getenv("GIT_INDEX_FILE"),
253 sha1_to_hex(tree->object.sha1));
254 #endif
255 + int rc;
256 const char *argv[] = { "git-read-tree", NULL, NULL, };
257 if (cache_dirty)
258 die("read-tree with dirty cache");
259 argv[1] = sha1_to_hex(tree->object.sha1);
297 - int rc = run_command_v(2, argv);
260 + rc = run_command_v(2, argv);
261 return rc < 0 ? -1: rc;
262 }
263
@@ -314,6 +277,7 @@ static int git_merge_trees(const char *update_arg,
277 sha1_to_hex(head->object.sha1),
278 sha1_to_hex(merge->object.sha1));
279 #endif
280 + int rc;
281 const char *argv[] = {
282 "git-read-tree", NULL, "-m", NULL, NULL, NULL,
283 NULL,
@@ -324,50 +288,42 @@ static int git_merge_trees(const char *update_arg,
288 argv[3] = sha1_to_hex(common->object.sha1);
289 argv[4] = sha1_to_hex(head->object.sha1);
290 argv[5] = sha1_to_hex(merge->object.sha1);
327 - int rc = run_command_v(6, argv);
291 + rc = run_command_v(6, argv);
292 return rc < 0 ? -1: rc;
293 }
294
295 /*
296 * TODO: this can be streamlined by refactoring builtin-write-tree.c
297 */
334 -static struct tree *git_write_tree()
298 +static struct tree *git_write_tree(void)
299 {
300 #if 0
301 fprintf(stderr, "GIT_INDEX_FILE='%s' git-write-tree\n",
302 getenv("GIT_INDEX_FILE"));
303 #endif
340 - if (cache_dirty)
341 - flush_cache();
342 - FILE *fp = popen("git-write-tree 2>/dev/null", "r");
304 + FILE *fp;
305 + int rc;
306 char buf[41];
307 unsigned char sha1[20];
308 int ch;
309 unsigned i = 0;
347 - while ( (ch = fgetc(fp)) != EOF )
348 - if ( i < sizeof(buf)-1 && ch >= '0' && ch <= 'f' )
310 + if (cache_dirty)
311 + flush_cache();
312 + fp = popen("git-write-tree 2>/dev/null", "r");
313 + while ((ch = fgetc(fp)) != EOF)
314 + if (i < sizeof(buf)-1 && ch >= '0' && ch <= 'f')
315 buf[i++] = ch;
316 else
317 break;
352 - int rc = pclose(fp);
353 - if ( rc == -1 || WEXITSTATUS(rc) )
318 + rc = pclose(fp);
319 + if (rc == -1 || WEXITSTATUS(rc))
320 return NULL;
321 buf[i] = '\0';
356 - if ( get_sha1(buf, sha1) != 0 )
322 + if (get_sha1(buf, sha1) != 0)
323 return NULL;
324 return lookup_tree(sha1);
325 }
326
361 -/*
362 - * TODO: get rid of files_and_dirs; we do not use it except for
363 - * current_file_set and current_dir_set, which are global already.
364 - */
365 -static struct
366 -{
367 - struct path_list *files;
368 - struct path_list *dirs;
369 -} files_and_dirs;
370 -
327 static int save_files_dirs(const unsigned char *sha1,
328 const char *base, int baselen, const char *path,
329 unsigned int mode, int stage)
@@ -379,70 +335,36 @@ static int save_files_dirs(const unsigned char *sha1,
335 newpath[baselen + len] = '\0';
336
337 if (S_ISDIR(mode))
382 - path_list_insert(newpath, files_and_dirs.dirs);
338 + path_list_insert(newpath, &currentDirectorySet);
339 else
384 - path_list_insert(newpath, files_and_dirs.files);
340 + path_list_insert(newpath, &currentFileSet);
341 free(newpath);
342
343 return READ_TREE_RECURSIVE;
344 }
345
390 -static int get_files_dirs(struct tree *tree,
391 - struct path_list *files,
392 - struct path_list *dirs)
346 +static int get_files_dirs(struct tree *tree)
347 {
348 int n;
395 - files_and_dirs.files = files;
396 - files_and_dirs.dirs = dirs;
349 debug("get_files_dirs ...\n");
350 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0) {
351 debug(" get_files_dirs done (0)\n");
352 return 0;
353 }
402 - n = files->nr + dirs->nr;
354 + n = currentFileSet.nr + currentDirectorySet.nr;
355 debug(" get_files_dirs done (%d)\n", n);
356 return n;
357 }
358
407 -/*
408 - * TODO: this wrapper is so small, we can use path_list_lookup directly.
409 - * Same goes for index_entry_get(), free_index_entries(), find_rename_bysrc(),
410 - * free_rename_entries().
411 - */
412 -static struct stage_data *index_entry_find(struct path_list *ents,
413 - const char *path)
414 -{
415 - struct path_list_item *item = path_list_lookup(path, ents);
416 - if (item)
417 - return item->util;
418 - return NULL;
419 -}
420 -
421 -static struct stage_data *index_entry_get(struct path_list *ents,
422 - const char *path)
423 -{
424 - struct path_list_item *item = path_list_lookup(path, ents);
425 -
426 - if (item == NULL) {
427 - item = path_list_insert(path, ents);
428 - item->util = xcalloc(1, sizeof(struct stage_data));
429 - }
430 - return item->util;
431 -}
432 -
433 -/*
434 - * TODO: since the result of index_entry_from_db() is tucked into a
435 - * path_list anyway, this helper can do that already.
436 - */
359 /*
360 * Returns a index_entry instance which doesn't have to correspond to
361 * a real cache entry in Git's index.
362 */
441 -static struct stage_data *index_entry_from_db(const char *path,
442 - struct tree *o,
443 - struct tree *a,
444 - struct tree *b)
363 +static struct stage_data *insert_stage_data(const char *path,
364 + struct tree *o, struct tree *a, struct tree *b,
365 + struct path_list *entries)
366 {
367 + struct path_list_item *item;
368 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
369 get_tree_entry(o->object.sha1, path,
370 e->stages[1].sha, &e->stages[1].mode);
@@ -450,24 +372,16 @@ static struct stage_data *index_entry_from_db(const char *path,
372 e->stages[2].sha, &e->stages[2].mode);
373 get_tree_entry(b->object.sha1, path,
374 e->stages[3].sha, &e->stages[3].mode);
375 + item = path_list_insert(path, entries);
376 + item->util = e;
377 return e;
378 }
379
456 -static void free_index_entries(struct path_list **ents)
457 -{
458 - if (!*ents)
459 - return;
460 -
461 - path_list_clear(*ents, 1);
462 - free(*ents);
463 - *ents = NULL;
464 -}
465 -
380 /*
381 * Create a dictionary mapping file names to CacheEntry objects. The
382 * dictionary contains one entry for every path with a non-zero stage entry.
383 */
470 -static struct path_list *get_unmerged()
384 +static struct path_list *get_unmerged(void)
385 {
386 struct path_list *unmerged = xcalloc(1, sizeof(struct path_list));
387 int i;
@@ -478,16 +392,22 @@ static struct path_list *get_unmerged()
392 cache_dirty++;
393 }
394 for (i = 0; i < active_nr; i++) {
395 + struct path_list_item *item;
396 + struct stage_data *e;
397 struct cache_entry *ce = active_cache[i];
398 if (!ce_stage(ce))
399 continue;
400
485 - struct stage_data *e = index_entry_get(unmerged, ce->name);
401 + item = path_list_lookup(ce->name, unmerged);
402 + if (!item) {
403 + item = path_list_insert(ce->name, unmerged);
404 + item->util = xcalloc(1, sizeof(struct stage_data));
405 + }
406 + e = item->util;
407 e->stages[ce_stage(ce)].mode = ntohl(ce->ce_mode);
408 memcpy(e->stages[ce_stage(ce)].sha, ce->sha1, 20);
409 }
410
490 - debug(" get_unmerged done\n");
411 return unmerged;
412 }
413
@@ -499,25 +419,6 @@ struct rename
419 unsigned processed:1;
420 };
421
502 -static struct rename *find_rename_bysrc(struct path_list *e,
503 - const char *name)
504 -{
505 - struct path_list_item *item = path_list_lookup(name, e);
506 - if (item)
507 - return item->util;
508 - return NULL;
509 -}
510 -
511 -static void free_rename_entries(struct path_list **list)
512 -{
513 - if (!*list)
514 - return;
515 -
516 - path_list_clear(*list, 0);
517 - free(*list);
518 - *list = NULL;
519 -}
520 -
422 /*
423 * Get information of all renames which occured between 'oTree' and
424 * 'tree'. We need the three trees in the merge ('oTree', 'aTree' and
@@ -530,13 +431,16 @@ static struct path_list *get_renames(struct tree *tree,
431 struct tree *bTree,
432 struct path_list *entries)
433 {
434 + int i;
435 + struct path_list *renames;
436 + struct diff_options opts;
437 #ifdef DEBUG
438 time_t t = time(0);
439 +
440 debug("getRenames ...\n");
441 #endif
537 - int i;
538 - struct path_list *renames = xcalloc(1, sizeof(struct path_list));
539 - struct diff_options opts;
442 +
443 + renames = xcalloc(1, sizeof(struct path_list));
444 diff_setup(&opts);
445 opts.recursive = 1;
446 opts.detect_rename = DIFF_DETECT_RENAME;
@@ -546,6 +450,7 @@ static struct path_list *get_renames(struct tree *tree,
450 diff_tree_sha1(oTree->object.sha1, tree->object.sha1, "", &opts);
451 diffcore_std(&opts);
452 for (i = 0; i < diff_queued_diff.nr; ++i) {
453 + struct path_list_item *item;
454 struct rename *re;
455 struct diff_filepair *pair = diff_queued_diff.queue[i];
456 if (pair->status != 'R') {
@@ -555,78 +460,66 @@ static struct path_list *get_renames(struct tree *tree,
460 re = xmalloc(sizeof(*re));
461 re->processed = 0;
462 re->pair = pair;
558 - re->src_entry = index_entry_find(entries, re->pair->one->path);
559 - /* TODO: should it not be an error, if src_entry was found? */
560 - if ( !re->src_entry ) {
561 - re->src_entry = index_entry_from_db(re->pair->one->path,
562 - oTree, aTree, bTree);
563 - struct path_list_item *item =
564 - path_list_insert(re->pair->one->path, entries);
565 - item->util = re->src_entry;
566 - }
567 - re->dst_entry = index_entry_find(entries, re->pair->two->path);
568 - if ( !re->dst_entry ) {
569 - re->dst_entry = index_entry_from_db(re->pair->two->path,
570 - oTree, aTree, bTree);
571 - struct path_list_item *item =
572 - path_list_insert(re->pair->two->path, entries);
573 - item->util = re->dst_entry;
574 - }
575 - struct path_list_item *item = path_list_insert(pair->one->path, renames);
463 + item = path_list_lookup(re->pair->one->path, entries);
464 + if (!item)
465 + re->src_entry = insert_stage_data(re->pair->one->path,
466 + oTree, aTree, bTree, entries);
467 + else
468 + re->src_entry = item->util;
469 +
470 + item = path_list_lookup(re->pair->two->path, entries);
471 + if (!item)
472 + re->dst_entry = insert_stage_data(re->pair->two->path,
473 + oTree, aTree, bTree, entries);
474 + else
475 + re->dst_entry = item->util;
476 + item = path_list_insert(pair->one->path, renames);
477 item->util = re;
478 }
479 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
480 diff_queued_diff.nr = 0;
481 diff_flush(&opts);
482 +#ifdef DEBUG
483 debug(" getRenames done in %ld\n", time(0)-t);
484 +#endif
485 return renames;
486 }
487
585 -/*
586 - * TODO: the code would be way nicer, if we had a struct containing just sha1 and mode.
587 - * In this particular case, we might get away reusing stage_data, no?
588 - */
589 -int update_stages(const char *path,
590 - unsigned char *osha, unsigned omode,
591 - unsigned char *asha, unsigned amode,
592 - unsigned char *bsha, unsigned bmode,
593 - int clear /* =True */)
488 +int update_stages(const char *path, struct diff_filespec *o,
489 + struct diff_filespec *a, struct diff_filespec *b, int clear)
490 {
491 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
596 - if ( clear )
597 - if (add_cacheinfo(0, null_sha1, path, 0, 0, options))
492 + if (clear)
493 + if (remove_file_from_cache(path))
494 return -1;
599 - if ( omode )
600 - if (add_cacheinfo(omode, osha, path, 1, 0, options))
495 + if (o)
496 + if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
497 return -1;
602 - if ( amode )
603 - if (add_cacheinfo(omode, osha, path, 2, 0, options))
498 + if (a)
499 + if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
500 return -1;
605 - if ( bmode )
606 - if (add_cacheinfo(omode, osha, path, 3, 0, options))
501 + if (b)
502 + if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
503 return -1;
504 return 0;
505 }
506
611 -/*
612 - * TODO: there has to be a function in libgit doing this exact thing.
613 - */
507 static int remove_path(const char *name)
508 {
616 - int ret;
617 - char *slash;
509 + int ret, len;
510 + char *slash, *dirs;
511
512 ret = unlink(name);
620 - if ( ret )
513 + if (ret)
514 return ret;
622 - int len = strlen(name);
623 - char *dirs = malloc(len+1);
515 + len = strlen(name);
516 + dirs = malloc(len+1);
517 memcpy(dirs, name, len);
518 dirs[len] = '\0';
626 - while ( (slash = strrchr(name, '/')) ) {
519 + while ((slash = strrchr(name, '/'))) {
520 *slash = '\0';
521 len = slash - name;
629 - if ( rmdir(name) != 0 )
522 + if (rmdir(name) != 0)
523 break;
524 }
525 free(dirs);
@@ -636,7 +529,7 @@ static int remove_path(const char *name)
529 /* General TODO: unC99ify the code: no declaration after code */
530 /* General TODO: no javaIfiCation: rename updateCache to update_cache */
531 /*
639 - * TODO: once we no longer call external programs, we'd probably be better of
532 + * TODO: once we no longer call external programs, we'd probably be better off
533 * not setting / getting the environment variable GIT_INDEX_FILE all the time.
534 */
535 int remove_file(int clean, const char *path)
@@ -644,17 +537,17 @@ int remove_file(int clean, const char *path)
537 int updateCache = index_only || clean;
538 int updateWd = !index_only;
539
647 - if ( updateCache ) {
540 + if (updateCache) {
541 if (!cache_dirty)
542 read_cache_from(getenv("GIT_INDEX_FILE"));
543 cache_dirty++;
544 if (remove_file_from_cache(path))
545 return -1;
546 }
654 - if ( updateWd )
547 + if (updateWd)
548 {
549 unlink(path);
657 - if ( errno != ENOENT || errno != EISDIR )
550 + if (errno != ENOENT || errno != EISDIR)
551 return -1;
552 remove_path(path);
553 }
@@ -664,55 +557,31 @@ int remove_file(int clean, const char *path)
557 static char *unique_path(const char *path, const char *branch)
558 {
559 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
560 + int suffix = 0;
561 + struct stat st;
562 + char *p = newpath + strlen(newpath);
563 strcpy(newpath, path);
564 strcat(newpath, "~");
669 - char *p = newpath + strlen(newpath);
565 strcpy(p, branch);
671 - for ( ; *p; ++p )
672 - if ( '/' == *p )
566 + for (; *p; ++p)
567 + if ('/' == *p)
568 *p = '_';
674 - int suffix = 0;
675 - struct stat st;
676 - while ( path_list_has_path(&currentFileSet, newpath) ||
677 - path_list_has_path(&currentDirectorySet, newpath) ||
678 - lstat(newpath, &st) == 0 ) {
569 + while (path_list_has_path(&currentFileSet, newpath) ||
570 + path_list_has_path(&currentDirectorySet, newpath) ||
571 + lstat(newpath, &st) == 0)
572 sprintf(p, "_%d", suffix++);
680 - }
573 +
574 path_list_insert(newpath, &currentFileSet);
575 return newpath;
576 }
577
685 -/*
686 - * TODO: except for create_last, this so looks like
687 - * safe_create_leading_directories().
688 - */
689 -static int mkdir_p(const char *path, unsigned long mode, int create_last)
578 +static int mkdir_p(const char *path, unsigned long mode)
579 {
580 + /* path points to cache entries, so strdup before messing with it */
581 char *buf = strdup(path);
692 - char *p;
693 -
694 - for ( p = buf; *p; ++p ) {
695 - if ( *p != '/' )
696 - continue;
697 - *p = '\0';
698 - if (mkdir(buf, mode)) {
699 - int e = errno;
700 - if ( e == EEXIST ) {
701 - struct stat st;
702 - if ( !stat(buf, &st) && S_ISDIR(st.st_mode) )
703 - goto next; /* ok */
704 - errno = e;
705 - }
706 - free(buf);
707 - return -1;
708 - }
709 - next:
710 - *p = '/';
711 - }
582 + int result = safe_create_leading_directories(buf);
583 free(buf);
713 - if ( create_last && mkdir(path, mode) )
714 - return -1;
715 - return 0;
584 + return result;
585 }
586
587 static void flush_buffer(int fd, const char *buf, unsigned long size)
@@ -732,17 +601,16 @@ static void flush_buffer(int fd, const char *buf, unsigned long size)
601 }
602 }
603
735 -/* General TODO: reindent according to guide lines (no if ( blabla )) */
604 void update_file_flags(const unsigned char *sha,
737 - unsigned mode,
738 - const char *path,
739 - int updateCache,
740 - int updateWd)
605 + unsigned mode,
606 + const char *path,
607 + int update_cache,
608 + int update_wd)
609 {
742 - if ( index_only )
743 - updateWd = 0;
610 + if (index_only)
611 + update_wd = 0;
612
745 - if ( updateWd ) {
613 + if (update_wd) {
614 char type[20];
615 void *buf;
616 unsigned long size;
@@ -750,37 +618,38 @@ void update_file_flags(const unsigned char *sha,
618 buf = read_sha1_file(sha, type, &size);
619 if (!buf)
620 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
753 - if ( strcmp(type, blob_type) != 0 )
621 + if (strcmp(type, blob_type) != 0)
622 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
623
756 - if ( S_ISREG(mode) ) {
757 - if ( mkdir_p(path, 0777, 0 /* don't create last element */) )
624 + if (S_ISREG(mode)) {
625 + int fd;
626 + if (mkdir_p(path, 0777))
627 die("failed to create path %s: %s", path, strerror(errno));
628 unlink(path);
760 - if ( mode & 0100 )
629 + if (mode & 0100)
630 mode = 0777;
631 else
632 mode = 0666;
764 - int fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
765 - if ( fd < 0 )
633 + fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
634 + if (fd < 0)
635 die("failed to open %s: %s", path, strerror(errno));
636 flush_buffer(fd, buf, size);
637 close(fd);
769 - } else if ( S_ISLNK(mode) ) {
770 - char *linkTarget = malloc(size + 1);
771 - memcpy(linkTarget, buf, size);
772 - linkTarget[size] = '\0';
773 - mkdir_p(path, 0777, 0);
774 - symlink(linkTarget, path);
638 + } else if (S_ISLNK(mode)) {
639 + char *lnk = malloc(size + 1);
640 + memcpy(lnk, buf, size);
641 + lnk[size] = '\0';
642 + mkdir_p(path, 0777);
643 + unlink(lnk);
644 + symlink(lnk, path);
645 } else
646 die("do not know what to do with %06o %s '%s'",
647 mode, sha1_to_hex(sha), path);
648 }
779 - if ( updateCache )
780 - add_cacheinfo(mode, sha, path, 0, updateWd, ADD_CACHE_OK_TO_ADD);
649 + if (update_cache)
650 + add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
651 }
652
783 -/* TODO: is this often used? if not, do direct call */
653 void update_file(int clean,
654 const unsigned char *sha,
655 unsigned mode,
@@ -819,64 +688,53 @@ static char *git_unpack_file(const unsigned char *sha1, char *path)
688 return path;
689 }
690
822 -/*
823 - * TODO: the signature would be much more efficient using stage_data
824 - */
825 -static struct merge_file_info merge_file(const char *oPath,
826 - const unsigned char *oSha,
827 - unsigned oMode,
828 - const char *aPath,
829 - const unsigned char *aSha,
830 - unsigned aMode,
831 - const char *bPath,
832 - const unsigned char *bSha,
833 - unsigned bMode,
834 - const char *branch1Name,
835 - const char *branch2Name)
691 +static struct merge_file_info merge_file(struct diff_filespec *o,
692 + struct diff_filespec *a, struct diff_filespec *b,
693 + const char *branch1Name, const char *branch2Name)
694 {
695 struct merge_file_info result;
696 result.merge = 0;
697 result.clean = 1;
698
841 - if ( (S_IFMT & aMode) != (S_IFMT & bMode) ) {
699 + if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
700 result.clean = 0;
843 - if ( S_ISREG(aMode) ) {
844 - result.mode = aMode;
845 - memcpy(result.sha, aSha, 20);
701 + if (S_ISREG(a->mode)) {
702 + result.mode = a->mode;
703 + memcpy(result.sha, a->sha1, 20);
704 } else {
847 - result.mode = bMode;
848 - memcpy(result.sha, bSha, 20);
705 + result.mode = b->mode;
706 + memcpy(result.sha, b->sha1, 20);
707 }
708 } else {
851 - if ( memcmp(aSha, oSha, 20) != 0 && memcmp(bSha, oSha, 20) != 0 )
709 + if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
710 result.merge = 1;
711
854 - result.mode = aMode == oMode ? bMode: aMode;
712 + result.mode = a->mode == o->mode ? b->mode: a->mode;
713
856 - if ( memcmp(aSha, oSha, 20) == 0 )
857 - memcpy(result.sha, bSha, 20);
858 - else if ( memcmp(bSha, oSha, 20) == 0 )
859 - memcpy(result.sha, aSha, 20);
860 - else if ( S_ISREG(aMode) ) {
861 -
862 - int code = 1;
714 + if (sha_eq(a->sha1, o->sha1))
715 + memcpy(result.sha, b->sha1, 20);
716 + else if (sha_eq(b->sha1, o->sha1))
717 + memcpy(result.sha, a->sha1, 20);
718 + else if (S_ISREG(a->mode)) {
719 + int code = 1, fd;
720 + struct stat st;
721 char orig[PATH_MAX];
722 char src1[PATH_MAX];
723 char src2[PATH_MAX];
866 -
867 - git_unpack_file(oSha, orig);
868 - git_unpack_file(aSha, src1);
869 - git_unpack_file(bSha, src2);
870 -
724 const char *argv[] = {
725 "merge", "-L", NULL, "-L", NULL, "-L", NULL,
726 src1, orig, src2,
727 NULL
728 };
729 char *la, *lb, *lo;
877 - argv[2] = la = strdup(mkpath("%s/%s", branch1Name, aPath));
878 - argv[6] = lb = strdup(mkpath("%s/%s", branch2Name, bPath));
879 - argv[4] = lo = strdup(mkpath("orig/%s", oPath));
730 +
731 + git_unpack_file(o->sha1, orig);
732 + git_unpack_file(a->sha1, src1);
733 + git_unpack_file(b->sha1, src2);
734 +
735 + argv[2] = la = strdup(mkpath("%s/%s", branch1Name, a->path));
736 + argv[6] = lb = strdup(mkpath("%s/%s", branch2Name, b->path));
737 + argv[4] = lo = strdup(mkpath("orig/%s", o->path));
738
739 #if 0
740 printf("%s %s %s %s %s %s %s %s %s %s\n",
@@ -888,17 +746,15 @@ static struct merge_file_info merge_file(const char *oPath,
746 free(la);
747 free(lb);
748 free(lo);
891 - if ( code && code < -256 ) {
749 + if (code && code < -256) {
750 die("Failed to execute 'merge'. merge(1) is used as the "
751 "file-level merge tool. Is 'merge' in your path?");
752 }
895 - struct stat st;
896 - int fd = open(src1, O_RDONLY);
753 + fd = open(src1, O_RDONLY);
754 if (fd < 0 || fstat(fd, &st) < 0 ||
755 index_fd(result.sha, fd, &st, 1,
756 "blob"))
757 die("Unable to add %s to database", src1);
901 - close(fd);
758
759 unlink(orig);
760 unlink(src1);
@@ -906,12 +762,12 @@ static struct merge_file_info merge_file(const char *oPath,
762
763 result.clean = WEXITSTATUS(code) == 0;
764 } else {
909 - if ( !(S_ISLNK(aMode) || S_ISLNK(bMode)) )
765 + if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode)))
766 die("cannot merge modes?");
767
912 - memcpy(result.sha, aSha, 20);
768 + memcpy(result.sha, a->sha1, 20);
769
914 - if ( memcmp(aSha, bSha, 20) != 0 )
770 + if (!sha_eq(a->sha1, b->sha1))
771 result.clean = 0;
772 }
773 }
@@ -942,17 +798,9 @@ static void conflict_rename_rename(struct rename *ren1,
798 ren2_dst, branch1, dstName2);
799 remove_file(0, ren2_dst);
800 }
945 - update_stages(dstName1,
946 - NULL, 0,
947 - ren1->pair->two->sha1, ren1->pair->two->mode,
948 - NULL, 0,
949 - 1 /* clear */);
950 - update_stages(dstName2,
951 - NULL, 0,
952 - NULL, 0,
953 - ren2->pair->two->sha1, ren2->pair->two->mode,
954 - 1 /* clear */);
955 - while ( delp-- )
801 + update_stages(dstName1, NULL, ren1->pair->two, NULL, 1);
802 + update_stages(dstName2, NULL, NULL, ren2->pair->two, 1);
803 + while (delp--)
804 free(del[delp]);
805 }
806
@@ -989,66 +837,73 @@ static int process_renames(struct path_list *renamesA,
837 const char *branchNameA,
838 const char *branchNameB)
839 {
992 - int cleanMerge = 1, i;
993 - struct path_list srcNames = {NULL, 0, 0, 0}, byDstA = {NULL, 0, 0, 0}, byDstB = {NULL, 0, 0, 0};
840 + int cleanMerge = 1, i, j;
841 + struct path_list byDstA = {NULL, 0, 0, 0}, byDstB = {NULL, 0, 0, 0};
842 const struct rename *sre;
843
996 - /*
997 - * TODO: think about a saner way to do this.
998 - * Since both renamesA and renamesB are sorted, it should
999 - * be much more efficient to traverse both simultaneously,
1000 - * only byDstA and byDstB should be needed.
1001 - */
1002 - debug("processRenames...\n");
844 for (i = 0; i < renamesA->nr; i++) {
845 sre = renamesA->items[i].util;
1005 - path_list_insert(sre->pair->one->path, &srcNames);
846 path_list_insert(sre->pair->two->path, &byDstA)->util
847 = sre->dst_entry;
848 }
849 for (i = 0; i < renamesB->nr; i++) {
850 sre = renamesB->items[i].util;
1011 - path_list_insert(sre->pair->one->path, &srcNames);
851 path_list_insert(sre->pair->two->path, &byDstB)->util
852 = sre->dst_entry;
853 }
854
1016 - for (i = 0; i < srcNames.nr; i++) {
1017 - char *src = srcNames.items[i].path;
855 + for (i = 0, j = 0; i < renamesA->nr || j < renamesB->nr;) {
856 + int compare;
857 + char *src;
858 struct path_list *renames1, *renames2, *renames2Dst;
1019 - struct rename *ren1, *ren2;
859 + struct rename *ren1 = NULL, *ren2 = NULL;
860 const char *branchName1, *branchName2;
1021 - ren1 = find_rename_bysrc(renamesA, src);
1022 - ren2 = find_rename_bysrc(renamesB, src);
861 + const char *ren1_src, *ren1_dst;
862 +
863 + if (i >= renamesA->nr) {
864 + compare = 1;
865 + ren2 = renamesB->items[j++].util;
866 + } else if (j >= renamesB->nr) {
867 + compare = -1;
868 + ren1 = renamesA->items[i++].util;
869 + } else {
870 + compare = strcmp(renamesA->items[i].path,
871 + renamesB->items[j].path);
872 + ren1 = renamesA->items[i++].util;
873 + ren2 = renamesB->items[j++].util;
874 + }
875 +
876 /* TODO: refactor, so that 1/2 are not needed */
1024 - if ( ren1 ) {
877 + if (ren1) {
878 renames1 = renamesA;
879 renames2 = renamesB;
880 renames2Dst = &byDstB;
881 branchName1 = branchNameA;
882 branchName2 = branchNameB;
883 } else {
884 + struct rename *tmp;
885 renames1 = renamesB;
886 renames2 = renamesA;
887 renames2Dst = &byDstA;
888 branchName1 = branchNameB;
889 branchName2 = branchNameA;
1036 - struct rename *tmp = ren2;
890 + tmp = ren2;
891 ren2 = ren1;
892 ren1 = tmp;
893 }
894 + src = ren1->pair->one->path;
895
896 ren1->dst_entry->processed = 1;
897 ren1->src_entry->processed = 1;
898
1044 - if ( ren1->processed )
899 + if (ren1->processed)
900 continue;
901 ren1->processed = 1;
902
1048 - const char *ren1_src = ren1->pair->one->path;
1049 - const char *ren1_dst = ren1->pair->two->path;
903 + ren1_src = ren1->pair->one->path;
904 + ren1_dst = ren1->pair->two->path;
905
1051 - if ( ren2 ) {
906 + if (ren2) {
907 const char *ren2_src = ren2->pair->one->path;
908 const char *ren2_dst = ren2->pair->two->path;
909 /* Renamed in 1 and renamed in 2 */
@@ -1067,57 +922,48 @@ static int process_renames(struct path_list *renamesA,
922 } else {
923 remove_file(1, ren1_src);
924 struct merge_file_info mfi;
1070 - mfi = merge_file(ren1_src,
1071 - ren1->pair->one->sha1,
1072 - ren1->pair->one->mode,
1073 - ren1_dst,
1074 - ren1->pair->two->sha1,
1075 - ren1->pair->two->mode,
1076 - ren2_dst,
1077 - ren2->pair->two->sha1,
1078 - ren2->pair->two->mode,
925 + mfi = merge_file(ren1->pair->one,
926 + ren1->pair->two,
927 + ren2->pair->two,
928 branchName1,
929 branchName2);
1081 - if ( mfi.merge || !mfi.clean )
930 + if (mfi.merge || !mfi.clean)
931 output("Renaming %s->%s", src, ren1_dst);
932
1084 - if ( mfi.merge )
933 + if (mfi.merge)
934 output("Auto-merging %s", ren1_dst);
935
1087 - if ( !mfi.clean ) {
936 + if (!mfi.clean) {
937 output("CONFLICT (content): merge conflict in %s",
938 ren1_dst);
939 cleanMerge = 0;
940
1092 - if ( !index_only )
941 + if (!index_only)
942 update_stages(ren1_dst,
1094 - ren1->pair->one->sha1,
1095 - ren1->pair->one->mode,
1096 - ren1->pair->two->sha1,
1097 - ren1->pair->two->mode,
1098 - ren2->pair->two->sha1,
1099 - ren2->pair->two->mode,
943 + ren1->pair->one,
944 + ren1->pair->two,
945 + ren2->pair->two,
946 1 /* clear */);
947 }
948 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
949 }
950 } else {
951 /* Renamed in 1, maybe changed in 2 */
1106 - remove_file(1, ren1_src);
1107 -
1108 - unsigned char srcShaOtherBranch[20], dstShaOtherBranch[20];
1109 - unsigned srcModeOtherBranch, dstModeOtherBranch;
952 + struct path_list_item *item;
953 + /* we only use sha1 and mode of these */
954 + struct diff_filespec src_other, dst_other;
955 + int tryMerge, stage = renamesA == renames1 ? 3: 2;
956
1111 - int stage = renamesA == renames1 ? 3: 2;
1112 -
1113 - memcpy(srcShaOtherBranch, ren1->src_entry->stages[stage].sha, 20);
1114 - srcModeOtherBranch = ren1->src_entry->stages[stage].mode;
957 + remove_file(1, ren1_src);
958
1116 - memcpy(dstShaOtherBranch, ren1->dst_entry->stages[stage].sha, 20);
1117 - dstModeOtherBranch = ren1->dst_entry->stages[stage].mode;
959 + memcpy(src_other.sha1,
960 + ren1->src_entry->stages[stage].sha, 20);
961 + src_other.mode = ren1->src_entry->stages[stage].mode;
962 + memcpy(dst_other.sha1,
963 + ren1->dst_entry->stages[stage].sha, 20);
964 + dst_other.mode = ren1->dst_entry->stages[stage].mode;
965
1119 - int tryMerge = 0;
1120 - char *newPath;
966 + tryMerge = 0;
967
968 if (path_list_has_path(&currentDirectorySet, ren1_dst)) {
969 cleanMerge = 0;
@@ -1126,14 +972,15 @@ static int process_renames(struct path_list *renamesA,
972 ren1_src, ren1_dst, branchName1,
973 ren1_dst, branchName2);
974 conflict_rename_dir(ren1, branchName1);
1129 - } else if ( memcmp(srcShaOtherBranch, null_sha1, 20) == 0 ) {
975 + } else if (sha_eq(src_other.sha1, null_sha1)) {
976 cleanMerge = 0;
977 output("CONFLICT (rename/delete): Rename %s->%s in %s "
978 "and deleted in %s",
979 ren1_src, ren1_dst, branchName1,
980 branchName2);
981 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
1136 - } else if ( memcmp(dstShaOtherBranch, null_sha1, 20) != 0 ) {
982 + } else if (!sha_eq(dst_other.sha1, null_sha1)) {
983 + const char *newPath;
984 cleanMerge = 0;
985 tryMerge = 1;
986 output("CONFLICT (rename/add): Rename %s->%s in %s. "
@@ -1142,8 +989,9 @@ static int process_renames(struct path_list *renamesA,
989 ren1_dst, branchName2);
990 newPath = unique_path(ren1_dst, branchName2);
991 output("Adding as %s instead", newPath);
1145 - update_file(0, dstShaOtherBranch, dstModeOtherBranch, newPath);
1146 - } else if ( (ren2 = find_rename_bysrc(renames2Dst, ren1_dst)) ) {
992 + update_file(0, dst_other.sha1, dst_other.mode, newPath);
993 + } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) {
994 + ren2 = item->util;
995 cleanMerge = 0;
996 ren2->processed = 1;
997 output("CONFLICT (rename/rename): Rename %s->%s in %s. "
@@ -1154,53 +1002,41 @@ static int process_renames(struct path_list *renamesA,
1002 } else
1003 tryMerge = 1;
1004
1157 - if ( tryMerge ) {
1158 - const char *oname = ren1_src;
1159 - const char *aname = ren1_dst;
1160 - const char *bname = ren1_src;
1161 - unsigned char osha[20], asha[20], bsha[20];
1162 - unsigned omode = ren1->pair->one->mode;
1163 - unsigned amode = ren1->pair->two->mode;
1164 - unsigned bmode = srcModeOtherBranch;
1165 - memcpy(osha, ren1->pair->one->sha1, 20);
1166 - memcpy(asha, ren1->pair->two->sha1, 20);
1167 - memcpy(bsha, srcShaOtherBranch, 20);
1168 - const char *aBranch = branchName1;
1169 - const char *bBranch = branchName2;
1170 -
1171 - if ( renamesA != renames1 ) {
1172 - memswp(&aname, &bname, sizeof(aname));
1173 - memswp(asha, bsha, 20);
1174 - memswp(&aBranch, &bBranch, sizeof(aBranch));
1175 - }
1005 + if (tryMerge) {
1006 + struct diff_filespec *o, *a, *b;
1007 struct merge_file_info mfi;
1177 - mfi = merge_file(oname, osha, omode,
1178 - aname, asha, amode,
1179 - bname, bsha, bmode,
1180 - aBranch, bBranch);
1008 + src_other.path = (char *)ren1_src;
1009 +
1010 + o = ren1->pair->one;
1011 + if (renamesA == renames1) {
1012 + a = ren1->pair->two;
1013 + b = &src_other;
1014 + } else {
1015 + b = ren1->pair->two;
1016 + a = &src_other;
1017 + }
1018 + mfi = merge_file(o, a, b,
1019 + branchNameA, branchNameB);
1020
1182 - if ( mfi.merge || !mfi.clean )
1021 + if (mfi.merge || !mfi.clean)
1022 output("Renaming %s => %s", ren1_src, ren1_dst);
1184 - if ( mfi.merge )
1023 + if (mfi.merge)
1024 output("Auto-merging %s", ren1_dst);
1186 - if ( !mfi.clean ) {
1025 + if (!mfi.clean) {
1026 output("CONFLICT (rename/modify): Merge conflict in %s",
1027 ren1_dst);
1028 cleanMerge = 0;
1029
1191 - if ( !index_only )
1030 + if (!index_only)
1031 update_stages(ren1_dst,
1193 - osha, omode,
1194 - asha, amode,
1195 - bsha, bmode,
1196 - 1 /* clear */);
1032 + o, a, b, 1);
1033 }
1034 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1035 }
1036 }
1037 }
1202 - path_list_clear(&srcNames, 0);
1203 - debug(" processRenames done\n");
1038 + path_list_clear(&byDstA, 0);
1039 + path_list_clear(&byDstB, 0);
1040
1041 if (cache_dirty)
1042 flush_cache();
@@ -1229,20 +1065,20 @@ static int process_entry(const char *path, struct stage_data *entry,
1065 unsigned aMode = entry->stages[2].mode;
1066 unsigned bMode = entry->stages[3].mode;
1067
1232 - if ( oSha && (!aSha || !bSha) ) {
1068 + if (oSha && (!aSha || !bSha)) {
1069 /* Case A: Deleted in one */
1234 - if ( (!aSha && !bSha) ||
1235 - (sha_eq(aSha, oSha) && !bSha) ||
1236 - (!aSha && sha_eq(bSha, oSha)) ) {
1070 + if ((!aSha && !bSha) ||
1071 + (sha_eq(aSha, oSha) && !bSha) ||
1072 + (!aSha && sha_eq(bSha, oSha))) {
1073 /* Deleted in both or deleted in one and
1074 * unchanged in the other */
1239 - if ( aSha )
1075 + if (aSha)
1076 output("Removing %s", path);
1077 remove_file(1, path);
1078 } else {
1079 /* Deleted in one and changed in the other */
1080 cleanMerge = 0;
1245 - if ( !aSha ) {
1081 + if (!aSha) {
1082 output("CONFLICT (delete/modify): %s deleted in %s "
1083 "and modified in %s. Version %s of %s left in tree.",
1084 path, branch1Name,
@@ -1257,8 +1093,8 @@ static int process_entry(const char *path, struct stage_data *entry,
1093 }
1094 }
1095
1260 - } else if ( (!oSha && aSha && !bSha) ||
1261 - (!oSha && !aSha && bSha) ) {
1096 + } else if ((!oSha && aSha && !bSha) ||
1097 + (!oSha && !aSha && bSha)) {
1098 /* Case B: Added in one. */
1099 const char *addBranch;
1100 const char *otherBranch;
@@ -1266,7 +1102,7 @@ static int process_entry(const char *path, struct stage_data *entry,
1102 const unsigned char *sha;
1103 const char *conf;
1104
1269 - if ( aSha ) {
1105 + if (aSha) {
1106 addBranch = branch1Name;
1107 otherBranch = branch2Name;
1108 mode = aMode;
@@ -1279,9 +1115,9 @@ static int process_entry(const char *path, struct stage_data *entry,
1115 sha = bSha;
1116 conf = "directory/file";
1117 }
1282 - if ( path_list_has_path(&currentDirectorySet, path) ) {
1283 - cleanMerge = 0;
1118 + if (path_list_has_path(&currentDirectorySet, path)) {
1119 const char *newPath = unique_path(path, addBranch);
1120 + cleanMerge = 0;
1121 output("CONFLICT (%s): There is a directory with name %s in %s. "
1122 "Adding %s as %s",
1123 conf, path, otherBranch, path, newPath);
@@ -1291,10 +1127,10 @@ static int process_entry(const char *path, struct stage_data *entry,
1127 output("Adding %s", path);
1128 update_file(1, sha, mode, path);
1129 }
1294 - } else if ( !oSha && aSha && bSha ) {
1130 + } else if (!oSha && aSha && bSha) {
1131 /* Case C: Added in both (check for same permissions). */
1296 - if ( sha_eq(aSha, bSha) ) {
1297 - if ( aMode != bMode ) {
1132 + if (sha_eq(aSha, bSha)) {
1133 + if (aMode != bMode) {
1134 cleanMerge = 0;
1135 output("CONFLICT: File %s added identically in both branches, "
1136 "but permissions conflict %06o->%06o",
@@ -1306,9 +1142,10 @@ static int process_entry(const char *path, struct stage_data *entry,
1142 assert(0 && "This case must be handled by git-read-tree");
1143 }
1144 } else {
1145 + const char *newPath1, *newPath2;
1146 cleanMerge = 0;
1310 - const char *newPath1 = unique_path(path, branch1Name);
1311 - const char *newPath2 = unique_path(path, branch2Name);
1147 + newPath1 = unique_path(path, branch1Name);
1148 + newPath2 = unique_path(path, branch2Name);
1149 output("CONFLICT (add/add): File %s added non-identically "
1150 "in both branches. Adding as %s and %s instead.",
1151 path, newPath1, newPath2);
@@ -1317,22 +1154,30 @@ static int process_entry(const char *path, struct stage_data *entry,
1154 update_file(0, bSha, bMode, newPath2);
1155 }
1156
1320 - } else if ( oSha && aSha && bSha ) {
1157 + } else if (oSha && aSha && bSha) {
1158 /* case D: Modified in both, but differently. */
1322 - output("Auto-merging %s", path);
1159 struct merge_file_info mfi;
1324 - mfi = merge_file(path, oSha, oMode,
1325 - path, aSha, aMode,
1326 - path, bSha, bMode,
1160 + struct diff_filespec o, a, b;
1161 +
1162 + output("Auto-merging %s", path);
1163 + o.path = a.path = b.path = (char *)path;
1164 + memcpy(o.sha1, oSha, 20);
1165 + o.mode = oMode;
1166 + memcpy(a.sha1, aSha, 20);
1167 + a.mode = aMode;
1168 + memcpy(b.sha1, bSha, 20);
1169 + b.mode = bMode;
1170 +
1171 + mfi = merge_file(&o, &a, &b,
1172 branch1Name, branch2Name);
1173
1329 - if ( mfi.clean )
1174 + if (mfi.clean)
1175 update_file(1, mfi.sha, mfi.mode, path);
1176 else {
1177 cleanMerge = 0;
1178 output("CONFLICT (content): Merge conflict in %s", path);
1179
1335 - if ( index_only )
1180 + if (index_only)
1181 update_file(0, mfi.sha, mfi.mode, path);
1182 else
1183 update_file_flags(mfi.sha, mfi.mode, path,
@@ -1347,73 +1192,68 @@ static int process_entry(const char *path, struct stage_data *entry,
1192 return cleanMerge;
1193 }
1194
1350 -static struct merge_tree_result merge_trees(struct tree *head,
1351 - struct tree *merge,
1352 - struct tree *common,
1353 - const char *branch1Name,
1354 - const char *branch2Name)
1195 +static int merge_trees(struct tree *head,
1196 + struct tree *merge,
1197 + struct tree *common,
1198 + const char *branch1Name,
1199 + const char *branch2Name,
1200 + struct tree **result)
1201 {
1356 - int code;
1357 - struct merge_tree_result result = { NULL, 0 };
1358 - if ( !memcmp(common->object.sha1, merge->object.sha1, 20) ) {
1202 + int code, clean;
1203 + if (sha_eq(common->object.sha1, merge->object.sha1)) {
1204 output("Already uptodate!");
1360 - result.tree = head;
1361 - result.clean = 1;
1362 - return result;
1205 + *result = head;
1206 + return 1;
1207 }
1208
1365 - debug("merge_trees ...\n");
1209 code = git_merge_trees(index_only ? "-i": "-u", common, head, merge);
1210
1368 - if ( code != 0 )
1211 + if (code != 0)
1212 die("merging of trees %s and %s failed",
1213 sha1_to_hex(head->object.sha1),
1214 sha1_to_hex(merge->object.sha1));
1215
1373 - result.tree = git_write_tree();
1216 + *result = git_write_tree();
1217
1375 - if ( !result.tree ) {
1218 + if (!*result) {
1219 + struct path_list *entries, *re_head, *re_merge;
1220 + int i;
1221 path_list_clear(&currentFileSet, 1);
1222 path_list_clear(&currentDirectorySet, 1);
1378 - get_files_dirs(head, &currentFileSet, &currentDirectorySet);
1379 - get_files_dirs(merge, &currentFileSet, &currentDirectorySet);
1223 + get_files_dirs(head);
1224 + get_files_dirs(merge);
1225
1381 - struct path_list *entries = get_unmerged();
1382 - struct path_list *re_head, *re_merge;
1226 + entries = get_unmerged();
1227 re_head = get_renames(head, common, head, merge, entries);
1228 re_merge = get_renames(merge, common, head, merge, entries);
1385 - result.clean = process_renames(re_head, re_merge,
1386 - branch1Name, branch2Name);
1387 - debug("\tprocessing entries...\n");
1388 - int i;
1229 + clean = process_renames(re_head, re_merge,
1230 + branch1Name, branch2Name);
1231 for (i = 0; i < entries->nr; i++) {
1232 const char *path = entries->items[i].path;
1233 struct stage_data *e = entries->items[i].util;
1234 if (e->processed)
1235 continue;
1236 if (!process_entry(path, e, branch1Name, branch2Name))
1395 - result.clean = 0;
1237 + clean = 0;
1238 }
1239
1398 - free_rename_entries(&re_merge);
1399 - free_rename_entries(&re_head);
1400 - free_index_entries(&entries);
1240 + path_list_clear(re_merge, 0);
1241 + path_list_clear(re_head, 0);
1242 + path_list_clear(entries, 1);
1243
1402 - if (result.clean || index_only)
1403 - result.tree = git_write_tree();
1244 + if (clean || index_only)
1245 + *result = git_write_tree();
1246 else
1405 - result.tree = NULL;
1406 - debug("\t processing entries done\n");
1247 + *result = NULL;
1248 } else {
1408 - result.clean = 1;
1249 + clean = 1;
1250 printf("merging of trees %s and %s resulted in %s\n",
1251 sha1_to_hex(head->object.sha1),
1252 sha1_to_hex(merge->object.sha1),
1412 - sha1_to_hex(result.tree->object.sha1));
1253 + sha1_to_hex((*result)->object.sha1));
1254 }
1255
1415 - debug(" merge_trees done\n");
1416 - return result;
1256 + return clean;
1257 }
1258
1259 /*
@@ -1421,76 +1261,75 @@ static struct merge_tree_result merge_trees(struct tree *head,
1261 * commit object and a flag indicating the cleaness of the merge.
1262 */
1263 static
1424 -struct merge_result merge(struct commit *h1,
1264 +int merge(struct commit *h1,
1265 struct commit *h2,
1266 const char *branch1Name,
1267 const char *branch2Name,
1268 int callDepth /* =0 */,
1429 - struct commit *ancestor /* =None */)
1269 + struct commit *ancestor /* =None */,
1270 + struct commit **result)
1271 {
1431 - struct merge_result result = { NULL, 0 };
1432 - const char *msg;
1433 - int msglen;
1272 struct commit_list *ca = NULL, *iter;
1273 struct commit *mergedCA;
1436 - struct merge_tree_result mtr;
1274 + struct tree *mrtree;
1275 + int clean;
1276
1277 output("Merging:");
1439 - msg = commit_title(h1, &msglen);
1440 - /* TODO: refactor. we always show the sha1 with the title */
1441 - output("%s %.*s", commit_hex_sha1(h1), msglen, msg);
1442 - msg = commit_title(h2, &msglen);
1443 - output("%s %.*s", commit_hex_sha1(h2), msglen, msg);
1278 + output_commit_title(h1);
1279 + output_commit_title(h2);
1280
1445 - if ( ancestor )
1281 + if (ancestor)
1282 commit_list_insert(ancestor, &ca);
1283 else
1284 ca = get_merge_bases(h1, h2, 1);
1285
1286 output("found %u common ancestor(s):", commit_list_count(ca));
1451 - for (iter = ca; iter; iter = iter->next) {
1452 - msg = commit_title(iter->item, &msglen);
1453 - output("%s %.*s", commit_hex_sha1(iter->item), msglen, msg);
1454 - }
1287 + for (iter = ca; iter; iter = iter->next)
1288 + output_commit_title(iter->item);
1289
1290 mergedCA = pop_commit(&ca);
1291
1458 - /* TODO: what happens when merge with virtual commits fails? */
1292 for (iter = ca; iter; iter = iter->next) {
1293 output_indent = callDepth + 1;
1461 - result = merge(mergedCA, iter->item,
1462 - "Temporary merge branch 1",
1463 - "Temporary merge branch 2",
1464 - callDepth + 1,
1465 - NULL);
1466 - mergedCA = result.commit;
1294 + /*
1295 + * When the merge fails, the result contains files
1296 + * with conflict markers. The cleanness flag is
1297 + * ignored, it was never acutally used, as result of
1298 + * merge_trees has always overwritten it: the commited
1299 + * "conflicts" were already resolved.
1300 + */
1301 + merge(mergedCA, iter->item,
1302 + "Temporary merge branch 1",
1303 + "Temporary merge branch 2",
1304 + callDepth + 1,
1305 + NULL,
1306 + &mergedCA);
1307 output_indent = callDepth;
1308
1469 - if ( !mergedCA )
1309 + if (!mergedCA)
1310 die("merge returned no commit");
1311 }
1312
1473 - if ( callDepth == 0 ) {
1474 - setup_index(0);
1313 + if (callDepth == 0) {
1314 + setup_index(0 /* $GIT_DIR/index */);
1315 index_only = 0;
1316 } else {
1477 - setup_index(1);
1317 + setup_index(1 /* temporary index */);
1318 git_read_tree(h1->tree);
1319 index_only = 1;
1320 }
1321
1482 - mtr = merge_trees(h1->tree, h2->tree,
1483 - mergedCA->tree, branch1Name, branch2Name);
1322 + clean = merge_trees(h1->tree, h2->tree, mergedCA->tree,
1323 + branch1Name, branch2Name, &mrtree);
1324
1485 - if ( !ancestor && (mtr.clean || index_only) ) {
1486 - result.commit = make_virtual_commit(mtr.tree, "merged tree");
1487 - commit_list_insert(h1, &result.commit->parents);
1488 - commit_list_insert(h2, &result.commit->parents->next);
1325 + if (!ancestor && (clean || index_only)) {
1326 + *result = make_virtual_commit(mrtree, "merged tree");
1327 + commit_list_insert(h1, &(*result)->parents);
1328 + commit_list_insert(h2, &(*result)->parents->next);
1329 } else
1490 - result.commit = NULL;
1330 + *result = NULL;
1331
1492 - result.clean = mtr.clean;
1493 - return result;
1332 + return clean;
1333 }
1334
1335 static struct commit *get_ref(const char *ref)
@@ -1512,6 +1351,9 @@ int main(int argc, char *argv[])
1351 {
1352 static const char *bases[2];
1353 static unsigned bases_count = 0;
1354 + int i, clean;
1355 + const char *branch1, *branch2;
1356 + struct commit *result, *h1, *h2;
1357
1358 original_index_file = getenv("GIT_INDEX_FILE");
1359
@@ -1523,7 +1365,6 @@ int main(int argc, char *argv[])
1365 if (argc < 4)
1366 die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
1367
1526 - int i;
1368 for (i = 1; i < argc; ++i) {
1369 if (!strcmp(argv[i], "--"))
1370 break;
@@ -1533,26 +1374,23 @@ int main(int argc, char *argv[])
1374 if (argc - i != 3) /* "--" "<head>" "<remote>" */
1375 die("Not handling anything other than two heads merge.");
1376
1536 - const char *branch1, *branch2;
1537 -
1377 branch1 = argv[++i];
1378 branch2 = argv[++i];
1379 printf("Merging %s with %s\n", branch1, branch2);
1380
1542 - struct merge_result result;
1543 - struct commit *h1 = get_ref(branch1);
1544 - struct commit *h2 = get_ref(branch2);
1381 + h1 = get_ref(branch1);
1382 + h2 = get_ref(branch2);
1383
1384 if (bases_count == 1) {
1385 struct commit *ancestor = get_ref(bases[0]);
1548 - result = merge(h1, h2, branch1, branch2, 0, ancestor);
1386 + clean = merge(h1, h2, branch1, branch2, 0, ancestor, &result);
1387 } else
1550 - result = merge(h1, h2, branch1, branch2, 0, NULL);
1388 + clean = merge(h1, h2, branch1, branch2, 0, NULL, &result);
1389
1390 if (cache_dirty)
1391 flush_cache();
1392
1555 - return result.clean ? 0: 1;
1393 + return clean ? 0: 1;
1394 }
1395
1396 /*