Ruby Patch to Zip

From EggeWiki
Jump to navigation Jump to search

I needed to take a patch file, which contained patches from more than one Eclipse project, and create a zip file with each file contained within.

The Eclipse patch file basically looks like:

<geshi lang="patch">

      1. Eclipse Workspace Patch 1.0
  1. P My Little Project

Index: src/com/


RCS file: /usr/dev/cvs/cvsroot/My Little Project/src/com/,v retrieving revision diff -u -r1. --- src/com/ 24 May 2007 01:20:56 -0000 +++ src/com/ 5 Sep 2007 01:00:23 -0000 @@ -338,8 +338,6 @@

private void setupCustomers( StringBuffer errorBuf_ )

- // a comment I removed -

    System.out.println("Hello customer");


You have a project definition, followed by files in that project. You can have multiple project definitions. I created a Ruby script to parse the file and keep track of the project name. The script then outputs the qualified path to each file it encounters. Sure, I could have done this with awk, but Ruby is more enjoyable and readable.

<geshi lang="ruby">

  1. !/bin/env ruby

ARGV.each do |arg|

 if !File.exists?(arg) then
   raise(RuntimeError,"#{arg} does not exist!")
 end, 'r') do |infile|
   while(line = infile.gets)
     if line =~ /^#P ([^\r]*)/ then # ignore any new line characters

project = $1

     if line =~ /^Index: ([^\r]*)/ then

puts '"' + project + '/' + $1 + '"'


end </geshi>

Lastly, to zip up these files, I used bash/Cygwin.

<geshi lang="bash"> ./patchToFiles.rb patches/mypatch.patch | xargs zip </geshi>