Commit aa2cba8b authored by Kai Willadsen's avatar Kai Willadsen

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

parent 156b7928
...@@ -75,25 +75,21 @@ def get_vcs(location): ...@@ -75,25 +75,21 @@ def get_vcs(location):
vcs = [] vcs = []
max_depth = 0 max_depth = 0
for plugin in _plugins: for plugin in _plugins:
try: root, location = plugin.Vc.is_in_repo(location)
vc = plugin.Vc(location) if not root:
except ValueError:
continue continue
# Choose the deepest root we find, unless it's from a VC that # 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 # doesn't walk; these can be spurious as the real root may be
# much higher up in the tree. # much higher up in the tree.
depth = len(vc.root) depth = len(root)
if depth > max_depth and vc.VC_ROOT_WALK: if depth > max_depth and plugin.Vc.VC_ROOT_WALK:
vcs, max_depth = [], depth vcs, max_depth = [], depth
if depth >= max_depth: if depth >= max_depth:
vcs.append(vc) vcs.append(plugin.Vc)
if not vcs: if not vcs:
# No plugin recognized that location, fallback to _null # 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) return sorted(vcs, key=lambda v: vc_sort_order.index(v.NAME))
vcs.sort(key=vc_sort_key)
return vcs
...@@ -200,10 +200,6 @@ class Vc(object): ...@@ -200,10 +200,6 @@ class Vc(object):
""" """
pass pass
# Determine if a directory is a valid git/svn/hg/cvs/etc repository
def valid_repo(self):
return True
def listdir(self, path="."): def listdir(self, path="."):
try: try:
entries = sorted(e for e in os.listdir(path) if e != self.VC_DIR) entries = sorted(e for e in os.listdir(path) if e != self.VC_DIR)
...@@ -275,6 +271,11 @@ class Vc(object): ...@@ -275,6 +271,11 @@ class Vc(object):
if location == old: if location == old:
break break
@classmethod
def valid_repo(cls, path):
"""Determine if a directory is a valid repository for this class"""
raise NotImplementedError
class CachedVc(Vc): class CachedVc(Vc):
......
...@@ -108,8 +108,9 @@ class Vc(_vc.CachedVc): ...@@ -108,8 +108,9 @@ class Vc(_vc.CachedVc):
[self.CMD] + self.CMDARGS + ["rm"] + files, [], refresh=True, [self.CMD] + self.CMDARGS + ["rm"] + files, [], refresh=True,
working_dir=self.root) working_dir=self.root)
def valid_repo(self): @classmethod
return not _vc.call([self.CMD, "root"], cwd=self.root) def valid_repo(cls, path):
return not _vc.call([cls.CMD, "root"], cwd=path)
def get_working_directory(self, workdir): def get_working_directory(self, workdir):
return self.root return self.root
......
...@@ -73,9 +73,9 @@ class Vc(_vc.Vc): ...@@ -73,9 +73,9 @@ class Vc(_vc.Vc):
def revert_command(self): def revert_command(self):
return [self.CMD, "update", "-C"] return [self.CMD, "update", "-C"]
def valid_repo(self): @classmethod
entry_path = os.path.join(self.root, self.VC_DIR, "Entries") def valid_repo(cls, path):
return os.path.exists(entry_path) return os.path.exists(os.path.join(path, cls.VC_DIR, "Entries"))
def get_path_for_repo_file(self, path, commit=None): def get_path_for_repo_file(self, path, commit=None):
if commit is not None: if commit is not None:
......
...@@ -69,8 +69,9 @@ class Vc(_vc.CachedVc): ...@@ -69,8 +69,9 @@ class Vc(_vc.CachedVc):
# untested # untested
return [self.CMD, "resolve"] return [self.CMD, "resolve"]
def valid_repo(self): @classmethod
return not _vc.call([self.CMD, "query", "tags"], cwd=self.root) def valid_repo(cls, path):
return not _vc.call([cls.CMD, "query", "tags"], cwd=path)
def get_working_directory(self, workdir): def get_working_directory(self, workdir):
return self.root return self.root
......
...@@ -65,8 +65,9 @@ class Vc(_vc.CachedVc): ...@@ -65,8 +65,9 @@ class Vc(_vc.CachedVc):
def revert_command(self): def revert_command(self):
return [self.CMD, "revert"] return [self.CMD, "revert"]
def valid_repo(self): @classmethod
return not _vc.call([self.CMD, "info"], cwd=self.root) def valid_repo(cls, path):
return not _vc.call([cls.CMD, "info"], cwd=path)
@classmethod @classmethod
def check_repo_root(self, location): def check_repo_root(self, location):
......
...@@ -255,10 +255,11 @@ class Vc(_vc.CachedVc): ...@@ -255,10 +255,11 @@ class Vc(_vc.CachedVc):
shutil.copyfileobj(vc_file, f) shutil.copyfileobj(vc_file, f)
return f.name 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 # TODO: On Windows, this exit code is wrong under the normal shell; it
# appears to be correct under the default git bash shell however. # 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): def get_working_directory(self, workdir):
if workdir.startswith("/"): if workdir.startswith("/"):
......
...@@ -61,8 +61,9 @@ class Vc(_vc.CachedVc): ...@@ -61,8 +61,9 @@ class Vc(_vc.CachedVc):
def revert_command(self): def revert_command(self):
return [self.CMD, "revert"] return [self.CMD, "revert"]
def valid_repo(self): @classmethod
return not _vc.call([self.CMD, "root"], cwd=self.root) def valid_repo(cls, path):
return not _vc.call([cls.CMD, "root"], cwd=path)
def get_working_directory(self, workdir): def get_working_directory(self, workdir):
if workdir.startswith("/"): if workdir.startswith("/"):
......
...@@ -133,8 +133,9 @@ class Vc(_vc.CachedVc): ...@@ -133,8 +133,9 @@ class Vc(_vc.CachedVc):
def resolved_command(self): def resolved_command(self):
return [self.CMD, "resolved"] return [self.CMD, "resolved"]
def valid_repo(self): @classmethod
return not _vc.call([self.CMD, "list", "tags"], cwd=self.root) def valid_repo(cls, path):
return not _vc.call([cls.CMD, "list", "tags"], cwd=path)
def get_working_directory(self, workdir): def get_working_directory(self, workdir):
return self.root return self.root
......
...@@ -143,17 +143,20 @@ class Vc(_vc.CachedVc): ...@@ -143,17 +143,20 @@ class Vc(_vc.CachedVc):
raise KeyError("Conflict file does not exist") raise KeyError("Conflict file does not exist")
def _repo_version_support(self, version):
@classmethod
def _repo_version_support(cls, version):
return version < 12 return version < 12
def valid_repo(self): @classmethod
if _vc.call([self.CMD, "info"], cwd=self.root): def valid_repo(cls, path):
if _vc.call([cls.CMD, "info"], cwd=path):
return False return False
# Check for repository version, trusting format file then entries file # Check for repository version, trusting format file then entries file
format_path = os.path.join(self.root, self.VC_DIR, "format") format_path = os.path.join(path, cls.VC_DIR, "format")
entries_path = os.path.join(self.root, self.VC_DIR, "entries") entries_path = os.path.join(path, cls.VC_DIR, "entries")
wcdb_path = os.path.join(self.root, self.VC_DIR, "wc.db") wcdb_path = os.path.join(path, cls.VC_DIR, "wc.db")
format_exists = os.path.exists(format_path) format_exists = os.path.exists(format_path)
entries_exists = os.path.exists(entries_path) entries_exists = os.path.exists(entries_path)
wcdb_exists = os.path.exists(wcdb_path) wcdb_exists = os.path.exists(wcdb_path)
...@@ -168,7 +171,7 @@ class Vc(_vc.CachedVc): ...@@ -168,7 +171,7 @@ class Vc(_vc.CachedVc):
else: else:
return False 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): def _update_tree_state_cache(self, path, tree_state):
while 1: while 1:
......
...@@ -35,7 +35,8 @@ class Vc(svn.Vc): ...@@ -35,7 +35,8 @@ class Vc(svn.Vc):
NAME = "Subversion 1.7" NAME = "Subversion 1.7"
VC_ROOT_WALK = True VC_ROOT_WALK = True
def _repo_version_support(self, version): @classmethod
def _repo_version_support(cls, version):
return version >= 12 return version >= 12
def get_path_for_repo_file(self, path, commit=None): def get_path_for_repo_file(self, path, commit=None):
......
...@@ -321,7 +321,8 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component): ...@@ -321,7 +321,8 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
vcs_model.clear() vcs_model.clear()
default_active = -1 default_active = -1
valid_vcs = [] 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() # Try to keep the same VC plugin active on refresh()
for idx, avc in enumerate(vcs): for idx, avc in enumerate(vcs):
# See if the necessary version control command exists. If so, # See if the necessary version control command exists. If so,
...@@ -334,7 +335,7 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component): ...@@ -334,7 +335,7 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
# TRANSLATORS: this is an error message when a version control # TRANSLATORS: this is an error message when a version control
# application isn't installed or can't be found # application isn't installed or can't be found
err_str = _("%s not installed" % avc.CMD) 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 # TRANSLATORS: this is an error message when a version
# controlled repository is invalid or corrupted # controlled repository is invalid or corrupted
err_str = _("Invalid repository") err_str = _("Invalid repository")
...@@ -349,7 +350,7 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component): ...@@ -349,7 +350,7 @@ class VcView(melddoc.MeldDoc, gnomeglade.Component):
[_("%s (%s)") % (avc.NAME, err_str), avc, False]) [_("%s (%s)") % (avc.NAME, err_str), avc, False])
else: else:
name = avc.NAME or _("None") name = avc.NAME or _("None")
vcs_model.append([name, avc, True]) vcs_model.append([name, avc(location), True])
if not valid_vcs: if not valid_vcs:
# If we didn't get any valid vcs then fallback to null # If we didn't get any valid vcs then fallback to null
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment