From beee9ab79e257b2abbc49dfe7cf1200491635c79 Mon Sep 17 00:00:00 2001 From: Dirk Eibach Date: Tue, 20 Jun 2023 12:15:35 +0200 Subject: [PATCH] Add override-branch argument Introduce a override-branch argument to be able to use teamscale-cli on local branches that are not known to Teamscale server. --- teamscale_precommit_client/precommit_client.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/teamscale_precommit_client/precommit_client.py b/teamscale_precommit_client/precommit_client.py index 3458179..9473a79 100644 --- a/teamscale_precommit_client/precommit_client.py +++ b/teamscale_precommit_client/precommit_client.py @@ -20,6 +20,7 @@ # Filename of the precommit configuration. The client expects this config file at the root of the repository. PRECOMMIT_CONFIG_FILENAME = '.teamscale-precommit.config' DEFAULT_PROJECT_SUBPATH = '' +DEFAULT_OVERRIDE_BRANCH = '' DEFAULT_PATH_PREFIX = '' DEFAULT_FILE_ENCODING = None # Use system encoding @@ -33,7 +34,7 @@ def __init__(self, teamscale_config, repository_path, path_prefix=DEFAULT_PATH_P analyzed_file=None, verify=True, omit_links_to_findings=False, exclude_findings_in_changed_code=False, fetch_existing_findings=False, fetch_all_findings=False, fetch_existing_findings_in_changes=False, fail_on_red_findings=False, log_to_stderr=False, file_encoding=DEFAULT_FILE_ENCODING, - ignore_subrepositories=False): + ignore_subrepositories=False, override_branch=DEFAULT_OVERRIDE_BRANCH): """Constructor""" self.teamscale_client = TeamscaleClient(teamscale_config.url, teamscale_config.username, teamscale_config.access_token, teamscale_config.project_id, verify) @@ -61,6 +62,7 @@ def __init__(self, teamscale_config, repository_path, path_prefix=DEFAULT_PATH_P self.parent_commit_timestamp = 0 self.file_encoding = file_encoding self.ignore_subrepositories = ignore_subrepositories + self.override_branch = override_branch def run(self): """Performs the precommit analysis. Depending on the modifications made and the flags provided to the client, @@ -97,7 +99,10 @@ def _calculate_modifications(self): def _retrieve_current_branch(self): """Retrieves the current branch from the repository.""" - self.current_branch = get_current_branch(self.repository_path) + if self.override_branch: + self.current_branch = self.override_branch + else: + self.current_branch = get_current_branch(self.repository_path) def _retrieve_parent_commit_timestamp(self): """Retrieves the commit timestamp from the repository.""" @@ -324,6 +329,9 @@ def _parse_args(): '(git submodules) in the current repository when determining which files changed. This ' 'affects the files considered for precommit analysis. It does not affect the retrieval ' 'of "existing" findings from the Teamscale server.') + parser.add_argument('--override-branch', metavar='override-branch', type=str, default=DEFAULT_OVERRIDE_BRANCH, + help='Override branch name to be used. ' + 'Allows to use local branches that are not known to Teamscale server.') return parser.parse_args() @@ -352,7 +360,8 @@ def _configure_precommit_client(parsed_args): fail_on_red_findings=parsed_args.fail_on_red_findings, log_to_stderr=parsed_args.log_to_stderr, file_encoding=parsed_args.file_encoding, - ignore_subrepositories=parsed_args.ignore_subrepositories) + ignore_subrepositories=parsed_args.ignore_subrepositories, + override_branch=parsed_args.override_branch) def run():