Skip to content

Commit

Permalink
chore: multiple patterns for sparse-checkout (#123)
Browse files Browse the repository at this point in the history
e.g.:

DISTRO="ubuntu centos" ./tools/archive-sparse-checkout.sh
ARCH=x86_64 DISTRO="ubuntu centos" ./tools/archive-sparse-checkout.sh
ARCH=arm64 VERSION=20.04 ./tools/archive-sparse-checkout.sh
VERSION=20.04 ./tools/archive-sparse-checkout.sh
  • Loading branch information
geyslan authored Dec 18, 2024
1 parent b155ff3 commit f023990
Showing 1 changed file with 49 additions and 22 deletions.
71 changes: 49 additions & 22 deletions tools/archive-sparse-checkout.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
#!/bin/sh
#!/bin/bash

die() {
echo "${@}"
exit 1
}

if [ -z "$ARCH" ]; then
ARCH="*"
else
case ${ARCH} in
"x86_64")
ARCH="x86_64"
;;
"aarch64"|"arm64")
ARCH="arm64"
;;
*)
die "unsupported architecture: ${ARCH}"
;;
esac
if [ -z "$VERSION" ]; then
VERSION="*"
fi

if [ -z "$DISTRO" ]; then
DISTRO="*"
fi
set -f # disable globbing
DISTROS=${DISTRO:-"*"}
ARCHS=${ARCH:-"*"}

if [ -z "$VERSION" ]; then
VERSION="*"
ARCHS="${ARCHS//aarch64/arm64}"
for arch in $ARCHS; do
case $arch in
x86_64|arm64|'*')
;;
*)
die "invalid ARCH: $arch"
;;
esac
done
set +f # enable globbing

IFS=' ' read -r -a DISTROS_ARRAY <<< "$DISTROS"
IFS=' ' read -r -a ARCHS_ARRAY <<< "$ARCHS"

# count the number of elements
NUM_DISTROS=${#DISTROS_ARRAY[@]}
NUM_ARCHS=${#ARCHS_ARRAY[@]}

if [ "$VERSION" != "*" ] && { [ "$NUM_DISTROS" -gt 1 ] || [ "$NUM_ARCHS" -gt 1 ]; }; then
die "VERSION must be * when DISTRO or ARCH are arrays"
fi

BASE_DIR=$(dirname "$(realpath "$0")")
Expand All @@ -46,11 +53,31 @@ else
git clone --sparse --filter=blob:none "$REPO_URL" "$ARCHIVE_DIR"
fi

SPARSE_CHECKOUT_FILE="$ARCHIVE_DIR/.git/info/sparse-checkout"
true > "$SPARSE_CHECKOUT_FILE" # clear existing file

# initialize sparse-checkout
git -C "$ARCHIVE_DIR" sparse-checkout init --no-cone

# write patterns for the files we want to pull
echo "$DISTRO/$VERSION/$ARCH/*.btf.tar.xz" > "$ARCHIVE_DIR/.git/info/sparse-checkout"
echo "Settings patterns to $SPARSE_CHECKOUT_FILE file..."
if [ "$NUM_DISTROS" -eq 1 ] && [ "$NUM_ARCHS" -eq 1 ]; then
echo "${DISTROS_ARRAY[0]}/$VERSION/${ARCHS_ARRAY[0]}/*.btf.tar.xz" | tee "$SPARSE_CHECKOUT_FILE"
elif [ "$NUM_DISTROS" -gt 1 ] && [ "$NUM_ARCHS" -eq 1 ]; then
for distro in "${DISTROS_ARRAY[@]}"; do
echo "$distro/*/${ARCHS_ARRAY[0]}/*.btf.tar.xz" | tee -a "$SPARSE_CHECKOUT_FILE"
done
elif [ "$NUM_DISTROS" -eq 1 ] && [ "$NUM_ARCHS" -gt 1 ]; then
for arch in "${ARCHS_ARRAY[@]}"; do
echo "${DISTROS_ARRAY[0]}/*/$arch/*.btf.tar.xz" | tee -a "$SPARSE_CHECKOUT_FILE"
done
else
for distro in "${DISTROS_ARRAY[@]}"; do
for arch in "${ARCHS_ARRAY[@]}"; do
echo "$distro/*/$arch/*.btf.tar.xz" | tee -a "$SPARSE_CHECKOUT_FILE"
done
done
fi

git -C "$ARCHIVE_DIR" sparse-checkout reapply || die "failed to reapply sparse-checkout"

echo "Sparse checkout completed successfully in $ARCHIVE_DIR"

0 comments on commit f023990

Please sign in to comment.