Skip to content

Commit c9f7a79

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent efe3874 commit c9f7a79

File tree

3 files changed

+46
-59
lines changed

3 files changed

+46
-59
lines changed

log-tree.c

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,59 @@ const struct name_decoration *get_name_decoration(const struct object *obj)
8181
return lookup_decoration(&name_decoration, obj);
8282
}
8383

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+
84129
static int add_ref_decoration(const char *refname, const struct object_id *oid,
85130
int flags, void *cb_data)
86131
{
87132
struct object *obj;
88133
enum decoration_type type = DECORATION_NONE;
89134
struct decoration_filter *filter = (struct decoration_filter *)cb_data;
90135

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))
94137
return 0;
95138

96139
if (starts_with(refname, git_replace_ref_base)) {

refs.c

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -321,50 +321,6 @@ int ref_exists(const char *refname)
321321
return refs_ref_exists(get_main_ref_store(the_repository), refname);
322322
}
323323

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-
368324
static int filter_refs(const char *refname, const struct object_id *oid,
369325
int flags, void *data)
370326
{

refs.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -361,18 +361,6 @@ int for_each_rawref(each_ref_fn fn, void *cb_data);
361361
void normalize_glob_ref(struct string_list_item *item, const char *prefix,
362362
const char *pattern);
363363

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-
376364
static inline const char *has_glob_specials(const char *pattern)
377365
{
378366
return strpbrk(pattern, "?*[");

0 commit comments

Comments
 (0)