Blame return_disttag.sh

4d96e5
#!/bin/bash -u
4d96e5
#
4d96e5
# Extracts what appears to be the value of %{dist} from the commit message
4d96e5
#
4d96e5
# Might want to drop this in ~/bin/ and chmod u+x it
4d96e5
4d96e5
#####################################################################
4d96e5
usage() {
4d96e5
    echo ''                                               >&2
4d96e5
    echo "$0 [-hr]"                                       >&2
4d96e5
    echo ''                                               >&2
4d96e5
    echo ' -h: This help message'                         >&2
4d96e5
    echo ' -r: Use the Redhat tag rather than centos tag' >&2
e5597e
    echo ' -q: Suppress warnings'                         >&2
4d96e5
    echo ''                                               >&2
4d96e5
    echo '  Attempt to extract what appears to be the value of %{dist}' >&2
4d96e5
    echo '  from the git.centos.org commit message'       >&2
4d96e5
    exit 1
4d96e5
}
4d96e5
01a821
#####################################################################
01a821
build_with_dist_scl() {
01a821
    SPECFILE=$1
01a821
    DIST=$2
01a821
    SCL=${3:-}
01a821
01a821
    if [[ "x${SCL}" == 'x' ]]; then
4a0580
        result=$(rpm --define "%_topdir `pwd`" --define "dist ${DIST}" -q --specfile "${SPECFILE}" --qf '%{n}-%{v}-%{r}\n' 2>/dev/null | head -n 1)
01a821
    else
4a0580
        result=$(rpm --define "%_topdir `pwd`" --define "dist ${DIST}" --define "scl ${SCL}" -q --specfile "${SPECFILE}" --qf '%{n}-%{v}-%{r}\n' 2>/dev/null | head -n 1)
01a821
    fi
01a821
4a0580
    echo ${result}
01a821
}
4d96e5
4d96e5
#####################################################################
4d96e5
# setup args in the right order for making getopt evaluation
4d96e5
# nice and easy.  You'll need to read the manpages for more info
e5597e
args=$(getopt -o hrq -- "$@")
4d96e5
if [[ $? -ne 0 ]]; then
4d96e5
    usage
4d96e5
fi
4d96e5
eval set -- "$args"
4d96e5
4d96e5
RHELTAG=0
e5597e
QUIET=0
e5597e
for arg in "$@"; do
4d96e5
    case $1 in
4d96e5
        -- )
4d96e5
            # end of getopt args, shift off the -- and get out of the loop
4d96e5
            shift
4d96e5
            break 2
4d96e5
           ;;
4d96e5
         -r )
4d96e5
            # skip any package with 'centos' in the dist area
4d96e5
            RHELTAG=1
4d96e5
           ;;
e5597e
         -q )
e5597e
            # suppress warnings
e5597e
            QUIET=1
e5597e
           ;;
4d96e5
         -h )
4d96e5
            # get help
4d96e5
            usage
4d96e5
           ;;
4d96e5
    esac
4d96e5
done
4d96e5
e5597e
warn () {
e5597e
    [[ ${QUIET} -eq 1 ]] && return
e5597e
    echo 1>&2 "$@"
e5597e
}
4d96e5
e5597e
if [[ ! -d .git ]] || [[ ! -d SPECS ]]; then
7520e4
    echo 'You need to run this from inside a sources git repo' >&2
4d96e5
    exit 1
4d96e5
fi
4d96e5
a68f52
rpm -q scl-utils-build >/dev/null 2>&1
a68f52
if [[ $? -ne 0 ]]; then
a68f52
    echo 'Without scl-utils-build some SCL style sources be parsed' >&2
a68f52
    exit 1
a68f52
fi
a68f52
e5597e
# check metadata file and extract package name
e5597e
packagename=""
e5597e
shopt -s nullglob
01a821
set -- .*.metadata
01a821
if (( $# == 0 ))
e5597e
then
7520e4
    echo 'Missing metadata. Please run from inside a sources git repo' >&2
4d96e5
    exit 1
01a821
elif (( $# > 1 ))
01a821
then
01a821
    warn "Warning: multiple metadata files found. Using $1"
4d96e5
fi
01a821
meta=$1
01a821
pn=${meta%.metadata}
01a821
pn=${pn#.}
4d96e5
e5597e
filter () {
e5597e
    # filter used for log messages
e5597e
    if [[ ${RHELTAG} -eq 1 ]]
e5597e
    then
e5597e
        grep -v centos | grep import
e5597e
    else
e5597e
        grep import
e5597e
    fi
e5597e
}
e5597e
e5597e
# extract nvr from commit message of last import
e5597e
msg=$(git log --pretty=format:"%s" | filter | head -n 1)
e5597e
set -- $msg
e5597e
pkg="$2"
e5597e
a6a9e5
# strip .src.rpm if present
01a821
git_nvr="${pkg%.src.rpm}"
01a821
01a821
scl=''
01a821
01a821
SPEC=$(cd SPECS; ls *.spec)
4d96e5
a6a9e5
#now get nvr from spec with placeholder dist
a6a9e5
mydist="XXXjsdf9ur7qlkasdh4gygXXX"
3a0ccf
test_nvr=$(build_with_dist_scl "SPECS/${SPEC}" ${mydist})
01a821
3a0ccf
test_nodist=$(echo ${test_nvr} | sed -e 's/-[a-zA-Z0-9\.]*$//')
3a0ccf
git_nodist=$(echo ${git_nvr} | sed -e 's/-[a-zA-Z0-9\._]*$//')
01a821
3a0ccf
if [[ "${git_nodist}" != "${test_nodist}" ]]; then
3a0ccf
    warn "Warning: ${git_nvr} != ${test_nvr}"
01a821
    warn "Warning: Trying as a Software Collection"
3a0ccf
    scl=$(echo ${git_nodist} |sed -e "s/-${test_nodist}//")
3a0ccf
    test_nvr=$(build_with_dist_scl "SPECS/${SPEC}" ${mydist} ${scl})
3a0ccf
    test_nodist=$(echo ${test_nvr} | sed -e 's/-[a-zA-Z0-9\.]*$//')
01a821
fi
01a821
3a0ccf
if [[ "${git_nodist}" != "${test_nodist}" ]]; then
3a0ccf
    git_name=$(echo ${git_nvr} | cut -d '-' -f 1)
3a0ccf
    test_name=$(echo ${test_nvr} | cut -d '-' -f 1)
01a821
    warn "Warning: ${git_name} != ${test_name}"
01a821
    echo "Warning: Couldn't match srpm name" >&2
01a821
    exit 1
01a821
fi
a6a9e5
a6a9e5
#use our placeholder dist to split the nvr
3a0ccf
head=${test_nvr%$mydist*}
a6a9e5
3a0ccf
if [ ".$head" = ".$test_nvr" ]
a6a9e5
then
a6a9e5
    #no dist tag
a6a9e5
    echo ""
a6a9e5
    exit
a6a9e5
fi
a6a9e5
3a0ccf
tail=${test_nvr#*$mydist}
a6a9e5
01a821
frag=${git_nvr#$head}
a6a9e5
dist=${frag%$tail}
a6a9e5
e5597e
# sanity check
01a821
if [[ "x${scl}" == 'x' ]]; then
01a821
    verifynvr=$(build_with_dist_scl "SPECS/${SPEC}" ${dist})
01a821
else
01a821
    verifynvr=$(build_with_dist_scl "SPECS/${SPEC}" ${dist} ${scl})
01a821
fi
01a821
01a821
if [ ".$verifynvr" != ".$git_nvr" ]
e5597e
then
01a821
    warn "Warning: $verifynvr != $git_nvr"
e5597e
    warn "Warning: check failed. The %{dist} value may be incorrect"
e5597e
fi
e5597e
a6a9e5
echo "$dist"