2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
9 #include "cache-tree.h"
13 #include "tree-walk.h"
17 #include "unpack-trees.h"
18 #include "string-list.h"
19 #include "xdiff-interface.h"
22 #include "merge-recursive.h"
24 #include "submodule.h"
26 static void flush_output(struct merge_options
*o
)
28 if (o
->buffer_output
< 2 && o
->obuf
.len
) {
29 fputs(o
->obuf
.buf
, stdout
);
30 strbuf_reset(&o
->obuf
);
34 static int err(struct merge_options
*o
, const char *err
, ...)
38 if (o
->buffer_output
< 2)
41 strbuf_complete(&o
->obuf
, '\n');
42 strbuf_addstr(&o
->obuf
, "error: ");
44 va_start(params
, err
);
45 strbuf_vaddf(&o
->obuf
, err
, params
);
47 if (o
->buffer_output
> 1)
48 strbuf_addch(&o
->obuf
, '\n');
50 error("%s", o
->obuf
.buf
);
51 strbuf_reset(&o
->obuf
);
57 static struct tree
*shift_tree_object(struct tree
*one
, struct tree
*two
,
58 const char *subtree_shift
)
60 struct object_id shifted
;
62 if (!*subtree_shift
) {
63 shift_tree(&one
->object
.oid
, &two
->object
.oid
, &shifted
, 0);
65 shift_tree_by(&one
->object
.oid
, &two
->object
.oid
, &shifted
,
68 if (!oidcmp(&two
->object
.oid
, &shifted
))
70 return lookup_tree(shifted
.hash
);
73 static struct commit
*make_virtual_commit(struct tree
*tree
, const char *comment
)
75 struct commit
*commit
= alloc_commit_node();
77 set_merge_remote_desc(commit
, comment
, (struct object
*)commit
);
79 commit
->object
.parsed
= 1;
84 * Since we use get_tree_entry(), which does not put the read object into
85 * the object pool, we cannot rely on a == b.
87 static int oid_eq(const struct object_id
*a
, const struct object_id
*b
)
91 return a
&& b
&& oidcmp(a
, b
) == 0;
97 RENAME_ONE_FILE_TO_ONE
,
98 RENAME_ONE_FILE_TO_TWO
,
99 RENAME_TWO_FILES_TO_ONE
102 struct rename_conflict_info
{
103 enum rename_type rename_type
;
104 struct diff_filepair
*pair1
;
105 struct diff_filepair
*pair2
;
108 struct stage_data
*dst_entry1
;
109 struct stage_data
*dst_entry2
;
110 struct diff_filespec ren1_other
;
111 struct diff_filespec ren2_other
;
115 * Since we want to write the index eventually, we cannot reuse the index
116 * for these (temporary) data.
121 struct object_id oid
;
123 struct rename_conflict_info
*rename_conflict_info
;
124 unsigned processed
:1;
127 static inline void setup_rename_conflict_info(enum rename_type rename_type
,
128 struct diff_filepair
*pair1
,
129 struct diff_filepair
*pair2
,
132 struct stage_data
*dst_entry1
,
133 struct stage_data
*dst_entry2
,
134 struct merge_options
*o
,
135 struct stage_data
*src_entry1
,
136 struct stage_data
*src_entry2
)
138 struct rename_conflict_info
*ci
= xcalloc(1, sizeof(struct rename_conflict_info
));
139 ci
->rename_type
= rename_type
;
141 ci
->branch1
= branch1
;
142 ci
->branch2
= branch2
;
144 ci
->dst_entry1
= dst_entry1
;
145 dst_entry1
->rename_conflict_info
= ci
;
146 dst_entry1
->processed
= 0;
148 assert(!pair2
== !dst_entry2
);
150 ci
->dst_entry2
= dst_entry2
;
152 dst_entry2
->rename_conflict_info
= ci
;
155 if (rename_type
== RENAME_TWO_FILES_TO_ONE
) {
157 * For each rename, there could have been
158 * modifications on the side of history where that
159 * file was not renamed.
161 int ostage1
= o
->branch1
== branch1
? 3 : 2;
162 int ostage2
= ostage1
^ 1;
164 ci
->ren1_other
.path
= pair1
->one
->path
;
165 oidcpy(&ci
->ren1_other
.oid
, &src_entry1
->stages
[ostage1
].oid
);
166 ci
->ren1_other
.mode
= src_entry1
->stages
[ostage1
].mode
;
168 ci
->ren2_other
.path
= pair2
->one
->path
;
169 oidcpy(&ci
->ren2_other
.oid
, &src_entry2
->stages
[ostage2
].oid
);
170 ci
->ren2_other
.mode
= src_entry2
->stages
[ostage2
].mode
;
174 static int show(struct merge_options
*o
, int v
)
176 return (!o
->call_depth
&& o
->verbosity
>= v
) || o
->verbosity
>= 5;
179 __attribute__((format (printf
, 3, 4)))
180 static void output(struct merge_options
*o
, int v
, const char *fmt
, ...)
187 strbuf_addchars(&o
->obuf
, ' ', o
->call_depth
* 2);
190 strbuf_vaddf(&o
->obuf
, fmt
, ap
);
193 strbuf_addch(&o
->obuf
, '\n');
194 if (!o
->buffer_output
)
198 static void output_commit_title(struct merge_options
*o
, struct commit
*commit
)
200 strbuf_addchars(&o
->obuf
, ' ', o
->call_depth
* 2);
202 strbuf_addf(&o
->obuf
, "virtual %s\n",
203 merge_remote_util(commit
)->name
);
205 strbuf_add_unique_abbrev(&o
->obuf
, commit
->object
.oid
.hash
,
207 strbuf_addch(&o
->obuf
, ' ');
208 if (parse_commit(commit
) != 0)
209 strbuf_addstr(&o
->obuf
, _("(bad commit)\n"));
212 const char *msg
= get_commit_buffer(commit
, NULL
);
213 int len
= find_commit_subject(msg
, &title
);
215 strbuf_addf(&o
->obuf
, "%.*s\n", len
, title
);
216 unuse_commit_buffer(commit
, msg
);
222 static int add_cacheinfo(struct merge_options
*o
,
223 unsigned int mode
, const struct object_id
*oid
,
224 const char *path
, int stage
, int refresh
, int options
)
226 struct cache_entry
*ce
;
229 ce
= make_cache_entry(mode
, oid
? oid
->hash
: null_sha1
, path
, stage
, 0);
231 return err(o
, _("addinfo_cache failed for path '%s'"), path
);
233 ret
= add_cache_entry(ce
, options
);
235 struct cache_entry
*nce
;
237 nce
= refresh_cache_entry(ce
, CE_MATCH_REFRESH
| CE_MATCH_IGNORE_MISSING
);
239 ret
= add_cache_entry(nce
, options
);
244 static void init_tree_desc_from_tree(struct tree_desc
*desc
, struct tree
*tree
)
247 init_tree_desc(desc
, tree
->buffer
, tree
->size
);
250 static int git_merge_trees(int index_only
,
256 struct tree_desc t
[3];
257 struct unpack_trees_options opts
;
259 memset(&opts
, 0, sizeof(opts
));
266 opts
.fn
= threeway_merge
;
267 opts
.src_index
= &the_index
;
268 opts
.dst_index
= &the_index
;
269 setup_unpack_trees_porcelain(&opts
, "merge");
271 init_tree_desc_from_tree(t
+0, common
);
272 init_tree_desc_from_tree(t
+1, head
);
273 init_tree_desc_from_tree(t
+2, merge
);
275 rc
= unpack_trees(3, t
, &opts
);
276 cache_tree_free(&active_cache_tree
);
280 struct tree
*write_tree_from_memory(struct merge_options
*o
)
282 struct tree
*result
= NULL
;
284 if (unmerged_cache()) {
286 fprintf(stderr
, "BUG: There are unmerged index entries:\n");
287 for (i
= 0; i
< active_nr
; i
++) {
288 const struct cache_entry
*ce
= active_cache
[i
];
290 fprintf(stderr
, "BUG: %d %.*s\n", ce_stage(ce
),
291 (int)ce_namelen(ce
), ce
->name
);
293 die("BUG: unmerged index entries in merge-recursive.c");
296 if (!active_cache_tree
)
297 active_cache_tree
= cache_tree();
299 if (!cache_tree_fully_valid(active_cache_tree
) &&
300 cache_tree_update(&the_index
, 0) < 0) {
301 err(o
, _("error building trees"));
305 result
= lookup_tree(active_cache_tree
->sha1
);
310 static int save_files_dirs(const unsigned char *sha1
,
311 struct strbuf
*base
, const char *path
,
312 unsigned int mode
, int stage
, void *context
)
314 int baselen
= base
->len
;
315 struct merge_options
*o
= context
;
317 strbuf_addstr(base
, path
);
320 string_list_insert(&o
->current_directory_set
, base
->buf
);
322 string_list_insert(&o
->current_file_set
, base
->buf
);
324 strbuf_setlen(base
, baselen
);
325 return (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
328 static int get_files_dirs(struct merge_options
*o
, struct tree
*tree
)
331 struct pathspec match_all
;
332 memset(&match_all
, 0, sizeof(match_all
));
333 if (read_tree_recursive(tree
, "", 0, 0, &match_all
, save_files_dirs
, o
))
335 n
= o
->current_file_set
.nr
+ o
->current_directory_set
.nr
;
340 * Returns an index_entry instance which doesn't have to correspond to
341 * a real cache entry in Git's index.
343 static struct stage_data
*insert_stage_data(const char *path
,
344 struct tree
*o
, struct tree
*a
, struct tree
*b
,
345 struct string_list
*entries
)
347 struct string_list_item
*item
;
348 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
349 get_tree_entry(o
->object
.oid
.hash
, path
,
350 e
->stages
[1].oid
.hash
, &e
->stages
[1].mode
);
351 get_tree_entry(a
->object
.oid
.hash
, path
,
352 e
->stages
[2].oid
.hash
, &e
->stages
[2].mode
);
353 get_tree_entry(b
->object
.oid
.hash
, path
,
354 e
->stages
[3].oid
.hash
, &e
->stages
[3].mode
);
355 item
= string_list_insert(entries
, path
);
361 * Create a dictionary mapping file names to stage_data objects. The
362 * dictionary contains one entry for every path with a non-zero stage entry.
364 static struct string_list
*get_unmerged(void)
366 struct string_list
*unmerged
= xcalloc(1, sizeof(struct string_list
));
369 unmerged
->strdup_strings
= 1;
371 for (i
= 0; i
< active_nr
; i
++) {
372 struct string_list_item
*item
;
373 struct stage_data
*e
;
374 const struct cache_entry
*ce
= active_cache
[i
];
378 item
= string_list_lookup(unmerged
, ce
->name
);
380 item
= string_list_insert(unmerged
, ce
->name
);
381 item
->util
= xcalloc(1, sizeof(struct stage_data
));
384 e
->stages
[ce_stage(ce
)].mode
= ce
->ce_mode
;
385 oidcpy(&e
->stages
[ce_stage(ce
)].oid
, &ce
->oid
);
391 static int string_list_df_name_compare(const char *one
, const char *two
)
393 int onelen
= strlen(one
);
394 int twolen
= strlen(two
);
396 * Here we only care that entries for D/F conflicts are
397 * adjacent, in particular with the file of the D/F conflict
398 * appearing before files below the corresponding directory.
399 * The order of the rest of the list is irrelevant for us.
401 * To achieve this, we sort with df_name_compare and provide
402 * the mode S_IFDIR so that D/F conflicts will sort correctly.
403 * We use the mode S_IFDIR for everything else for simplicity,
404 * since in other cases any changes in their order due to
405 * sorting cause no problems for us.
407 int cmp
= df_name_compare(one
, onelen
, S_IFDIR
,
408 two
, twolen
, S_IFDIR
);
410 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
411 * that 'foo' comes before 'foo/bar'.
415 return onelen
- twolen
;
418 static void record_df_conflict_files(struct merge_options
*o
,
419 struct string_list
*entries
)
421 /* If there is a D/F conflict and the file for such a conflict
422 * currently exist in the working tree, we want to allow it to be
423 * removed to make room for the corresponding directory if needed.
424 * The files underneath the directories of such D/F conflicts will
425 * be processed before the corresponding file involved in the D/F
426 * conflict. If the D/F directory ends up being removed by the
427 * merge, then we won't have to touch the D/F file. If the D/F
428 * directory needs to be written to the working copy, then the D/F
429 * file will simply be removed (in make_room_for_path()) to make
430 * room for the necessary paths. Note that if both the directory
431 * and the file need to be present, then the D/F file will be
432 * reinstated with a new unique name at the time it is processed.
434 struct string_list df_sorted_entries
= STRING_LIST_INIT_NODUP
;
435 const char *last_file
= NULL
;
440 * If we're merging merge-bases, we don't want to bother with
441 * any working directory changes.
446 /* Ensure D/F conflicts are adjacent in the entries list. */
447 for (i
= 0; i
< entries
->nr
; i
++) {
448 struct string_list_item
*next
= &entries
->items
[i
];
449 string_list_append(&df_sorted_entries
, next
->string
)->util
=
452 df_sorted_entries
.cmp
= string_list_df_name_compare
;
453 string_list_sort(&df_sorted_entries
);
455 string_list_clear(&o
->df_conflict_file_set
, 1);
456 for (i
= 0; i
< df_sorted_entries
.nr
; i
++) {
457 const char *path
= df_sorted_entries
.items
[i
].string
;
458 int len
= strlen(path
);
459 struct stage_data
*e
= df_sorted_entries
.items
[i
].util
;
462 * Check if last_file & path correspond to a D/F conflict;
463 * i.e. whether path is last_file+'/'+<something>.
464 * If so, record that it's okay to remove last_file to make
465 * room for path and friends if needed.
469 memcmp(path
, last_file
, last_len
) == 0 &&
470 path
[last_len
] == '/') {
471 string_list_insert(&o
->df_conflict_file_set
, last_file
);
475 * Determine whether path could exist as a file in the
476 * working directory as a possible D/F conflict. This
477 * will only occur when it exists in stage 2 as a
480 if (S_ISREG(e
->stages
[2].mode
) || S_ISLNK(e
->stages
[2].mode
)) {
487 string_list_clear(&df_sorted_entries
, 0);
491 struct diff_filepair
*pair
;
492 struct stage_data
*src_entry
;
493 struct stage_data
*dst_entry
;
494 unsigned processed
:1;
498 * Get information of all renames which occurred between 'o_tree' and
499 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
500 * 'b_tree') to be able to associate the correct cache entries with
501 * the rename information. 'tree' is always equal to either a_tree or b_tree.
503 static struct string_list
*get_renames(struct merge_options
*o
,
508 struct string_list
*entries
)
511 struct string_list
*renames
;
512 struct diff_options opts
;
514 renames
= xcalloc(1, sizeof(struct string_list
));
515 if (!o
->detect_rename
)
519 DIFF_OPT_SET(&opts
, RECURSIVE
);
520 DIFF_OPT_CLR(&opts
, RENAME_EMPTY
);
521 opts
.detect_rename
= DIFF_DETECT_RENAME
;
522 opts
.rename_limit
= o
->merge_rename_limit
>= 0 ? o
->merge_rename_limit
:
523 o
->diff_rename_limit
>= 0 ? o
->diff_rename_limit
:
525 opts
.rename_score
= o
->rename_score
;
526 opts
.show_rename_progress
= o
->show_rename_progress
;
527 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
528 diff_setup_done(&opts
);
529 diff_tree_sha1(o_tree
->object
.oid
.hash
, tree
->object
.oid
.hash
, "", &opts
);
531 if (opts
.needed_rename_limit
> o
->needed_rename_limit
)
532 o
->needed_rename_limit
= opts
.needed_rename_limit
;
533 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
534 struct string_list_item
*item
;
536 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
537 if (pair
->status
!= 'R') {
538 diff_free_filepair(pair
);
541 re
= xmalloc(sizeof(*re
));
544 item
= string_list_lookup(entries
, re
->pair
->one
->path
);
546 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
547 o_tree
, a_tree
, b_tree
, entries
);
549 re
->src_entry
= item
->util
;
551 item
= string_list_lookup(entries
, re
->pair
->two
->path
);
553 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
554 o_tree
, a_tree
, b_tree
, entries
);
556 re
->dst_entry
= item
->util
;
557 item
= string_list_insert(renames
, pair
->one
->path
);
560 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
561 diff_queued_diff
.nr
= 0;
566 static int update_stages(struct merge_options
*opt
, const char *path
,
567 const struct diff_filespec
*o
,
568 const struct diff_filespec
*a
,
569 const struct diff_filespec
*b
)
573 * NOTE: It is usually a bad idea to call update_stages on a path
574 * before calling update_file on that same path, since it can
575 * sometimes lead to spurious "refusing to lose untracked file..."
576 * messages from update_file (via make_room_for path via
577 * would_lose_untracked). Instead, reverse the order of the calls
578 * (executing update_file first and then update_stages).
581 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_SKIP_DFCHECK
;
583 if (remove_file_from_cache(path
))
586 if (add_cacheinfo(opt
, o
->mode
, &o
->oid
, path
, 1, 0, options
))
589 if (add_cacheinfo(opt
, a
->mode
, &a
->oid
, path
, 2, 0, options
))
592 if (add_cacheinfo(opt
, b
->mode
, &b
->oid
, path
, 3, 0, options
))
597 static void update_entry(struct stage_data
*entry
,
598 struct diff_filespec
*o
,
599 struct diff_filespec
*a
,
600 struct diff_filespec
*b
)
602 entry
->processed
= 0;
603 entry
->stages
[1].mode
= o
->mode
;
604 entry
->stages
[2].mode
= a
->mode
;
605 entry
->stages
[3].mode
= b
->mode
;
606 oidcpy(&entry
->stages
[1].oid
, &o
->oid
);
607 oidcpy(&entry
->stages
[2].oid
, &a
->oid
);
608 oidcpy(&entry
->stages
[3].oid
, &b
->oid
);
611 static int remove_file(struct merge_options
*o
, int clean
,
612 const char *path
, int no_wd
)
614 int update_cache
= o
->call_depth
|| clean
;
615 int update_working_directory
= !o
->call_depth
&& !no_wd
;
618 if (remove_file_from_cache(path
))
621 if (update_working_directory
) {
623 struct cache_entry
*ce
;
624 ce
= cache_file_exists(path
, strlen(path
), ignore_case
);
625 if (ce
&& ce_stage(ce
) == 0)
628 if (remove_path(path
))
634 /* add a string to a strbuf, but converting "/" to "_" */
635 static void add_flattened_path(struct strbuf
*out
, const char *s
)
638 strbuf_addstr(out
, s
);
639 for (; i
< out
->len
; i
++)
640 if (out
->buf
[i
] == '/')
644 static char *unique_path(struct merge_options
*o
, const char *path
, const char *branch
)
646 struct strbuf newpath
= STRBUF_INIT
;
650 strbuf_addf(&newpath
, "%s~", path
);
651 add_flattened_path(&newpath
, branch
);
653 base_len
= newpath
.len
;
654 while (string_list_has_string(&o
->current_file_set
, newpath
.buf
) ||
655 string_list_has_string(&o
->current_directory_set
, newpath
.buf
) ||
656 (!o
->call_depth
&& file_exists(newpath
.buf
))) {
657 strbuf_setlen(&newpath
, base_len
);
658 strbuf_addf(&newpath
, "_%d", suffix
++);
661 string_list_insert(&o
->current_file_set
, newpath
.buf
);
662 return strbuf_detach(&newpath
, NULL
);
666 * Check whether a directory in the index is in the way of an incoming
667 * file. Return 1 if so. If check_working_copy is non-zero, also
668 * check the working directory. If empty_ok is non-zero, also return
669 * 0 in the case where the working-tree dir exists but is empty.
671 static int dir_in_way(const char *path
, int check_working_copy
, int empty_ok
)
674 struct strbuf dirpath
= STRBUF_INIT
;
677 strbuf_addstr(&dirpath
, path
);
678 strbuf_addch(&dirpath
, '/');
680 pos
= cache_name_pos(dirpath
.buf
, dirpath
.len
);
684 if (pos
< active_nr
&&
685 !strncmp(dirpath
.buf
, active_cache
[pos
]->name
, dirpath
.len
)) {
686 strbuf_release(&dirpath
);
690 strbuf_release(&dirpath
);
691 return check_working_copy
&& !lstat(path
, &st
) && S_ISDIR(st
.st_mode
) &&
692 !(empty_ok
&& is_empty_dir(path
));
695 static int was_tracked(const char *path
)
697 int pos
= cache_name_pos(path
, strlen(path
));
700 /* we have been tracking this path */
704 * Look for an unmerged entry for the path,
705 * specifically stage #2, which would indicate
706 * that "our" side before the merge started
707 * had the path tracked (and resulted in a conflict).
710 pos
< active_nr
&& !strcmp(path
, active_cache
[pos
]->name
);
712 if (ce_stage(active_cache
[pos
]) == 2)
717 static int would_lose_untracked(const char *path
)
719 return !was_tracked(path
) && file_exists(path
);
722 static int make_room_for_path(struct merge_options
*o
, const char *path
)
725 const char *msg
= _("failed to create path '%s'%s");
727 /* Unlink any D/F conflict files that are in the way */
728 for (i
= 0; i
< o
->df_conflict_file_set
.nr
; i
++) {
729 const char *df_path
= o
->df_conflict_file_set
.items
[i
].string
;
730 size_t pathlen
= strlen(path
);
731 size_t df_pathlen
= strlen(df_path
);
732 if (df_pathlen
< pathlen
&&
733 path
[df_pathlen
] == '/' &&
734 strncmp(path
, df_path
, df_pathlen
) == 0) {
736 _("Removing %s to make room for subdirectory\n"),
739 unsorted_string_list_delete_item(&o
->df_conflict_file_set
,
745 /* Make sure leading directories are created */
746 status
= safe_create_leading_directories_const(path
);
748 if (status
== SCLD_EXISTS
)
749 /* something else exists */
750 return err(o
, msg
, path
, _(": perhaps a D/F conflict?"));
751 return err(o
, msg
, path
, "");
755 * Do not unlink a file in the work tree if we are not
758 if (would_lose_untracked(path
))
759 return err(o
, _("refusing to lose untracked file at '%s'"),
762 /* Successful unlink is good.. */
765 /* .. and so is no existing file */
768 /* .. but not some other error (who really cares what?) */
769 return err(o
, msg
, path
, _(": perhaps a D/F conflict?"));
772 static int update_file_flags(struct merge_options
*o
,
773 const struct object_id
*oid
,
785 enum object_type type
;
789 if (S_ISGITLINK(mode
)) {
791 * We may later decide to recursively descend into
792 * the submodule directory and update its index
793 * and/or work tree, but we do not do that now.
799 buf
= read_sha1_file(oid
->hash
, &type
, &size
);
801 return err(o
, _("cannot read object %s '%s'"), oid_to_hex(oid
), path
);
802 if (type
!= OBJ_BLOB
) {
803 ret
= err(o
, _("blob expected for %s '%s'"), oid_to_hex(oid
), path
);
807 struct strbuf strbuf
= STRBUF_INIT
;
808 if (convert_to_working_tree(path
, buf
, size
, &strbuf
)) {
811 buf
= strbuf_detach(&strbuf
, NULL
);
815 if (make_room_for_path(o
, path
) < 0) {
819 if (S_ISREG(mode
) || (!has_symlinks
&& S_ISLNK(mode
))) {
825 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
827 ret
= err(o
, _("failed to open '%s': %s"),
828 path
, strerror(errno
));
831 write_in_full(fd
, buf
, size
);
833 } else if (S_ISLNK(mode
)) {
834 char *lnk
= xmemdupz(buf
, size
);
835 safe_create_leading_directories_const(path
);
837 if (symlink(lnk
, path
))
838 ret
= err(o
, _("failed to symlink '%s': %s"),
839 path
, strerror(errno
));
843 _("do not know what to do with %06o %s '%s'"),
844 mode
, oid_to_hex(oid
), path
);
849 if (!ret
&& update_cache
)
850 add_cacheinfo(o
, mode
, oid
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
854 static int update_file(struct merge_options
*o
,
856 const struct object_id
*oid
,
860 return update_file_flags(o
, oid
, mode
, path
, o
->call_depth
|| clean
, !o
->call_depth
);
863 /* Low level file merging, update and removal */
865 struct merge_file_info
{
866 struct object_id oid
;
872 static int merge_3way(struct merge_options
*o
,
873 mmbuffer_t
*result_buf
,
874 const struct diff_filespec
*one
,
875 const struct diff_filespec
*a
,
876 const struct diff_filespec
*b
,
880 mmfile_t orig
, src1
, src2
;
881 struct ll_merge_options ll_opts
= {0};
882 char *base_name
, *name1
, *name2
;
885 ll_opts
.renormalize
= o
->renormalize
;
886 ll_opts
.xdl_opts
= o
->xdl_opts
;
889 ll_opts
.virtual_ancestor
= 1;
892 switch (o
->recursive_variant
) {
893 case MERGE_RECURSIVE_OURS
:
894 ll_opts
.variant
= XDL_MERGE_FAVOR_OURS
;
896 case MERGE_RECURSIVE_THEIRS
:
897 ll_opts
.variant
= XDL_MERGE_FAVOR_THEIRS
;
905 if (strcmp(a
->path
, b
->path
) ||
906 (o
->ancestor
!= NULL
&& strcmp(a
->path
, one
->path
) != 0)) {
907 base_name
= o
->ancestor
== NULL
? NULL
:
908 mkpathdup("%s:%s", o
->ancestor
, one
->path
);
909 name1
= mkpathdup("%s:%s", branch1
, a
->path
);
910 name2
= mkpathdup("%s:%s", branch2
, b
->path
);
912 base_name
= o
->ancestor
== NULL
? NULL
:
913 mkpathdup("%s", o
->ancestor
);
914 name1
= mkpathdup("%s", branch1
);
915 name2
= mkpathdup("%s", branch2
);
918 read_mmblob(&orig
, &one
->oid
);
919 read_mmblob(&src1
, &a
->oid
);
920 read_mmblob(&src2
, &b
->oid
);
922 merge_status
= ll_merge(result_buf
, a
->path
, &orig
, base_name
,
923 &src1
, name1
, &src2
, name2
, &ll_opts
);
934 static int merge_file_1(struct merge_options
*o
,
935 const struct diff_filespec
*one
,
936 const struct diff_filespec
*a
,
937 const struct diff_filespec
*b
,
940 struct merge_file_info
*result
)
945 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
947 if (S_ISREG(a
->mode
)) {
948 result
->mode
= a
->mode
;
949 oidcpy(&result
->oid
, &a
->oid
);
951 result
->mode
= b
->mode
;
952 oidcpy(&result
->oid
, &b
->oid
);
955 if (!oid_eq(&a
->oid
, &one
->oid
) && !oid_eq(&b
->oid
, &one
->oid
))
961 if (a
->mode
== b
->mode
|| a
->mode
== one
->mode
)
962 result
->mode
= b
->mode
;
964 result
->mode
= a
->mode
;
965 if (b
->mode
!= one
->mode
) {
971 if (oid_eq(&a
->oid
, &b
->oid
) || oid_eq(&a
->oid
, &one
->oid
))
972 oidcpy(&result
->oid
, &b
->oid
);
973 else if (oid_eq(&b
->oid
, &one
->oid
))
974 oidcpy(&result
->oid
, &a
->oid
);
975 else if (S_ISREG(a
->mode
)) {
976 mmbuffer_t result_buf
;
977 int ret
= 0, merge_status
;
979 merge_status
= merge_3way(o
, &result_buf
, one
, a
, b
,
982 if ((merge_status
< 0) || !result_buf
.ptr
)
983 ret
= err(o
, _("Failed to execute internal merge"));
985 if (!ret
&& write_sha1_file(result_buf
.ptr
, result_buf
.size
,
986 blob_type
, result
->oid
.hash
))
987 ret
= err(o
, _("Unable to add %s to database"),
990 free(result_buf
.ptr
);
993 result
->clean
= (merge_status
== 0);
994 } else if (S_ISGITLINK(a
->mode
)) {
995 result
->clean
= merge_submodule(result
->oid
.hash
,
1001 } else if (S_ISLNK(a
->mode
)) {
1002 oidcpy(&result
->oid
, &a
->oid
);
1004 if (!oid_eq(&a
->oid
, &b
->oid
))
1007 die("BUG: unsupported object type in the tree");
1013 static int merge_file_special_markers(struct merge_options
*o
,
1014 const struct diff_filespec
*one
,
1015 const struct diff_filespec
*a
,
1016 const struct diff_filespec
*b
,
1017 const char *branch1
,
1018 const char *filename1
,
1019 const char *branch2
,
1020 const char *filename2
,
1021 struct merge_file_info
*mfi
)
1028 side1
= xstrfmt("%s:%s", branch1
, filename1
);
1030 side2
= xstrfmt("%s:%s", branch2
, filename2
);
1032 ret
= merge_file_1(o
, one
, a
, b
,
1033 side1
? side1
: branch1
,
1034 side2
? side2
: branch2
, mfi
);
1040 static int merge_file_one(struct merge_options
*o
,
1042 const struct object_id
*o_oid
, int o_mode
,
1043 const struct object_id
*a_oid
, int a_mode
,
1044 const struct object_id
*b_oid
, int b_mode
,
1045 const char *branch1
,
1046 const char *branch2
,
1047 struct merge_file_info
*mfi
)
1049 struct diff_filespec one
, a
, b
;
1051 one
.path
= a
.path
= b
.path
= (char *)path
;
1052 oidcpy(&one
.oid
, o_oid
);
1054 oidcpy(&a
.oid
, a_oid
);
1056 oidcpy(&b
.oid
, b_oid
);
1058 return merge_file_1(o
, &one
, &a
, &b
, branch1
, branch2
, mfi
);
1061 static int handle_change_delete(struct merge_options
*o
,
1063 const struct object_id
*o_oid
, int o_mode
,
1064 const struct object_id
*a_oid
, int a_mode
,
1065 const struct object_id
*b_oid
, int b_mode
,
1066 const char *change
, const char *change_past
)
1068 char *renamed
= NULL
;
1070 if (dir_in_way(path
, !o
->call_depth
, 0)) {
1071 renamed
= unique_path(o
, path
, a_oid
? o
->branch1
: o
->branch2
);
1074 if (o
->call_depth
) {
1076 * We cannot arbitrarily accept either a_sha or b_sha as
1077 * correct; since there is no true "middle point" between
1078 * them, simply reuse the base version for virtual merge base.
1080 ret
= remove_file_from_cache(path
);
1082 ret
= update_file(o
, 0, o_oid
, o_mode
,
1083 renamed
? renamed
: path
);
1084 } else if (!a_oid
) {
1086 output(o
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1087 "and %s in %s. Version %s of %s left in tree."),
1088 change
, path
, o
->branch1
, change_past
,
1089 o
->branch2
, o
->branch2
, path
);
1090 ret
= update_file(o
, 0, b_oid
, b_mode
, path
);
1092 output(o
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1093 "and %s in %s. Version %s of %s left in tree at %s."),
1094 change
, path
, o
->branch1
, change_past
,
1095 o
->branch2
, o
->branch2
, path
, renamed
);
1096 ret
= update_file(o
, 0, b_oid
, b_mode
, renamed
);
1100 output(o
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1101 "and %s in %s. Version %s of %s left in tree."),
1102 change
, path
, o
->branch2
, change_past
,
1103 o
->branch1
, o
->branch1
, path
);
1105 output(o
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1106 "and %s in %s. Version %s of %s left in tree at %s."),
1107 change
, path
, o
->branch2
, change_past
,
1108 o
->branch1
, o
->branch1
, path
, renamed
);
1109 ret
= update_file(o
, 0, a_oid
, a_mode
, renamed
);
1112 * No need to call update_file() on path when !renamed, since
1113 * that would needlessly touch path. We could call
1114 * update_file_flags() with update_cache=0 and update_wd=0,
1115 * but that's a no-op.
1123 static int conflict_rename_delete(struct merge_options
*o
,
1124 struct diff_filepair
*pair
,
1125 const char *rename_branch
,
1126 const char *other_branch
)
1128 const struct diff_filespec
*orig
= pair
->one
;
1129 const struct diff_filespec
*dest
= pair
->two
;
1130 const struct object_id
*a_oid
= NULL
;
1131 const struct object_id
*b_oid
= NULL
;
1135 if (rename_branch
== o
->branch1
) {
1137 a_mode
= dest
->mode
;
1140 b_mode
= dest
->mode
;
1143 if (handle_change_delete(o
,
1144 o
->call_depth
? orig
->path
: dest
->path
,
1145 &orig
->oid
, orig
->mode
,
1148 _("rename"), _("renamed")))
1152 return remove_file_from_cache(dest
->path
);
1154 return update_stages(o
, dest
->path
, NULL
,
1155 rename_branch
== o
->branch1
? dest
: NULL
,
1156 rename_branch
== o
->branch1
? NULL
: dest
);
1159 static struct diff_filespec
*filespec_from_entry(struct diff_filespec
*target
,
1160 struct stage_data
*entry
,
1163 struct object_id
*oid
= &entry
->stages
[stage
].oid
;
1164 unsigned mode
= entry
->stages
[stage
].mode
;
1165 if (mode
== 0 || is_null_oid(oid
))
1167 oidcpy(&target
->oid
, oid
);
1168 target
->mode
= mode
;
1172 static int handle_file(struct merge_options
*o
,
1173 struct diff_filespec
*rename
,
1175 struct rename_conflict_info
*ci
)
1177 char *dst_name
= rename
->path
;
1178 struct stage_data
*dst_entry
;
1179 const char *cur_branch
, *other_branch
;
1180 struct diff_filespec other
;
1181 struct diff_filespec
*add
;
1185 dst_entry
= ci
->dst_entry1
;
1186 cur_branch
= ci
->branch1
;
1187 other_branch
= ci
->branch2
;
1189 dst_entry
= ci
->dst_entry2
;
1190 cur_branch
= ci
->branch2
;
1191 other_branch
= ci
->branch1
;
1194 add
= filespec_from_entry(&other
, dst_entry
, stage
^ 1);
1196 char *add_name
= unique_path(o
, rename
->path
, other_branch
);
1197 if (update_file(o
, 0, &add
->oid
, add
->mode
, add_name
))
1200 remove_file(o
, 0, rename
->path
, 0);
1201 dst_name
= unique_path(o
, rename
->path
, cur_branch
);
1203 if (dir_in_way(rename
->path
, !o
->call_depth
, 0)) {
1204 dst_name
= unique_path(o
, rename
->path
, cur_branch
);
1205 output(o
, 1, _("%s is a directory in %s adding as %s instead"),
1206 rename
->path
, other_branch
, dst_name
);
1209 if ((ret
= update_file(o
, 0, &rename
->oid
, rename
->mode
, dst_name
)))
1210 ; /* fall through, do allow dst_name to be released */
1211 else if (stage
== 2)
1212 ret
= update_stages(o
, rename
->path
, NULL
, rename
, add
);
1214 ret
= update_stages(o
, rename
->path
, NULL
, add
, rename
);
1216 if (dst_name
!= rename
->path
)
1222 static int conflict_rename_rename_1to2(struct merge_options
*o
,
1223 struct rename_conflict_info
*ci
)
1225 /* One file was renamed in both branches, but to different names. */
1226 struct diff_filespec
*one
= ci
->pair1
->one
;
1227 struct diff_filespec
*a
= ci
->pair1
->two
;
1228 struct diff_filespec
*b
= ci
->pair2
->two
;
1230 output(o
, 1, _("CONFLICT (rename/rename): "
1231 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1232 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1233 one
->path
, a
->path
, ci
->branch1
,
1234 one
->path
, b
->path
, ci
->branch2
,
1235 o
->call_depth
? _(" (left unresolved)") : "");
1236 if (o
->call_depth
) {
1237 struct merge_file_info mfi
;
1238 struct diff_filespec other
;
1239 struct diff_filespec
*add
;
1240 if (merge_file_one(o
, one
->path
,
1241 &one
->oid
, one
->mode
,
1244 ci
->branch1
, ci
->branch2
, &mfi
))
1248 * FIXME: For rename/add-source conflicts (if we could detect
1249 * such), this is wrong. We should instead find a unique
1250 * pathname and then either rename the add-source file to that
1251 * unique path, or use that unique path instead of src here.
1253 if (update_file(o
, 0, &mfi
.oid
, mfi
.mode
, one
->path
))
1257 * Above, we put the merged content at the merge-base's
1258 * path. Now we usually need to delete both a->path and
1259 * b->path. However, the rename on each side of the merge
1260 * could also be involved in a rename/add conflict. In
1261 * such cases, we should keep the added file around,
1262 * resolving the conflict at that path in its favor.
1264 add
= filespec_from_entry(&other
, ci
->dst_entry1
, 2 ^ 1);
1266 if (update_file(o
, 0, &add
->oid
, add
->mode
, a
->path
))
1270 remove_file_from_cache(a
->path
);
1271 add
= filespec_from_entry(&other
, ci
->dst_entry2
, 3 ^ 1);
1273 if (update_file(o
, 0, &add
->oid
, add
->mode
, b
->path
))
1277 remove_file_from_cache(b
->path
);
1278 } else if (handle_file(o
, a
, 2, ci
) || handle_file(o
, b
, 3, ci
))
1284 static int conflict_rename_rename_2to1(struct merge_options
*o
,
1285 struct rename_conflict_info
*ci
)
1287 /* Two files, a & b, were renamed to the same thing, c. */
1288 struct diff_filespec
*a
= ci
->pair1
->one
;
1289 struct diff_filespec
*b
= ci
->pair2
->one
;
1290 struct diff_filespec
*c1
= ci
->pair1
->two
;
1291 struct diff_filespec
*c2
= ci
->pair2
->two
;
1292 char *path
= c1
->path
; /* == c2->path */
1293 struct merge_file_info mfi_c1
;
1294 struct merge_file_info mfi_c2
;
1297 output(o
, 1, _("CONFLICT (rename/rename): "
1298 "Rename %s->%s in %s. "
1299 "Rename %s->%s in %s"),
1300 a
->path
, c1
->path
, ci
->branch1
,
1301 b
->path
, c2
->path
, ci
->branch2
);
1303 remove_file(o
, 1, a
->path
, o
->call_depth
|| would_lose_untracked(a
->path
));
1304 remove_file(o
, 1, b
->path
, o
->call_depth
|| would_lose_untracked(b
->path
));
1306 if (merge_file_special_markers(o
, a
, c1
, &ci
->ren1_other
,
1307 o
->branch1
, c1
->path
,
1308 o
->branch2
, ci
->ren1_other
.path
, &mfi_c1
) ||
1309 merge_file_special_markers(o
, b
, &ci
->ren2_other
, c2
,
1310 o
->branch1
, ci
->ren2_other
.path
,
1311 o
->branch2
, c2
->path
, &mfi_c2
))
1314 if (o
->call_depth
) {
1316 * If mfi_c1.clean && mfi_c2.clean, then it might make
1317 * sense to do a two-way merge of those results. But, I
1318 * think in all cases, it makes sense to have the virtual
1319 * merge base just undo the renames; they can be detected
1320 * again later for the non-recursive merge.
1322 remove_file(o
, 0, path
, 0);
1323 ret
= update_file(o
, 0, &mfi_c1
.oid
, mfi_c1
.mode
, a
->path
);
1325 ret
= update_file(o
, 0, &mfi_c2
.oid
, mfi_c2
.mode
,
1328 char *new_path1
= unique_path(o
, path
, ci
->branch1
);
1329 char *new_path2
= unique_path(o
, path
, ci
->branch2
);
1330 output(o
, 1, _("Renaming %s to %s and %s to %s instead"),
1331 a
->path
, new_path1
, b
->path
, new_path2
);
1332 remove_file(o
, 0, path
, 0);
1333 ret
= update_file(o
, 0, &mfi_c1
.oid
, mfi_c1
.mode
, new_path1
);
1335 ret
= update_file(o
, 0, &mfi_c2
.oid
, mfi_c2
.mode
,
1344 static int process_renames(struct merge_options
*o
,
1345 struct string_list
*a_renames
,
1346 struct string_list
*b_renames
)
1348 int clean_merge
= 1, i
, j
;
1349 struct string_list a_by_dst
= STRING_LIST_INIT_NODUP
;
1350 struct string_list b_by_dst
= STRING_LIST_INIT_NODUP
;
1351 const struct rename
*sre
;
1353 for (i
= 0; i
< a_renames
->nr
; i
++) {
1354 sre
= a_renames
->items
[i
].util
;
1355 string_list_insert(&a_by_dst
, sre
->pair
->two
->path
)->util
1358 for (i
= 0; i
< b_renames
->nr
; i
++) {
1359 sre
= b_renames
->items
[i
].util
;
1360 string_list_insert(&b_by_dst
, sre
->pair
->two
->path
)->util
1364 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
1365 struct string_list
*renames1
, *renames2Dst
;
1366 struct rename
*ren1
= NULL
, *ren2
= NULL
;
1367 const char *branch1
, *branch2
;
1368 const char *ren1_src
, *ren1_dst
;
1369 struct string_list_item
*lookup
;
1371 if (i
>= a_renames
->nr
) {
1372 ren2
= b_renames
->items
[j
++].util
;
1373 } else if (j
>= b_renames
->nr
) {
1374 ren1
= a_renames
->items
[i
++].util
;
1376 int compare
= strcmp(a_renames
->items
[i
].string
,
1377 b_renames
->items
[j
].string
);
1379 ren1
= a_renames
->items
[i
++].util
;
1381 ren2
= b_renames
->items
[j
++].util
;
1384 /* TODO: refactor, so that 1/2 are not needed */
1386 renames1
= a_renames
;
1387 renames2Dst
= &b_by_dst
;
1388 branch1
= o
->branch1
;
1389 branch2
= o
->branch2
;
1392 renames1
= b_renames
;
1393 renames2Dst
= &a_by_dst
;
1394 branch1
= o
->branch2
;
1395 branch2
= o
->branch1
;
1401 if (ren1
->processed
)
1403 ren1
->processed
= 1;
1404 ren1
->dst_entry
->processed
= 1;
1405 /* BUG: We should only mark src_entry as processed if we
1406 * are not dealing with a rename + add-source case.
1408 ren1
->src_entry
->processed
= 1;
1410 ren1_src
= ren1
->pair
->one
->path
;
1411 ren1_dst
= ren1
->pair
->two
->path
;
1414 /* One file renamed on both sides */
1415 const char *ren2_src
= ren2
->pair
->one
->path
;
1416 const char *ren2_dst
= ren2
->pair
->two
->path
;
1417 enum rename_type rename_type
;
1418 if (strcmp(ren1_src
, ren2_src
) != 0)
1419 die("BUG: ren1_src != ren2_src");
1420 ren2
->dst_entry
->processed
= 1;
1421 ren2
->processed
= 1;
1422 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
1423 rename_type
= RENAME_ONE_FILE_TO_TWO
;
1426 rename_type
= RENAME_ONE_FILE_TO_ONE
;
1427 /* BUG: We should only remove ren1_src in
1428 * the base stage (think of rename +
1429 * add-source cases).
1431 remove_file(o
, 1, ren1_src
, 1);
1432 update_entry(ren1
->dst_entry
,
1437 setup_rename_conflict_info(rename_type
,
1447 } else if ((lookup
= string_list_lookup(renames2Dst
, ren1_dst
))) {
1448 /* Two different files renamed to the same thing */
1450 ren2
= lookup
->util
;
1451 ren2_dst
= ren2
->pair
->two
->path
;
1452 if (strcmp(ren1_dst
, ren2_dst
) != 0)
1453 die("BUG: ren1_dst != ren2_dst");
1456 ren2
->processed
= 1;
1458 * BUG: We should only mark src_entry as processed
1459 * if we are not dealing with a rename + add-source
1462 ren2
->src_entry
->processed
= 1;
1464 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE
,
1476 /* Renamed in 1, maybe changed in 2 */
1477 /* we only use sha1 and mode of these */
1478 struct diff_filespec src_other
, dst_other
;
1482 * unpack_trees loads entries from common-commit
1483 * into stage 1, from head-commit into stage 2, and
1484 * from merge-commit into stage 3. We keep track
1485 * of which side corresponds to the rename.
1487 int renamed_stage
= a_renames
== renames1
? 2 : 3;
1488 int other_stage
= a_renames
== renames1
? 3 : 2;
1490 /* BUG: We should only remove ren1_src in the base
1491 * stage and in other_stage (think of rename +
1494 remove_file(o
, 1, ren1_src
,
1495 renamed_stage
== 2 || !was_tracked(ren1_src
));
1497 oidcpy(&src_other
.oid
,
1498 &ren1
->src_entry
->stages
[other_stage
].oid
);
1499 src_other
.mode
= ren1
->src_entry
->stages
[other_stage
].mode
;
1500 oidcpy(&dst_other
.oid
,
1501 &ren1
->dst_entry
->stages
[other_stage
].oid
);
1502 dst_other
.mode
= ren1
->dst_entry
->stages
[other_stage
].mode
;
1505 if (oid_eq(&src_other
.oid
, &null_oid
)) {
1506 setup_rename_conflict_info(RENAME_DELETE
,
1516 } else if ((dst_other
.mode
== ren1
->pair
->two
->mode
) &&
1517 oid_eq(&dst_other
.oid
, &ren1
->pair
->two
->oid
)) {
1519 * Added file on the other side identical to
1520 * the file being renamed: clean merge.
1521 * Also, there is no need to overwrite the
1522 * file already in the working copy, so call
1523 * update_file_flags() instead of
1526 if (update_file_flags(o
,
1527 &ren1
->pair
->two
->oid
,
1528 ren1
->pair
->two
->mode
,
1530 1, /* update_cache */
1533 } else if (!oid_eq(&dst_other
.oid
, &null_oid
)) {
1536 output(o
, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
1538 ren1_src
, ren1_dst
, branch1
,
1540 if (o
->call_depth
) {
1541 struct merge_file_info mfi
;
1542 if (merge_file_one(o
, ren1_dst
, &null_oid
, 0,
1543 &ren1
->pair
->two
->oid
,
1544 ren1
->pair
->two
->mode
,
1547 branch1
, branch2
, &mfi
)) {
1549 goto cleanup_and_return
;
1551 output(o
, 1, _("Adding merged %s"), ren1_dst
);
1552 if (update_file(o
, 0, &mfi
.oid
,
1553 mfi
.mode
, ren1_dst
))
1557 char *new_path
= unique_path(o
, ren1_dst
, branch2
);
1558 output(o
, 1, _("Adding as %s instead"), new_path
);
1559 if (update_file(o
, 0, &dst_other
.oid
,
1560 dst_other
.mode
, new_path
))
1567 if (clean_merge
< 0)
1568 goto cleanup_and_return
;
1570 struct diff_filespec
*one
, *a
, *b
;
1571 src_other
.path
= (char *)ren1_src
;
1573 one
= ren1
->pair
->one
;
1574 if (a_renames
== renames1
) {
1575 a
= ren1
->pair
->two
;
1578 b
= ren1
->pair
->two
;
1581 update_entry(ren1
->dst_entry
, one
, a
, b
);
1582 setup_rename_conflict_info(RENAME_NORMAL
,
1596 string_list_clear(&a_by_dst
, 0);
1597 string_list_clear(&b_by_dst
, 0);
1602 static struct object_id
*stage_oid(const struct object_id
*oid
, unsigned mode
)
1604 return (is_null_oid(oid
) || mode
== 0) ? NULL
: (struct object_id
*)oid
;
1607 static int read_oid_strbuf(struct merge_options
*o
,
1608 const struct object_id
*oid
, struct strbuf
*dst
)
1611 enum object_type type
;
1613 buf
= read_sha1_file(oid
->hash
, &type
, &size
);
1615 return err(o
, _("cannot read object %s"), oid_to_hex(oid
));
1616 if (type
!= OBJ_BLOB
) {
1618 return err(o
, _("object %s is not a blob"), oid_to_hex(oid
));
1620 strbuf_attach(dst
, buf
, size
, size
+ 1);
1624 static int blob_unchanged(struct merge_options
*opt
,
1625 const struct object_id
*o_oid
,
1627 const struct object_id
*a_oid
,
1629 int renormalize
, const char *path
)
1631 struct strbuf o
= STRBUF_INIT
;
1632 struct strbuf a
= STRBUF_INIT
;
1633 int ret
= 0; /* assume changed for safety */
1635 if (a_mode
!= o_mode
)
1637 if (oid_eq(o_oid
, a_oid
))
1642 assert(o_oid
&& a_oid
);
1643 if (read_oid_strbuf(opt
, o_oid
, &o
) || read_oid_strbuf(opt
, a_oid
, &a
))
1646 * Note: binary | is used so that both renormalizations are
1647 * performed. Comparison can be skipped if both files are
1648 * unchanged since their sha1s have already been compared.
1650 if (renormalize_buffer(path
, o
.buf
, o
.len
, &o
) |
1651 renormalize_buffer(path
, a
.buf
, a
.len
, &a
))
1652 ret
= (o
.len
== a
.len
&& !memcmp(o
.buf
, a
.buf
, o
.len
));
1660 static int handle_modify_delete(struct merge_options
*o
,
1662 struct object_id
*o_oid
, int o_mode
,
1663 struct object_id
*a_oid
, int a_mode
,
1664 struct object_id
*b_oid
, int b_mode
)
1666 return handle_change_delete(o
,
1671 _("modify"), _("modified"));
1674 static int merge_content(struct merge_options
*o
,
1676 struct object_id
*o_oid
, int o_mode
,
1677 struct object_id
*a_oid
, int a_mode
,
1678 struct object_id
*b_oid
, int b_mode
,
1679 struct rename_conflict_info
*rename_conflict_info
)
1681 const char *reason
= _("content");
1682 const char *path1
= NULL
, *path2
= NULL
;
1683 struct merge_file_info mfi
;
1684 struct diff_filespec one
, a
, b
;
1685 unsigned df_conflict_remains
= 0;
1688 reason
= _("add/add");
1689 o_oid
= (struct object_id
*)&null_oid
;
1691 one
.path
= a
.path
= b
.path
= (char *)path
;
1692 oidcpy(&one
.oid
, o_oid
);
1694 oidcpy(&a
.oid
, a_oid
);
1696 oidcpy(&b
.oid
, b_oid
);
1699 if (rename_conflict_info
) {
1700 struct diff_filepair
*pair1
= rename_conflict_info
->pair1
;
1702 path1
= (o
->branch1
== rename_conflict_info
->branch1
) ?
1703 pair1
->two
->path
: pair1
->one
->path
;
1704 /* If rename_conflict_info->pair2 != NULL, we are in
1705 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a
1708 path2
= (rename_conflict_info
->pair2
||
1709 o
->branch2
== rename_conflict_info
->branch1
) ?
1710 pair1
->two
->path
: pair1
->one
->path
;
1712 if (dir_in_way(path
, !o
->call_depth
,
1713 S_ISGITLINK(pair1
->two
->mode
)))
1714 df_conflict_remains
= 1;
1716 if (merge_file_special_markers(o
, &one
, &a
, &b
,
1718 o
->branch2
, path2
, &mfi
))
1721 if (mfi
.clean
&& !df_conflict_remains
&&
1722 oid_eq(&mfi
.oid
, a_oid
) && mfi
.mode
== a_mode
) {
1723 int path_renamed_outside_HEAD
;
1724 output(o
, 3, _("Skipped %s (merged same as existing)"), path
);
1726 * The content merge resulted in the same file contents we
1727 * already had. We can return early if those file contents
1728 * are recorded at the correct path (which may not be true
1729 * if the merge involves a rename).
1731 path_renamed_outside_HEAD
= !path2
|| !strcmp(path
, path2
);
1732 if (!path_renamed_outside_HEAD
) {
1733 add_cacheinfo(o
, mfi
.mode
, &mfi
.oid
, path
,
1734 0, (!o
->call_depth
), 0);
1738 output(o
, 2, _("Auto-merging %s"), path
);
1741 if (S_ISGITLINK(mfi
.mode
))
1742 reason
= _("submodule");
1743 output(o
, 1, _("CONFLICT (%s): Merge conflict in %s"),
1745 if (rename_conflict_info
&& !df_conflict_remains
)
1746 if (update_stages(o
, path
, &one
, &a
, &b
))
1750 if (df_conflict_remains
) {
1752 if (o
->call_depth
) {
1753 remove_file_from_cache(path
);
1756 if (update_stages(o
, path
, &one
, &a
, &b
))
1759 int file_from_stage2
= was_tracked(path
);
1760 struct diff_filespec merged
;
1761 oidcpy(&merged
.oid
, &mfi
.oid
);
1762 merged
.mode
= mfi
.mode
;
1764 if (update_stages(o
, path
, NULL
,
1765 file_from_stage2
? &merged
: NULL
,
1766 file_from_stage2
? NULL
: &merged
))
1771 new_path
= unique_path(o
, path
, rename_conflict_info
->branch1
);
1772 output(o
, 1, _("Adding as %s instead"), new_path
);
1773 if (update_file(o
, 0, &mfi
.oid
, mfi
.mode
, new_path
)) {
1779 } else if (update_file(o
, mfi
.clean
, &mfi
.oid
, mfi
.mode
, path
))
1784 /* Per entry merge function */
1785 static int process_entry(struct merge_options
*o
,
1786 const char *path
, struct stage_data
*entry
)
1788 int clean_merge
= 1;
1789 int normalize
= o
->renormalize
;
1790 unsigned o_mode
= entry
->stages
[1].mode
;
1791 unsigned a_mode
= entry
->stages
[2].mode
;
1792 unsigned b_mode
= entry
->stages
[3].mode
;
1793 struct object_id
*o_oid
= stage_oid(&entry
->stages
[1].oid
, o_mode
);
1794 struct object_id
*a_oid
= stage_oid(&entry
->stages
[2].oid
, a_mode
);
1795 struct object_id
*b_oid
= stage_oid(&entry
->stages
[3].oid
, b_mode
);
1797 entry
->processed
= 1;
1798 if (entry
->rename_conflict_info
) {
1799 struct rename_conflict_info
*conflict_info
= entry
->rename_conflict_info
;
1800 switch (conflict_info
->rename_type
) {
1802 case RENAME_ONE_FILE_TO_ONE
:
1803 clean_merge
= merge_content(o
, path
,
1804 o_oid
, o_mode
, a_oid
, a_mode
, b_oid
, b_mode
,
1809 if (conflict_rename_delete(o
,
1810 conflict_info
->pair1
,
1811 conflict_info
->branch1
,
1812 conflict_info
->branch2
))
1815 case RENAME_ONE_FILE_TO_TWO
:
1817 if (conflict_rename_rename_1to2(o
, conflict_info
))
1820 case RENAME_TWO_FILES_TO_ONE
:
1822 if (conflict_rename_rename_2to1(o
, conflict_info
))
1826 entry
->processed
= 0;
1829 } else if (o_oid
&& (!a_oid
|| !b_oid
)) {
1830 /* Case A: Deleted in one */
1831 if ((!a_oid
&& !b_oid
) ||
1832 (!b_oid
&& blob_unchanged(o
, o_oid
, o_mode
, a_oid
, a_mode
, normalize
, path
)) ||
1833 (!a_oid
&& blob_unchanged(o
, o_oid
, o_mode
, b_oid
, b_mode
, normalize
, path
))) {
1834 /* Deleted in both or deleted in one and
1835 * unchanged in the other */
1837 output(o
, 2, _("Removing %s"), path
);
1838 /* do not touch working file if it did not exist */
1839 remove_file(o
, 1, path
, !a_oid
);
1841 /* Modify/delete; deleted side may have put a directory in the way */
1843 if (handle_modify_delete(o
, path
, o_oid
, o_mode
,
1844 a_oid
, a_mode
, b_oid
, b_mode
))
1847 } else if ((!o_oid
&& a_oid
&& !b_oid
) ||
1848 (!o_oid
&& !a_oid
&& b_oid
)) {
1849 /* Case B: Added in one. */
1850 /* [nothing|directory] -> ([nothing|directory], file) */
1852 const char *add_branch
;
1853 const char *other_branch
;
1855 const struct object_id
*oid
;
1859 add_branch
= o
->branch1
;
1860 other_branch
= o
->branch2
;
1863 conf
= _("file/directory");
1865 add_branch
= o
->branch2
;
1866 other_branch
= o
->branch1
;
1869 conf
= _("directory/file");
1871 if (dir_in_way(path
, !o
->call_depth
,
1872 S_ISGITLINK(a_mode
))) {
1873 char *new_path
= unique_path(o
, path
, add_branch
);
1875 output(o
, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
1877 conf
, path
, other_branch
, path
, new_path
);
1878 if (update_file(o
, 0, oid
, mode
, new_path
))
1880 else if (o
->call_depth
)
1881 remove_file_from_cache(path
);
1884 output(o
, 2, _("Adding %s"), path
);
1885 /* do not overwrite file if already present */
1886 if (update_file_flags(o
, oid
, mode
, path
, 1, !a_oid
))
1889 } else if (a_oid
&& b_oid
) {
1890 /* Case C: Added in both (check for same permissions) and */
1891 /* case D: Modified in both, but differently. */
1892 clean_merge
= merge_content(o
, path
,
1893 o_oid
, o_mode
, a_oid
, a_mode
, b_oid
, b_mode
,
1895 } else if (!o_oid
&& !a_oid
&& !b_oid
) {
1897 * this entry was deleted altogether. a_mode == 0 means
1898 * we had that path and want to actively remove it.
1900 remove_file(o
, 1, path
, !a_mode
);
1902 die("BUG: fatal merge failure, shouldn't happen.");
1907 int merge_trees(struct merge_options
*o
,
1910 struct tree
*common
,
1911 struct tree
**result
)
1915 if (o
->subtree_shift
) {
1916 merge
= shift_tree_object(head
, merge
, o
->subtree_shift
);
1917 common
= shift_tree_object(head
, common
, o
->subtree_shift
);
1920 if (oid_eq(&common
->object
.oid
, &merge
->object
.oid
)) {
1921 output(o
, 0, _("Already up-to-date!"));
1926 code
= git_merge_trees(o
->call_depth
, common
, head
, merge
);
1929 if (show(o
, 4) || o
->call_depth
)
1930 err(o
, _("merging of trees %s and %s failed"),
1931 oid_to_hex(&head
->object
.oid
),
1932 oid_to_hex(&merge
->object
.oid
));
1936 if (unmerged_cache()) {
1937 struct string_list
*entries
, *re_head
, *re_merge
;
1939 string_list_clear(&o
->current_file_set
, 1);
1940 string_list_clear(&o
->current_directory_set
, 1);
1941 get_files_dirs(o
, head
);
1942 get_files_dirs(o
, merge
);
1944 entries
= get_unmerged();
1945 record_df_conflict_files(o
, entries
);
1946 re_head
= get_renames(o
, head
, common
, head
, merge
, entries
);
1947 re_merge
= get_renames(o
, merge
, common
, head
, merge
, entries
);
1948 clean
= process_renames(o
, re_head
, re_merge
);
1951 for (i
= entries
->nr
-1; 0 <= i
; i
--) {
1952 const char *path
= entries
->items
[i
].string
;
1953 struct stage_data
*e
= entries
->items
[i
].util
;
1954 if (!e
->processed
) {
1955 int ret
= process_entry(o
, path
, e
);
1962 for (i
= 0; i
< entries
->nr
; i
++) {
1963 struct stage_data
*e
= entries
->items
[i
].util
;
1965 die("BUG: unprocessed path??? %s",
1966 entries
->items
[i
].string
);
1969 string_list_clear(re_merge
, 0);
1970 string_list_clear(re_head
, 0);
1971 string_list_clear(entries
, 1);
1980 if (o
->call_depth
&& !(*result
= write_tree_from_memory(o
)))
1986 static struct commit_list
*reverse_commit_list(struct commit_list
*list
)
1988 struct commit_list
*next
= NULL
, *current
, *backup
;
1989 for (current
= list
; current
; current
= backup
) {
1990 backup
= current
->next
;
1991 current
->next
= next
;
1998 * Merge the commits h1 and h2, return the resulting virtual
1999 * commit object and a flag indicating the cleanness of the merge.
2001 int merge_recursive(struct merge_options
*o
,
2004 struct commit_list
*ca
,
2005 struct commit
**result
)
2007 struct commit_list
*iter
;
2008 struct commit
*merged_common_ancestors
;
2009 struct tree
*mrtree
= mrtree
;
2013 output(o
, 4, _("Merging:"));
2014 output_commit_title(o
, h1
);
2015 output_commit_title(o
, h2
);
2019 ca
= get_merge_bases(h1
, h2
);
2020 ca
= reverse_commit_list(ca
);
2024 unsigned cnt
= commit_list_count(ca
);
2026 output(o
, 5, Q_("found %u common ancestor:",
2027 "found %u common ancestors:", cnt
), cnt
);
2028 for (iter
= ca
; iter
; iter
= iter
->next
)
2029 output_commit_title(o
, iter
->item
);
2032 merged_common_ancestors
= pop_commit(&ca
);
2033 if (merged_common_ancestors
== NULL
) {
2034 /* if there is no common ancestor, use an empty tree */
2037 tree
= lookup_tree(EMPTY_TREE_SHA1_BIN
);
2038 merged_common_ancestors
= make_virtual_commit(tree
, "ancestor");
2041 for (iter
= ca
; iter
; iter
= iter
->next
) {
2042 const char *saved_b1
, *saved_b2
;
2045 * When the merge fails, the result contains files
2046 * with conflict markers. The cleanness flag is
2047 * ignored (unless indicating an error), it was never
2048 * actually used, as result of merge_trees has always
2049 * overwritten it: the committed "conflicts" were
2053 saved_b1
= o
->branch1
;
2054 saved_b2
= o
->branch2
;
2055 o
->branch1
= "Temporary merge branch 1";
2056 o
->branch2
= "Temporary merge branch 2";
2057 if (merge_recursive(o
, merged_common_ancestors
, iter
->item
,
2058 NULL
, &merged_common_ancestors
) < 0)
2060 o
->branch1
= saved_b1
;
2061 o
->branch2
= saved_b2
;
2064 if (!merged_common_ancestors
)
2065 return err(o
, _("merge returned no commit"));
2072 o
->ancestor
= "merged common ancestors";
2073 clean
= merge_trees(o
, h1
->tree
, h2
->tree
, merged_common_ancestors
->tree
,
2080 if (o
->call_depth
) {
2081 *result
= make_virtual_commit(mrtree
, "merged tree");
2082 commit_list_insert(h1
, &(*result
)->parents
);
2083 commit_list_insert(h2
, &(*result
)->parents
->next
);
2086 if (!o
->call_depth
&& o
->buffer_output
< 2)
2087 strbuf_release(&o
->obuf
);
2089 diff_warn_rename_limit("merge.renamelimit",
2090 o
->needed_rename_limit
, 0);
2094 static struct commit
*get_ref(const struct object_id
*oid
, const char *name
)
2096 struct object
*object
;
2098 object
= deref_tag(parse_object(oid
->hash
), name
, strlen(name
));
2101 if (object
->type
== OBJ_TREE
)
2102 return make_virtual_commit((struct tree
*)object
, name
);
2103 if (object
->type
!= OBJ_COMMIT
)
2105 if (parse_commit((struct commit
*)object
))
2107 return (struct commit
*)object
;
2110 int merge_recursive_generic(struct merge_options
*o
,
2111 const struct object_id
*head
,
2112 const struct object_id
*merge
,
2114 const struct object_id
**base_list
,
2115 struct commit
**result
)
2118 struct lock_file
*lock
= xcalloc(1, sizeof(struct lock_file
));
2119 struct commit
*head_commit
= get_ref(head
, o
->branch1
);
2120 struct commit
*next_commit
= get_ref(merge
, o
->branch2
);
2121 struct commit_list
*ca
= NULL
;
2125 for (i
= 0; i
< num_base_list
; ++i
) {
2126 struct commit
*base
;
2127 if (!(base
= get_ref(base_list
[i
], oid_to_hex(base_list
[i
]))))
2128 return err(o
, _("Could not parse object '%s'"),
2129 oid_to_hex(base_list
[i
]));
2130 commit_list_insert(base
, &ca
);
2134 hold_locked_index(lock
, 1);
2135 clean
= merge_recursive(o
, head_commit
, next_commit
, ca
,
2140 if (active_cache_changed
&&
2141 write_locked_index(&the_index
, lock
, COMMIT_LOCK
))
2142 return err(o
, _("Unable to write index."));
2144 return clean
? 0 : 1;
2147 static void merge_recursive_config(struct merge_options
*o
)
2149 git_config_get_int("merge.verbosity", &o
->verbosity
);
2150 git_config_get_int("diff.renamelimit", &o
->diff_rename_limit
);
2151 git_config_get_int("merge.renamelimit", &o
->merge_rename_limit
);
2152 git_config(git_xmerge_config
, NULL
);
2155 void init_merge_options(struct merge_options
*o
)
2157 memset(o
, 0, sizeof(struct merge_options
));
2159 o
->buffer_output
= 1;
2160 o
->diff_rename_limit
= -1;
2161 o
->merge_rename_limit
= -1;
2163 o
->detect_rename
= 1;
2164 merge_recursive_config(o
);
2165 if (getenv("GIT_MERGE_VERBOSITY"))
2167 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL
, 10);
2168 if (o
->verbosity
>= 5)
2169 o
->buffer_output
= 0;
2170 strbuf_init(&o
->obuf
, 0);
2171 string_list_init(&o
->current_file_set
, 1);
2172 string_list_init(&o
->current_directory_set
, 1);
2173 string_list_init(&o
->df_conflict_file_set
, 1);
2176 int parse_merge_opt(struct merge_options
*o
, const char *s
)
2182 if (!strcmp(s
, "ours"))
2183 o
->recursive_variant
= MERGE_RECURSIVE_OURS
;
2184 else if (!strcmp(s
, "theirs"))
2185 o
->recursive_variant
= MERGE_RECURSIVE_THEIRS
;
2186 else if (!strcmp(s
, "subtree"))
2187 o
->subtree_shift
= "";
2188 else if (skip_prefix(s
, "subtree=", &arg
))
2189 o
->subtree_shift
= arg
;
2190 else if (!strcmp(s
, "patience"))
2191 o
->xdl_opts
= DIFF_WITH_ALG(o
, PATIENCE_DIFF
);
2192 else if (!strcmp(s
, "histogram"))
2193 o
->xdl_opts
= DIFF_WITH_ALG(o
, HISTOGRAM_DIFF
);
2194 else if (skip_prefix(s
, "diff-algorithm=", &arg
)) {
2195 long value
= parse_algorithm_value(arg
);
2198 /* clear out previous settings */
2199 DIFF_XDL_CLR(o
, NEED_MINIMAL
);
2200 o
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
2201 o
->xdl_opts
|= value
;
2203 else if (!strcmp(s
, "ignore-space-change"))
2204 o
->xdl_opts
|= XDF_IGNORE_WHITESPACE_CHANGE
;
2205 else if (!strcmp(s
, "ignore-all-space"))
2206 o
->xdl_opts
|= XDF_IGNORE_WHITESPACE
;
2207 else if (!strcmp(s
, "ignore-space-at-eol"))
2208 o
->xdl_opts
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
2209 else if (!strcmp(s
, "renormalize"))
2211 else if (!strcmp(s
, "no-renormalize"))
2213 else if (!strcmp(s
, "no-renames"))
2214 o
->detect_rename
= 0;
2215 else if (!strcmp(s
, "find-renames")) {
2216 o
->detect_rename
= 1;
2217 o
->rename_score
= 0;
2219 else if (skip_prefix(s
, "find-renames=", &arg
) ||
2220 skip_prefix(s
, "rename-threshold=", &arg
)) {
2221 if ((o
->rename_score
= parse_rename_score(&arg
)) == -1 || *arg
!= 0)
2223 o
->detect_rename
= 1;