summaryrefslogtreecommitdiff
path: root/unpack-trees.c
diff options
context:
space:
mode:
authorDerrick Stolee <dstolee@microsoft.com>2021-07-14 13:12:31 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-07-14 20:42:48 (GMT)
commitcd807a5cdabe5720071d91801dbc76faa8a643ba (patch)
tree68f9a867fd32a22c1a47c9bd1347342c6252fb5b /unpack-trees.c
parent17a1bb570bcd165a3dc1c28c441bee45a941bb12 (diff)
downloadgit-cd807a5cdabe5720071d91801dbc76faa8a643ba.zip
git-cd807a5cdabe5720071d91801dbc76faa8a643ba.tar.gz
git-cd807a5cdabe5720071d91801dbc76faa8a643ba.tar.bz2
unpack-trees: compare sparse directories correctly
As we further integrate the sparse-index into unpack-trees, we need to ensure that we compare sparse directory entries correctly with other entries. This affects searching for an exact path as well as sorting index entries. Sparse directory entries contain the trailing directory separator. This is important for the sorting, in particular. Thus, within do_compare_entry() we stop using S_IFREG in all cases, since sparse directories should use S_IFDIR to indicate that the comparison should treat the entry name as a dirctory. Within compare_entry(), it first calls do_compare_entry() to check the leading portion of the name. When the input path is a directory name, we could match exactly already. Thus, we should return 0 if we have an exact string match on a sparse directory entry. The final check is a length comparison between the strings. Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'unpack-trees.c')
-rw-r--r--unpack-trees.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/unpack-trees.c b/unpack-trees.c
index 87c1ed2..b113cc7 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -983,6 +983,7 @@ static int do_compare_entry(const struct cache_entry *ce,
int pathlen, ce_len;
const char *ce_name;
int cmp;
+ unsigned ce_mode;
/*
* If we have not precomputed the traverse path, it is quicker
@@ -1005,7 +1006,8 @@ static int do_compare_entry(const struct cache_entry *ce,
ce_len -= pathlen;
ce_name = ce->name + pathlen;
- return df_name_compare(ce_name, ce_len, S_IFREG, name, namelen, mode);
+ ce_mode = S_ISSPARSEDIR(ce->ce_mode) ? S_IFDIR : S_IFREG;
+ return df_name_compare(ce_name, ce_len, ce_mode, name, namelen, mode);
}
static int compare_entry(const struct cache_entry *ce, const struct traverse_info *info, const struct name_entry *n)
@@ -1015,6 +1017,16 @@ static int compare_entry(const struct cache_entry *ce, const struct traverse_inf
return cmp;
/*
+ * At this point, we know that we have a prefix match. If ce
+ * is a sparse directory, then allow an exact match. This only
+ * works when the input name is a directory, since ce->name
+ * ends in a directory separator.
+ */
+ if (S_ISSPARSEDIR(ce->ce_mode) &&
+ ce->ce_namelen == traverse_path_len(info, tree_entry_len(n)) + 1)
+ return 0;
+
+ /*
* Even if the beginning compared identically, the ce should
* compare as bigger than a directory leading up to it!
*/