More SVN Code Snippets: Adding All New Files, Deleting All Removed Files

As we have mentioned before, we use Subversion to manage our codebase. That includes our WordPress set up with all its preferred and custom plugins. So, what’s the best way to update a plugin when it’s in your Subversion repository?

The approach we take (which you might find handy) is to:

  • Download the new version of the plugin and unzip it into a temporary directory. Let’s call it /mysite/wp-content/plugins/temp
  • In the temporary directory (cd /mysite/wp-content/plugins/temp), make sure all the files are in unix format: find . -type f -exec dos2unix {} +
  • In the directory for the existing version of the plugin (cd /mysite/wp-content/plugins/theplugin), delete all files but leave the directory structure and the .svn files intact: find . -type f -not -iwholename '*.svn*' -print0 | xargs -0 rm
  • Copy all the new files into to plugin directory: cp -R /mysite/wp-content/plugins/temp/theplugin/* /mysite/wp-content/plugins/theplugin/
  • Run svn remove on any files that used to be in the plugin but have been removed in the new version: svn st | grep "^\!" | awk "{print \$2}" | xargs svn remove $1
  • Run svn add on any new files in the plugin: svn st | grep "^\?" | awk "{print \$2}" | xargs svn add $1
  • Check that everything is as expected using svn status. There may be a bit of manual tidying up to be done – for example see this handy tip for dealing with filenames which contain an @ symbol: http://developers.enormego.com/view/add_2x_ios4_resources_svn
  • svn commit
  • Now, remove any completely empty directories using this routine. First run svn update
  • Now run find . -name .svn -type d | while read ss; do dir=$(dirname "$ss"); test $(ls -a "$dir" | wc -l) == 3 && svn rm "$dir"; done
  • svn commit
  • Repeat the update, find/remove, commit procedure until nothing else shows up as empty
  • A final svn status to see if anything has been missed, if so sort it out and then a final svn commit if necessary.

Have fun!