diff --git a/git_command.py b/git_command.py index 7ff1abac..d56ad0a8 100644 --- a/git_command.py +++ b/git_command.py @@ -68,6 +68,30 @@ class _GitCall(object): return fun git = _GitCall() +_git_version = None + +def git_require(min_version, fail=False): + global _git_version + + if _git_version is None: + ver_str = git.version() + if ver_str.startswith('git version '): + _git_version = tuple( + map(lambda x: int(x), + ver_str[len('git version '):].strip().split('.')[0:3] + )) + else: + print >>sys.stderr, 'fatal: "%s" unsupported' % ver_str + sys.exit(1) + + if min_version <= _git_version: + return True + if fail: + need = '.'.join(map(lambda x: str(x), min_version)) + print >>sys.stderr, 'fatal: git %s or later required' % need + sys.exit(1) + return False + class GitCommand(object): def __init__(self, project, diff --git a/subcmds/init.py b/subcmds/init.py index 5ba4d794..fbc406e2 100644 --- a/subcmds/init.py +++ b/subcmds/init.py @@ -20,7 +20,7 @@ from color import Coloring from command import InteractiveCommand, MirrorSafeCommand from error import ManifestParseError from project import SyncBuffer -from git_command import git, MIN_GIT_VERSION +from git_command import git_require, MIN_GIT_VERSION class Init(InteractiveCommand, MirrorSafeCommand): common = True @@ -85,19 +85,6 @@ to update the working directory files. dest='no_repo_verify', action='store_true', help='do not verify repo source code') - def _CheckGitVersion(self): - ver_str = git.version() - if not ver_str.startswith('git version '): - print >>sys.stderr, 'error: "%s" unsupported' % ver_str - sys.exit(1) - - ver_str = ver_str[len('git version '):].strip() - ver_act = tuple(map(lambda x: int(x), ver_str.split('.')[0:3])) - if ver_act < MIN_GIT_VERSION: - need = '.'.join(map(lambda x: str(x), MIN_GIT_VERSION)) - print >>sys.stderr, 'fatal: git %s or later required' % need - sys.exit(1) - def _SyncManifest(self, opt): m = self.manifest.manifestProject is_new = not m.Exists @@ -214,7 +201,7 @@ to update the working directory files. gc.SetString('color.ui', 'auto') def Execute(self, opt, args): - self._CheckGitVersion() + git_require(MIN_GIT_VERSION, fail=True) self._SyncManifest(opt) self._LinkManifest(opt.manifest_name)