Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/patch.py @ 31284:a8023a64c40d
patch: add a diffhunks function yielding (diffheaders, hunks)
trydiff function now yield (header, hunks) tuple that are processed by
diffhunks(). Then diff() is a wrapper around diffhunks().
author | Denis Laxalde <denis.laxalde@logilab.fr> |
---|---|
date | Fri, 03 Mar 2017 17:20:11 +0100 |
parents | 92714858dd3e |
children | 451c980a8b57 |
comparison
equal
deleted
inserted
replaced
31283:92714858dd3e | 31284:a8023a64c40d |
---|---|
2213 buildopts['nobinary'] = get('nobinary', forceplain=False) | 2213 buildopts['nobinary'] = get('nobinary', forceplain=False) |
2214 buildopts['noprefix'] = get('noprefix', forceplain=False) | 2214 buildopts['noprefix'] = get('noprefix', forceplain=False) |
2215 | 2215 |
2216 return mdiff.diffopts(**buildopts) | 2216 return mdiff.diffopts(**buildopts) |
2217 | 2217 |
2218 def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None, | 2218 def diff(repo, node1=None, node2=None, match=None, changes=None, |
2219 losedatafn=None, prefix='', relroot='', copy=None): | 2219 opts=None, losedatafn=None, prefix='', relroot='', copy=None): |
2220 '''yields diff of changes to files between two nodes, or node and | 2220 '''yields diff of changes to files between two nodes, or node and |
2221 working directory. | 2221 working directory. |
2222 | 2222 |
2223 if node1 is None, use first dirstate parent instead. | 2223 if node1 is None, use first dirstate parent instead. |
2224 if node2 is None, compare node1 with working directory. | 2224 if node2 is None, compare node1 with working directory. |
2237 relroot, if not empty, must be normalized with a trailing /. Any match | 2237 relroot, if not empty, must be normalized with a trailing /. Any match |
2238 patterns that fall outside it will be ignored. | 2238 patterns that fall outside it will be ignored. |
2239 | 2239 |
2240 copy, if not empty, should contain mappings {dst@y: src@x} of copy | 2240 copy, if not empty, should contain mappings {dst@y: src@x} of copy |
2241 information.''' | 2241 information.''' |
2242 for header, hunks in diffhunks(repo, node1=node1, node2=node2, match=match, | |
2243 changes=changes, opts=opts, | |
2244 losedatafn=losedatafn, prefix=prefix, | |
2245 relroot=relroot, copy=copy): | |
2246 text = ''.join(sum((list(hlines) for hrange, hlines in hunks), [])) | |
2247 if header and (text or len(header) > 1): | |
2248 yield '\n'.join(header) + '\n' | |
2249 if text: | |
2250 yield text | |
2251 | |
2252 def diffhunks(repo, node1=None, node2=None, match=None, changes=None, | |
2253 opts=None, losedatafn=None, prefix='', relroot='', copy=None): | |
2254 """Yield diff of changes to files in the form of (`header`, `hunks`) tuples | |
2255 where `header` is a list of diff headers and `hunks` is an iterable of | |
2256 (`hunkrange`, `hunklines`) tuples. | |
2257 | |
2258 See diff() for the meaning of parameters. | |
2259 """ | |
2242 | 2260 |
2243 if opts is None: | 2261 if opts is None: |
2244 opts = mdiff.defaultopts | 2262 opts = mdiff.defaultopts |
2245 | 2263 |
2246 if not node1 and not node2: | 2264 if not node1 and not node2: |
2537 if binary and opts.git and not opts.nobinary: | 2555 if binary and opts.git and not opts.nobinary: |
2538 text = mdiff.b85diff(content1, content2) | 2556 text = mdiff.b85diff(content1, content2) |
2539 if text: | 2557 if text: |
2540 header.append('index %s..%s' % | 2558 header.append('index %s..%s' % |
2541 (gitindex(content1), gitindex(content2))) | 2559 (gitindex(content1), gitindex(content2))) |
2560 hunks = (None, [text]), | |
2542 else: | 2561 else: |
2543 if opts.git and opts.index > 0: | 2562 if opts.git and opts.index > 0: |
2544 flag = flag1 | 2563 flag = flag1 |
2545 if flag is None: | 2564 if flag is None: |
2546 flag = flag2 | 2565 flag = flag2 |
2551 | 2570 |
2552 uheaders, hunks = mdiff.unidiff(content1, date1, | 2571 uheaders, hunks = mdiff.unidiff(content1, date1, |
2553 content2, date2, | 2572 content2, date2, |
2554 path1, path2, opts=opts) | 2573 path1, path2, opts=opts) |
2555 header.extend(uheaders) | 2574 header.extend(uheaders) |
2556 text = ''.join(sum((list(hlines) for hrange, hlines in hunks), [])) | 2575 yield header, hunks |
2557 if header and (text or len(header) > 1): | |
2558 yield '\n'.join(header) + '\n' | |
2559 if text: | |
2560 yield text | |
2561 | 2576 |
2562 def diffstatsum(stats): | 2577 def diffstatsum(stats): |
2563 maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False | 2578 maxfile, maxtotal, addtotal, removetotal, binary = 0, 0, 0, 0, False |
2564 for f, a, r, b in stats: | 2579 for f, a, r, b in stats: |
2565 maxfile = max(maxfile, encoding.colwidth(f)) | 2580 maxfile = max(maxfile, encoding.colwidth(f)) |