Translate

2017年8月7日月曜日

Amazon Linux版 プロンプトに現在のgitローカルリポジトリ/ブランチを表示する

http://tumulustone.blogspot.jp/2013/10/git.html に記述した記事そのままだとエスケープシーケンスが上手く動かなくなっていたので、修正版を。

cat ~/bin/gitbranch.bash
#!/bin/bash
# echo git branch for color PS1

name=`git rev-parse --abbrev-ref HEAD 2> /dev/null`
if [[ -z $name ]]
then
        exit
fi

st=`git status`
repos=`git remote -v | \
grep -o '\/.*' | \
sed -e "s/[ \s\/]//g" | \
sed -e "s/(fetch)//g" | \
sed -e "s/\.git//g" | \
awk "NR==1{print}"`
if [[ -n `echo $st | grep "nothing to commit"` ]]
then
    color="\[\e[32;1m\]"
else
    color="\[\e[31;1m\]"
fi
reset_color="\[\e[0m\]"
echo -E $color[$repos $name]$reset_color