repo: all ParseGitVersion to load git version info itself

All code that calls ParseGitVersion needs to run `git --version`
itself and parse the output before passing it in.  To avoid that
duplication, allow ParseGitVersion to run `git --version` itself
if ver_str=None.

Bug: https://crbug.com/gerrit/11144
Change-Id: Ie07793ca57a40c0231af808df04a576118d5eea3
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/231054
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
This commit is contained in:
Mike Frysinger 2019-07-10 15:37:43 -04:00
parent 6db1b9e282
commit f88b2fe569

19
repo
View File

@ -383,7 +383,11 @@ def _Init(args, gitc_init=False):
GitVersion = collections.namedtuple(
'GitVersion', ('major', 'minor', 'micro', 'full'))
def ParseGitVersion(ver_str):
def ParseGitVersion(ver_str=None):
if ver_str is None:
# Load the version ourselves.
ver_str = _GetGitVersion()
if not ver_str.startswith('git version '):
return None
@ -399,7 +403,7 @@ def ParseGitVersion(ver_str):
return GitVersion(*to_tuple)
def _CheckGitVersion():
def _GetGitVersion():
cmd = [GIT, '--version']
try:
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
@ -410,13 +414,20 @@ def _CheckGitVersion():
print(file=sys.stderr)
print('Please make sure %s is installed and in your path.' % GIT,
file=sys.stderr)
raise CloneFailure()
raise
ver_str = proc.stdout.read().strip()
proc.stdout.close()
proc.wait()
return ver_str.decode('utf-8')
def _CheckGitVersion():
try:
ver_act = ParseGitVersion()
except OSError:
raise CloneFailure()
ver_act = ParseGitVersion(ver_str)
if ver_act is None:
print('error: "%s" unsupported' % ver_str, file=sys.stderr)
raise CloneFailure()