Automate adding/deleting files from Subversion
Serious nerd alert - but if you use Drupal and/or Subversion, you'll likely find this interesting. Lately I've been dealing with a Drupal module called 'dbscripts 'that helps me track changes made in a database with Subversion (SVN). Besides a few moments requiring banging my head against the wall, it's a pretty slick module - before this, keeping track of database changes and making it easy to roll back to a previous version was not something easy to do. But one thing that's been tricky to manage with the module is remembering to add/delete the files it generates/deletes in my Subversion repository. This has had nasty consequences of forgetting to check-in a newly added database table or causing conflicts by regenerating a supposedly deleted database table during a database restore... Shell scripts to the rescue!
The dbscripts module 'dumps' your database into a series of files, one for each table schema and one for each data set in that table. It dumps these files all to the same directory, which is groovy - and since your database is broken out into its components (rather than the usual full database schema/data dump file), it's possible to use SVN (or some other revision control system [RCS]) to keep track of changes. But when a table gets removed from your database, dbscripts just deletes the old table dump files - no big deal, but if you're using SVN, you need to remember to run 'svn del' on those files to get them out of the repository. Likewise, when you add a table to your database, dbscripts adds the files... but you need to remember to run 'svn add' on those files to get them into SVN. I always forget to do this, and it wreaks all kinds of havoc.
So to solve my problem, I wrote a few shell scripts to automate these processes.
For SVN deleting:
#!/bin/bash
#####################################################################
# Delete missing files from a Subversion working directory.
# Obvi, use with caution.
#
# Sed command may need to be tweaked for your environment.
#
# Subversion /should/ tell you what files it's planning on deleting
# and I suggest you review it before checking in.
#
# Author: Arthur Richards (awjrichards [at] gmail)
# Copyright: None, just please do not use for evil.
#####################################################################
for f in `svn status | grep ! | sed 's/^![ ]*//'`
do
svn del "$f"
doneFor SVN adding:
#!/bin/bash
#####################################################################
# Add unversioned files to a Subversion working directory.
# Obvi, use with caution.
#
# Sed command may need to be tweaked for your environment.
#
# Subversion /should/ tell you what files it's planning on adding
# and I suggest you review it before checking in.
#
# Author: Arthur Richards (awjrichards [at] gmail)
# Copyright: None, just please do not use for evil.
#####################################################################
for f in `svn status | grep ? | sed 's/^?[ ]*//'`
do
svn add "$f"
donePretty straightforward and very basic... feel free to do what you will with these. I aliased these scripts to my /usr/bin/ directory so they can be easily called. Remember that these will determine the files to add/delete based on the directory you call the script(s) from. If you want to use these for directories that may not have particularly predictable missing/new files, etc., be careful. SVN will still tell you what files it's planning on adding/deleting, so be sure to review the output before committing your changes.




Post new comment