-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathbuck2-update-prelude.sh
executable file
·155 lines (133 loc) · 3.38 KB
/
buck2-update-prelude.sh
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env bash
print_usage() {
local program="$1"
local version="$2"
local author="$3"
cat <<-EOF
$program $version
Updates the upstream common prelude.
USAGE:
$program [FLAGS] [OPTIONS]
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
OPTIONS:
-g, --git-repo=<URL> Upstream Git repository with prelude
(default: $GIT_REPO)
-r, --git-ref=<REF> Git ref to update from
(default: $GIT_REMOTE_REF)
AUTHOR:
$author
EOF
}
main() {
set -euo pipefail
if [ -n "${DEBUG:-}" ]; then set -v; fi
if [ -n "${TRACE:-}" ]; then set -xv; fi
local program version author
program="$(basename "$0")"
version="0.1.0"
author="System Initiative Inc. <[email protected]>"
# Parse CLI arguments and set local variables
parse_cli_args "$program" "$version" "$author" "$@"
local git_repo="$GIT_REPO"
local git_remote_ref="$GIT_REMOTE_REF"
unset GIT_REPO GIT_REMOTE_REF
need_cmd git
update "$git_repo" "$git_remote_ref"
}
parse_cli_args() {
local program version author
program="$1"
shift
version="$1"
shift
author="$1"
shift
local long_optarg
# Upstream Git repository with prelude
GIT_REPO="[email protected]:facebookincubator/buck2-prelude.git"
# Git ref to update from
GIT_REMOTE_REF="main"
OPTIND=1
# Parse command line flags and options
while getopts ":g:hr:V-:" opt; do
case $opt in
g)
GIT_REPO="$OPTARG"
;;
h)
print_usage "$program" "$version" "$author"
exit 0
;;
r)
GIT_REMOTE_REF="$OPTARG"
;;
V)
print_version "$program" "$version"
exit 0
;;
-)
long_optarg="${OPTARG#*=}"
case "$OPTARG" in
help)
print_usage "$program" "$version" "$author"
exit 0
;;
git-ref=?*)
GIT_REMOTE_REF="$long_optarg"
;;
git-ref*)
print_usage "$program" "$version" "$author" >&2
die "missing required argument for --$OPTARG option"
;;
git-repo=?*)
GIT_REPO="$long_optarg"
;;
git-repo*)
print_usage "$program" "$version" "$author" >&2
die "missing required argument for --$OPTARG option"
;;
version)
print_version "$program" "$version" "true"
exit 0
;;
'')
# "--" terminates argument processing
break
;;
*)
print_usage "$program" "$version" "$author" >&2
die "invalid argument --$OPTARG"
;;
esac
;;
\?)
print_usage "$program" "$version" "$author" >&2
die "invalid option: -$OPTARG"
;;
esac
done
shift "$((OPTIND - 1))"
}
need_cmd() {
if ! command -v "$1" >/dev/null; then
die "Required command '$1' not found on PATH"
fi
}
die() {
printf -- "\nxxx %s\n\n" "$1" >&2
exit 1
}
update() {
local git_repo="$1"
local git_remote_ref="$2"
local git_root
git_root="$(git rev-parse --show-toplevel)"
cd "$git_root"
echo "--- Pulling prelude updates from $git_repo#$git_remote_ref"
git subtree pull --prefix prelude "$git_repo" "$git_remote_ref" --squash
}
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
main "$@" || exit 1
fi