forked from cockpit-project/bots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-checkout
executable file
·63 lines (50 loc) · 1.23 KB
/
make-checkout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/sh
set -eu
usage() {
echo "usage: make-checkout [-v] [--rebase BRANCH] --repo REPO REF [REVISION]" >&2
}
retry() {
for retry in $(seq 5); do
if "$@"; then
return
fi
echo "Retrying $@... (attempt $retry)" >&2
sleep $((retry * 5))
done
echo "Failed to run $@" >&2
exit 1
}
eval set -- "$(getopt -o vh -l repo:,rebase:,help,verbose -- "$@")"
while true; do
case "${1:-}" in
-v|--verbose) set -x ;;
-h|--help) usage; exit 0 ;;
--repo) shift; REPO="$1" ;;
--rebase) shift; REBASE="$1" ;;
-- ) shift; break ;;
* ) break ;;
esac
shift
done
if [ $# -gt 2 ] || [ $# -lt 1 ] || [ -z "${REPO:-}" ]; then
usage
exit 1
fi
REF="$1"
REV=${2:-"FETCH_HEAD"}
TARGET_DIR="make-checkout-workdir"
# avoid failures with non-writable subdirectories
if [ -e "$TARGET_DIR" ]; then
chmod -R u+w "$TARGET_DIR"
rm -rf "$TARGET_DIR"
fi
retry git clone "https://github.com/$REPO" "$TARGET_DIR"
cd "$TARGET_DIR"
retry git fetch origin "$REF"
git checkout --detach "$REV"
if [ -n "${REBASE:-}" ]; then
retry git fetch origin "$REBASE"
SHA=`git rev-parse "origin/$REBASE"`
echo "Rebasing onto origin/$REBASE $SHA ..."
git rebase "origin/$REBASE"
fi