pathspec: create parse_short_magic function
Factor out the logic responsible for parsing short magic 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
b4bebdce834d9186f6b1646a943612658f4fb91c
1 file changed
+36
-18
pathspec.c
+36
-18
@@ -156,6 +156,41 @@ static int get_global_magic(int element_magic)
156
return global_magic;
157
}
158
159
+/*
160
+ * Parse the pathspec element looking for short magic
161
+ *
162
+ * saves all magic in 'magic'
163
+ * returns the position in 'elem' after all magic has been parsed
164
+ */
165
+static const char *parse_short_magic(unsigned *magic, const char *elem)
166
+{
167
+ const char *pos;
168
+
169
+ for (pos = elem + 1; *pos && *pos != ':'; pos++) {
170
+ char ch = *pos;
171
+ int i;
172
+
173
+ if (!is_pathspec_magic(ch))
174
+ break;
175
+
176
+ for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
177
+ if (pathspec_magic[i].mnemonic == ch) {
178
+ *magic |= pathspec_magic[i].bit;
179
+ break;
180
+ }
181
+ }
182
+
183
+ if (ARRAY_SIZE(pathspec_magic) <= i)
184
+ die(_("Unimplemented pathspec magic '%c' in '%s'"),
185
+ ch, elem);
186
+ }
187
+
188
+ if (*pos == ':')
189
+ pos++;
190
+
191
+ return pos;
192
+}
193
+
194
/*
195
* Take an element of a pathspec and check for magic signatures.
196
* Append the result to the prefix. Return the magic bitmap.
@@ -220,24 +255,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
255
copyfrom++;
256
} else {
257
/* shorthand */
223
- for (copyfrom = elt + 1;
224
- *copyfrom && *copyfrom != ':';
225
- copyfrom++) {
226
- char ch = *copyfrom;
227
-
228
- if (!is_pathspec_magic(ch))
229
- break;
230
- for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
231
- if (pathspec_magic[i].mnemonic == ch) {
232
- element_magic |= pathspec_magic[i].bit;
233
- break;
234
- }
235
- if (ARRAY_SIZE(pathspec_magic) <= i)
236
- die(_("Unimplemented pathspec magic '%c' in '%s'"),
237
- ch, elt);
238
- }
239
- if (*copyfrom == ':')
240
- copyfrom++;
258
+ copyfrom = parse_short_magic(&element_magic, elt);
259
}
260
261
magic |= element_magic;