From f9aacd4087b02948da9a7878da48ea186ab99d5a Mon Sep 17 00:00:00 2001 From: Jason Chang Date: Thu, 3 Aug 2023 14:38:00 -0700 Subject: 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 Reviewed-by: Joanna Wang Commit-Queue: Jason Chang --- git_command.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'git_command.py') 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 from typing import Any, Optional from error import GitError +from error import RepoExitError from git_refs import HEAD import platform_utils from repo_trace import REPO_TRACE, IsTrace, Trace @@ -44,6 +45,7 @@ DEFAULT_GIT_FAIL_MESSAGE = "git command failure" # Common line length limit GIT_ERROR_STDOUT_LINES = 1 GIT_ERROR_STDERR_LINES = 1 +INVALID_GIT_EXIT_CODE = 126 class _GitCall(object): @@ -51,8 +53,9 @@ class _GitCall(object): def version_tuple(self): ret = Wrapper().ParseGitVersion() if ret is None: - print("fatal: unable to detect git version", file=sys.stderr) - sys.exit(1) + msg = "fatal: unable to detect git version" + print(msg, file=sys.stderr) + raise GitRequireError(msg) return ret def __getattr__(self, name): @@ -167,10 +170,9 @@ def git_require(min_version, fail=False, msg=""): need = ".".join(map(str, min_version)) if msg: msg = " for " + msg - print( - "fatal: git %s or later required%s" % (need, msg), file=sys.stderr - ) - sys.exit(1) + error_msg = "fatal: git %s or later required%s" % (need, msg) + print(error_msg, file=sys.stderr) + raise GitRequireError(error_msg) return False @@ -403,6 +405,13 @@ class GitCommand(object): ) +class GitRequireError(RepoExitError): + """Error raised when git version is unavailable or invalid.""" + + def __init__(self, message, exit_code: int = INVALID_GIT_EXIT_CODE): + super().__init__(message, exit_code=exit_code) + + class GitCommandError(GitError): """ Error raised from a failed git command. -- cgit v1.2.3-54-g00ecf