-
Notifications
You must be signed in to change notification settings - Fork 0
/
git
49 lines (41 loc) · 1.33 KB
/
git
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
function is_submodule() {
local git_dir parent_git module_name path strip
# Find the root of this git repo, then check if its parent dir is also a repo
git_dir="$(git rev-parse --show-toplevel)"
parent_git="$(cd "$git_dir/.." && git rev-parse --show-toplevel 2> /dev/null)"
if [[ -n $parent_git ]]; then
strip=$((${#parent_git} + 1))
module_name=${git_dir:$strip}
# List all the submodule paths for the parent repo
while read path
do
if [[ "$path" != "$module_name" ]]; then continue; fi
if [[ -d "$parent_git/$path" ]]; then
echo $module_name
return 0;
fi
done < <(cd $parent_git && git submodule --quiet foreach 'echo $path' 2> /dev/null)
fi
return 1
}
function parse_git_branch {
git show > /dev/null 2>&1 || return
P=
SM=
submodule=$(is_submodule)
if [[ $? -eq 0 ]]; then
SM="s:$submodule"
fi
P=$P${SM:+${P:+ }${SM}}
BRANCH=
ref=$(git symbolic-ref HEAD 2> /dev/null)
if [[ $? -eq 0 ]]; then
BRANCH="b:${ref#refs/heads/}"
fi
P=$P${BRANCH:+${P:+ }${BRANCH}}
DESC="d:"$(git describe 2> /dev/null) || DESC=""
P=$P${DESC:+${P:+ }${DESC}}
echo "(${P}) "
}
# add to prompt by adding to .bashrc somewhere:
# PS1="\$(parse_git_branch)$PS1"