/src/git/merge-recursive.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Recursive Merge algorithm stolen from git-merge-recursive.py by  | 
3  |  |  * Fredrik Kuivinen.  | 
4  |  |  * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006  | 
5  |  |  */  | 
6  |  |  | 
7  |  | #define USE_THE_REPOSITORY_VARIABLE  | 
8  |  |  | 
9  |  | #include "git-compat-util.h"  | 
10  |  | #include "merge-recursive.h"  | 
11  |  |  | 
12  |  | #include "alloc.h"  | 
13  |  | #include "cache-tree.h"  | 
14  |  | #include "commit.h"  | 
15  |  | #include "commit-reach.h"  | 
16  |  | #include "config.h"  | 
17  |  | #include "diff.h"  | 
18  |  | #include "diffcore.h"  | 
19  |  | #include "dir.h"  | 
20  |  | #include "environment.h"  | 
21  |  | #include "gettext.h"  | 
22  |  | #include "hex.h"  | 
23  |  | #include "merge-ll.h"  | 
24  |  | #include "lockfile.h"  | 
25  |  | #include "match-trees.h"  | 
26  |  | #include "name-hash.h"  | 
27  |  | #include "object-file.h"  | 
28  |  | #include "object-name.h"  | 
29  |  | #include "object-store-ll.h"  | 
30  |  | #include "path.h"  | 
31  |  | #include "repository.h"  | 
32  |  | #include "revision.h"  | 
33  |  | #include "sparse-index.h"  | 
34  |  | #include "string-list.h"  | 
35  |  | #include "symlinks.h"  | 
36  |  | #include "tag.h"  | 
37  |  | #include "tree-walk.h"  | 
38  |  | #include "unpack-trees.h"  | 
39  |  | #include "xdiff-interface.h"  | 
40  |  |  | 
41  |  | struct merge_options_internal { | 
42  |  |   int call_depth;  | 
43  |  |   int needed_rename_limit;  | 
44  |  |   struct hashmap current_file_dir_set;  | 
45  |  |   struct string_list df_conflict_file_set;  | 
46  |  |   struct unpack_trees_options unpack_opts;  | 
47  |  |   struct index_state orig_index;  | 
48  |  | };  | 
49  |  |  | 
50  |  | struct path_hashmap_entry { | 
51  |  |   struct hashmap_entry e;  | 
52  |  |   char path[FLEX_ARRAY];  | 
53  |  | };  | 
54  |  |  | 
55  |  | static int path_hashmap_cmp(const void *cmp_data UNUSED,  | 
56  |  |           const struct hashmap_entry *eptr,  | 
57  |  |           const struct hashmap_entry *entry_or_key,  | 
58  |  |           const void *keydata)  | 
59  | 0  | { | 
60  | 0  |   const struct path_hashmap_entry *a, *b;  | 
61  | 0  |   const char *key = keydata;  | 
62  |  | 
  | 
63  | 0  |   a = container_of(eptr, const struct path_hashmap_entry, e);  | 
64  | 0  |   b = container_of(entry_or_key, const struct path_hashmap_entry, e);  | 
65  |  | 
  | 
66  | 0  |   return fspathcmp(a->path, key ? key : b->path);  | 
67  | 0  | }  | 
68  |  |  | 
69  |  | /*  | 
70  |  |  * For dir_rename_entry, directory names are stored as a full path from the  | 
71  |  |  * toplevel of the repository and do not include a trailing '/'.  Also:  | 
72  |  |  *  | 
73  |  |  *   dir:                original name of directory being renamed  | 
74  |  |  *   non_unique_new_dir: if true, could not determine new_dir  | 
75  |  |  *   new_dir:            final name of directory being renamed  | 
76  |  |  *   possible_new_dirs:  temporary used to help determine new_dir; see comments  | 
77  |  |  *                       in get_directory_renames() for details  | 
78  |  |  */  | 
79  |  | struct dir_rename_entry { | 
80  |  |   struct hashmap_entry ent;  | 
81  |  |   char *dir;  | 
82  |  |   unsigned non_unique_new_dir:1;  | 
83  |  |   struct strbuf new_dir;  | 
84  |  |   struct string_list possible_new_dirs;  | 
85  |  | };  | 
86  |  |  | 
87  |  | static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,  | 
88  |  |                   char *dir)  | 
89  | 0  | { | 
90  | 0  |   struct dir_rename_entry key;  | 
91  |  | 
  | 
92  | 0  |   if (!dir)  | 
93  | 0  |     return NULL;  | 
94  | 0  |   hashmap_entry_init(&key.ent, strhash(dir));  | 
95  | 0  |   key.dir = dir;  | 
96  | 0  |   return hashmap_get_entry(hashmap, &key, ent, NULL);  | 
97  | 0  | }  | 
98  |  |  | 
99  |  | static int dir_rename_cmp(const void *cmp_data UNUSED,  | 
100  |  |         const struct hashmap_entry *eptr,  | 
101  |  |         const struct hashmap_entry *entry_or_key,  | 
102  |  |         const void *keydata UNUSED)  | 
103  | 0  | { | 
104  | 0  |   const struct dir_rename_entry *e1, *e2;  | 
105  |  | 
  | 
106  | 0  |   e1 = container_of(eptr, const struct dir_rename_entry, ent);  | 
107  | 0  |   e2 = container_of(entry_or_key, const struct dir_rename_entry, ent);  | 
108  |  | 
  | 
109  | 0  |   return strcmp(e1->dir, e2->dir);  | 
110  | 0  | }  | 
111  |  |  | 
112  |  | static void dir_rename_init(struct hashmap *map)  | 
113  | 0  | { | 
114  | 0  |   hashmap_init(map, dir_rename_cmp, NULL, 0);  | 
115  | 0  | }  | 
116  |  |  | 
117  |  | static void dir_rename_entry_init(struct dir_rename_entry *entry,  | 
118  |  |           char *directory)  | 
119  | 0  | { | 
120  | 0  |   hashmap_entry_init(&entry->ent, strhash(directory));  | 
121  | 0  |   entry->dir = directory;  | 
122  | 0  |   entry->non_unique_new_dir = 0;  | 
123  | 0  |   strbuf_init(&entry->new_dir, 0);  | 
124  | 0  |   string_list_init_nodup(&entry->possible_new_dirs);  | 
125  | 0  | }  | 
126  |  |  | 
127  |  | struct collision_entry { | 
128  |  |   struct hashmap_entry ent;  | 
129  |  |   char *target_file;  | 
130  |  |   struct string_list source_files;  | 
131  |  |   unsigned reported_already:1;  | 
132  |  | };  | 
133  |  |  | 
134  |  | static struct collision_entry *collision_find_entry(struct hashmap *hashmap,  | 
135  |  |                 char *target_file)  | 
136  | 0  | { | 
137  | 0  |   struct collision_entry key;  | 
138  |  | 
  | 
139  | 0  |   hashmap_entry_init(&key.ent, strhash(target_file));  | 
140  | 0  |   key.target_file = target_file;  | 
141  | 0  |   return hashmap_get_entry(hashmap, &key, ent, NULL);  | 
142  | 0  | }  | 
143  |  |  | 
144  |  | static int collision_cmp(const void *cmp_data UNUSED,  | 
145  |  |        const struct hashmap_entry *eptr,  | 
146  |  |        const struct hashmap_entry *entry_or_key,  | 
147  |  |        const void *keydata UNUSED)  | 
148  | 0  | { | 
149  | 0  |   const struct collision_entry *e1, *e2;  | 
150  |  | 
  | 
151  | 0  |   e1 = container_of(eptr, const struct collision_entry, ent);  | 
152  | 0  |   e2 = container_of(entry_or_key, const struct collision_entry, ent);  | 
153  |  | 
  | 
154  | 0  |   return strcmp(e1->target_file, e2->target_file);  | 
155  | 0  | }  | 
156  |  |  | 
157  |  | static void collision_init(struct hashmap *map)  | 
158  | 0  | { | 
159  | 0  |   hashmap_init(map, collision_cmp, NULL, 0);  | 
160  | 0  | }  | 
161  |  |  | 
162  |  | static void flush_output(struct merge_options *opt)  | 
163  | 0  | { | 
164  | 0  |   if (opt->buffer_output < 2 && opt->obuf.len) { | 
165  | 0  |     fputs(opt->obuf.buf, stdout);  | 
166  | 0  |     strbuf_reset(&opt->obuf);  | 
167  | 0  |   }  | 
168  | 0  | }  | 
169  |  |  | 
170  |  | __attribute__((format (printf, 2, 3)))  | 
171  |  | static int err(struct merge_options *opt, const char *err, ...)  | 
172  | 0  | { | 
173  | 0  |   va_list params;  | 
174  |  | 
  | 
175  | 0  |   if (opt->buffer_output < 2)  | 
176  | 0  |     flush_output(opt);  | 
177  | 0  |   else { | 
178  | 0  |     strbuf_complete(&opt->obuf, '\n');  | 
179  | 0  |     strbuf_addstr(&opt->obuf, "error: ");  | 
180  | 0  |   }  | 
181  | 0  |   va_start(params, err);  | 
182  | 0  |   strbuf_vaddf(&opt->obuf, err, params);  | 
183  | 0  |   va_end(params);  | 
184  | 0  |   if (opt->buffer_output > 1)  | 
185  | 0  |     strbuf_addch(&opt->obuf, '\n');  | 
186  | 0  |   else { | 
187  | 0  |     error("%s", opt->obuf.buf); | 
188  | 0  |     strbuf_reset(&opt->obuf);  | 
189  | 0  |   }  | 
190  |  | 
  | 
191  | 0  |   return -1;  | 
192  | 0  | }  | 
193  |  |  | 
194  |  | static struct tree *shift_tree_object(struct repository *repo,  | 
195  |  |               struct tree *one, struct tree *two,  | 
196  |  |               const char *subtree_shift)  | 
197  | 0  | { | 
198  | 0  |   struct object_id shifted;  | 
199  |  | 
  | 
200  | 0  |   if (!*subtree_shift) { | 
201  | 0  |     shift_tree(repo, &one->object.oid, &two->object.oid, &shifted, 0);  | 
202  | 0  |   } else { | 
203  | 0  |     shift_tree_by(repo, &one->object.oid, &two->object.oid, &shifted,  | 
204  | 0  |             subtree_shift);  | 
205  | 0  |   }  | 
206  | 0  |   if (oideq(&two->object.oid, &shifted))  | 
207  | 0  |     return two;  | 
208  | 0  |   return lookup_tree(repo, &shifted);  | 
209  | 0  | }  | 
210  |  |  | 
211  |  | static inline void set_commit_tree(struct commit *c, struct tree *t)  | 
212  | 0  | { | 
213  | 0  |   c->maybe_tree = t;  | 
214  | 0  | }  | 
215  |  |  | 
216  |  | static struct commit *make_virtual_commit(struct repository *repo,  | 
217  |  |             struct tree *tree,  | 
218  |  |             const char *comment)  | 
219  | 0  | { | 
220  | 0  |   struct commit *commit = alloc_commit_node(repo);  | 
221  |  | 
  | 
222  | 0  |   set_merge_remote_desc(commit, comment, (struct object *)commit);  | 
223  | 0  |   set_commit_tree(commit, tree);  | 
224  | 0  |   commit->object.parsed = 1;  | 
225  | 0  |   return commit;  | 
226  | 0  | }  | 
227  |  |  | 
228  |  | enum rename_type { | 
229  |  |   RENAME_NORMAL = 0,  | 
230  |  |   RENAME_VIA_DIR,  | 
231  |  |   RENAME_ADD,  | 
232  |  |   RENAME_DELETE,  | 
233  |  |   RENAME_ONE_FILE_TO_ONE,  | 
234  |  |   RENAME_ONE_FILE_TO_TWO,  | 
235  |  |   RENAME_TWO_FILES_TO_ONE  | 
236  |  | };  | 
237  |  |  | 
238  |  | /*  | 
239  |  |  * Since we want to write the index eventually, we cannot reuse the index  | 
240  |  |  * for these (temporary) data.  | 
241  |  |  */  | 
242  |  | struct stage_data { | 
243  |  |   struct diff_filespec stages[4]; /* mostly for oid & mode; maybe path */  | 
244  |  |   struct rename_conflict_info *rename_conflict_info;  | 
245  |  |   unsigned processed:1,  | 
246  |  |      rename_conflict_info_owned:1;  | 
247  |  | };  | 
248  |  |  | 
249  |  | struct rename { | 
250  |  |   unsigned processed:1;  | 
251  |  |   struct diff_filepair *pair;  | 
252  |  |   const char *branch; /* branch that the rename occurred on */  | 
253  |  |   /*  | 
254  |  |    * If directory rename detection affected this rename, what was its  | 
255  |  |    * original type ('A' or 'R') and it's original destination before | 
256  |  |    * the directory rename (otherwise, '\0' and NULL for these two vars).  | 
257  |  |    */  | 
258  |  |   char dir_rename_original_type;  | 
259  |  |   char *dir_rename_original_dest;  | 
260  |  |   /*  | 
261  |  |    * Purpose of src_entry and dst_entry:  | 
262  |  |    *  | 
263  |  |    * If 'before' is renamed to 'after' then src_entry will contain  | 
264  |  |    * the versions of 'before' from the merge_base, HEAD, and MERGE in  | 
265  |  |    * stages 1, 2, and 3; dst_entry will contain the respective  | 
266  |  |    * versions of 'after' in corresponding locations.  Thus, we have a  | 
267  |  |    * total of six modes and oids, though some will be null.  (Stage 0  | 
268  |  |    * is ignored; we're interested in handling conflicts.)  | 
269  |  |    *  | 
270  |  |    * Since we don't turn on break-rewrites by default, neither  | 
271  |  |    * src_entry nor dst_entry can have all three of their stages have  | 
272  |  |    * non-null oids, meaning at most four of the six will be non-null.  | 
273  |  |    * Also, since this is a rename, both src_entry and dst_entry will  | 
274  |  |    * have at least one non-null oid, meaning at least two will be  | 
275  |  |    * non-null.  Of the six oids, a typical rename will have three be  | 
276  |  |    * non-null.  Only two implies a rename/delete, and four implies a  | 
277  |  |    * rename/add.  | 
278  |  |    */  | 
279  |  |   struct stage_data *src_entry;  | 
280  |  |   struct stage_data *dst_entry;  | 
281  |  | };  | 
282  |  |  | 
283  |  | struct rename_conflict_info { | 
284  |  |   enum rename_type rename_type;  | 
285  |  |   struct rename *ren1;  | 
286  |  |   struct rename *ren2;  | 
287  |  | };  | 
288  |  |  | 
289  |  | static inline void setup_rename_conflict_info(enum rename_type rename_type,  | 
290  |  |                 struct merge_options *opt,  | 
291  |  |                 struct rename *ren1,  | 
292  |  |                 struct rename *ren2)  | 
293  | 0  | { | 
294  | 0  |   struct rename_conflict_info *ci;  | 
295  |  |  | 
296  |  |   /*  | 
297  |  |    * When we have two renames involved, it's easiest to get the  | 
298  |  |    * correct things into stage 2 and 3, and to make sure that the  | 
299  |  |    * content merge puts HEAD before the other branch if we just  | 
300  |  |    * ensure that branch1 == opt->branch1.  So, simply flip arguments  | 
301  |  |    * around if we don't have that.  | 
302  |  |    */  | 
303  | 0  |   if (ren2 && ren1->branch != opt->branch1) { | 
304  | 0  |     setup_rename_conflict_info(rename_type, opt, ren2, ren1);  | 
305  | 0  |     return;  | 
306  | 0  |   }  | 
307  |  |  | 
308  | 0  |   CALLOC_ARRAY(ci, 1);  | 
309  | 0  |   ci->rename_type = rename_type;  | 
310  | 0  |   ci->ren1 = ren1;  | 
311  | 0  |   ci->ren2 = ren2;  | 
312  |  | 
  | 
313  | 0  |   ci->ren1->dst_entry->processed = 0;  | 
314  | 0  |   ci->ren1->dst_entry->rename_conflict_info = ci;  | 
315  | 0  |   ci->ren1->dst_entry->rename_conflict_info_owned = 1;  | 
316  | 0  |   if (ren2) { | 
317  | 0  |     ci->ren2->dst_entry->rename_conflict_info = ci;  | 
318  | 0  |   }  | 
319  | 0  | }  | 
320  |  |  | 
321  |  | static int show(struct merge_options *opt, int v)  | 
322  | 0  | { | 
323  | 0  |   return (!opt->priv->call_depth && opt->verbosity >= v) ||  | 
324  | 0  |     opt->verbosity >= 5;  | 
325  | 0  | }  | 
326  |  |  | 
327  |  | __attribute__((format (printf, 3, 4)))  | 
328  |  | static void output(struct merge_options *opt, int v, const char *fmt, ...)  | 
329  | 0  | { | 
330  | 0  |   va_list ap;  | 
331  |  | 
  | 
332  | 0  |   if (!show(opt, v))  | 
333  | 0  |     return;  | 
334  |  |  | 
335  | 0  |   strbuf_addchars(&opt->obuf, ' ', opt->priv->call_depth * 2);  | 
336  |  | 
  | 
337  | 0  |   va_start(ap, fmt);  | 
338  | 0  |   strbuf_vaddf(&opt->obuf, fmt, ap);  | 
339  | 0  |   va_end(ap);  | 
340  |  | 
  | 
341  | 0  |   strbuf_addch(&opt->obuf, '\n');  | 
342  | 0  |   if (!opt->buffer_output)  | 
343  | 0  |     flush_output(opt);  | 
344  | 0  | }  | 
345  |  |  | 
346  |  | static void repo_output_commit_title(struct merge_options *opt,  | 
347  |  |              struct repository *repo,  | 
348  |  |              struct commit *commit)  | 
349  | 0  | { | 
350  | 0  |   struct merge_remote_desc *desc;  | 
351  |  | 
  | 
352  | 0  |   strbuf_addchars(&opt->obuf, ' ', opt->priv->call_depth * 2);  | 
353  | 0  |   desc = merge_remote_util(commit);  | 
354  | 0  |   if (desc)  | 
355  | 0  |     strbuf_addf(&opt->obuf, "virtual %s\n", desc->name);  | 
356  | 0  |   else { | 
357  | 0  |     strbuf_repo_add_unique_abbrev(&opt->obuf, repo,  | 
358  | 0  |                 &commit->object.oid,  | 
359  | 0  |                 DEFAULT_ABBREV);  | 
360  | 0  |     strbuf_addch(&opt->obuf, ' ');  | 
361  | 0  |     if (repo_parse_commit(repo, commit) != 0)  | 
362  | 0  |       strbuf_addstr(&opt->obuf, _("(bad commit)\n")); | 
363  | 0  |     else { | 
364  | 0  |       const char *title;  | 
365  | 0  |       const char *msg = repo_get_commit_buffer(repo, commit, NULL);  | 
366  | 0  |       int len = find_commit_subject(msg, &title);  | 
367  | 0  |       if (len)  | 
368  | 0  |         strbuf_addf(&opt->obuf, "%.*s\n", len, title);  | 
369  | 0  |       repo_unuse_commit_buffer(repo, commit, msg);  | 
370  | 0  |     }  | 
371  | 0  |   }  | 
372  | 0  |   flush_output(opt);  | 
373  | 0  | }  | 
374  |  |  | 
375  |  | static void output_commit_title(struct merge_options *opt, struct commit *commit)  | 
376  | 0  | { | 
377  | 0  |   repo_output_commit_title(opt, the_repository, commit);  | 
378  | 0  | }  | 
379  |  |  | 
380  |  | static int add_cacheinfo(struct merge_options *opt,  | 
381  |  |        const struct diff_filespec *blob,  | 
382  |  |        const char *path, int stage, int refresh, int options)  | 
383  | 0  | { | 
384  | 0  |   struct index_state *istate = opt->repo->index;  | 
385  | 0  |   struct cache_entry *ce;  | 
386  | 0  |   int ret;  | 
387  |  | 
  | 
388  | 0  |   ce = make_cache_entry(istate, blob->mode, &blob->oid, path, stage, 0);  | 
389  | 0  |   if (!ce)  | 
390  | 0  |     return err(opt, _("add_cacheinfo failed for path '%s'; merge aborting."), path); | 
391  |  |  | 
392  | 0  |   ret = add_index_entry(istate, ce, options);  | 
393  | 0  |   if (refresh) { | 
394  | 0  |     struct cache_entry *nce;  | 
395  |  | 
  | 
396  | 0  |     nce = refresh_cache_entry(istate, ce,  | 
397  | 0  |             CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);  | 
398  | 0  |     if (!nce)  | 
399  | 0  |       return err(opt, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path); | 
400  | 0  |     if (nce != ce)  | 
401  | 0  |       ret = add_index_entry(istate, nce, options);  | 
402  | 0  |   }  | 
403  | 0  |   return ret;  | 
404  | 0  | }  | 
405  |  |  | 
406  |  | static inline int merge_detect_rename(struct merge_options *opt)  | 
407  | 0  | { | 
408  | 0  |   return (opt->detect_renames >= 0) ? opt->detect_renames : 1;  | 
409  | 0  | }  | 
410  |  |  | 
411  |  | static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)  | 
412  | 0  | { | 
413  | 0  |   if (parse_tree(tree) < 0)  | 
414  | 0  |     exit(128);  | 
415  | 0  |   init_tree_desc(desc, &tree->object.oid, tree->buffer, tree->size);  | 
416  | 0  | }  | 
417  |  |  | 
418  |  | static int unpack_trees_start(struct merge_options *opt,  | 
419  |  |             struct tree *common,  | 
420  |  |             struct tree *head,  | 
421  |  |             struct tree *merge)  | 
422  | 0  | { | 
423  | 0  |   int rc;  | 
424  | 0  |   struct tree_desc t[3];  | 
425  | 0  |   struct index_state tmp_index = INDEX_STATE_INIT(opt->repo);  | 
426  |  | 
  | 
427  | 0  |   memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));  | 
428  | 0  |   if (opt->priv->call_depth)  | 
429  | 0  |     opt->priv->unpack_opts.index_only = 1;  | 
430  | 0  |   else { | 
431  | 0  |     opt->priv->unpack_opts.update = 1;  | 
432  |  |     /* FIXME: should only do this if !overwrite_ignore */  | 
433  | 0  |     opt->priv->unpack_opts.preserve_ignored = 0;  | 
434  | 0  |   }  | 
435  | 0  |   opt->priv->unpack_opts.merge = 1;  | 
436  | 0  |   opt->priv->unpack_opts.head_idx = 2;  | 
437  | 0  |   opt->priv->unpack_opts.fn = threeway_merge;  | 
438  | 0  |   opt->priv->unpack_opts.src_index = opt->repo->index;  | 
439  | 0  |   opt->priv->unpack_opts.dst_index = &tmp_index;  | 
440  | 0  |   opt->priv->unpack_opts.aggressive = !merge_detect_rename(opt);  | 
441  | 0  |   setup_unpack_trees_porcelain(&opt->priv->unpack_opts, "merge");  | 
442  |  | 
  | 
443  | 0  |   init_tree_desc_from_tree(t+0, common);  | 
444  | 0  |   init_tree_desc_from_tree(t+1, head);  | 
445  | 0  |   init_tree_desc_from_tree(t+2, merge);  | 
446  |  | 
  | 
447  | 0  |   rc = unpack_trees(3, t, &opt->priv->unpack_opts);  | 
448  | 0  |   cache_tree_free(&opt->repo->index->cache_tree);  | 
449  |  |  | 
450  |  |   /*  | 
451  |  |    * Update opt->repo->index to match the new results, AFTER saving a  | 
452  |  |    * copy in opt->priv->orig_index.  Update src_index to point to the  | 
453  |  |    * saved copy.  (verify_uptodate() checks src_index, and the original  | 
454  |  |    * index is the one that had the necessary modification timestamps.)  | 
455  |  |    */  | 
456  | 0  |   opt->priv->orig_index = *opt->repo->index;  | 
457  | 0  |   *opt->repo->index = tmp_index;  | 
458  | 0  |   opt->priv->unpack_opts.src_index = &opt->priv->orig_index;  | 
459  |  | 
  | 
460  | 0  |   return rc;  | 
461  | 0  | }  | 
462  |  |  | 
463  |  | static void unpack_trees_finish(struct merge_options *opt)  | 
464  | 0  | { | 
465  | 0  |   discard_index(&opt->priv->orig_index);  | 
466  | 0  |   clear_unpack_trees_porcelain(&opt->priv->unpack_opts);  | 
467  | 0  | }  | 
468  |  |  | 
469  |  | static int save_files_dirs(const struct object_id *oid UNUSED,  | 
470  |  |          struct strbuf *base, const char *path,  | 
471  |  |          unsigned int mode, void *context)  | 
472  | 0  | { | 
473  | 0  |   struct path_hashmap_entry *entry;  | 
474  | 0  |   int baselen = base->len;  | 
475  | 0  |   struct merge_options *opt = context;  | 
476  |  | 
  | 
477  | 0  |   strbuf_addstr(base, path);  | 
478  |  | 
  | 
479  | 0  |   FLEX_ALLOC_MEM(entry, path, base->buf, base->len);  | 
480  | 0  |   hashmap_entry_init(&entry->e, fspathhash(entry->path));  | 
481  | 0  |   hashmap_add(&opt->priv->current_file_dir_set, &entry->e);  | 
482  |  | 
  | 
483  | 0  |   strbuf_setlen(base, baselen);  | 
484  | 0  |   return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);  | 
485  | 0  | }  | 
486  |  |  | 
487  |  | static void get_files_dirs(struct merge_options *opt, struct tree *tree)  | 
488  | 0  | { | 
489  | 0  |   struct pathspec match_all;  | 
490  | 0  |   memset(&match_all, 0, sizeof(match_all));  | 
491  | 0  |   read_tree(opt->repo, tree,  | 
492  | 0  |       &match_all, save_files_dirs, opt);  | 
493  | 0  | }  | 
494  |  |  | 
495  |  | static int get_tree_entry_if_blob(struct repository *r,  | 
496  |  |           const struct object_id *tree,  | 
497  |  |           const char *path,  | 
498  |  |           struct diff_filespec *dfs)  | 
499  | 0  | { | 
500  | 0  |   int ret;  | 
501  |  | 
  | 
502  | 0  |   ret = get_tree_entry(r, tree, path, &dfs->oid, &dfs->mode);  | 
503  | 0  |   if (S_ISDIR(dfs->mode)) { | 
504  | 0  |     oidcpy(&dfs->oid, null_oid());  | 
505  | 0  |     dfs->mode = 0;  | 
506  | 0  |   }  | 
507  | 0  |   return ret;  | 
508  | 0  | }  | 
509  |  |  | 
510  |  | /*  | 
511  |  |  * Returns an index_entry instance which doesn't have to correspond to  | 
512  |  |  * a real cache entry in Git's index.  | 
513  |  |  */  | 
514  |  | static struct stage_data *insert_stage_data(struct repository *r,  | 
515  |  |     const char *path,  | 
516  |  |     struct tree *o, struct tree *a, struct tree *b,  | 
517  |  |     struct string_list *entries)  | 
518  | 0  | { | 
519  | 0  |   struct string_list_item *item;  | 
520  | 0  |   struct stage_data *e = xcalloc(1, sizeof(struct stage_data));  | 
521  | 0  |   get_tree_entry_if_blob(r, &o->object.oid, path, &e->stages[1]);  | 
522  | 0  |   get_tree_entry_if_blob(r, &a->object.oid, path, &e->stages[2]);  | 
523  | 0  |   get_tree_entry_if_blob(r, &b->object.oid, path, &e->stages[3]);  | 
524  | 0  |   item = string_list_insert(entries, path);  | 
525  | 0  |   item->util = e;  | 
526  | 0  |   return e;  | 
527  | 0  | }  | 
528  |  |  | 
529  |  | /*  | 
530  |  |  * Create a dictionary mapping file names to stage_data objects. The  | 
531  |  |  * dictionary contains one entry for every path with a non-zero stage entry.  | 
532  |  |  */  | 
533  |  | static struct string_list *get_unmerged(struct index_state *istate)  | 
534  | 0  | { | 
535  | 0  |   struct string_list *unmerged = xmalloc(sizeof(struct string_list));  | 
536  | 0  |   int i;  | 
537  |  | 
  | 
538  | 0  |   string_list_init_dup(unmerged);  | 
539  |  |  | 
540  |  |   /* TODO: audit for interaction with sparse-index. */  | 
541  | 0  |   ensure_full_index(istate);  | 
542  | 0  |   for (i = 0; i < istate->cache_nr; i++) { | 
543  | 0  |     struct string_list_item *item;  | 
544  | 0  |     struct stage_data *e;  | 
545  | 0  |     const struct cache_entry *ce = istate->cache[i];  | 
546  | 0  |     if (!ce_stage(ce))  | 
547  | 0  |       continue;  | 
548  |  |  | 
549  | 0  |     item = string_list_lookup(unmerged, ce->name);  | 
550  | 0  |     if (!item) { | 
551  | 0  |       item = string_list_insert(unmerged, ce->name);  | 
552  | 0  |       item->util = xcalloc(1, sizeof(struct stage_data));  | 
553  | 0  |     }  | 
554  | 0  |     e = item->util;  | 
555  | 0  |     e->stages[ce_stage(ce)].mode = ce->ce_mode;  | 
556  | 0  |     oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);  | 
557  | 0  |   }  | 
558  |  | 
  | 
559  | 0  |   return unmerged;  | 
560  | 0  | }  | 
561  |  |  | 
562  |  | static int string_list_df_name_compare(const char *one, const char *two)  | 
563  | 0  | { | 
564  | 0  |   int onelen = strlen(one);  | 
565  | 0  |   int twolen = strlen(two);  | 
566  |  |   /*  | 
567  |  |    * Here we only care that entries for D/F conflicts are  | 
568  |  |    * adjacent, in particular with the file of the D/F conflict  | 
569  |  |    * appearing before files below the corresponding directory.  | 
570  |  |    * The order of the rest of the list is irrelevant for us.  | 
571  |  |    *  | 
572  |  |    * To achieve this, we sort with df_name_compare and provide  | 
573  |  |    * the mode S_IFDIR so that D/F conflicts will sort correctly.  | 
574  |  |    * We use the mode S_IFDIR for everything else for simplicity,  | 
575  |  |    * since in other cases any changes in their order due to  | 
576  |  |    * sorting cause no problems for us.  | 
577  |  |    */  | 
578  | 0  |   int cmp = df_name_compare(one, onelen, S_IFDIR,  | 
579  | 0  |           two, twolen, S_IFDIR);  | 
580  |  |   /*  | 
581  |  |    * Now that 'foo' and 'foo/bar' compare equal, we have to make sure  | 
582  |  |    * that 'foo' comes before 'foo/bar'.  | 
583  |  |    */  | 
584  | 0  |   if (cmp)  | 
585  | 0  |     return cmp;  | 
586  | 0  |   return onelen - twolen;  | 
587  | 0  | }  | 
588  |  |  | 
589  |  | static void record_df_conflict_files(struct merge_options *opt,  | 
590  |  |              struct string_list *entries)  | 
591  | 0  | { | 
592  |  |   /* If there is a D/F conflict and the file for such a conflict  | 
593  |  |    * currently exists in the working tree, we want to allow it to be  | 
594  |  |    * removed to make room for the corresponding directory if needed.  | 
595  |  |    * The files underneath the directories of such D/F conflicts will  | 
596  |  |    * be processed before the corresponding file involved in the D/F  | 
597  |  |    * conflict.  If the D/F directory ends up being removed by the  | 
598  |  |    * merge, then we won't have to touch the D/F file.  If the D/F  | 
599  |  |    * directory needs to be written to the working copy, then the D/F  | 
600  |  |    * file will simply be removed (in make_room_for_path()) to make  | 
601  |  |    * room for the necessary paths.  Note that if both the directory  | 
602  |  |    * and the file need to be present, then the D/F file will be  | 
603  |  |    * reinstated with a new unique name at the time it is processed.  | 
604  |  |    */  | 
605  | 0  |   struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;  | 
606  | 0  |   const char *last_file = NULL;  | 
607  | 0  |   int last_len = 0;  | 
608  | 0  |   int i;  | 
609  |  |  | 
610  |  |   /*  | 
611  |  |    * If we're merging merge-bases, we don't want to bother with  | 
612  |  |    * any working directory changes.  | 
613  |  |    */  | 
614  | 0  |   if (opt->priv->call_depth)  | 
615  | 0  |     return;  | 
616  |  |  | 
617  |  |   /* Ensure D/F conflicts are adjacent in the entries list. */  | 
618  | 0  |   for (i = 0; i < entries->nr; i++) { | 
619  | 0  |     struct string_list_item *next = &entries->items[i];  | 
620  | 0  |     string_list_append(&df_sorted_entries, next->string)->util =  | 
621  | 0  |            next->util;  | 
622  | 0  |   }  | 
623  | 0  |   df_sorted_entries.cmp = string_list_df_name_compare;  | 
624  | 0  |   string_list_sort(&df_sorted_entries);  | 
625  |  | 
  | 
626  | 0  |   string_list_clear(&opt->priv->df_conflict_file_set, 1);  | 
627  | 0  |   for (i = 0; i < df_sorted_entries.nr; i++) { | 
628  | 0  |     const char *path = df_sorted_entries.items[i].string;  | 
629  | 0  |     int len = strlen(path);  | 
630  | 0  |     struct stage_data *e = df_sorted_entries.items[i].util;  | 
631  |  |  | 
632  |  |     /*  | 
633  |  |      * Check if last_file & path correspond to a D/F conflict;  | 
634  |  |      * i.e. whether path is last_file+'/'+<something>.  | 
635  |  |      * If so, record that it's okay to remove last_file to make  | 
636  |  |      * room for path and friends if needed.  | 
637  |  |      */  | 
638  | 0  |     if (last_file &&  | 
639  | 0  |         len > last_len &&  | 
640  | 0  |         memcmp(path, last_file, last_len) == 0 &&  | 
641  | 0  |         path[last_len] == '/') { | 
642  | 0  |       string_list_insert(&opt->priv->df_conflict_file_set, last_file);  | 
643  | 0  |     }  | 
644  |  |  | 
645  |  |     /*  | 
646  |  |      * Determine whether path could exist as a file in the  | 
647  |  |      * working directory as a possible D/F conflict.  This  | 
648  |  |      * will only occur when it exists in stage 2 as a  | 
649  |  |      * file.  | 
650  |  |      */  | 
651  | 0  |     if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) { | 
652  | 0  |       last_file = path;  | 
653  | 0  |       last_len = len;  | 
654  | 0  |     } else { | 
655  | 0  |       last_file = NULL;  | 
656  | 0  |     }  | 
657  | 0  |   }  | 
658  | 0  |   string_list_clear(&df_sorted_entries, 0);  | 
659  | 0  | }  | 
660  |  |  | 
661  |  | static int update_stages(struct merge_options *opt, const char *path,  | 
662  |  |        const struct diff_filespec *o,  | 
663  |  |        const struct diff_filespec *a,  | 
664  |  |        const struct diff_filespec *b)  | 
665  | 0  | { | 
666  |  |  | 
667  |  |   /*  | 
668  |  |    * NOTE: It is usually a bad idea to call update_stages on a path  | 
669  |  |    * before calling update_file on that same path, since it can  | 
670  |  |    * sometimes lead to spurious "refusing to lose untracked file..."  | 
671  |  |    * messages from update_file (via make_room_for path via  | 
672  |  |    * would_lose_untracked).  Instead, reverse the order of the calls  | 
673  |  |    * (executing update_file first and then update_stages).  | 
674  |  |    */  | 
675  | 0  |   int clear = 1;  | 
676  | 0  |   int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;  | 
677  | 0  |   if (clear)  | 
678  | 0  |     if (remove_file_from_index(opt->repo->index, path))  | 
679  | 0  |       return -1;  | 
680  | 0  |   if (o)  | 
681  | 0  |     if (add_cacheinfo(opt, o, path, 1, 0, options))  | 
682  | 0  |       return -1;  | 
683  | 0  |   if (a)  | 
684  | 0  |     if (add_cacheinfo(opt, a, path, 2, 0, options))  | 
685  | 0  |       return -1;  | 
686  | 0  |   if (b)  | 
687  | 0  |     if (add_cacheinfo(opt, b, path, 3, 0, options))  | 
688  | 0  |       return -1;  | 
689  | 0  |   return 0;  | 
690  | 0  | }  | 
691  |  |  | 
692  |  | static void update_entry(struct stage_data *entry,  | 
693  |  |        struct diff_filespec *o,  | 
694  |  |        struct diff_filespec *a,  | 
695  |  |        struct diff_filespec *b)  | 
696  | 0  | { | 
697  | 0  |   entry->processed = 0;  | 
698  | 0  |   entry->stages[1].mode = o->mode;  | 
699  | 0  |   entry->stages[2].mode = a->mode;  | 
700  | 0  |   entry->stages[3].mode = b->mode;  | 
701  | 0  |   oidcpy(&entry->stages[1].oid, &o->oid);  | 
702  | 0  |   oidcpy(&entry->stages[2].oid, &a->oid);  | 
703  | 0  |   oidcpy(&entry->stages[3].oid, &b->oid);  | 
704  | 0  | }  | 
705  |  |  | 
706  |  | static int remove_file(struct merge_options *opt, int clean,  | 
707  |  |            const char *path, int no_wd)  | 
708  | 0  | { | 
709  | 0  |   int update_cache = opt->priv->call_depth || clean;  | 
710  | 0  |   int update_working_directory = !opt->priv->call_depth && !no_wd;  | 
711  |  | 
  | 
712  | 0  |   if (update_cache) { | 
713  | 0  |     if (remove_file_from_index(opt->repo->index, path))  | 
714  | 0  |       return -1;  | 
715  | 0  |   }  | 
716  | 0  |   if (update_working_directory) { | 
717  | 0  |     if (ignore_case) { | 
718  | 0  |       struct cache_entry *ce;  | 
719  | 0  |       ce = index_file_exists(opt->repo->index, path, strlen(path),  | 
720  | 0  |                  ignore_case);  | 
721  | 0  |       if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name))  | 
722  | 0  |         return 0;  | 
723  | 0  |     }  | 
724  | 0  |     if (remove_path(path))  | 
725  | 0  |       return -1;  | 
726  | 0  |   }  | 
727  | 0  |   return 0;  | 
728  | 0  | }  | 
729  |  |  | 
730  |  | /* add a string to a strbuf, but converting "/" to "_" */  | 
731  |  | static void add_flattened_path(struct strbuf *out, const char *s)  | 
732  | 0  | { | 
733  | 0  |   size_t i = out->len;  | 
734  | 0  |   strbuf_addstr(out, s);  | 
735  | 0  |   for (; i < out->len; i++)  | 
736  | 0  |     if (out->buf[i] == '/')  | 
737  | 0  |       out->buf[i] = '_';  | 
738  | 0  | }  | 
739  |  |  | 
740  |  | static char *unique_path(struct merge_options *opt,  | 
741  |  |        const char *path,  | 
742  |  |        const char *branch)  | 
743  | 0  | { | 
744  | 0  |   struct path_hashmap_entry *entry;  | 
745  | 0  |   struct strbuf newpath = STRBUF_INIT;  | 
746  | 0  |   int suffix = 0;  | 
747  | 0  |   size_t base_len;  | 
748  |  | 
  | 
749  | 0  |   strbuf_addf(&newpath, "%s~", path);  | 
750  | 0  |   add_flattened_path(&newpath, branch);  | 
751  |  | 
  | 
752  | 0  |   base_len = newpath.len;  | 
753  | 0  |   while (hashmap_get_from_hash(&opt->priv->current_file_dir_set,  | 
754  | 0  |              fspathhash(newpath.buf), newpath.buf) ||  | 
755  | 0  |          (!opt->priv->call_depth && file_exists(newpath.buf))) { | 
756  | 0  |     strbuf_setlen(&newpath, base_len);  | 
757  | 0  |     strbuf_addf(&newpath, "_%d", suffix++);  | 
758  | 0  |   }  | 
759  |  | 
  | 
760  | 0  |   FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);  | 
761  | 0  |   hashmap_entry_init(&entry->e, fspathhash(entry->path));  | 
762  | 0  |   hashmap_add(&opt->priv->current_file_dir_set, &entry->e);  | 
763  | 0  |   return strbuf_detach(&newpath, NULL);  | 
764  | 0  | }  | 
765  |  |  | 
766  |  | /**  | 
767  |  |  * Check whether a directory in the index is in the way of an incoming  | 
768  |  |  * file.  Return 1 if so.  If check_working_copy is non-zero, also  | 
769  |  |  * check the working directory.  If empty_ok is non-zero, also return  | 
770  |  |  * 0 in the case where the working-tree dir exists but is empty.  | 
771  |  |  */  | 
772  |  | static int dir_in_way(struct index_state *istate, const char *path,  | 
773  |  |           int check_working_copy, int empty_ok)  | 
774  | 0  | { | 
775  | 0  |   int pos;  | 
776  | 0  |   struct strbuf dirpath = STRBUF_INIT;  | 
777  | 0  |   struct stat st;  | 
778  |  | 
  | 
779  | 0  |   strbuf_addstr(&dirpath, path);  | 
780  | 0  |   strbuf_addch(&dirpath, '/');  | 
781  |  | 
  | 
782  | 0  |   pos = index_name_pos(istate, dirpath.buf, dirpath.len);  | 
783  |  | 
  | 
784  | 0  |   if (pos < 0)  | 
785  | 0  |     pos = -1 - pos;  | 
786  | 0  |   if (pos < istate->cache_nr &&  | 
787  | 0  |       !strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) { | 
788  | 0  |     strbuf_release(&dirpath);  | 
789  | 0  |     return 1;  | 
790  | 0  |   }  | 
791  |  |  | 
792  | 0  |   strbuf_release(&dirpath);  | 
793  | 0  |   return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&  | 
794  | 0  |     !(empty_ok && is_empty_dir(path)) &&  | 
795  | 0  |     !has_symlink_leading_path(path, strlen(path));  | 
796  | 0  | }  | 
797  |  |  | 
798  |  | /*  | 
799  |  |  * Returns whether path was tracked in the index before the merge started,  | 
800  |  |  * and its oid and mode match the specified values  | 
801  |  |  */  | 
802  |  | static int was_tracked_and_matches(struct merge_options *opt, const char *path,  | 
803  |  |            const struct diff_filespec *blob)  | 
804  | 0  | { | 
805  | 0  |   int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));  | 
806  | 0  |   struct cache_entry *ce;  | 
807  |  | 
  | 
808  | 0  |   if (0 > pos)  | 
809  |  |     /* we were not tracking this path before the merge */  | 
810  | 0  |     return 0;  | 
811  |  |  | 
812  |  |   /* See if the file we were tracking before matches */  | 
813  | 0  |   ce = opt->priv->orig_index.cache[pos];  | 
814  | 0  |   return (oideq(&ce->oid, &blob->oid) && ce->ce_mode == blob->mode);  | 
815  | 0  | }  | 
816  |  |  | 
817  |  | /*  | 
818  |  |  * Returns whether path was tracked in the index before the merge started  | 
819  |  |  */  | 
820  |  | static int was_tracked(struct merge_options *opt, const char *path)  | 
821  | 0  | { | 
822  | 0  |   int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));  | 
823  |  | 
  | 
824  | 0  |   if (0 <= pos)  | 
825  |  |     /* we were tracking this path before the merge */  | 
826  | 0  |     return 1;  | 
827  |  |  | 
828  | 0  |   return 0;  | 
829  | 0  | }  | 
830  |  |  | 
831  |  | static int would_lose_untracked(struct merge_options *opt, const char *path)  | 
832  | 0  | { | 
833  | 0  |   struct index_state *istate = opt->repo->index;  | 
834  |  |  | 
835  |  |   /*  | 
836  |  |    * This may look like it can be simplified to:  | 
837  |  |    *   return !was_tracked(opt, path) && file_exists(path)  | 
838  |  |    * but it can't.  This function needs to know whether path was in  | 
839  |  |    * the working tree due to EITHER having been tracked in the index  | 
840  |  |    * before the merge OR having been put into the working copy and  | 
841  |  |    * index by unpack_trees().  Due to that either-or requirement, we  | 
842  |  |    * check the current index instead of the original one.  | 
843  |  |    *  | 
844  |  |    * Note that we do not need to worry about merge-recursive itself  | 
845  |  |    * updating the index after unpack_trees() and before calling this  | 
846  |  |    * function, because we strictly require all code paths in  | 
847  |  |    * merge-recursive to update the working tree first and the index  | 
848  |  |    * second.  Doing otherwise would break  | 
849  |  |    * update_file()/would_lose_untracked(); see every comment in this  | 
850  |  |    * file which mentions "update_stages".  | 
851  |  |    */  | 
852  | 0  |   int pos = index_name_pos(istate, path, strlen(path));  | 
853  |  | 
  | 
854  | 0  |   if (pos < 0)  | 
855  | 0  |     pos = -1 - pos;  | 
856  | 0  |   while (pos < istate->cache_nr &&  | 
857  | 0  |          !strcmp(path, istate->cache[pos]->name)) { | 
858  |  |     /*  | 
859  |  |      * If stage #0, it is definitely tracked.  | 
860  |  |      * If it has stage #2 then it was tracked  | 
861  |  |      * before this merge started.  All other  | 
862  |  |      * cases the path was not tracked.  | 
863  |  |      */  | 
864  | 0  |     switch (ce_stage(istate->cache[pos])) { | 
865  | 0  |     case 0:  | 
866  | 0  |     case 2:  | 
867  | 0  |       return 0;  | 
868  | 0  |     }  | 
869  | 0  |     pos++;  | 
870  | 0  |   }  | 
871  | 0  |   return file_exists(path);  | 
872  | 0  | }  | 
873  |  |  | 
874  |  | static int was_dirty(struct merge_options *opt, const char *path)  | 
875  | 0  | { | 
876  | 0  |   struct cache_entry *ce;  | 
877  | 0  |   int dirty = 1;  | 
878  |  | 
  | 
879  | 0  |   if (opt->priv->call_depth || !was_tracked(opt, path))  | 
880  | 0  |     return !dirty;  | 
881  |  |  | 
882  | 0  |   ce = index_file_exists(opt->priv->unpack_opts.src_index,  | 
883  | 0  |              path, strlen(path), ignore_case);  | 
884  | 0  |   dirty = verify_uptodate(ce, &opt->priv->unpack_opts) != 0;  | 
885  | 0  |   return dirty;  | 
886  | 0  | }  | 
887  |  |  | 
888  |  | static int make_room_for_path(struct merge_options *opt, const char *path)  | 
889  | 0  | { | 
890  | 0  |   int status, i;  | 
891  | 0  |   const char *msg = _("failed to create path '%s'%s"); | 
892  |  |  | 
893  |  |   /* Unlink any D/F conflict files that are in the way */  | 
894  | 0  |   for (i = 0; i < opt->priv->df_conflict_file_set.nr; i++) { | 
895  | 0  |     const char *df_path = opt->priv->df_conflict_file_set.items[i].string;  | 
896  | 0  |     size_t pathlen = strlen(path);  | 
897  | 0  |     size_t df_pathlen = strlen(df_path);  | 
898  | 0  |     if (df_pathlen < pathlen &&  | 
899  | 0  |         path[df_pathlen] == '/' &&  | 
900  | 0  |         strncmp(path, df_path, df_pathlen) == 0) { | 
901  | 0  |       output(opt, 3,  | 
902  | 0  |              _("Removing %s to make room for subdirectory\n"), | 
903  | 0  |              df_path);  | 
904  | 0  |       unlink(df_path);  | 
905  | 0  |       unsorted_string_list_delete_item(&opt->priv->df_conflict_file_set,  | 
906  | 0  |                i, 0);  | 
907  | 0  |       break;  | 
908  | 0  |     }  | 
909  | 0  |   }  | 
910  |  |  | 
911  |  |   /* Make sure leading directories are created */  | 
912  | 0  |   status = safe_create_leading_directories_const(path);  | 
913  | 0  |   if (status) { | 
914  | 0  |     if (status == SCLD_EXISTS)  | 
915  |  |       /* something else exists */  | 
916  | 0  |       return err(opt, msg, path, _(": perhaps a D/F conflict?")); | 
917  | 0  |     return err(opt, msg, path, "");  | 
918  | 0  |   }  | 
919  |  |  | 
920  |  |   /*  | 
921  |  |    * Do not unlink a file in the work tree if we are not  | 
922  |  |    * tracking it.  | 
923  |  |    */  | 
924  | 0  |   if (would_lose_untracked(opt, path))  | 
925  | 0  |     return err(opt, _("refusing to lose untracked file at '%s'"), | 
926  | 0  |          path);  | 
927  |  |  | 
928  |  |   /* Successful unlink is good.. */  | 
929  | 0  |   if (!unlink(path))  | 
930  | 0  |     return 0;  | 
931  |  |   /* .. and so is no existing file */  | 
932  | 0  |   if (errno == ENOENT)  | 
933  | 0  |     return 0;  | 
934  |  |   /* .. but not some other error (who really cares what?) */  | 
935  | 0  |   return err(opt, msg, path, _(": perhaps a D/F conflict?")); | 
936  | 0  | }  | 
937  |  |  | 
938  |  | static int update_file_flags(struct merge_options *opt,  | 
939  |  |            const struct diff_filespec *contents,  | 
940  |  |            const char *path,  | 
941  |  |            int update_cache,  | 
942  |  |            int update_wd)  | 
943  | 0  | { | 
944  | 0  |   int ret = 0;  | 
945  |  | 
  | 
946  | 0  |   if (opt->priv->call_depth)  | 
947  | 0  |     update_wd = 0;  | 
948  |  | 
  | 
949  | 0  |   if (update_wd) { | 
950  | 0  |     enum object_type type;  | 
951  | 0  |     void *buf;  | 
952  | 0  |     unsigned long size;  | 
953  |  | 
  | 
954  | 0  |     if (S_ISGITLINK(contents->mode)) { | 
955  |  |       /*  | 
956  |  |        * We may later decide to recursively descend into  | 
957  |  |        * the submodule directory and update its index  | 
958  |  |        * and/or work tree, but we do not do that now.  | 
959  |  |        */  | 
960  | 0  |       update_wd = 0;  | 
961  | 0  |       goto update_index;  | 
962  | 0  |     }  | 
963  |  |  | 
964  | 0  |     buf = repo_read_object_file(the_repository, &contents->oid,  | 
965  | 0  |               &type, &size);  | 
966  | 0  |     if (!buf) { | 
967  | 0  |       ret = err(opt, _("cannot read object %s '%s'"), | 
968  | 0  |           oid_to_hex(&contents->oid), path);  | 
969  | 0  |       goto free_buf;  | 
970  | 0  |     }  | 
971  | 0  |     if (type != OBJ_BLOB) { | 
972  | 0  |       ret = err(opt, _("blob expected for %s '%s'"), | 
973  | 0  |           oid_to_hex(&contents->oid), path);  | 
974  | 0  |       goto free_buf;  | 
975  | 0  |     }  | 
976  | 0  |     if (S_ISREG(contents->mode)) { | 
977  | 0  |       struct strbuf strbuf = STRBUF_INIT;  | 
978  | 0  |       if (convert_to_working_tree(opt->repo->index,  | 
979  | 0  |                 path, buf, size, &strbuf, NULL)) { | 
980  | 0  |         free(buf);  | 
981  | 0  |         size = strbuf.len;  | 
982  | 0  |         buf = strbuf_detach(&strbuf, NULL);  | 
983  | 0  |       }  | 
984  | 0  |     }  | 
985  |  | 
  | 
986  | 0  |     if (make_room_for_path(opt, path) < 0) { | 
987  | 0  |       update_wd = 0;  | 
988  | 0  |       goto free_buf;  | 
989  | 0  |     }  | 
990  | 0  |     if (S_ISREG(contents->mode) ||  | 
991  | 0  |         (!has_symlinks && S_ISLNK(contents->mode))) { | 
992  | 0  |       int fd;  | 
993  | 0  |       int mode = (contents->mode & 0100 ? 0777 : 0666);  | 
994  |  | 
  | 
995  | 0  |       fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);  | 
996  | 0  |       if (fd < 0) { | 
997  | 0  |         ret = err(opt, _("failed to open '%s': %s"), | 
998  | 0  |             path, strerror(errno));  | 
999  | 0  |         goto free_buf;  | 
1000  | 0  |       }  | 
1001  | 0  |       write_in_full(fd, buf, size);  | 
1002  | 0  |       close(fd);  | 
1003  | 0  |     } else if (S_ISLNK(contents->mode)) { | 
1004  | 0  |       char *lnk = xmemdupz(buf, size);  | 
1005  | 0  |       safe_create_leading_directories_const(path);  | 
1006  | 0  |       unlink(path);  | 
1007  | 0  |       if (symlink(lnk, path))  | 
1008  | 0  |         ret = err(opt, _("failed to symlink '%s': %s"), | 
1009  | 0  |             path, strerror(errno));  | 
1010  | 0  |       free(lnk);  | 
1011  | 0  |     } else  | 
1012  | 0  |       ret = err(opt,  | 
1013  | 0  |           _("do not know what to do with %06o %s '%s'"), | 
1014  | 0  |           contents->mode, oid_to_hex(&contents->oid), path);  | 
1015  | 0  |   free_buf:  | 
1016  | 0  |     free(buf);  | 
1017  | 0  |   }  | 
1018  | 0  | update_index:  | 
1019  | 0  |   if (!ret && update_cache) { | 
1020  | 0  |     int refresh = (!opt->priv->call_depth &&  | 
1021  | 0  |              contents->mode != S_IFGITLINK);  | 
1022  | 0  |     if (add_cacheinfo(opt, contents, path, 0, refresh,  | 
1023  | 0  |           ADD_CACHE_OK_TO_ADD))  | 
1024  | 0  |       return -1;  | 
1025  | 0  |   }  | 
1026  | 0  |   return ret;  | 
1027  | 0  | }  | 
1028  |  |  | 
1029  |  | static int update_file(struct merge_options *opt,  | 
1030  |  |            int clean,  | 
1031  |  |            const struct diff_filespec *contents,  | 
1032  |  |            const char *path)  | 
1033  | 0  | { | 
1034  | 0  |   return update_file_flags(opt, contents, path,  | 
1035  | 0  |          opt->priv->call_depth || clean, !opt->priv->call_depth);  | 
1036  | 0  | }  | 
1037  |  |  | 
1038  |  | /* Low level file merging, update and removal */  | 
1039  |  |  | 
1040  |  | struct merge_file_info { | 
1041  |  |   struct diff_filespec blob; /* mostly use oid & mode; sometimes path */  | 
1042  |  |   unsigned clean:1,  | 
1043  |  |      merge:1;  | 
1044  |  | };  | 
1045  |  |  | 
1046  |  | static int merge_3way(struct merge_options *opt,  | 
1047  |  |           mmbuffer_t *result_buf,  | 
1048  |  |           const struct diff_filespec *o,  | 
1049  |  |           const struct diff_filespec *a,  | 
1050  |  |           const struct diff_filespec *b,  | 
1051  |  |           const char *branch1,  | 
1052  |  |           const char *branch2,  | 
1053  |  |           const int extra_marker_size)  | 
1054  | 0  | { | 
1055  | 0  |   mmfile_t orig, src1, src2;  | 
1056  | 0  |   struct ll_merge_options ll_opts = LL_MERGE_OPTIONS_INIT;  | 
1057  | 0  |   char *base, *name1, *name2;  | 
1058  | 0  |   enum ll_merge_result merge_status;  | 
1059  |  | 
  | 
1060  | 0  |   ll_opts.renormalize = opt->renormalize;  | 
1061  | 0  |   ll_opts.extra_marker_size = extra_marker_size;  | 
1062  | 0  |   ll_opts.xdl_opts = opt->xdl_opts;  | 
1063  | 0  |   ll_opts.conflict_style = opt->conflict_style;  | 
1064  |  | 
  | 
1065  | 0  |   if (opt->priv->call_depth) { | 
1066  | 0  |     ll_opts.virtual_ancestor = 1;  | 
1067  | 0  |     ll_opts.variant = 0;  | 
1068  | 0  |   } else { | 
1069  | 0  |     switch (opt->recursive_variant) { | 
1070  | 0  |     case MERGE_VARIANT_OURS:  | 
1071  | 0  |       ll_opts.variant = XDL_MERGE_FAVOR_OURS;  | 
1072  | 0  |       break;  | 
1073  | 0  |     case MERGE_VARIANT_THEIRS:  | 
1074  | 0  |       ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;  | 
1075  | 0  |       break;  | 
1076  | 0  |     default:  | 
1077  | 0  |       ll_opts.variant = 0;  | 
1078  | 0  |       break;  | 
1079  | 0  |     }  | 
1080  | 0  |   }  | 
1081  |  |  | 
1082  | 0  |   assert(a->path && b->path && o->path && opt->ancestor);  | 
1083  | 0  |   if (strcmp(a->path, b->path) || strcmp(a->path, o->path) != 0) { | 
1084  | 0  |     base  = mkpathdup("%s:%s", opt->ancestor, o->path); | 
1085  | 0  |     name1 = mkpathdup("%s:%s", branch1, a->path); | 
1086  | 0  |     name2 = mkpathdup("%s:%s", branch2, b->path); | 
1087  | 0  |   } else { | 
1088  | 0  |     base  = mkpathdup("%s", opt->ancestor); | 
1089  | 0  |     name1 = mkpathdup("%s", branch1); | 
1090  | 0  |     name2 = mkpathdup("%s", branch2); | 
1091  | 0  |   }  | 
1092  |  | 
  | 
1093  | 0  |   read_mmblob(&orig, &o->oid);  | 
1094  | 0  |   read_mmblob(&src1, &a->oid);  | 
1095  | 0  |   read_mmblob(&src2, &b->oid);  | 
1096  |  |  | 
1097  |  |   /*  | 
1098  |  |    * FIXME: Using a->path for normalization rules in ll_merge could be  | 
1099  |  |    * wrong if we renamed from a->path to b->path.  We should use the  | 
1100  |  |    * target path for where the file will be written.  | 
1101  |  |    */  | 
1102  | 0  |   merge_status = ll_merge(result_buf, a->path, &orig, base,  | 
1103  | 0  |         &src1, name1, &src2, name2,  | 
1104  | 0  |         opt->repo->index, &ll_opts);  | 
1105  | 0  |   if (merge_status == LL_MERGE_BINARY_CONFLICT)  | 
1106  | 0  |     warning("Cannot merge binary files: %s (%s vs. %s)", | 
1107  | 0  |       a->path, name1, name2);  | 
1108  |  | 
  | 
1109  | 0  |   free(base);  | 
1110  | 0  |   free(name1);  | 
1111  | 0  |   free(name2);  | 
1112  | 0  |   free(orig.ptr);  | 
1113  | 0  |   free(src1.ptr);  | 
1114  | 0  |   free(src2.ptr);  | 
1115  | 0  |   return merge_status;  | 
1116  | 0  | }  | 
1117  |  |  | 
1118  |  | static int find_first_merges(struct repository *repo,  | 
1119  |  |            struct object_array *result, const char *path,  | 
1120  |  |            struct commit *a, struct commit *b)  | 
1121  | 0  | { | 
1122  | 0  |   int i, j;  | 
1123  | 0  |   struct object_array merges = OBJECT_ARRAY_INIT;  | 
1124  | 0  |   struct commit *commit;  | 
1125  | 0  |   int contains_another;  | 
1126  |  | 
  | 
1127  | 0  |   char merged_revision[GIT_MAX_HEXSZ + 2];  | 
1128  | 0  |   const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path", | 
1129  | 0  |            "--all", merged_revision, NULL };  | 
1130  | 0  |   struct rev_info revs;  | 
1131  | 0  |   struct setup_revision_opt rev_opts;  | 
1132  |  | 
  | 
1133  | 0  |   memset(result, 0, sizeof(struct object_array));  | 
1134  | 0  |   memset(&rev_opts, 0, sizeof(rev_opts));  | 
1135  |  |  | 
1136  |  |   /* get all revisions that merge commit a */  | 
1137  | 0  |   xsnprintf(merged_revision, sizeof(merged_revision), "^%s",  | 
1138  | 0  |       oid_to_hex(&a->object.oid));  | 
1139  | 0  |   repo_init_revisions(repo, &revs, NULL);  | 
1140  |  |   /* FIXME: can't handle linked worktrees in submodules yet */  | 
1141  | 0  |   revs.single_worktree = path != NULL;  | 
1142  | 0  |   setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);  | 
1143  |  |  | 
1144  |  |   /* save all revisions from the above list that contain b */  | 
1145  | 0  |   if (prepare_revision_walk(&revs))  | 
1146  | 0  |     die("revision walk setup failed"); | 
1147  | 0  |   while ((commit = get_revision(&revs)) != NULL) { | 
1148  | 0  |     struct object *o = &(commit->object);  | 
1149  | 0  |     int ret = repo_in_merge_bases(repo, b, commit);  | 
1150  | 0  |     if (ret < 0) { | 
1151  | 0  |       object_array_clear(&merges);  | 
1152  | 0  |       release_revisions(&revs);  | 
1153  | 0  |       return ret;  | 
1154  | 0  |     }  | 
1155  | 0  |     if (ret)  | 
1156  | 0  |       add_object_array(o, NULL, &merges);  | 
1157  | 0  |   }  | 
1158  | 0  |   reset_revision_walk();  | 
1159  |  |  | 
1160  |  |   /* Now we've got all merges that contain a and b. Prune all  | 
1161  |  |    * merges that contain another found merge and save them in  | 
1162  |  |    * result.  | 
1163  |  |    */  | 
1164  | 0  |   for (i = 0; i < merges.nr; i++) { | 
1165  | 0  |     struct commit *m1 = (struct commit *) merges.objects[i].item;  | 
1166  |  | 
  | 
1167  | 0  |     contains_another = 0;  | 
1168  | 0  |     for (j = 0; j < merges.nr; j++) { | 
1169  | 0  |       struct commit *m2 = (struct commit *) merges.objects[j].item;  | 
1170  | 0  |       if (i != j) { | 
1171  | 0  |         int ret = repo_in_merge_bases(repo, m2, m1);  | 
1172  | 0  |         if (ret < 0) { | 
1173  | 0  |           object_array_clear(&merges);  | 
1174  | 0  |           release_revisions(&revs);  | 
1175  | 0  |           return ret;  | 
1176  | 0  |         }  | 
1177  | 0  |         if (ret > 0) { | 
1178  | 0  |           contains_another = 1;  | 
1179  | 0  |           break;  | 
1180  | 0  |         }  | 
1181  | 0  |       }  | 
1182  | 0  |     }  | 
1183  |  |  | 
1184  | 0  |     if (!contains_another)  | 
1185  | 0  |       add_object_array(merges.objects[i].item, NULL, result);  | 
1186  | 0  |   }  | 
1187  |  |  | 
1188  | 0  |   object_array_clear(&merges);  | 
1189  | 0  |   release_revisions(&revs);  | 
1190  | 0  |   return result->nr;  | 
1191  | 0  | }  | 
1192  |  |  | 
1193  |  | static void print_commit(struct repository *repo, struct commit *commit)  | 
1194  | 0  | { | 
1195  | 0  |   struct strbuf sb = STRBUF_INIT;  | 
1196  | 0  |   struct pretty_print_context ctx = {0}; | 
1197  | 0  |   ctx.date_mode.type = DATE_NORMAL;  | 
1198  |  |   /* FIXME: Merge this with output_commit_title() */  | 
1199  | 0  |   assert(!merge_remote_util(commit));  | 
1200  | 0  |   repo_format_commit_message(repo, commit, " %h: %m %s", &sb, &ctx);  | 
1201  | 0  |   fprintf(stderr, "%s\n", sb.buf);  | 
1202  | 0  |   strbuf_release(&sb);  | 
1203  | 0  | }  | 
1204  |  |  | 
1205  |  | static int is_valid(const struct diff_filespec *dfs)  | 
1206  | 0  | { | 
1207  | 0  |   return dfs->mode != 0 && !is_null_oid(&dfs->oid);  | 
1208  | 0  | }  | 
1209  |  |  | 
1210  |  | static int merge_submodule(struct merge_options *opt,  | 
1211  |  |          struct object_id *result, const char *path,  | 
1212  |  |          const struct object_id *base, const struct object_id *a,  | 
1213  |  |          const struct object_id *b)  | 
1214  | 0  | { | 
1215  | 0  |   struct repository subrepo;  | 
1216  | 0  |   int ret = 0, ret2;  | 
1217  | 0  |   struct commit *commit_base, *commit_a, *commit_b;  | 
1218  | 0  |   int parent_count;  | 
1219  | 0  |   struct object_array merges;  | 
1220  |  | 
  | 
1221  | 0  |   int i;  | 
1222  | 0  |   int search = !opt->priv->call_depth;  | 
1223  |  |  | 
1224  |  |   /* store a in result in case we fail */  | 
1225  |  |   /* FIXME: This is the WRONG resolution for the recursive case when  | 
1226  |  |    * we need to be careful to avoid accidentally matching either side.  | 
1227  |  |    * Should probably use o instead there, much like we do for merging  | 
1228  |  |    * binaries.  | 
1229  |  |    */  | 
1230  | 0  |   oidcpy(result, a);  | 
1231  |  |  | 
1232  |  |   /* we can not handle deletion conflicts */  | 
1233  | 0  |   if (is_null_oid(base))  | 
1234  | 0  |     return 0;  | 
1235  | 0  |   if (is_null_oid(a))  | 
1236  | 0  |     return 0;  | 
1237  | 0  |   if (is_null_oid(b))  | 
1238  | 0  |     return 0;  | 
1239  |  |  | 
1240  | 0  |   if (repo_submodule_init(&subrepo, opt->repo, path, null_oid())) { | 
1241  | 0  |     output(opt, 1, _("Failed to merge submodule %s (not checked out)"), path); | 
1242  | 0  |     return 0;  | 
1243  | 0  |   }  | 
1244  |  |  | 
1245  | 0  |   if (!(commit_base = lookup_commit_reference(&subrepo, base)) ||  | 
1246  | 0  |       !(commit_a = lookup_commit_reference(&subrepo, a)) ||  | 
1247  | 0  |       !(commit_b = lookup_commit_reference(&subrepo, b))) { | 
1248  | 0  |     output(opt, 1, _("Failed to merge submodule %s (commits not present)"), path); | 
1249  | 0  |     goto cleanup;  | 
1250  | 0  |   }  | 
1251  |  |  | 
1252  |  |   /* check whether both changes are forward */  | 
1253  | 0  |   ret2 = repo_in_merge_bases(&subrepo, commit_base, commit_a);  | 
1254  | 0  |   if (ret2 < 0) { | 
1255  | 0  |     output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path); | 
1256  | 0  |     ret = -1;  | 
1257  | 0  |     goto cleanup;  | 
1258  | 0  |   }  | 
1259  | 0  |   if (ret2 > 0)  | 
1260  | 0  |     ret2 = repo_in_merge_bases(&subrepo, commit_base, commit_b);  | 
1261  | 0  |   if (ret2 < 0) { | 
1262  | 0  |     output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path); | 
1263  | 0  |     ret = -1;  | 
1264  | 0  |     goto cleanup;  | 
1265  | 0  |   }  | 
1266  | 0  |   if (!ret2) { | 
1267  | 0  |     output(opt, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path); | 
1268  | 0  |     goto cleanup;  | 
1269  | 0  |   }  | 
1270  |  |  | 
1271  |  |   /* Case #1: a is contained in b or vice versa */  | 
1272  | 0  |   ret2 = repo_in_merge_bases(&subrepo, commit_a, commit_b);  | 
1273  | 0  |   if (ret2 < 0) { | 
1274  | 0  |     output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path); | 
1275  | 0  |     ret = -1;  | 
1276  | 0  |     goto cleanup;  | 
1277  | 0  |   }  | 
1278  | 0  |   if (ret2) { | 
1279  | 0  |     oidcpy(result, b);  | 
1280  | 0  |     if (show(opt, 3)) { | 
1281  | 0  |       output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path); | 
1282  | 0  |       repo_output_commit_title(opt, &subrepo, commit_b);  | 
1283  | 0  |     } else if (show(opt, 2))  | 
1284  | 0  |       output(opt, 2, _("Fast-forwarding submodule %s"), path); | 
1285  | 0  |     else  | 
1286  | 0  |       ; /* no output */  | 
1287  |  | 
  | 
1288  | 0  |     ret = 1;  | 
1289  | 0  |     goto cleanup;  | 
1290  | 0  |   }  | 
1291  | 0  |   ret2 = repo_in_merge_bases(&subrepo, commit_b, commit_a);  | 
1292  | 0  |   if (ret2 < 0) { | 
1293  | 0  |     output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path); | 
1294  | 0  |     ret = -1;  | 
1295  | 0  |     goto cleanup;  | 
1296  | 0  |   }  | 
1297  | 0  |   if (ret2) { | 
1298  | 0  |     oidcpy(result, a);  | 
1299  | 0  |     if (show(opt, 3)) { | 
1300  | 0  |       output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path); | 
1301  | 0  |       repo_output_commit_title(opt, &subrepo, commit_a);  | 
1302  | 0  |     } else if (show(opt, 2))  | 
1303  | 0  |       output(opt, 2, _("Fast-forwarding submodule %s"), path); | 
1304  | 0  |     else  | 
1305  | 0  |       ; /* no output */  | 
1306  |  | 
  | 
1307  | 0  |     ret = 1;  | 
1308  | 0  |     goto cleanup;  | 
1309  | 0  |   }  | 
1310  |  |  | 
1311  |  |   /*  | 
1312  |  |    * Case #2: There are one or more merges that contain a and b in  | 
1313  |  |    * the submodule. If there is only one, then present it as a  | 
1314  |  |    * suggestion to the user, but leave it marked unmerged so the  | 
1315  |  |    * user needs to confirm the resolution.  | 
1316  |  |    */  | 
1317  |  |  | 
1318  |  |   /* Skip the search if makes no sense to the calling context.  */  | 
1319  | 0  |   if (!search)  | 
1320  | 0  |     goto cleanup;  | 
1321  |  |  | 
1322  |  |   /* find commit which merges them */  | 
1323  | 0  |   parent_count = find_first_merges(&subrepo, &merges, path,  | 
1324  | 0  |            commit_a, commit_b);  | 
1325  | 0  |   switch (parent_count) { | 
1326  | 0  |   case -1:  | 
1327  | 0  |     output(opt, 1,_("Failed to merge submodule %s (repository corrupt)"), path); | 
1328  | 0  |     ret = -1;  | 
1329  | 0  |     break;  | 
1330  | 0  |   case 0:  | 
1331  | 0  |     output(opt, 1, _("Failed to merge submodule %s (merge following commits not found)"), path); | 
1332  | 0  |     break;  | 
1333  |  |  | 
1334  | 0  |   case 1:  | 
1335  | 0  |     output(opt, 1, _("Failed to merge submodule %s (not fast-forward)"), path); | 
1336  | 0  |     output(opt, 2, _("Found a possible merge resolution for the submodule:\n")); | 
1337  | 0  |     print_commit(&subrepo, (struct commit *) merges.objects[0].item);  | 
1338  | 0  |     output(opt, 2, _(  | 
1339  | 0  |            "If this is correct simply add it to the index "  | 
1340  | 0  |            "for example\n"  | 
1341  | 0  |            "by using:\n\n"  | 
1342  | 0  |            "  git update-index --cacheinfo 160000 %s \"%s\"\n\n"  | 
1343  | 0  |            "which will accept this suggestion.\n"),  | 
1344  | 0  |            oid_to_hex(&merges.objects[0].item->oid), path);  | 
1345  | 0  |     break;  | 
1346  |  |  | 
1347  | 0  |   default:  | 
1348  | 0  |     output(opt, 1, _("Failed to merge submodule %s (multiple merges found)"), path); | 
1349  | 0  |     for (i = 0; i < merges.nr; i++)  | 
1350  | 0  |       print_commit(&subrepo, (struct commit *) merges.objects[i].item);  | 
1351  | 0  |   }  | 
1352  |  |  | 
1353  | 0  |   object_array_clear(&merges);  | 
1354  | 0  | cleanup:  | 
1355  | 0  |   repo_clear(&subrepo);  | 
1356  | 0  |   return ret;  | 
1357  | 0  | }  | 
1358  |  |  | 
1359  |  | static int merge_mode_and_contents(struct merge_options *opt,  | 
1360  |  |            const struct diff_filespec *o,  | 
1361  |  |            const struct diff_filespec *a,  | 
1362  |  |            const struct diff_filespec *b,  | 
1363  |  |            const char *filename,  | 
1364  |  |            const char *branch1,  | 
1365  |  |            const char *branch2,  | 
1366  |  |            const int extra_marker_size,  | 
1367  |  |            struct merge_file_info *result)  | 
1368  | 0  | { | 
1369  | 0  |   if (opt->branch1 != branch1) { | 
1370  |  |     /*  | 
1371  |  |      * It's weird getting a reverse merge with HEAD on the bottom  | 
1372  |  |      * side of the conflict markers and the other branch on the  | 
1373  |  |      * top.  Fix that.  | 
1374  |  |      */  | 
1375  | 0  |     return merge_mode_and_contents(opt, o, b, a,  | 
1376  | 0  |                  filename,  | 
1377  | 0  |                  branch2, branch1,  | 
1378  | 0  |                  extra_marker_size, result);  | 
1379  | 0  |   }  | 
1380  |  |  | 
1381  | 0  |   result->merge = 0;  | 
1382  | 0  |   result->clean = 1;  | 
1383  |  | 
  | 
1384  | 0  |   if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) { | 
1385  | 0  |     result->clean = 0;  | 
1386  |  |     /*  | 
1387  |  |      * FIXME: This is a bad resolution for recursive case; for  | 
1388  |  |      * the recursive case we want something that is unlikely to  | 
1389  |  |      * accidentally match either side.  Also, while it makes  | 
1390  |  |      * sense to prefer regular files over symlinks, it doesn't  | 
1391  |  |      * make sense to prefer regular files over submodules.  | 
1392  |  |      */  | 
1393  | 0  |     if (S_ISREG(a->mode)) { | 
1394  | 0  |       result->blob.mode = a->mode;  | 
1395  | 0  |       oidcpy(&result->blob.oid, &a->oid);  | 
1396  | 0  |     } else { | 
1397  | 0  |       result->blob.mode = b->mode;  | 
1398  | 0  |       oidcpy(&result->blob.oid, &b->oid);  | 
1399  | 0  |     }  | 
1400  | 0  |   } else { | 
1401  | 0  |     if (!oideq(&a->oid, &o->oid) && !oideq(&b->oid, &o->oid))  | 
1402  | 0  |       result->merge = 1;  | 
1403  |  |  | 
1404  |  |     /*  | 
1405  |  |      * Merge modes  | 
1406  |  |      */  | 
1407  | 0  |     if (a->mode == b->mode || a->mode == o->mode)  | 
1408  | 0  |       result->blob.mode = b->mode;  | 
1409  | 0  |     else { | 
1410  | 0  |       result->blob.mode = a->mode;  | 
1411  | 0  |       if (b->mode != o->mode) { | 
1412  | 0  |         result->clean = 0;  | 
1413  | 0  |         result->merge = 1;  | 
1414  | 0  |       }  | 
1415  | 0  |     }  | 
1416  |  | 
  | 
1417  | 0  |     if (oideq(&a->oid, &b->oid) || oideq(&a->oid, &o->oid))  | 
1418  | 0  |       oidcpy(&result->blob.oid, &b->oid);  | 
1419  | 0  |     else if (oideq(&b->oid, &o->oid))  | 
1420  | 0  |       oidcpy(&result->blob.oid, &a->oid);  | 
1421  | 0  |     else if (S_ISREG(a->mode)) { | 
1422  | 0  |       mmbuffer_t result_buf;  | 
1423  | 0  |       int ret = 0, merge_status;  | 
1424  |  | 
  | 
1425  | 0  |       merge_status = merge_3way(opt, &result_buf, o, a, b,  | 
1426  | 0  |               branch1, branch2,  | 
1427  | 0  |               extra_marker_size);  | 
1428  |  | 
  | 
1429  | 0  |       if ((merge_status < 0) || !result_buf.ptr)  | 
1430  | 0  |         ret = err(opt, _("failed to execute internal merge")); | 
1431  |  | 
  | 
1432  | 0  |       if (!ret &&  | 
1433  | 0  |           write_object_file(result_buf.ptr, result_buf.size,  | 
1434  | 0  |                 OBJ_BLOB, &result->blob.oid))  | 
1435  | 0  |         ret = err(opt, _("unable to add %s to database"), | 
1436  | 0  |             a->path);  | 
1437  |  | 
  | 
1438  | 0  |       free(result_buf.ptr);  | 
1439  | 0  |       if (ret)  | 
1440  | 0  |         return ret;  | 
1441  |  |       /* FIXME: bug, what if modes didn't match? */  | 
1442  | 0  |       result->clean = (merge_status == 0);  | 
1443  | 0  |     } else if (S_ISGITLINK(a->mode)) { | 
1444  | 0  |       int clean = merge_submodule(opt, &result->blob.oid,  | 
1445  | 0  |                 o->path,  | 
1446  | 0  |                 &o->oid,  | 
1447  | 0  |                 &a->oid,  | 
1448  | 0  |                 &b->oid);  | 
1449  | 0  |       if (clean < 0)  | 
1450  | 0  |         return -1;  | 
1451  | 0  |       result->clean = clean;  | 
1452  | 0  |     } else if (S_ISLNK(a->mode)) { | 
1453  | 0  |       switch (opt->recursive_variant) { | 
1454  | 0  |       case MERGE_VARIANT_NORMAL:  | 
1455  | 0  |         oidcpy(&result->blob.oid, &a->oid);  | 
1456  | 0  |         if (!oideq(&a->oid, &b->oid))  | 
1457  | 0  |           result->clean = 0;  | 
1458  | 0  |         break;  | 
1459  | 0  |       case MERGE_VARIANT_OURS:  | 
1460  | 0  |         oidcpy(&result->blob.oid, &a->oid);  | 
1461  | 0  |         break;  | 
1462  | 0  |       case MERGE_VARIANT_THEIRS:  | 
1463  | 0  |         oidcpy(&result->blob.oid, &b->oid);  | 
1464  | 0  |         break;  | 
1465  | 0  |       }  | 
1466  | 0  |     } else  | 
1467  | 0  |       BUG("unsupported object type in the tree"); | 
1468  | 0  |   }  | 
1469  |  |  | 
1470  | 0  |   if (result->merge)  | 
1471  | 0  |     output(opt, 2, _("Auto-merging %s"), filename); | 
1472  |  | 
  | 
1473  | 0  |   return 0;  | 
1474  | 0  | }  | 
1475  |  |  | 
1476  |  | static int handle_rename_via_dir(struct merge_options *opt,  | 
1477  |  |          struct rename_conflict_info *ci)  | 
1478  | 0  | { | 
1479  |  |   /*  | 
1480  |  |    * Handle file adds that need to be renamed due to directory rename  | 
1481  |  |    * detection.  This differs from handle_rename_normal, because  | 
1482  |  |    * there is no content merge to do; just move the file into the  | 
1483  |  |    * desired final location.  | 
1484  |  |    */  | 
1485  | 0  |   const struct rename *ren = ci->ren1;  | 
1486  | 0  |   const struct diff_filespec *dest = ren->pair->two;  | 
1487  | 0  |   char *file_path = dest->path;  | 
1488  | 0  |   int mark_conflicted = (opt->detect_directory_renames ==  | 
1489  | 0  |              MERGE_DIRECTORY_RENAMES_CONFLICT);  | 
1490  | 0  |   assert(ren->dir_rename_original_dest);  | 
1491  |  |  | 
1492  | 0  |   if (!opt->priv->call_depth && would_lose_untracked(opt, dest->path)) { | 
1493  | 0  |     mark_conflicted = 1;  | 
1494  | 0  |     file_path = unique_path(opt, dest->path, ren->branch);  | 
1495  | 0  |     output(opt, 1, _("Error: Refusing to lose untracked file at %s; " | 
1496  | 0  |          "writing to %s instead."),  | 
1497  | 0  |            dest->path, file_path);  | 
1498  | 0  |   }  | 
1499  |  | 
  | 
1500  | 0  |   if (mark_conflicted) { | 
1501  |  |     /*  | 
1502  |  |      * Write the file in worktree at file_path.  In the index,  | 
1503  |  |      * only record the file at dest->path in the appropriate  | 
1504  |  |      * higher stage.  | 
1505  |  |      */  | 
1506  | 0  |     if (update_file(opt, 0, dest, file_path))  | 
1507  | 0  |       return -1;  | 
1508  | 0  |     if (file_path != dest->path)  | 
1509  | 0  |       free(file_path);  | 
1510  | 0  |     if (update_stages(opt, dest->path, NULL,  | 
1511  | 0  |           ren->branch == opt->branch1 ? dest : NULL,  | 
1512  | 0  |           ren->branch == opt->branch1 ? NULL : dest))  | 
1513  | 0  |       return -1;  | 
1514  | 0  |     return 0; /* not clean, but conflicted */  | 
1515  | 0  |   } else { | 
1516  |  |     /* Update dest->path both in index and in worktree */  | 
1517  | 0  |     if (update_file(opt, 1, dest, dest->path))  | 
1518  | 0  |       return -1;  | 
1519  | 0  |     return 1; /* clean */  | 
1520  | 0  |   }  | 
1521  | 0  | }  | 
1522  |  |  | 
1523  |  | static int handle_change_delete(struct merge_options *opt,  | 
1524  |  |         const char *path, const char *old_path,  | 
1525  |  |         const struct diff_filespec *o,  | 
1526  |  |         const struct diff_filespec *changed,  | 
1527  |  |         const char *change_branch,  | 
1528  |  |         const char *delete_branch,  | 
1529  |  |         const char *change, const char *change_past)  | 
1530  | 0  | { | 
1531  | 0  |   char *alt_path = NULL;  | 
1532  | 0  |   const char *update_path = path;  | 
1533  | 0  |   int ret = 0;  | 
1534  |  | 
  | 
1535  | 0  |   if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0) ||  | 
1536  | 0  |       (!opt->priv->call_depth && would_lose_untracked(opt, path))) { | 
1537  | 0  |     update_path = alt_path = unique_path(opt, path, change_branch);  | 
1538  | 0  |   }  | 
1539  |  | 
  | 
1540  | 0  |   if (opt->priv->call_depth) { | 
1541  |  |     /*  | 
1542  |  |      * We cannot arbitrarily accept either a_sha or b_sha as  | 
1543  |  |      * correct; since there is no true "middle point" between  | 
1544  |  |      * them, simply reuse the base version for virtual merge base.  | 
1545  |  |      */  | 
1546  | 0  |     ret = remove_file_from_index(opt->repo->index, path);  | 
1547  | 0  |     if (!ret)  | 
1548  | 0  |       ret = update_file(opt, 0, o, update_path);  | 
1549  | 0  |   } else { | 
1550  |  |     /*  | 
1551  |  |      * Despite the four nearly duplicate messages and argument  | 
1552  |  |      * lists below and the ugliness of the nested if-statements,  | 
1553  |  |      * having complete messages makes the job easier for  | 
1554  |  |      * translators.  | 
1555  |  |      *  | 
1556  |  |      * The slight variance among the cases is due to the fact  | 
1557  |  |      * that:  | 
1558  |  |      *   1) directory/file conflicts (in effect if  | 
1559  |  |      *      !alt_path) could cause us to need to write the  | 
1560  |  |      *      file to a different path.  | 
1561  |  |      *   2) renames (in effect if !old_path) could mean that  | 
1562  |  |      *      there are two names for the path that the user  | 
1563  |  |      *      may know the file by.  | 
1564  |  |      */  | 
1565  | 0  |     if (!alt_path) { | 
1566  | 0  |       if (!old_path) { | 
1567  | 0  |         output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s " | 
1568  | 0  |                "and %s in %s. Version %s of %s left in tree."),  | 
1569  | 0  |                change, path, delete_branch, change_past,  | 
1570  | 0  |                change_branch, change_branch, path);  | 
1571  | 0  |       } else { | 
1572  | 0  |         output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s " | 
1573  | 0  |                "and %s to %s in %s. Version %s of %s left in tree."),  | 
1574  | 0  |                change, old_path, delete_branch, change_past, path,  | 
1575  | 0  |                change_branch, change_branch, path);  | 
1576  | 0  |       }  | 
1577  | 0  |     } else { | 
1578  | 0  |       if (!old_path) { | 
1579  | 0  |         output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s " | 
1580  | 0  |                "and %s in %s. Version %s of %s left in tree at %s."),  | 
1581  | 0  |                change, path, delete_branch, change_past,  | 
1582  | 0  |                change_branch, change_branch, path, alt_path);  | 
1583  | 0  |       } else { | 
1584  | 0  |         output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s " | 
1585  | 0  |                "and %s to %s in %s. Version %s of %s left in tree at %s."),  | 
1586  | 0  |                change, old_path, delete_branch, change_past, path,  | 
1587  | 0  |                change_branch, change_branch, path, alt_path);  | 
1588  | 0  |       }  | 
1589  | 0  |     }  | 
1590  |  |     /*  | 
1591  |  |      * No need to call update_file() on path when change_branch ==  | 
1592  |  |      * opt->branch1 && !alt_path, since that would needlessly touch  | 
1593  |  |      * path.  We could call update_file_flags() with update_cache=0  | 
1594  |  |      * and update_wd=0, but that's a no-op.  | 
1595  |  |      */  | 
1596  | 0  |     if (change_branch != opt->branch1 || alt_path)  | 
1597  | 0  |       ret = update_file(opt, 0, changed, update_path);  | 
1598  | 0  |   }  | 
1599  | 0  |   free(alt_path);  | 
1600  |  | 
  | 
1601  | 0  |   return ret;  | 
1602  | 0  | }  | 
1603  |  |  | 
1604  |  | static int handle_rename_delete(struct merge_options *opt,  | 
1605  |  |         struct rename_conflict_info *ci)  | 
1606  | 0  | { | 
1607  | 0  |   const struct rename *ren = ci->ren1;  | 
1608  | 0  |   const struct diff_filespec *orig = ren->pair->one;  | 
1609  | 0  |   const struct diff_filespec *dest = ren->pair->two;  | 
1610  | 0  |   const char *rename_branch = ren->branch;  | 
1611  | 0  |   const char *delete_branch = (opt->branch1 == ren->branch ?  | 
1612  | 0  |              opt->branch2 : opt->branch1);  | 
1613  |  | 
  | 
1614  | 0  |   if (handle_change_delete(opt,  | 
1615  | 0  |          opt->priv->call_depth ? orig->path : dest->path,  | 
1616  | 0  |          opt->priv->call_depth ? NULL : orig->path,  | 
1617  | 0  |          orig, dest,  | 
1618  | 0  |          rename_branch, delete_branch,  | 
1619  | 0  |          _("rename"), _("renamed"))) | 
1620  | 0  |     return -1;  | 
1621  |  |  | 
1622  | 0  |   if (opt->priv->call_depth)  | 
1623  | 0  |     return remove_file_from_index(opt->repo->index, dest->path);  | 
1624  | 0  |   else  | 
1625  | 0  |     return update_stages(opt, dest->path, NULL,  | 
1626  | 0  |              rename_branch == opt->branch1 ? dest : NULL,  | 
1627  | 0  |              rename_branch == opt->branch1 ? NULL : dest);  | 
1628  | 0  | }  | 
1629  |  |  | 
1630  |  | static int handle_file_collision(struct merge_options *opt,  | 
1631  |  |          const char *collide_path,  | 
1632  |  |          const char *prev_path1,  | 
1633  |  |          const char *prev_path2,  | 
1634  |  |          const char *branch1, const char *branch2,  | 
1635  |  |          struct diff_filespec *a,  | 
1636  |  |          struct diff_filespec *b)  | 
1637  | 0  | { | 
1638  | 0  |   struct merge_file_info mfi;  | 
1639  | 0  |   struct diff_filespec null;  | 
1640  | 0  |   char *alt_path = NULL;  | 
1641  | 0  |   const char *update_path = collide_path;  | 
1642  |  |  | 
1643  |  |   /*  | 
1644  |  |    * It's easiest to get the correct things into stage 2 and 3, and  | 
1645  |  |    * to make sure that the content merge puts HEAD before the other  | 
1646  |  |    * branch if we just ensure that branch1 == opt->branch1.  So, simply  | 
1647  |  |    * flip arguments around if we don't have that.  | 
1648  |  |    */  | 
1649  | 0  |   if (branch1 != opt->branch1) { | 
1650  | 0  |     return handle_file_collision(opt, collide_path,  | 
1651  | 0  |                prev_path2, prev_path1,  | 
1652  | 0  |                branch2, branch1,  | 
1653  | 0  |                b, a);  | 
1654  | 0  |   }  | 
1655  |  |  | 
1656  |  |   /* Remove rename sources if rename/add or rename/rename(2to1) */  | 
1657  | 0  |   if (prev_path1)  | 
1658  | 0  |     remove_file(opt, 1, prev_path1,  | 
1659  | 0  |           opt->priv->call_depth || would_lose_untracked(opt, prev_path1));  | 
1660  | 0  |   if (prev_path2)  | 
1661  | 0  |     remove_file(opt, 1, prev_path2,  | 
1662  | 0  |           opt->priv->call_depth || would_lose_untracked(opt, prev_path2));  | 
1663  |  |  | 
1664  |  |   /*  | 
1665  |  |    * Remove the collision path, if it wouldn't cause dirty contents  | 
1666  |  |    * or an untracked file to get lost.  We'll either overwrite with  | 
1667  |  |    * merged contents, or just write out to differently named files.  | 
1668  |  |    */  | 
1669  | 0  |   if (was_dirty(opt, collide_path)) { | 
1670  | 0  |     output(opt, 1, _("Refusing to lose dirty file at %s"), | 
1671  | 0  |            collide_path);  | 
1672  | 0  |     update_path = alt_path = unique_path(opt, collide_path, "merged");  | 
1673  | 0  |   } else if (would_lose_untracked(opt, collide_path)) { | 
1674  |  |     /*  | 
1675  |  |      * Only way we get here is if both renames were from  | 
1676  |  |      * a directory rename AND user had an untracked file  | 
1677  |  |      * at the location where both files end up after the  | 
1678  |  |      * two directory renames.  See testcase 10d of t6043.  | 
1679  |  |      */  | 
1680  | 0  |     output(opt, 1, _("Refusing to lose untracked file at " | 
1681  | 0  |              "%s, even though it's in the way."),  | 
1682  | 0  |            collide_path);  | 
1683  | 0  |     update_path = alt_path = unique_path(opt, collide_path, "merged");  | 
1684  | 0  |   } else { | 
1685  |  |     /*  | 
1686  |  |      * FIXME: It's possible that the two files are identical  | 
1687  |  |      * and that the current working copy happens to match, in  | 
1688  |  |      * which case we are unnecessarily touching the working  | 
1689  |  |      * tree file.  It's not a likely enough scenario that I  | 
1690  |  |      * want to code up the checks for it and a better fix is  | 
1691  |  |      * available if we restructure how unpack_trees() and  | 
1692  |  |      * merge-recursive interoperate anyway, so punting for  | 
1693  |  |      * now...  | 
1694  |  |      */  | 
1695  | 0  |     remove_file(opt, 0, collide_path, 0);  | 
1696  | 0  |   }  | 
1697  |  |  | 
1698  |  |   /* Store things in diff_filespecs for functions that need it */  | 
1699  | 0  |   null.path = (char *)collide_path;  | 
1700  | 0  |   oidcpy(&null.oid, null_oid());  | 
1701  | 0  |   null.mode = 0;  | 
1702  |  | 
  | 
1703  | 0  |   if (merge_mode_and_contents(opt, &null, a, b, collide_path,  | 
1704  | 0  |             branch1, branch2, opt->priv->call_depth * 2, &mfi))  | 
1705  | 0  |     return -1;  | 
1706  | 0  |   mfi.clean &= !alt_path;  | 
1707  | 0  |   if (update_file(opt, mfi.clean, &mfi.blob, update_path))  | 
1708  | 0  |     return -1;  | 
1709  | 0  |   if (!mfi.clean && !opt->priv->call_depth &&  | 
1710  | 0  |       update_stages(opt, collide_path, NULL, a, b))  | 
1711  | 0  |     return -1;  | 
1712  | 0  |   free(alt_path);  | 
1713  |  |   /*  | 
1714  |  |    * FIXME: If both a & b both started with conflicts (only possible  | 
1715  |  |    * if they came from a rename/rename(2to1)), but had IDENTICAL  | 
1716  |  |    * contents including those conflicts, then in the next line we claim  | 
1717  |  |    * it was clean.  If someone cares about this case, we should have the  | 
1718  |  |    * caller notify us if we started with conflicts.  | 
1719  |  |    */  | 
1720  | 0  |   return mfi.clean;  | 
1721  | 0  | }  | 
1722  |  |  | 
1723  |  | static int handle_rename_add(struct merge_options *opt,  | 
1724  |  |            struct rename_conflict_info *ci)  | 
1725  | 0  | { | 
1726  |  |   /* a was renamed to c, and a separate c was added. */  | 
1727  | 0  |   struct diff_filespec *a = ci->ren1->pair->one;  | 
1728  | 0  |   struct diff_filespec *c = ci->ren1->pair->two;  | 
1729  | 0  |   char *path = c->path;  | 
1730  | 0  |   char *prev_path_desc;  | 
1731  | 0  |   struct merge_file_info mfi;  | 
1732  |  | 
  | 
1733  | 0  |   const char *rename_branch = ci->ren1->branch;  | 
1734  | 0  |   const char *add_branch = (opt->branch1 == rename_branch ?  | 
1735  | 0  |           opt->branch2 : opt->branch1);  | 
1736  | 0  |   int other_stage = (ci->ren1->branch == opt->branch1 ? 3 : 2);  | 
1737  |  | 
  | 
1738  | 0  |   output(opt, 1, _("CONFLICT (rename/add): " | 
1739  | 0  |          "Rename %s->%s in %s.  Added %s in %s"),  | 
1740  | 0  |          a->path, c->path, rename_branch,  | 
1741  | 0  |          c->path, add_branch);  | 
1742  |  | 
  | 
1743  | 0  |   prev_path_desc = xstrfmt("version of %s from %s", path, a->path); | 
1744  | 0  |   ci->ren1->src_entry->stages[other_stage].path = a->path;  | 
1745  | 0  |   if (merge_mode_and_contents(opt, a, c,  | 
1746  | 0  |             &ci->ren1->src_entry->stages[other_stage],  | 
1747  | 0  |             prev_path_desc,  | 
1748  | 0  |             opt->branch1, opt->branch2,  | 
1749  | 0  |             1 + opt->priv->call_depth * 2, &mfi))  | 
1750  | 0  |     return -1;  | 
1751  | 0  |   free(prev_path_desc);  | 
1752  |  | 
  | 
1753  | 0  |   ci->ren1->dst_entry->stages[other_stage].path = mfi.blob.path = c->path;  | 
1754  | 0  |   return handle_file_collision(opt,  | 
1755  | 0  |              c->path, a->path, NULL,  | 
1756  | 0  |              rename_branch, add_branch,  | 
1757  | 0  |              &mfi.blob,  | 
1758  | 0  |              &ci->ren1->dst_entry->stages[other_stage]);  | 
1759  | 0  | }  | 
1760  |  |  | 
1761  |  | static char *find_path_for_conflict(struct merge_options *opt,  | 
1762  |  |             const char *path,  | 
1763  |  |             const char *branch1,  | 
1764  |  |             const char *branch2)  | 
1765  | 0  | { | 
1766  | 0  |   char *new_path = NULL;  | 
1767  | 0  |   if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0)) { | 
1768  | 0  |     new_path = unique_path(opt, path, branch1);  | 
1769  | 0  |     output(opt, 1, _("%s is a directory in %s adding " | 
1770  | 0  |              "as %s instead"),  | 
1771  | 0  |            path, branch2, new_path);  | 
1772  | 0  |   } else if (would_lose_untracked(opt, path)) { | 
1773  | 0  |     new_path = unique_path(opt, path, branch1);  | 
1774  | 0  |     output(opt, 1, _("Refusing to lose untracked file" | 
1775  | 0  |              " at %s; adding as %s instead"),  | 
1776  | 0  |            path, new_path);  | 
1777  | 0  |   }  | 
1778  |  | 
  | 
1779  | 0  |   return new_path;  | 
1780  | 0  | }  | 
1781  |  |  | 
1782  |  | /*  | 
1783  |  |  * Toggle the stage number between "ours" and "theirs" (2 and 3).  | 
1784  |  |  */  | 
1785  |  | static inline int flip_stage(int stage)  | 
1786  | 0  | { | 
1787  | 0  |   return (2 + 3) - stage;  | 
1788  | 0  | }  | 
1789  |  |  | 
1790  |  | static int handle_rename_rename_1to2(struct merge_options *opt,  | 
1791  |  |              struct rename_conflict_info *ci)  | 
1792  | 0  | { | 
1793  |  |   /* One file was renamed in both branches, but to different names. */  | 
1794  | 0  |   struct merge_file_info mfi;  | 
1795  | 0  |   struct diff_filespec *add;  | 
1796  | 0  |   struct diff_filespec *o = ci->ren1->pair->one;  | 
1797  | 0  |   struct diff_filespec *a = ci->ren1->pair->two;  | 
1798  | 0  |   struct diff_filespec *b = ci->ren2->pair->two;  | 
1799  | 0  |   char *path_desc;  | 
1800  |  | 
  | 
1801  | 0  |   output(opt, 1, _("CONFLICT (rename/rename): " | 
1802  | 0  |          "Rename \"%s\"->\"%s\" in branch \"%s\" "  | 
1803  | 0  |          "rename \"%s\"->\"%s\" in \"%s\"%s"),  | 
1804  | 0  |          o->path, a->path, ci->ren1->branch,  | 
1805  | 0  |          o->path, b->path, ci->ren2->branch,  | 
1806  | 0  |          opt->priv->call_depth ? _(" (left unresolved)") : ""); | 
1807  |  | 
  | 
1808  | 0  |   path_desc = xstrfmt("%s and %s, both renamed from %s", | 
1809  | 0  |           a->path, b->path, o->path);  | 
1810  | 0  |   if (merge_mode_and_contents(opt, o, a, b, path_desc,  | 
1811  | 0  |             ci->ren1->branch, ci->ren2->branch,  | 
1812  | 0  |             opt->priv->call_depth * 2, &mfi))  | 
1813  | 0  |     return -1;  | 
1814  | 0  |   free(path_desc);  | 
1815  |  | 
  | 
1816  | 0  |   if (opt->priv->call_depth)  | 
1817  | 0  |     remove_file_from_index(opt->repo->index, o->path);  | 
1818  |  |  | 
1819  |  |   /*  | 
1820  |  |    * For each destination path, we need to see if there is a  | 
1821  |  |    * rename/add collision.  If not, we can write the file out  | 
1822  |  |    * to the specified location.  | 
1823  |  |    */  | 
1824  | 0  |   add = &ci->ren1->dst_entry->stages[flip_stage(2)];  | 
1825  | 0  |   if (is_valid(add)) { | 
1826  | 0  |     add->path = mfi.blob.path = a->path;  | 
1827  | 0  |     if (handle_file_collision(opt, a->path,  | 
1828  | 0  |             NULL, NULL,  | 
1829  | 0  |             ci->ren1->branch,  | 
1830  | 0  |             ci->ren2->branch,  | 
1831  | 0  |             &mfi.blob, add) < 0)  | 
1832  | 0  |       return -1;  | 
1833  | 0  |   } else { | 
1834  | 0  |     char *new_path = find_path_for_conflict(opt, a->path,  | 
1835  | 0  |               ci->ren1->branch,  | 
1836  | 0  |               ci->ren2->branch);  | 
1837  | 0  |     if (update_file(opt, 0, &mfi.blob,  | 
1838  | 0  |         new_path ? new_path : a->path))  | 
1839  | 0  |       return -1;  | 
1840  | 0  |     free(new_path);  | 
1841  | 0  |     if (!opt->priv->call_depth &&  | 
1842  | 0  |         update_stages(opt, a->path, NULL, a, NULL))  | 
1843  | 0  |       return -1;  | 
1844  | 0  |   }  | 
1845  |  |  | 
1846  | 0  |   if (!mfi.clean && mfi.blob.mode == a->mode &&  | 
1847  | 0  |       oideq(&mfi.blob.oid, &a->oid)) { | 
1848  |  |     /*  | 
1849  |  |      * Getting here means we were attempting to merge a binary  | 
1850  |  |      * blob.  Since we can't merge binaries, the merge algorithm  | 
1851  |  |      * just takes one side.  But we don't want to copy the  | 
1852  |  |      * contents of one side to both paths; we'd rather use the  | 
1853  |  |      * original content at the given path for each path.  | 
1854  |  |      */  | 
1855  | 0  |     oidcpy(&mfi.blob.oid, &b->oid);  | 
1856  | 0  |     mfi.blob.mode = b->mode;  | 
1857  | 0  |   }  | 
1858  | 0  |   add = &ci->ren2->dst_entry->stages[flip_stage(3)];  | 
1859  | 0  |   if (is_valid(add)) { | 
1860  | 0  |     add->path = mfi.blob.path = b->path;  | 
1861  | 0  |     if (handle_file_collision(opt, b->path,  | 
1862  | 0  |             NULL, NULL,  | 
1863  | 0  |             ci->ren1->branch,  | 
1864  | 0  |             ci->ren2->branch,  | 
1865  | 0  |             add, &mfi.blob) < 0)  | 
1866  | 0  |       return -1;  | 
1867  | 0  |   } else { | 
1868  | 0  |     char *new_path = find_path_for_conflict(opt, b->path,  | 
1869  | 0  |               ci->ren2->branch,  | 
1870  | 0  |               ci->ren1->branch);  | 
1871  | 0  |     if (update_file(opt, 0, &mfi.blob,  | 
1872  | 0  |         new_path ? new_path : b->path))  | 
1873  | 0  |       return -1;  | 
1874  | 0  |     free(new_path);  | 
1875  | 0  |     if (!opt->priv->call_depth &&  | 
1876  | 0  |         update_stages(opt, b->path, NULL, NULL, b))  | 
1877  | 0  |       return -1;  | 
1878  | 0  |   }  | 
1879  |  |  | 
1880  | 0  |   return 0;  | 
1881  | 0  | }  | 
1882  |  |  | 
1883  |  | static int handle_rename_rename_2to1(struct merge_options *opt,  | 
1884  |  |              struct rename_conflict_info *ci)  | 
1885  | 0  | { | 
1886  |  |   /* Two files, a & b, were renamed to the same thing, c. */  | 
1887  | 0  |   struct diff_filespec *a = ci->ren1->pair->one;  | 
1888  | 0  |   struct diff_filespec *b = ci->ren2->pair->one;  | 
1889  | 0  |   struct diff_filespec *c1 = ci->ren1->pair->two;  | 
1890  | 0  |   struct diff_filespec *c2 = ci->ren2->pair->two;  | 
1891  | 0  |   char *path = c1->path; /* == c2->path */  | 
1892  | 0  |   char *path_side_1_desc;  | 
1893  | 0  |   char *path_side_2_desc;  | 
1894  | 0  |   struct merge_file_info mfi_c1;  | 
1895  | 0  |   struct merge_file_info mfi_c2;  | 
1896  | 0  |   int ostage1, ostage2;  | 
1897  |  | 
  | 
1898  | 0  |   output(opt, 1, _("CONFLICT (rename/rename): " | 
1899  | 0  |          "Rename %s->%s in %s. "  | 
1900  | 0  |          "Rename %s->%s in %s"),  | 
1901  | 0  |          a->path, c1->path, ci->ren1->branch,  | 
1902  | 0  |          b->path, c2->path, ci->ren2->branch);  | 
1903  |  | 
  | 
1904  | 0  |   path_side_1_desc = xstrfmt("version of %s from %s", path, a->path); | 
1905  | 0  |   path_side_2_desc = xstrfmt("version of %s from %s", path, b->path); | 
1906  | 0  |   ostage1 = ci->ren1->branch == opt->branch1 ? 3 : 2;  | 
1907  | 0  |   ostage2 = flip_stage(ostage1);  | 
1908  | 0  |   ci->ren1->src_entry->stages[ostage1].path = a->path;  | 
1909  | 0  |   ci->ren2->src_entry->stages[ostage2].path = b->path;  | 
1910  | 0  |   if (merge_mode_and_contents(opt, a, c1,  | 
1911  | 0  |             &ci->ren1->src_entry->stages[ostage1],  | 
1912  | 0  |             path_side_1_desc,  | 
1913  | 0  |             opt->branch1, opt->branch2,  | 
1914  | 0  |             1 + opt->priv->call_depth * 2, &mfi_c1) ||  | 
1915  | 0  |       merge_mode_and_contents(opt, b,  | 
1916  | 0  |             &ci->ren2->src_entry->stages[ostage2],  | 
1917  | 0  |             c2, path_side_2_desc,  | 
1918  | 0  |             opt->branch1, opt->branch2,  | 
1919  | 0  |             1 + opt->priv->call_depth * 2, &mfi_c2))  | 
1920  | 0  |     return -1;  | 
1921  | 0  |   free(path_side_1_desc);  | 
1922  | 0  |   free(path_side_2_desc);  | 
1923  | 0  |   mfi_c1.blob.path = path;  | 
1924  | 0  |   mfi_c2.blob.path = path;  | 
1925  |  | 
  | 
1926  | 0  |   return handle_file_collision(opt, path, a->path, b->path,  | 
1927  | 0  |              ci->ren1->branch, ci->ren2->branch,  | 
1928  | 0  |              &mfi_c1.blob, &mfi_c2.blob);  | 
1929  | 0  | }  | 
1930  |  |  | 
1931  |  | /*  | 
1932  |  |  * Get the diff_filepairs changed between o_tree and tree.  | 
1933  |  |  */  | 
1934  |  | static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,  | 
1935  |  |                  struct tree *o_tree,  | 
1936  |  |                  struct tree *tree)  | 
1937  | 0  | { | 
1938  | 0  |   struct diff_queue_struct *ret;  | 
1939  | 0  |   struct diff_options opts;  | 
1940  |  | 
  | 
1941  | 0  |   repo_diff_setup(opt->repo, &opts);  | 
1942  | 0  |   opts.flags.recursive = 1;  | 
1943  | 0  |   opts.flags.rename_empty = 0;  | 
1944  | 0  |   opts.detect_rename = merge_detect_rename(opt);  | 
1945  |  |   /*  | 
1946  |  |    * We do not have logic to handle the detection of copies.  In  | 
1947  |  |    * fact, it may not even make sense to add such logic: would we  | 
1948  |  |    * really want a change to a base file to be propagated through  | 
1949  |  |    * multiple other files by a merge?  | 
1950  |  |    */  | 
1951  | 0  |   if (opts.detect_rename > DIFF_DETECT_RENAME)  | 
1952  | 0  |     opts.detect_rename = DIFF_DETECT_RENAME;  | 
1953  | 0  |   opts.rename_limit = (opt->rename_limit >= 0) ? opt->rename_limit : 7000;  | 
1954  | 0  |   opts.rename_score = opt->rename_score;  | 
1955  | 0  |   opts.show_rename_progress = opt->show_rename_progress;  | 
1956  | 0  |   opts.output_format = DIFF_FORMAT_NO_OUTPUT;  | 
1957  | 0  |   diff_setup_done(&opts);  | 
1958  | 0  |   diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);  | 
1959  | 0  |   diffcore_std(&opts);  | 
1960  | 0  |   if (opts.needed_rename_limit > opt->priv->needed_rename_limit)  | 
1961  | 0  |     opt->priv->needed_rename_limit = opts.needed_rename_limit;  | 
1962  |  | 
  | 
1963  | 0  |   ret = xmalloc(sizeof(*ret));  | 
1964  | 0  |   *ret = diff_queued_diff;  | 
1965  |  | 
  | 
1966  | 0  |   opts.output_format = DIFF_FORMAT_NO_OUTPUT;  | 
1967  | 0  |   diff_queued_diff.nr = 0;  | 
1968  | 0  |   diff_queued_diff.queue = NULL;  | 
1969  | 0  |   diff_flush(&opts);  | 
1970  | 0  |   return ret;  | 
1971  | 0  | }  | 
1972  |  |  | 
1973  |  | static int tree_has_path(struct repository *r, struct tree *tree,  | 
1974  |  |        const char *path)  | 
1975  | 0  | { | 
1976  | 0  |   struct object_id hashy;  | 
1977  | 0  |   unsigned short mode_o;  | 
1978  |  | 
  | 
1979  | 0  |   return !get_tree_entry(r,  | 
1980  | 0  |              &tree->object.oid, path,  | 
1981  | 0  |              &hashy, &mode_o);  | 
1982  | 0  | }  | 
1983  |  |  | 
1984  |  | /*  | 
1985  |  |  * Return a new string that replaces the beginning portion (which matches  | 
1986  |  |  * entry->dir), with entry->new_dir.  In perl-speak:  | 
1987  |  |  *   new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);  | 
1988  |  |  * NOTE:  | 
1989  |  |  *   Caller must ensure that old_path starts with entry->dir + '/'.  | 
1990  |  |  */  | 
1991  |  | static char *apply_dir_rename(struct dir_rename_entry *entry,  | 
1992  |  |             const char *old_path)  | 
1993  | 0  | { | 
1994  | 0  |   struct strbuf new_path = STRBUF_INIT;  | 
1995  | 0  |   int oldlen, newlen;  | 
1996  |  | 
  | 
1997  | 0  |   if (entry->non_unique_new_dir)  | 
1998  | 0  |     return NULL;  | 
1999  |  |  | 
2000  | 0  |   oldlen = strlen(entry->dir);  | 
2001  | 0  |   if (entry->new_dir.len == 0)  | 
2002  |  |     /*  | 
2003  |  |      * If someone renamed/merged a subdirectory into the root  | 
2004  |  |      * directory (e.g. 'some/subdir' -> ''), then we want to  | 
2005  |  |      * avoid returning  | 
2006  |  |      *     '' + '/filename'  | 
2007  |  |      * as the rename; we need to make old_path + oldlen advance  | 
2008  |  |      * past the '/' character.  | 
2009  |  |      */  | 
2010  | 0  |     oldlen++;  | 
2011  | 0  |   newlen = entry->new_dir.len + (strlen(old_path) - oldlen) + 1;  | 
2012  | 0  |   strbuf_grow(&new_path, newlen);  | 
2013  | 0  |   strbuf_addbuf(&new_path, &entry->new_dir);  | 
2014  | 0  |   strbuf_addstr(&new_path, &old_path[oldlen]);  | 
2015  |  | 
  | 
2016  | 0  |   return strbuf_detach(&new_path, NULL);  | 
2017  | 0  | }  | 
2018  |  |  | 
2019  |  | static void get_renamed_dir_portion(const char *old_path, const char *new_path,  | 
2020  |  |             char **old_dir, char **new_dir)  | 
2021  | 0  | { | 
2022  | 0  |   char *end_of_old, *end_of_new;  | 
2023  |  |  | 
2024  |  |   /* Default return values: NULL, meaning no rename */  | 
2025  | 0  |   *old_dir = NULL;  | 
2026  | 0  |   *new_dir = NULL;  | 
2027  |  |  | 
2028  |  |   /*  | 
2029  |  |    * For  | 
2030  |  |    *    "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"  | 
2031  |  |    * the "e/foo.c" part is the same, we just want to know that  | 
2032  |  |    *    "a/b/c/d" was renamed to "a/b/some/thing/else"  | 
2033  |  |    * so, for this example, this function returns "a/b/c/d" in  | 
2034  |  |    * *old_dir and "a/b/some/thing/else" in *new_dir.  | 
2035  |  |    */  | 
2036  |  |  | 
2037  |  |   /*  | 
2038  |  |    * If the basename of the file changed, we don't care.  We want  | 
2039  |  |    * to know which portion of the directory, if any, changed.  | 
2040  |  |    */  | 
2041  | 0  |   end_of_old = strrchr(old_path, '/');  | 
2042  | 0  |   end_of_new = strrchr(new_path, '/');  | 
2043  |  |  | 
2044  |  |   /*  | 
2045  |  |    * If end_of_old is NULL, old_path wasn't in a directory, so there  | 
2046  |  |    * could not be a directory rename (our rule elsewhere that a  | 
2047  |  |    * directory which still exists is not considered to have been  | 
2048  |  |    * renamed means the root directory can never be renamed -- because  | 
2049  |  |    * the root directory always exists).  | 
2050  |  |    */  | 
2051  | 0  |   if (!end_of_old)  | 
2052  | 0  |     return; /* Note: *old_dir and *new_dir are still NULL */  | 
2053  |  |  | 
2054  |  |   /*  | 
2055  |  |    * If new_path contains no directory (end_of_new is NULL), then we  | 
2056  |  |    * have a rename of old_path's directory to the root directory.  | 
2057  |  |    */  | 
2058  | 0  |   if (!end_of_new) { | 
2059  | 0  |     *old_dir = xstrndup(old_path, end_of_old - old_path);  | 
2060  | 0  |     *new_dir = xstrdup(""); | 
2061  | 0  |     return;  | 
2062  | 0  |   }  | 
2063  |  |  | 
2064  |  |   /* Find the first non-matching character traversing backwards */  | 
2065  | 0  |   while (*--end_of_new == *--end_of_old &&  | 
2066  | 0  |          end_of_old != old_path &&  | 
2067  | 0  |          end_of_new != new_path)  | 
2068  | 0  |     ; /* Do nothing; all in the while loop */  | 
2069  |  |  | 
2070  |  |   /*  | 
2071  |  |    * If both got back to the beginning of their strings, then the  | 
2072  |  |    * directory didn't change at all, only the basename did.  | 
2073  |  |    */  | 
2074  | 0  |   if (end_of_old == old_path && end_of_new == new_path &&  | 
2075  | 0  |       *end_of_old == *end_of_new)  | 
2076  | 0  |     return; /* Note: *old_dir and *new_dir are still NULL */  | 
2077  |  |  | 
2078  |  |   /*  | 
2079  |  |    * If end_of_new got back to the beginning of its string, and  | 
2080  |  |    * end_of_old got back to the beginning of some subdirectory, then  | 
2081  |  |    * we have a rename/merge of a subdirectory into the root, which  | 
2082  |  |    * needs slightly special handling.  | 
2083  |  |    *  | 
2084  |  |    * Note: There is no need to consider the opposite case, with a  | 
2085  |  |    * rename/merge of the root directory into some subdirectory  | 
2086  |  |    * because as noted above the root directory always exists so it  | 
2087  |  |    * cannot be considered to be renamed.  | 
2088  |  |    */  | 
2089  | 0  |   if (end_of_new == new_path &&  | 
2090  | 0  |       end_of_old != old_path && end_of_old[-1] == '/') { | 
2091  | 0  |     *old_dir = xstrndup(old_path, --end_of_old - old_path);  | 
2092  | 0  |     *new_dir = xstrdup(""); | 
2093  | 0  |     return;  | 
2094  | 0  |   }  | 
2095  |  |  | 
2096  |  |   /*  | 
2097  |  |    * We've found the first non-matching character in the directory  | 
2098  |  |    * paths.  That means the current characters we were looking at  | 
2099  |  |    * were part of the first non-matching subdir name going back from  | 
2100  |  |    * the end of the strings.  Get the whole name by advancing both  | 
2101  |  |    * end_of_old and end_of_new to the NEXT '/' character.  That will  | 
2102  |  |    * represent the entire directory rename.  | 
2103  |  |    *  | 
2104  |  |    * The reason for the increment is cases like  | 
2105  |  |    *    a/b/star/foo/whatever.c -> a/b/tar/foo/random.c  | 
2106  |  |    * After dropping the basename and going back to the first  | 
2107  |  |    * non-matching character, we're now comparing:  | 
2108  |  |    *    a/b/s          and         a/b/  | 
2109  |  |    * and we want to be comparing:  | 
2110  |  |    *    a/b/star/      and         a/b/tar/  | 
2111  |  |    * but without the pre-increment, the one on the right would stay  | 
2112  |  |    * a/b/.  | 
2113  |  |    */  | 
2114  | 0  |   end_of_old = strchr(++end_of_old, '/');  | 
2115  | 0  |   end_of_new = strchr(++end_of_new, '/');  | 
2116  |  |  | 
2117  |  |   /* Copy the old and new directories into *old_dir and *new_dir. */  | 
2118  | 0  |   *old_dir = xstrndup(old_path, end_of_old - old_path);  | 
2119  | 0  |   *new_dir = xstrndup(new_path, end_of_new - new_path);  | 
2120  | 0  | }  | 
2121  |  |  | 
2122  |  | static void remove_hashmap_entries(struct hashmap *dir_renames,  | 
2123  |  |            struct string_list *items_to_remove)  | 
2124  | 0  | { | 
2125  | 0  |   int i;  | 
2126  | 0  |   struct dir_rename_entry *entry;  | 
2127  |  | 
  | 
2128  | 0  |   for (i = 0; i < items_to_remove->nr; i++) { | 
2129  | 0  |     entry = items_to_remove->items[i].util;  | 
2130  | 0  |     hashmap_remove(dir_renames, &entry->ent, NULL);  | 
2131  | 0  |   }  | 
2132  | 0  |   string_list_clear(items_to_remove, 0);  | 
2133  | 0  | }  | 
2134  |  |  | 
2135  |  | /*  | 
2136  |  |  * See if there is a directory rename for path, and if there are any file  | 
2137  |  |  * level conflicts for the renamed location.  If there is a rename and  | 
2138  |  |  * there are no conflicts, return the new name.  Otherwise, return NULL.  | 
2139  |  |  */  | 
2140  |  | static char *handle_path_level_conflicts(struct merge_options *opt,  | 
2141  |  |            const char *path,  | 
2142  |  |            struct dir_rename_entry *entry,  | 
2143  |  |            struct hashmap *collisions,  | 
2144  |  |            struct tree *tree)  | 
2145  | 0  | { | 
2146  | 0  |   char *new_path = NULL;  | 
2147  | 0  |   struct collision_entry *collision_ent;  | 
2148  | 0  |   int clean = 1;  | 
2149  | 0  |   struct strbuf collision_paths = STRBUF_INIT;  | 
2150  |  |  | 
2151  |  |   /*  | 
2152  |  |    * entry has the mapping of old directory name to new directory name  | 
2153  |  |    * that we want to apply to path.  | 
2154  |  |    */  | 
2155  | 0  |   new_path = apply_dir_rename(entry, path);  | 
2156  |  | 
  | 
2157  | 0  |   if (!new_path) { | 
2158  |  |     /* This should only happen when entry->non_unique_new_dir set */  | 
2159  | 0  |     if (!entry->non_unique_new_dir)  | 
2160  | 0  |       BUG("entry->non_unique_new_dir not set and !new_path"); | 
2161  | 0  |     output(opt, 1, _("CONFLICT (directory rename split): " | 
2162  | 0  |              "Unclear where to place %s because directory "  | 
2163  | 0  |              "%s was renamed to multiple other directories, "  | 
2164  | 0  |              "with no destination getting a majority of the "  | 
2165  | 0  |              "files."),  | 
2166  | 0  |            path, entry->dir);  | 
2167  | 0  |     clean = 0;  | 
2168  | 0  |     return NULL;  | 
2169  | 0  |   }  | 
2170  |  |  | 
2171  |  |   /*  | 
2172  |  |    * The caller needs to have ensured that it has pre-populated  | 
2173  |  |    * collisions with all paths that map to new_path.  Do a quick check  | 
2174  |  |    * to ensure that's the case.  | 
2175  |  |    */  | 
2176  | 0  |   collision_ent = collision_find_entry(collisions, new_path);  | 
2177  | 0  |   if (!collision_ent)  | 
2178  | 0  |     BUG("collision_ent is NULL"); | 
2179  |  |  | 
2180  |  |   /*  | 
2181  |  |    * Check for one-sided add/add/.../add conflicts, i.e.  | 
2182  |  |    * where implicit renames from the other side doing  | 
2183  |  |    * directory rename(s) can affect this side of history  | 
2184  |  |    * to put multiple paths into the same location.  Warn  | 
2185  |  |    * and bail on directory renames for such paths.  | 
2186  |  |    */  | 
2187  | 0  |   if (collision_ent->reported_already) { | 
2188  | 0  |     clean = 0;  | 
2189  | 0  |   } else if (tree_has_path(opt->repo, tree, new_path)) { | 
2190  | 0  |     collision_ent->reported_already = 1;  | 
2191  | 0  |     strbuf_add_separated_string_list(&collision_paths, ", ",  | 
2192  | 0  |              &collision_ent->source_files);  | 
2193  | 0  |     output(opt, 1, _("CONFLICT (implicit dir rename): Existing " | 
2194  | 0  |              "file/dir at %s in the way of implicit "  | 
2195  | 0  |              "directory rename(s) putting the following "  | 
2196  | 0  |              "path(s) there: %s."),  | 
2197  | 0  |            new_path, collision_paths.buf);  | 
2198  | 0  |     clean = 0;  | 
2199  | 0  |   } else if (collision_ent->source_files.nr > 1) { | 
2200  | 0  |     collision_ent->reported_already = 1;  | 
2201  | 0  |     strbuf_add_separated_string_list(&collision_paths, ", ",  | 
2202  | 0  |              &collision_ent->source_files);  | 
2203  | 0  |     output(opt, 1, _("CONFLICT (implicit dir rename): Cannot map " | 
2204  | 0  |              "more than one path to %s; implicit directory "  | 
2205  | 0  |              "renames tried to put these paths there: %s"),  | 
2206  | 0  |            new_path, collision_paths.buf);  | 
2207  | 0  |     clean = 0;  | 
2208  | 0  |   }  | 
2209  |  |  | 
2210  |  |   /* Free memory we no longer need */  | 
2211  | 0  |   strbuf_release(&collision_paths);  | 
2212  | 0  |   if (!clean && new_path) { | 
2213  | 0  |     free(new_path);  | 
2214  | 0  |     return NULL;  | 
2215  | 0  |   }  | 
2216  |  |  | 
2217  | 0  |   return new_path;  | 
2218  | 0  | }  | 
2219  |  |  | 
2220  |  | /*  | 
2221  |  |  * There are a couple things we want to do at the directory level:  | 
2222  |  |  *   1. Check for both sides renaming to the same thing, in order to avoid  | 
2223  |  |  *      implicit renaming of files that should be left in place.  (See  | 
2224  |  |  *      testcase 6b in t6043 for details.)  | 
2225  |  |  *   2. Prune directory renames if there are still files left in the  | 
2226  |  |  *      original directory.  These represent a partial directory rename,  | 
2227  |  |  *      i.e. a rename where only some of the files within the directory  | 
2228  |  |  *      were renamed elsewhere.  (Technically, this could be done earlier  | 
2229  |  |  *      in get_directory_renames(), except that would prevent us from  | 
2230  |  |  *      doing the previous check and thus failing testcase 6b.)  | 
2231  |  |  *   3. Check for rename/rename(1to2) conflicts (at the directory level).  | 
2232  |  |  *      In the future, we could potentially record this info as well and  | 
2233  |  |  *      omit reporting rename/rename(1to2) conflicts for each path within  | 
2234  |  |  *      the affected directories, thus cleaning up the merge output.  | 
2235  |  |  *   NOTE: We do NOT check for rename/rename(2to1) conflicts at the  | 
2236  |  |  *         directory level, because merging directories is fine.  If it  | 
2237  |  |  *         causes conflicts for files within those merged directories, then  | 
2238  |  |  *         that should be detected at the individual path level.  | 
2239  |  |  */  | 
2240  |  | static void handle_directory_level_conflicts(struct merge_options *opt,  | 
2241  |  |                struct hashmap *dir_re_head,  | 
2242  |  |                struct tree *head,  | 
2243  |  |                struct hashmap *dir_re_merge,  | 
2244  |  |                struct tree *merge)  | 
2245  | 0  | { | 
2246  | 0  |   struct hashmap_iter iter;  | 
2247  | 0  |   struct dir_rename_entry *head_ent;  | 
2248  | 0  |   struct dir_rename_entry *merge_ent;  | 
2249  |  | 
  | 
2250  | 0  |   struct string_list remove_from_head = STRING_LIST_INIT_NODUP;  | 
2251  | 0  |   struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;  | 
2252  |  | 
  | 
2253  | 0  |   hashmap_for_each_entry(dir_re_head, &iter, head_ent,  | 
2254  | 0  |         ent /* member name */) { | 
2255  | 0  |     merge_ent = dir_rename_find_entry(dir_re_merge, head_ent->dir);  | 
2256  | 0  |     if (merge_ent &&  | 
2257  | 0  |         !head_ent->non_unique_new_dir &&  | 
2258  | 0  |         !merge_ent->non_unique_new_dir &&  | 
2259  | 0  |         !strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) { | 
2260  |  |       /* 1. Renamed identically; remove it from both sides */  | 
2261  | 0  |       string_list_append(&remove_from_head,  | 
2262  | 0  |              head_ent->dir)->util = head_ent;  | 
2263  | 0  |       strbuf_release(&head_ent->new_dir);  | 
2264  | 0  |       string_list_append(&remove_from_merge,  | 
2265  | 0  |              merge_ent->dir)->util = merge_ent;  | 
2266  | 0  |       strbuf_release(&merge_ent->new_dir);  | 
2267  | 0  |     } else if (tree_has_path(opt->repo, head, head_ent->dir)) { | 
2268  |  |       /* 2. This wasn't a directory rename after all */  | 
2269  | 0  |       string_list_append(&remove_from_head,  | 
2270  | 0  |              head_ent->dir)->util = head_ent;  | 
2271  | 0  |       strbuf_release(&head_ent->new_dir);  | 
2272  | 0  |     }  | 
2273  | 0  |   }  | 
2274  |  | 
  | 
2275  | 0  |   remove_hashmap_entries(dir_re_head, &remove_from_head);  | 
2276  | 0  |   remove_hashmap_entries(dir_re_merge, &remove_from_merge);  | 
2277  |  | 
  | 
2278  | 0  |   hashmap_for_each_entry(dir_re_merge, &iter, merge_ent,  | 
2279  | 0  |         ent /* member name */) { | 
2280  | 0  |     head_ent = dir_rename_find_entry(dir_re_head, merge_ent->dir);  | 
2281  | 0  |     if (tree_has_path(opt->repo, merge, merge_ent->dir)) { | 
2282  |  |       /* 2. This wasn't a directory rename after all */  | 
2283  | 0  |       string_list_append(&remove_from_merge,  | 
2284  | 0  |              merge_ent->dir)->util = merge_ent;  | 
2285  | 0  |     } else if (head_ent &&  | 
2286  | 0  |          !head_ent->non_unique_new_dir &&  | 
2287  | 0  |          !merge_ent->non_unique_new_dir) { | 
2288  |  |       /* 3. rename/rename(1to2) */  | 
2289  |  |       /*  | 
2290  |  |        * We can assume it's not rename/rename(1to1) because  | 
2291  |  |        * that was case (1), already checked above.  So we  | 
2292  |  |        * know that head_ent->new_dir and merge_ent->new_dir  | 
2293  |  |        * are different strings.  | 
2294  |  |        */  | 
2295  | 0  |       output(opt, 1, _("CONFLICT (rename/rename): " | 
2296  | 0  |                "Rename directory %s->%s in %s. "  | 
2297  | 0  |                "Rename directory %s->%s in %s"),  | 
2298  | 0  |              head_ent->dir, head_ent->new_dir.buf, opt->branch1,  | 
2299  | 0  |              head_ent->dir, merge_ent->new_dir.buf, opt->branch2);  | 
2300  | 0  |       string_list_append(&remove_from_head,  | 
2301  | 0  |              head_ent->dir)->util = head_ent;  | 
2302  | 0  |       strbuf_release(&head_ent->new_dir);  | 
2303  | 0  |       string_list_append(&remove_from_merge,  | 
2304  | 0  |              merge_ent->dir)->util = merge_ent;  | 
2305  | 0  |       strbuf_release(&merge_ent->new_dir);  | 
2306  | 0  |     }  | 
2307  | 0  |   }  | 
2308  |  | 
  | 
2309  | 0  |   remove_hashmap_entries(dir_re_head, &remove_from_head);  | 
2310  | 0  |   remove_hashmap_entries(dir_re_merge, &remove_from_merge);  | 
2311  | 0  | }  | 
2312  |  |  | 
2313  |  | static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)  | 
2314  | 0  | { | 
2315  | 0  |   struct hashmap *dir_renames;  | 
2316  | 0  |   struct hashmap_iter iter;  | 
2317  | 0  |   struct dir_rename_entry *entry;  | 
2318  | 0  |   int i;  | 
2319  |  |  | 
2320  |  |   /*  | 
2321  |  |    * Typically, we think of a directory rename as all files from a  | 
2322  |  |    * certain directory being moved to a target directory.  However,  | 
2323  |  |    * what if someone first moved two files from the original  | 
2324  |  |    * directory in one commit, and then renamed the directory  | 
2325  |  |    * somewhere else in a later commit?  At merge time, we just know  | 
2326  |  |    * that files from the original directory went to two different  | 
2327  |  |    * places, and that the bulk of them ended up in the same place.  | 
2328  |  |    * We want each directory rename to represent where the bulk of the  | 
2329  |  |    * files from that directory end up; this function exists to find  | 
2330  |  |    * where the bulk of the files went.  | 
2331  |  |    *  | 
2332  |  |    * The first loop below simply iterates through the list of file  | 
2333  |  |    * renames, finding out how often each directory rename pair  | 
2334  |  |    * possibility occurs.  | 
2335  |  |    */  | 
2336  | 0  |   dir_renames = xmalloc(sizeof(*dir_renames));  | 
2337  | 0  |   dir_rename_init(dir_renames);  | 
2338  | 0  |   for (i = 0; i < pairs->nr; ++i) { | 
2339  | 0  |     struct string_list_item *item;  | 
2340  | 0  |     int *count;  | 
2341  | 0  |     struct diff_filepair *pair = pairs->queue[i];  | 
2342  | 0  |     char *old_dir, *new_dir;  | 
2343  |  |  | 
2344  |  |     /* File not part of directory rename if it wasn't renamed */  | 
2345  | 0  |     if (pair->status != 'R')  | 
2346  | 0  |       continue;  | 
2347  |  |  | 
2348  | 0  |     get_renamed_dir_portion(pair->one->path, pair->two->path,  | 
2349  | 0  |           &old_dir,        &new_dir);  | 
2350  | 0  |     if (!old_dir)  | 
2351  |  |       /* Directory didn't change at all; ignore this one. */  | 
2352  | 0  |       continue;  | 
2353  |  |  | 
2354  | 0  |     entry = dir_rename_find_entry(dir_renames, old_dir);  | 
2355  | 0  |     if (!entry) { | 
2356  | 0  |       entry = xmalloc(sizeof(*entry));  | 
2357  | 0  |       dir_rename_entry_init(entry, old_dir);  | 
2358  | 0  |       hashmap_put(dir_renames, &entry->ent);  | 
2359  | 0  |     } else { | 
2360  | 0  |       free(old_dir);  | 
2361  | 0  |     }  | 
2362  | 0  |     item = string_list_lookup(&entry->possible_new_dirs, new_dir);  | 
2363  | 0  |     if (!item) { | 
2364  | 0  |       item = string_list_insert(&entry->possible_new_dirs,  | 
2365  | 0  |               new_dir);  | 
2366  | 0  |       item->util = xcalloc(1, sizeof(int));  | 
2367  | 0  |     } else { | 
2368  | 0  |       free(new_dir);  | 
2369  | 0  |     }  | 
2370  | 0  |     count = item->util;  | 
2371  | 0  |     *count += 1;  | 
2372  | 0  |   }  | 
2373  |  |  | 
2374  |  |   /*  | 
2375  |  |    * For each directory with files moved out of it, we find out which  | 
2376  |  |    * target directory received the most files so we can declare it to  | 
2377  |  |    * be the "winning" target location for the directory rename.  This  | 
2378  |  |    * winner gets recorded in new_dir.  If there is no winner  | 
2379  |  |    * (multiple target directories received the same number of files),  | 
2380  |  |    * we set non_unique_new_dir.  Once we've determined the winner (or  | 
2381  |  |    * that there is no winner), we no longer need possible_new_dirs.  | 
2382  |  |    */  | 
2383  | 0  |   hashmap_for_each_entry(dir_renames, &iter, entry,  | 
2384  | 0  |         ent /* member name */) { | 
2385  | 0  |     int max = 0;  | 
2386  | 0  |     int bad_max = 0;  | 
2387  | 0  |     char *best = NULL;  | 
2388  |  | 
  | 
2389  | 0  |     for (i = 0; i < entry->possible_new_dirs.nr; i++) { | 
2390  | 0  |       int *count = entry->possible_new_dirs.items[i].util;  | 
2391  |  | 
  | 
2392  | 0  |       if (*count == max)  | 
2393  | 0  |         bad_max = max;  | 
2394  | 0  |       else if (*count > max) { | 
2395  | 0  |         max = *count;  | 
2396  | 0  |         best = entry->possible_new_dirs.items[i].string;  | 
2397  | 0  |       }  | 
2398  | 0  |     }  | 
2399  | 0  |     if (bad_max == max)  | 
2400  | 0  |       entry->non_unique_new_dir = 1;  | 
2401  | 0  |     else { | 
2402  | 0  |       assert(entry->new_dir.len == 0);  | 
2403  | 0  |       strbuf_addstr(&entry->new_dir, best);  | 
2404  | 0  |     }  | 
2405  |  |     /*  | 
2406  |  |      * The relevant directory sub-portion of the original full  | 
2407  |  |      * filepaths were xstrndup'ed before inserting into  | 
2408  |  |      * possible_new_dirs, and instead of manually iterating the  | 
2409  |  |      * list and free'ing each, just lie and tell  | 
2410  |  |      * possible_new_dirs that it did the strdup'ing so that it  | 
2411  |  |      * will free them for us.  | 
2412  |  |      */  | 
2413  | 0  |     entry->possible_new_dirs.strdup_strings = 1;  | 
2414  | 0  |     string_list_clear(&entry->possible_new_dirs, 1);  | 
2415  | 0  |   }  | 
2416  |  |  | 
2417  | 0  |   return dir_renames;  | 
2418  | 0  | }  | 
2419  |  |  | 
2420  |  | static struct dir_rename_entry *check_dir_renamed(const char *path,  | 
2421  |  |               struct hashmap *dir_renames)  | 
2422  | 0  | { | 
2423  | 0  |   char *temp = xstrdup(path);  | 
2424  | 0  |   char *end;  | 
2425  | 0  |   struct dir_rename_entry *entry = NULL;  | 
2426  |  | 
  | 
2427  | 0  |   while ((end = strrchr(temp, '/'))) { | 
2428  | 0  |     *end = '\0';  | 
2429  | 0  |     entry = dir_rename_find_entry(dir_renames, temp);  | 
2430  | 0  |     if (entry)  | 
2431  | 0  |       break;  | 
2432  | 0  |   }  | 
2433  | 0  |   free(temp);  | 
2434  | 0  |   return entry;  | 
2435  | 0  | }  | 
2436  |  |  | 
2437  |  | static void compute_collisions(struct hashmap *collisions,  | 
2438  |  |              struct hashmap *dir_renames,  | 
2439  |  |              struct diff_queue_struct *pairs)  | 
2440  | 0  | { | 
2441  | 0  |   int i;  | 
2442  |  |  | 
2443  |  |   /*  | 
2444  |  |    * Multiple files can be mapped to the same path due to directory  | 
2445  |  |    * renames done by the other side of history.  Since that other  | 
2446  |  |    * side of history could have merged multiple directories into one,  | 
2447  |  |    * if our side of history added the same file basename to each of  | 
2448  |  |    * those directories, then all N of them would get implicitly  | 
2449  |  |    * renamed by the directory rename detection into the same path,  | 
2450  |  |    * and we'd get an add/add/.../add conflict, and all those adds  | 
2451  |  |    * from *this* side of history.  This is not representable in the  | 
2452  |  |    * index, and users aren't going to easily be able to make sense of  | 
2453  |  |    * it.  So we need to provide a good warning about what's  | 
2454  |  |    * happening, and fall back to no-directory-rename detection  | 
2455  |  |    * behavior for those paths.  | 
2456  |  |    *  | 
2457  |  |    * See testcases 9e and all of section 5 from t6043 for examples.  | 
2458  |  |    */  | 
2459  | 0  |   collision_init(collisions);  | 
2460  |  | 
  | 
2461  | 0  |   for (i = 0; i < pairs->nr; ++i) { | 
2462  | 0  |     struct dir_rename_entry *dir_rename_ent;  | 
2463  | 0  |     struct collision_entry *collision_ent;  | 
2464  | 0  |     char *new_path;  | 
2465  | 0  |     struct diff_filepair *pair = pairs->queue[i];  | 
2466  |  | 
  | 
2467  | 0  |     if (pair->status != 'A' && pair->status != 'R')  | 
2468  | 0  |       continue;  | 
2469  | 0  |     dir_rename_ent = check_dir_renamed(pair->two->path,  | 
2470  | 0  |                dir_renames);  | 
2471  | 0  |     if (!dir_rename_ent)  | 
2472  | 0  |       continue;  | 
2473  |  |  | 
2474  | 0  |     new_path = apply_dir_rename(dir_rename_ent, pair->two->path);  | 
2475  | 0  |     if (!new_path)  | 
2476  |  |       /*  | 
2477  |  |        * dir_rename_ent->non_unique_new_path is true, which  | 
2478  |  |        * means there is no directory rename for us to use,  | 
2479  |  |        * which means it won't cause us any additional  | 
2480  |  |        * collisions.  | 
2481  |  |        */  | 
2482  | 0  |       continue;  | 
2483  | 0  |     collision_ent = collision_find_entry(collisions, new_path);  | 
2484  | 0  |     if (!collision_ent) { | 
2485  | 0  |       CALLOC_ARRAY(collision_ent, 1);  | 
2486  | 0  |       hashmap_entry_init(&collision_ent->ent,  | 
2487  | 0  |             strhash(new_path));  | 
2488  | 0  |       hashmap_put(collisions, &collision_ent->ent);  | 
2489  | 0  |       collision_ent->target_file = new_path;  | 
2490  | 0  |     } else { | 
2491  | 0  |       free(new_path);  | 
2492  | 0  |     }  | 
2493  | 0  |     string_list_insert(&collision_ent->source_files,  | 
2494  | 0  |            pair->two->path);  | 
2495  | 0  |   }  | 
2496  | 0  | }  | 
2497  |  |  | 
2498  |  | static char *check_for_directory_rename(struct merge_options *opt,  | 
2499  |  |           const char *path,  | 
2500  |  |           struct tree *tree,  | 
2501  |  |           struct hashmap *dir_renames,  | 
2502  |  |           struct hashmap *dir_rename_exclusions,  | 
2503  |  |           struct hashmap *collisions,  | 
2504  |  |           int *clean_merge)  | 
2505  | 0  | { | 
2506  | 0  |   char *new_path = NULL;  | 
2507  | 0  |   struct dir_rename_entry *entry = check_dir_renamed(path, dir_renames);  | 
2508  | 0  |   struct dir_rename_entry *oentry = NULL;  | 
2509  |  | 
  | 
2510  | 0  |   if (!entry)  | 
2511  | 0  |     return new_path;  | 
2512  |  |  | 
2513  |  |   /*  | 
2514  |  |    * This next part is a little weird.  We do not want to do an  | 
2515  |  |    * implicit rename into a directory we renamed on our side, because  | 
2516  |  |    * that will result in a spurious rename/rename(1to2) conflict.  An  | 
2517  |  |    * example:  | 
2518  |  |    *   Base commit: dumbdir/afile, otherdir/bfile  | 
2519  |  |    *   Side 1:      smrtdir/afile, otherdir/bfile  | 
2520  |  |    *   Side 2:      dumbdir/afile, dumbdir/bfile  | 
2521  |  |    * Here, while working on Side 1, we could notice that otherdir was  | 
2522  |  |    * renamed/merged to dumbdir, and change the diff_filepair for  | 
2523  |  |    * otherdir/bfile into a rename into dumbdir/bfile.  However, Side  | 
2524  |  |    * 2 will notice the rename from dumbdir to smrtdir, and do the  | 
2525  |  |    * transitive rename to move it from dumbdir/bfile to  | 
2526  |  |    * smrtdir/bfile.  That gives us bfile in dumbdir vs being in  | 
2527  |  |    * smrtdir, a rename/rename(1to2) conflict.  We really just want  | 
2528  |  |    * the file to end up in smrtdir.  And the way to achieve that is  | 
2529  |  |    * to not let Side1 do the rename to dumbdir, since we know that is  | 
2530  |  |    * the source of one of our directory renames.  | 
2531  |  |    *  | 
2532  |  |    * That's why oentry and dir_rename_exclusions is here.  | 
2533  |  |    *  | 
2534  |  |    * As it turns out, this also prevents N-way transient rename  | 
2535  |  |    * confusion; See testcases 9c and 9d of t6043.  | 
2536  |  |    */  | 
2537  | 0  |   oentry = dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);  | 
2538  | 0  |   if (oentry) { | 
2539  | 0  |     output(opt, 1, _("WARNING: Avoiding applying %s -> %s rename " | 
2540  | 0  |              "to %s, because %s itself was renamed."),  | 
2541  | 0  |            entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);  | 
2542  | 0  |   } else { | 
2543  | 0  |     new_path = handle_path_level_conflicts(opt, path, entry,  | 
2544  | 0  |                    collisions, tree);  | 
2545  | 0  |     *clean_merge &= (new_path != NULL);  | 
2546  | 0  |   }  | 
2547  |  | 
  | 
2548  | 0  |   return new_path;  | 
2549  | 0  | }  | 
2550  |  |  | 
2551  |  | static void apply_directory_rename_modifications(struct merge_options *opt,  | 
2552  |  |              struct diff_filepair *pair,  | 
2553  |  |              char *new_path,  | 
2554  |  |              struct rename *re,  | 
2555  |  |              struct tree *tree,  | 
2556  |  |              struct tree *o_tree,  | 
2557  |  |              struct tree *a_tree,  | 
2558  |  |              struct tree *b_tree,  | 
2559  |  |              struct string_list *entries)  | 
2560  | 0  | { | 
2561  | 0  |   struct string_list_item *item;  | 
2562  | 0  |   int stage = (tree == a_tree ? 2 : 3);  | 
2563  | 0  |   int update_wd;  | 
2564  |  |  | 
2565  |  |   /*  | 
2566  |  |    * In all cases where we can do directory rename detection,  | 
2567  |  |    * unpack_trees() will have read pair->two->path into the  | 
2568  |  |    * index and the working copy.  We need to remove it so that  | 
2569  |  |    * we can instead place it at new_path.  It is guaranteed to  | 
2570  |  |    * not be untracked (unpack_trees() would have errored out  | 
2571  |  |    * saying the file would have been overwritten), but it might  | 
2572  |  |    * be dirty, though.  | 
2573  |  |    */  | 
2574  | 0  |   update_wd = !was_dirty(opt, pair->two->path);  | 
2575  | 0  |   if (!update_wd)  | 
2576  | 0  |     output(opt, 1, _("Refusing to lose dirty file at %s"), | 
2577  | 0  |            pair->two->path);  | 
2578  | 0  |   remove_file(opt, 1, pair->two->path, !update_wd);  | 
2579  |  |  | 
2580  |  |   /* Find or create a new re->dst_entry */  | 
2581  | 0  |   item = string_list_lookup(entries, new_path);  | 
2582  | 0  |   if (item) { | 
2583  |  |     /*  | 
2584  |  |      * Since we're renaming on this side of history, and it's  | 
2585  |  |      * due to a directory rename on the other side of history  | 
2586  |  |      * (which we only allow when the directory in question no  | 
2587  |  |      * longer exists on the other side of history), the  | 
2588  |  |      * original entry for re->dst_entry is no longer  | 
2589  |  |      * necessary...  | 
2590  |  |      */  | 
2591  | 0  |     re->dst_entry->processed = 1;  | 
2592  |  |  | 
2593  |  |     /*  | 
2594  |  |      * ...because we'll be using this new one.  | 
2595  |  |      */  | 
2596  | 0  |     re->dst_entry = item->util;  | 
2597  | 0  |   } else { | 
2598  |  |     /*  | 
2599  |  |      * re->dst_entry is for the before-dir-rename path, and we  | 
2600  |  |      * need it to hold information for the after-dir-rename  | 
2601  |  |      * path.  Before creating a new entry, we need to mark the  | 
2602  |  |      * old one as unnecessary (...unless it is shared by  | 
2603  |  |      * src_entry, i.e. this didn't use to be a rename, in which  | 
2604  |  |      * case we can just allow the normal processing to happen  | 
2605  |  |      * for it).  | 
2606  |  |      */  | 
2607  | 0  |     if (pair->status == 'R')  | 
2608  | 0  |       re->dst_entry->processed = 1;  | 
2609  |  | 
  | 
2610  | 0  |     re->dst_entry = insert_stage_data(opt->repo, new_path,  | 
2611  | 0  |               o_tree, a_tree, b_tree,  | 
2612  | 0  |               entries);  | 
2613  | 0  |     item = string_list_insert(entries, new_path);  | 
2614  | 0  |     item->util = re->dst_entry;  | 
2615  | 0  |   }  | 
2616  |  |  | 
2617  |  |   /*  | 
2618  |  |    * Update the stage_data with the information about the path we are  | 
2619  |  |    * moving into place.  That slot will be empty and available for us  | 
2620  |  |    * to write to because of the collision checks in  | 
2621  |  |    * handle_path_level_conflicts().  In other words,  | 
2622  |  |    * re->dst_entry->stages[stage].oid will be the null_oid, so it's  | 
2623  |  |    * open for us to write to.  | 
2624  |  |    *  | 
2625  |  |    * It may be tempting to actually update the index at this point as  | 
2626  |  |    * well, using update_stages_for_stage_data(), but as per the big  | 
2627  |  |    * "NOTE" in update_stages(), doing so will modify the current  | 
2628  |  |    * in-memory index which will break calls to would_lose_untracked()  | 
2629  |  |    * that we need to make.  Instead, we need to just make sure that  | 
2630  |  |    * the various handle_rename_*() functions update the index  | 
2631  |  |    * explicitly rather than relying on unpack_trees() to have done it.  | 
2632  |  |    */  | 
2633  | 0  |   get_tree_entry(opt->repo,  | 
2634  | 0  |            &tree->object.oid,  | 
2635  | 0  |            pair->two->path,  | 
2636  | 0  |            &re->dst_entry->stages[stage].oid,  | 
2637  | 0  |            &re->dst_entry->stages[stage].mode);  | 
2638  |  |  | 
2639  |  |   /*  | 
2640  |  |    * Record the original change status (or 'type' of change).  If it  | 
2641  |  |    * was originally an add ('A'), this lets us differentiate later | 
2642  |  |    * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they  | 
2643  |  |    * otherwise look the same).  If it was originally a rename ('R'), | 
2644  |  |    * this lets us remember and report accurately about the transitive  | 
2645  |  |    * renaming that occurred via the directory rename detection.  Also,  | 
2646  |  |    * record the original destination name.  | 
2647  |  |    */  | 
2648  | 0  |   re->dir_rename_original_type = pair->status;  | 
2649  | 0  |   re->dir_rename_original_dest = pair->two->path;  | 
2650  |  |  | 
2651  |  |   /*  | 
2652  |  |    * We don't actually look at pair->status again, but it seems  | 
2653  |  |    * pedagogically correct to adjust it.  | 
2654  |  |    */  | 
2655  | 0  |   pair->status = 'R';  | 
2656  |  |  | 
2657  |  |   /*  | 
2658  |  |    * Finally, record the new location.  | 
2659  |  |    */  | 
2660  | 0  |   pair->two->path = new_path;  | 
2661  | 0  | }  | 
2662  |  |  | 
2663  |  | /*  | 
2664  |  |  * Get information of all renames which occurred in 'pairs', making use of  | 
2665  |  |  * any implicit directory renames inferred from the other side of history.  | 
2666  |  |  * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree') | 
2667  |  |  * to be able to associate the correct cache entries with the rename  | 
2668  |  |  * information; tree is always equal to either a_tree or b_tree.  | 
2669  |  |  */  | 
2670  |  | static struct string_list *get_renames(struct merge_options *opt,  | 
2671  |  |                const char *branch,  | 
2672  |  |                struct diff_queue_struct *pairs,  | 
2673  |  |                struct hashmap *dir_renames,  | 
2674  |  |                struct hashmap *dir_rename_exclusions,  | 
2675  |  |                struct tree *tree,  | 
2676  |  |                struct tree *o_tree,  | 
2677  |  |                struct tree *a_tree,  | 
2678  |  |                struct tree *b_tree,  | 
2679  |  |                struct string_list *entries,  | 
2680  |  |                int *clean_merge)  | 
2681  | 0  | { | 
2682  | 0  |   int i;  | 
2683  | 0  |   struct hashmap collisions;  | 
2684  | 0  |   struct hashmap_iter iter;  | 
2685  | 0  |   struct collision_entry *e;  | 
2686  | 0  |   struct string_list *renames;  | 
2687  |  | 
  | 
2688  | 0  |   compute_collisions(&collisions, dir_renames, pairs);  | 
2689  | 0  |   CALLOC_ARRAY(renames, 1);  | 
2690  |  | 
  | 
2691  | 0  |   for (i = 0; i < pairs->nr; ++i) { | 
2692  | 0  |     struct string_list_item *item;  | 
2693  | 0  |     struct rename *re;  | 
2694  | 0  |     struct diff_filepair *pair = pairs->queue[i];  | 
2695  | 0  |     char *new_path; /* non-NULL only with directory renames */  | 
2696  |  | 
  | 
2697  | 0  |     if (pair->status != 'A' && pair->status != 'R') { | 
2698  | 0  |       diff_free_filepair(pair);  | 
2699  | 0  |       continue;  | 
2700  | 0  |     }  | 
2701  | 0  |     new_path = check_for_directory_rename(opt, pair->two->path, tree,  | 
2702  | 0  |                   dir_renames,  | 
2703  | 0  |                   dir_rename_exclusions,  | 
2704  | 0  |                   &collisions,  | 
2705  | 0  |                   clean_merge);  | 
2706  | 0  |     if (pair->status != 'R' && !new_path) { | 
2707  | 0  |       diff_free_filepair(pair);  | 
2708  | 0  |       continue;  | 
2709  | 0  |     }  | 
2710  |  |  | 
2711  | 0  |     re = xmalloc(sizeof(*re));  | 
2712  | 0  |     re->processed = 0;  | 
2713  | 0  |     re->pair = pair;  | 
2714  | 0  |     re->branch = branch;  | 
2715  | 0  |     re->dir_rename_original_type = '\0';  | 
2716  | 0  |     re->dir_rename_original_dest = NULL;  | 
2717  | 0  |     item = string_list_lookup(entries, re->pair->one->path);  | 
2718  | 0  |     if (!item)  | 
2719  | 0  |       re->src_entry = insert_stage_data(opt->repo,  | 
2720  | 0  |           re->pair->one->path,  | 
2721  | 0  |           o_tree, a_tree, b_tree, entries);  | 
2722  | 0  |     else  | 
2723  | 0  |       re->src_entry = item->util;  | 
2724  |  | 
  | 
2725  | 0  |     item = string_list_lookup(entries, re->pair->two->path);  | 
2726  | 0  |     if (!item)  | 
2727  | 0  |       re->dst_entry = insert_stage_data(opt->repo,  | 
2728  | 0  |           re->pair->two->path,  | 
2729  | 0  |           o_tree, a_tree, b_tree, entries);  | 
2730  | 0  |     else  | 
2731  | 0  |       re->dst_entry = item->util;  | 
2732  | 0  |     item = string_list_insert(renames, pair->one->path);  | 
2733  | 0  |     item->util = re;  | 
2734  | 0  |     if (new_path)  | 
2735  | 0  |       apply_directory_rename_modifications(opt, pair, new_path,  | 
2736  | 0  |                    re, tree, o_tree,  | 
2737  | 0  |                    a_tree, b_tree,  | 
2738  | 0  |                    entries);  | 
2739  | 0  |   }  | 
2740  |  | 
  | 
2741  | 0  |   hashmap_for_each_entry(&collisions, &iter, e,  | 
2742  | 0  |         ent /* member name */) { | 
2743  | 0  |     free(e->target_file);  | 
2744  | 0  |     string_list_clear(&e->source_files, 0);  | 
2745  | 0  |   }  | 
2746  | 0  |   hashmap_clear_and_free(&collisions, struct collision_entry, ent);  | 
2747  | 0  |   return renames;  | 
2748  | 0  | }  | 
2749  |  |  | 
2750  |  | static int process_renames(struct merge_options *opt,  | 
2751  |  |          struct string_list *a_renames,  | 
2752  |  |          struct string_list *b_renames)  | 
2753  | 0  | { | 
2754  | 0  |   int clean_merge = 1, i, j;  | 
2755  | 0  |   struct string_list a_by_dst = STRING_LIST_INIT_NODUP;  | 
2756  | 0  |   struct string_list b_by_dst = STRING_LIST_INIT_NODUP;  | 
2757  | 0  |   const struct rename *sre;  | 
2758  |  |  | 
2759  |  |   /*  | 
2760  |  |    * FIXME: As string-list.h notes, it's O(n^2) to build a sorted  | 
2761  |  |    * string_list one-by-one, but O(n log n) to build it unsorted and  | 
2762  |  |    * then sort it.  Note that as we build the list, we do not need to  | 
2763  |  |    * check if the existing destination path is already in the list,  | 
2764  |  |    * because the structure of diffcore_rename guarantees we won't  | 
2765  |  |    * have duplicates.  | 
2766  |  |    */  | 
2767  | 0  |   for (i = 0; i < a_renames->nr; i++) { | 
2768  | 0  |     sre = a_renames->items[i].util;  | 
2769  | 0  |     string_list_insert(&a_by_dst, sre->pair->two->path)->util  | 
2770  | 0  |       = (void *)sre;  | 
2771  | 0  |   }  | 
2772  | 0  |   for (i = 0; i < b_renames->nr; i++) { | 
2773  | 0  |     sre = b_renames->items[i].util;  | 
2774  | 0  |     string_list_insert(&b_by_dst, sre->pair->two->path)->util  | 
2775  | 0  |       = (void *)sre;  | 
2776  | 0  |   }  | 
2777  |  | 
  | 
2778  | 0  |   for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) { | 
2779  | 0  |     struct string_list *renames1, *renames2Dst;  | 
2780  | 0  |     struct rename *ren1 = NULL, *ren2 = NULL;  | 
2781  | 0  |     const char *ren1_src, *ren1_dst;  | 
2782  | 0  |     struct string_list_item *lookup;  | 
2783  |  | 
  | 
2784  | 0  |     if (i >= a_renames->nr) { | 
2785  | 0  |       ren2 = b_renames->items[j++].util;  | 
2786  | 0  |     } else if (j >= b_renames->nr) { | 
2787  | 0  |       ren1 = a_renames->items[i++].util;  | 
2788  | 0  |     } else { | 
2789  | 0  |       int compare = strcmp(a_renames->items[i].string,  | 
2790  | 0  |                b_renames->items[j].string);  | 
2791  | 0  |       if (compare <= 0)  | 
2792  | 0  |         ren1 = a_renames->items[i++].util;  | 
2793  | 0  |       if (compare >= 0)  | 
2794  | 0  |         ren2 = b_renames->items[j++].util;  | 
2795  | 0  |     }  | 
2796  |  |  | 
2797  |  |     /* TODO: refactor, so that 1/2 are not needed */  | 
2798  | 0  |     if (ren1) { | 
2799  | 0  |       renames1 = a_renames;  | 
2800  | 0  |       renames2Dst = &b_by_dst;  | 
2801  | 0  |     } else { | 
2802  | 0  |       renames1 = b_renames;  | 
2803  | 0  |       renames2Dst = &a_by_dst;  | 
2804  | 0  |       SWAP(ren2, ren1);  | 
2805  | 0  |     }  | 
2806  |  | 
  | 
2807  | 0  |     if (ren1->processed)  | 
2808  | 0  |       continue;  | 
2809  | 0  |     ren1->processed = 1;  | 
2810  | 0  |     ren1->dst_entry->processed = 1;  | 
2811  |  |     /* BUG: We should only mark src_entry as processed if we  | 
2812  |  |      * are not dealing with a rename + add-source case.  | 
2813  |  |      */  | 
2814  | 0  |     ren1->src_entry->processed = 1;  | 
2815  |  | 
  | 
2816  | 0  |     ren1_src = ren1->pair->one->path;  | 
2817  | 0  |     ren1_dst = ren1->pair->two->path;  | 
2818  |  | 
  | 
2819  | 0  |     if (ren2) { | 
2820  |  |       /* One file renamed on both sides */  | 
2821  | 0  |       const char *ren2_src = ren2->pair->one->path;  | 
2822  | 0  |       const char *ren2_dst = ren2->pair->two->path;  | 
2823  | 0  |       enum rename_type rename_type;  | 
2824  | 0  |       if (strcmp(ren1_src, ren2_src) != 0)  | 
2825  | 0  |         BUG("ren1_src != ren2_src"); | 
2826  | 0  |       ren2->dst_entry->processed = 1;  | 
2827  | 0  |       ren2->processed = 1;  | 
2828  | 0  |       if (strcmp(ren1_dst, ren2_dst) != 0) { | 
2829  | 0  |         rename_type = RENAME_ONE_FILE_TO_TWO;  | 
2830  | 0  |         clean_merge = 0;  | 
2831  | 0  |       } else { | 
2832  | 0  |         rename_type = RENAME_ONE_FILE_TO_ONE;  | 
2833  |  |         /* BUG: We should only remove ren1_src in  | 
2834  |  |          * the base stage (think of rename +  | 
2835  |  |          * add-source cases).  | 
2836  |  |          */  | 
2837  | 0  |         remove_file(opt, 1, ren1_src, 1);  | 
2838  | 0  |         update_entry(ren1->dst_entry,  | 
2839  | 0  |                ren1->pair->one,  | 
2840  | 0  |                ren1->pair->two,  | 
2841  | 0  |                ren2->pair->two);  | 
2842  | 0  |       }  | 
2843  | 0  |       setup_rename_conflict_info(rename_type, opt, ren1, ren2);  | 
2844  | 0  |     } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) { | 
2845  |  |       /* Two different files renamed to the same thing */  | 
2846  | 0  |       char *ren2_dst;  | 
2847  | 0  |       ren2 = lookup->util;  | 
2848  | 0  |       ren2_dst = ren2->pair->two->path;  | 
2849  | 0  |       if (strcmp(ren1_dst, ren2_dst) != 0)  | 
2850  | 0  |         BUG("ren1_dst != ren2_dst"); | 
2851  |  |  | 
2852  | 0  |       clean_merge = 0;  | 
2853  | 0  |       ren2->processed = 1;  | 
2854  |  |       /*  | 
2855  |  |        * BUG: We should only mark src_entry as processed  | 
2856  |  |        * if we are not dealing with a rename + add-source  | 
2857  |  |        * case.  | 
2858  |  |        */  | 
2859  | 0  |       ren2->src_entry->processed = 1;  | 
2860  |  | 
  | 
2861  | 0  |       setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,  | 
2862  | 0  |                opt, ren1, ren2);  | 
2863  | 0  |     } else { | 
2864  |  |       /* Renamed in 1, maybe changed in 2 */  | 
2865  |  |       /* we only use sha1 and mode of these */  | 
2866  | 0  |       struct diff_filespec src_other, dst_other;  | 
2867  | 0  |       int try_merge;  | 
2868  |  |  | 
2869  |  |       /*  | 
2870  |  |        * unpack_trees loads entries from common-commit  | 
2871  |  |        * into stage 1, from head-commit into stage 2, and  | 
2872  |  |        * from merge-commit into stage 3.  We keep track  | 
2873  |  |        * of which side corresponds to the rename.  | 
2874  |  |        */  | 
2875  | 0  |       int renamed_stage = a_renames == renames1 ? 2 : 3;  | 
2876  | 0  |       int other_stage =   a_renames == renames1 ? 3 : 2;  | 
2877  |  |  | 
2878  |  |       /*  | 
2879  |  |        * Directory renames have a funny corner case...  | 
2880  |  |        */  | 
2881  | 0  |       int renamed_to_self = !strcmp(ren1_src, ren1_dst);  | 
2882  |  |  | 
2883  |  |       /* BUG: We should only remove ren1_src in the base  | 
2884  |  |        * stage and in other_stage (think of rename +  | 
2885  |  |        * add-source case).  | 
2886  |  |        */  | 
2887  | 0  |       if (!renamed_to_self)  | 
2888  | 0  |         remove_file(opt, 1, ren1_src,  | 
2889  | 0  |               renamed_stage == 2 ||  | 
2890  | 0  |               !was_tracked(opt, ren1_src));  | 
2891  |  | 
  | 
2892  | 0  |       oidcpy(&src_other.oid,  | 
2893  | 0  |              &ren1->src_entry->stages[other_stage].oid);  | 
2894  | 0  |       src_other.mode = ren1->src_entry->stages[other_stage].mode;  | 
2895  | 0  |       oidcpy(&dst_other.oid,  | 
2896  | 0  |              &ren1->dst_entry->stages[other_stage].oid);  | 
2897  | 0  |       dst_other.mode = ren1->dst_entry->stages[other_stage].mode;  | 
2898  | 0  |       try_merge = 0;  | 
2899  |  | 
  | 
2900  | 0  |       if (oideq(&src_other.oid, null_oid()) &&  | 
2901  | 0  |           ren1->dir_rename_original_type == 'A') { | 
2902  | 0  |         setup_rename_conflict_info(RENAME_VIA_DIR,  | 
2903  | 0  |                  opt, ren1, NULL);  | 
2904  | 0  |       } else if (renamed_to_self) { | 
2905  | 0  |         setup_rename_conflict_info(RENAME_NORMAL,  | 
2906  | 0  |                  opt, ren1, NULL);  | 
2907  | 0  |       } else if (oideq(&src_other.oid, null_oid())) { | 
2908  | 0  |         setup_rename_conflict_info(RENAME_DELETE,  | 
2909  | 0  |                  opt, ren1, NULL);  | 
2910  | 0  |       } else if ((dst_other.mode == ren1->pair->two->mode) &&  | 
2911  | 0  |            oideq(&dst_other.oid, &ren1->pair->two->oid)) { | 
2912  |  |         /*  | 
2913  |  |          * Added file on the other side identical to  | 
2914  |  |          * the file being renamed: clean merge.  | 
2915  |  |          * Also, there is no need to overwrite the  | 
2916  |  |          * file already in the working copy, so call  | 
2917  |  |          * update_file_flags() instead of  | 
2918  |  |          * update_file().  | 
2919  |  |          */  | 
2920  | 0  |         if (update_file_flags(opt,  | 
2921  | 0  |                   ren1->pair->two,  | 
2922  | 0  |                   ren1_dst,  | 
2923  | 0  |                   1, /* update_cache */  | 
2924  | 0  |                   0  /* update_wd    */))  | 
2925  | 0  |           clean_merge = -1;  | 
2926  | 0  |       } else if (!oideq(&dst_other.oid, null_oid())) { | 
2927  |  |         /*  | 
2928  |  |          * Probably not a clean merge, but it's  | 
2929  |  |          * premature to set clean_merge to 0 here,  | 
2930  |  |          * because if the rename merges cleanly and  | 
2931  |  |          * the merge exactly matches the newly added  | 
2932  |  |          * file, then the merge will be clean.  | 
2933  |  |          */  | 
2934  | 0  |         setup_rename_conflict_info(RENAME_ADD,  | 
2935  | 0  |                  opt, ren1, NULL);  | 
2936  | 0  |       } else  | 
2937  | 0  |         try_merge = 1;  | 
2938  |  | 
  | 
2939  | 0  |       if (clean_merge < 0)  | 
2940  | 0  |         goto cleanup_and_return;  | 
2941  | 0  |       if (try_merge) { | 
2942  | 0  |         struct diff_filespec *o, *a, *b;  | 
2943  | 0  |         src_other.path = (char *)ren1_src;  | 
2944  |  | 
  | 
2945  | 0  |         o = ren1->pair->one;  | 
2946  | 0  |         if (a_renames == renames1) { | 
2947  | 0  |           a = ren1->pair->two;  | 
2948  | 0  |           b = &src_other;  | 
2949  | 0  |         } else { | 
2950  | 0  |           b = ren1->pair->two;  | 
2951  | 0  |           a = &src_other;  | 
2952  | 0  |         }  | 
2953  | 0  |         update_entry(ren1->dst_entry, o, a, b);  | 
2954  | 0  |         setup_rename_conflict_info(RENAME_NORMAL,  | 
2955  | 0  |                  opt, ren1, NULL);  | 
2956  | 0  |       }  | 
2957  | 0  |     }  | 
2958  | 0  |   }  | 
2959  | 0  | cleanup_and_return:  | 
2960  | 0  |   string_list_clear(&a_by_dst, 0);  | 
2961  | 0  |   string_list_clear(&b_by_dst, 0);  | 
2962  |  | 
  | 
2963  | 0  |   return clean_merge;  | 
2964  | 0  | }  | 
2965  |  |  | 
2966  |  | struct rename_info { | 
2967  |  |   struct string_list *head_renames;  | 
2968  |  |   struct string_list *merge_renames;  | 
2969  |  | };  | 
2970  |  |  | 
2971  |  | static void initial_cleanup_rename(struct diff_queue_struct *pairs,  | 
2972  |  |            struct hashmap *dir_renames)  | 
2973  | 0  | { | 
2974  | 0  |   struct hashmap_iter iter;  | 
2975  | 0  |   struct dir_rename_entry *e;  | 
2976  |  | 
  | 
2977  | 0  |   hashmap_for_each_entry(dir_renames, &iter, e,  | 
2978  | 0  |         ent /* member name */) { | 
2979  | 0  |     free(e->dir);  | 
2980  | 0  |     strbuf_release(&e->new_dir);  | 
2981  |  |     /* possible_new_dirs already cleared in get_directory_renames */  | 
2982  | 0  |   }  | 
2983  | 0  |   hashmap_clear_and_free(dir_renames, struct dir_rename_entry, ent);  | 
2984  | 0  |   free(dir_renames);  | 
2985  |  | 
  | 
2986  | 0  |   free(pairs->queue);  | 
2987  | 0  |   free(pairs);  | 
2988  | 0  | }  | 
2989  |  |  | 
2990  |  | static int detect_and_process_renames(struct merge_options *opt,  | 
2991  |  |               struct tree *common,  | 
2992  |  |               struct tree *head,  | 
2993  |  |               struct tree *merge,  | 
2994  |  |               struct string_list *entries,  | 
2995  |  |               struct rename_info *ri)  | 
2996  | 0  | { | 
2997  | 0  |   struct diff_queue_struct *head_pairs, *merge_pairs;  | 
2998  | 0  |   struct hashmap *dir_re_head, *dir_re_merge;  | 
2999  | 0  |   int clean = 1;  | 
3000  |  | 
  | 
3001  | 0  |   ri->head_renames = NULL;  | 
3002  | 0  |   ri->merge_renames = NULL;  | 
3003  |  | 
  | 
3004  | 0  |   if (!merge_detect_rename(opt))  | 
3005  | 0  |     return 1;  | 
3006  |  |  | 
3007  | 0  |   head_pairs = get_diffpairs(opt, common, head);  | 
3008  | 0  |   merge_pairs = get_diffpairs(opt, common, merge);  | 
3009  |  | 
  | 
3010  | 0  |   if ((opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE) ||  | 
3011  | 0  |       (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&  | 
3012  | 0  |        !opt->priv->call_depth)) { | 
3013  | 0  |     dir_re_head = get_directory_renames(head_pairs);  | 
3014  | 0  |     dir_re_merge = get_directory_renames(merge_pairs);  | 
3015  |  | 
  | 
3016  | 0  |     handle_directory_level_conflicts(opt,  | 
3017  | 0  |              dir_re_head, head,  | 
3018  | 0  |              dir_re_merge, merge);  | 
3019  | 0  |   } else { | 
3020  | 0  |     dir_re_head  = xmalloc(sizeof(*dir_re_head));  | 
3021  | 0  |     dir_re_merge = xmalloc(sizeof(*dir_re_merge));  | 
3022  | 0  |     dir_rename_init(dir_re_head);  | 
3023  | 0  |     dir_rename_init(dir_re_merge);  | 
3024  | 0  |   }  | 
3025  |  | 
  | 
3026  | 0  |   ri->head_renames  = get_renames(opt, opt->branch1, head_pairs,  | 
3027  | 0  |           dir_re_merge, dir_re_head, head,  | 
3028  | 0  |           common, head, merge, entries,  | 
3029  | 0  |           &clean);  | 
3030  | 0  |   if (clean < 0)  | 
3031  | 0  |     goto cleanup;  | 
3032  | 0  |   ri->merge_renames = get_renames(opt, opt->branch2, merge_pairs,  | 
3033  | 0  |           dir_re_head, dir_re_merge, merge,  | 
3034  | 0  |           common, head, merge, entries,  | 
3035  | 0  |           &clean);  | 
3036  | 0  |   if (clean < 0)  | 
3037  | 0  |     goto cleanup;  | 
3038  | 0  |   clean &= process_renames(opt, ri->head_renames, ri->merge_renames);  | 
3039  |  | 
  | 
3040  | 0  | cleanup:  | 
3041  |  |   /*  | 
3042  |  |    * Some cleanup is deferred until cleanup_renames() because the  | 
3043  |  |    * data structures are still needed and referenced in  | 
3044  |  |    * process_entry().  But there are a few things we can free now.  | 
3045  |  |    */  | 
3046  | 0  |   initial_cleanup_rename(head_pairs, dir_re_head);  | 
3047  | 0  |   initial_cleanup_rename(merge_pairs, dir_re_merge);  | 
3048  |  | 
  | 
3049  | 0  |   return clean;  | 
3050  | 0  | }  | 
3051  |  |  | 
3052  |  | static void final_cleanup_rename(struct string_list *rename)  | 
3053  | 0  | { | 
3054  | 0  |   const struct rename *re;  | 
3055  | 0  |   int i;  | 
3056  |  | 
  | 
3057  | 0  |   if (!rename)  | 
3058  | 0  |     return;  | 
3059  |  |  | 
3060  | 0  |   for (i = 0; i < rename->nr; i++) { | 
3061  | 0  |     re = rename->items[i].util;  | 
3062  | 0  |     diff_free_filepair(re->pair);  | 
3063  | 0  |     if (re->src_entry->rename_conflict_info_owned)  | 
3064  | 0  |       FREE_AND_NULL(re->src_entry->rename_conflict_info);  | 
3065  | 0  |     if (re->dst_entry->rename_conflict_info_owned)  | 
3066  | 0  |       FREE_AND_NULL(re->dst_entry->rename_conflict_info);  | 
3067  | 0  |   }  | 
3068  | 0  |   string_list_clear(rename, 1);  | 
3069  | 0  |   free(rename);  | 
3070  | 0  | }  | 
3071  |  |  | 
3072  |  | static void final_cleanup_renames(struct rename_info *re_info)  | 
3073  | 0  | { | 
3074  | 0  |   final_cleanup_rename(re_info->head_renames);  | 
3075  | 0  |   final_cleanup_rename(re_info->merge_renames);  | 
3076  | 0  | }  | 
3077  |  |  | 
3078  |  | static int read_oid_strbuf(struct merge_options *opt,  | 
3079  |  |          const struct object_id *oid,  | 
3080  |  |          struct strbuf *dst)  | 
3081  | 0  | { | 
3082  | 0  |   void *buf;  | 
3083  | 0  |   enum object_type type;  | 
3084  | 0  |   unsigned long size;  | 
3085  | 0  |   buf = repo_read_object_file(the_repository, oid, &type, &size);  | 
3086  | 0  |   if (!buf)  | 
3087  | 0  |     return err(opt, _("cannot read object %s"), oid_to_hex(oid)); | 
3088  | 0  |   if (type != OBJ_BLOB) { | 
3089  | 0  |     free(buf);  | 
3090  | 0  |     return err(opt, _("object %s is not a blob"), oid_to_hex(oid)); | 
3091  | 0  |   }  | 
3092  | 0  |   strbuf_attach(dst, buf, size, size + 1);  | 
3093  | 0  |   return 0;  | 
3094  | 0  | }  | 
3095  |  |  | 
3096  |  | static int blob_unchanged(struct merge_options *opt,  | 
3097  |  |         const struct diff_filespec *o,  | 
3098  |  |         const struct diff_filespec *a,  | 
3099  |  |         int renormalize, const char *path)  | 
3100  | 0  | { | 
3101  | 0  |   struct strbuf obuf = STRBUF_INIT;  | 
3102  | 0  |   struct strbuf abuf = STRBUF_INIT;  | 
3103  | 0  |   int ret = 0; /* assume changed for safety */  | 
3104  | 0  |   struct index_state *idx = opt->repo->index;  | 
3105  |  | 
  | 
3106  | 0  |   if (a->mode != o->mode)  | 
3107  | 0  |     return 0;  | 
3108  | 0  |   if (oideq(&o->oid, &a->oid))  | 
3109  | 0  |     return 1;  | 
3110  | 0  |   if (!renormalize)  | 
3111  | 0  |     return 0;  | 
3112  |  |  | 
3113  | 0  |   if (read_oid_strbuf(opt, &o->oid, &obuf) ||  | 
3114  | 0  |       read_oid_strbuf(opt, &a->oid, &abuf))  | 
3115  | 0  |     goto error_return;  | 
3116  |  |   /*  | 
3117  |  |    * Note: binary | is used so that both renormalizations are  | 
3118  |  |    * performed.  Comparison can be skipped if both files are  | 
3119  |  |    * unchanged since their sha1s have already been compared.  | 
3120  |  |    */  | 
3121  | 0  |   if (renormalize_buffer(idx, path, obuf.buf, obuf.len, &obuf) |  | 
3122  | 0  |       renormalize_buffer(idx, path, abuf.buf, abuf.len, &abuf))  | 
3123  | 0  |     ret = (obuf.len == abuf.len && !memcmp(obuf.buf, abuf.buf, obuf.len));  | 
3124  |  | 
  | 
3125  | 0  | error_return:  | 
3126  | 0  |   strbuf_release(&obuf);  | 
3127  | 0  |   strbuf_release(&abuf);  | 
3128  | 0  |   return ret;  | 
3129  | 0  | }  | 
3130  |  |  | 
3131  |  | static int handle_modify_delete(struct merge_options *opt,  | 
3132  |  |         const char *path,  | 
3133  |  |         const struct diff_filespec *o,  | 
3134  |  |         const struct diff_filespec *a,  | 
3135  |  |         const struct diff_filespec *b)  | 
3136  | 0  | { | 
3137  | 0  |   const char *modify_branch, *delete_branch;  | 
3138  | 0  |   const struct diff_filespec *changed;  | 
3139  |  | 
  | 
3140  | 0  |   if (is_valid(a)) { | 
3141  | 0  |     modify_branch = opt->branch1;  | 
3142  | 0  |     delete_branch = opt->branch2;  | 
3143  | 0  |     changed = a;  | 
3144  | 0  |   } else { | 
3145  | 0  |     modify_branch = opt->branch2;  | 
3146  | 0  |     delete_branch = opt->branch1;  | 
3147  | 0  |     changed = b;  | 
3148  | 0  |   }  | 
3149  |  | 
  | 
3150  | 0  |   return handle_change_delete(opt,  | 
3151  | 0  |             path, NULL,  | 
3152  | 0  |             o, changed,  | 
3153  | 0  |             modify_branch, delete_branch,  | 
3154  | 0  |             _("modify"), _("modified")); | 
3155  | 0  | }  | 
3156  |  |  | 
3157  |  | static int handle_content_merge(struct merge_file_info *mfi,  | 
3158  |  |         struct merge_options *opt,  | 
3159  |  |         const char *path,  | 
3160  |  |         int is_dirty,  | 
3161  |  |         const struct diff_filespec *o,  | 
3162  |  |         const struct diff_filespec *a,  | 
3163  |  |         const struct diff_filespec *b,  | 
3164  |  |         struct rename_conflict_info *ci)  | 
3165  | 0  | { | 
3166  | 0  |   const char *reason = _("content"); | 
3167  | 0  |   unsigned df_conflict_remains = 0;  | 
3168  |  | 
  | 
3169  | 0  |   if (!is_valid(o))  | 
3170  | 0  |     reason = _("add/add"); | 
3171  |  | 
  | 
3172  | 0  |   assert(o->path && a->path && b->path);  | 
3173  | 0  |   if (ci && dir_in_way(opt->repo->index, path, !opt->priv->call_depth,  | 
3174  | 0  |            S_ISGITLINK(ci->ren1->pair->two->mode)))  | 
3175  | 0  |     df_conflict_remains = 1;  | 
3176  |  | 
  | 
3177  | 0  |   if (merge_mode_and_contents(opt, o, a, b, path,  | 
3178  | 0  |             opt->branch1, opt->branch2,  | 
3179  | 0  |             opt->priv->call_depth * 2, mfi))  | 
3180  | 0  |     return -1;  | 
3181  |  |  | 
3182  |  |   /*  | 
3183  |  |    * We can skip updating the working tree file iff:  | 
3184  |  |    *   a) The merge is clean  | 
3185  |  |    *   b) The merge matches what was in HEAD (content, mode, pathname)  | 
3186  |  |    *   c) The target path is usable (i.e. not involved in D/F conflict)  | 
3187  |  |    */  | 
3188  | 0  |   if (mfi->clean && was_tracked_and_matches(opt, path, &mfi->blob) &&  | 
3189  | 0  |       !df_conflict_remains) { | 
3190  | 0  |     int pos;  | 
3191  | 0  |     struct cache_entry *ce;  | 
3192  |  | 
  | 
3193  | 0  |     output(opt, 3, _("Skipped %s (merged same as existing)"), path); | 
3194  | 0  |     if (add_cacheinfo(opt, &mfi->blob, path,  | 
3195  | 0  |           0, (!opt->priv->call_depth && !is_dirty), 0))  | 
3196  | 0  |       return -1;  | 
3197  |  |     /*  | 
3198  |  |      * However, add_cacheinfo() will delete the old cache entry  | 
3199  |  |      * and add a new one.  We need to copy over any skip_worktree  | 
3200  |  |      * flag to avoid making the file appear as if it were  | 
3201  |  |      * deleted by the user.  | 
3202  |  |      */  | 
3203  | 0  |     pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));  | 
3204  | 0  |     ce = opt->priv->orig_index.cache[pos];  | 
3205  | 0  |     if (ce_skip_worktree(ce)) { | 
3206  | 0  |       pos = index_name_pos(opt->repo->index, path, strlen(path));  | 
3207  | 0  |       ce = opt->repo->index->cache[pos];  | 
3208  | 0  |       ce->ce_flags |= CE_SKIP_WORKTREE;  | 
3209  | 0  |     }  | 
3210  | 0  |     return mfi->clean;  | 
3211  | 0  |   }  | 
3212  |  |  | 
3213  | 0  |   if (!mfi->clean) { | 
3214  | 0  |     if (S_ISGITLINK(mfi->blob.mode))  | 
3215  | 0  |       reason = _("submodule"); | 
3216  | 0  |     output(opt, 1, _("CONFLICT (%s): Merge conflict in %s"), | 
3217  | 0  |         reason, path);  | 
3218  | 0  |     if (ci && !df_conflict_remains)  | 
3219  | 0  |       if (update_stages(opt, path, o, a, b))  | 
3220  | 0  |         return -1;  | 
3221  | 0  |   }  | 
3222  |  |  | 
3223  | 0  |   if (df_conflict_remains || is_dirty) { | 
3224  | 0  |     char *new_path;  | 
3225  | 0  |     if (opt->priv->call_depth) { | 
3226  | 0  |       remove_file_from_index(opt->repo->index, path);  | 
3227  | 0  |     } else { | 
3228  | 0  |       if (!mfi->clean) { | 
3229  | 0  |         if (update_stages(opt, path, o, a, b))  | 
3230  | 0  |           return -1;  | 
3231  | 0  |       } else { | 
3232  | 0  |         int file_from_stage2 = was_tracked(opt, path);  | 
3233  |  | 
  | 
3234  | 0  |         if (update_stages(opt, path, NULL,  | 
3235  | 0  |               file_from_stage2 ? &mfi->blob : NULL,  | 
3236  | 0  |               file_from_stage2 ? NULL : &mfi->blob))  | 
3237  | 0  |           return -1;  | 
3238  | 0  |       }  | 
3239  |  | 
  | 
3240  | 0  |     }  | 
3241  | 0  |     new_path = unique_path(opt, path, ci->ren1->branch);  | 
3242  | 0  |     if (is_dirty) { | 
3243  | 0  |       output(opt, 1, _("Refusing to lose dirty file at %s"), | 
3244  | 0  |              path);  | 
3245  | 0  |     }  | 
3246  | 0  |     output(opt, 1, _("Adding as %s instead"), new_path); | 
3247  | 0  |     if (update_file(opt, 0, &mfi->blob, new_path)) { | 
3248  | 0  |       free(new_path);  | 
3249  | 0  |       return -1;  | 
3250  | 0  |     }  | 
3251  | 0  |     free(new_path);  | 
3252  | 0  |     mfi->clean = 0;  | 
3253  | 0  |   } else if (update_file(opt, mfi->clean, &mfi->blob, path))  | 
3254  | 0  |     return -1;  | 
3255  | 0  |   return !is_dirty && mfi->clean;  | 
3256  | 0  | }  | 
3257  |  |  | 
3258  |  | static int handle_rename_normal(struct merge_options *opt,  | 
3259  |  |         const char *path,  | 
3260  |  |         const struct diff_filespec *o,  | 
3261  |  |         const struct diff_filespec *a,  | 
3262  |  |         const struct diff_filespec *b,  | 
3263  |  |         struct rename_conflict_info *ci)  | 
3264  | 0  | { | 
3265  | 0  |   struct rename *ren = ci->ren1;  | 
3266  | 0  |   struct merge_file_info mfi;  | 
3267  | 0  |   int clean;  | 
3268  |  |  | 
3269  |  |   /* Merge the content and write it out */  | 
3270  | 0  |   clean = handle_content_merge(&mfi, opt, path, was_dirty(opt, path),  | 
3271  | 0  |              o, a, b, ci);  | 
3272  |  | 
  | 
3273  | 0  |   if (clean &&  | 
3274  | 0  |       opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&  | 
3275  | 0  |       ren->dir_rename_original_dest) { | 
3276  | 0  |     if (update_stages(opt, path,  | 
3277  | 0  |           &mfi.blob, &mfi.blob, &mfi.blob))  | 
3278  | 0  |       return -1;  | 
3279  | 0  |     clean = 0; /* not clean, but conflicted */  | 
3280  | 0  |   }  | 
3281  | 0  |   return clean;  | 
3282  | 0  | }  | 
3283  |  |  | 
3284  |  | static void dir_rename_warning(const char *msg,  | 
3285  |  |              int is_add,  | 
3286  |  |              int clean,  | 
3287  |  |              struct merge_options *opt,  | 
3288  |  |              struct rename *ren)  | 
3289  | 0  | { | 
3290  | 0  |   const char *other_branch;  | 
3291  | 0  |   other_branch = (ren->branch == opt->branch1 ?  | 
3292  | 0  |       opt->branch2 : opt->branch1);  | 
3293  | 0  |   if (is_add) { | 
3294  | 0  |     output(opt, clean ? 2 : 1, msg,  | 
3295  | 0  |            ren->pair->one->path, ren->branch,  | 
3296  | 0  |            other_branch, ren->pair->two->path);  | 
3297  | 0  |     return;  | 
3298  | 0  |   }  | 
3299  | 0  |   output(opt, clean ? 2 : 1, msg,  | 
3300  | 0  |          ren->pair->one->path, ren->dir_rename_original_dest, ren->branch,  | 
3301  | 0  |          other_branch, ren->pair->two->path);  | 
3302  | 0  | }  | 
3303  |  | static int warn_about_dir_renamed_entries(struct merge_options *opt,  | 
3304  |  |             struct rename *ren)  | 
3305  | 0  | { | 
3306  | 0  |   const char *msg;  | 
3307  | 0  |   int clean = 1, is_add;  | 
3308  |  | 
  | 
3309  | 0  |   if (!ren)  | 
3310  | 0  |     return clean;  | 
3311  |  |  | 
3312  |  |   /* Return early if ren was not affected/created by a directory rename */  | 
3313  | 0  |   if (!ren->dir_rename_original_dest)  | 
3314  | 0  |     return clean;  | 
3315  |  |  | 
3316  |  |   /* Sanity checks */  | 
3317  | 0  |   assert(opt->detect_directory_renames > MERGE_DIRECTORY_RENAMES_NONE);  | 
3318  | 0  |   assert(ren->dir_rename_original_type == 'A' ||  | 
3319  | 0  |          ren->dir_rename_original_type == 'R');  | 
3320  |  |  | 
3321  |  |   /* Check whether to treat directory renames as a conflict */  | 
3322  | 0  |   clean = (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE);  | 
3323  |  | 
  | 
3324  | 0  |   is_add = (ren->dir_rename_original_type == 'A');  | 
3325  | 0  |   if (ren->dir_rename_original_type == 'A' && clean) { | 
3326  | 0  |     msg = _("Path updated: %s added in %s inside a " | 
3327  | 0  |       "directory that was renamed in %s; moving it to %s.");  | 
3328  | 0  |   } else if (ren->dir_rename_original_type == 'A' && !clean) { | 
3329  | 0  |     msg = _("CONFLICT (file location): %s added in %s " | 
3330  | 0  |       "inside a directory that was renamed in %s, "  | 
3331  | 0  |       "suggesting it should perhaps be moved to %s.");  | 
3332  | 0  |   } else if (ren->dir_rename_original_type == 'R' && clean) { | 
3333  | 0  |     msg = _("Path updated: %s renamed to %s in %s, inside a " | 
3334  | 0  |       "directory that was renamed in %s; moving it to %s.");  | 
3335  | 0  |   } else if (ren->dir_rename_original_type == 'R' && !clean) { | 
3336  | 0  |     msg = _("CONFLICT (file location): %s renamed to %s in %s, " | 
3337  | 0  |       "inside a directory that was renamed in %s, "  | 
3338  | 0  |       "suggesting it should perhaps be moved to %s.");  | 
3339  | 0  |   } else { | 
3340  | 0  |     BUG("Impossible dir_rename_original_type/clean combination"); | 
3341  | 0  |   }  | 
3342  | 0  |   dir_rename_warning(msg, is_add, clean, opt, ren);  | 
3343  |  | 
  | 
3344  | 0  |   return clean;  | 
3345  | 0  | }  | 
3346  |  |  | 
3347  |  | /* Per entry merge function */  | 
3348  |  | static int process_entry(struct merge_options *opt,  | 
3349  |  |        const char *path, struct stage_data *entry)  | 
3350  | 0  | { | 
3351  | 0  |   int clean_merge = 1;  | 
3352  | 0  |   int normalize = opt->renormalize;  | 
3353  |  | 
  | 
3354  | 0  |   struct diff_filespec *o = &entry->stages[1];  | 
3355  | 0  |   struct diff_filespec *a = &entry->stages[2];  | 
3356  | 0  |   struct diff_filespec *b = &entry->stages[3];  | 
3357  | 0  |   int o_valid = is_valid(o);  | 
3358  | 0  |   int a_valid = is_valid(a);  | 
3359  | 0  |   int b_valid = is_valid(b);  | 
3360  | 0  |   o->path = a->path = b->path = (char*)path;  | 
3361  |  | 
  | 
3362  | 0  |   entry->processed = 1;  | 
3363  | 0  |   if (entry->rename_conflict_info) { | 
3364  | 0  |     struct rename_conflict_info *ci = entry->rename_conflict_info;  | 
3365  | 0  |     struct diff_filespec *temp;  | 
3366  | 0  |     int path_clean;  | 
3367  |  | 
  | 
3368  | 0  |     path_clean = warn_about_dir_renamed_entries(opt, ci->ren1);  | 
3369  | 0  |     path_clean &= warn_about_dir_renamed_entries(opt, ci->ren2);  | 
3370  |  |  | 
3371  |  |     /*  | 
3372  |  |      * For cases with a single rename, {o,a,b}->path have all been | 
3373  |  |      * set to the rename target path; we need to set two of these  | 
3374  |  |      * back to the rename source.  | 
3375  |  |      * For rename/rename conflicts, we'll manually fix paths below.  | 
3376  |  |      */  | 
3377  | 0  |     temp = (opt->branch1 == ci->ren1->branch) ? b : a;  | 
3378  | 0  |     o->path = temp->path = ci->ren1->pair->one->path;  | 
3379  | 0  |     if (ci->ren2) { | 
3380  | 0  |       assert(opt->branch1 == ci->ren1->branch);  | 
3381  | 0  |     }  | 
3382  |  |  | 
3383  | 0  |     switch (ci->rename_type) { | 
3384  | 0  |     case RENAME_NORMAL:  | 
3385  | 0  |     case RENAME_ONE_FILE_TO_ONE:  | 
3386  | 0  |       clean_merge = handle_rename_normal(opt, path, o, a, b,  | 
3387  | 0  |                  ci);  | 
3388  | 0  |       break;  | 
3389  | 0  |     case RENAME_VIA_DIR:  | 
3390  | 0  |       clean_merge = handle_rename_via_dir(opt, ci);  | 
3391  | 0  |       break;  | 
3392  | 0  |     case RENAME_ADD:  | 
3393  |  |       /*  | 
3394  |  |        * Probably unclean merge, but if the renamed file  | 
3395  |  |        * merges cleanly and the result can then be  | 
3396  |  |        * two-way merged cleanly with the added file, I  | 
3397  |  |        * guess it's a clean merge?  | 
3398  |  |        */  | 
3399  | 0  |       clean_merge = handle_rename_add(opt, ci);  | 
3400  | 0  |       break;  | 
3401  | 0  |     case RENAME_DELETE:  | 
3402  | 0  |       clean_merge = 0;  | 
3403  | 0  |       if (handle_rename_delete(opt, ci))  | 
3404  | 0  |         clean_merge = -1;  | 
3405  | 0  |       break;  | 
3406  | 0  |     case RENAME_ONE_FILE_TO_TWO:  | 
3407  |  |       /*  | 
3408  |  |        * Manually fix up paths; note:  | 
3409  |  |        * ren[12]->pair->one->path are equal.  | 
3410  |  |        */  | 
3411  | 0  |       o->path = ci->ren1->pair->one->path;  | 
3412  | 0  |       a->path = ci->ren1->pair->two->path;  | 
3413  | 0  |       b->path = ci->ren2->pair->two->path;  | 
3414  |  | 
  | 
3415  | 0  |       clean_merge = 0;  | 
3416  | 0  |       if (handle_rename_rename_1to2(opt, ci))  | 
3417  | 0  |         clean_merge = -1;  | 
3418  | 0  |       break;  | 
3419  | 0  |     case RENAME_TWO_FILES_TO_ONE:  | 
3420  |  |       /*  | 
3421  |  |        * Manually fix up paths; note,  | 
3422  |  |        * ren[12]->pair->two->path are actually equal.  | 
3423  |  |        */  | 
3424  | 0  |       o->path = NULL;  | 
3425  | 0  |       a->path = ci->ren1->pair->two->path;  | 
3426  | 0  |       b->path = ci->ren2->pair->two->path;  | 
3427  |  |  | 
3428  |  |       /*  | 
3429  |  |        * Probably unclean merge, but if the two renamed  | 
3430  |  |        * files merge cleanly and the two resulting files  | 
3431  |  |        * can then be two-way merged cleanly, I guess it's  | 
3432  |  |        * a clean merge?  | 
3433  |  |        */  | 
3434  | 0  |       clean_merge = handle_rename_rename_2to1(opt, ci);  | 
3435  | 0  |       break;  | 
3436  | 0  |     default:  | 
3437  | 0  |       entry->processed = 0;  | 
3438  | 0  |       break;  | 
3439  | 0  |     }  | 
3440  | 0  |     if (path_clean < clean_merge)  | 
3441  | 0  |       clean_merge = path_clean;  | 
3442  | 0  |   } else if (o_valid && (!a_valid || !b_valid)) { | 
3443  |  |     /* Case A: Deleted in one */  | 
3444  | 0  |     if ((!a_valid && !b_valid) ||  | 
3445  | 0  |         (!b_valid && blob_unchanged(opt, o, a, normalize, path)) ||  | 
3446  | 0  |         (!a_valid && blob_unchanged(opt, o, b, normalize, path))) { | 
3447  |  |       /* Deleted in both or deleted in one and  | 
3448  |  |        * unchanged in the other */  | 
3449  | 0  |       if (a_valid)  | 
3450  | 0  |         output(opt, 2, _("Removing %s"), path); | 
3451  |  |       /* do not touch working file if it did not exist */  | 
3452  | 0  |       remove_file(opt, 1, path, !a_valid);  | 
3453  | 0  |     } else { | 
3454  |  |       /* Modify/delete; deleted side may have put a directory in the way */  | 
3455  | 0  |       clean_merge = 0;  | 
3456  | 0  |       if (handle_modify_delete(opt, path, o, a, b))  | 
3457  | 0  |         clean_merge = -1;  | 
3458  | 0  |     }  | 
3459  | 0  |   } else if ((!o_valid && a_valid && !b_valid) ||  | 
3460  | 0  |        (!o_valid && !a_valid && b_valid)) { | 
3461  |  |     /* Case B: Added in one. */  | 
3462  |  |     /* [nothing|directory] -> ([nothing|directory], file) */  | 
3463  |  | 
  | 
3464  | 0  |     const char *add_branch;  | 
3465  | 0  |     const char *other_branch;  | 
3466  | 0  |     const char *conf;  | 
3467  | 0  |     const struct diff_filespec *contents;  | 
3468  |  | 
  | 
3469  | 0  |     if (a_valid) { | 
3470  | 0  |       add_branch = opt->branch1;  | 
3471  | 0  |       other_branch = opt->branch2;  | 
3472  | 0  |       contents = a;  | 
3473  | 0  |       conf = _("file/directory"); | 
3474  | 0  |     } else { | 
3475  | 0  |       add_branch = opt->branch2;  | 
3476  | 0  |       other_branch = opt->branch1;  | 
3477  | 0  |       contents = b;  | 
3478  | 0  |       conf = _("directory/file"); | 
3479  | 0  |     }  | 
3480  | 0  |     if (dir_in_way(opt->repo->index, path,  | 
3481  | 0  |              !opt->priv->call_depth && !S_ISGITLINK(a->mode),  | 
3482  | 0  |              0)) { | 
3483  | 0  |       char *new_path = unique_path(opt, path, add_branch);  | 
3484  | 0  |       clean_merge = 0;  | 
3485  | 0  |       output(opt, 1, _("CONFLICT (%s): There is a directory with name %s in %s. " | 
3486  | 0  |              "Adding %s as %s"),  | 
3487  | 0  |              conf, path, other_branch, path, new_path);  | 
3488  | 0  |       if (update_file(opt, 0, contents, new_path))  | 
3489  | 0  |         clean_merge = -1;  | 
3490  | 0  |       else if (opt->priv->call_depth)  | 
3491  | 0  |         remove_file_from_index(opt->repo->index, path);  | 
3492  | 0  |       free(new_path);  | 
3493  | 0  |     } else { | 
3494  | 0  |       output(opt, 2, _("Adding %s"), path); | 
3495  |  |       /* do not overwrite file if already present */  | 
3496  | 0  |       if (update_file_flags(opt, contents, path, 1, !a_valid))  | 
3497  | 0  |         clean_merge = -1;  | 
3498  | 0  |     }  | 
3499  | 0  |   } else if (a_valid && b_valid) { | 
3500  | 0  |     if (!o_valid) { | 
3501  |  |       /* Case C: Added in both (check for same permissions) */  | 
3502  | 0  |       output(opt, 1,  | 
3503  | 0  |              _("CONFLICT (add/add): Merge conflict in %s"), | 
3504  | 0  |              path);  | 
3505  | 0  |       clean_merge = handle_file_collision(opt,  | 
3506  | 0  |                   path, NULL, NULL,  | 
3507  | 0  |                   opt->branch1,  | 
3508  | 0  |                   opt->branch2,  | 
3509  | 0  |                   a, b);  | 
3510  | 0  |     } else { | 
3511  |  |       /* case D: Modified in both, but differently. */  | 
3512  | 0  |       struct merge_file_info mfi;  | 
3513  | 0  |       int is_dirty = 0; /* unpack_trees would have bailed if dirty */  | 
3514  | 0  |       clean_merge = handle_content_merge(&mfi, opt, path,  | 
3515  | 0  |                  is_dirty,  | 
3516  | 0  |                  o, a, b, NULL);  | 
3517  | 0  |     }  | 
3518  | 0  |   } else if (!o_valid && !a_valid && !b_valid) { | 
3519  |  |     /*  | 
3520  |  |      * this entry was deleted altogether. a_mode == 0 means  | 
3521  |  |      * we had that path and want to actively remove it.  | 
3522  |  |      */  | 
3523  | 0  |     remove_file(opt, 1, path, !a->mode);  | 
3524  | 0  |   } else  | 
3525  | 0  |     BUG("fatal merge failure, shouldn't happen."); | 
3526  |  |  | 
3527  | 0  |   return clean_merge;  | 
3528  | 0  | }  | 
3529  |  |  | 
3530  |  | static int merge_trees_internal(struct merge_options *opt,  | 
3531  |  |         struct tree *head,  | 
3532  |  |         struct tree *merge,  | 
3533  |  |         struct tree *merge_base,  | 
3534  |  |         struct tree **result)  | 
3535  | 0  | { | 
3536  | 0  |   struct index_state *istate = opt->repo->index;  | 
3537  | 0  |   int code, clean;  | 
3538  |  | 
  | 
3539  | 0  |   if (opt->subtree_shift) { | 
3540  | 0  |     merge = shift_tree_object(opt->repo, head, merge,  | 
3541  | 0  |             opt->subtree_shift);  | 
3542  | 0  |     merge_base = shift_tree_object(opt->repo, head, merge_base,  | 
3543  | 0  |                  opt->subtree_shift);  | 
3544  | 0  |   }  | 
3545  |  | 
  | 
3546  | 0  |   if (oideq(&merge_base->object.oid, &merge->object.oid)) { | 
3547  | 0  |     output(opt, 0, _("Already up to date.")); | 
3548  | 0  |     *result = head;  | 
3549  | 0  |     return 1;  | 
3550  | 0  |   }  | 
3551  |  |  | 
3552  | 0  |   code = unpack_trees_start(opt, merge_base, head, merge);  | 
3553  |  | 
  | 
3554  | 0  |   if (code != 0) { | 
3555  | 0  |     if (show(opt, 4) || opt->priv->call_depth)  | 
3556  | 0  |       err(opt, _("merging of trees %s and %s failed"), | 
3557  | 0  |           oid_to_hex(&head->object.oid),  | 
3558  | 0  |           oid_to_hex(&merge->object.oid));  | 
3559  | 0  |     unpack_trees_finish(opt);  | 
3560  | 0  |     return -1;  | 
3561  | 0  |   }  | 
3562  |  |  | 
3563  | 0  |   if (unmerged_index(istate)) { | 
3564  | 0  |     struct string_list *entries;  | 
3565  | 0  |     struct rename_info re_info;  | 
3566  | 0  |     int i;  | 
3567  |  |     /*  | 
3568  |  |      * Only need the hashmap while processing entries, so  | 
3569  |  |      * initialize it here and free it when we are done running  | 
3570  |  |      * through the entries. Keeping it in the merge_options as  | 
3571  |  |      * opposed to decaring a local hashmap is for convenience  | 
3572  |  |      * so that we don't have to pass it to around.  | 
3573  |  |      */  | 
3574  | 0  |     hashmap_init(&opt->priv->current_file_dir_set, path_hashmap_cmp,  | 
3575  | 0  |            NULL, 512);  | 
3576  | 0  |     get_files_dirs(opt, head);  | 
3577  | 0  |     get_files_dirs(opt, merge);  | 
3578  |  | 
  | 
3579  | 0  |     entries = get_unmerged(opt->repo->index);  | 
3580  | 0  |     clean = detect_and_process_renames(opt, merge_base, head, merge,  | 
3581  | 0  |                entries, &re_info);  | 
3582  | 0  |     record_df_conflict_files(opt, entries);  | 
3583  | 0  |     if (clean < 0)  | 
3584  | 0  |       goto cleanup;  | 
3585  | 0  |     for (i = entries->nr-1; 0 <= i; i--) { | 
3586  | 0  |       const char *path = entries->items[i].string;  | 
3587  | 0  |       struct stage_data *e = entries->items[i].util;  | 
3588  | 0  |       if (!e->processed) { | 
3589  | 0  |         int ret = process_entry(opt, path, e);  | 
3590  | 0  |         if (!ret)  | 
3591  | 0  |           clean = 0;  | 
3592  | 0  |         else if (ret < 0) { | 
3593  | 0  |           clean = ret;  | 
3594  | 0  |           goto cleanup;  | 
3595  | 0  |         }  | 
3596  | 0  |       }  | 
3597  | 0  |     }  | 
3598  | 0  |     for (i = 0; i < entries->nr; i++) { | 
3599  | 0  |       struct stage_data *e = entries->items[i].util;  | 
3600  | 0  |       if (!e->processed)  | 
3601  | 0  |         BUG("unprocessed path??? %s", | 
3602  | 0  |             entries->items[i].string);  | 
3603  | 0  |     }  | 
3604  |  |  | 
3605  | 0  |   cleanup:  | 
3606  | 0  |     final_cleanup_renames(&re_info);  | 
3607  |  | 
  | 
3608  | 0  |     string_list_clear(entries, 1);  | 
3609  | 0  |     free(entries);  | 
3610  |  | 
  | 
3611  | 0  |     hashmap_clear_and_free(&opt->priv->current_file_dir_set,  | 
3612  | 0  |           struct path_hashmap_entry, e);  | 
3613  |  | 
  | 
3614  | 0  |     if (clean < 0) { | 
3615  | 0  |       unpack_trees_finish(opt);  | 
3616  | 0  |       return clean;  | 
3617  | 0  |     }  | 
3618  | 0  |   }  | 
3619  | 0  |   else  | 
3620  | 0  |     clean = 1;  | 
3621  |  |  | 
3622  | 0  |   unpack_trees_finish(opt);  | 
3623  |  | 
  | 
3624  | 0  |   if (opt->priv->call_depth &&  | 
3625  | 0  |       !(*result = write_in_core_index_as_tree(opt->repo)))  | 
3626  | 0  |     return -1;  | 
3627  |  |  | 
3628  | 0  |   return clean;  | 
3629  | 0  | }  | 
3630  |  |  | 
3631  |  | /*  | 
3632  |  |  * Merge the commits h1 and h2, returning a flag (int) indicating the  | 
3633  |  |  * cleanness of the merge.  Also, if opt->priv->call_depth, create a  | 
3634  |  |  * virtual commit and write its location to *result.  | 
3635  |  |  */  | 
3636  |  | static int merge_recursive_internal(struct merge_options *opt,  | 
3637  |  |             struct commit *h1,  | 
3638  |  |             struct commit *h2,  | 
3639  |  |             const struct commit_list *_merge_bases,  | 
3640  |  |             struct commit **result)  | 
3641  | 0  | { | 
3642  | 0  |   struct commit_list *merge_bases = copy_commit_list(_merge_bases);  | 
3643  | 0  |   struct commit_list *iter;  | 
3644  | 0  |   struct commit *merged_merge_bases;  | 
3645  | 0  |   struct tree *result_tree;  | 
3646  | 0  |   const char *ancestor_name;  | 
3647  | 0  |   struct strbuf merge_base_abbrev = STRBUF_INIT;  | 
3648  | 0  |   int ret;  | 
3649  |  | 
  | 
3650  | 0  |   if (show(opt, 4)) { | 
3651  | 0  |     output(opt, 4, _("Merging:")); | 
3652  | 0  |     output_commit_title(opt, h1);  | 
3653  | 0  |     output_commit_title(opt, h2);  | 
3654  | 0  |   }  | 
3655  |  | 
  | 
3656  | 0  |   if (!merge_bases) { | 
3657  | 0  |     if (repo_get_merge_bases(the_repository, h1, h2,  | 
3658  | 0  |            &merge_bases) < 0) { | 
3659  | 0  |       ret = -1;  | 
3660  | 0  |       goto out;  | 
3661  | 0  |     }  | 
3662  | 0  |     merge_bases = reverse_commit_list(merge_bases);  | 
3663  | 0  |   }  | 
3664  |  |  | 
3665  | 0  |   if (show(opt, 5)) { | 
3666  | 0  |     unsigned cnt = commit_list_count(merge_bases);  | 
3667  |  | 
  | 
3668  | 0  |     output(opt, 5, Q_("found %u common ancestor:", | 
3669  | 0  |         "found %u common ancestors:", cnt), cnt);  | 
3670  | 0  |     for (iter = merge_bases; iter; iter = iter->next)  | 
3671  | 0  |       output_commit_title(opt, iter->item);  | 
3672  | 0  |   }  | 
3673  |  | 
  | 
3674  | 0  |   merged_merge_bases = pop_commit(&merge_bases);  | 
3675  | 0  |   if (!merged_merge_bases) { | 
3676  |  |     /* if there is no common ancestor, use an empty tree */  | 
3677  | 0  |     struct tree *tree;  | 
3678  |  | 
  | 
3679  | 0  |     tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);  | 
3680  | 0  |     merged_merge_bases = make_virtual_commit(opt->repo, tree,  | 
3681  | 0  |                "ancestor");  | 
3682  | 0  |     ancestor_name = "empty tree";  | 
3683  | 0  |   } else if (opt->ancestor && !opt->priv->call_depth) { | 
3684  | 0  |     ancestor_name = opt->ancestor;  | 
3685  | 0  |   } else if (merge_bases) { | 
3686  | 0  |     ancestor_name = "merged common ancestors";  | 
3687  | 0  |   } else { | 
3688  | 0  |     strbuf_add_unique_abbrev(&merge_base_abbrev,  | 
3689  | 0  |            &merged_merge_bases->object.oid,  | 
3690  | 0  |            DEFAULT_ABBREV);  | 
3691  | 0  |     ancestor_name = merge_base_abbrev.buf;  | 
3692  | 0  |   }  | 
3693  |  | 
  | 
3694  | 0  |   for (iter = merge_bases; iter; iter = iter->next) { | 
3695  | 0  |     const char *saved_b1, *saved_b2;  | 
3696  | 0  |     opt->priv->call_depth++;  | 
3697  |  |     /*  | 
3698  |  |      * When the merge fails, the result contains files  | 
3699  |  |      * with conflict markers. The cleanness flag is  | 
3700  |  |      * ignored (unless indicating an error), it was never  | 
3701  |  |      * actually used, as result of merge_trees has always  | 
3702  |  |      * overwritten it: the committed "conflicts" were  | 
3703  |  |      * already resolved.  | 
3704  |  |      */  | 
3705  | 0  |     discard_index(opt->repo->index);  | 
3706  | 0  |     saved_b1 = opt->branch1;  | 
3707  | 0  |     saved_b2 = opt->branch2;  | 
3708  | 0  |     opt->branch1 = "Temporary merge branch 1";  | 
3709  | 0  |     opt->branch2 = "Temporary merge branch 2";  | 
3710  | 0  |     if (merge_recursive_internal(opt, merged_merge_bases, iter->item,  | 
3711  | 0  |                NULL, &merged_merge_bases) < 0) { | 
3712  | 0  |       ret = -1;  | 
3713  | 0  |       goto out;  | 
3714  | 0  |     }  | 
3715  | 0  |     opt->branch1 = saved_b1;  | 
3716  | 0  |     opt->branch2 = saved_b2;  | 
3717  | 0  |     opt->priv->call_depth--;  | 
3718  |  | 
  | 
3719  | 0  |     if (!merged_merge_bases) { | 
3720  | 0  |       ret = err(opt, _("merge returned no commit")); | 
3721  | 0  |       goto out;  | 
3722  | 0  |     }  | 
3723  | 0  |   }  | 
3724  |  |  | 
3725  |  |   /*  | 
3726  |  |    * FIXME: Since merge_recursive_internal() is only ever called by  | 
3727  |  |    * places that ensure the index is loaded first  | 
3728  |  |    * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common  | 
3729  |  |    * case where the merge base was unique that means when we get here  | 
3730  |  |    * we immediately discard the index and re-read it, which is a  | 
3731  |  |    * complete waste of time.  We should only be discarding and  | 
3732  |  |    * re-reading if we were forced to recurse.  | 
3733  |  |    */  | 
3734  | 0  |   discard_index(opt->repo->index);  | 
3735  | 0  |   if (!opt->priv->call_depth)  | 
3736  | 0  |     repo_read_index(opt->repo);  | 
3737  |  | 
  | 
3738  | 0  |   opt->ancestor = ancestor_name;  | 
3739  | 0  |   ret = merge_trees_internal(opt,  | 
3740  | 0  |            repo_get_commit_tree(opt->repo, h1),  | 
3741  | 0  |            repo_get_commit_tree(opt->repo, h2),  | 
3742  | 0  |            repo_get_commit_tree(opt->repo,  | 
3743  | 0  |               merged_merge_bases),  | 
3744  | 0  |            &result_tree);  | 
3745  | 0  |   opt->ancestor = NULL;  /* avoid accidental re-use of opt->ancestor */  | 
3746  | 0  |   if (ret < 0) { | 
3747  | 0  |     flush_output(opt);  | 
3748  | 0  |     goto out;  | 
3749  | 0  |   }  | 
3750  |  |  | 
3751  | 0  |   if (opt->priv->call_depth) { | 
3752  | 0  |     *result = make_virtual_commit(opt->repo, result_tree,  | 
3753  | 0  |                 "merged tree");  | 
3754  | 0  |     commit_list_insert(h1, &(*result)->parents);  | 
3755  | 0  |     commit_list_insert(h2, &(*result)->parents->next);  | 
3756  | 0  |   }  | 
3757  |  | 
  | 
3758  | 0  | out:  | 
3759  | 0  |   strbuf_release(&merge_base_abbrev);  | 
3760  | 0  |   free_commit_list(merge_bases);  | 
3761  | 0  |   return ret;  | 
3762  | 0  | }  | 
3763  |  |  | 
3764  |  | static int merge_start(struct merge_options *opt, struct tree *head)  | 
3765  | 0  | { | 
3766  | 0  |   struct strbuf sb = STRBUF_INIT;  | 
3767  |  |  | 
3768  |  |   /* Sanity checks on opt */  | 
3769  | 0  |   assert(opt->repo);  | 
3770  |  |  | 
3771  | 0  |   assert(opt->branch1 && opt->branch2);  | 
3772  |  |  | 
3773  | 0  |   assert(opt->detect_renames >= -1 &&  | 
3774  | 0  |          opt->detect_renames <= DIFF_DETECT_COPY);  | 
3775  | 0  |   assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&  | 
3776  | 0  |          opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);  | 
3777  | 0  |   assert(opt->rename_limit >= -1);  | 
3778  | 0  |   assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);  | 
3779  | 0  |   assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);  | 
3780  |  |  | 
3781  | 0  |   assert(opt->xdl_opts >= 0);  | 
3782  | 0  |   assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&  | 
3783  | 0  |          opt->recursive_variant <= MERGE_VARIANT_THEIRS);  | 
3784  |  |  | 
3785  | 0  |   assert(opt->verbosity >= 0 && opt->verbosity <= 5);  | 
3786  | 0  |   assert(opt->buffer_output <= 2);  | 
3787  | 0  |   assert(opt->obuf.len == 0);  | 
3788  |  |  | 
3789  | 0  |   assert(opt->priv == NULL);  | 
3790  |  |  | 
3791  |  |   /* Not supported; option specific to merge-ort */  | 
3792  | 0  |   assert(!opt->record_conflict_msgs_as_headers);  | 
3793  | 0  |   assert(!opt->msg_header_prefix);  | 
3794  |  |  | 
3795  |  |   /* Sanity check on repo state; index must match head */  | 
3796  | 0  |   if (repo_index_has_changes(opt->repo, head, &sb)) { | 
3797  | 0  |     err(opt, _("Your local changes to the following files would be overwritten by merge:\n  %s"), | 
3798  | 0  |         sb.buf);  | 
3799  | 0  |     strbuf_release(&sb);  | 
3800  | 0  |     return -1;  | 
3801  | 0  |   }  | 
3802  |  |  | 
3803  | 0  |   CALLOC_ARRAY(opt->priv, 1);  | 
3804  | 0  |   string_list_init_dup(&opt->priv->df_conflict_file_set);  | 
3805  | 0  |   return 0;  | 
3806  | 0  | }  | 
3807  |  |  | 
3808  |  | static void merge_finalize(struct merge_options *opt)  | 
3809  | 0  | { | 
3810  | 0  |   flush_output(opt);  | 
3811  | 0  |   if (!opt->priv->call_depth && opt->buffer_output < 2)  | 
3812  | 0  |     strbuf_release(&opt->obuf);  | 
3813  | 0  |   if (show(opt, 2))  | 
3814  | 0  |     diff_warn_rename_limit("merge.renamelimit", | 
3815  | 0  |                opt->priv->needed_rename_limit, 0);  | 
3816  | 0  |   hashmap_clear_and_free(&opt->priv->current_file_dir_set,  | 
3817  | 0  |              struct path_hashmap_entry, e);  | 
3818  | 0  |   string_list_clear(&opt->priv->df_conflict_file_set, 0);  | 
3819  | 0  |   FREE_AND_NULL(opt->priv);  | 
3820  | 0  | }  | 
3821  |  |  | 
3822  |  | int merge_trees(struct merge_options *opt,  | 
3823  |  |     struct tree *head,  | 
3824  |  |     struct tree *merge,  | 
3825  |  |     struct tree *merge_base)  | 
3826  | 0  | { | 
3827  | 0  |   int clean;  | 
3828  | 0  |   struct tree *ignored;  | 
3829  |  | 
  | 
3830  | 0  |   assert(opt->ancestor != NULL);  | 
3831  |  |  | 
3832  | 0  |   if (merge_start(opt, head))  | 
3833  | 0  |     return -1;  | 
3834  | 0  |   clean = merge_trees_internal(opt, head, merge, merge_base, &ignored);  | 
3835  | 0  |   merge_finalize(opt);  | 
3836  |  | 
  | 
3837  | 0  |   return clean;  | 
3838  | 0  | }  | 
3839  |  |  | 
3840  |  | int merge_recursive(struct merge_options *opt,  | 
3841  |  |         struct commit *h1,  | 
3842  |  |         struct commit *h2,  | 
3843  |  |         const struct commit_list *merge_bases,  | 
3844  |  |         struct commit **result)  | 
3845  | 0  | { | 
3846  | 0  |   int clean;  | 
3847  |  | 
  | 
3848  | 0  |   assert(opt->ancestor == NULL ||  | 
3849  | 0  |          !strcmp(opt->ancestor, "constructed merge base"));  | 
3850  |  |  | 
3851  | 0  |   prepare_repo_settings(opt->repo);  | 
3852  | 0  |   opt->repo->settings.command_requires_full_index = 1;  | 
3853  |  | 
  | 
3854  | 0  |   if (merge_start(opt, repo_get_commit_tree(opt->repo, h1)))  | 
3855  | 0  |     return -1;  | 
3856  | 0  |   clean = merge_recursive_internal(opt, h1, h2, merge_bases, result);  | 
3857  | 0  |   merge_finalize(opt);  | 
3858  |  | 
  | 
3859  | 0  |   return clean;  | 
3860  | 0  | }  | 
3861  |  |  | 
3862  |  | static struct commit *get_ref(struct repository *repo,  | 
3863  |  |             const struct object_id *oid,  | 
3864  |  |             const char *name)  | 
3865  | 0  | { | 
3866  | 0  |   struct object *object;  | 
3867  |  | 
  | 
3868  | 0  |   object = deref_tag(repo, parse_object(repo, oid),  | 
3869  | 0  |          name, strlen(name));  | 
3870  | 0  |   if (!object)  | 
3871  | 0  |     return NULL;  | 
3872  | 0  |   if (object->type == OBJ_TREE)  | 
3873  | 0  |     return make_virtual_commit(repo, (struct tree*)object, name);  | 
3874  | 0  |   if (object->type != OBJ_COMMIT)  | 
3875  | 0  |     return NULL;  | 
3876  | 0  |   if (repo_parse_commit(repo, (struct commit *)object))  | 
3877  | 0  |     return NULL;  | 
3878  | 0  |   return (struct commit *)object;  | 
3879  | 0  | }  | 
3880  |  |  | 
3881  |  | int merge_recursive_generic(struct merge_options *opt,  | 
3882  |  |           const struct object_id *head,  | 
3883  |  |           const struct object_id *merge,  | 
3884  |  |           int num_merge_bases,  | 
3885  |  |           const struct object_id *merge_bases,  | 
3886  |  |           struct commit **result)  | 
3887  | 0  | { | 
3888  | 0  |   int clean;  | 
3889  | 0  |   struct lock_file lock = LOCK_INIT;  | 
3890  | 0  |   struct commit *head_commit = get_ref(opt->repo, head, opt->branch1);  | 
3891  | 0  |   struct commit *next_commit = get_ref(opt->repo, merge, opt->branch2);  | 
3892  | 0  |   struct commit_list *ca = NULL;  | 
3893  |  | 
  | 
3894  | 0  |   if (merge_bases) { | 
3895  | 0  |     int i;  | 
3896  | 0  |     for (i = 0; i < num_merge_bases; ++i) { | 
3897  | 0  |       struct commit *base;  | 
3898  | 0  |       if (!(base = get_ref(opt->repo, &merge_bases[i],  | 
3899  | 0  |                oid_to_hex(&merge_bases[i]))))  | 
3900  | 0  |         return err(opt, _("Could not parse object '%s'"), | 
3901  | 0  |              oid_to_hex(&merge_bases[i]));  | 
3902  | 0  |       commit_list_insert(base, &ca);  | 
3903  | 0  |     }  | 
3904  | 0  |     if (num_merge_bases == 1)  | 
3905  | 0  |       opt->ancestor = "constructed merge base";  | 
3906  | 0  |   }  | 
3907  |  |  | 
3908  | 0  |   repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);  | 
3909  | 0  |   clean = merge_recursive(opt, head_commit, next_commit, ca,  | 
3910  | 0  |         result);  | 
3911  | 0  |   free_commit_list(ca);  | 
3912  | 0  |   if (clean < 0) { | 
3913  | 0  |     rollback_lock_file(&lock);  | 
3914  | 0  |     return clean;  | 
3915  | 0  |   }  | 
3916  |  |  | 
3917  | 0  |   if (write_locked_index(opt->repo->index, &lock,  | 
3918  | 0  |              COMMIT_LOCK | SKIP_IF_UNCHANGED))  | 
3919  | 0  |     return err(opt, _("Unable to write index.")); | 
3920  |  |  | 
3921  | 0  |   return clean ? 0 : 1;  | 
3922  | 0  | }  | 
3923  |  |  | 
3924  |  | static void merge_recursive_config(struct merge_options *opt, int ui)  | 
3925  | 0  | { | 
3926  | 0  |   char *value = NULL;  | 
3927  | 0  |   int renormalize = 0;  | 
3928  | 0  |   git_config_get_int("merge.verbosity", &opt->verbosity); | 
3929  | 0  |   git_config_get_int("diff.renamelimit", &opt->rename_limit); | 
3930  | 0  |   git_config_get_int("merge.renamelimit", &opt->rename_limit); | 
3931  | 0  |   git_config_get_bool("merge.renormalize", &renormalize); | 
3932  | 0  |   opt->renormalize = renormalize;  | 
3933  | 0  |   if (!git_config_get_string("diff.renames", &value)) { | 
3934  | 0  |     opt->detect_renames = git_config_rename("diff.renames", value); | 
3935  | 0  |     free(value);  | 
3936  | 0  |   }  | 
3937  | 0  |   if (!git_config_get_string("merge.renames", &value)) { | 
3938  | 0  |     opt->detect_renames = git_config_rename("merge.renames", value); | 
3939  | 0  |     free(value);  | 
3940  | 0  |   }  | 
3941  | 0  |   if (!git_config_get_string("merge.directoryrenames", &value)) { | 
3942  | 0  |     int boolval = git_parse_maybe_bool(value);  | 
3943  | 0  |     if (0 <= boolval) { | 
3944  | 0  |       opt->detect_directory_renames = boolval ?  | 
3945  | 0  |         MERGE_DIRECTORY_RENAMES_TRUE :  | 
3946  | 0  |         MERGE_DIRECTORY_RENAMES_NONE;  | 
3947  | 0  |     } else if (!strcasecmp(value, "conflict")) { | 
3948  | 0  |       opt->detect_directory_renames =  | 
3949  | 0  |         MERGE_DIRECTORY_RENAMES_CONFLICT;  | 
3950  | 0  |     } /* avoid erroring on values from future versions of git */  | 
3951  | 0  |     free(value);  | 
3952  | 0  |   }  | 
3953  | 0  |   if (ui) { | 
3954  | 0  |     if (!git_config_get_string("diff.algorithm", &value)) { | 
3955  | 0  |       long diff_algorithm = parse_algorithm_value(value);  | 
3956  | 0  |       if (diff_algorithm < 0)  | 
3957  | 0  |         die(_("unknown value for config '%s': %s"), "diff.algorithm", value); | 
3958  | 0  |       opt->xdl_opts = (opt->xdl_opts & ~XDF_DIFF_ALGORITHM_MASK) | diff_algorithm;  | 
3959  | 0  |       free(value);  | 
3960  | 0  |     }  | 
3961  | 0  |   }  | 
3962  | 0  |   git_config(git_xmerge_config, NULL);  | 
3963  | 0  | }  | 
3964  |  |  | 
3965  |  | static void init_merge_options(struct merge_options *opt,  | 
3966  |  |       struct repository *repo, int ui)  | 
3967  | 0  | { | 
3968  | 0  |   const char *merge_verbosity;  | 
3969  | 0  |   memset(opt, 0, sizeof(struct merge_options));  | 
3970  |  | 
  | 
3971  | 0  |   opt->repo = repo;  | 
3972  |  | 
  | 
3973  | 0  |   opt->detect_renames = -1;  | 
3974  | 0  |   opt->detect_directory_renames = MERGE_DIRECTORY_RENAMES_CONFLICT;  | 
3975  | 0  |   opt->rename_limit = -1;  | 
3976  |  | 
  | 
3977  | 0  |   opt->verbosity = 2;  | 
3978  | 0  |   opt->buffer_output = 1;  | 
3979  | 0  |   strbuf_init(&opt->obuf, 0);  | 
3980  |  | 
  | 
3981  | 0  |   opt->renormalize = 0;  | 
3982  |  | 
  | 
3983  | 0  |   opt->conflict_style = -1;  | 
3984  |  | 
  | 
3985  | 0  |   merge_recursive_config(opt, ui);  | 
3986  | 0  |   merge_verbosity = getenv("GIT_MERGE_VERBOSITY"); | 
3987  | 0  |   if (merge_verbosity)  | 
3988  | 0  |     opt->verbosity = strtol(merge_verbosity, NULL, 10);  | 
3989  | 0  |   if (opt->verbosity >= 5)  | 
3990  | 0  |     opt->buffer_output = 0;  | 
3991  | 0  | }  | 
3992  |  |  | 
3993  |  | void init_ui_merge_options(struct merge_options *opt,  | 
3994  |  |       struct repository *repo)  | 
3995  | 0  | { | 
3996  | 0  |   init_merge_options(opt, repo, 1);  | 
3997  | 0  | }  | 
3998  |  |  | 
3999  |  | void init_basic_merge_options(struct merge_options *opt,  | 
4000  |  |       struct repository *repo)  | 
4001  | 0  | { | 
4002  | 0  |   init_merge_options(opt, repo, 0);  | 
4003  | 0  | }  | 
4004  |  |  | 
4005  |  | /*  | 
4006  |  |  * For now, members of merge_options do not need deep copying, but  | 
4007  |  |  * it may change in the future, in which case we would need to update  | 
4008  |  |  * this, and also make a matching change to clear_merge_options() to  | 
4009  |  |  * release the resources held by a copied instance.  | 
4010  |  |  */  | 
4011  |  | void copy_merge_options(struct merge_options *dst, struct merge_options *src)  | 
4012  | 0  | { | 
4013  | 0  |   *dst = *src;  | 
4014  | 0  | }  | 
4015  |  |  | 
4016  |  | void clear_merge_options(struct merge_options *opt UNUSED)  | 
4017  | 0  | { | 
4018  | 0  |   ; /* no-op as our copy is shallow right now */  | 
4019  | 0  | }  | 
4020  |  |  | 
4021  |  | int parse_merge_opt(struct merge_options *opt, const char *s)  | 
4022  | 0  | { | 
4023  | 0  |   const char *arg;  | 
4024  |  | 
  | 
4025  | 0  |   if (!s || !*s)  | 
4026  | 0  |     return -1;  | 
4027  | 0  |   if (!strcmp(s, "ours"))  | 
4028  | 0  |     opt->recursive_variant = MERGE_VARIANT_OURS;  | 
4029  | 0  |   else if (!strcmp(s, "theirs"))  | 
4030  | 0  |     opt->recursive_variant = MERGE_VARIANT_THEIRS;  | 
4031  | 0  |   else if (!strcmp(s, "subtree"))  | 
4032  | 0  |     opt->subtree_shift = "";  | 
4033  | 0  |   else if (skip_prefix(s, "subtree=", &arg))  | 
4034  | 0  |     opt->subtree_shift = arg;  | 
4035  | 0  |   else if (!strcmp(s, "patience"))  | 
4036  | 0  |     opt->xdl_opts = DIFF_WITH_ALG(opt, PATIENCE_DIFF);  | 
4037  | 0  |   else if (!strcmp(s, "histogram"))  | 
4038  | 0  |     opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);  | 
4039  | 0  |   else if (skip_prefix(s, "diff-algorithm=", &arg)) { | 
4040  | 0  |     long value = parse_algorithm_value(arg);  | 
4041  | 0  |     if (value < 0)  | 
4042  | 0  |       return -1;  | 
4043  |  |     /* clear out previous settings */  | 
4044  | 0  |     DIFF_XDL_CLR(opt, NEED_MINIMAL);  | 
4045  | 0  |     opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;  | 
4046  | 0  |     opt->xdl_opts |= value;  | 
4047  | 0  |   }  | 
4048  | 0  |   else if (!strcmp(s, "ignore-space-change"))  | 
4049  | 0  |     DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);  | 
4050  | 0  |   else if (!strcmp(s, "ignore-all-space"))  | 
4051  | 0  |     DIFF_XDL_SET(opt, IGNORE_WHITESPACE);  | 
4052  | 0  |   else if (!strcmp(s, "ignore-space-at-eol"))  | 
4053  | 0  |     DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);  | 
4054  | 0  |   else if (!strcmp(s, "ignore-cr-at-eol"))  | 
4055  | 0  |     DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);  | 
4056  | 0  |   else if (!strcmp(s, "renormalize"))  | 
4057  | 0  |     opt->renormalize = 1;  | 
4058  | 0  |   else if (!strcmp(s, "no-renormalize"))  | 
4059  | 0  |     opt->renormalize = 0;  | 
4060  | 0  |   else if (!strcmp(s, "no-renames"))  | 
4061  | 0  |     opt->detect_renames = 0;  | 
4062  | 0  |   else if (!strcmp(s, "find-renames")) { | 
4063  | 0  |     opt->detect_renames = 1;  | 
4064  | 0  |     opt->rename_score = 0;  | 
4065  | 0  |   }  | 
4066  | 0  |   else if (skip_prefix(s, "find-renames=", &arg) ||  | 
4067  | 0  |      skip_prefix(s, "rename-threshold=", &arg)) { | 
4068  | 0  |     if ((opt->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)  | 
4069  | 0  |       return -1;  | 
4070  | 0  |     opt->detect_renames = 1;  | 
4071  | 0  |   }  | 
4072  |  |   /*  | 
4073  |  |    * Please update $__git_merge_strategy_options in  | 
4074  |  |    * git-completion.bash when you add new options  | 
4075  |  |    */  | 
4076  | 0  |   else  | 
4077  | 0  |     return -1;  | 
4078  | 0  |   return 0;  | 
4079  | 0  | }  |