Show More
@@ -922,18 +922,24 b' class recordhunk(object):' | |||
|
922 | 922 | |
|
923 | 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 |
|
|
928 | def trimcontext(number, lines): | |
|
929 | delta = len(lines) - self.maxcontext | |
|
930 | if False and delta > 0: | |
|
931 |
|
|
|
932 | return number, lines | |
|
926 | def __init__(self, header, fromline, toline, proc, before, hunk, after, | |
|
927 | maxcontext=None): | |
|
928 | def trimcontext(lines, reverse=False): | |
|
929 | if maxcontext is not None: | |
|
930 | delta = len(lines) - maxcontext | |
|
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 | 938 | self.header = header |
|
935 |
|
|
|
936 | self.toline, self.after = trimcontext(toline, after) | |
|
939 | trimedbefore, self.before = trimcontext(before, True) | |
|
940 | self.fromline = fromline + trimedbefore | |
|
941 | self.toline = toline + trimedbefore | |
|
942 | _trimedafter, self.after = trimcontext(after, False) | |
|
937 | 943 | self.proc = proc |
|
938 | 944 | self.hunk = hunk |
|
939 | 945 | self.added, self.removed = self.countchanges(self.hunk) |
@@ -1527,8 +1533,49 b' def reversehunks(hunks):' | |||
|
1527 | 1533 | newhunks.append(c) |
|
1528 | 1534 | return newhunks |
|
1529 | 1535 | |
|
1530 | def parsepatch(originalchunks): | |
|
1531 |
"""patch -> [] of headers -> [] of hunks |
|
|
1536 | def parsepatch(originalchunks, maxcontext=None): | |
|
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 | 1579 | class parser(object): |
|
1533 | 1580 | """patch parsing state machine""" |
|
1534 | 1581 | def __init__(self): |
@@ -1550,7 +1597,7 b' def parsepatch(originalchunks):' | |||
|
1550 | 1597 | def addcontext(self, context): |
|
1551 | 1598 | if self.hunk: |
|
1552 | 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 | 1601 | self.header.hunks.append(h) |
|
1555 | 1602 | self.fromline += len(self.before) + h.removed |
|
1556 | 1603 | self.toline += len(self.before) + h.added |
General Comments 0
You need to be logged in to leave comments.
Login now