mercurial/commands.py
changeset 698 df78d8ccac4c
parent 697 cb1be2327220
child 699 64046575a6f7
equal deleted inserted replaced
697:cb1be2327220 698:df78d8ccac4c
     4 #
     4 #
     5 # This software may be used and distributed according to the terms
     5 # This software may be used and distributed according to the terms
     6 # of the GNU General Public License, incorporated herein by reference.
     6 # of the GNU General Public License, incorporated herein by reference.
     7 
     7 
     8 from demandload import demandload
     8 from demandload import demandload
     9 demandload(globals(), "os re sys signal")
     9 demandload(globals(), "os re sys signal shutil")
    10 demandload(globals(), "fancyopts ui hg util")
    10 demandload(globals(), "fancyopts ui hg util")
    11 demandload(globals(), "fnmatch hgweb mdiff random signal time traceback")
    11 demandload(globals(), "fnmatch hgweb mdiff random signal time traceback")
    12 demandload(globals(), "errno socket version struct")
    12 demandload(globals(), "errno socket version struct")
    13 
    13 
    14 class UnknownCommand(Exception):
    14 class UnknownCommand(Exception):
   371             sys.exit(1);
   371             sys.exit(1);
   372     else:
   372     else:
   373         fp = sys.stdout
   373         fp = sys.stdout
   374     fp.write(r.read(n))
   374     fp.write(r.read(n))
   375 
   375 
   376 def clone(ui, source, dest = None, **opts):
   376 def clone(ui, source, dest=None, **opts):
   377     """make a copy of an existing repository"""
   377     """make a copy of an existing repository"""
   378     if dest is None:
   378     if dest is None:
   379         dest = os.path.basename(os.path.normpath(source))
   379         dest = os.path.basename(os.path.normpath(source))
   380 
   380 
   381     if os.path.exists(dest):
   381     if os.path.exists(dest):
   382         ui.warn("abort: destination '%s' already exists\n" % dest)
   382         ui.warn("abort: destination '%s' already exists\n" % dest)
   383         return 1
   383         return 1
   384 
   384 
   385     class Dircleanup:
   385     class Dircleanup:
   386         def __init__(self, dir_):
   386         def __init__(self, dir_):
   387             import shutil
       
   388             self.rmtree = shutil.rmtree
   387             self.rmtree = shutil.rmtree
   389             self.dir_ = dir_
   388             self.dir_ = dir_
   390             os.mkdir(dir_)
   389             os.mkdir(dir_)
   391         def close(self):
   390         def close(self):
   392             self.dir_ = None
   391             self.dir_ = None
   399     source = ui.expandpath(source)
   398     source = ui.expandpath(source)
   400     other = hg.repository(ui, source)
   399     other = hg.repository(ui, source)
   401 
   400 
   402     if other.dev() != -1:
   401     if other.dev() != -1:
   403         abspath = os.path.abspath(source)
   402         abspath = os.path.abspath(source)
   404 
   403         copyfile = (os.stat(dest).st_dev == other.dev()
   405     if other.dev() != -1 and os.stat(dest).st_dev == other.dev():
   404                     and getattr(os, 'link', None) or shutil.copy2)
   406         ui.note("cloning by hardlink\n")
   405         if copyfile is not shutil.copy2:
   407         util.system("cp -al '%s'/.hg '%s'/.hg" % (source, dest))
   406             ui.note("cloning by hardlink\n")
       
   407         util.copytree(os.path.join(source, ".hg"), os.path.join(dest, ".hg"),
       
   408                       copyfile)
   408         try:
   409         try:
   409             os.unlink(os.path.join(dest, ".hg", "dirstate"))
   410             os.unlink(os.path.join(dest, ".hg", "dirstate"))
   410         except IOError:
   411         except IOError:
   411             pass
   412             pass
   412 
   413