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"
18 #include "interpolate.h"
21 static int subtree_merge
;
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 static unsigned commit_list_count(const struct commit_list
*l
)
46 for (; l
; l
= l
->next
)
51 static struct commit
*make_virtual_commit(struct tree
*tree
, const char *comment
)
53 struct commit
*commit
= xcalloc(1, sizeof(struct commit
));
54 static unsigned virtual_id
= 1;
56 commit
->util
= (void*)comment
;
57 *(int*)commit
->object
.sha1
= virtual_id
++;
59 commit
->object
.parsed
= 1;
64 * Since we use get_tree_entry(), which does not put the read object into
65 * the object pool, we cannot rely on a == b.
67 static int sha_eq(const unsigned char *a
, const unsigned char *b
)
71 return a
&& b
&& hashcmp(a
, b
) == 0;
75 * Since we want to write the index eventually, we cannot reuse the index
76 * for these (temporary) data.
83 unsigned char sha
[20];
90 struct output_buffer
*next
;
94 static struct path_list current_file_set
= {NULL
, 0, 0, 1};
95 static struct path_list current_directory_set
= {NULL
, 0, 0, 1};
97 static int call_depth
= 0;
98 static int verbosity
= 2;
99 static int buffer_output
= 1;
100 static struct output_buffer
*output_list
, *output_end
;
102 static int show (int v
)
104 return (!call_depth
&& verbosity
>= v
) || verbosity
>= 5;
107 static void output(int v
, const char *fmt
, ...)
111 if (buffer_output
&& show(v
)) {
112 struct output_buffer
*b
= xmalloc(sizeof(*b
));
113 nfvasprintf(&b
->str
, fmt
, args
);
116 output_end
->next
= b
;
120 } else if (show(v
)) {
122 for (i
= call_depth
; i
--;)
124 vfprintf(stdout
, fmt
, args
);
130 static void flush_output()
132 struct output_buffer
*b
, *n
;
133 for (b
= output_list
; b
; b
= n
) {
135 for (i
= call_depth
; i
--;)
137 fputs(b
->str
, stdout
);
147 static void output_commit_title(struct commit
*commit
)
151 for (i
= call_depth
; i
--;)
154 printf("virtual %s\n", (char *)commit
->util
);
156 printf("%s ", find_unique_abbrev(commit
->object
.sha1
, DEFAULT_ABBREV
));
157 if (parse_commit(commit
) != 0)
158 printf("(bad commit)\n");
162 for (s
= commit
->buffer
; *s
; s
++)
163 if (*s
== '\n' && s
[1] == '\n') {
167 for (len
= 0; s
[len
] && '\n' != s
[len
]; len
++)
169 printf("%.*s\n", len
, s
);
174 static struct cache_entry
*make_cache_entry(unsigned int mode
,
175 const unsigned char *sha1
, const char *path
, int stage
, int refresh
)
178 struct cache_entry
*ce
;
180 if (!verify_path(path
))
184 size
= cache_entry_size(len
);
185 ce
= xcalloc(1, size
);
187 hashcpy(ce
->sha1
, sha1
);
188 memcpy(ce
->name
, path
, len
);
189 ce
->ce_flags
= create_ce_flags(len
, stage
);
190 ce
->ce_mode
= create_ce_mode(mode
);
193 return refresh_cache_entry(ce
, 0);
198 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
199 const char *path
, int stage
, int refresh
, int options
)
201 struct cache_entry
*ce
;
202 ce
= make_cache_entry(mode
, sha1
? sha1
: null_sha1
, path
, stage
, refresh
);
204 return error("addinfo_cache failed for path '%s'", path
);
205 return add_cache_entry(ce
, options
);
209 * This is a global variable which is used in a number of places but
210 * only written to in the 'merge' function.
212 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
213 * don't update the working directory.
214 * 0 => Leave unmerged entries in the cache and update
215 * the working directory.
217 static int index_only
= 0;
219 static int git_merge_trees(int index_only
,
225 struct object_list
*trees
= NULL
;
226 struct unpack_trees_options opts
;
228 memset(&opts
, 0, sizeof(opts
));
235 opts
.fn
= threeway_merge
;
237 object_list_append(&common
->object
, &trees
);
238 object_list_append(&head
->object
, &trees
);
239 object_list_append(&merge
->object
, &trees
);
241 rc
= unpack_trees(trees
, &opts
);
242 cache_tree_free(&active_cache_tree
);
246 static int unmerged_index(void)
249 for (i
= 0; i
< active_nr
; i
++) {
250 struct cache_entry
*ce
= active_cache
[i
];
257 static struct tree
*git_write_tree(void)
259 struct tree
*result
= NULL
;
261 if (unmerged_index()) {
263 output(0, "There are unmerged index entries:");
264 for (i
= 0; i
< active_nr
; i
++) {
265 struct cache_entry
*ce
= active_cache
[i
];
267 output(0, "%d %.*s", ce_stage(ce
), ce_namelen(ce
), ce
->name
);
272 if (!active_cache_tree
)
273 active_cache_tree
= cache_tree();
275 if (!cache_tree_fully_valid(active_cache_tree
) &&
276 cache_tree_update(active_cache_tree
,
277 active_cache
, active_nr
, 0, 0) < 0)
278 die("error building trees");
280 result
= lookup_tree(active_cache_tree
->sha1
);
285 static int save_files_dirs(const unsigned char *sha1
,
286 const char *base
, int baselen
, const char *path
,
287 unsigned int mode
, int stage
)
289 int len
= strlen(path
);
290 char *newpath
= xmalloc(baselen
+ len
+ 1);
291 memcpy(newpath
, base
, baselen
);
292 memcpy(newpath
+ baselen
, path
, len
);
293 newpath
[baselen
+ len
] = '\0';
296 path_list_insert(newpath
, ¤t_directory_set
);
298 path_list_insert(newpath
, ¤t_file_set
);
301 return READ_TREE_RECURSIVE
;
304 static int get_files_dirs(struct tree
*tree
)
307 if (read_tree_recursive(tree
, "", 0, 0, NULL
, save_files_dirs
) != 0)
309 n
= current_file_set
.nr
+ current_directory_set
.nr
;
314 * Returns a index_entry instance which doesn't have to correspond to
315 * a real cache entry in Git's index.
317 static struct stage_data
*insert_stage_data(const char *path
,
318 struct tree
*o
, struct tree
*a
, struct tree
*b
,
319 struct path_list
*entries
)
321 struct path_list_item
*item
;
322 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
323 get_tree_entry(o
->object
.sha1
, path
,
324 e
->stages
[1].sha
, &e
->stages
[1].mode
);
325 get_tree_entry(a
->object
.sha1
, path
,
326 e
->stages
[2].sha
, &e
->stages
[2].mode
);
327 get_tree_entry(b
->object
.sha1
, path
,
328 e
->stages
[3].sha
, &e
->stages
[3].mode
);
329 item
= path_list_insert(path
, entries
);
335 * Create a dictionary mapping file names to stage_data objects. The
336 * dictionary contains one entry for every path with a non-zero stage entry.
338 static struct path_list
*get_unmerged(void)
340 struct path_list
*unmerged
= xcalloc(1, sizeof(struct path_list
));
343 unmerged
->strdup_paths
= 1;
345 for (i
= 0; i
< active_nr
; i
++) {
346 struct path_list_item
*item
;
347 struct stage_data
*e
;
348 struct cache_entry
*ce
= active_cache
[i
];
352 item
= path_list_lookup(ce
->name
, unmerged
);
354 item
= path_list_insert(ce
->name
, unmerged
);
355 item
->util
= xcalloc(1, sizeof(struct stage_data
));
358 e
->stages
[ce_stage(ce
)].mode
= ntohl(ce
->ce_mode
);
359 hashcpy(e
->stages
[ce_stage(ce
)].sha
, ce
->sha1
);
367 struct diff_filepair
*pair
;
368 struct stage_data
*src_entry
;
369 struct stage_data
*dst_entry
;
370 unsigned processed
:1;
374 * Get information of all renames which occurred between 'o_tree' and
375 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
376 * 'b_tree') to be able to associate the correct cache entries with
377 * the rename information. 'tree' is always equal to either a_tree or b_tree.
379 static struct path_list
*get_renames(struct tree
*tree
,
383 struct path_list
*entries
)
386 struct path_list
*renames
;
387 struct diff_options opts
;
389 renames
= xcalloc(1, sizeof(struct path_list
));
392 opts
.detect_rename
= DIFF_DETECT_RENAME
;
393 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
394 if (diff_setup_done(&opts
) < 0)
395 die("diff setup failed");
396 diff_tree_sha1(o_tree
->object
.sha1
, tree
->object
.sha1
, "", &opts
);
398 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
399 struct path_list_item
*item
;
401 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
402 if (pair
->status
!= 'R') {
403 diff_free_filepair(pair
);
406 re
= xmalloc(sizeof(*re
));
409 item
= path_list_lookup(re
->pair
->one
->path
, entries
);
411 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
412 o_tree
, a_tree
, b_tree
, entries
);
414 re
->src_entry
= item
->util
;
416 item
= path_list_lookup(re
->pair
->two
->path
, entries
);
418 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
419 o_tree
, a_tree
, b_tree
, entries
);
421 re
->dst_entry
= item
->util
;
422 item
= path_list_insert(pair
->one
->path
, renames
);
425 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
426 diff_queued_diff
.nr
= 0;
431 static int update_stages(const char *path
, struct diff_filespec
*o
,
432 struct diff_filespec
*a
, struct diff_filespec
*b
,
435 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_OK_TO_REPLACE
;
437 if (remove_file_from_cache(path
))
440 if (add_cacheinfo(o
->mode
, o
->sha1
, path
, 1, 0, options
))
443 if (add_cacheinfo(a
->mode
, a
->sha1
, path
, 2, 0, options
))
446 if (add_cacheinfo(b
->mode
, b
->sha1
, path
, 3, 0, options
))
451 static int remove_path(const char *name
)
460 dirs
= xmalloc(len
+1);
461 memcpy(dirs
, name
, len
);
463 while ((slash
= strrchr(name
, '/'))) {
466 if (rmdir(name
) != 0)
473 static int remove_file(int clean
, const char *path
, int no_wd
)
475 int update_cache
= index_only
|| clean
;
476 int update_working_directory
= !index_only
&& !no_wd
;
479 if (remove_file_from_cache(path
))
482 if (update_working_directory
) {
484 if (errno
!= ENOENT
|| errno
!= EISDIR
)
491 static char *unique_path(const char *path
, const char *branch
)
493 char *newpath
= xmalloc(strlen(path
) + 1 + strlen(branch
) + 8 + 1);
496 char *p
= newpath
+ strlen(path
);
497 strcpy(newpath
, path
);
503 while (path_list_has_path(¤t_file_set
, newpath
) ||
504 path_list_has_path(¤t_directory_set
, newpath
) ||
505 lstat(newpath
, &st
) == 0)
506 sprintf(p
, "_%d", suffix
++);
508 path_list_insert(newpath
, ¤t_file_set
);
512 static int mkdir_p(const char *path
, unsigned long mode
)
514 /* path points to cache entries, so xstrdup before messing with it */
515 char *buf
= xstrdup(path
);
516 int result
= safe_create_leading_directories(buf
);
521 static void flush_buffer(int fd
, const char *buf
, unsigned long size
)
524 long ret
= write_in_full(fd
, buf
, size
);
529 die("merge-recursive: %s", strerror(errno
));
531 die("merge-recursive: disk full?");
538 static int make_room_for_path(const char *path
)
541 const char *msg
= "failed to create path '%s'%s";
543 status
= mkdir_p(path
, 0777);
546 /* something else exists */
547 error(msg
, path
, ": perhaps a D/F conflict?");
553 /* Successful unlink is good.. */
556 /* .. and so is no existing file */
559 /* .. but not some other error (who really cares what?) */
560 return error(msg
, path
, ": perhaps a D/F conflict?");
563 static void update_file_flags(const unsigned char *sha
,
573 enum object_type type
;
577 buf
= read_sha1_file(sha
, &type
, &size
);
579 die("cannot read object %s '%s'", sha1_to_hex(sha
), path
);
580 if (type
!= OBJ_BLOB
)
581 die("blob expected for %s '%s'", sha1_to_hex(sha
), path
);
583 if (make_room_for_path(path
) < 0) {
587 if (S_ISREG(mode
) || (!has_symlinks
&& S_ISLNK(mode
))) {
593 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
595 die("failed to open %s: %s", path
, strerror(errno
));
596 flush_buffer(fd
, buf
, size
);
598 } else if (S_ISLNK(mode
)) {
599 char *lnk
= xmalloc(size
+ 1);
600 memcpy(lnk
, buf
, size
);
607 die("do not know what to do with %06o %s '%s'",
608 mode
, sha1_to_hex(sha
), path
);
612 add_cacheinfo(mode
, sha
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
615 static void update_file(int clean
,
616 const unsigned char *sha
,
620 update_file_flags(sha
, mode
, path
, index_only
|| clean
, !index_only
);
623 /* Low level file merging, update and removal */
625 struct merge_file_info
627 unsigned char sha
[20];
633 static void fill_mm(const unsigned char *sha1
, mmfile_t
*mm
)
636 enum object_type type
;
638 if (!hashcmp(sha1
, null_sha1
)) {
639 mm
->ptr
= xstrdup("");
644 mm
->ptr
= read_sha1_file(sha1
, &type
, &size
);
645 if (!mm
->ptr
|| type
!= OBJ_BLOB
)
646 die("unable to read blob object %s", sha1_to_hex(sha1
));
651 * Customizable low-level merge drivers support.
654 struct ll_merge_driver
;
655 typedef int (*ll_merge_fn
)(const struct ll_merge_driver
*,
658 mmfile_t
*src1
, const char *name1
,
659 mmfile_t
*src2
, const char *name2
,
662 struct ll_merge_driver
{
664 const char *description
;
666 const char *recursive
;
667 struct ll_merge_driver
*next
;
672 * Built-in low-levels
674 static int ll_xdl_merge(const struct ll_merge_driver
*drv_unused
,
675 const char *path_unused
,
677 mmfile_t
*src1
, const char *name1
,
678 mmfile_t
*src2
, const char *name2
,
683 memset(&xpp
, 0, sizeof(xpp
));
684 return xdl_merge(orig
,
687 &xpp
, XDL_MERGE_ZEALOUS
,
691 static int ll_union_merge(const struct ll_merge_driver
*drv_unused
,
692 const char *path_unused
,
694 mmfile_t
*src1
, const char *name1
,
695 mmfile_t
*src2
, const char *name2
,
700 const int marker_size
= 7;
702 int status
= ll_xdl_merge(drv_unused
, path_unused
,
703 orig
, src1
, NULL
, src2
, NULL
, result
);
707 src
= dst
= result
->ptr
;
710 if ((marker_size
< size
) &&
711 (*src
== '<' || *src
== '=' || *src
== '>')) {
714 for (i
= 0; i
< marker_size
; i
++)
717 if (src
[marker_size
] != '\n')
719 src
+= marker_size
+ 1;
720 size
-= marker_size
+ 1;
728 } while (ch
!= '\n' && size
);
730 result
->size
= dst
- result
->ptr
;
734 static int ll_binary_merge(const struct ll_merge_driver
*drv_unused
,
735 const char *path_unused
,
737 mmfile_t
*src1
, const char *name1
,
738 mmfile_t
*src2
, const char *name2
,
742 * The tentative merge result is "ours" for the final round,
743 * or common ancestor for an internal merge. Still return
744 * "conflicted merge" status.
746 mmfile_t
*stolen
= index_only
? orig
: src1
;
748 result
->ptr
= stolen
->ptr
;
749 result
->size
= stolen
->size
;
754 #define LL_BINARY_MERGE 0
755 #define LL_TEXT_MERGE 1
756 #define LL_UNION_MERGE 2
757 static struct ll_merge_driver ll_merge_drv
[] = {
758 { "binary", "built-in binary merge", ll_binary_merge
},
759 { "text", "built-in 3-way text merge", ll_xdl_merge
},
760 { "union", "built-in union merge", ll_union_merge
},
763 static void create_temp(mmfile_t
*src
, char *path
)
767 strcpy(path
, ".merge_file_XXXXXX");
770 die("unable to create temp-file");
771 if (write_in_full(fd
, src
->ptr
, src
->size
) != src
->size
)
772 die("unable to write temp-file");
777 * User defined low-level merge driver support.
779 static int ll_ext_merge(const struct ll_merge_driver
*fn
,
782 mmfile_t
*src1
, const char *name1
,
783 mmfile_t
*src2
, const char *name2
,
788 struct interp table
[] = {
793 struct child_process child
;
794 const char *args
[20];
798 if (fn
->cmdline
== NULL
)
799 die("custom merge driver %s lacks command line.", fn
->name
);
803 create_temp(orig
, temp
[0]);
804 create_temp(src1
, temp
[1]);
805 create_temp(src2
, temp
[2]);
807 interp_set_entry(table
, 0, temp
[0]);
808 interp_set_entry(table
, 1, temp
[1]);
809 interp_set_entry(table
, 2, temp
[2]);
811 output(1, "merging %s using %s", path
,
812 fn
->description
? fn
->description
: fn
->name
);
814 interpolate(cmdbuf
, sizeof(cmdbuf
), fn
->cmdline
, table
, 3);
816 memset(&child
, 0, sizeof(child
));
823 status
= run_command(&child
);
824 if (status
< -ERR_RUN_COMMAND_FORK
)
825 ; /* failure in run-command */
828 fd
= open(temp
[1], O_RDONLY
);
833 result
->size
= st
.st_size
;
834 result
->ptr
= xmalloc(result
->size
+ 1);
835 if (read_in_full(fd
, result
->ptr
, result
->size
) != result
->size
) {
843 for (i
= 0; i
< 3; i
++)
849 * merge.default and merge.driver configuration items
851 static struct ll_merge_driver
*ll_user_merge
, **ll_user_merge_tail
;
852 static const char *default_ll_merge
;
854 static int read_merge_config(const char *var
, const char *value
)
856 struct ll_merge_driver
*fn
;
857 const char *ep
, *name
;
860 if (!strcmp(var
, "merge.default")) {
862 default_ll_merge
= strdup(value
);
867 * We are not interested in anything but "merge.<name>.variable";
868 * especially, we do not want to look at variables such as
869 * "merge.summary", "merge.tool", and "merge.verbosity".
871 if (prefixcmp(var
, "merge.") || (ep
= strrchr(var
, '.')) == var
+ 5)
875 * Find existing one as we might be processing merge.<name>.var2
876 * after seeing merge.<name>.var1.
880 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
881 if (!strncmp(fn
->name
, name
, namelen
) && !fn
->name
[namelen
])
885 fn
= xcalloc(1, sizeof(struct ll_merge_driver
));
886 namebuf
= xmalloc(namelen
+ 1);
887 memcpy(namebuf
, name
, namelen
);
888 namebuf
[namelen
] = 0;
890 fn
->fn
= ll_ext_merge
;
892 *ll_user_merge_tail
= fn
;
893 ll_user_merge_tail
= &(fn
->next
);
898 if (!strcmp("name", ep
)) {
900 return error("%s: lacks value", var
);
901 fn
->description
= strdup(value
);
905 if (!strcmp("driver", ep
)) {
907 return error("%s: lacks value", var
);
909 * merge.<name>.driver specifies the command line:
913 * The command-line will be interpolated with the following
914 * tokens and is given to the shell:
916 * %O - temporary file name for the merge base.
917 * %A - temporary file name for our version.
918 * %B - temporary file name for the other branches' version.
920 * The external merge driver should write the results in the
921 * file named by %A, and signal that it has done with zero exit
924 fn
->cmdline
= strdup(value
);
928 if (!strcmp("recursive", ep
)) {
930 return error("%s: lacks value", var
);
931 fn
->recursive
= strdup(value
);
938 static void initialize_ll_merge(void)
940 if (ll_user_merge_tail
)
942 ll_user_merge_tail
= &ll_user_merge
;
943 git_config(read_merge_config
);
946 static const struct ll_merge_driver
*find_ll_merge_driver(const char *merge_attr
)
948 struct ll_merge_driver
*fn
;
952 initialize_ll_merge();
954 if (ATTR_TRUE(merge_attr
))
955 return &ll_merge_drv
[LL_TEXT_MERGE
];
956 else if (ATTR_FALSE(merge_attr
))
957 return &ll_merge_drv
[LL_BINARY_MERGE
];
958 else if (ATTR_UNSET(merge_attr
)) {
959 if (!default_ll_merge
)
960 return &ll_merge_drv
[LL_TEXT_MERGE
];
962 name
= default_ll_merge
;
967 for (fn
= ll_user_merge
; fn
; fn
= fn
->next
)
968 if (!strcmp(fn
->name
, name
))
971 for (i
= 0; i
< ARRAY_SIZE(ll_merge_drv
); i
++)
972 if (!strcmp(ll_merge_drv
[i
].name
, name
))
973 return &ll_merge_drv
[i
];
975 /* default to the 3-way */
976 return &ll_merge_drv
[LL_TEXT_MERGE
];
979 static const char *git_path_check_merge(const char *path
)
981 static struct git_attr_check attr_merge_check
;
983 if (!attr_merge_check
.attr
)
984 attr_merge_check
.attr
= git_attr("merge", 5);
986 if (git_checkattr(path
, 1, &attr_merge_check
))
988 return attr_merge_check
.value
;
991 static int ll_merge(mmbuffer_t
*result_buf
,
992 struct diff_filespec
*o
,
993 struct diff_filespec
*a
,
994 struct diff_filespec
*b
,
998 mmfile_t orig
, src1
, src2
;
1001 const char *ll_driver_name
;
1002 const struct ll_merge_driver
*driver
;
1004 name1
= xstrdup(mkpath("%s:%s", branch1
, a
->path
));
1005 name2
= xstrdup(mkpath("%s:%s", branch2
, b
->path
));
1007 fill_mm(o
->sha1
, &orig
);
1008 fill_mm(a
->sha1
, &src1
);
1009 fill_mm(b
->sha1
, &src2
);
1011 ll_driver_name
= git_path_check_merge(a
->path
);
1012 driver
= find_ll_merge_driver(ll_driver_name
);
1014 if (index_only
&& driver
->recursive
)
1015 driver
= find_ll_merge_driver(driver
->recursive
);
1016 merge_status
= driver
->fn(driver
, a
->path
,
1017 &orig
, &src1
, name1
, &src2
, name2
,
1025 return merge_status
;
1028 static struct merge_file_info
merge_file(struct diff_filespec
*o
,
1029 struct diff_filespec
*a
, struct diff_filespec
*b
,
1030 const char *branch1
, const char *branch2
)
1032 struct merge_file_info result
;
1036 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
1038 if (S_ISREG(a
->mode
)) {
1039 result
.mode
= a
->mode
;
1040 hashcpy(result
.sha
, a
->sha1
);
1042 result
.mode
= b
->mode
;
1043 hashcpy(result
.sha
, b
->sha1
);
1046 if (!sha_eq(a
->sha1
, o
->sha1
) && !sha_eq(b
->sha1
, o
->sha1
))
1049 result
.mode
= a
->mode
== o
->mode
? b
->mode
: a
->mode
;
1051 if (sha_eq(a
->sha1
, o
->sha1
))
1052 hashcpy(result
.sha
, b
->sha1
);
1053 else if (sha_eq(b
->sha1
, o
->sha1
))
1054 hashcpy(result
.sha
, a
->sha1
);
1055 else if (S_ISREG(a
->mode
)) {
1056 mmbuffer_t result_buf
;
1059 merge_status
= ll_merge(&result_buf
, o
, a
, b
,
1062 if ((merge_status
< 0) || !result_buf
.ptr
)
1063 die("Failed to execute internal merge");
1065 if (write_sha1_file(result_buf
.ptr
, result_buf
.size
,
1066 blob_type
, result
.sha
))
1067 die("Unable to add %s to database",
1070 free(result_buf
.ptr
);
1071 result
.clean
= (merge_status
== 0);
1073 if (!(S_ISLNK(a
->mode
) || S_ISLNK(b
->mode
)))
1074 die("cannot merge modes?");
1076 hashcpy(result
.sha
, a
->sha1
);
1078 if (!sha_eq(a
->sha1
, b
->sha1
))
1086 static void conflict_rename_rename(struct rename
*ren1
,
1087 const char *branch1
,
1088 struct rename
*ren2
,
1089 const char *branch2
)
1093 const char *ren1_dst
= ren1
->pair
->two
->path
;
1094 const char *ren2_dst
= ren2
->pair
->two
->path
;
1095 const char *dst_name1
= ren1_dst
;
1096 const char *dst_name2
= ren2_dst
;
1097 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
1098 dst_name1
= del
[delp
++] = unique_path(ren1_dst
, branch1
);
1099 output(1, "%s is a directory in %s added as %s instead",
1100 ren1_dst
, branch2
, dst_name1
);
1101 remove_file(0, ren1_dst
, 0);
1103 if (path_list_has_path(¤t_directory_set
, ren2_dst
)) {
1104 dst_name2
= del
[delp
++] = unique_path(ren2_dst
, branch2
);
1105 output(1, "%s is a directory in %s added as %s instead",
1106 ren2_dst
, branch1
, dst_name2
);
1107 remove_file(0, ren2_dst
, 0);
1110 remove_file_from_cache(dst_name1
);
1111 remove_file_from_cache(dst_name2
);
1113 * Uncomment to leave the conflicting names in the resulting tree
1115 * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
1116 * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
1119 update_stages(dst_name1
, NULL
, ren1
->pair
->two
, NULL
, 1);
1120 update_stages(dst_name2
, NULL
, NULL
, ren2
->pair
->two
, 1);
1126 static void conflict_rename_dir(struct rename
*ren1
,
1127 const char *branch1
)
1129 char *new_path
= unique_path(ren1
->pair
->two
->path
, branch1
);
1130 output(1, "Renamed %s to %s instead", ren1
->pair
->one
->path
, new_path
);
1131 remove_file(0, ren1
->pair
->two
->path
, 0);
1132 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path
);
1136 static void conflict_rename_rename_2(struct rename
*ren1
,
1137 const char *branch1
,
1138 struct rename
*ren2
,
1139 const char *branch2
)
1141 char *new_path1
= unique_path(ren1
->pair
->two
->path
, branch1
);
1142 char *new_path2
= unique_path(ren2
->pair
->two
->path
, branch2
);
1143 output(1, "Renamed %s to %s and %s to %s instead",
1144 ren1
->pair
->one
->path
, new_path1
,
1145 ren2
->pair
->one
->path
, new_path2
);
1146 remove_file(0, ren1
->pair
->two
->path
, 0);
1147 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, new_path1
);
1148 update_file(0, ren2
->pair
->two
->sha1
, ren2
->pair
->two
->mode
, new_path2
);
1153 static int process_renames(struct path_list
*a_renames
,
1154 struct path_list
*b_renames
,
1155 const char *a_branch
,
1156 const char *b_branch
)
1158 int clean_merge
= 1, i
, j
;
1159 struct path_list a_by_dst
= {NULL
, 0, 0, 0}, b_by_dst
= {NULL
, 0, 0, 0};
1160 const struct rename
*sre
;
1162 for (i
= 0; i
< a_renames
->nr
; i
++) {
1163 sre
= a_renames
->items
[i
].util
;
1164 path_list_insert(sre
->pair
->two
->path
, &a_by_dst
)->util
1167 for (i
= 0; i
< b_renames
->nr
; i
++) {
1168 sre
= b_renames
->items
[i
].util
;
1169 path_list_insert(sre
->pair
->two
->path
, &b_by_dst
)->util
1173 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
1176 struct path_list
*renames1
, *renames2
, *renames2Dst
;
1177 struct rename
*ren1
= NULL
, *ren2
= NULL
;
1178 const char *branch1
, *branch2
;
1179 const char *ren1_src
, *ren1_dst
;
1181 if (i
>= a_renames
->nr
) {
1183 ren2
= b_renames
->items
[j
++].util
;
1184 } else if (j
>= b_renames
->nr
) {
1186 ren1
= a_renames
->items
[i
++].util
;
1188 compare
= strcmp(a_renames
->items
[i
].path
,
1189 b_renames
->items
[j
].path
);
1191 ren1
= a_renames
->items
[i
++].util
;
1193 ren2
= b_renames
->items
[j
++].util
;
1196 /* TODO: refactor, so that 1/2 are not needed */
1198 renames1
= a_renames
;
1199 renames2
= b_renames
;
1200 renames2Dst
= &b_by_dst
;
1205 renames1
= b_renames
;
1206 renames2
= a_renames
;
1207 renames2Dst
= &a_by_dst
;
1214 src
= ren1
->pair
->one
->path
;
1216 ren1
->dst_entry
->processed
= 1;
1217 ren1
->src_entry
->processed
= 1;
1219 if (ren1
->processed
)
1221 ren1
->processed
= 1;
1223 ren1_src
= ren1
->pair
->one
->path
;
1224 ren1_dst
= ren1
->pair
->two
->path
;
1227 const char *ren2_src
= ren2
->pair
->one
->path
;
1228 const char *ren2_dst
= ren2
->pair
->two
->path
;
1229 /* Renamed in 1 and renamed in 2 */
1230 if (strcmp(ren1_src
, ren2_src
) != 0)
1231 die("ren1.src != ren2.src");
1232 ren2
->dst_entry
->processed
= 1;
1233 ren2
->processed
= 1;
1234 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
1236 output(1, "CONFLICT (rename/rename): "
1237 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1238 "rename \"%s\"->\"%s\" in \"%s\"%s",
1239 src
, ren1_dst
, branch1
,
1240 src
, ren2_dst
, branch2
,
1241 index_only
? " (left unresolved)": "");
1243 remove_file_from_cache(src
);
1244 update_file(0, ren1
->pair
->one
->sha1
,
1245 ren1
->pair
->one
->mode
, src
);
1247 conflict_rename_rename(ren1
, branch1
, ren2
, branch2
);
1249 struct merge_file_info mfi
;
1250 remove_file(1, ren1_src
, 1);
1251 mfi
= merge_file(ren1
->pair
->one
,
1256 if (mfi
.merge
|| !mfi
.clean
)
1257 output(1, "Renamed %s->%s", src
, ren1_dst
);
1260 output(2, "Auto-merged %s", ren1_dst
);
1263 output(1, "CONFLICT (content): merge conflict in %s",
1268 update_stages(ren1_dst
,
1274 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
1277 /* Renamed in 1, maybe changed in 2 */
1278 struct path_list_item
*item
;
1279 /* we only use sha1 and mode of these */
1280 struct diff_filespec src_other
, dst_other
;
1281 int try_merge
, stage
= a_renames
== renames1
? 3: 2;
1283 remove_file(1, ren1_src
, index_only
|| stage
== 3);
1285 hashcpy(src_other
.sha1
, ren1
->src_entry
->stages
[stage
].sha
);
1286 src_other
.mode
= ren1
->src_entry
->stages
[stage
].mode
;
1287 hashcpy(dst_other
.sha1
, ren1
->dst_entry
->stages
[stage
].sha
);
1288 dst_other
.mode
= ren1
->dst_entry
->stages
[stage
].mode
;
1292 if (path_list_has_path(¤t_directory_set
, ren1_dst
)) {
1294 output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s "
1295 " directory %s added in %s",
1296 ren1_src
, ren1_dst
, branch1
,
1298 conflict_rename_dir(ren1
, branch1
);
1299 } else if (sha_eq(src_other
.sha1
, null_sha1
)) {
1301 output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s "
1302 "and deleted in %s",
1303 ren1_src
, ren1_dst
, branch1
,
1305 update_file(0, ren1
->pair
->two
->sha1
, ren1
->pair
->two
->mode
, ren1_dst
);
1306 } else if (!sha_eq(dst_other
.sha1
, null_sha1
)) {
1307 const char *new_path
;
1310 output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. "
1312 ren1_src
, ren1_dst
, branch1
,
1314 new_path
= unique_path(ren1_dst
, branch2
);
1315 output(1, "Added as %s instead", new_path
);
1316 update_file(0, dst_other
.sha1
, dst_other
.mode
, new_path
);
1317 } else if ((item
= path_list_lookup(ren1_dst
, renames2Dst
))) {
1320 ren2
->processed
= 1;
1321 output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. "
1322 "Renamed %s->%s in %s",
1323 ren1_src
, ren1_dst
, branch1
,
1324 ren2
->pair
->one
->path
, ren2
->pair
->two
->path
, branch2
);
1325 conflict_rename_rename_2(ren1
, branch1
, ren2
, branch2
);
1330 struct diff_filespec
*o
, *a
, *b
;
1331 struct merge_file_info mfi
;
1332 src_other
.path
= (char *)ren1_src
;
1334 o
= ren1
->pair
->one
;
1335 if (a_renames
== renames1
) {
1336 a
= ren1
->pair
->two
;
1339 b
= ren1
->pair
->two
;
1342 mfi
= merge_file(o
, a
, b
,
1343 a_branch
, b_branch
);
1345 if (mfi
.merge
|| !mfi
.clean
)
1346 output(1, "Renamed %s => %s", ren1_src
, ren1_dst
);
1348 output(2, "Auto-merged %s", ren1_dst
);
1350 output(1, "CONFLICT (rename/modify): Merge conflict in %s",
1355 update_stages(ren1_dst
,
1358 update_file(mfi
.clean
, mfi
.sha
, mfi
.mode
, ren1_dst
);
1362 path_list_clear(&a_by_dst
, 0);
1363 path_list_clear(&b_by_dst
, 0);
1368 static unsigned char *stage_sha(const unsigned char *sha
, unsigned mode
)
1370 return (is_null_sha1(sha
) || mode
== 0) ? NULL
: (unsigned char *)sha
;
1373 /* Per entry merge function */
1374 static int process_entry(const char *path
, struct stage_data
*entry
,
1375 const char *branch1
,
1376 const char *branch2
)
1379 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1380 print_index_entry("\tpath: ", entry);
1382 int clean_merge
= 1;
1383 unsigned o_mode
= entry
->stages
[1].mode
;
1384 unsigned a_mode
= entry
->stages
[2].mode
;
1385 unsigned b_mode
= entry
->stages
[3].mode
;
1386 unsigned char *o_sha
= stage_sha(entry
->stages
[1].sha
, o_mode
);
1387 unsigned char *a_sha
= stage_sha(entry
->stages
[2].sha
, a_mode
);
1388 unsigned char *b_sha
= stage_sha(entry
->stages
[3].sha
, b_mode
);
1390 if (o_sha
&& (!a_sha
|| !b_sha
)) {
1391 /* Case A: Deleted in one */
1392 if ((!a_sha
&& !b_sha
) ||
1393 (sha_eq(a_sha
, o_sha
) && !b_sha
) ||
1394 (!a_sha
&& sha_eq(b_sha
, o_sha
))) {
1395 /* Deleted in both or deleted in one and
1396 * unchanged in the other */
1398 output(2, "Removed %s", path
);
1399 /* do not touch working file if it did not exist */
1400 remove_file(1, path
, !a_sha
);
1402 /* Deleted in one and changed in the other */
1405 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1406 "and modified in %s. Version %s of %s left in tree.",
1408 branch2
, branch2
, path
);
1409 update_file(0, b_sha
, b_mode
, path
);
1411 output(1, "CONFLICT (delete/modify): %s deleted in %s "
1412 "and modified in %s. Version %s of %s left in tree.",
1414 branch1
, branch1
, path
);
1415 update_file(0, a_sha
, a_mode
, path
);
1419 } else if ((!o_sha
&& a_sha
&& !b_sha
) ||
1420 (!o_sha
&& !a_sha
&& b_sha
)) {
1421 /* Case B: Added in one. */
1422 const char *add_branch
;
1423 const char *other_branch
;
1425 const unsigned char *sha
;
1429 add_branch
= branch1
;
1430 other_branch
= branch2
;
1433 conf
= "file/directory";
1435 add_branch
= branch2
;
1436 other_branch
= branch1
;
1439 conf
= "directory/file";
1441 if (path_list_has_path(¤t_directory_set
, path
)) {
1442 const char *new_path
= unique_path(path
, add_branch
);
1444 output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
1446 conf
, path
, other_branch
, path
, new_path
);
1447 remove_file(0, path
, 0);
1448 update_file(0, sha
, mode
, new_path
);
1450 output(2, "Added %s", path
);
1451 update_file(1, sha
, mode
, path
);
1453 } else if (a_sha
&& b_sha
) {
1454 /* Case C: Added in both (check for same permissions) and */
1455 /* case D: Modified in both, but differently. */
1456 const char *reason
= "content";
1457 struct merge_file_info mfi
;
1458 struct diff_filespec o
, a
, b
;
1462 o_sha
= (unsigned char *)null_sha1
;
1464 output(2, "Auto-merged %s", path
);
1465 o
.path
= a
.path
= b
.path
= (char *)path
;
1466 hashcpy(o
.sha1
, o_sha
);
1468 hashcpy(a
.sha1
, a_sha
);
1470 hashcpy(b
.sha1
, b_sha
);
1473 mfi
= merge_file(&o
, &a
, &b
,
1477 update_file(1, mfi
.sha
, mfi
.mode
, path
);
1480 output(1, "CONFLICT (%s): Merge conflict in %s",
1484 update_file(0, mfi
.sha
, mfi
.mode
, path
);
1486 update_file_flags(mfi
.sha
, mfi
.mode
, path
,
1487 0 /* update_cache */, 1 /* update_working_directory */);
1489 } else if (!o_sha
&& !a_sha
&& !b_sha
) {
1491 * this entry was deleted altogether. a_mode == 0 means
1492 * we had that path and want to actively remove it.
1494 remove_file(1, path
, !a_mode
);
1496 die("Fatal merge failure, shouldn't happen.");
1501 static int merge_trees(struct tree
*head
,
1503 struct tree
*common
,
1504 const char *branch1
,
1505 const char *branch2
,
1506 struct tree
**result
)
1510 if (subtree_merge
) {
1511 merge
= shift_tree_object(head
, merge
);
1512 common
= shift_tree_object(head
, common
);
1515 if (sha_eq(common
->object
.sha1
, merge
->object
.sha1
)) {
1516 output(0, "Already uptodate!");
1521 code
= git_merge_trees(index_only
, common
, head
, merge
);
1524 die("merging of trees %s and %s failed",
1525 sha1_to_hex(head
->object
.sha1
),
1526 sha1_to_hex(merge
->object
.sha1
));
1528 if (unmerged_index()) {
1529 struct path_list
*entries
, *re_head
, *re_merge
;
1531 path_list_clear(¤t_file_set
, 1);
1532 path_list_clear(¤t_directory_set
, 1);
1533 get_files_dirs(head
);
1534 get_files_dirs(merge
);
1536 entries
= get_unmerged();
1537 re_head
= get_renames(head
, common
, head
, merge
, entries
);
1538 re_merge
= get_renames(merge
, common
, head
, merge
, entries
);
1539 clean
= process_renames(re_head
, re_merge
,
1541 for (i
= 0; i
< entries
->nr
; i
++) {
1542 const char *path
= entries
->items
[i
].path
;
1543 struct stage_data
*e
= entries
->items
[i
].util
;
1545 && !process_entry(path
, e
, branch1
, branch2
))
1549 path_list_clear(re_merge
, 0);
1550 path_list_clear(re_head
, 0);
1551 path_list_clear(entries
, 1);
1558 *result
= git_write_tree();
1563 static struct commit_list
*reverse_commit_list(struct commit_list
*list
)
1565 struct commit_list
*next
= NULL
, *current
, *backup
;
1566 for (current
= list
; current
; current
= backup
) {
1567 backup
= current
->next
;
1568 current
->next
= next
;
1575 * Merge the commits h1 and h2, return the resulting virtual
1576 * commit object and a flag indicating the cleanness of the merge.
1578 static int merge(struct commit
*h1
,
1580 const char *branch1
,
1581 const char *branch2
,
1582 struct commit_list
*ca
,
1583 struct commit
**result
)
1585 struct commit_list
*iter
;
1586 struct commit
*merged_common_ancestors
;
1587 struct tree
*mrtree
;
1591 output(4, "Merging:");
1592 output_commit_title(h1
);
1593 output_commit_title(h2
);
1597 ca
= get_merge_bases(h1
, h2
, 1);
1598 ca
= reverse_commit_list(ca
);
1602 output(5, "found %u common ancestor(s):", commit_list_count(ca
));
1603 for (iter
= ca
; iter
; iter
= iter
->next
)
1604 output_commit_title(iter
->item
);
1607 merged_common_ancestors
= pop_commit(&ca
);
1608 if (merged_common_ancestors
== NULL
) {
1609 /* if there is no common ancestor, make an empty tree */
1610 struct tree
*tree
= xcalloc(1, sizeof(struct tree
));
1612 tree
->object
.parsed
= 1;
1613 tree
->object
.type
= OBJ_TREE
;
1614 pretend_sha1_file(NULL
, 0, OBJ_TREE
, tree
->object
.sha1
);
1615 merged_common_ancestors
= make_virtual_commit(tree
, "ancestor");
1618 for (iter
= ca
; iter
; iter
= iter
->next
) {
1621 * When the merge fails, the result contains files
1622 * with conflict markers. The cleanness flag is
1623 * ignored, it was never actually used, as result of
1624 * merge_trees has always overwritten it: the committed
1625 * "conflicts" were already resolved.
1628 merge(merged_common_ancestors
, iter
->item
,
1629 "Temporary merge branch 1",
1630 "Temporary merge branch 2",
1632 &merged_common_ancestors
);
1635 if (!merged_common_ancestors
)
1636 die("merge returned no commit");
1646 clean
= merge_trees(h1
->tree
, h2
->tree
, merged_common_ancestors
->tree
,
1647 branch1
, branch2
, &mrtree
);
1650 *result
= make_virtual_commit(mrtree
, "merged tree");
1651 commit_list_insert(h1
, &(*result
)->parents
);
1652 commit_list_insert(h2
, &(*result
)->parents
->next
);
1658 static const char *better_branch_name(const char *branch
)
1660 static char githead_env
[8 + 40 + 1];
1663 if (strlen(branch
) != 40)
1665 sprintf(githead_env
, "GITHEAD_%s", branch
);
1666 name
= getenv(githead_env
);
1667 return name
? name
: branch
;
1670 static struct commit
*get_ref(const char *ref
)
1672 unsigned char sha1
[20];
1673 struct object
*object
;
1675 if (get_sha1(ref
, sha1
))
1676 die("Could not resolve ref '%s'", ref
);
1677 object
= deref_tag(parse_object(sha1
), ref
, strlen(ref
));
1678 if (object
->type
== OBJ_TREE
)
1679 return make_virtual_commit((struct tree
*)object
,
1680 better_branch_name(ref
));
1681 if (object
->type
!= OBJ_COMMIT
)
1683 if (parse_commit((struct commit
*)object
))
1684 die("Could not parse commit '%s'", sha1_to_hex(object
->sha1
));
1685 return (struct commit
*)object
;
1688 static int merge_config(const char *var
, const char *value
)
1690 if (!strcasecmp(var
, "merge.verbosity")) {
1691 verbosity
= git_config_int(var
, value
);
1694 return git_default_config(var
, value
);
1697 int main(int argc
, char *argv
[])
1699 static const char *bases
[20];
1700 static unsigned bases_count
= 0;
1702 const char *branch1
, *branch2
;
1703 struct commit
*result
, *h1
, *h2
;
1704 struct commit_list
*ca
= NULL
;
1705 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
1709 int namelen
= strlen(argv
[0]);
1711 !strcmp(argv
[0] + namelen
- 8, "-subtree"))
1715 git_config(merge_config
);
1716 if (getenv("GIT_MERGE_VERBOSITY"))
1717 verbosity
= strtol(getenv("GIT_MERGE_VERBOSITY"), NULL
, 10);
1720 die("Usage: %s <base>... -- <head> <remote> ...\n", argv
[0]);
1722 for (i
= 1; i
< argc
; ++i
) {
1723 if (!strcmp(argv
[i
], "--"))
1725 if (bases_count
< sizeof(bases
)/sizeof(*bases
))
1726 bases
[bases_count
++] = argv
[i
];
1728 if (argc
- i
!= 3) /* "--" "<head>" "<remote>" */
1729 die("Not handling anything other than two heads merge.");
1733 branch1
= argv
[++i
];
1734 branch2
= argv
[++i
];
1736 h1
= get_ref(branch1
);
1737 h2
= get_ref(branch2
);
1739 branch1
= better_branch_name(branch1
);
1740 branch2
= better_branch_name(branch2
);
1743 printf("Merging %s with %s\n", branch1
, branch2
);
1745 index_fd
= hold_locked_index(lock
, 1);
1747 for (i
= 0; i
< bases_count
; i
++) {
1748 struct commit
*ancestor
= get_ref(bases
[i
]);
1749 ca
= commit_list_insert(ancestor
, &ca
);
1751 clean
= merge(h1
, h2
, branch1
, branch2
, ca
, &result
);
1753 if (active_cache_changed
&&
1754 (write_cache(index_fd
, active_cache
, active_nr
) ||
1755 close(index_fd
) || commit_locked_index(lock
)))
1756 die ("unable to write %s", get_index_file());
1758 return clean
? 0: 1;