MAY
30

I love SVN. It frequently saves my life when I go to update one of my sites and end up crashing the whole thing. Hopefully all the developers out there are using SVN too. If not you really should head over to http://subversion.tigris.org and get dirty with it (I may make a post later about creating/adding repositories, but I think that is pretty well documented). One of the daily issues I face is that Dreamweaver forgets to tell SVN that I added or removed a file. I work in Dreamweaver about 80% of the time when I am coding for a website (the rest the time I use VIM on my Fedora box), and I use their built in FTP to manage files. Lets take todays example where I am checking in a new version of a template for a site that involved deleting and adding tons of images. Heres what my svn st looks like:

?      images/st.gif
?      images/wt.gif
?      images/lb.gif
?      images/lc.gif
?      images/nav.png
?      images/lt.gif
?      images/sb.gif
?      images/sc.gif
?      images/wb.gif
?      images/wc.gif
!      images/product_1.jpg
!      images/shadow_l.png
!      images/shadow_bl.png
!      images/shadow_r.png
!      images/shadow_br.png
!      images/shadow_t.png
!      images/nav_buttons
!      images/nav_over.png
!      images/shadow_tl.png
!      images/shadow_tr.png
!      images/nav_inactive.png
!      images/nav_active.png
!      images/shadow_b.png
M      teamr4r_add.php
M      css/tr4r.css
M      css/main.css
M      index.php
M      functions.php
M      teamr4r.php

Checking this stuff in right now would be a newb mistake. We need to add all the files with a ? and delete all those files with ! from our repository on the next check-in. Maybe I have ADHD or maybe I just like saving time, but either way I found this cool trick online a long time ago to quickly clean this kind of mess up (sorry to the person that shared it, but I forogot who you are). I have two really useful entries in my ~/.bash_profile that helps me to check-in a lot quicker. I used to use svn add –force * to add all the files, but that’s a couple more letters of typing than I would like so I added these aliases:

alias svnadderall="svn st | grep '^?' | cut -b 8- | xargs svn add"
alias svnridallin="svn st | grep '^!' | cut -b 8- | xargs svn rm"

So what on earth do they actually do? Hopefully you understand what svn st does, look at the man svn pages for help if you don’t. We then pipe that output to our buddy grep, which will find every line that starts with a ? and output that. So we then pipe that cut that output’s first 8 characters (the ? and the spaces after it) so that all that is left is the filenames of each of the files that we want to add to the repository, each on their own line. That ouput is thrown over to xargs, which will take each line and pass it as a parameter to the following command. Since we want to add each of those files to the repository, we run svn add on each line and we are done! svnridallin does the same kind of thing, but instead it finds all our ! files (files that we deleted but forgot to tell SVN about) and runs svn rm on them. After quickly typing svnridallin and svnadderall, we can finally run our svn ci -m “thanks aliases!”.

These two little aliases may not save a ton of time, but I found them to be quite useful. I use them all the time now. If you end up using them or if you find another useful SVN trick, drop me a comment and let me know!

1 Comment to “SVN Tricks”

  1. ignacio says:

    That’s a nice trick to have it as an alias Matt!. May I mention a possible tweak related to this?
    At my work we have svn sandboxes (copies of the files which could exist independently for testing purposes in our own directories) so I often do have stuff not yet checked into the repository (marked with either “?”, “!” or “M”). So instead of “xargs svn add” in such a case you can use “xargs echo svn add”.
    What the echo part does is simply output in lieu of just executing the command. The convenient list of file generated with xargs is at your disposal beforehand giving you a chance to inspect it. At that point we could:
    a) execute it right away piping it to bash (adding “| bash”)
    b) edit that list generated and exclude those files you want to preserve for being added (checked-in or deleted) to the repository, copy and then run it.

    Of course this is a tweak that may not apply to your work-flow, but I thought of sharing the use of the echo and bash thing which I found very helpful in general (some examples are here at my post http://ignaciopp.wordpress.com/2009/05/07/pipe-to-bash/

Leave a Reply