Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Version control prompt
#!/bin/bash
# Copyright © 2012 Martin Ueding <dev@martin-ueding.de>
# This script has been placed in the public domain via the CC0 license:
# https://creativecommons.org/publicdomain/zero/1.0/
# Checks the current working directory for a git, bzr (or svn or hg, …) tree
# and displays the apropriate name. If the status is supposed to be displayed,
# a ``bzr status`` is run. Git features the ``__git_ps1``, which is used instead in
# case a git repo is found.
scmprompt() {
local showstatus=false
# enable color
#shopt -s xpg_echo
local cdir=$(pwd)
while [[ "$cdir" != "/" ]]
do
if [[ "$cdir" = "/home/mu" ]]
then
exit 0
fi
# git
if [[ "$(basename "$cdir")" = ".git" ]]
then
exit 0
fi
if [[ -d "$cdir/.git" ]]
then
if type -t __git_ps1 > /dev/null
then
echo "$(__git_ps1)"
else
echo " git"
fi
exit 0
fi
# bzr
if [[ -d "$cdir/.bzr" ]]
then
if [[ -d "$cdir/.bzr/branch" ]]
then
if [[ $showstatus = 'false' ]]
then
echo " bzr"
else
if [[ "$(bzr status 2>&1 | wc -l)" -le 1 ]]
then
echo " bzr✓"
else
echo " bzr"
fi
fi
else
echo " bzr repo"
fi
exit 0
fi
# svn
if [[ -d "$cdir/.svn" ]]
then
echo " svn"
exit 0
fi
# mercurial
if [[ -d "$cdir/.hg" ]]
then
echo " hg"
exit 0
fi
# Go up one dir in the directory tree.
cdir=${cdir%/*}
if [[ -z "$cdir" ]]
then
cdir="/"
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment