forked from magicmonty/bash-git-prompt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use git status --porcelain to improve performance
Based off of changes: olivierverdier/zsh-git-prompt#65
- Loading branch information
1 parent
b46a233
commit 3b876d3
Showing
2 changed files
with
126 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,7 @@ | |
# Alan K. Stebbens <[email protected]> [http://github.com/aks] | ||
|
||
# helper functions | ||
count_lines() { echo "$1" | egrep -c "^$2" ; } | ||
all_lines() { echo "$1" | grep -v "^$" | wc -l ; } | ||
count_lines() { echo "$1" | egrep -c $3 "^$2" ; } | ||
|
||
if [ -z "${__GIT_PROMPT_DIR}" ]; then | ||
SOURCE="${BASH_SOURCE[0]}" | ||
|
@@ -19,92 +18,65 @@ if [ -z "${__GIT_PROMPT_DIR}" ]; then | |
__GIT_PROMPT_DIR="$( cd -P "$( dirname "${SOURCE}" )" && pwd )" | ||
fi | ||
|
||
gitsym=`git symbolic-ref HEAD` | ||
gitstatus=`git status --porcelain --branch` | ||
|
||
# if "fatal: Not a git repo .., then exit | ||
case "$gitsym" in fatal*) exit 0 ;; esac | ||
# if the status is fatal, exit now | ||
[[ "$?" -ne 0 ]] && exit 0 | ||
|
||
# the current branch is the tail end of the symbolic reference | ||
branch="${gitsym##refs/heads/}" # get the basename after "refs/heads/" | ||
num_staged=`count_lines "$gitstatus" "(\?\?|##| )" "-v"` | ||
num_changed=`count_lines "$gitstatus" ".M"` | ||
num_conflicts=`count_lines "$gitstatus" "U"` | ||
num_untracked=`count_lines "$gitstatus" "\?\?"` | ||
|
||
gitstatus=`git diff --name-status 2>&1` | ||
|
||
# if the diff is fatal, exit now | ||
case "$gitstatus" in fatal*) exit 0 ;; esac | ||
|
||
|
||
staged_files=`git diff --staged --name-status` | ||
|
||
num_changed=$(( `all_lines "$gitstatus"` - `count_lines "$gitstatus" U` )) | ||
num_conflicts=`count_lines "$staged_files" U` | ||
num_staged=$(( `all_lines "$staged_files"` - num_conflicts )) | ||
num_untracked=`git ls-files --others --exclude-standard $(git rev-parse --show-cdup) | wc -l` | ||
if [[ "$__GIT_PROMPT_IGNORE_STASH" = "1" ]]; then | ||
num_stashed=0 | ||
else | ||
num_stashed=`git stash list | wc -l` | ||
else | ||
stash_file="`git rev-parse --git-dir`/logs/refs/stash" | ||
if [[ -e "${stash_file}" ]]; then | ||
num_stashed=`wc -l "${stash_file}" | cut -d' ' -f 1` | ||
else | ||
num_stashed=0 | ||
fi | ||
fi | ||
|
||
clean=0 | ||
if (( num_changed == 0 && num_staged == 0 && num_U == 0 && num_untracked == 0 && num_stashed == 0 )) ; then | ||
if (( num_changed == 0 && num_staged == 0 && num_untracked == 0 && num_stashed == 0 )) ; then | ||
clean=1 | ||
fi | ||
|
||
remote= | ||
|
||
branch_line=`echo $gitstatus | grep "^##"` | ||
IFS="."; read -ra line <<< "${branch_line/\#\# }" | ||
branch="${line[0]}" | ||
|
||
if [[ -z "$branch" ]]; then | ||
tag=`git describe --exact-match` | ||
if [[ -n "$tag" ]]; then | ||
branch="$tag" | ||
else | ||
branch="_PREHASH_`git rev-parse --short HEAD`" | ||
fi | ||
elif [[ "$branch" == *"Initial commit on"* ]]; then | ||
IFS=" "; read -ra branch_line <<< "$branch" | ||
branch=${branch_line[-1]} | ||
elif [[ "$branch" == *"no branch"* ]]; then | ||
branch="_PREHASH_`git rev-parse --short HEAD`" | ||
else | ||
remote_name=`git config branch.${branch}.remote` | ||
|
||
if [[ -n "$remote_name" ]]; then | ||
merge_name=`git config branch.${branch}.merge` | ||
else | ||
remote_name='origin' | ||
merge_name="refs/heads/${branch}" | ||
fi | ||
|
||
if [[ "$remote_name" == '.' ]]; then | ||
remote_ref="$merge_name" | ||
else | ||
remote_ref="refs/remotes/$remote_name/${merge_name##refs/heads/}" | ||
fi | ||
|
||
# detect if the local branch have a remote tracking branch | ||
cmd_output=$(git rev-parse --abbrev-ref ${branch}@{upstream} 2>&1 >/dev/null) | ||
|
||
if [ `count_lines "$cmd_output" "fatal: no upstream"` == 1 ] ; then | ||
has_remote_tracking=0 | ||
else | ||
has_remote_tracking=1 | ||
fi | ||
|
||
# get the revision list, and count the leading "<" and ">" | ||
revgit=`git rev-list --left-right ${remote_ref}...HEAD` | ||
num_revs=`all_lines "$revgit"` | ||
num_ahead=`count_lines "$revgit" "^>"` | ||
num_behind=$(( num_revs - num_ahead )) | ||
if (( num_behind > 0 )) ; then | ||
remote="${remote}_BEHIND_${num_behind}" | ||
fi | ||
if (( num_ahead > 0 )) ; then | ||
IFS="[]"; read -ra remote_line <<< "${line[3]}" | ||
if [[ "${remote_line[1]}" == *ahead* ]]; then | ||
num_ahead=`echo "${remote_line[1]}" | cut -c 7-` | ||
remote="${remote}_AHEAD_${num_ahead}" | ||
elif [[ "${remote_line[1]}" == *behind* ]]; then | ||
num_behind=`echo "${remote_line[1]}" | cut -c 9-` | ||
remote="${remote}_BEHIND_${num_behind}" | ||
fi | ||
fi | ||
|
||
if [[ -z "$remote" ]] ; then | ||
remote='.' | ||
fi | ||
|
||
if [[ "$has_remote_tracking" == "0" ]] ; then | ||
remote='_NO_REMOTE_TRACKING_' | ||
fi | ||
|
||
for w in "$branch" "$remote" $num_staged $num_conflicts $num_changed $num_untracked $num_stashed $clean ; do | ||
echo "$w" | ||
done | ||
|