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"
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 * TODO: we should not have to copy the SHA1s around, but rather reference
51 * them. That way, sha_eq() is just sha1 == sha2.
53 static int sha_eq(const unsigned char *a
, const unsigned char *b
)
57 return a
&& b
&& memcmp(a
, b
, 20) == 0;
61 * TODO: check if we can just reuse the active_cache structure: it is already
62 * sorted (by name, stage).
63 * Only problem: do not write it when flushing the cache.
70 unsigned char sha
[20];
75 static struct path_list current_file_set
= {NULL
, 0, 0, 1};
76 static struct path_list current_directory_set
= {NULL
, 0, 0, 1};
78 static int output_indent
= 0;
80 static void output(const char *fmt
, ...)
84 for (i
= output_indent
; i
--;)
87 vfprintf(stdout
, fmt
, args
);
92 static void output_commit_title(struct commit
*commit
)
95 for (i
= output_indent
; i
--;)
98 printf("virtual %s\n", (char *)commit
->util
);
100 printf("%s ", sha1_to_hex(commit
->object
.sha1
));
101 if (parse_commit(commit
) != 0)
102 printf("(bad commit)\n");
106 for (s
= commit
->buffer
; *s
; s
++)
107 if (*s
== '\n' && s
[1] == '\n') {
111 for (len
= 0; s
[len
] && '\n' != s
[len
]; len
++)
113 printf("%.*s\n", len
, s
);
118 static const char *original_index_file
;
119 static const char *temporary_index_file
;
120 static int cache_dirty
= 0;
122 static int flush_cache(void)
124 /* flush temporary index */
125 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
126 int fd
= hold_lock_file_for_update(lock
, getenv("GIT_INDEX_FILE"));
128 die("could not lock %s", temporary_index_file
);
129 if (write_cache(fd
, active_cache
, active_nr
) ||
130 close(fd
) || commit_lock_file(lock
))
131 die ("unable to write %s", getenv("GIT_INDEX_FILE"));
137 static void setup_index(int temp
)
139 const char *idx
= temp
? temporary_index_file
: original_index_file
;
141 die("fatal: cache changed flush_cache();");
142 unlink(temporary_index_file
);
143 setenv("GIT_INDEX_FILE", idx
, 1);
147 static struct cache_entry
*make_cache_entry(unsigned int mode
,
148 const unsigned char *sha1
, const char *path
, int stage
, int refresh
)
151 struct cache_entry
*ce
;
153 if (!verify_path(path
))
157 size
= cache_entry_size(len
);
158 ce
= xcalloc(1, size
);
160 memcpy(ce
->sha1
, sha1
, 20);
161 memcpy(ce
->name
, path
, len
);
162 ce
->ce_flags
= create_ce_flags(len
, stage
);
163 ce
->ce_mode
= create_ce_mode(mode
);
166 return refresh_cache_entry(ce
, 0);
171 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
172 const char *path
, int stage
, int refresh
, int options
)
174 struct cache_entry
*ce
;
176 read_cache_from(getenv("GIT_INDEX_FILE"));
178 ce
= make_cache_entry(mode
, sha1
? sha1
: null_sha1
, path
, stage
, refresh
);
180 return error("cache_addinfo failed: %s", strerror(cache_errno
));
181 return add_cache_entry(ce
, options
);
185 * This is a global variable which is used in a number of places but
186 * only written to in the 'merge' function.
188 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
189 * don't update the working directory.
190 * 0 => Leave unmerged entries in the cache and update
191 * the working directory.
193 static int index_only
= 0;
196 * TODO: this can be streamlined by refactoring builtin-read-tree.c
198 static int git_read_tree(const struct tree
*tree
)
201 const char *argv
[] = { "git-read-tree", NULL
, NULL
, };
203 die("read-tree with dirty cache");
204 argv
[1] = sha1_to_hex(tree
->object
.sha1
);
205 rc
= run_command_v(2, argv
);
206 return rc
< 0 ? -1: rc
;
210 * TODO: this can be streamlined by refactoring builtin-read-tree.c
212 static int git_merge_trees(const char *update_arg
,
218 const char *argv
[] = {
219 "git-read-tree", NULL
, "-m", NULL
, NULL
, NULL
,
224 argv
[1] = update_arg
;
225 argv
[3] = sha1_to_hex(common
->object
.sha1
);
226 argv
[4] = sha1_to_hex(head
->object
.sha1
);
227 argv
[5] = sha1_to_hex(merge
->object
.sha1
);
228 rc
= run_command_v(6, argv
);
229 return rc
< 0 ? -1: rc
;
233 * TODO: this can be streamlined by refactoring builtin-write-tree.c
235 static struct tree
*git_write_tree(void)
240 unsigned char sha1
[20];
245 fp
= popen("git-write-tree 2>/dev/null", "r");
246 while ((ch
= fgetc(fp
)) != EOF
)
247 if (i
< sizeof(buf
)-1 && ch
>= '0' && ch
<= 'f')
252 if (rc
== -1 || WEXITSTATUS(rc
))
255 if (get_sha1(buf
, sha1
) != 0)
257 return lookup_tree(sha1
);
260 static int save_files_dirs(const unsigned char *sha1
,
261 const char *base
, int baselen
, const char *path
,
262 unsigned int mode
, int stage
)
264 int len
= strlen(path
);
265 char *newpath
= malloc(baselen
+ len
+ 1);
266 memcpy(newpath
, base
, baselen
);
267 memcpy(newpath
+ baselen
, path
, len
);
268 newpath
[baselen
+ len
] = '\0';
271 path_list_insert(newpath
, ¤t_directory_set
);
273 path_list_insert(newpath
, ¤t_file_set
);
276 return READ_TREE_RECURSIVE
;
279 static int get_files_dirs(struct tree
*tree
)
282 if (read_tree_recursive(tree
, "", 0, 0, NULL
, save_files_dirs
) != 0)
284 n
= current_file_set
.nr
+ current_directory_set
.nr
;
289 * Returns a index_entry instance which doesn't have to correspond to
290 * a real cache entry in Git's index.
292 static struct stage_data
*insert_stage_data(const char *path
,
293 struct tree
*o
, struct tree
*a
, struct tree
*b
,
294 struct path_list
*entries
)
296 struct path_list_item
*item
;
297 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
298 get_tree_entry(o
->object
.sha1
, path
,
299 e
->stages
[1].sha
, &e
->stages
[1].mode
);
300 get_tree_entry(a
->object
.sha1
, path
,
301 e
->stages
[2].sha
, &e
->stages
[2].mode
);
302 get_tree_entry(b
->object
.sha1
, path
,
303 e
->stages
[3].sha
, &e
->stages
[3].mode
);
304 item
= path_list_insert(path
, entries
);
310 * Create a dictionary mapping file names to stage_data objects. The
311 * dictionary contains one entry for every path with a non-zero stage entry.
313 static struct path_list
*get_unmerged(void)
315 struct path_list
*unmerged
= xcalloc(1, sizeof(struct path_list
));
318 unmerged
->strdup_paths
= 1;
320 read_cache_from(getenv("GIT_INDEX_FILE"));
323 for (i
= 0; i
< active_nr
; i
++) {
324 struct path_list_item
*item
;
325 struct stage_data
*e
;
326 struct cache_entry
*ce
= active_cache
[i
];
330 item
= path_list_lookup(ce
->name
, unmerged
);
332 item
= path_list_insert(ce
->name
, unmerged
);
333 item
->util
= xcalloc(1, sizeof(struct stage_data
));
336 e
->stages
[ce_stage(ce
)].mode
= ntohl(ce
->ce_mode
);
337 memcpy(e
->stages
[ce_stage(ce
)].sha
, ce
->sha1
, 20);
345 struct diff_filepair
*pair
;
346 struct stage_data
*src_entry
;
347 struct stage_data
*dst_entry
;
348 unsigned processed
:1;
352 * Get information of all renames which occured between 'o_tree' and
353 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
354 * 'b_tree') to be able to associate the correct cache entries with
355 * the rename information. 'tree' is always equal to either a_tree or b_tree.
357 static struct path_list
*get_renames(struct tree
*tree
,
361 struct path_list
*entries
)
364 struct path_list
*renames
;
365 struct diff_options opts
;
367 renames
= xcalloc(1, sizeof(struct path_list
));
370 opts
.detect_rename
= DIFF_DETECT_RENAME
;
371 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
372 if (diff_setup_done(&opts
) < 0)
373 die("diff setup failed");
374 diff_tree_sha1(o_tree
->object
.sha1
, tree
->object
.sha1
, "", &opts
);
376 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
377 struct path_list_item
*item
;
379 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
380 if (pair
->status
!= 'R') {
381 diff_free_filepair(pair
);
384 re
= xmalloc(sizeof(*re
));
387 item
= path_list_lookup(re
->pair
->one
->path
, entries
);
389 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
390 o_tree
, a_tree
, b_tree
, entries
);
392 re
->src_entry
= item
->util
;
394 item
= path_list_lookup(re
->pair
->two
->path
, entries
);
396 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
397 o_tree
, a_tree
, b_tree
, entries
);
399 re
->dst_entry
= item
->util
;
400 item
= path_list_insert(pair
->one
->path
, renames
);
403 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
404 diff_queued_diff
.nr
= 0;
409 int update_stages(const char *path
, struct diff_filespec
*o
,
410 struct diff_filespec
*a
, struct diff_filespec
*b
, int clear
)
412 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
414 if (remove_file_from_cache(path
))
417 if (add_cacheinfo(o
->mode
, o
->sha1
, path
, 1, 0, options
))
420 if (add_cacheinfo(a
->mode
, a
->sha1
, path
, 2, 0, options
))
423 if (add_cacheinfo(b
->mode
, b
->sha1
, path
, 3, 0, options
))
428 static int remove_path(const char *name
)
437 dirs
= malloc(len
+1);
438 memcpy(dirs
, name
, len
);
440 while ((slash
= strrchr(name
, '/'))) {
443 if (rmdir(name
) != 0)
451 * TODO: once we no longer call external programs, we'd probably be better off
452 * not setting / getting the environment variable GIT_INDEX_FILE all the time.
454 int remove_file(int clean
, const char *path
)
456 int update_cache
= index_only
|| clean
;
457 int update_working_directory
= !index_only
;
461 read_cache_from(getenv("GIT_INDEX_FILE"));
463 if (remove_file_from_cache(path
))
466 if (update_working_directory
)
469 if (errno
!= ENOENT
|| errno
!= EISDIR
)
476 static char *unique_path(const char *path
, const char *branch
)
478 char *newpath
= xmalloc(strlen(path
) + 1 + strlen(branch
) + 8 + 1);
481 char *p
= newpath
+ strlen(newpath
);
482 strcpy(newpath
, path
);
483 strcat(newpath
, "~");
488 while (path_list_has_path(¤t_file_set
, newpath
) ||
489 path_list_has_path(¤t_directory_set
, newpath
) ||
490 lstat(newpath
, &st
) == 0)
491 sprintf(p
, "_%d", suffix
++);
493 path_list_insert(newpath
, ¤t_file_set
);
497 static int mkdir_p(const char *path
, unsigned long mode
)
499 /* path points to cache entries, so strdup before messing with it */
500 char *buf
= strdup(path
);
501 int result
= safe_create_leading_directories(buf
);
506 static void flush_buffer(int fd
, const char *buf
, unsigned long size
)
509 long ret
= xwrite(fd
, buf
, size
);
514 die("merge-recursive: %s", strerror(errno
));
516 die("merge-recursive: disk full?");
523 void update_file_flags(const unsigned char *sha
,
537 buf
= read_sha1_file(sha
, type
, &size
);
539 die("cannot read object %s '%s'", sha1_to_hex(sha
), path
);
540 if (strcmp(type
, blob_type
) != 0)
541 die("blob expected for %s '%s'", sha1_to_hex(sha
), path
);
545 if (mkdir_p(path
, 0777))
546 die("failed to create path %s: %s", path
, strerror(errno
));
552 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
554 die("failed to open %s: %s", path
, strerror(errno
));
555 flush_buffer(fd
, buf
, size
);
557 } else if (S_ISLNK(mode
)) {
558 char *lnk
= malloc(size
+ 1);
559 memcpy(lnk
, buf
, size
);
565 die("do not know what to do with %06o %s '%s'",
566 mode
, sha1_to_hex(sha
), path
);
569 add_cacheinfo(mode
, sha
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
572 void update_file(int clean
,
573 const unsigned char *sha
,
577 update_file_flags(sha
, mode
, path
, index_only
|| clean
, !index_only
);
580 /* Low level file merging, update and removal */
582 struct merge_file_info
584 unsigned char sha
[20];
590 static char *git_unpack_file(const unsigned char *sha1
, char *path
)
597 buf
= read_sha1_file(sha1
, type
, &size
);
598 if (!buf
|| strcmp(type
, blob_type
))
599 die("unable to read blob object %s", sha1_to_hex(sha1
));
601 strcpy(path
, ".merge_file_XXXXXX");
604 die("unable to create temp-file");
605 flush_buffer(fd
, buf
, size
);
610 static struct merge_file_info
merge_file(struct diff_filespec
*o
,
611 struct diff_filespec
*a
, struct diff_filespec
*b
,
612 const char *branch1
, const char *branch2
)
614 struct merge_file_info result
;
618 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
620 if (S_ISREG(a
->mode
)) {
621 result
.mode
= a
->mode
;
622 memcpy(result
.sha
, a
->sha1
, 20);
624 result
.mode
= b
->mode
;
625 memcpy(result
.sha
, b
->sha1
, 20);
628 if (!sha_eq(a
->sha1
, o
->sha1
) && !sha_eq(b
->sha1
, o
->sha1
))
631 result
.mode
= a
->mode
== o
->mode
? b
->mode
: a
->mode
;
633 if (sha_eq(a
->sha1
, o
->sha1
))
634 memcpy(result
.sha
, b
->sha1
, 20);
635 else if (sha_eq(b
->sha1
, o
->sha1
))
636 memcpy(result
.sha
, a
->sha1
, 20);
637 else if (S_ISREG(a
->mode
)) {
643 const char *argv
[] = {
644 "merge", "-L", NULL
, "-L", NULL
, "-L", NULL
,
650 git_unpack_file(o
->sha1
, orig
);
651 git_unpack_file(a
->sha1
, src1
);
652 git_unpack_file(b
->sha1
, src2
);
654 argv
[2] = la
= strdup(mkpath("%s/%s", branch1
, a
->path
));
655 argv
[6] = lb
= strdup(mkpath("%s/%s", branch2
, b
->path
));
656 argv
[4] = lo
= strdup(mkpath("orig/%s", o
->path
));
661 code
= run_command_v(10, argv
);
666 if (code
&& code
< -256) {
667 die("Failed to execute 'merge'. merge(1) is used as the "
668 "file-level merge tool. Is 'merge' in your path?");
670 fd
= open(src1
, O_RDONLY
);
671 if (fd
< 0 || fstat(fd
, &st
) < 0 ||
672 index_fd(result
.sha
, fd
, &st
, 1,
674 die("Unable to add %s to database", src1
);
680 result
.clean
= WEXITSTATUS(code
) == 0;
682 if (!(S_ISLNK(a
->mode
) || S_ISLNK(b
->mode
)))
683 die("cannot merge modes?");
685 memcpy(result
.sha
, a
->sha1
, 20);
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
);
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
);
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
);
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
);
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
);
788 ren1
= a_renames
->items
[i
++].util
;
789 ren2
= b_renames
->items
[j
++].util
;
792 /* TODO: refactor, so that 1/2 are not needed */
794 renames1
= a_renames
;
795 renames2
= b_renames
;
796 renames2Dst
= &b_by_dst
;
801 renames1
= b_renames
;
802 renames2
= a_renames
;
803 renames2Dst
= &a_by_dst
;
810 src
= ren1
->pair
->one
->path
;
812 ren1
->dst_entry
->processed
= 1;
813 ren1
->src_entry
->processed
= 1;
819 ren1_src
= ren1
->pair
->one
->path
;
820 ren1_dst
= ren1
->pair
->two
->path
;
823 const char *ren2_src
= ren2
->pair
->one
->path
;
824 const char *ren2_dst
= ren2
->pair
->two
->path
;
825 /* Renamed in 1 and renamed in 2 */
826 if (strcmp(ren1_src
, ren2_src
) != 0)
827 die("ren1.src != ren2.src");
828 ren2
->dst_entry
->processed
= 1;
830 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
832 output("CONFLICT (rename/rename): "
833 "Rename %s->%s in branch %s "
834 "rename %s->%s in %s",
835 src
, ren1_dst
, branch1
,
836 src
, ren2_dst
, branch2
);
837 conflict_rename_rename(ren1
, branch1
, ren2
, branch2
);
839 struct merge_file_info mfi
;
840 remove_file(1, ren1_src
);
841 mfi
= merge_file(ren1
->pair
->one
,
846 if (mfi
.merge
|| !mfi
.clean
)
847 output("Renaming %s->%s", src
, ren1_dst
);
850 output("Auto-merging %s", ren1_dst
);
853 output("CONFLICT (content): merge conflict in %s",
858 update_stages(ren1_dst
,
864 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
867 /* Renamed in 1, maybe changed in 2 */
868 struct path_list_item
*item
;
869 /* we only use sha1 and mode of these */
870 struct diff_filespec src_other
, dst_other
;
871 int try_merge
, stage
= a_renames
== renames1
? 3: 2;
873 remove_file(1, ren1_src
);
875 memcpy(src_other
.sha1
,
876 ren1
->src_entry
->stages
[stage
].sha
, 20);
877 src_other
.mode
= ren1
->src_entry
->stages
[stage
].mode
;
878 memcpy(dst_other
.sha1
,
879 ren1
->dst_entry
->stages
[stage
].sha
, 20);
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 memcmp(sha
, null_sha1
, 20) == 0 ? 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 remove_file(1, path
);
995 /* Deleted in one and changed in the other */
998 output("CONFLICT (delete/modify): %s deleted in %s "
999 "and modified in %s. Version %s of %s left in tree.",
1001 branch2
, branch2
, path
);
1002 update_file(0, b_sha
, b_mode
, path
);
1004 output("CONFLICT (delete/modify): %s deleted in %s "
1005 "and modified in %s. Version %s of %s left in tree.",
1007 branch1
, branch1
, path
);
1008 update_file(0, a_sha
, a_mode
, path
);
1012 } else if ((!o_sha
&& a_sha
&& !b_sha
) ||
1013 (!o_sha
&& !a_sha
&& b_sha
)) {
1014 /* Case B: Added in one. */
1015 const char *add_branch
;
1016 const char *other_branch
;
1018 const unsigned char *sha
;
1022 add_branch
= branch1
;
1023 other_branch
= branch2
;
1026 conf
= "file/directory";
1028 add_branch
= branch2
;
1029 other_branch
= branch1
;
1032 conf
= "directory/file";
1034 if (path_list_has_path(¤t_directory_set
, path
)) {
1035 const char *new_path
= unique_path(path
, add_branch
);
1037 output("CONFLICT (%s): There is a directory with name %s in %s. "
1039 conf
, path
, other_branch
, path
, new_path
);
1040 remove_file(0, path
);
1041 update_file(0, sha
, mode
, new_path
);
1043 output("Adding %s", path
);
1044 update_file(1, sha
, mode
, path
);
1046 } else if (!o_sha
&& a_sha
&& b_sha
) {
1047 /* Case C: Added in both (check for same permissions). */
1048 if (sha_eq(a_sha
, b_sha
)) {
1049 if (a_mode
!= b_mode
) {
1051 output("CONFLICT: File %s added identically in both branches, "
1052 "but permissions conflict %06o->%06o",
1053 path
, a_mode
, b_mode
);
1054 output("CONFLICT: adding with permission: %06o", a_mode
);
1055 update_file(0, a_sha
, a_mode
, path
);
1057 /* This case is handled by git-read-tree */
1058 assert(0 && "This case must be handled by git-read-tree");
1061 const char *new_path1
, *new_path2
;
1063 new_path1
= unique_path(path
, branch1
);
1064 new_path2
= unique_path(path
, branch2
);
1065 output("CONFLICT (add/add): File %s added non-identically "
1066 "in both branches. Adding as %s and %s instead.",
1067 path
, new_path1
, new_path2
);
1068 remove_file(0, path
);
1069 update_file(0, a_sha
, a_mode
, new_path1
);
1070 update_file(0, b_sha
, b_mode
, new_path2
);
1073 } else if (o_sha
&& a_sha
&& b_sha
) {
1074 /* case D: Modified in both, but differently. */
1075 struct merge_file_info mfi
;
1076 struct diff_filespec o
, a
, b
;
1078 output("Auto-merging %s", path
);
1079 o
.path
= a
.path
= b
.path
= (char *)path
;
1080 memcpy(o
.sha1
, o_sha
, 20);
1082 memcpy(a
.sha1
, a_sha
, 20);
1084 memcpy(b
.sha1
, b_sha
, 20);
1087 mfi
= merge_file(&o
, &a
, &b
,
1091 update_file(1, mfi
.sha
, mfi
.mode
, path
);
1094 output("CONFLICT (content): Merge conflict in %s", path
);
1097 update_file(0, mfi
.sha
, mfi
.mode
, path
);
1099 update_file_flags(mfi
.sha
, mfi
.mode
, path
,
1100 0 /* update_cache */, 1 /* update_working_directory */);
1103 die("Fatal merge failure, shouldn't happen.");
1111 static int merge_trees(struct tree
*head
,
1113 struct tree
*common
,
1114 const char *branch1
,
1115 const char *branch2
,
1116 struct tree
**result
)
1119 if (sha_eq(common
->object
.sha1
, merge
->object
.sha1
)) {
1120 output("Already uptodate!");
1125 code
= git_merge_trees(index_only
? "-i": "-u", common
, head
, merge
);
1128 die("merging of trees %s and %s failed",
1129 sha1_to_hex(head
->object
.sha1
),
1130 sha1_to_hex(merge
->object
.sha1
));
1132 *result
= git_write_tree();
1135 struct path_list
*entries
, *re_head
, *re_merge
;
1137 path_list_clear(¤t_file_set
, 1);
1138 path_list_clear(¤t_directory_set
, 1);
1139 get_files_dirs(head
);
1140 get_files_dirs(merge
);
1142 entries
= get_unmerged();
1143 re_head
= get_renames(head
, common
, head
, merge
, entries
);
1144 re_merge
= get_renames(merge
, common
, head
, merge
, entries
);
1145 clean
= process_renames(re_head
, re_merge
,
1147 for (i
= 0; i
< entries
->nr
; i
++) {
1148 const char *path
= entries
->items
[i
].path
;
1149 struct stage_data
*e
= entries
->items
[i
].util
;
1152 if (!process_entry(path
, e
, branch1
, branch2
))
1156 path_list_clear(re_merge
, 0);
1157 path_list_clear(re_head
, 0);
1158 path_list_clear(entries
, 1);
1160 if (clean
|| index_only
)
1161 *result
= git_write_tree();
1166 printf("merging of trees %s and %s resulted in %s\n",
1167 sha1_to_hex(head
->object
.sha1
),
1168 sha1_to_hex(merge
->object
.sha1
),
1169 sha1_to_hex((*result
)->object
.sha1
));
1176 * Merge the commits h1 and h2, return the resulting virtual
1177 * commit object and a flag indicating the cleaness of the merge.
1180 int merge(struct commit
*h1
,
1182 const char *branch1
,
1183 const char *branch2
,
1184 int call_depth
/* =0 */,
1185 struct commit
*ancestor
/* =None */,
1186 struct commit
**result
)
1188 struct commit_list
*ca
= NULL
, *iter
;
1189 struct commit
*merged_common_ancestors
;
1190 struct tree
*mrtree
;
1194 output_commit_title(h1
);
1195 output_commit_title(h2
);
1198 commit_list_insert(ancestor
, &ca
);
1200 ca
= get_merge_bases(h1
, h2
, 1);
1202 output("found %u common ancestor(s):", commit_list_count(ca
));
1203 for (iter
= ca
; iter
; iter
= iter
->next
)
1204 output_commit_title(iter
->item
);
1206 merged_common_ancestors
= pop_commit(&ca
);
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 original_index_file
= getenv("GIT_INDEX_FILE");
1276 if (!original_index_file
)
1277 original_index_file
= strdup(git_path("index"));
1279 temporary_index_file
= strdup(git_path("mrg-rcrsv-tmp-idx"));
1282 die("Usage: %s <base>... -- <head> <remote> ...\n", argv
[0]);
1284 for (i
= 1; i
< argc
; ++i
) {
1285 if (!strcmp(argv
[i
], "--"))
1287 if (bases_count
< sizeof(bases
)/sizeof(*bases
))
1288 bases
[bases_count
++] = argv
[i
];
1290 if (argc
- i
!= 3) /* "--" "<head>" "<remote>" */
1291 die("Not handling anything other than two heads merge.");
1293 branch1
= argv
[++i
];
1294 branch2
= argv
[++i
];
1295 printf("Merging %s with %s\n", branch1
, branch2
);
1297 h1
= get_ref(branch1
);
1298 h2
= get_ref(branch2
);
1300 if (bases_count
== 1) {
1301 struct commit
*ancestor
= get_ref(bases
[0]);
1302 clean
= merge(h1
, h2
, branch1
, branch2
, 0, ancestor
, &result
);
1304 clean
= merge(h1
, h2
, branch1
, branch2
, 0, NULL
, &result
);
1309 return clean
? 0: 1;