Created
March 24, 2011 23:21
-
-
Save Igneous/886091 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Author: John Wolfe <bjw@onwav.com> | |
# Last Modified: Mar 24, 2011. 18:20 CDT | |
# License: zlib/libpng | |
# Declare git_ps1 and normal_ps1 in your bashrc or this file, set PROMPT_COMMAND='dotgitinvestigate >/dev/null 2>&1' in your bashrc. | |
# Be sure to use single quotes in the declaration of your ps1s, so the variables get evaluated in this file rather than at the time of declaration. | |
# Example git_ps1='[\[${bldblu}\]\u\[${txtwht}\]@\[${bldwht}\]\h\[${txtrst}\]:\[${txtpur}\]\W\[${txtrst}\]\[${txtgrn}\]($branch:${treeish:0:5})\[${txtrst}\]]$ ' | |
# Example normal_ps1='[\[${bldblu}\]\u\[${txtwht}\]@\[${bldwht}\]\h\[${txtrst}\]:\[${txtpur}\]\W\[${txtrst}\]]$ ' | |
# For the shell color variables in previous examples, include: https://gist.github.com/886087 in this file (or your bashrc) with ". /path/to/bashrc-colors.sh" | |
pwdln() { | |
# Doing PE from the beginning of the string is needed | |
# so we get a string of 0 len to break the until loop. | |
pwdmod="${PWD}/" | |
itr=0 | |
until [[ -z "$pwdmod" ]];do | |
itr=$(($itr+1)) | |
pwdmod="${pwdmod#*/}" | |
done | |
echo -n $(($itr-1)) | |
} | |
dotgitinvestigate() { | |
# Ghetto var scoping, we don't want to keep git_dir if it didn't get declared this run | |
unset -v git_dir | |
# If there is a git dir in the cwd, great, we're done. | |
if [[ -d "${PWD}/.git" ]];then | |
git_dir="${PWD}/.git" | |
else | |
pwdmod2="${PWD}" # Since we didn't find a $PWD/.git, we're going to pop | |
for (( i=2; i<=$(pwdln); i++ ));do # a directory off the end of the $pwdmod2 stack until we | |
pwdmod2="${pwdmod2%/*}" # come across a ./.git. /home/igneous/proj/1 will start at | |
if [[ -d "${pwdmod2}/.git" ]];then # /home/igneous/proj because of our loop starting at 2. | |
git_dir="${pwdmod2}/.git" | |
fi | |
done | |
fi | |
chkgitbranch | |
} | |
chkgitbranch() { | |
# More ghetto scoping. | |
unset -v branch ref treeish | |
if [[ -f "${git_dir}/rebase-apply/head-name" ]];then | |
read branch < "${git_dir}/rebase-apply/head-name" | |
read treeish < "${git_dir}/rebase-apply/original-commit" | |
branch="${branch##*/}-REBASE" | |
eval "PS1=\"$git_ps1\"" | |
elif [[ -f "${git_dir}/rebase-merge/head-name" ]];then | |
read branch < "${git_dir}/rebase-merge/head-name" | |
read treeish < "${git_dir}/rebase-merge/stopped-sha" | |
branch="${branch##*/}-REBASE" | |
eval "PS1=\"$git_ps1\"" | |
elif [[ "$git_dir" ]];then | |
read branch < "${git_dir}/HEAD" # Read the file | |
ref="${branch#ref: *}" # Let's declare $ref before we nuke $branch | |
branch="${branch##*/}" # We only care about the end of the file path here "refs/heads/master" -> "master". | |
read treeish < "${git_dir}/$ref" || treeish="INIT?" # git_dir ( ~/project ) + ref ( refs/heads/master ) == ~/project/refs/heads/master | |
eval "PS1=\"$git_ps1\"" # And if the git ref directory doesn't exist, we assume it's a new repo. | |
else | |
eval "PS1=\"$normal_ps1\"" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment