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];
72 struct output_buffer
*next
;
76 static struct path_list current_file_set
= {NULL
, 0, 0, 1};
77 static struct path_list current_directory_set
= {NULL
, 0, 0, 1};
79 static int call_depth
= 0;
80 static int verbosity
= 2;
81 static int buffer_output
= 1;
82 static int do_progress
= 1;
83 static unsigned last_percent
;
84 static unsigned merged_cnt
;
85 static unsigned total_cnt
;
86 static volatile sig_atomic_t progress_update
;
87 static struct output_buffer
*output_list
, *output_end
;
89 static int show (int v
)
91 return (!call_depth
&& verbosity
>= v
) || verbosity
>= 5;
94 static void output(int v
, const char *fmt
, ...)
98 if (buffer_output
&& show(v
)) {
99 struct output_buffer
*b
= xmalloc(sizeof(*b
));
100 nfvasprintf(&b
->str
, fmt
, args
);
103 output_end
->next
= b
;
107 } else if (show(v
)) {
109 for (i
= call_depth
; i
--;)
111 vfprintf(stdout
, fmt
, args
);
117 static void flush_output()
119 struct output_buffer
*b
, *n
;
120 for (b
= output_list
; b
; b
= n
) {
122 for (i
= call_depth
; i
--;)
124 fputs(b
->str
, stdout
);
134 static void output_commit_title(struct commit
*commit
)
138 for (i
= call_depth
; i
--;)
141 printf("virtual %s\n", (char *)commit
->util
);
143 printf("%s ", find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
));
144 if (parse_commit(commit
) != 0)
145 printf("(bad commit)\n");
149 for (s
= commit
->buffer
; *s
; s
++)
150 if (*s
== '\n' && s
[1] == '\n') {
154 for (len
= 0; s
[len
] && '\n' != s
[len
]; len
++)
156 printf("%.*s\n", len
, s
);
161 static void progress_interval(int signum
)
166 static void setup_progress_signal(void)
171 memset(&sa
, 0, sizeof(sa
));
172 sa
.sa_handler
= progress_interval
;
173 sigemptyset(&sa
.sa_mask
);
174 sa
.sa_flags
= SA_RESTART
;
175 sigaction(SIGALRM
, &sa
, NULL
);
177 v
.it_interval
.tv_sec
= 1;
178 v
.it_interval
.tv_usec
= 0;
179 v
.it_value
= v
.it_interval
;
180 setitimer(ITIMER_REAL
, &v
, NULL
);
183 static void display_progress()
185 unsigned percent
= total_cnt
? merged_cnt
* 100 / total_cnt
: 0;
186 if (progress_update
|| percent
!= last_percent
) {
187 fprintf(stderr
, "%4u%% (%u/%u) done\r",
188 percent
, merged_cnt
, total_cnt
);
190 last_percent
= percent
;
194 static struct cache_entry
*make_cache_entry(unsigned int mode
,
195 const unsigned char *sha1
, const char *path
, int stage
, int refresh
)
198 struct cache_entry
*ce
;
200 if (!verify_path(path
))
204 size
= cache_entry_size(len
);
205 ce
= xcalloc(1, size
);
207 hashcpy(ce
->sha1
, sha1
);
208 memcpy(ce
->name
, path
, len
);
209 ce
->ce_flags
= create_ce_flags(len
, stage
);
210 ce
->ce_mode
= create_ce_mode(mode
);
213 return refresh_cache_entry(ce
, 0);
218 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
219 const char *path
, int stage
, int refresh
, int options
)
221 struct cache_entry
*ce
;
222 ce
= make_cache_entry(mode
, sha1
? sha1
: null_sha1
, path
, stage
, refresh
);
224 return error("addinfo_cache failed for path '%s'", path
);
225 return add_cache_entry(ce
, options
);
229 * This is a global variable which is used in a number of places but
230 * only written to in the 'merge' function.
232 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
233 * don't update the working directory.
234 * 0 => Leave unmerged entries in the cache and update
235 * the working directory.
237 static int index_only
= 0;
239 static int git_merge_trees(int index_only
,
245 struct object_list
*trees
= NULL
;
246 struct unpack_trees_options opts
;
248 memset(&opts
, 0, sizeof(opts
));
255 opts
.fn
= threeway_merge
;
257 object_list_append(&common
->object
, &trees
);
258 object_list_append(&head
->object
, &trees
);
259 object_list_append(&merge
->object
, &trees
);
261 rc
= unpack_trees(trees
, &opts
);
262 cache_tree_free(&active_cache_tree
);
266 static int unmerged_index(void)
269 for (i
= 0; i
< active_nr
; i
++) {
270 struct cache_entry
*ce
= active_cache
[i
];
277 static struct tree
*git_write_tree(void)
279 struct tree
*result
= NULL
;
281 if (unmerged_index()) {
283 output(0, "There are unmerged index entries:");
284 for (i
= 0; i
< active_nr
; i
++) {
285 struct cache_entry
*ce
= active_cache
[i
];
287 output(0, "%d %.*s", ce_stage(ce
), ce_namelen(ce
), ce
->name
);
292 if (!active_cache_tree
)
293 active_cache_tree
= cache_tree();
295 if (!cache_tree_fully_valid(active_cache_tree
) &&
296 cache_tree_update(active_cache_tree
,
297 active_cache
, active_nr
, 0, 0) < 0)
298 die("error building trees");
300 result
= lookup_tree(active_cache_tree
->sha1
);
305 static int save_files_dirs(const unsigned char *sha1
,
306 const char *base
, int baselen
, const char *path
,
307 unsigned int mode
, int stage
)
309 int len
= strlen(path
);
310 char *newpath
= xmalloc(baselen
+ len
+ 1);
311 memcpy(newpath
, base
, baselen
);
312 memcpy(newpath
+ baselen
, path
, len
);
313 newpath
[baselen
+ len
] = '\0';
316 path_list_insert(newpath
, ¤t_directory_set
);
318 path_list_insert(newpath
, ¤t_file_set
);
321 return READ_TREE_RECURSIVE
;
324 static int get_files_dirs(struct tree
*tree
)
327 if (read_tree_recursive(tree
, "", 0, 0, NULL
, save_files_dirs
) != 0)
329 n
= current_file_set
.nr
+ current_directory_set
.nr
;
334 * Returns a index_entry instance which doesn't have to correspond to
335 * a real cache entry in Git's index.
337 static struct stage_data
*insert_stage_data(const char *path
,
338 struct tree
*o
, struct tree
*a
, struct tree
*b
,
339 struct path_list
*entries
)
341 struct path_list_item
*item
;
342 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
343 get_tree_entry(o
->object
.sha1
, path
,
344 e
->stages
[1].sha
, &e
->stages
[1].mode
);
345 get_tree_entry(a
->object
.sha1
, path
,
346 e
->stages
[2].sha
, &e
->stages
[2].mode
);
347 get_tree_entry(b
->object
.sha1
, path
,
348 e
->stages
[3].sha
, &e
->stages
[3].mode
);
349 item
= path_list_insert(path
, entries
);
355 * Create a dictionary mapping file names to stage_data objects. The
356 * dictionary contains one entry for every path with a non-zero stage entry.
358 static struct path_list
*get_unmerged(void)
360 struct path_list
*unmerged
= xcalloc(1, sizeof(struct path_list
));
363 unmerged
->strdup_paths
= 1;
364 total_cnt
+= active_nr
;
366 for (i
= 0; i
< active_nr
; i
++, merged_cnt
++) {
367 struct path_list_item
*item
;
368 struct stage_data
*e
;
369 struct cache_entry
*ce
= active_cache
[i
];
375 item
= path_list_lookup(ce
->name
, unmerged
);
377 item
= path_list_insert(ce
->name
, unmerged
);
378 item
->util
= xcalloc(1, sizeof(struct stage_data
));
381 e
->stages
[ce_stage(ce
)].mode
= ntohl(ce
->ce_mode
);
382 hashcpy(e
->stages
[ce_stage(ce
)].sha
, ce
->sha1
);
390 struct diff_filepair
*pair
;
391 struct stage_data
*src_entry
;
392 struct stage_data
*dst_entry
;
393 unsigned processed
:1;
397 * Get information of all renames which occurred between 'o_tree' and
398 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
399 * 'b_tree') to be able to associate the correct cache entries with
400 * the rename information. 'tree' is always equal to either a_tree or b_tree.
402 static struct path_list
*get_renames(struct tree
*tree
,
406 struct path_list
*entries
)
409 struct path_list
*renames
;
410 struct diff_options opts
;
412 renames
= xcalloc(1, sizeof(struct path_list
));
415 opts
.detect_rename
= DIFF_DETECT_RENAME
;
416 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
417 if (diff_setup_done(&opts
) < 0)
418 die("diff setup failed");
419 diff_tree_sha1(o_tree
->object
.sha1
, tree
->object
.sha1
, "", &opts
);
421 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
422 struct path_list_item
*item
;
424 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
425 if (pair
->status
!= 'R') {
426 diff_free_filepair(pair
);
429 re
= xmalloc(sizeof(*re
));
432 item
= path_list_lookup(re
->pair
->one
->path
, entries
);
434 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
435 o_tree
, a_tree
, b_tree
, entries
);
437 re
->src_entry
= item
->util
;
439 item
= path_list_lookup(re
->pair
->two
->path
, entries
);
441 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
442 o_tree
, a_tree
, b_tree
, entries
);
444 re
->dst_entry
= item
->util
;
445 item
= path_list_insert(pair
->one
->path
, renames
);
448 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
449 diff_queued_diff
.nr
= 0;
454 static int update_stages(const char *path
, struct diff_filespec
*o
,
455 struct diff_filespec
*a
, struct diff_filespec
*b
,
458 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
460 if (remove_file_from_cache(path
))
463 if (add_cacheinfo(o
->mode
, o
->sha1
, path
, 1, 0, options
))
466 if (add_cacheinfo(a
->mode
, a
->sha1
, path
, 2, 0, options
))
469 if (add_cacheinfo(b
->mode
, b
->sha1
, path
, 3, 0, options
))
474 static int remove_path(const char *name
)
483 dirs
= xmalloc(len
+1);
484 memcpy(dirs
, name
, len
);
486 while ((slash
= strrchr(name
, '/'))) {
489 if (rmdir(name
) != 0)
496 static int remove_file(int clean
, const char *path
, int no_wd
)
498 int update_cache
= index_only
|| clean
;
499 int update_working_directory
= !index_only
&& !no_wd
;
502 if (remove_file_from_cache(path
))
505 if (update_working_directory
) {
507 if (errno
!= ENOENT
|| errno
!= EISDIR
)
514 static char *unique_path(const char *path
, const char *branch
)
516 char *newpath
= xmalloc(strlen(path
) + 1 + strlen(branch
) + 8 + 1);
519 char *p
= newpath
+ strlen(path
);
520 strcpy(newpath
, path
);
526 while (path_list_has_path(¤t_file_set
, newpath
) ||
527 path_list_has_path(¤t_directory_set
, newpath
) ||
528 lstat(newpath
, &st
) == 0)
529 sprintf(p
, "_%d", suffix
++);
531 path_list_insert(newpath
, ¤t_file_set
);
535 static int mkdir_p(const char *path
, unsigned long mode
)
537 /* path points to cache entries, so xstrdup before messing with it */
538 char *buf
= xstrdup(path
);
539 int result
= safe_create_leading_directories(buf
);
544 static void flush_buffer(int fd
, const char *buf
, unsigned long size
)
547 long ret
= write_in_full(fd
, buf
, size
);
552 die("merge-recursive: %s", strerror(errno
));
554 die("merge-recursive: disk full?");
561 static void update_file_flags(const unsigned char *sha
,
571 enum object_type type
;
575 buf
= read_sha1_file(sha
, &type
, &size
);
577 die("cannot read object %s '%s'", sha1_to_hex(sha
), path
);
578 if (type
!= OBJ_BLOB
)
579 die("blob expected for %s '%s'", sha1_to_hex(sha
), path
);
581 if (S_ISREG(mode
) || (!has_symlinks
&& S_ISLNK(mode
))) {
583 if (mkdir_p(path
, 0777))
584 die("failed to create path %s: %s", path
, strerror(errno
));
590 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
592 die("failed to open %s: %s", path
, strerror(errno
));
593 flush_buffer(fd
, buf
, size
);
595 } else if (S_ISLNK(mode
)) {
596 char *lnk
= xmalloc(size
+ 1);
597 memcpy(lnk
, buf
, size
);
604 die("do not know what to do with %06o %s '%s'",
605 mode
, sha1_to_hex(sha
), path
);
608 add_cacheinfo(mode
, sha
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
611 static void update_file(int clean
,
612 const unsigned char *sha
,
616 update_file_flags(sha
, mode
, path
, index_only
|| clean
, !index_only
);
619 /* Low level file merging, update and removal */
621 struct merge_file_info
623 unsigned char sha
[20];
629 static void fill_mm(const unsigned char *sha1
, mmfile_t
*mm
)
632 enum object_type type
;
634 if (!hashcmp(sha1
, null_sha1
)) {
635 mm
->ptr
= xstrdup("");
640 mm
->ptr
= read_sha1_file(sha1
, &type
, &size
);
641 if (!mm
->ptr
|| type
!= OBJ_BLOB
)
642 die("unable to read blob object %s", sha1_to_hex(sha1
));
646 static struct merge_file_info
merge_file(struct diff_filespec
*o
,
647 struct diff_filespec
*a
, struct diff_filespec
*b
,
648 const char *branch1
, const char *branch2
)
650 struct merge_file_info result
;
654 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
656 if (S_ISREG(a
->mode
)) {
657 result
.mode
= a
->mode
;
658 hashcpy(result
.sha
, a
->sha1
);
660 result
.mode
= b
->mode
;
661 hashcpy(result
.sha
, b
->sha1
);
664 if (!sha_eq(a
->sha1
, o
->sha1
) && !sha_eq(b
->sha1
, o
->sha1
))
667 result
.mode
= a
->mode
== o
->mode
? b
->mode
: a
->mode
;
669 if (sha_eq(a
->sha1
, o
->sha1
))
670 hashcpy(result
.sha
, b
->sha1
);
671 else if (sha_eq(b
->sha1
, o
->sha1
))
672 hashcpy(result
.sha
, a
->sha1
);
673 else if (S_ISREG(a
->mode
)) {
674 mmfile_t orig
, src1
, src2
;
675 mmbuffer_t result_buf
;
680 name1
= xstrdup(mkpath("%s:%s", branch1
, a
->path
));
681 name2
= xstrdup(mkpath("%s:%s", branch2
, b
->path
));
683 fill_mm(o
->sha1
, &orig
);
684 fill_mm(a
->sha1
, &src1
);
685 fill_mm(b
->sha1
, &src2
);
687 memset(&xpp
, 0, sizeof(xpp
));
688 merge_status
= xdl_merge(&orig
,
691 &xpp
, XDL_MERGE_ZEALOUS
,
699 if ((merge_status
< 0) || !result_buf
.ptr
)
700 die("Failed to execute internal merge");
702 if (write_sha1_file(result_buf
.ptr
, result_buf
.size
,
703 blob_type
, result
.sha
))
704 die("Unable to add %s to database",
707 free(result_buf
.ptr
);
708 result
.clean
= (merge_status
== 0);
710 if (!(S_ISLNK(a
->mode
) || S_ISLNK(b
->mode
)))
711 die("cannot merge modes?");
713 hashcpy(result
.sha
, a
->sha1
);
715 if (!sha_eq(a
->sha1
, b
->sha1
))
723 static void conflict_rename_rename(struct rename
*ren1
,
730 const char *ren1_dst
= ren1
->pair
->two
->path
;
731 const char *ren2_dst
= ren2
->pair
->two
->path
;
732 const char *dst_name1
= ren1_dst
;
733 const char *dst_name2
= ren2_dst
;
734 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
735 dst_name1
= del
[delp
++] = unique_path(ren1_dst
, branch1
);
736 output(1, "%s is a directory in %s added as %s instead",
737 ren1_dst
, branch2
, dst_name1
);
738 remove_file(0, ren1_dst
, 0);
740 if (path_list_has_path(¤t_directory_set
, ren2_dst
)) {
741 dst_name2
= del
[delp
++] = unique_path(ren2_dst
, branch2
);
742 output(1, "%s is a directory in %s added as %s instead",
743 ren2_dst
, branch1
, dst_name2
);
744 remove_file(0, ren2_dst
, 0);
747 remove_file_from_cache(dst_name1
);
748 remove_file_from_cache(dst_name2
);
750 * Uncomment to leave the conflicting names in the resulting tree
752 * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
753 * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
756 update_stages(dst_name1
, NULL
, ren1
->pair
->two
, NULL
, 1);
757 update_stages(dst_name2
, NULL
, NULL
, ren2
->pair
->two
, 1);
763 static void conflict_rename_dir(struct rename
*ren1
,
766 char *new_path
= unique_path(ren1
->pair
->two
->path
, branch1
);
767 output(1, "Renamed %s to %s instead", ren1
->pair
->one
->path
, new_path
);
768 remove_file(0, ren1
->pair
->two
->path
, 0);
769 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path
);
773 static void conflict_rename_rename_2(struct rename
*ren1
,
778 char *new_path1
= unique_path(ren1
->pair
->two
->path
, branch1
);
779 char *new_path2
= unique_path(ren2
->pair
->two
->path
, branch2
);
780 output(1, "Renamed %s to %s and %s to %s instead",
781 ren1
->pair
->one
->path
, new_path1
,
782 ren2
->pair
->one
->path
, new_path2
);
783 remove_file(0, ren1
->pair
->two
->path
, 0);
784 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path1
);
785 update_file(0, ren2
->pair
->two
->sha1
, ren2
->pair
->two
->mode
, new_path2
);
790 static int process_renames(struct path_list
*a_renames
,
791 struct path_list
*b_renames
,
792 const char *a_branch
,
793 const char *b_branch
)
795 int clean_merge
= 1, i
, j
;
796 struct path_list a_by_dst
= {NULL
, 0, 0, 0}, b_by_dst
= {NULL
, 0, 0, 0};
797 const struct rename
*sre
;
799 for (i
= 0; i
< a_renames
->nr
; i
++) {
800 sre
= a_renames
->items
[i
].util
;
801 path_list_insert(sre
->pair
->two
->path
, &a_by_dst
)->util
804 for (i
= 0; i
< b_renames
->nr
; i
++) {
805 sre
= b_renames
->items
[i
].util
;
806 path_list_insert(sre
->pair
->two
->path
, &b_by_dst
)->util
810 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
813 struct path_list
*renames1
, *renames2
, *renames2Dst
;
814 struct rename
*ren1
= NULL
, *ren2
= NULL
;
815 const char *branch1
, *branch2
;
816 const char *ren1_src
, *ren1_dst
;
818 if (i
>= a_renames
->nr
) {
820 ren2
= b_renames
->items
[j
++].util
;
821 } else if (j
>= b_renames
->nr
) {
823 ren1
= a_renames
->items
[i
++].util
;
825 compare
= strcmp(a_renames
->items
[i
].path
,
826 b_renames
->items
[j
].path
);
828 ren1
= a_renames
->items
[i
++].util
;
830 ren2
= b_renames
->items
[j
++].util
;
833 /* TODO: refactor, so that 1/2 are not needed */
835 renames1
= a_renames
;
836 renames2
= b_renames
;
837 renames2Dst
= &b_by_dst
;
842 renames1
= b_renames
;
843 renames2
= a_renames
;
844 renames2Dst
= &a_by_dst
;
851 src
= ren1
->pair
->one
->path
;
853 ren1
->dst_entry
->processed
= 1;
854 ren1
->src_entry
->processed
= 1;
860 ren1_src
= ren1
->pair
->one
->path
;
861 ren1_dst
= ren1
->pair
->two
->path
;
864 const char *ren2_src
= ren2
->pair
->one
->path
;
865 const char *ren2_dst
= ren2
->pair
->two
->path
;
866 /* Renamed in 1 and renamed in 2 */
867 if (strcmp(ren1_src
, ren2_src
) != 0)
868 die("ren1.src != ren2.src");
869 ren2
->dst_entry
->processed
= 1;
871 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
873 output(1, "CONFLICT (rename/rename): "
874 "Rename \"%s\"->\"%s\" in branch \"%s\" "
875 "rename \"%s\"->\"%s\" in \"%s\"%s",
876 src
, ren1_dst
, branch1
,
877 src
, ren2_dst
, branch2
,
878 index_only
? " (left unresolved)": "");
880 remove_file_from_cache(src
);
881 update_file(0, ren1
->pair
->one
->sha1
,
882 ren1
->pair
->one
->mode
, src
);
884 conflict_rename_rename(ren1
, branch1
, ren2
, branch2
);
886 struct merge_file_info mfi
;
887 remove_file(1, ren1_src
, 1);
888 mfi
= merge_file(ren1
->pair
->one
,
893 if (mfi
.merge
|| !mfi
.clean
)
894 output(1, "Renamed %s->%s", src
, ren1_dst
);
897 output(2, "Auto-merged %s", ren1_dst
);
900 output(1, "CONFLICT (content): merge conflict in %s",
905 update_stages(ren1_dst
,
911 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
914 /* Renamed in 1, maybe changed in 2 */
915 struct path_list_item
*item
;
916 /* we only use sha1 and mode of these */
917 struct diff_filespec src_other
, dst_other
;
918 int try_merge
, stage
= a_renames
== renames1
? 3: 2;
920 remove_file(1, ren1_src
, index_only
|| stage
== 3);
922 hashcpy(src_other
.sha1
, ren1
->src_entry
->stages
[stage
].sha
);
923 src_other
.mode
= ren1
->src_entry
->stages
[stage
].mode
;
924 hashcpy(dst_other
.sha1
, ren1
->dst_entry
->stages
[stage
].sha
);
925 dst_other
.mode
= ren1
->dst_entry
->stages
[stage
].mode
;
929 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
931 output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s "
932 " directory %s added in %s",
933 ren1_src
, ren1_dst
, branch1
,
935 conflict_rename_dir(ren1
, branch1
);
936 } else if (sha_eq(src_other
.sha1
, null_sha1
)) {
938 output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s "
940 ren1_src
, ren1_dst
, branch1
,
942 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, ren1_dst
);
943 } else if (!sha_eq(dst_other
.sha1
, null_sha1
)) {
944 const char *new_path
;
947 output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. "
949 ren1_src
, ren1_dst
, branch1
,
951 new_path
= unique_path(ren1_dst
, branch2
);
952 output(1, "Added as %s instead", new_path
);
953 update_file(0, dst_other
.sha1
, dst_other
.mode
, new_path
);
954 } else if ((item
= path_list_lookup(ren1_dst
, renames2Dst
))) {
958 output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. "
959 "Renamed %s->%s in %s",
960 ren1_src
, ren1_dst
, branch1
,
961 ren2
->pair
->one
->path
, ren2
->pair
->two
->path
, branch2
);
962 conflict_rename_rename_2(ren1
, branch1
, ren2
, branch2
);
967 struct diff_filespec
*o
, *a
, *b
;
968 struct merge_file_info mfi
;
969 src_other
.path
= (char *)ren1_src
;
972 if (a_renames
== renames1
) {
979 mfi
= merge_file(o
, a
, b
,
982 if (mfi
.merge
|| !mfi
.clean
)
983 output(1, "Renamed %s => %s", ren1_src
, ren1_dst
);
985 output(2, "Auto-merged %s", ren1_dst
);
987 output(1, "CONFLICT (rename/modify): Merge conflict in %s",
992 update_stages(ren1_dst
,
995 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
999 path_list_clear(&a_by_dst
, 0);
1000 path_list_clear(&b_by_dst
, 0);
1005 static unsigned char *has_sha(const unsigned char *sha
)
1007 return is_null_sha1(sha
) ? NULL
: (unsigned char *)sha
;
1010 /* Per entry merge function */
1011 static int process_entry(const char *path
, struct stage_data
*entry
,
1012 const char *branch1
,
1013 const char *branch2
)
1016 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1017 print_index_entry("\tpath: ", entry);
1019 int clean_merge
= 1;
1020 unsigned char *o_sha
= has_sha(entry
->stages
[1].sha
);
1021 unsigned char *a_sha
= has_sha(entry
->stages
[2].sha
);
1022 unsigned char *b_sha
= has_sha(entry
->stages
[3].sha
);
1023 unsigned o_mode
= entry
->stages
[1].mode
;
1024 unsigned a_mode
= entry
->stages
[2].mode
;
1025 unsigned b_mode
= entry
->stages
[3].mode
;
1027 if (o_sha
&& (!a_sha
|| !b_sha
)) {
1028 /* Case A: Deleted in one */
1029 if ((!a_sha
&& !b_sha
) ||
1030 (sha_eq(a_sha
, o_sha
) && !b_sha
) ||
1031 (!a_sha
&& sha_eq(b_sha
, o_sha
))) {
1032 /* Deleted in both or deleted in one and
1033 * unchanged in the other */
1035 output(2, "Removed %s", path
);
1036 /* do not touch working file if it did not exist */
1037 remove_file(1, path
, !a_sha
);
1039 /* Deleted in one and changed in the other */
1042 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1043 "and modified in %s. Version %s of %s left in tree.",
1045 branch2
, branch2
, path
);
1046 update_file(0, b_sha
, b_mode
, path
);
1048 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1049 "and modified in %s. Version %s of %s left in tree.",
1051 branch1
, branch1
, path
);
1052 update_file(0, a_sha
, a_mode
, path
);
1056 } else if ((!o_sha
&& a_sha
&& !b_sha
) ||
1057 (!o_sha
&& !a_sha
&& b_sha
)) {
1058 /* Case B: Added in one. */
1059 const char *add_branch
;
1060 const char *other_branch
;
1062 const unsigned char *sha
;
1066 add_branch
= branch1
;
1067 other_branch
= branch2
;
1070 conf
= "file/directory";
1072 add_branch
= branch2
;
1073 other_branch
= branch1
;
1076 conf
= "directory/file";
1078 if (path_list_has_path(¤t_directory_set
, path
)) {
1079 const char *new_path
= unique_path(path
, add_branch
);
1081 output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
1083 conf
, path
, other_branch
, path
, new_path
);
1084 remove_file(0, path
, 0);
1085 update_file(0, sha
, mode
, new_path
);
1087 output(2, "Added %s", path
);
1088 update_file(1, sha
, mode
, path
);
1090 } else if (a_sha
&& b_sha
) {
1091 /* Case C: Added in both (check for same permissions) and */
1092 /* case D: Modified in both, but differently. */
1093 const char *reason
= "content";
1094 struct merge_file_info mfi
;
1095 struct diff_filespec o
, a
, b
;
1099 o_sha
= (unsigned char *)null_sha1
;
1101 output(2, "Auto-merged %s", path
);
1102 o
.path
= a
.path
= b
.path
= (char *)path
;
1103 hashcpy(o
.sha1
, o_sha
);
1105 hashcpy(a
.sha1
, a_sha
);
1107 hashcpy(b
.sha1
, b_sha
);
1110 mfi
= merge_file(&o
, &a
, &b
,
1114 update_file(1, mfi
.sha
, mfi
.mode
, path
);
1117 output(1, "CONFLICT (%s): Merge conflict in %s",
1121 update_file(0, mfi
.sha
, mfi
.mode
, path
);
1123 update_file_flags(mfi
.sha
, mfi
.mode
, path
,
1124 0 /* update_cache */, 1 /* update_working_directory */);
1127 die("Fatal merge failure, shouldn't happen.");
1132 static int merge_trees(struct tree
*head
,
1134 struct tree
*common
,
1135 const char *branch1
,
1136 const char *branch2
,
1137 struct tree
**result
)
1140 if (sha_eq(common
->object
.sha1
, merge
->object
.sha1
)) {
1141 output(0, "Already uptodate!");
1146 code
= git_merge_trees(index_only
, common
, head
, merge
);
1149 die("merging of trees %s and %s failed",
1150 sha1_to_hex(head
->object
.sha1
),
1151 sha1_to_hex(merge
->object
.sha1
));
1153 if (unmerged_index()) {
1154 struct path_list
*entries
, *re_head
, *re_merge
;
1156 path_list_clear(¤t_file_set
, 1);
1157 path_list_clear(¤t_directory_set
, 1);
1158 get_files_dirs(head
);
1159 get_files_dirs(merge
);
1161 entries
= get_unmerged();
1162 re_head
= get_renames(head
, common
, head
, merge
, entries
);
1163 re_merge
= get_renames(merge
, common
, head
, merge
, entries
);
1164 clean
= process_renames(re_head
, re_merge
,
1166 total_cnt
+= entries
->nr
;
1167 for (i
= 0; i
< entries
->nr
; i
++, merged_cnt
++) {
1168 const char *path
= entries
->items
[i
].path
;
1169 struct stage_data
*e
= entries
->items
[i
].util
;
1171 && !process_entry(path
, e
, branch1
, branch2
))
1177 path_list_clear(re_merge
, 0);
1178 path_list_clear(re_head
, 0);
1179 path_list_clear(entries
, 1);
1186 *result
= git_write_tree();
1191 static struct commit_list
*reverse_commit_list(struct commit_list
*list
)
1193 struct commit_list
*next
= NULL
, *current
, *backup
;
1194 for (current
= list
; current
; current
= backup
) {
1195 backup
= current
->next
;
1196 current
->next
= next
;
1203 * Merge the commits h1 and h2, return the resulting virtual
1204 * commit object and a flag indicating the cleanness of the merge.
1206 static int merge(struct commit
*h1
,
1208 const char *branch1
,
1209 const char *branch2
,
1210 struct commit_list
*ca
,
1211 struct commit
**result
)
1213 struct commit_list
*iter
;
1214 struct commit
*merged_common_ancestors
;
1215 struct tree
*mrtree
;
1219 output(4, "Merging:");
1220 output_commit_title(h1
);
1221 output_commit_title(h2
);
1225 ca
= get_merge_bases(h1
, h2
, 1);
1226 ca
= reverse_commit_list(ca
);
1230 output(5, "found %u common ancestor(s):", commit_list_count(ca
));
1231 for (iter
= ca
; iter
; iter
= iter
->next
)
1232 output_commit_title(iter
->item
);
1235 merged_common_ancestors
= pop_commit(&ca
);
1236 if (merged_common_ancestors
== NULL
) {
1237 /* if there is no common ancestor, make an empty tree */
1238 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
1240 tree
->object
.parsed
= 1;
1241 tree
->object
.type
= OBJ_TREE
;
1242 pretend_sha1_file(NULL
, 0, OBJ_TREE
, tree
->object
.sha1
);
1243 merged_common_ancestors
= make_virtual_commit(tree
, "ancestor");
1246 for (iter
= ca
; iter
; iter
= iter
->next
) {
1249 * When the merge fails, the result contains files
1250 * with conflict markers. The cleanness flag is
1251 * ignored, it was never actually used, as result of
1252 * merge_trees has always overwritten it: the committed
1253 * "conflicts" were already resolved.
1256 merge(merged_common_ancestors
, iter
->item
,
1257 "Temporary merge branch 1",
1258 "Temporary merge branch 2",
1260 &merged_common_ancestors
);
1263 if (!merged_common_ancestors
)
1264 die("merge returned no commit");
1274 clean
= merge_trees(h1
->tree
, h2
->tree
, merged_common_ancestors
->tree
,
1275 branch1
, branch2
, &mrtree
);
1278 *result
= make_virtual_commit(mrtree
, "merged tree");
1279 commit_list_insert(h1
, &(*result
)->parents
);
1280 commit_list_insert(h2
, &(*result
)->parents
->next
);
1282 if (!call_depth
&& do_progress
) {
1283 /* Make sure we end at 100% */
1286 merged_cnt
= total_cnt
;
1287 progress_update
= 1;
1289 fputc('\n', stderr
);
1295 static const char *better_branch_name(const char *branch
)
1297 static char githead_env
[8 + 40 + 1];
1300 if (strlen(branch
) != 40)
1302 sprintf(githead_env
, "GITHEAD_%s", branch
);
1303 name
= getenv(githead_env
);
1304 return name
? name
: branch
;
1307 static struct commit
*get_ref(const char *ref
)
1309 unsigned char sha1
[20];
1310 struct object
*object
;
1312 if (get_sha1(ref
, sha1
))
1313 die("Could not resolve ref '%s'", ref
);
1314 object
= deref_tag(parse_object(sha1
), ref
, strlen(ref
));
1315 if (object
->type
== OBJ_TREE
)
1316 return make_virtual_commit((struct tree
*)object
,
1317 better_branch_name(ref
));
1318 if (object
->type
!= OBJ_COMMIT
)
1320 if (parse_commit((struct commit
*)object
))
1321 die("Could not parse commit '%s'", sha1_to_hex(object
->sha1
));
1322 return (struct commit
*)object
;
1325 static int merge_config(const char *var
, const char *value
)
1327 if (!strcasecmp(var
, "merge.verbosity")) {
1328 verbosity
= git_config_int(var
, value
);
1331 return git_default_config(var
, value
);
1334 int main(int argc
, char *argv
[])
1336 static const char *bases
[20];
1337 static unsigned bases_count
= 0;
1339 const char *branch1
, *branch2
;
1340 struct commit
*result
, *h1
, *h2
;
1341 struct commit_list
*ca
= NULL
;
1342 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
1345 git_config(merge_config
);
1346 if (getenv("GIT_MERGE_VERBOSITY"))
1347 verbosity
= strtol(getenv("GIT_MERGE_VERBOSITY"), NULL
, 10);
1350 die("Usage: %s <base>... -- <head> <remote> ...\n", argv
[0]);
1352 for (i
= 1; i
< argc
; ++i
) {
1353 if (!strcmp(argv
[i
], "--"))
1355 if (bases_count
< sizeof(bases
)/sizeof(*bases
))
1356 bases
[bases_count
++] = argv
[i
];
1358 if (argc
- i
!= 3) /* "--" "<head>" "<remote>" */
1359 die("Not handling anything other than two heads merge.");
1360 if (verbosity
>= 5) {
1365 do_progress
= isatty(1);
1367 branch1
= argv
[++i
];
1368 branch2
= argv
[++i
];
1370 h1
= get_ref(branch1
);
1371 h2
= get_ref(branch2
);
1373 branch1
= better_branch_name(branch1
);
1374 branch2
= better_branch_name(branch2
);
1377 setup_progress_signal();
1379 printf("Merging %s with %s\n", branch1
, branch2
);
1381 index_fd
= hold_lock_file_for_update(lock
, get_index_file(), 1);
1383 for (i
= 0; i
< bases_count
; i
++) {
1384 struct commit
*ancestor
= get_ref(bases
[i
]);
1385 ca
= commit_list_insert(ancestor
, &ca
);
1387 clean
= merge(h1
, h2
, branch1
, branch2
, ca
, &result
);
1389 if (active_cache_changed
&&
1390 (write_cache(index_fd
, active_cache
, active_nr
) ||
1391 close(index_fd
) || commit_lock_file(lock
)))
1392 die ("unable to write %s", get_index_file());
1394 return clean
? 0: 1;