##// END OF EJS Templates
patch: make parsepatch optionally trim context lines...
Jun Wu -
r33270:f7b63571 default
parent child Browse files
Show More
@@ -922,18 +922,24 b' class recordhunk(object):'
922
922
923 XXX shouldn't we merge this with the other hunk class?
923 XXX shouldn't we merge this with the other hunk class?
924 """
924 """
925 maxcontext = 3
926
925
927 def __init__(self, header, fromline, toline, proc, before, hunk, after):
926 def __init__(self, header, fromline, toline, proc, before, hunk, after,
928 def trimcontext(number, lines):
927 maxcontext=None):
929 delta = len(lines) - self.maxcontext
928 def trimcontext(lines, reverse=False):
930 if False and delta > 0:
929 if maxcontext is not None:
931 return number + delta, lines[:self.maxcontext]
930 delta = len(lines) - maxcontext
932 return number, lines
931 if delta > 0:
932 if reverse:
933 return delta, lines[delta:]
934 else:
935 return delta, lines[:maxcontext]
936 return 0, lines
933
937
934 self.header = header
938 self.header = header
935 self.fromline, self.before = trimcontext(fromline, before)
939 trimedbefore, self.before = trimcontext(before, True)
936 self.toline, self.after = trimcontext(toline, after)
940 self.fromline = fromline + trimedbefore
941 self.toline = toline + trimedbefore
942 _trimedafter, self.after = trimcontext(after, False)
937 self.proc = proc
943 self.proc = proc
938 self.hunk = hunk
944 self.hunk = hunk
939 self.added, self.removed = self.countchanges(self.hunk)
945 self.added, self.removed = self.countchanges(self.hunk)
@@ -1527,8 +1533,49 b' def reversehunks(hunks):'
1527 newhunks.append(c)
1533 newhunks.append(c)
1528 return newhunks
1534 return newhunks
1529
1535
1530 def parsepatch(originalchunks):
1536 def parsepatch(originalchunks, maxcontext=None):
1531 """patch -> [] of headers -> [] of hunks """
1537 """patch -> [] of headers -> [] of hunks
1538
1539 If maxcontext is not None, trim context lines if necessary.
1540
1541 >>> rawpatch = '''diff --git a/folder1/g b/folder1/g
1542 ... --- a/folder1/g
1543 ... +++ b/folder1/g
1544 ... @@ -1,8 +1,10 @@
1545 ... 1
1546 ... 2
1547 ... -3
1548 ... 4
1549 ... 5
1550 ... 6
1551 ... +6.1
1552 ... +6.2
1553 ... 7
1554 ... 8
1555 ... +9'''
1556 >>> out = util.stringio()
1557 >>> headers = parsepatch([rawpatch], maxcontext=1)
1558 >>> for header in headers:
1559 ... header.write(out)
1560 ... for hunk in header.hunks:
1561 ... hunk.write(out)
1562 >>> print(out.getvalue())
1563 diff --git a/folder1/g b/folder1/g
1564 --- a/folder1/g
1565 +++ b/folder1/g
1566 @@ -2,3 +2,2 @@
1567 2
1568 -3
1569 4
1570 @@ -6,2 +5,4 @@
1571 6
1572 +6.1
1573 +6.2
1574 7
1575 @@ -8,1 +9,2 @@
1576 8
1577 +9
1578 """
1532 class parser(object):
1579 class parser(object):
1533 """patch parsing state machine"""
1580 """patch parsing state machine"""
1534 def __init__(self):
1581 def __init__(self):
@@ -1550,7 +1597,7 b' def parsepatch(originalchunks):'
1550 def addcontext(self, context):
1597 def addcontext(self, context):
1551 if self.hunk:
1598 if self.hunk:
1552 h = recordhunk(self.header, self.fromline, self.toline,
1599 h = recordhunk(self.header, self.fromline, self.toline,
1553 self.proc, self.before, self.hunk, context)
1600 self.proc, self.before, self.hunk, context, maxcontext)
1554 self.header.hunks.append(h)
1601 self.header.hunks.append(h)
1555 self.fromline += len(self.before) + h.removed
1602 self.fromline += len(self.before) + h.removed
1556 self.toline += len(self.before) + h.added
1603 self.toline += len(self.before) + h.added
General Comments 0
You need to be logged in to leave comments. Login now