From 2bc9ed2ee1274dbecc1f600f20730b0254ce4c40 Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Tue, 9 Jul 2024 17:53:18 +0200 Subject: [PATCH] feat!: retrieve remote default branch (#283) --- helm-git-plugin.sh | 21 ++++++++++++++++++--- tests/04-uri-validation.bats | 4 ++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index 68b5a163..fa9b983e 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -82,6 +82,15 @@ git_try() { GIT_TERMINAL_PROMPT=0 git ls-remote "$_git_repo" --refs >"${git_output}" 2>&1 || return 1 } +#git_get_default_branch(git_repo_path) +git_get_default_branch() { + _git_repo="${1?Missing git_repo as first parameter}" + + # Fetch default branch from remote + _git_symref=$(GIT_TERMINAL_PROMPT=0 git ls-remote --symref "${_git_repo}" origin HEAD 2>"${git_output}") || return + echo "$_git_symref" | awk '/^ref:/ {sub(/refs\/heads\//, "", $2); print $2}' || return +} + #git_fetch_ref(git_repo_path, git_ref) git_fetch_ref() { _git_repo_path="${1?Missing git_repo_path as first parameter}" @@ -122,7 +131,7 @@ git_cache_intercept() { if [ -z "$(GIT_DIR="${repo_path}" git tag -l "${_git_ref}" 2>"${git_output}")" ]; then debug "Did not find ${_git_ref} in our cache for ${_git_repo}, fetching...." # This fetches properly tags, annotated tags, branches and commits that match the name and leave them at the right place - git_fetch_ref "${repo_path}" "${git_ref}" || + git_fetch_ref "${repo_path}" "${_git_ref}" || debug "Could not fetch ${_git_ref}" && return 1 else debug "Ref ${_git_ref} was already cached for ${_git_repo}" @@ -306,8 +315,14 @@ parse_uri() { git_ref=$(echo "$_uri_query" | sed '/^.*ref=\([^&#]*\).*$/!d;s//\1/') # TODO: Validate git_ref if [ -z "$git_ref" ]; then - warning "git_ref is empty, defaulted to 'master'. Prefer to pin GIT ref in URI." - git_ref="master" + warning "git_ref was not given, trying to discover default branch from remote. Prefer to pin GIT ref in URI." + git_ref=$(git_get_default_branch "$git_repo") + if [ -z "$git_ref" ]; then + warning "git_ref could not be discovered from remote. Defaulting to 'master'. Prefer to pin GIT ref in URI." + git_ref="master" + else + debug "Discovered default branch from remote: '$git_ref'. Prefer to pin GIT ref in URI." + fi fi readonly git_ref trace "git_ref: $git_ref" diff --git a/tests/04-uri-validation.bats b/tests/04-uri-validation.bats index 2e64d897..b61138db 100644 --- a/tests/04-uri-validation.bats +++ b/tests/04-uri-validation.bats @@ -30,13 +30,13 @@ load 'test-helper' @test "should fail and warning with bad path and no ref" { _run_helm_git "git+https://github.com/jetstack/cert-manager@contrib/charts/cert-manager/index.yaml?ref=" [ $status = 1 ] - [ -n "$(echo $output | grep "git_ref is empty")" ] + [ -n "$(echo $output | grep "git_ref was not given")" ] } @test "should success and warning with no ref" { _run_helm_git "git+https://github.com/jetstack/cert-manager@deploy/charts/index.yaml" [ $status = 0 ] - [ -n "$(echo $output | grep "git_ref is empty")" ] + [ -n "$(echo $output | grep "git_ref was not given")" ] } @test "should success with username" {