branch.plugin.zsh
873 bytes
1# Branch: displays the current Git or Mercurial branch fast.
2# Victor Torres <vpaivatorres@gmail.com>
3# Oct 2, 2015
4
5function branch_prompt_info() {
6 # Start checking in current working directory
7 local branch="" dir="$PWD"
8 while [[ "$dir" != '/' ]]; do
9 # Found .git directory
10 if [[ -d "${dir}/.git" ]]; then
11 branch="${"$(<"${dir}/.git/HEAD")"##ref: refs/heads/}"
12 echo '±' "${branch:gs/%/%%}"
13 return
14 fi
15
16 # Found .hg directory
17 if [[ -d "${dir}/.hg" ]]; then
18 if [[ -f "${dir}/.hg/branch" ]]; then
19 branch="$(<"${dir}/.hg/branch")"
20 else
21 branch="default"
22 fi
23
24 if [[ -f "${dir}/.hg/bookmarks.current" ]]; then
25 branch="${branch}/$(<"${dir}/.hg/bookmarks.current")"
26 fi
27
28 echo '☿' "${branch:gs/%/%%}"
29 return
30 fi
31
32 # Check parent directory
33 dir="${dir:h}"
34 done
35}