-
-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite
test_git_hooks.py
to be hermetic (#8085)
This test failed when remoting because it relied on access to several scripts in `build-support/bin` that were not explicitly declared. This is fixed by explicitly depending on the scripts. We must jump through some hoops to then copy the scripts into the temporary workdir created by the tests.
- Loading branch information
1 parent
b5d41da
commit 44c2741
Showing
5 changed files
with
73 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import re | ||
import subprocess | ||
from contextlib import contextmanager | ||
from typing import Iterator, Optional | ||
|
||
from pants.base.revision import Revision | ||
from pants.scm.git import Git | ||
|
@@ -13,50 +14,45 @@ | |
MIN_REQUIRED_GIT_VERSION = Revision.semver('1.7.10') | ||
|
||
|
||
def git_version(): | ||
def git_version() -> Revision: | ||
"""Get a Version() based on installed command-line git's version""" | ||
process = subprocess.Popen(['git', '--version'], stdout=subprocess.PIPE) | ||
(stdout, stderr) = process.communicate() | ||
assert process.returncode == 0, "Failed to determine git version." | ||
stdout = subprocess.run( | ||
['git', '--version'], stdout=subprocess.PIPE, encoding="utf-8", check=True | ||
).stdout | ||
# stdout is like 'git version 1.9.1.598.g9119e8b\n' We want '1.9.1.598' | ||
matches = re.search(r'\s(\d+(?:\.\d+)*)[\s\.]', stdout.decode()) | ||
matches = re.search(r'\s(\d+(?:\.\d+)*)[\s\.]', stdout) | ||
if matches is None: | ||
raise ValueError(f"Not able to parse git version from {stdout}.") | ||
return Revision.lenient(matches.group(1)) | ||
|
||
|
||
def get_repo_root(): | ||
"""Return the absolute path to the root directory of the Pants git repo.""" | ||
return subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).strip().decode() | ||
|
||
|
||
@contextmanager | ||
def initialize_repo(worktree, gitdir=None): | ||
def initialize_repo(worktree: str, *, gitdir: Optional[str] = None) -> Iterator[Git]: | ||
"""Initialize a git repository for the given `worktree`. | ||
NB: The given `worktree` must contain at least one file which will be committed to form an initial | ||
commit. | ||
:param string worktree: The path to the git work tree. | ||
:param string gitdir: An optional path to the `.git` dir to use. | ||
:param worktree: The path to the git work tree. | ||
:param gitdir: An optional path to the `.git` dir to use. | ||
:returns: A `Git` repository object that can be used to interact with the repo. | ||
:rtype: :class:`pants.scm.git.Git` | ||
""" | ||
@contextmanager | ||
def use_gitdir(): | ||
def use_gitdir() -> Iterator[str]: | ||
if gitdir: | ||
yield gitdir | ||
else: | ||
with temporary_dir() as d: | ||
yield d | ||
|
||
with use_gitdir() as git_dir, environment_as(GIT_DIR=git_dir, GIT_WORK_TREE=worktree): | ||
subprocess.check_call(['git', 'init']) | ||
subprocess.check_call(['git', 'config', 'user.email', '[email protected]']) | ||
subprocess.run(['git', 'init'], check=True) | ||
subprocess.run(['git', 'config', 'user.email', '[email protected]'], check=True) | ||
# TODO: This method inherits the global git settings, so if a developer has gpg signing on, this | ||
# will turn that off. We should probably just disable reading from the global config somehow: | ||
# https://git-scm.com/docs/git-config. | ||
subprocess.check_call(['git', 'config', 'commit.gpgSign', 'false']) | ||
subprocess.check_call(['git', 'config', 'user.name', 'Your Name']) | ||
subprocess.check_call(['git', 'add', '.']) | ||
subprocess.check_call(['git', 'commit', '-am', 'Add project files.']) | ||
|
||
subprocess.run(['git', 'config', 'commit.gpgSign', 'false'], check=True) | ||
subprocess.run(['git', 'config', 'user.name', 'Your Name'], check=True) | ||
subprocess.run(['git', 'add', '.'], check=True) | ||
subprocess.run(['git', 'commit', '-am', 'Add project files.'], check=True) | ||
yield Git(gitdir=git_dir, worktree=worktree) |