Merge pull request #1008 from AladW/fetch-rework-iv
[aurutils.git] / lib / aur-repo
blob4ed4024549920e6053503c7757bfe4855f63a4a0
1 #!/bin/bash
2 # aur-repo - manage local repositories
3 [[ -v AUR_DEBUG ]] && set -o xtrace
4 argv0=repo
5 PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
7 # default options
8 db_query=local table_format=0 mode=none attr=none status=0 quiet=0
10 # default arguments
11 conf_args=()
13 db_count() {
14 bsdtar -tf "$1" | wc -l
17 db_attr() {
18 bsdtar -Oxf "$2" '*/desc' | awk -v attr="^%$1%$" '
19 $0 ~ attr {
20 while (getline && NF != 0) { print; }
24 db_attr_list() {
25 bsdtar -Oxf "$1" '*/desc' | awk -F% '/^%.+%$/ {print $2}' | sort -u
28 db_table() {
29 bsdtar -Oxf "$3" '*/desc' | awk -v table="$1" -v quiet="$2" '
30 function print_previous(format) {
31 # Add dependency to self (packages with no dependencies)
32 printf format, pkgname, pkgname, pkgbase, pkgver
34 if (table) {
35 for (i in dependencies) {
36 printf format, pkgname, dependencies[i], pkgbase, pkgver
41 BEGIN {
42 pkgname = pkgbase = pkgver = "-"
43 delete dependencies[0]
45 if (table)
46 format = "%s\t%s\t%s\t%s\n"
47 else if (quiet)
48 format = "%1$s\n"
49 else
50 format = "%1$s\t%4$s\n"
53 /^%FILENAME%$/ && FNR > 1 {
54 print_previous(format)
55 pkgname = pkgbase = pkgver = "-"
56 delete dependencies
59 /^%NAME%$/ {
60 getline
61 pkgname = $1
64 /^%BASE%$/ {
65 getline
66 pkgbase = $1
69 /^%VERSION%$/ {
70 getline
71 pkgver = $1
74 /^%(MAKE|CHECK)?DEPENDS%$/ {
75 while (table && getline && $0 != "") {
76 dependencies[i++] = $1
80 END {
81 print_previous(format)
85 usage() {
86 printf >&2 'usage: %s [-d repo] [-r path] [-alqtuS]\n' "$argv0"
87 exit 1
90 source /usr/share/makepkg/util/parseopts.sh
92 ## option parsing
93 opt_short='c:d:r:F:alqtuS'
94 opt_long=('config:' 'database:' 'root:' 'all' 'list' 'path' 'list-path' 'list-repo'
95 'list-attr' 'sync' 'upgrades' 'table' 'quiet' 'status-file:' 'attr:')
96 opt_hidden=('dump-options' 'repo:' 'repo-list' 'path-list' 'status')
98 if ! parseopts "$opt_short" "${opt_long[@]}" "${opt_hidden[@]}" -- "$@"; then
99 usage
101 set -- "${OPTRET[@]}"
103 unset mode list db_name db_root status_file pacman_conf vercmp_args attr
104 while true; do
105 case $1 in
106 -d|--database|--repo)
107 shift; db_name=$1 ;;
108 -r|--root)
109 shift; db_root=$1 ;;
110 -c|--config)
111 shift; pacman_conf=$1 ;;
112 -l|--list)
113 mode=packages; table_format=0 ;;
114 -t|--table)
115 mode=packages; table_format=1 ;;
116 -a|--all)
117 mode=upgrades; vercmp_args+=(-a) ;;
118 -u|--upgrades)
119 mode=upgrades ;;
120 --list-attr)
121 mode=list_attr ;;
122 -q|--quiet)
123 quiet=1; vercmp_args+=(-q) ;;
124 --status-file)
125 shift; status_file=$1 ;;
126 -S|--sync)
127 db_query=sync ;;
128 -F|--attr)
129 shift; mode=attr; attr=$1 ;;
130 ## deprecated options
131 --path)
132 mode=path ;;
133 --path-list|--list-path)
134 list=path ;;
135 --repo-list|--list-repo)
136 list=repo ;;
137 --status)
138 status=1 ;;
139 --dump-options)
140 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
141 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
142 exit ;;
143 --) shift; break ;;
144 esac
145 shift
146 done
148 if [[ -v pacman_conf ]]; then
149 conf_args+=(--config "$pacman_conf")
152 # assign environment variables
153 : "${db_ext=$AUR_DBEXT}" "${db_name=$AUR_REPO}" "${db_root=$AUR_DBROOT}"
155 unset conf_file_repo conf_file_serv conf_file_path server
156 while read -r key _ value; do
157 case $key=$value in
158 \[*\]*)
159 section=${key:1:-1}
161 DBPath=*)
162 pacman_dbpath=$value
164 Server=file://*)
165 server=${value#file://}
166 conf_file_repo+=("$section")
167 conf_file_serv+=("$server")
168 conf_file_path+=("$server/$section.${db_ext:-db}")
170 if [[ $section == "$db_name" ]]; then
171 if ! [[ $db_root ]]; then
172 db_root=$server
173 elif [[ $db_root != "$server" ]]; then
174 printf >&2 '%s: warning: --root and pacman.conf mismatch (%s)\n' "$argv0" "$db_name"
178 Server=*://*)
179 if [[ $section == "$db_name" ]] && ! [[ $db_root ]]; then
180 db_root=$value
183 esac
184 done < <(pacman-conf "${conf_args[@]}")
185 wait $! || exit
187 # list information on available local repositories
188 case $list in
189 path|repo)
190 if ! [[ ${conf_file_repo[*]} ]]; then
191 printf >&2 '%s: no file:// repository configured\n' "$argv0"
192 exit 2
195 path)
196 realpath -- "${conf_file_path[@]}" # resolve repo-add symlink
197 exit 0
199 repo)
200 printf '%s\n' "${conf_file_repo[@]}"
201 exit 0
203 esac
205 # select local repository from pacman configuration, if no repository
206 # was specified on the command-line
207 if ! [[ $db_name ]]; then
208 case ${#conf_file_repo[@]} in
209 1) db_name=${conf_file_repo[0]}
210 db_root=${conf_file_serv[0]}
212 0) printf >&2 '%s: no file:// repository configured\n' "$argv0"
213 exit 2
215 *) printf >&2 '%s: repository choice is ambiguous (use -d to specify)\n' "$argv0"
216 for i in "${!conf_file_repo[@]}"; do
217 printf '%q\t%q\n' "${conf_file_repo[$i]}" "${conf_file_path[$i]}"
218 done | column -o $'\t' -t >&2
219 exit 1
221 esac
224 case $db_query in
225 local)
226 if ! [[ $db_root ]]; then
227 printf >&2 '%s: %s: repository path not found\n' "$argv0" "$db_name"
228 exit 2
229 elif [[ $db_root == *://* ]]; then
230 printf >&2 '%s: %s: object is remote (use -S to query)\n' "$argv0" "$db_root"
231 exit 66
232 elif ! [[ -d $db_root ]]; then
233 printf >&2 '%s: %s: not a directory\n' "$argv0" "$db_root"
234 exit 20
236 db_path=$db_root/$db_name.${db_ext:-db}
237 db_path=$(realpath -- "$db_path") # resolve repo-add symlink
239 sync)
240 db_path=$pacman_dbpath/sync/$db_name.${db_ext:-db}
242 esac
244 if [[ ! -f $db_path ]]; then
245 printf >&2 '%s: %s: not a file\n' "$argv0" "$db_path"
246 exit 2
247 elif (( status )); then
248 printf 'repo:%s\nroot:%s\npath:%s\n' "$db_name" "$db_root" "$db_path"
249 elif [[ -v status_file ]]; then
250 printf 'repo:%s\nroot:%s\npath:%s\n' "$db_name" "$db_root" "$db_path" >"$status_file"
253 # empty mode, only print path
254 if [[ $mode == "path" ]]; then
255 printf '%s\n' "$db_path"
256 exit 0
259 # do not extract an empty database (#727)
260 if ! (( $(db_count "$db_path") )); then
261 exit 0
264 # database operations
265 case $mode in
266 upgrades)
267 db_table "0" "0" "$db_path" | aur vercmp "${vercmp_args[@]}"
269 packages)
270 db_table "$table_format" "$quiet" "$db_path"
272 attr)
273 db_attr "${attr^^}" "$db_path"
275 list_attr)
276 db_attr_list "$db_path"
280 esac
282 # vim: set et sw=4 sts=4 ft=sh: