Skip to content

Commit 8259a27

Browse files
committed
Add packcheck-remote to work with remote repositories directly
1 parent 2300f86 commit 8259a27

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ well on the command line. For example,
197197
$ ./packcheck-safe.sh cabal-v2 PATH=/bin:/usr/bin:/opt/ghc/bin
198198
```
199199

200+
## packcheck-remote
201+
202+
`packcheck-remote.sh` is a wrapper over `packcheck.sh`. It makes it easer to
203+
work with remote repositories.
204+
205+
```
206+
$ ./packcheck-remote.sh --force \
207+
--remote=https://github.com/user/repo \
208+
--checkout=origin/master \
209+
--merge=origin/branch \
210+
--directory=./repo.packcheck \
211+
-- cabal-v2 GHCVER=8.8.3
212+
```
213+
214+
Use `./packcheck-remote.sh --help` for more information.
215+
200216
## Full Reference
201217

202218
Please use `cabal` version 2.4 or later.

packcheck-remote.sh

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
#!/usr/bin/env bash
2+
3+
PACKCHECK_DIR="$(dirname $0)"
4+
PACKCHECK_EXE="$PACKCHECK_DIR/packcheck.sh"
5+
PWD="$(pwd)"
6+
DEFAULT_DIRECTORY_NAME="packcheck-remote-work"
7+
DEFAULT_DIRECTORY="$PWD/$DEFAULT_DIRECTORY_NAME"
8+
9+
# $1: msg
10+
show_step() {
11+
echo
12+
echo "--------------------------------------------------"
13+
echo "$1"
14+
echo "--------------------------------------------------"
15+
}
16+
17+
which_cmd() {
18+
hash -r && type -P "$1" || true
19+
}
20+
21+
# $1: executable (eg. git)
22+
require_cmd () {
23+
if test -z "$(which_cmd $1)"
24+
then
25+
echo "Required command [$1] not found in PATH [$PATH]."
26+
exit 1
27+
else
28+
echo "Using [$1] at [$(which_cmd $1)]"
29+
fi
30+
}
31+
32+
# $1: command
33+
function run_verbose() {
34+
echo "$*"
35+
bash -c "$*"
36+
}
37+
38+
# $1: Remote repository
39+
# $2: Revision to checkout
40+
# $3: Revision to merge into $2
41+
# $4: Directory to clone into
42+
# $5: Force mode
43+
# $6: Packcheck cli options
44+
try_git_clone_and_merge() {
45+
46+
# Check for prerequisites
47+
require_cmd git
48+
49+
local remote="$1"
50+
local rev="$2"
51+
local merge="$3"
52+
local dir="$4"
53+
local force_mode="$5"
54+
local packcheck_cli_opts="$6"
55+
56+
if test -z "$dir"
57+
then
58+
dir="$DEFAULT_DIRECTORY"
59+
echo "No directory specified to clone to."
60+
echo "Using $dir"
61+
fi
62+
63+
if test -z "$remote"
64+
then
65+
echo "No remote repository provided."
66+
echo "Skipping cloning."
67+
return
68+
fi
69+
70+
if test -z "$rev"
71+
then
72+
echo "No revision given."
73+
echo "Defaulting to 'origin/master'."
74+
rev="origin/master"
75+
fi
76+
77+
if test -d "$dir"
78+
then
79+
echo "$dir already exists"
80+
81+
if [ "$force_mode" == "true" ];
82+
then
83+
echo "Forcing the deletion of the directory."
84+
echo "Removing $dir"
85+
rm -rf "$dir" || exit 1
86+
else
87+
echo "Set the script to force mode to force the deletion."
88+
return
89+
fi
90+
fi
91+
92+
mkdir -p "$dir" || exit 1
93+
94+
run_verbose "git clone $remote $dir" || exit 1
95+
96+
cd "$dir" || exit 1
97+
run_verbose "git branch $rev" || exit 1
98+
99+
if test -n "$merge"
100+
then
101+
# This will fail is there are any merge conflicts
102+
run_verbose "git merge -X theirs $merge --no-edit --commit" || exit 1
103+
fi
104+
105+
show_step "Running packcheck"
106+
# Run packcheck here
107+
run_verbose "$PACKCHECK_EXE $packcheck_cli_opts"
108+
109+
# Go back to where the script was executed from
110+
cd "$PWD" || exit 1
111+
}
112+
113+
# Arguments for the command line
114+
FORCE="false"
115+
REMOTE=""
116+
CHECKOUT=""
117+
MERGE=""
118+
DIRECTORY=""
119+
PACKCHECK_CLI_OPTS=""
120+
121+
function run_help() {
122+
local script=$(basename $0)
123+
124+
echo
125+
echo "USAGE: --option[=value]"
126+
echo
127+
echo "-f (or) --force: Puts the script in force mode. This is ideal for CIs."
128+
echo "-h (or) --help: Print help."
129+
echo "--remote: Repository to clone."
130+
echo "--checkout: Revision to checkout. Defaults to 'origin/master'."
131+
echo "--merge: Revision to merge in the checked out branch."
132+
echo "--directory: Directory to clone the repository into."
133+
echo " Defaults to '$DEFAULT_DIRECTORY_NAME' under the present working directory."
134+
echo
135+
echo "All the arguments after '--' are passed to packcheck"
136+
echo
137+
138+
echo
139+
echo "EXAMPLE:"
140+
echo
141+
echo "$script --force \\"
142+
echo " --remote=https://github.com/user/repo \\"
143+
echo " --checkout=origin/master \\"
144+
echo " --merge=origin/branch \\"
145+
echo " --directory=./repo.packcheck \\"
146+
echo " -- cabal-v2 GHCVER=8.8.3"
147+
echo
148+
}
149+
150+
# Program entry point
151+
for i in "$@"
152+
do
153+
case $i in
154+
-f|--force)
155+
FORCE="true"
156+
shift
157+
;;
158+
-h|--help)
159+
run_help
160+
exit 0
161+
;;
162+
--remote=*)
163+
REMOTE="${i#*=}"
164+
shift
165+
;;
166+
--checkout=*)
167+
CHECKOUT="${i#*=}"
168+
shift
169+
;;
170+
--merge=*)
171+
MERGE="${i#*=}"
172+
shift
173+
;;
174+
--directory=*)
175+
DIRECTORY="${i#*=}"
176+
shift
177+
;;
178+
--)
179+
shift
180+
PACKCHECK_CLI_OPTS=$@
181+
break
182+
;;
183+
*)
184+
echo "Unknown argument to packcheck-remote"
185+
run_help
186+
exit 1
187+
;;
188+
esac
189+
done
190+
191+
try_git_clone_and_merge "$REMOTE" "$CHECKOUT" "$MERGE" "$DIRECTORY" "$FORCE" "$PACKCHECK_CLI_OPTS"

0 commit comments

Comments
 (0)