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/repository.py at master · FeodorFitsner/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 }}
FeodorFitsner
/
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
/
repository.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
148 lines (119 loc) · 4.47 KB
master
Breadcrumbs
pre-commit
/
pre_commit
/
repository.py
Copy path
Top
File metadata and controls
Code
Blame
148 lines (119 loc) · 4.47 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
from
__future__
import
unicode_literals
import
logging
import
shutil
from
cached_property
import
cached_property
from
pre_commit
import
git
from
pre_commit
.
clientlib
.
validate_config
import
is_local_hooks
from
pre_commit
.
clientlib
.
validate_manifest
import
MANIFEST_JSON_SCHEMA
from
pre_commit
.
jsonschema_extensions
import
apply_defaults
from
pre_commit
.
languages
.
all
import
languages
from
pre_commit
.
languages
.
helpers
import
environment_dir
from
pre_commit
.
manifest
import
Manifest
from
pre_commit
.
prefixed_command_runner
import
PrefixedCommandRunner
logger
=
logging
.
getLogger
(
'pre_commit'
)
class
Repository
(
object
):
def
__init__
(
self
,
repo_config
,
repo_path_getter
):
self
.
repo_config
=
repo_config
self
.
repo_path_getter
=
repo_path_getter
self
.
__installed
=
False
@
classmethod
def
create
(
cls
,
config
,
store
):
if
is_local_hooks
(
config
):
return
LocalRepository
(
config
)
else
:
repo_path_getter
=
store
.
get_repo_path_getter
(
config
[
'repo'
],
config
[
'sha'
]
)
return
cls
(
config
,
repo_path_getter
)
@
cached_property
def
repo_url
(
self
):
return
self
.
repo_config
[
'repo'
]
@
cached_property
def
sha
(
self
):
return
self
.
repo_config
[
'sha'
]
@
cached_property
def
languages
(
self
):
return
set
(
(
hook
[
'language'
],
hook
[
'language_version'
])
for
_
,
hook
in
self
.
hooks
)
@
cached_property
def
hooks
(
self
):
# TODO: merging in manifest dicts is a smell imo
return
tuple
(
(
hook
[
'id'
],
dict
(
self
.
manifest
.
hooks
[
hook
[
'id'
]],
**
hook
))
for
hook
in
self
.
repo_config
[
'hooks'
]
)
@
cached_property
def
manifest
(
self
):
return
Manifest
(
self
.
repo_path_getter
)
@
cached_property
def
cmd_runner
(
self
):
return
PrefixedCommandRunner
(
self
.
repo_path_getter
.
repo_path
)
def
require_installed
(
self
):
if
self
.
__installed
:
return
self
.
install
()
self
.
__installed
=
True
def
install
(
self
):
"""Install the hook repository."""
def
language_is_installed
(
language_name
,
language_version
):
language
=
languages
[
language_name
]
directory
=
environment_dir
(
language
.
ENVIRONMENT_DIR
,
language_version
,
)
return
(
directory
is
None
or
self
.
cmd_runner
.
exists
(
directory
,
'.installed'
)
)
if
not
all
(
language_is_installed
(
language_name
,
language_version
)
for
language_name
,
language_version
in
self
.
languages
):
logger
.
info
(
'Installing environment for {0}.'
.
format
(
self
.
repo_url
)
)
logger
.
info
(
'Once installed this environment will be reused.'
)
logger
.
info
(
'This may take a few minutes...'
)
for
language_name
,
language_version
in
self
.
languages
:
language
=
languages
[
language_name
]
if
language_is_installed
(
language_name
,
language_version
):
continue
directory
=
environment_dir
(
language
.
ENVIRONMENT_DIR
,
language_version
,
)
# There's potentially incomplete cleanup from previous runs
# Clean it up!
if
self
.
cmd_runner
.
exists
(
directory
):
shutil
.
rmtree
(
self
.
cmd_runner
.
path
(
directory
))
language
.
install_environment
(
self
.
cmd_runner
,
language_version
)
# Touch the .installed file (atomic) to indicate we've installed
open
(
self
.
cmd_runner
.
path
(
directory
,
'.installed'
),
'w'
).
close
()
def
run_hook
(
self
,
hook
,
file_args
):
"""Run a hook.
Args:
hook - Hook dictionary
file_args - List of files to run
"""
self
.
require_installed
()
return
languages
[
hook
[
'language'
]].
run_hook
(
self
.
cmd_runner
,
hook
,
file_args
,
)
class
LocalRepository
(
Repository
):
def
__init__
(
self
,
repo_config
):
super
(
LocalRepository
,
self
).
__init__
(
repo_config
,
None
)
@
cached_property
def
hooks
(
self
):
return
tuple
(
(
hook
[
'id'
],
apply_defaults
(
hook
,
MANIFEST_JSON_SCHEMA
[
'items'
]))
for
hook
in
self
.
repo_config
[
'hooks'
]
)
@
cached_property
def
cmd_runner
(
self
):
return
PrefixedCommandRunner
(
git
.
get_root
())
@
cached_property
def
sha
(
self
):
raise
NotImplementedError
@
cached_property
def
manifest
(
self
):
raise
NotImplementedError
You can’t perform that action at this time.