diff mbox series

Using git-prompt.sh is wrapping text to early in Bash.

Message ID CAKUjMCUEmrC8XfVKC4AkCzDyqWziPjDh5EiNmx02xQF=SVBVzQ@mail.gmail.com (mailing list archive)
State New, archived
Headers show
Series Using git-prompt.sh is wrapping text to early in Bash. | expand

Commit Message

Richard Quadling March 11, 2024, 1:34 p.m. UTC
The changes introduced in
https://github.com/git/git/commit/7ee1af8cb8b97385fc4e603bc024d877def5adb4
causes early wrapping of the command line whilst typing your commands
in Bash, when you are using the git-prompt.sh as part of the
PS1_COMMAND. I do not know about other platforms or shells.

The following setup is what I have:

GIT_PS1_COMPRESSSPARSESTATE=
GIT_PS1_DESCRIBE_STYLE=
GIT_PS1_HIDE_IF_PWD_IGNORED=yes
GIT_PS1_OMITSPARSESTATE=
GIT_PS1_SHOWCOLORHINTS=yes
GIT_PS1_SHOWDIRTYSTATE=yes
GIT_PS1_SHOWSTASHSTATE=yes
GIT_PS1_SHOWUNTRACKEDFILES=yes
GIT_PS1_SHOWUPSTREAM="auto"
GIT_PS1_STATESEPARATOR=" "
PS1_PROMPT=' \\$ '
export PROMPT_COMMAND='__git_ps1 "$PS1_COMBINED" "$PS1_PROMPT";'

Change to any git directory:
cd ~/dev/GitHub/git

If all is setup properly for git-prompt.sh, you should see prompt similar to:
~/dev/GitHub/git (master|u=) $
depending upon your actual prompt.

Confirm things by running:
$ git remote -v

and I get:
origin git@github.com:git/git.git (fetch)
origin git@github.com:git/git.git (push)

Now start typing a LONG command.
echo 'This is a really long command that, as you start typing will
suddenly wrap around to the same line before it reaches the end of the
line'

The issue can be more greatly seen if you examine what is actually
added to the prompt.

My prompt has a more elements in it (datetime, shell level, count of
pushd's, whoiam@machinename.

So, if I run:
echo $PS1

I get ...

\001$(code=${?##0};echo ${code:+$(tput setaf 9)\002(${code})\
})\001$(tput sgr0)\002\001$(tput setaf 6)\002${PS1_SHELL_IN}\001$(tput
setaf 14)\002$SHLVL\001$(tput setaf 6)\002${PS1_SHELL_OUT} \001$(tput
sgr0)\002\001$(tput setaf 5)\002\D{%Y-%m-%d %H:%M:%S} \001$(tput
sgr0)\002\001$(tput setaf 6)\002\u\001$(tput sgr0)\002\001$(tput setaf
8)\002@\001$(tput sgr0)\002\001$(tput setaf 2)\002\h\001$(tput
sgr0)\002\001$(tput setaf 8)\002:\001$(tput sgr0)\002\001$(tput setaf
3)\002\w\001$(tput sgr0)\002\001$([[ $(dirs -p | wc -l) -gt 1 ]] &&
echo "\002 [\001$(dirs -p | wc -l | xargs)\001]\002")
(${__git_ps1_branch_name}|u=) \\$

You will notice a LOT of "\001" and "\002"s.

But none near the " (${__git_ps1_branch_name}|u=) " part.

To ensure that the current code in
https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L262
is actually sending out the "\001" and "\002", you can run
echo $PS1 | hexdump -C
I'm on MacOS, so the equivalent command that shows the hex and ASCII
output may be needed elsewhere to show this.

The output I see is

000001f0  5c 30 30 32 5c 30 30 31  24 28 5b 5b 20 24 28 64  |\002\001$([[ $(d|
00000200  69 72 73 20 2d 70 20 7c  20 77 63 20 2d 6c 29 20  |irs -p | wc -l) |
00000210  2d 67 74 20 31 20 5d 5d  20 26 26 20 65 63 68 6f  |-gt 1 ]] && echo|
00000220  20 22 5c 30 30 32 20 5b  5c 30 30 31 24 28 64 69  | "\002 [\001$(di|
00000230  72 73 20 2d 70 20 7c 20  77 63 20 2d 6c 20 7c 20  |rs -p | wc -l | |
00000240  78 61 72 67 73 29 5c 30  30 31 5d 5c 30 30 32 22  |xargs)\001]\002"|
00000250  29 20 28 01 1b 5b 33 32  6d 02 24 7b 5f 5f 67 69  |) (..[32m.${__gi|
00000260  74 5f 70 73 31 5f 62 72  61 6e 63 68 5f 6e 61 6d  |t_ps1_branch_nam|
00000270  65 7d 01 1b 5b 30 6d 02  7c 75 3d 29 20 5c 5c 24  |e}..[0m.|u=) \\$|
00000280  0a                                                |.|


In the ASCII output, you can see my "\001" and "\002"s.

But in the hexadecimal output, you can see `01` and `02`.

The following patch fixes this, resulting in the "\001" and "\002"s
being present in the PS1 environment variable.



This is my first attempt to supply a patch to git considering it
pretty much never fails me!

If there is anything else needed, I hope I can provide it.

Regards.

Richard Quadling.

Comments

Junio C Hamano March 12, 2024, 5:25 a.m. UTC | #1
Richard Quadling <rquadling@gmail.com> writes:

> This is my first attempt to supply a patch to git considering it
> pretty much never fails me!
>
> If there is anything else needed, I hope I can provide it.

Thanks and welcome to the Git development community.

Documentation/SubmittingPatches is your friend to see what are
expected in a patch, covering how to pick a title of a patch, how
the changes are explained (and why it is so important to do so),
what's the procedure to "sign-it-off", etc.

Thanks.
diff mbox series

Patch

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 71f179cba3..6585164efe 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -259,10 +259,10 @@  __git_ps1_colorize_gitstring ()
  else
  # Using \001 and \002 around colors is necessary to prevent
  # issues with command line editing/browsing/completion!
- local c_red=$'\001\e[31m\002'
- local c_green=$'\001\e[32m\002'
- local c_lblue=$'\001\e[1;34m\002'
- local c_clear=$'\001\e[0m\002'
+ local c_red=$'\\001\e[31m\\002'
+ local c_green=$'\\001\e[32m\\002'
+ local c_lblue=$'\\001\e[1;34m\\002'
+ local c_clear=$'\\001\e[0m\\002'
  fi
  local bad_color=$c_red
  local ok_color=$c_green