Archives

Tags

Showing articles with tag svn. Show all articles

An improved shell script for Svn resetting

A few weeks ago I posted a simple script for resetting an Svn repository to an earlier revision (and recording it as a new revision, so the reset is resettable!).

I decided it was totally shit, and so, I have simplified it a little bit:

  • You no longer have to pass in the URL for the central repository. The script will figure it out for you.
  • The working copy (second) argument is optional. It defaults to the current directory.

Anyway, here it is:

if [ $# = 0 ]
then
  echo "Resets an Svn working copy to an earlier revision."
  echo "svnreset <revision> [PATH...]"
else
  dir=$2
  : ${dir:="."}

  # Find repository URL using AWK.
  repo_url=`svn info $dir | awk '/^URL: (.+)$/ { print $2 }'`

  # Update working copy, merge into older revision, commit with message.
  svn update $dir
  svn merge -rHEAD:$1 $repo_url
  svn commit -m "Reverted back to r$1"
fi

exit 0

Call it like this (with an appropriate revision number): $ svnreset 1242

Resetting your SVN repository to a previous revision

NOTE: See the improved version of this shell script here: http://andrewbuntine.com/articles/2008/11/26/an-improved-shell-script-for-svn-resetting

For the majority of the year, I have been using Git as my preferred version control system. Recently, however, I have been using Subversion on some older projects at work.

One thing I have noticed is that the "revert" command seems to be oddly named. It's more of a svn-merge-and-undo. Maybe it's just me?

In either case, I wrote a little shell script that will act more like the Git-equivelant by allowing you to forcfully revert back to any previous revision of your project. It looks like this:

if [ $# = 3 ]
then
  currdir=`pwd`

  cd $2
  svn update
  svn merge -rHEAD:$1 $3
  svn commit -m "Reverted back to r$1"

  cd $currdir
  exit 0
else
  echo "svnreset needs three arguments!"
  exit 1
fi

Save it as "svnreset" and call it like this (the period is intentional):

$ svnreset 200 . svn://server/project/trunk

That will reset your project to r200, provided you are currently in the repositories root directory. Note also, the changes will be committed as a new revision, so if you make a mistake, just reset the reset!

I suggest that you create a symlink to /usr/bin so you can just call it straight from the terminal whenever you need it.