Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(dependencies): Adding git proper git config management for deps #69

Merged
merged 6 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions devservices/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@
DEVSERVICES_LOCAL_DIR = os.path.expanduser("~/.local/share/sentry-devservices")
DEVSERVICES_LOCAL_DEPENDENCIES_DIR = os.path.join(DEVSERVICES_LOCAL_DIR, "dependencies")
DEVSERVICES_LOCAL_DEPENDENCIES_DIR_KEY = "DEVSERVICES_LOCAL_DEPENDENCIES_DIR"

DEPENDENCY_CONFIG_VERSION = "v1"
DEPENDENCY_GIT_PARTIAL_CLONE_CONFIG_OPTIONS = {
"protocol.version": "2",
"extensions.partialClone": "true",
"core.sparseCheckout": "true",
}
12 changes: 12 additions & 0 deletions devservices/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@ def __init__(self, repo_name: str, repo_link: str, branch: str):
self.repo_name = repo_name
self.repo_link = repo_link
self.branch = branch


class GitConfigError(Exception):
"""Base class for git config related errors."""

pass


class FailedToSetGitConfigError(GitConfigError):
"""Raised when a git config cannot be set."""

pass
128 changes: 112 additions & 16 deletions devservices/utils/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,83 @@
from devservices.configs.service_config import Dependency
from devservices.configs.service_config import RemoteConfig
from devservices.constants import CONFIG_FILE_NAME
from devservices.constants import DEPENDENCY_CONFIG_VERSION
from devservices.constants import DEPENDENCY_GIT_PARTIAL_CLONE_CONFIG_OPTIONS
from devservices.constants import DEVSERVICES_DIR_NAME
from devservices.constants import DEVSERVICES_LOCAL_DEPENDENCIES_DIR
from devservices.exceptions import DependencyError
from devservices.exceptions import FailedToSetGitConfigError


class SparseCheckoutManager:
"""
Manages sparse checkout for a repo
"""

def __init__(self, repo_dir: str):
self.repo_dir = repo_dir

def init_sparse_checkout(self) -> None:
"""
Initialize sparse checkout for the repo
"""
_run_command(["git", "sparse-checkout", "init"], cwd=self.repo_dir)

def set_sparse_checkout(self, pattern: str) -> None:
"""
Set sparse checkout patterns for the repo
"""
self.init_sparse_checkout()
_run_command(["git", "sparse-checkout", "set", pattern], cwd=self.repo_dir)

def clear_sparse_checkout(self) -> None:
"""
Clear sparse checkout for the repo
"""
try:
_run_command(["git", "sparse-checkout", "clear"], cwd=self.repo_dir)
except subprocess.CalledProcessError:
# Ignore if it fails as it might not be set
IanWoodard marked this conversation as resolved.
Show resolved Hide resolved
pass


class GitConfigManager:
"""
Manages git config for a repo
"""

def __init__(
self,
repo_dir: str,
config_options: dict[str, str],
sparse_pattern: str | None = None,
) -> None:
self.repo_dir = repo_dir
self.config_options = config_options
self.sparse_pattern = sparse_pattern
self.sparse_checkout_manager = SparseCheckoutManager(repo_dir)

def ensure_config(self) -> None:
"""
Ensure that the git config is set correctly for the repo
"""
# Otherwise, set the config options
for key, value in self.config_options.items():
self._set_config(key, value)

if self.sparse_pattern:
# Clear the sparse checkout if it's already set (to avoid conflicts)
self.sparse_checkout_manager.clear_sparse_checkout()
self.sparse_checkout_manager.set_sparse_checkout(self.sparse_pattern)

def _set_config(self, key: str, value: str) -> None:
"""
Set a git config option for the repo
"""
try:
_run_command(["git", "config", key, value], cwd=self.repo_dir)
except subprocess.CalledProcessError as e:
raise FailedToSetGitConfigError from e


def verify_local_dependencies(dependencies: list[Dependency]) -> bool:
Expand All @@ -30,6 +104,7 @@ def verify_local_dependencies(dependencies: list[Dependency]) -> bool:
os.path.exists(
os.path.join(
DEVSERVICES_LOCAL_DEPENDENCIES_DIR,
DEPENDENCY_CONFIG_VERSION,
remote_config.repo_name,
DEVSERVICES_DIR_NAME,
CONFIG_FILE_NAME,
Expand Down Expand Up @@ -62,10 +137,16 @@ def install_dependencies(dependencies: list[Dependency]) -> None:

def install_dependency(dependency: RemoteConfig) -> None:
dependency_repo_dir = os.path.join(
DEVSERVICES_LOCAL_DEPENDENCIES_DIR, dependency.repo_name
DEVSERVICES_LOCAL_DEPENDENCIES_DIR,
DEPENDENCY_CONFIG_VERSION,
dependency.repo_name,
)

if os.path.exists(dependency_repo_dir) and _is_valid_repo(dependency_repo_dir):
if (
os.path.exists(dependency_repo_dir)
and _is_valid_repo(dependency_repo_dir)
and _has_valid_config_file(dependency_repo_dir)
):
_update_dependency(dependency, dependency_repo_dir)
else:
_checkout_dependency(dependency, dependency_repo_dir)
Expand All @@ -75,6 +156,19 @@ def _update_dependency(
dependency: RemoteConfig,
dependency_repo_dir: str,
) -> None:
git_config_manager = GitConfigManager(
dependency_repo_dir,
DEPENDENCY_GIT_PARTIAL_CLONE_CONFIG_OPTIONS,
f"{DEVSERVICES_DIR_NAME}/",
IanWoodard marked this conversation as resolved.
Show resolved Hide resolved
)
try:
git_config_manager.ensure_config()
except FailedToSetGitConfigError as e:
raise DependencyError(
repo_name=dependency.repo_name,
repo_link=dependency.repo_link,
branch=dependency.branch,
) from e
try:
_run_command(
["git", "fetch", "origin", dependency.branch, "--filter=blob:none"],
Expand Down Expand Up @@ -128,22 +222,20 @@ def _checkout_dependency(
cwd=dependency_repo_dir,
)

# TODO: Should we include this with every command we run instead of setting config?
# Doing so avoids the issue of updating the config if we inevitably change something.
# Alternatively, we can version the dependency directory.
# Setup config for partial clone and sparse checkout
_run_command(["git", "config", "protocol.version", "2"], cwd=dependency_repo_dir)
_run_command(
["git", "config", "extensions.partialClone", "true"], cwd=dependency_repo_dir
)
_run_command(
["git", "config", "core.sparseCheckout", "true"], cwd=dependency_repo_dir
)
_run_command(["git", "sparse-checkout", "init"], cwd=dependency_repo_dir)
_run_command(
["git", "sparse-checkout", "set", f"{DEVSERVICES_DIR_NAME}/"],
cwd=dependency_repo_dir,
git_config_manager = GitConfigManager(
dependency_repo_dir,
DEPENDENCY_GIT_PARTIAL_CLONE_CONFIG_OPTIONS,
f"{DEVSERVICES_DIR_NAME}/",
)
try:
git_config_manager.ensure_config()
except FailedToSetGitConfigError as e:
raise DependencyError(
repo_name=dependency.repo_name,
repo_link=dependency.repo_link,
branch=dependency.branch,
) from e

_run_command(
["git", "checkout", dependency.branch],
Expand All @@ -161,6 +253,10 @@ def _is_valid_repo(path: str) -> bool:
return False


def _has_valid_config_file(path: str) -> bool:
return os.path.exists(os.path.join(path, DEVSERVICES_DIR_NAME, CONFIG_FILE_NAME))


def _get_remote_configs(dependencies: list[Dependency]) -> list[RemoteConfig]:
return [
dependency.remote
Expand Down
Loading