-
Notifications
You must be signed in to change notification settings - Fork 21
/
filter.c
163 lines (140 loc) · 3.72 KB
/
filter.c
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* $Copyright: $
* Copyright (c) 1996 - 2022 by Steve Baker ([email protected])
* All Rights reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "tree.h"
struct ignorefile *filterstack = NULL;
static char fpattern[PATH_MAX];
void gittrim(char *s)
{
int i, e = strnlen(s,PATH_MAX)-1;
if (s[e] == '\n') e--;
for(i = e; i >= 0; i--) {
if (s[i] != ' ') break;
if (i && s[i-1] != '\\') e--;
}
s[e+1] = '\0';
for(i = e = 0; s[i] != '\0';) {
if (s[i] == '\\') i++;
s[e++] = s[i++];
}
s[e] = '\0';
}
struct pattern *new_pattern(char *pattern)
{
struct pattern *p = xmalloc(sizeof(struct pattern));
p->pattern = scopy(pattern);
p->next = NULL;
return p;
}
struct ignorefile *new_ignorefile(char *path)
{
char buf[PATH_MAX];
struct ignorefile *ig;
struct pattern *remove = NULL, *remend, *p;
struct pattern *reverse = NULL, *revend;
int rev;
FILE *fp;
snprintf(buf, PATH_MAX, "%s/.gitignore", path);
fp = fopen(buf, "r");
if (fp == NULL) return NULL;
while (fgets(buf, PATH_MAX, fp) != NULL) {
if (buf[0] == '#') continue;
rev = (buf[0] == '!');
gittrim(buf);
if (strlen(buf) == 0) continue;
p = new_pattern(buf + (rev? 1 : 0));
if (rev) {
if (reverse == NULL) reverse = revend = p;
else {
revend->next = p;
revend = p;
}
} else {
if (remove == NULL) remove = remend = p;
else {
remend->next = p;
remend = p;
}
}
}
fclose(fp);
ig = xmalloc(sizeof(struct ignorefile));
ig->remove = remove;
ig->reverse = reverse;
ig->path = scopy(path);
ig->next = NULL;
return ig;
}
void push_filterstack(struct ignorefile *ig)
{
if (ig == NULL) return;
ig->next = filterstack;
filterstack = ig;
}
struct ignorefile *pop_filterstack(void)
{
struct ignorefile *ig = filterstack;
struct pattern *p, *c;
filterstack = filterstack->next;
for(p=c=ig->remove; p != NULL; c = p) {
p=p->next;
free(c->pattern);
}
for(p=c=ig->reverse; p != NULL; c = p) {
p=p->next;
free(c->pattern);
}
free(ig->path);
free(ig);
return NULL;
}
/**
* true if remove filter matches and no reverse filter matches.
*/
int filtercheck(char *path, char *name, int isdir)
{
int filter = 0;
struct ignorefile *ig;
struct pattern *p;
for(ig = filterstack; !filter && ig; ig = ig->next) {
int fpos = sprintf(fpattern, "%s/", ig->path);
for(p = ig->remove; p != NULL; p = p->next) {
if (patmatch(path, p->pattern, isdir) == 1) {
filter = 1;
break;
}
if (p->pattern[0] == '/') continue;
sprintf(fpattern + fpos, "%s", p->pattern);
if (patmatch(path, fpattern, isdir) == 1) {
filter = 1;
break;
}
}
}
if (!filter) return 0;
for(ig = filterstack; ig; ig = ig->next) {
int fpos = sprintf(fpattern, "%s/", ig->path);
for(p = ig->reverse; p != NULL; p = p->next) {
if (patmatch(path, p->pattern, isdir) == 1) return 0;
if (p->pattern[0] == '/') continue;
sprintf(fpattern + fpos, "%s", p->pattern);
if (patmatch(path, fpattern, isdir) == 1) return 0;
}
}
return 1;
}