{"thread":{"id":"34166","subject":"[PATCH/RFC 0/3] enable color prompt in non-pcmode","startedAt":"2013-06-16T17:32:09Z","lastAt":"2013-06-18T02:19:46Z","messageCount":9,"participants":["Eduardo R. D'Avila","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"221008","messageId":"cover.1371402672.git.erdavila@gmail.com","threadId":"34166","inReplyTo":null,"subject":"[PATCH/RFC 0/3] enable color prompt in non-pcmode","fromName":"Eduardo R. D'Avila","fromEmail":"erdavila@gmail.com","sentAt":"2013-06-16T17:32:09Z","receivedAt":"2013-06-16T17:32:09Z","isPatch":true,"sender":{"key":"erdavila@gmail.com","avatar":"https://avatars.githubusercontent.com/u/341488?v=4"},"body":"The use of colors in a prompt is only possible in\npcmode (using the variable PROMPT_COMMAND).\n\nMake color prompt work in non-pcmode (using the\nvariable PS1) for both Bash and ZSH.\n\nThis requires editing __git_ps1() and __git_ps1_colorize_gitstring(),\nwhich have duplicate code to handle the prompt\ngitstring and color codes.\n\nPrior to enable colors in non-pcmode, refactor\n__git_ps1() and __git_ps1_colorize_gitstring().\n\nEduardo R. D'Avila (3):\n  t9903: add tests for git-prompt pcmode\n  git-prompt.sh: refactor colored prompt code\n  git-prompt.sh: enable color prompt in non-pcmode\n\n contrib/completion/git-prompt.sh |  91 +++++--------\n t/t9903-bash-prompt.sh           | 269 +++++++++++++++++++++++++++++++++++++++\n 2 files changed, 301 insertions(+), 59 deletions(-)\n\n-- \n1.8.3.1.440.g82707f8\n"},{"id":"221011","messageId":"c4c2a37da989c8c39e8cd6be2370ce34dd0675a6.1371402672.git.erdavila@gmail.com","threadId":"34166","inReplyTo":"cover.1371402672.git.erdavila@gmail.com","subject":"[PATCH/RFC 1/3] t9903: add tests for git-prompt pcmode","fromName":"Eduardo R. D'Avila","fromEmail":"erdavila@gmail.com","sentAt":"2013-06-16T17:32:10Z","receivedAt":"2013-06-16T17:32:10Z","isPatch":true,"sender":{"key":"erdavila@gmail.com","avatar":"https://avatars.githubusercontent.com/u/341488?v=4"},"body":"git-prompt.sh lacks tests for PROMPT_COMMAND mode.\n\nAdd tests for:\n* pcmode prompt without colors\n* pcmode prompt with colors for bash\n* pcmode prompt with colors for zsh\n\nHaving these tests enables an upcoming refactor in\na safe way.\n\nSigned-off-by: Eduardo R. D'Avila <erdavila@gmail.com>\n---\n250\t0\tt/t9903-bash-prompt.sh\n t/t9903-bash-prompt.sh | 250 +++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 250 insertions(+)\n\ndiff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh\nindex 15521cc..ebca9de 100755\n--- a/t/t9903-bash-prompt.sh\n+++ b/t/t9903-bash-prompt.sh\n@@ -535,4 +535,254 @@ test_expect_success 'prompt - format string starting with dash' '\n \ttest_cmp expected \"$actual\"\n '\n \n+test_expect_success 'prompt - pc mode' '\n+\tprintf \"BEFORE: (master):AFTER\" > expected &&\n+\tprintf \"\" > expected_output &&\n+\t(\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" > \"$actual\" &&\n+\t\ttest_cmp expected_output \"$actual\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - branch name' '\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\]\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" > \"$actual\"\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - detached head' '\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[31m\\\\\\](%s...)\\\\\\[\\\\\\e[0m\\\\\\]\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" $(git log -1 --format=\"%h\" b1^) > expected &&\n+\tgit checkout b1^ &&\n+\ttest_when_finished \"git checkout master\" &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[31m\\\\\\]*\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\techo \"dirty\" > file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\t(\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[32m\\\\\\]+\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\techo \"dirty\" > file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\tgit add -u &&\n+\t(\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[31m\\\\\\]*\\\\\\[\\\\\\e[32m\\\\\\]+\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\techo \"dirty index\" > file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\tgit add -u &&\n+\techo \"dirty worktree\" > file &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[32m\\\\\\]#\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\t(\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tcd otherrepo &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - inside .git directory' '\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]GIT_DIR!\\\\\\[\\\\\\e[0m\\\\\\]\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\techo \"dirty\" > file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\t(\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tcd .git &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - stash status indicator' '\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[1;34m\\\\\\]$\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\techo 2 >file &&\n+\tgit stash &&\n+\ttest_when_finished \"git stash drop\" &&\n+\t(\n+\t\tGIT_PS1_SHOWSTASHSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[31m\\\\\\]%%\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\t(\n+\t\tGIT_PS1_SHOWUNTRACKEDFILES=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - branch name' '\n+\tprintf \"BEFORE: (%%F{green}master%%f%%f):AFTER\" > expected &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" > \"$actual\"\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - detached head' '\n+\tprintf \"BEFORE: (%%F{red}(%s...)%%f%%f):AFTER\" $(git log -1 --format=\"%h\" b1^) > expected &&\n+\tgit checkout b1^ &&\n+\ttest_when_finished \"git checkout master\" &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty worktree' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{red}*%%f):AFTER\" > expected &&\n+\techo \"dirty\" > file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{green}+%%f):AFTER\" > expected &&\n+\techo \"dirty\" > file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\tgit add -u &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index and worktree' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{red}*%%F{green}+%%f):AFTER\" > expected &&\n+\techo \"dirty index\" > file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\tgit add -u &&\n+\techo \"dirty worktree\" > file &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - before root commit' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{green}#%%f):AFTER\" > expected &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tcd otherrepo &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - inside .git directory' '\n+\tprintf \"BEFORE: (%%F{green}GIT_DIR!%%f%%f):AFTER\" > expected &&\n+\techo \"dirty\" > file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tcd .git &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - stash status indicator' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{blue}$%%f):AFTER\" > expected &&\n+\techo 2 >file &&\n+\tgit stash &&\n+\ttest_when_finished \"git stash drop\" &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWSTASHSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - untracked files status indicator' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{red}%%%%%%f):AFTER\" > expected &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWUNTRACKEDFILES=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n test_done\n-- \n1.8.3.1.440.g82707f8\n"},{"id":"221009","messageId":"4e4fb1731cc8e7890d917fa4d3d6b522a46989ff.1371402672.git.erdavila@gmail.com","threadId":"34166","inReplyTo":"cover.1371402672.git.erdavila@gmail.com","subject":"[PATCH/RFC 2/3] git-prompt.sh: refactor colored prompt code","fromName":"Eduardo R. D'Avila","fromEmail":"erdavila@gmail.com","sentAt":"2013-06-16T17:32:11Z","receivedAt":"2013-06-16T17:32:11Z","isPatch":true,"sender":{"key":"erdavila@gmail.com","avatar":"https://avatars.githubusercontent.com/u/341488?v=4"},"body":"Remove duplication of logic to build gitstring.\n\n__git_ps1_colorize_gitstring() sets color codes and\nbuilds the prompt gitstring. It has duplicated code\nto handle color codes for bash and zsh shells.\n__git_ps1() also has duplicated logic to build the\nprompt gitstring.\n\nRemove duplication of logic to build gitstring in\n__git_ps1_colorize_gitstring() and __git_ps1().\n\nLeave in __git_ps1_colorize_gitstring() only logic\nto set color codes.\n\nSigned-off-by: Eduardo R. D'Avila <erdavila@gmail.com>\n---\n26\t59\tcontrib/completion/git-prompt.sh\n6\t6\tt/t9903-bash-prompt.sh\n contrib/completion/git-prompt.sh | 85 ++++++++++++----------------------------\n t/t9903-bash-prompt.sh           | 12 +++---\n 2 files changed, 32 insertions(+), 65 deletions(-)\n\ndiff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh\nindex 86a4f3f..70515cc 100644\n--- a/contrib/completion/git-prompt.sh\n+++ b/contrib/completion/git-prompt.sh\n@@ -225,8 +225,8 @@ __git_ps1_show_upstream ()\n }\n \n # Helper function that is meant to be called from __git_ps1.  It\n-# builds up a gitstring injecting color codes into the appropriate\n-# places.\n+# injects color codes into the appropriate gitstring variables used\n+# to build a gitstring.\n __git_ps1_colorize_gitstring ()\n {\n \tif [[ -n ${ZSH_VERSION-} ]]; then\n@@ -234,74 +234,40 @@ __git_ps1_colorize_gitstring ()\n \t\tlocal c_green='%F{green}'\n \t\tlocal c_lblue='%F{blue}'\n \t\tlocal c_clear='%f'\n-\t\tlocal bad_color=$c_red\n-\t\tlocal ok_color=$c_green\n-\t\tlocal branch_color=\"$c_clear\"\n-\t\tlocal flags_color=\"$c_lblue\"\n-\t\tlocal branchstring=\"$c${b##refs/heads/}\"\n-\n-\t\tif [ $detached = no ]; then\n-\t\t\tbranch_color=\"$ok_color\"\n-\t\telse\n-\t\t\tbranch_color=\"$bad_color\"\n-\t\tfi\n-\n-\t\tgitstring=\"$branch_color$branchstring$c_clear\"\n-\n-\t\tif [ -n \"$w$i$s$u$r$p\" ]; then\n-\t\t\tgitstring=\"$gitstring$z\"\n-\t\tfi\n-\t\tif [ \"$w\" = \"*\" ]; then\n-\t\t\tgitstring=\"$gitstring$bad_color$w\"\n-\t\tfi\n-\t\tif [ -n \"$i\" ]; then\n-\t\t\tgitstring=\"$gitstring$ok_color$i\"\n-\t\tfi\n-\t\tif [ -n \"$s\" ]; then\n-\t\t\tgitstring=\"$gitstring$flags_color$s\"\n-\t\tfi\n-\t\tif [ -n \"$u\" ]; then\n-\t\t\tgitstring=\"$gitstring$bad_color$u\"\n-\t\tfi\n-\t\tgitstring=\"$gitstring$c_clear$r$p\"\n-\t\treturn\n+\telse\n+\t\t# Using \\[ and \\] around colors\n+\t\t# is necessary to prevent wrapping issues!\n+\t\tlocal c_red='\\[\\e[31m\\]'\n+\t\tlocal c_green='\\[\\e[32m\\]'\n+\t\tlocal c_lblue='\\[\\e[1;34m\\]'\n+\t\tlocal c_clear='\\[\\e[0m\\]'\n \tfi\n-\tlocal c_red='\\e[31m'\n-\tlocal c_green='\\e[32m'\n-\tlocal c_lblue='\\e[1;34m'\n-\tlocal c_clear='\\e[0m'\n \tlocal bad_color=$c_red\n \tlocal ok_color=$c_green\n-\tlocal branch_color=\"$c_clear\"\n \tlocal flags_color=\"$c_lblue\"\n-\tlocal branchstring=\"$c${b##refs/heads/}\"\n \n+\tlocal branch_color=\"\"\n \tif [ $detached = no ]; then\n \t\tbranch_color=\"$ok_color\"\n \telse\n \t\tbranch_color=\"$bad_color\"\n \tfi\n+\tc=\"$branch_color$c\"\n \n-\t# Setting gitstring directly with \\[ and \\] around colors\n-\t# is necessary to prevent wrapping issues!\n-\tgitstring=\"\\[$branch_color\\]$branchstring\\[$c_clear\\]\"\n-\n-\tif [ -n \"$w$i$s$u$r$p\" ]; then\n-\t\tgitstring=\"$gitstring$z\"\n-\tfi\n+\tz=\"$c_clear$z\"\n \tif [ \"$w\" = \"*\" ]; then\n-\t\tgitstring=\"$gitstring\\[$bad_color\\]$w\"\n+\t\tw=\"$bad_color$w\"\n \tfi\n \tif [ -n \"$i\" ]; then\n-\t\tgitstring=\"$gitstring\\[$ok_color\\]$i\"\n+\t\ti=\"$ok_color$i\"\n \tfi\n \tif [ -n \"$s\" ]; then\n-\t\tgitstring=\"$gitstring\\[$flags_color\\]$s\"\n+\t\ts=\"$flags_color$s\"\n \tfi\n \tif [ -n \"$u\" ]; then\n-\t\tgitstring=\"$gitstring\\[$bad_color\\]$u\"\n+\t\tu=\"$bad_color$u\"\n \tfi\n-\tgitstring=\"$gitstring\\[$c_clear\\]$r$p\"\n+\tr=\"$c_clear$r\"\n }\n \n # __git_ps1 accepts 0 or 1 arguments (i.e., format string)\n@@ -443,19 +409,20 @@ __git_ps1 ()\n \t\tfi\n \n \t\tlocal z=\"${GIT_PS1_STATESEPARATOR-\" \"}\"\n+\n+\t\t# NO color option unless in PROMPT_COMMAND mode\n+\t\tif [ $pcmode = yes ] && [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n+\t\t\t__git_ps1_colorize_gitstring\n+\t\tfi\n+\n \t\tlocal f=\"$w$i$s$u\"\n+\t\tlocal gitstring=\"$c${b##refs/heads/}${f:+$z$f}$r$p\"\n+\n \t\tif [ $pcmode = yes ]; then\n-\t\t\tlocal gitstring=\n-\t\t\tif [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n-\t\t\t\t__git_ps1_colorize_gitstring\n-\t\t\telse\n-\t\t\t\tgitstring=\"$c${b##refs/heads/}${f:+$z$f}$r$p\"\n-\t\t\tfi\n \t\t\tgitstring=$(printf -- \"$printf_format\" \"$gitstring\")\n \t\t\tPS1=\"$ps1pc_start$gitstring$ps1pc_end\"\n \t\telse\n-\t\t\t# NO color option unless in PROMPT_COMMAND mode\n-\t\t\tprintf -- \"$printf_format\" \"$c${b##refs/heads/}${f:+$z$f}$r$p\"\n+\t\t\tprintf -- \"$printf_format\" \"$gitstring\"\n \t\tfi\n \tfi\n }\ndiff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh\nindex ebca9de..22484c1 100755\n--- a/t/t9903-bash-prompt.sh\n+++ b/t/t9903-bash-prompt.sh\n@@ -547,7 +547,7 @@ test_expect_success 'prompt - pc mode' '\n '\n \n test_expect_success 'prompt - bash color pc mode - branch name' '\n-\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\]\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n \t(\n \t\tGIT_PS1_SHOWCOLORHINTS=y &&\n \t\t__git_ps1 \"BEFORE:\" \":AFTER\" > \"$actual\"\n@@ -557,7 +557,7 @@ test_expect_success 'prompt - bash color pc mode - branch name' '\n '\n \n test_expect_success 'prompt - bash color pc mode - detached head' '\n-\tprintf \"BEFORE: (\\\\\\[\\\\\\e[31m\\\\\\](%s...)\\\\\\[\\\\\\e[0m\\\\\\]\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" $(git log -1 --format=\"%h\" b1^) > expected &&\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[31m\\\\\\](%s...)\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" $(git log -1 --format=\"%h\" b1^) > expected &&\n \tgit checkout b1^ &&\n \ttest_when_finished \"git checkout master\" &&\n \t(\n@@ -623,7 +623,7 @@ test_expect_success 'prompt - bash color pc mode - dirty status indicator - befo\n '\n \n test_expect_success 'prompt - bash color pc mode - inside .git directory' '\n-\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]GIT_DIR!\\\\\\[\\\\\\e[0m\\\\\\]\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n+\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]GIT_DIR!\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n \techo \"dirty\" > file &&\n \ttest_when_finished \"git reset --hard\" &&\n \t(\n@@ -662,7 +662,7 @@ test_expect_success 'prompt - bash color pc mode - untracked files status indica\n '\n \n test_expect_success 'prompt - zsh color pc mode - branch name' '\n-\tprintf \"BEFORE: (%%F{green}master%%f%%f):AFTER\" > expected &&\n+\tprintf \"BEFORE: (%%F{green}master%%f):AFTER\" > expected &&\n \t(\n \t\tZSH_VERSION=5.0.0 &&\n \t\tGIT_PS1_SHOWCOLORHINTS=y &&\n@@ -673,7 +673,7 @@ test_expect_success 'prompt - zsh color pc mode - branch name' '\n '\n \n test_expect_success 'prompt - zsh color pc mode - detached head' '\n-\tprintf \"BEFORE: (%%F{red}(%s...)%%f%%f):AFTER\" $(git log -1 --format=\"%h\" b1^) > expected &&\n+\tprintf \"BEFORE: (%%F{red}(%s...)%%f):AFTER\" $(git log -1 --format=\"%h\" b1^) > expected &&\n \tgit checkout b1^ &&\n \ttest_when_finished \"git checkout master\" &&\n \t(\n@@ -744,7 +744,7 @@ test_expect_success 'prompt - zsh color pc mode - dirty status indicator - befor\n '\n \n test_expect_success 'prompt - zsh color pc mode - inside .git directory' '\n-\tprintf \"BEFORE: (%%F{green}GIT_DIR!%%f%%f):AFTER\" > expected &&\n+\tprintf \"BEFORE: (%%F{green}GIT_DIR!%%f):AFTER\" > expected &&\n \techo \"dirty\" > file &&\n \ttest_when_finished \"git reset --hard\" &&\n \t(\n-- \n1.8.3.1.440.g82707f8\n"},{"id":"221010","messageId":"c875c17e4745d57bd26b680e82aa221659faf3cd.1371402672.git.erdavila@gmail.com","threadId":"34166","inReplyTo":"cover.1371402672.git.erdavila@gmail.com","subject":"[PATCH/RFC 3/3] git-prompt.sh: enable color prompt in non-pcmode","fromName":"Eduardo R. D'Avila","fromEmail":"erdavila@gmail.com","sentAt":"2013-06-16T17:32:12Z","receivedAt":"2013-06-16T17:32:12Z","isPatch":true,"sender":{"key":"erdavila@gmail.com","avatar":"https://avatars.githubusercontent.com/u/341488?v=4"},"body":"The use of colors in a prompt is only possible in\npcmode (using the variable PROMPT_COMMAND).\n\nEnable color prompt in non-pcmode (using the variable\nPS1) for both Bash and ZSH.\n\nSigned-off-by: Eduardo R. D'Avila <erdavila@gmail.com>\n---\n15\t9\tcontrib/completion/git-prompt.sh\n19\t0\tt/t9903-bash-prompt.sh\n contrib/completion/git-prompt.sh | 24 +++++++++++++++---------\n t/t9903-bash-prompt.sh           | 19 +++++++++++++++++++\n 2 files changed, 34 insertions(+), 9 deletions(-)\n\ndiff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh\nindex 70515cc..c5c75e7 100644\n--- a/contrib/completion/git-prompt.sh\n+++ b/contrib/completion/git-prompt.sh\n@@ -13,7 +13,7 @@\n #    3a) Change your PS1 to call __git_ps1 as\n #        command-substitution:\n #        Bash: PS1='[\\u@\\h \\W$(__git_ps1 \" (%s)\")]\\$ '\n-#        ZSH:  PS1='[%n@%m %c$(__git_ps1 \" (%s)\")]\\$ '\n+#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 \" (%s)\")]\\$ '\n #        the optional argument will be used as format string.\n #    3b) Alternatively, if you are using bash, __git_ps1 can be\n #        used for PROMPT_COMMAND with two parameters, <pre> and\n@@ -235,12 +235,18 @@ __git_ps1_colorize_gitstring ()\n \t\tlocal c_lblue='%F{blue}'\n \t\tlocal c_clear='%f'\n \telse\n-\t\t# Using \\[ and \\] around colors\n-\t\t# is necessary to prevent wrapping issues!\n-\t\tlocal c_red='\\[\\e[31m\\]'\n-\t\tlocal c_green='\\[\\e[32m\\]'\n-\t\tlocal c_lblue='\\[\\e[1;34m\\]'\n-\t\tlocal c_clear='\\[\\e[0m\\]'\n+\t\tlocal c_red='\\e[31m'\n+\t\tlocal c_green='\\e[32m'\n+\t\tlocal c_lblue='\\e[1;34m'\n+\t\tlocal c_clear='\\e[0m'\n+\t\tif [ $pcmode = yes ]; then\n+\t\t\t# Using \\[ and \\] around colors\n+\t\t\t# is necessary to prevent wrapping issues!\n+\t\t\tc_red=\"\\[$c_red\\]\"\n+\t\t\tc_green=\"\\[$c_green\\]\"\n+\t\t\tc_lblue=\"\\[$c_lblue\\]\"\n+\t\t\tc_clear=\"\\[$c_clear\\]\"\n+\t\tfi\n \tfi\n \tlocal bad_color=$c_red\n \tlocal ok_color=$c_green\n@@ -411,7 +417,7 @@ __git_ps1 ()\n \t\tlocal z=\"${GIT_PS1_STATESEPARATOR-\" \"}\"\n \n \t\t# NO color option unless in PROMPT_COMMAND mode\n-\t\tif [ $pcmode = yes ] && [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n+\t\tif [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n \t\t\t__git_ps1_colorize_gitstring\n \t\tfi\n \n@@ -422,7 +428,7 @@ __git_ps1 ()\n \t\t\tgitstring=$(printf -- \"$printf_format\" \"$gitstring\")\n \t\t\tPS1=\"$ps1pc_start$gitstring$ps1pc_end\"\n \t\telse\n-\t\t\tprintf -- \"$printf_format\" \"$gitstring\"\n+\t\t\tprintf -- \"${printf_format//%s/%b}\" \"$gitstring\"\n \t\tfi\n \tfi\n }\ndiff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh\nindex 22484c1..63abc72 100755\n--- a/t/t9903-bash-prompt.sh\n+++ b/t/t9903-bash-prompt.sh\n@@ -785,4 +785,23 @@ test_expect_success 'prompt - zsh color pc mode - untracked files status indicat\n \ttest_cmp expected \"$actual\"\n '\n \n+test_expect_success 'prompt - bash color ps1 mode - untracked files status indicator' '\n+\tprintf \" (\\e[32mmaster\\e[0m)\" > expected &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color ps1 mode - untracked files status indicator' '\n+\tprintf \" (%%F{green}master%%f)\" > expected &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\t__git_ps1 > \"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n test_done\n-- \n1.8.3.1.440.g82707f8\n"},{"id":"221029","messageId":"7vhagxicu9.fsf@alter.siamese.dyndns.org","threadId":"34166","inReplyTo":"c4c2a37da989c8c39e8cd6be2370ce34dd0675a6.1371402672.git.erdavila@gmail.com","subject":"Re: [PATCH/RFC 1/3] t9903: add tests for git-prompt pcmode","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-06-17T04:24:30Z","receivedAt":"2013-06-17T04:24:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Eduardo R. D'Avila\" <erdavila@gmail.com> writes:\n\n> git-prompt.sh lacks tests for PROMPT_COMMAND mode.\n>\n> Add tests for:\n> * pcmode prompt without colors\n> * pcmode prompt with colors for bash\n> * pcmode prompt with colors for zsh\n>\n> Having these tests enables an upcoming refactor in\n> a safe way.\n>\n> Signed-off-by: Eduardo R. D'Avila <erdavila@gmail.com>\n> ---\n> 250\t0\tt/t9903-bash-prompt.sh\n>  t/t9903-bash-prompt.sh | 250 +++++++++++++++++++++++++++++++++++++++++++++++++\n>  1 file changed, 250 insertions(+)\n>\n> diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh\n> index 15521cc..ebca9de 100755\n> --- a/t/t9903-bash-prompt.sh\n> +++ b/t/t9903-bash-prompt.sh\n> @@ -535,4 +535,254 @@ test_expect_success 'prompt - format string starting with dash' '\n>  \ttest_cmp expected \"$actual\"\n>  '\n>  \n> +test_expect_success 'prompt - pc mode' '\n> +\tprintf \"BEFORE: (master):AFTER\" > expected &&\n\nStyle; redirected filename immediately follows the redirection\noperator, i.e.\n\n\tcommand >expected\n\n> +\tprintf \"\" > expected_output &&\n> +\t(\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" > \"$actual\" &&\n> +\t\ttest_cmp expected_output \"$actual\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - bash color pc mode - branch name' '\n> +\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\]\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n\nWith these escape codes that are hardcoded everywhere, the tests\nlook pretty much unreadable.  Could they be improved to something\nlike this (two ${reset} and some other characters may want to be\nthere):\n\n\tprintf \"BEFORE: (${C_green}master${C_reset}):AFTER\"\n\nby adding variable definitions early in this test file?\n\n[the rest of the original left unsnipped for reference; my comments\nend here]\n\n> +\t(\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" > \"$actual\"\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - bash color pc mode - detached head' '\n> +\tprintf \"BEFORE: (\\\\\\[\\\\\\e[31m\\\\\\](%s...)\\\\\\[\\\\\\e[0m\\\\\\]\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" $(git log -1 --format=\"%h\" b1^) > expected &&\n> +\tgit checkout b1^ &&\n> +\ttest_when_finished \"git checkout master\" &&\n> +\t(\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '\n> +\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[31m\\\\\\]*\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n> +\techo \"dirty\" > file &&\n> +\ttest_when_finished \"git reset --hard\" &&\n> +\t(\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '\n> +\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[32m\\\\\\]+\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n> +\techo \"dirty\" > file &&\n> +\ttest_when_finished \"git reset --hard\" &&\n> +\tgit add -u &&\n> +\t(\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '\n> +\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[31m\\\\\\]*\\\\\\[\\\\\\e[32m\\\\\\]+\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n> +\techo \"dirty index\" > file &&\n> +\ttest_when_finished \"git reset --hard\" &&\n> +\tgit add -u &&\n> +\techo \"dirty worktree\" > file &&\n> +\t(\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '\n> +\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[32m\\\\\\]#\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n> +\t(\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\tcd otherrepo &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - bash color pc mode - inside .git directory' '\n> +\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]GIT_DIR!\\\\\\[\\\\\\e[0m\\\\\\]\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n> +\techo \"dirty\" > file &&\n> +\ttest_when_finished \"git reset --hard\" &&\n> +\t(\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\tcd .git &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - bash color pc mode - stash status indicator' '\n> +\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[1;34m\\\\\\]$\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n> +\techo 2 >file &&\n> +\tgit stash &&\n> +\ttest_when_finished \"git stash drop\" &&\n> +\t(\n> +\t\tGIT_PS1_SHOWSTASHSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '\n> +\tprintf \"BEFORE: (\\\\\\[\\\\\\e[32m\\\\\\]master\\\\\\[\\\\\\e[0m\\\\\\] \\\\\\[\\\\\\e[31m\\\\\\]%%\\\\\\[\\\\\\e[0m\\\\\\]):AFTER\" > expected &&\n> +\t(\n> +\t\tGIT_PS1_SHOWUNTRACKEDFILES=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - zsh color pc mode - branch name' '\n> +\tprintf \"BEFORE: (%%F{green}master%%f%%f):AFTER\" > expected &&\n> +\t(\n> +\t\tZSH_VERSION=5.0.0 &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" > \"$actual\"\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - zsh color pc mode - detached head' '\n> +\tprintf \"BEFORE: (%%F{red}(%s...)%%f%%f):AFTER\" $(git log -1 --format=\"%h\" b1^) > expected &&\n> +\tgit checkout b1^ &&\n> +\ttest_when_finished \"git checkout master\" &&\n> +\t(\n> +\t\tZSH_VERSION=5.0.0 &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty worktree' '\n> +\tprintf \"BEFORE: (%%F{green}master%%f %%F{red}*%%f):AFTER\" > expected &&\n> +\techo \"dirty\" > file &&\n> +\ttest_when_finished \"git reset --hard\" &&\n> +\t(\n> +\t\tZSH_VERSION=5.0.0 &&\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index' '\n> +\tprintf \"BEFORE: (%%F{green}master%%f %%F{green}+%%f):AFTER\" > expected &&\n> +\techo \"dirty\" > file &&\n> +\ttest_when_finished \"git reset --hard\" &&\n> +\tgit add -u &&\n> +\t(\n> +\t\tZSH_VERSION=5.0.0 &&\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index and worktree' '\n> +\tprintf \"BEFORE: (%%F{green}master%%f %%F{red}*%%F{green}+%%f):AFTER\" > expected &&\n> +\techo \"dirty index\" > file &&\n> +\ttest_when_finished \"git reset --hard\" &&\n> +\tgit add -u &&\n> +\techo \"dirty worktree\" > file &&\n> +\t(\n> +\t\tZSH_VERSION=5.0.0 &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - zsh color pc mode - dirty status indicator - before root commit' '\n> +\tprintf \"BEFORE: (%%F{green}master%%f %%F{green}#%%f):AFTER\" > expected &&\n> +\t(\n> +\t\tZSH_VERSION=5.0.0 &&\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\tcd otherrepo &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - zsh color pc mode - inside .git directory' '\n> +\tprintf \"BEFORE: (%%F{green}GIT_DIR!%%f%%f):AFTER\" > expected &&\n> +\techo \"dirty\" > file &&\n> +\ttest_when_finished \"git reset --hard\" &&\n> +\t(\n> +\t\tZSH_VERSION=5.0.0 &&\n> +\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\tcd .git &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - zsh color pc mode - stash status indicator' '\n> +\tprintf \"BEFORE: (%%F{green}master%%f %%F{blue}$%%f):AFTER\" > expected &&\n> +\techo 2 >file &&\n> +\tgit stash &&\n> +\ttest_when_finished \"git stash drop\" &&\n> +\t(\n> +\t\tZSH_VERSION=5.0.0 &&\n> +\t\tGIT_PS1_SHOWSTASHSTATE=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n> +test_expect_success 'prompt - zsh color pc mode - untracked files status indicator' '\n> +\tprintf \"BEFORE: (%%F{green}master%%f %%F{red}%%%%%%f):AFTER\" > expected &&\n> +\t(\n> +\t\tZSH_VERSION=5.0.0 &&\n> +\t\tGIT_PS1_SHOWUNTRACKEDFILES=y &&\n> +\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n> +\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n> +\t\tprintf \"%s\" \"$PS1\" > \"$actual\"\n> +\t) &&\n> +\ttest_cmp expected \"$actual\"\n> +'\n> +\n>  test_done\n"},{"id":"221119","messageId":"1371521743-9744-1-git-send-email-erdavila@gmail.com","threadId":"34166","inReplyTo":"7vhagxicu9.fsf@alter.siamese.dyndns.org","subject":"[PATCH v2 1/3] t9903: add tests for git-prompt pcmode","fromName":"Eduardo R. D'Avila","fromEmail":"erdavila@gmail.com","sentAt":"2013-06-18T02:15:41Z","receivedAt":"2013-06-18T02:15:41Z","isPatch":true,"sender":{"key":"erdavila@gmail.com","avatar":"https://avatars.githubusercontent.com/u/341488?v=4"},"body":"git-prompt.sh lacks tests for PROMPT_COMMAND mode.\n\nAdd tests for:\n* pcmode prompt without colors\n* pcmode prompt with colors for bash\n* pcmode prompt with colors for zsh\n\nHaving these tests enables an upcoming refactor in\na safe way.\n\nSigned-off-by: Eduardo R. D'Avila <erdavila@gmail.com>\n---\n254\t0\tt/t9903-bash-prompt.sh\n t/t9903-bash-prompt.sh | 254 +++++++++++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 254 insertions(+)\n\ndiff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh\nindex 15521cc..6a88778 100755\n--- a/t/t9903-bash-prompt.sh\n+++ b/t/t9903-bash-prompt.sh\n@@ -10,6 +10,10 @@ test_description='test git-specific bash prompt functions'\n . \"$GIT_BUILD_DIR/contrib/completion/git-prompt.sh\"\n \n actual=\"$TRASH_DIRECTORY/actual\"\n+c_red='\\\\[\\\\e[31m\\\\]'\n+c_green='\\\\[\\\\e[32m\\\\]'\n+c_lblue='\\\\[\\\\e[1;34m\\\\]'\n+c_clear='\\\\[\\\\e[0m\\\\]'\n \n test_expect_success 'setup for prompt tests' '\n \tmkdir -p subdir/subsubdir &&\n@@ -535,4 +539,254 @@ test_expect_success 'prompt - format string starting with dash' '\n \ttest_cmp expected \"$actual\"\n '\n \n+test_expect_success 'prompt - pc mode' '\n+\tprintf \"BEFORE: (master):AFTER\" >expected &&\n+\tprintf \"\" >expected_output &&\n+\t(\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" >\"$actual\" &&\n+\t\ttest_cmp expected_output \"$actual\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - branch name' '\n+\tprintf \"BEFORE: (${c_green}master${c_clear}${c_clear}):AFTER\" >expected &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" >\"$actual\"\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - detached head' '\n+\tprintf \"BEFORE: (${c_red}(%s...)${c_clear}${c_clear}):AFTER\" $(git log -1 --format=\"%h\" b1^) >expected &&\n+\tgit checkout b1^ &&\n+\ttest_when_finished \"git checkout master\" &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '\n+\tprintf \"BEFORE: (${c_green}master${c_clear} ${c_red}*${c_clear}):AFTER\" >expected &&\n+\techo \"dirty\" >file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\t(\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '\n+\tprintf \"BEFORE: (${c_green}master${c_clear} ${c_green}+${c_clear}):AFTER\" >expected &&\n+\techo \"dirty\" >file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\tgit add -u &&\n+\t(\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '\n+\tprintf \"BEFORE: (${c_green}master${c_clear} ${c_red}*${c_green}+${c_clear}):AFTER\" >expected &&\n+\techo \"dirty index\" >file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\tgit add -u &&\n+\techo \"dirty worktree\" >file &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '\n+\tprintf \"BEFORE: (${c_green}master${c_clear} ${c_green}#${c_clear}):AFTER\" >expected &&\n+\t(\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tcd otherrepo &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - inside .git directory' '\n+\tprintf \"BEFORE: (${c_green}GIT_DIR!${c_clear}${c_clear}):AFTER\" >expected &&\n+\techo \"dirty\" >file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\t(\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tcd .git &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - stash status indicator' '\n+\tprintf \"BEFORE: (${c_green}master${c_clear} ${c_lblue}\\$${c_clear}):AFTER\" >expected &&\n+\techo 2 >file &&\n+\tgit stash &&\n+\ttest_when_finished \"git stash drop\" &&\n+\t(\n+\t\tGIT_PS1_SHOWSTASHSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '\n+\tprintf \"BEFORE: (${c_green}master${c_clear} ${c_red}%%${c_clear}):AFTER\" >expected &&\n+\t(\n+\t\tGIT_PS1_SHOWUNTRACKEDFILES=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - branch name' '\n+\tprintf \"BEFORE: (%%F{green}master%%f%%f):AFTER\" >expected &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" >\"$actual\"\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - detached head' '\n+\tprintf \"BEFORE: (%%F{red}(%s...)%%f%%f):AFTER\" $(git log -1 --format=\"%h\" b1^) >expected &&\n+\tgit checkout b1^ &&\n+\ttest_when_finished \"git checkout master\" &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty worktree' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{red}*%%f):AFTER\" >expected &&\n+\techo \"dirty\" >file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{green}+%%f):AFTER\" >expected &&\n+\techo \"dirty\" >file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\tgit add -u &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - dirty index and worktree' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{red}*%%F{green}+%%f):AFTER\" >expected &&\n+\techo \"dirty index\" >file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\tgit add -u &&\n+\techo \"dirty worktree\" >file &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - dirty status indicator - before root commit' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{green}#%%f):AFTER\" >expected &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tcd otherrepo &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - inside .git directory' '\n+\tprintf \"BEFORE: (%%F{green}GIT_DIR!%%f%%f):AFTER\" >expected &&\n+\techo \"dirty\" >file &&\n+\ttest_when_finished \"git reset --hard\" &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWDIRTYSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tcd .git &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - stash status indicator' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{blue}$%%f):AFTER\" >expected &&\n+\techo 2 >file &&\n+\tgit stash &&\n+\ttest_when_finished \"git stash drop\" &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWSTASHSTATE=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color pc mode - untracked files status indicator' '\n+\tprintf \"BEFORE: (%%F{green}master%%f %%F{red}%%%%%%f):AFTER\" >expected &&\n+\t(\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\tGIT_PS1_SHOWUNTRACKEDFILES=y &&\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 \"BEFORE:\" \":AFTER\" &&\n+\t\tprintf \"%s\" \"$PS1\" >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n test_done\n-- \n1.8.3.1.440.g82707f8\n"},{"id":"221120","messageId":"1371521811-9819-1-git-send-email-erdavila@gmail.com","threadId":"34166","inReplyTo":"7vhagxicu9.fsf@alter.siamese.dyndns.org","subject":"[PATCH v2 2/3] git-prompt.sh: refactor colored prompt code","fromName":"Eduardo R. D'Avila","fromEmail":"erdavila@gmail.com","sentAt":"2013-06-18T02:16:51Z","receivedAt":"2013-06-18T02:16:51Z","isPatch":true,"sender":{"key":"erdavila@gmail.com","avatar":"https://avatars.githubusercontent.com/u/341488?v=4"},"body":"__git_ps1_colorize_gitstring() sets color codes and\nbuilds the prompt gitstring. It has duplicated code\nto handle color codes for bash and zsh shells.\n__git_ps1() also has duplicated logic to build the\nprompt gitstring.\n\nRemove duplication of logic to build gitstring in\n__git_ps1_colorize_gitstring() and __git_ps1().\n\nLeave in __git_ps1_colorize_gitstring() only logic\nto set color codes.\n\nSigned-off-by: Eduardo R. D'Avila <erdavila@gmail.com>\n---\n26\t59\tcontrib/completion/git-prompt.sh\n6\t6\tt/t9903-bash-prompt.sh\n contrib/completion/git-prompt.sh | 85 ++++++++++++----------------------------\n t/t9903-bash-prompt.sh           | 12 +++---\n 2 files changed, 32 insertions(+), 65 deletions(-)\n\ndiff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh\nindex 86a4f3f..70515cc 100644\n--- a/contrib/completion/git-prompt.sh\n+++ b/contrib/completion/git-prompt.sh\n@@ -225,8 +225,8 @@ __git_ps1_show_upstream ()\n }\n \n # Helper function that is meant to be called from __git_ps1.  It\n-# builds up a gitstring injecting color codes into the appropriate\n-# places.\n+# injects color codes into the appropriate gitstring variables used\n+# to build a gitstring.\n __git_ps1_colorize_gitstring ()\n {\n \tif [[ -n ${ZSH_VERSION-} ]]; then\n@@ -234,74 +234,40 @@ __git_ps1_colorize_gitstring ()\n \t\tlocal c_green='%F{green}'\n \t\tlocal c_lblue='%F{blue}'\n \t\tlocal c_clear='%f'\n-\t\tlocal bad_color=$c_red\n-\t\tlocal ok_color=$c_green\n-\t\tlocal branch_color=\"$c_clear\"\n-\t\tlocal flags_color=\"$c_lblue\"\n-\t\tlocal branchstring=\"$c${b##refs/heads/}\"\n-\n-\t\tif [ $detached = no ]; then\n-\t\t\tbranch_color=\"$ok_color\"\n-\t\telse\n-\t\t\tbranch_color=\"$bad_color\"\n-\t\tfi\n-\n-\t\tgitstring=\"$branch_color$branchstring$c_clear\"\n-\n-\t\tif [ -n \"$w$i$s$u$r$p\" ]; then\n-\t\t\tgitstring=\"$gitstring$z\"\n-\t\tfi\n-\t\tif [ \"$w\" = \"*\" ]; then\n-\t\t\tgitstring=\"$gitstring$bad_color$w\"\n-\t\tfi\n-\t\tif [ -n \"$i\" ]; then\n-\t\t\tgitstring=\"$gitstring$ok_color$i\"\n-\t\tfi\n-\t\tif [ -n \"$s\" ]; then\n-\t\t\tgitstring=\"$gitstring$flags_color$s\"\n-\t\tfi\n-\t\tif [ -n \"$u\" ]; then\n-\t\t\tgitstring=\"$gitstring$bad_color$u\"\n-\t\tfi\n-\t\tgitstring=\"$gitstring$c_clear$r$p\"\n-\t\treturn\n+\telse\n+\t\t# Using \\[ and \\] around colors\n+\t\t# is necessary to prevent wrapping issues!\n+\t\tlocal c_red='\\[\\e[31m\\]'\n+\t\tlocal c_green='\\[\\e[32m\\]'\n+\t\tlocal c_lblue='\\[\\e[1;34m\\]'\n+\t\tlocal c_clear='\\[\\e[0m\\]'\n \tfi\n-\tlocal c_red='\\e[31m'\n-\tlocal c_green='\\e[32m'\n-\tlocal c_lblue='\\e[1;34m'\n-\tlocal c_clear='\\e[0m'\n \tlocal bad_color=$c_red\n \tlocal ok_color=$c_green\n-\tlocal branch_color=\"$c_clear\"\n \tlocal flags_color=\"$c_lblue\"\n-\tlocal branchstring=\"$c${b##refs/heads/}\"\n \n+\tlocal branch_color=\"\"\n \tif [ $detached = no ]; then\n \t\tbranch_color=\"$ok_color\"\n \telse\n \t\tbranch_color=\"$bad_color\"\n \tfi\n+\tc=\"$branch_color$c\"\n \n-\t# Setting gitstring directly with \\[ and \\] around colors\n-\t# is necessary to prevent wrapping issues!\n-\tgitstring=\"\\[$branch_color\\]$branchstring\\[$c_clear\\]\"\n-\n-\tif [ -n \"$w$i$s$u$r$p\" ]; then\n-\t\tgitstring=\"$gitstring$z\"\n-\tfi\n+\tz=\"$c_clear$z\"\n \tif [ \"$w\" = \"*\" ]; then\n-\t\tgitstring=\"$gitstring\\[$bad_color\\]$w\"\n+\t\tw=\"$bad_color$w\"\n \tfi\n \tif [ -n \"$i\" ]; then\n-\t\tgitstring=\"$gitstring\\[$ok_color\\]$i\"\n+\t\ti=\"$ok_color$i\"\n \tfi\n \tif [ -n \"$s\" ]; then\n-\t\tgitstring=\"$gitstring\\[$flags_color\\]$s\"\n+\t\ts=\"$flags_color$s\"\n \tfi\n \tif [ -n \"$u\" ]; then\n-\t\tgitstring=\"$gitstring\\[$bad_color\\]$u\"\n+\t\tu=\"$bad_color$u\"\n \tfi\n-\tgitstring=\"$gitstring\\[$c_clear\\]$r$p\"\n+\tr=\"$c_clear$r\"\n }\n \n # __git_ps1 accepts 0 or 1 arguments (i.e., format string)\n@@ -443,19 +409,20 @@ __git_ps1 ()\n \t\tfi\n \n \t\tlocal z=\"${GIT_PS1_STATESEPARATOR-\" \"}\"\n+\n+\t\t# NO color option unless in PROMPT_COMMAND mode\n+\t\tif [ $pcmode = yes ] && [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n+\t\t\t__git_ps1_colorize_gitstring\n+\t\tfi\n+\n \t\tlocal f=\"$w$i$s$u\"\n+\t\tlocal gitstring=\"$c${b##refs/heads/}${f:+$z$f}$r$p\"\n+\n \t\tif [ $pcmode = yes ]; then\n-\t\t\tlocal gitstring=\n-\t\t\tif [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n-\t\t\t\t__git_ps1_colorize_gitstring\n-\t\t\telse\n-\t\t\t\tgitstring=\"$c${b##refs/heads/}${f:+$z$f}$r$p\"\n-\t\t\tfi\n \t\t\tgitstring=$(printf -- \"$printf_format\" \"$gitstring\")\n \t\t\tPS1=\"$ps1pc_start$gitstring$ps1pc_end\"\n \t\telse\n-\t\t\t# NO color option unless in PROMPT_COMMAND mode\n-\t\t\tprintf -- \"$printf_format\" \"$c${b##refs/heads/}${f:+$z$f}$r$p\"\n+\t\t\tprintf -- \"$printf_format\" \"$gitstring\"\n \t\tfi\n \tfi\n }\ndiff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh\nindex 6a88778..1101adf 100755\n--- a/t/t9903-bash-prompt.sh\n+++ b/t/t9903-bash-prompt.sh\n@@ -551,7 +551,7 @@ test_expect_success 'prompt - pc mode' '\n '\n \n test_expect_success 'prompt - bash color pc mode - branch name' '\n-\tprintf \"BEFORE: (${c_green}master${c_clear}${c_clear}):AFTER\" >expected &&\n+\tprintf \"BEFORE: (${c_green}master${c_clear}):AFTER\" >expected &&\n \t(\n \t\tGIT_PS1_SHOWCOLORHINTS=y &&\n \t\t__git_ps1 \"BEFORE:\" \":AFTER\" >\"$actual\"\n@@ -561,7 +561,7 @@ test_expect_success 'prompt - bash color pc mode - branch name' '\n '\n \n test_expect_success 'prompt - bash color pc mode - detached head' '\n-\tprintf \"BEFORE: (${c_red}(%s...)${c_clear}${c_clear}):AFTER\" $(git log -1 --format=\"%h\" b1^) >expected &&\n+\tprintf \"BEFORE: (${c_red}(%s...)${c_clear}):AFTER\" $(git log -1 --format=\"%h\" b1^) >expected &&\n \tgit checkout b1^ &&\n \ttest_when_finished \"git checkout master\" &&\n \t(\n@@ -627,7 +627,7 @@ test_expect_success 'prompt - bash color pc mode - dirty status indicator - befo\n '\n \n test_expect_success 'prompt - bash color pc mode - inside .git directory' '\n-\tprintf \"BEFORE: (${c_green}GIT_DIR!${c_clear}${c_clear}):AFTER\" >expected &&\n+\tprintf \"BEFORE: (${c_green}GIT_DIR!${c_clear}):AFTER\" >expected &&\n \techo \"dirty\" >file &&\n \ttest_when_finished \"git reset --hard\" &&\n \t(\n@@ -666,7 +666,7 @@ test_expect_success 'prompt - bash color pc mode - untracked files status indica\n '\n \n test_expect_success 'prompt - zsh color pc mode - branch name' '\n-\tprintf \"BEFORE: (%%F{green}master%%f%%f):AFTER\" >expected &&\n+\tprintf \"BEFORE: (%%F{green}master%%f):AFTER\" >expected &&\n \t(\n \t\tZSH_VERSION=5.0.0 &&\n \t\tGIT_PS1_SHOWCOLORHINTS=y &&\n@@ -677,7 +677,7 @@ test_expect_success 'prompt - zsh color pc mode - branch name' '\n '\n \n test_expect_success 'prompt - zsh color pc mode - detached head' '\n-\tprintf \"BEFORE: (%%F{red}(%s...)%%f%%f):AFTER\" $(git log -1 --format=\"%h\" b1^) >expected &&\n+\tprintf \"BEFORE: (%%F{red}(%s...)%%f):AFTER\" $(git log -1 --format=\"%h\" b1^) >expected &&\n \tgit checkout b1^ &&\n \ttest_when_finished \"git checkout master\" &&\n \t(\n@@ -748,7 +748,7 @@ test_expect_success 'prompt - zsh color pc mode - dirty status indicator - befor\n '\n \n test_expect_success 'prompt - zsh color pc mode - inside .git directory' '\n-\tprintf \"BEFORE: (%%F{green}GIT_DIR!%%f%%f):AFTER\" >expected &&\n+\tprintf \"BEFORE: (%%F{green}GIT_DIR!%%f):AFTER\" >expected &&\n \techo \"dirty\" >file &&\n \ttest_when_finished \"git reset --hard\" &&\n \t(\n-- \n1.8.3.1.440.g82707f8\n"},{"id":"221127","messageId":"1371521844-9901-1-git-send-email-erdavila@gmail.com","threadId":"34166","inReplyTo":"7vhagxicu9.fsf@alter.siamese.dyndns.org","subject":"[PATCH 3/3] git-prompt.sh: enable color prompt in non-pcmode","fromName":"Eduardo R. D'Avila","fromEmail":"erdavila@gmail.com","sentAt":"2013-06-18T02:17:24Z","receivedAt":"2013-06-18T02:17:24Z","isPatch":true,"sender":{"key":"erdavila@gmail.com","avatar":"https://avatars.githubusercontent.com/u/341488?v=4"},"body":"The use of colors in a prompt is only possible in\npcmode (using the variable PROMPT_COMMAND).\n\nEnable color prompt in non-pcmode (using the variable\nPS1) for both Bash and ZSH.\n\nSigned-off-by: Eduardo R. D'Avila <erdavila@gmail.com>\n---\n15\t9\tcontrib/completion/git-prompt.sh\n19\t0\tt/t9903-bash-prompt.sh\n contrib/completion/git-prompt.sh | 24 +++++++++++++++---------\n t/t9903-bash-prompt.sh           | 19 +++++++++++++++++++\n 2 files changed, 34 insertions(+), 9 deletions(-)\n\ndiff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh\nindex 70515cc..c5c75e7 100644\n--- a/contrib/completion/git-prompt.sh\n+++ b/contrib/completion/git-prompt.sh\n@@ -13,7 +13,7 @@\n #    3a) Change your PS1 to call __git_ps1 as\n #        command-substitution:\n #        Bash: PS1='[\\u@\\h \\W$(__git_ps1 \" (%s)\")]\\$ '\n-#        ZSH:  PS1='[%n@%m %c$(__git_ps1 \" (%s)\")]\\$ '\n+#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 \" (%s)\")]\\$ '\n #        the optional argument will be used as format string.\n #    3b) Alternatively, if you are using bash, __git_ps1 can be\n #        used for PROMPT_COMMAND with two parameters, <pre> and\n@@ -235,12 +235,18 @@ __git_ps1_colorize_gitstring ()\n \t\tlocal c_lblue='%F{blue}'\n \t\tlocal c_clear='%f'\n \telse\n-\t\t# Using \\[ and \\] around colors\n-\t\t# is necessary to prevent wrapping issues!\n-\t\tlocal c_red='\\[\\e[31m\\]'\n-\t\tlocal c_green='\\[\\e[32m\\]'\n-\t\tlocal c_lblue='\\[\\e[1;34m\\]'\n-\t\tlocal c_clear='\\[\\e[0m\\]'\n+\t\tlocal c_red='\\e[31m'\n+\t\tlocal c_green='\\e[32m'\n+\t\tlocal c_lblue='\\e[1;34m'\n+\t\tlocal c_clear='\\e[0m'\n+\t\tif [ $pcmode = yes ]; then\n+\t\t\t# Using \\[ and \\] around colors\n+\t\t\t# is necessary to prevent wrapping issues!\n+\t\t\tc_red=\"\\[$c_red\\]\"\n+\t\t\tc_green=\"\\[$c_green\\]\"\n+\t\t\tc_lblue=\"\\[$c_lblue\\]\"\n+\t\t\tc_clear=\"\\[$c_clear\\]\"\n+\t\tfi\n \tfi\n \tlocal bad_color=$c_red\n \tlocal ok_color=$c_green\n@@ -411,7 +417,7 @@ __git_ps1 ()\n \t\tlocal z=\"${GIT_PS1_STATESEPARATOR-\" \"}\"\n \n \t\t# NO color option unless in PROMPT_COMMAND mode\n-\t\tif [ $pcmode = yes ] && [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n+\t\tif [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n \t\t\t__git_ps1_colorize_gitstring\n \t\tfi\n \n@@ -422,7 +428,7 @@ __git_ps1 ()\n \t\t\tgitstring=$(printf -- \"$printf_format\" \"$gitstring\")\n \t\t\tPS1=\"$ps1pc_start$gitstring$ps1pc_end\"\n \t\telse\n-\t\t\tprintf -- \"$printf_format\" \"$gitstring\"\n+\t\t\tprintf -- \"${printf_format//%s/%b}\" \"$gitstring\"\n \t\tfi\n \tfi\n }\ndiff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh\nindex 1101adf..7dccc1c 100755\n--- a/t/t9903-bash-prompt.sh\n+++ b/t/t9903-bash-prompt.sh\n@@ -789,4 +789,23 @@ test_expect_success 'prompt - zsh color pc mode - untracked files status indicat\n \ttest_cmp expected \"$actual\"\n '\n \n+test_expect_success 'prompt - bash color ps1 mode - untracked files status indicator' '\n+\tprintf \" (\\e[32mmaster\\e[0m)\" >expected &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color ps1 mode - untracked files status indicator' '\n+\tprintf \" (%%F{green}master%%f)\" >expected &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\t__git_ps1 >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n test_done\n-- \n1.8.3.1.440.g82707f8\n"},{"id":"221136","messageId":"1371521986-10179-1-git-send-email-erdavila@gmail.com","threadId":"34166","inReplyTo":"7vhagxicu9.fsf@alter.siamese.dyndns.org","subject":"[PATCH v2 3/3] git-prompt.sh: enable color prompt in non-pcmode","fromName":"Eduardo R. D'Avila","fromEmail":"erdavila@gmail.com","sentAt":"2013-06-18T02:19:46Z","receivedAt":"2013-06-18T02:19:46Z","isPatch":true,"sender":{"key":"erdavila@gmail.com","avatar":"https://avatars.githubusercontent.com/u/341488?v=4"},"body":"The use of colors in a prompt is only possible in\npcmode (using the variable PROMPT_COMMAND).\n\nEnable color prompt in non-pcmode (using the variable\nPS1) for both Bash and ZSH.\n\nSigned-off-by: Eduardo R. D'Avila <erdavila@gmail.com>\n---\n15\t9\tcontrib/completion/git-prompt.sh\n19\t0\tt/t9903-bash-prompt.sh\n contrib/completion/git-prompt.sh | 24 +++++++++++++++---------\n t/t9903-bash-prompt.sh           | 19 +++++++++++++++++++\n 2 files changed, 34 insertions(+), 9 deletions(-)\n\ndiff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh\nindex 70515cc..c5c75e7 100644\n--- a/contrib/completion/git-prompt.sh\n+++ b/contrib/completion/git-prompt.sh\n@@ -13,7 +13,7 @@\n #    3a) Change your PS1 to call __git_ps1 as\n #        command-substitution:\n #        Bash: PS1='[\\u@\\h \\W$(__git_ps1 \" (%s)\")]\\$ '\n-#        ZSH:  PS1='[%n@%m %c$(__git_ps1 \" (%s)\")]\\$ '\n+#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 \" (%s)\")]\\$ '\n #        the optional argument will be used as format string.\n #    3b) Alternatively, if you are using bash, __git_ps1 can be\n #        used for PROMPT_COMMAND with two parameters, <pre> and\n@@ -235,12 +235,18 @@ __git_ps1_colorize_gitstring ()\n \t\tlocal c_lblue='%F{blue}'\n \t\tlocal c_clear='%f'\n \telse\n-\t\t# Using \\[ and \\] around colors\n-\t\t# is necessary to prevent wrapping issues!\n-\t\tlocal c_red='\\[\\e[31m\\]'\n-\t\tlocal c_green='\\[\\e[32m\\]'\n-\t\tlocal c_lblue='\\[\\e[1;34m\\]'\n-\t\tlocal c_clear='\\[\\e[0m\\]'\n+\t\tlocal c_red='\\e[31m'\n+\t\tlocal c_green='\\e[32m'\n+\t\tlocal c_lblue='\\e[1;34m'\n+\t\tlocal c_clear='\\e[0m'\n+\t\tif [ $pcmode = yes ]; then\n+\t\t\t# Using \\[ and \\] around colors\n+\t\t\t# is necessary to prevent wrapping issues!\n+\t\t\tc_red=\"\\[$c_red\\]\"\n+\t\t\tc_green=\"\\[$c_green\\]\"\n+\t\t\tc_lblue=\"\\[$c_lblue\\]\"\n+\t\t\tc_clear=\"\\[$c_clear\\]\"\n+\t\tfi\n \tfi\n \tlocal bad_color=$c_red\n \tlocal ok_color=$c_green\n@@ -411,7 +417,7 @@ __git_ps1 ()\n \t\tlocal z=\"${GIT_PS1_STATESEPARATOR-\" \"}\"\n \n \t\t# NO color option unless in PROMPT_COMMAND mode\n-\t\tif [ $pcmode = yes ] && [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n+\t\tif [ -n \"${GIT_PS1_SHOWCOLORHINTS-}\" ]; then\n \t\t\t__git_ps1_colorize_gitstring\n \t\tfi\n \n@@ -422,7 +428,7 @@ __git_ps1 ()\n \t\t\tgitstring=$(printf -- \"$printf_format\" \"$gitstring\")\n \t\t\tPS1=\"$ps1pc_start$gitstring$ps1pc_end\"\n \t\telse\n-\t\t\tprintf -- \"$printf_format\" \"$gitstring\"\n+\t\t\tprintf -- \"${printf_format//%s/%b}\" \"$gitstring\"\n \t\tfi\n \tfi\n }\ndiff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh\nindex 1101adf..7dccc1c 100755\n--- a/t/t9903-bash-prompt.sh\n+++ b/t/t9903-bash-prompt.sh\n@@ -789,4 +789,23 @@ test_expect_success 'prompt - zsh color pc mode - untracked files status indicat\n \ttest_cmp expected \"$actual\"\n '\n \n+test_expect_success 'prompt - bash color ps1 mode - untracked files status indicator' '\n+\tprintf \" (\\e[32mmaster\\e[0m)\" >expected &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\t__git_ps1 >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n+test_expect_success 'prompt - zsh color ps1 mode - untracked files status indicator' '\n+\tprintf \" (%%F{green}master%%f)\" >expected &&\n+\t(\n+\t\tGIT_PS1_SHOWCOLORHINTS=y &&\n+\t\tZSH_VERSION=5.0.0 &&\n+\t\t__git_ps1 >\"$actual\"\n+\t) &&\n+\ttest_cmp expected \"$actual\"\n+'\n+\n test_done\n-- \n1.8.3.1.440.g82707f8\n"}]}