1 # narrowpatch.py - extensions to mercurial patch module to support narrow clones |
|
2 # |
|
3 # Copyright 2017 Google, Inc. |
|
4 # |
|
5 # This software may be used and distributed according to the terms of the |
|
6 # GNU General Public License version 2 or any later version. |
|
7 |
|
8 from __future__ import absolute_import |
|
9 |
|
10 from mercurial import ( |
|
11 extensions, |
|
12 patch, |
|
13 ) |
|
14 |
|
15 def setup(repo): |
|
16 def _filepairs(orig, *args): |
|
17 """Only includes files within the narrow spec in the diff.""" |
|
18 narrowmatch = repo.narrowmatch() |
|
19 if not narrowmatch.always(): |
|
20 for x in orig(*args): |
|
21 f1, f2, copyop = x |
|
22 if ((not f1 or narrowmatch(f1)) and |
|
23 (not f2 or narrowmatch(f2))): |
|
24 yield x |
|
25 else: |
|
26 for x in orig(*args): |
|
27 yield x |
|
28 |
|
29 def trydiff(orig, repo, revs, ctx1, ctx2, modified, added, removed, |
|
30 copy, getfilectx, *args, **kwargs): |
|
31 narrowmatch = repo.narrowmatch() |
|
32 if not narrowmatch.always(): |
|
33 modified = [f for f in modified if narrowmatch(f)] |
|
34 added = [f for f in added if narrowmatch(f)] |
|
35 removed = [f for f in removed if narrowmatch(f)] |
|
36 copy = {k: v for k, v in copy.iteritems() if narrowmatch(k)} |
|
37 return orig(repo, revs, ctx1, ctx2, modified, added, removed, copy, |
|
38 getfilectx, *args, **kwargs) |
|
39 |
|
40 extensions.wrapfunction(patch, '_filepairs', _filepairs) |
|
41 extensions.wrapfunction(patch, 'trydiff', trydiff) |
|