summaryrefslogtreecommitdiff
path: root/asp.in
blob: 1c4902eb5eb75c50ab5f9a2c9cbf826c7ca7b965 (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
#!/bin/bash

ARCH_GIT_REPOS=(packages community)

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

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

log_meta() {
  printf "$1 $2\n" "${@:3}"
}

log_error() {
  log_meta 'error:' "$@" >&2
}

log_fatal() {
  log_error "$@"
  exit 1
}

log_warning() {
  log_meta 'warning:' "$@" >&2
}

log_info() {
  log_meta '==>' "$@"
}

map() {
  local map_r=0
  for _ in "${@:2}"; do
    "$1" "$_" || (( $# > 255 ? map_r=1 : ++r ))
  done
  return $map_r
}

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:
  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
  log          NAME        Show revision history
  shortlog     NAME        Show revision history in short form
  update       [NAME...]   Update packages (update all tracked if none specified)
  untrack      NAME...     Remove a package from the local repository
EOF
}

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

update() {
  local r

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

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

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

  for remote in "${!refspec_map[@]}"; do
    read -ra refspecs <<<"${refspec_map["$remote"]}"
    remote_update_refs "$remote" "${refspecs[@]}"
  done
}

initialize() {
  local remote

  if [[ -d .git && OPT_FORCE -eq 0 ]]; then
    log_fatal 'refusing to overwrite existing repo in %s' "$ASPROOT"
  fi

  git init --bare || return 1

  for remote in "${ARCH_GIT_REPOS[@]}"; do
    rm -rf "$remote"
    git remote add "$remote" git://projects.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
      ;;
  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"
}

log() {
  package_log "$@" "$FUNCNAME"
}

difflog() {
  package_log "$@" "$FUNCNAME"
}

gc() {
  git prune
  git gc
}

untrack() {
  local pkgname=$1 remote

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

  remote_untrack "$remote" "$pkgname"
}

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

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

umask 0022
startdir=$PWD
cd "$ASPROOT" || log_fatal "ASPROOT ($ASPROOT) does not exist!"
[[ -f .asp ]] || 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 ))

action=$1
shift

case $action in
  update)
    if (( $# == 0 )); then
      update
    else
      update_packages "$@"
    fi
    ;;
  list-repos)
    map package_get_repos "$@"
    ;;
  list-arches)
    map package_get_arches "$@"
    ;;
  list-all)
    list_all
    ;;
  list-local)
    list_local
    ;;
  export)
    map package_export "$@"
    ;;
  shortlog)
    shortlog "$1"
    ;;
  log)
    log "$1"
    ;;
  difflog)
    difflog "$1"
    ;;
  disk-usage)
    disk_usage
    ;;
  gc)
    gc
    ;;
  untrack)
    map untrack "$@"
    ;;
  help)
    usage
    exit 0
    ;;
  *)
    log_fatal 'unknown action: %s' "$action"
    ;;
esac