2012-09-28 41 views
0

混帳回購協議的髒狀態,如果我用的git回購運行此命令行1.4.3在命令行中,

git status 2> /dev/null | grep -iw 'modified' 

輸出我得到

# modified: .rvmrc 

,所以我的假設是,如果我插入這在進入if語句我會得到真正的將要執行的代碼

行了,但是當我創建函數作爲.bash_profile中的一部分,這裏是我的代碼有:

## Prompt ## 
path="$white\w$reset" 
folder_name="$white\W$reset" 
git_branch="\$(git branch 2>/dev/null | grep -e '\* ' | sed 's/^..\(.*\)/$red(\1)$reset/')" 
rvm="$green(\$(~/.rvm/bin/rvm-prompt i v p))$reset" 

# Checks if working tree is dirty 
function _git_st { 
    if [[ $(git status 2> /dev/null | grep -iw 'modified') ]]; then 
    echo "[x]" 
    fi 
} 
git_st="$yellow`_git_st`$reset" 

PS1="$rvm $folder_name $git_branch $git_st \$ " 

X沒有迴應....我有點失落,不知道我做錯了什麼。

這是出把我得到:

(ruby-1.9.2-p290) folder-git (master) $ git status 
# On branch master 
# Changes not staged for commit: 
# (use "git add <file>..." to update what will be committed) 
# (use "git checkout -- <file>..." to discard changes in working directory) 
# 
# modified: .rvmrc 
# 
no changes added to commit (use "git add" and/or "git commit -a") 
(ruby-1.9.2-p290) folder-git (master) $ 
+1

另外,你的'_git_st'函數比它需要的複雜一點。 '如果git status 2>/dev/null | grep -qiw'修改';那麼'應該工作相同,而不需要捕獲輸出並檢查它是否爲空。 – chepner

+0

謝謝@chepner明天我會試試這個。 – GnrlBzik

回答

1

這樣做的原因是因爲你使用的是可變git_st設置PS1這將在設定PS1的時間進行評估。您可以嘗試的是調用函數_git_st而不是使用git_st而不是使用git_st,即嘗試以下行:
PS1="$rvm $folder_name $git_branch \$(_git_st) \$ "
另外,您可能有興趣知道git的新版本提供的功能以及提供此類實用程序的bash完成。如果可用,你可以看看__git_ps1函數。
希望這有助於!
P.S .: Here是SO帖子,它可能會提供一些使用__git_ps1的指針。 This是Google的快速搜索結果。

+0

謝謝@ another.anon.coward指出我的缺陷,將嘗試一點。 – GnrlBzik

+0

那就是它的工作。非常感謝。我會稍微檢查一下鏈接。 – GnrlBzik