Fix tests after "use new main branch"

Tests worked fine if init.defaultBranch main was used,
but failed due to git branch reasons if master was still used.

Since we can only use init.defaultBranch if git version >= 2.28,
I also went with a template dir HEAD main tweak if lower so tests
now pass regardless of client git default branch and version.

Test: Ran tests with ~/.gitconfig:init.defaultBranch=master
Test: Ran tests with ~/.gitconfig:init.defaultBranch=main
Test: Ran tests for both code branches of git require

Change-Id: I49fa1e4ae45b8aec16a093132ee9fa466cbc11ec
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/290404
Tested-by: Fredrik de Groot <fredrik.de.groot@volvocars.com>
Reviewed-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Fredrik de Groot 2020-12-01 15:58:53 +01:00
parent 9dfd69f773
commit 6342d56914
2 changed files with 28 additions and 2 deletions

View File

@ -26,6 +26,7 @@ import tempfile
import unittest import unittest
import error import error
import git_command
import git_config import git_config
import platform_utils import platform_utils
import project import project
@ -38,7 +39,19 @@ def TempGitTree():
# Python 2 support entirely. # Python 2 support entirely.
try: try:
tempdir = tempfile.mkdtemp(prefix='repo-tests') tempdir = tempfile.mkdtemp(prefix='repo-tests')
subprocess.check_call(['git', 'init'], cwd=tempdir)
# Tests need to assume, that main is default branch at init,
# which is not supported in config until 2.28.
cmd = ['git', 'init']
if git_command.git_require((2, 28, 0)):
cmd += ['--initial-branch=main']
else:
# Use template dir for init.
templatedir = tempfile.mkdtemp(prefix='.test-template')
with open(os.path.join(templatedir, 'HEAD'), 'w') as fp:
fp.write('ref: refs/heads/main\n')
cmd += ['--template=', templatedir]
subprocess.check_call(cmd, cwd=tempdir)
yield tempdir yield tempdir
finally: finally:
platform_utils.rmtree(tempdir) platform_utils.rmtree(tempdir)

View File

@ -25,6 +25,7 @@ import shutil
import tempfile import tempfile
import unittest import unittest
import git_command
import platform_utils import platform_utils
from pyversion import is_python3 from pyversion import is_python3
import wrapper import wrapper
@ -357,7 +358,19 @@ class GitCheckoutTestCase(RepoWrapperTestCase):
remote = os.path.join(cls.GIT_DIR, 'remote') remote = os.path.join(cls.GIT_DIR, 'remote')
os.mkdir(remote) os.mkdir(remote)
run_git('init', cwd=remote)
# Tests need to assume, that main is default branch at init,
# which is not supported in config until 2.28.
if git_command.git_require((2, 28, 0)):
initstr = '--initial-branch=main'
else:
# Use template dir for init.
templatedir = tempfile.mkdtemp(prefix='.test-template')
with open(os.path.join(templatedir, 'HEAD'), 'w') as fp:
fp.write('ref: refs/heads/main\n')
initstr = '--template=' + templatedir
run_git('init', initstr, cwd=remote)
run_git('commit', '--allow-empty', '-minit', cwd=remote) run_git('commit', '--allow-empty', '-minit', cwd=remote)
run_git('branch', 'stable', cwd=remote) run_git('branch', 'stable', cwd=remote)
run_git('tag', 'v1.0', cwd=remote) run_git('tag', 'v1.0', cwd=remote)