您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

133 行
4.8KB

  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 * # NOQA @UnusedWildImport
  8. from git.compat import UnicodeMixin, safe_decode, string_types
  9. class GitError(Exception):
  10. """ Base class for all package exceptions """
  11. class InvalidGitRepositoryError(GitError):
  12. """ Thrown if the given repository appears to have an invalid format. """
  13. class WorkTreeRepositoryUnsupported(InvalidGitRepositoryError):
  14. """ Thrown to indicate we can't handle work tree repositories """
  15. class NoSuchPathError(GitError, OSError):
  16. """ Thrown if a path could not be access by the system. """
  17. class CommandError(UnicodeMixin, GitError):
  18. """Base class for exceptions thrown at every stage of `Popen()` execution.
  19. :param command:
  20. A non-empty list of argv comprising the command-line.
  21. """
  22. #: A unicode print-format with 2 `%s for `<cmdline>` and the rest,
  23. #: e.g.
  24. #: u"'%s' failed%s"
  25. _msg = u"Cmd('%s') failed%s"
  26. def __init__(self, command, status=None, stderr=None, stdout=None):
  27. if not isinstance(command, (tuple, list)):
  28. command = command.split()
  29. self.command = command
  30. self.status = status
  31. if status:
  32. if isinstance(status, Exception):
  33. status = u"%s('%s')" % (type(status).__name__, safe_decode(str(status)))
  34. else:
  35. try:
  36. status = u'exit code(%s)' % int(status)
  37. except (ValueError, TypeError):
  38. s = safe_decode(str(status))
  39. status = u"'%s'" % s if isinstance(status, string_types) else s
  40. self._cmd = safe_decode(command[0])
  41. self._cmdline = u' '.join(safe_decode(i) for i in command)
  42. self._cause = status and u" due to: %s" % status or "!"
  43. self.stdout = stdout and u"\n stdout: '%s'" % safe_decode(stdout) or ''
  44. self.stderr = stderr and u"\n stderr: '%s'" % safe_decode(stderr) or ''
  45. def __unicode__(self):
  46. return (self._msg + "\n cmdline: %s%s%s") % (
  47. self._cmd, self._cause, self._cmdline, self.stdout, self.stderr)
  48. class GitCommandNotFound(CommandError):
  49. """Thrown if we cannot find the `git` executable in the PATH or at the path given by
  50. the GIT_PYTHON_GIT_EXECUTABLE environment variable"""
  51. def __init__(self, command, cause):
  52. super(GitCommandNotFound, self).__init__(command, cause)
  53. self._msg = u"Cmd('%s') not found%s"
  54. class GitCommandError(CommandError):
  55. """ Thrown if execution of the git command fails with non-zero status code. """
  56. def __init__(self, command, status, stderr=None, stdout=None):
  57. super(GitCommandError, self).__init__(command, status, stderr, stdout)
  58. class CheckoutError(GitError):
  59. """Thrown if a file could not be checked out from the index as it contained
  60. changes.
  61. The .failed_files attribute contains a list of relative paths that failed
  62. to be checked out as they contained changes that did not exist in the index.
  63. The .failed_reasons attribute contains a string informing about the actual
  64. cause of the issue.
  65. The .valid_files attribute contains a list of relative paths to files that
  66. were checked out successfully and hence match the version stored in the
  67. index"""
  68. def __init__(self, message, failed_files, valid_files, failed_reasons):
  69. Exception.__init__(self, message)
  70. self.failed_files = failed_files
  71. self.failed_reasons = failed_reasons
  72. self.valid_files = valid_files
  73. def __str__(self):
  74. return Exception.__str__(self) + ":%s" % self.failed_files
  75. class CacheError(GitError):
  76. """Base for all errors related to the git index, which is called cache internally"""
  77. class UnmergedEntriesError(CacheError):
  78. """Thrown if an operation cannot proceed as there are still unmerged
  79. entries in the cache"""
  80. class HookExecutionError(CommandError):
  81. """Thrown if a hook exits with a non-zero exit code. It provides access to the exit code and the string returned
  82. via standard output"""
  83. def __init__(self, command, status, stderr=None, stdout=None):
  84. super(HookExecutionError, self).__init__(command, status, stderr, stdout)
  85. self._msg = u"Hook('%s') failed%s"
  86. class RepositoryDirtyError(GitError):
  87. """Thrown whenever an operation on a repository fails as it has uncommitted changes that would be overwritten"""
  88. def __init__(self, repo, message):
  89. self.repo = repo
  90. self.message = message
  91. def __str__(self):
  92. return "Operation cannot be performed on %r: %s" % (self.repo, self.message)