-
Notifications
You must be signed in to change notification settings - Fork 4
fix: resolve dependency line for flow-style multiline additional_dependencies (#64) #69
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,7 +158,8 @@ def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, PreC | |
| original_lines = self.original_file_lines | ||
| updated_lines = original_lines[:] | ||
|
|
||
| for repo_rev in self.yaml["repos"]: | ||
| repos_list = self.yaml["repos"] | ||
| for repo_idx, repo_rev in enumerate(repos_list): | ||
| if "rev" not in repo_rev: | ||
| continue | ||
|
|
||
|
|
@@ -179,6 +180,12 @@ def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, PreC | |
| original_rev_line: str = updated_lines[rev_line_idx] | ||
| updated_lines[rev_line_idx] = original_rev_line.replace(str(rev), updated_repo.rev) | ||
|
|
||
| if repo_idx + 1 < len(repos_list): | ||
| next_repo_start_line = repos_list[repo_idx + 1]["repo"].start_line + self.document_start_offset | ||
| repo_end_idx = next_repo_start_line - 2 | ||
| else: | ||
| repo_end_idx = len(updated_lines) - 1 | ||
|
|
||
| for src_hook, old_hook, new_hook in zip(hooks, normalized_repo.hooks, updated_repo.hooks): | ||
| if new_hook == old_hook: | ||
| continue | ||
|
|
@@ -191,6 +198,15 @@ def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, PreC | |
| continue | ||
| dep_line_number: int = src_dep.end_line + self.document_start_offset | ||
| dep_line_idx: int = dep_line_number - 1 | ||
| old_dep_str = str(old_dep) | ||
| if dep_line_idx >= len(updated_lines) or old_dep_str not in updated_lines[dep_line_idx]: | ||
| search_start = rev_line_idx | ||
| search_end = min(repo_end_idx + 1, len(updated_lines)) | ||
| candidates = [ | ||
| idx for idx in range(search_start, search_end) if old_dep_str in updated_lines[idx] | ||
| ] | ||
| if candidates: | ||
| dep_line_idx = min(candidates, key=lambda idx: abs(idx - dep_line_idx)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fallback search crash when no candidates foundLow Severity When the fallback search at line 202 triggers because |
||
| original_dep_line: str = updated_lines[dep_line_idx] | ||
| updated_lines[dep_line_idx] = original_dep_line.replace(str(src_dep), new_dep) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
|
|
||
| # Many unused lines before document separator | ||
|
|
||
| --- | ||
| default_language_version: | ||
| python: python3.11 | ||
|
|
||
| repos: | ||
|
|
||
| - repo: https://github.com/pre-commit/mirrors-mypy | ||
| # Some comment | ||
| rev: v1.5.0 | ||
| hooks: | ||
| - id: mypy | ||
| additional_dependencies: [ | ||
| types-PyYAML==1.2.4, | ||
| types-requests==3.4.5, | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
|
|
||
| # Many unused lines before document separator | ||
|
|
||
| --- | ||
| default_language_version: | ||
| python: python3.11 | ||
|
|
||
| repos: | ||
|
|
||
| - repo: https://github.com/pre-commit/mirrors-mypy | ||
| # Some comment | ||
| rev: v1.0.0 | ||
| hooks: | ||
| - id: mypy | ||
| additional_dependencies: [ | ||
| types-PyYAML==1.2.4, | ||
| types-requests, | ||
| ] |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Substring search in fallback matches wrong dependency line
Medium Severity
The fallback candidate search uses
old_dep_str in updated_lines[idx](substring containment) to find the correct line for a dependency. When one dependency name is a prefix of another in the same hook (e.g.,types-requestsandtypes-requests-oauthlib), this matches multiple lines. The closest-to-dep_line_idxheuristic then picks the wrong line (the one nearest the]closing bracket), and the subsequentreplacecall corrupts that unrelated dependency line. Before this change, the code would silently no-op on the wrong line; now it actively corrupts a different dependency.