-
-
Save wayneeseguin/894291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function git_rev_head | |
{ | |
# Can declare multiple variables on one line IFS separated. | |
# By default IFS is "white characters" | |
local rev_prompt_token=" » " rev=1 sym=0 sym_prompt="" rev_prompt="" \ | |
colors=() branches=() | |
# If git branches contain whitespace, then the following should be quoted. | |
branches[$rev]="$(git name-rev --name-only head 2> /dev/null)" | |
branches[$sym]="$(git symbolic-ref -q head 2> /dev/null)" | |
branches[$sym]="${branches[$sym]##refs/heads/}" | |
# Use [[ not [ as [[ is builtin and [ is an external process, | |
# specifically/bin/test | |
if [[ ${#branches} -gt 0 ]]; then | |
for branch in $sym $rev; do | |
case "${branches[$branch]}" in | |
production*|master*) | |
colors[$branch]="${bold}${red_text}" | |
;; | |
merge_*) | |
colors[$branch]="${cyan_text}" | |
;; | |
feature_*) | |
colors[$branch]="${yellow_text}" | |
;; | |
*) | |
colors[$branch]="${green_text}" | |
;; | |
esac | |
done | |
sym_prompt="${colors[$sym]}${branches[$sym]}${reset}" | |
rev_prompt="${colors[$rev]}${branches[$rev]}${reset}${rev_prompt_token}" | |
if [[ "${branches[$sym]}" = "${branches[$rev]}" ]]; then | |
rev_prompt="" | |
fi | |
echo "(${rev_prompt}${sym_prompt})" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment