From f828a048158643de94905cea7ccc190d07a8ff0f Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 18:11:59 +0200 Subject: [PATCH 01/13] feat: retrieve remote default branch --- helm-git-plugin.sh | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index 68b5a163..cf180702 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -82,11 +82,24 @@ git_try() { GIT_TERMINAL_PROMPT=0 git ls-remote "$_git_repo" --refs >"${git_output}" 2>&1 || return 1 } +#git_fetch_ref(git_repo_path) +git_fetch_default_branch() { + _git_repo_path="${1?Missing git_repo_path as first parameter}" + + # Fetch default branch + git rev-parse --abbrev-ref origin/HEAD | cut -c8- +} + #git_fetch_ref(git_repo_path, git_ref) git_fetch_ref() { _git_repo_path="${1?Missing git_repo_path as first parameter}" _git_ref="${2?Mising git_ref as second parameter}" + # Retrieve default branch if no ref is given + if [ -z "$_git_ref" ]; then + _git_ref=$(git_fetch_default_branch "$_git_repo_path") + fi + # Fetches any kind of ref to its right place, tags, annotated tags, branches and commit refs GIT_DIR="${_git_repo_path}" git fetch -u --depth=1 origin "refs/*/${_git_ref}:refs/*/${_git_ref}" "${_git_ref}" >"${git_output}" 2>&1 } @@ -122,7 +135,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}" @@ -305,10 +318,6 @@ 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" - fi readonly git_ref trace "git_ref: $git_ref" From 799af00e5a521a45ea8af9a33a6e4057a3ae3843 Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 18:13:01 +0200 Subject: [PATCH 02/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index cf180702..94b798fe 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -82,12 +82,12 @@ git_try() { GIT_TERMINAL_PROMPT=0 git ls-remote "$_git_repo" --refs >"${git_output}" 2>&1 || return 1 } -#git_fetch_ref(git_repo_path) -git_fetch_default_branch() { +#git_get_default_branch(git_repo_path) +git_get_default_branch() { _git_repo_path="${1?Missing git_repo_path as first parameter}" # Fetch default branch - git rev-parse --abbrev-ref origin/HEAD | cut -c8- + GIT_DIR="${_git_repo_path}" git rev-parse --abbrev-ref origin/HEAD | cut -c8- } #git_fetch_ref(git_repo_path, git_ref) @@ -97,7 +97,7 @@ git_fetch_ref() { # Retrieve default branch if no ref is given if [ -z "$_git_ref" ]; then - _git_ref=$(git_fetch_default_branch "$_git_repo_path") + _git_ref=$(git_get_default_branch "$_git_repo_path") fi # Fetches any kind of ref to its right place, tags, annotated tags, branches and commit refs From df9232f207091a6073901152eb9d2d793f9c5801 Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 18:15:01 +0200 Subject: [PATCH 03/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index 94b798fe..797295c7 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -87,6 +87,7 @@ git_get_default_branch() { _git_repo_path="${1?Missing git_repo_path as first parameter}" # Fetch default branch + echo $GIT_DIR; exit 1 GIT_DIR="${_git_repo_path}" git rev-parse --abbrev-ref origin/HEAD | cut -c8- } From fda36b3d58d0ed6a071bd0fa1673da84098732b8 Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 18:15:20 +0200 Subject: [PATCH 04/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index 797295c7..900ec15f 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -87,7 +87,7 @@ git_get_default_branch() { _git_repo_path="${1?Missing git_repo_path as first parameter}" # Fetch default branch - echo $GIT_DIR; exit 1 + debug $GIT_DIR; exit 1 GIT_DIR="${_git_repo_path}" git rev-parse --abbrev-ref origin/HEAD | cut -c8- } From e7153a72c3b80d8056df789d2f0b89553fce68f8 Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 18:15:51 +0200 Subject: [PATCH 05/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index 900ec15f..f666ca7b 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -87,7 +87,7 @@ git_get_default_branch() { _git_repo_path="${1?Missing git_repo_path as first parameter}" # Fetch default branch - debug $GIT_DIR; exit 1 + debug $_git_repo_path; exit 1 GIT_DIR="${_git_repo_path}" git rev-parse --abbrev-ref origin/HEAD | cut -c8- } From 2269244501a4efa2c7c7248f85725eaeb1b6ed9f Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 18:16:51 +0200 Subject: [PATCH 06/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index f666ca7b..d84558e6 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -386,7 +386,7 @@ main() { rm -rf "$git_root_path" "${helm_home_target_path:-}" ${CACHE_CHARTS} || rm -rf "${helm_target_path:-}" } - trap cleanup EXIT + # trap cleanup EXIT git_root_path="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")" readonly git_root_path="$git_root_path" From e47b916d9d9cb80e23d0ca93c8c3d6634243a331 Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 18:24:50 +0200 Subject: [PATCH 07/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index d84558e6..ba36a386 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -86,9 +86,8 @@ git_try() { git_get_default_branch() { _git_repo_path="${1?Missing git_repo_path as first parameter}" - # Fetch default branch - debug $_git_repo_path; exit 1 - GIT_DIR="${_git_repo_path}" git rev-parse --abbrev-ref origin/HEAD | cut -c8- + # Fetch default branch from remote + GIT_DIR="${_git_repo_path}" git ls-remote --symref origin HEAD 2>"${git_output}" | awk '/^ref:/ {sub(/refs\/heads\//, "", $2); print $2}' } #git_fetch_ref(git_repo_path, git_ref) From 82fb8cc6dc8905b96b5974614c3f9b85c08faa8c Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 18:26:05 +0200 Subject: [PATCH 08/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index ba36a386..321dc54c 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -98,6 +98,7 @@ git_fetch_ref() { # Retrieve default branch if no ref is given if [ -z "$_git_ref" ]; then _git_ref=$(git_get_default_branch "$_git_repo_path") + debug "Found '$_git_ref' as default branch" fi # Fetches any kind of ref to its right place, tags, annotated tags, branches and commit refs From 4fd8515d2f5fdd6d9fda9fdf7e64f0c784ea0089 Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 18:26:29 +0200 Subject: [PATCH 09/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index 321dc54c..3e68e249 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -386,7 +386,7 @@ main() { rm -rf "$git_root_path" "${helm_home_target_path:-}" ${CACHE_CHARTS} || rm -rf "${helm_target_path:-}" } - # trap cleanup EXIT + trap cleanup EXIT git_root_path="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")" readonly git_root_path="$git_root_path" From 8099847ef403a27d635fa16a67fb25e7953ad24d Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 20:11:07 +0200 Subject: [PATCH 10/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index 3e68e249..30415b11 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -84,10 +84,11 @@ git_try() { #git_get_default_branch(git_repo_path) git_get_default_branch() { - _git_repo_path="${1?Missing git_repo_path as first parameter}" + _git_repo="${1?Missing git_repo as first parameter}" # Fetch default branch from remote - GIT_DIR="${_git_repo_path}" git ls-remote --symref origin HEAD 2>"${git_output}" | awk '/^ref:/ {sub(/refs\/heads\//, "", $2); print $2}' + _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) @@ -95,12 +96,6 @@ git_fetch_ref() { _git_repo_path="${1?Missing git_repo_path as first parameter}" _git_ref="${2?Mising git_ref as second parameter}" - # Retrieve default branch if no ref is given - if [ -z "$_git_ref" ]; then - _git_ref=$(git_get_default_branch "$_git_repo_path") - debug "Found '$_git_ref' as default branch" - fi - # Fetches any kind of ref to its right place, tags, annotated tags, branches and commit refs GIT_DIR="${_git_repo_path}" git fetch -u --depth=1 origin "refs/*/${_git_ref}:refs/*/${_git_ref}" "${_git_ref}" >"${git_output}" 2>&1 } @@ -320,6 +315,16 @@ parse_uri() { git_ref=$(echo "$_uri_query" | sed '/^.*ref=\([^&#]*\).*$/!d;s//\1/') # TODO: Validate git_ref readonly git_ref + if [ -z "$git_ref" ]; then + 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 remote default branch: '$git_ref'" + fi + fi trace "git_ref: $git_ref" git_sparse=$(echo "$_uri_query" | sed '/^.*sparse=\([^&#]*\).*$/!d;s//\1/') From 11e36ddb0cbf4a922528e6a21ce0556f2601d5bf Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 20:11:28 +0200 Subject: [PATCH 11/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index 30415b11..f22ae3dc 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -314,7 +314,6 @@ parse_uri() { git_ref=$(echo "$_uri_query" | sed '/^.*ref=\([^&#]*\).*$/!d;s//\1/') # TODO: Validate git_ref - readonly git_ref if [ -z "$git_ref" ]; then 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") @@ -325,6 +324,7 @@ parse_uri() { debug "Discovered remote default branch: '$git_ref'" fi fi + readonly git_ref trace "git_ref: $git_ref" git_sparse=$(echo "$_uri_query" | sed '/^.*sparse=\([^&#]*\).*$/!d;s//\1/') From 566db15123c50604ce407b6c057d029f512d84dd Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 20:14:16 +0200 Subject: [PATCH 12/13] Update helm-git-plugin.sh --- helm-git-plugin.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helm-git-plugin.sh b/helm-git-plugin.sh index f22ae3dc..fa9b983e 100755 --- a/helm-git-plugin.sh +++ b/helm-git-plugin.sh @@ -321,7 +321,7 @@ parse_uri() { 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 remote default branch: '$git_ref'" + debug "Discovered default branch from remote: '$git_ref'. Prefer to pin GIT ref in URI." fi fi readonly git_ref From 5a3043b8af5f14da753d5a8c2646b2daa82b9b3a Mon Sep 17 00:00:00 2001 From: Zadkiel AHARONIAN Date: Sun, 7 Jul 2024 20:35:09 +0200 Subject: [PATCH 13/13] Update 04-uri-validation.bats --- tests/04-uri-validation.bats | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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" {