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/store.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
/
store.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
140 lines (113 loc) · 4.36 KB
master
Breadcrumbs
pre-commit
/
pre_commit
/
store.py
Copy path
Top
File metadata and controls
Code
Blame
140 lines (113 loc) · 4.36 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
from
__future__
import
unicode_literals
import
contextlib
import
io
import
logging
import
os
.
path
import
sqlite3
import
tempfile
from
cached_property
import
cached_property
from
pre_commit
.
prefixed_command_runner
import
PrefixedCommandRunner
from
pre_commit
.
util
import
clean_path_on_failure
from
pre_commit
.
util
import
cmd_output
from
pre_commit
.
util
import
cwd
from
pre_commit
.
util
import
no_git_env
logger
=
logging
.
getLogger
(
'pre_commit'
)
def
_get_default_directory
():
"""Returns the default directory for the Store. This is intentionally
underscored to indicate that `Store.get_default_directory` is the intended
way to get this information. This is also done so
`Store.get_default_directory` can be mocked in tests and
`_get_default_directory` can be tested.
"""
return
os
.
environ
.
get
(
'PRE_COMMIT_HOME'
,
os
.
path
.
join
(
os
.
path
.
expanduser
(
'~'
),
'.pre-commit'
),
)
class
Store
(
object
):
get_default_directory
=
staticmethod
(
_get_default_directory
)
class
RepoPathGetter
(
object
):
def
__init__
(
self
,
repo
,
sha
,
store
):
self
.
_repo
=
repo
self
.
_sha
=
sha
self
.
_store
=
store
@
cached_property
def
repo_path
(
self
):
return
self
.
_store
.
clone
(
self
.
_repo
,
self
.
_sha
)
def
__init__
(
self
,
directory
=
None
):
if
directory
is
None
:
directory
=
self
.
get_default_directory
()
self
.
directory
=
directory
self
.
__created
=
False
def
_write_readme
(
self
):
with
io
.
open
(
os
.
path
.
join
(
self
.
directory
,
'README'
),
'w'
)
as
readme
:
readme
.
write
(
'This directory is maintained by the pre-commit project.
\n
'
'Learn more: https://github.com/pre-commit/pre-commit
\n
'
)
def
_write_sqlite_db
(
self
):
# To avoid a race where someone ^Cs between db creation and execution
# of the CREATE TABLE statement
fd
,
tmpfile
=
tempfile
.
mkstemp
(
dir
=
self
.
directory
)
# We'll be managing this file ourselves
os
.
close
(
fd
)
# sqlite doesn't close its fd with its contextmanager >.<
# contextlib.closing fixes this.
# See: http://stackoverflow.com/a/28032829/812183
with
contextlib
.
closing
(
sqlite3
.
connect
(
tmpfile
))
as
db
:
db
.
executescript
(
'CREATE TABLE repos ('
' repo CHAR(255) NOT NULL,'
' ref CHAR(255) NOT NULL,'
' path CHAR(255) NOT NULL,'
' PRIMARY KEY (repo, ref)'
');'
)
# Atomic file move
os
.
rename
(
tmpfile
,
self
.
db_path
)
def
_create
(
self
):
if
os
.
path
.
exists
(
self
.
db_path
):
return
if
not
os
.
path
.
exists
(
self
.
directory
):
os
.
makedirs
(
self
.
directory
)
self
.
_write_readme
()
self
.
_write_sqlite_db
()
def
require_created
(
self
):
"""Require the pre-commit file store to be created."""
if
self
.
__created
:
return
self
.
_create
()
self
.
__created
=
True
def
clone
(
self
,
url
,
sha
):
"""Clone the given url and checkout the specific sha."""
self
.
require_created
()
# Check if we already exist
with
sqlite3
.
connect
(
self
.
db_path
)
as
db
:
result
=
db
.
execute
(
'SELECT path FROM repos WHERE repo = ? AND ref = ?'
,
[
url
,
sha
],
).
fetchone
()
if
result
:
return
result
[
0
]
logger
.
info
(
'Initializing environment for {}.'
.
format
(
url
))
dir
=
tempfile
.
mkdtemp
(
prefix
=
'repo'
,
dir
=
self
.
directory
)
with
clean_path_on_failure
(
dir
):
cmd_output
(
'git'
,
'clone'
,
'--no-checkout'
,
url
,
dir
,
env
=
no_git_env
(),
)
with
cwd
(
dir
):
cmd_output
(
'git'
,
'reset'
,
sha
,
'--hard'
,
env
=
no_git_env
())
# Update our db with the created repo
with
sqlite3
.
connect
(
self
.
db_path
)
as
db
:
db
.
execute
(
'INSERT INTO repos (repo, ref, path) VALUES (?, ?, ?)'
,
[
url
,
sha
,
dir
],
)
return
dir
def
get_repo_path_getter
(
self
,
repo
,
sha
):
return
self
.
RepoPathGetter
(
repo
,
sha
,
self
)
@
cached_property
def
cmd_runner
(
self
):
return
PrefixedCommandRunner
(
self
.
directory
)
@
cached_property
def
db_path
(
self
):
return
os
.
path
.
join
(
self
.
directory
,
'db.db'
)
You can’t perform that action at this time.