Sunday, January 17, 2010

Cleaning up with SVN

So I had this great idea the other day when I was working with svn to remove all unversioned files from my working directory. Kind of like make clean, but without having to specifically list the files you want to keep in the Makefile. The way I did it is using the svn status command as follows:

svn status | grep ^? | cut -c8- | xargs rm -rf
svn status --no-ignore | grep ^I | cut -c8- | xargs rm -rf 

The first line removes all unversioned files, but it misses all files which svn ignores (back up files or flies that start with '.'). The second line takes care of the files svn ignores. This is a pretty dangerous command to use if you haven't added any of your newly created source files. So that I don't do that I added this to a script which has a confirmation dialog. This will list all the files that will be deleted and then asks you to confirm you want to delete them.

#!/bin/bash
true=0;
false=1;
yesNo=$false;
confirmYesNo() {
  validAnswer=$false
  until [ $validAnswer == $true ]; do
    echo -n "$1";
    read -a yesNo;
    if [ -z "$yesNo" ]; then
      validAnswer=$false;
    else
      yesNo=`echo $yesNo | tr [:upper:] [:lower:]`;
      yesNo=`expr substr $yesNo 1 1`;
      if [ $yesNo = y ] || [ $yesNo = n ]; then
        validAnswer=$true;
      else
        echo "  '$yesNo' is not an option";
        echo;
        validAnswer=$false;
      fi
    fi
  done
}

###############
# Main Method #
###############
files=`svn status | grep ^? | cut -c8-`;
files="$files`svn status --no-ignore | grep ^I | cut -c8-`";
if [ -z "$files" ]; then 
  echo "No unversioned files, exiting....";
else 
  echo "Warning! This will erase the following files:"
  echo;
  svn status | grep ^?
  svn status --no-ignore | grep ^I
  echo;
  confirmYesNo "Are you sure? [y] or [n]: "
  if [ $yesNo == y ]; then
    svn status | grep ^? | awk '{print $2}' | xargs rm -rf
    svn status --no-ignore | grep ^I | awk '{print $2}' | xargs rm -rf
    echo "Done!"
  fi
fi

FYI, this will delete folders as well as files. Its particularly useful when your using programs like Xilinx's ISE or Altera's Quartus II synthesis tools, which generate a lot of flies when you use them.

P.S. If you like the nifty syntax highlighting that I used in this post, then see Alex Gorbatchev's SyntaxHighlighter page.
P.S.S. If you want to use it with blogger then see this nice guide "easy syntax highlighting for blogger" on Carter Cole's blog.

No comments:

Post a Comment