Skip to content

Instantly share code, notes, and snippets.

@cwalther
Created May 13, 2012 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwalther/2688500 to your computer and use it in GitHub Desktop.
Save cwalther/2688500 to your computer and use it in GitHub Desktop.
Applies some of the changes to unified diff necessary to port from H-uru/Plasma to CWE-ou
#!/usr/bin/env python
import sys
import re
import fileinput
import time
import calendar
def innerspacestotabs(m):
if m.end() % 4 == 0:
return (m.end()-m.start()+3)//4*"\t"
else:
return m.group()
kStateFirstLine = 0
kStateHeaders = 1
kStateMessage = 2
kStateDiffstat = 3
kStateOutsideHunk = 4
kStateInsideHunk = 5
state = kStateFirstLine
spacestotabs = True
for line in fileinput.input():
if line.endswith("\r\n"):
line = line[:-2]
elif line.endswith("\n") or line.endswith("\r"):
line = line[:-1]
if state == kStateFirstLine:
if line.startswith("From "):
state = kStateHeaders
metadata = "# HG changeset patch\n"
message = ""
continue
else:
state = kStateOutsideHunk
if state == kStateHeaders:
if line.startswith("From: "):
metadata += "# User %s\n" % line[6:]
elif line.startswith("Date: "):
tzoffset = (1 if line[-5] == "-" else -1)*(int(line[-4:-2])*3600 + int(line[-2:])*60)
metadata += "# Date %d %d\n" % (calendar.timegm(time.strptime(line[6:-6], "%a, %d %b %Y %H:%M:%S")) + tzoffset, tzoffset)
elif line.startswith("Subject: "):
message = re.sub(r"^\[PATCH.*\] ", "", line[9:]) + "\n"
elif len(line) == 0:
sys.stdout.write(metadata)
message += "\n"
state = kStateMessage
else:
# junk header, ignore
pass
elif state == kStateMessage:
if line == "---":
sys.stdout.write(message)
state = kStateDiffstat
else:
message += line + "\n"
elif state == kStateDiffstat:
if line.startswith("diff "):
sys.stdout.write("\n")
sys.stdout.write(line)
sys.stdout.write("\n")
state = kStateOutsideHunk
else:
# ignore
pass
elif state == kStateOutsideHunk:
# outside of hunk
m = re.match(r"^(--- (a/)?|\+\+\+ (b/)?)(.*)$", line)
if m:
# file name
prefix = m.group(1)
filename = m.group(4)
if filename.startswith("Sources/"):
filename = "MOULOpenSourceClientPlugin/Plasma20/" + filename
spacestotabs = not filename.endswith(".py") and not filename.endswith(".rc") and not filename.endswith("resource.h")
line = prefix + filename
else:
m = re.match(r"^@@ -([0-9]+),([0-9]+) \+([0-9]+),([0-9]+) @", line)
if m:
# hunk header
hunklenold = int(m.group(2))
hunklennew = int(m.group(4))
state = kStateInsideHunk
else:
# junk
pass
sys.stdout.write(line)
sys.stdout.write("\n")
else:
# kStateInsideHunk
m = re.match(r"([ +-])(( )*)(.*)$", line)
if m:
if spacestotabs:
line = m.group(1) + len(m.group(2))//4*"\t" + re.sub(r" {2,}", innerspacestotabs, m.group(4))
if m.group(1) == " ":
hunklenold -= 1
hunklennew -= 1
elif m.group(1) == "+":
hunklennew -= 1
elif m.group(1) == "-":
hunklenold -= 1
if hunklennew <= 0 and hunklenold <= 0:
if hunklennew != hunklenold:
sys.stderr.write("hunk length mismatch on line %d:\n" % fileinput.lineno())
sys.stderr.write(line)
sys.stderr.write("\n")
# end of hunk
state = kStateOutsideHunk
elif line[0] == "\\":
# "\ No newline at end of file"
pass
else:
sys.stderr.write("malformed line %d:\n" % fileinput.lineno())
sys.stderr.write(line)
sys.stderr.write("\n")
sys.exit(1)
sys.stdout.write(line)
sys.stdout.write("\r\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment