|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from os import PathLike |
| 4 | +from pathlib import Path |
| 5 | +import sys |
| 6 | + |
| 7 | +from pygit2 import init_repository, Oid |
| 8 | +from pygit2 import Repository as _Repository |
| 9 | + |
| 10 | +from ..constants import GL_REPOSITORY_URL |
| 11 | +from ..logger import LoggerSetup |
| 12 | + |
| 13 | + |
| 14 | +class Repository(_Repository): |
| 15 | + """ |
| 16 | + Repository operations handler based on the given Git directory. |
| 17 | +
|
| 18 | + :author: Garden Linux Maintainers |
| 19 | + :copyright: Copyright 2024 SAP SE |
| 20 | + :package: gardenlinux |
| 21 | + :subpackage: git |
| 22 | + :since: 0.10.0 |
| 23 | + :license: https://www.apache.org/licenses/LICENSE-2.0 |
| 24 | + Apache License, Version 2.0 |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, git_directory=".", logger=None, **kwargs): |
| 28 | + """ |
| 29 | + Constructor __init__(Repository) |
| 30 | +
|
| 31 | + :param git_directory: Git directory |
| 32 | + :param logger: Logger instance |
| 33 | +
|
| 34 | + :since: 0.10.0 |
| 35 | + """ |
| 36 | + |
| 37 | + if logger is None or not logger.hasHandlers(): |
| 38 | + logger = LoggerSetup.get_logger("gardenlinux.git") |
| 39 | + |
| 40 | + if not isinstance(git_directory, PathLike): |
| 41 | + git_directory = Path(git_directory) |
| 42 | + |
| 43 | + if not git_directory.exists(): |
| 44 | + raise RuntimeError(f"Git directory given is invalid: {git_directory}") |
| 45 | + |
| 46 | + _Repository.__init__(self, git_directory, **kwargs) |
| 47 | + |
| 48 | + self._git_directory = git_directory |
| 49 | + self._logger = logger |
| 50 | + |
| 51 | + @property |
| 52 | + def commit_id(self): |
| 53 | + """ |
| 54 | + Returns the commit ID for Git `HEAD`. |
| 55 | +
|
| 56 | + :return: (str) Git commit ID |
| 57 | + :since: 0.10.0 |
| 58 | + """ |
| 59 | + |
| 60 | + return str(self.root_repo.head.target) |
| 61 | + |
| 62 | + @property |
| 63 | + def root(self): |
| 64 | + """ |
| 65 | + Returns the root directory of the current Git repository. |
| 66 | +
|
| 67 | + :return: (object) Git root directory |
| 68 | + :since: 0.10.0 |
| 69 | + """ |
| 70 | + |
| 71 | + root_dir = self.workdir |
| 72 | + |
| 73 | + if self.is_bare: |
| 74 | + root_dir = self.path |
| 75 | + |
| 76 | + usual_git_dir = Path(root_dir, ".git") |
| 77 | + |
| 78 | + # Possible submodule Git repository. Validate repository containing `.git` directory. |
| 79 | + if self.path != str(usual_git_dir): |
| 80 | + try: |
| 81 | + repo = Repository(usual_git_dir, self._logger) |
| 82 | + |
| 83 | + if self.path != repo.path: |
| 84 | + root_dir = repo.root |
| 85 | + except Exception as exc: |
| 86 | + self._logger.warning(f"Failed to inspect Git directory: {exc}") |
| 87 | + |
| 88 | + self._logger.debug(f"Git root directory: {root_dir}") |
| 89 | + return Path(root_dir) |
| 90 | + |
| 91 | + @property |
| 92 | + def root_repo(self): |
| 93 | + """ |
| 94 | + Returns the root Git `Repository` instance. |
| 95 | +
|
| 96 | + :return: (object) Git root `Repository` instance |
| 97 | + :since: 0.10.0 |
| 98 | + """ |
| 99 | + |
| 100 | + repo = self |
| 101 | + |
| 102 | + if self._git_directory != self.root: |
| 103 | + repo = Repository(self.root, self._logger) |
| 104 | + |
| 105 | + return repo |
| 106 | + |
| 107 | + @staticmethod |
| 108 | + def checkout_repo( |
| 109 | + git_directory, |
| 110 | + repo_url=GL_REPOSITORY_URL, |
| 111 | + branch="main", |
| 112 | + commit=None, |
| 113 | + pathspecs=None, |
| 114 | + logger=None, |
| 115 | + **kwargs, |
| 116 | + ): |
| 117 | + """ |
| 118 | + Returns the root Git `Repo` instance. |
| 119 | +
|
| 120 | + :return: (object) Git `Repo` instance |
| 121 | + :since: 0.10.0 |
| 122 | + """ |
| 123 | + |
| 124 | + if not isinstance(git_directory, PathLike): |
| 125 | + git_directory = Path(git_directory) |
| 126 | + |
| 127 | + if not git_directory.is_dir() or git_directory.match("/*"): |
| 128 | + raise RuntimeError( |
| 129 | + "Git directory should not exist or be empty before checkout" |
| 130 | + ) |
| 131 | + |
| 132 | + repo = init_repository(git_directory, origin_url=repo_url) |
| 133 | + repo.remotes["origin"].fetch() |
| 134 | + |
| 135 | + if commit is None: |
| 136 | + refish = f"origin/{branch}" |
| 137 | + resolved = repo.resolve_refish(refish) |
| 138 | + commit = str(resolved[0].id) |
| 139 | + |
| 140 | + checkout_tree_kwargs = kwargs |
| 141 | + if pathspecs is not None: |
| 142 | + checkout_tree_kwargs["paths"] = pathspecs |
| 143 | + |
| 144 | + repo.checkout_tree(repo[Oid(hex=commit)], **checkout_tree_kwargs) |
| 145 | + |
| 146 | + return Repository(git_directory, logger) |
| 147 | + |
| 148 | + @staticmethod |
| 149 | + def checkout_repo_sparse( |
| 150 | + git_directory, |
| 151 | + pathspecs=[], |
| 152 | + repo_url=GL_REPOSITORY_URL, |
| 153 | + branch="main", |
| 154 | + commit=None, |
| 155 | + logger=None, |
| 156 | + **kwargs, |
| 157 | + ): |
| 158 | + """ |
| 159 | + Sparse checkout given Git repository and return the `Repository` instance. |
| 160 | +
|
| 161 | + :return: (object) Git `Repository` instance |
| 162 | + :since: 0.10.0 |
| 163 | + """ |
| 164 | + |
| 165 | + # @TODO: pygit2 does not support sparse checkouts. We use the `paths` parameter at the moment. |
| 166 | + return Repository.checkout_repo( |
| 167 | + git_directory, |
| 168 | + repo_url=repo_url, |
| 169 | + branch=branch, |
| 170 | + commit=commit, |
| 171 | + pathspecs=pathspecs, |
| 172 | + logger=logger, |
| 173 | + ) |
0 commit comments