Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Eventually, you get to the point where you can’t subvert another tool or find an open source project that does just what you want. That means it’s time to build your own little jig or shim. This chapter contains lots of different ways to build tools; here are some examples of using these tools to solve problems on real projects.
I’m a big fan of the open source version control system Subversion. It is just the right combination of power, simplicity, and ease of use. Subversion is ultimately a command-line version control system, but lots of developers have created frontends for it (my favorite is the Tortoise integration with Windows Explorer). However, the real power of Subversion lies at the command line. Let’s look at an example.
I tend to add files in small batches to Subversion. To use the command-line tool, you must specify each of the filenames you want to add. This isn’t bad if you have only a few, but if you’ve added 20 files, it is cumbersome. You can use wildcards, but you’ll likely grab files that are already in version control (which doesn’t hurt anything, but you’ll get piles of error messages that might obscure other error messages). To solve this problem, I wrote a little one-line bash command:
svn st | grep '^\?' | tr '^\?' ' ' | sed 's/[ ]*//' | sed 's/[ ]/\\ /g' | xargs svn add
Table 4-3 shows what this one-liner does.
| Command | Result |
|---|---|
| svn st | Get Subversion status on all files in this directory and all its subdirectories. The new ones come back with a ? at the beginning and a tab before the filename. |
| grep '^\?' | Find all the lines that start with the ?. |
| tr '^\?' ' ' | Replace the ? with a space (the tr command translates one character for another). |
| sed 's/[ ]*//' | Using sed, the stream-based editor, substitute spaces to nothing for the leading part of the line. |
| sed 's/[ ]/\\ /g' | The filenames may have embedded spaces, so use sed again to substitute any remaining spaces with the escaped space character (a space with a \ in front). |
| xargs svn add | Take the resulting lines and pump them into the svn add command. |
This command line took the better part of 15 minutes to implement, but I’ve used this little shim (or is it a jig?) hundreds of times since.