Archive for the ‘hacks’ Category
More xargs hackery
Monday, June 25th, 2007Oftentimes, 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 statuswill list any files that have changed in the working copy, along with files that are not under version control, appended by a question markgrep '?'andgrep -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 filesedperforms text manipulation specified by-e, which is interpreted as a-Eextended regular expression, replacing all question marks followed by space with nothing- Finally,
xargspasses each line from the previous chain as an argument tosvn 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.
