dir.c: move, rename and export match_attrs()

The function will be reused for matching attributes in pathspec when walking trees (currently it's used for matching pathspec when walking a list). pathspec.c would be a more neutral place for this. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Nov 18, 2018 at 17:47 UTC 22af33bece7e121b9d535d0a117cd4553b00fe07
3 files changed +43 -39
dir.c
+2 -39
@@ -276,44 +276,6 @@ static int do_read_blob(const struct object_id *oid, struct oid_stat *oid_stat,
276 #define DO_MATCH_DIRECTORY (1<<1)
277 #define DO_MATCH_SUBMODULE (1<<2)
278
279 -static int match_attrs(const struct index_state *istate,
280 - const char *name, int namelen,
281 - const struct pathspec_item *item)
282 -{
283 - int i;
284 - char *to_free = NULL;
285 -
286 - if (name[namelen])
287 - name = to_free = xmemdupz(name, namelen);
288 -
289 - git_check_attr(istate, name, item->attr_check);
290 -
291 - free(to_free);
292 -
293 - for (i = 0; i < item->attr_match_nr; i++) {
294 - const char *value;
295 - int matched;
296 - enum attr_match_mode match_mode;
297 -
298 - value = item->attr_check->items[i].value;
299 - match_mode = item->attr_match[i].match_mode;
300 -
301 - if (ATTR_TRUE(value))
302 - matched = (match_mode == MATCH_SET);
303 - else if (ATTR_FALSE(value))
304 - matched = (match_mode == MATCH_UNSET);
305 - else if (ATTR_UNSET(value))
306 - matched = (match_mode == MATCH_UNSPECIFIED);
307 - else
308 - matched = (match_mode == MATCH_VALUE &&
309 - !strcmp(item->attr_match[i].value, value));
310 - if (!matched)
311 - return 0;
312 - }
313 -
314 - return 1;
315 -}
316 -
279 /*
280 * Does 'match' match the given name?
281 * A match is found if
@@ -367,7 +329,8 @@ static int match_pathspec_item(const struct index_state *istate,
329 strncmp(item->match, name - prefix, item->prefix))
330 return 0;
331
370 - if (item->attr_match_nr && !match_attrs(istate, name, namelen, item))
332 + if (item->attr_match_nr &&
333 + !match_pathspec_attrs(istate, name, namelen, item))
334 return 0;
335
336 /* If the match was just the prefix, we matched */
pathspec.c
+38
@@ -659,3 +659,41 @@ void clear_pathspec(struct pathspec *pathspec)
659 FREE_AND_NULL(pathspec->items);
660 pathspec->nr = 0;
661 }
662 +
663 +int match_pathspec_attrs(const struct index_state *istate,
664 + const char *name, int namelen,
665 + const struct pathspec_item *item)
666 +{
667 + int i;
668 + char *to_free = NULL;
669 +
670 + if (name[namelen])
671 + name = to_free = xmemdupz(name, namelen);
672 +
673 + git_check_attr(istate, name, item->attr_check);
674 +
675 + free(to_free);
676 +
677 + for (i = 0; i < item->attr_match_nr; i++) {
678 + const char *value;
679 + int matched;
680 + enum attr_match_mode match_mode;
681 +
682 + value = item->attr_check->items[i].value;
683 + match_mode = item->attr_match[i].match_mode;
684 +
685 + if (ATTR_TRUE(value))
686 + matched = (match_mode == MATCH_SET);
687 + else if (ATTR_FALSE(value))
688 + matched = (match_mode == MATCH_UNSET);
689 + else if (ATTR_UNSET(value))
690 + matched = (match_mode == MATCH_UNSPECIFIED);
691 + else
692 + matched = (match_mode == MATCH_VALUE &&
693 + !strcmp(item->attr_match[i].value, value));
694 + if (!matched)
695 + return 0;
696 + }
697 +
698 + return 1;
699 +}
pathspec.h
+3
@@ -111,5 +111,8 @@ void add_pathspec_matches_against_index(const struct pathspec *pathspec,
111 char *seen);
112 char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
113 const struct index_state *istate);
114 +int match_pathspec_attrs(const struct index_state *istate,
115 + const char *name, int namelen,
116 + const struct pathspec_item *item);
117
118 #endif /* PATHSPEC_H */