Commit d166deff authored by Vincent Legoll's avatar Vincent Legoll

Factorize repository root finding code

Bug 573049

There's code duplication in vc plugins.

The code that tries to find the repository root is duplicated in a lot of files
and is almost always identical.

The attached patch tries to consolidate this into a single implementation in
vc.py


svn path=/trunk/; revision=1189
parent aebef210
......@@ -70,6 +70,10 @@ class File(Entry):
class Vc(object):
PATCH_STRIP_NUM = 0
VC_DIR = None
def __init__(self, location):
self.root = self.find_repo_root(location, self.VC_DIR)
def commit_command(self, message):
raise NotImplementedError()
......@@ -86,6 +90,15 @@ class Vc(object):
def patch_command(self, workdir):
return ["patch","--strip=%i"%self.PATCH_STRIP_NUM,"--reverse","--directory=%s" % workdir]
def find_repo_root(self, start, subdir, raiseError = True):
while start != "/":
if os.path.isdir(os.path.join(start, subdir)):
return start
start = os.path.dirname(start)
if raiseError:
raise ValueError()
return None
def get_working_directory(self, workdir):
return workdir
......
......@@ -30,17 +30,13 @@ class Vc(_vc.Vc):
CMD = "bzr"
NAME = "Bazaar-NG"
VC_DIR = ".bzr"
PATCH_STRIP_NUM = 0
PATCH_INDEX_RE = "^=== modified file '(.*)'$"
def __init__(self, location):
self._tree_cache = None
while location != "/":
if os.path.isdir( "%s/.bzr" % location):
self.root = location
return
location = os.path.dirname(location)
raise ValueError()
_vc.Vc.__init__(self, location)
def commit_command(self, message):
return [self.CMD,"commit","-m",message]
......
......@@ -37,17 +37,13 @@ class Vc(_vc.Vc):
CMD = "darcs"
NAME = "Darcs"
VC_DIR = "_darcs"
PATCH_STRIP_NUM = 1
PATCH_INDEX_RE = "--- old.+?/(.+?)\\t+.*[0-9]{4}$"
def __init__(self, location):
self._cachetime = None
while location != "/":
if os.path.isdir( "%s/_darcs" % location):
self.root = location
return
location = os.path.dirname(location)
raise ValueError()
_vc.Vc.__init__(self, location)
def commit_command(self, message):
return [self.CMD, "record",
......
......@@ -35,21 +35,20 @@ class Vc(_vc.Vc):
CMD = "git"
NAME = "Git"
VC_DIR = ".git"
PATCH_STRIP_NUM = 1
PATCH_INDEX_RE = "^diff --git a/(.*) b/.*$"
def __init__(self, location):
self._tree_cache = None
while location != "/":
if os.path.isdir( "%s/.git" % location):
self.root = location
try:
_vc.Vc.__init__(self, location)
except ValueError:
gitdir = os.environ.get("GIT_DIR")
if gitdir and os.path.isdir(gitdir):
self.root = gitdir
return
location = os.path.dirname(location)
gitdir = os.environ.get("GIT_DIR")
if gitdir and os.path.isdir(gitdir):
self.root = gitdir
return
raise ValueError()
raise ValueError()
def commit_command(self, message):
return [self.CMD,"commit","-m",message]
......
......@@ -29,17 +29,10 @@ class Vc(_vc.Vc):
CMD = "hg"
NAME = "Mercurial"
VC_DIR = ".hg"
PATCH_STRIP_NUM = 1
PATCH_INDEX_RE = "^diff -r \w+ (.*)$"
def __init__(self, location):
while location != "/":
if os.path.isdir( "%s/.hg" % location):
self.root = location
return
location = os.path.dirname(location)
raise ValueError()
def commit_command(self, message):
return [self.CMD,"commit","-m",message]
def diff_command(self):
......
......@@ -36,15 +36,8 @@ class Vc(_vc.Vc):
self._tree_cache = None
location = os.path.normpath(location)
def find_folder(where, tofind):
while where != "/":
cur = os.path.join(where,tofind)
if os.path.isdir(cur):
return where
where = os.path.dirname(where)
# for monotone >= 0.26
mtn = find_folder(location,"_MTN")
mtn = self.find_repo_root(location, "_MTN", raiseError = False)
if mtn:
self.root = mtn
......@@ -55,7 +48,7 @@ class Vc(_vc.Vc):
return
# for monotone <= 0.25 (different metadata directory, different executable)
mt = find_folder(location,"MT")
mt = self.find_repo_root(location, "MT", raiseError = False)
if mt:
self.root = mt
self.CMD = "monotone"
......
......@@ -61,18 +61,14 @@ class Vc(_vc.Vc):
CMD = "tla"
NAME = "Arch"
VC_DIR = "{arch}"
PATCH_STRIP_NUM = 1
PATCH_INDEX_RE = "--- orig/(.*)"
def __init__(self, location):
self._cachetime = None
self._cached_statuses = None
while location != "/":
if os.path.isdir( "%s/{arch}" % location):
self.root = location
return
location = os.path.dirname(location)
raise ValueError()
_vc.Vc.__init__(self, location)
def commit_command(self, message):
return [self.CMD, "commit",
......
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