Skip to content

Instantly share code, notes, and snippets.

@brettstimmerman
Created April 28, 2010 18:33
Show Gist options
  • Save brettstimmerman/382508 to your computer and use it in GitHub Desktop.
Save brettstimmerman/382508 to your computer and use it in GitHub Desktop.
Show svn info in your bash prompt
# Returns (svn:<revision>:<branch|tag>[*]) if applicable
svn_prompt() {
if [ -d ".svn" ]; then
local branch dirty rev info=$(svn info 2>/dev/null)
branch=$(svn_parse_branch "$info")
# Uncomment if you want to display the current revision.
#rev=$(echo "$info" | awk '/^Revision: [0-9]+/{print $2}')
# Uncomment if you want to display whether the repo is 'dirty.' In some
# cases (on large repos) this may take a few seconds, which can
# noticeably delay your prompt after a command executes.
#[ "$(svn status)" ] && dirty='*'
if [ "$branch" != "" ] ; then
echo "(svn:$rev:$branch$dirty)"
fi
fi
}
# Returns the current branch or tag name from the given `svn info` output
svn_parse_branch() {
local chunk url=$(echo "$1" | awk '/^URL: .*/{print $2}')
echo $url | grep -q "/trunk\b"
if [ $? -eq 0 ] ; then
echo trunk
return
else
chunk=$(echo $url | grep -o "/releases.*")
if [ "$chunk" == "" ] ; then
chunk=$(echo $url | grep -o "/branches.*")
if [ "$chunk" == "" ] ; then
chunk=$(echo $url | grep -o "/tags.*")
fi
fi
fi
echo $chunk | awk -F/ '{print $3}'
}
export PS1="\w\$(svn_prompt)>"
@ardnew
Copy link

ardnew commented May 21, 2024

The following patch enables this to work in subdirectories of your working copy (not just the root). It also fixes the stray colon : between ${rev}:${branch} when you have ${rev} commented out (per OP's default):

@@ -1,25 +1,34 @@
 # Returns (svn:<revision>:<branch|tag>[*]) if applicable
 svn_prompt() {
-    if [[ -d ".svn" ]] ; then
-        local branch dirty rev info=$(svn info 2>/dev/null)
+    if wc=$( svn_metadata_path ); then
+        local branch dirty rev info=$(svn info "${wc}" 2>/dev/null)

         branch=$(svn_parse_branch "${info}")

         # Uncomment if you want to display the current revision.
-        #rev=$(echo "${info}" | awk '/^Revision: [0-9]+/{print $2}')
+        #rev=$(echo "${info}" | awk '/^Revision: [0-9]+/{print $2":"}')

         # Uncomment if you want to display whether the repo is 'dirty.' In some
         # cases (on large repos) this may take a few seconds, which can
         # noticeably delay your prompt after a command executes.
         #[ "$(svn status)" ] && dirty='*'

         if [ "$branch" != "" ] ; then
-            echo "(svn:${rev}:${branch}${dirty})"
+            echo "(svn:${rev}${branch}${dirty})"
         fi
     fi
 }

+svn_metadata_path() {
+    local curr='.'
+    while [ ! -d "${curr}/.svn" ]; do
+        [ "${curr}" != "/" ] || return 1
+        curr=$( realpath "${curr}/.." )
+    done
+    echo "${curr}"
+}
+
 # Returns the current branch or tag name from the given `svn info` output
 svn_parse_branch() {
     local chunk url=$(echo "$1" | awk '/^URL: .*/{print $2}')

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