Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hasstash #46

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
26 changes: 23 additions & 3 deletions prompt.sh
Original file line number Diff line number Diff line change
@@ -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\]\$ "

Expand Down