Skip to content

Instantly share code, notes, and snippets.

@paulcarey
Created September 29, 2012 11:57
Show Gist options
  • Save paulcarey/3803797 to your computer and use it in GitHub Desktop.
Save paulcarey/3803797 to your computer and use it in GitHub Desktop.
Auto-generate a list of ignore-dirs for ack from git ls-files
#!/bin/bash
#
# This script serves as a useful base for generating a list of ignore-dirs
# for ack.
# It's slow to run (e.g. 30 sec on a 3000 file project on a 2010 MBA)
# and emits no output until complete.
#
# create the list of dirs tracked by git
GIT_DIR_FN=$(mktemp /tmp/create-ack-ignores_git.XXXXX) || exit 1
git ls-files | xargs -n 1 dirname | sort | uniq > $GIT_DIR_FN
# create the list of dirs tracked by ack
ACK_DIR_FN=$(mktemp /tmp/create-ack-ignores_ack.XXXXX) || exit 1
ack -f | xargs -n 1 dirname | sort | uniq > $ACK_DIR_FN
# diff between git and ack dirs
ACK_DIR_DIFF_FN=$(mktemp /tmp/create-ack-ignores_diff.XXXXX) || exit 1
# list of dirs to exclude and the shortest path to exclude them
ACK_IGNORES_FN=$(mktemp /tmp/create-ack-ignores_ignore.XXXXX) || exit 1
diff -u $GIT_DIR_FN $ACK_DIR_FN > $ACK_DIR_DIFF_FN
# grab the dirs tracked by ack, but not by git
for line in $(tail -n +4 $ACK_DIR_DIFF_FN | ack '^\+'); do
# strip the leading +
line=${line:1}
no_path_cs=$(echo $line | awk -F '/' '{print NF}')
i=1
while [[ $i -le $no_path_cs ]]; do
path=$(echo $line | cut -d '/' -f 1-$i)
ack $path $GIT_DIR_FN > /dev/null
if [[ $? -eq 0 ]]; then
# directory is tracked by git, descend
i=$[$i+1]
else
# add directory to ignore list
echo $line $path >> $ACK_IGNORES_FN
break
fi
done
done
awk '{print $2}' $ACK_IGNORES_FN | sort | uniq | awk '{print "--ignore-dir=" $0}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment