From fcbc2eaa470dbd083d1a1be75dc79d71916d0a80 Mon Sep 17 00:00:00 2001 From: Aradhya Tripathi <67282231+Aradhya-Tripathi@users.noreply.github.com> Date: Thu, 8 Sep 2022 11:27:34 +0530 Subject: [PATCH] fix: no-git support for dependency resolver (#1368) * feat: introducing support for no-git refactor: lint * feat: added support for validating git repos * refactor: removed formatting changes --- bench/app.py | 23 ++++++++++++++++++++--- bench/bench.py | 28 ++++++++++++++++++---------- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/bench/app.py b/bench/app.py index aae317a3..0ef69adb 100755 --- a/bench/app.py +++ b/bench/app.py @@ -1,5 +1,4 @@ # imports - standard imports -from functools import lru_cache import json import logging import os @@ -10,11 +9,12 @@ import sys import typing from collections import OrderedDict from datetime import date +from functools import lru_cache from urllib.parse import urlparse # imports - third party imports import click -from git import Repo +import git # imports - module imports import bench @@ -66,12 +66,21 @@ class AppMeta: self.branch = branch self.app_name = None self.git_repo = None + self.is_repo = ( + is_git_repo(app_path=get_repo_dir(self.name)) + if os.path.exists(get_repo_dir(self.name)) + else True + ) self.mount_path = os.path.abspath( os.path.join(urlparse(self.name).netloc, urlparse(self.name).path) ) self.setup_details() def setup_details(self): + # support for --no-git + if not self.is_repo: + self.repo = self.app_name = self.name + return # fetch meta from installed apps if self.bench and os.path.exists(os.path.join(self.bench.name, "apps", self.name)): self.mount_path = os.path.join(self.bench.name, "apps", self.name) @@ -99,7 +108,7 @@ class AppMeta: def _setup_details_from_mounted_disk(self): # If app is a git repo - self.git_repo = Repo(self.mount_path) + self.git_repo = git.Repo(self.mount_path) try: self._setup_details_from_git_url(self.git_repo.remotes[0].url) if not (self.branch or self.tag): @@ -663,6 +672,14 @@ def get_repo_dir(app, bench_path="."): return os.path.join(bench_path, "apps", app) +def is_git_repo(app_path): + try: + git.Repo(app_path, search_parent_directories=False) + return True + except git.exc.InvalidGitRepositoryError: + return False + + def install_apps_from_path(path, bench_path="."): apps = get_apps_json(path) for app in apps: diff --git a/bench/bench.py b/bench/bench.py index e4663aaf..8586c712 100644 --- a/bench/bench.py +++ b/bench/bench.py @@ -34,6 +34,7 @@ from bench.utils.bench import ( ) from bench.utils.render import job, step from bench.utils.app import get_current_version +from bench.app import is_git_repo if TYPE_CHECKING: @@ -222,21 +223,28 @@ class BenchApps(MutableSequence): version = get_current_version(app_name, self.bench.name) app_dir = os.path.join(self.apps_path, app_dir) - if not branch: - branch = ( - subprocess.check_output("git rev-parse --abbrev-ref HEAD", shell=True, cwd=app_dir) + is_repo = is_git_repo(app_dir) + if is_repo: + if not branch: + branch = ( + subprocess.check_output( + "git rev-parse --abbrev-ref HEAD", shell=True, cwd=app_dir + ) + .decode("utf-8") + .rstrip() + ) + + commit_hash = ( + subprocess.check_output(f"git rev-parse {branch}", shell=True, cwd=app_dir) .decode("utf-8") .rstrip() ) - commit_hash = ( - subprocess.check_output(f"git rev-parse {branch}", shell=True, cwd=app_dir) - .decode("utf-8") - .rstrip() - ) - self.states[app_name] = { - "resolution": {"commit_hash": commit_hash, "branch": branch}, + "is_repo": is_repo, + "resolution": "not a repo" + if not is_repo + else {"commit_hash": commit_hash, "branch": branch}, "required": required, "idx": len(self.states) + 1, "version": version,