1
0
Fork 0
mirror of synced 2024-11-22 08:15:34 -05:00

Support both [] and ? glob patterns

This commit is contained in:
Eric Engstrom 2021-04-15 23:28:19 -05:00
parent dcb3743115
commit f56e903cee
No known key found for this signature in database
GPG key ID: 9232FD58D13AAAB2
3 changed files with 113 additions and 7 deletions

View file

@ -60,15 +60,14 @@ class Link(dotbot.Plugin):
self._log.warning("Globbing couldn't find anything matching " + str(path)) self._log.warning("Globbing couldn't find anything matching " + str(path))
success = False success = False
continue continue
glob_star_loc = path.find('*') if len(glob_results) == 1 and destination[-1] == '/':
if glob_star_loc == -1 and destination[-1] == '/':
self._log.error("Ambiguous action requested.") self._log.error("Ambiguous action requested.")
self._log.error("No wildcard in glob, directory use undefined: " + self._log.error("No wildcard in glob, directory use undefined: " +
destination + " -> " + str(glob_results)) destination + " -> " + str(glob_results))
self._log.warning("Did you want to link the directory or into it?") self._log.warning("Did you want to link the directory or into it?")
success = False success = False
continue continue
elif glob_star_loc == -1 and len(glob_results) == 1: elif len(glob_results) == 1 and destination[-1] != '/':
# perform a normal link operation # perform a normal link operation
if create: if create:
success &= self._create(destination) success &= self._create(destination)
@ -77,11 +76,11 @@ class Link(dotbot.Plugin):
success &= self._link(path, destination, relative, canonical_path, ignore_missing) success &= self._link(path, destination, relative, canonical_path, ignore_missing)
else: else:
self._log.lowinfo("Globs from '" + path + "': " + str(glob_results)) self._log.lowinfo("Globs from '" + path + "': " + str(glob_results))
glob_base = path[:glob_star_loc]
if glob_base.endswith('/.') or glob_base == '.':
glob_base = path[:glob_star_loc - 1]
for glob_full_item in glob_results: for glob_full_item in glob_results:
glob_item = glob_full_item[len(glob_base):] # Find common dirname between pattern and the item:
glob_dirname = os.path.dirname(os.path.commonprefix([path, glob_full_item]))
glob_item = (glob_full_item if len(glob_dirname) == 0 else glob_full_item[len(glob_dirname) + 1:])
# where is it going
glob_link_destination = os.path.join(destination, glob_item) glob_link_destination = os.path.join(destination, glob_item)
if create: if create:
success &= self._create(glob_link_destination) success &= self._create(glob_link_destination)

1
test/.gitignore vendored
View file

@ -1 +1,2 @@
.vagrant/ .vagrant/
*.log

View file

@ -0,0 +1,106 @@
test_description='link glob, all patterns'
. '../test-lib.bash'
allfruit=(apple apricot banana cherry currant cantalope)
test_expect_success 'glob patterns setup' '
mkdir ${DOTFILES}/conf &&
for fruit in "${allfruit[@]}"; do
echo "${fruit}" > ${DOTFILES}/conf/${fruit}
echo "dot-${fruit}" > ${DOTFILES}/conf/.${fruit}
done
'
test_expect_success 'glob patterns: "conf/*"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/*
EOF
for fruit in "${allfruit[@]}"; do
grep "${fruit}" ~/globtest/${fruit} &&
test \! -e ~/globtest/.${fruit}
done
'
test_expect_success 'reset' 'rm -rf ~/globtest'
test_expect_success 'glob pattern: "conf/.*"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/.*
EOF
for fruit in "${allfruit[@]}"; do
test \! -e ~/globtest/${fruit} &&
grep "dot-${fruit}" ~/globtest/.${fruit}
done
'
test_expect_success 'reset 2' 'rm -rf ~/globtest'
test_expect_success 'glob pattern: "conf/[bc]*"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/[bc]*
EOF
for fruit in "${allfruit[@]}"; do
[[ $fruit = [bc]* ]] &&
grep "${fruit}" ~/globtest/${fruit} ||
test \! -e ~/globtest/${fruit} &&
test \! -e ~/globtest/.${fruit}
done
'
test_expect_success 'reset 3' 'rm -rf ~/globtest'
test_expect_success 'glob pattern: "conf/*e"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/*e
EOF
for fruit in "${allfruit[@]}"; do
[[ $fruit = *e ]] &&
grep "${fruit}" ~/globtest/${fruit} ||
test \! -e ~/globtest/${fruit} &&
test \! -e ~/globtest/.${fruit}
done
'
test_expect_success 'reset 4' 'rm -rf ~/globtest'
test_expect_success 'glob pattern: "conf/??r*"' '
run_dotbot -v <<EOF
- defaults:
link:
glob: true
create: true
- link:
~/globtest: conf/??r*
EOF
for fruit in "${allfruit[@]}"; do
[[ $fruit = ??r* ]] &&
grep "${fruit}" ~/globtest/${fruit} ||
test \! -e ~/globtest/${fruit} &&
test \! -e ~/globtest/.${fruit}
done
'