kas: Support --force-checkout argument

When checking out a repository the default behaviour is to abort if
local changes are present. If the new `--force-checkout` argument is
passed on the command line then any local changes will instead be
discarded so that the desired refspec can be checked out.

Signed-off-by: Paul Barker <pbarker@konsulko.com>
Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
This commit is contained in:
Paul Barker 2020-06-15 22:03:30 +02:00 committed by Jan Kiszka
parent 90598db3c8
commit 1569895001
3 changed files with 21 additions and 8 deletions

View File

@ -126,3 +126,7 @@ class Context:
The reference directory for the repo
"""
return self.__kas_repo_ref_dir
@property
def force_checkout(self):
return getattr(self.args, 'force_checkout', None)

View File

@ -106,6 +106,9 @@ def setup_parser_common_args(parser):
parser.add_argument('--skip',
help='Skip build steps',
default=[])
parser.add_argument('--force-checkout', action='store_true',
help='Always checkout the desired refspec of each '
'repository, discarding any local changes')
def kas_get_argparser():
"""

View File

@ -221,13 +221,14 @@ class RepoImpl(Repo):
if self.operations_disabled or self.refspec is None:
return
# Check if repos is dirty
(_, output) = run_cmd(self.is_dirty_cmd(),
cwd=self.path,
fail=False)
if output:
logging.warning('Repo %s is dirty - no checkout', self.name)
return
if not get_context().force_checkout:
# Check if repos is dirty
(_, output) = run_cmd(self.is_dirty_cmd(),
cwd=self.path,
fail=False)
if output:
logging.warning('Repo %s is dirty - no checkout', self.name)
return
(_, output) = run_cmd(self.resolve_branch_cmd(),
cwd=self.path, fail=False)
@ -359,6 +360,8 @@ class GitRepo(RepoImpl):
cmd = ['git', 'checkout', '-q', desired_ref]
if branch:
cmd.extend(['-B', self.refspec])
if get_context().force_checkout:
cmd.append('--force')
return cmd
def prepare_patches_cmd(self):
@ -400,7 +403,10 @@ class MercurialRepo(RepoImpl):
return ['false']
def checkout_cmd(self, desired_ref, branch):
return ['hg', 'checkout', desired_ref]
cmd = ['hg', 'checkout', desired_ref]
if get_context().force_checkout:
cmd.append('--clean')
return cmd
def prepare_patches_cmd(self):
return ['hg', 'branch', '-f',