Extract Files From Subversion for Propagation

Posted about 16 years ago

Problem:

You store your site files in a subversion repository for source control. When you update your site you don't want to upload the entire tree (nor the svn working copy files) nor do you want to spend time hunting and pecking for the changed files.

Solution:

Use the following script on *nix to extract non-svn files that have recently changed:

find . -not ( -name '.svn' -type d -prune ) -not ( -name '*.log' ) -type f -mmin -240 -print | zip ~/Desktop/prop -@

Discussion:

We're building upon syntax we've used in two previous posts: delete files from a subversion sandbox and zip changed files in your subversion sandbox.

In this case we are using the "find" command combined with the -mmin option to find file modified within a specified number of minutes. find . -mmin -X will find files in the current directory structure modified in the last X minutes. find . -mmin X will find files in the current directory structure modified before X minutes ago.

In the example above we're findiing all files not in .svn folders modified less than 240 minutes ago. We pipe the results of that find to the zip utility to zip up the results. We are also filtering out the Rails-specific "development.log" from being zipped.

If you want to propagate an entire directory structure you can use svn export on your working copy to extract out all your files (this is the same as a checkout without the .svn folders). You can also svn export from the repository to pull a specific revision or branch.

svn export . ~/Desktop/prop
svn export http://repos/project/tags/release_1.0 ~/Desktop/prop

And for those Rails developers you can obviously use Capistrano to get around most of these issues.

Delete All Files in a Subversion Working Copy

Posted about 16 years ago

Problem:

You want to remove all the files in your subversion working copy but you do not want to remove the folder structure.

Description:

I have had to do this when upgrading an application from Rails 1.x to Rails 2.0. The file structure of my project changed significantly enough that I just moved everything out to a non-working copy to do the work. When I started to move the work back in there were a lot of files/folders that had changed.

Rather than work through the manual process of moving files in svn it's sometimes easier to just start from a new point in time within the same repository. In that case you want to:

  1. remove all the files in your current working copy yet preserve the folder structure for SVN
  2. move all the new files into your working copy and commit those changes
  3. clean up any new/missing files and folders.

Solution:

*nix

From the command line run:

find . -not \( -name '.svn' -type d -prune \) -type f -print | xargs rm 

This says....

find from the current directory (find .)
...ignoring files in a directory called '.svn' (-not ( -name '.svn' -type d -prune ))
...any file (-type f)
...and delete it (-print lists the files and xargs rm removes any file in that list)

oddly enough the following _will_ list out the same as above...

find . -type d -name '.svn' -prune -o -type f -print
but -delete is invoked with -depth implied so you will end up deleting required svn files if you run

find . -type d -name '.svn' -prune -o -type f -print

Windows

This is easier from the UI with your favorite search tool. I use Directory Opus so from the advanced tab specify Location to not match .svn and type matches files only.

Any other good ideas for this on Windows?

RAILS_DEFAULT_LOGGER: Writing to the Log in Rails

Posted about 16 years ago

One great thing about Rails is the logs that are produced in development mode. By default rails will list out all SQL statements and trace all controller calls.

It is possible to add custom messages to the rails log through a poorly documented RAILS_DEFAULT_LOGGER object which is defined in environment.rb.

The following log levels are available:

  • debug
  • info
  • warn
  • error
  • fatal

Therefore, to add debug calls to your code for development mode just add the following:

RAILS_DEFAULT_LOGGER.info "foo bar"

I've found it useful to add a method to application_helper for this for consistency. It helps as well to add line feeds and searchable text to make your output stand out in the log file.

def add_to_log(output)
  RAILS_DEFAULT_LOGGER.info "\n ***mylog*** #{output}\n"
end

Easily Zip Changed Files in Your Subversion Working Copy

Posted about 16 years ago

Subversion is a great tool for source control on a team of software developers. Every check-in we do into subversion must be accompanied by a code review from a fellow developer. The best method for doing this is to send the modified files to another developer in zip format. But how does one do this with a large source tree?

*nix

From the command line run the following:

svn status | grep ^[AM] | awk '{print $2}' | zip ~/Desktop/cr -@

A small explanation of what's going on here:

  • this makes heavy use of piping (aka the | or "pipe" character) which sends the output of a command on the left of the pipe as input to the command on the right. as a short exampe ls -l | grep ^A will send a directory listing to grep will will then evaluate that list and parse out only those entries that start with A (which really will be none...).

  • svn status will list all files in your working copy (local sandbox) that are not in sync with the svn repository.

  • svn status returns ?, !, A, M, D for "unknown," "file missing," "added," "modified," and "deleted" respectively. it lists this info in column 1 and the file path in column 2.

  • we use grep ^[AM] to filter out the list for added and modified files only. it's a regular expression that says find matches where the line starts with A or M. this means the output of svn status | grep ^[AM] will be a list of all added and modified files in your working copy.

  • we use awk '{print $2}' to print the 2nd column front standard input. this means that svn status | grep ^[AM] | awk '{print $2}' will list the paths of each file in your svn working copy that has been added or modified.

  • zip is the most obvious command in this sequence - it builds a zip file in the location specified. the only odd option is -@ which tells zip to take the list of files to zip from standard input.

I have actually saved this to a bash script to make sure I delete any existing cr.zip.

Windows

Heaven forbid you can actually do something more easily and nearly as geeky on Windows but it's true....

This assumes you use TortoiseSVN which is a stellar program that integrates into the Windows shell.

Right-click on your trunk directory and choose TortoiseSVN => SVN Check for Modifications. This command sends svn status and returns the list to a dialog box. From here you can double-click each file for easy diff.

To zip these files up create a new zip file (on your desktop) with your zip utility of choice (aka WinRar or WinZip - as you'll see in a moment) and open the zip in your utility.

From the TortoiseSVN window select all the files you want to zip (you will have to manually unselect (or not select) deleted, missing, or non-added files which is made easier if you sort by the "text status" column) and drag them to the title bar of your zip utility. With WinRar and WinZip you get a dialog giving you options about how you want to add these files - make sure you select to add the files with full paths or when they are unzipped they'll be all at the root of the zip file.

I have not played with other zip utilities on windows outside of 7z which does give you the option to add files by full path.