#!/bin/bash warn() { echo "$1" >&2 } die() { warn "fatal: $1" exit 3 } usage() { warn "usage: $0 [options] COPR_PROJECT" warn "" warn "Options:" warn "" warn " -n dry mode, don't do anything (just show" warn " what would be done)" warn " -r REL use REL as Release number in SPEC file" warn " -v VER use VER as Version number in SPEC file" warn " -u URL use URL as base (to get last tag and" warn " compose Source0 in SPEC file)" warn "" warn "If -r or -v are not specified, git-describe master" warn "is consulted." exit 2 } last_version() { git ls-remote --tag "$UrlBase" \ | grep '/tags/' \ | cut -d/ -f3 \ | sort -V \ | tail -1 \ | sed "s/^v//" } trigger() { sed -e " /^Version/ s|__SLOP_VERSION__|$Version| /^Release/ s|__SLOP_RELEASE__|$Release| /^Source0/ s|__SLOP_URLBASE__|$UrlBase| " "$Tmp/slop.spec" copr build --nowait "$CoprProject" "$Tmp/slop.spec" } git_guess() { # # Print git-guessed $1 # local what=$1 # what we want (ver|rel) local describe # full git-describe output local xtra= # extra part (-N-gHASH) local num=0 # num. of commits since tag (N) local sha= # '.g'+sha1 of this commit (gHASH) describe=$(git describe --tags --always HEAD) case $describe in *-*) # v1.2.3-21-g654cba tag=${describe%%-*} # tag=v1.2.3 xtra=${describe#$tag-} # xtra=-21-g654cba num=${xtra%%-*} # num=21 sha=.${xtra#$num-} # sha=.g654cba ;; *) tag=$describe ;; esac case $what in ver) echo "${tag#v}" ;; rel) echo "$((num + 1))$sha" ;; esac } main() { local Version # Version in SPEC file local Release # Release in SPEC file local CoprProject # COPR project local UrlBase # GitHub URL base local Tmp # our temp local Branch # branch to use, if empty, tags are considered local DryRun=false # do not do anything UrlBase=https://github.com/AloisMahdal/slop Tmp=$(mktemp -d) while true; do case $1 in -b) Branch=$2; shift 2 || usage ;; -u) UrlBase=$2; shift 2 || usage ;; -r) Release=$2; shift 2 || usage ;; -v) Version=${2#v}; shift 2 || usage ;; -n) DryRun=true; shift ;; -*) usage ;; *) break ;; esac done CoprProject=$1 test -n "$CoprProject" || usage which copr >/dev/null \ || die "copr not found, try 'sudo install python-copr'" if test -n "$Branch"; then die "not implemented" test -n "$Version" || Version=$(git_guess ver) test -n "$Release" || Release=$(git_guess rel) else test -n "$Version" || Version=$(last_version) test -n "$Release" || Release=1 fi $DryRun && { warn "dry mode active, we would build:" warn " at $CoprProject" test -n "$Branch" && warn " using branch $Branch" test -n "$Branch" || warn " using last tag" warn " from $UrlBase" warn " yielding slop-$Version-$Release.*.rpm" exit 1 } trigger rm -rf "$Tmp" } main "$@"