Mercurial > hg-stable
comparison mercurial/mdiff.py @ 31808:ca3b4a2b7e54
mdiff: add a hunkinrange helper function
This factors out hunk filtering logic by line range that is similar in
mdiff.blocksinrange() and hgweb.webutil.diffs().
author | Denis Laxalde <denis@laxalde.org> |
---|---|
date | Sat, 01 Apr 2017 12:24:59 +0200 |
parents | 6c80f985a13c |
children | 2d84947cd85d |
comparison
equal
deleted
inserted
replaced
31807:e6eb86b154c5 | 31808:ca3b4a2b7e54 |
---|---|
115 i2 += 1 | 115 i2 += 1 |
116 yield [base1 + s1, base1 + i1, base2 + s2, base2 + i2], btype | 116 yield [base1 + s1, base1 + i1, base2 + s2, base2 + i2], btype |
117 s1 = i1 | 117 s1 = i1 |
118 s2 = i2 | 118 s2 = i2 |
119 | 119 |
120 def hunkinrange(hunk, linerange): | |
121 """Return True if `hunk` defined as (start, length) is in `linerange` | |
122 defined as (lowerbound, upperbound). | |
123 | |
124 >>> hunkinrange((5, 10), (2, 7)) | |
125 True | |
126 >>> hunkinrange((5, 10), (6, 12)) | |
127 True | |
128 >>> hunkinrange((5, 10), (13, 17)) | |
129 True | |
130 >>> hunkinrange((5, 10), (3, 17)) | |
131 True | |
132 >>> hunkinrange((5, 10), (1, 3)) | |
133 False | |
134 >>> hunkinrange((5, 10), (18, 20)) | |
135 False | |
136 >>> hunkinrange((5, 10), (1, 5)) | |
137 False | |
138 >>> hunkinrange((5, 10), (15, 27)) | |
139 False | |
140 """ | |
141 start, length = hunk | |
142 lowerbound, upperbound = linerange | |
143 return lowerbound < start + length and start < upperbound | |
144 | |
120 def blocksinrange(blocks, rangeb): | 145 def blocksinrange(blocks, rangeb): |
121 """filter `blocks` like (a1, a2, b1, b2) from items outside line range | 146 """filter `blocks` like (a1, a2, b1, b2) from items outside line range |
122 `rangeb` from ``(b1, b2)`` point of view. | 147 `rangeb` from ``(b1, b2)`` point of view. |
123 | 148 |
124 Return `filteredblocks, rangea` where: | 149 Return `filteredblocks, rangea` where: |
148 if b1 < ubb <= b2: | 173 if b1 < ubb <= b2: |
149 if stype == '=': | 174 if stype == '=': |
150 uba = a1 + (ubb - b1) | 175 uba = a1 + (ubb - b1) |
151 else: | 176 else: |
152 uba = a2 | 177 uba = a2 |
153 if lbb < b2 and b1 < ubb: | 178 if hunkinrange((b1, (b2 - b1)), rangeb): |
154 filteredblocks.append(block) | 179 filteredblocks.append(block) |
155 if lba is None or uba is None or uba < lba: | 180 if lba is None or uba is None or uba < lba: |
156 raise error.Abort(_('line range exceeds file size')) | 181 raise error.Abort(_('line range exceeds file size')) |
157 return filteredblocks, (lba, uba) | 182 return filteredblocks, (lba, uba) |
158 | 183 |