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];
70 static struct path_list current_file_set
= {NULL
, 0, 0, 1};
71 static struct path_list current_directory_set
= {NULL
, 0, 0, 1};
73 static int call_depth
= 0;
74 static int verbosity
= 2;
76 static int show (int v
)
78 return (!call_depth
&& verbosity
>= v
) || verbosity
>= 5;
81 static void output(int v
, const char *fmt
, ...)
87 for (i
= call_depth
; i
--;)
89 vfprintf(stdout
, fmt
, args
);
95 static void output_commit_title(struct commit
*commit
)
98 for (i
= call_depth
; i
--;)
101 printf("virtual %s\n", (char *)commit
->util
);
103 printf("%s ", find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
));
104 if (parse_commit(commit
) != 0)
105 printf("(bad commit)\n");
109 for (s
= commit
->buffer
; *s
; s
++)
110 if (*s
== '\n' && s
[1] == '\n') {
114 for (len
= 0; s
[len
] && '\n' != s
[len
]; len
++)
116 printf("%.*s\n", len
, s
);
121 static struct cache_entry
*make_cache_entry(unsigned int mode
,
122 const unsigned char *sha1
, const char *path
, int stage
, int refresh
)
125 struct cache_entry
*ce
;
127 if (!verify_path(path
))
131 size
= cache_entry_size(len
);
132 ce
= xcalloc(1, size
);
134 hashcpy(ce
->sha1
, sha1
);
135 memcpy(ce
->name
, path
, len
);
136 ce
->ce_flags
= create_ce_flags(len
, stage
);
137 ce
->ce_mode
= create_ce_mode(mode
);
140 return refresh_cache_entry(ce
, 0);
145 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
146 const char *path
, int stage
, int refresh
, int options
)
148 struct cache_entry
*ce
;
149 ce
= make_cache_entry(mode
, sha1
? sha1
: null_sha1
, path
, stage
, refresh
);
151 return error("cache_addinfo failed: %s", strerror(cache_errno
));
152 return add_cache_entry(ce
, options
);
156 * This is a global variable which is used in a number of places but
157 * only written to in the 'merge' function.
159 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
160 * don't update the working directory.
161 * 0 => Leave unmerged entries in the cache and update
162 * the working directory.
164 static int index_only
= 0;
166 static int git_merge_trees(int index_only
,
172 struct object_list
*trees
= NULL
;
173 struct unpack_trees_options opts
;
175 memset(&opts
, 0, sizeof(opts
));
182 opts
.fn
= threeway_merge
;
184 object_list_append(&common
->object
, &trees
);
185 object_list_append(&head
->object
, &trees
);
186 object_list_append(&merge
->object
, &trees
);
188 rc
= unpack_trees(trees
, &opts
);
189 cache_tree_free(&active_cache_tree
);
193 static int unmerged_index(void)
196 for (i
= 0; i
< active_nr
; i
++) {
197 struct cache_entry
*ce
= active_cache
[i
];
204 static struct tree
*git_write_tree(void)
206 struct tree
*result
= NULL
;
208 if (unmerged_index())
211 if (!active_cache_tree
)
212 active_cache_tree
= cache_tree();
214 if (!cache_tree_fully_valid(active_cache_tree
) &&
215 cache_tree_update(active_cache_tree
,
216 active_cache
, active_nr
, 0, 0) < 0)
217 die("error building trees");
219 result
= lookup_tree(active_cache_tree
->sha1
);
224 static int save_files_dirs(const unsigned char *sha1
,
225 const char *base
, int baselen
, const char *path
,
226 unsigned int mode
, int stage
)
228 int len
= strlen(path
);
229 char *newpath
= xmalloc(baselen
+ len
+ 1);
230 memcpy(newpath
, base
, baselen
);
231 memcpy(newpath
+ baselen
, path
, len
);
232 newpath
[baselen
+ len
] = '\0';
235 path_list_insert(newpath
, ¤t_directory_set
);
237 path_list_insert(newpath
, ¤t_file_set
);
240 return READ_TREE_RECURSIVE
;
243 static int get_files_dirs(struct tree
*tree
)
246 if (read_tree_recursive(tree
, "", 0, 0, NULL
, save_files_dirs
) != 0)
248 n
= current_file_set
.nr
+ current_directory_set
.nr
;
253 * Returns a index_entry instance which doesn't have to correspond to
254 * a real cache entry in Git's index.
256 static struct stage_data
*insert_stage_data(const char *path
,
257 struct tree
*o
, struct tree
*a
, struct tree
*b
,
258 struct path_list
*entries
)
260 struct path_list_item
*item
;
261 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
262 get_tree_entry(o
->object
.sha1
, path
,
263 e
->stages
[1].sha
, &e
->stages
[1].mode
);
264 get_tree_entry(a
->object
.sha1
, path
,
265 e
->stages
[2].sha
, &e
->stages
[2].mode
);
266 get_tree_entry(b
->object
.sha1
, path
,
267 e
->stages
[3].sha
, &e
->stages
[3].mode
);
268 item
= path_list_insert(path
, entries
);
274 * Create a dictionary mapping file names to stage_data objects. The
275 * dictionary contains one entry for every path with a non-zero stage entry.
277 static struct path_list
*get_unmerged(void)
279 struct path_list
*unmerged
= xcalloc(1, sizeof(struct path_list
));
282 unmerged
->strdup_paths
= 1;
284 for (i
= 0; i
< active_nr
; i
++) {
285 struct path_list_item
*item
;
286 struct stage_data
*e
;
287 struct cache_entry
*ce
= active_cache
[i
];
291 item
= path_list_lookup(ce
->name
, unmerged
);
293 item
= path_list_insert(ce
->name
, unmerged
);
294 item
->util
= xcalloc(1, sizeof(struct stage_data
));
297 e
->stages
[ce_stage(ce
)].mode
= ntohl(ce
->ce_mode
);
298 hashcpy(e
->stages
[ce_stage(ce
)].sha
, ce
->sha1
);
306 struct diff_filepair
*pair
;
307 struct stage_data
*src_entry
;
308 struct stage_data
*dst_entry
;
309 unsigned processed
:1;
313 * Get information of all renames which occured between 'o_tree' and
314 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
315 * 'b_tree') to be able to associate the correct cache entries with
316 * the rename information. 'tree' is always equal to either a_tree or b_tree.
318 static struct path_list
*get_renames(struct tree
*tree
,
322 struct path_list
*entries
)
325 struct path_list
*renames
;
326 struct diff_options opts
;
328 renames
= xcalloc(1, sizeof(struct path_list
));
331 opts
.detect_rename
= DIFF_DETECT_RENAME
;
332 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
333 if (diff_setup_done(&opts
) < 0)
334 die("diff setup failed");
335 diff_tree_sha1(o_tree
->object
.sha1
, tree
->object
.sha1
, "", &opts
);
337 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
338 struct path_list_item
*item
;
340 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
341 if (pair
->status
!= 'R') {
342 diff_free_filepair(pair
);
345 re
= xmalloc(sizeof(*re
));
348 item
= path_list_lookup(re
->pair
->one
->path
, entries
);
350 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
351 o_tree
, a_tree
, b_tree
, entries
);
353 re
->src_entry
= item
->util
;
355 item
= path_list_lookup(re
->pair
->two
->path
, entries
);
357 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
358 o_tree
, a_tree
, b_tree
, entries
);
360 re
->dst_entry
= item
->util
;
361 item
= path_list_insert(pair
->one
->path
, renames
);
364 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
365 diff_queued_diff
.nr
= 0;
370 static int update_stages(const char *path
, struct diff_filespec
*o
,
371 struct diff_filespec
*a
, struct diff_filespec
*b
,
374 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
376 if (remove_file_from_cache(path
))
379 if (add_cacheinfo(o
->mode
, o
->sha1
, path
, 1, 0, options
))
382 if (add_cacheinfo(a
->mode
, a
->sha1
, path
, 2, 0, options
))
385 if (add_cacheinfo(b
->mode
, b
->sha1
, path
, 3, 0, options
))
390 static int remove_path(const char *name
)
399 dirs
= xmalloc(len
+1);
400 memcpy(dirs
, name
, len
);
402 while ((slash
= strrchr(name
, '/'))) {
405 if (rmdir(name
) != 0)
412 static int remove_file(int clean
, const char *path
, int no_wd
)
414 int update_cache
= index_only
|| clean
;
415 int update_working_directory
= !index_only
&& !no_wd
;
418 if (remove_file_from_cache(path
))
421 if (update_working_directory
) {
423 if (errno
!= ENOENT
|| errno
!= EISDIR
)
430 static char *unique_path(const char *path
, const char *branch
)
432 char *newpath
= xmalloc(strlen(path
) + 1 + strlen(branch
) + 8 + 1);
435 char *p
= newpath
+ strlen(path
);
436 strcpy(newpath
, path
);
442 while (path_list_has_path(¤t_file_set
, newpath
) ||
443 path_list_has_path(¤t_directory_set
, newpath
) ||
444 lstat(newpath
, &st
) == 0)
445 sprintf(p
, "_%d", suffix
++);
447 path_list_insert(newpath
, ¤t_file_set
);
451 static int mkdir_p(const char *path
, unsigned long mode
)
453 /* path points to cache entries, so xstrdup before messing with it */
454 char *buf
= xstrdup(path
);
455 int result
= safe_create_leading_directories(buf
);
460 static void flush_buffer(int fd
, const char *buf
, unsigned long size
)
463 long ret
= write_in_full(fd
, buf
, size
);
468 die("merge-recursive: %s", strerror(errno
));
470 die("merge-recursive: disk full?");
477 static void update_file_flags(const unsigned char *sha
,
491 buf
= read_sha1_file(sha
, type
, &size
);
493 die("cannot read object %s '%s'", sha1_to_hex(sha
), path
);
494 if (strcmp(type
, blob_type
) != 0)
495 die("blob expected for %s '%s'", sha1_to_hex(sha
), path
);
499 if (mkdir_p(path
, 0777))
500 die("failed to create path %s: %s", path
, strerror(errno
));
506 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
508 die("failed to open %s: %s", path
, strerror(errno
));
509 flush_buffer(fd
, buf
, size
);
511 } else if (S_ISLNK(mode
)) {
512 char *lnk
= xmalloc(size
+ 1);
513 memcpy(lnk
, buf
, size
);
519 die("do not know what to do with %06o %s '%s'",
520 mode
, sha1_to_hex(sha
), path
);
523 add_cacheinfo(mode
, sha
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
526 static void update_file(int clean
,
527 const unsigned char *sha
,
531 update_file_flags(sha
, mode
, path
, index_only
|| clean
, !index_only
);
534 /* Low level file merging, update and removal */
536 struct merge_file_info
538 unsigned char sha
[20];
544 static void fill_mm(const unsigned char *sha1
, mmfile_t
*mm
)
549 if (!hashcmp(sha1
, null_sha1
)) {
550 mm
->ptr
= xstrdup("");
555 mm
->ptr
= read_sha1_file(sha1
, type
, &size
);
556 if (!mm
->ptr
|| strcmp(type
, blob_type
))
557 die("unable to read blob object %s", sha1_to_hex(sha1
));
561 static struct merge_file_info
merge_file(struct diff_filespec
*o
,
562 struct diff_filespec
*a
, struct diff_filespec
*b
,
563 const char *branch1
, const char *branch2
)
565 struct merge_file_info result
;
569 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
571 if (S_ISREG(a
->mode
)) {
572 result
.mode
= a
->mode
;
573 hashcpy(result
.sha
, a
->sha1
);
575 result
.mode
= b
->mode
;
576 hashcpy(result
.sha
, b
->sha1
);
579 if (!sha_eq(a
->sha1
, o
->sha1
) && !sha_eq(b
->sha1
, o
->sha1
))
582 result
.mode
= a
->mode
== o
->mode
? b
->mode
: a
->mode
;
584 if (sha_eq(a
->sha1
, o
->sha1
))
585 hashcpy(result
.sha
, b
->sha1
);
586 else if (sha_eq(b
->sha1
, o
->sha1
))
587 hashcpy(result
.sha
, a
->sha1
);
588 else if (S_ISREG(a
->mode
)) {
589 mmfile_t orig
, src1
, src2
;
590 mmbuffer_t result_buf
;
595 name1
= xstrdup(mkpath("%s:%s", branch1
, a
->path
));
596 name2
= xstrdup(mkpath("%s:%s", branch2
, b
->path
));
598 fill_mm(o
->sha1
, &orig
);
599 fill_mm(a
->sha1
, &src1
);
600 fill_mm(b
->sha1
, &src2
);
602 memset(&xpp
, 0, sizeof(xpp
));
603 merge_status
= xdl_merge(&orig
,
606 &xpp
, XDL_MERGE_ZEALOUS
,
614 if ((merge_status
< 0) || !result_buf
.ptr
)
615 die("Failed to execute internal merge");
617 if (write_sha1_file(result_buf
.ptr
, result_buf
.size
,
618 blob_type
, result
.sha
))
619 die("Unable to add %s to database",
622 free(result_buf
.ptr
);
623 result
.clean
= (merge_status
== 0);
625 if (!(S_ISLNK(a
->mode
) || S_ISLNK(b
->mode
)))
626 die("cannot merge modes?");
628 hashcpy(result
.sha
, a
->sha1
);
630 if (!sha_eq(a
->sha1
, b
->sha1
))
638 static void conflict_rename_rename(struct rename
*ren1
,
645 const char *ren1_dst
= ren1
->pair
->two
->path
;
646 const char *ren2_dst
= ren2
->pair
->two
->path
;
647 const char *dst_name1
= ren1_dst
;
648 const char *dst_name2
= ren2_dst
;
649 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
650 dst_name1
= del
[delp
++] = unique_path(ren1_dst
, branch1
);
651 output(1, "%s is a directory in %s adding as %s instead",
652 ren1_dst
, branch2
, dst_name1
);
653 remove_file(0, ren1_dst
, 0);
655 if (path_list_has_path(¤t_directory_set
, ren2_dst
)) {
656 dst_name2
= del
[delp
++] = unique_path(ren2_dst
, branch2
);
657 output(1, "%s is a directory in %s adding as %s instead",
658 ren2_dst
, branch1
, dst_name2
);
659 remove_file(0, ren2_dst
, 0);
661 update_stages(dst_name1
, NULL
, ren1
->pair
->two
, NULL
, 1);
662 update_stages(dst_name2
, NULL
, NULL
, ren2
->pair
->two
, 1);
667 static void conflict_rename_dir(struct rename
*ren1
,
670 char *new_path
= unique_path(ren1
->pair
->two
->path
, branch1
);
671 output(1, "Renaming %s to %s instead", ren1
->pair
->one
->path
, new_path
);
672 remove_file(0, ren1
->pair
->two
->path
, 0);
673 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path
);
677 static void conflict_rename_rename_2(struct rename
*ren1
,
682 char *new_path1
= unique_path(ren1
->pair
->two
->path
, branch1
);
683 char *new_path2
= unique_path(ren2
->pair
->two
->path
, branch2
);
684 output(1, "Renaming %s to %s and %s to %s instead",
685 ren1
->pair
->one
->path
, new_path1
,
686 ren2
->pair
->one
->path
, new_path2
);
687 remove_file(0, ren1
->pair
->two
->path
, 0);
688 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path1
);
689 update_file(0, ren2
->pair
->two
->sha1
, ren2
->pair
->two
->mode
, new_path2
);
694 static int process_renames(struct path_list
*a_renames
,
695 struct path_list
*b_renames
,
696 const char *a_branch
,
697 const char *b_branch
)
699 int clean_merge
= 1, i
, j
;
700 struct path_list a_by_dst
= {NULL
, 0, 0, 0}, b_by_dst
= {NULL
, 0, 0, 0};
701 const struct rename
*sre
;
703 for (i
= 0; i
< a_renames
->nr
; i
++) {
704 sre
= a_renames
->items
[i
].util
;
705 path_list_insert(sre
->pair
->two
->path
, &a_by_dst
)->util
708 for (i
= 0; i
< b_renames
->nr
; i
++) {
709 sre
= b_renames
->items
[i
].util
;
710 path_list_insert(sre
->pair
->two
->path
, &b_by_dst
)->util
714 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
717 struct path_list
*renames1
, *renames2
, *renames2Dst
;
718 struct rename
*ren1
= NULL
, *ren2
= NULL
;
719 const char *branch1
, *branch2
;
720 const char *ren1_src
, *ren1_dst
;
722 if (i
>= a_renames
->nr
) {
724 ren2
= b_renames
->items
[j
++].util
;
725 } else if (j
>= b_renames
->nr
) {
727 ren1
= a_renames
->items
[i
++].util
;
729 compare
= strcmp(a_renames
->items
[i
].path
,
730 b_renames
->items
[j
].path
);
732 ren1
= a_renames
->items
[i
++].util
;
734 ren2
= b_renames
->items
[j
++].util
;
737 /* TODO: refactor, so that 1/2 are not needed */
739 renames1
= a_renames
;
740 renames2
= b_renames
;
741 renames2Dst
= &b_by_dst
;
746 renames1
= b_renames
;
747 renames2
= a_renames
;
748 renames2Dst
= &a_by_dst
;
755 src
= ren1
->pair
->one
->path
;
757 ren1
->dst_entry
->processed
= 1;
758 ren1
->src_entry
->processed
= 1;
764 ren1_src
= ren1
->pair
->one
->path
;
765 ren1_dst
= ren1
->pair
->two
->path
;
768 const char *ren2_src
= ren2
->pair
->one
->path
;
769 const char *ren2_dst
= ren2
->pair
->two
->path
;
770 /* Renamed in 1 and renamed in 2 */
771 if (strcmp(ren1_src
, ren2_src
) != 0)
772 die("ren1.src != ren2.src");
773 ren2
->dst_entry
->processed
= 1;
775 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
777 output(1, "CONFLICT (rename/rename): "
778 "Rename %s->%s in branch %s "
779 "rename %s->%s in %s",
780 src
, ren1_dst
, branch1
,
781 src
, ren2_dst
, branch2
);
782 conflict_rename_rename(ren1
, branch1
, ren2
, branch2
);
784 struct merge_file_info mfi
;
785 remove_file(1, ren1_src
, 1);
786 mfi
= merge_file(ren1
->pair
->one
,
791 if (mfi
.merge
|| !mfi
.clean
)
792 output(1, "Renaming %s->%s", src
, ren1_dst
);
795 output(2, "Auto-merging %s", ren1_dst
);
798 output(1, "CONFLICT (content): merge conflict in %s",
803 update_stages(ren1_dst
,
809 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
812 /* Renamed in 1, maybe changed in 2 */
813 struct path_list_item
*item
;
814 /* we only use sha1 and mode of these */
815 struct diff_filespec src_other
, dst_other
;
816 int try_merge
, stage
= a_renames
== renames1
? 3: 2;
818 remove_file(1, ren1_src
, index_only
);
820 hashcpy(src_other
.sha1
, ren1
->src_entry
->stages
[stage
].sha
);
821 src_other
.mode
= ren1
->src_entry
->stages
[stage
].mode
;
822 hashcpy(dst_other
.sha1
, ren1
->dst_entry
->stages
[stage
].sha
);
823 dst_other
.mode
= ren1
->dst_entry
->stages
[stage
].mode
;
827 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
829 output(1, "CONFLICT (rename/directory): Rename %s->%s in %s "
830 " directory %s added in %s",
831 ren1_src
, ren1_dst
, branch1
,
833 conflict_rename_dir(ren1
, branch1
);
834 } else if (sha_eq(src_other
.sha1
, null_sha1
)) {
836 output(1, "CONFLICT (rename/delete): Rename %s->%s in %s "
838 ren1_src
, ren1_dst
, branch1
,
840 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, ren1_dst
);
841 } else if (!sha_eq(dst_other
.sha1
, null_sha1
)) {
842 const char *new_path
;
845 output(1, "CONFLICT (rename/add): Rename %s->%s in %s. "
847 ren1_src
, ren1_dst
, branch1
,
849 new_path
= unique_path(ren1_dst
, branch2
);
850 output(1, "Adding as %s instead", new_path
);
851 update_file(0, dst_other
.sha1
, dst_other
.mode
, new_path
);
852 } else if ((item
= path_list_lookup(ren1_dst
, renames2Dst
))) {
856 output(1, "CONFLICT (rename/rename): Rename %s->%s in %s. "
857 "Rename %s->%s in %s",
858 ren1_src
, ren1_dst
, branch1
,
859 ren2
->pair
->one
->path
, ren2
->pair
->two
->path
, branch2
);
860 conflict_rename_rename_2(ren1
, branch1
, ren2
, branch2
);
865 struct diff_filespec
*o
, *a
, *b
;
866 struct merge_file_info mfi
;
867 src_other
.path
= (char *)ren1_src
;
870 if (a_renames
== renames1
) {
877 mfi
= merge_file(o
, a
, b
,
880 if (mfi
.merge
|| !mfi
.clean
)
881 output(1, "Renaming %s => %s", ren1_src
, ren1_dst
);
883 output(2, "Auto-merging %s", ren1_dst
);
885 output(1, "CONFLICT (rename/modify): Merge conflict in %s",
890 update_stages(ren1_dst
,
893 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
897 path_list_clear(&a_by_dst
, 0);
898 path_list_clear(&b_by_dst
, 0);
903 static unsigned char *has_sha(const unsigned char *sha
)
905 return is_null_sha1(sha
) ? NULL
: (unsigned char *)sha
;
908 /* Per entry merge function */
909 static int process_entry(const char *path
, struct stage_data
*entry
,
914 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
915 print_index_entry("\tpath: ", entry);
918 unsigned char *o_sha
= has_sha(entry
->stages
[1].sha
);
919 unsigned char *a_sha
= has_sha(entry
->stages
[2].sha
);
920 unsigned char *b_sha
= has_sha(entry
->stages
[3].sha
);
921 unsigned o_mode
= entry
->stages
[1].mode
;
922 unsigned a_mode
= entry
->stages
[2].mode
;
923 unsigned b_mode
= entry
->stages
[3].mode
;
925 if (o_sha
&& (!a_sha
|| !b_sha
)) {
926 /* Case A: Deleted in one */
927 if ((!a_sha
&& !b_sha
) ||
928 (sha_eq(a_sha
, o_sha
) && !b_sha
) ||
929 (!a_sha
&& sha_eq(b_sha
, o_sha
))) {
930 /* Deleted in both or deleted in one and
931 * unchanged in the other */
933 output(2, "Removing %s", path
);
934 /* do not touch working file if it did not exist */
935 remove_file(1, path
, !a_sha
);
937 /* Deleted in one and changed in the other */
940 output(1, "CONFLICT (delete/modify): %s deleted in %s "
941 "and modified in %s. Version %s of %s left in tree.",
943 branch2
, branch2
, path
);
944 update_file(0, b_sha
, b_mode
, path
);
946 output(1, "CONFLICT (delete/modify): %s deleted in %s "
947 "and modified in %s. Version %s of %s left in tree.",
949 branch1
, branch1
, path
);
950 update_file(0, a_sha
, a_mode
, path
);
954 } else if ((!o_sha
&& a_sha
&& !b_sha
) ||
955 (!o_sha
&& !a_sha
&& b_sha
)) {
956 /* Case B: Added in one. */
957 const char *add_branch
;
958 const char *other_branch
;
960 const unsigned char *sha
;
964 add_branch
= branch1
;
965 other_branch
= branch2
;
968 conf
= "file/directory";
970 add_branch
= branch2
;
971 other_branch
= branch1
;
974 conf
= "directory/file";
976 if (path_list_has_path(¤t_directory_set
, path
)) {
977 const char *new_path
= unique_path(path
, add_branch
);
979 output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
981 conf
, path
, other_branch
, path
, new_path
);
982 remove_file(0, path
, 0);
983 update_file(0, sha
, mode
, new_path
);
985 output(2, "Adding %s", path
);
986 update_file(1, sha
, mode
, path
);
988 } else if (a_sha
&& b_sha
) {
989 /* Case C: Added in both (check for same permissions) and */
990 /* case D: Modified in both, but differently. */
991 const char *reason
= "content";
992 struct merge_file_info mfi
;
993 struct diff_filespec o
, a
, b
;
997 o_sha
= (unsigned char *)null_sha1
;
999 output(2, "Auto-merging %s", path
);
1000 o
.path
= a
.path
= b
.path
= (char *)path
;
1001 hashcpy(o
.sha1
, o_sha
);
1003 hashcpy(a
.sha1
, a_sha
);
1005 hashcpy(b
.sha1
, b_sha
);
1008 mfi
= merge_file(&o
, &a
, &b
,
1012 update_file(1, mfi
.sha
, mfi
.mode
, path
);
1015 output(1, "CONFLICT (%s): Merge conflict in %s",
1019 update_file(0, mfi
.sha
, mfi
.mode
, path
);
1021 update_file_flags(mfi
.sha
, mfi
.mode
, path
,
1022 0 /* update_cache */, 1 /* update_working_directory */);
1025 die("Fatal merge failure, shouldn't happen.");
1030 static int merge_trees(struct tree
*head
,
1032 struct tree
*common
,
1033 const char *branch1
,
1034 const char *branch2
,
1035 struct tree
**result
)
1038 if (sha_eq(common
->object
.sha1
, merge
->object
.sha1
)) {
1039 output(0, "Already uptodate!");
1044 code
= git_merge_trees(index_only
, common
, head
, merge
);
1047 die("merging of trees %s and %s failed",
1048 sha1_to_hex(head
->object
.sha1
),
1049 sha1_to_hex(merge
->object
.sha1
));
1051 if (unmerged_index()) {
1052 struct path_list
*entries
, *re_head
, *re_merge
;
1054 path_list_clear(¤t_file_set
, 1);
1055 path_list_clear(¤t_directory_set
, 1);
1056 get_files_dirs(head
);
1057 get_files_dirs(merge
);
1059 entries
= get_unmerged();
1060 re_head
= get_renames(head
, common
, head
, merge
, entries
);
1061 re_merge
= get_renames(merge
, common
, head
, merge
, entries
);
1062 clean
= process_renames(re_head
, re_merge
,
1064 for (i
= 0; i
< entries
->nr
; i
++) {
1065 const char *path
= entries
->items
[i
].path
;
1066 struct stage_data
*e
= entries
->items
[i
].util
;
1069 if (!process_entry(path
, e
, branch1
, branch2
))
1073 path_list_clear(re_merge
, 0);
1074 path_list_clear(re_head
, 0);
1075 path_list_clear(entries
, 1);
1082 *result
= git_write_tree();
1087 static struct commit_list
*reverse_commit_list(struct commit_list
*list
)
1089 struct commit_list
*next
= NULL
, *current
, *backup
;
1090 for (current
= list
; current
; current
= backup
) {
1091 backup
= current
->next
;
1092 current
->next
= next
;
1099 * Merge the commits h1 and h2, return the resulting virtual
1100 * commit object and a flag indicating the cleaness of the merge.
1102 static int merge(struct commit
*h1
,
1104 const char *branch1
,
1105 const char *branch2
,
1106 struct commit_list
*ca
,
1107 struct commit
**result
)
1109 struct commit_list
*iter
;
1110 struct commit
*merged_common_ancestors
;
1111 struct tree
*mrtree
;
1115 output(4, "Merging:");
1116 output_commit_title(h1
);
1117 output_commit_title(h2
);
1121 ca
= get_merge_bases(h1
, h2
, 1);
1122 ca
= reverse_commit_list(ca
);
1126 output(5, "found %u common ancestor(s):", commit_list_count(ca
));
1127 for (iter
= ca
; iter
; iter
= iter
->next
)
1128 output_commit_title(iter
->item
);
1131 merged_common_ancestors
= pop_commit(&ca
);
1132 if (merged_common_ancestors
== NULL
) {
1133 /* if there is no common ancestor, make an empty tree */
1134 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
1136 tree
->object
.parsed
= 1;
1137 tree
->object
.type
= OBJ_TREE
;
1138 write_sha1_file(NULL
, 0, tree_type
, tree
->object
.sha1
);
1139 merged_common_ancestors
= make_virtual_commit(tree
, "ancestor");
1142 for (iter
= ca
; iter
; iter
= iter
->next
) {
1145 * When the merge fails, the result contains files
1146 * with conflict markers. The cleanness flag is
1147 * ignored, it was never acutally used, as result of
1148 * merge_trees has always overwritten it: the commited
1149 * "conflicts" were already resolved.
1152 merge(merged_common_ancestors
, iter
->item
,
1153 "Temporary merge branch 1",
1154 "Temporary merge branch 2",
1156 &merged_common_ancestors
);
1159 if (!merged_common_ancestors
)
1160 die("merge returned no commit");
1170 clean
= merge_trees(h1
->tree
, h2
->tree
, merged_common_ancestors
->tree
,
1171 branch1
, branch2
, &mrtree
);
1174 *result
= make_virtual_commit(mrtree
, "merged tree");
1175 commit_list_insert(h1
, &(*result
)->parents
);
1176 commit_list_insert(h2
, &(*result
)->parents
->next
);
1181 static const char *better_branch_name(const char *branch
)
1183 static char githead_env
[8 + 40 + 1];
1186 if (strlen(branch
) != 40)
1188 sprintf(githead_env
, "GITHEAD_%s", branch
);
1189 name
= getenv(githead_env
);
1190 return name
? name
: branch
;
1193 static struct commit
*get_ref(const char *ref
)
1195 unsigned char sha1
[20];
1196 struct object
*object
;
1198 if (get_sha1(ref
, sha1
))
1199 die("Could not resolve ref '%s'", ref
);
1200 object
= deref_tag(parse_object(sha1
), ref
, strlen(ref
));
1201 if (object
->type
== OBJ_TREE
)
1202 return make_virtual_commit((struct tree
*)object
,
1203 better_branch_name(ref
));
1204 if (object
->type
!= OBJ_COMMIT
)
1206 if (parse_commit((struct commit
*)object
))
1207 die("Could not parse commit '%s'", sha1_to_hex(object
->sha1
));
1208 return (struct commit
*)object
;
1211 static int merge_config(const char *var
, const char *value
)
1213 if (!strcasecmp(var
, "merge.verbosity")) {
1214 verbosity
= git_config_int(var
, value
);
1217 return git_default_config(var
, value
);
1220 int main(int argc
, char *argv
[])
1222 static const char *bases
[20];
1223 static unsigned bases_count
= 0;
1225 const char *branch1
, *branch2
;
1226 struct commit
*result
, *h1
, *h2
;
1227 struct commit_list
*ca
= NULL
;
1228 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
1231 git_config(merge_config
);
1232 if (getenv("GIT_MERGE_VERBOSITY"))
1233 verbosity
= strtol(getenv("GIT_MERGE_VERBOSITY"), NULL
, 10);
1236 die("Usage: %s <base>... -- <head> <remote> ...\n", argv
[0]);
1238 for (i
= 1; i
< argc
; ++i
) {
1239 if (!strcmp(argv
[i
], "--"))
1241 if (bases_count
< sizeof(bases
)/sizeof(*bases
))
1242 bases
[bases_count
++] = argv
[i
];
1244 if (argc
- i
!= 3) /* "--" "<head>" "<remote>" */
1245 die("Not handling anything other than two heads merge.");
1247 branch1
= argv
[++i
];
1248 branch2
= argv
[++i
];
1250 h1
= get_ref(branch1
);
1251 h2
= get_ref(branch2
);
1253 branch1
= better_branch_name(branch1
);
1254 branch2
= better_branch_name(branch2
);
1256 printf("Merging %s with %s\n", branch1
, branch2
);
1258 index_fd
= hold_lock_file_for_update(lock
, get_index_file(), 1);
1260 for (i
= 0; i
< bases_count
; i
++) {
1261 struct commit
*ancestor
= get_ref(bases
[i
]);
1262 ca
= commit_list_insert(ancestor
, &ca
);
1264 clean
= merge(h1
, h2
, branch1
, branch2
, ca
, &result
);
1266 if (active_cache_changed
&&
1267 (write_cache(index_fd
, active_cache
, active_nr
) ||
1268 close(index_fd
) || commit_lock_file(lock
)))
1269 die ("unable to write %s", get_index_file());
1271 return clean
? 0: 1;