Add non-bare repository tests.
[jelmer/dulwich-libgit2.git] / dulwich / errors.py
1 # errors.py -- errors for dulwich
2 # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
3 # Copyright (C) 2009 Jelmer Vernooij <jelmer@samba.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; version 2
8 # or (at your option) any later version of the License.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 # MA  02110-1301, USA.
19
20 """Dulwich-related exception classes and utility functions."""
21
22 import binascii
23
24
25 class ChecksumMismatch(Exception):
26     """A checksum didn't match the expected contents."""
27
28     def __init__(self, expected, got, extra=None):
29         if len(expected) == 20:
30             expected = binascii.hexlify(expected)
31         if len(got) == 20:
32             got = binascii.hexlify(got)
33         self.expected = expected
34         self.got = got
35         self.extra = extra
36         if self.extra is None:
37             Exception.__init__(self,
38                 "Checksum mismatch: Expected %s, got %s" % (expected, got))
39         else:
40             Exception.__init__(self,
41                 "Checksum mismatch: Expected %s, got %s; %s" %
42                 (expected, got, extra))
43
44
45 class WrongObjectException(Exception):
46     """Baseclass for all the _ is not a _ exceptions on objects.
47
48     Do not instantiate directly.
49
50     Subclasses should define a type_name attribute that indicates what
51     was expected if they were raised.
52     """
53
54     def __init__(self, sha, *args, **kwargs):
55         Exception.__init__(self, "%s is not a %s" % (sha, self.type_name))
56
57
58 class NotCommitError(WrongObjectException):
59     """Indicates that the sha requested does not point to a commit."""
60
61     type_name = 'commit'
62
63
64 class NotTreeError(WrongObjectException):
65     """Indicates that the sha requested does not point to a tree."""
66
67     type_name = 'tree'
68
69
70 class NotTagError(WrongObjectException):
71     """Indicates that the sha requested does not point to a tag."""
72
73     type_name = 'tag'
74
75
76 class NotBlobError(WrongObjectException):
77     """Indicates that the sha requested does not point to a blob."""
78
79     type_name = 'blob'
80
81
82 class MissingCommitError(Exception):
83     """Indicates that a commit was not found in the repository"""
84
85     def __init__(self, sha, *args, **kwargs):
86         Exception.__init__(self, "%s is not in the revision store" % sha)
87
88
89 class ObjectMissing(Exception):
90     """Indicates that a requested object is missing."""
91
92     def __init__(self, sha, *args, **kwargs):
93         Exception.__init__(self, "%s is not in the pack" % sha)
94
95
96 class ApplyDeltaError(Exception):
97     """Indicates that applying a delta failed."""
98
99     def __init__(self, *args, **kwargs):
100         Exception.__init__(self, *args, **kwargs)
101
102
103 class NotGitRepository(Exception):
104     """Indicates that no Git repository was found."""
105
106     def __init__(self, *args, **kwargs):
107         Exception.__init__(self, *args, **kwargs)
108
109
110 class GitProtocolError(Exception):
111     """Git protocol exception."""
112
113     def __init__(self, *args, **kwargs):
114         Exception.__init__(self, *args, **kwargs)
115
116
117 class SendPackError(GitProtocolError):
118     """An error occurred during send_pack."""
119
120     def __init__(self, *args, **kwargs):
121         Exception.__init__(self, *args, **kwargs)
122
123
124 class UpdateRefsError(GitProtocolError):
125     """The server reported errors updating refs."""
126
127     def __init__(self, *args, **kwargs):
128         self.ref_status = kwargs.pop('ref_status')
129         Exception.__init__(self, *args, **kwargs)
130
131
132 class HangupException(GitProtocolError):
133     """Hangup exception."""
134
135     def __init__(self):
136         Exception.__init__(self,
137             "The remote server unexpectedly closed the connection.")
138
139
140 class FileFormatException(Exception):
141     """Base class for exceptions relating to reading git file formats."""
142
143
144 class PackedRefsException(FileFormatException):
145     """Indicates an error parsing a packed-refs file."""
146
147
148 class ObjectFormatException(FileFormatException):
149     """Indicates an error parsing an object."""
150
151
152 class NoIndexPresent(Exception):
153     """No index is present."""
154
155
156 class CommitError(Exception):
157     """An error occurred while performing a commit."""