Merge pull request #987 from AladW/pkglist-local-ttl
[aurutils.git] / lib / aur-repo
blobea8e174568a5df2a2b7565a429467239755a7f19
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 field=none status=0
10 # default arguments
11 conf_args=()
13 db_field() {
14 # do not extract an empty database (#727)
15 (( $(bsdtar -tf "$2" | wc -l) )) || return 0
17 bsdtar -Oxf "$2" '*/desc' | awk -v field="^%$1%$" '
18 $0 ~ field {
19 while (getline && NF != 0) { print; }
23 db_table() {
24 # do not extract an empty database (#727)
25 (( $(bsdtar -tf "$2" | wc -l) )) || return 0
27 bsdtar -Oxf "$2" '*/desc' | awk -v table="$1" '
28 function print_previous() {
29 table ? format = "%s\t%s\t%s\t%s\n" : format = "%1$s\t%4$s\n"
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]
46 /^%FILENAME%$/ && FNR > 1 {
47 print_previous()
48 pkgname = pkgbase = pkgver = "-"
49 delete dependencies
52 /^%NAME%$/ {
53 getline
54 pkgname = $1
57 /^%BASE%$/ {
58 getline
59 pkgbase = $1
62 /^%VERSION%$/ {
63 getline
64 pkgver = $1
67 /^%(MAKE|CHECK)?DEPENDS%$/ {
68 while (table && getline && $0 != "") {
69 dependencies[i++] = $1
73 END {
74 print_previous()
78 usage() {
79 printf >&2 'usage: %s [-d repo] [-r path] [-alqtuS]\n' "$argv0"
80 exit 1
83 source /usr/share/makepkg/util/parseopts.sh
85 ## option parsing
86 opt_short='c:d:r:F:alqtuS'
87 opt_long=('config:' 'database:' 'repo:' 'root:' 'all' 'list' 'path'
88 'path-list' 'repo-list' 'sync' 'upgrades' 'table' 'quiet'
89 'status-file:' 'field:')
90 opt_hidden=('dump-options' 'list-repo' 'list-path' 'status')
92 if ! parseopts "$opt_short" "${opt_long[@]}" "${opt_hidden[@]}" -- "$@"; then
93 usage
95 set -- "${OPTRET[@]}"
97 unset mode list db_name db_root status_file pacman_conf vercmp_args field
98 while true; do
99 case $1 in
100 -d|--database|--repo)
101 shift; db_name=$1 ;;
102 -r|--root)
103 shift; db_root=$1 ;;
104 -c|--config)
105 shift; pacman_conf=$1 ;;
106 -l|--list)
107 mode=packages; table_format=0 ;;
108 -t|--table)
109 mode=packages; table_format=1 ;;
110 -a|--all)
111 mode=upgrades; vercmp_args+=(-a) ;;
112 -q|--quiet)
113 mode=upgrades; vercmp_args+=(-q) ;;
114 -u|--upgrades)
115 mode=upgrades ;;
116 --status-file)
117 shift; status_file=$1 ;;
118 -S|--sync)
119 db_query=sync ;;
120 -F|--field)
121 shift; mode=files; field=$1 ;;
122 ## deprecated options
123 --path)
124 mode=path ;;
125 --path-list|--list-path)
126 list=path ;;
127 --repo-list|--list-repo)
128 list=repo ;;
129 --status)
130 status=1 ;;
131 --dump-options)
132 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
133 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
134 exit ;;
135 --) shift; break ;;
136 esac
137 shift
138 done
140 if [[ -v pacman_conf ]]; then
141 conf_args+=(--config "$pacman_conf")
144 # assign environment variables
145 : "${db_ext=$AUR_DBEXT}" "${db_name=$AUR_REPO}" "${db_root=$AUR_DBROOT}"
147 unset conf_file_repo conf_file_serv conf_file_path server
148 while read -r key _ value; do
149 case $key=$value in
150 \[*\]*)
151 section=${key:1:-1}
153 DBPath=*)
154 pacman_dbpath=$value
156 Server=file://*)
157 server=${value#file://}
158 conf_file_repo+=("$section")
159 conf_file_serv+=("$server")
160 conf_file_path+=("$server/$section.${db_ext:-db}")
162 if [[ $section == "$db_name" ]]; then
163 if ! [[ $db_root ]]; then
164 db_root=$server
165 elif [[ $db_root != "$server" ]]; then
166 printf >&2 '%s: warning: --root and pacman.conf mismatch (%s)\n' "$argv0" "$db_name"
170 Server=*://*)
171 if [[ $section == "$db_name" ]] && ! [[ $db_root ]]; then
172 db_root=$value
175 esac
176 done < <(pacman-conf "${conf_args[@]}")
177 wait $! || exit
179 # list information on available local repositories
180 case $list in
181 path|repo)
182 if ! [[ ${conf_file_repo[*]} ]]; then
183 printf >&2 '%s: no file:// repository configured\n' "$argv0"
184 exit 2
187 path)
188 realpath -- "${conf_file_path[@]}" # resolve repo-add symlink
189 exit 0
191 repo)
192 printf '%s\n' "${conf_file_repo[@]}"
193 exit 0
195 esac
197 # select local repository from pacman configuration, if no repository
198 # was specified on the command-line
199 if ! [[ $db_name ]]; then
200 case ${#conf_file_repo[@]} in
201 1) db_name=${conf_file_repo[0]}
202 db_root=${conf_file_serv[0]}
204 0) printf >&2 '%s: no file:// repository configured\n' "$argv0"
205 exit 2
207 *) printf >&2 '%s: repository choice is ambiguous (use -d to specify)\n' "$argv0"
208 for i in "${!conf_file_repo[@]}"; do
209 printf '%q\t%q\n' "${conf_file_repo[$i]}" "${conf_file_path[$i]}"
210 done | column -o $'\t' -t >&2
211 exit 1
213 esac
216 case $db_query in
217 local)
218 if ! [[ $db_root ]]; then
219 printf >&2 '%s: %s: repository path not found\n' "$argv0" "$db_name"
220 exit 2
221 elif [[ $db_root == *://* ]]; then
222 printf >&2 '%s: %s: object is remote (use -S to query)\n' "$argv0" "$db_root"
223 exit 66
224 elif ! [[ -d $db_root ]]; then
225 printf >&2 '%s: %s: not a directory\n' "$argv0" "$db_root"
226 exit 20
228 db_path=$db_root/$db_name.${db_ext:-db}
229 db_path=$(realpath -- "$db_path") # resolve repo-add symlink
231 sync)
232 db_path=$pacman_dbpath/sync/$db_name.${db_ext:-db}
234 esac
236 if [[ ! -f $db_path ]]; then
237 printf >&2 '%s: %s: not a file\n' "$argv0" "$db_path"
238 exit 2
241 if (( status )); then
242 printf 'repo:%s\nroot:%s\npath:%s\n' "$db_name" "$db_root" "$db_path"
244 elif [[ -v status_file ]]; then
245 printf 'repo:%s\nroot:%s\npath:%s\n' "$db_name" "$db_root" "$db_path" >"$status_file"
248 case $mode in
249 files)
250 db_field "${field^^}" "$db_path"
252 upgrades)
253 db_table '0' "$db_path" | aur vercmp "${vercmp_args[@]}"
255 packages)
256 db_table "$table_format" "$db_path"
258 path)
259 printf '%s\n' "$db_path"
263 esac
265 # vim: set et sw=4 sts=4 ft=sh: