summaryrefslogtreecommitdiff
path: root/package.inc.sh
blob: 8f5032d11963b725d0c1350e379750091b711b67 (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
package_resolve() {
  local pkgbase

  [[ $pkgname ]] || log_fatal 'BUG: package_resolve called without pkgname var set'

  if package_find_remote "$1" "$2"; then
    return 0
  fi

  if pkgbase=$(archweb_get_pkgbase "$1") && package_find_remote "$pkgbase" "$2"; then
    log_info '%s is part of package %s' "$1" "$pkgbase"
    pkgname=$pkgbase
    return 0
  fi

  log_error 'unknown package: %s' "$pkgname"
  return 1
}

package_init() {
  local do_update=1

  if [[ $1 = -n ]]; then
    do_update=0
    shift
  fi

  pkgname=$1

  package_resolve "$pkgname" "$2" || return

  (( do_update )) || return 0

  if ! [[ ${!2} = packages32 ]]; then
    remote_is_tracking "${!2}" "$pkgname" ||
        remote_update_refs "${!2}" "packages/$pkgname"
  fi

  #fetch packages32/master
  #test if master exists, if not, then fetch(very slow first time)
  if ! (( OPT_UPSTREAM )); then
    git show-ref -q packages32/master || quiet_git fetch packages32 master
  fi
}

package_find_remote() {
  pkgname=$1

  # fastpath, checks local caches only
  for r in "${ARCH_GIT_REPOS[@]}"; do
    if remote_is_tracking "$r" "$pkgname"; then
      printf -v "$2" %s "$r"
      return 0
    fi
  done

  # slowpath, needs to talk to the remote
  for r in "${ARCH_GIT_REPOS[@]}"; do
    if remote_has_package "$r" "$pkgname"; then
      printf -v "$2" %s "$r"
      return 0
    fi
  done

  # arch32-only packages
  if ! (( OPT_UPSTREAM )); then
    printf -v "$2" %s "$(git ls-tree -r --name-only packages32/master \
      | awk -F/ -v pkg="${pkgname}" ' $0 ~ "/"pkg"/PKGBUILD" {print "packages32";exit}')"
    if [[ ${!2} ]]; then
      return 0
    fi
  fi

  return 1
}

package_log() {
  local method=$2 logargs remote
  pkgname=$1

  package_init "$pkgname" remote || return

  case $method in
    shortlog)
      logargs=('--pretty=oneline')
      ;;
    difflog)
      logargs=('-p')
      ;;
    log)
      logargs=()
      ;;
    *)
      log_fatal 'BUG: unknown log method: %s' "$method"
      ;;
  esac

  if ! (( OPT_UPSTREAM )); then
    repo=$(package_get_repos_with_arch "$pkgname" packages32|awk '{print $1}')
    [[ $repo ]] && git log "${logargs[@]}" "packages32/master" -- "$repo/$pkgname" \
      || log_info 'There is no Arch32 patch for %s' "$pkgname"
  else
    git log "${logargs[@]}" "$remote/packages/$pkgname" -- trunk/
  fi
}

package_show_file() {
  local file=${2:-PKGBUILD} remote repo subtree
  pkgname=$1

  if [[ $pkgname = */* ]]; then
    IFS=/ read -r repo pkgname <<<"$pkgname"
  fi

  package_init "$pkgname" remote || return

  if [[ $file != */* ]]; then
    if [[ $repo ]]; then
      subtree=repos/$repo-$OPT_ARCH/
    else
      subtree=trunk/
    fi
  fi

  if ! (( OPT_UPSTREAM )); then
    local trepo
    read -r trepo _ < <(package_get_repos_with_arch "$pkgname" packages32)
    [[ $trepo ]] && git show "remotes/packages32/master:$trepo/$pkgname/$file" 2>/dev/null \
      && return
  fi
  git show "remotes/$remote/packages/$pkgname:$subtree$file"
}

package_list_files() {
  local remote subtree=trunk trepo list_ups list_dwn
  pkgname=$1

  if [[ $pkgname = */* ]]; then
    IFS=/ read -r repo pkgname <<<"$pkgname"
  fi

  package_init "$pkgname" remote || return

  if [[ $repo ]]; then
    subtree=repos/$repo-$OPT_ARCH
  fi

  list_ups=$(git ls-tree -r --name-only "remotes/$remote/packages/$pkgname" "$subtree" \
    2>/dev/null | awk -v "prefix=$subtree/" 'sub(prefix, "")')

  if ! (( OPT_UPSTREAM )); then
    read -r trepo _ < <(package_get_repos_with_arch "$pkgname" packages32)
    if [[ $trepo ]]; then
      list_dwn=$(git ls-tree -r --name-only "remotes/packages32/master" "$trepo/$pkgname" |
        awk -v "prefix=$trepo/$pkgname/" 'sub(prefix, "")')
      [[ $list_ups ]] && printf -v list_ups "%s\n%s" "$list_ups" "$list_dwn" \
        || printf -v list_ups "%s" "$list_dwn"
    fi
  fi
  echo "$list_ups" | sort -u
}

package_patch_arch32() {
  local arch repo subtree
  read -r repo arch < <(package_get_repos_with_arch "$pkgname" "$remote" \
    | awk '!/testing/ && (/x86_64/ || /arch32/ || /any/) {print $1" "$2; exit}')
  subtree=${1:-repos/${repo}-${arch}}
  mkdir -p ${pkgname}/${subtree} && touch ${pkgname}/${subtree}/PKGBUILD
  awk -i inplace '!/^arch=[^#]*any/ {gsub(/^arch=\(/,"arch=(i486 i686 pentium4 ")}; {print}' \
    "$pkgname/${subtree}/PKGBUILD"
  for line in $(git ls-tree -r --name-only remotes/packages32/master ${repo}/${pkgname}); do
    if [[ "${line##*/}" = "PKGBUILD" ]]; then
      git show remotes/packages32/master:${line} >> ${pkgname}/${subtree}/PKGBUILD
    else
      git show remotes/packages32/master:${line} > ${pkgname}/${subtree}/${line##*/}
    fi
  done
}

package_export() {
  local remote repo arch path subtree=trunk
  pkgname=$1

  if [[ $pkgname = */* ]]; then
    IFS=/ read -r repo pkgname <<<"$pkgname"
  fi

  package_init "$pkgname" remote || return

  if [[ $repo ]]; then
    subtree=repos/$repo-$OPT_ARCH
  fi

  if ! [[ $remote = packages32 ]]; then
    if ! git show "remotes/$remote/packages/$pkgname:$subtree/" &>/dev/null; then
      if [[ $repo ]]; then
        log_error "package '%s' not found in repo '%s-%s'" "$pkgname" "$repo" "$OPT_ARCH"
        return 1
      else
        log_error "package '%s' has no trunk directory!" "$pkgname"
        return 1
      fi
    fi

    if (( ! OPT_FORCE )); then
      # shellcheck disable=SC2154
      mkdir "$pkgname" || return
    fi

    log_info 'exporting %s:%s' "$pkgname" "$subtree"
    git archive --format=tar "remotes/$remote/packages/$pkgname" "$subtree/" |
        tar --transform "s,^$subtree,$pkgname," -xf - "$subtree/"
  fi

  if ! (( OPT_UPSTREAM )); then
    package_patch_arch32 .
  fi
}

package_checkout() {
  local remote
  pkgname=$1

  package_init "$pkgname" remote || return

  if ! [[ $remote = packages32 ]]; then
    git show-ref -q "refs/heads/$remote/packages/$pkgname" ||
        git branch -qf --no-track {,}"$remote/packages/$pkgname"

    quiet_git clone \
      --shared \
      --single-branch \
      --branch "$remote/packages/$pkgname" \
      --config "pull.rebase=true" \
      "$ASPROOT" "$pkgname" || return
  fi

  if ! (( OPT_UPSTREAM )); then
    package_patch_arch32
  fi
}

package_get_repos_with_arch() {
  local remote=$2 path arch repo
  pkgname=$1

  if [[ $remote == packages32 ]]; then
    repo="$(git ls-tree -r --name-only packages32/master \
      | awk -F/ -v pkg="${pkgname}" ' $0 ~ "/"pkg"/PKGBUILD" {print $1;exit}')"
    [[ $repo ]] && arch=arch32
    printf '%s %s\n' "$repo" "$arch"
  else
    while read -r path; do
      path=${path##*/}
      repo=${path%-*}
      arch=${path##*-}
      printf '%s %s\n' "$repo" "$arch"
    done < <(git ls-tree --name-only "remotes/$remote/packages/$pkgname" repos/)
  fi

}

package_get_arches() {
  local remote arch
  declare -A arches
  pkgname=$1

  package_init "$pkgname" remote || return

  while read -r _ arch; do
    arches["$arch"]=1
  done < <(package_get_repos_with_arch "$pkgname" "$remote")

  printf '%s\n' "${!arches[@]}"
}

package_get_repos() {
  local remote repo
  declare -A repos
  pkgname=$1

  package_init "$pkgname" remote || return

  while read -r repo _; do
    repos["$repo"]=1
  done < <(package_get_repos_with_arch "$pkgname" "$remote")

  printf '%s\n' "${!repos[@]}"
}

package_untrack() {
  local remote=$2
  pkgname=$1

  if git show-ref -q "refs/heads/$remote/packages/$pkgname"; then
    git branch -D "$remote/packages/$pkgname"
  fi
}