Last active
December 24, 2015 01:19
-
-
Save jmather/6722544 to your computer and use it in GitHub Desktop.
My work in progress bash profile to make the git repos nicer to work with...
This file contains hidden or 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
if tput setaf 1 &> /dev/null; then | |
tput sgr0 | |
if [[ $(tput colors) -ge 256 ]] 2>/dev/null; then | |
MAGENTA=$(tput setaf 9) | |
ORANGE=$(tput setaf 172) | |
GREEN=$(tput setaf 190) | |
PURPLE=$(tput setaf 141) | |
WHITE=$(tput setaf 256) | |
else | |
MAGENTA=$(tput setaf 5) | |
ORANGE=$(tput setaf 4) | |
GREEN=$(tput setaf 2) | |
PURPLE=$(tput setaf 1) | |
WHITE=$(tput setaf 7) | |
fi | |
BOLD=$(tput bold) | |
RESET=$(tput sgr0) | |
else | |
MAGENTA="\033[1;31m" | |
ORANGE="\033[1;33m" | |
GREEN="\033[1;32m" | |
PURPLE="\033[1;35m" | |
WHITE="\033[1;37m" | |
BOLD="" | |
RESET="\033[m" | |
fi | |
export MAGENTA | |
export ORANGE | |
export GREEN | |
export PURPLE | |
export WHITE | |
export BOLD | |
export RESET | |
function isGitDirectory() { | |
git branch 1> /dev/null 2>&1 | |
if [ "$?" == "0" ] | |
then | |
echo "1" | |
else | |
echo "0" | |
fi | |
} | |
function isSvnDirectory() { | |
svn info 1> /dev/null 2>&1 | |
if [ "$?" == "0" ] | |
then | |
echo "1" | |
else | |
echo "0" | |
fi | |
} | |
function parse_git_branch() { | |
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/" | |
} | |
function buildPrompt() { | |
CWD=`pwd` | |
NCWD=${CWD##$HOME} | |
if [ "$CWD" != "$NCWD" ] | |
then | |
CWD="~$NCWD" | |
fi | |
OUT="$GREEN${CWD}$WHITE" | |
if [ "$(isGitDirectory)" == "1" ] | |
then | |
COMMIT_ID="$(git log -n1 --pretty=oneline --abbrev-commit --no-color | cut -d' ' -f1)" | |
COMMIT_DATE="$(git log -n 1 --date=relative | grep 'Date:' | sed -e 's/^Date: \(.*\)$/\1/')" | |
git_status="$(git status 2> /dev/null)" | |
# Set color based on clean/staged/dirty. | |
if [[ ${git_status} =~ "Changes to be committed" ]]; then | |
G_STATE="dirty" | |
G_COLOR="${PURPLE}" | |
G_CHAR="●" | |
else | |
G_STATE="clean" | |
G_COLOR="${GREEN}" | |
G_CHAR="○" | |
fi | |
if [[ ${git_status} =~ "Changes not staged for commit" ]]; then | |
S_STATE="dirty" | |
S_CHAR="●" | |
else | |
S_STATE="clean" | |
S_CHAR="○" | |
fi | |
if [[ ${git_status} =~ "Untracked files:" ]]; then | |
U_STATE="dirty" | |
U_CHAR="●" | |
else | |
U_STATE="clean" | |
U_CHAR="○" | |
fi | |
echo " " | |
echo "$RESET[$U_CHAR$S_CHAR$G_CHAR] $OUT on $G_COLOR$(parse_git_branch)$WHITE (ref: $COMMIT_ID, $COMMIT_DATE)" | |
echo "\$ " | |
elif [ "$(isSvnDirectory)" == "1" ]; then | |
COMMIT_DATE="$(svn info | grep 'Last Changed Date:' | sed -e 's/^Last Changed Date: \(.*\)$/\1/' | cut -d' ' -f1,2)" | |
COMMIT_TIMESTAMP=`date -j -f "%Y-%m-%d %H:%M:%S" "$COMMIT_DATE" +"%s"` | |
CURRENT_TIMESTAMP=`date -j +"%s"` | |
let "TIMESTAMP_DIFF = $CURRENT_TIMESTAMP - $COMMIT_TIMESTAMP" | |
DATE_DIFF_DAY=$(date -j -u -f \"%s\" \"$TIMESTAMP_DIFF\" +%-j) | |
DATE_DIFF_HOUR=$(date -j -u -f \"%s\" \"$TIMESTAMP_DIFF\" +%-H) | |
DATE_DIFF_MIN=$(date -j -u -f \"%s\" \"$TIMESTAMP_DIFF\" +%-M) | |
if [ "$DATE_DIFF_DAY" -gt "1" ]; then | |
COMMIT_DATE="$DATE_DIFF_DAY days ago" | |
elif [ $DATE_DIFF_HOUR -gt 1 ]; then | |
COMMIT_DATE="$DATE_DIFF_HOUR hours ago" | |
elif [ $DATE_DIFF_MIN -gt 1 ]; then | |
COMMIT_DATE="$DATE_DIFF_MIN minutes ago" | |
else | |
COMMIT_DATE="$TIMESTAMP_DIFF seconds ago" | |
fi | |
REPO_ROOT="$(svn info 2> /dev/null | grep "^Repository Root:" | sed -e "s/Repository Root: \(.*\)/\1/" | grep [a-z])" | |
REPO_REV="$(svn info 2> /dev/null | grep "^Revision:" | sed -e "s/Revision: \(.*\)/\1/" | grep [0-9])" | |
SVN_PATH="$(svn info 2> /dev/null | grep "^URL:" | sed -e "s/URL: \(.*\)/\1/" | grep [a-z])" | |
ROOT_PATH="$(svn info 2> /dev/null | grep "^Working Copy Root Path:" | sed -e "s/Working Copy Root Path: \(.*\)/\1/" | grep [a-z])" | |
ROOT_PATH_REL=${ROOT_PATH##$HOME} | |
if [ "$ROOT_PATH" != "" ] && [ "$ROOT_PATH" != "$(pwd)" ] && [ "$ROOT_PATH" != "$ROOT_PATH_REL" ] | |
then | |
ROOT_PATH=", symlink: ~$ROOT_PATH_REL" | |
else | |
ROOT_PATH="" | |
fi | |
SVN_REL_PATH=${SVN_PATH##$REPO_ROOT} | |
if [ "$SVN_REL_PATH" != "$REPO_ROOT" ] | |
then | |
SVN_PATH="^$SVN_REL_PATH" | |
fi | |
echo " " | |
echo "$RESET[SVN] $OUT on $GREEN$SVN_PATH$WHITE (rev $REPO_REV, $COMMIT_DATE$ROOT_PATH)" | |
echo "\$ " | |
else | |
echo " " | |
echo "$OUT" | |
echo "\$ " | |
fi | |
} | |
PS1='$(buildPrompt)' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment