Skip to content

Instantly share code, notes, and snippets.

@Code0x58
Last active January 24, 2019 21:31
Show Gist options
  • Save Code0x58/4642b43dc767de9fe3704356d4a278bf to your computer and use it in GitHub Desktop.
Save Code0x58/4642b43dc767de9fe3704356d4a278bf to your computer and use it in GitHub Desktop.
bash script to use tree and exclude any files/directories git is ignoring
#!/bin/bash
set -e
_=<<DOC
You can pass options to tree, but it's a bit bodgy, so you'll have to use -- before adding any directories if you pass any options.
If this script is available on your \$PATH then you can do \`git tree\` in repositories.
DOC
options=()
directories=()
# this is a bodge to make things like `git tree dir...` work so you don't have to do `git tree -- dir...`
if [ "${1:0:1}" == "-" ]; then
updating=options
else
updating=directories
fi
# build options and directories lists
while [ "$#" -ne 0 ]; do
part="$1"
shift
if [ "$updating" == options ] && [ "$part" == "--" ] ; then
updating=directories
continue
fi
if [ "$updating" == "options" ]; then
options+=("$part")
else
directories+=("${part%%/}")
fi
done
# default to the current directory if none were given
if [ "${#directories[@]}" -eq 0 ]; then
directories=(".")
fi
# for each directory, run tree for it
for dir in "${directories[@]}"; do
# this doesn't use the patterns in the 3 ignore file places as the patterns don't match up with those of tree
# TODO(Code0x58): see about submodules. --recurse-submodules can be used so might have to git -C into each dir
ignore=".git|"
ignore+="$(cd "$dir" && git ls-files --ignored --exclude-standard --others --directory --cached -z \
| sed -zr 's#/$##' \
| tr "\\0" "|")"
tree -a -I "${ignore::-1}" "${options[@]}" -- "$dir"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment