search you tried in the issue tracker
autoupdate newline, autoupdate comments, autoupdate formatting removed, autoupdate "end of file"
describe your issue
When the rev: line is the last line of the file and the file has no final newline, pre-commit autoupdate drops every comment in the config. REV_LINE_RE in pre_commit/commands/autoupdate.py:116 requires (\r?\n)$, so that line is not counted, the count differs from the number of repos, and _original_lines falls back to yaml_dump(yaml_load(...)).
$ printf '%s' "$CONFIG" > .pre-commit-config.yaml && pre-commit autoupdate && cat -A .pre-commit-config.yaml
[/var/tmp/p9b-L20/pc-repro/hooks] updating v1.0.0 -> v2.0.0
repos:$
- repo: /var/tmp/p9b-L20/pc-repro/hooks$
hooks:$
- id: a$
rev: v2.0.0$
$ printf '%s' "$CONFIG" > .pre-commit-config.yaml && pre-commit autoupdate && cat -A .pre-commit-config.yaml # with the change below
[/var/tmp/p9b-L20/pc-repro/hooks] updating v1.0.0 -> v2.0.0
# keep this comment$
repos:$
- repo: /var/tmp/p9b-L20/pc-repro/hooks$
hooks:$
- id: a # and this one$
rev: v2.0.0
/var/tmp/p9b-L20/pc-repro/hooks is a local git repository with tags v1.0.0 and v2.0.0. The yaml_dump fallback is for configs whose rev: lines can't be found, such as flow style. Here the line is block style and present, and only the missing line ending hides it. Allowing an empty line ending fixes it:
-REV_LINE_RE = re.compile(r'^(\s+)rev:(\s*)([\'"]?)([^\s#]+)(.*)(\r?\n)$')
+REV_LINE_RE = re.compile(r'^(\s+)rev:(\s*)([\'"]?)([^\s#]+)(.*)(\r?\n|)$')
$ python -m pytest tests/commands/autoupdate_test.py -q # 368bf47
33 passed in 4.41s
# with the change above
33 passed in 4.43s
pre-commit --version
pre-commit 4.6.2 (368bf47, CPython 3.12.3, Linux)
.pre-commit-config.yaml
# keep this comment
repos:
- repo: /var/tmp/p9b-L20/pc-repro/hooks
hooks:
- id: a # and this one
rev: v1.0.0
No newline after rev: v1.0.0.
~/.cache/pre-commit/pre-commit.log (if present)
Not present. The command exits 0.
@asottile would you take the one-line change above, with a test for this case?
search you tried in the issue tracker
autoupdate newline, autoupdate comments, autoupdate formatting removed, autoupdate "end of file"
describe your issue
When the rev: line is the last line of the file and the file has no final newline, pre-commit autoupdate drops every comment in the config. REV_LINE_RE in pre_commit/commands/autoupdate.py:116 requires (\r?\n)$, so that line is not counted, the count differs from the number of repos, and _original_lines falls back to yaml_dump(yaml_load(...)).
$ printf '%s' "$CONFIG" > .pre-commit-config.yaml && pre-commit autoupdate && cat -A .pre-commit-config.yaml [/var/tmp/p9b-L20/pc-repro/hooks] updating v1.0.0 -> v2.0.0 repos:$ - repo: /var/tmp/p9b-L20/pc-repro/hooks$ hooks:$ - id: a$ rev: v2.0.0$ $ printf '%s' "$CONFIG" > .pre-commit-config.yaml && pre-commit autoupdate && cat -A .pre-commit-config.yaml # with the change below [/var/tmp/p9b-L20/pc-repro/hooks] updating v1.0.0 -> v2.0.0 # keep this comment$ repos:$ - repo: /var/tmp/p9b-L20/pc-repro/hooks$ hooks:$ - id: a # and this one$ rev: v2.0.0/var/tmp/p9b-L20/pc-repro/hooks is a local git repository with tags v1.0.0 and v2.0.0. The yaml_dump fallback is for configs whose rev: lines can't be found, such as flow style. Here the line is block style and present, and only the missing line ending hides it. Allowing an empty line ending fixes it:
pre-commit --version
pre-commit 4.6.2 (368bf47, CPython 3.12.3, Linux)
.pre-commit-config.yaml
No newline after rev: v1.0.0.
~/.cache/pre-commit/pre-commit.log (if present)
Not present. The command exits 0.
@asottile would you take the one-line change above, with a test for this case?