exc.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. from gitdb.exc import BadName # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614
  8. from gitdb.exc import * # NOQA @UnusedWildImport skipcq: PYL-W0401, PYL-W0614
  9. from git.compat import safe_decode
  10. from git.util import remove_password_if_present
  11. # typing ----------------------------------------------------
  12. from typing import List, Sequence, Tuple, Union, TYPE_CHECKING
  13. from git.types import PathLike
  14. if TYPE_CHECKING:
  15. from git.repo.base import Repo
  16. # ------------------------------------------------------------------
  17. class GitError(Exception):
  18. """Base class for all package exceptions"""
  19. class InvalidGitRepositoryError(GitError):
  20. """Thrown if the given repository appears to have an invalid format."""
  21. class WorkTreeRepositoryUnsupported(InvalidGitRepositoryError):
  22. """Thrown to indicate we can't handle work tree repositories"""
  23. class NoSuchPathError(GitError, OSError):
  24. """Thrown if a path could not be access by the system."""
  25. class UnsafeProtocolError(GitError):
  26. """Thrown if unsafe protocols are passed without being explicitly allowed."""
  27. class UnsafeOptionError(GitError):
  28. """Thrown if unsafe options are passed without being explicitly allowed."""
  29. class CommandError(GitError):
  30. """Base class for exceptions thrown at every stage of `Popen()` execution.
  31. :param command:
  32. A non-empty list of argv comprising the command-line.
  33. """
  34. #: A unicode print-format with 2 `%s for `<cmdline>` and the rest,
  35. #: e.g.
  36. #: "'%s' failed%s"
  37. _msg = "Cmd('%s') failed%s"
  38. def __init__(
  39. self,
  40. command: Union[List[str], Tuple[str, ...], str],
  41. status: Union[str, int, None, Exception] = None,
  42. stderr: Union[bytes, str, None] = None,
  43. stdout: Union[bytes, str, None] = None,
  44. ) -> None:
  45. if not isinstance(command, (tuple, list)):
  46. command = command.split()
  47. self.command = remove_password_if_present(command)
  48. self.status = status
  49. if status:
  50. if isinstance(status, Exception):
  51. status = "%s('%s')" % (type(status).__name__, safe_decode(str(status)))
  52. else:
  53. try:
  54. status = "exit code(%s)" % int(status)
  55. except (ValueError, TypeError):
  56. s = safe_decode(str(status))
  57. status = "'%s'" % s if isinstance(status, str) else s
  58. self._cmd = safe_decode(self.command[0])
  59. self._cmdline = " ".join(safe_decode(i) for i in self.command)
  60. self._cause = status and " due to: %s" % status or "!"
  61. stdout_decode = safe_decode(stdout)
  62. stderr_decode = safe_decode(stderr)
  63. self.stdout = stdout_decode and "\n stdout: '%s'" % stdout_decode or ""
  64. self.stderr = stderr_decode and "\n stderr: '%s'" % stderr_decode or ""
  65. def __str__(self) -> str:
  66. return (self._msg + "\n cmdline: %s%s%s") % (
  67. self._cmd,
  68. self._cause,
  69. self._cmdline,
  70. self.stdout,
  71. self.stderr,
  72. )
  73. class GitCommandNotFound(CommandError):
  74. """Thrown if we cannot find the `git` executable in the PATH or at the path given by
  75. the GIT_PYTHON_GIT_EXECUTABLE environment variable"""
  76. def __init__(self, command: Union[List[str], Tuple[str], str], cause: Union[str, Exception]) -> None:
  77. super(GitCommandNotFound, self).__init__(command, cause)
  78. self._msg = "Cmd('%s') not found%s"
  79. class GitCommandError(CommandError):
  80. """Thrown if execution of the git command fails with non-zero status code."""
  81. def __init__(
  82. self,
  83. command: Union[List[str], Tuple[str, ...], str],
  84. status: Union[str, int, None, Exception] = None,
  85. stderr: Union[bytes, str, None] = None,
  86. stdout: Union[bytes, str, None] = None,
  87. ) -> None:
  88. super(GitCommandError, self).__init__(command, status, stderr, stdout)
  89. class CheckoutError(GitError):
  90. """Thrown if a file could not be checked out from the index as it contained
  91. changes.
  92. The .failed_files attribute contains a list of relative paths that failed
  93. to be checked out as they contained changes that did not exist in the index.
  94. The .failed_reasons attribute contains a string informing about the actual
  95. cause of the issue.
  96. The .valid_files attribute contains a list of relative paths to files that
  97. were checked out successfully and hence match the version stored in the
  98. index"""
  99. def __init__(
  100. self,
  101. message: str,
  102. failed_files: Sequence[PathLike],
  103. valid_files: Sequence[PathLike],
  104. failed_reasons: List[str],
  105. ) -> None:
  106. Exception.__init__(self, message)
  107. self.failed_files = failed_files
  108. self.failed_reasons = failed_reasons
  109. self.valid_files = valid_files
  110. def __str__(self) -> str:
  111. return Exception.__str__(self) + ":%s" % self.failed_files
  112. class CacheError(GitError):
  113. """Base for all errors related to the git index, which is called cache internally"""
  114. class UnmergedEntriesError(CacheError):
  115. """Thrown if an operation cannot proceed as there are still unmerged
  116. entries in the cache"""
  117. class HookExecutionError(CommandError):
  118. """Thrown if a hook exits with a non-zero exit code. It provides access to the exit code and the string returned
  119. via standard output"""
  120. def __init__(
  121. self,
  122. command: Union[List[str], Tuple[str, ...], str],
  123. status: Union[str, int, None, Exception],
  124. stderr: Union[bytes, str, None] = None,
  125. stdout: Union[bytes, str, None] = None,
  126. ) -> None:
  127. super(HookExecutionError, self).__init__(command, status, stderr, stdout)
  128. self._msg = "Hook('%s') failed%s"
  129. class RepositoryDirtyError(GitError):
  130. """Thrown whenever an operation on a repository fails as it has uncommitted changes that would be overwritten"""
  131. def __init__(self, repo: "Repo", message: str) -> None:
  132. self.repo = repo
  133. self.message = message
  134. def __str__(self) -> str:
  135. return "Operation cannot be performed on %r: %s" % (self.repo, self.message)