github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/mkversion.sh (about) 1 #!/bin/sh 2 set -e 3 4 # debugging if anything fails is tricky as dh-golang eats up all output 5 # uncomment the lines below to get a useful trace if you have to touch 6 # this again (my advice is: DON'T) 7 #set -x 8 #logfile=/tmp/mkversions.log 9 #exec >> $logfile 2>&1 10 #echo "env: $(set)" 11 #echo "mkversion.sh run from: $0" 12 #echo "pwd: $(pwd)" 13 14 # we have two directories we need to care about: 15 # - our toplevel pkg builddir which is where "mkversion.sh" is located 16 # and where "snap-confine" expects its cmd/VERSION file 17 # - the GO_GENERATE_BUILDDIR which may be the toplevel pkg dir. but 18 # during "dpkg-buildpackage" it will become a different _build/ dir 19 # that dh-golang creates and that only contains a subset of the 20 # files of the toplevel buildir. 21 PKG_BUILDDIR=$(dirname "$0") 22 GO_GENERATE_BUILDDIR="$(pwd)" 23 24 # run from "go generate" adjust path 25 if [ "$GOPACKAGE" = "snapdtool" ]; then 26 GO_GENERATE_BUILDDIR="$(pwd)/.." 27 fi 28 29 OUTPUT_ONLY=false 30 if [ "$1" = "--output-only" ]; then 31 OUTPUT_ONLY=true 32 shift 33 fi 34 35 # If the version is passed in as an argument to mkversion.sh, let's use that. 36 if [ -n "$1" ]; then 37 version_from_user="$1" 38 fi 39 40 DIRTY=false 41 42 # Let's try to derive the version from git.. 43 if command -v git >/dev/null; then 44 # don't include --dirty here as we independently track whether the tree is 45 # dirty and append that last, including it here will make dirty trees 46 # directly on top of tags show up with version_from_git as 2.46-dirty which 47 # will not match 2.46 from the changelog and then result in a final version 48 # like 2.46+git2.46.2.46 which is silly and unhelpful 49 # tracking the dirty independently like this will produce instead 2.46-dirty 50 # for a dirty tree on top of a tag, and 2.46+git83.g1671726-dirty for a 51 # commit not directly on top of a tag 52 version_from_git="$(git describe --always | sed -e 's/-/+git/;y/-/./' )" 53 54 # check if we are using a dirty tree 55 if git describe --always --dirty | grep -q dirty; then 56 DIRTY=true 57 fi 58 fi 59 60 # at this point we maybe in _build/src/github etc where we have no 61 # debian/changelog (dh-golang only exports the sources here) 62 # switch to the real source dir for the changelog parsing 63 if command -v dpkg-parsechangelog >/dev/null; then 64 version_from_changelog="$(cd "$PKG_BUILDDIR"; dpkg-parsechangelog --show-field Version)"; 65 fi 66 67 # select version based on priority 68 if [ -n "$version_from_user" ]; then 69 # version from user always wins 70 v="$version_from_user" 71 o="user" 72 elif [ -n "$version_from_git" ]; then 73 v="$version_from_git" 74 o="git" 75 elif [ -n "$version_from_changelog" ]; then 76 v="$version_from_changelog" 77 o="changelog" 78 else 79 echo "Cannot generate version" 80 exit 1 81 fi 82 83 # if we don't have a user provided version and if the version is not 84 # a release (i.e. the git tag does not match the debian changelog 85 # version) then we need to construct the version similar to how we do 86 # it in a packaging recipe. We take the debian version from the changelog 87 # and append the git revno and commit hash. A simpler approach would be 88 # to git tag all pre/rc releases. 89 if [ -z "$version_from_user" ] && [ "$version_from_git" != "" ] && \ 90 [ -n "$version_from_changelog" ] && [ "$version_from_git" != "$version_from_changelog" ]; then 91 # if the changelog version has "git" in it and we also have a git version 92 # directly, that is a bad changelog version, so fail, otherwise the below 93 # code will produce a duplicated git info 94 if echo "$version_from_changelog" | grep -q git; then 95 echo "Cannot generate version, there is a version from git and the changelog has a git version" 96 exit 1 97 else 98 revno=$(git describe --always --abbrev=7|cut -d- -f2) 99 commit=$(git describe --always --abbrev=7|cut -d- -f3) 100 v="${version_from_changelog}+git${revno}.${commit}" 101 o="changelog+git" 102 fi 103 fi 104 105 # append dirty at the end if we had a dirty tree 106 if [ "$DIRTY" = "true" ]; then 107 v="$v-dirty" 108 fi 109 110 if [ "$OUTPUT_ONLY" = true ]; then 111 echo "$v" 112 exit 0 113 fi 114 115 echo "*** Setting version to '$v' from $o." >&2 116 117 cat <<EOF > "$GO_GENERATE_BUILDDIR/snapdtool/version_generated.go" 118 package snapdtool 119 120 // generated by mkversion.sh; do not edit 121 122 func init() { 123 Version = "$v" 124 } 125 EOF 126 127 cat <<EOF > "$PKG_BUILDDIR/cmd/VERSION" 128 $v 129 EOF 130 131 cat <<EOF > "$PKG_BUILDDIR/data/info" 132 VERSION=$v 133 EOF