-
Notifications
You must be signed in to change notification settings - Fork 68
CM-72985 Add scan local-diff command for IDE plugin integration #550
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
Open
aaron-butler-cy-int
wants to merge
10
commits into
cycodehq:main
Choose a base branch
from
aaron-butler-cy-int:CM-72985---Add-new-local-diff-command-to-support-IDE-plugins
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cc09977
CM-72985 Add scan local-diff command for IDE plugin integration
aaron-butler-cy-int e56fca3
CM-72985 Fix ruff formatting in local-diff scanner and tests
aaron-butler-cy-int 6824afe
CM-72985 Fix Windows-only test failures in local-diff tests
aaron-butler-cy-int 63b9c1c
CM-72985 Use real chdir instead of mocking os.getcwd in local-diff tests
aaron-butler-cy-int 5b89fd1
CM-72985 chdir back before temp dir cleanup in local-diff tests
aaron-butler-cy-int eb02d0a
Merge branch 'main' into CM-72985---Add-new-local-diff-command-to-sup…
aaron-butler-cy-int 2d035ce
CM-72985 Fold local-diff into pre-commit per PO feedback
aaron-butler-cy-int 8c820d7
Merge branch 'main' into CM-72985---Add-new-local-diff-command-to-sup…
aaron-butler-cy-int 0251e5b
CM-72985 Address PR review feedback on pre-commit diff collection
aaron-butler-cy-int 1d8d2a9
CM-72985 Fix Windows CI failure: realpath the --path containment check
aaron-butler-cy-int File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,100 @@ | ||
| import os | ||
| from pathlib import Path | ||
| from typing import Annotated, Optional | ||
|
|
||
| import typer | ||
|
|
||
| from cycode.cli import consts | ||
| from cycode.cli.apps.scan.commit_range_scanner import scan_pre_commit | ||
| from cycode.cli.exceptions.custom_exceptions import ScanPathOutsideRepositoryError, UnresolvedGitRefError | ||
| from cycode.cli.exceptions.handle_scan_errors import handle_scan_exception | ||
| from cycode.cli.logger import logger | ||
| from cycode.cli.utils.git_proxy import git_proxy | ||
|
|
||
|
|
||
| def _resolve_repo_root(cwd: str) -> str: | ||
| """Resolve the repository root from `cwd`, which may be any subdirectory of the repo. | ||
|
|
||
| `git_proxy.get_repo()` requires an exact match (the root or a `.git` dir) unless told to | ||
| search parent directories, so without this, running the command from anywhere but the repo | ||
| root raises a misleading "not a git repository" error. The git pre-commit hook framework | ||
| always invokes from the repo root, so this is a no-op there; it matters for IDE-style | ||
| invocations (--include-unstaged etc.), which may run from any subdirectory. | ||
| """ | ||
| repo = git_proxy.get_repo(cwd, search_parent_directories=True) | ||
| return repo.working_tree_dir or cwd | ||
|
|
||
|
|
||
| def _validate_base_ref(repo_path: str, base_ref: str) -> None: | ||
| """Raise a clear, user-facing error for an unresolvable `--base-ref`. | ||
|
|
||
| A repository with no commits at all is a valid state (the diff falls back to comparing | ||
| against the empty tree), so validation is skipped in that case. | ||
| """ | ||
| repo = git_proxy.get_repo(repo_path) | ||
|
|
||
| try: | ||
| repo.rev_parse(consts.GIT_HEAD_COMMIT_REV) | ||
| except Exception as e: | ||
| logger.debug('Repository has no commits yet; skipping --base-ref validation', exc_info=e) | ||
| return | ||
|
|
||
| try: | ||
| repo.commit(base_ref) | ||
| except Exception as e: | ||
| raise UnresolvedGitRefError(base_ref) from e | ||
|
|
||
|
|
||
| def pre_commit_command( | ||
| ctx: typer.Context, | ||
| _: Annotated[Optional[list[str]], typer.Argument(help='Ignored arguments', hidden=True)] = None, | ||
| base_ref: Annotated[ | ||
| str, | ||
| typer.Option( | ||
| '--base-ref', | ||
| '-b', | ||
| help='Git ref (commit, branch, or tag) to diff against. Defaults to HEAD, matching the ' | ||
| 'pre-commit hook behavior. Combine with --include-unstaged for IDE-style local diff scanning.', | ||
| ), | ||
| ] = consts.GIT_HEAD_COMMIT_REV, | ||
| include_unstaged: Annotated[ | ||
| bool, | ||
| typer.Option( | ||
| '--include-unstaged', | ||
| help='Also scan unstaged changes to tracked files, not just what is staged. ' | ||
| 'Off by default, so the pre-commit hook flow is unaffected.', | ||
| ), | ||
| ] = False, | ||
| paths: Annotated[ | ||
| Optional[list[Path]], | ||
| typer.Option( | ||
| '--path', | ||
| help='Optional paths to scope the diff scan to (e.g. the file currently open in an IDE). ' | ||
| 'Defaults to the entire working directory. Repeatable.', | ||
| show_default=False, | ||
| resolve_path=True, | ||
| ), | ||
| ] = None, | ||
| ) -> None: | ||
| repo_path = os.getcwd() # change locally for easy testing | ||
| try: | ||
| repo_path = _resolve_repo_root(os.getcwd()) | ||
| _validate_base_ref(repo_path, base_ref) | ||
| str_paths = [str(path) for path in paths] if paths else None | ||
| # realpath both sides: repo_path (from GitPython) and str_path (from Click's | ||
| # resolve_path=True) can disagree on 8.3 short-name vs long-name form on Windows, | ||
| # which would otherwise make an in-repo path look like it's outside the repo. | ||
| repo_path_real = os.path.realpath(repo_path) | ||
| for str_path in str_paths or []: | ||
| if os.path.commonpath([repo_path_real, os.path.realpath(str_path)]) != repo_path_real: | ||
| raise ScanPathOutsideRepositoryError(str_path, repo_path) | ||
| except Exception as e: | ||
| handle_scan_exception(ctx, e) | ||
| return | ||
|
|
||
| progress_bar = ctx.obj['progress_bar'] | ||
| progress_bar.start() | ||
|
|
||
| scan_pre_commit(ctx, repo_path) | ||
| try: | ||
| scan_pre_commit(ctx, repo_path, base_ref=base_ref, include_unstaged=include_unstaged, paths=str_paths) | ||
| except Exception as e: | ||
| handle_scan_exception(ctx, e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.