diff --git a/README.md b/README.md index 936ca9d..275acad 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ Edit your `~/.profile` or `~/.bash_profile` and add the following to the top: ```bash export GITAWAREPROMPT=~/.bash/git-aware-prompt source $GITAWAREPROMPT/main.sh -export PS1="\u@\h \w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ " +export PS1="\u@\h \w \[$txtcyn\]\$git_branch\[$txtred\]\$git_stash$git_dirty\[$txtrst\]\$ " ``` Optionally, if you want a nice pretty prompt when using `sudo -s`, also add @@ -69,6 +69,8 @@ your `PS1` variable, and make sure the variable value is defined with double quotes. A set of color variables have also been set for you to use. For a list of available colors check `colors.sh`. +To show if the current branch as stashes associated with it add `$git_stash` to +your `PS1` variable. ## Updating diff --git a/prompt.sh b/prompt.sh index 6a737b8..18c5fc9 100644 --- a/prompt.sh +++ b/prompt.sh @@ -1,30 +1,50 @@ +# Users can set the dirtysymb env var in their .bash_profile, etc, +# or they can leave it unset and it will default to the '*'. +export dirtysymb=${dirtysymb:="*"}; + +# Show that there is a stash for the current branch +export hasstash=${hasstash:="S"}; + find_git_branch() { # Based on: http://stackoverflow.com/a/13003854/170413 - local branch if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then if [[ "$branch" == "HEAD" ]]; then branch='detached*' fi git_branch="($branch)" + else git_branch="" fi + +} + +find_git_stash() { + local stash + if [[ -n $(git stash list --grep-reflog="${branch}:" 2> /dev/null ) ]]; then + git_stash="[$hasstash]" + else + git_stash="" + fi } find_git_dirty() { local status=$(git status --porcelain 2> /dev/null) if [[ "$status" != "" ]]; then - git_dirty='*' + git_dirty=$dirtysymb else git_dirty='' fi } -PROMPT_COMMAND="find_git_branch; find_git_dirty; $PROMPT_COMMAND" +PROMPT_COMMAND="find_git_branch; find_git_dirty; find_git_stash; $PROMPT_COMMAND" # Default Git enabled prompt with dirty state # export PS1="\u@\h \w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ " +# Default Git enabled prompt with dirty state & showing stashes that for the current branch +# export PS1="\u@\h \w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty$git_stash\[$txtrst\]\$ " + # Another variant: # export PS1="\[$bldgrn\]\u@\h\[$txtrst\] \w \[$bldylw\]\$git_branch\[$txtcyn\]\$git_dirty\[$txtrst\]\$ "