git-prompt.sh: add submodule indicator
PATCH 1/1 git-prompt.sh: add submodule indicator
I expirienced that working with submodules can be confusing. This indicator will make you notice very easy when you switch into a submodule. The new prompt will look like this: (sub:master)
Adding a new optional env variable for the new feature.
--- contrib/completion/git-prompt.sh | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
contrib/completion/git-prompt.sh +36 -1
@@ -93,6 +93,10 @@ # 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". +# +# If you would like __git_ps1 to indicate that you are in a submodule, +# set GIT_PS1_SHOWSUBMODULE. In this case a "sub:" will be added before +# the branch name. # check whether printf supports -v __git_printf_supports_v=
@@ -284,6 +288,32 @@ __git_eread () test -r "$f" && read "$@" <"$f" } +# __git_is_submodule +# Based on: +# http://stackoverflow.com/questions/7359204/git-command-line-know-if-in-submodule +__git_is_submodule () +{ + local git_dir parent_git module_name path + # Find the root of this git repo, then check if its parent dir is also a repo + git_dir="$(git rev-parse --show-toplevel)" + module_name="$(basename "$git_dir")" + parent_git="$(cd "$git_dir/.." && git rev-parse --show-toplevel 2> /dev/null)" + if [[ -n $parent_git ]]; then + # List all the submodule paths for the parent repo + while read path + do + if [[ "$path" != "$module_name" ]]; then continue; fi + if [[ -d "$git_dir/../$path" ]]; then return 0; fi + done < <(cd $parent_git && git submodule --quiet foreach 'echo $path' 2> /dev/null) + fi + return 1 +} + +__git_ps1_submodule () +{ + __git_is_submodule && printf "sub:" +} + # __git_ps1 accepts 0 or 1 arguments (i.e., format string) # when called from PS1 using command substitution # in this mode it prints text to add to bash PS1 prompt (includes branch name)
@@ -513,8 +543,13 @@ __git_ps1 () b="\${__git_ps1_branch_name}" fi + local sub="" + if [ -n "${GIT_PS1_SHOWSUBMODULE}" ]; then + sub="$(__git_ps1_submodule)" + fi + local f="$w$i$s$u" - local gitstring="$c$b${f:+$z$f}$r$p" + local gitstring="$c$sub$b${f:+$z$f}$r$p" if [ $pcmode = yes ]; then if [ "${__git_printf_supports_v-}" != yes ]; then
Signature · 1 line
2.7.4
Quoted 6 lines · Benjamin Fuchs
I expirienced that working with submodules can be confusing. This indicator will make you notice very easy when you switch into a submodule. The new prompt will look like this: (sub:master)
Adding a new optional env variable for the new feature.Signed-off-by Benjamin Fuchs <[email protected]>
Thanks for making submodules better :)
Relevant tangent, just posted today:
https://public-inbox.org/git/[email protected]/T/#u
Quoted 23 lines
--- contrib/completion/git-prompt.sh | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)contrib/completion/git-prompt.sh +4
-0@@ -93,6 +93,10 @@ # 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". +# +# If you would like __git_ps1 to indicate that you are in a submodule, +# set GIT_PS1_SHOWSUBMODULE. In this case a "sub:" will be added before +# the branch name. # check whether printf supports -v __git_printf_supports_v=@@ -284,6 +288,32 @@ __git_eread () test -r "$f" && read "$@" <"$f" }
contrib/completion/git-prompt.sh __git_eread () · line 300
Quoted 10 lines
contrib/completion/git-prompt.sh +10
-0+# __git_is_submodule +# Based on: +# http://stackoverflow.com/questions/7359204/git-command-line-know-if-in-submodule +__git_is_submodule () +{ + local git_dir parent_git module_name path + # Find the root of this git repo, then check if its parent dir is also a repo + git_dir="$(git rev-parse --show-toplevel)" + module_name="$(basename "$git_dir")" + parent_git="$(cd "$git_dir/.." && git rev-parse --show-toplevel 2> /dev/null)"
I wonder if we want to have better plumbing commands for submodules here as well:
Here we only check if we have an embedded git repository, which may not be a submodule. It could be a standalone repo that just happens to be inside another. It could be a fake submodule [1], though I think the last time I brought these up,
the upstream Git community considered these fake submodules are bug not worth fixing.
And this doesn't cover the case that I addressed in the RFC-ish patch above:
$ git submodule deinit --all $ cd <submodule> && git status # in an ideal world this tells you: # "You are in an un-populated submodule. To change the submodule state..."
So I guess this is a good first approximation that actually gets most of the cases right,
thereby helping a lot of people. But I wonder about these cornercases as well?
[1] debuggable.com/posts/git-fake-submodules:4b563ee4-f3cc-4061-967e-0e48cbdd56cb
Thanks,
Stefan
I'm not well-versed in submodules, so I won't comment on whether this is the right way to determine that a repository is a submodule, but I was surprised to see how much you have to work to find this out.
My comments will mostly focus on how to eliminate or at least limit the scope of subshells and command executions, because fork()s and exec()s are rather expensive on Windows.
Quoted 9 lines · Benjamin Fuchs
I expirienced that working with submodules can be confusing. This indicator will make you notice very easy when you switch into a submodule. The new prompt will look like this: (sub:master)
Adding a new optional env variable for the new feature.Signed-off-by Benjamin Fuchs <[email protected]>--- contrib/completion/git-prompt.sh | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
Tests?
Quoted 16 lines
contrib/completion/git-prompt.sh +4
-0@@ -93,6 +93,10 @@ # 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". +# +# If you would like __git_ps1 to indicate that you are in a submodule, +# set GIT_PS1_SHOWSUBMODULE. In this case a "sub:" will be added before +# the branch name. # check whether printf supports -v __git_printf_supports_v=
contrib/completion/git-prompt.sh __git_eread () · line 294
Quoted 7 lines
contrib/completion/git-prompt.sh +4
-0@@ -284,6 +288,32 @@ __git_eread () test -r "$f" && read "$@" <"$f" } +# __git_is_submodule +# Based on: +# http://stackoverflow.com/questions/7359204/git-command-line-know-if-in-submodule +__git_is_submodule ()
Use the '__git_ps1' prefix in the function name, like the other functions.
contrib/completion/git-prompt.sh __git_eread () · line 298
Quoted 4 lines
contrib/completion/git-prompt.sh +4
-0+{ + local git_dir parent_git module_name path + # Find the root of this git repo, then check if its parent dir is also a repo + git_dir="$(git rev-parse --show-toplevel)"
1. This is a somewhat misleading variable name, because git_dir (with or without underscore or dash) refers to the path to the .git directory of a repository through the codebase, documentation and CLI options, not the top-level directory of the worktree.
2. In a bare repository or in the .git directory of a "regular" repository '--show-toplevel' doesn't output anything, leading to an empty $module_name below, which ultimately results in no submodule indicator.
As fas as behavior is concerned, this is good in the bare repository case, because as I understand it there is no such thing as a bare submodule. I'm not sure whether the submodule indicator should be displayed in the ".git dir of a submodule" case. I leave it up to you and Stefan to discuss.
However, the info about whether we are in a bare repository or .git dir is already availabe in certain variables, so we know upfront when the current repository can't be a submodule. In those cases this function should not be called only to spend several subshells and command executions to figure out what we already knew anyway.
3. The path to the .git directory of the current repository is already available in the (not particularly descriptively named) $g variable from __git_ps1. Perhaps you could use that variable instead, thus avoiding a subshell and executing a git command here.
contrib/completion/git-prompt.sh __git_eread () · line 299
Quoted 1 line
contrib/completion/git-prompt.sh +1
-0+ module_name="$(basename "$git_dir")"
This is executed even when there is no repository in the parent directories, but it's only needed when there _is_ a repo up there. Please move it into the conditional below, to avoid a subshell and command execution in the main code path.
Since this $git_dir comes directly from our own 'git rev-parse' do we have to be prepared for a Windows-style path there? If it were always a UNIX-style path, then we could strip all directories with shell parameter expansion, eliminating both the subshell and the basename execution.
contrib/completion/git-prompt.sh __git_eread () · line 300
Quoted 1 line
contrib/completion/git-prompt.sh +1
-0+ parent_git="$(cd "$git_dir/.." && git rev-parse --show-toplevel 2> /dev/null)"
Style nit: no space after redirection, i.e. it should be '2>/dev/null'.
More importantly, I don't think you really need this variable:
1. The existence of a parent git repository can be determined from 'git rev-parse's exit code alone.
2. When you run 'git submodule' below, you don't have to cd to the _top-level_ directory of the parent repository's worktree. You just have to cd to _any_ directory in the parent, and you can do that with 'git -C "$git_dir/.." submodule ...', without knowing the parent's top-level directory.
This means that you don't need 'git rev-parse's output, thus there is no need for the command substitution. Yet another subshell spared! :) However, then you have to be careful with changing directories, and should write it as 'git -C "$git_dir/.." rev-parse ...'.
contrib/completion/git-prompt.sh __git_eread () · line 301
Quoted 1 line
contrib/completion/git-prompt.sh +1
-0+ if [[ -n $parent_git ]]; then
Unquoted path.
contrib/completion/git-prompt.sh __git_eread () · line 307
Quoted 6 lines
contrib/completion/git-prompt.sh +6
-0+ # List all the submodule paths for the parent repo + while read path + do + if [[ "$path" != "$module_name" ]]; then continue; fi + if [[ -d "$git_dir/../$path" ]]; then return 0; fi + done < <(cd $parent_git && git submodule --quiet foreach 'echo $path' 2> /dev/null)
Unquoted path and space after redirection again.
This loop will get confused if one of the submodule paths contains a newline, though probably no one in their right mind would do something like that. Is it even possible to have a submodule name with newline in it?
I wonder whether it would be possible to move the conditions from the loop body to the shell command evaluated by 'submodule foreach', thus eliminating the loop entirely and making submodule detection newline-safe. Or whether it's worth the trouble...
contrib/completion/git-prompt.sh __git_eread () · line 309
Quoted 2 lines
contrib/completion/git-prompt.sh +2
-0+ fi + return 1
Indent with spaces, please use tabs instead.
contrib/completion/git-prompt.sh __git_eread () · line 315
Quoted 6 lines
contrib/completion/git-prompt.sh +6
-0+} + +__git_ps1_submodule () +{ + __git_is_submodule && printf "sub:" +}
This function is unnecessary.
First, it's so short that it's body can be safely inlined in its caller. Second, it _prints_ its output to stdout, forcing the caller to use a command substitution. You can simply assign the value to $sub based on __git_ps1_is_submodule()'s return value.
contrib/completion/git-prompt.sh __git_ps1 () · line 546
Quoted 9 lines
contrib/completion/git-prompt.sh +2
-0+ # __git_ps1 accepts 0 or 1 arguments (i.e., format string) # when called from PS1 using command substitution # in this mode it prints text to add to bash PS1 prompt (includes branch name)@@ -513,8 +543,13 @@ __git_ps1 () b="\${__git_ps1_branch_name}" fi + local sub=""
Thank you for _not_ introducing yet another one letter variable name :)
Quoted 13 lines
contrib/completion/git-prompt.sh +5
-2+ if [ -n "${GIT_PS1_SHOWSUBMODULE}" ]; then + sub="$(__git_ps1_submodule)" + fi + local f="$w$i$s$u" - local gitstring="$c$b${f:+$z$f}$r$p" + local gitstring="$c$sub$b${f:+$z$f}$r$p" if [ $pcmode = yes ]; then if [ "${__git_printf_supports_v-}" != yes ]; then --2.7.4