Automating repetitive tasks is like candy for many software developers. And what better way to do this than to write fun one liners that boost efficiency and geek cred? Welcome to the joy of pipes, stdin, grep and awk.
A one liner that will add all the unversioned files to your Subversion repository:
svn status | grep ^? | awk '{print $2}' | while read line; \
do svn add $line; done
Building on this, we can do an atomic commit of the added files:
svn status | grep ^A | awk '{print $2} | while read line; \
do svn ci -m "Committing awesomeness in these files" $line; done
After we’ve modified the files and wish to make another commit:
svn status | grep ^M | awk '{print $2} | while read line; \
do svn ci -m "Awesomeness added to codebase" $line; done
So what’s going on here?
The above commands have some common parts, and perform similar functions:
svn status
Outputs the status of files in a versioned folder: modified, added, unversioned, etc
grep ^?
Looks for a line beginning with the “?” character, which is an unversioned file when using Subversion. Other symbols are M (modified), U (updated), D (deleted), A (added).
awk '{print $2}'
awk, another one of my favorites, is only printing the second field, the path of the file. The default for awk is to split fields on a tab, but you can easily change the field delimiter with the-Fswitch.
while read line;
Reads stdin a line at a time into the variableline. In this case, stdin is piped from awk.
svn add $line;
Tells the subversion server to add the file to the repository. We created the variablelinein the previous command, now we’re accessing it.
done
End of loop
Don’t forget that you can use regular expressions with most modern versions of grep. I use version 2.5.1 and it supports regex such as the one found in this little gem:
svn status | grep ^[AM] | awk '{print $2}' | while read line; do svn revert; done
here is finding any line that starts with either an A or M. Yes, throwing away changes after a couple of hours of work is typically not a cause for celebration, but once that decision is made, this script sure makes it easy!
grep