-
Notifications
You must be signed in to change notification settings - Fork 2
/
root
executable file
·47 lines (40 loc) · 963 Bytes
/
root
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
#!/bin/bash
# Prints the root of the current repository.
if echo "$*" | grep -Eq -- '--help\b|-h\b'; then
echo "usage: ${0##*/} [-h|--help]" 1>&2
echo "Prints the root directory of the current source repository." 1>&2
exit 1
fi
function abort_root() {
echo "unknown" 1>&2
exit 1
}
if [[ -f /usr/local/etc/bash_completion.d/git-prompt.sh ]]; then
source /usr/local/etc/bash_completion.d/git-prompt.sh
else
abort_root
fi
branch=$(type __git_ps1 &>/dev/null && __git_ps1 "%s")
if [[ -n $branch ]]; then
vcs=git
else
branch=$(type -P hg &>/dev/null && hg branch 2>/dev/null)
if [[ -n $branch ]]; then
vcs=hg
elif [[ -e .bzr ]]; then
vcs=bzr
elif [[ -e .svn ]]; then
vcs=svn
else
vcs=
fi
fi
if [[ -n $vcs ]]; then
if [[ $vcs == "hg" ]]; then
hg root
elif [[ $vcs == "git" ]]; then
git rev-parse --show-toplevel
else
abort_root
fi
fi