Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: retrieve remote default branch #283

Merged
merged 13 commits into from
Jul 9, 2024
21 changes: 18 additions & 3 deletions helm-git-plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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}"
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions tests/04-uri-validation.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Loading