hg.py: add islocal() and defaultdest() functions, refactor
authorVadim Gelfer <vadim.gelfer@gmail.com>
Fri, 28 Jul 2006 10:46:25 -0700
changeset 2719 532809ba1db5
parent 2717 14ebe97542a7
child 2720 c91ca61c8953
hg.py: add islocal() and defaultdest() functions, refactor islocal tells if a repo or url is local. defaultdest returns default path for clone if explicit path not given. clone can now take repo or url as source
mercurial/hg.py
--- a/mercurial/hg.py	Fri Jul 28 09:01:13 2006 +0200
+++ b/mercurial/hg.py	Fri Jul 28 10:46:25 2006 -0700
@@ -57,6 +57,23 @@
     'static-http': static_http,
     }
 
+remote_schemes = [
+    'bundle',
+    'hg',
+    'http',
+    'https',
+    'old-http',
+    'ssh',
+    'static-http',
+    ]
+
+def islocal(repo):
+    '''return true if repo or path is local'''
+    if isinstance(repo, str):
+        c = repo.find(':')
+        return c <= 0 or repo[:c] not in remote_schemes
+    return repo.local()
+
 def repository(ui, path=None, create=0):
     scheme = None
     if path:
@@ -74,6 +91,10 @@
                              scheme)
     return ctor(ui, path)
 
+def defaultdest(source):
+    '''return default destination of clone if none is given'''
+    return os.path.basename(os.path.normpath(source))
+    
 def clone(ui, source, dest=None, pull=False, rev=None, update=True,
           stream=False):
     """Make a copy of an existing repository.
@@ -90,7 +111,9 @@
     If an exception is raised, the partly cloned/updated destination
     repository will be deleted.
 
-    Keyword arguments:
+    Arguments:
+
+    source: repository object or URL
 
     dest: URL of destination repository to create (defaults to base
     name of source repository)
@@ -105,8 +128,24 @@
     update: update working directory after clone completes, if
     destination is local repository
     """
+    if isinstance(source, str):
+        src_repo = repository(ui, source)
+    else:
+        src_repo = source
+        source = src_repo.url()
+
     if dest is None:
-        dest = os.path.basename(os.path.normpath(source))
+        dest = defaultdest(source)
+
+    def localpath(path):
+        if path.startswith('file://'):
+            return path[7:]
+        if path.startswith('file:'):
+            return path[5:]
+        return path
+
+    dest = localpath(dest)
+    source = localpath(source)
 
     if os.path.exists(dest):
         raise util.Abort(_("destination '%s' already exists"), dest)
@@ -121,8 +160,6 @@
             if self.dir_:
                 self.rmtree(self.dir_, True)
 
-    src_repo = repository(ui, source)
-
     dest_repo = None
     try:
         dest_repo = repository(ui, dest)
@@ -133,7 +170,7 @@
     dest_path = None
     dir_cleanup = None
     if dest_repo.local():
-        dest_path = os.path.realpath(dest)
+        dest_path = os.path.realpath(dest_repo.root)
         dir_cleanup = DirCleanup(dest_path)
 
     abspath = source