Skip to content

Commit

Permalink
add prev version argument to pre-commit (#4177)
Browse files Browse the repository at this point in the history
* add prev version argument to pre-commit

* changelog

* Update .changelog/4177.yml

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

---------

Co-authored-by: Guy Afik <53861351+GuyAfik@users.noreply.github.com>
  • Loading branch information
ilaner and GuyAfik committed Mar 26, 2024
1 parent 84798a8 commit 88373c8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
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

0 comments on commit 88373c8

Please sign in to comment.