github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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" = "cmd" ]; 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  # Let's try to derive the version from git..
    41  if command -v git >/dev/null; then
    42      # not using "--dirty" here until the following bug is fixed:
    43      # https://bugs.launchpad.net/snapcraft/+bug/1662388
    44      version_from_git="$(git describe --always | sed -e 's/-/+git/;y/-/./' )"
    45  fi
    46  
    47  # at this point we maybe in _build/src/github etc where we have no
    48  # debian/changelog (dh-golang only exports the sources here)
    49  # switch to the real source dir for the changelog parsing
    50  if command -v dpkg-parsechangelog >/dev/null; then
    51      version_from_changelog="$(cd "$PKG_BUILDDIR"; dpkg-parsechangelog --show-field Version)";
    52  fi
    53  
    54  # select version based on priority
    55  if [ -n "$version_from_user" ]; then
    56      # version from user always wins
    57      v="$version_from_user"
    58      o="user"
    59  elif [ -n "$version_from_git" ]; then
    60      v="$version_from_git"
    61      o="git"
    62  elif [ -n "$version_from_changelog" ]; then
    63      v="$version_from_changelog"
    64      o="changelog"
    65  else
    66      echo "Cannot generate version"
    67      exit 1
    68  fi
    69  
    70  # if we don't have a user provided versions and if the version is not
    71  # a release (i.e. the git tag does not match the debian changelog
    72  # version) then we need to construct the version similar to how we do
    73  # it in a packaging recipe. We take the debian version from the changelog
    74  # and append the git revno and commit hash. A simpler approach would be
    75  # to git tag all pre/rc releases.
    76  if [ -z "$version_from_user" ] && [ "$version_from_git" != "" ] && [ "$version_from_git" != "$version_from_changelog" ]; then
    77      revno=$(git describe --always --abbrev=7|cut -d- -f2)
    78      commit=$(git describe --always --abbrev=7|cut -d- -f3)
    79      v="${version_from_changelog}+git${revno}.${commit}"
    80      o="changelog+git"
    81  fi
    82  
    83  
    84  if [ "$OUTPUT_ONLY" = true ]; then
    85      echo "$v"
    86      exit 0
    87  fi
    88  
    89  echo "*** Setting version to '$v' from $o." >&2
    90  
    91  cat <<EOF > "$GO_GENERATE_BUILDDIR/cmd/version_generated.go"
    92  package cmd
    93  
    94  // generated by mkversion.sh; do not edit
    95  
    96  func init() {
    97  	Version = "$v"
    98  }
    99  EOF
   100  
   101  cat <<EOF > "$PKG_BUILDDIR/cmd/VERSION"
   102  $v
   103  EOF
   104  
   105  cat <<EOF > "$PKG_BUILDDIR/data/info"
   106  VERSION=$v
   107  EOF