pathspec: create strip submodule slash helpers
Factor out the logic responsible for stripping the trailing slash on pathspecs referencing submodules 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
5590215b1392b4f094f60d7db7a3c796b8bc9aae
1 file changed
+42
-26
pathspec.c
+42
-26
@@ -258,6 +258,44 @@ static const char *parse_element_magic(unsigned *magic, int *prefix_len,
258
return parse_short_magic(magic, elem);
259
}
260
261
+static void strip_submodule_slash_cheap(struct pathspec_item *item)
262
+{
263
+ if (item->len >= 1 && item->match[item->len - 1] == '/') {
264
+ int i = cache_name_pos(item->match, item->len - 1);
265
+
266
+ if (i >= 0 && S_ISGITLINK(active_cache[i]->ce_mode)) {
267
+ item->len--;
268
+ item->match[item->len] = '\0';
269
+ }
270
+ }
271
+}
272
+
273
+static void strip_submodule_slash_expensive(struct pathspec_item *item)
274
+{
275
+ int i;
276
+
277
+ for (i = 0; i < active_nr; i++) {
278
+ struct cache_entry *ce = active_cache[i];
279
+ int ce_len = ce_namelen(ce);
280
+
281
+ if (!S_ISGITLINK(ce->ce_mode))
282
+ continue;
283
+
284
+ if (item->len <= ce_len || item->match[ce_len] != '/' ||
285
+ memcmp(ce->name, item->match, ce_len))
286
+ continue;
287
+
288
+ if (item->len == ce_len + 1) {
289
+ /* strip trailing slash */
290
+ item->len--;
291
+ item->match[item->len] = '\0';
292
+ } else {
293
+ die(_("Pathspec '%s' is in submodule '%.*s'"),
294
+ item->original, ce_len, ce->name);
295
+ }
296
+ }
297
+}
298
+
299
/*
300
* Take an element of a pathspec and check for magic signatures.
301
* Append the result to the prefix. Return the magic bitmap.
@@ -278,7 +316,7 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
316
unsigned magic = 0, element_magic = 0;
317
const char *copyfrom = elt;
318
char *match;
281
- int i, pathspec_prefix = -1;
319
+ int pathspec_prefix = -1;
320
321
/* PATHSPEC_LITERAL_PATH ignores magic */
322
if (flags & PATHSPEC_LITERAL_PATH) {
@@ -329,33 +367,11 @@ static unsigned prefix_pathspec(struct pathspec_item *item, unsigned flags,
367
item->len = strlen(item->match);
368
item->prefix = prefixlen;
369
332
- if ((flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP) &&
333
- (item->len >= 1 && item->match[item->len - 1] == '/') &&
334
- (i = cache_name_pos(item->match, item->len - 1)) >= 0 &&
335
- S_ISGITLINK(active_cache[i]->ce_mode)) {
336
- item->len--;
337
- match[item->len] = '\0';
338
- }
370
+ if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP)
371
+ strip_submodule_slash_cheap(item);
372
373
if (flags & PATHSPEC_STRIP_SUBMODULE_SLASH_EXPENSIVE)
341
- for (i = 0; i < active_nr; i++) {
342
- struct cache_entry *ce = active_cache[i];
343
- int ce_len = ce_namelen(ce);
344
-
345
- if (!S_ISGITLINK(ce->ce_mode))
346
- continue;
347
-
348
- if (item->len <= ce_len || match[ce_len] != '/' ||
349
- memcmp(ce->name, match, ce_len))
350
- continue;
351
- if (item->len == ce_len + 1) {
352
- /* strip trailing slash */
353
- item->len--;
354
- match[item->len] = '\0';
355
- } else
356
- die (_("Pathspec '%s' is in submodule '%.*s'"),
357
- elt, ce_len, ce->name);
358
- }
374
+ strip_submodule_slash_expensive(item);
375
376
if (magic & PATHSPEC_LITERAL)
377
item->nowildcard_len = item->len;