Skip to content

Commit

Permalink
Restore support for git < 2.28 by checking version number
Browse files Browse the repository at this point in the history
git init -b/--initial-branch was added in git 2.28.
  • Loading branch information
mgedmin committed Oct 22, 2022
1 parent e085dca commit d281624
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions tests.py
Expand Up @@ -2,6 +2,7 @@
import locale
import os
import posixpath
import re
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -929,11 +930,23 @@ class VCSHelper:
# override in subclasses
command = None # type: Optional[str]

@property
def version(self):
if not hasattr(self, '_version'):
if not self.is_installed():
self._version = None
return self._version

@property
def version_tuple(self):
return tuple(map(int, re.findall(r'\d+', self.version)))

def is_installed(self):
try:
p = subprocess.Popen([self.command, '--version'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = p.communicate()
self._version = stdout.decode('ascii', 'backslashreplace').strip()
rc = p.wait()
return (rc == 0)
except OSError:
Expand All @@ -950,10 +963,7 @@ def _run(self, *command):
stdout, stderr = p.communicate()
rc = p.wait()
if stdout:
print(
stdout if isinstance(stdout, str) else
stdout.decode('ascii', 'backslashreplace')
)
print(stdout.decode('ascii', 'backslashreplace'))
if rc:
raise subprocess.CalledProcessError(rc, command[0], output=stdout)

Expand Down Expand Up @@ -1053,7 +1063,10 @@ class GitHelper(VCSHelper):
command = 'git'

def _init_vcs(self):
self._run('git', 'init', '-b', 'main')
if self.version_tuple >= (2, 28):
self._run('git', 'init', '-b', 'main')
else:
self._run('git', 'init')
self._run('git', 'config', 'user.name', 'Unit Test')
self._run('git', 'config', 'user.email', 'test@example.com')

Expand Down

0 comments on commit d281624

Please sign in to comment.