Skip to content

Instantly share code, notes, and snippets.

@adrianmgg
Last active March 4, 2024 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmgg/dd2dbb29432dbaa3cf403a6f1566638e to your computer and use it in GitHub Desktop.
Save adrianmgg/dd2dbb29432dbaa3cf403a6f1566638e to your computer and use it in GitHub Desktop.
{
"files.exclude": {
// show .git/info/exclude in file tree, while still hiding everything else in .git
"**/.git": false, "**/.git/{[^i]*,i,i[^n]*,in,in[^f]*,inf,inf[^o]*,info/[^e]*,info/e,info/e[^x]*,info/ex,info/ex[^c]*,info/exc,info/exc[^l]*,info/excl,info/excl[^u]*,info/exclu,info/exclu[^d]*,info/exclud,info/exclud[^e]*,info/exclude?*}": true,
},
}
import itertools, json, sys
def pairwise_sentinel(iterable):
return itertools.pairwise(itertools.chain(iterable, (pairwise_sentinel.sentinel,)))
pairwise_sentinel.sentinel = object()
def iter_capinfo(iterable):
"""like enumerate(), but gives you (item, is_first, is_last) pairs"""
first = True
for cur, nxt in pairwise_sentinel(iterable):
yield cur, first, (nxt is pairwise_sentinel.sentinel)
first = False
def foo(base_exclude: str, exception: str):
"""generate config to exclude all files in `base_exclude` except for `exception`"""
assert not exception.endswith('/')
def inner():
so_far = ''
for chunk in exception.split('/'):
for char, _, char_islast in iter_capinfo(chunk):
assert char not in '{}[]?*' # no special glob stuff in the exception
assert char not in ',' # grouping these, so also can't have the group delimiter
yield f'{so_far}[^{char}]*'
if not char_islast: yield f'{so_far}{char}'
so_far += char
so_far += '/'
yield f'{exception}?*' # one final glob to get any files that start with the name
return {
base_exclude: False,
f'''{base_exclude}/{{{','.join(inner())}}}''': True,
}
json.dump(foo('**/.git', 'info/exclude'), sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment