Commit b86bfd15 authored by Vincent Legoll's avatar Vincent Legoll

Create a CachedVc class to gather all common code

duplicated between VC plugins that cache repository
status informations.

There are slight behaviour changes:

bzr, monotone & git used to have a get_tree() which
always called lookup_tree() if it was not already cached,
but without caching at that time either. I think this was
wrong and only cut'n'pasted between plugins. But
would like to hear from the creators of those plugins.

The remaining of the patch is mostly renaming and
passing an additional "directory" argument to
_lookup_tree_cache() as was required for bzr, but
which remains unused for other plugins.
parent eaf703dd
......@@ -163,3 +163,24 @@ class Vc(object):
def _get_dirsandfiles(self, directory, dirs, files):
raise NotImplementedError()
class CachedVc(Vc):
def __init__(self, location):
super(CachedVc, self).__init__(location)
self._tree_cache = None
def cache_inventory(self, directory):
self._tree_cache = self._lookup_tree_cache(directory)
def uncache_inventory(self):
self._tree_cache = None
def _lookup_tree_cache(self, directory):
raise NotImplementedError()
def _get_tree_cache(self, directory):
if self._tree_cache is None:
self.cache_inventory(directory)
return self._tree_cache
......@@ -26,7 +26,7 @@ import os
import errno
import _vc
class Vc(_vc.Vc):
class Vc(_vc.CachedVc):
CMD = "bzr"
NAME = "Bazaar-NG"
......@@ -34,10 +34,6 @@ class Vc(_vc.Vc):
PATCH_STRIP_NUM = 0
PATCH_INDEX_RE = "^=== modified file '(.*)'$"
def __init__(self, location):
self._tree_cache = None
super(Vc, self).__init__(location)
def commit_command(self, message):
return [self.CMD,"commit","-m",message]
def diff_command(self):
......@@ -53,13 +49,7 @@ class Vc(_vc.Vc):
def get_working_directory(self, workdir):
return self.root
def cache_inventory(self, rootdir):
self._tree_cache = self.lookup_tree(rootdir)
def uncache_inventory(self):
self._tree_cache = None
def lookup_tree(self, rootdir):
def _lookup_tree_cache(self, rootdir):
branch_root = os.popen("%s root %s" % (self.CMD, rootdir)).read().rstrip('\n')
while 1:
try:
......@@ -88,15 +78,8 @@ class Vc(_vc.Vc):
tree_state[os.path.join(rootdir, entry[2:])] = cur_state
return tree_state
def get_tree(self, directory):
if self._tree_cache is None:
return self.lookup_tree(directory)
else:
return self._tree_cache
def _get_dirsandfiles(self, directory, dirs, files):
tree = self.get_tree(directory)
tree = self._get_tree_cache(directory)
retfiles = []
retdirs = []
......
......@@ -33,7 +33,7 @@ STATES = {
"R": _vc.STATE_REMOVED
}
class Vc(_vc.Vc):
class Vc(_vc.CachedVc):
CMD = "darcs"
NAME = "Darcs"
......@@ -41,10 +41,6 @@ class Vc(_vc.Vc):
PATCH_STRIP_NUM = 1
PATCH_INDEX_RE = "--- old.+?/(.+?)\\t+.*[0-9]{4}$"
def __init__(self, location):
self._cached_statuses = None
super(Vc, self).__init__(location)
def commit_command(self, message):
return [self.CMD, "record",
"--skip-long-comment",
......@@ -73,25 +69,14 @@ class Vc(_vc.Vc):
def get_working_directory(self, workdir):
return self.root
def cache_inventory(self, rootdir):
self._cached_statuses = self._calculate_statuses()
def uncache_inventory(self):
self._cached_statuses = None
def _get_dirsandfiles(self, directory, dirs, files):
whatsnew = self._get_cached_statuses()
whatsnew = self._get_tree_cache(directory)
retfiles, retdirs = (self._get_statuses(whatsnew, files, _vc.File),
self._get_statuses(whatsnew, dirs, _vc.Dir))
return retfiles, retdirs
def _get_cached_statuses(self):
if self._cached_statuses is None:
self._cached_statuses = self._calculate_statuses()
return self._cached_statuses
def _calculate_statuses(self):
def _lookup_tree_cache(self, rootdir):
non_boring = self._get_whatsnew()
boring = self._get_whatsnew(boring=True)
for path in boring:
......
......@@ -31,7 +31,7 @@ import os
import errno
import _vc
class Vc(_vc.Vc):
class Vc(_vc.CachedVc):
CMD = "git"
NAME = "Git"
......@@ -39,10 +39,6 @@ class Vc(_vc.Vc):
PATCH_STRIP_NUM = 1
PATCH_INDEX_RE = "^diff --git a/(.*) b/.*$"
def __init__(self, location):
self._tree_cache = None
super(Vc, self).__init__(location)
def commit_command(self, message):
return [self.CMD,"commit","-m",message]
def diff_command(self):
......@@ -61,13 +57,7 @@ class Vc(_vc.Vc):
else:
return ''
def cache_inventory(self, topdir):
self._tree_cache = self.lookup_tree()
def uncache_inventory(self):
self._tree_cache = None
def lookup_tree(self):
def _lookup_tree_cache(self, rootdir):
while 1:
try:
proc = os.popen("cd %s && %s status --untracked-files" % (self.root, self.CMD))
......@@ -110,15 +100,9 @@ class Vc(_vc.Vc):
tree_state[os.path.join(self.root, dst)] = _vc.STATE_NEW
return tree_state
def get_tree(self):
if self._tree_cache is None:
return self.lookup_tree()
else:
return self._tree_cache
def _get_dirsandfiles(self, directory, dirs, files):
tree = self.get_tree()
tree = self._get_tree_cache(directory)
retfiles = []
retdirs = []
......
......@@ -26,14 +26,13 @@ import os
import _vc
import errno
class Vc(_vc.Vc):
class Vc(_vc.CachedVc):
NAME = "Monotone"
VC_METADATA = ['MT', '_MTN']
PATCH_STRIP_NUM = 0
PATCH_INDEX_RE = "^[+]{3,3} ([^ ]*)\t[0-9a-f]{40,40}$"
def __init__(self, location):
self._tree_cache = None
self.interface_version = 0.0
self.choose_monotone_version()
super(Vc, self).__init__(os.path.normpath(location))
......@@ -69,13 +68,7 @@ class Vc(_vc.Vc):
def get_working_directory(self, workdir):
return self.root
def cache_inventory(self, rootdir):
self._tree_cache = self.lookup_tree()
def uncache_inventory(self):
self._tree_cache = None
def lookup_tree(self):
def _lookup_tree_cache(self, rootdir):
while 1:
try:
entries = os.popen("cd %s && %s automate inventory" %
......@@ -228,15 +221,9 @@ class Vc(_vc.Vc):
return tree_state
def get_tree(self):
if self._tree_cache is None:
return self.lookup_tree()
else:
return self._tree_cache
def _get_dirsandfiles(self, directory, dirs, files):
tree = self.get_tree()
tree = self._get_tree_cache(directory)
retfiles = []
retdirs = []
......
......@@ -57,7 +57,7 @@ STATES = {
"-/": _vc.STATE_MODIFIED,
}
class Vc(_vc.Vc):
class Vc(_vc.CachedVc):
CMD = "tla"
NAME = "Arch"
......@@ -66,10 +66,6 @@ class Vc(_vc.Vc):
PATCH_STRIP_NUM = 1
PATCH_INDEX_RE = "--- orig/(.*)"
def __init__(self, location):
self._cached_statuses = None
super(Vc, self).__init__(location)
def commit_command(self, message):
return [self.CMD, "commit",
"-s", message]
......@@ -92,25 +88,14 @@ class Vc(_vc.Vc):
def get_working_directory(self, workdir):
return self.root
def cache_inventory(self, rootdir):
self._cached_statuses = self._calculate_statuses()
def uncache_inventory(self):
self._cached_statuses = None
def _get_dirsandfiles(self, directory, dirs, files):
whatsnew = self._get_cached_statuses()
whatsnew = self._get_tree_cache(directory)
retfiles, retdirs = (self._get_statuses(whatsnew, files, _vc.File),
self._get_statuses(whatsnew, dirs, _vc.Dir))
return retfiles, retdirs
def _get_cached_statuses(self):
if self._cached_statuses is None:
self._cached_statuses = self._calculate_statuses()
return self._cached_statuses
def _calculate_statuses(self):
def _lookup_tree_cache(self, rootdir):
whatsnew = {}
commandline = ('%s changes -d %s' % (self.CMD, self.root))
while 1:
......
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