From a6db52b7b42cb0e2286f4df29441a72557326b68 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Sun, 8 Jan 2023 14:58:53 +0100 Subject: [PATCH] apps: don't clone 'master' branch by default, use git ls-remote to check what's the default branch instead --- src/app.py | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/app.py b/src/app.py index ed1432685..b1ae7410b 100644 --- a/src/app.py +++ b/src/app.py @@ -29,7 +29,7 @@ import subprocess import tempfile import copy from collections import OrderedDict -from typing import List, Tuple, Dict, Any, Iterator +from typing import List, Tuple, Dict, Any, Iterator, Optional from packaging import version from moulinette import Moulinette, m18n @@ -2300,19 +2300,19 @@ def _extract_app(src: str) -> Tuple[Dict, str]: url = app_info["git"]["url"] branch = app_info["git"]["branch"] revision = str(app_info["git"]["revision"]) - return _extract_app_from_gitrepo(url, branch, revision, app_info) + return _extract_app_from_gitrepo(url, branch=branch, revision=revision, app_info=app_info) # App is a git repo url elif _is_app_repo_url(src): url = src.strip().strip("/") - branch = "master" - revision = "HEAD" # gitlab urls may look like 'https://domain/org/group/repo/-/tree/testing' # compated to github urls looking like 'https://domain/org/repo/tree/testing' if "/-/" in url: url = url.replace("/-/", "/") if "/tree/" in url: url, branch = url.split("/tree/", 1) - return _extract_app_from_gitrepo(url, branch, revision, {}) + else: + branch = None + return _extract_app_from_gitrepo(url, branch=branch) # App is a local folder elif os.path.exists(src): return _extract_app_from_folder(src) @@ -2369,9 +2369,36 @@ def _extract_app_from_folder(path: str) -> Tuple[Dict, str]: def _extract_app_from_gitrepo( - url: str, branch: str, revision: str, app_info: Dict = {} + url: str, branch: Optional[str] = None, revision: str = "HEAD", app_info: Dict = {} ) -> Tuple[Dict, str]: + + logger.debug("Checking default branch") + + try: + git_remote_show = check_output(["git", "remote", "show", url], env={"GIT_TERMINAL_PROMPT": "0", "LC_ALL": "C"}, shell=False) + except Exception: + raise YunohostError("app_sources_fetch_failed") + + if not branch: + default_branch = None + try: + for line in git_remote_show.split('\n'): + if "HEAD branch:" in line: + default_branch = line.split()[-1] + except Exception: + pass + + if not default_branch: + logger.warning("Failed to parse default branch, trying 'main'") + branch = 'main' + else: + if default_branch in ['testing', 'dev']: + logger.warning(f"Trying 'master' branch instead of default '{default_branch}'") + branch = 'master' + else: + branch = default_branch + logger.debug(m18n.n("downloading")) extracted_app_folder = _make_tmp_workdir_for_app()