Skip to content

Fix early wrapping of the command line when using git-prompt.sh #1910

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 1 commit into
base: master
Choose a base branch
from

Conversation

rquadling
Copy link

@rquadling rquadling commented Apr 28, 2025

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".

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

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 <[email protected]>
@rquadling
Copy link
Author

/allow

@rquadling
Copy link
Author

/submit

Copy link

gitgitgadget bot commented Apr 28, 2025

User rquadling already allowed to use GitGitGadget.

Copy link

gitgitgadget bot commented Apr 28, 2025

Submitted as [email protected]

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-1910/rquadling/master-v1

To fetch this version to local tag pr-1910/rquadling/master-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-1910/rquadling/master-v1

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.

1 participant