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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
#!/bin/bash
#
# SPDX-License-Identifier: GPL-3.0-or-later
[[ -z ${DEVTOOLS_INCLUDE_SEARCH_SH:-} ]] || return 0
DEVTOOLS_INCLUDE_SEARCH_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/cache.sh
source "${_DEVTOOLS_LIBRARY_DIR}"/lib/cache.sh
# shellcheck source=src/lib/api/gitlab.sh
source "${_DEVTOOLS_LIBRARY_DIR}"/lib/api/gitlab.sh
# shellcheck source=src/lib/valid-search.sh
source "${_DEVTOOLS_LIBRARY_DIR}"/lib/valid-search.sh
source /usr/share/makepkg/util/util.sh
source /usr/share/makepkg/util/message.sh
set -eo pipefail
pkgctl_search_usage() {
local -r COMMAND=${_DEVTOOLS_COMMAND:-${BASH_SOURCE[0]##*/}}
cat <<- _EOF_
Usage: ${COMMAND} [OPTIONS] QUERY
Search for an expression across the GitLab packaging group.
To use a filter, include it in your query. You may use wildcards (*) to
use glob matching.
Available filters for the blobs scope: path, extension
Every usage of the search command must be authenticated. Consult the
'pkgctl auth' command to authenticate with GitLab or view the
authentication status.
SEARCH TIPS
Syntax Description Example
───────────────────────────────────────
" Exact search "gem sidekiq"
~ Fuzzy search J~ Doe
| Or display | banner
+ And display +banner
- Exclude display -banner
* Partial bug error 50*
\\ Escape \\*md
# Issue ID #23456
! Merge request !23456
OPTIONS
-h, --help Show this help text
FILTER OPTIONS
--no-default-filter Do not apply default filter (like -path:keys/pgp/*.asc)
OUTPUT OPTIONS
--json Enable printing in JSON; Shorthand for '--format json'
-F, --format FORMAT Controls the formatting of the results; FORMAT is 'pretty',
'plain', or 'json' (default: pretty)
-N, --no-line-number Don't show line numbers when formatting results
EXAMPLES
$ ${COMMAND} linux
$ ${COMMAND} --json '"pytest -v" +PYTHONPATH'
_EOF_
}
pkgctl_search_check_option_group_format() {
local option=$1
local output_format=$2
if [[ -n ${output_format} ]]; then
die "The argument '%s' cannot be used with one or more of the other specified arguments" "${option}"
exit 1
fi
return 0
}
pkgctl_search() {
if (( $# < 1 )); then
pkgctl_search_usage
exit 0
fi
# options
local search
local output_format=
local use_default_filter=1
local line_numbers=1
# variables
local bat_style="header,grid"
local default_filter="-path:keys/pgp/*.asc"
local graphql_lookup_batch=200
local output result query entries from until length
local project_name_cache_file project_name_lookup project_ids project_id project_name project_slice
local mapping_output path startline currentline data line
while (( $# )); do
case $1 in
-h|--help)
pkgctl_search_usage
exit 0
;;
--no-default-filter)
use_default_filter=0
shift
;;
--json)
pkgctl_search_check_option_group_format "$1" "${output_format}"
output_format=json
shift
;;
-F|--format)
(( $# <= 1 )) && die "missing argument for %s" "$1"
pkgctl_search_check_option_group_format "$1" "${output_format}"
output_format="${2}"
if ! in_array "${output_format}" "${valid_search_output_format[@]}"; then
die "Unknown output format: %s" "${output_format}"
fi
shift 2
;;
-N|--no-line-number)
line_numbers=0
shift
;;
--)
shift
break
;;
-*)
die "invalid argument: %s" "$1"
;;
*)
break
;;
esac
done
if (( $# == 0 )); then
pkgctl_search_usage
exit 1
fi
# assign search parameter
search="${*}"
if (( use_default_filter )); then
search+=" ${default_filter}"
fi
# assign default output format
if [[ -z ${output_format} ]]; then
output_format=pretty
fi
# check for optional dependencies
if [[ ${output_format} == pretty ]] && ! command -v bat &>/dev/null; then
warning "Failed to find optional dependency 'bat': falling back to plain output"
output_format=plain
fi
# populate line numbers option
if (( line_numbers )); then
bat_style="numbers,${bat_style}"
fi
# call the gitlab search API
stat_busy "Querying gitlab search api"
output=$(gitlab_api_search "${search}")
stat_done
# collect project ids whose name needs to be looked up
project_name_cache_file=$(get_cache_file gitlab/project_id_to_name)
lock 11 "${project_name_cache_file}" "Locking project name cache"
mapfile -t project_ids < <(
jq --raw-output '[.[].project_id] | unique[]' <<< "${output}" | \
grep --invert-match --file <(awk '{ print $1 }' < "${project_name_cache_file}" ))
# look up project names
stat_busy "Querying project names"
local entries="${#project_ids[@]}"
local until=0
while (( until < entries )); do
from=${until}
until=$(( until + graphql_lookup_batch ))
if (( until > entries )); then
until=${entries}
fi
length=$(( until - from ))
project_slice=("${project_ids[@]:${from}:${length}}")
printf -v projects '"gid://gitlab/Project/%s",' "${project_slice[@]}"
query='{
projects(after: "" ids: ['"${projects}"']) {
pageInfo {
startCursor
endCursor
hasNextPage
}
nodes {
id
name
}
}
}'
mapping_output=$(gitlab_api_get_project_name_mapping "${query}")
# update cache
while read -r project_id project_name; do
printf "%s %s\n" "${project_id}" "${project_name}" >> "${project_name_cache_file}"
done < <(jq --raw-output \
'.[] | "\(.id | rindex("/") as $lastSlash | .[$lastSlash+1:]) \(.name)"' \
<<< "${mapping_output}")
done
stat_done
# read project_id to name mapping from cache
declare -A project_name_lookup=()
while read -r project_id project_name; do
project_name_lookup[${project_id}]=${project_name}
done < "${project_name_cache_file}"
# close project name cache lock
lock_close 11
# output mode JSON
if [[ ${output_format} == json ]]; then
jq --from-file <(
for project_id in $(jq '.[].project_id' <<< "${output}"); do
project_name=${project_name_lookup[${project_id}]}
printf 'map(if .project_id == %s then . + {"project_name": "%s"} else . end) | ' \
"${project_id}" "${project_name}"
done
printf .
) <<< "${output}"
exit 0
fi
# pretty print each result
while read -r result; do
# read properties from search result
mapfile -t data < <(jq --raw-output ".data" <<< "${result}")
{ read -r project_id; read -r path; read -r startline; } < <(
jq --raw-output ".project_id, .path, .startline" <<< "${result}"
)
project_name=${project_name_lookup[${project_id}]}
# remove trailing newline for multiline results
if (( ${#data[@]} > 1 )) && [[ ${data[-1]} == "" ]]; then
unset "data[${#data[@]}-1]"
fi
# output mode plain
if [[ ${output_format} == plain ]]; then
printf "%s%s%s\n" "${PURPLE}" "${project_name}/${path}" "${ALL_OFF}"
currentline=${startline}
for line in "${data[@]}"; do
if (( line_numbers )); then
line="${DARK_GREEN}${currentline}${ALL_OFF}: ${line}"
currentline=$(( currentline + 1 ))
fi
printf "%s\n" "${line}"
done
printf "\n"
continue
fi
# prepend empty lines to match startline
if (( startline > 1 )); then
mapfile -t data < <(
printf '%.0s\n' $(seq 1 "$(( startline - 1 ))")
printf "%s\n" "${data[@]}"
)
fi
bat \
--file-name="${project_name}/${path}" \
--line-range "${startline}:" \
--paging=never \
--force-colorization \
--style "${bat_style}" \
--map-syntax "PKGBUILD:Bourne Again Shell (bash)" \
--map-syntax ".SRCINFO:INI" \
--map-syntax "*install:Bourne Again Shell (bash)" \
--map-syntax "*sysusers*:Bourne Again Shell (bash)" \
--map-syntax "*tmpfiles*:Bourne Again Shell (bash)" \
--map-syntax "*.hook:INI" \
<(printf "%s\n" "${data[@]}")
done < <(jq --compact-output '.[]' <<< "${output}")
}
|