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/tests/git_test.py at master · 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
master
Breadcrumbs
pre-commit
/
tests
/
git_test.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
193 lines (137 loc) · 5.3 KB
master
Breadcrumbs
pre-commit
/
tests
/
git_test.py
Copy path
Top
File metadata and controls
Code
Blame
193 lines (137 loc) · 5.3 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
import
os
.
path
import
pytest
from
pre_commit
import
git
from
pre_commit
.
util
import
cmd_output
from
testing
.
util
import
git_commit
def
test_get_root_at_root
(
in_git_dir
):
expected
=
os
.
path
.
normcase
(
in_git_dir
.
strpath
)
assert
os
.
path
.
normcase
(
git
.
get_root
())
==
expected
def
test_get_root_deeper
(
in_git_dir
):
expected
=
os
.
path
.
normcase
(
in_git_dir
.
strpath
)
with
in_git_dir
.
join
(
'foo'
).
ensure_dir
().
as_cwd
():
assert
os
.
path
.
normcase
(
git
.
get_root
())
==
expected
def
test_get_staged_files_deleted
(
in_git_dir
):
in_git_dir
.
join
(
'test'
).
ensure
()
cmd_output
(
'git'
,
'add'
,
'test'
)
git_commit
()
cmd_output
(
'git'
,
'rm'
,
'--cached'
,
'test'
)
assert
git
.
get_staged_files
()
==
[]
def
test_is_not_in_merge_conflict
(
in_git_dir
):
assert
git
.
is_in_merge_conflict
()
is
False
def
test_is_in_merge_conflict
(
in_merge_conflict
):
assert
git
.
is_in_merge_conflict
()
is
True
def
test_is_in_merge_conflict_submodule
(
in_conflicting_submodule
):
assert
git
.
is_in_merge_conflict
()
is
True
def
test_cherry_pick_conflict
(
in_merge_conflict
):
cmd_output
(
'git'
,
'merge'
,
'--abort'
)
foo_ref
=
cmd_output
(
'git'
,
'rev-parse'
,
'foo'
)[
1
].
strip
()
cmd_output
(
'git'
,
'cherry-pick'
,
foo_ref
,
retcode
=
None
)
assert
git
.
is_in_merge_conflict
()
is
False
def
resolve_conflict
():
with
open
(
'conflict_file'
,
'w'
)
as
conflicted_file
:
conflicted_file
.
write
(
'herp
\n
derp
\n
'
)
cmd_output
(
'git'
,
'add'
,
'conflict_file'
)
def
test_get_conflicted_files
(
in_merge_conflict
):
resolve_conflict
()
with
open
(
'other_file'
,
'w'
)
as
other_file
:
other_file
.
write
(
'oh hai'
)
cmd_output
(
'git'
,
'add'
,
'other_file'
)
ret
=
set
(
git
.
get_conflicted_files
())
assert
ret
==
{
'conflict_file'
,
'other_file'
}
def
test_get_conflicted_files_in_submodule
(
in_conflicting_submodule
):
resolve_conflict
()
assert
set
(
git
.
get_conflicted_files
())
==
{
'conflict_file'
}
def
test_get_conflicted_files_unstaged_files
(
in_merge_conflict
):
"""This case no longer occurs, but it is a useful test nonetheless"""
resolve_conflict
()
# Make unstaged file.
with
open
(
'bar_only_file'
,
'w'
)
as
bar_only_file
:
bar_only_file
.
write
(
'new contents!
\n
'
)
ret
=
set
(
git
.
get_conflicted_files
())
assert
ret
==
{
'conflict_file'
}
MERGE_MSG
=
b"Merge branch 'foo' into bar
\n
\n
Conflicts:
\n
\t
conflict_file
\n
"
OTHER_MERGE_MSG
=
MERGE_MSG
+
b'
\t
other_conflict_file
\n
'
@
pytest
.
mark
.
parametrize
(
(
'input'
,
'expected_output'
),
(
(
MERGE_MSG
, [
'conflict_file'
]),
(
OTHER_MERGE_MSG
, [
'conflict_file'
,
'other_conflict_file'
]),
),
)
def
test_parse_merge_msg_for_conflicts
(
input
,
expected_output
):
ret
=
git
.
parse_merge_msg_for_conflicts
(
input
)
assert
ret
==
expected_output
def
test_get_changed_files
(
in_git_dir
):
git_commit
()
in_git_dir
.
join
(
'a.txt'
).
ensure
()
in_git_dir
.
join
(
'b.txt'
).
ensure
()
cmd_output
(
'git'
,
'add'
,
'.'
)
git_commit
()
files
=
git
.
get_changed_files
(
'HEAD^'
,
'HEAD'
)
assert
files
==
[
'a.txt'
,
'b.txt'
]
# files changed in source but not in origin should not be returned
files
=
git
.
get_changed_files
(
'HEAD'
,
'HEAD^'
)
assert
files
==
[]
@
pytest
.
mark
.
parametrize
(
(
's'
,
'expected'
),
(
(
'foo
\0
bar
\0
'
, [
'foo'
,
'bar'
]),
(
'foo
\0
'
, [
'foo'
]),
(
''
, []),
(
'foo'
, [
'foo'
]),
),
)
def
test_zsplit
(
s
,
expected
):
assert
git
.
zsplit
(
s
)
==
expected
@
pytest
.
fixture
def
non_ascii_repo
(
in_git_dir
):
git_commit
()
in_git_dir
.
join
(
'интервью'
).
ensure
()
cmd_output
(
'git'
,
'add'
,
'.'
)
git_commit
()
yield
in_git_dir
def
test_all_files_non_ascii
(
non_ascii_repo
):
ret
=
git
.
get_all_files
()
assert
ret
==
[
'интервью'
]
def
test_staged_files_non_ascii
(
non_ascii_repo
):
non_ascii_repo
.
join
(
'интервью'
).
write
(
'hi'
)
cmd_output
(
'git'
,
'add'
,
'.'
)
assert
git
.
get_staged_files
()
==
[
'интервью'
]
def
test_changed_files_non_ascii
(
non_ascii_repo
):
ret
=
git
.
get_changed_files
(
'HEAD^'
,
'HEAD'
)
assert
ret
==
[
'интервью'
]
def
test_get_conflicted_files_non_ascii
(
in_merge_conflict
):
open
(
'интервью'
,
'a'
).
close
()
cmd_output
(
'git'
,
'add'
,
'.'
)
ret
=
git
.
get_conflicted_files
()
assert
ret
==
{
'conflict_file'
,
'интервью'
}
def
test_intent_to_add
(
in_git_dir
):
in_git_dir
.
join
(
'a'
).
ensure
()
cmd_output
(
'git'
,
'add'
,
'--intent-to-add'
,
'a'
)
assert
git
.
intent_to_add_files
()
==
[
'a'
]
def
test_status_output_with_rename
(
in_git_dir
):
in_git_dir
.
join
(
'a'
).
write
(
'1
\n
2
\n
3
\n
4
\n
5
\n
6
\n
7
\n
8
\n
9
\n
10
\n
'
)
cmd_output
(
'git'
,
'add'
,
'a'
)
git_commit
()
cmd_output
(
'git'
,
'mv'
,
'a'
,
'b'
)
in_git_dir
.
join
(
'c'
).
ensure
()
cmd_output
(
'git'
,
'add'
,
'--intent-to-add'
,
'c'
)
assert
git
.
intent_to_add_files
()
==
[
'c'
]
def
test_no_git_env
():
env
=
{
'http_proxy'
:
'http://myproxy:80'
,
'GIT_EXEC_PATH'
:
'/some/git/exec/path'
,
'GIT_SSH'
:
'/usr/bin/ssh'
,
'GIT_SSH_COMMAND'
:
'ssh -o'
,
'GIT_DIR'
:
'/none/shall/pass'
,
}
no_git_env
=
git
.
no_git_env
(
env
)
assert
no_git_env
==
{
'http_proxy'
:
'http://myproxy:80'
,
'GIT_EXEC_PATH'
:
'/some/git/exec/path'
,
'GIT_SSH'
:
'/usr/bin/ssh'
,
'GIT_SSH_COMMAND'
:
'ssh -o'
,
}
def
test_init_repo_no_hooks
(
tmpdir
):
git
.
init_repo
(
str
(
tmpdir
),
remote
=
'dne'
)
assert
not
tmpdir
.
join
(
'.git/hooks'
).
exists
()
You can’t perform that action at this time.