Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/git/__init__.py: 75%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors
2#
3# This module is part of GitPython and is released under the
4# 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/
6# @PydevCodeAnalysisIgnore
8__all__ = [
9 "Actor",
10 "AmbiguousObjectName",
11 "BadName",
12 "BadObject",
13 "BadObjectType",
14 "BaseIndexEntry",
15 "Blob",
16 "BlobFilter",
17 "BlockingLockFile",
18 "CacheError",
19 "CheckoutError",
20 "CommandError",
21 "Commit",
22 "Diff",
23 "DiffConstants",
24 "DiffIndex",
25 "Diffable",
26 "FetchInfo",
27 "Git",
28 "GitCmdObjectDB",
29 "GitCommandError",
30 "GitCommandNotFound",
31 "GitConfigParser",
32 "GitDB",
33 "GitError",
34 "HEAD",
35 "Head",
36 "HookExecutionError",
37 "INDEX",
38 "IndexEntry",
39 "IndexFile",
40 "IndexObject",
41 "InvalidDBRoot",
42 "InvalidGitRepositoryError",
43 "List", # Deprecated - import this from `typing` instead.
44 "LockFile",
45 "NULL_TREE",
46 "NoSuchPathError",
47 "ODBError",
48 "Object",
49 "Optional", # Deprecated - import this from `typing` instead.
50 "ParseError",
51 "PathLike",
52 "PushInfo",
53 "RefLog",
54 "RefLogEntry",
55 "Reference",
56 "Remote",
57 "RemoteProgress",
58 "RemoteReference",
59 "Repo",
60 "RepositoryDirtyError",
61 "RootModule",
62 "RootUpdateProgress",
63 "Sequence", # Deprecated - import from `typing`, or `collections.abc` in 3.9+.
64 "StageType",
65 "Stats",
66 "Submodule",
67 "SymbolicReference",
68 "TYPE_CHECKING", # Deprecated - import this from `typing` instead.
69 "Tag",
70 "TagObject",
71 "TagReference",
72 "Tree",
73 "TreeModifier",
74 "Tuple", # Deprecated - import this from `typing` instead.
75 "Union", # Deprecated - import this from `typing` instead.
76 "UnmergedEntriesError",
77 "UnsafeOptionError",
78 "UnsafeProtocolError",
79 "UnsupportedOperation",
80 "UpdateProgress",
81 "WorkTreeRepositoryUnsupported",
82 "refresh",
83 "remove_password_if_present",
84 "rmtree",
85 "safe_decode",
86 "to_hex_sha",
87]
89__version__ = '3.1.43'
91from typing import Any, List, Optional, Sequence, TYPE_CHECKING, Tuple, Union
93if TYPE_CHECKING:
94 from types import ModuleType
96import warnings
98from gitdb.util import to_hex_sha
100from git.exc import (
101 AmbiguousObjectName,
102 BadName,
103 BadObject,
104 BadObjectType,
105 CacheError,
106 CheckoutError,
107 CommandError,
108 GitCommandError,
109 GitCommandNotFound,
110 GitError,
111 HookExecutionError,
112 InvalidDBRoot,
113 InvalidGitRepositoryError,
114 NoSuchPathError,
115 ODBError,
116 ParseError,
117 RepositoryDirtyError,
118 UnmergedEntriesError,
119 UnsafeOptionError,
120 UnsafeProtocolError,
121 UnsupportedOperation,
122 WorkTreeRepositoryUnsupported,
123)
124from git.types import PathLike
126try:
127 from git.compat import safe_decode # @NoMove
128 from git.config import GitConfigParser # @NoMove
129 from git.objects import ( # @NoMove
130 Blob,
131 Commit,
132 IndexObject,
133 Object,
134 RootModule,
135 RootUpdateProgress,
136 Submodule,
137 TagObject,
138 Tree,
139 TreeModifier,
140 UpdateProgress,
141 )
142 from git.refs import ( # @NoMove
143 HEAD,
144 Head,
145 RefLog,
146 RefLogEntry,
147 Reference,
148 RemoteReference,
149 SymbolicReference,
150 Tag,
151 TagReference,
152 )
153 from git.diff import ( # @NoMove
154 INDEX,
155 NULL_TREE,
156 Diff,
157 DiffConstants,
158 DiffIndex,
159 Diffable,
160 )
161 from git.db import GitCmdObjectDB, GitDB # @NoMove
162 from git.cmd import Git # @NoMove
163 from git.repo import Repo # @NoMove
164 from git.remote import FetchInfo, PushInfo, Remote, RemoteProgress # @NoMove
165 from git.index import ( # @NoMove
166 BaseIndexEntry,
167 BlobFilter,
168 CheckoutError,
169 IndexEntry,
170 IndexFile,
171 StageType,
172 # NOTE: This tells type checkers what util resolves to. We delete it, and it is
173 # really resolved by __getattr__, which warns. See below on what to use instead.
174 util,
175 )
176 from git.util import ( # @NoMove
177 Actor,
178 BlockingLockFile,
179 LockFile,
180 Stats,
181 remove_password_if_present,
182 rmtree,
183 )
184except GitError as _exc:
185 raise ImportError("%s: %s" % (_exc.__class__.__name__, _exc)) from _exc
188def _warned_import(message: str, fullname: str) -> "ModuleType":
189 import importlib
191 warnings.warn(message, DeprecationWarning, stacklevel=3)
192 return importlib.import_module(fullname)
195def _getattr(name: str) -> Any:
196 # TODO: If __version__ is made dynamic and lazily fetched, put that case right here.
198 if name == "util":
199 return _warned_import(
200 "The expression `git.util` and the import `from git import util` actually "
201 "reference git.index.util, and not the git.util module accessed in "
202 '`from git.util import XYZ` or `sys.modules["git.util"]`. This potentially '
203 "confusing behavior is currently preserved for compatibility, but may be "
204 "changed in the future and should not be relied on.",
205 fullname="git.index.util",
206 )
208 for names, prefix in (
209 ({"head", "log", "reference", "symbolic", "tag"}, "git.refs"),
210 ({"base", "fun", "typ"}, "git.index"),
211 ):
212 if name not in names:
213 continue
215 fullname = f"{prefix}.{name}"
217 return _warned_import(
218 f"{__name__}.{name} is a private alias of {fullname} and subject to "
219 f"immediate removal. Use {fullname} instead.",
220 fullname=fullname,
221 )
223 raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
226if not TYPE_CHECKING:
227 # NOTE: The expression `git.util` gives git.index.util and `from git import util`
228 # imports git.index.util, NOT git.util. It may not be feasible to change this until
229 # the next major version, to avoid breaking code inadvertently relying on it.
230 #
231 # - If git.index.util *is* what you want, use (or import from) that, to avoid
232 # confusion.
233 #
234 # - To use the "real" git.util module, write `from git.util import ...`, or if
235 # necessary access it as `sys.modules["git.util"]`.
236 #
237 # Note also that `import git.util` technically imports the "real" git.util... but
238 # the *expression* `git.util` after doing so is still git.index.util!
239 #
240 # (This situation differs from that of other indirect-submodule imports that are
241 # unambiguously non-public and subject to immediate removal. Here, the public
242 # git.util module, though different, makes less discoverable that the expression
243 # `git.util` refers to a non-public attribute of the git module.)
244 #
245 # This had originally come about by a wildcard import. Now that all intended imports
246 # are explicit, the intuitive but potentially incompatible binding occurs due to the
247 # usual rules for Python submodule bindings. So for now we replace that binding with
248 # git.index.util, delete that, and let __getattr__ handle it and issue a warning.
249 #
250 # For the same runtime behavior, it would be enough to forgo importing util, and
251 # delete util as created naturally; __getattr__ would behave the same. But type
252 # checkers would not know what util refers to when accessed as an attribute of git.
253 del util
255 # This is "hidden" to preserve static checking for undefined/misspelled attributes.
256 __getattr__ = _getattr
258# { Initialize git executable path
260GIT_OK = None
263def refresh(path: Optional[PathLike] = None) -> None:
264 """Convenience method for setting the git executable path.
266 :param path:
267 Optional path to the Git executable. If not absolute, it is resolved
268 immediately, relative to the current directory.
270 :note:
271 The `path` parameter is usually omitted and cannot be used to specify a custom
272 command whose location is looked up in a path search on each call. See
273 :meth:`Git.refresh <git.cmd.Git.refresh>` for details on how to achieve this.
275 :note:
276 This calls :meth:`Git.refresh <git.cmd.Git.refresh>` and sets other global
277 configuration according to the effect of doing so. As such, this function should
278 usually be used instead of using :meth:`Git.refresh <git.cmd.Git.refresh>` or
279 :meth:`FetchInfo.refresh <git.remote.FetchInfo.refresh>` directly.
281 :note:
282 This function is called automatically, with no arguments, at import time.
283 """
284 global GIT_OK
285 GIT_OK = False
287 if not Git.refresh(path=path):
288 return
289 if not FetchInfo.refresh(): # noqa: F405
290 return # type: ignore[unreachable]
292 GIT_OK = True
295try:
296 refresh()
297except Exception as _exc:
298 raise ImportError("Failed to initialize: {0}".format(_exc)) from _exc
300# } END initialize git executable path