-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitree
executable file
·50 lines (38 loc) · 1.19 KB
/
gitree
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash
# gitree 0.1.0
# Copyright 2016 Jacob Wahlgren <[email protected]>
script_name="gitree"
function binary_exists_or_exit {
hash "$1" 2>/dev/null || { echo >&2 "$1 is required to run $script_name. Please install it and try again."; exit 1; }
}
# Build a string of .gitignored files separated by pipes (|)
function ignore_string {
# Removing leading ./ works around bug in tree -I option
# TODO Use -execdir to check for nested git repos even if root dir is not
find . -exec git check-ignore {} \; -prune | skip_with_pipes | sed 's|^./||' | tr '\n' '|'
}
# Names including a pipe character would not be properly handled by tree -I
function skip_with_pipes {
# TODO Print message to stderr when a file is skipped
grep -v '|'
}
binary_exists_or_exit git
binary_exists_or_exit tree
exit_val=1
# Default to cwd
# TODO Don't use cd to make output same as tree
dir="$1"
test -z ${dir} && dir=.
cd ${dir} || exit 1
# TODO Pass options to tree
if git rev-parse --is-inside-work-tree 2>/dev/null >/dev/null; then
# TODO Following lines are unsafe e.g. if string contains whitespace
cmd="tree -I $(ignore_string)"
echo "Running $cmd"
$cmd
exit_val=$?
else
tree
exit_val=$?
fi
exit ${exit_val}