From 1b027dafefb2686db4c63cbc4397b9441b52c9cc Mon Sep 17 00:00:00 2001 From: eacousineau Date: Tue, 19 Mar 2013 14:05:02 -0500 Subject: [PATCH 1/4] Trying out some mods to this. Seems like speed is an issue with git rev-parse --- prompt.sh | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/prompt.sh b/prompt.sh index 5385754..43d4943 100644 --- a/prompt.sh +++ b/prompt.sh @@ -1,20 +1,14 @@ 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 - git_branch=" (${head#*/*/})" - elif [[ $head != '' ]]; then - git_branch=' (detached)' - else - git_branch=' (unknown)' - fi - return - fi - dir="../$dir" - done - git_branch='' + local dir=. head branch + if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) + then + if [[ -z "$branch" ]]; then + branch='detached' + fi + git_branch=" ($branch)" + else + git_branch="" + fi } function find_git_dirty { st=$(git status 2>/dev/null | tail -n 1) @@ -32,4 +26,4 @@ PROMPT_COMMAND="find_git_branch; find_git_dirty; $PROMPT_COMMAND" # export PS1="\u@\h \w\[$txtcyn\]\$git_branch\[$txtylw\]\$git_dirty\[$txtrst\]\$ " # Default Git enabled root prompt (for use with "sudo -s") -# export SUDO_PS1="\[$bakred\]\u@\h\[$txtrst\] \w\$ " \ No newline at end of file +# export SUDO_PS1="\[$bakred\]\u@\h\[$txtrst\] \w\$ " From 226e2fc80b0f4aec9dd45570c9e7bba1ee11c127 Mon Sep 17 00:00:00 2001 From: eacousineau Date: Tue, 19 Mar 2013 14:20:51 -0500 Subject: [PATCH 2/4] Changed find_git_branch to use rev-parse and find_git_dirty to use diff-files for speed (I think). Also removed preceding space in front of branch, and changed function syntax to to what the Git shell scripts use. --- prompt.sh | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/prompt.sh b/prompt.sh index 43d4943..67e66b0 100644 --- a/prompt.sh +++ b/prompt.sh @@ -1,29 +1,39 @@ -function find_git_branch { - local dir=. head branch +find_git_branch() { + local branch + # Based on: http://stackoverflow.com/a/13003854/170413 if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) then if [[ -z "$branch" ]]; then branch='detached' fi - git_branch=" ($branch)" + git_branch="($branch)" else git_branch="" fi } -function find_git_dirty { - st=$(git status 2>/dev/null | tail -n 1) - if [[ $st == "" ]]; then - git_dirty='' - elif [[ $st == "nothing to commit (working directory clean)" ]]; then - git_dirty='' - else - git_dirty='*' - fi + +find_git_dirty() { + if [[ -z "$git_branch" ]] + then + git_dirty='' + else + # Based on: http://stackoverflow.com/a/2659808/170413 + if git diff-files --quiet + then + git_dirty='' + else + git_dirty='*' + fi + fi } + PROMPT_COMMAND="find_git_branch; find_git_dirty; $PROMPT_COMMAND" # Default Git enabled prompt with dirty state # export PS1="\u@\h \w\[$txtcyn\]\$git_branch\[$txtylw\]\$git_dirty\[$txtrst\]\$ " +# Another variant: +# export PS1="\[$bldgrn\]\u@\h\[$txtrst\] \w\[$bldylw\]\$git_branch\[$txtcyn\]\$git_dirty\[$txtrst\]\$ " + # Default Git enabled root prompt (for use with "sudo -s") # export SUDO_PS1="\[$bakred\]\u@\h\[$txtrst\] \w\$ " From c58bee1f677e3ed62a86c75573a504054e3b82f4 Mon Sep 17 00:00:00 2001 From: eacousineau Date: Tue, 19 Mar 2013 14:51:29 -0500 Subject: [PATCH 3/4] find_git_dirty: Added in check between staged and unstaged changes --- prompt.sh | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/prompt.sh b/prompt.sh index 67e66b0..d60fe9f 100644 --- a/prompt.sh +++ b/prompt.sh @@ -3,8 +3,8 @@ find_git_branch() { # Based on: http://stackoverflow.com/a/13003854/170413 if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) then - if [[ -z "$branch" ]]; then - branch='detached' + if [[ "$branch" == "HEAD" ]]; then + branch='(detached head)' fi git_branch="($branch)" else @@ -20,9 +20,15 @@ find_git_dirty() { # Based on: http://stackoverflow.com/a/2659808/170413 if git diff-files --quiet then - git_dirty='' + if git diff-index --quiet --cached HEAD + then + git_dirty='' + else + # Can't figure out different colors + git_dirty="^" + fi else - git_dirty='*' + git_dirty="*" fi fi } @@ -30,7 +36,7 @@ find_git_dirty() { PROMPT_COMMAND="find_git_branch; find_git_dirty; $PROMPT_COMMAND" # Default Git enabled prompt with dirty state -# export PS1="\u@\h \w\[$txtcyn\]\$git_branch\[$txtylw\]\$git_dirty\[$txtrst\]\$ " +# export PS1="\u@\h \w\[$txtcyn\]\$git_branch\$git_dirty\[$txtrst\]\$ " # Another variant: # export PS1="\[$bldgrn\]\u@\h\[$txtrst\] \w\[$bldylw\]\$git_branch\[$txtcyn\]\$git_dirty\[$txtrst\]\$ " From 8c7e3069cd22273c4dca9fcc8bbd2350d9af867f Mon Sep 17 00:00:00 2001 From: eacousineau Date: Wed, 20 Mar 2013 15:49:43 -0500 Subject: [PATCH 4/4] Added in some redirection to check and hide error message in find_git_dirty --- prompt.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/prompt.sh b/prompt.sh index d60fe9f..cfe5e94 100644 --- a/prompt.sh +++ b/prompt.sh @@ -18,7 +18,8 @@ find_git_dirty() { git_dirty='' else # Based on: http://stackoverflow.com/a/2659808/170413 - if git diff-files --quiet + local err + if err=$(git diff-files --quiet 2>&1) then if git diff-index --quiet --cached HEAD then @@ -27,6 +28,11 @@ find_git_dirty() { # Can't figure out different colors git_dirty="^" fi + elif [ -n "$err" ] + then + # Some error - most likely that it was run within $GIT_DIR + # Resolve repo root instead? `git rev-parse --git-dir` does not work, nor does the 'git root' alias trick + git_dirty="" else git_dirty="*" fi