@matheusbsbs / matheus-bagliano / commits / c9f7a793e8
Fork 0

log-tree: make ref_filter_match() a helper method

The ref_filter_match() method is defined in refs.h and implemented in refs.c, but is only used by add_ref_decoration() in log-tree.c. Move it into that file as a static helper method. The match_ref_pattern() comes along for the ride. While moving the code, also make a slight adjustment to have ref_filter_match() take a struct decoration_filter pointer instead of multiple string lists. This is non-functional, but will make a later change be much cleaner. The diff is easier to parse when using the --color-moved option. Reported-by: Junio C Hamano <gister@pobox.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Apr 16, 2020 at 14:15 UTC c9f7a793e81f517593383768ec8839dac85368c4
3 files changed +46 -59
log-tree.c
+46 -3
@@ -81,6 +81,51 @@ const struct name_decoration *get_name_decoration(const struct object *obj)
81 return lookup_decoration(&name_decoration, obj);
82 }
83
84 +static int match_ref_pattern(const char *refname,
85 + const struct string_list_item *item)
86 +{
87 + int matched = 0;
88 + if (item->util == NULL) {
89 + if (!wildmatch(item->string, refname, 0))
90 + matched = 1;
91 + } else {
92 + const char *rest;
93 + if (skip_prefix(refname, item->string, &rest) &&
94 + (!*rest || *rest == '/'))
95 + matched = 1;
96 + }
97 + return matched;
98 +}
99 +
100 +static int ref_filter_match(const char *refname,
101 + const struct decoration_filter *filter)
102 +{
103 + struct string_list_item *item;
104 + const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
105 + const struct string_list *include_patterns = filter->include_ref_pattern;
106 +
107 + if (exclude_patterns && exclude_patterns->nr) {
108 + for_each_string_list_item(item, exclude_patterns) {
109 + if (match_ref_pattern(refname, item))
110 + return 0;
111 + }
112 + }
113 +
114 + if (include_patterns && include_patterns->nr) {
115 + int found = 0;
116 + for_each_string_list_item(item, include_patterns) {
117 + if (match_ref_pattern(refname, item)) {
118 + found = 1;
119 + break;
120 + }
121 + }
122 +
123 + if (!found)
124 + return 0;
125 + }
126 + return 1;
127 +}
128 +
129 static int add_ref_decoration(const char *refname, const struct object_id *oid,
130 int flags, void *cb_data)
131 {
@@ -88,9 +133,7 @@ static int add_ref_decoration(const char *refname, const struct object_id *oid,
133 enum decoration_type type = DECORATION_NONE;
134 struct decoration_filter *filter = (struct decoration_filter *)cb_data;
135
91 - if (filter && !ref_filter_match(refname,
92 - filter->include_ref_pattern,
93 - filter->exclude_ref_pattern))
136 + if (filter && !ref_filter_match(refname, filter))
137 return 0;
138
139 if (starts_with(refname, git_replace_ref_base)) {
refs.c
-44
@@ -321,50 +321,6 @@ int ref_exists(const char *refname)
321 return refs_ref_exists(get_main_ref_store(the_repository), refname);
322 }
323
324 -static int match_ref_pattern(const char *refname,
325 - const struct string_list_item *item)
326 -{
327 - int matched = 0;
328 - if (item->util == NULL) {
329 - if (!wildmatch(item->string, refname, 0))
330 - matched = 1;
331 - } else {
332 - const char *rest;
333 - if (skip_prefix(refname, item->string, &rest) &&
334 - (!*rest || *rest == '/'))
335 - matched = 1;
336 - }
337 - return matched;
338 -}
339 -
340 -int ref_filter_match(const char *refname,
341 - const struct string_list *include_patterns,
342 - const struct string_list *exclude_patterns)
343 -{
344 - struct string_list_item *item;
345 -
346 - if (exclude_patterns && exclude_patterns->nr) {
347 - for_each_string_list_item(item, exclude_patterns) {
348 - if (match_ref_pattern(refname, item))
349 - return 0;
350 - }
351 - }
352 -
353 - if (include_patterns && include_patterns->nr) {
354 - int found = 0;
355 - for_each_string_list_item(item, include_patterns) {
356 - if (match_ref_pattern(refname, item)) {
357 - found = 1;
358 - break;
359 - }
360 - }
361 -
362 - if (!found)
363 - return 0;
364 - }
365 - return 1;
366 -}
367 -
324 static int filter_refs(const char *refname, const struct object_id *oid,
325 int flags, void *data)
326 {
refs.h
-12
@@ -361,18 +361,6 @@ int for_each_rawref(each_ref_fn fn, void *cb_data);
361 void normalize_glob_ref(struct string_list_item *item, const char *prefix,
362 const char *pattern);
363
364 -/*
365 - * Returns 0 if refname matches any of the exclude_patterns, or if it doesn't
366 - * match any of the include_patterns. Returns 1 otherwise.
367 - *
368 - * If pattern list is NULL or empty, matching against that list is skipped.
369 - * This has the effect of matching everything by default, unless the user
370 - * specifies rules otherwise.
371 - */
372 -int ref_filter_match(const char *refname,
373 - const struct string_list *include_patterns,
374 - const struct string_list *exclude_patterns);
375 -
364 static inline const char *has_glob_specials(const char *pattern)
365 {
366 return strpbrk(pattern, "?*[");