From e085dca4d40770ec03b06c60c8b22c96b42bd794 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Sat, 22 Oct 2022 12:09:53 +0300 Subject: [PATCH 1/6] Suppress git warnings about default branch name Suppresses hint: Using 'master' as the name for the initial branch. This default branch name hint: is subject to change. To configure the initial branch name to use in all hint: of your new repositories, which will suppress this warning, call: hint: hint: git config --global init.defaultBranch hint: hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and hint: 'development'. The just-created branch can be renamed via this command: hint: hint: git branch -m This requires Git 2.28 or newer. --- tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests.py b/tests.py index fcd9221..8f22153 100644 --- a/tests.py +++ b/tests.py @@ -1053,7 +1053,7 @@ class GitHelper(VCSHelper): command = 'git' def _init_vcs(self): - self._run('git', 'init') + self._run('git', 'init', '-b', 'main') self._run('git', 'config', 'user.name', 'Unit Test') self._run('git', 'config', 'user.email', 'test@example.com') From d281624f661565b9ddad371053ba61725b4ab2d8 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Sat, 22 Oct 2022 12:20:34 +0300 Subject: [PATCH 2/6] Restore support for git < 2.28 by checking version number git init -b/--initial-branch was added in git 2.28. --- tests.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/tests.py b/tests.py index 8f22153..9416721 100644 --- a/tests.py +++ b/tests.py @@ -2,6 +2,7 @@ import locale import os import posixpath +import re import shutil import subprocess import sys @@ -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: @@ -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) @@ -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') From b3295821e3f9393586fc35a64ed7945f7be2d9e4 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Sat, 22 Oct 2022 12:28:41 +0300 Subject: [PATCH 3/6] Fix tests for modern Git that forbids file:// by default Git 2.30.6 (and 2.31.5, and 2.32.4, and 2.33.5, and 2.34.5, and 2.35.5, and 2.36.3, and 2.37.4, and 2.38.1, and I suppose the forthcoming 2.39.0) forbids file:// protocols by default for git submodules, as a security measure. See https://github.com/git/git/commit/a1d4f67c12ac172f835e6d5e4e0a197075e2146b for details. --- tests.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests.py b/tests.py index 9416721..deba964 100644 --- a/tests.py +++ b/tests.py @@ -1069,6 +1069,7 @@ def _init_vcs(self): self._run('git', 'init') self._run('git', 'config', 'user.name', 'Unit Test') self._run('git', 'config', 'user.email', 'test@example.com') + self._run('git', 'config', 'protocol.http.allow', 'always') def _add_to_vcs(self, filenames): # Note that we use --force to prevent errors when we want to From 227fb10c4353959357dccfdf3ae0694b2ea25c80 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Sat, 22 Oct 2022 12:42:31 +0300 Subject: [PATCH 4/6] Spell the name of the file: protocol correctly --- tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests.py b/tests.py index deba964..61e9ccd 100644 --- a/tests.py +++ b/tests.py @@ -1069,7 +1069,7 @@ def _init_vcs(self): self._run('git', 'init') self._run('git', 'config', 'user.name', 'Unit Test') self._run('git', 'config', 'user.email', 'test@example.com') - self._run('git', 'config', 'protocol.http.allow', 'always') + self._run('git', 'config', 'protocol.file.allow', 'always') def _add_to_vcs(self, filenames): # Note that we use --force to prevent errors when we want to From 3fc19891324c7db46a54a141eb2acb70c06b55e8 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Sat, 22 Oct 2022 12:46:33 +0300 Subject: [PATCH 5/6] git config doesn't work, try git -c --- tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests.py b/tests.py index 61e9ccd..8ec42f5 100644 --- a/tests.py +++ b/tests.py @@ -1069,7 +1069,6 @@ def _init_vcs(self): self._run('git', 'init') self._run('git', 'config', 'user.name', 'Unit Test') self._run('git', 'config', 'user.email', 'test@example.com') - self._run('git', 'config', 'protocol.file.allow', 'always') def _add_to_vcs(self, filenames): # Note that we use --force to prevent errors when we want to @@ -1094,7 +1093,8 @@ def _init_repo_with_files(self, dirname, filenames): def _add_submodule(self, repo, subdir, subrepo): os.chdir(repo) - self.vcs._run('git', 'submodule', 'add', subrepo, subdir) + self.vcs._run('git', '-c', 'protocol.file.allow=always', + 'submodule', 'add', subrepo, subdir) self._commit() os.chdir(self.tmpdir) From 1ce75c466c218a23082d685d34377cfbbe35f413 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Sat, 22 Oct 2022 12:54:36 +0300 Subject: [PATCH 6/6] Try a different workaround If we set the environment variable maybe we won't need to pass the -c to multiple commands. (The previous workaround fixed the git submodule add, but git submodule update --init --recursive remained broken). --- tests.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests.py b/tests.py index 8ec42f5..6f05d3f 100644 --- a/tests.py +++ b/tests.py @@ -929,6 +929,7 @@ class VCSHelper: # override in subclasses command = None # type: Optional[str] + extra_env = {} @property def version(self): @@ -959,7 +960,8 @@ def _run(self, *command): command = [s.encode(locale.getpreferredencoding()) for s in command] print('$', ' '.join(command)) p = subprocess.Popen(command, stdout=subprocess.PIPE, - stderr=subprocess.STDOUT) + stderr=subprocess.STDOUT, + env={**os.environ, **self.extra_env}) stdout, stderr = p.communicate() rc = p.wait() if stdout: @@ -1061,6 +1063,9 @@ def test_get_vcs_files_empty(self): class GitHelper(VCSHelper): command = 'git' + extra_env = dict( + GIT_ALLOW_PROTOCOL='file', + ) def _init_vcs(self): if self.version_tuple >= (2, 28): @@ -1093,8 +1098,7 @@ def _init_repo_with_files(self, dirname, filenames): def _add_submodule(self, repo, subdir, subrepo): os.chdir(repo) - self.vcs._run('git', '-c', 'protocol.file.allow=always', - 'submodule', 'add', subrepo, subdir) + self.vcs._run('git', 'submodule', 'add', subrepo, subdir) self._commit() os.chdir(self.tmpdir)