Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/patch.py @ 41629:e834f6f6f221
patch: pass in context objects into diffhunks() (API)
It's a pretty low-level function and having the contexts in
patch.diff() makes future patches easier.
Differential Revision: https://phab.mercurial-scm.org/D5891
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Wed, 06 Feb 2019 17:27:43 -0800 |
parents | 47c92f8ed128 |
children | 035cae1d197f |
comparison
equal
deleted
inserted
replaced
41628:36ee0d6d64c5 | 41629:e834f6f6f221 |
---|---|
2266 information. | 2266 information. |
2267 | 2267 |
2268 hunksfilterfn, if not None, should be a function taking a filectx and | 2268 hunksfilterfn, if not None, should be a function taking a filectx and |
2269 hunks generator that may yield filtered hunks. | 2269 hunks generator that may yield filtered hunks. |
2270 ''' | 2270 ''' |
2271 if not node1 and not node2: | |
2272 node1 = repo.dirstate.p1() | |
2273 | |
2274 ctx1 = repo[node1] | |
2275 ctx2 = repo[node2] | |
2276 | |
2271 for fctx1, fctx2, hdr, hunks in diffhunks( | 2277 for fctx1, fctx2, hdr, hunks in diffhunks( |
2272 repo, node1=node1, node2=node2, | 2278 repo, ctx1=ctx1, ctx2=ctx2, |
2273 match=match, changes=changes, opts=opts, | 2279 match=match, changes=changes, opts=opts, |
2274 losedatafn=losedatafn, prefix=prefix, relroot=relroot, copy=copy, | 2280 losedatafn=losedatafn, prefix=prefix, relroot=relroot, copy=copy, |
2275 ): | 2281 ): |
2276 if hunksfilterfn is not None: | 2282 if hunksfilterfn is not None: |
2277 # If the file has been removed, fctx2 is None; but this should | 2283 # If the file has been removed, fctx2 is None; but this should |
2284 if hdr and (text or len(hdr) > 1): | 2290 if hdr and (text or len(hdr) > 1): |
2285 yield '\n'.join(hdr) + '\n' | 2291 yield '\n'.join(hdr) + '\n' |
2286 if text: | 2292 if text: |
2287 yield text | 2293 yield text |
2288 | 2294 |
2289 def diffhunks(repo, node1=None, node2=None, match=None, changes=None, | 2295 def diffhunks(repo, ctx1, ctx2, match=None, changes=None, |
2290 opts=None, losedatafn=None, prefix='', relroot='', copy=None): | 2296 opts=None, losedatafn=None, prefix='', relroot='', copy=None): |
2291 """Yield diff of changes to files in the form of (`header`, `hunks`) tuples | 2297 """Yield diff of changes to files in the form of (`header`, `hunks`) tuples |
2292 where `header` is a list of diff headers and `hunks` is an iterable of | 2298 where `header` is a list of diff headers and `hunks` is an iterable of |
2293 (`hunkrange`, `hunklines`) tuples. | 2299 (`hunkrange`, `hunklines`) tuples. |
2294 | 2300 |
2295 See diff() for the meaning of parameters. | 2301 See diff() for the meaning of parameters. |
2296 """ | 2302 """ |
2297 | 2303 |
2298 if opts is None: | 2304 if opts is None: |
2299 opts = mdiff.defaultopts | 2305 opts = mdiff.defaultopts |
2300 | |
2301 if not node1 and not node2: | |
2302 node1 = repo.dirstate.p1() | |
2303 | 2306 |
2304 def lrugetfilectx(): | 2307 def lrugetfilectx(): |
2305 cache = {} | 2308 cache = {} |
2306 order = collections.deque() | 2309 order = collections.deque() |
2307 def getfilectx(f, ctx): | 2310 def getfilectx(f, ctx): |
2314 order.remove(f) | 2317 order.remove(f) |
2315 order.append(f) | 2318 order.append(f) |
2316 return fctx | 2319 return fctx |
2317 return getfilectx | 2320 return getfilectx |
2318 getfilectx = lrugetfilectx() | 2321 getfilectx = lrugetfilectx() |
2319 | |
2320 ctx1 = repo[node1] | |
2321 ctx2 = repo[node2] | |
2322 | 2322 |
2323 if relroot: | 2323 if relroot: |
2324 relrootmatch = scmutil.match(ctx2, pats=[relroot], default='path') | 2324 relrootmatch = scmutil.match(ctx2, pats=[relroot], default='path') |
2325 match = matchmod.intersectmatchers(match, relrootmatch) | 2325 match = matchmod.intersectmatchers(match, relrootmatch) |
2326 | 2326 |