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/clientlib.py at master · ashanbrown/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 }}
ashanbrown
/
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
master
Breadcrumbs
pre-commit
/
pre_commit
/
clientlib.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
268 lines (224 loc) · 7.44 KB
master
Breadcrumbs
pre-commit
/
pre_commit
/
clientlib.py
Copy path
Top
File metadata and controls
Code
Blame
268 lines (224 loc) · 7.44 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
from
__future__
import
absolute_import
from
__future__
import
unicode_literals
import
argparse
import
functools
import
pipes
import
sys
import
cfgv
from
aspy
.
yaml
import
ordered_load
from
identify
.
identify
import
ALL_TAGS
import
pre_commit
.
constants
as
C
from
pre_commit
.
error_handler
import
FatalError
from
pre_commit
.
languages
.
all
import
all_languages
def
check_type_tag
(
tag
):
if
tag
not
in
ALL_TAGS
:
raise
cfgv
.
ValidationError
(
'Type tag {!r} is not recognized. '
'Try upgrading identify and pre-commit?'
.
format
(
tag
),
)
def
_make_argparser
(
filenames_help
):
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
'filenames'
,
nargs
=
'*'
,
help
=
filenames_help
)
parser
.
add_argument
(
'-V'
,
'--version'
,
action
=
'version'
,
version
=
C
.
VERSION
)
return
parser
MANIFEST_HOOK_DICT
=
cfgv
.
Map
(
'Hook'
,
'id'
,
cfgv
.
Required
(
'id'
,
cfgv
.
check_string
),
cfgv
.
Required
(
'name'
,
cfgv
.
check_string
),
cfgv
.
Required
(
'entry'
,
cfgv
.
check_string
),
cfgv
.
Required
(
'language'
,
cfgv
.
check_one_of
(
all_languages
)),
cfgv
.
Optional
(
'alias'
,
cfgv
.
check_string
,
''
),
cfgv
.
Optional
(
'files'
,
cfgv
.
check_and
(
cfgv
.
check_string
,
cfgv
.
check_regex
),
''
,
),
cfgv
.
Optional
(
'exclude'
,
cfgv
.
check_and
(
cfgv
.
check_string
,
cfgv
.
check_regex
),
'^$'
,
),
cfgv
.
Optional
(
'types'
,
cfgv
.
check_array
(
check_type_tag
), [
'file'
]),
cfgv
.
Optional
(
'exclude_types'
,
cfgv
.
check_array
(
check_type_tag
), []),
cfgv
.
Optional
(
'additional_dependencies'
,
cfgv
.
check_array
(
cfgv
.
check_string
), [],
),
cfgv
.
Optional
(
'args'
,
cfgv
.
check_array
(
cfgv
.
check_string
), []),
cfgv
.
Optional
(
'always_run'
,
cfgv
.
check_bool
,
False
),
cfgv
.
Optional
(
'pass_filenames'
,
cfgv
.
check_bool
,
True
),
cfgv
.
Optional
(
'description'
,
cfgv
.
check_string
,
''
),
cfgv
.
Optional
(
'language_version'
,
cfgv
.
check_string
,
C
.
DEFAULT
),
cfgv
.
Optional
(
'log_file'
,
cfgv
.
check_string
,
''
),
cfgv
.
Optional
(
'minimum_pre_commit_version'
,
cfgv
.
check_string
,
'0'
),
cfgv
.
Optional
(
'require_serial'
,
cfgv
.
check_bool
,
False
),
cfgv
.
Optional
(
'stages'
,
cfgv
.
check_array
(
cfgv
.
check_one_of
(
C
.
STAGES
)), []),
cfgv
.
Optional
(
'verbose'
,
cfgv
.
check_bool
,
False
),
)
MANIFEST_SCHEMA
=
cfgv
.
Array
(
MANIFEST_HOOK_DICT
)
class
InvalidManifestError
(
FatalError
):
pass
load_manifest
=
functools
.
partial
(
cfgv
.
load_from_filename
,
schema
=
MANIFEST_SCHEMA
,
load_strategy
=
ordered_load
,
exc_tp
=
InvalidManifestError
,
)
def
validate_manifest_main
(
argv
=
None
):
parser
=
_make_argparser
(
'Manifest filenames.'
)
args
=
parser
.
parse_args
(
argv
)
ret
=
0
for
filename
in
args
.
filenames
:
try
:
load_manifest
(
filename
)
except
InvalidManifestError
as
e
:
print
(
e
)
ret
=
1
return
ret
LOCAL
=
'local'
META
=
'meta'
class
MigrateShaToRev
(
object
):
@
staticmethod
def
_cond
(
key
):
return
cfgv
.
Conditional
(
key
,
cfgv
.
check_string
,
condition_key
=
'repo'
,
condition_value
=
cfgv
.
NotIn
(
LOCAL
,
META
),
ensure_absent
=
True
,
)
def
check
(
self
,
dct
):
if
dct
.
get
(
'repo'
)
in
{
LOCAL
,
META
}:
self
.
_cond
(
'rev'
).
check
(
dct
)
self
.
_cond
(
'sha'
).
check
(
dct
)
elif
'sha'
in
dct
and
'rev'
in
dct
:
raise
cfgv
.
ValidationError
(
'Cannot specify both sha and rev'
)
elif
'sha'
in
dct
:
self
.
_cond
(
'sha'
).
check
(
dct
)
else
:
self
.
_cond
(
'rev'
).
check
(
dct
)
def
apply_default
(
self
,
dct
):
if
'sha'
in
dct
:
dct
[
'rev'
]
=
dct
.
pop
(
'sha'
)
def
remove_default
(
self
,
dct
):
pass
def
_entry
(
modname
):
"""the hook `entry` is passed through `shlex.split()` by the command
runner, so to prevent issues with spaces and backslashes (on Windows)
it must be quoted here.
"""
return
'{} -m pre_commit.meta_hooks.{}'
.
format
(
pipes
.
quote
(
sys
.
executable
),
modname
,
)
_meta
=
(
(
'check-hooks-apply'
, (
(
'name'
,
'Check hooks apply to the repository'
),
(
'files'
,
C
.
CONFIG_FILE
),
(
'entry'
,
_entry
(
'check_hooks_apply'
)),
),
),
(
'check-useless-excludes'
, (
(
'name'
,
'Check for useless excludes'
),
(
'files'
,
C
.
CONFIG_FILE
),
(
'entry'
,
_entry
(
'check_useless_excludes'
)),
),
),
(
'identity'
, (
(
'name'
,
'identity'
),
(
'verbose'
,
True
),
(
'entry'
,
_entry
(
'identity'
)),
),
),
)
META_HOOK_DICT
=
cfgv
.
Map
(
'Hook'
,
'id'
,
cfgv
.
Required
(
'id'
,
cfgv
.
check_string
),
cfgv
.
Required
(
'id'
,
cfgv
.
check_one_of
(
tuple
(
k
for
k
,
_
in
_meta
))),
# language must be system
cfgv
.
Optional
(
'language'
,
cfgv
.
check_one_of
({
'system'
}),
'system'
),
*
([
# default to the hook definition for the meta hooks
cfgv
.
ConditionalOptional
(
key
,
cfgv
.
check_any
,
value
,
'id'
,
hook_id
)
for
hook_id
,
values
in
_meta
for
key
,
value
in
values
]
+
[
# default to the "manifest" parsing
cfgv
.
OptionalNoDefault
(
item
.
key
,
item
.
check_fn
)
# these will always be defaulted above
if
item
.
key
in
{
'name'
,
'language'
,
'entry'
}
else
item
for
item
in
MANIFEST_HOOK_DICT
.
items
])
)
CONFIG_HOOK_DICT
=
cfgv
.
Map
(
'Hook'
,
'id'
,
cfgv
.
Required
(
'id'
,
cfgv
.
check_string
),
# All keys in manifest hook dict are valid in a config hook dict, but
# are optional.
# No defaults are provided here as the config is merged on top of the
# manifest.
*
[
cfgv
.
OptionalNoDefault
(
item
.
key
,
item
.
check_fn
)
for
item
in
MANIFEST_HOOK_DICT
.
items
if
item
.
key
!=
'id'
]
)
CONFIG_REPO_DICT
=
cfgv
.
Map
(
'Repository'
,
'repo'
,
cfgv
.
Required
(
'repo'
,
cfgv
.
check_string
),
cfgv
.
ConditionalRecurse
(
'hooks'
,
cfgv
.
Array
(
CONFIG_HOOK_DICT
),
'repo'
,
cfgv
.
NotIn
(
LOCAL
,
META
),
),
cfgv
.
ConditionalRecurse
(
'hooks'
,
cfgv
.
Array
(
MANIFEST_HOOK_DICT
),
'repo'
,
LOCAL
,
),
cfgv
.
ConditionalRecurse
(
'hooks'
,
cfgv
.
Array
(
META_HOOK_DICT
),
'repo'
,
META
,
),
MigrateShaToRev
(),
)
DEFAULT_LANGUAGE_VERSION
=
cfgv
.
Map
(
'DefaultLanguageVersion'
,
None
,
cfgv
.
NoAdditionalKeys
(
all_languages
),
*
[
cfgv
.
Optional
(
x
,
cfgv
.
check_string
,
C
.
DEFAULT
)
for
x
in
all_languages
]
)
CONFIG_SCHEMA
=
cfgv
.
Map
(
'Config'
,
None
,
cfgv
.
RequiredRecurse
(
'repos'
,
cfgv
.
Array
(
CONFIG_REPO_DICT
)),
cfgv
.
OptionalRecurse
(
'default_language_version'
,
DEFAULT_LANGUAGE_VERSION
, {},
),
cfgv
.
Optional
(
'default_stages'
,
cfgv
.
check_array
(
cfgv
.
check_one_of
(
C
.
STAGES
)),
C
.
STAGES
,
),
cfgv
.
Optional
(
'exclude'
,
cfgv
.
check_regex
,
'^$'
),
cfgv
.
Optional
(
'fail_fast'
,
cfgv
.
check_bool
,
False
),
)
class
InvalidConfigError
(
FatalError
):
pass
def
ordered_load_normalize_legacy_config
(
contents
):
data
=
ordered_load
(
contents
)
if
isinstance
(
data
,
list
):
# TODO: Once happy, issue a deprecation warning and instructions
return
{
'repos'
:
data
}
else
:
return
data
load_config
=
functools
.
partial
(
cfgv
.
load_from_filename
,
schema
=
CONFIG_SCHEMA
,
load_strategy
=
ordered_load_normalize_legacy_config
,
exc_tp
=
InvalidConfigError
,
)
def
validate_config_main
(
argv
=
None
):
parser
=
_make_argparser
(
'Config filenames.'
)
args
=
parser
.
parse_args
(
argv
)
ret
=
0
for
filename
in
args
.
filenames
:
try
:
load_config
(
filename
)
except
InvalidConfigError
as
e
:
print
(
e
)
ret
=
1
return
ret
You can’t perform that action at this time.