From 26b8db3149cee47a9b985a5959cea558d6fb3932 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:05:48 +0200 Subject: [PATCH 01/10] Add `try` and `except` blocks --- demisto_sdk/commands/common/git_util.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/demisto_sdk/commands/common/git_util.py b/demisto_sdk/commands/common/git_util.py index d797d4e0b70..0cca05cae72 100644 --- a/demisto_sdk/commands/common/git_util.py +++ b/demisto_sdk/commands/common/git_util.py @@ -20,6 +20,8 @@ DEMISTO_GIT_UPSTREAM, PACKS_FOLDER, ) +from demisto_sdk.commands.common.logger import logger + class CommitOrBranchNotFoundError(GitError): @@ -624,7 +626,13 @@ def _get_all_changed_files(self, prev_ver: str = "") -> Set[Path]: Returns: Set: of Paths to files changed in the current branch. """ - self.fetch() + try: + self.fetch() + + except Exception as e: + logger.warning("Failed to fetch remote branch. Generating diff without fetching.") + logger.debug(f"Error: {e}") + remote, branch = self.handle_prev_ver(prev_ver) current_hash = self.get_current_commit_hash() From 51a10d293c75eddaca6854daa45d6116f3138802 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:55:10 +0200 Subject: [PATCH 02/10] Bypass logger circular import issues --- demisto_sdk/commands/common/git_util.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/demisto_sdk/commands/common/git_util.py b/demisto_sdk/commands/common/git_util.py index 0cca05cae72..b69629ce59e 100644 --- a/demisto_sdk/commands/common/git_util.py +++ b/demisto_sdk/commands/common/git_util.py @@ -1,3 +1,4 @@ +import logging import os import re from functools import lru_cache @@ -20,7 +21,9 @@ DEMISTO_GIT_UPSTREAM, PACKS_FOLDER, ) -from demisto_sdk.commands.common.logger import logger + + +logger = logging.getLogger("demisto-sdk") From d087dc25ede3006bf9940fb081b1dba81f9ca655 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:48:08 +0200 Subject: [PATCH 03/10] Add release notes --- .changelog/3878.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changelog/3878.yml diff --git a/.changelog/3878.yml b/.changelog/3878.yml new file mode 100644 index 00000000000..c10b71f08b6 --- /dev/null +++ b/.changelog/3878.yml @@ -0,0 +1,6 @@ +changes: +- description: Fixed an issue where the **pre-commit** command would fail if there are connection issues with the git repository. + type: fix +- description: Fixed an issue where if running `git fetch` would fail on `git_util._get_all_changed_files`, an error would be raised. + type: internal +pr_number: 3878 From 5d9e4e6951d9ece33c4f77acb5a3d70fc3842ef6 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:57:36 +0200 Subject: [PATCH 04/10] Import logger --- demisto_sdk/commands/common/git_util.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/demisto_sdk/commands/common/git_util.py b/demisto_sdk/commands/common/git_util.py index b69629ce59e..05643285005 100644 --- a/demisto_sdk/commands/common/git_util.py +++ b/demisto_sdk/commands/common/git_util.py @@ -1,4 +1,3 @@ -import logging import os import re from functools import lru_cache @@ -21,10 +20,7 @@ DEMISTO_GIT_UPSTREAM, PACKS_FOLDER, ) - - -logger = logging.getLogger("demisto-sdk") - +from demisto_sdk.commands.common.logger import logger class CommitOrBranchNotFoundError(GitError): From ded91f03addf4b99e69a9b9013f1d75160388861 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Wed, 17 Jan 2024 17:59:14 +0200 Subject: [PATCH 05/10] Update log message --- demisto_sdk/commands/common/git_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demisto_sdk/commands/common/git_util.py b/demisto_sdk/commands/common/git_util.py index 05643285005..7f5d9e9acd0 100644 --- a/demisto_sdk/commands/common/git_util.py +++ b/demisto_sdk/commands/common/git_util.py @@ -629,7 +629,7 @@ def _get_all_changed_files(self, prev_ver: str = "") -> Set[Path]: self.fetch() except Exception as e: - logger.warning("Failed to fetch remote branch. Generating diff without fetching.") + logger.warning("Failed to fetch remote branch. Continuing without fetching.") logger.debug(f"Error: {e}") remote, branch = self.handle_prev_ver(prev_ver) From 741a85cfaf1d6d2bfb22a11609ba6237393f3d78 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Wed, 17 Jan 2024 20:14:58 +0200 Subject: [PATCH 06/10] pre-commit --- demisto_sdk/commands/common/git_util.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/demisto_sdk/commands/common/git_util.py b/demisto_sdk/commands/common/git_util.py index 7f5d9e9acd0..d065efa6d1b 100644 --- a/demisto_sdk/commands/common/git_util.py +++ b/demisto_sdk/commands/common/git_util.py @@ -629,7 +629,9 @@ def _get_all_changed_files(self, prev_ver: str = "") -> Set[Path]: self.fetch() except Exception as e: - logger.warning("Failed to fetch remote branch. Continuing without fetching.") + logger.warning( + "Failed to fetch remote branch. Continuing without fetching." + ) logger.debug(f"Error: {e}") remote, branch = self.handle_prev_ver(prev_ver) From 8e8f478bd7a9704d2bc12d8a4fc61d06430b3203 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Mon, 29 Jan 2024 15:04:20 +0200 Subject: [PATCH 07/10] Update .changelog/3878.yml Co-authored-by: Guy Afik <53861351+GuyAfik@users.noreply.github.com> --- .changelog/3878.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.changelog/3878.yml b/.changelog/3878.yml index c10b71f08b6..9778e72a8c9 100644 --- a/.changelog/3878.yml +++ b/.changelog/3878.yml @@ -1,6 +1,4 @@ changes: - description: Fixed an issue where the **pre-commit** command would fail if there are connection issues with the git repository. type: fix -- description: Fixed an issue where if running `git fetch` would fail on `git_util._get_all_changed_files`, an error would be raised. - type: internal pr_number: 3878 From bb4145d3b7b5748be2c19bc036fdfa63c7b806a8 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Tue, 30 Jan 2024 11:21:12 +0200 Subject: [PATCH 08/10] Remove lru_cache --- demisto_sdk/commands/common/git_util.py | 14 ++++++++------ .../commands/pre_commit/pre_commit_command.py | 6 +++++- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/demisto_sdk/commands/common/git_util.py b/demisto_sdk/commands/common/git_util.py index d065efa6d1b..1adf571b81f 100644 --- a/demisto_sdk/commands/common/git_util.py +++ b/demisto_sdk/commands/common/git_util.py @@ -619,11 +619,14 @@ def _get_staged_files(self) -> Set[Path]: } def _get_all_changed_files(self, prev_ver: str = "") -> Set[Path]: - """Get all the files changed in the current branch without status distinction. + """ + Get all the files changed in the current branch without status distinction. + Args: prev_ver (str): The base branch against which the comparison is made. + Returns: - Set: of Paths to files changed in the current branch. + Set[Path]: of Paths to files changed in the current branch. """ try: self.fetch() @@ -908,7 +911,8 @@ def get_all_changed_files( debug: bool = False, include_untracked: bool = False, ) -> Set[Path]: - """Get a set of all changed files in the branch (modified, added and renamed) + """ + Get a set of all changed files in the branch (modified, added and renamed) Args: prev_ver (str): The base branch against which the comparison is made. @@ -917,7 +921,7 @@ def get_all_changed_files( debug (bool): Whether to print the debug logs. include_untracked (bool): Whether to include untracked files. Returns: - Set. A set of all the changed files in the given branch when comparing to prev_ver + Set[Path]: A set of all the changed files in the given branch when comparing to prev_ver """ self.fetch() modified_files: Set[Path] = self.modified_files( @@ -953,11 +957,9 @@ def _is_file_git_ignored(self, file_path: str) -> bool: """ return bool(self.repo.ignored(file_path)) - @lru_cache def fetch(self): self.repo.remote().fetch() - @lru_cache def fetch_all(self): for remote in self.repo.remotes: remote.fetch() diff --git a/demisto_sdk/commands/pre_commit/pre_commit_command.py b/demisto_sdk/commands/pre_commit/pre_commit_command.py index c48a33840f0..5d3494c0b8b 100644 --- a/demisto_sdk/commands/pre_commit/pre_commit_command.py +++ b/demisto_sdk/commands/pre_commit/pre_commit_command.py @@ -439,7 +439,11 @@ def pre_commit_manager( git_diff = True files_to_run = preprocess_files( - input_files, staged_only, commited_only, git_diff, all_files + input_files=input_files, + staged_only=staged_only, + commited_only=commited_only, + use_git=git_diff, + all_files=all_files, ) if not files_to_run: logger.info("No files were changed, skipping pre-commit.") From a33518b7fc27334c923a7ae8afa110fc1fda9cb4 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Tue, 30 Jan 2024 13:21:29 +0200 Subject: [PATCH 09/10] Address review notes --- .changelog/3878.yml | 2 +- demisto_sdk/commands/common/git_util.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.changelog/3878.yml b/.changelog/3878.yml index 9778e72a8c9..a87d7e90bfd 100644 --- a/.changelog/3878.yml +++ b/.changelog/3878.yml @@ -1,4 +1,4 @@ changes: -- description: Fixed an issue where the **pre-commit** command would fail if there are connection issues with the git repository. +- description: Fixed an issue where fetching from a git remote would cause a failure in cases where fetching is not necessary. type: fix pr_number: 3878 diff --git a/demisto_sdk/commands/common/git_util.py b/demisto_sdk/commands/common/git_util.py index 1adf571b81f..b6dcd13eada 100644 --- a/demisto_sdk/commands/common/git_util.py +++ b/demisto_sdk/commands/common/git_util.py @@ -633,7 +633,8 @@ def _get_all_changed_files(self, prev_ver: str = "") -> Set[Path]: except Exception as e: logger.warning( - "Failed to fetch remote branch. Continuing without fetching." + f"Failed to fetch branch '{self.get_current_working_branch()}' " + f"from remote '{self.repo.remote()}' ({self.repo.remote().url}). Continuing without fetching." ) logger.debug(f"Error: {e}") From c11e2ef5aa4429ab3ba7215d136031ecbb29bae4 Mon Sep 17 00:00:00 2001 From: Michael Yochpaz <8832013+MichaelYochpaz@users.noreply.github.com> Date: Tue, 30 Jan 2024 14:23:19 +0200 Subject: [PATCH 10/10] Minor change --- demisto_sdk/commands/common/git_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demisto_sdk/commands/common/git_util.py b/demisto_sdk/commands/common/git_util.py index b6dcd13eada..d1e42f6dbd9 100644 --- a/demisto_sdk/commands/common/git_util.py +++ b/demisto_sdk/commands/common/git_util.py @@ -634,7 +634,7 @@ def _get_all_changed_files(self, prev_ver: str = "") -> Set[Path]: except Exception as e: logger.warning( f"Failed to fetch branch '{self.get_current_working_branch()}' " - f"from remote '{self.repo.remote()}' ({self.repo.remote().url}). Continuing without fetching." + f"from remote '{self.repo.remote().name}' ({self.repo.remote().url}). Continuing without fetching." ) logger.debug(f"Error: {e}")