mercurial/commands.py
changeset 3666 025f68f22ae2
parent 3665 d12c8668b102
child 3667 8500a13ec44b
equal deleted inserted replaced
3665:d12c8668b102 3666:025f68f22ae2
     8 from demandload import demandload
     8 from demandload import demandload
     9 from node import *
     9 from node import *
    10 from i18n import gettext as _
    10 from i18n import gettext as _
    11 demandload(globals(), "os re sys signal imp urllib pdb shlex")
    11 demandload(globals(), "os re sys signal imp urllib pdb shlex")
    12 demandload(globals(), "fancyopts ui hg util lock revlog bundlerepo")
    12 demandload(globals(), "fancyopts ui hg util lock revlog bundlerepo")
    13 demandload(globals(), "difflib patch tempfile time")
    13 demandload(globals(), "difflib patch time")
    14 demandload(globals(), "traceback errno version atexit bz2")
    14 demandload(globals(), "traceback errno version atexit bz2")
    15 demandload(globals(), "archival changegroup cmdutil hgweb.server sshserver")
    15 demandload(globals(), "archival changegroup cmdutil hgweb.server sshserver")
    16 
    16 
    17 class UnknownCommand(Exception):
    17 class UnknownCommand(Exception):
    18     """Exception raised if command is not in the command table."""
    18     """Exception raised if command is not in the command table."""
    40                 message = open(logfile).read()
    40                 message = open(logfile).read()
    41         except IOError, inst:
    41         except IOError, inst:
    42             raise util.Abort(_("can't read commit message '%s': %s") %
    42             raise util.Abort(_("can't read commit message '%s': %s") %
    43                              (logfile, inst.strerror))
    43                              (logfile, inst.strerror))
    44     return message
    44     return message
    45 
       
    46 def write_bundle(cg, filename=None, compress=True):
       
    47     """Write a bundle file and return its filename.
       
    48 
       
    49     Existing files will not be overwritten.
       
    50     If no filename is specified, a temporary file is created.
       
    51     bz2 compression can be turned off.
       
    52     The bundle file will be deleted in case of errors.
       
    53     """
       
    54     class nocompress(object):
       
    55         def compress(self, x):
       
    56             return x
       
    57         def flush(self):
       
    58             return ""
       
    59 
       
    60     fh = None
       
    61     cleanup = None
       
    62     try:
       
    63         if filename:
       
    64             if os.path.exists(filename):
       
    65                 raise util.Abort(_("file '%s' already exists") % filename)
       
    66             fh = open(filename, "wb")
       
    67         else:
       
    68             fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
       
    69             fh = os.fdopen(fd, "wb")
       
    70         cleanup = filename
       
    71 
       
    72         if compress:
       
    73             fh.write("HG10")
       
    74             z = bz2.BZ2Compressor(9)
       
    75         else:
       
    76             fh.write("HG10UN")
       
    77             z = nocompress()
       
    78         # parse the changegroup data, otherwise we will block
       
    79         # in case of sshrepo because we don't know the end of the stream
       
    80 
       
    81         # an empty chunkiter is the end of the changegroup
       
    82         empty = False
       
    83         while not empty:
       
    84             empty = True
       
    85             for chunk in changegroup.chunkiter(cg):
       
    86                 empty = False
       
    87                 fh.write(z.compress(changegroup.genchunk(chunk)))
       
    88             fh.write(z.compress(changegroup.closechunk()))
       
    89         fh.write(z.flush())
       
    90         cleanup = None
       
    91         return filename
       
    92     finally:
       
    93         if fh is not None:
       
    94             fh.close()
       
    95         if cleanup is not None:
       
    96             os.unlink(cleanup)
       
    97 
    45 
    98 def setremoteconfig(ui, opts):
    46 def setremoteconfig(ui, opts):
    99     "copy remote options to ui tree"
    47     "copy remote options to ui tree"
   100     if opts.get('ssh'):
    48     if opts.get('ssh'):
   101         ui.setconfig("ui", "ssh", opts['ssh'])
    49         ui.setconfig("ui", "ssh", opts['ssh'])
   382 
   330 
   383     if revs:
   331     if revs:
   384         cg = repo.changegroupsubset(o, revs, 'bundle')
   332         cg = repo.changegroupsubset(o, revs, 'bundle')
   385     else:
   333     else:
   386         cg = repo.changegroup(o, 'bundle')
   334         cg = repo.changegroup(o, 'bundle')
   387     write_bundle(cg, fname)
   335     changegroup.writebundle(cg, fname, False)
   388 
   336 
   389 def cat(ui, repo, file1, *pats, **opts):
   337 def cat(ui, repo, file1, *pats, **opts):
   390     """output the latest or given revisions of files
   338     """output the latest or given revisions of files
   391 
   339 
   392     Print the specified files as they were at the given revision.
   340     Print the specified files as they were at the given revision.
  1342     try:
  1290     try:
  1343         fname = opts["bundle"]
  1291         fname = opts["bundle"]
  1344         if fname or not other.local():
  1292         if fname or not other.local():
  1345             # create a bundle (uncompressed if other repo is not local)
  1293             # create a bundle (uncompressed if other repo is not local)
  1346             cg = other.changegroup(incoming, "incoming")
  1294             cg = other.changegroup(incoming, "incoming")
  1347             fname = cleanup = write_bundle(cg, fname, compress=other.local())
  1295             fname = cleanup = changegroup.writebundle(cg, fname, other.local())
  1348             # keep written bundle?
  1296             # keep written bundle?
  1349             if opts["bundle"]:
  1297             if opts["bundle"]:
  1350                 cleanup = None
  1298                 cleanup = None
  1351             if not other.local():
  1299             if not other.local():
  1352                 # use the created uncompressed bundlerepo
  1300                 # use the created uncompressed bundlerepo
  1780         files += open(opts['files']).read().splitlines()
  1728         files += open(opts['files']).read().splitlines()
  1781 
  1729 
  1782     parents = [repo.lookup(p) for p in opts['parent']]
  1730     parents = [repo.lookup(p) for p in opts['parent']]
  1783 
  1731 
  1784     try:
  1732     try:
  1785         repo.rawcommit(files, message,
  1733         repo.rawcommit(files, message, opts['user'], opts['date'], *parents)
  1786                        opts['user'], opts['date'], *parents)
       
  1787     except ValueError, inst:
  1734     except ValueError, inst:
  1788         raise util.Abort(str(inst))
  1735         raise util.Abort(str(inst))
  1789 
  1736 
  1790 def recover(ui, repo):
  1737 def recover(ui, repo):
  1791     """roll back an interrupted transaction
  1738     """roll back an interrupted transaction