Skip to content

Instantly share code, notes, and snippets.

@k-takata
Last active August 29, 2015 14:13
Show Gist options
  • Save k-takata/7b658c256e8d790c9ac7 to your computer and use it in GitHub Desktop.
Save k-takata/7b658c256e8d790c9ac7 to your computer and use it in GitHub Desktop.
Check unused bash_completion files
#!/bin/bash
#
# check_unused_bash_completion.sh
#
# Check unused bash_completion files and moves them into "unused"
# subdirectory. Loading many completion files makes Cygwin/MSYS bash
# very slow.
#
if [ -d /etc/bash_completion.d ]; then
# Cygwin
BASH_COMPLETION_DIR=/etc/bash_completion.d
elif [ -d /usr/share/bash-completion/completions ]; then
# MSYS2
BASH_COMPLETION_DIR=/usr/share/bash-completion/completions
else
echo 'completion dir not found' 1>&2
exit 1
fi
BASH_COMPLETION_DIR_UNUSED="$BASH_COMPLETION_DIR/unused"
# This function checks whether we have a given program on the system.
# No need for bulky functions in memory if we don't.
#
have()
{
unset -v have
if [[ $1 = @(bash-builtins|configure) ]]; then
have="yes"
return
fi
# Completions for system administrator commands are installed as well in
# case completion is attempted via `sudo command ...'.
PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin type $1 &>/dev/null &&
have="yes"
}
if [ ! -d $BASH_COMPLETION_DIR_UNUSED ]; then
mkdir $BASH_COMPLETION_DIR_UNUSED
fi
for i in $(LC_ALL=C command ls "$BASH_COMPLETION_DIR"); do
unset -v have
j=$BASH_COMPLETION_DIR/$i
if [[ ${j##*/} != @(*~|*.bak|*.swp|\#*\#|*.dpkg*|*.rpm@(orig|new|save)|Makefile*) \
&& -f $j && -r $j ]]; then
have "$i" || have "${i%-completion}" || \
for k in $(grep -P '^\(?have' "$j" | grep -o -P '(?<=\bhave )[^ )]+'); do
have "$k" && break
done
if [ "$i" = "mercurial" ]; then
have hg
fi
if [ "$have" = "yes" ]; then
echo -e "$j: \e[32mused\e[m"
else
echo -e "$j: \e[31munused\e[m"
mv "$j" $BASH_COMPLETION_DIR_UNUSED
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment