Skip to content

Commit 150115a

Browse files
committed
diff --cached: do not borrow from a work tree when a path is marked as assume-unchanged
When the index says that the file in the work tree that corresponds to the blob object that is used for comparison is known to be unchanged, "diff" reads from the file and applies convert_to_git(), instead of inflating the object, to feed the internal diff engine with, because an earlier benchnark found that it tends to be faster to use this optimization. However, the index can lie when the path is marked as assume-unchanged. Disable the optimization for such paths. Signed-off-by: Junio C Hamano <[email protected]>
1 parent ea02eef commit 150115a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

diff.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,8 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
16871687
struct stat st;
16881688
int pos, len;
16891689

1690-
/* We do not read the cache ourselves here, because the
1690+
/*
1691+
* We do not read the cache ourselves here, because the
16911692
* benchmark with my previous version that always reads cache
16921693
* shows that it makes things worse for diff-tree comparing
16931694
* two linux-2.6 kernel trees in an already checked out work
@@ -1727,6 +1728,13 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
17271728
if (hashcmp(sha1, ce->sha1) || !S_ISREG(ce->ce_mode))
17281729
return 0;
17291730

1731+
/*
1732+
* If ce is marked as "assume unchanged", there is no
1733+
* guarantee that work tree matches what we are looking for.
1734+
*/
1735+
if (ce->ce_flags & CE_VALID)
1736+
return 0;
1737+
17301738
/*
17311739
* If ce matches the file in the work tree, we can reuse it.
17321740
*/

t/t4020-diff-external.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,12 @@ test_expect_success 'force diff with "diff"' '
107107
test_cmp ../t4020/diff.NUL actual
108108
'
109109

110+
test_expect_success 'diff --cached' '
111+
git add file &&
112+
git update-index --assume-unchanged file &&
113+
echo second >file &&
114+
git diff --cached >actual &&
115+
test_cmp ../t4020/diff.NUL actual
116+
'
117+
110118
test_done

0 commit comments

Comments
 (0)