changeset 1948:ebe273a16048

merge with crew/templates
author Benoit Boissinot <benoit.boissinot@ens-lyon.org>
date Mon, 13 Mar 2006 23:38:14 +0100
parents 9fee186f7f0d (diff) 65aff2ed61ae (current diff)
children d2c2e77826c0
files mercurial/commands.py
diffstat 4 files changed, 310 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mercurial/bundlerepo.py	Mon Mar 13 23:38:14 2006 +0100
@@ -0,0 +1,208 @@
+"""
+bundlerepo.py - repository class for viewing uncompressed bundles
+
+This provides a read-only repository interface to bundles as if
+they were part of the actual repository.
+
+Copyright 2006 Benoit Boissinot <benoit.boissinot@ens-lyon.org>
+
+This software may be used and distributed according to the terms
+of the GNU General Public License, incorporated herein by reference.
+"""
+
+from node import *
+from i18n import gettext as _
+from demandload import demandload
+demandload(globals(), "util os struct")
+
+import localrepo, changelog, manifest, filelog, revlog
+
+def getchunk(source):
+    """get a chunk from a group"""
+    d = source.read(4)
+    if not d:
+        return ""
+    l = struct.unpack(">l", d)[0]
+    if l <= 4:
+        return ""
+    d = source.read(l - 4)
+    if len(d) < l - 4:
+        raise util.Abort(_("premature EOF reading chunk"
+                           " (got %d bytes, expected %d)")
+                          % (len(d), l - 4))
+    return d
+
+class bundlerevlog(revlog.revlog):
+    def __init__(self, opener, indexfile, datafile, bundlefile,
+                 linkmapper=None):
+        # How it works:
+        # to retrieve a revision, we need to know the offset of
+        # the revision in the bundlefile (an opened file).
+        #
+        # We store this offset in the index (start), to differentiate a
+        # rev in the bundle and from a rev in the revlog, we check
+        # len(index[r]). If the tuple is bigger than 7, it is a bundle
+        # (it is bigger since we store the node to which the delta is)
+        #
+        revlog.revlog.__init__(self, opener, indexfile, datafile)
+        self.bundlefile = bundlefile
+        def genchunk():
+            while 1:
+                pos = bundlefile.tell()
+                chunk = getchunk(bundlefile)
+                if not chunk:
+                    break
+                yield chunk, pos + 4 # XXX struct.calcsize(">l") == 4
+        n = self.count()
+        prev = None
+        for chunk, start in genchunk():
+            size = len(chunk)
+            if size < 80:
+                raise util.Abort("invalid changegroup")
+            start += 80
+            size -= 80
+            node, p1, p2, cs = struct.unpack("20s20s20s20s", chunk[:80])
+            if node in self.nodemap:
+                prev = node
+                continue
+            for p in (p1, p2):
+                if not p in self.nodemap:
+                    raise RevlogError(_("unknown parent %s") % short(p1))
+            if linkmapper is None:
+                link = n
+            else:
+                link = linkmapper(cs)
+
+            if not prev:
+                prev = p1
+            # start, size, base is not used, link, p1, p2, delta ref
+            e = (start, size, None, link, p1, p2, node, prev)
+            self.index.append(e)
+            self.nodemap[node] = n
+            prev = node
+            n += 1
+
+    def bundle(self, rev):
+        """is rev from the bundle"""
+        if rev < 0:
+            return False
+        return len(self.index[rev]) > 7
+    def bundlebase(self, rev): return self.index[rev][7]
+    def chunk(self, rev):
+        # Warning: in case of bundle, the diff is against bundlebase,
+        # not against rev - 1
+        # XXX: could use some caching
+        if not self.bundle(rev):
+            return revlog.revlog.chunk(self, rev)
+        self.bundlefile.seek(self.start(rev))
+        return self.bundlefile.read(self.length(rev))
+
+    def revdiff(self, rev1, rev2):
+        """return or calculate a delta between two revisions"""
+        if self.bundle(rev1) and self.bundle(rev2):
+            # hot path for bundle
+            revb = self.rev(self.bundlebase(rev2))
+            if revb == rev1:
+                return self.chunk(rev2)
+        elif not self.bundle(rev1) and not self.bundle(rev2):
+            return revlog.revlog.chunk(self, rev1, rev2)
+
+        return self.diff(self.revision(self.node(rev1)),
+                         self.revision(self.node(rev2)))
+
+    def revision(self, node):
+        """return an uncompressed revision of a given"""
+        if node == nullid: return ""
+
+        text = None
+        chain = []
+        iter_node = node
+        rev = self.rev(iter_node)
+        # reconstruct the revision if it is from a changegroup
+        while self.bundle(rev):
+            if self.cache and self.cache[0] == iter_node:
+                text = self.cache[2]
+                break
+            chain.append(rev)
+            iter_node = self.bundlebase(rev)
+            rev = self.rev(iter_node)
+        if text is None:
+            text = revlog.revlog.revision(self, iter_node)
+
+        while chain:
+            delta = self.chunk(chain.pop())
+            text = self.patches(text, [delta])
+
+        p1, p2 = self.parents(node)
+        if node != revlog.hash(text, p1, p2):
+            raise RevlogError(_("integrity check failed on %s:%d")
+                          % (self.datafile, self.rev(node)))
+
+        self.cache = (node, rev, text)
+        return text
+
+    def addrevision(self, text, transaction, link, p1=None, p2=None, d=None):
+        raise NotImplementedError
+    def addgroup(self, revs, linkmapper, transaction, unique=0):
+        raise NotImplementedError
+    def strip(self, rev, minlink):
+        raise NotImplementedError
+    def checksize(self):
+        raise NotImplementedError
+
+class bundlechangelog(bundlerevlog, changelog.changelog):
+    def __init__(self, opener, bundlefile):
+        changelog.changelog.__init__(self, opener)
+        bundlerevlog.__init__(self, opener, "00changelog.i", "00changelog.d",
+                              bundlefile)
+
+class bundlemanifest(bundlerevlog, manifest.manifest):
+    def __init__(self, opener, bundlefile, linkmapper):
+        manifest.manifest.__init__(self, opener)
+        bundlerevlog.__init__(self, opener, self.indexfile, self.datafile,
+                              bundlefile, linkmapper)
+
+class bundlefilelog(bundlerevlog, filelog.filelog):
+    def __init__(self, opener, path, bundlefile, linkmapper):
+        filelog.filelog.__init__(self, opener, path)
+        bundlerevlog.__init__(self, opener, self.indexfile, self.datafile,
+                              bundlefile, linkmapper)
+
+class bundlerepository(localrepo.localrepository):
+    def __init__(self, ui, path, bundlename):
+        localrepo.localrepository.__init__(self, ui, path)
+        f = open(bundlename, "rb")
+        s = os.fstat(f.fileno())
+        self.bundlefile = f
+        header = self.bundlefile.read(4)
+        if header == "HG10":
+            raise util.Abort(_("%s: compressed bundle not supported")
+                             % bundlename)
+        elif header != "HG11":
+            raise util.Abort(_("%s: not a Mercurial bundle file") % bundlename)
+        self.changelog = bundlechangelog(self.opener, self.bundlefile)
+        self.manifest = bundlemanifest(self.opener, self.bundlefile,
+                                       self.changelog.rev)
+        # dict with the mapping 'filename' -> position in the bundle
+        self.bundlefilespos = {}
+        while 1:
+                f = getchunk(self.bundlefile)
+                if not f:
+                    break
+                self.bundlefilespos[f] = self.bundlefile.tell()
+                while getchunk(self.bundlefile):
+                    pass
+
+    def dev(self):
+        return -1
+
+    def file(self, f):
+        if f[0] == '/':
+            f = f[1:]
+        if f in self.bundlefilespos:
+            self.bundlefile.seek(self.bundlefilespos[f])
+            return bundlefilelog(self.opener, f, self.bundlefile,
+                                 self.changelog.rev)
+        else:
+            return filelog.filelog(self.opener, f)
+
--- a/mercurial/commands.py	Mon Mar 13 23:32:57 2006 +0100
+++ b/mercurial/commands.py	Mon Mar 13 23:38:14 2006 +0100
@@ -9,9 +9,9 @@
 from node import *
 from i18n import gettext as _
 demandload(globals(), "os re sys signal shutil imp urllib pdb")
-demandload(globals(), "fancyopts ui hg util lock revlog templater")
-demandload(globals(), "fnmatch hgweb mdiff random signal time traceback")
-demandload(globals(), "errno socket version struct atexit sets bz2")
+demandload(globals(), "fancyopts ui hg util lock revlog templater bundlerepo")
+demandload(globals(), "fnmatch hgweb mdiff random signal tempfile time")
+demandload(globals(), "traceback errno socket version struct atexit sets bz2")
 
 class UnknownCommand(Exception):
     """Exception raised if command is not in the command table."""
@@ -274,6 +274,32 @@
                               pathname),
                 mode)
 
+def write_bundle(cg, filename, compress=True, fh=None):
+    if fh is None:
+        fh = open(filename, "wb")
+
+    class nocompress(object):
+        def compress(self, x):
+            return x
+        def flush(self):
+            return ""
+    try:
+        if compress:
+            fh.write("HG10")
+            z = bz2.BZ2Compressor(9)
+        else:
+            fh.write("HG11")
+            z = nocompress()
+        while 1:
+            chunk = cg.read(4096)
+            if not chunk:
+                break
+            fh.write(z.compress(chunk))
+        fh.write(z.flush())
+    except:
+        os.unlink(filename)
+        raise
+
 def dodiff(fp, ui, repo, node1, node2, files=None, match=util.always,
            changes=None, text=False, opts={}):
     if not node1:
@@ -834,24 +860,11 @@
     Unlike import/export, this exactly preserves all changeset
     contents including permissions, rename data, and revision history.
     """
-    f = open(fname, "wb")
     dest = ui.expandpath(dest)
     other = hg.repository(ui, dest)
     o = repo.findoutgoing(other)
     cg = repo.changegroup(o, 'bundle')
-
-    try:
-        f.write("HG10")
-        z = bz2.BZ2Compressor(9)
-        while 1:
-            chunk = cg.read(4096)
-            if not chunk:
-                break
-            f.write(z.compress(chunk))
-        f.write(z.flush())
-    except:
-        os.unlink(fname)
-        raise
+    write_bundle(cg, fname)
 
 def cat(ui, repo, file1, *pats, **opts):
     """output the latest or given revisions of files
@@ -1748,16 +1761,36 @@
     pull repo. These are the changesets that would be pulled if a pull
     was requested.
 
-    Currently only local repositories are supported.
+    For remote repository, using --bundle avoids downloading the changesets
+    twice if the incoming is followed by a pull.
     """
     source = ui.expandpath(source)
     other = hg.repository(ui, source)
-    if not other.local():
-        raise util.Abort(_("incoming doesn't work for remote repositories yet"))
-    o = repo.findincoming(other)
-    if not o:
+    incoming = repo.findincoming(other)
+    if not incoming:
         return
-    o = other.changelog.nodesbetween(o)[0]
+
+    cleanup = None
+    if not other.local() or opts["bundle"]:
+        # create an uncompressed bundle
+        if not opts["bundle"]:
+            # create a temporary bundle
+            fd, fname = tempfile.mkstemp(suffix=".hg",
+                                         prefix="tmp-hg-incoming")
+            f = os.fdopen(fd, "wb")
+            cleanup = fname
+        else:
+            fname = opts["bundle"]
+            f = open(fname, "wb")
+
+        cg = other.changegroup(incoming, "incoming")
+        write_bundle(cg, fname, compress=other.local(), fh=f)
+        f.close()
+        if not other.local():
+            # use a bundlerepo
+            other = bundlerepo.bundlerepository(ui, repo.root, fname)
+
+    o = other.changelog.nodesbetween(incoming)[0]
     if opts['newest_first']:
         o.reverse()
     displayer = show_changeset(ui, other, opts)
@@ -1771,6 +1804,9 @@
             dodiff(ui, ui, other, prev, n)
             ui.write("\n")
 
+    if cleanup:
+        os.unlink(cleanup)
+
 def init(ui, dest="."):
     """create a new repository in the given directory
 
@@ -2496,16 +2532,20 @@
     """
     f = urllib.urlopen(fname)
 
-    if f.read(4) != "HG10":
+    header = f.read(4)
+    if header == "HG10":
+        def generator(f):
+            zd = bz2.BZ2Decompressor()
+            for chunk in f:
+                yield zd.decompress(chunk)
+    elif header == "HG11":
+        def generator(f):
+            for chunk in f:
+                yield chunk
+    else:
         raise util.Abort(_("%s: not a Mercurial bundle file") % fname)
-
-    def bzgenerator(f):
-        zd = bz2.BZ2Decompressor()
-        for chunk in f:
-            yield zd.decompress(chunk)
-
-    bzgen = bzgenerator(util.filechunkiter(f, 4096))
-    if repo.addchangegroup(util.chunkbuffer(bzgen)):
+    gen = generator(util.filechunkiter(f, 4096))
+    if repo.addchangegroup(util.chunkbuffer(gen)):
         return 1
 
     if opts['update']:
@@ -2719,9 +2759,10 @@
          [('M', 'no-merges', None, _('do not show merges')),
           ('', 'style', '', _('display using template map file')),
           ('n', 'newest-first', None, _('show newest record first')),
+          ('', 'bundle', '', _('file to store the bundles into')),
           ('p', 'patch', None, _('show patch')),
           ('', 'template', '', _('display with template'))],
-         _('hg incoming [-p] [-n] [-M] [SOURCE]')),
+         _('hg incoming [-p] [-n] [-M] [--bundle FILENAME] [SOURCE]')),
     "^init": (init, [], _('hg init [DEST]')),
     "locate":
         (locate,
--- a/mercurial/hg.py	Mon Mar 13 23:32:57 2006 +0100
+++ b/mercurial/hg.py	Mon Mar 13 23:38:14 2006 +0100
@@ -8,7 +8,7 @@
 from node import *
 from repo import *
 from demandload import *
-demandload(globals(), "localrepo httprepo sshrepo statichttprepo")
+demandload(globals(), "localrepo bundlerepo httprepo sshrepo statichttprepo")
 
 def repository(ui, path=None, create=0):
     if path:
@@ -24,5 +24,13 @@
                 ui, path.replace("old-http://", "http://"))
         if path.startswith("ssh://"):
             return sshrepo.sshrepository(ui, path)
+        if path.startswith("bundle://"):
+            path = path[9:]
+            s = path.split("+", 1)
+            if  len(s) == 1:
+                repopath, bundlename = "", s[0]
+            else:
+                repopath, bundlename = s
+            return bundlerepo.bundlerepository(ui, repopath, bundlename)
 
     return localrepo.localrepository(ui, path, create)
--- a/mercurial/revlog.py	Mon Mar 13 23:32:57 2006 +0100
+++ b/mercurial/revlog.py	Mon Mar 13 23:38:14 2006 +0100
@@ -254,10 +254,14 @@
         if node == nullid: return (nullid, nullid)
         return self.index[self.rev(node)][4:6]
 
-    def start(self, rev): return self.index[rev][0]
-    def length(self, rev): return self.index[rev][1]
+    def start(self, rev): return (rev < 0) and -1 or self.index[rev][0]
+    def length(self, rev):
+        if rev < 0:
+            return 0
+        else:
+            return self.index[rev][1]
     def end(self, rev): return self.start(rev) + self.length(rev)
-    def base(self, rev): return self.index[rev][2]
+    def base(self, rev): return (rev < 0) and rev or self.index[rev][2]
 
     def reachable(self, rev, stop=None):
         reachable = {}
@@ -528,12 +532,17 @@
     def delta(self, node):
         """return or calculate a delta between a node and its predecessor"""
         r = self.rev(node)
-        b = self.base(r)
-        if r == b:
-            return self.diff(self.revision(self.node(r - 1)),
-                             self.revision(node))
+        return self.revdiff(r - 1, r)
+
+    def revdiff(self, rev1, rev2):
+        """return or calculate a delta between two revisions"""
+        b1 = self.base(rev1)
+        b2 = self.base(rev2)
+        if b1 == b2 and rev1 + 1 == rev2:
+            return self.chunk(rev2)
         else:
-            return self.chunk(r)
+            return self.diff(self.revision(self.node(rev1)),
+                             self.revision(self.node(rev2)))
 
     def revision(self, node):
         """return an uncompressed revision of a given"""
@@ -556,7 +565,7 @@
         for r in xrange(base + 1, rev + 1):
             bins.append(self.chunk(r))
 
-        text = mdiff.patches(text, bins)
+        text = self.patches(text, bins)
 
         p1, p2 = self.parents(node)
         if node != hash(text, p1, p2):
@@ -709,20 +718,12 @@
         # build deltas
         for d in xrange(0, len(revs) - 1):
             a, b = revs[d], revs[d + 1]
-            na = self.node(a)
             nb = self.node(b)
 
             if infocollect is not None:
                 infocollect(nb)
 
-            # do we need to construct a new delta?
-            if a + 1 != b or self.base(b) == b:
-                ta = self.revision(na)
-                tb = self.revision(nb)
-                d = self.diff(ta, tb)
-            else:
-                d = self.chunk(b)
-
+            d = self.revdiff(a, b)
             p = self.parents(nb)
             meta = nb + p[0] + p[1] + lookup(nb)
             l = struct.pack(">l", len(meta) + len(d) + 4)