Linux shell에서 git prompt 추가하기 (git branch 보이기, git status에 따라 색상 변경)

HANJIN·2019년 12월 12일
0

shell

목록 보기
1/1

사용하는 쉘에따라
vi ~/.쉘이름rc

ex) bash shell
vi ~/.bashrc

아래 코드 추가

################################### git prompt
c_cyan=`tput setaf 6`
c_red=`tput setaf 1`
c_green=`tput setaf 2`
c_sgr0=`tput sgr0`
parse_git_branch ()
{
        if git rev-parse --git-dir >/dev/null 2>&1
        then
                gitver=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
        else return 0
        fi
        echo -e $gitver
}

branch_color ()
{
        if git rev-parse --git-dir >/dev/null 2>&1
        then
                color=""
                if git diff --quiet 2>/dev/null >&2
                then
                        color="${c_green}"
                else color=${c_red}
                fi
        else return 0
        fi
        echo -ne $color
}

if [ "$color_prompt" = yes ]; then
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else # PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\[${c_sgr0}\] (\[$(branch_color)\]$(parse_git_branch)\[${c_sgr0}\])\$ '
fi
########################
profile
소프트웨어 엔지니어.

0개의 댓글