diff --git a/kas/context.py b/kas/context.py index a2cee02..a17bac3 100644 --- a/kas/context.py +++ b/kas/context.py @@ -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) diff --git a/kas/kas.py b/kas/kas.py index ab6076b..453d464 100644 --- a/kas/kas.py +++ b/kas/kas.py @@ -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(): """ diff --git a/kas/repos.py b/kas/repos.py index 1e3929e..c1c3281 100644 --- a/kas/repos.py +++ b/kas/repos.py @@ -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',