aurutils: v15
[aurutils.git] / lib / aur-fetch
blob17ebf49c0b12f7626ccc152957389938d4d86aad
1 #!/bin/bash
2 # aur-fetch - retrieve build files from the AUR
3 [[ -v AUR_DEBUG ]] && set -o xtrace
4 shopt -s extglob
5 argv0=fetch
6 XDG_CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}
7 XDG_CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
8 AUR_FETCH_USE_MIRROR=${AUR_FETCH_USE_MIRROR:-0}
9 AUR_LOCATION=${AUR_LOCATION:-https://aur.archlinux.org}
10 PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[1]}(): }'
12 # Author information for merge commits
13 export GIT_AUTHOR_NAME=aurutils
14 export GIT_AUTHOR_EMAIL=aurutils@localhost
15 export GIT_COMMITTER_NAME=aurutils
16 export GIT_COMMITTER_EMAIL=aurutils@localhost
17 export GIT_HTTP_USER_AGENT=aurutils
19 # Placeholder for repositories without commits
20 git_empty_object=$(git hash-object -t tree /dev/null)
22 # default options
23 existing=0 recurse=0 discard=0 sync=fetch
25 args_csv() {
26 # shellcheck disable=SC2155
27 local str=$(printf '%s,' "$@")
28 printf '%s' "${str%,}"
31 # XXX: races with multiple fetch instances
32 results() {
33 local mode=$1 prev=$2 current=$3 path=$4 dest=$5
35 if [[ -w $dest ]]; then
36 printf >> "$dest" '%s:%s:%s:file://%s\n' "$mode" "$prev" "$current" "$path"
40 sync_package_config() {
41 case $(git config --get --type bool aurutils.rebase) in
42 true)
43 printf >&2 '%s: aurutils.rebase is set for %s\n' "$argv0" "$1"
44 printf '%s' rebase ;;
46 printf '%s' merge ;;
47 esac
50 usage() {
51 cat <<! | base64 -d
52 ICAgICAgICAgICAgIC4tLX5+LF9fCjotLi4uLiwtLS0tLS0tYH5+Jy5fLicKIGAtLCwsICAs
53 XyAgICAgIDsnflUnCiAgXywtJyAsJ2AtX187ICctLS4KIChfLyd+fiAgICAgICcnJycoOwoK
55 printf >&2 'usage: %s [-Sefr] [--rebase|--reset|--merge] [--] pkgname...\n' "$argv0"
56 exit 1
59 # option handling
60 opt_short='efrS'
61 opt_long=('auto' 'merge' 'reset' 'rebase' 'discard' 'existing' 'results:' 'ff'
62 'ff-only' 'no-ff' 'no-commit' 'recurse')
63 opt_hidden=('dump-options' 'sync:')
65 if opts=$(getopt -o "$opt_short" -l "$(args_csv "${opt_long[@]}" "${opt_hidden[@]}")" -n "$argv0" -- "$@"); then
66 eval set -- "$opts"
67 else
68 usage
71 unset rebase_args merge_args results_file
72 while true; do
73 case "$1" in
74 # fetch options
75 -S|--auto)
76 sync=auto ;;
77 -f|--discard)
78 discard=1 ;;
79 -e|--existing)
80 existing=1 ;;
81 --merge)
82 sync=merge ;;
83 --rebase)
84 sync=rebase ;;
85 --reset)
86 sync=reset ;;
87 --results)
88 shift; results_file=$(realpath -- "$1") ;;
89 # git options
90 --ff)
91 merge_args+=(-ff) ;;
92 --ff-only)
93 merge_args+=(--ff-only) ;;
94 --no-commit)
95 merge_args+=(--no-commit) ;;
96 --no-ff)
97 merge_args+=(--no-ff); rebase_args+=(--no-ff) ;;
98 # Compatibility options
99 --sync)
100 shift; sync=$1 ;;
101 -r|--recurse)
102 recurse=1 ;;
103 --dump-options)
104 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
105 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
106 exit ;;
107 --) shift; break ;;
108 esac
109 shift
110 done
112 # Default to only allowing fast-forward merges (as git-pull)
113 if (( ! ${#merge_args[@]} )); then
114 merge_args=(--ff-only)
117 # option validation
118 if [[ $sync == !(auto|merge|rebase|reset|fetch) ]]; then
119 printf >&2 '%s: invalid --sync mode\n' "$argv0"
120 exit 1
123 if (( ! $# )); then
124 printf >&2 '%s: no targets specified\n' "$argv0"
125 exit 1
128 # XXX: race with concurrent processes
129 if [[ -v results_file ]]; then
130 : >"$results_file" || exit 1 # truncate file
133 # Save stdin/depends in array (2 passes)
134 if (( recurse )); then
135 mapfile -t packages < <(aur depends --reverse "$@" | tsort)
136 wait "$!" || exit
138 elif (( $# == 1 )) && [[ $1 == "-" || $1 == "/dev/stdin" ]]; then
139 mapfile -t packages
140 else
141 packages=("$@")
142 set --
145 # Exit gracefully on empty stdin, e.g. when piping from `aur repo -u`.
146 if (( ! ${#packages[@]} )); then
147 exit 0
150 # Update revisions in local AUR mirror
151 declare -A local_clones
153 # With an AUR mirror, updates are retrieved in two steps. First, updates to the
154 # mirror are synchronized with `git-fetch`. Secondly, local clones of the miror
155 # are created and are updated with `git-fetch` and `git-merge` as usual.
156 if (( AUR_FETCH_USE_MIRROR )); then
157 while IFS=':' read -r pkg head; do
158 printf "Cloning into '%s'\n" "$pkg"
159 git -C "$pkg" --no-pager log --pretty=reference -1
161 if [[ -v results_file ]]; then
162 results 'clone' "$git_empty_object" "$head" "$PWD/$pkg" "$results_file"
164 local_clones[$pkg]=$head
165 done < <(
166 aur fetch--mirror --lclone "${packages[@]}"
168 wait "$!" || exit
171 # Main loop
172 for pkg in "${packages[@]}"; do
173 unset -f git
175 # Local clone by fetch--mirror
176 if [[ ${local_clones[$pkg]} ]]; then
177 continue
179 # Verify if the repository is hosted on AUR (#959)
180 elif (( existing )) && ! git ls-remote --exit-code "$AUR_LOCATION/$pkg" >/dev/null; then
181 printf >&2 '%s: warning: package %s is not in AUR, skipping\n' "$argv0" "$pkg"
182 continue
184 # Clone package if not existing
185 elif [[ ! -d $pkg/.git ]]; then
186 git clone "$AUR_LOCATION/$pkg" || exit 1
188 head=$(git -C "$pkg" rev-parse --verify HEAD)
189 [[ $head ]] && git -C "$pkg" --no-pager log --pretty=reference -1
191 if [[ -v results_file ]]; then
192 results 'clone' "$git_empty_object" "${head:-$git_empty_object}" "$PWD/$pkg" "$results_file"
195 # Update existing git repository
196 else
197 # Per-package lock
198 exec {fd}< "$pkg"/.git
199 flock --wait 5 "$fd" || exit 1
201 # Avoid issues with filesystem boundaries (#274)
202 git() { command git -C "$pkg" "$@"; }
204 # Retrieve per-package configuration (aurutils.rebase, #1007)
205 if [[ $sync == 'auto' ]]; then
206 sync_pkg=$(sync_package_config "$pkg")
207 else
208 sync_pkg=$sync
211 # Retrieve new upstream commits
212 git fetch origin || exit
214 # Store original HEAD for --results output
215 orig_head=$(git rev-parse --verify --quiet HEAD)
216 orig_head=${orig_head:-$git_empty_object}
218 # Set correct branch for rebasing local clones
219 if (( AUR_FETCH_USE_MIRROR )); then
220 upstream=origin/$pkg # master -> origin/$pkg
221 else
222 upstream=origin/master # master -> origin/master
225 # Merge in new history
226 case $sync_pkg in
227 rebase)
228 dest='HEAD'
229 if (( discard )); then
230 # reset worktree
231 git checkout ./
233 git rebase "${rebase_args[@]}" "$upstream" ;;
234 merge)
235 dest='HEAD'
236 if (( discard )); then
237 # reset worktree if new commits are available
238 git merge-base --is-ancestor 'master@{u}' HEAD || git checkout ./
240 git merge "${merge_args[@]}" "$upstream" ;;
241 reset)
242 dest='master@{u}'
243 git reset --hard 'master@{u}' ;;
244 fetch)
245 dest='master@{u}'
247 esac || {
248 printf >&2 '%s: failed to %s %s\n' "$argv0" "$sync_pkg" "$pkg"
249 exit 1
251 head=$(git rev-parse --verify "$dest")
253 if [[ -v results_file ]]; then
254 results "$sync_pkg" "$orig_head" "$head" "$PWD/$pkg" "$results_file"
256 exec {fd}<&- # release lock
257 fi >&2 # print all git output to stderr
258 done
260 # vim: set et sw=4 sts=4 ft=sh: