Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
pre-commit/pre_commit/commands/autoupdate.py at allow_ci_key · precommit/pre-commit · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
precommit
/
pre-commit
Public
forked from
pre-commit/pre-commit
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
allow_ci_key
Breadcrumbs
pre-commit
/
pre_commit
/
commands
/
autoupdate.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
184 lines (154 loc) · 5.81 KB
allow_ci_key
Breadcrumbs
pre-commit
/
pre_commit
/
commands
/
autoupdate.py
Copy path
Top
File metadata and controls
Code
Blame
184 lines (154 loc) · 5.81 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import
os
.
path
import
re
from
typing
import
Any
from
typing
import
Dict
from
typing
import
List
from
typing
import
NamedTuple
from
typing
import
Optional
from
typing
import
Sequence
from
typing
import
Tuple
import
pre_commit
.
constants
as
C
from
pre_commit
import
git
from
pre_commit
import
output
from
pre_commit
.
clientlib
import
InvalidManifestError
from
pre_commit
.
clientlib
import
load_config
from
pre_commit
.
clientlib
import
load_manifest
from
pre_commit
.
clientlib
import
LOCAL
from
pre_commit
.
clientlib
import
META
from
pre_commit
.
commands
.
migrate_config
import
migrate_config
from
pre_commit
.
store
import
Store
from
pre_commit
.
util
import
CalledProcessError
from
pre_commit
.
util
import
cmd_output
from
pre_commit
.
util
import
cmd_output_b
from
pre_commit
.
util
import
tmpdir
from
pre_commit
.
util
import
yaml_dump
from
pre_commit
.
util
import
yaml_load
class
RevInfo
(
NamedTuple
):
repo
:
str
rev
:
str
frozen
:
Optional
[
str
]
@
classmethod
def
from_config
(
cls
,
config
:
Dict
[
str
,
Any
])
->
'RevInfo'
:
return
cls
(
config
[
'repo'
],
config
[
'rev'
],
None
)
def
update
(
self
,
tags_only
:
bool
,
freeze
:
bool
)
->
'RevInfo'
:
if
tags_only
:
tag_cmd
=
(
'git'
,
'describe'
,
'FETCH_HEAD'
,
'--tags'
,
'--abbrev=0'
)
else
:
tag_cmd
=
(
'git'
,
'describe'
,
'FETCH_HEAD'
,
'--tags'
,
'--exact'
)
with
tmpdir
()
as
tmp
:
git
.
init_repo
(
tmp
,
self
.
repo
)
cmd_output_b
(
'git'
,
'fetch'
,
'origin'
,
'HEAD'
,
'--tags'
,
cwd
=
tmp
)
try
:
rev
=
cmd_output
(
*
tag_cmd
,
cwd
=
tmp
)[
1
].
strip
()
except
CalledProcessError
:
cmd
=
(
'git'
,
'rev-parse'
,
'FETCH_HEAD'
)
rev
=
cmd_output
(
*
cmd
,
cwd
=
tmp
)[
1
].
strip
()
frozen
=
None
if
freeze
:
exact
=
cmd_output
(
'git'
,
'rev-parse'
,
rev
,
cwd
=
tmp
)[
1
].
strip
()
if
exact
!=
rev
:
rev
,
frozen
=
exact
,
rev
return
self
.
_replace
(
rev
=
rev
,
frozen
=
frozen
)
class
RepositoryCannotBeUpdatedError
(
RuntimeError
):
pass
def
_check_hooks_still_exist_at_rev
(
repo_config
:
Dict
[
str
,
Any
],
info
:
RevInfo
,
store
:
Store
,
)
->
None
:
try
:
path
=
store
.
clone
(
repo_config
[
'repo'
],
info
.
rev
)
manifest
=
load_manifest
(
os
.
path
.
join
(
path
,
C
.
MANIFEST_FILE
))
except
InvalidManifestError
as
e
:
raise
RepositoryCannotBeUpdatedError
(
str
(
e
))
# See if any of our hooks were deleted with the new commits
hooks
=
{
hook
[
'id'
]
for
hook
in
repo_config
[
'hooks'
]}
hooks_missing
=
hooks
-
{
hook
[
'id'
]
for
hook
in
manifest
}
if
hooks_missing
:
raise
RepositoryCannotBeUpdatedError
(
f'Cannot update because the tip of HEAD is missing these hooks:
\n
'
f'
{
", "
.
join
(
sorted
(
hooks_missing
))
}
'
,
)
REV_LINE_RE
=
re
.
compile
(
r'^(\s+)rev:(\s*)([\'"]?)([^\s#]+)(.*)(\r?\n)$'
,
re
.
DOTALL
,
)
def
_original_lines
(
path
:
str
,
rev_infos
:
List
[
Optional
[
RevInfo
]],
retry
:
bool
=
False
,
)
->
Tuple
[
List
[
str
],
List
[
int
]]:
"""detect `rev:` lines or reformat the file"""
with
open
(
path
,
newline
=
''
)
as
f
:
original
=
f
.
read
()
lines
=
original
.
splitlines
(
True
)
idxs
=
[
i
for
i
,
line
in
enumerate
(
lines
)
if
REV_LINE_RE
.
match
(
line
)]
if
len
(
idxs
)
==
len
(
rev_infos
):
return
lines
,
idxs
elif
retry
:
raise
AssertionError
(
'could not find rev lines'
)
else
:
with
open
(
path
,
'w'
)
as
f
:
f
.
write
(
yaml_dump
(
yaml_load
(
original
)))
return
_original_lines
(
path
,
rev_infos
,
retry
=
True
)
def
_write_new_config
(
path
:
str
,
rev_infos
:
List
[
Optional
[
RevInfo
]])
->
None
:
lines
,
idxs
=
_original_lines
(
path
,
rev_infos
)
for
idx
,
rev_info
in
zip
(
idxs
,
rev_infos
):
if
rev_info
is
None
:
continue
match
=
REV_LINE_RE
.
match
(
lines
[
idx
])
assert
match
is
not
None
new_rev_s
=
yaml_dump
({
'rev'
:
rev_info
.
rev
},
default_style
=
match
[
3
])
new_rev
=
new_rev_s
.
split
(
':'
,
1
)[
1
].
strip
()
if
rev_info
.
frozen
is
not
None
:
comment
=
f' # frozen:
{
rev_info
.
frozen
}
'
elif
match
[
5
].
strip
().
startswith
(
'# frozen:'
):
comment
=
''
else
:
comment
=
match
[
5
]
lines
[
idx
]
=
f'
{
match
[
1
]
}
rev:
{
match
[
2
]
}
{
new_rev
}
{
comment
}
{
match
[
6
]
}
'
with
open
(
path
,
'w'
,
newline
=
''
)
as
f
:
f
.
write
(
''
.
join
(
lines
))
def
autoupdate
(
config_file
:
str
,
store
:
Store
,
tags_only
:
bool
,
freeze
:
bool
,
repos
:
Sequence
[
str
]
=
(),
)
->
int
:
"""Auto-update the pre-commit config to the latest versions of repos."""
migrate_config
(
config_file
,
quiet
=
True
)
retv
=
0
rev_infos
:
List
[
Optional
[
RevInfo
]]
=
[]
changed
=
False
config
=
load_config
(
config_file
)
for
repo_config
in
config
[
'repos'
]:
if
repo_config
[
'repo'
]
in
{
LOCAL
,
META
}:
continue
info
=
RevInfo
.
from_config
(
repo_config
)
if
repos
and
info
.
repo
not
in
repos
:
rev_infos
.
append
(
None
)
continue
output
.
write
(
f'Updating
{
info
.
repo
}
... '
)
new_info
=
info
.
update
(
tags_only
=
tags_only
,
freeze
=
freeze
)
try
:
_check_hooks_still_exist_at_rev
(
repo_config
,
new_info
,
store
)
except
RepositoryCannotBeUpdatedError
as
error
:
output
.
write_line
(
error
.
args
[
0
])
rev_infos
.
append
(
None
)
retv
=
1
continue
if
new_info
.
rev
!=
info
.
rev
:
changed
=
True
if
new_info
.
frozen
:
updated_to
=
f'
{
new_info
.
frozen
}
(frozen)'
else
:
updated_to
=
new_info
.
rev
msg
=
f'updating
{
info
.
rev
}
->
{
updated_to
}
.'
output
.
write_line
(
msg
)
rev_infos
.
append
(
new_info
)
else
:
output
.
write_line
(
'already up to date.'
)
rev_infos
.
append
(
None
)
if
changed
:
_write_new_config
(
config_file
,
rev_infos
)
return
retv
You can’t perform that action at this time.