Hegel2011的博客

读书 - 工作 - 生活 - 笔记

给shell加上显示git的分支

function find_git_branch {

local dir=. head
until [ "$dir" -ef / ]; do
    if [ -f "$dir/.git/HEAD" ]; then
        head=$(< "$dir/.git/HEAD")
        if [[ $head == ref:\ refs/heads/* ]]; then

        head=$(< "$dir/.git/HEAD")
        if [[ $head == ref:\ refs/heads/* ]]; then
            git_branch=" (${head#*/*/})"
        elif [[ $head != '' ]]; then
            git_branch=' (detached)'
        else
            git_branch=' (unknown)'
        fi
        return
    fi
    dir="../$dir"
done
git_branch=''

} PROMPT_COMMAND=”find_git_branch; $PROMPT_COMMAND” green=$’\e[1;32m’ magenta=$’\e[1;35m’ normal_colours=$’\e[m’

PS1=”[$green]\u@\h:\w[$magenta]\$git_branch[$green]\$[$normal_colours] ” 秘密在于PROMPT_COMMAND,以保证每次都会执行以下find_git_branch函数

Included file 'twitter_sharing.html' not found in _includes directory