Git 2.23
[gitweb.git] / contrib / completion / git-prompt.sh
index 64219e631ab9babdc9eb78137478bde2c2cabec8..1d510cd47bef49d918704a45e9592e0ffcb7ee0f 100644 (file)
@@ -82,6 +82,7 @@
 #     contains      relative to newer annotated tag (v1.6.3.2~35)
 #     branch        relative to newer tag or branch (master~4)
 #     describe      relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
+#     tag           relative to any older tag (v1.6.3.1-13-gdd42c2f)
 #     default       exactly matching tag
 #
 # If you would like a colored hint about the current dirty state, set
@@ -277,11 +278,43 @@ __git_ps1_colorize_gitstring ()
        r="$c_clear$r"
 }
 
+# Helper function to read the first line of a file into a variable.
+# __git_eread requires 2 arguments, the file path and the name of the
+# variable, in that order.
 __git_eread ()
 {
-       local f="$1"
-       shift
-       test -r "$f" && read "$@" <"$f"
+       test -r "$1" && IFS=$'\r\n' read "$2" <"$1"
+}
+
+# see if a cherry-pick or revert is in progress, if the user has committed a
+# conflict resolution with 'git commit' in the middle of a sequence of picks or
+# reverts then CHERRY_PICK_HEAD/REVERT_HEAD will not exist so we have to read
+# the todo file.
+__git_sequencer_status ()
+{
+       local todo
+       if test -f "$g/CHERRY_PICK_HEAD"
+       then
+               r="|CHERRY-PICKING"
+               return 0;
+       elif test -f "$g/REVERT_HEAD"
+       then
+               r="|REVERTING"
+               return 0;
+       elif __git_eread "$g/sequencer/todo" todo
+       then
+               case "$todo" in
+               p[\ \   ]|pick[\ \      ]*)
+                       r="|CHERRY-PICKING"
+                       return 0
+               ;;
+               revert[\ \      ]*)
+                       r="|REVERTING"
+                       return 0
+               ;;
+               esac
+       fi
+       return 1
 }
 
 # __git_ps1 accepts 0 or 1 arguments (i.e., format string)
@@ -355,8 +388,8 @@ __git_ps1 ()
        # incorrect.)
        #
        local ps1_expanded=yes
-       [ -z "$ZSH_VERSION" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no
-       [ -z "$BASH_VERSION" ] || shopt -q promptvars || ps1_expanded=no
+       [ -z "${ZSH_VERSION-}" ] || [[ -o PROMPT_SUBST ]] || ps1_expanded=no
+       [ -z "${BASH_VERSION-}" ] || shopt -q promptvars || ps1_expanded=no
 
        local repo_info rev_parse_exit_code
        repo_info="$(git rev-parse --git-dir --is-inside-git-dir \
@@ -368,7 +401,7 @@ __git_ps1 ()
                return $exit
        fi
 
-       local short_sha
+       local short_sha=""
        if [ "$rev_parse_exit_code" = "0" ]; then
                short_sha="${repo_info##*$'\n'}"
                repo_info="${repo_info%$'\n'*}"
@@ -415,10 +448,8 @@ __git_ps1 ()
                        fi
                elif [ -f "$g/MERGE_HEAD" ]; then
                        r="|MERGING"
-               elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
-                       r="|CHERRY-PICKING"
-               elif [ -f "$g/REVERT_HEAD" ]; then
-                       r="|REVERTING"
+               elif __git_sequencer_status; then
+                       :
                elif [ -f "$g/BISECT_LOG" ]; then
                        r="|BISECTING"
                fi
@@ -443,6 +474,8 @@ __git_ps1 ()
                                        git describe --contains HEAD ;;
                                (branch)
                                        git describe --contains --all HEAD ;;
+                               (tag)
+                                       git describe --tags HEAD ;;
                                (describe)
                                        git describe HEAD ;;
                                (* | default)