Skip to content

Instantly share code, notes, and snippets.

@martin-ueding
Created May 6, 2020 12:55
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 martin-ueding/9a826ebb0c77e4a659d4cc8548076a1e to your computer and use it in GitHub Desktop.
Save martin-ueding/9a826ebb0c77e4a659d4cc8548076a1e to your computer and use it in GitHub Desktop.
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