Skip to content

Commit

Permalink
[Pre-Commit] Continue Even if Fetch Failed (#3878)
Browse files Browse the repository at this point in the history
* Add `try` and `except` blocks

* Bypass logger circular import issues

* Add release notes

* Import logger

* Update log message

* pre-commit

* Update .changelog/3878.yml

Co-authored-by: Guy Afik <53861351+GuyAfik@users.noreply.github.com>

* Remove lru_cache

* Address review notes

* Minor change

---------

Co-authored-by: Guy Afik <53861351+GuyAfik@users.noreply.github.com>
  • Loading branch information
MichaelYochpaz and GuyAfik committed Jan 30, 2024
1 parent 8c37d00 commit 411b244
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .changelog/3878.yml
@@ -0,0 +1,4 @@
changes:
- 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
26 changes: 19 additions & 7 deletions demisto_sdk/commands/common/git_util.py
Expand Up @@ -20,6 +20,7 @@
DEMISTO_GIT_UPSTREAM,
PACKS_FOLDER,
)
from demisto_sdk.commands.common.logger import logger


class CommitOrBranchNotFoundError(GitError):
Expand Down Expand Up @@ -618,13 +619,25 @@ 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.
"""
self.fetch()
try:
self.fetch()

except Exception as e:
logger.warning(
f"Failed to fetch branch '{self.get_current_working_branch()}' "
f"from remote '{self.repo.remote().name}' ({self.repo.remote().url}). Continuing without fetching."
)
logger.debug(f"Error: {e}")

remote, branch = self.handle_prev_ver(prev_ver)
current_hash = self.get_current_commit_hash()

Expand Down Expand Up @@ -899,7 +912,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.
Expand All @@ -908,7 +922,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(
Expand Down Expand Up @@ -944,11 +958,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()
Expand Down
6 changes: 5 additions & 1 deletion demisto_sdk/commands/pre_commit/pre_commit_command.py
Expand Up @@ -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.")
Expand Down

0 comments on commit 411b244

Please sign in to comment.