changeset 1285:1546c2aa6b30

Make 'hg import' platform independent. - moved popen("patch ...") from commands.py to util.py - files may not be single quoted in popen under windows: fixed - patch returns the files quoted under windows. quotes need to be stripped off: fixed
author Volker Kleinfeld <Volker.Kleinfeld@gmx.de>
date Tue, 20 Sep 2005 21:35:51 -0700
parents 59d07a6bd513
children 4f08da74cae8 40ccb87dac71 2073e5a71008
files mercurial/commands.py mercurial/util.py
diffstat 2 files changed, 28 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Tue Sep 20 21:29:07 2005 -0700
+++ b/mercurial/commands.py	Tue Sep 20 21:35:51 2005 -0700
@@ -1173,18 +1173,7 @@
             message = "%s\n" % '\n'.join(message)
         ui.debug('message:\n%s\n' % message)
 
-        f = os.popen("patch -p%d < '%s'" % (strip, pf))
-        files = []
-        for l in f.read().splitlines():
-            l.rstrip('\r\n');
-            ui.status("%s\n" % l)
-            if l.startswith('patching file '):
-                pf = l[14:]
-                if pf not in files:
-                    files.append(pf)
-        patcherr = f.close()
-        if patcherr:
-            raise util.Abort("patch failed")
+        files = util.patch(strip, pf, ui)
 
         if len(files) > 0:
             addremove(ui, repo, *files)
--- a/mercurial/util.py	Tue Sep 20 21:29:07 2005 -0700
+++ b/mercurial/util.py	Tue Sep 20 21:35:51 2005 -0700
@@ -30,6 +30,22 @@
     w.join()
     return f
 
+def patch(strip, patchname, ui):
+    """apply the patch <patchname> to the working directory.
+    a list of patched files is returned"""
+    fp = os.popen('patch -p%d < "%s"' % (strip, patchname))
+    files = {}
+    for line in fp:
+        line = line.rstrip()
+        ui.status("%s\n" % line)
+        if line.startswith('patching file '):
+            pf = parse_patch_output(line)
+            files.setdefault(pf, 1)
+    code = fp.close()
+    if code:
+        raise Abort("patch command failed: exit status %s " % code)
+    return files.keys()
+    
 def binary(s):
     """return true if a string is binary data using diff's heuristic"""
     if s and '\0' in s[:4096]:
@@ -315,6 +331,13 @@
 if os.name == 'nt':
     nulldev = 'NUL:'
 
+    def parse_patch_output(output_line):
+        """parses the output produced by patch and returns the file name"""
+        pf = output_line[14:]
+        if pf[0] == '`':
+            pf = pf[1:-1] # Remove the quotes
+        return pf
+
     try: # ActivePython can create hard links using win32file module
         import win32file
 
@@ -360,6 +383,10 @@
 else:
     nulldev = '/dev/null'
 
+    def parse_patch_output(output_line):
+        """parses the output produced by patch and returns the file name"""
+        return output_line[14:]
+
     def is_exec(f, last):
         """check whether a file is executable"""
         return (os.stat(f).st_mode & 0100 != 0)