aks: added gitstatus.sh, and configured it in gitprompt.sh

master
Alan Stebbens 12 years ago
parent 42e4bccf7d
commit 0704670f8e
  1. 2
      README.md
  2. 12
      gitprompt.sh
  3. 86
      gitstatus.sh

@ -56,6 +56,8 @@ The symbols are as follows:
2. If you want to tweak the colors, 2. If you want to tweak the colors,
currently you have to tweak it in the ``gitprompt.sh`` currently you have to tweak it in the ``gitprompt.sh``
3. You can define ``prompt_callback`` function to tweak your prompt dynamicly 3. You can define ``prompt_callback`` function to tweak your prompt dynamicly
4. The current git repo information is obtained by the script `gitstatus.sh` or
`gitstatus.py`. Both scripts do the same thing.
```sh ```sh
function prompt_callback { function prompt_callback {

@ -81,7 +81,17 @@ function git_prompt_config()
if [ "x$__GIT_STATUS_CMD" == "x" ] if [ "x$__GIT_STATUS_CMD" == "x" ]
then then
git_prompt_dir git_prompt_dir
__GIT_STATUS_CMD="${__GIT_PROMPT_DIR}/gitstatus.py" local sfx file
# look first for a '.sh' version, then use the python version
for sfx in sh py ; do
file="${__GIT_PROMPT_DIR}/gitstatus.$sfx"
if [[ -x "$file" ]]; then
__GIT_STATUS_CMD="$file"
break
fi
done
# The old way
#__GIT_STATUS_CMD="${__GIT_PROMPT_DIR}/gitstatus.py"
fi fi
} }

@ -0,0 +1,86 @@
#/bin/bash
# -*- coding: UTF-8 -*-
# change those symbols to whatever you prefer
declare -a symbols
symbols['ahead']='↑·'
symbols['behind']='↓·'
symbols['prehash']=':'
gitsym=`git symbolic-ref HEAD`
# if "fatal: Not a git repo .., then exit
case "$gitsym" in fatal*) exit 0 ;; esac
# the current branch is the tail end of the symbolic reference
branch="${gitsym##*/}" # get the basename after "refs/head/"
tmp="/tmp/$$-gitstatus.out"
trap "rm -f \"$tmp\"" EXIT
status=`git diff --name-status >$tmp`
# if the diff is fatal, exit now
if grep -s "^fatal:" $tmp 2>/dev/null ; then
exit
fi
# count_lines U
count_lines() { egrep -c "^$1" <$tmp ; }
num_changed=$(( `wc -l <$tmp` - `count_lines U` ))
staged_files=`git diff --staged --name-status >$tmp`
num_conflicts=`count_lines U`
num_staged=$(( `wc -l <$tmp` - num_conflicts ))
num_untracked=`git status -s -uall | grep -c "^??"`
num_stashed=`git stash list | wc -l`
clean=0
if (( num_changed == 0 && num_staged == 0 && num_U == 0 && num_untracked == 0 && num_stashed == 0 )) ; then
clean=1
fi
remote=
if [[ -z "$branch" ]]; then
tag=`git describe --exact-match`
if [[ -n "$tag" ]]; then
branch="$tag"
else
branch="${symbols['prehash']}`git rev-parse --short HEAD`"
fi
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##*/}"
fi
# get the revision list, and count the leading "<" and ">"
revgit=`git rev-list --left-right ${remote_ref}...HEAD >$tmp`
num_revs=`wc -l <$tmp`
num_ahead=`count_lines "^>"`
num_behind=$(( num_revs - num_ahead ))
if (( num_behind > 0 )) ; then
remote="${remote}${symbols['behind']}${num_behind}"
fi
if (( num_ahead > 0 )) ; then
remote="${remote}${symbols['ahead']}${num_ahead}"
fi
fi
if [[ -z "$remote" ]] ; then
remote='.'
fi
for w in "$branch" "$remote" $num_staged $num_conflicts $num_changed $num_untracked $num_stashed $clean ; do
echo "$w"
done
exit
Loading…
Cancel
Save