summaryrefslogtreecommitdiff
path: root/package.inc.sh
blob: 63b968a6e6efb8a525f855352c9cd6f487c70cf1 (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
package_init() {
  local pkgname=$1
  local do_update=1

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

  package_find_remote "$pkgname" "$2" || return 1

  (( do_update )) || return 0

  if ! remote_is_tracking "${!2}" "$pkgname"; then
    package_update "$pkgname" "${!2}" || return 1
  fi
}

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

  git fetch "$remote" "packages/$pkgname"
}

package_find_remote() {
  local pkgname=$1 out=$2

  # fastpath, checks local caches only
  for r in "${ARCH_GIT_REPOS[@]}"; do
    if remote_is_tracking "$r" "$pkgname"; then
      printf -v "$out" %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 "$out" %s "$r"
      return 0
    fi
  done

  log_error 'unknown package: %s' "$pkgname"

  return 1
}

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

  package_init "$pkgname" remote || return

  "_package_$method" "$pkgname" "$remote"
}

package_export() {
  local pkgname=$1 remote repo arch
  local mode objtype objid path

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

  package_init "$pkgname" remote || return 1

  # support $repo/$pkgname syntax
  if [[ $repo ]]; then
    subtree=repos/$repo-$OPT_ARCH
  else
    subtree=trunk
  fi

  if [[ -z $(git ls-tree "$remote/packages/$pkgname" "$subtree/") ]]; 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
    mkdir "$startdir/$pkgname" || return 1
  fi

  log_info 'exporting %s:%s' "$pkgname" "$subtree"
  git archive --format=tar "$remote/packages/$pkgname" "$subtree/" |
      bsdtar -C "$startdir" -s ",^$subtree/,$pkgname/," -xf - "$subtree/"
}

package_checkout() {
  local pkgname=$1 remote

  package_init "$pkgname" remote || return 1

  # create a local tracking branch to clone from. ignore errors because
  # it might already exist.
  git branch "$remote/packages/$pkgname" "$remote/packages/$pkgname" 2>/dev/null

  git clone "$ASPROOT" --single-branch --branch "$remote/packages/$pkgname" \
    "$startdir/$pkgname"
}

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

  while read _ objtype _ path; do
    [[ $objtype = tree ]] || continue
    IFS=- read repo arch <<<"${path#repos/}"
    printf '%s %s\n' "$repo" "$arch"
  done < <(git ls-tree "$remote/packages/$pkgname" repos/)
}

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

  package_init "$pkgname" remote || return 1

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

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

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

  package_init "$pkgname" remote || return 1

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

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

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

  git log --pretty=oneline "$remote/packages/$pkgname"
}

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

  git log -p "$remote/packages/$pkgname"
}

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

  git log "$remote/packages/$pkgname"
}