2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
10 #include <sys/types.h>
14 #include "cache-tree.h"
17 #include "tree-walk.h"
20 #include "run-command.h"
22 #include "unpack-trees.h"
23 #include "path-list.h"
26 * A virtual commit has
27 * - (const char *)commit->util set to the name, and
28 * - *(int *)commit->object.sha1 set to the virtual id.
31 static unsigned commit_list_count(const struct commit_list
*l
)
34 for (; l
; l
= l
->next
)
39 static struct commit
*make_virtual_commit(struct tree
*tree
, const char *comment
)
41 struct commit
*commit
= xcalloc(1, sizeof(struct commit
));
42 static unsigned virtual_id
= 1;
44 commit
->util
= (void*)comment
;
45 *(int*)commit
->object
.sha1
= virtual_id
++;
50 * Since we use get_tree_entry(), which does not put the read object into
51 * the object pool, we cannot rely on a == b.
53 static int sha_eq(const unsigned char *a
, const unsigned char *b
)
57 return a
&& b
&& memcmp(a
, b
, 20) == 0;
61 * Since we want to write the index eventually, we cannot reuse the index
62 * for these (temporary) data.
69 unsigned char sha
[20];
74 static struct path_list current_file_set
= {NULL
, 0, 0, 1};
75 static struct path_list current_directory_set
= {NULL
, 0, 0, 1};
77 static int output_indent
= 0;
79 static void output(const char *fmt
, ...)
83 for (i
= output_indent
; i
--;)
86 vfprintf(stdout
, fmt
, args
);
91 static void output_commit_title(struct commit
*commit
)
94 for (i
= output_indent
; i
--;)
97 printf("virtual %s\n", (char *)commit
->util
);
99 printf("%s ", sha1_to_hex(commit
->object
.sha1
));
100 if (parse_commit(commit
) != 0)
101 printf("(bad commit)\n");
105 for (s
= commit
->buffer
; *s
; s
++)
106 if (*s
== '\n' && s
[1] == '\n') {
110 for (len
= 0; s
[len
] && '\n' != s
[len
]; len
++)
112 printf("%.*s\n", len
, s
);
117 static const char *original_index_file
;
118 static const char *temporary_index_file
;
119 static int cache_dirty
= 0;
121 static int flush_cache(void)
123 /* flush temporary index */
124 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
125 int fd
= hold_lock_file_for_update(lock
, getenv("GIT_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"));
136 static void setup_index(int temp
)
138 const char *idx
= temp
? temporary_index_file
: original_index_file
;
140 die("fatal: cache changed flush_cache();");
141 unlink(temporary_index_file
);
142 setenv("GIT_INDEX_FILE", idx
, 1);
146 static struct cache_entry
*make_cache_entry(unsigned int mode
,
147 const unsigned char *sha1
, const char *path
, int stage
, int refresh
)
150 struct cache_entry
*ce
;
152 if (!verify_path(path
))
156 size
= cache_entry_size(len
);
157 ce
= xcalloc(1, size
);
159 memcpy(ce
->sha1
, sha1
, 20);
160 memcpy(ce
->name
, path
, len
);
161 ce
->ce_flags
= create_ce_flags(len
, stage
);
162 ce
->ce_mode
= create_ce_mode(mode
);
165 return refresh_cache_entry(ce
, 0);
170 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
171 const char *path
, int stage
, int refresh
, int options
)
173 struct cache_entry
*ce
;
175 read_cache_from(getenv("GIT_INDEX_FILE"));
177 ce
= make_cache_entry(mode
, sha1
? sha1
: null_sha1
, path
, stage
, refresh
);
179 return error("cache_addinfo failed: %s", strerror(cache_errno
));
180 return add_cache_entry(ce
, options
);
184 * This is a global variable which is used in a number of places but
185 * only written to in the 'merge' function.
187 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
188 * don't update the working directory.
189 * 0 => Leave unmerged entries in the cache and update
190 * the working directory.
192 static int index_only
= 0;
194 static int git_read_tree(struct tree
*tree
)
197 struct object_list
*trees
= NULL
;
198 struct unpack_trees_options opts
;
201 die("read-tree with dirty cache");
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
);
214 static int git_merge_trees(int index_only
,
220 struct object_list
*trees
= NULL
;
221 struct unpack_trees_options opts
;
224 read_cache_from(getenv("GIT_INDEX_FILE"));
228 memset(&opts
, 0, sizeof(opts
));
235 opts
.fn
= threeway_merge
;
237 object_list_append(&common
->object
, &trees
);
238 object_list_append(&head
->object
, &trees
);
239 object_list_append(&merge
->object
, &trees
);
241 rc
= unpack_trees(trees
, &opts
);
242 cache_tree_free(&active_cache_tree
);
250 * TODO: this can be streamlined by refactoring builtin-write-tree.c
252 static struct tree
*git_write_tree(void)
257 unsigned char sha1
[20];
261 for (i
= 0; i
< active_nr
; i
++) {
262 struct cache_entry
*ce
= active_cache
[i
];
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')
275 if (rc
== -1 || WEXITSTATUS(rc
))
278 if (get_sha1(buf
, sha1
) != 0)
280 return lookup_tree(sha1
);
283 static int save_files_dirs(const unsigned char *sha1
,
284 const char *base
, int baselen
, const char *path
,
285 unsigned int mode
, int stage
)
287 int len
= strlen(path
);
288 char *newpath
= malloc(baselen
+ len
+ 1);
289 memcpy(newpath
, base
, baselen
);
290 memcpy(newpath
+ baselen
, path
, len
);
291 newpath
[baselen
+ len
] = '\0';
294 path_list_insert(newpath
, ¤t_directory_set
);
296 path_list_insert(newpath
, ¤t_file_set
);
299 return READ_TREE_RECURSIVE
;
302 static int get_files_dirs(struct tree
*tree
)
305 if (read_tree_recursive(tree
, "", 0, 0, NULL
, save_files_dirs
) != 0)
307 n
= current_file_set
.nr
+ current_directory_set
.nr
;
312 * Returns a index_entry instance which doesn't have to correspond to
313 * a real cache entry in Git's index.
315 static struct stage_data
*insert_stage_data(const char *path
,
316 struct tree
*o
, struct tree
*a
, struct tree
*b
,
317 struct path_list
*entries
)
319 struct path_list_item
*item
;
320 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
321 get_tree_entry(o
->object
.sha1
, path
,
322 e
->stages
[1].sha
, &e
->stages
[1].mode
);
323 get_tree_entry(a
->object
.sha1
, path
,
324 e
->stages
[2].sha
, &e
->stages
[2].mode
);
325 get_tree_entry(b
->object
.sha1
, path
,
326 e
->stages
[3].sha
, &e
->stages
[3].mode
);
327 item
= path_list_insert(path
, entries
);
333 * Create a dictionary mapping file names to stage_data objects. The
334 * dictionary contains one entry for every path with a non-zero stage entry.
336 static struct path_list
*get_unmerged(void)
338 struct path_list
*unmerged
= xcalloc(1, sizeof(struct path_list
));
341 unmerged
->strdup_paths
= 1;
343 read_cache_from(getenv("GIT_INDEX_FILE"));
346 for (i
= 0; i
< active_nr
; i
++) {
347 struct path_list_item
*item
;
348 struct stage_data
*e
;
349 struct cache_entry
*ce
= active_cache
[i
];
353 item
= path_list_lookup(ce
->name
, unmerged
);
355 item
= path_list_insert(ce
->name
, unmerged
);
356 item
->util
= xcalloc(1, sizeof(struct stage_data
));
359 e
->stages
[ce_stage(ce
)].mode
= ntohl(ce
->ce_mode
);
360 memcpy(e
->stages
[ce_stage(ce
)].sha
, ce
->sha1
, 20);
368 struct diff_filepair
*pair
;
369 struct stage_data
*src_entry
;
370 struct stage_data
*dst_entry
;
371 unsigned processed
:1;
375 * Get information of all renames which occured between 'o_tree' and
376 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
377 * 'b_tree') to be able to associate the correct cache entries with
378 * the rename information. 'tree' is always equal to either a_tree or b_tree.
380 static struct path_list
*get_renames(struct tree
*tree
,
384 struct path_list
*entries
)
387 struct path_list
*renames
;
388 struct diff_options opts
;
390 renames
= xcalloc(1, sizeof(struct path_list
));
393 opts
.detect_rename
= DIFF_DETECT_RENAME
;
394 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
395 if (diff_setup_done(&opts
) < 0)
396 die("diff setup failed");
397 diff_tree_sha1(o_tree
->object
.sha1
, tree
->object
.sha1
, "", &opts
);
399 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
400 struct path_list_item
*item
;
402 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
403 if (pair
->status
!= 'R') {
404 diff_free_filepair(pair
);
407 re
= xmalloc(sizeof(*re
));
410 item
= path_list_lookup(re
->pair
->one
->path
, entries
);
412 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
413 o_tree
, a_tree
, b_tree
, entries
);
415 re
->src_entry
= item
->util
;
417 item
= path_list_lookup(re
->pair
->two
->path
, entries
);
419 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
420 o_tree
, a_tree
, b_tree
, entries
);
422 re
->dst_entry
= item
->util
;
423 item
= path_list_insert(pair
->one
->path
, renames
);
426 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
427 diff_queued_diff
.nr
= 0;
432 int update_stages(const char *path
, struct diff_filespec
*o
,
433 struct diff_filespec
*a
, struct diff_filespec
*b
, int clear
)
435 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
437 if (remove_file_from_cache(path
))
440 if (add_cacheinfo(o
->mode
, o
->sha1
, path
, 1, 0, options
))
443 if (add_cacheinfo(a
->mode
, a
->sha1
, path
, 2, 0, options
))
446 if (add_cacheinfo(b
->mode
, b
->sha1
, path
, 3, 0, options
))
451 static int remove_path(const char *name
)
460 dirs
= malloc(len
+1);
461 memcpy(dirs
, name
, len
);
463 while ((slash
= strrchr(name
, '/'))) {
466 if (rmdir(name
) != 0)
474 * TODO: once we no longer call external programs, we'd probably be better off
475 * not setting / getting the environment variable GIT_INDEX_FILE all the time.
477 int remove_file(int clean
, const char *path
)
479 int update_cache
= index_only
|| clean
;
480 int update_working_directory
= !index_only
;
484 read_cache_from(getenv("GIT_INDEX_FILE"));
486 if (remove_file_from_cache(path
))
489 if (update_working_directory
)
492 if (errno
!= ENOENT
|| errno
!= EISDIR
)
499 static char *unique_path(const char *path
, const char *branch
)
501 char *newpath
= xmalloc(strlen(path
) + 1 + strlen(branch
) + 8 + 1);
504 char *p
= newpath
+ strlen(path
);
505 strcpy(newpath
, path
);
511 while (path_list_has_path(¤t_file_set
, newpath
) ||
512 path_list_has_path(¤t_directory_set
, newpath
) ||
513 lstat(newpath
, &st
) == 0)
514 sprintf(p
, "_%d", suffix
++);
516 path_list_insert(newpath
, ¤t_file_set
);
520 static int mkdir_p(const char *path
, unsigned long mode
)
522 /* path points to cache entries, so strdup before messing with it */
523 char *buf
= strdup(path
);
524 int result
= safe_create_leading_directories(buf
);
529 static void flush_buffer(int fd
, const char *buf
, unsigned long size
)
532 long ret
= xwrite(fd
, buf
, size
);
537 die("merge-recursive: %s", strerror(errno
));
539 die("merge-recursive: disk full?");
546 void update_file_flags(const unsigned char *sha
,
560 buf
= read_sha1_file(sha
, type
, &size
);
562 die("cannot read object %s '%s'", sha1_to_hex(sha
), path
);
563 if (strcmp(type
, blob_type
) != 0)
564 die("blob expected for %s '%s'", sha1_to_hex(sha
), path
);
568 if (mkdir_p(path
, 0777))
569 die("failed to create path %s: %s", path
, strerror(errno
));
575 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
577 die("failed to open %s: %s", path
, strerror(errno
));
578 flush_buffer(fd
, buf
, size
);
580 } else if (S_ISLNK(mode
)) {
581 char *lnk
= malloc(size
+ 1);
582 memcpy(lnk
, buf
, size
);
588 die("do not know what to do with %06o %s '%s'",
589 mode
, sha1_to_hex(sha
), path
);
592 add_cacheinfo(mode
, sha
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
595 void update_file(int clean
,
596 const unsigned char *sha
,
600 update_file_flags(sha
, mode
, path
, index_only
|| clean
, !index_only
);
603 /* Low level file merging, update and removal */
605 struct merge_file_info
607 unsigned char sha
[20];
613 static char *git_unpack_file(const unsigned char *sha1
, char *path
)
620 buf
= read_sha1_file(sha1
, type
, &size
);
621 if (!buf
|| strcmp(type
, blob_type
))
622 die("unable to read blob object %s", sha1_to_hex(sha1
));
624 strcpy(path
, ".merge_file_XXXXXX");
627 die("unable to create temp-file");
628 flush_buffer(fd
, buf
, size
);
633 static struct merge_file_info
merge_file(struct diff_filespec
*o
,
634 struct diff_filespec
*a
, struct diff_filespec
*b
,
635 const char *branch1
, const char *branch2
)
637 struct merge_file_info result
;
641 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
643 if (S_ISREG(a
->mode
)) {
644 result
.mode
= a
->mode
;
645 memcpy(result
.sha
, a
->sha1
, 20);
647 result
.mode
= b
->mode
;
648 memcpy(result
.sha
, b
->sha1
, 20);
651 if (!sha_eq(a
->sha1
, o
->sha1
) && !sha_eq(b
->sha1
, o
->sha1
))
654 result
.mode
= a
->mode
== o
->mode
? b
->mode
: a
->mode
;
656 if (sha_eq(a
->sha1
, o
->sha1
))
657 memcpy(result
.sha
, b
->sha1
, 20);
658 else if (sha_eq(b
->sha1
, o
->sha1
))
659 memcpy(result
.sha
, a
->sha1
, 20);
660 else if (S_ISREG(a
->mode
)) {
666 const char *argv
[] = {
667 "merge", "-L", NULL
, "-L", NULL
, "-L", NULL
,
673 git_unpack_file(o
->sha1
, orig
);
674 git_unpack_file(a
->sha1
, src1
);
675 git_unpack_file(b
->sha1
, src2
);
677 argv
[2] = la
= strdup(mkpath("%s/%s", branch1
, a
->path
));
678 argv
[6] = lb
= strdup(mkpath("%s/%s", branch2
, b
->path
));
679 argv
[4] = lo
= strdup(mkpath("orig/%s", o
->path
));
684 code
= run_command_v(10, argv
);
689 if (code
&& code
< -256) {
690 die("Failed to execute 'merge'. merge(1) is used as the "
691 "file-level merge tool. Is 'merge' in your path?");
693 fd
= open(src1
, O_RDONLY
);
694 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
695 index_fd(result
.sha
, fd
, &st
, 1,
697 die("Unable to add %s to database", src1
);
703 result
.clean
= WEXITSTATUS(code
) == 0;
705 if (!(S_ISLNK(a
->mode
) || S_ISLNK(b
->mode
)))
706 die("cannot merge modes?");
708 memcpy(result
.sha
, a
->sha1
, 20);
710 if (!sha_eq(a
->sha1
, b
->sha1
))
718 static void conflict_rename_rename(struct rename
*ren1
,
725 const char *ren1_dst
= ren1
->pair
->two
->path
;
726 const char *ren2_dst
= ren2
->pair
->two
->path
;
727 const char *dst_name1
= ren1_dst
;
728 const char *dst_name2
= ren2_dst
;
729 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
730 dst_name1
= del
[delp
++] = unique_path(ren1_dst
, branch1
);
731 output("%s is a directory in %s adding as %s instead",
732 ren1_dst
, branch2
, dst_name1
);
733 remove_file(0, ren1_dst
);
735 if (path_list_has_path(¤t_directory_set
, ren2_dst
)) {
736 dst_name2
= del
[delp
++] = unique_path(ren2_dst
, branch2
);
737 output("%s is a directory in %s adding as %s instead",
738 ren2_dst
, branch1
, dst_name2
);
739 remove_file(0, ren2_dst
);
741 update_stages(dst_name1
, NULL
, ren1
->pair
->two
, NULL
, 1);
742 update_stages(dst_name2
, NULL
, NULL
, ren2
->pair
->two
, 1);
747 static void conflict_rename_dir(struct rename
*ren1
,
750 char *new_path
= unique_path(ren1
->pair
->two
->path
, branch1
);
751 output("Renaming %s to %s instead", ren1
->pair
->one
->path
, new_path
);
752 remove_file(0, ren1
->pair
->two
->path
);
753 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path
);
757 static void conflict_rename_rename_2(struct rename
*ren1
,
762 char *new_path1
= unique_path(ren1
->pair
->two
->path
, branch1
);
763 char *new_path2
= unique_path(ren2
->pair
->two
->path
, branch2
);
764 output("Renaming %s to %s and %s to %s instead",
765 ren1
->pair
->one
->path
, new_path1
,
766 ren2
->pair
->one
->path
, new_path2
);
767 remove_file(0, ren1
->pair
->two
->path
);
768 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path1
);
769 update_file(0, ren2
->pair
->two
->sha1
, ren2
->pair
->two
->mode
, new_path2
);
774 static int process_renames(struct path_list
*a_renames
,
775 struct path_list
*b_renames
,
776 const char *a_branch
,
777 const char *b_branch
)
779 int clean_merge
= 1, i
, j
;
780 struct path_list a_by_dst
= {NULL
, 0, 0, 0}, b_by_dst
= {NULL
, 0, 0, 0};
781 const struct rename
*sre
;
783 for (i
= 0; i
< a_renames
->nr
; i
++) {
784 sre
= a_renames
->items
[i
].util
;
785 path_list_insert(sre
->pair
->two
->path
, &a_by_dst
)->util
788 for (i
= 0; i
< b_renames
->nr
; i
++) {
789 sre
= b_renames
->items
[i
].util
;
790 path_list_insert(sre
->pair
->two
->path
, &b_by_dst
)->util
794 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
797 struct path_list
*renames1
, *renames2
, *renames2Dst
;
798 struct rename
*ren1
= NULL
, *ren2
= NULL
;
799 const char *branch1
, *branch2
;
800 const char *ren1_src
, *ren1_dst
;
802 if (i
>= a_renames
->nr
) {
804 ren2
= b_renames
->items
[j
++].util
;
805 } else if (j
>= b_renames
->nr
) {
807 ren1
= a_renames
->items
[i
++].util
;
809 compare
= strcmp(a_renames
->items
[i
].path
,
810 b_renames
->items
[j
].path
);
811 ren1
= a_renames
->items
[i
++].util
;
812 ren2
= b_renames
->items
[j
++].util
;
815 /* TODO: refactor, so that 1/2 are not needed */
817 renames1
= a_renames
;
818 renames2
= b_renames
;
819 renames2Dst
= &b_by_dst
;
824 renames1
= b_renames
;
825 renames2
= a_renames
;
826 renames2Dst
= &a_by_dst
;
833 src
= ren1
->pair
->one
->path
;
835 ren1
->dst_entry
->processed
= 1;
836 ren1
->src_entry
->processed
= 1;
842 ren1_src
= ren1
->pair
->one
->path
;
843 ren1_dst
= ren1
->pair
->two
->path
;
846 const char *ren2_src
= ren2
->pair
->one
->path
;
847 const char *ren2_dst
= ren2
->pair
->two
->path
;
848 /* Renamed in 1 and renamed in 2 */
849 if (strcmp(ren1_src
, ren2_src
) != 0)
850 die("ren1.src != ren2.src");
851 ren2
->dst_entry
->processed
= 1;
853 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
855 output("CONFLICT (rename/rename): "
856 "Rename %s->%s in branch %s "
857 "rename %s->%s in %s",
858 src
, ren1_dst
, branch1
,
859 src
, ren2_dst
, branch2
);
860 conflict_rename_rename(ren1
, branch1
, ren2
, branch2
);
862 struct merge_file_info mfi
;
863 remove_file(1, ren1_src
);
864 mfi
= merge_file(ren1
->pair
->one
,
869 if (mfi
.merge
|| !mfi
.clean
)
870 output("Renaming %s->%s", src
, ren1_dst
);
873 output("Auto-merging %s", ren1_dst
);
876 output("CONFLICT (content): merge conflict in %s",
881 update_stages(ren1_dst
,
887 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
890 /* Renamed in 1, maybe changed in 2 */
891 struct path_list_item
*item
;
892 /* we only use sha1 and mode of these */
893 struct diff_filespec src_other
, dst_other
;
894 int try_merge
, stage
= a_renames
== renames1
? 3: 2;
896 remove_file(1, ren1_src
);
898 memcpy(src_other
.sha1
,
899 ren1
->src_entry
->stages
[stage
].sha
, 20);
900 src_other
.mode
= ren1
->src_entry
->stages
[stage
].mode
;
901 memcpy(dst_other
.sha1
,
902 ren1
->dst_entry
->stages
[stage
].sha
, 20);
903 dst_other
.mode
= ren1
->dst_entry
->stages
[stage
].mode
;
907 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
909 output("CONFLICT (rename/directory): Rename %s->%s in %s "
910 " directory %s added in %s",
911 ren1_src
, ren1_dst
, branch1
,
913 conflict_rename_dir(ren1
, branch1
);
914 } else if (sha_eq(src_other
.sha1
, null_sha1
)) {
916 output("CONFLICT (rename/delete): Rename %s->%s in %s "
918 ren1_src
, ren1_dst
, branch1
,
920 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, ren1_dst
);
921 } else if (!sha_eq(dst_other
.sha1
, null_sha1
)) {
922 const char *new_path
;
925 output("CONFLICT (rename/add): Rename %s->%s in %s. "
927 ren1_src
, ren1_dst
, branch1
,
929 new_path
= unique_path(ren1_dst
, branch2
);
930 output("Adding as %s instead", new_path
);
931 update_file(0, dst_other
.sha1
, dst_other
.mode
, new_path
);
932 } else if ((item
= path_list_lookup(ren1_dst
, renames2Dst
))) {
936 output("CONFLICT (rename/rename): Rename %s->%s in %s. "
937 "Rename %s->%s in %s",
938 ren1_src
, ren1_dst
, branch1
,
939 ren2
->pair
->one
->path
, ren2
->pair
->two
->path
, branch2
);
940 conflict_rename_rename_2(ren1
, branch1
, ren2
, branch2
);
945 struct diff_filespec
*o
, *a
, *b
;
946 struct merge_file_info mfi
;
947 src_other
.path
= (char *)ren1_src
;
950 if (a_renames
== renames1
) {
957 mfi
= merge_file(o
, a
, b
,
960 if (mfi
.merge
|| !mfi
.clean
)
961 output("Renaming %s => %s", ren1_src
, ren1_dst
);
963 output("Auto-merging %s", ren1_dst
);
965 output("CONFLICT (rename/modify): Merge conflict in %s",
970 update_stages(ren1_dst
,
973 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
977 path_list_clear(&a_by_dst
, 0);
978 path_list_clear(&b_by_dst
, 0);
985 static unsigned char *has_sha(const unsigned char *sha
)
987 return memcmp(sha
, null_sha1
, 20) == 0 ? NULL
: (unsigned char *)sha
;
990 /* Per entry merge function */
991 static int process_entry(const char *path
, struct stage_data
*entry
,
996 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
997 print_index_entry("\tpath: ", entry);
1000 unsigned char *o_sha
= has_sha(entry
->stages
[1].sha
);
1001 unsigned char *a_sha
= has_sha(entry
->stages
[2].sha
);
1002 unsigned char *b_sha
= has_sha(entry
->stages
[3].sha
);
1003 unsigned o_mode
= entry
->stages
[1].mode
;
1004 unsigned a_mode
= entry
->stages
[2].mode
;
1005 unsigned b_mode
= entry
->stages
[3].mode
;
1007 if (o_sha
&& (!a_sha
|| !b_sha
)) {
1008 /* Case A: Deleted in one */
1009 if ((!a_sha
&& !b_sha
) ||
1010 (sha_eq(a_sha
, o_sha
) && !b_sha
) ||
1011 (!a_sha
&& sha_eq(b_sha
, o_sha
))) {
1012 /* Deleted in both or deleted in one and
1013 * unchanged in the other */
1015 output("Removing %s", path
);
1016 remove_file(1, path
);
1018 /* Deleted in one and changed in the other */
1021 output("CONFLICT (delete/modify): %s deleted in %s "
1022 "and modified in %s. Version %s of %s left in tree.",
1024 branch2
, branch2
, path
);
1025 update_file(0, b_sha
, b_mode
, path
);
1027 output("CONFLICT (delete/modify): %s deleted in %s "
1028 "and modified in %s. Version %s of %s left in tree.",
1030 branch1
, branch1
, path
);
1031 update_file(0, a_sha
, a_mode
, path
);
1035 } else if ((!o_sha
&& a_sha
&& !b_sha
) ||
1036 (!o_sha
&& !a_sha
&& b_sha
)) {
1037 /* Case B: Added in one. */
1038 const char *add_branch
;
1039 const char *other_branch
;
1041 const unsigned char *sha
;
1045 add_branch
= branch1
;
1046 other_branch
= branch2
;
1049 conf
= "file/directory";
1051 add_branch
= branch2
;
1052 other_branch
= branch1
;
1055 conf
= "directory/file";
1057 if (path_list_has_path(¤t_directory_set
, path
)) {
1058 const char *new_path
= unique_path(path
, add_branch
);
1060 output("CONFLICT (%s): There is a directory with name %s in %s. "
1062 conf
, path
, other_branch
, path
, new_path
);
1063 remove_file(0, path
);
1064 update_file(0, sha
, mode
, new_path
);
1066 output("Adding %s", path
);
1067 update_file(1, sha
, mode
, path
);
1069 } else if (!o_sha
&& a_sha
&& b_sha
) {
1070 /* Case C: Added in both (check for same permissions). */
1071 if (sha_eq(a_sha
, b_sha
)) {
1072 if (a_mode
!= b_mode
) {
1074 output("CONFLICT: File %s added identically in both branches, "
1075 "but permissions conflict %06o->%06o",
1076 path
, a_mode
, b_mode
);
1077 output("CONFLICT: adding with permission: %06o", a_mode
);
1078 update_file(0, a_sha
, a_mode
, path
);
1080 /* This case is handled by git-read-tree */
1081 assert(0 && "This case must be handled by git-read-tree");
1084 const char *new_path1
, *new_path2
;
1086 new_path1
= unique_path(path
, branch1
);
1087 new_path2
= unique_path(path
, branch2
);
1088 output("CONFLICT (add/add): File %s added non-identically "
1089 "in both branches. Adding as %s and %s instead.",
1090 path
, new_path1
, new_path2
);
1091 remove_file(0, path
);
1092 update_file(0, a_sha
, a_mode
, new_path1
);
1093 update_file(0, b_sha
, b_mode
, new_path2
);
1096 } else if (o_sha
&& a_sha
&& b_sha
) {
1097 /* case D: Modified in both, but differently. */
1098 struct merge_file_info mfi
;
1099 struct diff_filespec o
, a
, b
;
1101 output("Auto-merging %s", path
);
1102 o
.path
= a
.path
= b
.path
= (char *)path
;
1103 memcpy(o
.sha1
, o_sha
, 20);
1105 memcpy(a
.sha1
, a_sha
, 20);
1107 memcpy(b
.sha1
, b_sha
, 20);
1110 mfi
= merge_file(&o
, &a
, &b
,
1114 update_file(1, mfi
.sha
, mfi
.mode
, path
);
1117 output("CONFLICT (content): Merge conflict in %s", path
);
1120 update_file(0, mfi
.sha
, mfi
.mode
, path
);
1122 update_file_flags(mfi
.sha
, mfi
.mode
, path
,
1123 0 /* update_cache */, 1 /* update_working_directory */);
1126 die("Fatal merge failure, shouldn't happen.");
1134 static int merge_trees(struct tree
*head
,
1136 struct tree
*common
,
1137 const char *branch1
,
1138 const char *branch2
,
1139 struct tree
**result
)
1142 if (sha_eq(common
->object
.sha1
, merge
->object
.sha1
)) {
1143 output("Already uptodate!");
1148 code
= git_merge_trees(index_only
, common
, head
, merge
);
1151 die("merging of trees %s and %s failed",
1152 sha1_to_hex(head
->object
.sha1
),
1153 sha1_to_hex(merge
->object
.sha1
));
1155 *result
= git_write_tree();
1158 struct path_list
*entries
, *re_head
, *re_merge
;
1160 path_list_clear(¤t_file_set
, 1);
1161 path_list_clear(¤t_directory_set
, 1);
1162 get_files_dirs(head
);
1163 get_files_dirs(merge
);
1165 entries
= get_unmerged();
1166 re_head
= get_renames(head
, common
, head
, merge
, entries
);
1167 re_merge
= get_renames(merge
, common
, head
, merge
, entries
);
1168 clean
= process_renames(re_head
, re_merge
,
1170 for (i
= 0; i
< entries
->nr
; i
++) {
1171 const char *path
= entries
->items
[i
].path
;
1172 struct stage_data
*e
= entries
->items
[i
].util
;
1175 if (!process_entry(path
, e
, branch1
, branch2
))
1179 path_list_clear(re_merge
, 0);
1180 path_list_clear(re_head
, 0);
1181 path_list_clear(entries
, 1);
1183 if (clean
|| index_only
)
1184 *result
= git_write_tree();
1189 printf("merging of trees %s and %s resulted in %s\n",
1190 sha1_to_hex(head
->object
.sha1
),
1191 sha1_to_hex(merge
->object
.sha1
),
1192 sha1_to_hex((*result
)->object
.sha1
));
1199 * Merge the commits h1 and h2, return the resulting virtual
1200 * commit object and a flag indicating the cleaness of the merge.
1203 int merge(struct commit
*h1
,
1205 const char *branch1
,
1206 const char *branch2
,
1207 int call_depth
/* =0 */,
1208 struct commit
*ancestor
/* =None */,
1209 struct commit
**result
)
1211 struct commit_list
*ca
= NULL
, *iter
;
1212 struct commit
*merged_common_ancestors
;
1213 struct tree
*mrtree
;
1217 output_commit_title(h1
);
1218 output_commit_title(h2
);
1221 commit_list_insert(ancestor
, &ca
);
1223 ca
= get_merge_bases(h1
, h2
, 1);
1225 output("found %u common ancestor(s):", commit_list_count(ca
));
1226 for (iter
= ca
; iter
; iter
= iter
->next
)
1227 output_commit_title(iter
->item
);
1229 merged_common_ancestors
= pop_commit(&ca
);
1231 for (iter
= ca
; iter
; iter
= iter
->next
) {
1232 output_indent
= call_depth
+ 1;
1234 * When the merge fails, the result contains files
1235 * with conflict markers. The cleanness flag is
1236 * ignored, it was never acutally used, as result of
1237 * merge_trees has always overwritten it: the commited
1238 * "conflicts" were already resolved.
1240 merge(merged_common_ancestors
, iter
->item
,
1241 "Temporary merge branch 1",
1242 "Temporary merge branch 2",
1245 &merged_common_ancestors
);
1246 output_indent
= call_depth
;
1248 if (!merged_common_ancestors
)
1249 die("merge returned no commit");
1252 if (call_depth
== 0) {
1253 setup_index(0 /* $GIT_DIR/index */);
1256 setup_index(1 /* temporary index */);
1257 git_read_tree(h1
->tree
);
1261 clean
= merge_trees(h1
->tree
, h2
->tree
, merged_common_ancestors
->tree
,
1262 branch1
, branch2
, &mrtree
);
1264 if (!ancestor
&& (clean
|| index_only
)) {
1265 *result
= make_virtual_commit(mrtree
, "merged tree");
1266 commit_list_insert(h1
, &(*result
)->parents
);
1267 commit_list_insert(h2
, &(*result
)->parents
->next
);
1274 static struct commit
*get_ref(const char *ref
)
1276 unsigned char sha1
[20];
1277 struct object
*object
;
1279 if (get_sha1(ref
, sha1
))
1280 die("Could not resolve ref '%s'", ref
);
1281 object
= deref_tag(parse_object(sha1
), ref
, strlen(ref
));
1282 if (object
->type
!= OBJ_COMMIT
)
1284 if (parse_commit((struct commit
*)object
))
1285 die("Could not parse commit '%s'", sha1_to_hex(object
->sha1
));
1286 return (struct commit
*)object
;
1289 int main(int argc
, char *argv
[])
1291 static const char *bases
[2];
1292 static unsigned bases_count
= 0;
1294 const char *branch1
, *branch2
;
1295 struct commit
*result
, *h1
, *h2
;
1297 original_index_file
= getenv("GIT_INDEX_FILE");
1299 if (!original_index_file
)
1300 original_index_file
= strdup(git_path("index"));
1302 temporary_index_file
= strdup(git_path("mrg-rcrsv-tmp-idx"));
1305 die("Usage: %s <base>... -- <head> <remote> ...\n", argv
[0]);
1307 for (i
= 1; i
< argc
; ++i
) {
1308 if (!strcmp(argv
[i
], "--"))
1310 if (bases_count
< sizeof(bases
)/sizeof(*bases
))
1311 bases
[bases_count
++] = argv
[i
];
1313 if (argc
- i
!= 3) /* "--" "<head>" "<remote>" */
1314 die("Not handling anything other than two heads merge.");
1316 branch1
= argv
[++i
];
1317 branch2
= argv
[++i
];
1318 printf("Merging %s with %s\n", branch1
, branch2
);
1320 h1
= get_ref(branch1
);
1321 h2
= get_ref(branch2
);
1323 if (bases_count
== 1) {
1324 struct commit
*ancestor
= get_ref(bases
[0]);
1325 clean
= merge(h1
, h2
, branch1
, branch2
, 0, ancestor
, &result
);
1327 clean
= merge(h1
, h2
, branch1
, branch2
, 0, NULL
, &result
);
1332 return clean
? 0: 1;