pathspec: factor global magic into its own function
Create helper functions to read the global magic environment variables in additon to factoring out the global magic gathering logic into its own function. Signed-off-by: Brandon Williams <bmwill@google.com> Reviewed-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Brandon Williams committed
Jan 4, 2017 at 10:04 UTC
db7e85988f71b13f83f37c30e772d0e9a90d840d
1 file changed
+78
-49
pathspec.c
+78
-49
@@ -87,6 +87,75 @@ static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
87
strbuf_addf(sb, ",prefix:%d)", prefixlen);
88
}
89
90
+static inline int get_literal_global(void)
91
+{
92
+ static int literal = -1;
93
+
94
+ if (literal < 0)
95
+ literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
96
+
97
+ return literal;
98
+}
99
+
100
+static inline int get_glob_global(void)
101
+{
102
+ static int glob = -1;
103
+
104
+ if (glob < 0)
105
+ glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
106
+
107
+ return glob;
108
+}
109
+
110
+static inline int get_noglob_global(void)
111
+{
112
+ static int noglob = -1;
113
+
114
+ if (noglob < 0)
115
+ noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
116
+
117
+ return noglob;
118
+}
119
+
120
+static inline int get_icase_global(void)
121
+{
122
+ static int icase = -1;
123
+
124
+ if (icase < 0)
125
+ icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
126
+
127
+ return icase;
128
+}
129
+
130
+static int get_global_magic(int element_magic)
131
+{
132
+ int global_magic = 0;
133
+
134
+ if (get_literal_global())
135
+ global_magic |= PATHSPEC_LITERAL;
136
+
137
+ /* --glob-pathspec is overridden by :(literal) */
138
+ if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
139
+ global_magic |= PATHSPEC_GLOB;
140
+
141
+ if (get_glob_global() && get_noglob_global())
142
+ die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
143
+
144
+ if (get_icase_global())
145
+ global_magic |= PATHSPEC_ICASE;
146
+
147
+ if ((global_magic & PATHSPEC_LITERAL) &&
148
+ (global_magic & ~PATHSPEC_LITERAL))
149
+ die(_("global 'literal' pathspec setting is incompatible "
150
+ "with all other global pathspec settings"));
151
+
152
+ /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
153
+ if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
154
+ global_magic |= PATHSPEC_LITERAL;
155
+
156
+ return global_magic;
157
+}
158
+
159
/*
160
* Take an element of a pathspec and check for magic signatures.
161
* Append the result to the prefix. Return the magic bitmap.
@@ -104,46 +173,12 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
173
const char *prefix, int prefixlen,
174
const char *elt)
175
{
107
- static int literal_global = -1;
108
- static int glob_global = -1;
109
- static int noglob_global = -1;
110
- static int icase_global = -1;
111
- unsigned magic = 0, element_magic = 0, global_magic = 0;
176
+ unsigned magic = 0, element_magic = 0;
177
const char *copyfrom = elt;
178
char *match;
179
int i, pathspec_prefix = -1;
180
116
- if (literal_global < 0)
117
- literal_global = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
118
- if (literal_global)
119
- global_magic |= PATHSPEC_LITERAL;
120
-
121
- if (glob_global < 0)
122
- glob_global = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
123
- if (glob_global)
124
- global_magic |= PATHSPEC_GLOB;
125
-
126
- if (noglob_global < 0)
127
- noglob_global = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
128
-
129
- if (glob_global && noglob_global)
130
- die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
131
-
132
-
133
- if (icase_global < 0)
134
- icase_global = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
135
- if (icase_global)
136
- global_magic |= PATHSPEC_ICASE;
137
-
138
- if ((global_magic & PATHSPEC_LITERAL) &&
139
- (global_magic & ~PATHSPEC_LITERAL))
140
- die(_("global 'literal' pathspec setting is incompatible "
141
- "with all other global pathspec settings"));
142
-
143
- if (flags & PATHSPEC_LITERAL_PATH)
144
- global_magic = 0;
145
-
146
- if (elt[0] != ':' || literal_global ||
181
+ if (elt[0] != ':' || get_literal_global() ||
182
(flags & PATHSPEC_LITERAL_PATH)) {
183
; /* nothing to do */
184
} else if (elt[1] == '(') {
@@ -207,15 +242,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
242
243
magic |= element_magic;
244
210
- /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
211
- if (noglob_global && !(magic & PATHSPEC_GLOB))
212
- global_magic |= PATHSPEC_LITERAL;
213
-
214
- /* --glob-pathspec is overridden by :(literal) */
215
- if ((global_magic & PATHSPEC_GLOB) && (magic & PATHSPEC_LITERAL))
216
- global_magic &= ~PATHSPEC_GLOB;
217
-
218
- magic |= global_magic;
245
+ /* PATHSPEC_LITERAL_PATH ignores magic */
246
+ if (flags & PATHSPEC_LITERAL_PATH)
247
+ magic = PATHSPEC_LITERAL;
248
+ else
249
+ magic |= get_global_magic(element_magic);
250
251
if (pathspec_prefix >= 0 &&
252
(prefixlen || (prefix && *prefix)))
@@ -241,7 +272,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
272
* original. Useful for passing to another command.
273
*/
274
if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
244
- prefixlen && !literal_global) {
275
+ prefixlen && !get_literal_global()) {
276
struct strbuf sb = STRBUF_INIT;
277
278
/* Preserve the actual prefix length of each pattern */
@@ -408,9 +439,7 @@ void parse_pathspec(struct pathspec *pathspec,
439
440
item[i].magic = prefix_pathspec(item + i, flags,
441
prefix, prefixlen, entry);
411
- if ((flags & PATHSPEC_LITERAL_PATH) &&
412
- !(magic_mask & PATHSPEC_LITERAL))
413
- item[i].magic |= PATHSPEC_LITERAL;
442
+
443
if (item[i].magic & PATHSPEC_EXCLUDE)
444
nr_exclude++;
445
if (item[i].magic & magic_mask)