changeset 260:6977263c4d80

Merge with abderrahim.
author Augie Fackler <durin42@gmail.com>
date Sun, 25 Oct 2009 10:55:12 -0500
parents 8b9fc1b1cc40 (current diff) 1590c97d7af0 (diff)
children 29e5072ddaab
files hggit/__init__.py hggit/git_handler.py hggit/gitrepo.py
diffstat 3 files changed, 24 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/hggit/__init__.py
+++ b/hggit/__init__.py
@@ -15,12 +15,11 @@
 Try hg clone git:// or hg clone git+ssh://
 '''
 
+import os
+
 from mercurial import commands, extensions, hg, util
 from mercurial.i18n import _
 
-from dulwich.repo import Repo
-from dulwich.errors import NotGitRepository
-
 import gitrepo, hgrepo
 from git_handler import GitHandler
 
@@ -29,15 +28,15 @@
 hg.schemes['git'] = gitrepo
 hg.schemes['git+ssh'] = gitrepo
 
+# support for `hg clone localgitrepo`
 _oldlocal = hg.schemes['file']
 
 def _local(path):
     p = util.drop_scheme('file', path)
-    try:
-        Repo(p)
+    if (os.path.exists(os.path.join(p, '.git')) and 
+        not os.path.exists(os.path.join(p, '.hg'))):
         return gitrepo
-    except NotGitRepository:
-        return _oldlocal(path)
+    return _oldlocal(path)
 
 hg.schemes['file'] = _local
 
--- a/hggit/git_handler.py
+++ b/hggit/git_handler.py
@@ -33,19 +33,16 @@
 
         self.paths = ui.configitems('paths')
 
-        self.init_if_missing()
-        self.load_git()
         self.load_map()
         self.load_tags()
 
     # make the git data directory
     def init_if_missing(self):
-        if not os.path.exists(self.gitdir):
+        if os.path.exists(self.gitdir):
+            self.git = Repo(self.gitdir)
+        else:
             os.mkdir(self.gitdir)
-            Repo.init_bare(self.gitdir)
-
-    def load_git(self):
-        self.git = Repo(self.gitdir)
+            self.git = Repo.init_bare(self.gitdir)
 
     ## FILE LOAD AND SAVE METHODS
 
@@ -176,6 +173,8 @@
 
     def export_git_objects(self):
         self.ui.status(_("importing Hg objects into Git\n"))
+        self.init_if_missing()
+
         nodes = [self.repo.lookup(n) for n in self.repo]
         export = [node for node in nodes if not hex(node) in self._map_hg]
         total = len(export)
@@ -352,6 +351,8 @@
 
     def import_git_objects(self, remote_name=None, refs=None):
         self.ui.status(_("importing Git objects into Hg\n"))
+        self.init_if_missing()
+
         # import heads and fetched tags as remote references
         todo = []
         done = set()
--- a/hggit/gitrepo.py
+++ b/hggit/gitrepo.py
@@ -1,4 +1,9 @@
 from mercurial import repo, util
+try:
+    from mercurial.error import RepoError
+except ImportError:
+    from mercurial.repo import RepoError
+
 from git_handler import GitHandler
 
 class gitrepo(repo.repository):
@@ -6,9 +11,14 @@
     def __init__(self, ui, path, create):
         if create: # pragma: no cover
             raise util.Abort('Cannot create a git repository.')
+        self.ui = ui
         self.path = path
     def lookup(self, key):
         if isinstance(key, str):
             return key
+    def local(self):
+        if not self.path:
+            raise RepoError
+
 
 instance = gitrepo