Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (41 sloc) 1.33 KB
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"