Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/src/lib/repo/configure.sh
blob: 73300ae41a58603f6dba5d2cf6d815567c10d25f (plain)
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
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
#
# SPDX-License-Identifier: GPL-3.0-or-later

[[ -z ${DEVTOOLS_INCLUDE_REPO_CONFIGURE_SH:-} ]] || return 0
DEVTOOLS_INCLUDE_REPO_CONFIGURE_SH=1

_DEVTOOLS_LIBRARY_DIR=${_DEVTOOLS_LIBRARY_DIR:-@pkgdatadir@}
# shellcheck source=src/lib/common.sh
source "${_DEVTOOLS_LIBRARY_DIR}"/lib/common.sh
# shellcheck source=src/lib/api/gitlab.sh
source "${_DEVTOOLS_LIBRARY_DIR}"/lib/api/gitlab.sh

source /usr/share/makepkg/util/config.sh
source /usr/share/makepkg/util/message.sh

set -e


pkgctl_repo_configure_usage() {
	local -r COMMAND=${_DEVTOOLS_COMMAND:-${BASH_SOURCE[0]##*/}}
	cat <<- _EOF_
		Usage: ${COMMAND} [OPTIONS] [PATH]...

		Configure Git packaging repositories according to distro specs and
		makepkg.conf settings.

		Git author information and the used signing key is set up from
		makepkg.conf read from any valid location like /etc or XDG_CONFIG_HOME.

		The remote protocol is automatically determined from the author email
		address by choosing SSH for all official packager identities and
		read-only HTTPS otherwise.

		OPTIONS
		    --protocol https     Configure remote url to use https
		    -j, --jobs N         Run up to N jobs in parallel (default: $(nproc))
		    -h, --help           Show this help text

		EXAMPLES
		    $ ${COMMAND} *
_EOF_
}

get_packager_name() {
	local packager=$1
	local packager_pattern="(.+) <(.+@.+)>"
	local name

	if [[ ! $packager =~ $packager_pattern ]]; then
		return 1
	fi

	name=$(echo "${packager}"|sed -E "s/${packager_pattern}/\1/")
	printf "%s" "${name}"
}

get_packager_email() {
	local packager=$1
	local packager_pattern="(.+) <(.+@.+)>"
	local email

	if [[ ! $packager =~ $packager_pattern ]]; then
		return 1
	fi

	email=$(echo "${packager}"|sed -E "s/${packager_pattern}/\2/")
	printf "%s" "${email}"
}

is_packager_name_valid() {
	local packager_name=$1
	if [[ -z ${packager_name} ]]; then
		return 1
	elif [[ ${packager_name} == "John Doe" ]]; then
		return 1
	elif [[ ${packager_name} == "Unknown Packager" ]]; then
		return 1
	fi
	return 0
}

is_packager_email_official() {
	local packager_email=$1
	if [[ -z ${packager_email} ]]; then
		return 1
	elif [[ $packager_email =~ .+@archlinux.org ]]; then
		return 0
	fi
	return 1
}

pkgctl_repo_configure() {
	# options
	local GIT_REPO_BASE_URL=${GIT_PACKAGING_URL_HTTPS}
	local official=0
	local proto=https
	local proto_force=0
	local jobs=
	jobs=$(nproc)
	local paths=()

	# variables
	local -r command=${_DEVTOOLS_COMMAND:-${BASH_SOURCE[0]##*/}}
	local path realpath pkgbase remote_url project_path
	local PACKAGER GPGKEY packager_name packager_email

	while (( $# )); do
		case $1 in
			-h|--help)
				pkgctl_repo_configure_usage
				exit 0
				;;
			--protocol=https)
				proto_force=1
				shift
				;;
			--protocol)
				(( $# <= 1 )) && die "missing argument for %s" "$1"
				if [[ $2 == https ]]; then
					proto_force=1
				else
					die "unsupported protocol: %s" "$2"
				fi
				shift 2
				;;
			-j|--jobs)
				(( $# <= 1 )) && die "missing argument for %s" "$1"
				jobs=$2
				shift 2
				;;
			--)
				shift
				break
				;;
			-*)
				die "invalid argument: %s" "$1"
				;;
			*)
				paths=("$@")
				break
				;;
		esac
	done

	# check if invoked without any path from within a packaging repo
	if (( ${#paths[@]} == 0 )); then
		if [[ -f PKGBUILD ]]; then
			paths=(".")
		else
			pkgctl_repo_configure_usage
			exit 1
		fi
	fi

	# Load makepkg.conf variables to be available for packager identity
	msg "Collecting packager identity from makepkg.conf"
	# shellcheck disable=2119
	load_makepkg_config
	if [[ -n ${PACKAGER} ]]; then
		if ! packager_name=$(get_packager_name "${PACKAGER}") || \
		   ! packager_email=$(get_packager_email "${PACKAGER}"); then
			die "invalid PACKAGER format '${PACKAGER}' in makepkg.conf"
		fi
		if ! is_packager_name_valid "${packager_name}"; then
			die "invalid PACKAGER '${PACKAGER}' in makepkg.conf"
		fi
		if is_packager_email_official "${packager_email}"; then
			official=1
			if (( ! proto_force )); then
				proto=ssh
				GIT_REPO_BASE_URL=${GIT_PACKAGING_URL_SSH}
			fi
		fi
	fi

	msg2 "name    : ${packager_name:-${YELLOW}undefined${ALL_OFF}}"
	msg2 "email   : ${packager_email:-${YELLOW}undefined${ALL_OFF}}"
	msg2 "gpg-key : ${GPGKEY:-${YELLOW}undefined${ALL_OFF}}"
	if [[ ${proto} == ssh ]]; then
		msg2 "protocol: ${GREEN}${proto}${ALL_OFF}"
	else
		msg2 "protocol: ${YELLOW}${proto}${ALL_OFF}"
	fi

	# parallelization
	if [[ ${jobs} != 1 ]] && (( ${#paths[@]} > 1 )); then
		if [[ -n ${BOLD} ]]; then
			export DEVTOOLS_COLOR=always
		fi
		if ! parallel --bar --jobs "${jobs}" "${command}" ::: "${paths[@]}"; then
			die 'Failed to configure some packages, please check the output'
			exit 1
		fi
		exit 0
	fi

	for path in "${paths[@]}"; do
		if ! realpath=$(realpath -e "${path}"); then
			die "No such directory: ${path}"
		fi

		pkgbase=$(basename "${realpath}")
		pkgbase=${pkgbase%.git}
		msg "Configuring ${pkgbase}"

		if [[ ! -d "${path}/.git" ]]; then
			die "Not a Git repository: ${path}"
		fi

		pushd "${path}" >/dev/null

		project_path=$(gitlab_project_name_to_path "${pkgbase}")
		remote_url="${GIT_REPO_BASE_URL}/${project_path}.git"
		if ! git remote add origin "${remote_url}" &>/dev/null; then
			git remote set-url origin "${remote_url}"
		fi

		# move the master branch to main
		if [[ $(git symbolic-ref --quiet --short HEAD) == master ]]; then
			git branch --move main
			git config branch.main.merge refs/heads/main
		fi

		git config devtools.version "${GIT_REPO_SPEC_VERSION}"
		git config pull.rebase true
		git config branch.autoSetupRebase always
		git config branch.main.remote origin
		git config branch.main.rebase true

		git config transfer.fsckobjects true
		git config fetch.fsckobjects true
		git config receive.fsckobjects true

		# setup author identity
		if [[ -n ${packager_name} ]]; then
			git config user.name "${packager_name}"
			git config user.email "${packager_email}"
		fi

		# force gpg for official packagers
		if (( official )); then
			git config commit.gpgsign true
		fi

		# set custom pgp key from makepkg.conf
		if [[ -n $GPGKEY ]]; then
			git config commit.gpgsign true
			git config user.signingKey "${GPGKEY}"
		fi

		if ! git ls-remote origin &>/dev/null; then
			warning "configured remote origin may not exist, run:"
			msg2 "pkgctl repo create ${pkgbase}"
		fi

		popd >/dev/null
	done
}