1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
| | #include "test-tool.h"
#include "pathspec.h"
static const char usage_msg[] =
"test-tool pathspec trie [pathspecs...] -- [paths....]";
/*
* XXX Yuck. This is a lot of complicated code specific to our test. Even if it
* runs correctly, we have no real guarantee that the actual trie users are
* doing it right. And reusing their code is tough, because it happens as part
* of their own traversals (e.g., we walk the pathspec trie while walking the
* tree objects themselves).
*
* This whole test program should probably go away in favor of directly testing
* the tree-diff code.
*/
static int trie_match(const struct pathspec_trie *pst,
const char *path)
{
int pathlen = strlen(path);
int is_dir = 0;
if (pathlen > 0 && path[pathlen-1] == '/') {
is_dir = 1;
pathlen--;
}
while (pathlen) {
const char *slash = memchr(path, '/', pathlen);
int component_len;
int pos;
if (slash)
component_len = slash - path;
else
component_len = pathlen;
pos = pathspec_trie_lookup(pst, path, component_len);
if (pos < 0)
return 0;
pst = pst->entries[pos];
path += component_len;
pathlen -= component_len;
while (pathlen && *path == '/') {
path++;
pathlen--;
}
if (pst->terminal) {
if (!pst->must_be_dir)
return 1;
if (pathlen)
return 1;
return is_dir;
}
}
return 0;
}
static int cmd_trie(const char **argv)
{
const char **specs, **paths;
struct pathspec pathspec;
struct pathspec_trie *trie;
paths = specs = argv;
while (*paths && strcmp(*paths, "--"))
paths++;
if (*paths)
*paths++ = NULL;
parse_pathspec(&pathspec, 0, 0, "", specs);
trie = build_pathspec_trie(&pathspec);
if (!trie)
die("unable to make trie from pathspec");
for (; *paths; paths++) {
if (trie_match(trie, *paths))
printf("yes\n");
else
printf("no\n");
}
return 0;
}
int cmd__pathspec(int argc, const char **argv)
{
const char *cmd = argv[1];
if (!cmd)
usage(usage_msg);
else if (!strcmp(cmd, "trie"))
return cmd_trie(argv + 2);
else
die("unknown cmd: %s", cmd);
}
|