Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/git/exc.py: 48%

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

66 statements  

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/ 

5 

6"""Exceptions thrown throughout the git package.""" 

7 

8__all__ = [ 

9 # Defined in gitdb.exc: 

10 "AmbiguousObjectName", 

11 "BadName", 

12 "BadObject", 

13 "BadObjectType", 

14 "InvalidDBRoot", 

15 "ODBError", 

16 "ParseError", 

17 "UnsupportedOperation", 

18 # Introduced in this module: 

19 "GitError", 

20 "InvalidGitRepositoryError", 

21 "WorkTreeRepositoryUnsupported", 

22 "NoSuchPathError", 

23 "UnsafeProtocolError", 

24 "UnsafeOptionError", 

25 "CommandError", 

26 "GitCommandNotFound", 

27 "GitCommandError", 

28 "CheckoutError", 

29 "CacheError", 

30 "UnmergedEntriesError", 

31 "HookExecutionError", 

32 "RepositoryDirtyError", 

33] 

34 

35from gitdb.exc import ( 

36 AmbiguousObjectName, 

37 BadName, 

38 BadObject, 

39 BadObjectType, 

40 InvalidDBRoot, 

41 ODBError, 

42 ParseError, 

43 UnsupportedOperation, 

44) 

45 

46from git.compat import safe_decode 

47from git.util import remove_password_if_present 

48 

49# typing ---------------------------------------------------- 

50 

51from typing import List, Sequence, Tuple, TYPE_CHECKING, Union 

52 

53from git.types import PathLike 

54 

55if TYPE_CHECKING: 

56 from git.repo.base import Repo 

57 

58# ------------------------------------------------------------------ 

59 

60 

61class GitError(Exception): 

62 """Base class for all package exceptions.""" 

63 

64 

65class InvalidGitRepositoryError(GitError): 

66 """Thrown if the given repository appears to have an invalid format.""" 

67 

68 

69class WorkTreeRepositoryUnsupported(InvalidGitRepositoryError): 

70 """Thrown to indicate we can't handle work tree repositories.""" 

71 

72 

73class NoSuchPathError(GitError, OSError): 

74 """Thrown if a path could not be access by the system.""" 

75 

76 

77class UnsafeProtocolError(GitError): 

78 """Thrown if unsafe protocols are passed without being explicitly allowed.""" 

79 

80 

81class UnsafeOptionError(GitError): 

82 """Thrown if unsafe options are passed without being explicitly allowed.""" 

83 

84 

85class CommandError(GitError): 

86 """Base class for exceptions thrown at every stage of :class:`~subprocess.Popen` 

87 execution. 

88 

89 :param command: 

90 A non-empty list of argv comprising the command-line. 

91 """ 

92 

93 _msg = "Cmd('%s') failed%s" 

94 """Format string with 2 ``%s`` for ``<cmdline>`` and the rest. 

95 

96 For example: ``"'%s' failed%s"`` 

97 

98 Subclasses may override this attribute, provided it is still in this form. 

99 """ 

100 

101 def __init__( 

102 self, 

103 command: Union[List[str], Tuple[str, ...], str], 

104 status: Union[str, int, None, Exception] = None, 

105 stderr: Union[bytes, str, None] = None, 

106 stdout: Union[bytes, str, None] = None, 

107 ) -> None: 

108 if not isinstance(command, (tuple, list)): 

109 command = command.split() 

110 self.command = remove_password_if_present(command) 

111 self.status = status 

112 if status: 

113 if isinstance(status, Exception): 

114 status = "%s('%s')" % (type(status).__name__, safe_decode(str(status))) 

115 else: 

116 try: 

117 status = "exit code(%s)" % int(status) 

118 except (ValueError, TypeError): 

119 s = safe_decode(str(status)) 

120 status = "'%s'" % s if isinstance(status, str) else s 

121 

122 self._cmd = safe_decode(self.command[0]) 

123 self._cmdline = " ".join(safe_decode(i) for i in self.command) 

124 self._cause = status and " due to: %s" % status or "!" 

125 stdout_decode = safe_decode(stdout) 

126 stderr_decode = safe_decode(stderr) 

127 self.stdout = stdout_decode and "\n stdout: '%s'" % stdout_decode or "" 

128 self.stderr = stderr_decode and "\n stderr: '%s'" % stderr_decode or "" 

129 

130 def __str__(self) -> str: 

131 return (self._msg + "\n cmdline: %s%s%s") % ( 

132 self._cmd, 

133 self._cause, 

134 self._cmdline, 

135 self.stdout, 

136 self.stderr, 

137 ) 

138 

139 

140class GitCommandNotFound(CommandError): 

141 """Thrown if we cannot find the ``git`` executable in the :envvar:`PATH` or at the 

142 path given by the :envvar:`GIT_PYTHON_GIT_EXECUTABLE` environment variable.""" 

143 

144 def __init__(self, command: Union[List[str], Tuple[str], str], cause: Union[str, Exception]) -> None: 

145 super().__init__(command, cause) 

146 self._msg = "Cmd('%s') not found%s" 

147 

148 

149class GitCommandError(CommandError): 

150 """Thrown if execution of the git command fails with non-zero status code.""" 

151 

152 def __init__( 

153 self, 

154 command: Union[List[str], Tuple[str, ...], str], 

155 status: Union[str, int, None, Exception] = None, 

156 stderr: Union[bytes, str, None] = None, 

157 stdout: Union[bytes, str, None] = None, 

158 ) -> None: 

159 super().__init__(command, status, stderr, stdout) 

160 

161 

162class CheckoutError(GitError): 

163 """Thrown if a file could not be checked out from the index as it contained 

164 changes. 

165 

166 The :attr:`failed_files` attribute contains a list of relative paths that failed to 

167 be checked out as they contained changes that did not exist in the index. 

168 

169 The :attr:`failed_reasons` attribute contains a string informing about the actual 

170 cause of the issue. 

171 

172 The :attr:`valid_files` attribute contains a list of relative paths to files that 

173 were checked out successfully and hence match the version stored in the index. 

174 """ 

175 

176 def __init__( 

177 self, 

178 message: str, 

179 failed_files: Sequence[PathLike], 

180 valid_files: Sequence[PathLike], 

181 failed_reasons: List[str], 

182 ) -> None: 

183 Exception.__init__(self, message) 

184 self.failed_files = failed_files 

185 self.failed_reasons = failed_reasons 

186 self.valid_files = valid_files 

187 

188 def __str__(self) -> str: 

189 return Exception.__str__(self) + ":%s" % self.failed_files 

190 

191 

192class CacheError(GitError): 

193 """Base for all errors related to the git index, which is called "cache" 

194 internally.""" 

195 

196 

197class UnmergedEntriesError(CacheError): 

198 """Thrown if an operation cannot proceed as there are still unmerged 

199 entries in the cache.""" 

200 

201 

202class HookExecutionError(CommandError): 

203 """Thrown if a hook exits with a non-zero exit code. 

204 

205 This provides access to the exit code and the string returned via standard output. 

206 """ 

207 

208 def __init__( 

209 self, 

210 command: Union[List[str], Tuple[str, ...], str], 

211 status: Union[str, int, None, Exception], 

212 stderr: Union[bytes, str, None] = None, 

213 stdout: Union[bytes, str, None] = None, 

214 ) -> None: 

215 super().__init__(command, status, stderr, stdout) 

216 self._msg = "Hook('%s') failed%s" 

217 

218 

219class RepositoryDirtyError(GitError): 

220 """Thrown whenever an operation on a repository fails as it has uncommitted changes 

221 that would be overwritten.""" 

222 

223 def __init__(self, repo: "Repo", message: str) -> None: 

224 self.repo = repo 

225 self.message = message 

226 

227 def __str__(self) -> str: 

228 return "Operation cannot be performed on %r: %s" % (self.repo, self.message)