mpatch with mq

Andrei Vermel avermel at mail.ru
Tue Jun 12 08:04:53 CDT 2007


> Sounds good. Did you build an exe version? What patches did you apply?

No, install it from source. Patch below and attached.

Andrei

# HG changeset patch
# User Andrei Vermel <avermel at mail.ru>
# Date 1181653353 -14400
# Node ID d7b938b5e52e04aa642f5a64fc87d7b0e54dad3d
# Parent  341deb14f76ac5f03e4f921f9f67c33f5ea8173e
Various windows related tweaks

* handle lf and crlf files by detecting the type and forcing it in the output
* add dummy --binary command line switch for compatibility with gnu patch
* make sure that mpatch doesn't import itself
* do not use os.sep when parsing diff file as it always has '/'

diff -r 341deb14f76a -r d7b938b5e52e cmd/mpatch
--- a/cmd/mpatch  Tue Jan 30 19:33:00 2007 -0500
+++ b/cmd/mpatch  Tue Jun 12 17:02:33 2007 +0400
@@ -2,7 +2,13 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License v2, incorporated herein by reference.
 
-import sys, os, bisect, re
+import sys, os
+cwd = os.getcwd()
+if cwd in sys.path and os.path.dirname(__file__) != cwd:
+  sys.path.remove(cwd)
+  sys.path.append(cwd)
+
+import bisect, re
 from optparse import OptionParser
 from mpatch import patch, ui, diffhelpers, util, version
 from mpatch.i18n import gettext as _
@@ -277,8 +283,8 @@ def rejmerge(options):
             fname = orig + ".mergebackup"
             try: os.unlink(fname)
             except: pass
-            old = file(orig)
-            new = file(fname, 'w')
+            old = file(orig, 'rb')
+            new = file(fname, 'wb')
             for x in old:
                 new.write(x)
             return fname
@@ -324,9 +330,9 @@ def rejmerge(options):
                     os.system("%s -o %s %s %s" % (prog, dest, dest, backupf))
                 else:
                     ttyin = ""
-                    if not sys.stdin.isatty():
-                        sys.stderr.write("Using /dev/tty for stdin\n")
-                        ttyin = " < /dev/tty"
+                    #if not sys.stdin.isatty():
+                    #    sys.stderr.write("I am Using /dev/tty for stdin\n")
+                    #    ttyin = " < /dev/tty"
                     os.system("%s %s %s %s" % (prog, dest, backupf, ttyin))
             if options.no_backup:
                 try: os.unlink(backupf)
@@ -356,6 +362,8 @@ parser.add_option("-N", "--no-backup", h
                   default=False, action="store_true")
 parser.add_option("", "--lsprof", help="profile with lsprof",
                   default=False, action="store_true")
+parser.add_option("", "--binary", help="avv's dummy option",
+                  default=False, action="store_true")
 parser.add_option("-p", "--strip", help="strip level", default=1)
 parser.add_option("-R", "--reverse", help="reverse the patch", default=False,
                   action="store_true")
diff -r 341deb14f76a -r d7b938b5e52e mpatch/patch.py
--- a/mpatch/patch.py  Tue Jan 30 19:33:00 2007 -0500
+++ b/mpatch/patch.py  Tue Jun 12 17:02:33 2007 +0400
@@ -134,7 +134,21 @@ unidesc = re.compile('@@ -(\d+)(,(\d+))?
 unidesc = re.compile('@@ -(\d+)(,(\d+))? \+(\d+)(,(\d+))? @@')
 contextdesc = re.compile('(---|\*\*\*) (\d+)(,(\d+))? (---|\*\*\*)')
 
+def is_crlf(fname):
+    fp = file(fname, 'rb')
+    num_cr = 0
+    num_lf = 0        
+    while 1:
+        c = fp.read(1)          # read by character
+        if not c: break
+        if c == '\r':
+            num_cr += 1
+        if c == '\n':
+            num_lf += 1
+    return num_cr > num_lf - num_cr
+
 class patchfile:
+
     def __init__(self, ui, fname):
         self.fname = fname
         self.ui = ui
@@ -142,6 +156,7 @@ class patchfile:
             fp = file(fname, 'r')
             self.lines = fp.readlines()
             self.exists = True
+            self.crlf = is_crlf(fname)
         except IOError:
             dirname = os.path.dirname(fname)
             if dirname and not os.path.isdir(dirname):
@@ -219,12 +234,15 @@ class patchfile:
         try: os.unlink(fname)
         except:
             pass
-        fp = file(fname, 'w')
+        fp = file(fname, 'wb')
         base = os.path.basename(self.fname)
         fp.write("--- %s\n+++ %s\n" % (base, base))
         for x in self.rej:
             for l in x.hunk:
-                fp.write(l)
+                if self.crlf:
+                    fp.write(l.replace('\n', '\r\n'))
+                else:
+                    fp.write(l)
                 if l[-1] != '\n':
                     fp.write("\n\ No newline at end of file\n")
 
@@ -238,10 +256,14 @@ class patchfile:
                 if st.st_nlink > 1:
                     os.unlink(dest)
             except: pass
-            fp = file(dest, 'w')
+            fp = file(dest, 'wb')
             if st:
                 os.chmod(dest, st.st_mode)
-            fp.writelines(self.lines)
+            if self.crlf:
+                for l in self.lines:
+                    fp.write(l.replace('\n', '\r\n'))
+            else:
+                fp.writelines(self.lines)
             fp.close()
 
     def close(self):
@@ -611,13 +633,13 @@ def selectfile(afile_orig, bfile_orig, h
         if count == 0:
             return path.rstrip()
         while count > 0:
-            i = path.find(os.sep, i)
+            i = path.find('/', i)
             if i == -1:
                 raise PatchError("Unable to strip away %d dirs from %s" %
                                  (count, path))
             i += 1
             # consume '//' in the path
-            while i < pathlen - 1 and path[i] == os.sep:
+            while i < pathlen - 1 and path[i] == '/':
                 i += 1
             count -= 1
         return path[i:].rstrip()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: win_tweaks.diff
Type: application/octet-stream
Size: 5544 bytes
Desc: not available
Url : http://selenic.com/pipermail/mercurial-devel/attachments/20070612/80e20dee/attachment-0001.obj 


More information about the Mercurial-devel mailing list