2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
7 #include "cache-tree.h"
10 #include "tree-walk.h"
13 #include "run-command.h"
15 #include "unpack-trees.h"
16 #include "path-list.h"
17 #include "xdiff-interface.h"
20 * A virtual commit has
21 * - (const char *)commit->util set to the name, and
22 * - *(int *)commit->object.sha1 set to the virtual id.
25 static unsigned commit_list_count(const struct commit_list
*l
)
28 for (; l
; l
= l
->next
)
33 static struct commit
*make_virtual_commit(struct tree
*tree
, const char *comment
)
35 struct commit
*commit
= xcalloc(1, sizeof(struct commit
));
36 static unsigned virtual_id
= 1;
38 commit
->util
= (void*)comment
;
39 *(int*)commit
->object
.sha1
= virtual_id
++;
41 commit
->object
.parsed
= 1;
46 * Since we use get_tree_entry(), which does not put the read object into
47 * the object pool, we cannot rely on a == b.
49 static int sha_eq(const unsigned char *a
, const unsigned char *b
)
53 return a
&& b
&& hashcmp(a
, b
) == 0;
57 * Since we want to write the index eventually, we cannot reuse the index
58 * for these (temporary) data.
65 unsigned char sha
[20];
70 static struct path_list current_file_set
= {NULL
, 0, 0, 1};
71 static struct path_list current_directory_set
= {NULL
, 0, 0, 1};
73 static int output_indent
= 0;
75 static void output(const char *fmt
, ...)
79 for (i
= output_indent
; i
--;)
82 vfprintf(stdout
, fmt
, args
);
87 static void output_commit_title(struct commit
*commit
)
90 for (i
= output_indent
; i
--;)
93 printf("virtual %s\n", (char *)commit
->util
);
95 printf("%s ", find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
));
96 if (parse_commit(commit
) != 0)
97 printf("(bad commit)\n");
101 for (s
= commit
->buffer
; *s
; s
++)
102 if (*s
== '\n' && s
[1] == '\n') {
106 for (len
= 0; s
[len
] && '\n' != s
[len
]; len
++)
108 printf("%.*s\n", len
, s
);
113 static const char *current_index_file
= NULL
;
114 static const char *original_index_file
;
115 static const char *temporary_index_file
;
116 static int cache_dirty
= 0;
118 static int flush_cache(void)
120 /* flush temporary index */
121 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
122 int fd
= hold_lock_file_for_update(lock
, current_index_file
, 1);
123 if (write_cache(fd
, active_cache
, active_nr
) ||
124 close(fd
) || commit_lock_file(lock
))
125 die ("unable to write %s", current_index_file
);
131 static void setup_index(int temp
)
133 current_index_file
= temp
? temporary_index_file
: original_index_file
;
138 unlink(temporary_index_file
);
142 static struct cache_entry
*make_cache_entry(unsigned int mode
,
143 const unsigned char *sha1
, const char *path
, int stage
, int refresh
)
146 struct cache_entry
*ce
;
148 if (!verify_path(path
))
152 size
= cache_entry_size(len
);
153 ce
= xcalloc(1, size
);
155 hashcpy(ce
->sha1
, sha1
);
156 memcpy(ce
->name
, path
, len
);
157 ce
->ce_flags
= create_ce_flags(len
, stage
);
158 ce
->ce_mode
= create_ce_mode(mode
);
161 return refresh_cache_entry(ce
, 0);
166 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
167 const char *path
, int stage
, int refresh
, int options
)
169 struct cache_entry
*ce
;
171 read_cache_from(current_index_file
);
173 ce
= make_cache_entry(mode
, sha1
? sha1
: null_sha1
, path
, stage
, refresh
);
175 return error("cache_addinfo failed: %s", strerror(cache_errno
));
176 return add_cache_entry(ce
, options
);
180 * This is a global variable which is used in a number of places but
181 * only written to in the 'merge' function.
183 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
184 * don't update the working directory.
185 * 0 => Leave unmerged entries in the cache and update
186 * the working directory.
188 static int index_only
= 0;
190 static int git_read_tree(struct tree
*tree
)
193 struct object_list
*trees
= NULL
;
194 struct unpack_trees_options opts
;
197 die("read-tree with dirty cache");
199 memset(&opts
, 0, sizeof(opts
));
200 object_list_append(&tree
->object
, &trees
);
201 rc
= unpack_trees(trees
, &opts
);
202 cache_tree_free(&active_cache_tree
);
210 static int git_merge_trees(int index_only
,
216 struct object_list
*trees
= NULL
;
217 struct unpack_trees_options opts
;
220 read_cache_from(current_index_file
);
224 memset(&opts
, 0, sizeof(opts
));
231 opts
.fn
= threeway_merge
;
233 object_list_append(&common
->object
, &trees
);
234 object_list_append(&head
->object
, &trees
);
235 object_list_append(&merge
->object
, &trees
);
237 rc
= unpack_trees(trees
, &opts
);
238 cache_tree_free(&active_cache_tree
);
245 static struct tree
*git_write_tree(void)
247 struct tree
*result
= NULL
;
251 for (i
= 0; i
< active_nr
; i
++) {
252 struct cache_entry
*ce
= active_cache
[i
];
257 read_cache_from(current_index_file
);
259 if (!active_cache_tree
)
260 active_cache_tree
= cache_tree();
262 if (!cache_tree_fully_valid(active_cache_tree
) &&
263 cache_tree_update(active_cache_tree
,
264 active_cache
, active_nr
, 0, 0) < 0)
265 die("error building trees");
267 result
= lookup_tree(active_cache_tree
->sha1
);
275 static int save_files_dirs(const unsigned char *sha1
,
276 const char *base
, int baselen
, const char *path
,
277 unsigned int mode
, int stage
)
279 int len
= strlen(path
);
280 char *newpath
= xmalloc(baselen
+ len
+ 1);
281 memcpy(newpath
, base
, baselen
);
282 memcpy(newpath
+ baselen
, path
, len
);
283 newpath
[baselen
+ len
] = '\0';
286 path_list_insert(newpath
, ¤t_directory_set
);
288 path_list_insert(newpath
, ¤t_file_set
);
291 return READ_TREE_RECURSIVE
;
294 static int get_files_dirs(struct tree
*tree
)
297 if (read_tree_recursive(tree
, "", 0, 0, NULL
, save_files_dirs
) != 0)
299 n
= current_file_set
.nr
+ current_directory_set
.nr
;
304 * Returns a index_entry instance which doesn't have to correspond to
305 * a real cache entry in Git's index.
307 static struct stage_data
*insert_stage_data(const char *path
,
308 struct tree
*o
, struct tree
*a
, struct tree
*b
,
309 struct path_list
*entries
)
311 struct path_list_item
*item
;
312 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
313 get_tree_entry(o
->object
.sha1
, path
,
314 e
->stages
[1].sha
, &e
->stages
[1].mode
);
315 get_tree_entry(a
->object
.sha1
, path
,
316 e
->stages
[2].sha
, &e
->stages
[2].mode
);
317 get_tree_entry(b
->object
.sha1
, path
,
318 e
->stages
[3].sha
, &e
->stages
[3].mode
);
319 item
= path_list_insert(path
, entries
);
325 * Create a dictionary mapping file names to stage_data objects. The
326 * dictionary contains one entry for every path with a non-zero stage entry.
328 static struct path_list
*get_unmerged(void)
330 struct path_list
*unmerged
= xcalloc(1, sizeof(struct path_list
));
333 unmerged
->strdup_paths
= 1;
335 read_cache_from(current_index_file
);
338 for (i
= 0; i
< active_nr
; i
++) {
339 struct path_list_item
*item
;
340 struct stage_data
*e
;
341 struct cache_entry
*ce
= active_cache
[i
];
345 item
= path_list_lookup(ce
->name
, unmerged
);
347 item
= path_list_insert(ce
->name
, unmerged
);
348 item
->util
= xcalloc(1, sizeof(struct stage_data
));
351 e
->stages
[ce_stage(ce
)].mode
= ntohl(ce
->ce_mode
);
352 hashcpy(e
->stages
[ce_stage(ce
)].sha
, ce
->sha1
);
360 struct diff_filepair
*pair
;
361 struct stage_data
*src_entry
;
362 struct stage_data
*dst_entry
;
363 unsigned processed
:1;
367 * Get information of all renames which occured between 'o_tree' and
368 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
369 * 'b_tree') to be able to associate the correct cache entries with
370 * the rename information. 'tree' is always equal to either a_tree or b_tree.
372 static struct path_list
*get_renames(struct tree
*tree
,
376 struct path_list
*entries
)
379 struct path_list
*renames
;
380 struct diff_options opts
;
382 renames
= xcalloc(1, sizeof(struct path_list
));
385 opts
.detect_rename
= DIFF_DETECT_RENAME
;
386 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
387 if (diff_setup_done(&opts
) < 0)
388 die("diff setup failed");
389 diff_tree_sha1(o_tree
->object
.sha1
, tree
->object
.sha1
, "", &opts
);
391 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
392 struct path_list_item
*item
;
394 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
395 if (pair
->status
!= 'R') {
396 diff_free_filepair(pair
);
399 re
= xmalloc(sizeof(*re
));
402 item
= path_list_lookup(re
->pair
->one
->path
, entries
);
404 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
405 o_tree
, a_tree
, b_tree
, entries
);
407 re
->src_entry
= item
->util
;
409 item
= path_list_lookup(re
->pair
->two
->path
, entries
);
411 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
412 o_tree
, a_tree
, b_tree
, entries
);
414 re
->dst_entry
= item
->util
;
415 item
= path_list_insert(pair
->one
->path
, renames
);
418 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
419 diff_queued_diff
.nr
= 0;
424 static int update_stages(const char *path
, struct diff_filespec
*o
,
425 struct diff_filespec
*a
, struct diff_filespec
*b
,
428 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
430 if (remove_file_from_cache(path
))
433 if (add_cacheinfo(o
->mode
, o
->sha1
, path
, 1, 0, options
))
436 if (add_cacheinfo(a
->mode
, a
->sha1
, path
, 2, 0, options
))
439 if (add_cacheinfo(b
->mode
, b
->sha1
, path
, 3, 0, options
))
444 static int remove_path(const char *name
)
453 dirs
= xmalloc(len
+1);
454 memcpy(dirs
, name
, len
);
456 while ((slash
= strrchr(name
, '/'))) {
459 if (rmdir(name
) != 0)
466 static int remove_file(int clean
, const char *path
, int no_wd
)
468 int update_cache
= index_only
|| clean
;
469 int update_working_directory
= !index_only
&& !no_wd
;
473 read_cache_from(current_index_file
);
475 if (remove_file_from_cache(path
))
478 if (update_working_directory
) {
480 if (errno
!= ENOENT
|| errno
!= EISDIR
)
487 static char *unique_path(const char *path
, const char *branch
)
489 char *newpath
= xmalloc(strlen(path
) + 1 + strlen(branch
) + 8 + 1);
492 char *p
= newpath
+ strlen(path
);
493 strcpy(newpath
, path
);
499 while (path_list_has_path(¤t_file_set
, newpath
) ||
500 path_list_has_path(¤t_directory_set
, newpath
) ||
501 lstat(newpath
, &st
) == 0)
502 sprintf(p
, "_%d", suffix
++);
504 path_list_insert(newpath
, ¤t_file_set
);
508 static int mkdir_p(const char *path
, unsigned long mode
)
510 /* path points to cache entries, so xstrdup before messing with it */
511 char *buf
= xstrdup(path
);
512 int result
= safe_create_leading_directories(buf
);
517 static void flush_buffer(int fd
, const char *buf
, unsigned long size
)
520 long ret
= xwrite(fd
, buf
, size
);
525 die("merge-recursive: %s", strerror(errno
));
527 die("merge-recursive: disk full?");
534 static void update_file_flags(const unsigned char *sha
,
548 buf
= read_sha1_file(sha
, type
, &size
);
550 die("cannot read object %s '%s'", sha1_to_hex(sha
), path
);
551 if (strcmp(type
, blob_type
) != 0)
552 die("blob expected for %s '%s'", sha1_to_hex(sha
), path
);
556 if (mkdir_p(path
, 0777))
557 die("failed to create path %s: %s", path
, strerror(errno
));
563 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
565 die("failed to open %s: %s", path
, strerror(errno
));
566 flush_buffer(fd
, buf
, size
);
568 } else if (S_ISLNK(mode
)) {
569 char *lnk
= xmalloc(size
+ 1);
570 memcpy(lnk
, buf
, size
);
576 die("do not know what to do with %06o %s '%s'",
577 mode
, sha1_to_hex(sha
), path
);
580 add_cacheinfo(mode
, sha
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
583 static void update_file(int clean
,
584 const unsigned char *sha
,
588 update_file_flags(sha
, mode
, path
, index_only
|| clean
, !index_only
);
591 /* Low level file merging, update and removal */
593 struct merge_file_info
595 unsigned char sha
[20];
601 static void fill_mm(const unsigned char *sha1
, mmfile_t
*mm
)
606 if (!hashcmp(sha1
, null_sha1
)) {
607 mm
->ptr
= xstrdup("");
612 mm
->ptr
= read_sha1_file(sha1
, type
, &size
);
613 if (!mm
->ptr
|| strcmp(type
, blob_type
))
614 die("unable to read blob object %s", sha1_to_hex(sha1
));
618 static struct merge_file_info
merge_file(struct diff_filespec
*o
,
619 struct diff_filespec
*a
, struct diff_filespec
*b
,
620 const char *branch1
, const char *branch2
)
622 struct merge_file_info result
;
626 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
628 if (S_ISREG(a
->mode
)) {
629 result
.mode
= a
->mode
;
630 hashcpy(result
.sha
, a
->sha1
);
632 result
.mode
= b
->mode
;
633 hashcpy(result
.sha
, b
->sha1
);
636 if (!sha_eq(a
->sha1
, o
->sha1
) && !sha_eq(b
->sha1
, o
->sha1
))
639 result
.mode
= a
->mode
== o
->mode
? b
->mode
: a
->mode
;
641 if (sha_eq(a
->sha1
, o
->sha1
))
642 hashcpy(result
.sha
, b
->sha1
);
643 else if (sha_eq(b
->sha1
, o
->sha1
))
644 hashcpy(result
.sha
, a
->sha1
);
645 else if (S_ISREG(a
->mode
)) {
646 mmfile_t orig
, src1
, src2
;
647 mmbuffer_t result_buf
;
652 name1
= xstrdup(mkpath("%s/%s", branch1
, a
->path
));
653 name2
= xstrdup(mkpath("%s/%s", branch2
, b
->path
));
655 fill_mm(o
->sha1
, &orig
);
656 fill_mm(a
->sha1
, &src1
);
657 fill_mm(b
->sha1
, &src2
);
659 memset(&xpp
, 0, sizeof(xpp
));
660 merge_status
= xdl_merge(&orig
,
663 &xpp
, XDL_MERGE_ZEALOUS
,
671 if ((merge_status
< 0) || !result_buf
.ptr
)
672 die("Failed to execute internal merge");
674 if (write_sha1_file(result_buf
.ptr
, result_buf
.size
,
675 blob_type
, result
.sha
))
676 die("Unable to add %s to database",
679 free(result_buf
.ptr
);
680 result
.clean
= (merge_status
== 0);
682 if (!(S_ISLNK(a
->mode
) || S_ISLNK(b
->mode
)))
683 die("cannot merge modes?");
685 hashcpy(result
.sha
, a
->sha1
);
687 if (!sha_eq(a
->sha1
, b
->sha1
))
695 static void conflict_rename_rename(struct rename
*ren1
,
702 const char *ren1_dst
= ren1
->pair
->two
->path
;
703 const char *ren2_dst
= ren2
->pair
->two
->path
;
704 const char *dst_name1
= ren1_dst
;
705 const char *dst_name2
= ren2_dst
;
706 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
707 dst_name1
= del
[delp
++] = unique_path(ren1_dst
, branch1
);
708 output("%s is a directory in %s adding as %s instead",
709 ren1_dst
, branch2
, dst_name1
);
710 remove_file(0, ren1_dst
, 0);
712 if (path_list_has_path(¤t_directory_set
, ren2_dst
)) {
713 dst_name2
= del
[delp
++] = unique_path(ren2_dst
, branch2
);
714 output("%s is a directory in %s adding as %s instead",
715 ren2_dst
, branch1
, dst_name2
);
716 remove_file(0, ren2_dst
, 0);
718 update_stages(dst_name1
, NULL
, ren1
->pair
->two
, NULL
, 1);
719 update_stages(dst_name2
, NULL
, NULL
, ren2
->pair
->two
, 1);
724 static void conflict_rename_dir(struct rename
*ren1
,
727 char *new_path
= unique_path(ren1
->pair
->two
->path
, branch1
);
728 output("Renaming %s to %s instead", ren1
->pair
->one
->path
, new_path
);
729 remove_file(0, ren1
->pair
->two
->path
, 0);
730 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path
);
734 static void conflict_rename_rename_2(struct rename
*ren1
,
739 char *new_path1
= unique_path(ren1
->pair
->two
->path
, branch1
);
740 char *new_path2
= unique_path(ren2
->pair
->two
->path
, branch2
);
741 output("Renaming %s to %s and %s to %s instead",
742 ren1
->pair
->one
->path
, new_path1
,
743 ren2
->pair
->one
->path
, new_path2
);
744 remove_file(0, ren1
->pair
->two
->path
, 0);
745 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path1
);
746 update_file(0, ren2
->pair
->two
->sha1
, ren2
->pair
->two
->mode
, new_path2
);
751 static int process_renames(struct path_list
*a_renames
,
752 struct path_list
*b_renames
,
753 const char *a_branch
,
754 const char *b_branch
)
756 int clean_merge
= 1, i
, j
;
757 struct path_list a_by_dst
= {NULL
, 0, 0, 0}, b_by_dst
= {NULL
, 0, 0, 0};
758 const struct rename
*sre
;
760 for (i
= 0; i
< a_renames
->nr
; i
++) {
761 sre
= a_renames
->items
[i
].util
;
762 path_list_insert(sre
->pair
->two
->path
, &a_by_dst
)->util
765 for (i
= 0; i
< b_renames
->nr
; i
++) {
766 sre
= b_renames
->items
[i
].util
;
767 path_list_insert(sre
->pair
->two
->path
, &b_by_dst
)->util
771 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
774 struct path_list
*renames1
, *renames2
, *renames2Dst
;
775 struct rename
*ren1
= NULL
, *ren2
= NULL
;
776 const char *branch1
, *branch2
;
777 const char *ren1_src
, *ren1_dst
;
779 if (i
>= a_renames
->nr
) {
781 ren2
= b_renames
->items
[j
++].util
;
782 } else if (j
>= b_renames
->nr
) {
784 ren1
= a_renames
->items
[i
++].util
;
786 compare
= strcmp(a_renames
->items
[i
].path
,
787 b_renames
->items
[j
].path
);
789 ren1
= a_renames
->items
[i
++].util
;
791 ren2
= b_renames
->items
[j
++].util
;
794 /* TODO: refactor, so that 1/2 are not needed */
796 renames1
= a_renames
;
797 renames2
= b_renames
;
798 renames2Dst
= &b_by_dst
;
803 renames1
= b_renames
;
804 renames2
= a_renames
;
805 renames2Dst
= &a_by_dst
;
812 src
= ren1
->pair
->one
->path
;
814 ren1
->dst_entry
->processed
= 1;
815 ren1
->src_entry
->processed
= 1;
821 ren1_src
= ren1
->pair
->one
->path
;
822 ren1_dst
= ren1
->pair
->two
->path
;
825 const char *ren2_src
= ren2
->pair
->one
->path
;
826 const char *ren2_dst
= ren2
->pair
->two
->path
;
827 /* Renamed in 1 and renamed in 2 */
828 if (strcmp(ren1_src
, ren2_src
) != 0)
829 die("ren1.src != ren2.src");
830 ren2
->dst_entry
->processed
= 1;
832 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
834 output("CONFLICT (rename/rename): "
835 "Rename %s->%s in branch %s "
836 "rename %s->%s in %s",
837 src
, ren1_dst
, branch1
,
838 src
, ren2_dst
, branch2
);
839 conflict_rename_rename(ren1
, branch1
, ren2
, branch2
);
841 struct merge_file_info mfi
;
842 remove_file(1, ren1_src
, 1);
843 mfi
= merge_file(ren1
->pair
->one
,
848 if (mfi
.merge
|| !mfi
.clean
)
849 output("Renaming %s->%s", src
, ren1_dst
);
852 output("Auto-merging %s", ren1_dst
);
855 output("CONFLICT (content): merge conflict in %s",
860 update_stages(ren1_dst
,
866 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
869 /* Renamed in 1, maybe changed in 2 */
870 struct path_list_item
*item
;
871 /* we only use sha1 and mode of these */
872 struct diff_filespec src_other
, dst_other
;
873 int try_merge
, stage
= a_renames
== renames1
? 3: 2;
875 remove_file(1, ren1_src
, index_only
);
877 hashcpy(src_other
.sha1
, ren1
->src_entry
->stages
[stage
].sha
);
878 src_other
.mode
= ren1
->src_entry
->stages
[stage
].mode
;
879 hashcpy(dst_other
.sha1
, ren1
->dst_entry
->stages
[stage
].sha
);
880 dst_other
.mode
= ren1
->dst_entry
->stages
[stage
].mode
;
884 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
886 output("CONFLICT (rename/directory): Rename %s->%s in %s "
887 " directory %s added in %s",
888 ren1_src
, ren1_dst
, branch1
,
890 conflict_rename_dir(ren1
, branch1
);
891 } else if (sha_eq(src_other
.sha1
, null_sha1
)) {
893 output("CONFLICT (rename/delete): Rename %s->%s in %s "
895 ren1_src
, ren1_dst
, branch1
,
897 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, ren1_dst
);
898 } else if (!sha_eq(dst_other
.sha1
, null_sha1
)) {
899 const char *new_path
;
902 output("CONFLICT (rename/add): Rename %s->%s in %s. "
904 ren1_src
, ren1_dst
, branch1
,
906 new_path
= unique_path(ren1_dst
, branch2
);
907 output("Adding as %s instead", new_path
);
908 update_file(0, dst_other
.sha1
, dst_other
.mode
, new_path
);
909 } else if ((item
= path_list_lookup(ren1_dst
, renames2Dst
))) {
913 output("CONFLICT (rename/rename): Rename %s->%s in %s. "
914 "Rename %s->%s in %s",
915 ren1_src
, ren1_dst
, branch1
,
916 ren2
->pair
->one
->path
, ren2
->pair
->two
->path
, branch2
);
917 conflict_rename_rename_2(ren1
, branch1
, ren2
, branch2
);
922 struct diff_filespec
*o
, *a
, *b
;
923 struct merge_file_info mfi
;
924 src_other
.path
= (char *)ren1_src
;
927 if (a_renames
== renames1
) {
934 mfi
= merge_file(o
, a
, b
,
937 if (mfi
.merge
|| !mfi
.clean
)
938 output("Renaming %s => %s", ren1_src
, ren1_dst
);
940 output("Auto-merging %s", ren1_dst
);
942 output("CONFLICT (rename/modify): Merge conflict in %s",
947 update_stages(ren1_dst
,
950 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
954 path_list_clear(&a_by_dst
, 0);
955 path_list_clear(&b_by_dst
, 0);
962 static unsigned char *has_sha(const unsigned char *sha
)
964 return is_null_sha1(sha
) ? NULL
: (unsigned char *)sha
;
967 /* Per entry merge function */
968 static int process_entry(const char *path
, struct stage_data
*entry
,
973 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
974 print_index_entry("\tpath: ", entry);
977 unsigned char *o_sha
= has_sha(entry
->stages
[1].sha
);
978 unsigned char *a_sha
= has_sha(entry
->stages
[2].sha
);
979 unsigned char *b_sha
= has_sha(entry
->stages
[3].sha
);
980 unsigned o_mode
= entry
->stages
[1].mode
;
981 unsigned a_mode
= entry
->stages
[2].mode
;
982 unsigned b_mode
= entry
->stages
[3].mode
;
984 if (o_sha
&& (!a_sha
|| !b_sha
)) {
985 /* Case A: Deleted in one */
986 if ((!a_sha
&& !b_sha
) ||
987 (sha_eq(a_sha
, o_sha
) && !b_sha
) ||
988 (!a_sha
&& sha_eq(b_sha
, o_sha
))) {
989 /* Deleted in both or deleted in one and
990 * unchanged in the other */
992 output("Removing %s", path
);
993 /* do not touch working file if it did not exist */
994 remove_file(1, path
, !a_sha
);
996 /* Deleted in one and changed in the other */
999 output("CONFLICT (delete/modify): %s deleted in %s "
1000 "and modified in %s. Version %s of %s left in tree.",
1002 branch2
, branch2
, path
);
1003 update_file(0, b_sha
, b_mode
, path
);
1005 output("CONFLICT (delete/modify): %s deleted in %s "
1006 "and modified in %s. Version %s of %s left in tree.",
1008 branch1
, branch1
, path
);
1009 update_file(0, a_sha
, a_mode
, path
);
1013 } else if ((!o_sha
&& a_sha
&& !b_sha
) ||
1014 (!o_sha
&& !a_sha
&& b_sha
)) {
1015 /* Case B: Added in one. */
1016 const char *add_branch
;
1017 const char *other_branch
;
1019 const unsigned char *sha
;
1023 add_branch
= branch1
;
1024 other_branch
= branch2
;
1027 conf
= "file/directory";
1029 add_branch
= branch2
;
1030 other_branch
= branch1
;
1033 conf
= "directory/file";
1035 if (path_list_has_path(¤t_directory_set
, path
)) {
1036 const char *new_path
= unique_path(path
, add_branch
);
1038 output("CONFLICT (%s): There is a directory with name %s in %s. "
1040 conf
, path
, other_branch
, path
, new_path
);
1041 remove_file(0, path
, 0);
1042 update_file(0, sha
, mode
, new_path
);
1044 output("Adding %s", path
);
1045 update_file(1, sha
, mode
, path
);
1047 } else if (a_sha
&& b_sha
) {
1048 /* Case C: Added in both (check for same permissions) and */
1049 /* case D: Modified in both, but differently. */
1050 const char *reason
= "content";
1051 struct merge_file_info mfi
;
1052 struct diff_filespec o
, a
, b
;
1056 o_sha
= (unsigned char *)null_sha1
;
1058 output("Auto-merging %s", path
);
1059 o
.path
= a
.path
= b
.path
= (char *)path
;
1060 hashcpy(o
.sha1
, o_sha
);
1062 hashcpy(a
.sha1
, a_sha
);
1064 hashcpy(b
.sha1
, b_sha
);
1067 mfi
= merge_file(&o
, &a
, &b
,
1071 update_file(1, mfi
.sha
, mfi
.mode
, path
);
1074 output("CONFLICT (%s): Merge conflict in %s",
1078 update_file(0, mfi
.sha
, mfi
.mode
, path
);
1080 update_file_flags(mfi
.sha
, mfi
.mode
, path
,
1081 0 /* update_cache */, 1 /* update_working_directory */);
1084 die("Fatal merge failure, shouldn't happen.");
1092 static int merge_trees(struct tree
*head
,
1094 struct tree
*common
,
1095 const char *branch1
,
1096 const char *branch2
,
1097 struct tree
**result
)
1100 if (sha_eq(common
->object
.sha1
, merge
->object
.sha1
)) {
1101 output("Already uptodate!");
1106 code
= git_merge_trees(index_only
, common
, head
, merge
);
1109 die("merging of trees %s and %s failed",
1110 sha1_to_hex(head
->object
.sha1
),
1111 sha1_to_hex(merge
->object
.sha1
));
1113 *result
= git_write_tree();
1116 struct path_list
*entries
, *re_head
, *re_merge
;
1118 path_list_clear(¤t_file_set
, 1);
1119 path_list_clear(¤t_directory_set
, 1);
1120 get_files_dirs(head
);
1121 get_files_dirs(merge
);
1123 entries
= get_unmerged();
1124 re_head
= get_renames(head
, common
, head
, merge
, entries
);
1125 re_merge
= get_renames(merge
, common
, head
, merge
, entries
);
1126 clean
= process_renames(re_head
, re_merge
,
1128 for (i
= 0; i
< entries
->nr
; i
++) {
1129 const char *path
= entries
->items
[i
].path
;
1130 struct stage_data
*e
= entries
->items
[i
].util
;
1133 if (!process_entry(path
, e
, branch1
, branch2
))
1137 path_list_clear(re_merge
, 0);
1138 path_list_clear(re_head
, 0);
1139 path_list_clear(entries
, 1);
1141 if (clean
|| index_only
)
1142 *result
= git_write_tree();
1147 printf("merging of trees %s and %s resulted in %s\n",
1148 sha1_to_hex(head
->object
.sha1
),
1149 sha1_to_hex(merge
->object
.sha1
),
1150 sha1_to_hex((*result
)->object
.sha1
));
1156 static struct commit_list
*reverse_commit_list(struct commit_list
*list
)
1158 struct commit_list
*next
= NULL
, *current
, *backup
;
1159 for (current
= list
; current
; current
= backup
) {
1160 backup
= current
->next
;
1161 current
->next
= next
;
1168 * Merge the commits h1 and h2, return the resulting virtual
1169 * commit object and a flag indicating the cleaness of the merge.
1171 static int merge(struct commit
*h1
,
1173 const char *branch1
,
1174 const char *branch2
,
1175 int call_depth
/* =0 */,
1176 struct commit
*ancestor
/* =None */,
1177 struct commit
**result
)
1179 struct commit_list
*ca
= NULL
, *iter
;
1180 struct commit
*merged_common_ancestors
;
1181 struct tree
*mrtree
;
1185 output_commit_title(h1
);
1186 output_commit_title(h2
);
1189 commit_list_insert(ancestor
, &ca
);
1191 ca
= reverse_commit_list(get_merge_bases(h1
, h2
, 1));
1193 output("found %u common ancestor(s):", commit_list_count(ca
));
1194 for (iter
= ca
; iter
; iter
= iter
->next
)
1195 output_commit_title(iter
->item
);
1197 merged_common_ancestors
= pop_commit(&ca
);
1198 if (merged_common_ancestors
== NULL
) {
1199 /* if there is no common ancestor, make an empty tree */
1200 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
1202 tree
->object
.parsed
= 1;
1203 tree
->object
.type
= OBJ_TREE
;
1204 write_sha1_file(NULL
, 0, tree_type
, tree
->object
.sha1
);
1205 merged_common_ancestors
= make_virtual_commit(tree
, "ancestor");
1208 for (iter
= ca
; iter
; iter
= iter
->next
) {
1209 output_indent
= call_depth
+ 1;
1211 * When the merge fails, the result contains files
1212 * with conflict markers. The cleanness flag is
1213 * ignored, it was never acutally used, as result of
1214 * merge_trees has always overwritten it: the commited
1215 * "conflicts" were already resolved.
1217 merge(merged_common_ancestors
, iter
->item
,
1218 "Temporary merge branch 1",
1219 "Temporary merge branch 2",
1222 &merged_common_ancestors
);
1223 output_indent
= call_depth
;
1225 if (!merged_common_ancestors
)
1226 die("merge returned no commit");
1229 if (call_depth
== 0) {
1230 setup_index(0 /* $GIT_DIR/index */);
1233 setup_index(1 /* temporary index */);
1234 git_read_tree(h1
->tree
);
1238 clean
= merge_trees(h1
->tree
, h2
->tree
, merged_common_ancestors
->tree
,
1239 branch1
, branch2
, &mrtree
);
1241 if (!ancestor
&& (clean
|| index_only
)) {
1242 *result
= make_virtual_commit(mrtree
, "merged tree");
1243 commit_list_insert(h1
, &(*result
)->parents
);
1244 commit_list_insert(h2
, &(*result
)->parents
->next
);
1251 static struct commit
*get_ref(const char *ref
)
1253 unsigned char sha1
[20];
1254 struct object
*object
;
1256 if (get_sha1(ref
, sha1
))
1257 die("Could not resolve ref '%s'", ref
);
1258 object
= deref_tag(parse_object(sha1
), ref
, strlen(ref
));
1259 if (object
->type
!= OBJ_COMMIT
)
1261 if (parse_commit((struct commit
*)object
))
1262 die("Could not parse commit '%s'", sha1_to_hex(object
->sha1
));
1263 return (struct commit
*)object
;
1266 int main(int argc
, char *argv
[])
1268 static const char *bases
[2];
1269 static unsigned bases_count
= 0;
1271 const char *branch1
, *branch2
;
1272 struct commit
*result
, *h1
, *h2
;
1274 git_config(git_default_config
); /* core.filemode */
1275 original_index_file
= getenv(INDEX_ENVIRONMENT
);
1277 if (!original_index_file
)
1278 original_index_file
= xstrdup(git_path("index"));
1280 temporary_index_file
= xstrdup(git_path("mrg-rcrsv-tmp-idx"));
1283 die("Usage: %s <base>... -- <head> <remote> ...\n", argv
[0]);
1285 for (i
= 1; i
< argc
; ++i
) {
1286 if (!strcmp(argv
[i
], "--"))
1288 if (bases_count
< sizeof(bases
)/sizeof(*bases
))
1289 bases
[bases_count
++] = argv
[i
];
1291 if (argc
- i
!= 3) /* "--" "<head>" "<remote>" */
1292 die("Not handling anything other than two heads merge.");
1294 branch1
= argv
[++i
];
1295 branch2
= argv
[++i
];
1296 printf("Merging %s with %s\n", branch1
, branch2
);
1298 h1
= get_ref(branch1
);
1299 h2
= get_ref(branch2
);
1301 if (bases_count
== 1) {
1302 struct commit
*ancestor
= get_ref(bases
[0]);
1303 clean
= merge(h1
, h2
, branch1
, branch2
, 0, ancestor
, &result
);
1305 clean
= merge(h1
, h2
, branch1
, branch2
, 0, NULL
, &result
);
1310 return clean
? 0: 1;