changeset 64:b3e2ddff0159

Diff in subdirectories from Jake Edge Dates in diff Fix O(n^2) behaviour of manifest diff Add a/ and b/ to work with patch -p1
author mpm@selenic.com
date Thu, 12 May 2005 17:54:55 -0800
parents 1c590d34bf61
children d40cc5aacc31
files hg mercurial/hg.py mercurial/mdiff.py mercurial/revlog.py
diffstat 4 files changed, 31 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/hg	Thu May 12 01:23:51 2005 -0800
+++ b/hg	Thu May 12 17:54:55 2005 -0800
@@ -48,20 +48,25 @@
     return l
 
 def diff(files = None, node1 = None, node2 = None):
+    def date(c):
+        return time.asctime(time.gmtime(float(c[2].split(' ')[0])))
 
     if node2:
         change = repo.changelog.read(node2)
         mmap2 = repo.manifest.read(change[0])
         (c, a, d) = repo.diffrevs(node1, node2)
         def read(f): return repo.file(f).read(mmap2[f])
+        date2 = date(change)
     else:
+        date2 = time.asctime()
         if not node1:
             node1 = repo.current
         (c, a, d) = repo.diffdir(repo.root, node1)
-        def read(f): return file(f).read()
+        def read(f): return file(os.path.join(repo.root, f)).read()
 
     change = repo.changelog.read(node1)
     mmap = repo.manifest.read(change[0])
+    date1 = date(change)
 
     if files:
         (c, a, d) = map(lambda x: filterfiles(x, files), (c, a, d))
@@ -69,16 +74,15 @@
     for f in c:
         to = repo.file(f).read(mmap[f])
         tn = read(f)
-        sys.stdout.write(mdiff.unidiff(to, tn, f))
+        sys.stdout.write(mdiff.unidiff(to, date1, tn, date2, f))
     for f in a:
         to = ""
         tn = read(f)
-        sys.stdout.write(mdiff.unidiff(to, tn, f))
+        sys.stdout.write(mdiff.unidiff(to, date1, tn, date2, f))
     for f in d:
         to = repo.file(f).read(mmap[f])
         tn = ""
-        sys.stdout.write(mdiff.unidiff(to, tn, f))
-    
+        sys.stdout.write(mdiff.unidiff(to, date1, tn, date2, f))
 
 options = {}
 opts = [('v', 'verbose', None, 'verbose'),
@@ -178,8 +182,13 @@
     if len(revs) > 2:
         print "too many revisions to diff"
         sys.exit(1)
-    else:
-        diff(args, *revs)
+
+    if os.getcwd() != repo.root:
+        relpath = os.getcwd()[len(repo.root) + 1: ]
+        if not args: args = [ relpath ]
+        else: args = [ os.path.join(relpath, x) for x in args ]
+
+    diff(args, *revs)
 
 elif cmd == "export":
     node = repo.changelog.lookup(args[0])
--- a/mercurial/hg.py	Thu May 12 01:23:51 2005 -0800
+++ b/mercurial/hg.py	Thu May 12 17:54:55 2005 -0800
@@ -499,7 +499,7 @@
             dc = dict.fromkeys(mf)
 
         def fcmp(fn):
-            t1 = file(fn).read()
+            t1 = file(os.path.join(self.root, fn)).read()
             t2 = self.file(fn).revision(mf[fn])
             return cmp(t1, t2)
 
@@ -509,7 +509,7 @@
             
             for f in files:
                 fn = os.path.join(d, f)
-                try: s = os.stat(fn)
+                try: s = os.stat(os.path.join(self.root, fn))
                 except: continue
                 if fn in dc:
                     c = dc[fn]
--- a/mercurial/mdiff.py	Thu May 12 01:23:51 2005 -0800
+++ b/mercurial/mdiff.py	Thu May 12 17:54:55 2005 -0800
@@ -2,11 +2,11 @@
 import difflib, struct
 from cStringIO import StringIO
 
-def unidiff(a, b, fn):
+def unidiff(a, ad, b, bd, fn):
     if not a and not b: return ""
     a = a.splitlines(1)
     b = b.splitlines(1)
-    l = list(difflib.unified_diff(a, b, fn, fn))
+    l = list(difflib.unified_diff(a, b, "a/" + fn, "b/" + fn, ad, bd))
     return "".join(l)
 
 def textdiff(a, b):
@@ -29,15 +29,11 @@
             la += 1
             lb += 1
 
-    si = lb
-    while lb < len(b):
-        lb += 1
-        yield "insert", la, la, si, lb
+    if lb < len(b):
+        yield "insert", la, la, lb, len(b)
 
-    si = la
-    while la < len(a):
-        la += 1
-        yield "delete", si, la, lb, lb
+    if la < len(a):
+        yield "delete", la, len(a), lb, lb
 
 def diff(a, b, sorted=0):
     bin = []
@@ -60,6 +56,7 @@
     last = pos = 0
     r = []
 
+    c = 0
     while pos < len(bin):
         p1, p2, l = struct.unpack(">lll", bin[pos:pos + 12])
         pos += 12
@@ -67,6 +64,7 @@
         r.append(bin[pos:pos + l])
         pos += l
         last = p2
+        c += 1
     r.append(a[last:])
 
     return "".join(r)
--- a/mercurial/revlog.py	Thu May 12 01:23:51 2005 -0800
+++ b/mercurial/revlog.py	Thu May 12 17:54:55 2005 -0800
@@ -114,7 +114,7 @@
             last = self.length(base)
             text = decompress(data[:last])
 
-        for r in range(base + 1, rev + 1):
+        for r in xrange(base + 1, rev + 1):
             s = self.length(r)
             b = decompress(data[last:last + s])
             text = self.patch(text, b)
@@ -138,14 +138,16 @@
         t = n - 1
 
         if n:
-            start = self.start(self.base(t))
+            base = self.base(t)
+            start = self.start(base)
             end = self.end(t)
             prev = self.revision(self.tip())
             data = compress(self.diff(prev, text))
+            dist = end - start + len(data)
 
         # full versions are inserted when the needed deltas
         # become comparable to the uncompressed text
-        if not n or (end + len(data) - start) > len(text) * 2:
+        if not n or dist > len(text) * 2:
             data = compress(text)
             base = n
         else: