From 771404d656a43664fab88755a0cf1910f1b50121 Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Sat, 20 Jun 2020 08:31:53 -0700 Subject: [PATCH] filter-repo: allow globs to match file or directory names I added special code to filter-repo so that --path expressions could match filenames or some leading directory name. --path-regex, since it does not implicitly add anchorings, can also match a leading path, and can thus be used to match against directories. --path-glob could not be used to match a leading directory of a path, since fnmatch.fnmatch() requires the full string to match. But users like being able to specify directory names, such as '*/bin', so let's take any glob expression and treat it as two: '' and '/*' and try to match against either one; this will allow it to match against file or directory names like the other two types of path matching. Signed-off-by: Elijah Newren --- git-filter-repo | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/git-filter-repo b/git-filter-repo index bf25ea7..39cdd85 100755 --- a/git-filter-repo +++ b/git-filter-repo @@ -1650,6 +1650,10 @@ class FilteringOptions(object): values = re.compile(values) items = getattr(namespace, self.dest, []) or [] items.append((mod_type, match_type, values)) + if (match_type, mod_type) == ('glob', 'filter'): + if not values.endswith(b'*'): + extension = b'*' if values.endswith(b'/') else b'/*' + items.append((mod_type, match_type, values+extension)) setattr(namespace, self.dest, items) class HelperFilter(argparse.Action): @@ -2151,6 +2155,9 @@ EXAMPLES new_path_changes.append(['rename', match_type, (match, repl)]) else: new_path_changes.append(['filter', match_type, match]) + if match_type == 'glob' and not match.endswith(b'*'): + extension = b'*' if match.endswith(b'/') else b'/*' + new_path_changes.append(['filter', match_type, match+extension]) return new_path_changes @staticmethod