Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pre-Commit] Continue Even if Fetch Failed #3878

Merged
merged 11 commits into from
Jan 30, 2024
4 changes: 4 additions & 0 deletions .changelog/3878.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
DEMISTO_GIT_UPSTREAM,
PACKS_FOLDER,
)
from demisto_sdk.commands.common.logger import logger
MichaelYochpaz marked this conversation as resolved.
Show resolved Hide resolved


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
Original file line number Diff line number Diff line change
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