changeset 24244:5918bb365c72

patch.pathtransform: add a prefix parameter This is preparation for upcoming patches that will add support for applying a patch within a subdirectory. The prefix is applied after path components are stripped.
author Siddharth Agarwal <sid0@fb.com>
date Fri, 06 Mar 2015 22:17:24 -0800
parents daee2039dd11
children 740a17f885a1
files mercurial/patch.py
diffstat 1 files changed, 15 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/patch.py	Fri Mar 06 21:48:40 2015 -0800
+++ b/mercurial/patch.py	Fri Mar 06 22:17:24 2015 -0800
@@ -1087,20 +1087,22 @@
             return s
     return s[:i]
 
-def pathtransform(path, strip):
+def pathtransform(path, strip, prefix):
     '''turn a path from a patch into a path suitable for the repository
 
+    prefix, if not empty, is expected to be normalized with a / at the end.
+
     Returns (stripped components, path in repository).
 
-    >>> pathtransform('a/b/c', 0)
+    >>> pathtransform('a/b/c', 0, '')
     ('', 'a/b/c')
-    >>> pathtransform('   a/b/c   ', 0)
+    >>> pathtransform('   a/b/c   ', 0, '')
     ('', '   a/b/c')
-    >>> pathtransform('   a/b/c   ', 2)
+    >>> pathtransform('   a/b/c   ', 2, '')
     ('a/b/', 'c')
-    >>> pathtransform('   a//b/c   ', 2)
-    ('a//b/', 'c')
-    >>> pathtransform('a/b/c', 3)
+    >>> pathtransform('   a//b/c   ', 2, 'd/e/')
+    ('a//b/', 'd/e/c')
+    >>> pathtransform('a/b/c', 3, '')
     Traceback (most recent call last):
     PatchError: unable to strip away 1 of 3 dirs from a/b/c
     '''
@@ -1119,16 +1121,16 @@
         while i < pathlen - 1 and path[i] == '/':
             i += 1
         count -= 1
-    return path[:i].lstrip(), path[i:].rstrip()
+    return path[:i].lstrip(), prefix + path[i:].rstrip()
 
 def makepatchmeta(backend, afile_orig, bfile_orig, hunk, strip):
     nulla = afile_orig == "/dev/null"
     nullb = bfile_orig == "/dev/null"
     create = nulla and hunk.starta == 0 and hunk.lena == 0
     remove = nullb and hunk.startb == 0 and hunk.lenb == 0
-    abase, afile = pathtransform(afile_orig, strip)
+    abase, afile = pathtransform(afile_orig, strip, '')
     gooda = not nulla and backend.exists(afile)
-    bbase, bfile = pathtransform(bfile_orig, strip)
+    bbase, bfile = pathtransform(bfile_orig, strip, '')
     if afile == bfile:
         goodb = gooda
     else:
@@ -1368,7 +1370,7 @@
                eolmode='strict'):
 
     def pstrip(p):
-        return pathtransform(p, strip - 1)[1]
+        return pathtransform(p, strip - 1, '')[1]
 
     rejects = 0
     err = 0
@@ -1557,9 +1559,9 @@
             if state == 'file':
                 afile, bfile, first_hunk, gp = values
                 if gp:
-                    gp.path = pathtransform(gp.path, strip - 1)[1]
+                    gp.path = pathtransform(gp.path, strip - 1, '')[1]
                     if gp.oldpath:
-                        gp.oldpath = pathtransform(gp.oldpath, strip - 1)[1]
+                        gp.oldpath = pathtransform(gp.oldpath, strip - 1, '')[1]
                 else:
                     gp = makepatchmeta(backend, afile, bfile, first_hunk, strip)
                 changed.add(gp.path)