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

Patrick Mézard pmezard at gmail.com
Tue Dec 9 15:39:36 CST 2008


stefan at rusek.org a écrit :
> # HG changeset patch
> # User Stefan Rusek <stefan at rusek.org>
> # Date 1228829267 -3600
> # Node ID 9f077936e48748ec33442ed9639c6ab32f374284
> # 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,15 @@
>  
>  # 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.exists(dstdir):

Any reason to prefer os.path.exists() to isdir() here ? I prefer the latter, makedirs() is likely to die sooner and for more obvious reasons.
Otherwise the patch is good for me, and I have a test ready.

> +        os.makedirs(dstdir)
>  
>      util.copyfile(abssrc, absdst)
>  

--
Patrick Mézard


More information about the Mercurial-devel mailing list