@@ -887,6 +887,8 @@ update-deps-in-gomod() {
887887 ensure-clean-working-dir
888888}
889889
890+ # Reads the module major version from go.mod.
891+ # If no major version suffix is found, returns "v0".
890892gomod-module-major () {
891893 grep ' ^module ' go.mod | sed -E ' s|^module .*/(v[0-9]+)$|\1|; t; s|.*|v0|'
892894}
@@ -898,47 +900,68 @@ gomod-pseudo-version() {
898900 local commit_ts
899901 commit_ts=" $( TZ=UTC git show -s --date=' format-local:%Y%m%d%H%M%S' --format=%cd HEAD) "
900902
903+ # Get tag pointing at HEAD (the current commit), if any
901904 local commit_tag
902- commit_tag=" $( (git tag --points-at HEAD 2> /dev/null || true) | grep ' origin\/v' | sed ' s|^origin/||' | sort -V | tail -n1) "
903-
904- # latest commit has a tag -> tag
905- if [[ -n " ${commit_tag} " ]]; then
905+ commit_tag=" $( git tag --points-at HEAD 2> /dev/null || true) "
906+
907+ # We assume that tags will always be valid semver tags starting with 'v'.
908+ # Repositories cloned by the publishing-bot always have two remotes:
909+ # - origin: the published repository (e.g. github.com/kcp-dev/apimachinery)
910+ # - upstream: the local source repository (e.g. ../kcp)
911+ # We only consider tags from the published repository (origin).
912+ # Technically, we should never ever hit this case. That's because tags are synced
913+ # only after the initial publishing is done. We might eventually hit this case if
914+ # there are two different tags/versions on the same commit.
915+ if [[ -n " ${commit_tag:- } " ]]; then
916+ commit_tag=$( echo " ${commit_tag} " | grep ' origin\/v' | sed ' s|^origin/||' | sort -V | tail -n1)
906917 echo " ${commit_tag} "
907918 return
908919 fi
909-
920+
921+ # Get the latest tag from the published repository (origin).
922+ # This tag does not point at HEAD, otherwise the previous case would handle it.
910923 local latest_tag
911- latest_tag=" $( ( git ls-remote --tags origin 2> /dev/null || true) | awk -F/ ' {print $3} ' | grep -v ' \^{} ' | grep ' v ' | sort -V | tail -n1 ) "
924+ latest_tag=" $( git ls-remote --tags origin 2> /dev/null || true) "
912925
913- # tag does not exist at all -> v0.0.0-<timestamp>-<hash>
914926 if [[ -z " ${latest_tag:- } " ]]; then
927+ # No tag exists, generate and return a pseudo-version string.
915928 echo " v0.0.0-${commit_ts} -${commit_sha} "
916929 return
917930 fi
918931
932+ # "git ls-remote" returns a bit more data than needed, so we parse it take valid semver tag.
933+ latest_tag=" $( echo " ${latest_tag} " | awk -F/ ' {print $3}' | grep -v ' \^{}' | grep ' v' | sort -V | tail -n1) "
934+
935+ # This returns the module major version as defined in go.mod.
919936 local module_major
920937 module_major=" $( gomod-module-major) "
921938
922- # head is not a tag ->
923- # - the latest available tag is vX.Y.Z -> vX.Y.(Z+1)-0.<timestamp>-<hash>
924- # - the latest available tag is vX.Y.Z-PR -> vX.Y.Z-PR.0.<timestamp>-<hash>
939+ # Parse the latest tag and determine the semver elements.
925940 if [[ " $latest_tag " =~ ^v([0-9]+)\. ([0-9]+)\. ([0-9]+)(-.+)? $ ]]; then
926941 local major=" ${BASH_REMATCH[1]} "
927942 local minor=" ${BASH_REMATCH[2]} "
928943 local patch=" ${BASH_REMATCH[3]} "
929944 local pre=" ${BASH_REMATCH[4]} "
930945
946+ # If the module's major version matches the latest tag's major version.
931947 if [[ " $module_major " == " v$major " ]]; then
932948 if [[ -z " ${pre} " ]]; then
949+ # Stable tags are handled by incrementing the patch version
950+ # and appending the pseudo-version suffix.
933951 echo " v${major} .${minor} .$(( patch+ 1 )) -0.${commit_ts} -${commit_sha} "
934952 else
953+ # Pre-release tags are handled by appending the pseudo-version suffix
954+ # to the pre-release tag.
935955 echo " ${latest_tag} .0.${commit_ts} -${commit_sha} "
936956 fi
937957 else
958+ # Otherwise, Go handles this in a little strange way. It takes the latest tag
959+ # and appends "+incompatible" to it.
938960 echo " ${latest_tag} +incompatible"
939961 fi
940962 else
941- # the latest tag is not semver -> ignore it
963+ # If we hit this case, the latest tag is not a valid semver tag.
964+ # We just generate v0.0.0 pseudo-version string instead.
942965 echo " v0.0.0-${commit_ts} -${commit_sha} "
943966 fi
944967}
@@ -986,7 +1009,12 @@ checkout-deps-to-kube-commit() {
9861009 git checkout -q " ${dep_commit} "
9871010
9881011 local pseudo_version=$( gomod-pseudo-version)
1012+ local mod_major=$( gomod-module-major)
9891013 local cache_dir=" ${GOPATH} /pkg/mod/cache/download/${base_package} /${dep} /@v"
1014+ if [ " ${mod_major} " != " v0" ] && [ " ${mod_major} " != " v1" ]; then
1015+ cache_dir=" ${GOPATH} /pkg/mod/cache/download/${base_package} /${dep} /@v/${mod_major} "
1016+ fi
1017+
9901018 if [ -f " ${cache_dir} /list" ] && grep -q " ${pseudo_version} " " ${cache_dir} /list" ; then
9911019 echo " Pseudo version ${pseudo_version} is already packaged up."
9921020 else
0 commit comments