I expirienced that working with submodules can be confusing. A submodule can be anywhere in your parent git repository. While walking through the parent repository it would be good to have a reminder to know when you entered a submodule. This new indicator will show if you are in a submodule. The new prompt will look like this: (sub:master). If the currently checked out submodule commit does not match the SHA-1 found in the index of the containing repository a "+" will be prepended (+sub:master). Adding a new optional env variable for the new feature. Signed-off-by: Benjamin Fuchs <email@xxxxxxxxxxxxxxxx> --- contrib/completion/git-prompt.sh | 40 ++++++++++++++++++++++++++- t/t9903-bash-prompt.sh | 59 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh index 97eacd7..9a0c4af 100644 --- a/contrib/completion/git-prompt.sh +++ b/contrib/completion/git-prompt.sh @@ -93,6 +93,13 @@ # 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 to a nonempty value. In this case the name +# of the submodule will be prepended to the branch name (e.g. module:master). +# If you set GIT_PS1_SHOWDIRTYSTATE to a nonempty value, the name will be +# prepended by "+" if the currently checked out submodule commit does not +# match the SHA-1 found in the index of the containing repository. # check whether printf supports -v __git_printf_supports_v= @@ -284,6 +291,32 @@ __git_eread () test -r "$f" && read "$@" <"$f" } +# __git_ps1_submodule +# Requires the git dir set in $g by the caller. +# Returns the name of the submodule in $sub if we are currently inside one. +# The name will be prepended by "+" if the currently checked out submodule commit +# does not match the SHA-1 found in the index of the containing repository. +# NOTE: git_dir looks like in a ... +# - submodule: "GIT_PARENT/.git/modules/PATH_TO_SUBMODULE" +# - parent: "GIT_PARENT/.git" (exception "." in .git) +__git_ps1_submodule () +{ + local git_dir="$g" + local submodule_name="$(basename "$git_dir")" + if [ "$submodule_name" != ".git" ] && [ "$submodule_name" != "." ]; then + local status="" + if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] && + [ "$(git config --bool bash.showDirtyState)" != "false" ] + then + local parent_top="${git_dir%.git*}" + local submodule_top="${git_dir#*modules/}" + git -C "$parent_top" diff --no-ext-diff --ignore-submodules=dirty --quiet -- "$submodule_top" 2>/dev/null || status="+" + fi + + sub="$status$submodule_name:" + fi +} + # __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 +546,13 @@ __git_ps1 () b="\${__git_ps1_branch_name}" fi + local sub="" + if [ -n "${GIT_PS1_SHOWSUBMODULE-}" ]; then + __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 diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh index 97c9b32..ac82c99 100755 --- a/t/t9903-bash-prompt.sh +++ b/t/t9903-bash-prompt.sh @@ -16,9 +16,22 @@ c_lblue='\\[\\e[1;34m\\]' c_clear='\\[\\e[0m\\]' test_expect_success 'setup for prompt tests' ' + mkdir .subrepo && + (cd .subrepo && + git init && + echo 1 >file && + git add file && + git commit -m initial && + git checkout -b dirty && + echo 2 >file && + git commit -m "dirty branch" file + ) && git init otherrepo && echo 1 >file && git add file && + git submodule add ./.subrepo sub && + git -C sub checkout master && + git add sub && test_tick && git commit -m initial && git tag -a -m msg1 t1 && @@ -755,4 +768,50 @@ test_expect_success 'prompt - hide if pwd ignored - inside gitdir (stderr)' ' test_cmp expected "$actual" ' +test_expect_success 'prompt - submodule indicator' ' + printf " (sub:master)" >expected && + ( + cd sub && + GIT_PS1_SHOWSUBMODULE=y && + __git_ps1 >"$actual" + ) && + test_cmp expected "$actual" +' + +test_expect_success 'prompt - submodule indicator - disabled' ' + printf " (master)" >expected && + ( + cd sub && + GIT_PS1_SHOWSUBMODULE= && + __git_ps1 >"$actual" + ) && + test_cmp expected "$actual" +' + +test_expect_success 'prompt - submodule indicator - dirty status indicator' ' + printf " (+sub:dirty)" >expected && + git -C sub checkout dirty && + test_when_finished "git -C sub checkout master" && + ( + cd sub && + GIT_PS1_SHOWSUBMODULE=y && + GIT_PS1_SHOWDIRTYSTATE=y && + __git_ps1 >"$actual" + ) && + test_cmp expected "$actual" +' + +test_expect_success 'prompt - submodule indicator - dirty status indicator disabled' ' + printf " (sub:dirty)" >expected && + git -C sub checkout dirty && + test_when_finished "git -C sub checkout master" && + ( + cd sub && + GIT_PS1_SHOWSUBMODULE=y && + GIT_PS1_SHOWDIRTYSTATE= && + __git_ps1 >"$actual" + ) && + test_cmp expected "$actual" +' + test_done -- 2.7.4