Skip to content

Update git-prompt.sh for Zsh Error #631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

JaviAir
Copy link

@JaviAir JaviAir commented Jul 5, 2025

Howdy!

Problem

  • If Zsh ever runs /etc/profile.d/git-prompt.sh, then zsh will also run git-completion.bash.
  • git-completion.bash throws an Error when Zsh is detected.
  • This Error echoes this message: "ERROR: this script is obsolete, please see git-completion.zsh"
  • This Error breaks certain git completions and functionality.

Solution

  • Add condition to git-prompt.sh to avoid git-completion.bash if Zsh is detected.
  • Add prompt that enables git prompts to work.

Note:
Git completions will still work for Bash and Zsh.
Prompts with "__git_ps1" will still work for Bash and Zsh.

JaviAir added 5 commits July 5, 2025 06:39
Howdy!

Problem
If Zsh ever runs /etc/profile.d/git-prompts.sh, then zsh will also run git-completions.bash.
git-completions.bash throws an Error when Zsh is detected.
This Error echoes this message: "ERROR: this script is obsolete, please see git-completion.zsh"
This Error breaks certain git completions and functionality.

Solution
Add condition to git-prompt.sh to avoid git-completion.bash if Zsh is detected.
Add prompt that enables git prompts to work.

Note:
Git completions will still work for Bash and Zsh.
Prompts with "__git_ps1" will still work for Bash and Zsh.

Signed-off-by: Javier S. Orejarena <[email protected]>
Signed-off-by: Javier S. Orejarena <[email protected]>
Signed-off-by: Javier S. Orejarena <[email protected]>
@rimrul
Copy link
Member

rimrul commented Jul 5, 2025

/updpkgsums

The workflow run was started.

Signed-off-by: Matthias Aßhauer <[email protected]>
@rimrul
Copy link
Member

rimrul commented Jul 5, 2025

@JaviAir The build failures aren't due to issues in your script, but because the PKGBUILD file didn't match.

Feel free to force push additional changes over my commit, I'll try to look at this PR throughout the day and have the bot update the file again.

@JaviAir
Copy link
Author

JaviAir commented Jul 6, 2025

@rimrul Thank you! Is there anything else I should do for this PR?

Copy link
Member

@dscho dscho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have serious concerns about the shape of the commits. They reflect the (quite messy, and unhelpful to the reader) chronological development of the changes. I do not need to see every attempt you made to make this work (even though there are admittedly times when it is educative to describe in the commit message e.g. when an attempted approach failed to work unexpectedly and potential reviewers might be curious why it had not been attempted, when it had been).

A much more helpful way to represent the changes would be to squash them into combined commits as needed and then accompany them with stellar commit messages that closely follow the advice given in https://github.blog/2022-06-30-write-better-commits-build-better-projects/, focusing on:

  What you’re doing Why you’re doing it
High-level (strategic) Intent (what does this accomplish?) Context (why does the code do what it does now?)
Low-level (tactical) Implementation (what did you do to accomplish your goal?) Justification (why is this change being made?)

One of the things I am missing in particular is a justification why this change should be accepted into Git for Windows when it does not even distribute a zsh, but only a Bash.

Comment on lines 2 to 3
# newline='
# '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be commented out? I would think that it doesn't work, then, it is used in the precmd below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, it was initially not commented.
It was only commented to see if that was causing the build to fail earlier.
Thankfully rimrul helped explain what was going on.
I have now un-commented this. 👍

@@ -25,8 +29,18 @@ else
COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
if test -f "$COMPLETION_PATH/git-prompt.sh"
then
. "$COMPLETION_PATH/git-prompt.sh"
if [ ! "x${ZSH_VERSION}" = "x" ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a careful look at the code style used in this script, e.g. 3 lines up: It uses test, not [ ... ], the trailing ; is unwanted, and the condition dearly desires to be not so uncomplicated ;-)

Suggested change
if [ ! "x${ZSH_VERSION}" = "x" ];
if test -n "$ZSH_VERSION"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this syntax is more legible. Applied suggested change. 👍

Comment on lines 34 to 40
then
precmd() # Assembles git prompt that closely resembles the bash prompt.
{
pre="${newline}%F{green}%n@%m%f %F{magenta}${MSYSTEM:-"ZSH"}%f %F{yellow}%~%f %F{cyan}"
post="%f${newline}$ "
__git_ps1 "${pre}" "${post}" "(%s)"
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's different from what is recommended in Git's own contrib/completion/git-prompt.sh:

#        ZSH:  setopt PROMPT_SUBST ; PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '

Would it not be preferable to follow that suggestion instead, or am I missing anything?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few reasons,

I tried that approach first (Line 16) however, it did not work when using it in this file.
It only worked when adding it to a zshrc file.
A couple lines below they themselves recommend a faster alternative that worked when using it in this file:

Faster alternative recommended in Git's own contrib/completion/git-prompt.sh

ZSH:  precmd () { __git_ps1 "%n" ":%~$ " "|%s" }

post="%f${newline}$ "
__git_ps1 "${pre}" "${post}" "(%s)"
}
return # Avoids zsh git-completion.bash Error.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is sourced. What happens when you return in such a scenario? Especially to any other files in /etc/profile.d/ that are supposed to be sourced, too, not to mention the remainder of the file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was under the impression the return would only end the current scope of the if block.
I have pushed a restructured if else block without the need for the return. 👍

__git_ps1 "${pre}" "${post}" "(%s)"
}
return # Avoids zsh git-completion.bash Error.
fi
. "$COMPLETION_PATH/git-completion.bash"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a git-completion.zsh. Why don't we use that with zsh?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that first but it gave even more errors and broke git completions completely.
Afterwards I discovered that Zsh comes with its own Git completions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants