]> git.ipfire.org Git - thirdparty/git.git/commitdiff
log-tree: make ref_filter_match() a helper method
authorDerrick Stolee <dstolee@microsoft.com>
Thu, 16 Apr 2020 14:15:48 +0000 (14:15 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Apr 2020 18:04:55 +0000 (11:04 -0700)
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>
log-tree.c
refs.c
refs.h

index 897a90233e470f5314fa5b6607875b74b139479d..fa6af69da387269a07d69cebd757eafafe24c01c 100644 (file)
@@ -81,6 +81,51 @@ const struct name_decoration *get_name_decoration(const struct object *obj)
        return lookup_decoration(&name_decoration, obj);
 }
 
+static int match_ref_pattern(const char *refname,
+                            const struct string_list_item *item)
+{
+       int matched = 0;
+       if (item->util == NULL) {
+               if (!wildmatch(item->string, refname, 0))
+                       matched = 1;
+       } else {
+               const char *rest;
+               if (skip_prefix(refname, item->string, &rest) &&
+                   (!*rest || *rest == '/'))
+                       matched = 1;
+       }
+       return matched;
+}
+
+static int ref_filter_match(const char *refname,
+                           const struct decoration_filter *filter)
+{
+       struct string_list_item *item;
+       const struct string_list *exclude_patterns = filter->exclude_ref_pattern;
+       const struct string_list *include_patterns = filter->include_ref_pattern;
+
+       if (exclude_patterns && exclude_patterns->nr) {
+               for_each_string_list_item(item, exclude_patterns) {
+                       if (match_ref_pattern(refname, item))
+                               return 0;
+               }
+       }
+
+       if (include_patterns && include_patterns->nr) {
+               int found = 0;
+               for_each_string_list_item(item, include_patterns) {
+                       if (match_ref_pattern(refname, item)) {
+                               found = 1;
+                               break;
+                       }
+               }
+
+               if (!found)
+                       return 0;
+       }
+       return 1;
+}
+
 static int add_ref_decoration(const char *refname, const struct object_id *oid,
                              int flags, void *cb_data)
 {
@@ -88,9 +133,7 @@ static int add_ref_decoration(const char *refname, const struct object_id *oid,
        enum decoration_type type = DECORATION_NONE;
        struct decoration_filter *filter = (struct decoration_filter *)cb_data;
 
-       if (filter && !ref_filter_match(refname,
-                             filter->include_ref_pattern,
-                             filter->exclude_ref_pattern))
+       if (filter && !ref_filter_match(refname, filter))
                return 0;
 
        if (starts_with(refname, git_replace_ref_base)) {
diff --git a/refs.c b/refs.c
index 1ab0bb54d3d73bfe17f0eab4cd02d760bb9a7d01..28c91d603c26bfad213d007796e5e63ba5f416d2 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -321,50 +321,6 @@ int ref_exists(const char *refname)
        return refs_ref_exists(get_main_ref_store(the_repository), refname);
 }
 
-static int match_ref_pattern(const char *refname,
-                            const struct string_list_item *item)
-{
-       int matched = 0;
-       if (item->util == NULL) {
-               if (!wildmatch(item->string, refname, 0))
-                       matched = 1;
-       } else {
-               const char *rest;
-               if (skip_prefix(refname, item->string, &rest) &&
-                   (!*rest || *rest == '/'))
-                       matched = 1;
-       }
-       return matched;
-}
-
-int ref_filter_match(const char *refname,
-                    const struct string_list *include_patterns,
-                    const struct string_list *exclude_patterns)
-{
-       struct string_list_item *item;
-
-       if (exclude_patterns && exclude_patterns->nr) {
-               for_each_string_list_item(item, exclude_patterns) {
-                       if (match_ref_pattern(refname, item))
-                               return 0;
-               }
-       }
-
-       if (include_patterns && include_patterns->nr) {
-               int found = 0;
-               for_each_string_list_item(item, include_patterns) {
-                       if (match_ref_pattern(refname, item)) {
-                               found = 1;
-                               break;
-                       }
-               }
-
-               if (!found)
-                       return 0;
-       }
-       return 1;
-}
-
 static int filter_refs(const char *refname, const struct object_id *oid,
                           int flags, void *data)
 {
diff --git a/refs.h b/refs.h
index 545029c6d8050a173a1486cdaffb8f33c51ae93e..a92d2c74c8306a25c0b6eab7624a06adae37a1b8 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -361,18 +361,6 @@ int for_each_rawref(each_ref_fn fn, void *cb_data);
 void normalize_glob_ref(struct string_list_item *item, const char *prefix,
                        const char *pattern);
 
-/*
- * Returns 0 if refname matches any of the exclude_patterns, or if it doesn't
- * match any of the include_patterns. Returns 1 otherwise.
- *
- * If pattern list is NULL or empty, matching against that list is skipped.
- * This has the effect of matching everything by default, unless the user
- * specifies rules otherwise.
- */
-int ref_filter_match(const char *refname,
-                    const struct string_list *include_patterns,
-                    const struct string_list *exclude_patterns);
-
 static inline const char *has_glob_specials(const char *pattern)
 {
        return strpbrk(pattern, "?*[");