github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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" != "" ] && [ "$version_from_git" != "$version_from_changelog" ]; then 90 # if the changelog version has "git" in it and we also have a git version 91 # directly, that is a bad changelog version, so fail, otherwise the below 92 # code will produce a duplicated git info 93 if echo "$version_from_changelog" | grep -q git; then 94 echo "Cannot generate version, there is a version from git and the changelog has a git version" 95 exit 1 96 else 97 revno=$(git describe --always --abbrev=7|cut -d- -f2) 98 commit=$(git describe --always --abbrev=7|cut -d- -f3) 99 v="${version_from_changelog}+git${revno}.${commit}" 100 o="changelog+git" 101 fi 102 fi 103 104 # append dirty at the end if we had a dirty tree 105 if [ "$DIRTY" = "true" ]; then 106 v="$v-dirty" 107 fi 108 109 if [ "$OUTPUT_ONLY" = true ]; then 110 echo "$v" 111 exit 0 112 fi 113 114 echo "*** Setting version to '$v' from $o." >&2 115 116 cat <<EOF > "$GO_GENERATE_BUILDDIR/snapdtool/version_generated.go" 117 package snapdtool 118 119 // generated by mkversion.sh; do not edit 120 121 func init() { 122 Version = "$v" 123 } 124 EOF 125 126 cat <<EOF > "$PKG_BUILDDIR/cmd/VERSION" 127 $v 128 EOF 129 130 cat <<EOF > "$PKG_BUILDDIR/data/info" 131 VERSION=$v 132 EOF