Skip to content

Commit 84b8b5d

Browse files
pcloudsgitster
authored andcommitted
remove match_pathspec() in favor of match_pathspec_depth()
match_pathspec_depth was created to replace match_pathspec (see 61cf282 (pathspec: add match_pathspec_depth() - 2010-12-15). It took more than two years, but the replacement finally happens :-) Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9a08727 commit 84b8b5d

File tree

6 files changed

+25
-140
lines changed

6 files changed

+25
-140
lines changed

builtin/add.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,23 +195,21 @@ int add_files_to_cache(const char *prefix,
195195
}
196196

197197
#define WARN_IMPLICIT_DOT (1u << 0)
198-
static char *prune_directory(struct dir_struct *dir, const char **pathspec,
198+
static char *prune_directory(struct dir_struct *dir, struct pathspec *pathspec,
199199
int prefix, unsigned flag)
200200
{
201201
char *seen;
202-
int i, specs;
202+
int i;
203203
struct dir_entry **src, **dst;
204204

205-
for (specs = 0; pathspec[specs]; specs++)
206-
/* nothing */;
207-
seen = xcalloc(specs, 1);
205+
seen = xcalloc(pathspec->nr, 1);
208206

209207
src = dst = dir->entries;
210208
i = dir->nr;
211209
while (--i >= 0) {
212210
struct dir_entry *entry = *src++;
213-
if (match_pathspec(pathspec, entry->name, entry->len,
214-
prefix, seen))
211+
if (match_pathspec_depth(pathspec, entry->name, entry->len,
212+
prefix, seen))
215213
*dst++ = entry;
216214
else if (flag & WARN_IMPLICIT_DOT)
217215
/*
@@ -225,7 +223,7 @@ static char *prune_directory(struct dir_struct *dir, const char **pathspec,
225223
warn_pathless_add();
226224
}
227225
dir->nr = dst - dir->entries;
228-
add_pathspec_matches_against_index(pathspec, seen, specs);
226+
add_pathspec_matches_against_index(pathspec, seen);
229227
return seen;
230228
}
231229

@@ -523,7 +521,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
523521
/* This picks up the paths that are not tracked */
524522
baselen = fill_directory(&dir, implicit_dot ? &empty_pathspec : &pathspec);
525523
if (pathspec.nr)
526-
seen = prune_directory(&dir, pathspec.raw, baselen,
524+
seen = prune_directory(&dir, &pathspec, baselen,
527525
implicit_dot ? WARN_IMPLICIT_DOT : 0);
528526
}
529527

@@ -538,23 +536,23 @@ int cmd_add(int argc, const char **argv, const char *prefix)
538536
int i;
539537

540538
if (!seen)
541-
seen = find_pathspecs_matching_against_index(pathspec.raw);
539+
seen = find_pathspecs_matching_against_index(&pathspec);
542540

543541
/*
544542
* file_exists() assumes exact match
545543
*/
546544
GUARD_PATHSPEC(&pathspec, PATHSPEC_FROMTOP);
547545

548-
for (i = 0; pathspec.raw[i]; i++) {
549-
if (!seen[i] && pathspec.raw[i][0]
550-
&& !file_exists(pathspec.raw[i])) {
546+
for (i = 0; i < pathspec.nr; i++) {
547+
const char *path = pathspec.items[i].match;
548+
if (!seen[i] && !file_exists(path)) {
551549
if (ignore_missing) {
552550
int dtype = DT_UNKNOWN;
553-
if (is_excluded(&dir, pathspec.raw[i], &dtype))
554-
dir_add_ignored(&dir, pathspec.raw[i], strlen(pathspec.raw[i]));
551+
if (is_excluded(&dir, path, &dtype))
552+
dir_add_ignored(&dir, path, pathspec.items[i].len);
555553
} else
556554
die(_("pathspec '%s' did not match any files"),
557-
pathspec.raw[i]);
555+
pathspec.items[i].original);
558556
}
559557
}
560558
free(seen);

builtin/check-ignore.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ static int check_ignore(struct dir_struct *dir,
9494
* should not be ignored, in order to be consistent with
9595
* 'git status', 'git add' etc.
9696
*/
97-
seen = find_pathspecs_matching_against_index(pathspec.raw);
97+
seen = find_pathspecs_matching_against_index(&pathspec);
9898
for (i = 0; i < pathspec.nr; i++) {
99-
full_path = pathspec.raw[i];
99+
full_path = pathspec.items[i].match;
100100
exclude = NULL;
101101
if (!seen[i]) {
102102
exclude = last_exclude_matching(dir, full_path, &dtype);

dir.c

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -171,113 +171,6 @@ int within_depth(const char *name, int namelen,
171171
return 1;
172172
}
173173

174-
/*
175-
* Does 'match' match the given name?
176-
* A match is found if
177-
*
178-
* (1) the 'match' string is leading directory of 'name', or
179-
* (2) the 'match' string is a wildcard and matches 'name', or
180-
* (3) the 'match' string is exactly the same as 'name'.
181-
*
182-
* and the return value tells which case it was.
183-
*
184-
* It returns 0 when there is no match.
185-
*/
186-
static int match_one(const char *match, const char *name, int namelen)
187-
{
188-
int matchlen;
189-
int literal = limit_pathspec_to_literal();
190-
191-
/* If the match was just the prefix, we matched */
192-
if (!*match)
193-
return MATCHED_RECURSIVELY;
194-
195-
if (ignore_case) {
196-
for (;;) {
197-
unsigned char c1 = tolower(*match);
198-
unsigned char c2 = tolower(*name);
199-
if (c1 == '\0' || (!literal && is_glob_special(c1)))
200-
break;
201-
if (c1 != c2)
202-
return 0;
203-
match++;
204-
name++;
205-
namelen--;
206-
}
207-
} else {
208-
for (;;) {
209-
unsigned char c1 = *match;
210-
unsigned char c2 = *name;
211-
if (c1 == '\0' || (!literal && is_glob_special(c1)))
212-
break;
213-
if (c1 != c2)
214-
return 0;
215-
match++;
216-
name++;
217-
namelen--;
218-
}
219-
}
220-
221-
/*
222-
* If we don't match the matchstring exactly,
223-
* we need to match by fnmatch
224-
*/
225-
matchlen = strlen(match);
226-
if (strncmp_icase(match, name, matchlen)) {
227-
if (literal)
228-
return 0;
229-
return !fnmatch_icase(match, name, 0) ? MATCHED_FNMATCH : 0;
230-
}
231-
232-
if (namelen == matchlen)
233-
return MATCHED_EXACTLY;
234-
if (match[matchlen-1] == '/' || name[matchlen] == '/')
235-
return MATCHED_RECURSIVELY;
236-
return 0;
237-
}
238-
239-
/*
240-
* Given a name and a list of pathspecs, returns the nature of the
241-
* closest (i.e. most specific) match of the name to any of the
242-
* pathspecs.
243-
*
244-
* The caller typically calls this multiple times with the same
245-
* pathspec and seen[] array but with different name/namelen
246-
* (e.g. entries from the index) and is interested in seeing if and
247-
* how each pathspec matches all the names it calls this function
248-
* with. A mark is left in the seen[] array for each pathspec element
249-
* indicating the closest type of match that element achieved, so if
250-
* seen[n] remains zero after multiple invocations, that means the nth
251-
* pathspec did not match any names, which could indicate that the
252-
* user mistyped the nth pathspec.
253-
*/
254-
int match_pathspec(const char **pathspec, const char *name, int namelen,
255-
int prefix, char *seen)
256-
{
257-
int i, retval = 0;
258-
259-
if (!pathspec)
260-
return 1;
261-
262-
name += prefix;
263-
namelen -= prefix;
264-
265-
for (i = 0; pathspec[i] != NULL; i++) {
266-
int how;
267-
const char *match = pathspec[i] + prefix;
268-
if (seen && seen[i] == MATCHED_EXACTLY)
269-
continue;
270-
how = match_one(match, name, namelen);
271-
if (how) {
272-
if (retval < how)
273-
retval = how;
274-
if (seen && seen[i] < how)
275-
seen[i] = how;
276-
}
277-
}
278-
return retval;
279-
}
280-
281174
/*
282175
* Does 'match' match the given name?
283176
* A match is found if

dir.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,6 @@ struct dir_struct {
131131
extern int simple_length(const char *match);
132132
extern int no_wildcard(const char *string);
133133
extern char *common_prefix(const struct pathspec *pathspec);
134-
extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
135134
extern int match_pathspec_depth(const struct pathspec *pathspec,
136135
const char *name, int namelen,
137136
int prefix, char *seen);

pathspec.c

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* If seen[] has not already been written to, it may make sense
1616
* to use find_pathspecs_matching_against_index() instead.
1717
*/
18-
void add_pathspec_matches_against_index(const char **pathspec,
19-
char *seen, int specs)
18+
void add_pathspec_matches_against_index(const struct pathspec *pathspec,
19+
char *seen)
2020
{
2121
int num_unmatched = 0, i;
2222

@@ -26,14 +26,14 @@ void add_pathspec_matches_against_index(const char **pathspec,
2626
* mistakenly think that the user gave a pathspec that did not match
2727
* anything.
2828
*/
29-
for (i = 0; i < specs; i++)
29+
for (i = 0; i < pathspec->nr; i++)
3030
if (!seen[i])
3131
num_unmatched++;
3232
if (!num_unmatched)
3333
return;
3434
for (i = 0; i < active_nr; i++) {
3535
struct cache_entry *ce = active_cache[i];
36-
match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
36+
match_pathspec_depth(pathspec, ce->name, ce_namelen(ce), 0, seen);
3737
}
3838
}
3939

@@ -45,15 +45,10 @@ void add_pathspec_matches_against_index(const char **pathspec,
4545
* nature of the "closest" (i.e. most specific) matches which each of the
4646
* given pathspecs achieves against all items in the index.
4747
*/
48-
char *find_pathspecs_matching_against_index(const char **pathspec)
48+
char *find_pathspecs_matching_against_index(const struct pathspec *pathspec)
4949
{
50-
char *seen;
51-
int i;
52-
53-
for (i = 0; pathspec[i]; i++)
54-
; /* just counting */
55-
seen = xcalloc(i, 1);
56-
add_pathspec_matches_against_index(pathspec, seen, i);
50+
char *seen = xcalloc(pathspec->nr, 1);
51+
add_pathspec_matches_against_index(pathspec, seen);
5752
return seen;
5853
}
5954

pathspec.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ extern void free_pathspec(struct pathspec *);
6363

6464
extern int limit_pathspec_to_literal(void);
6565

66-
extern char *find_pathspecs_matching_against_index(const char **pathspec);
67-
extern void add_pathspec_matches_against_index(const char **pathspec, char *seen, int specs);
66+
extern char *find_pathspecs_matching_against_index(const struct pathspec *pathspec);
67+
extern void add_pathspec_matches_against_index(const struct pathspec *pathspec, char *seen);
6868
extern const char *check_path_for_gitlink(const char *path);
6969
extern void die_if_path_beyond_symlink(const char *path, const char *prefix);
7070

0 commit comments

Comments
 (0)