[p4ruby] diff output

bob p4 jeffperforce at gmail.com
Tue Mar 11 12:55:30 PDT 2008


I was surprised that p4.run("diff", "...") gives an array containing a
mixture of hashes and diff lines.  I expected all hashes with the diff
lines under the "output" key or some such.

One of my pet peeves about perforce is that its diff output cannot
immediately be fed into patch.  The following script produces a proper
patch format.

require 'p4'

p4 = P4.new
Hash[*ARGV].each_pair { |key, value|
   p4.send("#{key}=", value)
}
p4.connect

last_elem = nil
p4.run("diff", "-du", "...").each { |elem|
   if elem.is_a?(String)
      # encountered a diff line ... I hope ...

      if last_elem.is_a?(Hash)
         # write the diff header
         %w(- +).each { |sig|
            puts([sig*3, last_elem["clientFile"]].join(" "))
         }
      end

      # write a diff line
      puts(elem)
   end
   last_elem = elem
}

It's not really a big deal that the p4ruby-diff behaves as it does,
it's just that remembering the last element and checking the type
seems rather clunky.  I expected something along the lines of,

p4.run("diff", "-du", "...").each { |elem|
   if elem["output"]
      # write the diff header
      %w(- +).each { |sig|
         puts([sig*3, elem["clientFile"]].join(" "))
      }

      # write diff lines
      puts(elem["output"])
   end
}

--Jeff



More information about the p4ruby mailing list