@matheusbsbs / matheus-bagliano / commits / 7f8ab8dc07
Fork 0

Don't update unchanged merge entries

In commit 34110cd4e394e3f92c01a4709689b384c34645d8 ("Make 'unpack_trees()' have a separate source and destination index") I introduced a really stupid bug in that it would always add merged entries with the CE_UPDATE flag set. That caused us to always re-write the file, even when it was already up-to-date in the source index. Not only is that really stupid from a performance angle, but more importantly it's actively wrong: if we have dirty state in the tree when we merge, overwriting it with the result of the merge will incorrectly overwrite that dirty state. This trivially fixes the problem - simply don't set the CE_UPDATE flag when the merge result matches the old state. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Mar 16, 2008 at 11:42 UTC 7f8ab8dc07ba4e892d0c2b5e7ac8436f9e845920
2 files changed +47 -3
t/t1004-read-tree-m-u-wf.sh
+41
@@ -116,4 +116,45 @@ test_expect_success 'three-way not complaining on an untracked file' '
116 git read-tree -m -u --exclude-per-directory=.gitignore branch-point master side
117 '
118
119 +test_expect_success '3-way not overwriting local changes (setup)' '
120 +
121 + git reset --hard &&
122 + git checkout -b side-a branch-point &&
123 + echo >>file1 "new line to be kept in the merge result" &&
124 + git commit -a -m "side-a changes file1" &&
125 + git checkout -b side-b branch-point &&
126 + echo >>file2 "new line to be kept in the merge result" &&
127 + git commit -a -m "side-b changes file2" &&
128 + git checkout side-a
129 +
130 +'
131 +
132 +test_expect_success '3-way not overwriting local changes (our side)' '
133 +
134 + # At this point, file1 from side-a should be kept as side-b
135 + # did not touch it.
136 +
137 + git reset --hard &&
138 +
139 + echo >>file1 "local changes" &&
140 + git read-tree -m -u branch-point side-a side-b &&
141 + grep "new line to be kept" file1 &&
142 + grep "local changes" file1
143 +
144 +'
145 +
146 +test_expect_success '3-way not overwriting local changes (their side)' '
147 +
148 + # At this point, file2 from side-b should be taken as side-a
149 + # did not touch it.
150 +
151 + git reset --hard &&
152 +
153 + echo >>file2 "local changes" &&
154 + test_must_fail git read-tree -m -u branch-point side-a side-b &&
155 + ! grep "new line to be kept" file2 &&
156 + grep "local changes" file2
157 +
158 +'
159 +
160 test_done
unpack-trees.c
+6 -3
@@ -595,16 +595,19 @@ static int verify_absent(struct cache_entry *ce, const char *action,
595 static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
596 struct unpack_trees_options *o)
597 {
598 + int update = CE_UPDATE;
599 +
600 if (old) {
601 /*
602 * See if we can re-use the old CE directly?
603 * That way we get the uptodate stat info.
604 *
603 - * This also removes the UPDATE flag on
604 - * a match.
605 + * This also removes the UPDATE flag on a match; otherwise
606 + * we will end up overwriting local changes in the work tree.
607 */
608 if (same(old, merge)) {
609 copy_cache_entry(merge, old);
610 + update = 0;
611 } else {
612 if (verify_uptodate(old, o))
613 return -1;
@@ -617,7 +620,7 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
620 invalidate_ce_path(merge, o);
621 }
622
620 - add_entry(o, merge, CE_UPDATE, CE_STAGEMASK);
623 + add_entry(o, merge, update, CE_STAGEMASK);
624 return 1;
625 }
626