Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/git/exc.py: 57%

58 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1# exc.py 

2# Copyright (C) 2008, 2009 Michael Trier (mtrier@gmail.com) and contributors 

3# 

4# This module is part of GitPython and is released under 

5# the BSD License: http://www.opensource.org/licenses/bsd-license.php 

6""" Module containing all exceptions thrown throughout the git package, """ 

7 

8from gitdb.exc import BadName # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614 

9from gitdb.exc import * # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614 

10from git.compat import safe_decode 

11from git.util import remove_password_if_present 

12 

13# typing ---------------------------------------------------- 

14 

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

16from git.types import PathLike 

17 

18if TYPE_CHECKING: 18 ↛ 19line 18 didn't jump to line 19, because the condition on line 18 was never true

19 from git.repo.base import Repo 

20 

21# ------------------------------------------------------------------ 

22 

23 

24class GitError(Exception): 

25 """Base class for all package exceptions""" 

26 

27 

28class InvalidGitRepositoryError(GitError): 

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

30 

31 

32class WorkTreeRepositoryUnsupported(InvalidGitRepositoryError): 

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

34 

35 

36class NoSuchPathError(GitError, OSError): 

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

38 

39 

40class CommandError(GitError): 

41 """Base class for exceptions thrown at every stage of `Popen()` execution. 

42 

43 :param command: 

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

45 """ 

46 

47 #: A unicode print-format with 2 `%s for `<cmdline>` and the rest, 

48 #: e.g. 

49 #: "'%s' failed%s" 

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

51 

52 def __init__( 

53 self, 

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

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

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

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

58 ) -> None: 

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

60 command = command.split() 

61 self.command = remove_password_if_present(command) 

62 self.status = status 

63 if status: 

64 if isinstance(status, Exception): 

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

66 else: 

67 try: 

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

69 except (ValueError, TypeError): 

70 s = safe_decode(str(status)) 

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

72 

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

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

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

76 stdout_decode = safe_decode(stdout) 

77 stderr_decode = safe_decode(stderr) 

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

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

80 

81 def __str__(self) -> str: 

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

83 self._cmd, 

84 self._cause, 

85 self._cmdline, 

86 self.stdout, 

87 self.stderr, 

88 ) 

89 

90 

91class GitCommandNotFound(CommandError): 

92 """Thrown if we cannot find the `git` executable in the PATH or at the path given by 

93 the GIT_PYTHON_GIT_EXECUTABLE environment variable""" 

94 

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

96 super(GitCommandNotFound, self).__init__(command, cause) 

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

98 

99 

100class GitCommandError(CommandError): 

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

102 

103 def __init__( 

104 self, 

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

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

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

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

109 ) -> None: 

110 super(GitCommandError, self).__init__(command, status, stderr, stdout) 

111 

112 

113class CheckoutError(GitError): 

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

115 changes. 

116 

117 The .failed_files attribute contains a list of relative paths that failed 

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

119 

120 The .failed_reasons attribute contains a string informing about the actual 

121 cause of the issue. 

122 

123 The .valid_files attribute contains a list of relative paths to files that 

124 were checked out successfully and hence match the version stored in the 

125 index""" 

126 

127 def __init__( 

128 self, 

129 message: str, 

130 failed_files: Sequence[PathLike], 

131 valid_files: Sequence[PathLike], 

132 failed_reasons: List[str], 

133 ) -> None: 

134 

135 Exception.__init__(self, message) 

136 self.failed_files = failed_files 

137 self.failed_reasons = failed_reasons 

138 self.valid_files = valid_files 

139 

140 def __str__(self) -> str: 

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

142 

143 

144class CacheError(GitError): 

145 

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

147 

148 

149class UnmergedEntriesError(CacheError): 

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

151 entries in the cache""" 

152 

153 

154class HookExecutionError(CommandError): 

155 """Thrown if a hook exits with a non-zero exit code. It provides access to the exit code and the string returned 

156 via standard output""" 

157 

158 def __init__( 

159 self, 

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

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

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

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

164 ) -> None: 

165 

166 super(HookExecutionError, self).__init__(command, status, stderr, stdout) 

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

168 

169 

170class RepositoryDirtyError(GitError): 

171 """Thrown whenever an operation on a repository fails as it has uncommitted changes that would be overwritten""" 

172 

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

174 self.repo = repo 

175 self.message = message 

176 

177 def __str__(self) -> str: 

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