patch.trydiff: add support for stripping a relative root
authorSiddharth Agarwal <sid0@fb.com>
Tue, 17 Mar 2015 12:59:41 -0700
changeset 24416 f07047a506d1
parent 24415 1cfded2fa1a9
child 24417 f2e1e097cda1
patch.trydiff: add support for stripping a relative root This assumes that if relroot is not None, all the files in modified, added and removed start with it. In upcoming patches we'll follow that.
mercurial/patch.py
--- a/mercurial/patch.py	Fri Mar 20 17:30:38 2015 -0500
+++ b/mercurial/patch.py	Tue Mar 17 12:59:41 2015 -0700
@@ -2122,7 +2122,7 @@
 
     def difffn(opts, losedata):
         return trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
-                       copy, getfilectx, opts, losedata, prefix)
+                       copy, getfilectx, opts, losedata, prefix, '')
     if opts.upgrade and not opts.git:
         try:
             def losedata(fn):
@@ -2230,13 +2230,16 @@
         yield f1, f2, copyop
 
 def trydiff(repo, revs, ctx1, ctx2, modified, added, removed,
-            copy, getfilectx, opts, losedatafn, prefix):
+            copy, getfilectx, opts, losedatafn, prefix, relroot):
     '''given input data, generate a diff and yield it in blocks
 
     If generating a diff would lose data like flags or binary data and
     losedatafn is not None, it will be called.
 
-    prefix is added to every path in the diff output.'''
+    relroot is removed and prefix is added to every path in the diff output.
+
+    If relroot is not empty, this function expects every path in modified,
+    added, removed and copy to start with it.'''
 
     def gitindex(text):
         if not text:
@@ -2261,6 +2264,13 @@
 
     gitmode = {'l': '120000', 'x': '100755', '': '100644'}
 
+    if relroot != '' and (repo.ui.configbool('devel', 'all')
+                          or repo.ui.configbool('devel', 'check-relroot')):
+        for f in modified + added + removed + copy.keys() + copy.values():
+            if f is not None and not f.startswith(relroot):
+                raise AssertionError(
+                    "file %s doesn't start with relroot %s" % (f, relroot))
+
     for f1, f2, copyop in _filepairs(
             ctx1, modified, added, removed, copy, opts):
         content1 = None
@@ -2293,8 +2303,10 @@
                 (f1 and f2 and flag1 != flag2)):
                 losedatafn(f2 or f1)
 
-        path1 = posixpath.join(prefix, f1 or f2)
-        path2 = posixpath.join(prefix, f2 or f1)
+        path1 = f1 or f2
+        path2 = f2 or f1
+        path1 = posixpath.join(prefix, path1[len(relroot):])
+        path2 = posixpath.join(prefix, path2[len(relroot):])
         header = []
         if opts.git:
             header.append('diff --git %s%s %s%s' %