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/run.py at master · bpicolo/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 }}
bpicolo
/
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
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
pre-commit
/
pre_commit
/
commands
/
run.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
227 lines (177 loc) · 6.71 KB
master
Breadcrumbs
pre-commit
/
pre_commit
/
commands
/
run.py
Copy path
Top
File metadata and controls
Code
Blame
227 lines (177 loc) · 6.71 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
from
__future__
import
print_function
from
__future__
import
unicode_literals
import
logging
import
os
import
sys
from
pre_commit
import
color
from
pre_commit
import
git
from
pre_commit
import
output
from
pre_commit
.
output
import
get_hook_message
from
pre_commit
.
staged_files_only
import
staged_files_only
from
pre_commit
.
util
import
cmd_output
from
pre_commit
.
util
import
noop_context
logger
=
logging
.
getLogger
(
'pre_commit'
)
def
_get_skips
(
environ
):
skips
=
environ
.
get
(
'SKIP'
,
''
)
return
set
(
skip
.
strip
()
for
skip
in
skips
.
split
(
','
)
if
skip
.
strip
())
def
_hook_msg_start
(
hook
,
verbose
):
return
'{}{}'
.
format
(
'[{}] '
.
format
(
hook
[
'id'
])
if
verbose
else
''
,
hook
[
'name'
],
)
def
get_changed_files
(
new
,
old
):
return
cmd_output
(
'git'
,
'diff'
,
'--name-only'
,
'{}...{}'
.
format
(
old
,
new
),
)[
1
].
splitlines
()
def
get_filenames
(
args
,
include_expr
,
exclude_expr
):
if
args
.
origin
and
args
.
source
:
getter
=
git
.
get_files_matching
(
lambda
:
get_changed_files
(
args
.
origin
,
args
.
source
),
)
elif
args
.
files
:
getter
=
git
.
get_files_matching
(
lambda
:
args
.
files
)
elif
args
.
all_files
:
getter
=
git
.
get_all_files_matching
elif
git
.
is_in_merge_conflict
():
getter
=
git
.
get_conflicted_files_matching
else
:
getter
=
git
.
get_staged_files_matching
return
getter
(
include_expr
,
exclude_expr
)
SKIPPED
=
'Skipped'
NO_FILES
=
'(no files to check)'
def
_run_single_hook
(
hook
,
repo
,
args
,
skips
,
cols
):
filenames
=
get_filenames
(
args
,
hook
[
'files'
],
hook
[
'exclude'
])
if
hook
[
'id'
]
in
skips
:
output
.
write
(
get_hook_message
(
_hook_msg_start
(
hook
,
args
.
verbose
),
end_msg
=
SKIPPED
,
end_color
=
color
.
YELLOW
,
use_color
=
args
.
color
,
cols
=
cols
,
))
return
0
elif
not
filenames
and
not
hook
[
'always_run'
]:
output
.
write
(
get_hook_message
(
_hook_msg_start
(
hook
,
args
.
verbose
),
postfix
=
NO_FILES
,
end_msg
=
SKIPPED
,
end_color
=
color
.
TURQUOISE
,
use_color
=
args
.
color
,
cols
=
cols
,
))
return
0
# Print the hook and the dots first in case the hook takes hella long to
# run.
output
.
write
(
get_hook_message
(
_hook_msg_start
(
hook
,
args
.
verbose
),
end_len
=
6
,
cols
=
cols
,
))
sys
.
stdout
.
flush
()
diff_before
=
cmd_output
(
'git'
,
'diff'
,
retcode
=
None
,
encoding
=
None
)
retcode
,
stdout
,
stderr
=
repo
.
run_hook
(
hook
,
tuple
(
filenames
))
diff_after
=
cmd_output
(
'git'
,
'diff'
,
retcode
=
None
,
encoding
=
None
)
file_modifications
=
diff_before
!=
diff_after
# If the hook makes changes, fail the commit
if
file_modifications
:
retcode
=
1
if
retcode
:
retcode
=
1
print_color
=
color
.
RED
pass_fail
=
'Failed'
else
:
retcode
=
0
print_color
=
color
.
GREEN
pass_fail
=
'Passed'
output
.
write_line
(
color
.
format_color
(
pass_fail
,
print_color
,
args
.
color
))
if
(
stdout
or
stderr
or
file_modifications
)
and
(
retcode
or
args
.
verbose
):
output
.
write_line
(
'hookid: {}
\n
'
.
format
(
hook
[
'id'
]))
# Print a message if failing due to file modifications
if
file_modifications
:
output
.
write
(
'Files were modified by this hook.'
)
if
stdout
or
stderr
:
output
.
write_line
(
' Additional output:'
)
output
.
write_line
()
for
out
in
(
stdout
,
stderr
):
assert
type
(
out
)
is
bytes
,
type
(
out
)
if
out
.
strip
():
output
.
write_line
(
out
.
strip
())
output
.
write_line
()
return
retcode
def
_compute_cols
(
hooks
,
verbose
):
"""Compute the number of columns to display hook messages. The widest
that will be displayed is in the no files skipped case:
Hook name...(no files to check) Skipped
or in the verbose case
Hook name [hookid]...(no files to check) Skipped
"""
if
hooks
:
name_len
=
max
(
len
(
_hook_msg_start
(
hook
,
verbose
))
for
hook
in
hooks
)
else
:
name_len
=
0
cols
=
name_len
+
3
+
len
(
NO_FILES
)
+
1
+
len
(
SKIPPED
)
return
max
(
cols
,
80
)
def
_run_hooks
(
repo_hooks
,
args
,
environ
):
"""Actually run the hooks."""
skips
=
_get_skips
(
environ
)
cols
=
_compute_cols
([
hook
for
_
,
hook
in
repo_hooks
],
args
.
verbose
)
retval
=
0
for
repo
,
hook
in
repo_hooks
:
retval
|=
_run_single_hook
(
hook
,
repo
,
args
,
skips
,
cols
)
return
retval
def
get_repo_hooks
(
runner
):
for
repo
in
runner
.
repositories
:
for
_
,
hook
in
repo
.
hooks
:
yield
(
repo
,
hook
)
def
_has_unmerged_paths
(
runner
):
_
,
stdout
,
_
=
runner
.
cmd_runner
.
run
([
'git'
,
'ls-files'
,
'--unmerged'
])
return
bool
(
stdout
.
strip
())
def
_has_unstaged_config
(
runner
):
retcode
,
_
,
_
=
runner
.
cmd_runner
.
run
(
(
'git'
,
'diff'
,
'--exit-code'
,
runner
.
config_file_path
),
retcode
=
None
,
)
# be explicit, other git errors don't mean it has an unstaged config.
return
retcode
==
1
def
run
(
runner
,
args
,
environ
=
os
.
environ
):
no_stash
=
args
.
no_stash
or
args
.
all_files
or
bool
(
args
.
files
)
# Check if we have unresolved merge conflict files and fail fast.
if
_has_unmerged_paths
(
runner
):
logger
.
error
(
'Unmerged files. Resolve before committing.'
)
return
1
if
bool
(
args
.
source
)
!=
bool
(
args
.
origin
):
logger
.
error
(
'Specify both --origin and --source.'
)
return
1
if
_has_unstaged_config
(
runner
)
and
not
no_stash
:
if
args
.
allow_unstaged_config
:
logger
.
warn
(
'You have an unstaged config file and have specified the '
'--allow-unstaged-config option.
\n
'
'Note that your config will be stashed before the config is '
'parsed unless --no-stash is specified.'
,
)
else
:
logger
.
error
(
'Your .pre-commit-config.yaml is unstaged.
\n
'
'`git add .pre-commit-config.yaml` to fix this.
\n
'
'Run pre-commit with --allow-unstaged-config to silence this.'
)
return
1
if
no_stash
:
ctx
=
noop_context
()
else
:
ctx
=
staged_files_only
(
runner
.
cmd_runner
)
with
ctx
:
repo_hooks
=
list
(
get_repo_hooks
(
runner
))
if
args
.
hook
:
repo_hooks
=
[
(
repo
,
hook
)
for
repo
,
hook
in
repo_hooks
if
hook
[
'id'
]
==
args
.
hook
]
if
not
repo_hooks
:
output
.
write_line
(
'No hook with id `{}`'
.
format
(
args
.
hook
))
return
1
# Filter hooks for stages
repo_hooks
=
[
(
repo
,
hook
)
for
repo
,
hook
in
repo_hooks
if
not
hook
[
'stages'
]
or
args
.
hook_stage
in
hook
[
'stages'
]
]
return
_run_hooks
(
repo_hooks
,
args
,
environ
)
You can’t perform that action at this time.