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.

3893 lines
115KB

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