diff: ignore sparse paths in diffstat

The diff_populate_filespec() method is used to describe the diff after a merge operation is complete. In order to avoid expanding a sparse index, the reuse_worktree_file() needs to be adapted to ignore files that are outside of the sparse-checkout cone. The file names and OIDs used for this check come from the merged tree in the case of the ORT strategy, not the index, hence the ability to look into these paths without having already expanded the index. The work done by reuse_worktree_file() is only an optimization, and requires the file being on disk for it to be of any value. Thus, it is safe to exit the method early if we do not expect the file on disk. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 8, 2021 at 11:23 UTC ad90da73513d7b0055e47c6918e1d75c6165fa69
1 file changed +8
diff.c
+8
@@ -26,6 +26,7 @@
26 #include "parse-options.h"
27 #include "help.h"
28 #include "promisor-remote.h"
29 +#include "dir.h"
30
31 #ifdef NO_FAST_WORKING_DIRECTORY
32 #define FAST_WORKING_DIRECTORY 0
@@ -3907,6 +3908,13 @@ static int reuse_worktree_file(struct index_state *istate,
3908 if (!want_file && would_convert_to_git(istate, name))
3909 return 0;
3910
3911 + /*
3912 + * If this path does not match our sparse-checkout definition,
3913 + * then the file will not be in the working directory.
3914 + */
3915 + if (!path_in_sparse_checkout(name, istate))
3916 + return 0;
3917 +
3918 len = strlen(name);
3919 pos = index_name_pos(istate, name, len);
3920 if (pos < 0)