-
Notifications
You must be signed in to change notification settings - Fork 61
CM-42771 - Add support of .gitignore files for a file excluding from scans
#272
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3cdb459
CM-42771 - Support `.gitignore` and `.cycodeignore` files for a file …
MarshalX 747950a
fix ruff
MarshalX 51522ba
make test deterministic
MarshalX 8438f2f
fix tests on Windows
MarshalX 1e9de19
perf optimization; code refactor
MarshalX 7cb0f15
fix ignoring; simplify global ignoring patterns
MarshalX 5813c91
add inheriting of ignore patterns
MarshalX 0724e5e
cover walk_ignore generator with tests
MarshalX 7a96e3c
Merge branch 'main' into CM-42771-support-ignore-files
MarshalX 0c6ff01
fix lock after merge with main
MarshalX 6612402
drop poetry cache
MarshalX 1041135
fix CI crashes on Windows (Python 3.12 & 3.13) https://github.com/pyt…
MarshalX 9b9ab86
add ignorelib as is with copyright notice
MarshalX cd0852a
fix and format ignorelib
MarshalX 266bcfe
align codebase of ignorelib with dulwich
MarshalX 85993ff
fix inefficient subfolders filtering
MarshalX 4fbf23b
migrate to ignore_utils; add tests; remove unused pathspec; remove .c…
MarshalX 5c819e3
Merge branch 'main' into CM-42771-support-ignore-files
MarshalX 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,4 +50,4 @@ jobs: | |
| run: poetry install | ||
|
|
||
| - name: Run Tests | ||
| run: poetry run pytest | ||
| run: poetry run python -m pytest | ||
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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import os | ||
| from collections import defaultdict | ||
| from typing import Generator, Iterable, List, Tuple | ||
|
|
||
| import pathspec | ||
| from pathspec.util import StrPath | ||
|
|
||
| from cycode.cli.utils.path_utils import get_file_content | ||
| from cycode.cyclient import logger | ||
|
|
||
| _SUPPORTED_IGNORE_PATTERN_FILES = {'.gitignore', '.cycodeignore'} | ||
| _DEFAULT_GLOBAL_IGNORE_PATTERNS = [ | ||
| '**/.git', | ||
| '**/.cycode', | ||
| ] | ||
|
|
||
|
|
||
| def _walk_to_top(path: str) -> Iterable[str]: | ||
| while os.path.dirname(path) != path: | ||
| yield path | ||
| path = os.path.dirname(path) | ||
|
|
||
| if path: | ||
| yield path # Include the top-level directory | ||
|
|
||
|
|
||
| def _collect_top_level_ignore_files(path: str) -> List[str]: | ||
| ignore_files = [] | ||
| for dir_path in _walk_to_top(path): | ||
| for ignore_file in _SUPPORTED_IGNORE_PATTERN_FILES: | ||
| ignore_file_path = os.path.join(dir_path, ignore_file) | ||
| if os.path.exists(ignore_file_path): | ||
cycode-security[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| logger.debug('Apply top level ignore file: %s', ignore_file_path) | ||
| ignore_files.append(ignore_file_path) | ||
cycode-security[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return ignore_files | ||
|
|
||
|
|
||
| def _get_global_ignore_patterns(path: str) -> List[str]: | ||
| ignore_patterns = _DEFAULT_GLOBAL_IGNORE_PATTERNS.copy() | ||
| for ignore_file in _collect_top_level_ignore_files(path): | ||
| file_patterns = get_file_content(ignore_file).splitlines() | ||
| ignore_patterns.extend(file_patterns) | ||
| return ignore_patterns | ||
|
|
||
|
|
||
| def _should_include_path(ignore_patterns: List[str], path: StrPath) -> bool: | ||
| path_spec = pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, ignore_patterns) | ||
| return not path_spec.match_file(path) # works with both files and directories; negative match | ||
|
|
||
|
|
||
| def walk_ignore(path: str) -> Generator[Tuple[str, List[str], List[str]], None, None]: | ||
| global_ignore_patterns = _get_global_ignore_patterns(path) | ||
| path_to_ignore_patterns = defaultdict(list) | ||
|
|
||
| for dirpath, dirnames, filenames in os.walk(path, topdown=True): | ||
| # finds and processes ignore files first to get the patterns | ||
| for filename in filenames: | ||
| filepath = os.path.join(dirpath, filename) | ||
| if filename in _SUPPORTED_IGNORE_PATTERN_FILES: | ||
| logger.debug('Apply ignore file: %s', filepath) | ||
|
|
||
| parent_dir = os.path.dirname(dirpath) | ||
| if dirpath not in path_to_ignore_patterns and parent_dir in path_to_ignore_patterns: | ||
| # inherit ignore patterns from parent directory on first occurrence | ||
| logger.debug('Inherit ignore patterns: %s', {'inherit_from': parent_dir, 'inherit_to': dirpath}) | ||
| path_to_ignore_patterns[dirpath].extend(path_to_ignore_patterns[parent_dir]) | ||
|
|
||
| # always read ignore patterns for the current directory | ||
| path_to_ignore_patterns[dirpath].extend(get_file_content(filepath).splitlines()) | ||
|
|
||
| ignore_patterns = global_ignore_patterns + path_to_ignore_patterns.get(dirpath, []) | ||
|
|
||
| # decrease recursion depth of os.walk() because of topdown=True by changing the list in-place | ||
| # slicing ([:]) is mandatory to change dict in-place! | ||
| dirnames[:] = [d for d in dirnames if _should_include_path(ignore_patterns, os.path.join(dirpath, d))] | ||
| filenames[:] = [f for f in filenames if _should_include_path(ignore_patterns, os.path.join(dirpath, f))] | ||
|
|
||
| yield dirpath, dirnames, filenames | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Empty file.
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.