344
{
345
int rc;
346
struct tree_desc t[3];
347
+ struct index_state tmp_index = { NULL };
348
349
memset(&o->unpack_opts, 0, sizeof(o->unpack_opts));
350
if (o->call_depth)
355
o->unpack_opts.head_idx = 2;
356
o->unpack_opts.fn = threeway_merge;
357
o->unpack_opts.src_index = &the_index;
357
- o->unpack_opts.dst_index = &the_index;
358
+ o->unpack_opts.dst_index = &tmp_index;
359
setup_unpack_trees_porcelain(&o->unpack_opts, "merge");
360
361
init_tree_desc_from_tree(t+0, common);
363
init_tree_desc_from_tree(t+2, merge);
364
365
rc = unpack_trees(3, t, &o->unpack_opts);
366
+ cache_tree_free(&active_cache_tree);
367
+
368
/*
366
- * unpack_trees NULLifies src_index, but it's used in verify_uptodate,
367
- * so set to the new index which will usually have modification
368
- * timestamp info copied over.
369
+ * Update the_index to match the new results, AFTER saving a copy
370
+ * in o->orig_index. Update src_index to point to the saved copy.
371
+ * (verify_uptodate() checks src_index, and the original index is
372
+ * the one that had the necessary modification timestamps.)
373
*/
370
- o->unpack_opts.src_index = &the_index;
371
- cache_tree_free(&active_cache_tree);
374
+ o->orig_index = the_index;
375
+ the_index = tmp_index;
376
+ o->unpack_opts.src_index = &o->orig_index;
377
+
378
return rc;
379
}
380
779
!(empty_ok && is_empty_dir(path));
780
}
781
776
-static int was_tracked(const char *path)
782
+/*
783
+ * Returns whether path was tracked in the index before the merge started
784
+ */
785
+static int was_tracked(struct merge_options *o, const char *path)
786
{
778
- int pos = cache_name_pos(path, strlen(path));
787
+ int pos = index_name_pos(&o->orig_index, path, strlen(path));
788
789
if (0 <= pos)
781
- /* we have been tracking this path */
790
+ /* we were tracking this path before the merge */
791
return 1;
792
784
- /*
785
- * Look for an unmerged entry for the path,
786
- * specifically stage #2, which would indicate
787
- * that "our" side before the merge started
788
- * had the path tracked (and resulted in a conflict).
789
- */
790
- for (pos = -1 - pos;
791
- pos < active_nr && !strcmp(path, active_cache[pos]->name);
792
- pos++)
793
- if (ce_stage(active_cache[pos]) == 2)
794
- return 1;
793
return 0;
794
}
795
796
static int would_lose_untracked(const char *path)
797
{
800
- return !was_tracked(path) && file_exists(path);
798
+ /*
799
+ * This may look like it can be simplified to:
800
+ * return !was_tracked(o, path) && file_exists(path)
801
+ * but it can't. This function needs to know whether path was in
802
+ * the working tree due to EITHER having been tracked in the index
803
+ * before the merge OR having been put into the working copy and
804
+ * index by unpack_trees(). Due to that either-or requirement, we
805
+ * check the current index instead of the original one.
806
+ *
807
+ * Note that we do not need to worry about merge-recursive itself
808
+ * updating the index after unpack_trees() and before calling this
809
+ * function, because we strictly require all code paths in
810
+ * merge-recursive to update the working tree first and the index
811
+ * second. Doing otherwise would break
812
+ * update_file()/would_lose_untracked(); see every comment in this
813
+ * file which mentions "update_stages".
814
+ */
815
+ int pos = cache_name_pos(path, strlen(path));
816
+
817
+ if (pos < 0)
818
+ pos = -1 - pos;
819
+ while (pos < active_nr &&
820
+ !strcmp(path, active_cache[pos]->name)) {
821
+ /*
822
+ * If stage #0, it is definitely tracked.
823
+ * If it has stage #2 then it was tracked
824
+ * before this merge started. All other
825
+ * cases the path was not tracked.
826
+ */
827
+ switch (ce_stage(active_cache[pos])) {
828
+ case 0:
829
+ case 2:
830
+ return 0;
831
+ }
832
+ pos++;
833
+ }
834
+ return file_exists(path);
835
}
836
837
static int was_dirty(struct merge_options *o, const char *path)
839
struct cache_entry *ce;
840
int dirty = 1;
841
808
- if (o->call_depth || !was_tracked(path))
842
+ if (o->call_depth || !was_tracked(o, path))
843
return !dirty;
844
845
ce = cache_file_exists(path, strlen(path), ignore_case);
2453
* add-source case).
2454
*/
2455
remove_file(o, 1, ren1_src,
2422
- renamed_stage == 2 || !was_tracked(ren1_src));
2456
+ renamed_stage == 2 || !was_tracked(o, ren1_src));
2457
2458
oidcpy(&src_other.oid,
2459
&ren1->src_entry->stages[other_stage].oid);
2846
if (update_stages(o, path, &one, &a, &b))
2847
return -1;
2848
} else {
2815
- int file_from_stage2 = was_tracked(path);
2849
+ int file_from_stage2 = was_tracked(o, path);
2850
struct diff_filespec merged;
2851
oidcpy(&merged.oid, &mfi.oid);
2852
merged.mode = mfi.mode;
3115
else
3116
clean = 1;
3117
3118
+ /* Free the extra index left from git_merge_trees() */
3119
+ /*
3120
+ * FIXME: Need to also free data allocated by
3121
+ * setup_unpack_trees_porcelain() tucked away in o->unpack_opts.msgs,
3122
+ * but the problem is that only half of it refers to dynamically
3123
+ * allocated data, while the other half points at static strings.
3124
+ */
3125
+ discard_index(&o->orig_index);
3126
+
3127
if (o->call_depth && !(*result = write_tree_from_memory(o)))
3128
return -1;
3129