Skip to content

Commit 56fc510

Browse files
Linus TorvaldsJunio C Hamano
authored andcommitted
[PATCH] git-ls-files: generalized pathspecs
This generalizes the git "glob" string to be a lot more like the git-diff-* pathspecs (but there are still differences: the diff family doesn't do any globbing, and because the diff family always generates the full native pathname, it doesn't have the issue with ".."). It does three things: - it allows multiple matching strings, ie you can do things like git-ls-files arch/i386/ include/asm-i386/ | xargs grep pattern - the "matching" criteria is a combination of "exact path component match" (the same as the git-diff-* family), and "fnmatch()". However, you should be careful with the confusion between the git-ls-files internal globbing and the standard shell globbing, ie git-ls-files fs/*.c does globbing in the shell, and does something totally different from git-ls-files 'fs/*.c' which does the globbing inside git-ls-files. The latter has _one_ pathspec with a wildcard, and will match any .c file anywhere under the fs/ directory, while the former has been expanded by the shell into having _lots_ of pathspec entries, all of which are just in the top-level fs/ subdirectory. They will happily be matched exactly, but we will thus miss all the subdirectories under fs/. As a result, the first one will (on the current kernel) match 55 files, while the second one will match 664 files! - it uses the generic path prefixing, so that ".." and friends at the beginning of the path spec work automatically NOTE! When generating relative pathname output (the default), a pathspec that causes the base to be outside the current working directory will be rejected with an error message like: fatal: git-ls-files: cannot generate relative filenames containing '..' because we do not actually generate ".." in the output. However, the ".." format works fine for the --full-name case: cd arch/i386/kernel git-ls-files --full-name ../mm/ results in arch/i386/mm/Makefile arch/i386/mm/boot_ioremap.c arch/i386/mm/discontig.c arch/i386/mm/extable.c arch/i386/mm/fault.c arch/i386/mm/highmem.c arch/i386/mm/hugetlbpage.c arch/i386/mm/init.c arch/i386/mm/ioremap.c arch/i386/mm/mmap.c arch/i386/mm/pageattr.c arch/i386/mm/pgtable.c Perhaps more commonly, the generic path prefixing means that "." and "./" automatically get simplified and work properly. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 5be4efb commit 56fc510

File tree

1 file changed

+73
-35
lines changed

1 file changed

+73
-35
lines changed

ls-files.c

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static int line_terminator = '\n';
2121

2222
static int prefix_len = 0, prefix_offset = 0;
2323
static const char *prefix = NULL;
24-
static const char *glob = NULL;
24+
static const char **pathspec = NULL;
2525

2626
static const char *tag_cached = "";
2727
static const char *tag_unmerged = "";
@@ -302,6 +302,33 @@ static int cmp_name(const void *p1, const void *p2)
302302
e2->name, e2->len);
303303
}
304304

305+
/*
306+
* Match a pathspec against a filename. The first "len" characters
307+
* are the common prefix
308+
*/
309+
static int match(const char **spec, const char *filename, int len)
310+
{
311+
const char *m;
312+
313+
while ((m = *spec++) != NULL) {
314+
int matchlen = strlen(m + len);
315+
316+
if (!matchlen)
317+
return 1;
318+
if (!strncmp(m + len, filename + len, matchlen)) {
319+
if (m[len + matchlen - 1] == '/')
320+
return 1;
321+
switch (filename[len + matchlen]) {
322+
case '/': case '\0':
323+
return 1;
324+
}
325+
}
326+
if (!fnmatch(m + len, filename + len, 0))
327+
return 1;
328+
}
329+
return 0;
330+
}
331+
305332
static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
306333
{
307334
int len = prefix_len;
@@ -310,7 +337,7 @@ static void show_dir_entry(const char *tag, struct nond_on_fs *ent)
310337
if (len >= ent->len)
311338
die("git-ls-files: internal error - directory entry not superset of prefix");
312339

313-
if (glob && fnmatch(glob, ent->name + len, 0))
340+
if (pathspec && !match(pathspec, ent->name, len))
314341
return;
315342

316343
printf("%s%s%c", tag, ent->name + offset, line_terminator);
@@ -373,7 +400,7 @@ static void show_ce_entry(const char *tag, struct cache_entry *ce)
373400
if (len >= ce_namelen(ce))
374401
die("git-ls-files: internal error - cache entry not superset of prefix");
375402

376-
if (glob && fnmatch(glob, ce->name + len, 0))
403+
if (pathspec && !match(pathspec, ce->name, len))
377404
return;
378405

379406
if (!show_stage)
@@ -455,36 +482,44 @@ static void prune_cache(void)
455482
active_nr = last;
456483
}
457484

458-
/*
459-
* If the glob starts with a subdirectory, append it to
460-
* the prefix instead, for more efficient operation.
461-
*
462-
* But we do not update the "prefix_offset", which tells
463-
* how much of the name to ignore at printout.
464-
*/
465-
static void extend_prefix(void)
485+
static void verify_pathspec(void)
466486
{
467-
const char *p, *slash;
468-
char c;
469-
470-
p = glob;
471-
slash = NULL;
472-
while ((c = *p++) != '\0') {
473-
if (c == '*')
474-
break;
475-
if (c == '/')
476-
slash = p;
487+
const char **p, *n, *prev;
488+
char *real_prefix;
489+
unsigned long max;
490+
491+
prev = NULL;
492+
max = PATH_MAX;
493+
for (p = pathspec; (n = *p) != NULL; p++) {
494+
int i, len = 0;
495+
for (i = 0; i < max; i++) {
496+
char c = n[i];
497+
if (prev && prev[i] != c)
498+
break;
499+
if (c == '*' || c == '?')
500+
break;
501+
if (c == '/')
502+
len = i+1;
503+
}
504+
prev = n;
505+
if (len < max) {
506+
max = len;
507+
if (!max)
508+
break;
509+
}
477510
}
478-
if (slash) {
479-
int len = slash - glob;
480-
char *newprefix = xmalloc(len + prefix_len + 1);
481-
memcpy(newprefix, prefix, prefix_len);
482-
memcpy(newprefix + prefix_len, glob, len);
483-
prefix_len += len;
484-
newprefix[prefix_len] = 0;
485-
prefix = newprefix;
486-
glob = *slash ? slash : NULL;
511+
512+
if (prefix_offset > max || memcmp(prev, prefix, prefix_offset))
513+
die("git-ls-files: cannot generate relative filenames containing '..'");
514+
515+
real_prefix = NULL;
516+
prefix_len = max;
517+
if (max) {
518+
real_prefix = xmalloc(max + 1);
519+
memcpy(real_prefix, prev, max);
520+
real_prefix[max] = 0;
487521
}
522+
prefix = real_prefix;
488523
}
489524

490525
static const char ls_files_usage[] =
@@ -499,7 +534,7 @@ int main(int argc, char **argv)
499534

500535
prefix = setup_git_directory();
501536
if (prefix)
502-
prefix_offset = prefix_len = strlen(prefix);
537+
prefix_offset = strlen(prefix);
503538

504539
for (i = 1; i < argc; i++) {
505540
char *arg = argv[i];
@@ -577,13 +612,16 @@ int main(int argc, char **argv)
577612
prefix_offset = 0;
578613
continue;
579614
}
580-
if (glob || *arg == '-')
615+
if (*arg == '-')
581616
usage(ls_files_usage);
582-
glob = arg;
617+
break;
583618
}
584619

585-
if (glob)
586-
extend_prefix();
620+
pathspec = get_pathspec(prefix, argv + i);
621+
622+
/* Verify that the pathspec matches the prefix */
623+
if (pathspec)
624+
verify_pathspec();
587625

588626
if (show_ignored && !exc_given) {
589627
fprintf(stderr, "%s: --ignored needs some exclude pattern\n",

0 commit comments

Comments
 (0)