From c9f7a793e81f517593383768ec8839dac85368c4 Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Thu, 16 Apr 2020 14:15:48 +0000 Subject: 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 Signed-off-by: Derrick Stolee Signed-off-by: Junio C Hamano --- log-tree.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) (limited to 'log-tree.c') diff --git a/log-tree.c b/log-tree.c index 897a90233e..fa6af69da3 100644 --- a/log-tree.c +++ b/log-tree.c @@ -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)) { -- cgit v1.2.3 From a6be5e6764aabd4b418f7f365254518ec44c38d8 Mon Sep 17 00:00:00 2001 From: Derrick Stolee Date: Thu, 16 Apr 2020 14:15:49 +0000 Subject: log: add log.excludeDecoration config option In 'git log', the --decorate-refs-exclude option appends a pattern to a string_list. This list is used to prevent showing some refs in the decoration output, or even by --simplify-by-decoration. Users may want to use their refs space to store utility refs that should not appear in the decoration output. For example, Scalar [1] runs a background fetch but places the "new" refs inside the refs/scalar/hidden//* refspace instead of refs//* to avoid updating remote refs when the user is not looking. However, these "hidden" refs appear during regular 'git log' queries. A similar idea to use "hidden" refs is under consideration for core Git [2]. Add the 'log.excludeDecoration' config option so users can exclude some refs from decorations by default instead of needing to use --decorate-refs-exclude manually. The config value is multi-valued much like the command-line option. The documentation is careful to point out that the config value can be overridden by the --decorate-refs option, even though --decorate-refs-exclude would always "win" over --decorate-refs. Since the 'log.excludeDecoration' takes lower precedence to --decorate-refs, and --decorate-refs-exclude takes higher precedence, the struct decoration_filter needed another field. This led also to new logic in load_ref_decorations() and ref_filter_match(). There are several tests in t4202-log.sh that test the --decorate-refs-(include|exclude) options, so these are extended. Since the expected output is already stored as a file, most tests could simply replace a "--decorate-refs-exclude" option with an in-line config setting. Other tests involve the precedence of the config option compared to command-line options and needed more modification. [1] https://github.com/microsoft/scalar [2] https://lore.kernel.org/git/77b1da5d3063a2404cd750adfe3bb8be9b6c497d.1585946894.git.gitgitgadget@gmail.com/ Helped-by: Junio C Hamano Signed-off-by: Derrick Stolee Signed-off-by: Junio C Hamano --- log-tree.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'log-tree.c') diff --git a/log-tree.c b/log-tree.c index fa6af69da3..1cfd345861 100644 --- a/log-tree.c +++ b/log-tree.c @@ -103,6 +103,8 @@ static int ref_filter_match(const char *refname, 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; + const struct string_list *exclude_patterns_config = + filter->exclude_ref_config_pattern; if (exclude_patterns && exclude_patterns->nr) { for_each_string_list_item(item, exclude_patterns) { @@ -112,17 +114,20 @@ static int ref_filter_match(const char *refname, } 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 (match_ref_pattern(refname, item)) + return 1; } + return 0; + } - if (!found) - return 0; + if (exclude_patterns_config && exclude_patterns_config->nr) { + for_each_string_list_item(item, exclude_patterns_config) { + if (match_ref_pattern(refname, item)) + return 0; + } } + return 1; } @@ -198,6 +203,9 @@ void load_ref_decorations(struct decoration_filter *filter, int flags) for_each_string_list_item(item, filter->include_ref_pattern) { normalize_glob_ref(item, NULL, item->string); } + for_each_string_list_item(item, filter->exclude_ref_config_pattern) { + normalize_glob_ref(item, NULL, item->string); + } } decoration_loaded = 1; decoration_flags = flags; -- cgit v1.2.3