repos: factor git-cmds out of a common Repo implementation
This creates an interface that could also be used to support other version control systems. Signed-off-by: Henning Schild <henning.schild@siemens.com>
This commit is contained in:
parent
6a85296836
commit
c47b511d34
62
kas/repos.py
62
kas/repos.py
@ -86,7 +86,7 @@ class Repo:
|
|||||||
disable_operations = False
|
disable_operations = False
|
||||||
|
|
||||||
if url is None:
|
if url is None:
|
||||||
# No git operation on repository
|
# No version control operation on repository
|
||||||
if path is None:
|
if path is None:
|
||||||
path = Repo.get_root_path(os.path.dirname(config.filename),
|
path = Repo.get_root_path(os.path.dirname(config.filename),
|
||||||
config.environ)
|
config.environ)
|
||||||
@ -120,9 +120,9 @@ class Repo:
|
|||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
class GitRepo(Repo):
|
class RepoImpl(Repo):
|
||||||
"""
|
"""
|
||||||
Provides the git implementations for a Repo.
|
Provides a generic implementation for a Repo.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
@ -135,14 +135,11 @@ class GitRepo(Repo):
|
|||||||
|
|
||||||
if not os.path.exists(self.path):
|
if not os.path.exists(self.path):
|
||||||
os.makedirs(os.path.dirname(self.path), exist_ok=True)
|
os.makedirs(os.path.dirname(self.path), exist_ok=True)
|
||||||
gitsrcdir = os.path.join(config.get_repo_ref_dir() or '',
|
sdir = os.path.join(config.get_repo_ref_dir() or '',
|
||||||
self.qualified_name)
|
self.qualified_name)
|
||||||
logging.debug('Looking for repo ref dir in %s', gitsrcdir)
|
logging.debug('Looking for repo ref dir in %s', sdir)
|
||||||
|
|
||||||
cmd = ['git', 'clone', '-q', self.url, self.path]
|
(retc, _) = yield from run_cmd_async(self.clone_cmd(sdir, config),
|
||||||
if config.get_repo_ref_dir() and os.path.exists(gitsrcdir):
|
|
||||||
cmd.extend(['--reference', gitsrcdir])
|
|
||||||
(retc, _) = yield from run_cmd_async(cmd,
|
|
||||||
env=config.environ,
|
env=config.environ,
|
||||||
cwd=config.kas_work_dir)
|
cwd=config.kas_work_dir)
|
||||||
if retc == 0:
|
if retc == 0:
|
||||||
@ -154,9 +151,7 @@ class GitRepo(Repo):
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
# Does refspec exist in the current repository?
|
# Does refspec exist in the current repository?
|
||||||
(retc, output) = yield from run_cmd_async(['git',
|
(retc, output) = yield from run_cmd_async(self.contains_refspec_cmd(),
|
||||||
'cat-file', '-t',
|
|
||||||
self.refspec],
|
|
||||||
env=config.environ,
|
env=config.environ,
|
||||||
cwd=self.path,
|
cwd=self.path,
|
||||||
fail=False,
|
fail=False,
|
||||||
@ -167,8 +162,7 @@ class GitRepo(Repo):
|
|||||||
return retc
|
return retc
|
||||||
|
|
||||||
# No it is missing, try to fetch
|
# No it is missing, try to fetch
|
||||||
(retc, output) = yield from run_cmd_async(['git',
|
(retc, output) = yield from run_cmd_async(self.fetch_cmd(),
|
||||||
'fetch', '--all'],
|
|
||||||
env=config.environ,
|
env=config.environ,
|
||||||
cwd=self.path,
|
cwd=self.path,
|
||||||
fail=False)
|
fail=False)
|
||||||
@ -187,7 +181,7 @@ class GitRepo(Repo):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Check if repos is dirty
|
# Check if repos is dirty
|
||||||
(_, output) = run_cmd(['git', 'diff', '--shortstat'],
|
(_, output) = run_cmd(self.is_dirty_cmd(),
|
||||||
env=config.environ, cwd=self.path,
|
env=config.environ, cwd=self.path,
|
||||||
fail=False)
|
fail=False)
|
||||||
if output:
|
if output:
|
||||||
@ -195,8 +189,7 @@ class GitRepo(Repo):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# Check if current HEAD is what in the config file is defined.
|
# Check if current HEAD is what in the config file is defined.
|
||||||
(_, output) = run_cmd(['git', 'rev-parse',
|
(_, output) = run_cmd(self.current_rev_cmd(),
|
||||||
'--verify', 'HEAD'],
|
|
||||||
env=config.environ, cwd=self.path)
|
env=config.environ, cwd=self.path)
|
||||||
|
|
||||||
if output.strip() == self.refspec:
|
if output.strip() == self.refspec:
|
||||||
@ -204,6 +197,33 @@ class GitRepo(Repo):
|
|||||||
'refspec. nothing to do', self.name)
|
'refspec. nothing to do', self.name)
|
||||||
return
|
return
|
||||||
|
|
||||||
run_cmd(['git', 'checkout', '-q',
|
run_cmd(self.checkout_cmd(), cwd=self.path)
|
||||||
'{refspec}'.format(refspec=self.refspec)],
|
|
||||||
cwd=self.path)
|
|
||||||
|
class GitRepo(RepoImpl):
|
||||||
|
"""
|
||||||
|
Provides the git implementations for a Repo.
|
||||||
|
"""
|
||||||
|
# pylint: disable=no-self-use,missing-docstring
|
||||||
|
|
||||||
|
def clone_cmd(self, gitsrcdir, config):
|
||||||
|
cmd = ['git', 'clone', '-q', self.url, self.path]
|
||||||
|
if config.get_repo_ref_dir() and os.path.exists(gitsrcdir):
|
||||||
|
cmd.extend(['--reference', gitsrcdir])
|
||||||
|
return cmd
|
||||||
|
|
||||||
|
def contains_refspec_cmd(self):
|
||||||
|
return ['git', 'cat-file', '-t', self.refspec]
|
||||||
|
|
||||||
|
def fetch_cmd(self):
|
||||||
|
return ['git', 'fetch', '--all']
|
||||||
|
|
||||||
|
def is_dirty_cmd(self):
|
||||||
|
return ['git', 'diff', '--shortstat']
|
||||||
|
|
||||||
|
def current_rev_cmd(self):
|
||||||
|
return ['git', 'rev-parse', '--verify', 'HEAD']
|
||||||
|
|
||||||
|
def checkout_cmd(self):
|
||||||
|
return ['git', 'checkout', '-q',
|
||||||
|
'{refspec}'.format(refspec=self.refspec)]
|
||||||
|
Loading…
Reference in New Issue
Block a user