From 6b00d43d7413309b8b5e8c5bd3dffe5fed694a8b Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Sun, 14 Aug 2016 00:16:29 +1200 Subject: Port Git prompt function from Bash to pdksh --- pdksh/pdkshrc.d/prompt.pdksh | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/pdksh/pdkshrc.d/prompt.pdksh b/pdksh/pdkshrc.d/prompt.pdksh index d261b90d..c3db782b 100644 --- a/pdksh/pdkshrc.d/prompt.pdksh +++ b/pdksh/pdkshrc.d/prompt.pdksh @@ -18,6 +18,7 @@ prompt() { # Set up prompt, including optional PROMPT_PREFIX and PROMPT_SUFFIX # variables PS1='[\u@\h:\w]' + PS1=$PS1'$(prompt vcs)' PS1=$PS1'$(prompt job)' PS1='${PROMPT_PREFIX}'$PS1 PS1=$PS1'${PROMPT_SUFFIX}' @@ -76,6 +77,44 @@ prompt() { PS4='+<$?> $LINENO:' ;; + # Git prompt function + git) + # Bail if we have no git(1) + if ! hash git 2>/dev/null ; then + return 1 + fi + + # Attempt to determine git branch, bail if we can't + typeset branch + branch=$( { + git symbolic-ref --quiet HEAD || + git rev-parse --short HEAD + } 2>/dev/null ) + if [[ ! -n $branch ]] ; then + return 1 + fi + branch=${branch##*/} + + typeset state + if ! git diff-files --quiet ; then + # Two exclamation marks, as that's how you get a literal "!" in + # a pdksh PS1 + state=${state}!! + fi + if ! git diff-index --cached --quiet HEAD ; then + state=${state}+ + fi + if [[ -n $(git ls-files --others --exclude-standard) ]] ; then + state=${state}? + fi + if git rev-parse --verify refs/stash >/dev/null 2>&1 ; then + state=${state}^ + fi + + # Print the status in brackets with a git: prefix + printf '(git:%s%s)' "${branch:-unknown}" "$state" + ;; + # Revert to simple inexpensive prompts off) PS1='\$ ' @@ -84,6 +123,16 @@ prompt() { PS4='+ ' ;; + # VCS wrapper prompt function; print the first relevant prompt, if any + vcs) + typeset vcs + for vcs in "${PROMPT_VCS[@]:-git}" ; do + if prompt "$vcs" ; then + return + fi + done + ;; + # Show the count of background jobs in curly brackets, if not zero job) typeset -i jobc -- cgit v1.2.3