changeset 11022:0429d0d49f92

patch: strip paths in leaked git patchmeta objects
author Mads Kiilerich <mads@kiilerich.com>
date Mon, 26 Apr 2010 13:21:03 +0200
parents c47a1cfad572
children 27f98676f11b 4efdccaca21d
files mercurial/patch.py
diffstat 1 files changed, 21 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- 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