Skip to content

Instantly share code, notes, and snippets.

@clarkema
Created January 12, 2015 19:18
Show Gist options
  • Select an option

  • Save clarkema/9c43b8862ffe71e1d4f4 to your computer and use it in GitHub Desktop.

Select an option

Save clarkema/9c43b8862ffe71e1d4f4 to your computer and use it in GitHub Desktop.
# resolve_path from here https://github.com/keen99/shell-functions/tree/master/resolve_path
resolve_path() {
#I'm bash only, please!
# usage: resolve_path <a file or directory>
# follows symlinks and relative paths, returns a full real path
#
local owd="$PWD"
#echo "$FUNCNAME for $1" >&2
local opath="$1"
local npath=""
local obase=$(basename "$opath")
local odir=$(dirname "$opath")
if [[ -L "$opath" ]]
then
#it's a link.
#file or directory, we want to cd into it's dir
cd $odir
#then extract where the link points.
npath=$(readlink "$obase")
#have to -L BEFORE we -f, because -f includes -L :(
if [[ -L $npath ]]
then
#the link points to another symlink, so go follow that.
resolve_path "$npath"
#and finish out early, we're done.
return $?
#done
elif [[ -f $npath ]]
#the link points to a file.
then
#get the dir for the new file
nbase=$(basename $npath)
npath=$(dirname $npath)
cd "$npath"
ndir=$(pwd -P)
retval=0
#done
elif [[ -d $npath ]]
then
#the link points to a directory.
cd "$npath"
ndir=$(pwd -P)
retval=0
#done
else
echo "$FUNCNAME: ERROR: unknown condition inside link!!" >&2
echo "opath [[ $opath ]]" >&2
echo "npath [[ $npath ]]" >&2
return 1
fi
else
if ! [[ -e "$opath" ]]
then
echo "$FUNCNAME: $opath: No such file or directory" >&2
return 1
#and break early
elif [[ -d "$opath" ]]
then
cd "$opath"
ndir=$(pwd -P)
retval=0
#done
elif [[ -f "$opath" ]]
then
cd $odir
ndir=$(pwd -P)
nbase=$(basename "$opath")
retval=0
#done
else
echo "$FUNCNAME: ERROR: unknown condition outside link!!" >&2
echo "opath [[ $opath ]]" >&2
return 1
fi
fi
#now assemble our output
echo -n "$ndir"
if [[ "x${nbase:=}" != "x" ]]
then
echo "/$nbase"
else
echo
fi
#now return to where we were
cd "$owd"
return $retval
}
#=======================================================================
# Git-ified prompt {{{
#=======================================================================
default_prompt="\u@\h:\w ${vcs_info_msg_0_}> "
function prompt_setup() {
local git_branch=$(parse_git_branch)
local gitcolor gitcolor_new gitcolor_old reset
reset='\e[0m'
if [[ -n "$git_branch" ]]; then
git_clean_p
if [[ $? == 0 ]]; then
if [[ "$git_branch" == "master" ]]; then
gitcolor='\e[1;33m' # Yellow
else
gitcolor='\e[1;32m' # Green
fi
else
gitcolor='\e[1;31m' # Red
fi
PS1="$(path_within_git_repo):$gitcolor$git_branch$reset> "
else
PS1=$default_prompt
fi
}
function parse_git_branch ()
{
local ref=$(git symbolic-ref HEAD 2> /dev/null) || return 1
echo ${ref#refs/heads/}
}
function path_within_git_repo ()
{
local repo_base=$(resolve_path $( git rev-parse --git-dir ))
local repo_parent=$(dirname $(dirname ${repo_base}))
echo ${PWD##$repo_parent/}
}
# Exit value of 0 if the repo is clean, 1 otherwise
function git_clean_p ()
{
git status 2> /dev/null | fgrep 'working directory clean' > /dev/null
}
PROMPT_COMMAND=prompt_setup
# }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment