patch.diff: add support for diffs relative to a subdirectory
authorSiddharth Agarwal <sid0@fb.com>
Tue, 17 Mar 2015 13:41:24 -0700
changeset 24417 f2e1e097cda1
parent 24416 f07047a506d1
child 24419 0e41f110e69e
patch.diff: add support for diffs relative to a subdirectory For now this implementation is pretty naive -- it filters out files right before passing them into trydiff. In upcoming patches we'll add some more smarts.
mercurial/patch.py
--- a/mercurial/patch.py	Tue Mar 17 12:59:41 2015 -0700
+++ b/mercurial/patch.py	Tue Mar 17 13:41:24 2015 -0700
@@ -2059,7 +2059,7 @@
     return mdiff.diffopts(**buildopts)
 
 def diff(repo, node1=None, node2=None, match=None, changes=None, opts=None,
-         losedatafn=None, prefix=''):
+         losedatafn=None, prefix='', relroot=''):
     '''yields diff of changes to files between two nodes, or node and
     working directory.
 
@@ -2076,7 +2076,9 @@
 
     prefix is a filename prefix that is prepended to all filenames on
     display (used for subrepos).
-    '''
+
+    relroot, if not empty, must be normalized with a trailing /. Any match
+    patterns that fall outside it will be ignored.'''
 
     if opts is None:
         opts = mdiff.defaultopts
@@ -2120,9 +2122,23 @@
     if opts.git or opts.upgrade:
         copy = copies.pathcopies(ctx1, ctx2)
 
+    if relroot is not None:
+        # XXX this would ideally be done in the matcher, but that is generally
+        # meant to 'or' patterns, not 'and' them. In this case we need to 'and'
+        # all the patterns from the matcher with relroot.
+        def filterrel(l):
+            return [f for f in l if f.startswith(relroot)]
+        modified = filterrel(modified)
+        added = filterrel(added)
+        removed = filterrel(removed)
+        # filter out copies where either side isn't inside the relative root
+        copy = dict(((dst, src) for (dst, src) in copy.iteritems()
+                     if dst.startswith(relroot)
+                     and src.startswith(relroot)))
+
     def difffn(opts, losedata):
         return trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
-                       copy, getfilectx, opts, losedata, prefix, '')
+                       copy, getfilectx, opts, losedata, prefix, relroot)
     if opts.upgrade and not opts.git:
         try:
             def losedata(fn):