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

add prev version argument to pre-commit #4177

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/4177.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Add `--prev-version` argument to **pre-commit** command.
type: feature
pr_number: 4177
7 changes: 7 additions & 0 deletions demisto_sdk/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3602,6 +3602,12 @@ def pre_commit(
"-g",
help="Whether to use git to determine which files to run on",
),
prev_version: Optional[str] = typer.Option(
None,
"--prev-version",
help="The previous version to compare against. "
"If not provided, the previous version will be determined using git.",
),
all_files: bool = typer.Option(
False, "--all-files", "-a", help="Whether to run on all files"
),
Expand Down Expand Up @@ -3664,6 +3670,7 @@ def pre_commit(
staged_only,
commited_only,
git_diff,
prev_version,
all_files,
mode,
skip,
Expand Down
4 changes: 2 additions & 2 deletions demisto_sdk/commands/common/git_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ def _get_staged_files(self) -> Set[Path]:
if item
}

def _get_all_changed_files(self, prev_ver: str = "") -> Set[Path]:
def _get_all_changed_files(self, prev_ver: Optional[str] = None) -> Set[Path]:
"""
Get all the files changed in the current branch without status distinction.

Expand Down Expand Up @@ -813,7 +813,7 @@ def find_primary_branch(repo: Repo) -> str:
return DEMISTO_GIT_PRIMARY_BRANCH
return ""

def handle_prev_ver(self, prev_ver: str = ""):
def handle_prev_ver(self, prev_ver: Optional[str] = None):
# check for sha1 in regex
sha1_pattern = re.compile(r"\b[0-9a-f]{40}\b", flags=re.IGNORECASE)
if prev_ver and sha1_pattern.match(prev_ver):
Expand Down
5 changes: 4 additions & 1 deletion demisto_sdk/commands/pre_commit/pre_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def pre_commit_manager(
staged_only: bool = False,
commited_only: bool = False,
git_diff: bool = False,
prev_version: Optional[str] = None,
all_files: bool = False,
mode: str = "",
skip_hooks: Optional[List[str]] = None,
Expand Down Expand Up @@ -449,6 +450,7 @@ def pre_commit_manager(
commited_only=commited_only,
use_git=git_diff,
all_files=all_files,
prev_version=prev_version,
)
if not files_to_run:
logger.info("No files were changed, skipping pre-commit.")
Expand Down Expand Up @@ -492,6 +494,7 @@ def preprocess_files(
commited_only: bool = False,
use_git: bool = False,
all_files: bool = False,
prev_version: Optional[str] = None,
) -> Set[Path]:
git_util = GitUtil()
staged_files = git_util._get_staged_files()
Expand All @@ -501,7 +504,7 @@ def preprocess_files(
elif staged_only:
raw_files = staged_files
elif use_git:
raw_files = git_util._get_all_changed_files()
raw_files = git_util._get_all_changed_files(prev_version)
if not commited_only:
raw_files = raw_files.union(staged_files)
elif all_files:
Expand Down