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 <l.s.r@web.de> Reviewed-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Feb 10, 2017 at 20:42 UTC
7b4158a8d8d027491e6e2f7eff3789bd25e093d5
1 file changed
+7
-5
builtin/ls-files.c
+7
-5
@@ -369,11 +369,14 @@ static void show_files(struct dir_struct *dir)
369
/*
370
* Prune the index to only contain stuff starting with "prefix"
371
*/
372
-static void prune_cache(const char *prefix)
372
+static void prune_cache(const char *prefix, size_t prefixlen)
373
{
374
- int pos = cache_name_pos(prefix, max_prefix_len);
374
+ int pos;
375
unsigned int first, last;
376
377
+ if (!prefix)
378
+ return;
379
+ pos = cache_name_pos(prefix, prefixlen);
380
if (pos < 0)
381
pos = -pos-1;
382
memmove(active_cache, active_cache + pos,
@@ -384,7 +387,7 @@ static void prune_cache(const char *prefix)
387
while (last > first) {
388
int next = (last + first) >> 1;
389
const struct cache_entry *ce = active_cache[next];
387
- if (!strncmp(ce->name, prefix, max_prefix_len)) {
390
+ if (!strncmp(ce->name, prefix, prefixlen)) {
391
first = next+1;
392
continue;
393
}
@@ -641,8 +644,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
644
show_killed || show_modified || show_resolve_undo))
645
show_cached = 1;
646
644
- if (max_prefix)
645
- prune_cache(max_prefix);
647
+ prune_cache(max_prefix, max_prefix_len);
648
if (with_tree) {
649
/*
650
* Basic sanity check; show-stages and show-unmerged