gitee.com/mysnapcore/mysnapd@v0.1.0/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="${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 only if the snapd source tree is
    43  # tracked by git. The script can be invoked when building distro packages in
    44  # which case, the source tree could be a tarball, but the distro packaging files
    45  # can be in git, so try not to confuse the two.
    46  if command -v git >/dev/null && [ -d "$(dirname "$0")/.git" ] ; then
    47      # don't include --dirty here as we independently track whether the tree is
    48      # dirty and append that last, including it here will make dirty trees 
    49      # directly on top of tags show up with version_from_git as 2.46-dirty which
    50      # will not match 2.46 from the changelog and then result in a final version
    51      # like 2.46+git2.46.2.46 which is silly and unhelpful
    52      # tracking the dirty independently like this will produce instead 2.46-dirty
    53      # for a dirty tree on top of a tag, and 2.46+git83.g1671726-dirty for a 
    54      # commit not directly on top of a tag
    55      version_from_git="$(git describe --always | sed -e 's/-/+git/;y/-/./' )"
    56  
    57      # check if we are using a dirty tree
    58      if git describe --always --dirty | grep -q dirty; then
    59          DIRTY=true
    60      fi
    61  fi
    62  
    63  # at this point we maybe in _build/src/github etc where we have no
    64  # debian/changelog (dh-golang only exports the sources here)
    65  # switch to the real source dir for the changelog parsing
    66  if command -v dpkg-parsechangelog >/dev/null; then
    67      version_from_changelog="$(cd "$PKG_BUILDDIR"; dpkg-parsechangelog --show-field Version)";
    68  fi
    69  
    70  # select version based on priority
    71  if [ -n "$version_from_user" ]; then
    72      # version from user always wins
    73      v="$version_from_user"
    74      o="user"
    75  elif [ -n "$version_from_git" ]; then
    76      v="$version_from_git"
    77      o="git"
    78  elif [ -n "$version_from_changelog" ]; then
    79      v="$version_from_changelog"
    80      o="changelog"
    81  else
    82      echo "Cannot generate version"
    83      exit 1
    84  fi
    85  
    86  # if we don't have a user provided version and if the version is not
    87  # a release (i.e. the git tag does not match the debian changelog
    88  # version) then we need to construct the version similar to how we do
    89  # it in a packaging recipe. We take the debian version from the changelog
    90  # and append the git revno and commit hash. A simpler approach would be
    91  # to git tag all pre/rc releases.
    92  if [ -z "$version_from_user" ] && [ "$version_from_git" != "" ] && \
    93         [ -n "$version_from_changelog" ] && [ "$version_from_git" != "$version_from_changelog" ]; then
    94      # if the changelog version has "git" in it and we also have a git version
    95      # directly, that is a bad changelog version, so fail, otherwise the below
    96      # code will produce a duplicated git info
    97      if echo "$version_from_changelog" | grep -q git; then
    98          echo "Cannot generate version, there is a version from git and the changelog has a git version"
    99          exit 1
   100      else
   101          revno=$(git describe --always --abbrev=7|cut -d- -f2)
   102          commit=$(git describe --always --abbrev=7|cut -d- -f3)
   103          v="${version_from_changelog}+git${revno}.${commit}"
   104          o="changelog+git"
   105      fi
   106  fi
   107  
   108  # append dirty at the end if we had a dirty tree
   109  if [ "$DIRTY" = "true" ]; then
   110      v="$v-dirty"
   111  fi
   112  
   113  if [ "$OUTPUT_ONLY" = true ]; then
   114      echo "$v"
   115      exit 0
   116  fi
   117  
   118  echo "*** Setting version to '$v' from $o." >&2
   119  
   120  cat <<EOF > "$GO_GENERATE_BUILDDIR/snapdtool/version_generated.go"
   121  package snapdtool
   122  
   123  // generated by mkversion.sh; do not edit
   124  
   125  func init() {
   126  	Version = "$v"
   127  }
   128  EOF
   129  
   130  cat <<EOF > "$PKG_BUILDDIR/cmd/VERSION"
   131  $v
   132  EOF
   133  
   134  MOD=-mod=vendor
   135  if [ "$GO111MODULE" = "off" ] ; then
   136      MOD=--
   137  elif [ ! -d "$GO_GENERATE_BUILDDIR/vendor/github.com"  ] ; then
   138      MOD=--
   139  fi
   140  fmts=$(cd "$GO_GENERATE_BUILDDIR" ; go run $MOD ./asserts/info)
   141  
   142  cat <<EOF > "$PKG_BUILDDIR/data/info"
   143  VERSION=$v
   144  SNAPD_APPARMOR_REEXEC=1
   145  ${fmts}
   146  EOF