Processing invalid patches

time to read 7 min | 1272 words

I just got a patch file that SVN couldn't handle. I handled the issue with this piece of code, here for future reference:

import System.IO


file = """random.patch""";

lines = File.ReadAllLines(file)

currentFile as TextWriter

for line in lines:

      if line.StartsWith("+++"):#new file

            currentFile.Dispose() if currentFile is not null

            fileName = line.Replace("+++","").Replace("(revision 0)","").Trim()

            currentFile = File.CreateText(fileName)

            print fileName


      continue if not line.StartsWith("+")

      currentFile.WriteLine( line.Substring(1) )

I love Boo!