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"
11 #include "tree-walk.h"
15 #include "unpack-trees.h"
16 #include "string-list.h"
17 #include "xdiff-interface.h"
19 #include "interpolate.h"
21 #include "merge-recursive.h"
23 static struct tree
*shift_tree_object(struct tree
*one
, struct tree
*two
)
25 unsigned char shifted
[20];
28 * NEEDSWORK: this limits the recursion depth to hardcoded
29 * value '2' to avoid excessive overhead.
31 shift_tree(one
->object
.sha1
, two
->object
.sha1
, shifted
, 2);
32 if (!hashcmp(two
->object
.sha1
, shifted
))
34 return lookup_tree(shifted
);
38 * A virtual commit has
39 * - (const char *)commit->util set to the name, and
40 * - *(int *)commit->object.sha1 set to the virtual id.
43 struct commit
*make_virtual_commit(struct tree
*tree
, const char *comment
)
45 struct commit
*commit
= xcalloc(1, sizeof(struct commit
));
46 static unsigned virtual_id
= 1;
48 commit
->util
= (void*)comment
;
49 *(int*)commit
->object
.sha1
= virtual_id
++;
51 commit
->object
.parsed
= 1;
56 * Since we use get_tree_entry(), which does not put the read object into
57 * the object pool, we cannot rely on a == b.
59 static int sha_eq(const unsigned char *a
, const unsigned char *b
)
63 return a
&& b
&& hashcmp(a
, b
) == 0;
67 * Since we want to write the index eventually, we cannot reuse the index
68 * for these (temporary) data.
75 unsigned char sha
[20];
80 static struct string_list current_file_set
= {NULL
, 0, 0, 1};
81 static struct string_list current_directory_set
= {NULL
, 0, 0, 1};
83 static struct strbuf obuf
= STRBUF_INIT
;
85 static int show(struct merge_options
*o
, int v
)
87 return (!o
->call_depth
&& o
->verbosity
>= v
) || o
->verbosity
>= 5;
90 static void flush_output(void)
93 fputs(obuf
.buf
, stdout
);
98 static void output(struct merge_options
*o
, int v
, const char *fmt
, ...)
106 strbuf_grow(&obuf
, o
->call_depth
* 2 + 2);
107 memset(obuf
.buf
+ obuf
.len
, ' ', o
->call_depth
* 2);
108 strbuf_setlen(&obuf
, obuf
.len
+ o
->call_depth
* 2);
111 len
= vsnprintf(obuf
.buf
+ obuf
.len
, strbuf_avail(&obuf
), fmt
, ap
);
116 if (len
>= strbuf_avail(&obuf
)) {
117 strbuf_grow(&obuf
, len
+ 2);
119 len
= vsnprintf(obuf
.buf
+ obuf
.len
, strbuf_avail(&obuf
), fmt
, ap
);
121 if (len
>= strbuf_avail(&obuf
)) {
122 die("this should not happen, your snprintf is broken");
125 strbuf_setlen(&obuf
, obuf
.len
+ len
);
126 strbuf_add(&obuf
, "\n", 1);
127 if (!o
->buffer_output
)
131 static void output_commit_title(struct merge_options
*o
, struct commit
*commit
)
135 for (i
= o
->call_depth
; i
--;)
138 printf("virtual %s\n", (char *)commit
->util
);
140 printf("%s ", find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
));
141 if (parse_commit(commit
) != 0)
142 printf("(bad commit)\n");
146 for (s
= commit
->buffer
; *s
; s
++)
147 if (*s
== '\n' && s
[1] == '\n') {
151 for (len
= 0; s
[len
] && '\n' != s
[len
]; len
++)
153 printf("%.*s\n", len
, s
);
158 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
159 const char *path
, int stage
, int refresh
, int options
)
161 struct cache_entry
*ce
;
162 ce
= make_cache_entry(mode
, sha1
? sha1
: null_sha1
, path
, stage
, refresh
);
164 return error("addinfo_cache failed for path '%s'", path
);
165 return add_cache_entry(ce
, options
);
169 * This is a global variable which is used in a number of places but
170 * only written to in the 'merge' function.
172 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
173 * don't update the working directory.
174 * 0 => Leave unmerged entries in the cache and update
175 * the working directory.
177 static int index_only
= 0;
179 static void init_tree_desc_from_tree(struct tree_desc
*desc
, struct tree
*tree
)
182 init_tree_desc(desc
, tree
->buffer
, tree
->size
);
185 static int git_merge_trees(int index_only
,
191 struct tree_desc t
[3];
192 struct unpack_trees_options opts
;
194 memset(&opts
, 0, sizeof(opts
));
201 opts
.fn
= threeway_merge
;
202 opts
.src_index
= &the_index
;
203 opts
.dst_index
= &the_index
;
205 init_tree_desc_from_tree(t
+0, common
);
206 init_tree_desc_from_tree(t
+1, head
);
207 init_tree_desc_from_tree(t
+2, merge
);
209 rc
= unpack_trees(3, t
, &opts
);
210 cache_tree_free(&active_cache_tree
);
214 struct tree
*write_tree_from_memory(struct merge_options
*o
)
216 struct tree
*result
= NULL
;
218 if (unmerged_cache()) {
220 output(o
, 0, "There are unmerged index entries:");
221 for (i
= 0; i
< active_nr
; i
++) {
222 struct cache_entry
*ce
= active_cache
[i
];
224 output(o
, 0, "%d %.*s", ce_stage(ce
), ce_namelen(ce
), ce
->name
);
229 if (!active_cache_tree
)
230 active_cache_tree
= cache_tree();
232 if (!cache_tree_fully_valid(active_cache_tree
) &&
233 cache_tree_update(active_cache_tree
,
234 active_cache
, active_nr
, 0, 0) < 0)
235 die("error building trees");
237 result
= lookup_tree(active_cache_tree
->sha1
);
242 static int save_files_dirs(const unsigned char *sha1
,
243 const char *base
, int baselen
, const char *path
,
244 unsigned int mode
, int stage
, void *context
)
246 int len
= strlen(path
);
247 char *newpath
= xmalloc(baselen
+ len
+ 1);
248 memcpy(newpath
, base
, baselen
);
249 memcpy(newpath
+ baselen
, path
, len
);
250 newpath
[baselen
+ len
] = '\0';
253 string_list_insert(newpath
, ¤t_directory_set
);
255 string_list_insert(newpath
, ¤t_file_set
);
258 return READ_TREE_RECURSIVE
;
261 static int get_files_dirs(struct tree
*tree
)
264 if (read_tree_recursive(tree
, "", 0, 0, NULL
, save_files_dirs
, NULL
))
266 n
= current_file_set
.nr
+ current_directory_set
.nr
;
271 * Returns an index_entry instance which doesn't have to correspond to
272 * a real cache entry in Git's index.
274 static struct stage_data
*insert_stage_data(const char *path
,
275 struct tree
*o
, struct tree
*a
, struct tree
*b
,
276 struct string_list
*entries
)
278 struct string_list_item
*item
;
279 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
280 get_tree_entry(o
->object
.sha1
, path
,
281 e
->stages
[1].sha
, &e
->stages
[1].mode
);
282 get_tree_entry(a
->object
.sha1
, path
,
283 e
->stages
[2].sha
, &e
->stages
[2].mode
);
284 get_tree_entry(b
->object
.sha1
, path
,
285 e
->stages
[3].sha
, &e
->stages
[3].mode
);
286 item
= string_list_insert(path
, entries
);
292 * Create a dictionary mapping file names to stage_data objects. The
293 * dictionary contains one entry for every path with a non-zero stage entry.
295 static struct string_list
*get_unmerged(void)
297 struct string_list
*unmerged
= xcalloc(1, sizeof(struct string_list
));
300 unmerged
->strdup_strings
= 1;
302 for (i
= 0; i
< active_nr
; i
++) {
303 struct string_list_item
*item
;
304 struct stage_data
*e
;
305 struct cache_entry
*ce
= active_cache
[i
];
309 item
= string_list_lookup(ce
->name
, unmerged
);
311 item
= string_list_insert(ce
->name
, unmerged
);
312 item
->util
= xcalloc(1, sizeof(struct stage_data
));
315 e
->stages
[ce_stage(ce
)].mode
= ce
->ce_mode
;
316 hashcpy(e
->stages
[ce_stage(ce
)].sha
, ce
->sha1
);
324 struct diff_filepair
*pair
;
325 struct stage_data
*src_entry
;
326 struct stage_data
*dst_entry
;
327 unsigned processed
:1;
331 * Get information of all renames which occurred between 'o_tree' and
332 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
333 * 'b_tree') to be able to associate the correct cache entries with
334 * the rename information. 'tree' is always equal to either a_tree or b_tree.
336 static struct string_list
*get_renames(struct merge_options
*o
,
341 struct string_list
*entries
)
344 struct string_list
*renames
;
345 struct diff_options opts
;
347 renames
= xcalloc(1, sizeof(struct string_list
));
349 DIFF_OPT_SET(&opts
, RECURSIVE
);
350 opts
.detect_rename
= DIFF_DETECT_RENAME
;
351 opts
.rename_limit
= o
->merge_rename_limit
>= 0 ? o
->merge_rename_limit
:
352 o
->diff_rename_limit
>= 0 ? o
->diff_rename_limit
:
354 opts
.warn_on_too_large_rename
= 1;
355 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
356 if (diff_setup_done(&opts
) < 0)
357 die("diff setup failed");
358 diff_tree_sha1(o_tree
->object
.sha1
, tree
->object
.sha1
, "", &opts
);
360 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
361 struct string_list_item
*item
;
363 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
364 if (pair
->status
!= 'R') {
365 diff_free_filepair(pair
);
368 re
= xmalloc(sizeof(*re
));
371 item
= string_list_lookup(re
->pair
->one
->path
, entries
);
373 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
374 o_tree
, a_tree
, b_tree
, entries
);
376 re
->src_entry
= item
->util
;
378 item
= string_list_lookup(re
->pair
->two
->path
, entries
);
380 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
381 o_tree
, a_tree
, b_tree
, entries
);
383 re
->dst_entry
= item
->util
;
384 item
= string_list_insert(pair
->one
->path
, renames
);
387 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
388 diff_queued_diff
.nr
= 0;
393 static int update_stages(const char *path
, struct diff_filespec
*o
,
394 struct diff_filespec
*a
, struct diff_filespec
*b
,
397 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
399 if (remove_file_from_cache(path
))
402 if (add_cacheinfo(o
->mode
, o
->sha1
, path
, 1, 0, options
))
405 if (add_cacheinfo(a
->mode
, a
->sha1
, path
, 2, 0, options
))
408 if (add_cacheinfo(b
->mode
, b
->sha1
, path
, 3, 0, options
))
413 static int remove_path(const char *name
)
421 dirs
= xstrdup(name
);
422 while ((slash
= strrchr(name
, '/'))) {
424 if (rmdir(name
) != 0)
431 static int remove_file(int clean
, const char *path
, int no_wd
)
433 int update_cache
= index_only
|| clean
;
434 int update_working_directory
= !index_only
&& !no_wd
;
437 if (remove_file_from_cache(path
))
440 if (update_working_directory
) {
442 if (errno
!= ENOENT
|| errno
!= EISDIR
)
449 static char *unique_path(const char *path
, const char *branch
)
451 char *newpath
= xmalloc(strlen(path
) + 1 + strlen(branch
) + 8 + 1);
454 char *p
= newpath
+ strlen(path
);
455 strcpy(newpath
, path
);
461 while (string_list_has_string(¤t_file_set
, newpath
) ||
462 string_list_has_string(¤t_directory_set
, newpath
) ||
463 lstat(newpath
, &st
) == 0)
464 sprintf(p
, "_%d", suffix
++);
466 string_list_insert(newpath
, ¤t_file_set
);
470 static void flush_buffer(int fd
, const char *buf
, unsigned long size
)
473 long ret
= write_in_full(fd
, buf
, size
);
478 die("merge-recursive: %s", strerror(errno
));
480 die("merge-recursive: disk full?");
487 static int make_room_for_path(const char *path
)
490 const char *msg
= "failed to create path '%s'%s";
492 status
= safe_create_leading_directories_const(path
);
495 /* something else exists */
496 error(msg
, path
, ": perhaps a D/F conflict?");
502 /* Successful unlink is good.. */
505 /* .. and so is no existing file */
508 /* .. but not some other error (who really cares what?) */
509 return error(msg
, path
, ": perhaps a D/F conflict?");
512 static void update_file_flags(const unsigned char *sha
,
522 enum object_type type
;
526 if (S_ISGITLINK(mode
))
527 die("cannot read object %s '%s': It is a submodule!",
528 sha1_to_hex(sha
), path
);
530 buf
= read_sha1_file(sha
, &type
, &size
);
532 die("cannot read object %s '%s'", sha1_to_hex(sha
), path
);
533 if (type
!= OBJ_BLOB
)
534 die("blob expected for %s '%s'", sha1_to_hex(sha
), path
);
536 struct strbuf strbuf
;
537 strbuf_init(&strbuf
, 0);
538 if (convert_to_working_tree(path
, buf
, size
, &strbuf
)) {
541 buf
= strbuf_detach(&strbuf
, NULL
);
545 if (make_room_for_path(path
) < 0) {
550 if (S_ISREG(mode
) || (!has_symlinks
&& S_ISLNK(mode
))) {
556 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
558 die("failed to open %s: %s", path
, strerror(errno
));
559 flush_buffer(fd
, buf
, size
);
561 } else if (S_ISLNK(mode
)) {
562 char *lnk
= xmemdupz(buf
, size
);
563 safe_create_leading_directories_const(path
);
568 die("do not know what to do with %06o %s '%s'",
569 mode
, sha1_to_hex(sha
), path
);
574 add_cacheinfo(mode
, sha
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
577 static void update_file(int clean
,
578 const unsigned char *sha
,
582 update_file_flags(sha
, mode
, path
, index_only
|| clean
, !index_only
);
585 /* Low level file merging, update and removal */
587 struct merge_file_info
589 unsigned char sha
[20];
595 static void fill_mm(const unsigned char *sha1
, mmfile_t
*mm
)
598 enum object_type type
;
600 if (!hashcmp(sha1
, null_sha1
)) {
601 mm
->ptr
= xstrdup("");
606 mm
->ptr
= read_sha1_file(sha1
, &type
, &size
);
607 if (!mm
->ptr
|| type
!= OBJ_BLOB
)
608 die("unable to read blob object %s", sha1_to_hex(sha1
));
612 static int merge_3way(mmbuffer_t
*result_buf
,
613 struct diff_filespec
*o
,
614 struct diff_filespec
*a
,
615 struct diff_filespec
*b
,
619 mmfile_t orig
, src1
, src2
;
623 name1
= xstrdup(mkpath("%s:%s", branch1
, a
->path
));
624 name2
= xstrdup(mkpath("%s:%s", branch2
, b
->path
));
626 fill_mm(o
->sha1
, &orig
);
627 fill_mm(a
->sha1
, &src1
);
628 fill_mm(b
->sha1
, &src2
);
630 merge_status
= ll_merge(result_buf
, a
->path
, &orig
,
631 &src1
, name1
, &src2
, name2
,
642 static struct merge_file_info
merge_file(struct diff_filespec
*o
,
643 struct diff_filespec
*a
, struct diff_filespec
*b
,
644 const char *branch1
, const char *branch2
)
646 struct merge_file_info result
;
650 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
652 if (S_ISREG(a
->mode
)) {
653 result
.mode
= a
->mode
;
654 hashcpy(result
.sha
, a
->sha1
);
656 result
.mode
= b
->mode
;
657 hashcpy(result
.sha
, b
->sha1
);
660 if (!sha_eq(a
->sha1
, o
->sha1
) && !sha_eq(b
->sha1
, o
->sha1
))
666 if (a
->mode
== b
->mode
|| a
->mode
== o
->mode
)
667 result
.mode
= b
->mode
;
669 result
.mode
= a
->mode
;
670 if (b
->mode
!= o
->mode
) {
676 if (sha_eq(a
->sha1
, b
->sha1
) || sha_eq(a
->sha1
, o
->sha1
))
677 hashcpy(result
.sha
, b
->sha1
);
678 else if (sha_eq(b
->sha1
, o
->sha1
))
679 hashcpy(result
.sha
, a
->sha1
);
680 else if (S_ISREG(a
->mode
)) {
681 mmbuffer_t result_buf
;
684 merge_status
= merge_3way(&result_buf
, o
, a
, b
,
687 if ((merge_status
< 0) || !result_buf
.ptr
)
688 die("Failed to execute internal merge");
690 if (write_sha1_file(result_buf
.ptr
, result_buf
.size
,
691 blob_type
, result
.sha
))
692 die("Unable to add %s to database",
695 free(result_buf
.ptr
);
696 result
.clean
= (merge_status
== 0);
697 } else if (S_ISGITLINK(a
->mode
)) {
699 hashcpy(result
.sha
, a
->sha1
);
700 } else if (S_ISLNK(a
->mode
)) {
701 hashcpy(result
.sha
, a
->sha1
);
703 if (!sha_eq(a
->sha1
, b
->sha1
))
706 die("unsupported object type in the tree");
713 static void conflict_rename_rename(struct merge_options
*o
,
721 const char *ren1_dst
= ren1
->pair
->two
->path
;
722 const char *ren2_dst
= ren2
->pair
->two
->path
;
723 const char *dst_name1
= ren1_dst
;
724 const char *dst_name2
= ren2_dst
;
725 if (string_list_has_string(¤t_directory_set
, ren1_dst
)) {
726 dst_name1
= del
[delp
++] = unique_path(ren1_dst
, branch1
);
727 output(o
, 1, "%s is a directory in %s adding as %s instead",
728 ren1_dst
, branch2
, dst_name1
);
729 remove_file(0, ren1_dst
, 0);
731 if (string_list_has_string(¤t_directory_set
, ren2_dst
)) {
732 dst_name2
= del
[delp
++] = unique_path(ren2_dst
, branch2
);
733 output(o
, 1, "%s is a directory in %s adding as %s instead",
734 ren2_dst
, branch1
, dst_name2
);
735 remove_file(0, ren2_dst
, 0);
738 remove_file_from_cache(dst_name1
);
739 remove_file_from_cache(dst_name2
);
741 * Uncomment to leave the conflicting names in the resulting tree
743 * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
744 * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
747 update_stages(dst_name1
, NULL
, ren1
->pair
->two
, NULL
, 1);
748 update_stages(dst_name2
, NULL
, NULL
, ren2
->pair
->two
, 1);
754 static void conflict_rename_dir(struct merge_options
*o
,
758 char *new_path
= unique_path(ren1
->pair
->two
->path
, branch1
);
759 output(o
, 1, "Renaming %s to %s instead", ren1
->pair
->one
->path
, new_path
);
760 remove_file(0, ren1
->pair
->two
->path
, 0);
761 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path
);
765 static void conflict_rename_rename_2(struct merge_options
*o
,
771 char *new_path1
= unique_path(ren1
->pair
->two
->path
, branch1
);
772 char *new_path2
= unique_path(ren2
->pair
->two
->path
, branch2
);
773 output(o
, 1, "Renaming %s to %s and %s to %s instead",
774 ren1
->pair
->one
->path
, new_path1
,
775 ren2
->pair
->one
->path
, new_path2
);
776 remove_file(0, ren1
->pair
->two
->path
, 0);
777 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path1
);
778 update_file(0, ren2
->pair
->two
->sha1
, ren2
->pair
->two
->mode
, new_path2
);
783 static int process_renames(struct merge_options
*o
,
784 struct string_list
*a_renames
,
785 struct string_list
*b_renames
)
787 int clean_merge
= 1, i
, j
;
788 struct string_list a_by_dst
= {NULL
, 0, 0, 0}, b_by_dst
= {NULL
, 0, 0, 0};
789 const struct rename
*sre
;
791 for (i
= 0; i
< a_renames
->nr
; i
++) {
792 sre
= a_renames
->items
[i
].util
;
793 string_list_insert(sre
->pair
->two
->path
, &a_by_dst
)->util
796 for (i
= 0; i
< b_renames
->nr
; i
++) {
797 sre
= b_renames
->items
[i
].util
;
798 string_list_insert(sre
->pair
->two
->path
, &b_by_dst
)->util
802 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
805 struct string_list
*renames1
, *renames2
, *renames2Dst
;
806 struct rename
*ren1
= NULL
, *ren2
= NULL
;
807 const char *branch1
, *branch2
;
808 const char *ren1_src
, *ren1_dst
;
810 if (i
>= a_renames
->nr
) {
812 ren2
= b_renames
->items
[j
++].util
;
813 } else if (j
>= b_renames
->nr
) {
815 ren1
= a_renames
->items
[i
++].util
;
817 compare
= strcmp(a_renames
->items
[i
].string
,
818 b_renames
->items
[j
].string
);
820 ren1
= a_renames
->items
[i
++].util
;
822 ren2
= b_renames
->items
[j
++].util
;
825 /* TODO: refactor, so that 1/2 are not needed */
827 renames1
= a_renames
;
828 renames2
= b_renames
;
829 renames2Dst
= &b_by_dst
;
830 branch1
= o
->branch1
;
831 branch2
= o
->branch2
;
834 renames1
= b_renames
;
835 renames2
= a_renames
;
836 renames2Dst
= &a_by_dst
;
837 branch1
= o
->branch2
;
838 branch2
= o
->branch1
;
843 src
= ren1
->pair
->one
->path
;
845 ren1
->dst_entry
->processed
= 1;
846 ren1
->src_entry
->processed
= 1;
852 ren1_src
= ren1
->pair
->one
->path
;
853 ren1_dst
= ren1
->pair
->two
->path
;
856 const char *ren2_src
= ren2
->pair
->one
->path
;
857 const char *ren2_dst
= ren2
->pair
->two
->path
;
858 /* Renamed in 1 and renamed in 2 */
859 if (strcmp(ren1_src
, ren2_src
) != 0)
860 die("ren1.src != ren2.src");
861 ren2
->dst_entry
->processed
= 1;
863 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
865 output(o
, 1, "CONFLICT (rename/rename): "
866 "Rename \"%s\"->\"%s\" in branch \"%s\" "
867 "rename \"%s\"->\"%s\" in \"%s\"%s",
868 src
, ren1_dst
, branch1
,
869 src
, ren2_dst
, branch2
,
870 index_only
? " (left unresolved)": "");
872 remove_file_from_cache(src
);
873 update_file(0, ren1
->pair
->one
->sha1
,
874 ren1
->pair
->one
->mode
, src
);
876 conflict_rename_rename(o
, ren1
, branch1
, ren2
, branch2
);
878 struct merge_file_info mfi
;
879 remove_file(1, ren1_src
, 1);
880 mfi
= merge_file(ren1
->pair
->one
,
885 if (mfi
.merge
|| !mfi
.clean
)
886 output(o
, 1, "Renaming %s->%s", src
, ren1_dst
);
889 output(o
, 2, "Auto-merging %s", ren1_dst
);
892 output(o
, 1, "CONFLICT (content): merge conflict in %s",
897 update_stages(ren1_dst
,
903 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
906 /* Renamed in 1, maybe changed in 2 */
907 struct string_list_item
*item
;
908 /* we only use sha1 and mode of these */
909 struct diff_filespec src_other
, dst_other
;
910 int try_merge
, stage
= a_renames
== renames1
? 3: 2;
912 remove_file(1, ren1_src
, index_only
|| stage
== 3);
914 hashcpy(src_other
.sha1
, ren1
->src_entry
->stages
[stage
].sha
);
915 src_other
.mode
= ren1
->src_entry
->stages
[stage
].mode
;
916 hashcpy(dst_other
.sha1
, ren1
->dst_entry
->stages
[stage
].sha
);
917 dst_other
.mode
= ren1
->dst_entry
->stages
[stage
].mode
;
921 if (string_list_has_string(¤t_directory_set
, ren1_dst
)) {
923 output(o
, 1, "CONFLICT (rename/directory): Rename %s->%s in %s "
924 " directory %s added in %s",
925 ren1_src
, ren1_dst
, branch1
,
927 conflict_rename_dir(o
, ren1
, branch1
);
928 } else if (sha_eq(src_other
.sha1
, null_sha1
)) {
930 output(o
, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
932 ren1_src
, ren1_dst
, branch1
,
934 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, ren1_dst
);
935 } else if (!sha_eq(dst_other
.sha1
, null_sha1
)) {
936 const char *new_path
;
939 output(o
, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
941 ren1_src
, ren1_dst
, branch1
,
943 new_path
= unique_path(ren1_dst
, branch2
);
944 output(o
, 1, "Adding as %s instead", new_path
);
945 update_file(0, dst_other
.sha1
, dst_other
.mode
, new_path
);
946 } else if ((item
= string_list_lookup(ren1_dst
, renames2Dst
))) {
950 output(o
, 1, "CONFLICT (rename/rename): "
951 "Rename %s->%s in %s. "
952 "Rename %s->%s in %s",
953 ren1_src
, ren1_dst
, branch1
,
954 ren2
->pair
->one
->path
, ren2
->pair
->two
->path
, branch2
);
955 conflict_rename_rename_2(o
, ren1
, branch1
, ren2
, branch2
);
960 struct diff_filespec
*one
, *a
, *b
;
961 struct merge_file_info mfi
;
962 src_other
.path
= (char *)ren1_src
;
964 one
= ren1
->pair
->one
;
965 if (a_renames
== renames1
) {
972 mfi
= merge_file(one
, a
, b
,
973 o
->branch1
, o
->branch2
);
976 sha_eq(mfi
.sha
, ren1
->pair
->two
->sha1
) &&
977 mfi
.mode
== ren1
->pair
->two
->mode
)
979 * This messaged is part of
980 * t6022 test. If you change
981 * it update the test too.
983 output(o
, 3, "Skipped %s (merged same as existing)", ren1_dst
);
985 if (mfi
.merge
|| !mfi
.clean
)
986 output(o
, 1, "Renaming %s => %s", ren1_src
, ren1_dst
);
988 output(o
, 2, "Auto-merging %s", ren1_dst
);
990 output(o
, 1, "CONFLICT (rename/modify): Merge conflict in %s",
995 update_stages(ren1_dst
,
998 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
1003 string_list_clear(&a_by_dst
, 0);
1004 string_list_clear(&b_by_dst
, 0);
1009 static unsigned char *stage_sha(const unsigned char *sha
, unsigned mode
)
1011 return (is_null_sha1(sha
) || mode
== 0) ? NULL
: (unsigned char *)sha
;
1014 /* Per entry merge function */
1015 static int process_entry(struct merge_options
*o
,
1016 const char *path
, struct stage_data
*entry
)
1019 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1020 print_index_entry("\tpath: ", entry);
1022 int clean_merge
= 1;
1023 unsigned o_mode
= entry
->stages
[1].mode
;
1024 unsigned a_mode
= entry
->stages
[2].mode
;
1025 unsigned b_mode
= entry
->stages
[3].mode
;
1026 unsigned char *o_sha
= stage_sha(entry
->stages
[1].sha
, o_mode
);
1027 unsigned char *a_sha
= stage_sha(entry
->stages
[2].sha
, a_mode
);
1028 unsigned char *b_sha
= stage_sha(entry
->stages
[3].sha
, b_mode
);
1030 if (o_sha
&& (!a_sha
|| !b_sha
)) {
1031 /* Case A: Deleted in one */
1032 if ((!a_sha
&& !b_sha
) ||
1033 (sha_eq(a_sha
, o_sha
) && !b_sha
) ||
1034 (!a_sha
&& sha_eq(b_sha
, o_sha
))) {
1035 /* Deleted in both or deleted in one and
1036 * unchanged in the other */
1038 output(o
, 2, "Removing %s", path
);
1039 /* do not touch working file if it did not exist */
1040 remove_file(1, path
, !a_sha
);
1042 /* Deleted in one and changed in the other */
1045 output(o
, 1, "CONFLICT (delete/modify): %s deleted in %s "
1046 "and modified in %s. Version %s of %s left in tree.",
1048 o
->branch2
, o
->branch2
, path
);
1049 update_file(0, b_sha
, b_mode
, path
);
1051 output(o
, 1, "CONFLICT (delete/modify): %s deleted in %s "
1052 "and modified in %s. Version %s of %s left in tree.",
1054 o
->branch1
, o
->branch1
, path
);
1055 update_file(0, a_sha
, a_mode
, path
);
1059 } else if ((!o_sha
&& a_sha
&& !b_sha
) ||
1060 (!o_sha
&& !a_sha
&& b_sha
)) {
1061 /* Case B: Added in one. */
1062 const char *add_branch
;
1063 const char *other_branch
;
1065 const unsigned char *sha
;
1069 add_branch
= o
->branch1
;
1070 other_branch
= o
->branch2
;
1073 conf
= "file/directory";
1075 add_branch
= o
->branch2
;
1076 other_branch
= o
->branch1
;
1079 conf
= "directory/file";
1081 if (string_list_has_string(¤t_directory_set
, path
)) {
1082 const char *new_path
= unique_path(path
, add_branch
);
1084 output(o
, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
1086 conf
, path
, other_branch
, path
, new_path
);
1087 remove_file(0, path
, 0);
1088 update_file(0, sha
, mode
, new_path
);
1090 output(o
, 2, "Adding %s", path
);
1091 update_file(1, sha
, mode
, path
);
1093 } else if (a_sha
&& b_sha
) {
1094 /* Case C: Added in both (check for same permissions) and */
1095 /* case D: Modified in both, but differently. */
1096 const char *reason
= "content";
1097 struct merge_file_info mfi
;
1098 struct diff_filespec one
, a
, b
;
1102 o_sha
= (unsigned char *)null_sha1
;
1104 output(o
, 2, "Auto-merging %s", path
);
1105 one
.path
= a
.path
= b
.path
= (char *)path
;
1106 hashcpy(one
.sha1
, o_sha
);
1108 hashcpy(a
.sha1
, a_sha
);
1110 hashcpy(b
.sha1
, b_sha
);
1113 mfi
= merge_file(&one
, &a
, &b
,
1114 o
->branch1
, o
->branch2
);
1116 clean_merge
= mfi
.clean
;
1118 update_file(1, mfi
.sha
, mfi
.mode
, path
);
1119 else if (S_ISGITLINK(mfi
.mode
))
1120 output(o
, 1, "CONFLICT (submodule): Merge conflict in %s "
1121 "- needs %s", path
, sha1_to_hex(b
.sha1
));
1123 output(o
, 1, "CONFLICT (%s): Merge conflict in %s",
1127 update_file(0, mfi
.sha
, mfi
.mode
, path
);
1129 update_file_flags(mfi
.sha
, mfi
.mode
, path
,
1130 0 /* update_cache */, 1 /* update_working_directory */);
1132 } else if (!o_sha
&& !a_sha
&& !b_sha
) {
1134 * this entry was deleted altogether. a_mode == 0 means
1135 * we had that path and want to actively remove it.
1137 remove_file(1, path
, !a_mode
);
1139 die("Fatal merge failure, shouldn't happen.");
1144 int merge_trees(struct merge_options
*o
,
1147 struct tree
*common
,
1148 struct tree
**result
)
1152 if (o
->subtree_merge
) {
1153 merge
= shift_tree_object(head
, merge
);
1154 common
= shift_tree_object(head
, common
);
1157 if (sha_eq(common
->object
.sha1
, merge
->object
.sha1
)) {
1158 output(o
, 0, "Already uptodate!");
1163 code
= git_merge_trees(index_only
, common
, head
, merge
);
1166 die("merging of trees %s and %s failed",
1167 sha1_to_hex(head
->object
.sha1
),
1168 sha1_to_hex(merge
->object
.sha1
));
1170 if (unmerged_cache()) {
1171 struct string_list
*entries
, *re_head
, *re_merge
;
1173 string_list_clear(¤t_file_set
, 1);
1174 string_list_clear(¤t_directory_set
, 1);
1175 get_files_dirs(head
);
1176 get_files_dirs(merge
);
1178 entries
= get_unmerged();
1179 re_head
= get_renames(o
, head
, common
, head
, merge
, entries
);
1180 re_merge
= get_renames(o
, merge
, common
, head
, merge
, entries
);
1181 clean
= process_renames(o
, re_head
, re_merge
);
1182 for (i
= 0; i
< entries
->nr
; i
++) {
1183 const char *path
= entries
->items
[i
].string
;
1184 struct stage_data
*e
= entries
->items
[i
].util
;
1186 && !process_entry(o
, path
, e
))
1190 string_list_clear(re_merge
, 0);
1191 string_list_clear(re_head
, 0);
1192 string_list_clear(entries
, 1);
1199 *result
= write_tree_from_memory(o
);
1204 static struct commit_list
*reverse_commit_list(struct commit_list
*list
)
1206 struct commit_list
*next
= NULL
, *current
, *backup
;
1207 for (current
= list
; current
; current
= backup
) {
1208 backup
= current
->next
;
1209 current
->next
= next
;
1216 * Merge the commits h1 and h2, return the resulting virtual
1217 * commit object and a flag indicating the cleanness of the merge.
1219 int merge_recursive(struct merge_options
*o
,
1222 struct commit_list
*ca
,
1223 struct commit
**result
)
1225 struct commit_list
*iter
;
1226 struct commit
*merged_common_ancestors
;
1227 struct tree
*mrtree
= mrtree
;
1231 output(o
, 4, "Merging:");
1232 output_commit_title(o
, h1
);
1233 output_commit_title(o
, h2
);
1237 ca
= get_merge_bases(h1
, h2
, 1);
1238 ca
= reverse_commit_list(ca
);
1242 output(o
, 5, "found %u common ancestor(s):", commit_list_count(ca
));
1243 for (iter
= ca
; iter
; iter
= iter
->next
)
1244 output_commit_title(o
, iter
->item
);
1247 merged_common_ancestors
= pop_commit(&ca
);
1248 if (merged_common_ancestors
== NULL
) {
1249 /* if there is no common ancestor, make an empty tree */
1250 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
1252 tree
->object
.parsed
= 1;
1253 tree
->object
.type
= OBJ_TREE
;
1254 pretend_sha1_file(NULL
, 0, OBJ_TREE
, tree
->object
.sha1
);
1255 merged_common_ancestors
= make_virtual_commit(tree
, "ancestor");
1258 for (iter
= ca
; iter
; iter
= iter
->next
) {
1259 const char *saved_b1
, *saved_b2
;
1262 * When the merge fails, the result contains files
1263 * with conflict markers. The cleanness flag is
1264 * ignored, it was never actually used, as result of
1265 * merge_trees has always overwritten it: the committed
1266 * "conflicts" were already resolved.
1269 saved_b1
= o
->branch1
;
1270 saved_b2
= o
->branch2
;
1271 o
->branch1
= "Temporary merge branch 1";
1272 o
->branch2
= "Temporary merge branch 2";
1273 merge_recursive(o
, merged_common_ancestors
, iter
->item
,
1274 NULL
, &merged_common_ancestors
);
1275 o
->branch1
= saved_b1
;
1276 o
->branch2
= saved_b2
;
1279 if (!merged_common_ancestors
)
1280 die("merge returned no commit");
1284 if (!o
->call_depth
) {
1290 clean
= merge_trees(o
, h1
->tree
, h2
->tree
, merged_common_ancestors
->tree
,
1294 *result
= make_virtual_commit(mrtree
, "merged tree");
1295 commit_list_insert(h1
, &(*result
)->parents
);
1296 commit_list_insert(h2
, &(*result
)->parents
->next
);
1302 static struct commit
*get_ref(const unsigned char *sha1
, const char *name
)
1304 struct object
*object
;
1306 object
= deref_tag(parse_object(sha1
), name
, strlen(name
));
1309 if (object
->type
== OBJ_TREE
)
1310 return make_virtual_commit((struct tree
*)object
, name
);
1311 if (object
->type
!= OBJ_COMMIT
)
1313 if (parse_commit((struct commit
*)object
))
1315 return (struct commit
*)object
;
1318 int merge_recursive_generic(struct merge_options
*o
,
1319 const unsigned char *head
,
1320 const unsigned char *merge
,
1322 const unsigned char **base_list
,
1323 struct commit
**result
)
1325 int clean
, index_fd
;
1326 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
1327 struct commit
*head_commit
= get_ref(head
, o
->branch1
);
1328 struct commit
*next_commit
= get_ref(merge
, o
->branch2
);
1329 struct commit_list
*ca
= NULL
;
1333 for (i
= 0; i
< num_base_list
; ++i
) {
1334 struct commit
*base
;
1335 if (!(base
= get_ref(base_list
[i
], sha1_to_hex(base_list
[i
]))))
1336 return error("Could not parse object '%s'",
1337 sha1_to_hex(base_list
[i
]));
1338 commit_list_insert(base
, &ca
);
1342 index_fd
= hold_locked_index(lock
, 1);
1343 clean
= merge_recursive(o
, head_commit
, next_commit
, ca
,
1345 if (active_cache_changed
&&
1346 (write_cache(index_fd
, active_cache
, active_nr
) ||
1347 commit_locked_index(lock
)))
1348 return error("Unable to write index.");
1350 return clean
? 0 : 1;
1353 static int merge_recursive_config(const char *var
, const char *value
, void *cb
)
1355 struct merge_options
*o
= cb
;
1356 if (!strcasecmp(var
, "merge.verbosity")) {
1357 o
->verbosity
= git_config_int(var
, value
);
1360 if (!strcasecmp(var
, "diff.renamelimit")) {
1361 o
->diff_rename_limit
= git_config_int(var
, value
);
1364 if (!strcasecmp(var
, "merge.renamelimit")) {
1365 o
->merge_rename_limit
= git_config_int(var
, value
);
1368 return git_default_config(var
, value
, cb
);
1371 void init_merge_options(struct merge_options
*o
)
1373 memset(o
, 0, sizeof(struct merge_options
));
1375 o
->buffer_output
= 1;
1376 o
->diff_rename_limit
= -1;
1377 o
->merge_rename_limit
= -1;
1378 git_config(merge_recursive_config
, o
);
1379 if (getenv("GIT_MERGE_VERBOSITY"))
1381 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL
, 10);
1382 if (o
->verbosity
>= 5)
1383 o
->buffer_output
= 0;