@@ -881,7 +881,59 @@ update-deps-in-gomod() {
881881}
882882
883883gomod-pseudo-version () {
884- TZ=GMT git show -q --pretty=' format:v0.0.0-%cd-%h' --date=' format-local:%Y%m%d%H%M%S' --abbrev=12
884+ local commit_sha
885+ commit_sha=" $( git rev-parse --short=12 HEAD) "
886+
887+ local commit_ts
888+ commit_ts=" $( TZ=UTC git show -s --date=' format-local:%Y%m%d%H%M%S' --format=%cd HEAD) "
889+
890+ local tag
891+ tag=" $( git describe --tags --abbrev=0 --match ' v[0-9]*' 2> /dev/null || true) "
892+
893+ # tag does not exist at all -> v0.0.0-<timestamp>-<hash>
894+ if [[ -z " ${tag:- } " ]]; then
895+ echo " v0.0.0-${commit_ts} -${commit_sha} "
896+ return
897+ fi
898+
899+ local head_rev
900+ head_rev=" $( git rev-parse HEAD) "
901+ local tag_rev
902+ tag_rev=" $( git rev-list -n1 " $tag " ) "
903+
904+ # tag on head -> tag
905+ if [[ " $head_rev " == " $tag_rev " ]]; then
906+ echo " $tag "
907+ return
908+ fi
909+
910+ local current_major
911+ current_major=" $( grep ' ^module ' go.mod | sed -E ' s|^module .*/(v[0-9]+)$|\1|; t; s|.*|v0|' ) "
912+
913+ # head is not a tag ->
914+ # - the latest available tag is vX.Y.Z -> vX.Y.(Z+1)-0.<timestamp>-<hash>
915+ # - the latest available tag is vX.Y.Z-PR -> vX.Y.Z-PR.0.<timestamp>-<hash>
916+ if [[ " $tag " =~ ^v([0-9]+)\. ([0-9]+)\. ([0-9]+)(-.+)? $ ]]; then
917+ local major=" ${BASH_REMATCH[1]} "
918+ local minor=" ${BASH_REMATCH[2]} "
919+ local patch=" ${BASH_REMATCH[3]} "
920+ local pre=" ${BASH_REMATCH[4]} "
921+
922+ # if go.mod major matches the latest tag major
923+ if [[ " $current_major " == " v$major " ]]; then
924+ if [[ -z " ${pre} " ]]; then
925+ echo " v${major} .${minor} .$(( patch+ 1 )) -0.${commit_ts} -${commit_sha} "
926+ else
927+ echo " ${tag} .0.${commit_ts} -${commit_sha} "
928+ fi
929+ # if go.mod major does not match the latest tag major, then logic similar to v0.0.0 is used
930+ else
931+ echo " ${current_major} .0.0-${commit_ts} -${commit_sha} "
932+ fi
933+ else
934+ # the latest tag is not semver -> ignore it
935+ echo " ${current_major} .0.0-${commit_ts} -${commit_sha} "
936+ fi
885937}
886938
887939# checkout the dependencies to the versions corresponding to the kube commit of HEAD
0 commit comments