From 8a31940ec0800f3f28b8927423087a22f6e24469 Mon Sep 17 00:00:00 2001 From: sonnym Date: Sun, 25 Mar 2012 16:10:32 -0400 Subject: [PATCH 1/3] make git_prompt_info aware of detached HEAD this commit also checks for tag names once the repository has been determined to be in a detached HEAD state --- lib/git.zsh | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/git.zsh b/lib/git.zsh index fb4ad8ca6..86f46ed5d 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -1,23 +1,41 @@ # get the name of the branch we are on function git_prompt_info() { - ref=$(git symbolic-ref HEAD 2> /dev/null) || return + ref=$(git symbolic-ref HEAD 2> /dev/null) + if [ -z $ref ]; then + # check if in detached HEAD mode + branch_name=$(git status -sb $(git_submodule_syntax) 2> /dev/null) + if [ $branch_name = "## HEAD (no branch)" ]; then + # set tag name if found + tag_name=$(git name-rev --name-only $(git --no-pager show --stat 2> /dev/null | head -n 1 | awk '{ print $2 }') 2> /dev/null) + if [ -z $tag_name ]; then + ref="detached HEAD" + else + ref=$tag_name + fi + else + return + fi + fi echo "$ZSH_THEME_GIT_PROMPT_PREFIX${ref#refs/heads/}$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_SUFFIX" } # Checks if working tree is dirty parse_git_dirty() { - local SUBMODULE_SYNTAX='' - if [[ $POST_1_7_2_GIT -gt 0 ]]; then - SUBMODULE_SYNTAX="--ignore-submodules=dirty" - fi - if [[ -n $(git status -s ${SUBMODULE_SYNTAX} 2> /dev/null) ]]; then + if [[ -n $(git status -s $(git_submodule_syntax) 2> /dev/null) ]]; then echo "$ZSH_THEME_GIT_PROMPT_DIRTY" else echo "$ZSH_THEME_GIT_PROMPT_CLEAN" fi } +git_submodule_syntax() { + if [[ $POST_1_7_2_GIT -gt 0 ]]; then + echo "--ignore-submodules=dirty" + else + echo "" + fi +} # Checks if there are commits ahead from remote function git_prompt_ahead() { From a0278ad0404e8f5f3f76dbc2fb5617d1e2845ddc Mon Sep 17 00:00:00 2001 From: sonnym Date: Fri, 30 Mar 2012 13:25:39 -0400 Subject: [PATCH 2/3] fix broken equality check when outside git repo --- lib/git.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/git.zsh b/lib/git.zsh index 86f46ed5d..915575246 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -4,7 +4,7 @@ function git_prompt_info() { if [ -z $ref ]; then # check if in detached HEAD mode branch_name=$(git status -sb $(git_submodule_syntax) 2> /dev/null) - if [ $branch_name = "## HEAD (no branch)" ]; then + if [ "$branch_name" = "## HEAD (no branch)" ]; then # set tag name if found tag_name=$(git name-rev --name-only $(git --no-pager show --stat 2> /dev/null | head -n 1 | awk '{ print $2 }') 2> /dev/null) if [ -z $tag_name ]; then From 7ba3cbcfa3c6b87a339c6d49281f146823e08ebc Mon Sep 17 00:00:00 2001 From: sonnym Date: Tue, 10 Apr 2012 17:35:19 -0400 Subject: [PATCH 3/3] ignore subsequent lines of git status as they may break the equality check by including changes to the repository --- lib/git.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/git.zsh b/lib/git.zsh index 915575246..b0d99bd2b 100644 --- a/lib/git.zsh +++ b/lib/git.zsh @@ -3,7 +3,7 @@ function git_prompt_info() { ref=$(git symbolic-ref HEAD 2> /dev/null) if [ -z $ref ]; then # check if in detached HEAD mode - branch_name=$(git status -sb $(git_submodule_syntax) 2> /dev/null) + branch_name=$(git status -sb $(git_submodule_syntax) 2> /dev/null | head -n 1) if [ "$branch_name" = "## HEAD (no branch)" ]; then # set tag name if found tag_name=$(git name-rev --name-only $(git --no-pager show --stat 2> /dev/null | head -n 1 | awk '{ print $2 }') 2> /dev/null)