@matheusbsbs / matheus-bagliano / commits / 3c6a370b0e
Fork 0

Move pathspec matching from builtin-add.c into dir.c

I'll use it for builtin-rm.c too. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 19, 2006 at 16:07 UTC 3c6a370b0ee78114a656acba04fddf306252b9d5
3 files changed +84 -81
builtin-add.c
+1 -81
@@ -12,86 +12,6 @@
12 static const char builtin_add_usage[] =
13 "git-add [-n] [-v] <filepattern>...";
14
15 -static int common_prefix(const char **pathspec)
16 -{
17 - const char *path, *slash, *next;
18 - int prefix;
19 -
20 - if (!pathspec)
21 - return 0;
22 -
23 - path = *pathspec;
24 - slash = strrchr(path, '/');
25 - if (!slash)
26 - return 0;
27 -
28 - prefix = slash - path + 1;
29 - while ((next = *++pathspec) != NULL) {
30 - int len = strlen(next);
31 - if (len >= prefix && !memcmp(path, next, len))
32 - continue;
33 - for (;;) {
34 - if (!len)
35 - return 0;
36 - if (next[--len] != '/')
37 - continue;
38 - if (memcmp(path, next, len+1))
39 - continue;
40 - prefix = len + 1;
41 - break;
42 - }
43 - }
44 - return prefix;
45 -}
46 -
47 -static int match_one(const char *match, const char *name, int namelen)
48 -{
49 - int matchlen;
50 -
51 - /* If the match was just the prefix, we matched */
52 - matchlen = strlen(match);
53 - if (!matchlen)
54 - return 1;
55 -
56 - /*
57 - * If we don't match the matchstring exactly,
58 - * we need to match by fnmatch
59 - */
60 - if (strncmp(match, name, matchlen))
61 - return !fnmatch(match, name, 0);
62 -
63 - /*
64 - * If we did match the string exactly, we still
65 - * need to make sure that it happened on a path
66 - * component boundary (ie either the last character
67 - * of the match was '/', or the next character of
68 - * the name was '/' or the terminating NUL.
69 - */
70 - return match[matchlen-1] == '/' ||
71 - name[matchlen] == '/' ||
72 - !name[matchlen];
73 -}
74 -
75 -static int match(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
76 -{
77 - int retval;
78 - const char *match;
79 -
80 - name += prefix;
81 - namelen -= prefix;
82 -
83 - for (retval = 0; (match = *pathspec++) != NULL; seen++) {
84 - if (retval & *seen)
85 - continue;
86 - match += prefix;
87 - if (match_one(match, name, namelen)) {
88 - retval = 1;
89 - *seen = 1;
90 - }
91 - }
92 - return retval;
93 -}
94 -
15 static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
16 {
17 char *seen;
@@ -107,7 +27,7 @@ static void prune_directory(struct dir_struct *dir, const char **pathspec, int p
27 i = dir->nr;
28 while (--i >= 0) {
29 struct dir_entry *entry = *src++;
110 - if (!match(pathspec, entry->name, entry->len, prefix, seen)) {
30 + if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) {
31 free(entry);
32 continue;
33 }
dir.c
+80
@@ -11,6 +11,86 @@
11 #include "cache.h"
12 #include "dir.h"
13
14 +int common_prefix(const char **pathspec)
15 +{
16 + const char *path, *slash, *next;
17 + int prefix;
18 +
19 + if (!pathspec)
20 + return 0;
21 +
22 + path = *pathspec;
23 + slash = strrchr(path, '/');
24 + if (!slash)
25 + return 0;
26 +
27 + prefix = slash - path + 1;
28 + while ((next = *++pathspec) != NULL) {
29 + int len = strlen(next);
30 + if (len >= prefix && !memcmp(path, next, len))
31 + continue;
32 + for (;;) {
33 + if (!len)
34 + return 0;
35 + if (next[--len] != '/')
36 + continue;
37 + if (memcmp(path, next, len+1))
38 + continue;
39 + prefix = len + 1;
40 + break;
41 + }
42 + }
43 + return prefix;
44 +}
45 +
46 +static int match_one(const char *match, const char *name, int namelen)
47 +{
48 + int matchlen;
49 +
50 + /* If the match was just the prefix, we matched */
51 + matchlen = strlen(match);
52 + if (!matchlen)
53 + return 1;
54 +
55 + /*
56 + * If we don't match the matchstring exactly,
57 + * we need to match by fnmatch
58 + */
59 + if (strncmp(match, name, matchlen))
60 + return !fnmatch(match, name, 0);
61 +
62 + /*
63 + * If we did match the string exactly, we still
64 + * need to make sure that it happened on a path
65 + * component boundary (ie either the last character
66 + * of the match was '/', or the next character of
67 + * the name was '/' or the terminating NUL.
68 + */
69 + return match[matchlen-1] == '/' ||
70 + name[matchlen] == '/' ||
71 + !name[matchlen];
72 +}
73 +
74 +int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen)
75 +{
76 + int retval;
77 + const char *match;
78 +
79 + name += prefix;
80 + namelen -= prefix;
81 +
82 + for (retval = 0; (match = *pathspec++) != NULL; seen++) {
83 + if (retval & *seen)
84 + continue;
85 + match += prefix;
86 + if (match_one(match, name, namelen)) {
87 + retval = 1;
88 + *seen = 1;
89 + }
90 + }
91 + return retval;
92 +}
93 +
94 void add_exclude(const char *string, const char *base,
95 int baselen, struct exclude_list *which)
96 {
dir.h
+3
@@ -39,6 +39,9 @@ struct dir_struct {
39 struct exclude_list exclude_list[3];
40 };
41
42 +extern int common_prefix(const char **pathspec);
43 +extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
44 +
45 extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen);
46 extern int excluded(struct dir_struct *, const char *);
47 extern void add_excludes_from_file(struct dir_struct *, const char *fname);