Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/dulwich/errors.py: 59%

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

70 statements  

1# errors.py -- errors for dulwich 

2# Copyright (C) 2007 James Westby <jw+debian@jameswestby.net> 

3# Copyright (C) 2009-2012 Jelmer Vernooij <jelmer@jelmer.uk> 

4# 

5# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 

6# Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU 

7# General Public License as published by the Free Software Foundation; version 2.0 

8# or (at your option) any later version. You can redistribute it and/or 

9# modify it under the terms of either of these two licenses. 

10# 

11# Unless required by applicable law or agreed to in writing, software 

12# distributed under the License is distributed on an "AS IS" BASIS, 

13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

14# See the License for the specific language governing permissions and 

15# limitations under the License. 

16# 

17# You should have received a copy of the licenses; if not, see 

18# <http://www.gnu.org/licenses/> for a copy of the GNU General Public License 

19# and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache 

20# License, Version 2.0. 

21# 

22 

23"""Dulwich-related exception classes and utility functions.""" 

24 

25 

26# Please do not add more errors here, but instead add them close to the code 

27# that raises the error. 

28 

29import binascii 

30from typing import Optional, Union 

31 

32 

33class ChecksumMismatch(Exception): 

34 """A checksum didn't match the expected contents.""" 

35 

36 def __init__( 

37 self, 

38 expected: Union[bytes, str], 

39 got: Union[bytes, str], 

40 extra: Optional[str] = None, 

41 ) -> None: 

42 if isinstance(expected, bytes) and len(expected) == 20: 

43 expected_str = binascii.hexlify(expected).decode("ascii") 

44 else: 

45 expected_str = ( 

46 expected if isinstance(expected, str) else expected.decode("ascii") 

47 ) 

48 if isinstance(got, bytes) and len(got) == 20: 

49 got_str = binascii.hexlify(got).decode("ascii") 

50 else: 

51 got_str = got if isinstance(got, str) else got.decode("ascii") 

52 self.expected = expected_str 

53 self.got = got_str 

54 self.extra = extra 

55 message = f"Checksum mismatch: Expected {expected_str}, got {got_str}" 

56 if self.extra is not None: 

57 message += f"; {extra}" 

58 Exception.__init__(self, message) 

59 

60 

61class WrongObjectException(Exception): 

62 """Baseclass for all the _ is not a _ exceptions on objects. 

63 

64 Do not instantiate directly. 

65 

66 Subclasses should define a type_name attribute that indicates what 

67 was expected if they were raised. 

68 """ 

69 

70 type_name: str 

71 

72 def __init__(self, sha: bytes, *args: object, **kwargs: object) -> None: 

73 Exception.__init__(self, f"{sha.decode('ascii')} is not a {self.type_name}") 

74 

75 

76class NotCommitError(WrongObjectException): 

77 """Indicates that the sha requested does not point to a commit.""" 

78 

79 type_name = "commit" 

80 

81 

82class NotTreeError(WrongObjectException): 

83 """Indicates that the sha requested does not point to a tree.""" 

84 

85 type_name = "tree" 

86 

87 

88class NotTagError(WrongObjectException): 

89 """Indicates that the sha requested does not point to a tag.""" 

90 

91 type_name = "tag" 

92 

93 

94class NotBlobError(WrongObjectException): 

95 """Indicates that the sha requested does not point to a blob.""" 

96 

97 type_name = "blob" 

98 

99 

100class MissingCommitError(Exception): 

101 """Indicates that a commit was not found in the repository.""" 

102 

103 def __init__(self, sha: bytes, *args: object, **kwargs: object) -> None: 

104 self.sha = sha 

105 Exception.__init__(self, f"{sha.decode('ascii')} is not in the revision store") 

106 

107 

108class ObjectMissing(Exception): 

109 """Indicates that a requested object is missing.""" 

110 

111 def __init__(self, sha: bytes, *args: object, **kwargs: object) -> None: 

112 Exception.__init__(self, f"{sha.decode('ascii')} is not in the pack") 

113 

114 

115class ApplyDeltaError(Exception): 

116 """Indicates that applying a delta failed.""" 

117 

118 def __init__(self, *args: object, **kwargs: object) -> None: 

119 Exception.__init__(self, *args, **kwargs) 

120 

121 

122class NotGitRepository(Exception): 

123 """Indicates that no Git repository was found.""" 

124 

125 def __init__(self, *args: object, **kwargs: object) -> None: 

126 Exception.__init__(self, *args, **kwargs) 

127 

128 

129class GitProtocolError(Exception): 

130 """Git protocol exception.""" 

131 

132 def __init__(self, *args: object, **kwargs: object) -> None: 

133 Exception.__init__(self, *args, **kwargs) 

134 

135 def __eq__(self, other: object) -> bool: 

136 return isinstance(other, GitProtocolError) and self.args == other.args 

137 

138 

139class SendPackError(GitProtocolError): 

140 """An error occurred during send_pack.""" 

141 

142 

143class HangupException(GitProtocolError): 

144 """Hangup exception.""" 

145 

146 def __init__(self, stderr_lines: Optional[list[bytes]] = None) -> None: 

147 if stderr_lines: 

148 super().__init__( 

149 "\n".join( 

150 line.decode("utf-8", "surrogateescape") for line in stderr_lines 

151 ) 

152 ) 

153 else: 

154 super().__init__("The remote server unexpectedly closed the connection.") 

155 self.stderr_lines = stderr_lines 

156 

157 def __eq__(self, other: object) -> bool: 

158 return ( 

159 isinstance(other, HangupException) 

160 and self.stderr_lines == other.stderr_lines 

161 ) 

162 

163 

164class UnexpectedCommandError(GitProtocolError): 

165 """Unexpected command received in a proto line.""" 

166 

167 def __init__(self, command: Optional[str]) -> None: 

168 command_str = "flush-pkt" if command is None else f"command {command}" 

169 super().__init__(f"Protocol got unexpected {command_str}") 

170 

171 

172class FileFormatException(Exception): 

173 """Base class for exceptions relating to reading git file formats.""" 

174 

175 

176class PackedRefsException(FileFormatException): 

177 """Indicates an error parsing a packed-refs file.""" 

178 

179 

180class ObjectFormatException(FileFormatException): 

181 """Indicates an error parsing an object.""" 

182 

183 

184class NoIndexPresent(Exception): 

185 """No index is present.""" 

186 

187 

188class CommitError(Exception): 

189 """An error occurred while performing a commit.""" 

190 

191 

192class RefFormatError(Exception): 

193 """Indicates an invalid ref name.""" 

194 

195 

196class HookError(Exception): 

197 """An error occurred while executing a hook.""" 

198 

199 

200class WorkingTreeModifiedError(Exception): 

201 """Indicates that the working tree has modifications that would be overwritten."""