THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3940 lines
117KB

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