Skip to content

Instantly share code, notes, and snippets.

@horx
Last active December 27, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save horx/7334816 to your computer and use it in GitHub Desktop.
Save horx/7334816 to your computer and use it in GitHub Desktop.
git-ps.sh
#!/bin/bash
#
# git-ps1 - git-augmented PS1
#
# See README for configuration options.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
#
# 为了使得git更cool,在自己的 ~/.bash_profile 里面,
# 添加 PS1="$PS1\$($( cat PATH_TO_THIS_FILE ))",
# 然后 source ~/.bashrc 或重新打开终端即可生效.
# 注意,PATH_TO_THIS_FILE 指 git-ps.sh 这个文件的路径.
#
branch=$(git symbolic-ref HEAD 2>/dev/null \
|| git rev-parse HEAD 2>/dev/null | cut -c1-10 \
)
# if no branch or hash was returned, then we're not in a repository
if [ -z "$branch" ]; then
exit
fi
# creates a color from the given color code
mkcolor()
{
echo "\[\033[00;$1m\]"
}
branch=${branch#refs/heads/}
git_status=$( git status 2>/dev/null )
# colors can be overridden via the GITPS1_COLOR_* environment variables
color_default=$( mkcolor ${GITPS1_COLOR_DEFAULT:-33} )
color_fastfwd=$( mkcolor ${GITPS1_COLOR_FASTFWD:-31} )
color_staged=$( mkcolor ${GITPS1_COLOR_STAGED:-32} )
color_untracked=$( mkcolor ${GITPS1_COLOR_UNTRACKED:-31} )
color_unstaged=$( mkcolor ${GITPS1_COLOR_UNSTAGED:-33} )
color_ahead=$( mkcolor ${GITPS1_COLOR_AHEAD:-33} )
color_state=$( mkcolor ${GITPS1_COLOR_STATE:-35} )
color_clr=$( mkcolor 0 )
# indicators may be overridden via the GITPS1_IND_* environment vars; set to
# '0' to disable
ind_staged=${GITPS1_IND_STAGED:-*}
ind_unstaged=${GITPS1_IND_UNSTAGED:-*}
ind_untracked=${GITPS1_IND_UNTRACKED:-*}
ind_ahead=${GITPS1_IND_AHEAD:-@}
ind_ahead_count=${GITPS1_IND_AHEAD_COUNT:-@}
ind_state=${GITPS1_IND_STATE:-1}
statusmsg=''
statemsg=''
color=$color_default
# uncommited files
if [ "$ind_unstaged" != '0' ]; then
git diff --no-ext-diff --quiet --exit-code 2>/dev/null || \
statusmsg="${statusmsg}${color_unstaged}${ind_unstaged}"
fi
# not on branch/behind origin
if [ "$( echo $git_status | grep 'Not currently on\|is behind' )" ]; then
color=$color_fastfwd
fi
# staged
if [ "$ind_staged" != '0' ]; then
if [ "$( echo $git_status | grep 'to be committed' )" ]; then
statusmsg="${statusmsg}${color_staged}${ind_staged}"
fi
fi
# untracked
if [ "$ind_untracked" != '0' ]; then
if [ -n "$( git ls-files --others --exclude-standard 2>/dev/null )" ]; then
statusmsg="${statusmsg}${color_untracked}${ind_untracked}"
fi
fi
# ahead of tracking
if [ "$ind_ahead" != '0' ]; then
grep -q 'is ahead' <<< "$git_status" && {
statusmsg="${statusmsg}${color_ahead}${ind_ahead}"
# append count?
if [ "$ind_ahead_count" != '0' ]; then
ahead_count=$( echo $git_status \
| grep -o 'by [0-9]\+ commits\?' \
| cut -d' ' -f2 \
)
statusmsg="${statusmsg}${ahead_count}"
fi
}
fi
# state message
if [ "$ind_state" != '0' ]; then
statemsg=$( git state 2>/dev/null ) && state=" $color_state($statemsg)"
fi
# output the status string
echo "$color[${branch}${statusmsg}${color}]$state$color_clr "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment