summaryrefslogtreecommitdiff
path: root/asp.in
blob: 6471aa154a196a7135c9dd830213f217fdc6f4a0 (plain)
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/bin/bash

ARCH_GIT_REPOS=(packages community)

OPT_ARCH=$(uname -m)
OPT_FORCE=0
: ${ASPROOT:=${XDG_CACHE_HOME:-$HOME/.cache/asp}}
: ${ASPCACHE:=$ASPROOT/cache}

m4_include(util.inc.sh)
m4_include(remote.inc.sh)
m4_include(package.inc.sh)
m4_include(archweb.inc.sh)

usage() {
  cat<<EOF
asp [OPTIONS...] {COMMAND} ...

Manage build sources for Arch packages.

Options:
  -a           ARCH        Specify an architecture other than the host's
  -f                       Allow files to be overwritten
  -h                       Show this help
  -V                       Show package version

Commands:
  checkout     NAME...     Create a mutable git repository for packages
  difflog      NAME        Show revision history with diffs
  export       NAME...     Export packages
  gc                       Cleanup and optimize the local repository
  disk-usage               Show amount of disk used by locally tracked packages
  help                     Show this help
  list-all                 List all known packages
  list-arches  NAME...     List architectures for packages
  list-local               List tracked packages
  list-repos   NAME...     List repos for packages
  ls-files     NAME        List files for package
  log          NAME        Show revision history
  shortlog     NAME        Show revision history in short form
  show         NAME [FILE] Show the PKGBUILD or other FILE
  update       [NAME...]   Update packages (update all tracked if none specified)
  untrack      NAME...     Remove a package from the local repository
EOF
}

__require_argc() {
  local min max argc=$2

  case $1 in
    *-*)
      IFS=- read -r min max <<<"$1"
      ;;
    *-)
      min=${1%-}
      ;;
    *)
      min=$1 max=$1
      ;;
  esac

  if (( min == max && argc != min )); then
    log_fatal '%s expects %d args, got %d' "${FUNCNAME[1]#action__}" "$min" "$argc"
  elif (( max && argc > max )); then
    log_fatal '%s expects at most %d args, got %d' "${FUNCNAME[1]#action__}" "$max" "$argc"
  elif (( argc < min )); then
    log_fatal '%s expects at least %d args, got %d' "${FUNCNAME[1]#action__}" "$min" "$argc"
  fi
}

version() {
  printf 'asp v0\n'
}

update_all() {
  local r

  for r in "${ARCH_GIT_REPOS[@]}"; do
    log_info "updating remote '%s'" "$r"
    remote_update "$r"
  done
}

update_local_branches() {
  local r=0

  while read -r branchname; do
    git branch -qf "$branchname" "refs/remotes/$branchname" || r=1
  done < <(git branch)

  return "$r"
}

update_remote_branches() {
  local refspecs=() remote pkgname
  declare -A refspec_map

  if (( $# == 0 )); then
    update_all
    return
  fi

  # map packages to remotes
  for pkgname; do
    package_init -n "$pkgname" remote || return 1
    refspec_map["$remote"]+=" packages/$pkgname"
  done

  # update each remote all at once
  for remote in "${!refspec_map[@]}"; do
    read -ra refspecs <<<"${refspec_map["$remote"]}"
    remote_update_refs "$remote" "${refspecs[@]}"
  done
}

update_packages() {
  update_remote_branches "$@" && update_local_branches
}

initialize() {
  local remote

  [[ -d $ASPCACHE ]] || mkdir -p "$ASPCACHE"

  [[ -f .asp ]] && return 0

  git init || return 1

  for remote in "${ARCH_GIT_REPOS[@]}"; do
    git remote add "$remote" git://git.archlinux.org/svntogit/"$remote".git
  done

  touch .asp
}

dump_packages() {
  local remote refspecs dumpfn

  case $1 in
    all)
      dumpfn=remote_get_all_refs
      ;;
    local)
      dumpfn=remote_get_tracked_refs
      ;;
    *)
      die 'internal error: invalid dump type: "%s"' "$1"
      ;;
  esac

  for remote in "${ARCH_GIT_REPOS[@]}"; do
    "$dumpfn" "$remote" refspecs
    if [[ $refspecs ]]; then
      printf '%s\n' "${refspecs[@]/#packages/"$remote"}"
    fi
  done
}

list_local() {
  dump_packages 'local'
}

list_all() {
  dump_packages 'all'
}

shortlog() {
  package_log "$@" "${FUNCNAME[0]}"
}

log() {
  package_log "$@" "${FUNCNAME[0]}"
}

difflog() {
  package_log "$@" "${FUNCNAME[0]}"
}

gc() {
  git gc --prune=all
}

untrack() {
  local pkgname=$1 remote

  package_init -n "$pkgname" remote || return 1

  remote_untrack "$remote" "$pkgname"
  package_untrack "$pkgname" "$remote"
}

disk_usage() {
  local usage
  read usage _ < <(du -sh "$ASPROOT")

  log_info 'Using %s on disk.' "$usage"
}

action__checkout() {
  __require_argc 1- $#
  map package_checkout "$@"
}

action__difflog() {
  __require_argc 1 $#
  difflog "$1"
}

action__disk-usage() {
  __require_argc 0 $#
  disk_usage
}

action__export() {
  __require_argc 1- $#
  map package_export "$@"
}

action__gc() {
  __require_argc 0 $#
  gc
}

action__help() {
  __require_argc 0 $#
  usage
}

action__list-all() {
  __require_argc 0 $#
  list_all
}

action__list-arches() {
  __require_argc 1- $#
  map package_get_arches "$@"
}

action__list-local() {
  __require_argc 0 $#
  list_local
}

action__list-repos() {
  __require_argc 1- $#
  map package_get_repos "$@"
}

action__log() {
  __require_argc 1 $#
  log "$1"
}

action__shortlog() {
  __require_argc 1 $#
  shortlog "$1"
}

action__show() {
  __require_argc 1-2 $#
  package_show_file "$@"
}

action__untrack() {
  __require_argc 1- $#
  map untrack "$@"
}

action__update() {
  update_packages "$@"
}

action__ls-files() {
  __require_argc 1 $#

  package_list_files "$1"
}

dispatch_action() {
  local a candidates=()
  local actions=(
    checkout
    difflog
    disk-usage
    export
    gc
    help
    list-all
    list-arches
    list-local
    list-repos
    ls-files
    log
    shortlog
    show
    untrack
    update
  )

  [[ $1 ]] || log_fatal 'no action specified (use -h for help)'

  for a in "${actions[@]}"; do
    if [[ $a = "$1" ]]; then
      candidates=("$a")
      break
    fi

    [[ $a = "$1"* ]] && candidates+=("$a")
  done

  case ${#candidates[*]} in
    0)
      log_fatal 'unknown action: %s' "$1"
      ;;
    1)
      "action__${candidates[0]}" "${@:2}"
      ;;
    *)
      {
        printf "error: verb '%s' is ambiguous; possibilities:" "$1"
        printf " '%s'" "${candidates[@]}"
        echo
      } >&2
  esac
}

umask 0022
startdir=$PWD
cd "$ASPROOT" || log_fatal "ASPROOT ($ASPROOT) does not exist!"
initialize

while getopts ':a:fhV' flag; do
  case $flag in
    a)
      OPT_ARCH=$OPTARG
      ;;
    f)
      OPT_FORCE=1
      ;;
    h)
      usage
      exit 0
      ;;
    V)
      version
      exit 0
      ;;
    \?)
      log_fatal "invalid option -- '%s'" "$OPTARG"
      ;;
    :)
      log_fatal "option '-%s' requires an argument" "$OPTARG"
      ;;
  esac
done
shift $(( OPTIND - 1 ))

dispatch_action "$@"