2017-05-29 63 views
1

我想在下面的終端上通過git repo顯示當前分支狀態(如*表示已修改)。但是,即使切換到git repo,它總是顯示以下內容。在Mac終端上顯示git狀態不能用於自定義bash配置文件

ᴾᴋᴹɴ iFeelNewsBot master wrong... ↪ 

當我在終端打開一個新的標籤中,顯示了進一步看到當前的Git分支正確的文本渲染。應該如何看待

ᴾᴋᴹɴ iFeelNewsBot master * ↪ 

我定製的bash配置文件代碼如下

# user name and color 
USER_NAME='mr.universe'; 
TRAINER_TITLE='ᴾᴋᴹɴ' 
USER_NAME_COLOR='\[\033[00m\]'; 
END_COLOR='\e[0m'; 

# \W = current working path 
# \u = user 
function parse_git_dirty { 
    gitStatus=$(git status 2> /dev/null | tail -n1) 
    clean="nothing to commit, working directory clean" 
    if [[ -d "./.git" ]] 
    then 
    [[ $gitStatus != $clean ]] && echo "*" || echo "=" 
    elif [[ ! -d "./.git" ]] 
    then 
    echo "wrong...." 
    else 
    echo "STOP" 
    fi 
} 

function parse_git_branch { 
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/" 
} 

export PS1="\[\033[40;02m\]$TRAINER_TITLE \W \[\033[36;02m\]\[\033[29;00m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2) $(parse_git_dirty) ↪ " 
+0

看起來'parse_git_branch'函數從未被使用。 – ThomasW

回答

0

你假設一個Git倉庫的每個目錄中有.git,但只有一個倉庫的頂級目錄中有一個.git目錄。因此,第一個if失敗,elif檢查.git目錄是否不存在。看起來結果是真的,它打印出'錯誤'。

0

你可以檢查,而不是如果你是inside a working tree of a git repo

inside_git_repo="$(git -C $PWD rev-parse --is-inside-work-tree 2>/dev/null)" 

if [ "$inside_git_repo" ]; then 
    echo "inside git repo" 
else 
    echo "not in git repo" 
fi 

這比找一個.git文件夾更可靠。

+0

在'git repo中顯示'''工作,但是當我換到一個新的非git目錄時''不在git repo中'不會顯示,直到我打開一個新標籤或刷新我的終端 –

+0

通過交換到一個新的分支,你的意思是'git checkout aNewBranch'? 「刷新終端」是什麼意思? – VonC

+0

我的錯誤我的意思是cd到一個新的非git目錄。我的意思是關閉並重新打開我的終端。 –