Skip to content

Commit

Permalink
Don't update unchanged merge entries
Browse files Browse the repository at this point in the history
In commit 34110cd ("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>
  • Loading branch information
Linus Torvalds authored and Junio C Hamano committed Mar 16, 2008
1 parent 198724a commit 7f8ab8d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
41 changes: 41 additions & 0 deletions t/t1004-read-tree-m-u-wf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,45 @@ test_expect_success 'three-way not complaining on an untracked file' '
git read-tree -m -u --exclude-per-directory=.gitignore branch-point master side
'

test_expect_success '3-way not overwriting local changes (setup)' '
git reset --hard &&
git checkout -b side-a branch-point &&
echo >>file1 "new line to be kept in the merge result" &&
git commit -a -m "side-a changes file1" &&
git checkout -b side-b branch-point &&
echo >>file2 "new line to be kept in the merge result" &&
git commit -a -m "side-b changes file2" &&
git checkout side-a
'

test_expect_success '3-way not overwriting local changes (our side)' '
# At this point, file1 from side-a should be kept as side-b
# did not touch it.
git reset --hard &&
echo >>file1 "local changes" &&
git read-tree -m -u branch-point side-a side-b &&
grep "new line to be kept" file1 &&
grep "local changes" file1
'

test_expect_success '3-way not overwriting local changes (their side)' '
# At this point, file2 from side-b should be taken as side-a
# did not touch it.
git reset --hard &&
echo >>file2 "local changes" &&
test_must_fail git read-tree -m -u branch-point side-a side-b &&
! grep "new line to be kept" file2 &&
grep "local changes" file2
'

test_done
9 changes: 6 additions & 3 deletions unpack-trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,16 +595,19 @@ static int verify_absent(struct cache_entry *ce, const char *action,
static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
struct unpack_trees_options *o)
{
int update = CE_UPDATE;

if (old) {
/*
* See if we can re-use the old CE directly?
* That way we get the uptodate stat info.
*
* This also removes the UPDATE flag on
* a match.
* This also removes the UPDATE flag on a match; otherwise
* we will end up overwriting local changes in the work tree.
*/
if (same(old, merge)) {
copy_cache_entry(merge, old);
update = 0;
} else {
if (verify_uptodate(old, o))
return -1;
Expand All @@ -617,7 +620,7 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
invalidate_ce_path(merge, o);
}

add_entry(o, merge, CE_UPDATE, CE_STAGEMASK);
add_entry(o, merge, update, CE_STAGEMASK);
return 1;
}

Expand Down

0 comments on commit 7f8ab8d

Please sign in to comment.