Skip to content

Instantly share code, notes, and snippets.

@marcpalmer
Last active May 30, 2018 11:05
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 marcpalmer/8e61abe8c24584e655cb59e004e2beab to your computer and use it in GitHub Desktop.
Save marcpalmer/8e61abe8c24584e655cb59e004e2beab to your computer and use it in GitHub Desktop.
Change your macOS bash prompt to include Xcode version and git branch + status
#
# Author: Marc Palmer
# Twitter: https://twitter.com/marcpalmerdev
# Blog: https://marcpalmer.net
#
# This is a .bash_profile file that you can write to your home directory to give
# you a custom shell prompt that will include the git branch and status of your
# current working dir as well as the current Xcode version.
#
# This is really handy when you're running multiple Xcode versions on your machine and have to run
# command line builds such as carthage, fastlane or cocoapods which can fail or do the
# wrong thing and waste a lot of time if xcode-select is not using the version of Xcode tools you expect.
#
# These git parts were verbatim copied from http://ezprompt.net
#
# get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo "[${BRANCH}${STAT}]"
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}
# Parse out version from xcodebuild's plist based on output of xcode-select
# Thanks to @danielpunkass for the performance improvement that avoids executing "xcodebuild -version"
function parse_xcode_version {
echo `plutil -p \`xcode-select -p\`/../Info.plist | grep -e CFBundleShortVersionString | sed 's/[^0-9\.]*//g'`
}
# Set the prompt to include colored Xcode version and git status.
# We use a light blue bg and the hammer emoji to signal that the version information is about Xcode
# This sets the prompt to expand to "<host>:<working-dir> 🔨<xcode-version> [<git-info>]$ "
export PS1="\h:\W \[\033[46m\] 🔨\[\033[30m\]\`parse_xcode_version\` \[\033[00m\] \`parse_git_branch\`$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment