Skip to content

Commit 0dbe3d3

Browse files
avihgitster
authored andcommitted
git-prompt: ta-da! document usage in other shells
With one big exception, git-prompt.sh should now be both almost posix compliant, and also compatible with most (posix-ish) shells. That exception is the use of "local" vars in functions, which happens extensively in the current code, and is not simple to replace with posix compliant code (but also not impossible). Luckily, almost all shells support "local" as used by the current code, with the notable exception of ksh93[u+m], but also the Schily minimal posix sh (pbosh), and yash in posix mode. See assessment below that "local" is likely the only blocker in those. So except mainly ksh93, git-prompt.sh now works in most shells: - bash, zsh, dash since at least 0.5.8, free/net bsd sh, busybox-ash, mksh, openbsd sh, pdksh(!), Schily extended Bourne sh (bosh), yash. which is quite nice. As an anecdote, replacing the 1st line in __git_ps1() (local exit=$?) with these 2 makes it work in all tested shells, even without "local": # handles only 0/1 args for simplicity. needs +5 LOC for any $# __git_e=$?; local exit="$__git_e" 2>/dev/null || {(eval 'local() { export "$@"; }'; __git_ps1 "$@"); return "$__git_e"; } Explanation: If the shell doesn't have the command "local", define our own function "local" which instead does plain (global) assignents. Then use __git_ps1 in a subshell to not clober the caller's vars. This happens to work because currently there are no name conflicts (shadow) at the code, initial value is not assumed (i.e. always doing either 'local x=...' or 'local x;... x=...'), and assigned initial values are quoted (local x="$y"), preventing word split and glob expansion (i.e. assignment context is not assumed). The last two (always init, quote values) seem to be enough to use "local" portably if supported, and otherwise shells indeed differ. Uses "eval", else shells with "local" may reject it during parsing. We don't need "export", but it's smaller than writing our own loop. While cute, this approach is not really sustainable because all the vars become global, which is hard to maintain without conflicts (but hey, it currently has no conflicts - without even trying...). However, regardless of being an anecdote, it provides some support to the assessment that "local" is the only blocker in those shells. Signed-off-by: Avi Halachmi (:avih) <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 29bcec8 commit 0dbe3d3

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

contrib/completion/git-prompt.sh

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
# To enable:
99
#
1010
# 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
11-
# 2) Add the following line to your .bashrc/.zshrc:
12-
# source ~/.git-prompt.sh
11+
# 2) Add the following line to your .bashrc/.zshrc/.profile:
12+
# . ~/.git-prompt.sh # dot path/to/this-file
1313
# 3a) Change your PS1 to call __git_ps1 as
1414
# command-substitution:
1515
# Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
@@ -30,6 +30,8 @@
3030
# Optionally, you can supply a third argument with a printf
3131
# format string to finetune the output of the branch status
3232
#
33+
# See notes below about compatibility with other shells.
34+
#
3335
# The repository status will be displayed only if you are currently in a
3436
# git repository. The %s token is the placeholder for the shown status.
3537
#
@@ -106,6 +108,33 @@
106108
# directory is set up to be ignored by git, then set
107109
# GIT_PS1_HIDE_IF_PWD_IGNORED to a nonempty value. Override this on the
108110
# repository level by setting bash.hideIfPwdIgnored to "false".
111+
#
112+
# Compatibility with other shells (beyond bash/zsh):
113+
#
114+
# We require posix-ish shell plus "local" support, which is most
115+
# shells (even pdksh), but excluding ksh93 (because no "local").
116+
#
117+
# Prompt integration might differ between shells, but the gist is
118+
# to load it once on shell init with '. path/to/git-prompt.sh',
119+
# set GIT_PS1* vars once as needed, and either place $(__git_ps1..)
120+
# inside PS1 once (0/1 args), or, before each prompt is displayed,
121+
# call __git_ps1 (2/3 args) which sets PS1 with the status embedded.
122+
#
123+
# Many shells support the 1st method of command substitution,
124+
# though some might need to first enable cmd substitution in PS1.
125+
#
126+
# When using colors, each escape sequence is wrapped between byte
127+
# values 1 and 2 (control chars SOH, STX, respectively), which are
128+
# invisible at the output, but for bash/readline they mark 0-width
129+
# strings (SGR color sequences) when calculating the on-screen
130+
# prompt width, to maintain correct input editing at the prompt.
131+
#
132+
# Currently there's no support for different markers, so if editing
133+
# behaves weird when using colors in __git_ps1, then the solution
134+
# is either to disable colors, or, in some shells which only care
135+
# about the width of the last prompt line (e.g. busybox-ash),
136+
# ensure the git output is not at the last line, maybe like so:
137+
# PS1='\n\w \u@\h$(__git_ps1 " (%s)")\n\$ '
109138

110139
# check whether printf supports -v
111140
__git_printf_supports_v=

0 commit comments

Comments
 (0)