[meld] vc: Move validity checks to class methods; use classes in VcView



commit aa2cba8bf9e705ce1ab9bd26bd5be106ecc57111
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Aug 18 14:00:37 2013 +1000

    vc: Move validity checks to class methods; use classes in VcView

 meld/vc/__init__.py  |   18 +++++++-----------
 meld/vc/_vc.py       |    9 +++++----
 meld/vc/bzr.py       |    5 +++--
 meld/vc/cvs.py       |    6 +++---
 meld/vc/darcs.py     |    5 +++--
 meld/vc/fossil.py    |    5 +++--
 meld/vc/git.py       |    5 +++--
 meld/vc/mercurial.py |    5 +++--
 meld/vc/monotone.py  |    5 +++--
 meld/vc/svn.py       |   17 ++++++++++-------
 meld/vc/svn_17.py    |    3 ++-
 meld/vcview.py       |    7 ++++---
 12 files changed, 49 insertions(+), 41 deletions(-)
---
diff --git a/meld/vc/__init__.py b/meld/vc/__init__.py
index 92a4da0..134a0a9 100644
--- a/meld/vc/__init__.py
+++ b/meld/vc/__init__.py
@@ -75,25 +75,21 @@ def get_vcs(location):
     vcs = []
     max_depth = 0
     for plugin in _plugins:
-        try:
-            vc = plugin.Vc(location)
-        except ValueError:
+        root, location = plugin.Vc.is_in_repo(location)
+        if not root:
             continue
 
         # Choose the deepest root we find, unless it's from a VC that
         # doesn't walk; these can be spurious as the real root may be
         # much higher up in the tree.
-        depth = len(vc.root)
-        if depth > max_depth and vc.VC_ROOT_WALK:
+        depth = len(root)
+        if depth > max_depth and plugin.Vc.VC_ROOT_WALK:
             vcs, max_depth = [], depth
         if depth >= max_depth:
-            vcs.append(vc)
+            vcs.append(plugin.Vc)
 
     if not vcs:
         # No plugin recognized that location, fallback to _null
-        return [_null.Vc(location)]
+        return [_null]
 
-    vc_sort_key = lambda v: vc_sort_order.index(v.NAME)
-    vcs.sort(key=vc_sort_key)
-
-    return vcs
+    return sorted(vcs, key=lambda v: vc_sort_order.index(v.NAME))
diff --git a/meld/vc/_vc.py b/meld/vc/_vc.py
index 1fd9bc9..be3c8c2 100644
--- a/meld/vc/_vc.py
+++ b/meld/vc/_vc.py
@@ -200,10 +200,6 @@ class Vc(object):
         """
         pass
 
-    # Determine if a directory is a valid git/svn/hg/cvs/etc repository
-    def valid_repo(self):
-        return True
-
     def listdir(self, path="."):
         try:
             entries = sorted(e for e in os.listdir(path) if e != self.VC_DIR)
@@ -275,6 +271,11 @@ class Vc(object):
             if location == old:
                 break
 
+    @classmethod
+    def valid_repo(cls, path):
+        """Determine if a directory is a valid repository for this class"""
+        raise NotImplementedError
+
 
 class CachedVc(Vc):
 
diff --git a/meld/vc/bzr.py b/meld/vc/bzr.py
index caf3d18..17efbfc 100644
--- a/meld/vc/bzr.py
+++ b/meld/vc/bzr.py
@@ -108,8 +108,9 @@ class Vc(_vc.CachedVc):
             [self.CMD] + self.CMDARGS + ["rm"] + files, [], refresh=True,
             working_dir=self.root)
 
-    def valid_repo(self):
-        return not _vc.call([self.CMD, "root"], cwd=self.root)
+    @classmethod
+    def valid_repo(cls, path):
+        return not _vc.call([cls.CMD, "root"], cwd=path)
 
     def get_working_directory(self, workdir):
         return self.root
diff --git a/meld/vc/cvs.py b/meld/vc/cvs.py
index 5541b75..1479de6 100644
--- a/meld/vc/cvs.py
+++ b/meld/vc/cvs.py
@@ -73,9 +73,9 @@ class Vc(_vc.Vc):
     def revert_command(self):
         return [self.CMD, "update", "-C"]
 
-    def valid_repo(self):
-        entry_path = os.path.join(self.root, self.VC_DIR, "Entries")
-        return os.path.exists(entry_path)
+    @classmethod
+    def valid_repo(cls, path):
+        return os.path.exists(os.path.join(path, cls.VC_DIR, "Entries"))
 
     def get_path_for_repo_file(self, path, commit=None):
         if commit is not None:
diff --git a/meld/vc/darcs.py b/meld/vc/darcs.py
index 01f086d..1c7340c 100644
--- a/meld/vc/darcs.py
+++ b/meld/vc/darcs.py
@@ -69,8 +69,9 @@ class Vc(_vc.CachedVc):
         # untested
         return [self.CMD, "resolve"]
 
-    def valid_repo(self):
-        return not _vc.call([self.CMD, "query", "tags"], cwd=self.root)
+    @classmethod
+    def valid_repo(cls, path):
+        return not _vc.call([cls.CMD, "query", "tags"], cwd=path)
 
     def get_working_directory(self, workdir):
         return self.root
diff --git a/meld/vc/fossil.py b/meld/vc/fossil.py
index 8d8f956..512ebc2 100644
--- a/meld/vc/fossil.py
+++ b/meld/vc/fossil.py
@@ -65,8 +65,9 @@ class Vc(_vc.CachedVc):
     def revert_command(self):
         return [self.CMD, "revert"]
 
-    def valid_repo(self):
-        return not _vc.call([self.CMD, "info"], cwd=self.root)
+    @classmethod
+    def valid_repo(cls, path):
+        return not _vc.call([cls.CMD, "info"], cwd=path)
 
     @classmethod
     def check_repo_root(self, location):
diff --git a/meld/vc/git.py b/meld/vc/git.py
index 2dba2f7..477ef86 100644
--- a/meld/vc/git.py
+++ b/meld/vc/git.py
@@ -255,10 +255,11 @@ class Vc(_vc.CachedVc):
             shutil.copyfileobj(vc_file, f)
         return f.name
 
-    def valid_repo(self):
+    @classmethod
+    def valid_repo(cls, path):
         # TODO: On Windows, this exit code is wrong under the normal shell; it
         # appears to be correct under the default git bash shell however.
-        return not _vc.call([self.CMD, "branch"], cwd=self.root)
+        return not _vc.call([cls.CMD, "branch"], cwd=path)
 
     def get_working_directory(self, workdir):
         if workdir.startswith("/"):
diff --git a/meld/vc/mercurial.py b/meld/vc/mercurial.py
index 8d606be..fb839a5 100644
--- a/meld/vc/mercurial.py
+++ b/meld/vc/mercurial.py
@@ -61,8 +61,9 @@ class Vc(_vc.CachedVc):
     def revert_command(self):
         return [self.CMD, "revert"]
 
-    def valid_repo(self):
-        return not _vc.call([self.CMD, "root"], cwd=self.root)
+    @classmethod
+    def valid_repo(cls, path):
+        return not _vc.call([cls.CMD, "root"], cwd=path)
 
     def get_working_directory(self, workdir):
         if workdir.startswith("/"):
diff --git a/meld/vc/monotone.py b/meld/vc/monotone.py
index 6f5922f..4e26e1d 100644
--- a/meld/vc/monotone.py
+++ b/meld/vc/monotone.py
@@ -133,8 +133,9 @@ class Vc(_vc.CachedVc):
     def resolved_command(self):
         return [self.CMD, "resolved"]
 
-    def valid_repo(self):
-        return not _vc.call([self.CMD, "list", "tags"], cwd=self.root)
+    @classmethod
+    def valid_repo(cls, path):
+        return not _vc.call([cls.CMD, "list", "tags"], cwd=path)
 
     def get_working_directory(self, workdir):
         return self.root
diff --git a/meld/vc/svn.py b/meld/vc/svn.py
index e5a780c..321b8ee 100644
--- a/meld/vc/svn.py
+++ b/meld/vc/svn.py
@@ -143,17 +143,20 @@ class Vc(_vc.CachedVc):
 
         raise KeyError("Conflict file does not exist")
 
-    def _repo_version_support(self, version):
+
+    @classmethod
+    def _repo_version_support(cls, version):
         return version < 12
 
-    def valid_repo(self):
-        if _vc.call([self.CMD, "info"], cwd=self.root):
+    @classmethod
+    def valid_repo(cls, path):
+        if _vc.call([cls.CMD, "info"], cwd=path):
             return False
 
         # Check for repository version, trusting format file then entries file
-        format_path = os.path.join(self.root, self.VC_DIR, "format")
-        entries_path = os.path.join(self.root, self.VC_DIR, "entries")
-        wcdb_path = os.path.join(self.root, self.VC_DIR, "wc.db")
+        format_path = os.path.join(path, cls.VC_DIR, "format")
+        entries_path = os.path.join(path, cls.VC_DIR, "entries")
+        wcdb_path = os.path.join(path, cls.VC_DIR, "wc.db")
         format_exists = os.path.exists(format_path)
         entries_exists = os.path.exists(entries_path)
         wcdb_exists = os.path.exists(wcdb_path)
@@ -168,7 +171,7 @@ class Vc(_vc.CachedVc):
         else:
             return False
 
-        return self._repo_version_support(repo_version)
+        return cls._repo_version_support(repo_version)
 
     def _update_tree_state_cache(self, path, tree_state):
         while 1:
diff --git a/meld/vc/svn_17.py b/meld/vc/svn_17.py
index 810300f..0870f44 100644
--- a/meld/vc/svn_17.py
+++ b/meld/vc/svn_17.py
@@ -35,7 +35,8 @@ class Vc(svn.Vc):
     NAME = "Subversion 1.7"
     VC_ROOT_WALK = True
 
-    def _repo_version_support(self, version):
+    @classmethod
+    def _repo_version_support(cls, version):
         return version >= 12
 
     def get_path_for_repo_file(self, path, commit=None):
diff --git a/meld/vcview.py b/meld/vcview.py
index f4f191d..499c664 100644
--- a/meld/vcview.py
+++ b/meld/vcview.py
@@ -321,7 +321,8 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
         vcs_model.clear()
         default_active = -1
         valid_vcs = []
-        vcs = vc.get_vcs(os.path.abspath(location or "."))
+        location = os.path.abspath(location or ".")
+        vcs = vc.get_vcs(location)
         # Try to keep the same VC plugin active on refresh()
         for idx, avc in enumerate(vcs):
             # See if the necessary version control command exists.  If so,
@@ -334,7 +335,7 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
                 # TRANSLATORS: this is an error message when a version control
                 # application isn't installed or can't be found
                 err_str = _("%s not installed" % avc.CMD)
-            elif not avc.valid_repo():
+            elif not avc.valid_repo(location):
                 # TRANSLATORS: this is an error message when a version
                 # controlled repository is invalid or corrupted
                 err_str = _("Invalid repository")
@@ -349,7 +350,7 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
                     [_("%s (%s)") % (avc.NAME, err_str), avc, False])
             else:
                 name = avc.NAME or _("None")
-                vcs_model.append([name, avc, True])
+                vcs_model.append([name, avc(location), True])
 
         if not valid_vcs:
             # If we didn't get any valid vcs then fallback to null


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]