Patchwork [1,of,2,V2,relative-diff] patch.trydiff: add support for stripping a relative root

login
register
mail settings
Submitter Siddharth Agarwal
Date March 23, 2015, 2:51 a.m.
Message ID <65f80a79101b818b3685.1427079117@devbig136.prn2.facebook.com>
Download mbox | patch
Permalink /patch/8221/
State Accepted
Commit f07047a506d1df83da2929209a34952a989df1c8
Headers show

Comments

Siddharth Agarwal - March 23, 2015, 2:51 a.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1426622381 25200
#      Tue Mar 17 12:59:41 2015 -0700
# Node ID 65f80a79101b818b3685121f7cfe3b29a8501494
# Parent  1cfded2fa1a92ee9b55d1f62675569e340a39083
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.

Patch

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -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' %