Skip to content

Commit af870cd

Browse files
committed
fix: resolve dependency line for flow-style multiline additional_dependencies (#64)
1 parent 2921303 commit af870cd

4 files changed

Lines changed: 57 additions & 2 deletions

File tree

src/sync_pre_commit_lock/pre_commit_config.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, PreC
158158
original_lines = self.original_file_lines
159159
updated_lines = original_lines[:]
160160

161-
for repo_rev in self.yaml["repos"]:
161+
repos_list = self.yaml["repos"]
162+
for repo_idx, repo_rev in enumerate(repos_list):
162163
if "rev" not in repo_rev:
163164
continue
164165

@@ -179,6 +180,12 @@ def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, PreC
179180
original_rev_line: str = updated_lines[rev_line_idx]
180181
updated_lines[rev_line_idx] = original_rev_line.replace(str(rev), updated_repo.rev)
181182

183+
if repo_idx + 1 < len(repos_list):
184+
next_repo_start_line = repos_list[repo_idx + 1]["repo"].start_line + self.document_start_offset
185+
repo_end_idx = next_repo_start_line - 2
186+
else:
187+
repo_end_idx = len(updated_lines) - 1
188+
182189
for src_hook, old_hook, new_hook in zip(hooks, normalized_repo.hooks, updated_repo.hooks):
183190
if new_hook == old_hook:
184191
continue
@@ -191,6 +198,15 @@ def update_pre_commit_repo_versions(self, new_versions: dict[PreCommitRepo, PreC
191198
continue
192199
dep_line_number: int = src_dep.end_line + self.document_start_offset
193200
dep_line_idx: int = dep_line_number - 1
201+
old_dep_str = str(old_dep)
202+
if dep_line_idx >= len(updated_lines) or old_dep_str not in updated_lines[dep_line_idx]:
203+
search_start = rev_line_idx
204+
search_end = min(repo_end_idx + 1, len(updated_lines))
205+
candidates = [
206+
idx for idx in range(search_start, search_end) if old_dep_str in updated_lines[idx]
207+
]
208+
if candidates:
209+
dep_line_idx = min(candidates, key=lambda idx: abs(idx - dep_line_idx))
194210
original_dep_line: str = updated_lines[dep_line_idx]
195211
updated_lines[dep_line_idx] = original_dep_line.replace(str(src_dep), new_dep)
196212

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# Many unused lines before document separator
3+
4+
---
5+
default_language_version:
6+
python: python3.11
7+
8+
repos:
9+
10+
- repo: https://github.com/pre-commit/mirrors-mypy
11+
# Some comment
12+
rev: v1.5.0
13+
hooks:
14+
- id: mypy
15+
additional_dependencies: [
16+
types-PyYAML==1.2.4,
17+
types-requests==3.4.5,
18+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
# Many unused lines before document separator
3+
4+
---
5+
default_language_version:
6+
python: python3.11
7+
8+
repos:
9+
10+
- repo: https://github.com/pre-commit/mirrors-mypy
11+
# Some comment
12+
rev: v1.0.0
13+
hooks:
14+
- id: mypy
15+
additional_dependencies: [
16+
types-PyYAML==1.2.4,
17+
types-requests,
18+
]

tests/test_pre_commit_config_file.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ def test_update_versions() -> None:
8787
assert config.pre_commit_config_file_path.open.call_count == 1
8888

8989

90-
@pytest.mark.parametrize("base", ["only-deps", "with-deps", "with-one-liner-deps", "without-new-deps"])
90+
@pytest.mark.parametrize(
91+
"base",
92+
["only-deps", "with-deps", "with-one-liner-deps", "without-new-deps", "flow-multiline-deps"],
93+
)
9194
def test_update_additional_dependencies_versions(base: str) -> None:
9295
config = PreCommitHookConfig.from_yaml_file(FIXTURES / f"pre-commit-config-{base}.yaml")
9396
mock_file = config.pre_commit_config_file_path = MagicMock()

0 commit comments

Comments
 (0)