Skip to content

Commit 33e5251

Browse files
committed
Adding failsafes and new options
1 parent 911b5b9 commit 33e5251

1 file changed

Lines changed: 126 additions & 55 deletions

File tree

opam-release.sh

Lines changed: 126 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
#!/usr/bin/env bash
22
set -e
3+
shopt -s nullglob
34

45
if [ "$DEBUG" = 1 ]; then
56
set -x
67
fi
78

89
show_help(){
910
cat <<EOF
10-
Usage : opam-release (arguments)* (opam-files)*
11+
Usage : opam-release (options)* (opam-files)*
1112
--
12-
Optional arguments:
13-
opam-files: one or many opam files to push to opam-coq-archive
14-
*if no argument is provided, it lists *.opam files in the current directory
15-
-u GITHUBUSER / --user GITHUBUSER:
16-
where GITHUBUSER is your github username,
13+
opam-files: an opam file to push to opam-coq-archive
14+
* if no argument is provided, it lists *.opam files in the current directory
15+
options:
16+
-u GITHUB_USER / --user GITHUB_USER:
17+
where GITHUB_USER is your github username,
1718
you must have a fork of coq/opam-coq-archive under
18-
https://github.com/$GITHUBUSER/opam-coq-archive
19+
https://github.com/$GITHUB_USER/opam-coq-archive
1920
for this command to work,
2021
* if not provided, tries "git config --get github.user"
22+
which you can set using "git config --global github.user GITHUB_SER"
2123
-p PROJECT / --project PROJECT:
22-
where PROJECT is a name of the project,
23-
without space, it is used solely for generating
24-
the name of the branch and PR,
25-
* automatically infered from the name of the opam file
26-
if only one was given,
24+
where PROJECT is a name of the project, without space,
25+
it is used solely for generating the name of the branch and PR,
26+
* automatically infered from the name of the opam file,
27+
if only one was given.
2728
-e DEPTH / --depth DEPTH:
2829
sets the depth of the local clone of opam-coq-archive to $DEPTH
2930
* defaults to full clone,
@@ -38,16 +39,25 @@ Optional arguments:
3839
with the version to release.
3940
* tries to infer it from the "dev-repo" section if one opam file is provided
4041
-x PREFIX / --version-prefix PREFIX: the tag of a version is equal to $PREFIX$VERSION
41-
* default is ""
42+
* default is ""
4243
** this options is used only if URL is not provided
44+
-r / --local-opam-coq-archive LOCAL_OCA: where LOCAL_OCA is the path to
45+
a local copy of the opam-coq-archive repository
4346
-d / --dev: pushes an update to extra-dev instead of releases
4447
-C / --no-check-upstream: tells opam lint not to check the sum online
48+
-c / --check-upstream: foce opam lint to check the sum online, (even
49+
if it was disable for some reason, e.g. incompatible opam version)
4550
-h / -? / --help: print this usage
4651
-s / --show-defaults: prints the default inferred setting and exists
4752
-v / --verbose: show debug information
4853
-L / --no-lint: do not run opam lint
4954
(not recommended but useful if you do not have opam installed)
55+
-l / --lint: force opam lint, (even if it was disabled for some reason)
5056
-n / --do-nothing: do not push anything
57+
--
58+
All above options can be set with the corresponding environment variable.
59+
Additionally, one may use the DEBUG environement variable tuo turn on
60+
debugging through options -x.
5161
EOF
5262
}
5363

@@ -56,20 +66,37 @@ die() {
5666
exit 1
5767
}
5868

59-
GITHUBUSER=$(git config --get github.user)
60-
TAG=$(git describe --tags --abbrev=0)
61-
VERSION=$(echo $TAG | sed -e "s/[^0-9]*\(\([0-9]+|\.\)\)*/\1/")
62-
PROJECT=
63-
URL=
64-
LINT=1
65-
NOTHING=0
66-
DEPTH=
67-
HDEPTH=
68-
TARGET="released"
69+
# Checking git and opam are installed
70+
if ! command -v git &> /dev/null; then
71+
die "ERROR: git cannot be found, you must install it."
72+
fi
73+
if ! command -v opam &> /dev/null; then
74+
die "ERROR: opam cannot be found, you must install it."
75+
fi
76+
# checking opam version
77+
verle() {
78+
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
79+
}
80+
if verle "2.1.0" $(opam --version); then
81+
CHECKUPSTREAM="${CHECKUPSTREAM:---check-upstream}"
82+
fi
83+
#
84+
85+
# trying to retrieve github user
86+
GITHUB_USER="${GITHUB_USER:-$(git config --default '' --get github.user)}"
87+
88+
if ! [ "$TAG" ]; then
89+
if (git describe --tags &> /dev/null) then
90+
TAG=$(git describe --tags --abbrev=0)
91+
fi
92+
fi
93+
VERSION="${VERSION:-$(echo $TAG | sed -e 's/[^0-9]*\(\([0-9]+|\.\)\)*/\1/')}"
94+
PROJECT="${PROJECT:-}"
95+
LINT="${LINT:-1}"
96+
NOTHING="${NOTHING:-0}"
97+
TARGET="${TARGET:-released}"
98+
SHOWDEFAULTS="${SHOWDEFAULTS:-0}"
6999
OPAM=()
70-
CHECKUPSTREAM="--check-upstream"
71-
PREFIX=
72-
SHOWDEFAULTS=0
73100

74101
while :; do
75102
case $1 in
@@ -78,35 +105,43 @@ while :; do
78105
exit 0
79106
;;
80107
-u|--user)
81-
if [ "$2" ]; then GITHUBUSER=$2; shift; shift
108+
if [ "$2" ]; then GITHUB_USER=$2; shift; shift
82109
else die 'ERROR: "--user" requires a non-empty argument, cf --help'
83110
fi
84111
;;
85112
-p|--project)
86113
if [ "$2" ]; then PROJECT=$2; shift; shift
87-
else die 'ERROR: "--name" requires a non-empty argument, cf --help'
114+
else die 'ERROR: "--project" requires a non-empty argument, cf --help'
88115
fi
89116
;;
90117
-V|--version)
91118
if [ "$2" ]; then VERSION=$2; shift; shift
92-
else die 'ERROR: "--name" requires a non-empty argument, cf --help'
119+
else die 'ERROR: "--version" requires a non-empty argument, cf --help'
93120
fi
94121
;;
95122
-U|--url)
96123
if [ "$2" ]; then URL=$2; shift; shift
97-
else die 'ERROR: "--name" requires a non-empty argument, cf --help'
124+
else die 'ERROR: "--url" requires a non-empty argument, cf --help'
98125
fi
99126
;;
100127
-e|--depth)
101128
if [ "$2" ]; then HDEPTH="$2"; DEPTH="--depth=$2"; shift; shift
102-
else die 'ERROR: "--name" requires a non-empty argument, cf --help'
129+
else die 'ERROR: "--depth" requires a non-empty argument, cf --help'
103130
fi
104131
;;
105132
-x|--version-prefix)
106133
if [ "$2" ]; then PREFIX=$2; shift; shift
107-
else die 'ERROR: "--name" requires a non-empty argument, cf --help'
134+
else die 'ERROR: "--version-prefix" requires a non-empty argument, cf --help'
108135
fi
109136
;;
137+
-r|--local-opam-coq-archive)
138+
if [ "$2" ]; then LOCAL_OCA=$2; shift; shift
139+
else die 'ERROR: "--local-opam-coq-archive" requires a non-empty argument, cf --help'
140+
fi
141+
;;
142+
-l|--lint)
143+
LINT=1; shift
144+
;;
110145
-L|--no-lint)
111146
LINT=0; shift
112147
;;
@@ -116,6 +151,10 @@ while :; do
116151
CHECKUPSTREAM=
117152
shift
118153
;;
154+
-c|--check-upstream)
155+
CHECKUPSTREAM="--check-upstream"
156+
shift
157+
;;
119158
-C|--no-check-upstream)
120159
CHECKUPSTREAM=
121160
shift
@@ -133,9 +172,9 @@ while :; do
133172
if ! [ "$*" ]; then
134173
break
135174
elif [ -f "$1" ]; then
136-
OPAM+=("$1")
137-
shift
138-
else die 'ERROR: positional arguments must be a file'
175+
OPAM+=("$1")
176+
shift
177+
else die 'ERROR: positional argument must be an existing file.'
139178
fi
140179
;;
141180
esac
@@ -147,15 +186,14 @@ fi
147186

148187
eval set -- "${OPAM[@]}"
149188

150-
if ! [ "GITHUBUSER" ]; then
151-
die 'ERROR: -u / --user argument is required, cf --help'
189+
if ! [ "$GITHUB_USER" ]; then
190+
die 'ERROR: -u / --user argument is required, cf --help'
152191
fi
153192

154-
if ! [ "VERSION" ]; then
193+
if ! [ "$VERSION" ]; then
155194
die 'ERROR: -V / --version argument is required, cf --help'
156195
fi
157196

158-
159197
if [ "$PREFIX" ]; then
160198
TAG=$PREFIX$VERSION
161199
fi
@@ -171,7 +209,7 @@ case "${#OPAM[@]}" in
171209
;;
172210
*)
173211
if ! [ "$PROJECT" ]; then
174-
die 'ERROR: -p / --project argument is required when more than one opam files are given, cf --help'
212+
die 'ERROR: --project argument is required when more than one opam files are given, cf --help'
175213
fi
176214
;;
177215
esac
@@ -184,14 +222,40 @@ if ! [ "$URL" ]; then
184222
fi
185223
fi
186224

187-
if [ "$VERBOSE" = 1 ] || [ "$SHOWDEFAULTS" = 1 ]; then
225+
if ! [ "$URL" ]; then
226+
die 'ERROR: --url argument cannot be inferred, please provide it manually, cf --help'
227+
fi
228+
229+
if [ "$VERBOSE" = 1 ]; then
230+
echo "DEBUG=$DEBUG";
231+
echo "TAG=$TAG";
232+
echo "PREFIX=$PREFIX";
233+
echo "VERSION=$VERSION";
234+
echo "VERBOSE=$VERBOSE";
235+
echo "PROJECT=$PROJECT";
236+
echo "GITHUB_USER=$GITHUB_USER";
237+
echo "URL=$URL";
238+
echo "LINT=$LINT";
239+
echo "NOTHING=$NOTHING";
240+
echo "DEPTH=$DEPTH";
241+
echo "HDEPTH=$HDEPTH";
242+
echo "TARGET=$TARGET";
243+
echo "OPAM=$OPAM";
244+
echo "CHECKUPSTREAM=$CHECKUPSTREAM";
245+
echo "PREFIX=$PREFIX";
246+
echo "SHOWDEFAULTS=$SHOWDEFAULTS";
247+
echo "LOCAL_OCA=$LOCAL_OCA";
248+
fi
249+
250+
if [ "$SHOWDEFAULTS" = 1 ]; then
188251
echo "# this would call"
189252
echo "$0 \\"
190253
if [ "$VERBOSE" = 1 ]; then echo "-v \\"; fi
191-
echo "-u $GITHUBUSER \\"
254+
echo "-u $GITHUB_USER \\"
192255
echo "-V $VERSION \\"
193256
echo "-p $PROJECT \\"
194257
echo "-U $URL \\"
258+
if [ "$LOCAL_OCA" ]; then echo "-r $LOCAL_OCA \\"; fi
195259
if [ "$LINT" = 0 ]; then echo "-L \\"; fi
196260
if [ "$NOTHING" = 1 ]; then echo "-n \\"; fi
197261
if [ "$HDEPTH" ]; then echo "-e $HDEPTH \\"; fi
@@ -204,12 +268,19 @@ if [ "$SHOWDEFAULTS" = 1 ]; then
204268
exit
205269
fi
206270

207-
COA=$(mktemp -d) # stands for Coq Opam Archive
208-
git clone $DEPTH git@github.com:coq/opam-coq-archive $COA -o upstream
209-
git -C $COA remote add origin git@github.com:$GITHUBUSER/opam-coq-archive
271+
TMP_OCA=$(mktemp -d) # stands for Opam Coq Archive
272+
if [ "$LOCAL_OCA" ]; then
273+
git clone $LOCAL_OCA $TMP_OCA -o local
274+
git -C $TMP_OCA remote add upstream git@github.com:coq/opam-coq-archive
275+
git -C $TMP_OCA fetch upstream master
276+
git -C $TMP_OCA reset --hard upstream/master
277+
else
278+
git clone $DEPTH git@github.com:coq/opam-coq-archive $TMP_OCA -o upstream
279+
fi
280+
git -C $TMP_OCA remote add origin git@github.com:$GITHUB_USER/opam-coq-archive
210281
BRANCH=$PROJECT.$VERSION
211-
git -C $COA checkout -b $BRANCH
212-
PKGS=$COA/$TARGET/packages
282+
git -C $TMP_OCA checkout -b $BRANCH
283+
PKGS=$TMP_OCA/$TARGET/packages
213284

214285
ARCHIVE=$(mktemp)
215286
if [ "$TARGET" = "released" ]; then
@@ -218,7 +289,7 @@ if [ "$TARGET" = "released" ]; then
218289
fi
219290

220291
if [ "$VERBOSE" = 1 ]; then
221-
echo "COA=$COA"
292+
echo "TMP_OCA=$TMP_OCA"
222293
echo "BRANCH=$BRANCH"
223294
echo "PKGS=$PKGS"
224295
echo "ARCHIVE=$ARCHIVE"
@@ -243,24 +314,24 @@ for opam in ${OPAM[@]}; do
243314
else
244315
echo "linting disabled (not recommended)"
245316
fi
246-
git -C $COA add $P/opam
317+
git -C $TMP_OCA add $P/opam
247318
done
248319

249-
git -C $COA commit -m "Update $PROJECT $VERSION"
320+
git -C $TMP_OCA commit -m "Update $PROJECT $VERSION"
250321
if [ "$NOTHING" = 1 ]; then
251322
echo "**********************************************************************"
252323
echo "Dry run!"
253-
echo git -C $COA push origin -f $BRANCH
324+
echo git -C $TMP_OCA push origin -f $BRANCH
254325
echo "if you want to see the diff, run"
255-
echo git -C $COA diff HEAD~1
326+
echo git -C $TMP_OCA diff HEAD~1
256327
echo "**********************************************************************"
257328
else
258-
git -C $COA push origin -f $BRANCH
329+
git -C $TMP_OCA push origin -f $BRANCH
259330
echo "**********************************************************************"
260331
echo "Create a pull request by visiting"
261-
echo "https://github.com/$GITHUBUSER/opam-coq-archive/pull/new/$BRANCH"
332+
echo "https://github.com/$GITHUB_USER/opam-coq-archive/pull/new/$BRANCH"
262333
echo "**** PLEASE CHECK CAREFULLY THE GENERATED CODE ***"
263334
echo "If you wish to delete the resulting branch, execute:"
264-
echo "git -C $COA push origin --delete $BRANCH"
335+
echo "git -C $TMP_OCA push origin --delete $BRANCH"
265336
echo "**********************************************************************"
266337
fi

0 commit comments

Comments
 (0)