diff mercurial/commands.py @ 2859:b3d1145ed06c

Teach import to understand git diff extensions. Vanilla patch chokes on git patches that include files that are copied or renamed, then modified. So this code detects that case and rewrites the patch if necessary.
author Brendan Cully <brendan@kublai.com>
date Fri, 11 Aug 2006 15:50:07 -0700
parents 7706fa503677
children 0f08f2c042ec
line wrap: on
line diff
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -1825,6 +1825,7 @@ def import_(ui, repo, patch1, *patches, 
     wlock = repo.wlock()
     lock = repo.lock()
 
+    wlock = repo.wlock()
     for patch in patches:
         pf = os.path.join(d, patch)
 
@@ -1908,12 +1909,44 @@ def import_(ui, repo, patch1, *patches, 
                 raise util.Abort(_('no diffs found'))
 
             files = util.patch(strip, tmpname, ui, cwd=repo.root)
+            removes = []
             if len(files) > 0:
-                cfiles = files
+                cfiles = files.keys()
+                copies = []
+                copts = {'after': False, 'force': False}
                 cwd = repo.getcwd()
                 if cwd:
-                    cfiles = [util.pathto(cwd, f) for f in files]
+                    cfiles = [util.pathto(cwd, f) for f in files.keys()]
+                for f in files:
+                    ctype, gp = files[f]
+                    if ctype == 'RENAME':
+                        copies.append((gp.oldpath, gp.path, gp.copymod))
+                        removes.append(gp.oldpath)
+                    elif ctype == 'COPY':
+                        copies.append((gp.oldpath, gp.path, gp.copymod))
+                    elif ctype == 'DELETE':
+                        removes.append(gp.path)
+                for src, dst, after in copies:
+                    absdst = os.path.join(repo.root, dst)
+                    if not after and os.path.exists(absdst):
+                        raise util.Abort(_('patch creates existing file %s') % dst)
+                    if cwd:
+                        src, dst = [util.pathto(cwd, f) for f in (src, dst)]
+                    copts['after'] = after
+                    errs, copied = docopy(ui, repo, (src, dst), copts, wlock=wlock)
+                    if errs:
+                        raise util.Abort(errs)
+                if removes:
+                    repo.remove(removes, True, wlock=wlock)
+                for f in files:
+                    ctype, gp = files[f]
+                    if gp and gp.mode:
+                        x = gp.mode & 0100 != 0
+                        dst = os.path.join(repo.root, gp.path)
+                        util.set_exec(dst, x)
                 addremove_lock(ui, repo, cfiles, {}, wlock=wlock)
+            files = files.keys()
+            files.extend([r for r in removes if r not in files])
             repo.commit(files, message, user, date, wlock=wlock, lock=lock)
         finally:
             os.unlink(tmpname)