diff options
| author | Jason Chang <jasonnc@google.com> | 2023-08-03 14:38:00 -0700 |
|---|---|---|
| committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-08-07 23:56:07 +0000 |
| commit | f9aacd4087b02948da9a7878da48ea186ab99d5a (patch) | |
| tree | b683190635cd6fcb7cf817837ad0c4259b53078f /git_command.py | |
| parent | b8a7b4a629c3435d77a3266a4e6dce51dc342bd9 (diff) | |
| download | git-repo-f9aacd4087b02948da9a7878da48ea186ab99d5a.tar.gz | |
Raise repo exit errors in place of sys.exit
Bug: b/293344017
Change-Id: I92d81c78eba8ff31b5252415f4c9a515a6c76411
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/381774
Tested-by: Jason Chang <jasonnc@google.com>
Reviewed-by: Joanna Wang <jojwang@google.com>
Commit-Queue: Jason Chang <jasonnc@google.com>
Diffstat (limited to 'git_command.py')
| -rw-r--r-- | git_command.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/git_command.py b/git_command.py index 588a64fd..36fcfe7c 100644 --- a/git_command.py +++ b/git_command.py | |||
| @@ -19,6 +19,7 @@ import subprocess | |||
| 19 | from typing import Any, Optional | 19 | from typing import Any, Optional |
| 20 | 20 | ||
| 21 | from error import GitError | 21 | from error import GitError |
| 22 | from error import RepoExitError | ||
| 22 | from git_refs import HEAD | 23 | from git_refs import HEAD |
| 23 | import platform_utils | 24 | import platform_utils |
| 24 | from repo_trace import REPO_TRACE, IsTrace, Trace | 25 | from repo_trace import REPO_TRACE, IsTrace, Trace |
| @@ -44,6 +45,7 @@ DEFAULT_GIT_FAIL_MESSAGE = "git command failure" | |||
| 44 | # Common line length limit | 45 | # Common line length limit |
| 45 | GIT_ERROR_STDOUT_LINES = 1 | 46 | GIT_ERROR_STDOUT_LINES = 1 |
| 46 | GIT_ERROR_STDERR_LINES = 1 | 47 | GIT_ERROR_STDERR_LINES = 1 |
| 48 | INVALID_GIT_EXIT_CODE = 126 | ||
| 47 | 49 | ||
| 48 | 50 | ||
| 49 | class _GitCall(object): | 51 | class _GitCall(object): |
| @@ -51,8 +53,9 @@ class _GitCall(object): | |||
| 51 | def version_tuple(self): | 53 | def version_tuple(self): |
| 52 | ret = Wrapper().ParseGitVersion() | 54 | ret = Wrapper().ParseGitVersion() |
| 53 | if ret is None: | 55 | if ret is None: |
| 54 | print("fatal: unable to detect git version", file=sys.stderr) | 56 | msg = "fatal: unable to detect git version" |
| 55 | sys.exit(1) | 57 | print(msg, file=sys.stderr) |
| 58 | raise GitRequireError(msg) | ||
| 56 | return ret | 59 | return ret |
| 57 | 60 | ||
| 58 | def __getattr__(self, name): | 61 | def __getattr__(self, name): |
| @@ -167,10 +170,9 @@ def git_require(min_version, fail=False, msg=""): | |||
| 167 | need = ".".join(map(str, min_version)) | 170 | need = ".".join(map(str, min_version)) |
| 168 | if msg: | 171 | if msg: |
| 169 | msg = " for " + msg | 172 | msg = " for " + msg |
| 170 | print( | 173 | error_msg = "fatal: git %s or later required%s" % (need, msg) |
| 171 | "fatal: git %s or later required%s" % (need, msg), file=sys.stderr | 174 | print(error_msg, file=sys.stderr) |
| 172 | ) | 175 | raise GitRequireError(error_msg) |
| 173 | sys.exit(1) | ||
| 174 | return False | 176 | return False |
| 175 | 177 | ||
| 176 | 178 | ||
| @@ -403,6 +405,13 @@ class GitCommand(object): | |||
| 403 | ) | 405 | ) |
| 404 | 406 | ||
| 405 | 407 | ||
| 408 | class GitRequireError(RepoExitError): | ||
| 409 | """Error raised when git version is unavailable or invalid.""" | ||
| 410 | |||
| 411 | def __init__(self, message, exit_code: int = INVALID_GIT_EXIT_CODE): | ||
| 412 | super().__init__(message, exit_code=exit_code) | ||
| 413 | |||
| 414 | |||
| 406 | class GitCommandError(GitError): | 415 | class GitCommandError(GitError): |
| 407 | """ | 416 | """ |
| 408 | Error raised from a failed git command. | 417 | Error raised from a failed git command. |
