Skip to content

Commit 2512644

Browse files
committed
Add support for "Builder: oci-import" in diff-pr
Also, update other scripts to use `bashbrew fetch` and `gitCache` effectively to remove unnecessary reimplementations of `BASHBREW_CACHE` default value calculation.
1 parent ea8f235 commit 2512644

File tree

3 files changed

+91
-40
lines changed

3 files changed

+91
-40
lines changed

diff-pr.sh

Lines changed: 86 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ if [ "$#" -eq 0 ]; then
9191
set -- $images
9292
fi
9393

94-
export BASHBREW_CACHE="${BASHBREW_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/bashbrew}"
9594
export BASHBREW_LIBRARY="$PWD/oi/library"
9695

9796
: "${BASHBREW_ARCH:=amd64}" # TODO something smarter with arches
@@ -103,9 +102,8 @@ template='
103102
{{- "\n" -}}
104103
{{- range $.Entries -}}
105104
{{- $arch := .HasArchitecture arch | ternary arch (.Architectures | first) -}}
106-
{{- $froms := $.ArchDockerFroms $arch . -}}
107105
{{- $outDir := join "_" $.RepoName (.Tags | last) -}}
108-
git -C "$BASHBREW_CACHE/git" archive --format=tar
106+
git -C "{{ gitCache }}" archive --format=tar
109107
{{- " " -}}
110108
{{- "--prefix=" -}}
111109
{{- $outDir -}}
@@ -116,12 +114,28 @@ template='
116114
{{- $dir := .ArchDirectory $arch -}}
117115
{{- (eq $dir ".") | ternary "" $dir -}}
118116
{{- "\n" -}}
119-
mkdir -p "$tempDir/{{- $outDir -}}" && echo "{{- .ArchFile $arch -}}" > "$tempDir/{{- $outDir -}}/.bashbrew-dockerfile-name"
117+
mkdir -p "$tempDir/{{- $outDir -}}" && echo "{{- .ArchBuilder $arch -}}" > "$tempDir/{{- $outDir -}}/.bashbrew-builder" && echo "{{- .ArchFile $arch -}}" > "$tempDir/{{- $outDir -}}/.bashbrew-file"
120118
{{- "\n" -}}
121119
{{- end -}}
122120
tar -cC "$tempDir" . && rm -rf "$tempDir"
123121
'
124122

123+
_tar-t() {
124+
tar -t "$@" \
125+
| grep -vE "$uninterestingTarballGrep" \
126+
| sed -e 's!^[.]/!!' \
127+
-r \
128+
-e 's!([/.-]|^)((lib)?(c?python|py)-?)[0-9]+([.][0-9]+)?([/.-]|$)!\1\2XXX\6!g' \
129+
| sort
130+
}
131+
132+
_jq() {
133+
if [ "$#" -eq 0 ]; then
134+
set -- '.'
135+
fi
136+
jq --tab -S "$@"
137+
}
138+
125139
copy-tar() {
126140
local src="$1"; shift
127141
local dst="$1"; shift
@@ -132,19 +146,73 @@ copy-tar() {
132146
return
133147
fi
134148

135-
local d dockerfiles=()
136-
for d in "$src"/*/.bashbrew-dockerfile-name; do
149+
local d indexes=() dockerfiles=()
150+
for d in "$src"/*/.bashbrew-file; do
137151
[ -f "$d" ] || continue
138152
local bf; bf="$(< "$d")"
139153
local dDir; dDir="$(dirname "$d")"
140-
dockerfiles+=( "$dDir/$bf" )
141-
if [ "$bf" = 'Dockerfile' ]; then
142-
# if "Dockerfile.builder" exists, let's check that too (busybox, hello-world)
143-
if [ -f "$dDir/$bf.builder" ]; then
144-
dockerfiles+=( "$dDir/$bf.builder" )
154+
local builder; builder="$(< "$dDir/.bashbrew-builder")"
155+
if [ "$builder" = 'oci-import' ]; then
156+
indexes+=( "$dDir/$bf" )
157+
else
158+
dockerfiles+=( "$dDir/$bf" )
159+
if [ "$bf" = 'Dockerfile' ]; then
160+
# if "Dockerfile.builder" exists, let's check that too (busybox, hello-world)
161+
if [ -f "$dDir/$bf.builder" ]; then
162+
dockerfiles+=( "$dDir/$bf.builder" )
163+
fi
145164
fi
146165
fi
147-
rm "$d" # remove the ".bashbrew-dockerfile-name" file we created
166+
rm "$d" "$dDir/.bashbrew-builder" # remove the ".bashbrew-*" files we created
167+
done
168+
169+
# now that we're done with our globbing needs, let's disable globbing so it doesn't give us wrong answers
170+
local -
171+
set -o noglob
172+
173+
for i in "${indexes[@]}"; do
174+
local iName; iName="$(basename "$i")"
175+
local iDir; iDir="$(dirname "$i")"
176+
local iDirName; iDirName="$(basename "$iDir")"
177+
local iDst="$dst/$iDirName"
178+
179+
mkdir -p "$iDst"
180+
181+
_jq . "$i" > "$iDst/$iName"
182+
183+
local digest
184+
digest="$(jq -r --arg name "$iName" '
185+
if $name == "index.json" then
186+
.manifests[0].digest
187+
else
188+
.digest
189+
end
190+
' "$i")"
191+
192+
local blob="blobs/${digest//://}"
193+
local blobDir; blobDir="$(dirname "$blob")"
194+
local manifest="$iDir/$blob"
195+
mkdir -p "$iDst/$blobDir"
196+
_jq . "$manifest" > "$iDst/$blob"
197+
198+
local configDigest; configDigest="$(jq -r '.config.digest' "$manifest")"
199+
local blob="blobs/${configDigest//://}"
200+
local blobDir; blobDir="$(dirname "$blob")"
201+
local config="$iDir/$blob"
202+
mkdir -p "$iDst/$blobDir"
203+
_jq . "$config" > "$iDst/$blob"
204+
205+
local layers
206+
layers="$(jq -r '[ .layers[].digest | @sh ] | join(" ")' "$manifest")"
207+
eval "layers=( $layers )"
208+
local layerDigest
209+
for layerDigest in "${layers[@]}"; do
210+
local blob="blobs/${layerDigest//://}"
211+
local blobDir; blobDir="$(dirname "$blob")"
212+
local layer="$iDir/$blob"
213+
mkdir -p "$iDst/$blobDir"
214+
_tar-t -f "$layer" > "$iDst/$blob 'tar -t'"
215+
done
148216
done
149217

150218
for d in "${dockerfiles[@]}"; do
@@ -238,13 +306,7 @@ copy-tar() {
238306
case "$g" in
239307
*.tar.* | *.tgz)
240308
if [ -s "$dstG" ]; then
241-
tar -tf "$dstG" \
242-
| grep -vE "$uninterestingTarballGrep" \
243-
| sed -e 's!^[.]/!!' \
244-
-r \
245-
-e 's!([/.-]|^)((lib)?(c?python|py)-?)[0-9]+([.][0-9]+)?([/.-]|$)!\1\2XXX\6!g' \
246-
| sort \
247-
> "$dstG 'tar -t'"
309+
_tar-t -f "$dstG" > "$dstG 'tar -t'"
248310
fi
249311
;;
250312
esac
@@ -277,14 +339,15 @@ _metadata-files() {
277339
| xargs -r bashbrew cat --format "$templateLastTags" 2>>temp/_bashbrew.err \
278340
> temp/_bashbrew-list-build-order || :
279341

342+
bashbrew fetch --arch-filter "$@"
280343
script="$(bashbrew cat --format "$template" "$@")"
281344
mkdir tar
282345
( eval "$script" | tar -xiC tar )
283346
copy-tar tar temp
284347
rm -rf tar
285348
fi
286349

287-
if [ -n "$externalPins" ] && command -v crane &> /dev/null && command -v jq &> /dev/null; then
350+
if [ -n "$externalPins" ] && command -v crane &> /dev/null; then
288351
local file
289352
for file in $externalPins; do
290353
[ -e "oi/$file" ] || continue
@@ -293,15 +356,15 @@ _metadata-files() {
293356
digest="$(< "oi/$file")"
294357
dir="temp/$file"
295358
mkdir -p "$dir"
296-
bashbrew remote arches --json "$pin@$digest" | jq -S . > "$dir/bashbrew.json"
359+
bashbrew remote arches --json "$pin@$digest" | _jq > "$dir/bashbrew.json"
297360
local manifests manifest
298361
manifests="$(jq -r '[ .arches[][].digest | @sh ] | join(" ")' "$dir/bashbrew.json")"
299362
eval "manifests=( $manifests )"
300363
for manifest in "${manifests[@]}"; do
301-
crane manifest "$pin@$manifest" | jq -S . > "$dir/manifest-${manifest//:/_}.json"
364+
crane manifest "$pin@$manifest" | _jq > "$dir/manifest-${manifest//:/_}.json"
302365
local config
303366
config="$(jq -r '.config.digest' "$dir/manifest-${manifest//:/_}.json")"
304-
crane blob "$pin@$config" | jq -S . > "$dir/manifest-${manifest//:/_}-config.json"
367+
crane blob "$pin@$config" | _jq > "$dir/manifest-${manifest//:/_}-config.json"
305368
done
306369
done
307370
fi

naughty-commits.sh

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ set -Eeuo pipefail
33

44
fileSizeThresholdMB='2'
55

6-
: "${BASHBREW_CACHE:=$HOME/.cache/bashbrew}"
7-
export BASHBREW_CACHE BASHBREW_ARCH=
8-
9-
if [ ! -d "$BASHBREW_CACHE/git" ]; then
10-
# initialize the "bashbrew cache"
11-
bashbrew --arch amd64 from --uniq --apply-constraints hello-world:linux > /dev/null
12-
fi
6+
export BASHBREW_ARCH=
137

8+
gitCache="$(bashbrew cat --format '{{ gitCache }}' <(echo 'Maintainers: empty hack (@example)'))"
149
_git() {
15-
git -C "$BASHBREW_CACHE/git" "$@"
10+
git -C "$gitCache" "$@"
1611
}
1712

1813
if [ "$#" -eq 0 ]; then
@@ -21,12 +16,11 @@ fi
2116

2217
imgs="$(bashbrew list --repos "$@" | sort -u)"
2318
for img in $imgs; do
19+
bashbrew fetch --all "$img" # force `git fetch`
2420
commits="$(
2521
bashbrew cat --format '
2622
{{- range $e := .Entries -}}
2723
{{- range $a := .Architectures -}}
28-
{{- /* force `git fetch` */ -}}
29-
{{- $froms := $.ArchDockerFroms $a $e -}}
3024
{
3125
{{- json "GitRepo" }}:{{ json ($e.ArchGitRepo $a) -}},
3226
{{- json "GitFetch" }}:{{ json ($e.ArchGitFetch $a) -}},

naughty-constraints.sh

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
#!/usr/bin/env bash
22
set -Eeuo pipefail
33

4-
: "${BASHBREW_CACHE:=$HOME/.cache/bashbrew}"
5-
export BASHBREW_CACHE BASHBREW_ARCH=
6-
7-
if [ ! -d "$BASHBREW_CACHE/git" ]; then
8-
# initialize the "bashbrew cache"
9-
bashbrew --arch amd64 from --uniq --apply-constraints hello-world:linux > /dev/null
10-
fi
4+
export BASHBREW_ARCH=
115

126
if [ "$#" -eq 0 ]; then
137
set -- '--all'

0 commit comments

Comments
 (0)