diff --git a/opam-release.sh b/opam-release.sh index 8e780cc..117d4b0 100755 --- a/opam-release.sh +++ b/opam-release.sh @@ -1,5 +1,6 @@ #!/usr/bin/env bash set -e +shopt -s nullglob if [ "$DEBUG" = 1 ]; then set -x @@ -7,23 +8,23 @@ fi show_help(){ cat < /dev/null; then + die "ERROR: git cannot be found, you must install it." +fi +if ! command -v opam &> /dev/null; then + die "ERROR: opam cannot be found, you must install it." +fi +# checking opam version +verle() { + [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ] +} +if verle "2.1.0" $(opam --version); then + CHECKUPSTREAM="${CHECKUPSTREAM:---check-upstream}" +fi +# + +# trying to retrieve github user +GITHUB_USER="${GITHUB_USER:-$(git config --default '' --get github.user)}" + +if ! [ "$TAG" ]; then + if (git describe --tags &> /dev/null) then + TAG=$(git describe --tags --abbrev=0) + fi +fi +VERSION="${VERSION:-$(echo $TAG | sed -e 's/[^0-9]*\(\([0-9]+|\.\)\)*/\1/')}" +PROJECT="${PROJECT:-}" +LINT="${LINT:-1}" +NOTHING="${NOTHING:-0}" +TARGET="${TARGET:-released}" +SHOWDEFAULTS="${SHOWDEFAULTS:-0}" OPAM=() -CHECKUPSTREAM="--check-upstream" -PREFIX= -SHOWDEFAULTS=0 while :; do case $1 in @@ -78,35 +105,43 @@ while :; do exit 0 ;; -u|--user) - if [ "$2" ]; then GITHUBUSER=$2; shift; shift + if [ "$2" ]; then GITHUB_USER=$2; shift; shift else die 'ERROR: "--user" requires a non-empty argument, cf --help' fi ;; -p|--project) if [ "$2" ]; then PROJECT=$2; shift; shift - else die 'ERROR: "--name" requires a non-empty argument, cf --help' + else die 'ERROR: "--project" requires a non-empty argument, cf --help' fi ;; -V|--version) if [ "$2" ]; then VERSION=$2; shift; shift - else die 'ERROR: "--name" requires a non-empty argument, cf --help' + else die 'ERROR: "--version" requires a non-empty argument, cf --help' fi ;; -U|--url) if [ "$2" ]; then URL=$2; shift; shift - else die 'ERROR: "--name" requires a non-empty argument, cf --help' + else die 'ERROR: "--url" requires a non-empty argument, cf --help' fi ;; -e|--depth) if [ "$2" ]; then HDEPTH="$2"; DEPTH="--depth=$2"; shift; shift - else die 'ERROR: "--name" requires a non-empty argument, cf --help' + else die 'ERROR: "--depth" requires a non-empty argument, cf --help' fi ;; -x|--version-prefix) if [ "$2" ]; then PREFIX=$2; shift; shift - else die 'ERROR: "--name" requires a non-empty argument, cf --help' + else die 'ERROR: "--version-prefix" requires a non-empty argument, cf --help' fi ;; + -r|--local-opam-coq-archive) + if [ "$2" ]; then LOCAL_OCA=$2; shift; shift + else die 'ERROR: "--local-opam-coq-archive" requires a non-empty argument, cf --help' + fi + ;; + -l|--lint) + LINT=1; shift + ;; -L|--no-lint) LINT=0; shift ;; @@ -116,6 +151,10 @@ while :; do CHECKUPSTREAM= shift ;; + -c|--check-upstream) + CHECKUPSTREAM="--check-upstream" + shift + ;; -C|--no-check-upstream) CHECKUPSTREAM= shift @@ -133,9 +172,9 @@ while :; do if ! [ "$*" ]; then break elif [ -f "$1" ]; then - OPAM+=("$1") - shift - else die 'ERROR: positional arguments must be a file' + OPAM+=("$1") + shift + else die 'ERROR: positional argument must be an existing file.' fi ;; esac @@ -147,15 +186,14 @@ fi eval set -- "${OPAM[@]}" -if ! [ "GITHUBUSER" ]; then - die 'ERROR: -u / --user argument is required, cf --help' +if ! [ "$GITHUB_USER" ]; then + die 'ERROR: -u / --user argument is required, cf --help' fi -if ! [ "VERSION" ]; then +if ! [ "$VERSION" ]; then die 'ERROR: -V / --version argument is required, cf --help' fi - if [ "$PREFIX" ]; then TAG=$PREFIX$VERSION fi @@ -171,7 +209,7 @@ case "${#OPAM[@]}" in ;; *) if ! [ "$PROJECT" ]; then - die 'ERROR: -p / --project argument is required when more than one opam files are given, cf --help' + die 'ERROR: --project argument is required when more than one opam files are given, cf --help' fi ;; esac @@ -184,14 +222,40 @@ if ! [ "$URL" ]; then fi fi -if [ "$VERBOSE" = 1 ] || [ "$SHOWDEFAULTS" = 1 ]; then +if ! [ "$URL" ]; then + die 'ERROR: --url argument cannot be inferred, please provide it manually, cf --help' +fi + +if [ "$VERBOSE" = 1 ]; then + echo "DEBUG=$DEBUG"; + echo "TAG=$TAG"; + echo "PREFIX=$PREFIX"; + echo "VERSION=$VERSION"; + echo "VERBOSE=$VERBOSE"; + echo "PROJECT=$PROJECT"; + echo "GITHUB_USER=$GITHUB_USER"; + echo "URL=$URL"; + echo "LINT=$LINT"; + echo "NOTHING=$NOTHING"; + echo "DEPTH=$DEPTH"; + echo "HDEPTH=$HDEPTH"; + echo "TARGET=$TARGET"; + echo "OPAM=$OPAM"; + echo "CHECKUPSTREAM=$CHECKUPSTREAM"; + echo "PREFIX=$PREFIX"; + echo "SHOWDEFAULTS=$SHOWDEFAULTS"; + echo "LOCAL_OCA=$LOCAL_OCA"; +fi + +if [ "$SHOWDEFAULTS" = 1 ]; then echo "# this would call" echo "$0 \\" if [ "$VERBOSE" = 1 ]; then echo "-v \\"; fi - echo "-u $GITHUBUSER \\" + echo "-u $GITHUB_USER \\" echo "-V $VERSION \\" echo "-p $PROJECT \\" echo "-U $URL \\" + if [ "$LOCAL_OCA" ]; then echo "-r $LOCAL_OCA \\"; fi if [ "$LINT" = 0 ]; then echo "-L \\"; fi if [ "$NOTHING" = 1 ]; then echo "-n \\"; fi if [ "$HDEPTH" ]; then echo "-e $HDEPTH \\"; fi @@ -204,12 +268,19 @@ if [ "$SHOWDEFAULTS" = 1 ]; then exit fi -COA=$(mktemp -d) # stands for Coq Opam Archive -git clone $DEPTH git@github.com:coq/opam-coq-archive $COA -o upstream -git -C $COA remote add origin git@github.com:$GITHUBUSER/opam-coq-archive +TMP_OCA=$(mktemp -d) # stands for Opam Coq Archive +if [ "$LOCAL_OCA" ]; then + git clone $LOCAL_OCA $TMP_OCA -o local + git -C $TMP_OCA remote add upstream git@github.com:coq/opam-coq-archive + git -C $TMP_OCA fetch upstream master + git -C $TMP_OCA reset --hard upstream/master +else + git clone $DEPTH git@github.com:coq/opam-coq-archive $TMP_OCA -o upstream +fi +git -C $TMP_OCA remote add origin git@github.com:$GITHUB_USER/opam-coq-archive BRANCH=$PROJECT.$VERSION -git -C $COA checkout -b $BRANCH -PKGS=$COA/$TARGET/packages +git -C $TMP_OCA checkout -b $BRANCH +PKGS=$TMP_OCA/$TARGET/packages ARCHIVE=$(mktemp) if [ "$TARGET" = "released" ]; then @@ -218,7 +289,7 @@ if [ "$TARGET" = "released" ]; then fi if [ "$VERBOSE" = 1 ]; then - echo "COA=$COA" + echo "TMP_OCA=$TMP_OCA" echo "BRANCH=$BRANCH" echo "PKGS=$PKGS" echo "ARCHIVE=$ARCHIVE" @@ -243,24 +314,24 @@ for opam in ${OPAM[@]}; do else echo "linting disabled (not recommended)" fi - git -C $COA add $P/opam + git -C $TMP_OCA add $P/opam done -git -C $COA commit -m "Update $PROJECT $VERSION" +git -C $TMP_OCA commit -m "Update $PROJECT $VERSION" if [ "$NOTHING" = 1 ]; then echo "**********************************************************************" echo "Dry run!" - echo git -C $COA push origin -f $BRANCH + echo git -C $TMP_OCA push origin -f $BRANCH echo "if you want to see the diff, run" - echo git -C $COA diff HEAD~1 + echo git -C $TMP_OCA diff HEAD~1 echo "**********************************************************************" else - git -C $COA push origin -f $BRANCH + git -C $TMP_OCA push origin -f $BRANCH echo "**********************************************************************" echo "Create a pull request by visiting" - echo "https://github.com/$GITHUBUSER/opam-coq-archive/pull/new/$BRANCH" + echo "https://github.com/$GITHUB_USER/opam-coq-archive/pull/new/$BRANCH" echo "**** PLEASE CHECK CAREFULLY THE GENERATED CODE ***" echo "If you wish to delete the resulting branch, execute:" - echo "git -C $COA push origin --delete $BRANCH" + echo "git -C $TMP_OCA push origin --delete $BRANCH" echo "**********************************************************************" fi