Skip to content

Commit 7b4158a

Browse files
rscharfegitster
authored andcommitted
ls-files: pass prefix length explicitly to prune_cache()
The function prune_cache() relies on the fact that it is only called on max_prefix and sneakily uses the matching global variable max_prefix_len directly. Tighten its interface by passing both the string and its length as parameters. While at it move the NULL check into the function to collect all cache-pruning related logic in one place. Signed-off-by: Rene Scharfe <[email protected]> Reviewed-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b9e3c2 commit 7b4158a

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

builtin/ls-files.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,14 @@ static void show_files(struct dir_struct *dir)
369369
/*
370370
* Prune the index to only contain stuff starting with "prefix"
371371
*/
372-
static void prune_cache(const char *prefix)
372+
static void prune_cache(const char *prefix, size_t prefixlen)
373373
{
374-
int pos = cache_name_pos(prefix, max_prefix_len);
374+
int pos;
375375
unsigned int first, last;
376376

377+
if (!prefix)
378+
return;
379+
pos = cache_name_pos(prefix, prefixlen);
377380
if (pos < 0)
378381
pos = -pos-1;
379382
memmove(active_cache, active_cache + pos,
@@ -384,7 +387,7 @@ static void prune_cache(const char *prefix)
384387
while (last > first) {
385388
int next = (last + first) >> 1;
386389
const struct cache_entry *ce = active_cache[next];
387-
if (!strncmp(ce->name, prefix, max_prefix_len)) {
390+
if (!strncmp(ce->name, prefix, prefixlen)) {
388391
first = next+1;
389392
continue;
390393
}
@@ -641,8 +644,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
641644
show_killed || show_modified || show_resolve_undo))
642645
show_cached = 1;
643646

644-
if (max_prefix)
645-
prune_cache(max_prefix);
647+
prune_cache(max_prefix, max_prefix_len);
646648
if (with_tree) {
647649
/*
648650
* Basic sanity check; show-stages and show-unmerged

0 commit comments

Comments
 (0)