Message ID | 4f77b7eb7f1110e47201b8c97c34a0cbcd14e24f.1721762306.git.gitgitgadget@gmail.com (mailing list archive) |
---|---|
State | Accepted |
Commit | b732e08671f037373f615a6d8509da2dbc476322 |
Headers | show |
Series | git-prompt: support more shells | expand |
"Avi Halachmi (:avih) via GitGitGadget" <gitgitgadget@gmail.com> writes: > From: "Avi Halachmi (:avih)" <avihpit@yahoo.com> > > The issues which this commit fixes are unlikely to be broken > in real life, but the fixes improve correctness, and would prevent > bugs in some uncommon cases, such as weird IFS values. > > Listing some portability guideline here for future reference. > > I'm leaving it to someone else to decide whether to include > it in the file itself, place is as a new file, or not. Check Documentation/CodingGuidelines; I think we have something to say about local var="val" construct to help dash. We allowed liberal uses of bash-ism in this file, as it was initially written for bash anyway. If we were rewriting the prompt scripts to be usable by other shells, great. But then we'd want to make sure it adheres to existing coding guidelines we have.
On Tuesday, July 23, 2024 at 10:40:30 PM GMT+3, Junio C Hamano <gitster@pobox.com> wrote: Thanks for the quick reply, and aplogies for my delayed reply. I replied at the github PR https://github.com/git/git/pull/1750 and didn't realize GitGitGadget doesn't forward it to the list. Then I accidentally sent email with HTML. 3rd time the charm... >> Listing some portability guideline here for future reference. >> >> I'm leaving it to someone else to decide whether to include >> it in the file itself, place is as a new file, or not. > Check Documentation/CodingGuidelines; I think we have something to > say about local var="val" construct to help dash. I wasn't aware of this file, but I should have searched for it before posting. Thanks for the pointer. As far as I can tell CodingGuidelines and my guideline align perfectly on every subject which both mention, down to nuances like that quoted initial value in "local", though each also has few subjects which the other doesn't. > ... If we were rewriting the prompt > scripts to be usable by other shells, great. But then we'd want to > make sure it adheres to existing coding guidelines we have. Not sure how many prompt scripts there are, but if you're referring to the scripts at contrib/completion then only git-prompt.sh is applicable in many shells and would gain by being portable. The others are shell-specific, so I wouldn't think they need be portable. As for git-prompt.sh, as far as I can tell, after this patchset, this file adheres to CodingGuidelines completely as far as correctness and compatibility go. However, regardless of not being aware of CodingGuidelines, the goal of this patchset was to improve compatibility and correctness, and I wouldn't have chosen or felt comfortable to included style changes ("'then' in new line" can have also portability implications, though not in the many shells which I tested). So no change in terms of style, it still diverges from the guidelines. Shall I add a commit which fixes style issues?
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh index 4781261f868..5d7f236fe48 100644 --- a/contrib/completion/git-prompt.sh +++ b/contrib/completion/git-prompt.sh @@ -246,7 +246,7 @@ __git_ps1_show_upstream () if [ -n "$count" ] && [ -n "$name" ]; then __git_ps1_upstream_name=$(git rev-parse \ --abbrev-ref "$upstream_type" 2>/dev/null) - if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then + if [ "$pcmode" = yes ] && [ "$ps1_expanded" = yes ]; then upstream="$upstream \${__git_ps1_upstream_name}" else upstream="$upstream ${__git_ps1_upstream_name}" @@ -278,12 +278,12 @@ __git_ps1_colorize_gitstring () 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 + local bad_color="$c_red" + local ok_color="$c_green" local flags_color="$c_lblue" local branch_color="" - if [ $detached = no ]; then + if [ "$detached" = no ]; then branch_color="$ok_color" else branch_color="$bad_color" @@ -360,7 +360,7 @@ __git_sequencer_status () __git_ps1 () { # preserve exit status - local exit=$? + local exit="$?" local pcmode=no local detached=no local ps1pc_start='\u@\h:\w ' @@ -379,7 +379,7 @@ __git_ps1 () ;; 0|1) printf_format="${1:-$printf_format}" ;; - *) return $exit + *) return "$exit" ;; esac @@ -427,7 +427,7 @@ __git_ps1 () rev_parse_exit_code="$?" if [ -z "$repo_info" ]; then - return $exit + return "$exit" fi local short_sha="" @@ -449,7 +449,7 @@ __git_ps1 () [ "$(git config --bool bash.hideIfPwdIgnored)" != "false" ] && git check-ignore -q . then - return $exit + return "$exit" fi local sparse="" @@ -499,7 +499,7 @@ __git_ps1 () case "$ref_format" in files) if ! __git_eread "$g/HEAD" head; then - return $exit + return "$exit" fi case $head in @@ -597,10 +597,10 @@ __git_ps1 () fi fi - local z="${GIT_PS1_STATESEPARATOR-" "}" + local z="${GIT_PS1_STATESEPARATOR- }" b=${b##refs/heads/} - if [ $pcmode = yes ] && [ $ps1_expanded = yes ]; then + if [ "$pcmode" = yes ] && [ "$ps1_expanded" = yes ]; then __git_ps1_branch_name=$b b="\${__git_ps1_branch_name}" fi @@ -612,7 +612,7 @@ __git_ps1 () local f="$h$w$i$s$u$p" local gitstring="$c$b${f:+$z$f}${sparse}$r${upstream}${conflict}" - if [ $pcmode = yes ]; then + if [ "$pcmode" = yes ]; then if [ "${__git_printf_supports_v-}" != yes ]; then gitstring=$(printf -- "$printf_format" "$gitstring") else @@ -623,5 +623,5 @@ __git_ps1 () printf -- "$printf_format" "$gitstring" fi - return $exit + return "$exit" }