-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsync-with-gentoo
More file actions
executable file
·248 lines (220 loc) · 6.46 KB
/
sync-with-gentoo
File metadata and controls
executable file
·248 lines (220 loc) · 6.46 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/bin/bash
# Used for syncing with gentoo. Needs to be called from the
# toplevel-directory of portage-stable. Expects the actual gentoo repo
# to be either in ../gentoo or ../../gentoo.
#
# Example invocations:
#
# sync-with-gentoo --help
#
# Print a help message.
#
# sync-with-gentoo dev-libs/nettle app-crypt/argon2
#
# This will update the packages, each in a separate commit. The
# commit message will contain the commit hash from gentoo repo.
#
# sync-with-gentoo dev-libs
#
# This will update all the packages in dev-libs category.
#
set -euo pipefail
fail() {
printf '%s\n' "$*" >&2
exit 1
}
declare -a GLOBAL_extra_git_commit_options=()
GLOBAL_single_commit=''
declare -a GLOBAL_obsolete_packages=()
GLOBAL_gentoo_repo="${GENTOO_REPO:-../gentoo}"
GLOBAL_amend_mode=''
while true; do
case "${1-}" in
'--help'|'-h')
echo "${0} [OPTIONS] CATEGORY[/PACKAGE_NAME] [CATEGORY[/PACKAGE_NAME] […]]"
echo 'OPTIONS:'
echo ' --help|-h: Print this help and quit'
echo ' --message|-m: Additional messages for commits, will be passed to git commit --message, can be specified many times'
echo ' --amend-mode|-a: Updates commit message to use the new used hash sum of the commit.'
echo ' --single-commit: Lump all the changes under a single commit'
echo
echo 'ENVIRONMENT VARIABLES:'
echo ' GENTOO_REPO: Path to the Gentoo repo, from which the script syncs stuff.'
echo
exit 0
;;
'--message'|'-m')
GLOBAL_extra_git_commit_options+=(--message "${2}")
shift 2
;;
'--amend-mode'|'-a')
GLOBAL_amend_mode=1
shift
;;
'--single-commit'|'-s')
GLOBAL_single_commit='x'
shift
;;
*)
break
;;
esac
done
if [[ $# -lt 1 ]]; then
fail 'expected at least one package, try --help or -h'
fi
if [[ ! -e 'profiles/repo_name' ]]; then
fail 'sync is only possible from ebuild packages top-level directory (a directory from which "./profiles/repo_name" is accessible)'
fi
if [[ ! -d "${GLOBAL_gentoo_repo}" ]]; then
gentoo_repo_tmp='../../gentoo'
if [[ ! -d "${gentoo_repo_tmp}" ]]; then
fail "can't find Gentoo repo (tried ${GLOBAL_gentoo_repo} and ${gentoo_repo_tmp}), try using GENTOO_REPO environment variable"
fi
GLOBAL_gentoo_repo="${gentoo_repo_tmp}"
unset -v gentoo_repo_tmp
fi
if [[ $(realpath '.') = $(realpath "${GLOBAL_gentoo_repo}") ]]; then
fail 'trying to sync within a Gentoo repo?'
fi
echo "using Gentoo repo at ${GLOBAL_gentoo_repo}"
commit_and_show() {
if [[ -n "$(git status --porcelain | grep -v '^ ')" ]]; then
git commit \
--quiet \
--signoff \
"${@}" \
"${GLOBAL_extra_git_commit_options[@]}"
GIT_PAGER=cat git show --stat
return 0
fi
return 1
}
sync_git_prepare() {
local path="${1}"
local sync=''
local gentoo_path="${GLOBAL_gentoo_repo}/${path}"
local rsync_opts=()
if [[ ! -e "${gentoo_path}" ]]; then
GLOBAL_obsolete_packages+=("${path}")
return 0
fi
case "${path}" in
profiles) rsync_opts+=( --exclude /profiles/repo_name )
esac
if [[ -e "${path}" ]]; then
sync='x'
fi
mkdir --parents "$(dirname ${path})"
rsync --archive --delete-before "${rsync_opts[@]}" "${gentoo_path}" "$(dirname ${path})"
git add "${path}"
if [[ -n "${sync}" ]]; then
return 1
fi
return 0
}
maybe_commit_with_gentoo_sha() {
local path="${1}"
local name="${2}"
local sync="${3}"
if [[ -z "${GLOBAL_single_commit}" ]]; then
local do_amend=''
local do_add=''
if [[ -n "${GLOBAL_amend_mode}" ]]; then
local subject
subject="$(git log -1 --pretty=format:%s)"
if [[ "${subject}" = "${name}:"* ]]; then
do_amend=1
fi
if [[ "${subject}" = *'Add from Gentoo'* ]]; then
do_add=1
fi
fi
local commit=''
local commit_msg=''
local amend_args=()
if [[ -n "${do_amend}" ]]; then
amend_args+=(--amend --no-edit)
fi
commit=$(git -C "${GLOBAL_gentoo_repo}" log --pretty=oneline -1 -- "${path}" | cut -f1 -d' ')
commit_msg="${name}: Add from Gentoo"
if [[ -n "${sync}" ]] && [[ -z "${do_add}" ]]; then
commit_msg="${name}: Sync with Gentoo"
fi
if ! commit_and_show \
"${amend_args[@]}" \
--message "${commit_msg}" \
--message "It's from Gentoo commit ${commit}."; then
echo "no changes in ${path}"
fi
fi
}
path_sync() {
local path="${1}"
local name="${2}"
local sync=''
if ! sync_git_prepare "${path}"; then
sync='x'
fi
maybe_commit_with_gentoo_sha "${path}" "${name}" "${sync}"
}
category_sync() {
local path="${1}"
local sync=''
if [[ ! -e "${path}" ]]; then
sync_git_prepare "${path}" || :
else
local pkg=''
for pkg in "${path}"/*; do
sync_git_prepare "${pkg}" || :
done
sync='x'
fi
maybe_commit_with_gentoo_sha "${path}" "${path}" "${sync}"
}
for cpn; do
while [[ "${cpn}" != "${cpn%/}" ]]; do
cpn="${cpn%/}"
done
case "${cpn}" in
licenses|eclass/tests|eclass|profiles|scripts)
path_sync "${cpn}" "${cpn}"
;;
eclass/*.eclass)
path_sync "${cpn}" "${cpn%.eclass}"
;;
metadata/glsa)
path_sync "${cpn}" "${cpn}"
;;
metadata)
fail "metadata directory can't be synced"
;;
virtual/*/*|*-*/*/*)
fail "invalid thing to sync: ${cpn}"
;;
virtual/*|*-*/*)
path_sync "${cpn}" "${cpn}"
;;
virtual|*-*)
category_sync "${cpn}"
;;
*)
fail "invalid thing to sync: ${cpn}"
;;
esac
done
if [[ -n "${GLOBAL_single_commit}" ]]; then
if ! commit_and_show \
--message '*: Sync with Gentoo'; then
echo 'no changes made'
fi
fi
if [[ ${#GLOBAL_obsolete_packages[@]} -gt 0 ]]; then
echo
echo 'the following packages are obsolete (not found in gentoo repo):'
printf ' %s\n' "${GLOBAL_obsolete_packages[@]}"
echo
fi
echo
echo 'done'
echo