hledger/tools/gtree

60 lines
1.3 KiB
Plaintext
Raw Normal View History

2022-12-05 04:47:48 +03:00
#!/usr/bin/env bash
2022-12-05 19:50:32 +03:00
# List git files as an indented tree (because the other tools suck).
2022-12-05 04:47:48 +03:00
2022-12-05 19:50:32 +03:00
set -e
usage() {
cat <<EOF
gtree [-d] [REGEX] - list some or all git-tracked files as an indented tree.
Directories are shown with a trailing slash.
REGEX is a case-insensitive grep regexp matching the relative file paths.
-d: show only directories
Examples:
gtree
gtree -d
gtree yaml
gtree '(^|/)((bsd)?m|sh)ake'
EOF
exit
}
ARGS=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-h|--help)
HELP=1
shift
;;
-d)
D=1
shift
;;
*)
if [[ "$1" != -* ]]
then
ARGS+=("$1")
shift
else
>&2 echo "Error: unknown option $1"
exit 1
fi
;;
esac
done
#if [[ $HELP = 1 || ${#ARGS} -eq 0 ]]; then usage; exit; fi
if [[ $HELP = 1 ]]; then usage; exit; fi
REGEX="${ARGS[0]:-.}"
ROOT=$(pwd)
2022-12-05 04:47:48 +03:00
# list git-tracked files
2022-12-05 19:50:32 +03:00
# keep only the paths matching REGEX, if provided
2022-12-05 04:47:48 +03:00
# convert paths list to an indented tree
2022-12-05 19:50:32 +03:00
# keep only the directories, if -d provided
git ls-files \
| grep -iE "$REGEX" \
| sed -e 's%/%/:%g' -e "s%^%account $ROOT/:%" | hledger -f- accounts -t \
| grep -E "$(if [[ $D = 1 ]]; then echo '/$'; else echo '.'; fi)"