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"
19 static int subtree_merge
;
21 static struct tree
*shift_tree_object(struct tree
*one
, struct tree
*two
)
23 unsigned char shifted
[20];
26 * NEEDSWORK: this limits the recursion depth to hardcoded
27 * value '2' to avoid excessive overhead.
29 shift_tree(one
->object
.sha1
, two
->object
.sha1
, shifted
, 2);
30 if (!hashcmp(two
->object
.sha1
, shifted
))
32 return lookup_tree(shifted
);
36 * A virtual commit has
37 * - (const char *)commit->util set to the name, and
38 * - *(int *)commit->object.sha1 set to the virtual id.
41 static unsigned commit_list_count(const struct commit_list
*l
)
44 for (; l
; l
= l
->next
)
49 static struct commit
*make_virtual_commit(struct tree
*tree
, const char *comment
)
51 struct commit
*commit
= xcalloc(1, sizeof(struct commit
));
52 static unsigned virtual_id
= 1;
54 commit
->util
= (void*)comment
;
55 *(int*)commit
->object
.sha1
= virtual_id
++;
57 commit
->object
.parsed
= 1;
62 * Since we use get_tree_entry(), which does not put the read object into
63 * the object pool, we cannot rely on a == b.
65 static int sha_eq(const unsigned char *a
, const unsigned char *b
)
69 return a
&& b
&& hashcmp(a
, b
) == 0;
73 * Since we want to write the index eventually, we cannot reuse the index
74 * for these (temporary) data.
81 unsigned char sha
[20];
88 struct output_buffer
*next
;
92 static struct path_list current_file_set
= {NULL
, 0, 0, 1};
93 static struct path_list current_directory_set
= {NULL
, 0, 0, 1};
95 static int call_depth
= 0;
96 static int verbosity
= 2;
97 static int buffer_output
= 1;
98 static struct output_buffer
*output_list
, *output_end
;
100 static int show (int v
)
102 return (!call_depth
&& verbosity
>= v
) || verbosity
>= 5;
105 static void output(int v
, const char *fmt
, ...)
109 if (buffer_output
&& show(v
)) {
110 struct output_buffer
*b
= xmalloc(sizeof(*b
));
111 nfvasprintf(&b
->str
, fmt
, args
);
114 output_end
->next
= b
;
118 } else if (show(v
)) {
120 for (i
= call_depth
; i
--;)
122 vfprintf(stdout
, fmt
, args
);
128 static void flush_output()
130 struct output_buffer
*b
, *n
;
131 for (b
= output_list
; b
; b
= n
) {
133 for (i
= call_depth
; i
--;)
135 fputs(b
->str
, stdout
);
145 static void output_commit_title(struct commit
*commit
)
149 for (i
= call_depth
; i
--;)
152 printf("virtual %s\n", (char *)commit
->util
);
154 printf("%s ", find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
));
155 if (parse_commit(commit
) != 0)
156 printf("(bad commit)\n");
160 for (s
= commit
->buffer
; *s
; s
++)
161 if (*s
== '\n' && s
[1] == '\n') {
165 for (len
= 0; s
[len
] && '\n' != s
[len
]; len
++)
167 printf("%.*s\n", len
, s
);
172 static struct cache_entry
*make_cache_entry(unsigned int mode
,
173 const unsigned char *sha1
, const char *path
, int stage
, int refresh
)
176 struct cache_entry
*ce
;
178 if (!verify_path(path
))
182 size
= cache_entry_size(len
);
183 ce
= xcalloc(1, size
);
185 hashcpy(ce
->sha1
, sha1
);
186 memcpy(ce
->name
, path
, len
);
187 ce
->ce_flags
= create_ce_flags(len
, stage
);
188 ce
->ce_mode
= create_ce_mode(mode
);
191 return refresh_cache_entry(ce
, 0);
196 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
197 const char *path
, int stage
, int refresh
, int options
)
199 struct cache_entry
*ce
;
200 ce
= make_cache_entry(mode
, sha1
? sha1
: null_sha1
, path
, stage
, refresh
);
202 return error("addinfo_cache failed for path '%s'", path
);
203 return add_cache_entry(ce
, options
);
207 * This is a global variable which is used in a number of places but
208 * only written to in the 'merge' function.
210 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
211 * don't update the working directory.
212 * 0 => Leave unmerged entries in the cache and update
213 * the working directory.
215 static int index_only
= 0;
217 static int git_merge_trees(int index_only
,
223 struct object_list
*trees
= NULL
;
224 struct unpack_trees_options opts
;
226 memset(&opts
, 0, sizeof(opts
));
233 opts
.fn
= threeway_merge
;
235 object_list_append(&common
->object
, &trees
);
236 object_list_append(&head
->object
, &trees
);
237 object_list_append(&merge
->object
, &trees
);
239 rc
= unpack_trees(trees
, &opts
);
240 cache_tree_free(&active_cache_tree
);
244 static int unmerged_index(void)
247 for (i
= 0; i
< active_nr
; i
++) {
248 struct cache_entry
*ce
= active_cache
[i
];
255 static struct tree
*git_write_tree(void)
257 struct tree
*result
= NULL
;
259 if (unmerged_index()) {
261 output(0, "There are unmerged index entries:");
262 for (i
= 0; i
< active_nr
; i
++) {
263 struct cache_entry
*ce
= active_cache
[i
];
265 output(0, "%d %.*s", ce_stage(ce
), ce_namelen(ce
), ce
->name
);
270 if (!active_cache_tree
)
271 active_cache_tree
= cache_tree();
273 if (!cache_tree_fully_valid(active_cache_tree
) &&
274 cache_tree_update(active_cache_tree
,
275 active_cache
, active_nr
, 0, 0) < 0)
276 die("error building trees");
278 result
= lookup_tree(active_cache_tree
->sha1
);
283 static int save_files_dirs(const unsigned char *sha1
,
284 const char *base
, int baselen
, const char *path
,
285 unsigned int mode
, int stage
)
287 int len
= strlen(path
);
288 char *newpath
= xmalloc(baselen
+ len
+ 1);
289 memcpy(newpath
, base
, baselen
);
290 memcpy(newpath
+ baselen
, path
, len
);
291 newpath
[baselen
+ len
] = '\0';
294 path_list_insert(newpath
, ¤t_directory_set
);
296 path_list_insert(newpath
, ¤t_file_set
);
299 return READ_TREE_RECURSIVE
;
302 static int get_files_dirs(struct tree
*tree
)
305 if (read_tree_recursive(tree
, "", 0, 0, NULL
, save_files_dirs
) != 0)
307 n
= current_file_set
.nr
+ current_directory_set
.nr
;
312 * Returns a index_entry instance which doesn't have to correspond to
313 * a real cache entry in Git's index.
315 static struct stage_data
*insert_stage_data(const char *path
,
316 struct tree
*o
, struct tree
*a
, struct tree
*b
,
317 struct path_list
*entries
)
319 struct path_list_item
*item
;
320 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
321 get_tree_entry(o
->object
.sha1
, path
,
322 e
->stages
[1].sha
, &e
->stages
[1].mode
);
323 get_tree_entry(a
->object
.sha1
, path
,
324 e
->stages
[2].sha
, &e
->stages
[2].mode
);
325 get_tree_entry(b
->object
.sha1
, path
,
326 e
->stages
[3].sha
, &e
->stages
[3].mode
);
327 item
= path_list_insert(path
, entries
);
333 * Create a dictionary mapping file names to stage_data objects. The
334 * dictionary contains one entry for every path with a non-zero stage entry.
336 static struct path_list
*get_unmerged(void)
338 struct path_list
*unmerged
= xcalloc(1, sizeof(struct path_list
));
341 unmerged
->strdup_paths
= 1;
343 for (i
= 0; i
< active_nr
; i
++) {
344 struct path_list_item
*item
;
345 struct stage_data
*e
;
346 struct cache_entry
*ce
= active_cache
[i
];
350 item
= path_list_lookup(ce
->name
, unmerged
);
352 item
= path_list_insert(ce
->name
, unmerged
);
353 item
->util
= xcalloc(1, sizeof(struct stage_data
));
356 e
->stages
[ce_stage(ce
)].mode
= ntohl(ce
->ce_mode
);
357 hashcpy(e
->stages
[ce_stage(ce
)].sha
, ce
->sha1
);
365 struct diff_filepair
*pair
;
366 struct stage_data
*src_entry
;
367 struct stage_data
*dst_entry
;
368 unsigned processed
:1;
372 * Get information of all renames which occurred between 'o_tree' and
373 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
374 * 'b_tree') to be able to associate the correct cache entries with
375 * the rename information. 'tree' is always equal to either a_tree or b_tree.
377 static struct path_list
*get_renames(struct tree
*tree
,
381 struct path_list
*entries
)
384 struct path_list
*renames
;
385 struct diff_options opts
;
387 renames
= xcalloc(1, sizeof(struct path_list
));
390 opts
.detect_rename
= DIFF_DETECT_RENAME
;
391 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
392 if (diff_setup_done(&opts
) < 0)
393 die("diff setup failed");
394 diff_tree_sha1(o_tree
->object
.sha1
, tree
->object
.sha1
, "", &opts
);
396 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
397 struct path_list_item
*item
;
399 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
400 if (pair
->status
!= 'R') {
401 diff_free_filepair(pair
);
404 re
= xmalloc(sizeof(*re
));
407 item
= path_list_lookup(re
->pair
->one
->path
, entries
);
409 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
410 o_tree
, a_tree
, b_tree
, entries
);
412 re
->src_entry
= item
->util
;
414 item
= path_list_lookup(re
->pair
->two
->path
, entries
);
416 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
417 o_tree
, a_tree
, b_tree
, entries
);
419 re
->dst_entry
= item
->util
;
420 item
= path_list_insert(pair
->one
->path
, renames
);
423 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
424 diff_queued_diff
.nr
= 0;
429 static int update_stages(const char *path
, struct diff_filespec
*o
,
430 struct diff_filespec
*a
, struct diff_filespec
*b
,
433 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
435 if (remove_file_from_cache(path
))
438 if (add_cacheinfo(o
->mode
, o
->sha1
, path
, 1, 0, options
))
441 if (add_cacheinfo(a
->mode
, a
->sha1
, path
, 2, 0, options
))
444 if (add_cacheinfo(b
->mode
, b
->sha1
, path
, 3, 0, options
))
449 static int remove_path(const char *name
)
458 dirs
= xmalloc(len
+1);
459 memcpy(dirs
, name
, len
);
461 while ((slash
= strrchr(name
, '/'))) {
464 if (rmdir(name
) != 0)
471 static int remove_file(int clean
, const char *path
, int no_wd
)
473 int update_cache
= index_only
|| clean
;
474 int update_working_directory
= !index_only
&& !no_wd
;
477 if (remove_file_from_cache(path
))
480 if (update_working_directory
) {
482 if (errno
!= ENOENT
|| errno
!= EISDIR
)
489 static char *unique_path(const char *path
, const char *branch
)
491 char *newpath
= xmalloc(strlen(path
) + 1 + strlen(branch
) + 8 + 1);
494 char *p
= newpath
+ strlen(path
);
495 strcpy(newpath
, path
);
501 while (path_list_has_path(¤t_file_set
, newpath
) ||
502 path_list_has_path(¤t_directory_set
, newpath
) ||
503 lstat(newpath
, &st
) == 0)
504 sprintf(p
, "_%d", suffix
++);
506 path_list_insert(newpath
, ¤t_file_set
);
510 static int mkdir_p(const char *path
, unsigned long mode
)
512 /* path points to cache entries, so xstrdup before messing with it */
513 char *buf
= xstrdup(path
);
514 int result
= safe_create_leading_directories(buf
);
519 static void flush_buffer(int fd
, const char *buf
, unsigned long size
)
522 long ret
= write_in_full(fd
, buf
, size
);
527 die("merge-recursive: %s", strerror(errno
));
529 die("merge-recursive: disk full?");
536 static int make_room_for_path(const char *path
)
539 const char *msg
= "failed to create path '%s'%s";
541 status
= mkdir_p(path
, 0777);
544 /* something else exists */
545 error(msg
, path
, ": perhaps a D/F conflict?");
551 /* Successful unlink is good.. */
554 /* .. and so is no existing file */
557 /* .. but not some other error (who really cares what?) */
558 return error(msg
, path
, ": perhaps a D/F conflict?");
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 (make_room_for_path(path
) < 0) {
585 if (S_ISREG(mode
) || (!has_symlinks
&& S_ISLNK(mode
))) {
591 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
593 die("failed to open %s: %s", path
, strerror(errno
));
594 flush_buffer(fd
, buf
, size
);
596 } else if (S_ISLNK(mode
)) {
597 char *lnk
= xmalloc(size
+ 1);
598 memcpy(lnk
, buf
, size
);
605 die("do not know what to do with %06o %s '%s'",
606 mode
, sha1_to_hex(sha
), path
);
610 add_cacheinfo(mode
, sha
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
613 static void update_file(int clean
,
614 const unsigned char *sha
,
618 update_file_flags(sha
, mode
, path
, index_only
|| clean
, !index_only
);
621 /* Low level file merging, update and removal */
623 struct merge_file_info
625 unsigned char sha
[20];
631 static void fill_mm(const unsigned char *sha1
, mmfile_t
*mm
)
634 enum object_type type
;
636 if (!hashcmp(sha1
, null_sha1
)) {
637 mm
->ptr
= xstrdup("");
642 mm
->ptr
= read_sha1_file(sha1
, &type
, &size
);
643 if (!mm
->ptr
|| type
!= OBJ_BLOB
)
644 die("unable to read blob object %s", sha1_to_hex(sha1
));
648 static struct merge_file_info
merge_file(struct diff_filespec
*o
,
649 struct diff_filespec
*a
, struct diff_filespec
*b
,
650 const char *branch1
, const char *branch2
)
652 struct merge_file_info result
;
656 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
658 if (S_ISREG(a
->mode
)) {
659 result
.mode
= a
->mode
;
660 hashcpy(result
.sha
, a
->sha1
);
662 result
.mode
= b
->mode
;
663 hashcpy(result
.sha
, b
->sha1
);
666 if (!sha_eq(a
->sha1
, o
->sha1
) && !sha_eq(b
->sha1
, o
->sha1
))
669 result
.mode
= a
->mode
== o
->mode
? b
->mode
: a
->mode
;
671 if (sha_eq(a
->sha1
, o
->sha1
))
672 hashcpy(result
.sha
, b
->sha1
);
673 else if (sha_eq(b
->sha1
, o
->sha1
))
674 hashcpy(result
.sha
, a
->sha1
);
675 else if (S_ISREG(a
->mode
)) {
676 mmfile_t orig
, src1
, src2
;
677 mmbuffer_t result_buf
;
682 name1
= xstrdup(mkpath("%s:%s", branch1
, a
->path
));
683 name2
= xstrdup(mkpath("%s:%s", branch2
, b
->path
));
685 fill_mm(o
->sha1
, &orig
);
686 fill_mm(a
->sha1
, &src1
);
687 fill_mm(b
->sha1
, &src2
);
689 memset(&xpp
, 0, sizeof(xpp
));
690 merge_status
= xdl_merge(&orig
,
693 &xpp
, XDL_MERGE_ZEALOUS
,
701 if ((merge_status
< 0) || !result_buf
.ptr
)
702 die("Failed to execute internal merge");
704 if (write_sha1_file(result_buf
.ptr
, result_buf
.size
,
705 blob_type
, result
.sha
))
706 die("Unable to add %s to database",
709 free(result_buf
.ptr
);
710 result
.clean
= (merge_status
== 0);
712 if (!(S_ISLNK(a
->mode
) || S_ISLNK(b
->mode
)))
713 die("cannot merge modes?");
715 hashcpy(result
.sha
, a
->sha1
);
717 if (!sha_eq(a
->sha1
, b
->sha1
))
725 static void conflict_rename_rename(struct rename
*ren1
,
732 const char *ren1_dst
= ren1
->pair
->two
->path
;
733 const char *ren2_dst
= ren2
->pair
->two
->path
;
734 const char *dst_name1
= ren1_dst
;
735 const char *dst_name2
= ren2_dst
;
736 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
737 dst_name1
= del
[delp
++] = unique_path(ren1_dst
, branch1
);
738 output(1, "%s is a directory in %s added as %s instead",
739 ren1_dst
, branch2
, dst_name1
);
740 remove_file(0, ren1_dst
, 0);
742 if (path_list_has_path(¤t_directory_set
, ren2_dst
)) {
743 dst_name2
= del
[delp
++] = unique_path(ren2_dst
, branch2
);
744 output(1, "%s is a directory in %s added as %s instead",
745 ren2_dst
, branch1
, dst_name2
);
746 remove_file(0, ren2_dst
, 0);
749 remove_file_from_cache(dst_name1
);
750 remove_file_from_cache(dst_name2
);
752 * Uncomment to leave the conflicting names in the resulting tree
754 * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
755 * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
758 update_stages(dst_name1
, NULL
, ren1
->pair
->two
, NULL
, 1);
759 update_stages(dst_name2
, NULL
, NULL
, ren2
->pair
->two
, 1);
765 static void conflict_rename_dir(struct rename
*ren1
,
768 char *new_path
= unique_path(ren1
->pair
->two
->path
, branch1
);
769 output(1, "Renamed %s to %s instead", ren1
->pair
->one
->path
, new_path
);
770 remove_file(0, ren1
->pair
->two
->path
, 0);
771 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path
);
775 static void conflict_rename_rename_2(struct rename
*ren1
,
780 char *new_path1
= unique_path(ren1
->pair
->two
->path
, branch1
);
781 char *new_path2
= unique_path(ren2
->pair
->two
->path
, branch2
);
782 output(1, "Renamed %s to %s and %s to %s instead",
783 ren1
->pair
->one
->path
, new_path1
,
784 ren2
->pair
->one
->path
, new_path2
);
785 remove_file(0, ren1
->pair
->two
->path
, 0);
786 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path1
);
787 update_file(0, ren2
->pair
->two
->sha1
, ren2
->pair
->two
->mode
, new_path2
);
792 static int process_renames(struct path_list
*a_renames
,
793 struct path_list
*b_renames
,
794 const char *a_branch
,
795 const char *b_branch
)
797 int clean_merge
= 1, i
, j
;
798 struct path_list a_by_dst
= {NULL
, 0, 0, 0}, b_by_dst
= {NULL
, 0, 0, 0};
799 const struct rename
*sre
;
801 for (i
= 0; i
< a_renames
->nr
; i
++) {
802 sre
= a_renames
->items
[i
].util
;
803 path_list_insert(sre
->pair
->two
->path
, &a_by_dst
)->util
806 for (i
= 0; i
< b_renames
->nr
; i
++) {
807 sre
= b_renames
->items
[i
].util
;
808 path_list_insert(sre
->pair
->two
->path
, &b_by_dst
)->util
812 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
815 struct path_list
*renames1
, *renames2
, *renames2Dst
;
816 struct rename
*ren1
= NULL
, *ren2
= NULL
;
817 const char *branch1
, *branch2
;
818 const char *ren1_src
, *ren1_dst
;
820 if (i
>= a_renames
->nr
) {
822 ren2
= b_renames
->items
[j
++].util
;
823 } else if (j
>= b_renames
->nr
) {
825 ren1
= a_renames
->items
[i
++].util
;
827 compare
= strcmp(a_renames
->items
[i
].path
,
828 b_renames
->items
[j
].path
);
830 ren1
= a_renames
->items
[i
++].util
;
832 ren2
= b_renames
->items
[j
++].util
;
835 /* TODO: refactor, so that 1/2 are not needed */
837 renames1
= a_renames
;
838 renames2
= b_renames
;
839 renames2Dst
= &b_by_dst
;
844 renames1
= b_renames
;
845 renames2
= a_renames
;
846 renames2Dst
= &a_by_dst
;
853 src
= ren1
->pair
->one
->path
;
855 ren1
->dst_entry
->processed
= 1;
856 ren1
->src_entry
->processed
= 1;
862 ren1_src
= ren1
->pair
->one
->path
;
863 ren1_dst
= ren1
->pair
->two
->path
;
866 const char *ren2_src
= ren2
->pair
->one
->path
;
867 const char *ren2_dst
= ren2
->pair
->two
->path
;
868 /* Renamed in 1 and renamed in 2 */
869 if (strcmp(ren1_src
, ren2_src
) != 0)
870 die("ren1.src != ren2.src");
871 ren2
->dst_entry
->processed
= 1;
873 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
875 output(1, "CONFLICT (rename/rename): "
876 "Rename \"%s\"->\"%s\" in branch \"%s\" "
877 "rename \"%s\"->\"%s\" in \"%s\"%s",
878 src
, ren1_dst
, branch1
,
879 src
, ren2_dst
, branch2
,
880 index_only
? " (left unresolved)": "");
882 remove_file_from_cache(src
);
883 update_file(0, ren1
->pair
->one
->sha1
,
884 ren1
->pair
->one
->mode
, src
);
886 conflict_rename_rename(ren1
, branch1
, ren2
, branch2
);
888 struct merge_file_info mfi
;
889 remove_file(1, ren1_src
, 1);
890 mfi
= merge_file(ren1
->pair
->one
,
895 if (mfi
.merge
|| !mfi
.clean
)
896 output(1, "Renamed %s->%s", src
, ren1_dst
);
899 output(2, "Auto-merged %s", ren1_dst
);
902 output(1, "CONFLICT (content): merge conflict in %s",
907 update_stages(ren1_dst
,
913 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
916 /* Renamed in 1, maybe changed in 2 */
917 struct path_list_item
*item
;
918 /* we only use sha1 and mode of these */
919 struct diff_filespec src_other
, dst_other
;
920 int try_merge
, stage
= a_renames
== renames1
? 3: 2;
922 remove_file(1, ren1_src
, index_only
|| stage
== 3);
924 hashcpy(src_other
.sha1
, ren1
->src_entry
->stages
[stage
].sha
);
925 src_other
.mode
= ren1
->src_entry
->stages
[stage
].mode
;
926 hashcpy(dst_other
.sha1
, ren1
->dst_entry
->stages
[stage
].sha
);
927 dst_other
.mode
= ren1
->dst_entry
->stages
[stage
].mode
;
931 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
933 output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s "
934 " directory %s added in %s",
935 ren1_src
, ren1_dst
, branch1
,
937 conflict_rename_dir(ren1
, branch1
);
938 } else if (sha_eq(src_other
.sha1
, null_sha1
)) {
940 output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s "
942 ren1_src
, ren1_dst
, branch1
,
944 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, ren1_dst
);
945 } else if (!sha_eq(dst_other
.sha1
, null_sha1
)) {
946 const char *new_path
;
949 output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. "
951 ren1_src
, ren1_dst
, branch1
,
953 new_path
= unique_path(ren1_dst
, branch2
);
954 output(1, "Added as %s instead", new_path
);
955 update_file(0, dst_other
.sha1
, dst_other
.mode
, new_path
);
956 } else if ((item
= path_list_lookup(ren1_dst
, renames2Dst
))) {
960 output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. "
961 "Renamed %s->%s in %s",
962 ren1_src
, ren1_dst
, branch1
,
963 ren2
->pair
->one
->path
, ren2
->pair
->two
->path
, branch2
);
964 conflict_rename_rename_2(ren1
, branch1
, ren2
, branch2
);
969 struct diff_filespec
*o
, *a
, *b
;
970 struct merge_file_info mfi
;
971 src_other
.path
= (char *)ren1_src
;
974 if (a_renames
== renames1
) {
981 mfi
= merge_file(o
, a
, b
,
984 if (mfi
.merge
|| !mfi
.clean
)
985 output(1, "Renamed %s => %s", ren1_src
, ren1_dst
);
987 output(2, "Auto-merged %s", ren1_dst
);
989 output(1, "CONFLICT (rename/modify): Merge conflict in %s",
994 update_stages(ren1_dst
,
997 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
1001 path_list_clear(&a_by_dst
, 0);
1002 path_list_clear(&b_by_dst
, 0);
1007 static unsigned char *stage_sha(const unsigned char *sha
, unsigned mode
)
1009 return (is_null_sha1(sha
) || mode
== 0) ? NULL
: (unsigned char *)sha
;
1012 /* Per entry merge function */
1013 static int process_entry(const char *path
, struct stage_data
*entry
,
1014 const char *branch1
,
1015 const char *branch2
)
1018 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1019 print_index_entry("\tpath: ", entry);
1021 int clean_merge
= 1;
1022 unsigned o_mode
= entry
->stages
[1].mode
;
1023 unsigned a_mode
= entry
->stages
[2].mode
;
1024 unsigned b_mode
= entry
->stages
[3].mode
;
1025 unsigned char *o_sha
= stage_sha(entry
->stages
[1].sha
, o_mode
);
1026 unsigned char *a_sha
= stage_sha(entry
->stages
[2].sha
, a_mode
);
1027 unsigned char *b_sha
= stage_sha(entry
->stages
[3].sha
, b_mode
);
1029 if (o_sha
&& (!a_sha
|| !b_sha
)) {
1030 /* Case A: Deleted in one */
1031 if ((!a_sha
&& !b_sha
) ||
1032 (sha_eq(a_sha
, o_sha
) && !b_sha
) ||
1033 (!a_sha
&& sha_eq(b_sha
, o_sha
))) {
1034 /* Deleted in both or deleted in one and
1035 * unchanged in the other */
1037 output(2, "Removed %s", path
);
1038 /* do not touch working file if it did not exist */
1039 remove_file(1, path
, !a_sha
);
1041 /* Deleted in one and changed in the other */
1044 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1045 "and modified in %s. Version %s of %s left in tree.",
1047 branch2
, branch2
, path
);
1048 update_file(0, b_sha
, b_mode
, path
);
1050 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1051 "and modified in %s. Version %s of %s left in tree.",
1053 branch1
, branch1
, path
);
1054 update_file(0, a_sha
, a_mode
, path
);
1058 } else if ((!o_sha
&& a_sha
&& !b_sha
) ||
1059 (!o_sha
&& !a_sha
&& b_sha
)) {
1060 /* Case B: Added in one. */
1061 const char *add_branch
;
1062 const char *other_branch
;
1064 const unsigned char *sha
;
1068 add_branch
= branch1
;
1069 other_branch
= branch2
;
1072 conf
= "file/directory";
1074 add_branch
= branch2
;
1075 other_branch
= branch1
;
1078 conf
= "directory/file";
1080 if (path_list_has_path(¤t_directory_set
, path
)) {
1081 const char *new_path
= unique_path(path
, add_branch
);
1083 output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
1085 conf
, path
, other_branch
, path
, new_path
);
1086 remove_file(0, path
, 0);
1087 update_file(0, sha
, mode
, new_path
);
1089 output(2, "Added %s", path
);
1090 update_file(1, sha
, mode
, path
);
1092 } else if (a_sha
&& b_sha
) {
1093 /* Case C: Added in both (check for same permissions) and */
1094 /* case D: Modified in both, but differently. */
1095 const char *reason
= "content";
1096 struct merge_file_info mfi
;
1097 struct diff_filespec o
, a
, b
;
1101 o_sha
= (unsigned char *)null_sha1
;
1103 output(2, "Auto-merged %s", path
);
1104 o
.path
= a
.path
= b
.path
= (char *)path
;
1105 hashcpy(o
.sha1
, o_sha
);
1107 hashcpy(a
.sha1
, a_sha
);
1109 hashcpy(b
.sha1
, b_sha
);
1112 mfi
= merge_file(&o
, &a
, &b
,
1116 update_file(1, mfi
.sha
, mfi
.mode
, path
);
1119 output(1, "CONFLICT (%s): Merge conflict in %s",
1123 update_file(0, mfi
.sha
, mfi
.mode
, path
);
1125 update_file_flags(mfi
.sha
, mfi
.mode
, path
,
1126 0 /* update_cache */, 1 /* update_working_directory */);
1128 } else if (!o_sha
&& !a_sha
&& !b_sha
) {
1130 * this entry was deleted altogether. a_mode == 0 means
1131 * we had that path and want to actively remove it.
1133 remove_file(1, path
, !a_mode
);
1135 die("Fatal merge failure, shouldn't happen.");
1140 static int merge_trees(struct tree
*head
,
1142 struct tree
*common
,
1143 const char *branch1
,
1144 const char *branch2
,
1145 struct tree
**result
)
1149 if (subtree_merge
) {
1150 merge
= shift_tree_object(head
, merge
);
1151 common
= shift_tree_object(head
, common
);
1154 if (sha_eq(common
->object
.sha1
, merge
->object
.sha1
)) {
1155 output(0, "Already uptodate!");
1160 code
= git_merge_trees(index_only
, common
, head
, merge
);
1163 die("merging of trees %s and %s failed",
1164 sha1_to_hex(head
->object
.sha1
),
1165 sha1_to_hex(merge
->object
.sha1
));
1167 if (unmerged_index()) {
1168 struct path_list
*entries
, *re_head
, *re_merge
;
1170 path_list_clear(¤t_file_set
, 1);
1171 path_list_clear(¤t_directory_set
, 1);
1172 get_files_dirs(head
);
1173 get_files_dirs(merge
);
1175 entries
= get_unmerged();
1176 re_head
= get_renames(head
, common
, head
, merge
, entries
);
1177 re_merge
= get_renames(merge
, common
, head
, merge
, entries
);
1178 clean
= process_renames(re_head
, re_merge
,
1180 for (i
= 0; i
< entries
->nr
; i
++) {
1181 const char *path
= entries
->items
[i
].path
;
1182 struct stage_data
*e
= entries
->items
[i
].util
;
1184 && !process_entry(path
, e
, branch1
, branch2
))
1188 path_list_clear(re_merge
, 0);
1189 path_list_clear(re_head
, 0);
1190 path_list_clear(entries
, 1);
1197 *result
= git_write_tree();
1202 static struct commit_list
*reverse_commit_list(struct commit_list
*list
)
1204 struct commit_list
*next
= NULL
, *current
, *backup
;
1205 for (current
= list
; current
; current
= backup
) {
1206 backup
= current
->next
;
1207 current
->next
= next
;
1214 * Merge the commits h1 and h2, return the resulting virtual
1215 * commit object and a flag indicating the cleanness of the merge.
1217 static int merge(struct commit
*h1
,
1219 const char *branch1
,
1220 const char *branch2
,
1221 struct commit_list
*ca
,
1222 struct commit
**result
)
1224 struct commit_list
*iter
;
1225 struct commit
*merged_common_ancestors
;
1226 struct tree
*mrtree
;
1230 output(4, "Merging:");
1231 output_commit_title(h1
);
1232 output_commit_title(h2
);
1236 ca
= get_merge_bases(h1
, h2
, 1);
1237 ca
= reverse_commit_list(ca
);
1241 output(5, "found %u common ancestor(s):", commit_list_count(ca
));
1242 for (iter
= ca
; iter
; iter
= iter
->next
)
1243 output_commit_title(iter
->item
);
1246 merged_common_ancestors
= pop_commit(&ca
);
1247 if (merged_common_ancestors
== NULL
) {
1248 /* if there is no common ancestor, make an empty tree */
1249 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
1251 tree
->object
.parsed
= 1;
1252 tree
->object
.type
= OBJ_TREE
;
1253 pretend_sha1_file(NULL
, 0, OBJ_TREE
, tree
->object
.sha1
);
1254 merged_common_ancestors
= make_virtual_commit(tree
, "ancestor");
1257 for (iter
= ca
; iter
; iter
= iter
->next
) {
1260 * When the merge fails, the result contains files
1261 * with conflict markers. The cleanness flag is
1262 * ignored, it was never actually used, as result of
1263 * merge_trees has always overwritten it: the committed
1264 * "conflicts" were already resolved.
1267 merge(merged_common_ancestors
, iter
->item
,
1268 "Temporary merge branch 1",
1269 "Temporary merge branch 2",
1271 &merged_common_ancestors
);
1274 if (!merged_common_ancestors
)
1275 die("merge returned no commit");
1285 clean
= merge_trees(h1
->tree
, h2
->tree
, merged_common_ancestors
->tree
,
1286 branch1
, branch2
, &mrtree
);
1289 *result
= make_virtual_commit(mrtree
, "merged tree");
1290 commit_list_insert(h1
, &(*result
)->parents
);
1291 commit_list_insert(h2
, &(*result
)->parents
->next
);
1297 static const char *better_branch_name(const char *branch
)
1299 static char githead_env
[8 + 40 + 1];
1302 if (strlen(branch
) != 40)
1304 sprintf(githead_env
, "GITHEAD_%s", branch
);
1305 name
= getenv(githead_env
);
1306 return name
? name
: branch
;
1309 static struct commit
*get_ref(const char *ref
)
1311 unsigned char sha1
[20];
1312 struct object
*object
;
1314 if (get_sha1(ref
, sha1
))
1315 die("Could not resolve ref '%s'", ref
);
1316 object
= deref_tag(parse_object(sha1
), ref
, strlen(ref
));
1317 if (object
->type
== OBJ_TREE
)
1318 return make_virtual_commit((struct tree
*)object
,
1319 better_branch_name(ref
));
1320 if (object
->type
!= OBJ_COMMIT
)
1322 if (parse_commit((struct commit
*)object
))
1323 die("Could not parse commit '%s'", sha1_to_hex(object
->sha1
));
1324 return (struct commit
*)object
;
1327 static int merge_config(const char *var
, const char *value
)
1329 if (!strcasecmp(var
, "merge.verbosity")) {
1330 verbosity
= git_config_int(var
, value
);
1333 return git_default_config(var
, value
);
1336 int main(int argc
, char *argv
[])
1338 static const char *bases
[20];
1339 static unsigned bases_count
= 0;
1341 const char *branch1
, *branch2
;
1342 struct commit
*result
, *h1
, *h2
;
1343 struct commit_list
*ca
= NULL
;
1344 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
1348 int namelen
= strlen(argv
[0]);
1350 !strcmp(argv
[0] + namelen
- 8, "-subtree"))
1354 git_config(merge_config
);
1355 if (getenv("GIT_MERGE_VERBOSITY"))
1356 verbosity
= strtol(getenv("GIT_MERGE_VERBOSITY"), NULL
, 10);
1359 die("Usage: %s <base>... -- <head> <remote> ...\n", argv
[0]);
1361 for (i
= 1; i
< argc
; ++i
) {
1362 if (!strcmp(argv
[i
], "--"))
1364 if (bases_count
< sizeof(bases
)/sizeof(*bases
))
1365 bases
[bases_count
++] = argv
[i
];
1367 if (argc
- i
!= 3) /* "--" "<head>" "<remote>" */
1368 die("Not handling anything other than two heads merge.");
1372 branch1
= argv
[++i
];
1373 branch2
= argv
[++i
];
1375 h1
= get_ref(branch1
);
1376 h2
= get_ref(branch2
);
1378 branch1
= better_branch_name(branch1
);
1379 branch2
= better_branch_name(branch2
);
1382 printf("Merging %s with %s\n", branch1
, branch2
);
1384 index_fd
= hold_locked_index(lock
, 1);
1386 for (i
= 0; i
< bases_count
; i
++) {
1387 struct commit
*ancestor
= get_ref(bases
[i
]);
1388 ca
= commit_list_insert(ancestor
, &ca
);
1390 clean
= merge(h1
, h2
, branch1
, branch2
, ca
, &result
);
1392 if (active_cache_changed
&&
1393 (write_cache(index_fd
, active_cache
, active_nr
) ||
1394 close(index_fd
) || commit_locked_index(lock
)))
1395 die ("unable to write %s", get_index_file());
1397 return clean
? 0: 1;