Make the get_sources shell script support flat layout

The idea of this commit is to make get_sources.sh support both the
'traditional' git layout used on dist-git for CentOS Linux where the
spec files are stored in a SPECS folder and the patches in a SOURCES
folder with the sha of the tarball being in a '.<pkg_name>.metadata'
file as well as the 'flat' git layout that have adopted Fedora as well
as CentOS-Stream (9+) where the spec files and patches are all stored
at the top level directory of the repository and the sha of the tarball
is present in a 'sources' file.

This commit re-uses code from the fedpkg-minimal project which is
license under the GPL (so the License field of the spec file for
centpkg-minimal may need to be adjusted to MIT and GPL).

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
This commit is contained in:
Pierre-Yves Chibon 2022-02-01 10:59:10 +01:00
parent 510f1aa0bc
commit 518994fd59

View file

@ -9,6 +9,7 @@
# Pat Riehecky <riehecky@fnal.gov>
# Tyler Parsons <tparsons@fnal.gov>
# Tuomo Soini <tis@foobar.fi>
set -eux
#####################################################################
@ -114,10 +115,83 @@ if [[ $? -ne 0 ]]; then
exit 1
fi
if [ ! -d .git ] && ([ ! -d SPECS ] || [[ ! -s sources ]] ); then
echo 'You need to run this from inside a sources git repo' >&2
exit 1
fi
# sort out our branch
if [ -n "$BRANCH" ]
then
branches=("$BRANCH")
else
# generate a list of all branches containing current HEAD
branches=()
while IFS='' read -r line
do
# input from: git branch --contains HEAD
branch="${line:2}"
if [[ "$branch" =~ "detached from" ]]
then
# ignore detached heads
continue
fi
if [ ".${line:0:1}" = ".*" ]
then
# current branch, put it first
branches=("$branch" "${branches[@]}")
else
branches=("${branches[@]}" "$branch")
fi
done <<< "$(git branch -r --contains HEAD | sed 's#origin/##g')"
fi
if [[ -s sources ]]; then
# This section is for the "flat" dist-git layout, where the spec file and
# patches are all present at the top level directory and the sha of the tarball
# present in a 'sources' file.
# This code was re-used from the fedpkg-pkg minimal project which is licensed
# under GPLv3 or any later version.
pkgname=$(basename "$PWD")
# Read first word of first line. For old MD5 format it's the 32 character
# hash. Otherwise let's assume the sources have the BSD format where lines
# start with hash type.
hashtype="$(head -n1 sources | cut -d' ' -f1 | tr '[:upper:]' '[:lower:]')"
# The format is
# SHA512 (filename) = ABCDEF
# We don't care about the equals sign. We also assume all hashes are
# the same type, so we don't need to read it again for each line.
while read -r _ filename _ hash || [[ -n "$filename" && -n "$hash" ]]; do
if [ -z "$filename" ] || [ -z "$hash" ]; then
continue
fi
# Remove parenthesis around tarball name
filename=${filename#(}
tarball=${filename%)}
if [ ! -e "$tarball" ]; then
for br in "${branches[@]}"
do
br=$(echo ${br}| sed -e s'|remotes/origin/||')
url="${SURL}/$pkgname/${br}/$hash"
echo "Retrieving ${url}"
curl -L ${QUIET} -f "${url}" -o "$tarball" && break
done
else
echo "$filename exists. skipping"
fi
done < sources
"${hashtype}sum" -c sources
else
# This section is for the "non-flat" dist-git layout, where the spec file
# is stored in a SPECS folder, the patches in a SOURCES folder and the sha
# of the tarball of the project is present in a '.<pkg_name>.metadata' file.
mkdir -p SOURCES
# should go into a function section at some point
weakHashDetection () {
strHash=${1};
case $((`echo "${strHash}"|wc -m` - 1 )) in
case $((`echo ${strHash}|wc -m` - 1 )) in
128)
hashBin='sha512sum'
;;
@ -152,49 +226,18 @@ meta=$1
pn=${meta%.metadata}
pn=${pn#.}
if [ ! -d .git ] || [ ! -d SPECS ]; then
echo 'You need to run this from inside a sources git repo' >&2
exit 1
fi
mkdir -p SOURCES
# sort out our branch
if [ -n "$BRANCH" ]
then
branches=("$BRANCH")
else
# generate a list of all branches containing current HEAD
branches=()
while IFS='' read -r line
do
# input from: git branch --contains HEAD
branch="${line:2}"
if [[ "$branch" =~ "detached from" ]]
then
# ignore detached heads
continue
fi
if [ ".${line:0:1}" = ".*" ]
then
# current branch, put it first
branches=("$branch" "${branches[@]}")
else
branches=("${branches[@]}" "$branch")
fi
done <<< "$(git branch -r --contains HEAD | grep '^\s\+origin/'| sed 's#origin/##g')"
fi
while read -r fsha fname ; do
if [ ".${fsha}" = ".da39a3ee5e6b4b0d3255bfef95601890afd80709" ]; then
# zero byte file
touch "${fname}"
touch ${fname}
else
if [ ${CHECK} -eq 1 ]; then
hashType=$(weakHashDetection "${fsha}")
hashType=$(weakHashDetection ${fsha})
if [ "${hashType}" == "unknown" ]; then
echo 'Failure: Hash type unknown.' >&2
exit 1;
else
command -v "${hashType}" >/dev/null 2>&1
which ${hashType} >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Failure: You need ${hashType} in PATH." >&2
exit 1;
@ -203,15 +246,15 @@ while read -r fsha fname ; do
fi
if [ -e ${fname} -a ${CHECK} -eq 1 ]; then
# check hash sum and force download if wrong
downsum=$(${hashType} "${fname}" | awk '{print $1}')
downsum=$(${hashType} ${fname} | awk '{print $1}')
if [ "${fsha}" != "${downsum}" ]; then
rm -f "${fname}"
rm -f ${fname}
fi
fi
if [ ! -e "${fname}" ]; then
for br in "${branches[@]}"
do
br=$(echo "${br}"| sed -e s'|remotes/origin/||')
br=$(echo ${br}| sed -e s'|remotes/origin/||')
url="${SURL}/${pn}/${br}/${fsha}"
echo "Retrieving ${url}"
curl -L ${QUIET} -f "${url}" -o "${fname}" && break
@ -220,12 +263,14 @@ while read -r fsha fname ; do
echo "${fname} exists. skipping"
fi
if [ ${CHECK} -eq 1 ]; then
downsum=$(${hashType} "${fname}" | awk '{print $1}')
downsum=$(${hashType} ${fname} | awk '{print $1}')
if [ "${fsha}" != "${downsum}" ]; then
rm -f "${fname}"
rm -f ${fname}
echo "Failure: ${fname} hash does not match hash from the .metadata file" >&2
exit 1;
fi
fi
fi
done < "${meta}"
fi