Skip to content

Instantly share code, notes, and snippets.

@dtjm
Created August 19, 2010 18:13
Show Gist options
  • Save dtjm/538522 to your computer and use it in GitHub Desktop.
Save dtjm/538522 to your computer and use it in GitHub Desktop.
Subversion PS1 prompt functions for bash
#
# If you want to see svn modifications:
# export SVN_SHOWDIRTYSTATE=1
#
# Put this in your PS1 like this:
# PS1='\u@\h:\W\[\033[01;33m\]$(__git_svn_ps1)\[\033[00m\]$ '
# Git/Subversion prompt function
__git_svn_ps1() {
local s=
if [[ -d ".svn" ]] ; then
local r=`__svn_rev`
local b=`__svn_branch`
s=" [$b:$r]"
elif [[ -d .git ]] ; then
s=`__git_ps1`
fi
echo -n "$s"
}
# Outputs the current trunk, branch, or tag
__svn_branch() {
local url=
if [[ -d .svn ]]; then
url=`svn info | awk '/URL:/ {print $2}'`
if [[ $url =~ trunk ]]; then
echo trunk
elif [[ $url =~ /branches/ ]]; then
echo $url | sed -e 's#^.*/\(branches/.*\)/.*$#\1#'
elif [[ $url =~ /tags/ ]]; then
echo $url | sed -e 's#^.*/\(tags/.*\)/.*$#\1#'
fi
fi
}
# Outputs the current revision
__svn_rev() {
local r=$(svn info | awk '/Revision:/ {print $2}')
if [ ! -z $SVN_SHOWDIRTYSTATE ]; then
local svnst flag
svnst=$(svn status | grep '^\s*[?ACDMR?!]')
[ -z "$svnst" ] && flag=*
r=$r$flag
fi
echo $r
}
@kenbellows
Copy link

@pestophagous Though otoh you would be right if this was for git. There's only a single .git directory at the root of the repo, so line 16 might need to be amended

@adamjstewart
Copy link

adamjstewart commented Jan 31, 2018

I definitely have less SVN experience than you, but I currently have an SVN repo where the root has a .svn directory but none of the subdirectories do. I did an initial clone of an existing repo, then did an svn cp of another repo, and now the subdirectories have no .svn directories even though I committed them and they are under version control. I think a more robust way of checking if you're in an svn repo is to run svn info (slow) or check all parent directories for a .svn directory (fast, but symlinks...).

EDIT: I implemented the latter solution in my svn prompt if anyone wants to steal it. For a comparison of speeds:

# Only check the current directory
$ time test -d .svn

real	0m0.000s
user	0m0.000s
sys	0m0.000s

# Check all parent directories
$ time __in_svn_repo

real	0m0.009s
user	0m0.003s
sys	0m0.005s

# Use svn to check
$ time svn info > /dev/null

real	0m0.037s
user	0m0.011s
sys	0m0.012s

@dagoss
Copy link

dagoss commented Feb 21, 2018

@adamjstewart I tried your method, but your __svn_ps1() hangs for me (on SunOS 5.10 with SVN 1.8.9.... given my environment, don't take my report seriously)

@dagoss
Copy link

dagoss commented Feb 21, 2018

I did the regex on L30 a little different; sed apparently can't do non-greedy regexes so I used perl instead:

elif [[ $url =~ /branches/ ]]; then
    echo $url | perl -pe 's|^.*/branches/(.*?)/.*$|\1|'

This made it so only the branch name was displayed, not the branch and remaining path to CWD.

I also added @pestophagous __svn_info_str method. I'm on a pretty old version of SVN though (1.8.9)

@dbalakirev
Copy link

A key feature of the changes introduced in Subversion 1.7 is the centralization of working copy metadata storage into a single location. Instead of a .svn directory in every directory in the working copy, Subversion 1.7 working copies have just one .svn directory—in the root of the working copy.
From: https://subversion.apache.org/docs/release-notes/1.7.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment