[PATCH] When applying a git diff, ensure that the target dir exists for new files

stefan at rusek.org stefan at rusek.org
Thu Dec 11 07:22:44 CST 2008


# HG changeset patch
# User Stefan Rusek <stefan at rusek.org>
# Date 1228829267 -3600
# Node ID d9a8284b26a779f210f13ca49d09d72fac9d0b52
# Parent  518afef5e35007a050f6b0d9e89d99afd97740e8
When applying a git diff, ensure that the target dir exists for new files

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -22,17 +22,20 @@
 
 # helper functions
 
-def copyfile(src, dst, basedir=None):
-    if not basedir:
-        basedir = os.getcwd()
-
-    abssrc, absdst = [os.path.join(basedir, n) for n in (src, dst)]
+def copyfile(src, dst, basedir):
+    abssrc, absdst = [util.canonpath(basedir, basedir, x) for x in [src, dst]]
     if os.path.exists(absdst):
         raise util.Abort(_("cannot create %s: destination already exists") %
                          dst)
 
-    if not os.path.isdir(basedir):
-        os.makedirs(basedir)
+    dstdir = os.path.dirname(absdst)
+    if dstdir and not os.path.isdir(dstdir):
+        try:
+            os.makedirs(dstdir)
+        except:
+            raise util.Abort(
+                _("cannot create %s: unable to create destination directory")
+                % dst)            
 
     util.copyfile(abssrc, absdst)
 
@@ -977,9 +980,7 @@
             cwd = os.getcwd()
             for gp in gitpatches:
                 if gp.op in ('COPY', 'RENAME'):
-                    src, dst = [util.canonpath(cwd, cwd, x)
-                                for x in [gp.oldpath, gp.path]]
-                    copyfile(src, dst)
+                    copyfile(gp.oldpath, gp.path, cwd)
                 changed[gp.path] = gp
         else:
             raise util.Abort(_('unsupported parser state: %s') % state)


More information about the Mercurial-devel mailing list