2 # aur-fetch - retrieve build files from the AUR
3 [[ -v AUR_DEBUG
]] && set -o xtrace
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
)
23 existing
=0 recurse
=0 discard
=0 sync
=fetch
26 # shellcheck disable=SC2155
27 local str
=$
(printf '%s,' "$@")
28 printf '%s' "${str%,}"
31 # XXX: races with multiple fetch instances
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
43 printf >&2 '%s: aurutils.rebase is set for %s\n' "$argv0" "$1"
51 local upstream
=$1 dest
=$2 pkg
=$3
53 # Check if last upstream commit can be reached from $dest
54 if ! git merge-base
--is-ancestor "$upstream" "$dest"; then
57 # Print diagnostic with prefixed argv0 (unlike git)
58 printf >&2 '%s: %s: already up to date\n' "$argv0" "$pkg"
65 ICAgICAgICAgICAgIC4tLX5
+LF9fCjotLi4uLiwtLS0tLS0tYH5
+Jy5fLicKIGAtLCwsICAs
66 XyAgICAgIDsnflUnCiAgXywtJyAsJ2AtX187ICctLS4KIChfLyd
+fiAgICAgICcnJycoOwoK
68 printf >&2 'usage: %s [-Sefr] [--rebase|--reset|--merge] [--] pkgname...\n' "$argv0"
74 opt_long
=('auto' 'merge' 'reset' 'rebase' 'discard' 'existing' 'results:' 'ff'
75 'ff-only' 'no-ff' 'no-commit' 'recurse')
76 opt_hidden
=('dump-options' 'sync:')
78 if opts
=$
(getopt
-o "$opt_short" -l "$(args_csv "${opt_long[@]}" "${opt_hidden[@]}")" -n "$argv0" -- "$@"); then
84 unset rebase_args merge_args results_file
101 shift; results_file
=$
(realpath
-- "$1") ;;
106 merge_args
+=(--ff-only) ;;
108 merge_args
+=(--no-commit) ;;
110 merge_args
+=(--no-ff); rebase_args
+=(--no-ff) ;;
111 # Compatibility options
117 printf -- '--%s\n' "${opt_long[@]}" ${AUR_DEBUG+"${opt_hidden[@]}"}
118 printf -- '%s' "${opt_short}" | sed 's/.:\?/-&\n/g'
125 # Default to only allowing fast-forward merges (as git-pull)
126 if (( ! ${#merge_args[@]} )); then
127 merge_args=(--ff-only)
131 if [[ $sync == !(auto|merge|rebase|reset|fetch) ]]; then
132 printf >&2 '%s: invalid --sync mode\n' "$argv0"
137 printf >&2 '%s: no targets specified\n' "$argv0"
141 # XXX: race with concurrent processes
142 if [[ -v results_file ]]; then
143 : >"$results_file" || exit 1 # truncate file
146 # Save stdin/depends in array (2 passes)
147 if (( recurse )); then
148 mapfile -t packages < <(aur depends --reverse "$@
" | tsort)
151 elif (( $# == 1 )) && [[ $1 == "-" || $1 == "/dev
/stdin
" ]]; then
158 # Exit gracefully on empty stdin, e.g. when piping from `aur repo -u`.
159 if (( ! ${#packages[@]} )); then
163 # Update revisions in local AUR mirror
164 declare -A local_clones
166 # With an AUR mirror, updates are retrieved in two steps. First, updates to the
167 # mirror are synchronized with `git-fetch`. Secondly, local clones of the miror
168 # are created and are updated with `git-fetch` and `git-merge` as usual.
169 if (( AUR_FETCH_USE_MIRROR )); then
170 while IFS=':' read -r pkg head; do
171 printf "Cloning into
'%s'\n" "$pkg"
172 git -C "$pkg" --no-pager log --pretty=reference -1
174 if [[ -v results_file ]]; then
175 results 'clone' "$git_empty_object" "$head" "$PWD/$pkg" "$results_file"
177 local_clones[$pkg]=$head
179 aur fetch--mirror --lclone "${packages[@]}"
185 for pkg in "${packages[@]}"; do
188 # Local clone by fetch--mirror
189 if [[ ${local_clones[$pkg]} ]]; then
192 # Verify if the repository is hosted on AUR (#959)
193 elif (( existing )) && ! git ls-remote --exit-code "$AUR_LOCATION/$pkg" >/dev/null; then
194 printf >&2 '%s: warning: package %s is not in AUR, skipping\n' "$argv0" "$pkg"
197 # Clone package if not existing
198 elif [[ ! -d $pkg/.git ]]; then
199 git clone "$AUR_LOCATION/$pkg" || exit 1
201 head=$(git -C "$pkg" rev-parse --verify HEAD)
202 [[ $head ]] && git -C "$pkg" --no-pager log --pretty=reference -1
204 if [[ -v results_file ]]; then
205 results 'clone' "$git_empty_object" "${head:-$git_empty_object}" "$PWD/$pkg" "$results_file"
208 # Update existing git repository
211 exec {fd}< "$pkg"/.git
212 flock --wait 5 "$fd" || exit 1
214 # Avoid issues with filesystem boundaries (#274)
215 git() { command git -C "$pkg" "$@
"; }
217 # Retrieve per-package configuration (aurutils.rebase, #1007)
218 if [[ $sync == 'auto' ]]; then
219 sync_pkg=$(sync_package_config "$pkg")
224 # Retrieve new upstream commits
225 git fetch origin || exit
227 # Store original HEAD for --results output
228 orig_head=$(git rev-parse --verify --quiet HEAD)
229 orig_head=${orig_head:-$git_empty_object}
231 # Merge in new history
237 if sync_should_merge "$upstream" "$dest" "$pkg"; then
238 if (( discard )); then
241 git rebase "${rebase_args[@]}" "$upstream"
245 if sync_should_merge "$upstream" "$dest" "$pkg"; then
246 if (( discard )); then
249 git merge "${merge_args[@]}" "$upstream"
253 git reset --hard "$dest"
259 printf >&2 '%s: failed to %s %s\n' "$argv0" "$sync_pkg" "$pkg"
262 head=$(git rev-parse --verify "$dest")
264 if [[ -v results_file ]]; then
265 results "$sync_pkg" "$orig_head" "$head" "$PWD/$pkg" "$results_file"
267 exec {fd}<&- # release lock
268 fi >&2 # print all git output to stderr
271 # vim: set et sw=4 sts=4 ft=sh: