I am using this function in Bash
function parse_git_branch {
git_status="$(git status 2> /dev/null)"
pattern="^# On branch ([^${IFS}]*)"
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="*"
fi
# add an else if or two here if you want to get more specific
if [[ ${git_status} =~ ${pattern} ]]; then
branch=${BASH_REMATCH[1]}
echo "(${branch}${state})"
fi
}
but I’m determined to use zsh. While I can use this perfectly as a shell script (even without a shebang) in my .zshrc the error is a parse error on this line if [[ ! ${git_status}}…
What do I need to do to get it ready for zshell?
Edit: The “actual error” I’m getting is " parse error near } and it refers to the line with the strange double }}, which works on Bash.
Edit: Here’s the final code, just for fun:
parse_git_branch() {
git_status="$(git status 2> /dev/null)"
pattern="^# On branch ([^[:space:]]*)"
if [[ ! ${git_status} =~ "working directory clean" ]]; then
state="*"
fi
if [[ ${git_status} =~ ${pattern} ]]; then
branch=${match[1]}
echo "(${branch}${state})"
fi
}
setopt PROMPT_SUBST
PROMPT='$PR_GREEN%n@$PR_GREEN%m%u$PR_NO_COLOR:$PR_BLUE%2c$PR_NO_COLOR%(!.#.$)'
RPROMPT='$PR_GREEN$(parse_git_branch)$PR_NO_COLOR'
Thanks to everybody for your patience and help.
Edit: The best answer has schooled us all: git status is porcelain (UI). Good scripting goes against GIT plumbing. Here’s the final function:
# The latest version of Chris' function below
PROMPT='$PR_GREEN%n@$PR_GREEN%m%u$PR_NO_COLOR:$PR_BLUE%2c$PR_NO_COLOR%(!.#.$)'
RPROMPT='$PR_GREEN$(parse_git_branch)$PR_NO_COLOR'
Note that only the prompt is zsh-specific. In Bash it would be your prompt plus "\$(parse_git_branch)".
This might be slower (more calls to GIT, but that’s an empirical question) but it won’t be broken by changes in GIT (they don’t change the plumbing). And that is very important for a good script moving forward.
You should really use Git “plumbing” commands to extract the info you want. The output from the “porcelain” commands (e.g.
git status) may change over time, but the behavior of the “plumbing” commands is much more stable.With the porcelain interfaces, it can also be done without “bashisms” or “zshisms” (i.e. the
=~matching operator):Integrating the output into the prompt is still shell specific (i.e. escape or quote the
$(for both bash and zsh) and set PROMPT_SUBST (for zsh)).