import: make patch apply if run in subdir
authorVadim Gelfer <vadim.gelfer@gmail.com>
Tue, 01 Aug 2006 15:51:13 -0700
changeset 2760 e6bef16b6cec
parent 2759 19436facb073
child 2761 0aa458261901
import: make patch apply if run in subdir fix is same as for mq patch. patch apply code should be merged.
mercurial/commands.py
mercurial/util.py
--- a/mercurial/commands.py	Wed Aug 02 00:20:28 2006 +0200
+++ b/mercurial/commands.py	Tue Aug 01 15:51:13 2006 -0700
@@ -1832,9 +1832,13 @@
             if not diffs_seen:
                 raise util.Abort(_('no diffs found'))
 
-            files = util.patch(strip, tmpname, ui)
+            files = util.patch(strip, tmpname, ui, cwd=repo.root)
             if len(files) > 0:
-                addremove_lock(ui, repo, files, {})
+                cfiles = files
+                cwd = repo.getcwd()
+                if cwd:
+                    cfiles = [util.pathto(cwd, f) for f in files]
+                addremove_lock(ui, repo, cfiles, {})
             repo.commit(files, message, user, date)
         finally:
             os.unlink(tmpname)
--- a/mercurial/util.py	Wed Aug 02 00:20:28 2006 +0200
+++ b/mercurial/util.py	Tue Aug 01 15:51:13 2006 -0700
@@ -93,11 +93,15 @@
             return p_name
     return default
 
-def patch(strip, patchname, ui):
+def patch(strip, patchname, ui, cwd=None):
     """apply the patch <patchname> to the working directory.
     a list of patched files is returned"""
     patcher = find_in_path('gpatch', os.environ.get('PATH', ''), 'patch')
-    fp = os.popen('%s -p%d < "%s"' % (patcher, strip, patchname))
+    args = []
+    if cwd:
+        args.append('-d "%s"' % cwd)
+    fp = os.popen('%s %s -p%d < "%s"' % (patcher, ' '.join(args), strip,
+                                         patchname))
     files = {}
     for line in fp:
         line = line.rstrip()