Skip to content

Commit 6f525e7

Browse files
aspiersgitster
authored andcommitted
add.c: move pathspec matchers into new pathspec.c for reuse
Extract the following functions from builtin/add.c to pathspec.c, in preparation for reuse by a new git check-ignore command: - fill_pathspec_matches() - find_used_pathspec() The functions being extracted are not changed in any way, except removal of the 'static' qualifier. Also add comments documenting these newly public functions, including clarifications that they operate on the index. Signed-off-by: Adam Spiers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f8a1113 commit 6f525e7

File tree

4 files changed

+67
-33
lines changed

4 files changed

+67
-33
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ LIB_H += pack-refs.h
645645
LIB_H += pack-revindex.h
646646
LIB_H += parse-options.h
647647
LIB_H += patch-ids.h
648+
LIB_H += pathspec.h
648649
LIB_H += pkt-line.h
649650
LIB_H += progress.h
650651
LIB_H += prompt.h
@@ -758,6 +759,7 @@ LIB_OBJS += parse-options-cb.o
758759
LIB_OBJS += patch-delta.o
759760
LIB_OBJS += patch-ids.o
760761
LIB_OBJS += path.o
762+
LIB_OBJS += pathspec.o
761763
LIB_OBJS += pkt-line.o
762764
LIB_OBJS += preload-index.o
763765
LIB_OBJS += pretty.o

builtin/add.c

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "cache.h"
77
#include "builtin.h"
88
#include "dir.h"
9+
#include "pathspec.h"
910
#include "exec_cmd.h"
1011
#include "cache-tree.h"
1112
#include "run-command.h"
@@ -97,39 +98,6 @@ int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
9798
return !!data.add_errors;
9899
}
99100

100-
static void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
101-
{
102-
int num_unmatched = 0, i;
103-
104-
/*
105-
* Since we are walking the index as if we were walking the directory,
106-
* we have to mark the matched pathspec as seen; otherwise we will
107-
* mistakenly think that the user gave a pathspec that did not match
108-
* anything.
109-
*/
110-
for (i = 0; i < specs; i++)
111-
if (!seen[i])
112-
num_unmatched++;
113-
if (!num_unmatched)
114-
return;
115-
for (i = 0; i < active_nr; i++) {
116-
struct cache_entry *ce = active_cache[i];
117-
match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
118-
}
119-
}
120-
121-
static char *find_used_pathspec(const char **pathspec)
122-
{
123-
char *seen;
124-
int i;
125-
126-
for (i = 0; pathspec[i]; i++)
127-
; /* just counting */
128-
seen = xcalloc(i, 1);
129-
fill_pathspec_matches(pathspec, seen, i);
130-
return seen;
131-
}
132-
133101
static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
134102
{
135103
char *seen;

pathspec.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include "cache.h"
2+
#include "dir.h"
3+
#include "pathspec.h"
4+
5+
/*
6+
* Finds which of the given pathspecs match items in the index.
7+
*
8+
* For each pathspec, sets the corresponding entry in the seen[] array
9+
* (which should be specs items long, i.e. the same size as pathspec)
10+
* to the nature of the "closest" (i.e. most specific) match found for
11+
* that pathspec in the index, if it was a closer type of match than
12+
* the existing entry. As an optimization, matching is skipped
13+
* altogether if seen[] already only contains non-zero entries.
14+
*
15+
* If seen[] has not already been written to, it may make sense
16+
* to use find_used_pathspec() instead.
17+
*/
18+
void fill_pathspec_matches(const char **pathspec, char *seen, int specs)
19+
{
20+
int num_unmatched = 0, i;
21+
22+
/*
23+
* Since we are walking the index as if we were walking the directory,
24+
* we have to mark the matched pathspec as seen; otherwise we will
25+
* mistakenly think that the user gave a pathspec that did not match
26+
* anything.
27+
*/
28+
for (i = 0; i < specs; i++)
29+
if (!seen[i])
30+
num_unmatched++;
31+
if (!num_unmatched)
32+
return;
33+
for (i = 0; i < active_nr; i++) {
34+
struct cache_entry *ce = active_cache[i];
35+
match_pathspec(pathspec, ce->name, ce_namelen(ce), 0, seen);
36+
}
37+
}
38+
39+
/*
40+
* Finds which of the given pathspecs match items in the index.
41+
*
42+
* This is a one-shot wrapper around fill_pathspec_matches() which
43+
* allocates, populates, and returns a seen[] array indicating the
44+
* nature of the "closest" (i.e. most specific) matches which each of
45+
* the given pathspecs achieves against all items in the index.
46+
*/
47+
char *find_used_pathspec(const char **pathspec)
48+
{
49+
char *seen;
50+
int i;
51+
52+
for (i = 0; pathspec[i]; i++)
53+
; /* just counting */
54+
seen = xcalloc(i, 1);
55+
fill_pathspec_matches(pathspec, seen, i);
56+
return seen;
57+
}

pathspec.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef PATHSPEC_H
2+
#define PATHSPEC_H
3+
4+
extern char *find_used_pathspec(const char **pathspec);
5+
extern void fill_pathspec_matches(const char **pathspec, char *seen, int specs);
6+
7+
#endif /* PATHSPEC_H */

0 commit comments

Comments
 (0)