public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
* [PATCH] Fix early wrapping of the command line when using git-prompt.sh
@ 2025-04-28 12:33 Richard Quadling via GitGitGadget
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Quadling via GitGitGadget @ 2025-04-28 12:33 UTC (permalink / raw)
  To: git; +Cc: Richard Quadling, Richard Quadling

From: Richard Quadling <RQuadling@GMail•com>

When running git-prompt in Bash, the lack of \001 and \002 causes
the command line to wrap early.

The issue is the current \001 and \002 were not themselves escaped
and so resulted in binary 0b1 and 0b10 being present, rather than
the strings "\001" and "\002".

Signed-off-by: Richard Quadling <RQuadling@GMail•com>
---
    Fix early wrapping of the command line when using git-prompt.sh
    
    When running git-prompt in Bash, the lack of \001 and \002 causes the
    command line to wrap early.
    
    The issue is the current \001 and \002 were not themselves escaped and
    so resulted in binary 0b1 and 0b10 being present, rather than the
    strings "\001" and "\002".

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1910%2Frquadling%2Fmaster-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1910/rquadling/master-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1910

 contrib/completion/git-prompt.sh | 2 +-
 t/t9903-bash-prompt.sh           | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index 6186c474ba7..4a64aca3eda 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -147,7 +147,7 @@ printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1
 
 # like __git_SOH=$'\001' etc but works also in shells without $'...'
 eval "$(printf '
-	__git_SOH="\001" __git_STX="\002" __git_ESC="\033"
+	__git_SOH="\\001" __git_STX="\\002" __git_ESC="\033"
 	__git_LF="\n" __git_CRLF="\r\n"
 ')"
 
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
index d667dda654e..fc6fdd36c69 100755
--- a/t/t9903-bash-prompt.sh
+++ b/t/t9903-bash-prompt.sh
@@ -13,10 +13,10 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 . "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
 
 actual="$TRASH_DIRECTORY/actual"
-c_red='\001\e[31m\002'
-c_green='\001\e[32m\002'
-c_lblue='\001\e[1;34m\002'
-c_clear='\001\e[0m\002'
+c_red='\\001\e[31m\\002'
+c_green='\\001\e[32m\\002'
+c_lblue='\\001\e[1;34m\\002'
+c_clear='\\001\e[0m\\002'
 
 test_expect_success 'setup for prompt tests' '
 	git init otherrepo &&

base-commit: f65182a99e545d2f2bc22e6c1c2da192133b16a3
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fix early wrapping of the command line when using git-prompt.sh
@ 2025-04-28 12:58 Richard Quadling
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Quadling @ 2025-04-28 12:58 UTC (permalink / raw)
  To: gitgitgadget; +Cc: Richard Quadling, git

As an example:

The following is my prompt whilst in a Git Repo:

$ echo $PS1 | hexdump -C
00000000  5c 30 30 31 24 28 63 6f  64 65 3d 24 7b 3f 23 23  |\001$(code=${?##|
00000010  30 7d 3b 65 63 68 6f 20  24 7b 63 6f 64 65 3a 2b  |0};echo ${code:+|
00000020  24 28 74 70 75 74 20 73  65 74 61 66 20 39 29 5c  |$(tput setaf 9)\|
00000030  30 30 32 28 24 7b 63 6f  64 65 7d 29 5c 20 7d 29  |002(${code})\ })|
00000040  5c 30 30 31 24 28 74 70  75 74 20 73 67 72 30 29  |\001$(tput sgr0)|
00000050  5c 30 30 32 5c 30 30 31  24 28 74 70 75 74 20 73  |\002\001$(tput s|
00000060  65 74 61 66 20 36 29 5c  30 30 32 24 7b 50 53 31  |etaf 6)\002${PS1|
00000070  5f 53 48 45 4c 4c 53 5f  49 4e 3a 30 3a 24 53 48  |_SHELLS_IN:0:$SH|
00000080  4c 56 4c 7d 5c 30 30 31  24 28 74 70 75 74 20 73  |LVL}\001$(tput s|
00000090  65 74 61 66 20 31 34 29  5c 30 30 32 24 53 48 4c  |etaf 14)\002$SHL|
000000a0  56 4c 5c 30 30 31 24 28  74 70 75 74 20 73 65 74  |VL\001$(tput set|
000000b0  61 66 20 36 29 5c 30 30  32 24 7b 50 53 31 5f 53  |af 6)\002${PS1_S|
000000c0  48 45 4c 4c 53 5f 4f 55  54 3a 20 2d 24 53 48 4c  |HELLS_OUT: -$SHL|
000000d0  56 4c 7d 20 5c 30 30 31  24 28 74 70 75 74 20 73  |VL} \001$(tput s|
000000e0  67 72 30 29 5c 30 30 32  24 28 65 63 68 6f 20 2d  |gr0)\002$(echo -|
000000f0  65 20 24 7b 41 57 53 43  52 45 44 53 7d 29 5c 30  |e ${AWSCREDS})\0|
00000100  30 31 24 28 74 70 75 74  20 73 65 74 61 66 20 35  |01$(tput setaf 5|
00000110  29 5c 30 30 32 5c 44 7b  25 59 2d 25 6d 2d 25 64  |)\002\D{%Y-%m-%d|
00000120  20 25 48 3a 25 4d 3a 25  53 7d 20 5c 30 30 31 24  | %H:%M:%S} \001$|
00000130  28 74 70 75 74 20 73 67  72 30 29 5c 30 30 32 5c  |(tput sgr0)\002\|
00000140  30 30 31 24 28 74 70 75  74 20 73 65 74 61 66 20  |001$(tput setaf |
00000150  36 29 5c 30 30 32 5c 75  5c 30 30 31 24 28 74 70  |6)\002\u\001$(tp|
00000160  75 74 20 73 67 72 30 29  5c 30 30 32 5c 30 30 31  |ut sgr0)\002\001|
00000170  24 28 74 70 75 74 20 73  65 74 61 66 20 38 29 5c  |$(tput setaf 8)\|
00000180  30 30 32 40 5c 30 30 31  24 28 74 70 75 74 20 73  |002@\001$(tput s|
00000190  67 72 30 29 5c 30 30 32  5c 30 30 31 24 28 74 70  |gr0)\002\001$(tp|
000001a0  75 74 20 73 65 74 61 66  20 32 29 5c 30 30 32 5c  |ut setaf 2)\002\|
000001b0  68 5c 30 30 31 24 28 74  70 75 74 20 73 67 72 30  |h\001$(tput sgr0|
000001c0  29 5c 30 30 32 5c 30 30  31 24 28 74 70 75 74 20  |)\002\001$(tput |
000001d0  73 65 74 61 66 20 38 29  5c 30 30 32 3a 5c 30 30  |setaf 8)\002:\00|
000001e0  31 24 28 74 70 75 74 20  73 67 72 30 29 5c 30 30  |1$(tput sgr0)\00|
000001f0  32 5c 30 30 31 24 28 74  70 75 74 20 73 65 74 61  |2\001$(tput seta|
00000200  66 20 33 29 5c 30 30 32  5c 77 5c 30 30 31 24 28  |f 3)\002\w\001$(|
00000210  74 70 75 74 20 73 67 72  30 29 5c 30 30 32 5c 30  |tput sgr0)\002\0|
00000220  30 31 24 28 5b 5b 20 24  63 6f 75 6e 74 20 2d 67  |01$([[ $count -g|
00000230  74 20 31 20 5d 5d 20 26  26 20 65 63 68 6f 20 22  |t 1 ]] && echo "|
00000240  5c 30 30 32 20 5b 24 50  53 31 5f 50 41 54 48 5f  |\002 [$PS1_PATH_|
00000250  43 4f 55 4e 54 5d 5c 30  30 31 29 22 29 5c 30 30  |COUNT]\001)")\00|
00000260  32 20 28 01 1b 5b 33 32  6d 02 24 7b 5f 5f 67 69  |2 (..[32m.${__gi|
                   ^^                 ^^
00000270  74 5f 70 73 31 5f 62 72  61 6e 63 68 5f 6e 61 6d  |t_ps1_branch_nam|
00000280  65 7d 01 1b 5b 30 6d 02  20 01 1b 5b 31 3b 33 34  |e}..[0m. ..[1;34|
                ^^             ^^     ^^
00000290  6d 02 24 01 1b 5b 30 6d  02 3e 29 20 5c 5c 24 0a  |m.$..[0m.>) \\$.|
                   ^^              ^^
000002a0

With the fix

$ echo $PS1 | hexdump -C
00000000  5c 30 30 31 24 28 63 6f  64 65 3d 24 7b 3f 23 23  |\001$(code=${?##|
00000010  30 7d 3b 65 63 68 6f 20  24 7b 63 6f 64 65 3a 2b  |0};echo ${code:+|
00000020  24 28 74 70 75 74 20 73  65 74 61 66 20 39 29 5c  |$(tput setaf 9)\|
00000030  30 30 32 28 24 7b 63 6f  64 65 7d 29 5c 20 7d 29  |002(${code})\ })|
00000040  5c 30 30 31 24 28 74 70  75 74 20 73 67 72 30 29  |\001$(tput sgr0)|
00000050  5c 30 30 32 5c 30 30 31  24 28 74 70 75 74 20 73  |\002\001$(tput s|
00000060  65 74 61 66 20 36 29 5c  30 30 32 24 7b 50 53 31  |etaf 6)\002${PS1|
00000070  5f 53 48 45 4c 4c 53 5f  49 4e 3a 30 3a 24 53 48  |_SHELLS_IN:0:$SH|
00000080  4c 56 4c 7d 5c 30 30 31  24 28 74 70 75 74 20 73  |LVL}\001$(tput s|
00000090  65 74 61 66 20 31 34 29  5c 30 30 32 24 53 48 4c  |etaf 14)\002$SHL|
000000a0  56 4c 5c 30 30 31 24 28  74 70 75 74 20 73 65 74  |VL\001$(tput set|
000000b0  61 66 20 36 29 5c 30 30  32 24 7b 50 53 31 5f 53  |af 6)\002${PS1_S|
000000c0  48 45 4c 4c 53 5f 4f 55  54 3a 20 2d 24 53 48 4c  |HELLS_OUT: -$SHL|
000000d0  56 4c 7d 20 5c 30 30 31  24 28 74 70 75 74 20 73  |VL} \001$(tput s|
000000e0  67 72 30 29 5c 30 30 32  24 28 65 63 68 6f 20 2d  |gr0)\002$(echo -|
000000f0  65 20 24 7b 41 57 53 43  52 45 44 53 7d 29 5c 30  |e ${AWSCREDS})\0|
00000100  30 31 24 28 74 70 75 74  20 73 65 74 61 66 20 35  |01$(tput setaf 5|
00000110  29 5c 30 30 32 5c 44 7b  25 59 2d 25 6d 2d 25 64  |)\002\D{%Y-%m-%d|
00000120  20 25 48 3a 25 4d 3a 25  53 7d 20 5c 30 30 31 24  | %H:%M:%S} \001$|
00000130  28 74 70 75 74 20 73 67  72 30 29 5c 30 30 32 5c  |(tput sgr0)\002\|
00000140  30 30 31 24 28 74 70 75  74 20 73 65 74 61 66 20  |001$(tput setaf |
00000150  36 29 5c 30 30 32 5c 75  5c 30 30 31 24 28 74 70  |6)\002\u\001$(tp|
00000160  75 74 20 73 67 72 30 29  5c 30 30 32 5c 30 30 31  |ut sgr0)\002\001|
00000170  24 28 74 70 75 74 20 73  65 74 61 66 20 38 29 5c  |$(tput setaf 8)\|
00000180  30 30 32 40 5c 30 30 31  24 28 74 70 75 74 20 73  |002@\001$(tput s|
00000190  67 72 30 29 5c 30 30 32  5c 30 30 31 24 28 74 70  |gr0)\002\001$(tp|
000001a0  75 74 20 73 65 74 61 66  20 32 29 5c 30 30 32 5c  |ut setaf 2)\002\|
000001b0  68 5c 30 30 31 24 28 74  70 75 74 20 73 67 72 30  |h\001$(tput sgr0|
000001c0  29 5c 30 30 32 5c 30 30  31 24 28 74 70 75 74 20  |)\002\001$(tput |
000001d0  73 65 74 61 66 20 38 29  5c 30 30 32 3a 5c 30 30  |setaf 8)\002:\00|
000001e0  31 24 28 74 70 75 74 20  73 67 72 30 29 5c 30 30  |1$(tput sgr0)\00|
000001f0  32 5c 30 30 31 24 28 74  70 75 74 20 73 65 74 61  |2\001$(tput seta|
00000200  66 20 33 29 5c 30 30 32  5c 77 5c 30 30 31 24 28  |f 3)\002\w\001$(|
00000210  74 70 75 74 20 73 67 72  30 29 5c 30 30 32 5c 30  |tput sgr0)\002\0|
00000220  30 31 24 28 5b 5b 20 24  63 6f 75 6e 74 20 2d 67  |01$([[ $count -g|
00000230  74 20 31 20 5d 5d 20 26  26 20 65 63 68 6f 20 22  |t 1 ]] && echo "|
00000240  5c 30 30 32 20 5b 24 50  53 31 5f 50 41 54 48 5f  |\002 [$PS1_PATH_|
00000250  43 4f 55 4e 54 5d 5c 30  30 31 29 22 29 5c 30 30  |COUNT]\001)")\00|
00000260  32 20 28 5c 30 30 31 1b  5b 33 32 6d 5c 30 30 32  |2 (\001.[32m\002|
                                                                ^^^^     ^^^^
00000270  24 7b 5f 5f 67 69 74 5f  70 73 31 5f 62 72 61 6e  |${__git_ps1_bran|
00000280  63 68 5f 6e 61 6d 65 7d  5c 30 30 31 1b 5b 30 6d  |ch_name}\001.[0m|
                                                                     ^^^^
00000290  5c 30 30 32 20 5c 30 30  31 1b 5b 31 3b 33 34 6d  |\002 \001.[1;34m|
                                                             ^^^^ ^^^^
000002a0  5c 30 30 32 24 5c 30 30  31 1b 5b 30 6d 5c 30 30  |\002$\001.[0m\00|
                                                             ^^^^ ^^^^
000002b0  32 3e 29 20 5c 5c 24 0a                           |2>) \\$.|
000002b8

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-04-28 12:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-28 12:33 [PATCH] Fix early wrapping of the command line when using git-prompt.sh Richard Quadling via GitGitGadget
  -- strict thread matches above, loose matches on Subject: below --
2025-04-28 12:58 Richard Quadling

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox