pgreco / centos-git-common

Forked from centos-git-common 5 years ago
Clone
Blob Blame History Raw
#!/bin/bash -u
#
# Extracts what appears to be the value of %{dist} from the commit message
#
# Might want to drop this in ~/bin/ and chmod u+x it

#####################################################################
usage() {
    echo ''                                               >&2
    echo "$0 [-hr]"                                       >&2
    echo ''                                               >&2
    echo ' -h: This help message'                         >&2
    echo ' -r: Use the Redhat tag rather than centos tag' >&2
    echo ' -q: Suppress warnings'                         >&2
    echo ''                                               >&2
    echo '  Attempt to extract what appears to be the value of %{dist}' >&2
    echo '  from the git.centos.org commit message'       >&2
    exit 1
}


#####################################################################
# setup args in the right order for making getopt evaluation
# nice and easy.  You'll need to read the manpages for more info
args=$(getopt -o hrq -- "$@")
if [[ $? -ne 0 ]]; then
    usage
fi
eval set -- "$args"

RHELTAG=0
QUIET=0
for arg in "$@"; do
    case $1 in
        -- )
            # end of getopt args, shift off the -- and get out of the loop
            shift
            break 2
           ;;
         -r )
            # skip any package with 'centos' in the dist area
            RHELTAG=1
           ;;
         -q )
            # suppress warnings
            QUIET=1
           ;;
         -h )
            # get help
            usage
           ;;
    esac
done

warn () {
    [[ ${QUIET} -eq 1 ]] && return
    echo 1>&2 "$@"
}

if [[ ! -d .git ]] || [[ ! -d SPECS ]]; then
    echo 'You need to run this from inside a sources git repo'
    exit 1
fi

# check metadata file and extract package name
packagename=""
shopt -s nullglob
for fn in .*.metadata
do
    pn=${fn%.metadata}
    pn=${pn#.}
    if [ -e "SPECS/$pn.spec" ]
    then
        packagename="$pn"
        break
    fi
done
if [ -z "$packagename" ]
then
    echo 'Missing metadata or spec. Please run from inside a sources git repo'
    exit 1
fi

filter () {
    # filter used for log messages
    if [[ ${RHELTAG} -eq 1 ]]
    then
        grep -v centos | grep import
    else
        grep import
    fi
}

# extract nvr from commit message of last import
msg=$(git log --pretty=format:"%s" | filter | head -n 1)
set -- $msg
pkg="$2"

# strip .src.rpm if present
nvr1="${pkg%.src.rpm}"

#now get nvr from spec with placeholder dist
mydist="XXXjsdf9ur7qlkasdh4gygXXX"
nvr2=$(rpm --define "dist $mydist" -q --specfile "SPECS/$packagename.spec" --qf '%{n}-%{v}-%{r}\n' 2>/dev/null | head -n 1)

#use our placeholder dist to split the nvr
head=${nvr2%$mydist*}

if [ ".$head" = ".$nvr2" ]
then
    #no dist tag
    echo ""
    exit
fi

tail=${nvr2#*$mydist}

frag=${nvr1#$head}
dist=${frag%$tail}

# sanity check
nvr3=$(rpm --define "dist $dist" -q --specfile "SPECS/$packagename.spec" --qf '%{n}-%{v}-%{r}\n' 2>/dev/null | head -n 1)
if [ ".$nvr3" != ".$nvr1" ]
then
    warn "Warning: $nvr3 != $nvr1"
    warn "Warning: check failed. The %{dist} value may be incorrect"
fi

echo "$dist"