Archive for the ‘hacks’ Category

Google in Kitteh

Sunday, April 27th, 2008

Needs moar cute.

More xargs hackery

Monday, June 25th, 2007

Oftentimes, one will create a number of files in a source-controlled working copy when implementing a feature. When you go to commit, you realize you have to run svn add on each file. You might try to re-add the working copy, but SVN will balk and return this error:

svn: warning: 'nameofworkingcopy' is already under version control

But this is Linux, right? Surely you don’t have to manually add each file!

Sure enough, xargs can save us some time. The following command will run svn add on each file in the working copy that is not under version control and whose filename does not contain ‘.pyc‘ (in this instance, compiled Python bytecode.)

svn status | grep '?' | grep -v '.pyc' | sed -Ee 's/?      //' | xargs svn add

The breakdown:

  • svn status will list any files that have changed in the working copy, along with files that are not under version control, appended by a question mark
  • grep '?' and grep -v '.pyc' match (and in the case of the inverted -v, does not match) lines signifying an unknown file or filenames to a compiled Python bytecode file
  • sed performs text manipulation specified by -e, which is interpreted as a -E extended regular expression, replacing all question marks followed by space with nothing
  • Finally, xargs passes each line from the previous chain as an argument to svn add, putting these files under version control.

If you have a few files you don’t want to add, add the -P -l1 option to xargs for a confirmation on each execution.