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 <git@adamspiers.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Adam Spiers committed
Jan 6, 2013 at 16:58 UTC
6f525e7100061fb0dbc2d9230cc9948c7c5b2978
4 files changed
+67
-33
Makefile
+2
@@ -645,6 +645,7 @@ LIB_H += pack-refs.h
645
LIB_H += pack-revindex.h
646
LIB_H += parse-options.h
647
LIB_H += patch-ids.h
648
+LIB_H += pathspec.h
649
LIB_H += pkt-line.h
650
LIB_H += progress.h
651
LIB_H += prompt.h
@@ -758,6 +759,7 @@ LIB_OBJS += parse-options-cb.o
759
LIB_OBJS += patch-delta.o
760
LIB_OBJS += patch-ids.o
761
LIB_OBJS += path.o
762
+LIB_OBJS += pathspec.o
763
LIB_OBJS += pkt-line.o
764
LIB_OBJS += preload-index.o
765
LIB_OBJS += pretty.o
builtin/add.c
+1
-33
@@ -6,6 +6,7 @@
6
#include "cache.h"
7
#include "builtin.h"
8
#include "dir.h"
9
+#include "pathspec.h"
10
#include "exec_cmd.h"
11
#include "cache-tree.h"
12
#include "run-command.h"
@@ -97,39 +98,6 @@ int add_files_to_cache(const char *prefix, const char **pathspec, int flags)
98
return !!data.add_errors;
99
}
100
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
-
101
static char *prune_directory(struct dir_struct *dir, const char **pathspec, int prefix)
102
{
103
char *seen;
pathspec.c
new
+57
@@ -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
new
+7
@@ -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 */