文章

bashrc_增加git信息显示_3

此版本功能

1. 会识别是否在仓库中,如果不在则不返回任何东西,如果在返回对应分支

2. 如果当前环境文件无更改,只返回分支名;如果有任何的文件更改会返回对应文件数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function parse_git_dirty {
    local git_status=$(git status 2> /dev/null);
    local modify_file_num=0;
    local commit_file_num=0;
    local change_file_num=0;
    local untracked_file_num=0;

    if [[ "${git_status}" =~ "nothing to commit, working tree clean" ]]; then
        echo " []"
        exit 0
    fi
    if [[ "${git_status}" =~ "Changes not staged for commit" ]]; then
        # 有修改的文件
        modify_file_num=$( git diff --name-only 2> /dev/null | wc -l | tr -d ' ' )
    fi
    if [[ "${git_status}" =~ "Changes to be committed" ]]; then
        # 有要提交的文件
        commit_file_num=$( git diff --cached --name-only 2> /dev/null | wc -l | tr -d ' ' )
    fi
    if [[ "${git_status}" =~ "Untracked files" ]]; then
        # 存在改动的文件
        change_file_num=$( cd $( git rev-parse --show-toplevel ); git ls-files --others --exclude-standard 2> /dev/null | wc -l | tr -d ' '  )
    fi
    echo " [A:${commit_file_num}|M:${modify_file_num}|+:${change_file_num}]";                                                                                                                                       
}
function git_branch()
{   
    branch=$( git rev-parse --abbrev-ref HEAD 2>/dev/null )
    if [ "${branch}" != "" ]; then
        if [ "${branch}" = "(no branch)" ]; then
            branch="($( git rev-parse --short HEAD )...)"
        fi
        echo -e "⎇ \[\033[01;33m\]${branch}\[\033[01;31m\]$(parse_git_dirty)"
    fi                                         
}

function prompt_command {
    PS1="\n\[\033[01;36m\][\u@\h\[\033[01;32m\] \w\[\033[01;36m\]]\[\033[01;36m\] $(git_branch)\[\033[00m\]\n\\$\[\033[00m\] "
}

#后面与其他的一样
...
...
...
本文由作者按照 CC BY 4.0 进行授权