From b155ff3920f8a9b5daf507fc674bbf1f4e55caff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=C3=B3rio=20G=2E?= Date: Wed, 18 Dec 2024 16:07:27 -0300 Subject: [PATCH] chore: add archive-sparse-checkout.sh (#122) It is a script to do sparse checkout of btfhub-archive repository. It can be used like this: ARCH=x86_64 ./tools/archive-sparse-checkout.sh ARCH=arm64 DISTRO=ubuntu VERSION=20.04 ./tools/archive-sparse-checkout.sh --- archive/.gitignore | 1 - custom-archive/.gitignore | 1 - tools/archive-sparse-checkout.sh | 56 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) delete mode 100644 archive/.gitignore delete mode 100644 custom-archive/.gitignore create mode 100755 tools/archive-sparse-checkout.sh diff --git a/archive/.gitignore b/archive/.gitignore deleted file mode 100644 index 72e8ffc0..00000000 --- a/archive/.gitignore +++ /dev/null @@ -1 +0,0 @@ -* diff --git a/custom-archive/.gitignore b/custom-archive/.gitignore deleted file mode 100644 index 72e8ffc0..00000000 --- a/custom-archive/.gitignore +++ /dev/null @@ -1 +0,0 @@ -* diff --git a/tools/archive-sparse-checkout.sh b/tools/archive-sparse-checkout.sh new file mode 100755 index 00000000..0461ce46 --- /dev/null +++ b/tools/archive-sparse-checkout.sh @@ -0,0 +1,56 @@ +#!/bin/sh + +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 +fi + +if [ -z "$DISTRO" ]; then + DISTRO="*" +fi + +if [ -z "$VERSION" ]; then + VERSION="*" +fi + +BASE_DIR=$(dirname "$(realpath "$0")") +ARCHIVE_DIR=$(realpath "$BASE_DIR/../archive") +REPO_URL="https://github.com/aquasecurity/btfhub-archive.git" + +if [ ! -d "$ARCHIVE_DIR" ]; then + echo "Directory does not exist. Creating $ARCHIVE_DIR..." + mkdir -p "$ARCHIVE_DIR" +fi + +# if the directory is already a git repository, skip cloning +if [ -d "$ARCHIVE_DIR/.git" ]; then + echo "Directory is already a Git repository." +else + # clone into the directory + git clone --sparse --filter=blob:none "$REPO_URL" "$ARCHIVE_DIR" +fi + +# 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" +git -C "$ARCHIVE_DIR" sparse-checkout reapply || die "failed to reapply sparse-checkout" + +echo "Sparse checkout completed successfully in $ARCHIVE_DIR"