{"thread":{"id":"44937","subject":"[PATCH 2/2] git-prompt.sh: rework of submodule indicator","startedAt":"2017-01-22T09:07:46Z","lastAt":"2017-01-22T09:07:46Z","messageCount":1,"participants":["Benjamin Fuchs"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"309925","messageId":"1485076037-14660-1-git-send-email-email@benjaminfuchs.de","threadId":"44937","inReplyTo":null,"subject":"[PATCH 2/2] git-prompt.sh: rework of submodule indicator","fromName":"Benjamin Fuchs","fromEmail":"email@benjaminfuchs.de","sentAt":"2017-01-22T09:07:17Z","receivedAt":"2017-01-22T09:07:46Z","isPatch":true,"sender":{"key":"email@benjaminfuchs.de","avatar":null},"body":"Rework of the first patch. The prompt now will look like this:\n(+name:master). I tried to considere all suggestions.\nTests still missing.\n---\n contrib/completion/git-prompt.sh | 49 ++++++++++++++++++++--------------------\n 1 file changed, 24 insertions(+), 25 deletions(-)\n\ndiff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh\nindex 4c82e7f..c44b9a2 100644\n--- a/contrib/completion/git-prompt.sh\n+++ b/contrib/completion/git-prompt.sh\n@@ -95,8 +95,10 @@\n # repository level by setting bash.hideIfPwdIgnored to \"false\".\n #\n # If you would like __git_ps1 to indicate that you are in a submodule,\n-# set GIT_PS1_SHOWSUBMODULE. In this case a \"sub:\" will be added before\n-# the branch name.\n+# set GIT_PS1_SHOWSUBMODULE to a nonempty value. In this case the name\n+# of the submodule will be prepended to the branch name (e.g. module:master).\n+# The name will be prepended by \"+\" if the currently checked out submodule\n+# commit does not match the SHA-1 found in the index of the containing repository.\n \n # check whether printf supports -v\n __git_printf_supports_v=\n@@ -288,30 +290,27 @@ __git_eread ()\n \ttest -r \"$f\" && read \"$@\" <\"$f\"\n }\n \n-# __git_is_submodule\n-# Based on:\n-# http://stackoverflow.com/questions/7359204/git-command-line-know-if-in-submodule\n-__git_is_submodule ()\n-{\n-\tlocal git_dir parent_git module_name path\n-\t# Find the root of this git repo, then check if its parent dir is also a repo\n-\tgit_dir=\"$(git rev-parse --show-toplevel)\"\n-\tmodule_name=\"$(basename \"$git_dir\")\"\n-\tparent_git=\"$(cd \"$git_dir/..\" && git rev-parse --show-toplevel 2> /dev/null)\"\n-\tif [[ -n $parent_git ]]; then\n-\t\t# List all the submodule paths for the parent repo\n-\t\twhile read path\n-\t\tdo\n-\t\t\tif [[ \"$path\" != \"$module_name\" ]]; then continue; fi\n-\t\t\tif [[ -d \"$git_dir/../$path\" ]];    then return 0; fi\n-\t\tdone < <(cd $parent_git && git submodule --quiet foreach 'echo $path' 2> /dev/null)\n-    fi\n-    return 1\n-}\n-\n+# __git_ps1_submodule\n+#\n+# $1 - git_dir path\n+#\n+# Returns the name of the submodule if we are currently inside one. The name\n+# will be prepended by \"+\" if the currently checked out submodule commit does\n+# not match the SHA-1 found in the index of the containing repository.\n+# NOTE: git_dir looks like in a ...\n+# - submodule: \"GIT_PARENT/.git/modules/PATH_TO_SUBMODULE\"\n+# - parent: \"GIT_PARENT/.git\" (exception \".\" in .git)\n __git_ps1_submodule ()\n {\n-\t__git_is_submodule && printf \"sub:\"\n+\tlocal git_dir=\"$1\"\n+\tlocal submodule_name=\"$(basename \"$git_dir\")\"\n+\tif [ \"$submodule_name\" != \".git\" ] && [ \"$submodule_name\" != \".\" ]; then\n+\t\tlocal parent_top=\"${git_dir%.git*}\"\n+\t\tlocal submodule_top=\"${git_dir#*modules}\"\n+\t\tlocal status=\"\"\n+\t\tgit diff -C \"$parent_top\" --no-ext-diff --ignore-submodules=dirty --quiet -- \"$submodule_top\" 2>/dev/null || status=\"+\"\n+\t\tprintf \"$status$submodule_name:\"\n+\tfi\n }\n \n # __git_ps1 accepts 0 or 1 arguments (i.e., format string)\n@@ -545,7 +544,7 @@ __git_ps1 ()\n \n \tlocal sub=\"\"\n \tif [ -n \"${GIT_PS1_SHOWSUBMODULE}\" ]; then\n-\t\tsub=\"$(__git_ps1_submodule)\"\n+\t\tsub=\"$(__git_ps1_submodule $g)\"\n \tfi\n \n \tlocal f=\"$w$i$s$u\"\n-- \n2.7.4\n\n"}]}