[PATCH 6 of 7] patch: strip paths in leaked git patchmeta objects

Mads Kiilerich mads at kiilerich.com
Mon Apr 26 07:37:57 CDT 2010


# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1272280863 -7200
# Node ID fcb1bf03d7a33af55bf57f8c64ee8bc25e3360d1
# Parent  9531f5c39c0732ac451c9c191340b99455569401
patch: strip paths in leaked git patchmeta objects

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -904,25 +904,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)
@@ -1167,6 +1167,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


More information about the Mercurial-devel mailing list