patch: strip paths in leaked git patchmeta objects
authorMads Kiilerich <mads@kiilerich.com>
Mon, 26 Apr 2010 13:21:03 +0200
changeset 11022 0429d0d49f92
parent 11021 c47a1cfad572
child 11032 27f98676f11b
child 11036 4efdccaca21d
patch: strip paths in leaked git patchmeta objects
mercurial/patch.py
--- a/mercurial/patch.py	Mon Apr 26 13:21:03 2010 +0200
+++ b/mercurial/patch.py	Mon Apr 26 13:21:03 2010 +0200
@@ -907,25 +907,25 @@
             return s
     return s[:i]
 
+def pathstrip(path, strip):
+    pathlen = len(path)
+    i = 0
+    if strip == 0:
+        return '', path.rstrip()
+    count = strip
+    while count > 0:
+        i = path.find('/', i)
+        if i == -1:
+            raise PatchError(_("unable to strip away %d of %d dirs from %s") %
+                             (count, strip, path))
+        i += 1
+        # consume '//' in the path
+        while i < pathlen - 1 and path[i] == '/':
+            i += 1
+        count -= 1
+    return path[:i].lstrip(), path[i:].rstrip()
+
 def selectfile(afile_orig, bfile_orig, hunk, strip):
-    def pathstrip(path, strip):
-        pathlen = len(path)
-        i = 0
-        if strip == 0:
-            return '', path.rstrip()
-        count = strip
-        while count > 0:
-            i = path.find('/', i)
-            if i == -1:
-                raise PatchError(_("unable to strip away %d of %d dirs from %s") %
-                                 (count, strip, path))
-            i += 1
-            # consume '//' in the path
-            while i < pathlen - 1 and path[i] == '/':
-                i += 1
-            count -= 1
-        return path[:i].lstrip(), path[i:].rstrip()
-
     nulla = afile_orig == "/dev/null"
     nullb = bfile_orig == "/dev/null"
     abase, afile = pathstrip(afile_orig, strip)
@@ -1190,6 +1190,9 @@
                 continue
         elif state == 'git':
             for gp in values:
+                gp.path = pathstrip(gp.path, strip - 1)[1]
+                if gp.oldpath:
+                    gp.oldpath = pathstrip(gp.oldpath, strip - 1)[1]
                 if gp.op in ('COPY', 'RENAME'):
                     copyfn(gp.oldpath, gp.path, cwd)
                 changed[gp.path] = gp