From: "Avi Halachmi (:avih) via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C. Hamano" <gitster@pobox.com>,
"brian m. carlson" <sandals@crustytoothpaste.net>,
Patrick Steinhardt <ps@pks.im>,
Eric Sunshine <sunshine@sunshineco.com>,
Avi Halachmi <avihpit@yahoo.com>,
"Avi Halachmi (:avih)" <avihpit@yahoo.com>
Subject: [PATCH v4 7/8] git-prompt: ta-da! document usage in other shells
Date: Tue, 20 Aug 2024 01:48:31 +0000 [thread overview]
Message-ID: <cd20b830b24f236ee348ec549a7ae1e499f8c187.1724118513.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1750.v4.git.git.1724118513.gitgitgadget@gmail.com>
From: "Avi Halachmi (:avih)" <avihpit@yahoo.com>
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) <avihpit@yahoo.com>
---
contrib/completion/git-prompt.sh | 33 ++++++++++++++++++++++++++++++--
1 file changed, 31 insertions(+), 2 deletions(-)
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh
index c3dd38f847c..6be2f1dd901 100644
--- a/contrib/completion/git-prompt.sh
+++ b/contrib/completion/git-prompt.sh
@@ -8,8 +8,8 @@
# To enable:
#
# 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
-# 2) Add the following line to your .bashrc/.zshrc:
-# source ~/.git-prompt.sh
+# 2) Add the following line to your .bashrc/.zshrc/.profile:
+# . ~/.git-prompt.sh # dot path/to/this-file
# 3a) Change your PS1 to call __git_ps1 as
# command-substitution:
# Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
@@ -30,6 +30,8 @@
# Optionally, you can supply a third argument with a printf
# format string to finetune the output of the branch status
#
+# See notes below about compatibility with other shells.
+#
# The repository status will be displayed only if you are currently in a
# git repository. The %s token is the placeholder for the shown status.
#
@@ -106,6 +108,33 @@
# directory is set up to be ignored by git, then set
# GIT_PS1_HIDE_IF_PWD_IGNORED to a nonempty value. Override this on the
# repository level by setting bash.hideIfPwdIgnored to "false".
+#
+# Compatibility with other shells (beyond bash/zsh):
+#
+# We require posix-ish shell plus "local" support, which is most
+# shells (even pdksh), but excluding ksh93 (because no "local").
+#
+# Prompt integration might differ between shells, but the gist is
+# to load it once on shell init with '. path/to/git-prompt.sh',
+# set GIT_PS1* vars once as needed, and either place $(__git_ps1..)
+# inside PS1 once (0/1 args), or, before each prompt is displayed,
+# call __git_ps1 (2/3 args) which sets PS1 with the status embedded.
+#
+# Many shells support the 1st method of command substitution,
+# though some might need to first enable cmd substitution in PS1.
+#
+# When using colors, each escape sequence is wrapped between byte
+# values 1 and 2 (control chars SOH, STX, respectively), which are
+# invisible at the output, but for bash/readline they mark 0-width
+# strings (SGR color sequences) when calculating the on-screen
+# prompt width, to maintain correct input editing at the prompt.
+#
+# Currently there's no support for different markers, so if editing
+# behaves weird when using colors in __git_ps1, then the solution
+# is either to disable colors, or, in some shells which only care
+# about the width of the last prompt line (e.g. busybox-ash),
+# ensure the git output is not at the last line, maybe like so:
+# PS1='\n\w \u@\h$(__git_ps1 " (%s)")\n\$ '
# check whether printf supports -v
__git_printf_supports_v=
--
gitgitgadget
next prev parent reply other threads:[~2024-08-20 1:48 UTC|newest]
Thread overview: 80+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-23 19:18 [PATCH 0/8] git-prompt: support more shells Avi Halachmi via GitGitGadget
2024-07-23 19:18 ` [PATCH 1/8] git-prompt: use here-doc instead of here-string Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` [PATCH 2/8] git-prompt: fix uninitialized variable Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` [PATCH 3/8] git-prompt: don't use shell arrays Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` [PATCH 4/8] git-prompt: replace [[...]] with standard code Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` [PATCH 5/8] git-prompt: add some missing quotes Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:40 ` Junio C Hamano
2024-07-24 0:47 ` avih
2024-07-23 19:18 ` [PATCH 6/8] git-prompt: add fallback for shells without $'...' Avi Halachmi (:avih) via GitGitGadget
2024-07-23 20:25 ` Junio C Hamano
2024-07-24 2:08 ` avih
2024-07-25 11:27 ` avih
2024-07-25 13:28 ` avih
2024-07-25 16:24 ` Junio C Hamano
2024-07-25 20:03 ` avih
2024-07-25 16:19 ` Junio C Hamano
2024-08-14 18:09 ` avih
2024-08-14 19:32 ` Junio C Hamano
2024-08-15 4:17 ` avih
2024-08-15 12:44 ` Junio C Hamano
2024-07-23 19:18 ` [PATCH 7/8] git-prompt: ta-da! document usage in other shells Avi Halachmi (:avih) via GitGitGadget
2024-07-23 19:18 ` [PATCH 8/8] git-prompt: support custom 0-width PS1 markers Avi Halachmi (:avih) via GitGitGadget
2024-07-23 22:50 ` [PATCH 0/8] git-prompt: support more shells brian m. carlson
2024-07-24 2:41 ` avih
2024-07-24 15:29 ` Junio C Hamano
2024-07-24 17:08 ` avih
2024-07-24 17:13 ` Junio C Hamano
2024-08-15 13:14 ` [PATCH v2 0/8] git-prompt: support more shells v2 Avi Halachmi via GitGitGadget
2024-08-15 13:14 ` [PATCH v2 1/8] git-prompt: use here-doc instead of here-string Avi Halachmi (:avih) via GitGitGadget
2024-08-16 8:50 ` Patrick Steinhardt
2024-08-16 9:37 ` avih
2024-08-16 10:52 ` Patrick Steinhardt
2024-08-15 13:14 ` [PATCH v2 2/8] git-prompt: fix uninitialized variable Avi Halachmi (:avih) via GitGitGadget
2024-08-15 13:14 ` [PATCH v2 3/8] git-prompt: don't use shell arrays Avi Halachmi (:avih) via GitGitGadget
2024-08-16 8:50 ` Patrick Steinhardt
2024-08-16 9:53 ` avih
2024-08-16 10:52 ` Patrick Steinhardt
2024-08-16 11:35 ` avih
2024-08-16 12:38 ` Patrick Steinhardt
2024-08-15 13:14 ` [PATCH v2 4/8] git-prompt: replace [[...]] with standard code Avi Halachmi (:avih) via GitGitGadget
2024-08-15 16:27 ` Junio C Hamano
2024-08-16 10:36 ` avih
2024-08-16 16:42 ` Junio C Hamano
2024-08-15 13:14 ` [PATCH v2 5/8] git-prompt: add some missing quotes Avi Halachmi (:avih) via GitGitGadget
2024-08-15 16:36 ` Junio C Hamano
2024-08-15 17:35 ` avih
2024-08-15 19:15 ` Junio C Hamano
2024-08-15 19:53 ` avih
2024-08-15 13:14 ` [PATCH v2 6/8] git-prompt: don't use shell $'...' Avi Halachmi (:avih) via GitGitGadget
2024-08-15 13:14 ` [PATCH v2 7/8] git-prompt: ta-da! document usage in other shells Avi Halachmi (:avih) via GitGitGadget
2024-08-16 8:50 ` Patrick Steinhardt
2024-08-16 9:59 ` avih
2024-08-15 13:14 ` [PATCH v2 8/8] git-prompt: support custom 0-width PS1 markers Avi Halachmi (:avih) via GitGitGadget
2024-08-15 18:48 ` [PATCH v2 0/8] git-prompt: support more shells v2 Junio C Hamano
2024-08-16 8:50 ` Patrick Steinhardt
2024-08-17 9:25 ` [PATCH v3 0/8] git-prompt: support more shells v3 Avi Halachmi via GitGitGadget
2024-08-17 9:25 ` [PATCH v3 1/8] git-prompt: use here-doc instead of here-string Avi Halachmi (:avih) via GitGitGadget
2024-08-17 9:25 ` [PATCH v3 2/8] git-prompt: fix uninitialized variable Avi Halachmi (:avih) via GitGitGadget
2024-08-17 9:25 ` [PATCH v3 3/8] git-prompt: don't use shell arrays Avi Halachmi (:avih) via GitGitGadget
2024-08-17 9:25 ` [PATCH v3 4/8] git-prompt: replace [[...]] with standard code Avi Halachmi (:avih) via GitGitGadget
2024-08-17 9:25 ` [PATCH v3 5/8] git-prompt: add some missing quotes Avi Halachmi (:avih) via GitGitGadget
2024-08-17 9:38 ` Eric Sunshine
2024-08-17 10:07 ` avih
2024-08-17 16:28 ` Junio C Hamano
2024-08-17 18:02 ` avih
2024-08-17 9:25 ` [PATCH v3 6/8] git-prompt: don't use shell $'...' Avi Halachmi (:avih) via GitGitGadget
2024-08-17 9:25 ` [PATCH v3 7/8] git-prompt: ta-da! document usage in other shells Avi Halachmi (:avih) via GitGitGadget
2024-08-17 9:26 ` [PATCH v3 8/8] git-prompt: support custom 0-width PS1 markers Avi Halachmi (:avih) via GitGitGadget
2024-08-20 1:48 ` [PATCH v4 0/8] git-prompt: support more shells v4 Avi Halachmi via GitGitGadget
2024-08-20 1:48 ` [PATCH v4 1/8] git-prompt: use here-doc instead of here-string Avi Halachmi (:avih) via GitGitGadget
2024-08-20 1:48 ` [PATCH v4 2/8] git-prompt: fix uninitialized variable Avi Halachmi (:avih) via GitGitGadget
2024-08-20 1:48 ` [PATCH v4 3/8] git-prompt: don't use shell arrays Avi Halachmi (:avih) via GitGitGadget
2024-08-20 1:48 ` [PATCH v4 4/8] git-prompt: replace [[...]] with standard code Avi Halachmi (:avih) via GitGitGadget
2024-08-20 1:48 ` [PATCH v4 5/8] git-prompt: add some missing quotes Avi Halachmi (:avih) via GitGitGadget
2024-08-20 1:48 ` [PATCH v4 6/8] git-prompt: don't use shell $'...' Avi Halachmi (:avih) via GitGitGadget
2024-08-20 1:48 ` Avi Halachmi (:avih) via GitGitGadget [this message]
2024-08-20 1:48 ` [PATCH v4 8/8] git-prompt: support custom 0-width PS1 markers Avi Halachmi (:avih) via GitGitGadget
2024-08-20 15:32 ` [PATCH v4 0/8] git-prompt: support more shells v4 Junio C Hamano
2024-08-20 15:47 ` avih
2024-08-28 19:54 ` avih
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cd20b830b24f236ee348ec549a7ae1e499f8c187.1724118513.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=avihpit@yahoo.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=ps@pks.im \
--cc=sandals@crustytoothpaste.net \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).