FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
GitPython/git/repo/fun.py at gix-backend · gitpython-developers/GitPython · GitHub
gitpython-developers
GitPython
Repository navigation
Code
Issues
6
(6)
Pull requests
Discussions
Actions
Security and quality
42
(42)
Insights
Expand file tree
Breadcrumbs
GitPython
/
git
/
repo
/
fun.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
206 lines (174 loc) · 7.46 KB
Breadcrumbs
GitPython
/
git
/
repo
/
fun.py
Copy path
File metadata and controls
206 lines (174 loc) · 7.46 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
# This module is part of GitPython and is released under the
# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
"""Repository and revision queries delegated to Git."""
from
__future__
import
annotations
__all__
=
[
"rev_parse"
,
"is_git_dir"
,
"touch"
,
"find_submodule_git_dir"
,
"name_to_object"
,
"short_to_long"
,
"deref_tag"
,
"to_commit"
,
"find_worktree_git_dir"
,
]
import
os
import
os
.
path
as
osp
import
posixpath
import
tempfile
from
typing
import
Optional
,
TYPE_CHECKING
,
Union
,
overload
from
gitdb
.
exc
import
BadName
,
BadObject
from
git
import
_backend
from
git
.
cmd
import
Git
from
git
.
compat
import
defenc
from
git
.
exc
import
GitCommandError
from
git
.
objects
import
Object
from
git
.
objects
.
base
import
IndexObject
from
git
.
refs
import
SymbolicReference
from
git
.
types
import
AnyGitObject
,
Literal
,
PathLike
from
git
.
util
import
bin_to_hex
,
hex_to_bin
,
to_native_path_linux
if
TYPE_CHECKING
:
from
gitdb
.
db
import
CompoundDB
,
LooseObjectDB
from
git
.
objects
import
Commit
from
.
base
import
Repo
def
touch
(
filename
:
str
)
->
str
:
with
open
(
filename
,
"ab"
):
pass
return
filename
def
find_submodule_git_dir
(
d
:
PathLike
)
->
Optional
[
PathLike
]:
"""Resolve a repository directory or gitfile using the selected backend."""
path
=
osp
.
abspath
(
os
.
fspath
(
d
))
if
not
osp
.
exists
(
path
):
return
None
try
:
native
=
_backend
.
discover_repository
(
path
, {})
if
native
is
not
NotImplemented
:
return
to_native_path_linux
(
os
.
fspath
(
native
.
git_dir
()))
return
Git
().
_call_process_safe
(
"rev_parse"
,
"--resolve-git-dir"
,
to_native_path_linux
(
path
))
except
GitCommandError
:
return
None
def
is_git_dir
(
d
:
PathLike
)
->
bool
:
"""Whether Git recognizes the directory as repository storage."""
return
osp
.
isdir
(
d
)
and
find_submodule_git_dir
(
d
)
is
not
None
def
find_worktree_git_dir
(
dotgit
:
PathLike
)
->
Optional
[
str
]:
"""Resolve an existing worktree gitfile without parsing its contents."""
if
not
osp
.
isfile
(
dotgit
):
return
None
result
=
find_submodule_git_dir
(
dotgit
)
return
os
.
fspath
(
result
)
if
result
is
not
None
else
None
def
short_to_long
(
odb
:
Union
[
"CompoundDB"
,
"LooseObjectDB"
],
hexsha
:
str
)
->
Optional
[
bytes
]:
"""Resolve an abbreviated object ID using the selected object database."""
try
:
return
bin_to_hex
(
odb
.
partial_to_complete_sha_hex
(
hexsha
))
except
BadObject
:
return
None
@
overload
def
name_to_object
(
repo
:
"Repo"
,
name
:
str
,
return_ref
:
Literal
[
False
]
=
...)
->
AnyGitObject
: ...
@
overload
def
name_to_object
(
repo
:
"Repo"
,
name
:
str
,
return_ref
:
Literal
[
True
])
->
Union
[
AnyGitObject
,
SymbolicReference
]: ...
def
name_to_object
(
repo
:
"Repo"
,
name
:
str
,
return_ref
:
bool
=
False
)
->
Union
[
AnyGitObject
,
SymbolicReference
]:
"""Resolve a revision, optionally returning its fully qualified reference."""
name
=
Git
.
_check_operand
(
name
,
"revision"
)
if
not
return_ref
:
return
rev_parse
(
repo
,
name
)
try
:
path
=
repo
.
git
.
_call_process_safe
(
"rev_parse"
,
"--symbolic-full-name"
,
"--verify"
,
"--quiet"
,
"--end-of-options"
,
name
)
except
GitCommandError
as
exc
:
if
exc
.
status
!=
1
:
raise
raise
BadObject
(
name
)
from
exc
if
not
path
:
raise
BadObject
(
name
)
SymbolicReference
.
_check_ref_name_valid
(
path
)
return
SymbolicReference
.
from_path
(
repo
,
path
)
def
deref_tag
(
tag
:
AnyGitObject
)
->
AnyGitObject
:
"""Return the object obtained by peeling tags through Git."""
return
rev_parse
(
tag
.
repo
,
tag
.
hexsha
+
"^{}"
)
if
tag
.
type
==
"tag"
else
tag
def
to_commit
(
obj
:
AnyGitObject
)
->
"Commit"
:
"""Convert an object or annotated tag to a commit."""
obj
=
deref_tag
(
obj
)
if
obj
.
type
!=
"commit"
:
raise
ValueError
(
"Cannot convert object %r to type commit"
%
obj
)
return
obj
def
_tree_path
(
repo
:
"Repo"
,
rev
:
str
)
->
Optional
[
tuple
[
str
,
str
]]:
"""Ask Git which colon follows a tree-ish, including message-search revisions."""
if
rev
.
startswith
(
":"
):
return
None
for
index
,
char
in
enumerate
(
rev
):
if
char
!=
":"
:
continue
try
:
tree
=
repo
.
git
.
_call_process_safe
(
"rev_parse"
,
"--verify"
,
"--quiet"
,
"--end-of-options"
,
rev
[:
index
]
+
"^{tree}"
)
except
GitCommandError
as
exc
:
if
exc
.
status
!=
1
:
raise
continue
return
tree
,
rev
[
index
+
1
:]
return
None
def
rev_parse
(
repo
:
"Repo"
,
rev
:
str
)
->
AnyGitObject
:
"""Resolve a Git revision to an object using Git's native revision grammar.
Invalid or missing revisions raise :class:`gitdb.exc.BadName`. Missing tree
paths retain the tree lookup's :class:`KeyError`. Option-like revisions and
embedded command delimiters are rejected before invoking Git.
"""
rev
=
Git
.
_check_operand
(
rev
,
"revision"
)
native
=
_backend
.
revision_info
(
repo
.
git
,
rev
)
if
":"
in
rev
else
NotImplemented
try
:
oid
=
(
native
[
0
]
if
native
is
not
NotImplemented
else
repo
.
git
.
_call_process_safe
(
"rev_parse"
,
"--verify"
,
"--quiet"
,
"--end-of-options"
,
rev
)
)
except
GitCommandError
as
exc
:
if
exc
.
status
!=
1
and
"Invalid regular expression"
not
in
exc
.
stderr
:
raise
if
exc
.
status
==
1
and
":"
in
rev
:
context
=
_tree_path
(
repo
,
rev
)
if
context
is
not
None
and
context
[
1
]:
# Reuse the public, backend-managed tree query for its legacy
# missing-component diagnostics; do not parse Git storage here.
repo
.
tree
(
context
[
0
]).
join
(
context
[
1
])
raise
BadName
(
rev
)
from
exc
if
not
repo
.
re_hexsha_only
.
fullmatch
(
oid
):
raise
BadName
(
rev
)
obj
=
Object
.
new_from_sha
(
repo
,
hex_to_bin
(
oid
))
if
isinstance
(
obj
,
IndexObject
)
and
":"
in
rev
:
if
native
is
not
NotImplemented
:
if
native
[
1
]
is
not
None
:
path_bytes
,
obj
.
mode
=
native
[
1
]
path
=
path_bytes
.
decode
(
defenc
,
"surrogateescape"
)
obj
.
path
=
posixpath
.
normpath
(
path
)
if
path
else
""
elif
obj
.
type
==
"tree"
:
# Gix omits path metadata for a successfully resolved root tree.
obj
.
path
=
""
return
obj
# Git resolves the mode, including index stages and executable/symlink
# entries. No object storage or revision grammar is decoded in Python.
with
tempfile
.
TemporaryFile
()
as
stream
:
stream
.
write
(
rev
.
encode
(
defenc
,
"surrogateescape"
)
+
b"
\0
"
)
stream
.
seek
(
0
)
mode
=
repo
.
git
.
_call_process_safe
(
"cat_file"
,
"--batch-check=%(objectmode)"
,
"-Z"
,
istream
=
stream
).
strip
(
"
\0
"
)
if
mode
:
obj
.
mode
=
int
(
mode
,
8
)
if
rev
.
startswith
(
":"
):
# The only remaining blob/tree context is an index entry.
path
=
rev
[
3
:]
if
len
(
rev
)
>
2
and
rev
[
1
]
in
"0123"
and
rev
[
2
]
==
":"
else
rev
[
1
:]
obj
.
path
=
posixpath
.
normpath
(
path
)
if
path
else
""
else
:
context
=
_tree_path
(
repo
,
rev
)
if
context
is
not
None
:
path
=
context
[
1
]
obj
.
path
=
posixpath
.
normpath
(
path
)
if
path
else
""
elif
obj
.
type
==
"tree"
:
# Git's objectmode atom is empty for the root tree, too.
context
=
_tree_path
(
repo
,
rev
)
if
context
is
not
None
and
not
context
[
1
]:
obj
.
path
=
""
return
obj
Back
|
FazBrowse Home
|
New Git URL