2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
10 #include "cache-tree.h"
14 #include "tree-walk.h"
19 #include "unpack-trees.h"
20 #include "string-list.h"
21 #include "xdiff-interface.h"
24 #include "merge-recursive.h"
26 #include "submodule.h"
28 struct path_hashmap_entry
{
29 struct hashmap_entry e
;
30 char path
[FLEX_ARRAY
];
33 static int path_hashmap_cmp(const void *cmp_data
,
35 const void *entry_or_key
,
38 const struct path_hashmap_entry
*a
= entry
;
39 const struct path_hashmap_entry
*b
= entry_or_key
;
40 const char *key
= keydata
;
43 return strcasecmp(a
->path
, key
? key
: b
->path
);
45 return strcmp(a
->path
, key
? key
: b
->path
);
48 static unsigned int path_hash(const char *path
)
50 return ignore_case
? strihash(path
) : strhash(path
);
53 static void flush_output(struct merge_options
*o
)
55 if (o
->buffer_output
< 2 && o
->obuf
.len
) {
56 fputs(o
->obuf
.buf
, stdout
);
57 strbuf_reset(&o
->obuf
);
61 static int err(struct merge_options
*o
, const char *err
, ...)
65 if (o
->buffer_output
< 2)
68 strbuf_complete(&o
->obuf
, '\n');
69 strbuf_addstr(&o
->obuf
, "error: ");
71 va_start(params
, err
);
72 strbuf_vaddf(&o
->obuf
, err
, params
);
74 if (o
->buffer_output
> 1)
75 strbuf_addch(&o
->obuf
, '\n');
77 error("%s", o
->obuf
.buf
);
78 strbuf_reset(&o
->obuf
);
84 static struct tree
*shift_tree_object(struct tree
*one
, struct tree
*two
,
85 const char *subtree_shift
)
87 struct object_id shifted
;
89 if (!*subtree_shift
) {
90 shift_tree(&one
->object
.oid
, &two
->object
.oid
, &shifted
, 0);
92 shift_tree_by(&one
->object
.oid
, &two
->object
.oid
, &shifted
,
95 if (!oidcmp(&two
->object
.oid
, &shifted
))
97 return lookup_tree(&shifted
);
100 static struct commit
*make_virtual_commit(struct tree
*tree
, const char *comment
)
102 struct commit
*commit
= alloc_commit_node(the_repository
);
104 set_merge_remote_desc(commit
, comment
, (struct object
*)commit
);
106 commit
->object
.parsed
= 1;
111 * Since we use get_tree_entry(), which does not put the read object into
112 * the object pool, we cannot rely on a == b.
114 static int oid_eq(const struct object_id
*a
, const struct object_id
*b
)
118 return a
&& b
&& oidcmp(a
, b
) == 0;
124 RENAME_ONE_FILE_TO_ONE
,
125 RENAME_ONE_FILE_TO_TWO
,
126 RENAME_TWO_FILES_TO_ONE
129 struct rename_conflict_info
{
130 enum rename_type rename_type
;
131 struct diff_filepair
*pair1
;
132 struct diff_filepair
*pair2
;
135 struct stage_data
*dst_entry1
;
136 struct stage_data
*dst_entry2
;
137 struct diff_filespec ren1_other
;
138 struct diff_filespec ren2_other
;
142 * Since we want to write the index eventually, we cannot reuse the index
143 * for these (temporary) data.
148 struct object_id oid
;
150 struct rename_conflict_info
*rename_conflict_info
;
151 unsigned processed
:1;
154 static inline void setup_rename_conflict_info(enum rename_type rename_type
,
155 struct diff_filepair
*pair1
,
156 struct diff_filepair
*pair2
,
159 struct stage_data
*dst_entry1
,
160 struct stage_data
*dst_entry2
,
161 struct merge_options
*o
,
162 struct stage_data
*src_entry1
,
163 struct stage_data
*src_entry2
)
165 struct rename_conflict_info
*ci
= xcalloc(1, sizeof(struct rename_conflict_info
));
166 ci
->rename_type
= rename_type
;
168 ci
->branch1
= branch1
;
169 ci
->branch2
= branch2
;
171 ci
->dst_entry1
= dst_entry1
;
172 dst_entry1
->rename_conflict_info
= ci
;
173 dst_entry1
->processed
= 0;
175 assert(!pair2
== !dst_entry2
);
177 ci
->dst_entry2
= dst_entry2
;
179 dst_entry2
->rename_conflict_info
= ci
;
182 if (rename_type
== RENAME_TWO_FILES_TO_ONE
) {
184 * For each rename, there could have been
185 * modifications on the side of history where that
186 * file was not renamed.
188 int ostage1
= o
->branch1
== branch1
? 3 : 2;
189 int ostage2
= ostage1
^ 1;
191 ci
->ren1_other
.path
= pair1
->one
->path
;
192 oidcpy(&ci
->ren1_other
.oid
, &src_entry1
->stages
[ostage1
].oid
);
193 ci
->ren1_other
.mode
= src_entry1
->stages
[ostage1
].mode
;
195 ci
->ren2_other
.path
= pair2
->one
->path
;
196 oidcpy(&ci
->ren2_other
.oid
, &src_entry2
->stages
[ostage2
].oid
);
197 ci
->ren2_other
.mode
= src_entry2
->stages
[ostage2
].mode
;
201 static int show(struct merge_options
*o
, int v
)
203 return (!o
->call_depth
&& o
->verbosity
>= v
) || o
->verbosity
>= 5;
206 __attribute__((format (printf
, 3, 4)))
207 static void output(struct merge_options
*o
, int v
, const char *fmt
, ...)
214 strbuf_addchars(&o
->obuf
, ' ', o
->call_depth
* 2);
217 strbuf_vaddf(&o
->obuf
, fmt
, ap
);
220 strbuf_addch(&o
->obuf
, '\n');
221 if (!o
->buffer_output
)
225 static void output_commit_title(struct merge_options
*o
, struct commit
*commit
)
227 strbuf_addchars(&o
->obuf
, ' ', o
->call_depth
* 2);
229 strbuf_addf(&o
->obuf
, "virtual %s\n",
230 merge_remote_util(commit
)->name
);
232 strbuf_add_unique_abbrev(&o
->obuf
, &commit
->object
.oid
,
234 strbuf_addch(&o
->obuf
, ' ');
235 if (parse_commit(commit
) != 0)
236 strbuf_addstr(&o
->obuf
, _("(bad commit)\n"));
239 const char *msg
= get_commit_buffer(commit
, NULL
);
240 int len
= find_commit_subject(msg
, &title
);
242 strbuf_addf(&o
->obuf
, "%.*s\n", len
, title
);
243 unuse_commit_buffer(commit
, msg
);
249 static int add_cacheinfo(struct merge_options
*o
,
250 unsigned int mode
, const struct object_id
*oid
,
251 const char *path
, int stage
, int refresh
, int options
)
253 struct cache_entry
*ce
;
256 ce
= make_cache_entry(mode
, oid
? oid
->hash
: null_sha1
, path
, stage
, 0);
258 return err(o
, _("addinfo_cache failed for path '%s'"), path
);
260 ret
= add_cache_entry(ce
, options
);
262 struct cache_entry
*nce
;
264 nce
= refresh_cache_entry(ce
, CE_MATCH_REFRESH
| CE_MATCH_IGNORE_MISSING
);
266 return err(o
, _("addinfo_cache failed for path '%s'"), path
);
268 ret
= add_cache_entry(nce
, options
);
273 static void init_tree_desc_from_tree(struct tree_desc
*desc
, struct tree
*tree
)
276 init_tree_desc(desc
, tree
->buffer
, tree
->size
);
279 static int git_merge_trees(int index_only
,
285 struct tree_desc t
[3];
286 struct unpack_trees_options opts
;
288 memset(&opts
, 0, sizeof(opts
));
295 opts
.fn
= threeway_merge
;
296 opts
.src_index
= &the_index
;
297 opts
.dst_index
= &the_index
;
298 setup_unpack_trees_porcelain(&opts
, "merge");
300 init_tree_desc_from_tree(t
+0, common
);
301 init_tree_desc_from_tree(t
+1, head
);
302 init_tree_desc_from_tree(t
+2, merge
);
304 rc
= unpack_trees(3, t
, &opts
);
305 cache_tree_free(&active_cache_tree
);
309 struct tree
*write_tree_from_memory(struct merge_options
*o
)
311 struct tree
*result
= NULL
;
313 if (unmerged_cache()) {
315 fprintf(stderr
, "BUG: There are unmerged index entries:\n");
316 for (i
= 0; i
< active_nr
; i
++) {
317 const struct cache_entry
*ce
= active_cache
[i
];
319 fprintf(stderr
, "BUG: %d %.*s\n", ce_stage(ce
),
320 (int)ce_namelen(ce
), ce
->name
);
322 die("BUG: unmerged index entries in merge-recursive.c");
325 if (!active_cache_tree
)
326 active_cache_tree
= cache_tree();
328 if (!cache_tree_fully_valid(active_cache_tree
) &&
329 cache_tree_update(&the_index
, 0) < 0) {
330 err(o
, _("error building trees"));
334 result
= lookup_tree(&active_cache_tree
->oid
);
339 static int save_files_dirs(const struct object_id
*oid
,
340 struct strbuf
*base
, const char *path
,
341 unsigned int mode
, int stage
, void *context
)
343 struct path_hashmap_entry
*entry
;
344 int baselen
= base
->len
;
345 struct merge_options
*o
= context
;
347 strbuf_addstr(base
, path
);
349 FLEX_ALLOC_MEM(entry
, path
, base
->buf
, base
->len
);
350 hashmap_entry_init(entry
, path_hash(entry
->path
));
351 hashmap_add(&o
->current_file_dir_set
, entry
);
353 strbuf_setlen(base
, baselen
);
354 return (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
357 static void get_files_dirs(struct merge_options
*o
, struct tree
*tree
)
359 struct pathspec match_all
;
360 memset(&match_all
, 0, sizeof(match_all
));
361 read_tree_recursive(tree
, "", 0, 0, &match_all
, save_files_dirs
, o
);
365 * Returns an index_entry instance which doesn't have to correspond to
366 * a real cache entry in Git's index.
368 static struct stage_data
*insert_stage_data(const char *path
,
369 struct tree
*o
, struct tree
*a
, struct tree
*b
,
370 struct string_list
*entries
)
372 struct string_list_item
*item
;
373 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
374 get_tree_entry(&o
->object
.oid
, path
,
375 &e
->stages
[1].oid
, &e
->stages
[1].mode
);
376 get_tree_entry(&a
->object
.oid
, path
,
377 &e
->stages
[2].oid
, &e
->stages
[2].mode
);
378 get_tree_entry(&b
->object
.oid
, path
,
379 &e
->stages
[3].oid
, &e
->stages
[3].mode
);
380 item
= string_list_insert(entries
, path
);
386 * Create a dictionary mapping file names to stage_data objects. The
387 * dictionary contains one entry for every path with a non-zero stage entry.
389 static struct string_list
*get_unmerged(void)
391 struct string_list
*unmerged
= xcalloc(1, sizeof(struct string_list
));
394 unmerged
->strdup_strings
= 1;
396 for (i
= 0; i
< active_nr
; i
++) {
397 struct string_list_item
*item
;
398 struct stage_data
*e
;
399 const struct cache_entry
*ce
= active_cache
[i
];
403 item
= string_list_lookup(unmerged
, ce
->name
);
405 item
= string_list_insert(unmerged
, ce
->name
);
406 item
->util
= xcalloc(1, sizeof(struct stage_data
));
409 e
->stages
[ce_stage(ce
)].mode
= ce
->ce_mode
;
410 oidcpy(&e
->stages
[ce_stage(ce
)].oid
, &ce
->oid
);
416 static int string_list_df_name_compare(const char *one
, const char *two
)
418 int onelen
= strlen(one
);
419 int twolen
= strlen(two
);
421 * Here we only care that entries for D/F conflicts are
422 * adjacent, in particular with the file of the D/F conflict
423 * appearing before files below the corresponding directory.
424 * The order of the rest of the list is irrelevant for us.
426 * To achieve this, we sort with df_name_compare and provide
427 * the mode S_IFDIR so that D/F conflicts will sort correctly.
428 * We use the mode S_IFDIR for everything else for simplicity,
429 * since in other cases any changes in their order due to
430 * sorting cause no problems for us.
432 int cmp
= df_name_compare(one
, onelen
, S_IFDIR
,
433 two
, twolen
, S_IFDIR
);
435 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
436 * that 'foo' comes before 'foo/bar'.
440 return onelen
- twolen
;
443 static void record_df_conflict_files(struct merge_options
*o
,
444 struct string_list
*entries
)
446 /* If there is a D/F conflict and the file for such a conflict
447 * currently exist in the working tree, we want to allow it to be
448 * removed to make room for the corresponding directory if needed.
449 * The files underneath the directories of such D/F conflicts will
450 * be processed before the corresponding file involved in the D/F
451 * conflict. If the D/F directory ends up being removed by the
452 * merge, then we won't have to touch the D/F file. If the D/F
453 * directory needs to be written to the working copy, then the D/F
454 * file will simply be removed (in make_room_for_path()) to make
455 * room for the necessary paths. Note that if both the directory
456 * and the file need to be present, then the D/F file will be
457 * reinstated with a new unique name at the time it is processed.
459 struct string_list df_sorted_entries
= STRING_LIST_INIT_NODUP
;
460 const char *last_file
= NULL
;
465 * If we're merging merge-bases, we don't want to bother with
466 * any working directory changes.
471 /* Ensure D/F conflicts are adjacent in the entries list. */
472 for (i
= 0; i
< entries
->nr
; i
++) {
473 struct string_list_item
*next
= &entries
->items
[i
];
474 string_list_append(&df_sorted_entries
, next
->string
)->util
=
477 df_sorted_entries
.cmp
= string_list_df_name_compare
;
478 string_list_sort(&df_sorted_entries
);
480 string_list_clear(&o
->df_conflict_file_set
, 1);
481 for (i
= 0; i
< df_sorted_entries
.nr
; i
++) {
482 const char *path
= df_sorted_entries
.items
[i
].string
;
483 int len
= strlen(path
);
484 struct stage_data
*e
= df_sorted_entries
.items
[i
].util
;
487 * Check if last_file & path correspond to a D/F conflict;
488 * i.e. whether path is last_file+'/'+<something>.
489 * If so, record that it's okay to remove last_file to make
490 * room for path and friends if needed.
494 memcmp(path
, last_file
, last_len
) == 0 &&
495 path
[last_len
] == '/') {
496 string_list_insert(&o
->df_conflict_file_set
, last_file
);
500 * Determine whether path could exist as a file in the
501 * working directory as a possible D/F conflict. This
502 * will only occur when it exists in stage 2 as a
505 if (S_ISREG(e
->stages
[2].mode
) || S_ISLNK(e
->stages
[2].mode
)) {
512 string_list_clear(&df_sorted_entries
, 0);
516 struct diff_filepair
*pair
;
518 * Purpose of src_entry and dst_entry:
520 * If 'before' is renamed to 'after' then src_entry will contain
521 * the versions of 'before' from the merge_base, HEAD, and MERGE in
522 * stages 1, 2, and 3; dst_entry will contain the respective
523 * versions of 'after' in corresponding locations. Thus, we have a
524 * total of six modes and oids, though some will be null. (Stage 0
525 * is ignored; we're interested in handling conflicts.)
527 * Since we don't turn on break-rewrites by default, neither
528 * src_entry nor dst_entry can have all three of their stages have
529 * non-null oids, meaning at most four of the six will be non-null.
530 * Also, since this is a rename, both src_entry and dst_entry will
531 * have at least one non-null oid, meaning at least two will be
532 * non-null. Of the six oids, a typical rename will have three be
533 * non-null. Only two implies a rename/delete, and four implies a
536 struct stage_data
*src_entry
;
537 struct stage_data
*dst_entry
;
538 unsigned processed
:1;
542 * Get information of all renames which occurred between 'o_tree' and
543 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
544 * 'b_tree') to be able to associate the correct cache entries with
545 * the rename information. 'tree' is always equal to either a_tree or b_tree.
547 static struct string_list
*get_renames(struct merge_options
*o
,
552 struct string_list
*entries
)
555 struct string_list
*renames
;
556 struct diff_options opts
;
558 renames
= xcalloc(1, sizeof(struct string_list
));
559 if (!o
->detect_rename
)
563 opts
.flags
.recursive
= 1;
564 opts
.flags
.rename_empty
= 0;
565 opts
.detect_rename
= DIFF_DETECT_RENAME
;
566 opts
.rename_limit
= o
->merge_rename_limit
>= 0 ? o
->merge_rename_limit
:
567 o
->diff_rename_limit
>= 0 ? o
->diff_rename_limit
:
569 opts
.rename_score
= o
->rename_score
;
570 opts
.show_rename_progress
= o
->show_rename_progress
;
571 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
572 diff_setup_done(&opts
);
573 diff_tree_oid(&o_tree
->object
.oid
, &tree
->object
.oid
, "", &opts
);
575 if (opts
.needed_rename_limit
> o
->needed_rename_limit
)
576 o
->needed_rename_limit
= opts
.needed_rename_limit
;
577 for (i
= 0; i
< diff_queued_diff
.nr
; ++i
) {
578 struct string_list_item
*item
;
580 struct diff_filepair
*pair
= diff_queued_diff
.queue
[i
];
581 if (pair
->status
!= 'R') {
582 diff_free_filepair(pair
);
585 re
= xmalloc(sizeof(*re
));
588 item
= string_list_lookup(entries
, re
->pair
->one
->path
);
590 re
->src_entry
= insert_stage_data(re
->pair
->one
->path
,
591 o_tree
, a_tree
, b_tree
, entries
);
593 re
->src_entry
= item
->util
;
595 item
= string_list_lookup(entries
, re
->pair
->two
->path
);
597 re
->dst_entry
= insert_stage_data(re
->pair
->two
->path
,
598 o_tree
, a_tree
, b_tree
, entries
);
600 re
->dst_entry
= item
->util
;
601 item
= string_list_insert(renames
, pair
->one
->path
);
604 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
605 diff_queued_diff
.nr
= 0;
610 static int update_stages(struct merge_options
*opt
, const char *path
,
611 const struct diff_filespec
*o
,
612 const struct diff_filespec
*a
,
613 const struct diff_filespec
*b
)
617 * NOTE: It is usually a bad idea to call update_stages on a path
618 * before calling update_file on that same path, since it can
619 * sometimes lead to spurious "refusing to lose untracked file..."
620 * messages from update_file (via make_room_for path via
621 * would_lose_untracked). Instead, reverse the order of the calls
622 * (executing update_file first and then update_stages).
625 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_SKIP_DFCHECK
;
627 if (remove_file_from_cache(path
))
630 if (add_cacheinfo(opt
, o
->mode
, &o
->oid
, path
, 1, 0, options
))
633 if (add_cacheinfo(opt
, a
->mode
, &a
->oid
, path
, 2, 0, options
))
636 if (add_cacheinfo(opt
, b
->mode
, &b
->oid
, path
, 3, 0, options
))
641 static void update_entry(struct stage_data
*entry
,
642 struct diff_filespec
*o
,
643 struct diff_filespec
*a
,
644 struct diff_filespec
*b
)
646 entry
->processed
= 0;
647 entry
->stages
[1].mode
= o
->mode
;
648 entry
->stages
[2].mode
= a
->mode
;
649 entry
->stages
[3].mode
= b
->mode
;
650 oidcpy(&entry
->stages
[1].oid
, &o
->oid
);
651 oidcpy(&entry
->stages
[2].oid
, &a
->oid
);
652 oidcpy(&entry
->stages
[3].oid
, &b
->oid
);
655 static int remove_file(struct merge_options
*o
, int clean
,
656 const char *path
, int no_wd
)
658 int update_cache
= o
->call_depth
|| clean
;
659 int update_working_directory
= !o
->call_depth
&& !no_wd
;
662 if (remove_file_from_cache(path
))
665 if (update_working_directory
) {
667 struct cache_entry
*ce
;
668 ce
= cache_file_exists(path
, strlen(path
), ignore_case
);
669 if (ce
&& ce_stage(ce
) == 0 && strcmp(path
, ce
->name
))
672 if (remove_path(path
))
678 /* add a string to a strbuf, but converting "/" to "_" */
679 static void add_flattened_path(struct strbuf
*out
, const char *s
)
682 strbuf_addstr(out
, s
);
683 for (; i
< out
->len
; i
++)
684 if (out
->buf
[i
] == '/')
688 static char *unique_path(struct merge_options
*o
, const char *path
, const char *branch
)
690 struct path_hashmap_entry
*entry
;
691 struct strbuf newpath
= STRBUF_INIT
;
695 strbuf_addf(&newpath
, "%s~", path
);
696 add_flattened_path(&newpath
, branch
);
698 base_len
= newpath
.len
;
699 while (hashmap_get_from_hash(&o
->current_file_dir_set
,
700 path_hash(newpath
.buf
), newpath
.buf
) ||
701 (!o
->call_depth
&& file_exists(newpath
.buf
))) {
702 strbuf_setlen(&newpath
, base_len
);
703 strbuf_addf(&newpath
, "_%d", suffix
++);
706 FLEX_ALLOC_MEM(entry
, path
, newpath
.buf
, newpath
.len
);
707 hashmap_entry_init(entry
, path_hash(entry
->path
));
708 hashmap_add(&o
->current_file_dir_set
, entry
);
709 return strbuf_detach(&newpath
, NULL
);
713 * Check whether a directory in the index is in the way of an incoming
714 * file. Return 1 if so. If check_working_copy is non-zero, also
715 * check the working directory. If empty_ok is non-zero, also return
716 * 0 in the case where the working-tree dir exists but is empty.
718 static int dir_in_way(const char *path
, int check_working_copy
, int empty_ok
)
721 struct strbuf dirpath
= STRBUF_INIT
;
724 strbuf_addstr(&dirpath
, path
);
725 strbuf_addch(&dirpath
, '/');
727 pos
= cache_name_pos(dirpath
.buf
, dirpath
.len
);
731 if (pos
< active_nr
&&
732 !strncmp(dirpath
.buf
, active_cache
[pos
]->name
, dirpath
.len
)) {
733 strbuf_release(&dirpath
);
737 strbuf_release(&dirpath
);
738 return check_working_copy
&& !lstat(path
, &st
) && S_ISDIR(st
.st_mode
) &&
739 !(empty_ok
&& is_empty_dir(path
));
742 static int was_tracked(const char *path
)
744 int pos
= cache_name_pos(path
, strlen(path
));
747 /* we have been tracking this path */
751 * Look for an unmerged entry for the path,
752 * specifically stage #2, which would indicate
753 * that "our" side before the merge started
754 * had the path tracked (and resulted in a conflict).
757 pos
< active_nr
&& !strcmp(path
, active_cache
[pos
]->name
);
759 if (ce_stage(active_cache
[pos
]) == 2)
764 static int would_lose_untracked(const char *path
)
766 return !was_tracked(path
) && file_exists(path
);
769 static int make_room_for_path(struct merge_options
*o
, const char *path
)
772 const char *msg
= _("failed to create path '%s'%s");
774 /* Unlink any D/F conflict files that are in the way */
775 for (i
= 0; i
< o
->df_conflict_file_set
.nr
; i
++) {
776 const char *df_path
= o
->df_conflict_file_set
.items
[i
].string
;
777 size_t pathlen
= strlen(path
);
778 size_t df_pathlen
= strlen(df_path
);
779 if (df_pathlen
< pathlen
&&
780 path
[df_pathlen
] == '/' &&
781 strncmp(path
, df_path
, df_pathlen
) == 0) {
783 _("Removing %s to make room for subdirectory\n"),
786 unsorted_string_list_delete_item(&o
->df_conflict_file_set
,
792 /* Make sure leading directories are created */
793 status
= safe_create_leading_directories_const(path
);
795 if (status
== SCLD_EXISTS
)
796 /* something else exists */
797 return err(o
, msg
, path
, _(": perhaps a D/F conflict?"));
798 return err(o
, msg
, path
, "");
802 * Do not unlink a file in the work tree if we are not
805 if (would_lose_untracked(path
))
806 return err(o
, _("refusing to lose untracked file at '%s'"),
809 /* Successful unlink is good.. */
812 /* .. and so is no existing file */
815 /* .. but not some other error (who really cares what?) */
816 return err(o
, msg
, path
, _(": perhaps a D/F conflict?"));
819 static int update_file_flags(struct merge_options
*o
,
820 const struct object_id
*oid
,
832 enum object_type type
;
836 if (S_ISGITLINK(mode
)) {
838 * We may later decide to recursively descend into
839 * the submodule directory and update its index
840 * and/or work tree, but we do not do that now.
846 buf
= read_object_file(oid
, &type
, &size
);
848 return err(o
, _("cannot read object %s '%s'"), oid_to_hex(oid
), path
);
849 if (type
!= OBJ_BLOB
) {
850 ret
= err(o
, _("blob expected for %s '%s'"), oid_to_hex(oid
), path
);
854 struct strbuf strbuf
= STRBUF_INIT
;
855 if (convert_to_working_tree(path
, buf
, size
, &strbuf
)) {
858 buf
= strbuf_detach(&strbuf
, NULL
);
862 if (make_room_for_path(o
, path
) < 0) {
866 if (S_ISREG(mode
) || (!has_symlinks
&& S_ISLNK(mode
))) {
872 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
874 ret
= err(o
, _("failed to open '%s': %s"),
875 path
, strerror(errno
));
878 write_in_full(fd
, buf
, size
);
880 } else if (S_ISLNK(mode
)) {
881 char *lnk
= xmemdupz(buf
, size
);
882 safe_create_leading_directories_const(path
);
884 if (symlink(lnk
, path
))
885 ret
= err(o
, _("failed to symlink '%s': %s"),
886 path
, strerror(errno
));
890 _("do not know what to do with %06o %s '%s'"),
891 mode
, oid_to_hex(oid
), path
);
896 if (!ret
&& update_cache
)
897 add_cacheinfo(o
, mode
, oid
, path
, 0, update_wd
, ADD_CACHE_OK_TO_ADD
);
901 static int update_file(struct merge_options
*o
,
903 const struct object_id
*oid
,
907 return update_file_flags(o
, oid
, mode
, path
, o
->call_depth
|| clean
, !o
->call_depth
);
910 /* Low level file merging, update and removal */
912 struct merge_file_info
{
913 struct object_id oid
;
919 static int merge_3way(struct merge_options
*o
,
920 mmbuffer_t
*result_buf
,
921 const struct diff_filespec
*one
,
922 const struct diff_filespec
*a
,
923 const struct diff_filespec
*b
,
927 mmfile_t orig
, src1
, src2
;
928 struct ll_merge_options ll_opts
= {0};
929 char *base_name
, *name1
, *name2
;
932 ll_opts
.renormalize
= o
->renormalize
;
933 ll_opts
.xdl_opts
= o
->xdl_opts
;
936 ll_opts
.virtual_ancestor
= 1;
939 switch (o
->recursive_variant
) {
940 case MERGE_RECURSIVE_OURS
:
941 ll_opts
.variant
= XDL_MERGE_FAVOR_OURS
;
943 case MERGE_RECURSIVE_THEIRS
:
944 ll_opts
.variant
= XDL_MERGE_FAVOR_THEIRS
;
952 if (strcmp(a
->path
, b
->path
) ||
953 (o
->ancestor
!= NULL
&& strcmp(a
->path
, one
->path
) != 0)) {
954 base_name
= o
->ancestor
== NULL
? NULL
:
955 mkpathdup("%s:%s", o
->ancestor
, one
->path
);
956 name1
= mkpathdup("%s:%s", branch1
, a
->path
);
957 name2
= mkpathdup("%s:%s", branch2
, b
->path
);
959 base_name
= o
->ancestor
== NULL
? NULL
:
960 mkpathdup("%s", o
->ancestor
);
961 name1
= mkpathdup("%s", branch1
);
962 name2
= mkpathdup("%s", branch2
);
965 read_mmblob(&orig
, &one
->oid
);
966 read_mmblob(&src1
, &a
->oid
);
967 read_mmblob(&src2
, &b
->oid
);
969 merge_status
= ll_merge(result_buf
, a
->path
, &orig
, base_name
,
970 &src1
, name1
, &src2
, name2
, &ll_opts
);
981 static int merge_file_1(struct merge_options
*o
,
982 const struct diff_filespec
*one
,
983 const struct diff_filespec
*a
,
984 const struct diff_filespec
*b
,
987 struct merge_file_info
*result
)
992 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
994 if (S_ISREG(a
->mode
)) {
995 result
->mode
= a
->mode
;
996 oidcpy(&result
->oid
, &a
->oid
);
998 result
->mode
= b
->mode
;
999 oidcpy(&result
->oid
, &b
->oid
);
1002 if (!oid_eq(&a
->oid
, &one
->oid
) && !oid_eq(&b
->oid
, &one
->oid
))
1008 if (a
->mode
== b
->mode
|| a
->mode
== one
->mode
)
1009 result
->mode
= b
->mode
;
1011 result
->mode
= a
->mode
;
1012 if (b
->mode
!= one
->mode
) {
1018 if (oid_eq(&a
->oid
, &b
->oid
) || oid_eq(&a
->oid
, &one
->oid
))
1019 oidcpy(&result
->oid
, &b
->oid
);
1020 else if (oid_eq(&b
->oid
, &one
->oid
))
1021 oidcpy(&result
->oid
, &a
->oid
);
1022 else if (S_ISREG(a
->mode
)) {
1023 mmbuffer_t result_buf
;
1024 int ret
= 0, merge_status
;
1026 merge_status
= merge_3way(o
, &result_buf
, one
, a
, b
,
1029 if ((merge_status
< 0) || !result_buf
.ptr
)
1030 ret
= err(o
, _("Failed to execute internal merge"));
1033 write_object_file(result_buf
.ptr
, result_buf
.size
,
1034 blob_type
, &result
->oid
))
1035 ret
= err(o
, _("Unable to add %s to database"),
1038 free(result_buf
.ptr
);
1041 result
->clean
= (merge_status
== 0);
1042 } else if (S_ISGITLINK(a
->mode
)) {
1043 result
->clean
= merge_submodule(&result
->oid
,
1049 } else if (S_ISLNK(a
->mode
)) {
1050 switch (o
->recursive_variant
) {
1051 case MERGE_RECURSIVE_NORMAL
:
1052 oidcpy(&result
->oid
, &a
->oid
);
1053 if (!oid_eq(&a
->oid
, &b
->oid
))
1056 case MERGE_RECURSIVE_OURS
:
1057 oidcpy(&result
->oid
, &a
->oid
);
1059 case MERGE_RECURSIVE_THEIRS
:
1060 oidcpy(&result
->oid
, &b
->oid
);
1064 die("BUG: unsupported object type in the tree");
1070 static int merge_file_special_markers(struct merge_options
*o
,
1071 const struct diff_filespec
*one
,
1072 const struct diff_filespec
*a
,
1073 const struct diff_filespec
*b
,
1074 const char *branch1
,
1075 const char *filename1
,
1076 const char *branch2
,
1077 const char *filename2
,
1078 struct merge_file_info
*mfi
)
1085 side1
= xstrfmt("%s:%s", branch1
, filename1
);
1087 side2
= xstrfmt("%s:%s", branch2
, filename2
);
1089 ret
= merge_file_1(o
, one
, a
, b
,
1090 side1
? side1
: branch1
,
1091 side2
? side2
: branch2
, mfi
);
1097 static int merge_file_one(struct merge_options
*o
,
1099 const struct object_id
*o_oid
, int o_mode
,
1100 const struct object_id
*a_oid
, int a_mode
,
1101 const struct object_id
*b_oid
, int b_mode
,
1102 const char *branch1
,
1103 const char *branch2
,
1104 struct merge_file_info
*mfi
)
1106 struct diff_filespec one
, a
, b
;
1108 one
.path
= a
.path
= b
.path
= (char *)path
;
1109 oidcpy(&one
.oid
, o_oid
);
1111 oidcpy(&a
.oid
, a_oid
);
1113 oidcpy(&b
.oid
, b_oid
);
1115 return merge_file_1(o
, &one
, &a
, &b
, branch1
, branch2
, mfi
);
1118 static int handle_change_delete(struct merge_options
*o
,
1119 const char *path
, const char *old_path
,
1120 const struct object_id
*o_oid
, int o_mode
,
1121 const struct object_id
*changed_oid
,
1123 const char *change_branch
,
1124 const char *delete_branch
,
1125 const char *change
, const char *change_past
)
1127 char *alt_path
= NULL
;
1128 const char *update_path
= path
;
1131 if (dir_in_way(path
, !o
->call_depth
, 0)) {
1132 update_path
= alt_path
= unique_path(o
, path
, change_branch
);
1135 if (o
->call_depth
) {
1137 * We cannot arbitrarily accept either a_sha or b_sha as
1138 * correct; since there is no true "middle point" between
1139 * them, simply reuse the base version for virtual merge base.
1141 ret
= remove_file_from_cache(path
);
1143 ret
= update_file(o
, 0, o_oid
, o_mode
, update_path
);
1147 output(o
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1148 "and %s in %s. Version %s of %s left in tree."),
1149 change
, path
, delete_branch
, change_past
,
1150 change_branch
, change_branch
, path
);
1152 output(o
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1153 "and %s to %s in %s. Version %s of %s left in tree."),
1154 change
, old_path
, delete_branch
, change_past
, path
,
1155 change_branch
, change_branch
, path
);
1159 output(o
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1160 "and %s in %s. Version %s of %s left in tree at %s."),
1161 change
, path
, delete_branch
, change_past
,
1162 change_branch
, change_branch
, path
, alt_path
);
1164 output(o
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1165 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1166 change
, old_path
, delete_branch
, change_past
, path
,
1167 change_branch
, change_branch
, path
, alt_path
);
1171 * No need to call update_file() on path when change_branch ==
1172 * o->branch1 && !alt_path, since that would needlessly touch
1173 * path. We could call update_file_flags() with update_cache=0
1174 * and update_wd=0, but that's a no-op.
1176 if (change_branch
!= o
->branch1
|| alt_path
)
1177 ret
= update_file(o
, 0, changed_oid
, changed_mode
, update_path
);
1184 static int conflict_rename_delete(struct merge_options
*o
,
1185 struct diff_filepair
*pair
,
1186 const char *rename_branch
,
1187 const char *delete_branch
)
1189 const struct diff_filespec
*orig
= pair
->one
;
1190 const struct diff_filespec
*dest
= pair
->two
;
1192 if (handle_change_delete(o
,
1193 o
->call_depth
? orig
->path
: dest
->path
,
1194 o
->call_depth
? NULL
: orig
->path
,
1195 &orig
->oid
, orig
->mode
,
1196 &dest
->oid
, dest
->mode
,
1197 rename_branch
, delete_branch
,
1198 _("rename"), _("renamed")))
1202 return remove_file_from_cache(dest
->path
);
1204 return update_stages(o
, dest
->path
, NULL
,
1205 rename_branch
== o
->branch1
? dest
: NULL
,
1206 rename_branch
== o
->branch1
? NULL
: dest
);
1209 static struct diff_filespec
*filespec_from_entry(struct diff_filespec
*target
,
1210 struct stage_data
*entry
,
1213 struct object_id
*oid
= &entry
->stages
[stage
].oid
;
1214 unsigned mode
= entry
->stages
[stage
].mode
;
1215 if (mode
== 0 || is_null_oid(oid
))
1217 oidcpy(&target
->oid
, oid
);
1218 target
->mode
= mode
;
1222 static int handle_file(struct merge_options
*o
,
1223 struct diff_filespec
*rename
,
1225 struct rename_conflict_info
*ci
)
1227 char *dst_name
= rename
->path
;
1228 struct stage_data
*dst_entry
;
1229 const char *cur_branch
, *other_branch
;
1230 struct diff_filespec other
;
1231 struct diff_filespec
*add
;
1235 dst_entry
= ci
->dst_entry1
;
1236 cur_branch
= ci
->branch1
;
1237 other_branch
= ci
->branch2
;
1239 dst_entry
= ci
->dst_entry2
;
1240 cur_branch
= ci
->branch2
;
1241 other_branch
= ci
->branch1
;
1244 add
= filespec_from_entry(&other
, dst_entry
, stage
^ 1);
1246 char *add_name
= unique_path(o
, rename
->path
, other_branch
);
1247 if (update_file(o
, 0, &add
->oid
, add
->mode
, add_name
))
1250 remove_file(o
, 0, rename
->path
, 0);
1251 dst_name
= unique_path(o
, rename
->path
, cur_branch
);
1253 if (dir_in_way(rename
->path
, !o
->call_depth
, 0)) {
1254 dst_name
= unique_path(o
, rename
->path
, cur_branch
);
1255 output(o
, 1, _("%s is a directory in %s adding as %s instead"),
1256 rename
->path
, other_branch
, dst_name
);
1259 if ((ret
= update_file(o
, 0, &rename
->oid
, rename
->mode
, dst_name
)))
1260 ; /* fall through, do allow dst_name to be released */
1261 else if (stage
== 2)
1262 ret
= update_stages(o
, rename
->path
, NULL
, rename
, add
);
1264 ret
= update_stages(o
, rename
->path
, NULL
, add
, rename
);
1266 if (dst_name
!= rename
->path
)
1272 static int conflict_rename_rename_1to2(struct merge_options
*o
,
1273 struct rename_conflict_info
*ci
)
1275 /* One file was renamed in both branches, but to different names. */
1276 struct diff_filespec
*one
= ci
->pair1
->one
;
1277 struct diff_filespec
*a
= ci
->pair1
->two
;
1278 struct diff_filespec
*b
= ci
->pair2
->two
;
1280 output(o
, 1, _("CONFLICT (rename/rename): "
1281 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1282 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1283 one
->path
, a
->path
, ci
->branch1
,
1284 one
->path
, b
->path
, ci
->branch2
,
1285 o
->call_depth
? _(" (left unresolved)") : "");
1286 if (o
->call_depth
) {
1287 struct merge_file_info mfi
;
1288 struct diff_filespec other
;
1289 struct diff_filespec
*add
;
1290 if (merge_file_one(o
, one
->path
,
1291 &one
->oid
, one
->mode
,
1294 ci
->branch1
, ci
->branch2
, &mfi
))
1298 * FIXME: For rename/add-source conflicts (if we could detect
1299 * such), this is wrong. We should instead find a unique
1300 * pathname and then either rename the add-source file to that
1301 * unique path, or use that unique path instead of src here.
1303 if (update_file(o
, 0, &mfi
.oid
, mfi
.mode
, one
->path
))
1307 * Above, we put the merged content at the merge-base's
1308 * path. Now we usually need to delete both a->path and
1309 * b->path. However, the rename on each side of the merge
1310 * could also be involved in a rename/add conflict. In
1311 * such cases, we should keep the added file around,
1312 * resolving the conflict at that path in its favor.
1314 add
= filespec_from_entry(&other
, ci
->dst_entry1
, 2 ^ 1);
1316 if (update_file(o
, 0, &add
->oid
, add
->mode
, a
->path
))
1320 remove_file_from_cache(a
->path
);
1321 add
= filespec_from_entry(&other
, ci
->dst_entry2
, 3 ^ 1);
1323 if (update_file(o
, 0, &add
->oid
, add
->mode
, b
->path
))
1327 remove_file_from_cache(b
->path
);
1328 } else if (handle_file(o
, a
, 2, ci
) || handle_file(o
, b
, 3, ci
))
1334 static int conflict_rename_rename_2to1(struct merge_options
*o
,
1335 struct rename_conflict_info
*ci
)
1337 /* Two files, a & b, were renamed to the same thing, c. */
1338 struct diff_filespec
*a
= ci
->pair1
->one
;
1339 struct diff_filespec
*b
= ci
->pair2
->one
;
1340 struct diff_filespec
*c1
= ci
->pair1
->two
;
1341 struct diff_filespec
*c2
= ci
->pair2
->two
;
1342 char *path
= c1
->path
; /* == c2->path */
1343 struct merge_file_info mfi_c1
;
1344 struct merge_file_info mfi_c2
;
1347 output(o
, 1, _("CONFLICT (rename/rename): "
1348 "Rename %s->%s in %s. "
1349 "Rename %s->%s in %s"),
1350 a
->path
, c1
->path
, ci
->branch1
,
1351 b
->path
, c2
->path
, ci
->branch2
);
1353 remove_file(o
, 1, a
->path
, o
->call_depth
|| would_lose_untracked(a
->path
));
1354 remove_file(o
, 1, b
->path
, o
->call_depth
|| would_lose_untracked(b
->path
));
1356 if (merge_file_special_markers(o
, a
, c1
, &ci
->ren1_other
,
1357 o
->branch1
, c1
->path
,
1358 o
->branch2
, ci
->ren1_other
.path
, &mfi_c1
) ||
1359 merge_file_special_markers(o
, b
, &ci
->ren2_other
, c2
,
1360 o
->branch1
, ci
->ren2_other
.path
,
1361 o
->branch2
, c2
->path
, &mfi_c2
))
1364 if (o
->call_depth
) {
1366 * If mfi_c1.clean && mfi_c2.clean, then it might make
1367 * sense to do a two-way merge of those results. But, I
1368 * think in all cases, it makes sense to have the virtual
1369 * merge base just undo the renames; they can be detected
1370 * again later for the non-recursive merge.
1372 remove_file(o
, 0, path
, 0);
1373 ret
= update_file(o
, 0, &mfi_c1
.oid
, mfi_c1
.mode
, a
->path
);
1375 ret
= update_file(o
, 0, &mfi_c2
.oid
, mfi_c2
.mode
,
1378 char *new_path1
= unique_path(o
, path
, ci
->branch1
);
1379 char *new_path2
= unique_path(o
, path
, ci
->branch2
);
1380 output(o
, 1, _("Renaming %s to %s and %s to %s instead"),
1381 a
->path
, new_path1
, b
->path
, new_path2
);
1382 remove_file(o
, 0, path
, 0);
1383 ret
= update_file(o
, 0, &mfi_c1
.oid
, mfi_c1
.mode
, new_path1
);
1385 ret
= update_file(o
, 0, &mfi_c2
.oid
, mfi_c2
.mode
,
1394 static int process_renames(struct merge_options
*o
,
1395 struct string_list
*a_renames
,
1396 struct string_list
*b_renames
)
1398 int clean_merge
= 1, i
, j
;
1399 struct string_list a_by_dst
= STRING_LIST_INIT_NODUP
;
1400 struct string_list b_by_dst
= STRING_LIST_INIT_NODUP
;
1401 const struct rename
*sre
;
1403 for (i
= 0; i
< a_renames
->nr
; i
++) {
1404 sre
= a_renames
->items
[i
].util
;
1405 string_list_insert(&a_by_dst
, sre
->pair
->two
->path
)->util
1408 for (i
= 0; i
< b_renames
->nr
; i
++) {
1409 sre
= b_renames
->items
[i
].util
;
1410 string_list_insert(&b_by_dst
, sre
->pair
->two
->path
)->util
1414 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
1415 struct string_list
*renames1
, *renames2Dst
;
1416 struct rename
*ren1
= NULL
, *ren2
= NULL
;
1417 const char *branch1
, *branch2
;
1418 const char *ren1_src
, *ren1_dst
;
1419 struct string_list_item
*lookup
;
1421 if (i
>= a_renames
->nr
) {
1422 ren2
= b_renames
->items
[j
++].util
;
1423 } else if (j
>= b_renames
->nr
) {
1424 ren1
= a_renames
->items
[i
++].util
;
1426 int compare
= strcmp(a_renames
->items
[i
].string
,
1427 b_renames
->items
[j
].string
);
1429 ren1
= a_renames
->items
[i
++].util
;
1431 ren2
= b_renames
->items
[j
++].util
;
1434 /* TODO: refactor, so that 1/2 are not needed */
1436 renames1
= a_renames
;
1437 renames2Dst
= &b_by_dst
;
1438 branch1
= o
->branch1
;
1439 branch2
= o
->branch2
;
1441 renames1
= b_renames
;
1442 renames2Dst
= &a_by_dst
;
1443 branch1
= o
->branch2
;
1444 branch2
= o
->branch1
;
1448 if (ren1
->processed
)
1450 ren1
->processed
= 1;
1451 ren1
->dst_entry
->processed
= 1;
1452 /* BUG: We should only mark src_entry as processed if we
1453 * are not dealing with a rename + add-source case.
1455 ren1
->src_entry
->processed
= 1;
1457 ren1_src
= ren1
->pair
->one
->path
;
1458 ren1_dst
= ren1
->pair
->two
->path
;
1461 /* One file renamed on both sides */
1462 const char *ren2_src
= ren2
->pair
->one
->path
;
1463 const char *ren2_dst
= ren2
->pair
->two
->path
;
1464 enum rename_type rename_type
;
1465 if (strcmp(ren1_src
, ren2_src
) != 0)
1466 die("BUG: ren1_src != ren2_src");
1467 ren2
->dst_entry
->processed
= 1;
1468 ren2
->processed
= 1;
1469 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
1470 rename_type
= RENAME_ONE_FILE_TO_TWO
;
1473 rename_type
= RENAME_ONE_FILE_TO_ONE
;
1474 /* BUG: We should only remove ren1_src in
1475 * the base stage (think of rename +
1476 * add-source cases).
1478 remove_file(o
, 1, ren1_src
, 1);
1479 update_entry(ren1
->dst_entry
,
1484 setup_rename_conflict_info(rename_type
,
1494 } else if ((lookup
= string_list_lookup(renames2Dst
, ren1_dst
))) {
1495 /* Two different files renamed to the same thing */
1497 ren2
= lookup
->util
;
1498 ren2_dst
= ren2
->pair
->two
->path
;
1499 if (strcmp(ren1_dst
, ren2_dst
) != 0)
1500 die("BUG: ren1_dst != ren2_dst");
1503 ren2
->processed
= 1;
1505 * BUG: We should only mark src_entry as processed
1506 * if we are not dealing with a rename + add-source
1509 ren2
->src_entry
->processed
= 1;
1511 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE
,
1523 /* Renamed in 1, maybe changed in 2 */
1524 /* we only use sha1 and mode of these */
1525 struct diff_filespec src_other
, dst_other
;
1529 * unpack_trees loads entries from common-commit
1530 * into stage 1, from head-commit into stage 2, and
1531 * from merge-commit into stage 3. We keep track
1532 * of which side corresponds to the rename.
1534 int renamed_stage
= a_renames
== renames1
? 2 : 3;
1535 int other_stage
= a_renames
== renames1
? 3 : 2;
1537 /* BUG: We should only remove ren1_src in the base
1538 * stage and in other_stage (think of rename +
1541 remove_file(o
, 1, ren1_src
,
1542 renamed_stage
== 2 || !was_tracked(ren1_src
));
1544 oidcpy(&src_other
.oid
,
1545 &ren1
->src_entry
->stages
[other_stage
].oid
);
1546 src_other
.mode
= ren1
->src_entry
->stages
[other_stage
].mode
;
1547 oidcpy(&dst_other
.oid
,
1548 &ren1
->dst_entry
->stages
[other_stage
].oid
);
1549 dst_other
.mode
= ren1
->dst_entry
->stages
[other_stage
].mode
;
1552 if (oid_eq(&src_other
.oid
, &null_oid
)) {
1553 setup_rename_conflict_info(RENAME_DELETE
,
1563 } else if ((dst_other
.mode
== ren1
->pair
->two
->mode
) &&
1564 oid_eq(&dst_other
.oid
, &ren1
->pair
->two
->oid
)) {
1566 * Added file on the other side identical to
1567 * the file being renamed: clean merge.
1568 * Also, there is no need to overwrite the
1569 * file already in the working copy, so call
1570 * update_file_flags() instead of
1573 if (update_file_flags(o
,
1574 &ren1
->pair
->two
->oid
,
1575 ren1
->pair
->two
->mode
,
1577 1, /* update_cache */
1580 } else if (!oid_eq(&dst_other
.oid
, &null_oid
)) {
1583 output(o
, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
1585 ren1_src
, ren1_dst
, branch1
,
1587 if (o
->call_depth
) {
1588 struct merge_file_info mfi
;
1589 if (merge_file_one(o
, ren1_dst
, &null_oid
, 0,
1590 &ren1
->pair
->two
->oid
,
1591 ren1
->pair
->two
->mode
,
1594 branch1
, branch2
, &mfi
)) {
1596 goto cleanup_and_return
;
1598 output(o
, 1, _("Adding merged %s"), ren1_dst
);
1599 if (update_file(o
, 0, &mfi
.oid
,
1600 mfi
.mode
, ren1_dst
))
1604 char *new_path
= unique_path(o
, ren1_dst
, branch2
);
1605 output(o
, 1, _("Adding as %s instead"), new_path
);
1606 if (update_file(o
, 0, &dst_other
.oid
,
1607 dst_other
.mode
, new_path
))
1614 if (clean_merge
< 0)
1615 goto cleanup_and_return
;
1617 struct diff_filespec
*one
, *a
, *b
;
1618 src_other
.path
= (char *)ren1_src
;
1620 one
= ren1
->pair
->one
;
1621 if (a_renames
== renames1
) {
1622 a
= ren1
->pair
->two
;
1625 b
= ren1
->pair
->two
;
1628 update_entry(ren1
->dst_entry
, one
, a
, b
);
1629 setup_rename_conflict_info(RENAME_NORMAL
,
1643 string_list_clear(&a_by_dst
, 0);
1644 string_list_clear(&b_by_dst
, 0);
1649 static struct object_id
*stage_oid(const struct object_id
*oid
, unsigned mode
)
1651 return (is_null_oid(oid
) || mode
== 0) ? NULL
: (struct object_id
*)oid
;
1654 static int read_oid_strbuf(struct merge_options
*o
,
1655 const struct object_id
*oid
, struct strbuf
*dst
)
1658 enum object_type type
;
1660 buf
= read_object_file(oid
, &type
, &size
);
1662 return err(o
, _("cannot read object %s"), oid_to_hex(oid
));
1663 if (type
!= OBJ_BLOB
) {
1665 return err(o
, _("object %s is not a blob"), oid_to_hex(oid
));
1667 strbuf_attach(dst
, buf
, size
, size
+ 1);
1671 static int blob_unchanged(struct merge_options
*opt
,
1672 const struct object_id
*o_oid
,
1674 const struct object_id
*a_oid
,
1676 int renormalize
, const char *path
)
1678 struct strbuf o
= STRBUF_INIT
;
1679 struct strbuf a
= STRBUF_INIT
;
1680 int ret
= 0; /* assume changed for safety */
1682 if (a_mode
!= o_mode
)
1684 if (oid_eq(o_oid
, a_oid
))
1689 assert(o_oid
&& a_oid
);
1690 if (read_oid_strbuf(opt
, o_oid
, &o
) || read_oid_strbuf(opt
, a_oid
, &a
))
1693 * Note: binary | is used so that both renormalizations are
1694 * performed. Comparison can be skipped if both files are
1695 * unchanged since their sha1s have already been compared.
1697 if (renormalize_buffer(&the_index
, path
, o
.buf
, o
.len
, &o
) |
1698 renormalize_buffer(&the_index
, path
, a
.buf
, a
.len
, &a
))
1699 ret
= (o
.len
== a
.len
&& !memcmp(o
.buf
, a
.buf
, o
.len
));
1707 static int handle_modify_delete(struct merge_options
*o
,
1709 struct object_id
*o_oid
, int o_mode
,
1710 struct object_id
*a_oid
, int a_mode
,
1711 struct object_id
*b_oid
, int b_mode
)
1713 const char *modify_branch
, *delete_branch
;
1714 struct object_id
*changed_oid
;
1718 modify_branch
= o
->branch1
;
1719 delete_branch
= o
->branch2
;
1720 changed_oid
= a_oid
;
1721 changed_mode
= a_mode
;
1723 modify_branch
= o
->branch2
;
1724 delete_branch
= o
->branch1
;
1725 changed_oid
= b_oid
;
1726 changed_mode
= b_mode
;
1729 return handle_change_delete(o
,
1732 changed_oid
, changed_mode
,
1733 modify_branch
, delete_branch
,
1734 _("modify"), _("modified"));
1737 static int merge_content(struct merge_options
*o
,
1739 struct object_id
*o_oid
, int o_mode
,
1740 struct object_id
*a_oid
, int a_mode
,
1741 struct object_id
*b_oid
, int b_mode
,
1742 struct rename_conflict_info
*rename_conflict_info
)
1744 const char *reason
= _("content");
1745 const char *path1
= NULL
, *path2
= NULL
;
1746 struct merge_file_info mfi
;
1747 struct diff_filespec one
, a
, b
;
1748 unsigned df_conflict_remains
= 0;
1751 reason
= _("add/add");
1752 o_oid
= (struct object_id
*)&null_oid
;
1754 one
.path
= a
.path
= b
.path
= (char *)path
;
1755 oidcpy(&one
.oid
, o_oid
);
1757 oidcpy(&a
.oid
, a_oid
);
1759 oidcpy(&b
.oid
, b_oid
);
1762 if (rename_conflict_info
) {
1763 struct diff_filepair
*pair1
= rename_conflict_info
->pair1
;
1765 path1
= (o
->branch1
== rename_conflict_info
->branch1
) ?
1766 pair1
->two
->path
: pair1
->one
->path
;
1767 /* If rename_conflict_info->pair2 != NULL, we are in
1768 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a
1771 path2
= (rename_conflict_info
->pair2
||
1772 o
->branch2
== rename_conflict_info
->branch1
) ?
1773 pair1
->two
->path
: pair1
->one
->path
;
1775 if (dir_in_way(path
, !o
->call_depth
,
1776 S_ISGITLINK(pair1
->two
->mode
)))
1777 df_conflict_remains
= 1;
1779 if (merge_file_special_markers(o
, &one
, &a
, &b
,
1781 o
->branch2
, path2
, &mfi
))
1784 if (mfi
.clean
&& !df_conflict_remains
&&
1785 oid_eq(&mfi
.oid
, a_oid
) && mfi
.mode
== a_mode
) {
1786 int path_renamed_outside_HEAD
;
1787 output(o
, 3, _("Skipped %s (merged same as existing)"), path
);
1789 * The content merge resulted in the same file contents we
1790 * already had. We can return early if those file contents
1791 * are recorded at the correct path (which may not be true
1792 * if the merge involves a rename).
1794 path_renamed_outside_HEAD
= !path2
|| !strcmp(path
, path2
);
1795 if (!path_renamed_outside_HEAD
) {
1796 add_cacheinfo(o
, mfi
.mode
, &mfi
.oid
, path
,
1797 0, (!o
->call_depth
), 0);
1801 output(o
, 2, _("Auto-merging %s"), path
);
1804 if (S_ISGITLINK(mfi
.mode
))
1805 reason
= _("submodule");
1806 output(o
, 1, _("CONFLICT (%s): Merge conflict in %s"),
1808 if (rename_conflict_info
&& !df_conflict_remains
)
1809 if (update_stages(o
, path
, &one
, &a
, &b
))
1813 if (df_conflict_remains
) {
1815 if (o
->call_depth
) {
1816 remove_file_from_cache(path
);
1819 if (update_stages(o
, path
, &one
, &a
, &b
))
1822 int file_from_stage2
= was_tracked(path
);
1823 struct diff_filespec merged
;
1824 oidcpy(&merged
.oid
, &mfi
.oid
);
1825 merged
.mode
= mfi
.mode
;
1827 if (update_stages(o
, path
, NULL
,
1828 file_from_stage2
? &merged
: NULL
,
1829 file_from_stage2
? NULL
: &merged
))
1834 new_path
= unique_path(o
, path
, rename_conflict_info
->branch1
);
1835 output(o
, 1, _("Adding as %s instead"), new_path
);
1836 if (update_file(o
, 0, &mfi
.oid
, mfi
.mode
, new_path
)) {
1842 } else if (update_file(o
, mfi
.clean
, &mfi
.oid
, mfi
.mode
, path
))
1847 /* Per entry merge function */
1848 static int process_entry(struct merge_options
*o
,
1849 const char *path
, struct stage_data
*entry
)
1851 int clean_merge
= 1;
1852 int normalize
= o
->renormalize
;
1853 unsigned o_mode
= entry
->stages
[1].mode
;
1854 unsigned a_mode
= entry
->stages
[2].mode
;
1855 unsigned b_mode
= entry
->stages
[3].mode
;
1856 struct object_id
*o_oid
= stage_oid(&entry
->stages
[1].oid
, o_mode
);
1857 struct object_id
*a_oid
= stage_oid(&entry
->stages
[2].oid
, a_mode
);
1858 struct object_id
*b_oid
= stage_oid(&entry
->stages
[3].oid
, b_mode
);
1860 entry
->processed
= 1;
1861 if (entry
->rename_conflict_info
) {
1862 struct rename_conflict_info
*conflict_info
= entry
->rename_conflict_info
;
1863 switch (conflict_info
->rename_type
) {
1865 case RENAME_ONE_FILE_TO_ONE
:
1866 clean_merge
= merge_content(o
, path
,
1867 o_oid
, o_mode
, a_oid
, a_mode
, b_oid
, b_mode
,
1872 if (conflict_rename_delete(o
,
1873 conflict_info
->pair1
,
1874 conflict_info
->branch1
,
1875 conflict_info
->branch2
))
1878 case RENAME_ONE_FILE_TO_TWO
:
1880 if (conflict_rename_rename_1to2(o
, conflict_info
))
1883 case RENAME_TWO_FILES_TO_ONE
:
1885 if (conflict_rename_rename_2to1(o
, conflict_info
))
1889 entry
->processed
= 0;
1892 } else if (o_oid
&& (!a_oid
|| !b_oid
)) {
1893 /* Case A: Deleted in one */
1894 if ((!a_oid
&& !b_oid
) ||
1895 (!b_oid
&& blob_unchanged(o
, o_oid
, o_mode
, a_oid
, a_mode
, normalize
, path
)) ||
1896 (!a_oid
&& blob_unchanged(o
, o_oid
, o_mode
, b_oid
, b_mode
, normalize
, path
))) {
1897 /* Deleted in both or deleted in one and
1898 * unchanged in the other */
1900 output(o
, 2, _("Removing %s"), path
);
1901 /* do not touch working file if it did not exist */
1902 remove_file(o
, 1, path
, !a_oid
);
1904 /* Modify/delete; deleted side may have put a directory in the way */
1906 if (handle_modify_delete(o
, path
, o_oid
, o_mode
,
1907 a_oid
, a_mode
, b_oid
, b_mode
))
1910 } else if ((!o_oid
&& a_oid
&& !b_oid
) ||
1911 (!o_oid
&& !a_oid
&& b_oid
)) {
1912 /* Case B: Added in one. */
1913 /* [nothing|directory] -> ([nothing|directory], file) */
1915 const char *add_branch
;
1916 const char *other_branch
;
1918 const struct object_id
*oid
;
1922 add_branch
= o
->branch1
;
1923 other_branch
= o
->branch2
;
1926 conf
= _("file/directory");
1928 add_branch
= o
->branch2
;
1929 other_branch
= o
->branch1
;
1932 conf
= _("directory/file");
1934 if (dir_in_way(path
,
1935 !o
->call_depth
&& !S_ISGITLINK(a_mode
),
1937 char *new_path
= unique_path(o
, path
, add_branch
);
1939 output(o
, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
1941 conf
, path
, other_branch
, path
, new_path
);
1942 if (update_file(o
, 0, oid
, mode
, new_path
))
1944 else if (o
->call_depth
)
1945 remove_file_from_cache(path
);
1948 output(o
, 2, _("Adding %s"), path
);
1949 /* do not overwrite file if already present */
1950 if (update_file_flags(o
, oid
, mode
, path
, 1, !a_oid
))
1953 } else if (a_oid
&& b_oid
) {
1954 /* Case C: Added in both (check for same permissions) and */
1955 /* case D: Modified in both, but differently. */
1956 clean_merge
= merge_content(o
, path
,
1957 o_oid
, o_mode
, a_oid
, a_mode
, b_oid
, b_mode
,
1959 } else if (!o_oid
&& !a_oid
&& !b_oid
) {
1961 * this entry was deleted altogether. a_mode == 0 means
1962 * we had that path and want to actively remove it.
1964 remove_file(o
, 1, path
, !a_mode
);
1966 die("BUG: fatal merge failure, shouldn't happen.");
1971 int merge_trees(struct merge_options
*o
,
1974 struct tree
*common
,
1975 struct tree
**result
)
1979 if (o
->subtree_shift
) {
1980 merge
= shift_tree_object(head
, merge
, o
->subtree_shift
);
1981 common
= shift_tree_object(head
, common
, o
->subtree_shift
);
1984 if (oid_eq(&common
->object
.oid
, &merge
->object
.oid
)) {
1985 struct strbuf sb
= STRBUF_INIT
;
1987 if (!o
->call_depth
&& index_has_changes(&sb
)) {
1988 err(o
, _("Dirty index: cannot merge (dirty: %s)"),
1992 output(o
, 0, _("Already up to date!"));
1997 code
= git_merge_trees(o
->call_depth
, common
, head
, merge
);
2000 if (show(o
, 4) || o
->call_depth
)
2001 err(o
, _("merging of trees %s and %s failed"),
2002 oid_to_hex(&head
->object
.oid
),
2003 oid_to_hex(&merge
->object
.oid
));
2007 if (unmerged_cache()) {
2008 struct string_list
*entries
, *re_head
, *re_merge
;
2011 * Only need the hashmap while processing entries, so
2012 * initialize it here and free it when we are done running
2013 * through the entries. Keeping it in the merge_options as
2014 * opposed to decaring a local hashmap is for convenience
2015 * so that we don't have to pass it to around.
2017 hashmap_init(&o
->current_file_dir_set
, path_hashmap_cmp
, NULL
, 512);
2018 get_files_dirs(o
, head
);
2019 get_files_dirs(o
, merge
);
2021 entries
= get_unmerged();
2022 re_head
= get_renames(o
, head
, common
, head
, merge
, entries
);
2023 re_merge
= get_renames(o
, merge
, common
, head
, merge
, entries
);
2024 clean
= process_renames(o
, re_head
, re_merge
);
2025 record_df_conflict_files(o
, entries
);
2028 for (i
= entries
->nr
-1; 0 <= i
; i
--) {
2029 const char *path
= entries
->items
[i
].string
;
2030 struct stage_data
*e
= entries
->items
[i
].util
;
2031 if (!e
->processed
) {
2032 int ret
= process_entry(o
, path
, e
);
2041 for (i
= 0; i
< entries
->nr
; i
++) {
2042 struct stage_data
*e
= entries
->items
[i
].util
;
2044 die("BUG: unprocessed path??? %s",
2045 entries
->items
[i
].string
);
2049 string_list_clear(re_merge
, 0);
2050 string_list_clear(re_head
, 0);
2051 string_list_clear(entries
, 1);
2053 hashmap_free(&o
->current_file_dir_set
, 1);
2065 if (o
->call_depth
&& !(*result
= write_tree_from_memory(o
)))
2071 static struct commit_list
*reverse_commit_list(struct commit_list
*list
)
2073 struct commit_list
*next
= NULL
, *current
, *backup
;
2074 for (current
= list
; current
; current
= backup
) {
2075 backup
= current
->next
;
2076 current
->next
= next
;
2083 * Merge the commits h1 and h2, return the resulting virtual
2084 * commit object and a flag indicating the cleanness of the merge.
2086 int merge_recursive(struct merge_options
*o
,
2089 struct commit_list
*ca
,
2090 struct commit
**result
)
2092 struct commit_list
*iter
;
2093 struct commit
*merged_common_ancestors
;
2094 struct tree
*mrtree
;
2098 output(o
, 4, _("Merging:"));
2099 output_commit_title(o
, h1
);
2100 output_commit_title(o
, h2
);
2104 ca
= get_merge_bases(h1
, h2
);
2105 ca
= reverse_commit_list(ca
);
2109 unsigned cnt
= commit_list_count(ca
);
2111 output(o
, 5, Q_("found %u common ancestor:",
2112 "found %u common ancestors:", cnt
), cnt
);
2113 for (iter
= ca
; iter
; iter
= iter
->next
)
2114 output_commit_title(o
, iter
->item
);
2117 merged_common_ancestors
= pop_commit(&ca
);
2118 if (merged_common_ancestors
== NULL
) {
2119 /* if there is no common ancestor, use an empty tree */
2122 tree
= lookup_tree(the_hash_algo
->empty_tree
);
2123 merged_common_ancestors
= make_virtual_commit(tree
, "ancestor");
2126 for (iter
= ca
; iter
; iter
= iter
->next
) {
2127 const char *saved_b1
, *saved_b2
;
2130 * When the merge fails, the result contains files
2131 * with conflict markers. The cleanness flag is
2132 * ignored (unless indicating an error), it was never
2133 * actually used, as result of merge_trees has always
2134 * overwritten it: the committed "conflicts" were
2138 saved_b1
= o
->branch1
;
2139 saved_b2
= o
->branch2
;
2140 o
->branch1
= "Temporary merge branch 1";
2141 o
->branch2
= "Temporary merge branch 2";
2142 if (merge_recursive(o
, merged_common_ancestors
, iter
->item
,
2143 NULL
, &merged_common_ancestors
) < 0)
2145 o
->branch1
= saved_b1
;
2146 o
->branch2
= saved_b2
;
2149 if (!merged_common_ancestors
)
2150 return err(o
, _("merge returned no commit"));
2157 o
->ancestor
= "merged common ancestors";
2158 clean
= merge_trees(o
, h1
->tree
, h2
->tree
, merged_common_ancestors
->tree
,
2165 if (o
->call_depth
) {
2166 *result
= make_virtual_commit(mrtree
, "merged tree");
2167 commit_list_insert(h1
, &(*result
)->parents
);
2168 commit_list_insert(h2
, &(*result
)->parents
->next
);
2171 if (!o
->call_depth
&& o
->buffer_output
< 2)
2172 strbuf_release(&o
->obuf
);
2174 diff_warn_rename_limit("merge.renamelimit",
2175 o
->needed_rename_limit
, 0);
2179 static struct commit
*get_ref(const struct object_id
*oid
, const char *name
)
2181 struct object
*object
;
2183 object
= deref_tag(parse_object(oid
), name
, strlen(name
));
2186 if (object
->type
== OBJ_TREE
)
2187 return make_virtual_commit((struct tree
*)object
, name
);
2188 if (object
->type
!= OBJ_COMMIT
)
2190 if (parse_commit((struct commit
*)object
))
2192 return (struct commit
*)object
;
2195 int merge_recursive_generic(struct merge_options
*o
,
2196 const struct object_id
*head
,
2197 const struct object_id
*merge
,
2199 const struct object_id
**base_list
,
2200 struct commit
**result
)
2203 struct lock_file lock
= LOCK_INIT
;
2204 struct commit
*head_commit
= get_ref(head
, o
->branch1
);
2205 struct commit
*next_commit
= get_ref(merge
, o
->branch2
);
2206 struct commit_list
*ca
= NULL
;
2210 for (i
= 0; i
< num_base_list
; ++i
) {
2211 struct commit
*base
;
2212 if (!(base
= get_ref(base_list
[i
], oid_to_hex(base_list
[i
]))))
2213 return err(o
, _("Could not parse object '%s'"),
2214 oid_to_hex(base_list
[i
]));
2215 commit_list_insert(base
, &ca
);
2219 hold_locked_index(&lock
, LOCK_DIE_ON_ERROR
);
2220 clean
= merge_recursive(o
, head_commit
, next_commit
, ca
,
2223 rollback_lock_file(&lock
);
2227 if (write_locked_index(&the_index
, &lock
,
2228 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
2229 return err(o
, _("Unable to write index."));
2231 return clean
? 0 : 1;
2234 static void merge_recursive_config(struct merge_options
*o
)
2236 git_config_get_int("merge.verbosity", &o
->verbosity
);
2237 git_config_get_int("diff.renamelimit", &o
->diff_rename_limit
);
2238 git_config_get_int("merge.renamelimit", &o
->merge_rename_limit
);
2239 git_config(git_xmerge_config
, NULL
);
2242 void init_merge_options(struct merge_options
*o
)
2244 const char *merge_verbosity
;
2245 memset(o
, 0, sizeof(struct merge_options
));
2247 o
->buffer_output
= 1;
2248 o
->diff_rename_limit
= -1;
2249 o
->merge_rename_limit
= -1;
2251 o
->detect_rename
= 1;
2252 merge_recursive_config(o
);
2253 merge_verbosity
= getenv("GIT_MERGE_VERBOSITY");
2254 if (merge_verbosity
)
2255 o
->verbosity
= strtol(merge_verbosity
, NULL
, 10);
2256 if (o
->verbosity
>= 5)
2257 o
->buffer_output
= 0;
2258 strbuf_init(&o
->obuf
, 0);
2259 string_list_init(&o
->df_conflict_file_set
, 1);
2262 int parse_merge_opt(struct merge_options
*o
, const char *s
)
2268 if (!strcmp(s
, "ours"))
2269 o
->recursive_variant
= MERGE_RECURSIVE_OURS
;
2270 else if (!strcmp(s
, "theirs"))
2271 o
->recursive_variant
= MERGE_RECURSIVE_THEIRS
;
2272 else if (!strcmp(s
, "subtree"))
2273 o
->subtree_shift
= "";
2274 else if (skip_prefix(s
, "subtree=", &arg
))
2275 o
->subtree_shift
= arg
;
2276 else if (!strcmp(s
, "patience"))
2277 o
->xdl_opts
= DIFF_WITH_ALG(o
, PATIENCE_DIFF
);
2278 else if (!strcmp(s
, "histogram"))
2279 o
->xdl_opts
= DIFF_WITH_ALG(o
, HISTOGRAM_DIFF
);
2280 else if (skip_prefix(s
, "diff-algorithm=", &arg
)) {
2281 long value
= parse_algorithm_value(arg
);
2284 /* clear out previous settings */
2285 DIFF_XDL_CLR(o
, NEED_MINIMAL
);
2286 o
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
2287 o
->xdl_opts
|= value
;
2289 else if (!strcmp(s
, "ignore-space-change"))
2290 DIFF_XDL_SET(o
, IGNORE_WHITESPACE_CHANGE
);
2291 else if (!strcmp(s
, "ignore-all-space"))
2292 DIFF_XDL_SET(o
, IGNORE_WHITESPACE
);
2293 else if (!strcmp(s
, "ignore-space-at-eol"))
2294 DIFF_XDL_SET(o
, IGNORE_WHITESPACE_AT_EOL
);
2295 else if (!strcmp(s
, "ignore-cr-at-eol"))
2296 DIFF_XDL_SET(o
, IGNORE_CR_AT_EOL
);
2297 else if (!strcmp(s
, "renormalize"))
2299 else if (!strcmp(s
, "no-renormalize"))
2301 else if (!strcmp(s
, "no-renames"))
2302 o
->detect_rename
= 0;
2303 else if (!strcmp(s
, "find-renames")) {
2304 o
->detect_rename
= 1;
2305 o
->rename_score
= 0;
2307 else if (skip_prefix(s
, "find-renames=", &arg
) ||
2308 skip_prefix(s
, "rename-threshold=", &arg
)) {
2309 if ((o
->rename_score
= parse_rename_score(&arg
)) == -1 || *arg
!= 0)
2311 o
->detect_rename
= 1;