Skip to content

Commit

Permalink
g.extension: fix extracting Git version string (#2929)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmszi authored Apr 25, 2023
1 parent c05cc14 commit 41288a6
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions scripts/g.extension/g.extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def __init__(
self,
addons=[],
url="https://github.com/osgeo/grass-addons",
git="git",
working_directory=None,
official_repository_structure=True,
major_grass_version=None,
Expand All @@ -227,6 +228,8 @@ def __init__(
):
#: Attribute containing list of addons names
self._addons = addons
#: Attribute containing Git command name
self._git = git
#: Attribute containing the URL to the online repository
self.url = url
self.major_grass_version = major_grass_version
Expand All @@ -240,6 +243,8 @@ def __init__(

# Check if working directory is writable
self.__check_permissions()
# Check if Git is installed
self._is_git_installed()

#: Attribute containing available branches
self.branches = self._get_branch_list()
Expand All @@ -256,15 +261,21 @@ def __init__(

def _get_version(self):
"""Get the installed git version"""
git_version = gs.Popen(["git", "--version"], stdout=PIPE)
return float(
".".join(
gs.decode(git_version.communicate()[0])
.rstrip()
.rsplit(" ", 1)[-1]
.split(".")[0:2]
git_version = gs.Popen([self._git, "--version"], stdout=PIPE, stderr=PIPE)
git_version, stderr = git_version.communicate()
if stderr:
gs.fatal(
_("Failed to get Git version.\n{error}").format(
gs.decode(stderr),
)
)
)
git_version = re.search(r"\d+.(\d+.\d+|\d+)", gs.decode(git_version))
if not git_version:
gs.fatal(_("Failed to get Git version."))
git_version = git_version.group()
if git_version.count(".") == 2:
git_version = git_version.rsplit(".", 1)[0]
return float(git_version)

def _initialize_clone(self):
"""Get a minimal working copy of a git repository without content"""
Expand All @@ -273,7 +284,7 @@ def _initialize_clone(self):
self.working_directory.mkdir(exist_ok=True, parents=True)
gs.call(
[
"git",
self._git,
"clone",
"-q",
"--no-checkout",
Expand All @@ -285,6 +296,13 @@ def _initialize_clone(self):
)
self.local_copy = self.working_directory / repo_directory

def _is_git_installed(self):
"""Check if Git command is installed"""
try:
gs.call([self._git], stdout=PIPE)
except OSError:
gs.fatal(_("Could not found Git. Please install it."))

def __check_permissions(self):
""""""
# Create working directory if it does not exist
Expand All @@ -305,7 +323,7 @@ def _get_branch_list(self):
addon repository
"""
branch_list = gs.Popen(
["git", "ls-remote", "--heads", self.url],
[self._git, "ls-remote", "--heads", self.url],
stdout=PIPE,
)
branch_list = gs.decode(branch_list.communicate()[0])
Expand All @@ -322,7 +340,7 @@ def _get_default_branch(self):
addon repository
"""
default_branch = gs.Popen(
["git", "symbolic-ref", "refs/remotes/origin/HEAD"],
[self._git, "symbolic-ref", "refs/remotes/origin/HEAD"],
cwd=self.local_copy,
stdout=PIPE,
)
Expand Down Expand Up @@ -366,7 +384,7 @@ def _get_addons_list(self):
"""Build a dictionary with addon name as key and path to directory with
Makefile in repository"""
file_list = gs.Popen(
["git", "ls-tree", "--name-only", "-r", self.branch],
[self._git, "ls-tree", "--name-only", "-r", self.branch],
cwd=self.local_copy,
stdout=PIPE,
stderr=PIPE,
Expand Down Expand Up @@ -410,20 +428,20 @@ def fetch_addons(self, addon_list, all_addons=False):
if addon_list:
if self.git_version >= 2.25 and not all_addons:
gs.call(
["git", "sparse-checkout", "init", "--cone"],
[self._git, "sparse-checkout", "init", "--cone"],
cwd=self.local_copy,
)
gs.call(
[
"git",
self._git,
"sparse-checkout",
"set",
*[self.addons[addon] for addon in addon_list],
],
cwd=self.local_copy,
)
gs.call(
["git", "checkout", self.branch],
[self._git, "checkout", self.branch],
cwd=self.local_copy,
)

Expand Down

0 comments on commit 41288a6

Please sign in to comment.