comparison mercurial/mdiff.py @ 31269:5e7fd3a0b17f

mdiff: let _unidiff yield hunks as (<range information>, <hunk lines>) Now _unidiff yields each hunk lines packed into a tuple with the "range information" `(s1, l1, s2, l2)` that is used to build the typical hunk header '@@ -s1,l1 +s2,l2 @@'. This will be used to make it possible to filter diff hunks based on this range information. The new "range information" is ignored in unidiff() (only caller of _unidiff) for now.
author Denis Laxalde <denis.laxalde@logilab.fr>
date Thu, 02 Mar 2017 17:22:46 +0100
parents 4fba214708ee
children b3861be6aa6c
comparison
equal deleted inserted replaced
31268:4fba214708ee 31269:5e7fd3a0b17f
238 else: 238 else:
239 l2 = "+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2)) 239 l2 = "+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2))
240 l3 = "@@ -1,%d +0,0 @@\n" % len(a) 240 l3 = "@@ -1,%d +0,0 @@\n" % len(a)
241 l = [l1, l2, l3] + ["-" + e for e in a] 241 l = [l1, l2, l3] + ["-" + e for e in a]
242 else: 242 else:
243 l = list(_unidiff(a, b, opts=opts)) 243 l = sum((hlines for hrange, hlines in _unidiff(a, b, opts=opts)), [])
244 if not l: 244 if not l:
245 return "" 245 return ""
246 246
247 l.insert(0, "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1))) 247 l.insert(0, "--- %s%s%s" % (aprefix, fn1, datetag(ad, fn1)))
248 l.insert(1, "+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2))) 248 l.insert(1, "+++ %s%s%s" % (bprefix, fn2, datetag(bd, fn2)))
252 l[ln] += "\n\ No newline at end of file\n" 252 l[ln] += "\n\ No newline at end of file\n"
253 253
254 return "".join(l) 254 return "".join(l)
255 255
256 def _unidiff(t1, t2, opts=defaultopts): 256 def _unidiff(t1, t2, opts=defaultopts):
257 """Yield hunks of a headerless unified diff from t1 and t2 texts.""" 257 """Yield hunks of a headerless unified diff from t1 and t2 texts.
258
259 Each hunk consists of a (hunkrange, hunklines) tuple where `hunkrange` is a
260 tuple (s1, l1, s2, l2) representing the range information of the hunk to
261 form the '@@ -s1,l1 +s2,l2 @@' header and `hunklines` is a list of lines
262 of the hunk combining said header followed by line additions and
263 deletions.
264 """
258 l1 = splitnewlines(t1) 265 l1 = splitnewlines(t1)
259 l2 = splitnewlines(t2) 266 l2 = splitnewlines(t2)
260 def contextend(l, len): 267 def contextend(l, len):
261 ret = l + opts.context 268 ret = l + opts.context
262 if ret > len: 269 if ret > len:
296 if alen: 303 if alen:
297 astart += 1 304 astart += 1
298 if blen: 305 if blen:
299 bstart += 1 306 bstart += 1
300 307
301 yield "@@ -%d,%d +%d,%d @@%s\n" % (astart, alen, 308 hunkrange = astart, alen, bstart, blen
302 bstart, blen, func) 309 hunklines = (
303 for x in delta: 310 ["@@ -%d,%d +%d,%d @@%s\n" % (hunkrange + (func,))]
304 yield x 311 + delta
305 for x in xrange(a2, aend): 312 + [' ' + l1[x] for x in xrange(a2, aend)]
306 yield ' ' + l1[x] 313 )
314 yield hunkrange, hunklines
307 315
308 # bdiff.blocks gives us the matching sequences in the files. The loop 316 # bdiff.blocks gives us the matching sequences in the files. The loop
309 # below finds the spaces between those matching sequences and translates 317 # below finds the spaces between those matching sequences and translates
310 # them into diff output. 318 # them into diff output.
311 # 319 #