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

faster git prompt #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
57 changes: 9 additions & 48 deletions .bash_prompt
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,15 @@ elif infocmp xterm-256color >/dev/null 2>&1; then
fi;

prompt_git() {
local s='';
local branchName='';

# Check if the current directory is in a Git repository.
if [ "$(git rev-parse --is-inside-work-tree &>/dev/null; echo "${?}")" == '0' ]; then

# check if the current directory is in .git before running git checks
if [ "$(git rev-parse --is-inside-git-dir 2> /dev/null)" == 'false' ]; then

if [[ -O "$(git rev-parse --show-toplevel)/.git/index" ]]; then
git update-index --really-refresh -q &> /dev/null;
fi;

# Check for uncommitted changes in the index.
if ! git diff --quiet --ignore-submodules --cached; then
s+='+';
fi;

# Check for unstaged changes.
if ! git diff-files --quiet --ignore-submodules --; then
s+='!';
fi;

# Check for untracked files.
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
s+='?';
fi;

# Check for stashed files.
if git rev-parse --verify refs/stash &>/dev/null; then
s+='$';
fi;

fi;

# Get the short symbolic ref.
# If HEAD isn’t a symbolic ref, get the short SHA for the latest commit
# Otherwise, just give up.
branchName="$(git symbolic-ref --quiet --short HEAD 2> /dev/null || \
git rev-parse --short HEAD 2> /dev/null || \
echo '(unknown)')";

[ -n "${s}" ] && s=" [${s}]";

echo -e "${1}${branchName}${blue}${s}";
else
return;
fi;
local gs='' s='' branch='';
gs=$(git status --show-stash 2>&1) || return;
[[ "$gs" =~ On\ branch\ ([[:print:]]*) ]] && branch=${BASH_REMATCH[1]} # credit @rasa. see https://github.com/jessfraz/dotfiles/pull/38#discussion_r382945541
[[ "$gs" =~ "Changes to be committed:" ]] && s+="+";
[[ "$gs" =~ "Changes not staged for commit:" ]] && s+="!";
[[ "$gs" =~ "Untracked files" ]] && s+="?";
[[ "$gs" =~ "Your stash currently has" ]] && s+="$";
[ -n "${s}" ] && s=" [${s}]";
echo -e "${1}${branch}${blue}${s}";
}

cloud=""
Expand Down