SVN stash on bash

Since my new team uses svn, which I have not used in almost 10 years, I was very curious as to which git/mercurial features I will miss the most. Turns out - local branches. Then local commits and ability to squash them to perfection before pushing them up. But I am not writing about them yet. This blog post is about the third feature I miss the most: git stash.

It is so often that I need to temporary revert the changes I just made in a file... For example, I am fixing a method and suddenly an unrelated unit test starts to fail. I am puzzled, so I stash the changes and re-execute the original code, if anything it will provide me with a better insight on the way the code works. Here is what I came up with. This code is a few bash functions (one could put them into .bashrc file) that mimic the stash functionality I need.

# Stash some changes away
svnstash()
{
  if [ -f stash.patch ]; then
    echo "There are stashed changes already"
  else
    svn diff "${@}" > stash.patch
    svn revert -R "${@}"
  fi
}

# Re-apply stashed changes
svnunstash()
{
  patch -p0 < stash.patch
  if [ "$?" -ne "0" ]; then
    echo "There was an error applying the patch. Please apply stash.patch manually"
  else
    rm stash.patch
  fi
}

# Describe stashed changes
svndescstash()
{
  if [ -f stash.patch ]; then
    echo "Stashed changes in files:"
    grep '^Index' stash.patch
  else
    echo "No stashed changes in the current directory"
  fi
}

There are some differences from git stash. First it does not allow multiple stashed changes. This feature in git was only giving me a headache to try to remember what is in those dozen of stashed changes from months ago which I forgot to unstash. Second, it allows "partial" stashes. What if I want to temporary revert just a few files but leave the rest of the changes. I always dreaded that git did not allow that (even though there is a decent workaround - one can locally commit changes she wants to keep - but that's too much work).

Here is the sample usage session:

$ svn status src/test.h 
M       src/test.h
$ svnstash src/test.h 
Reverted 'src/test.h'
$ svn status src/test.h 
$ svndescstash 
Stashed changes in files:
Index: src/test.h
$ svnunstash 
patching file src/test.h
$ svn status src/test.h 
M       src/test.h
$ svndescstash 
No stashed changes in the current directory
$

Good.

Posted On

Category:

Tags: / / /