{"thread":{"id":"61090","subject":"Using git-prompt.sh is wrapping text to early in Bash.","startedAt":"2024-03-11T13:34:26Z","lastAt":"2024-03-12T05:25:21Z","messageCount":2,"participants":["Richard Quadling","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"490356","messageId":"CAKUjMCUEmrC8XfVKC4AkCzDyqWziPjDh5EiNmx02xQF=SVBVzQ@mail.gmail.com","threadId":"61090","inReplyTo":null,"subject":"Using git-prompt.sh is wrapping text to early in Bash.","fromName":"Richard Quadling","fromEmail":"rquadling@gmail.com","sentAt":"2024-03-11T13:34:14Z","receivedAt":"2024-03-11T13:34:26Z","isPatch":false,"sender":{"key":"rquadling@gmail.com","avatar":"https://gravatar.com/avatar/4c385f24d4f13c86b9c3e54c61b2532189c1ffb3099a8e63738fd75de90b20ba?d=mp&s=160"},"body":"The changes introduced in\nhttps://github.com/git/git/commit/7ee1af8cb8b97385fc4e603bc024d877def5adb4\ncauses early wrapping of the command line whilst typing your commands\nin Bash, when you are using the git-prompt.sh as part of the\nPS1_COMMAND. I do not know about other platforms or shells.\n\nThe following setup is what I have:\n\nGIT_PS1_COMPRESSSPARSESTATE=\nGIT_PS1_DESCRIBE_STYLE=\nGIT_PS1_HIDE_IF_PWD_IGNORED=yes\nGIT_PS1_OMITSPARSESTATE=\nGIT_PS1_SHOWCOLORHINTS=yes\nGIT_PS1_SHOWDIRTYSTATE=yes\nGIT_PS1_SHOWSTASHSTATE=yes\nGIT_PS1_SHOWUNTRACKEDFILES=yes\nGIT_PS1_SHOWUPSTREAM=\"auto\"\nGIT_PS1_STATESEPARATOR=\" \"\nPS1_PROMPT=' \\\\$ '\nexport PROMPT_COMMAND='__git_ps1 \"$PS1_COMBINED\" \"$PS1_PROMPT\";'\n\nChange to any git directory:\ncd ~/dev/GitHub/git\n\nIf all is setup properly for git-prompt.sh, you should see prompt similar to:\n~/dev/GitHub/git (master|u=) $\ndepending upon your actual prompt.\n\nConfirm things by running:\n$ git remote -v\n\nand I get:\norigin git@github.com:git/git.git (fetch)\norigin git@github.com:git/git.git (push)\n\nNow start typing a LONG command.\necho 'This is a really long command that, as you start typing will\nsuddenly wrap around to the same line before it reaches the end of the\nline'\n\nThe issue can be more greatly seen if you examine what is actually\nadded to the prompt.\n\nMy prompt has a more elements in it (datetime, shell level, count of\npushd's, whoiam@machinename.\n\nSo, if I run:\necho $PS1\n\nI get ...\n\n\\001$(code=${?##0};echo ${code:+$(tput setaf 9)\\002(${code})\\\n})\\001$(tput sgr0)\\002\\001$(tput setaf 6)\\002${PS1_SHELL_IN}\\001$(tput\nsetaf 14)\\002$SHLVL\\001$(tput setaf 6)\\002${PS1_SHELL_OUT} \\001$(tput\nsgr0)\\002\\001$(tput setaf 5)\\002\\D{%Y-%m-%d %H:%M:%S} \\001$(tput\nsgr0)\\002\\001$(tput setaf 6)\\002\\u\\001$(tput sgr0)\\002\\001$(tput setaf\n8)\\002@\\001$(tput sgr0)\\002\\001$(tput setaf 2)\\002\\h\\001$(tput\nsgr0)\\002\\001$(tput setaf 8)\\002:\\001$(tput sgr0)\\002\\001$(tput setaf\n3)\\002\\w\\001$(tput sgr0)\\002\\001$([[ $(dirs -p | wc -l) -gt 1 ]] &&\necho \"\\002 [\\001$(dirs -p | wc -l | xargs)\\001]\\002\")\n(${__git_ps1_branch_name}|u=) \\\\$\n\nYou will notice a LOT of \"\\001\" and \"\\002\"s.\n\nBut none near the \" (${__git_ps1_branch_name}|u=) \" part.\n\nTo ensure that the current code in\nhttps://github.com/git/git/blob/master/contrib/completion/git-prompt.sh#L262\nis actually sending out the \"\\001\" and \"\\002\", you can run\necho $PS1 | hexdump -C\nI'm on MacOS, so the equivalent command that shows the hex and ASCII\noutput may be needed elsewhere to show this.\n\nThe output I see is\n\n000001f0  5c 30 30 32 5c 30 30 31  24 28 5b 5b 20 24 28 64  |\\002\\001$([[ $(d|\n00000200  69 72 73 20 2d 70 20 7c  20 77 63 20 2d 6c 29 20  |irs -p | wc -l) |\n00000210  2d 67 74 20 31 20 5d 5d  20 26 26 20 65 63 68 6f  |-gt 1 ]] && echo|\n00000220  20 22 5c 30 30 32 20 5b  5c 30 30 31 24 28 64 69  | \"\\002 [\\001$(di|\n00000230  72 73 20 2d 70 20 7c 20  77 63 20 2d 6c 20 7c 20  |rs -p | wc -l | |\n00000240  78 61 72 67 73 29 5c 30  30 31 5d 5c 30 30 32 22  |xargs)\\001]\\002\"|\n00000250  29 20 28 01 1b 5b 33 32  6d 02 24 7b 5f 5f 67 69  |) (..[32m.${__gi|\n00000260  74 5f 70 73 31 5f 62 72  61 6e 63 68 5f 6e 61 6d  |t_ps1_branch_nam|\n00000270  65 7d 01 1b 5b 30 6d 02  7c 75 3d 29 20 5c 5c 24  |e}..[0m.|u=) \\\\$|\n00000280  0a                                                |.|\n\n\nIn the ASCII output, you can see my \"\\001\" and \"\\002\"s.\n\nBut in the hexadecimal output, you can see `01` and `02`.\n\nThe following patch fixes this, resulting in the \"\\001\" and \"\\002\"s\nbeing present in the PS1 environment variable.\n\ndiff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh\nindex 71f179cba3..6585164efe 100644\n--- a/contrib/completion/git-prompt.sh\n+++ b/contrib/completion/git-prompt.sh\n@@ -259,10 +259,10 @@ __git_ps1_colorize_gitstring ()\n  else\n  # Using \\001 and \\002 around colors is necessary to prevent\n  # issues with command line editing/browsing/completion!\n- local c_red=$'\\001\\e[31m\\002'\n- local c_green=$'\\001\\e[32m\\002'\n- local c_lblue=$'\\001\\e[1;34m\\002'\n- local c_clear=$'\\001\\e[0m\\002'\n+ local c_red=$'\\\\001\\e[31m\\\\002'\n+ local c_green=$'\\\\001\\e[32m\\\\002'\n+ local c_lblue=$'\\\\001\\e[1;34m\\\\002'\n+ local c_clear=$'\\\\001\\e[0m\\\\002'\n  fi\n  local bad_color=$c_red\n  local ok_color=$c_green\n\n\nThis is my first attempt to supply a patch to git considering it\npretty much never fails me!\n\nIf there is anything else needed, I hope I can provide it.\n\nRegards.\n\nRichard Quadling.\n"},{"id":"490445","messageId":"xmqqsf0wkon7.fsf@gitster.g","threadId":"61090","inReplyTo":"CAKUjMCUEmrC8XfVKC4AkCzDyqWziPjDh5EiNmx02xQF=SVBVzQ@mail.gmail.com","subject":"Re: Using git-prompt.sh is wrapping text to early in Bash.","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2024-03-12T05:25:16Z","receivedAt":"2024-03-12T05:25:21Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Richard Quadling <rquadling@gmail.com> writes:\n\n> This is my first attempt to supply a patch to git considering it\n> pretty much never fails me!\n>\n> If there is anything else needed, I hope I can provide it.\n\nThanks and welcome to the Git development community.\n\nDocumentation/SubmittingPatches is your friend to see what are\nexpected in a patch, covering how to pick a title of a patch, how\nthe changes are explained (and why it is so important to do so),\nwhat's the procedure to \"sign-it-off\", etc.\n\nThanks.\n"}]}