Skip to content

Commit 771404d

Browse files
committed
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: '<glob>' and '<glob>/*' 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 <[email protected]>
1 parent 25b226b commit 771404d

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

git-filter-repo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,10 @@ class FilteringOptions(object):
16501650
values = re.compile(values)
16511651
items = getattr(namespace, self.dest, []) or []
16521652
items.append((mod_type, match_type, values))
1653+
if (match_type, mod_type) == ('glob', 'filter'):
1654+
if not values.endswith(b'*'):
1655+
extension = b'*' if values.endswith(b'/') else b'/*'
1656+
items.append((mod_type, match_type, values+extension))
16531657
setattr(namespace, self.dest, items)
16541658

16551659
class HelperFilter(argparse.Action):
@@ -2151,6 +2155,9 @@ EXAMPLES
21512155
new_path_changes.append(['rename', match_type, (match, repl)])
21522156
else:
21532157
new_path_changes.append(['filter', match_type, match])
2158+
if match_type == 'glob' and not match.endswith(b'*'):
2159+
extension = b'*' if match.endswith(b'/') else b'/*'
2160+
new_path_changes.append(['filter', match_type, match+extension])
21542161
return new_path_changes
21552162

21562163
@staticmethod

0 commit comments

Comments
 (0)