From 76c71a9120a3f1f66b2fd38e21a84b3c9283133a Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Thu, 22 Sep 2022 22:29:28 -0600 Subject: [PATCH] bundle.sh: allow a PR # to be specified Allow pull requests (and merge requests) to be specified by using a branch name like "pr/111" or "mr/222". This allows CI to use this script as well, instead of multiple variations of the same thing. Additonally allow the destination directory to be overridden with the DESTDIR environment variable. --- scripts/bundle.sh | 81 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 13 deletions(-) diff --git a/scripts/bundle.sh b/scripts/bundle.sh index 46c0c0500c..ac4c174588 100755 --- a/scripts/bundle.sh +++ b/scripts/bundle.sh @@ -4,10 +4,63 @@ # # To use, run from the top Suricata source directory: # -# ./scripts/bundle.sh +# ./scripts/bundle.sh [suricata-update|libhtp] +# +# If no arguments are provided, both suricata-update and libhtp will +# be bundled. +# +# Environment variables: +# +# SU_REPO: Overrides the Suricata-Update git repo +# SU_BRANCH: Override the Suricata-Update branch to a branch, tag or +# {pull,merge}-request. +# +# LIBHTP_REPO: Overrides the libhtp git repo +# LIBHTP_BRANCH: Override the libhtp branch to a branch, tag or +# {pull,merge}-request. +# +# DESTDIR: Checkout to another directory instead of the current +# directory. +# +# To specify a pull or merge request in a branch name in the format of +# pr/NNN or mr/NNN. + +set -e + +DESTDIR=${DESTDIR:-.} what="$1" +# Transforms a branch name in the form of "pr/" or +# "mr/" into a proper ref for GitHub or Gitlab. +transform_branch() { + pr=$(echo "${1}" | sed -n 's/^pr\/\([[:digit:]]\+\)$/\1/p') + if [ "${pr}" ]; then + echo "refs/pull/${pr}/head" + return + fi + + mr=$(echo "${1}" | sed -n 's/^mr\/\([[:digit:]]\+\)$/\1/p') + if [ "${mr}" ]; then + echo "refs/merge-requests/${mr}/head" + return + fi + + echo "${1}" +} + +fetch() { + repo="$1" + dest="$2" + branch="$3" + + git clone --depth 1 "${repo}" "${dest}" + pushd "${dest}" + git fetch origin "${branch}" + git -c advice.detachedHead=false checkout FETCH_HEAD + popd +} + while IFS= read -r requirement; do set -- $requirement @@ -20,20 +73,22 @@ while IFS= read -r requirement; do fi case "$1" in suricata-update) - repo=${SU_REPO:-$2} - branch=${SU_BRANCH:-$3} - echo "===> Bundling ${repo} -b ${branch}" - rm -rf suricata-update.tmp - git clone "${repo}" -b "${branch}" suricata-update.tmp - cp -a suricata-update.tmp/* suricata-update/ - rm -rf suricata-update.tmp + SU_REPO=${SU_REPO:-$2} + SU_BRANCH=$(transform_branch ${SU_BRANCH:-$3}) + echo "===> Bundling ${SU_REPO} (${SU_BRANCH})" + rm -rf ${DESTDIR}/suricata-update.tmp + fetch "${SU_REPO}" "${DESTDIR}/suricata-update.tmp" "${SU_BRANCH}" + rm -rf ${DESTDIR}/suricata-update.tmp/.git + cp -a ${DESTDIR}/suricata-update.tmp/. ${DESTDIR}/suricata-update + rm -rf ${DESTDIR}/suricata-update.tmp ;; libhtp) - repo=${LIBHTP_REPO:-$2} - branch=${LIBHTP_BRANCH:-$3} - echo "===> Bundling ${repo} -b ${branch}" - rm -rf libhtp - git clone "${repo}" -b "${branch}" libhtp + LIBHTP_REPO=${LIBHTP_REPO:-$2} + LIBHTP_BRANCH=$(transform_branch ${LIBHTP_BRANCH:-$3}) + echo "===> Bundling ${LIBHTP_REPO} (${LIBHTP_BRANCH})" + rm -rf ${DESTDIR}/libhtp + fetch "${LIBHTP_REPO}" "${DESTDIR}/libhtp" "${LIBHTP_BRANCH}" + rm -rf libhtp/.git ;; \#*) # Ignore comment.