Petr Mládek 809939
#!/bin/bash
Petr Mládek 809939
Petr Mládek 809939
usage()
Petr Mládek 809939
{
Petr Mládek 809939
    echo "Check whether a given list of commit is available in"
Petr Mládek 809939
    echo "a given list of branches."
Petr Mládek 809939
    echo
Michal Koutný c34e0e
    echo "Usage: ${0##*/} [branches.conf] term..."
Petr Mládek 809939
    echo
Petr Mládek 809939
    echo "Parametes:"
Michal Koutný e7c888
    echo "	branches.conf: file with the list of branches to be checked"
Michal Koutný c34e0e
    echo "	term: hash of the commit|CVE|bsc to be found"
Petr Mládek 809939
}
Petr Mládek 809939
Michal Koutný e7c888
fetch_branches()
Michal Koutný e7c888
{
Jiri Slaby 1dae15
    local CACHED_BRANCHES="/tmp/$USER-branches.conf"
Michal Koutný e7c888
    local URL="https://kerncvs.suse.de/branches.conf"
Michal Koutný e7c888
    local EXPIRE=7
Michal Koutný e7c888
    branches=$CACHED_BRANCHES
Michal Koutný 311b20
    if [[ $(find "$CACHED_BRANCHES" -mtime -$EXPIRE -print 2>/dev/null) \
Michal Koutný 311b20
            && -s "$CACHED_BRANCHES" ]]; then
Michal Koutný e7c888
        echo "Using cached $CACHED_BRANCHES" >&2
Michal Koutný e7c888
        return
Michal Koutný e7c888
    fi
Michal Koutný 311b20
    curl "$URL" -o "$CACHED_BRANCHES"
Michal Koutný e7c888
}
Michal Koutný e7c888
Michal Koutný e7c888
if [ $# -lt 1 ] ; then
Petr Mládek 809939
    usage
Petr Mládek 809939
    exit 1
Petr Mládek 809939
fi
Petr Mládek 809939
Petr Mládek 809939
branches=$1
Petr Mládek 809939
if [ ! -f "$branches" ] ; then
Michal Koutný e7c888
    echo "Branches file not specified, trying to fetch it..." >&2
Michal Koutný e7c888
    if ! fetch_branches ; then
Michal Koutný e7c888
        "Error: Can't find the file with the list of branches: $branches nor fetch it"
Michal Koutný e7c888
        exit 1
Michal Koutný e7c888
    fi
Michal Koutný e7c888
else
Michal Koutný e7c888
    shift;
Petr Mládek 809939
fi
Petr Mládek 809939
Michal Koutný c34e0e
KBC_CHECK_TERMS="$*"
Michal Koutný c34e0e
Michal Koutný c34e0e
term2regex()
Michal Koutný c34e0e
{
Jiri Slaby 9ca71f
    shopt -q nocasematch
Michal Koutný c34e0e
    local t=$1
Michal Koutný c34e0e
    case $t in
Michal Koutný c34e0e
        # CVEs first
Michal Koutný c34e0e
        2[0-9][0-9][0-9]-*)
Michal Koutný c34e0e
            t=cve-$t
Michal Koutný c34e0e
            ;&
Michal Koutný c34e0e
        cve-*)
Michal Koutný c34e0e
            echo "^References:.*$t"
Michal Koutný c34e0e
            ;;
Michal Koutný c34e0e
        # looks like a hash, look for commits
Michal Koutný c34e0e
        [a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]*)
Michal Koutný c34e0e
            echo "^Git-commit:.*$t"
Michal Koutný c34e0e
            ;;
Michal Koutný c34e0e
        # treat rest as a generic reference
Michal Koutný c34e0e
        *)
Michal Koutný c34e0e
            echo "^References:.*$t"
Michal Koutný c34e0e
            ;;
Michal Koutný c34e0e
    esac
Michal Koutný c34e0e
}
Michal Koutný e7c888
Petr Mládek 809939
check_branch()
Petr Mládek 809939
{
Petr Mládek 809939
    verbose=0
Jiri Slaby acae7f
    if [ "$1" = "-v" ] ; then
Petr Mládek 809939
        verbose=1
Petr Mládek 809939
        shift
Petr Mládek 809939
    fi
Petr Mládek 809939
Petr Mládek 809939
    branch="$1"
Petr Mládek 809939
    found=""
Petr Mládek 809939
    missing=""
Petr Mládek 809939
Michal Koutný c34e0e
    for term in $KBC_CHECK_TERMS ; do
Jiri Slaby 2ea817
        git grep -qi "$(term2regex $term)" "remotes/origin/$branch" -- 'patches.*' 2>/dev/null
Petr Mládek 809939
        if [ $? -eq 0 ] ; then
Michal Koutný c34e0e
            found="$found $term"
Petr Mládek 809939
        else
Michal Koutný c34e0e
            missing="$missing $term"
Petr Mládek 809939
        fi
Petr Mládek 809939
    done
Petr Mládek 809939
Petr Mládek 809939
    # found
Petr Mládek 809939
    if [ -z "$missing" ] ; then
Petr Mládek 809939
        return 0
Petr Mládek 809939
    fi
Petr Mládek 809939
Petr Mládek 809939
    # missing
Petr Mládek 809939
    if [ -z "$found" ] ; then
Petr Mládek 809939
        return 2
Petr Mládek 809939
    fi
Petr Mládek 809939
Petr Mládek 809939
    # partly
Petr Mládek 809939
    if [ $verbose -ne 0 ] ; then
Petr Mládek 809939
        echo "        missing hash:"
Petr Mládek 809939
        for hash in $missing ; do
Michal Koutný c34e0e
            echo "                $term"
Petr Mládek 809939
        done
Petr Mládek 809939
        echo
Petr Mládek 809939
    fi
Petr Mládek 809939
    return 1
Petr Mládek 809939
}
Petr Mládek 809939
Petr Mládek 809939
check_parents()
Petr Mládek 809939
{
Petr Mládek 809939
    last_branch=""
Petr Mládek 809939
    for branch in "$@" ; do
Petr Mládek 809939
        check_branch $branch
Petr Mládek 809939
        case $? in
Petr Mládek 809939
            0)
Petr Mládek 809939
                echo "    (found in $branch)"
Petr Mládek 809939
                return
Petr Mládek 809939
                ;;
Petr Mládek 809939
            1)
Petr Mládek 809939
                echo "    (partly in $branch)"
Petr Mládek 809939
                return
Petr Mládek 809939
                ;;
Petr Mládek 809939
            *)
Petr Mládek 809939
                ;;
Petr Mládek 809939
        esac
Petr Mládek 809939
        last_branch="$branch"
Petr Mládek 809939
    done
Petr Mládek 809939
Petr Mládek 809939
    # not found anywhere
Petr Mládek 809939
    echo "    (not even in $last_branch)"
Petr Mládek 809939
}
Petr Mládek 809939
Michal Koutný 0c2b4b
grep -w build "$branches" | grep -v -E "^(master|vanilla|linux-next|cve)" | \
Petr Mládek 809939
while read line ; do
Petr Mládek 809939
    line=${line%%\#*}
Michal Koutný 0c2b4b
    branch=${line%%:*}
Petr Mládek 809939
Petr Mládek 809939
    # empty line or comment
Petr Mládek 809939
    if [ -z "$branch" ] ; then
Petr Mládek 809939
       continue
Petr Mládek 809939
    fi
Petr Mládek 809939
Michal Koutný 0c2b4b
    # always check also the _EMBARGO branch as a possible parent
Michal Koutný 0c2b4b
    parents="${branch}_EMBARGO"
Michal Koutný 0c2b4b
    set dummy ${line#$branch:}
Michal Koutný 0c2b4b
    while [ $# -gt 0 ] ; do
Michal Koutný 0c2b4b
        shift
Michal Koutný 0c2b4b
        [[ "$1" =~ "merge:" ]] || continue
Michal Koutný 0c2b4b
        tmp="${1//*merge:-/}"
Michal Koutný 0c2b4b
        parents="$parents ${tmp//*merge:/}"
Michal Koutný 0c2b4b
    done
Michal Koutný 0c2b4b
Jiri Slaby ec10bb
    printf "%-23s" "$branch"
Petr Mládek 809939
    check_branch "$branch"
Petr Mládek 809939
Petr Mládek 809939
    case $? in
Petr Mládek 809939
        0)
Petr Mládek 809939
            echo "<ok>"
Petr Mládek 809939
            ;;
Petr Mládek 809939
        1)
Petr Mládek 809939
            echo -n "<partly> "
Petr Mládek 809939
            check_parents $parents
Petr Mládek 809939
            # print missing commits
Petr Mládek 809939
            check_branch -v "$branch"
Petr Mládek 809939
            ;;
Petr Mládek 809939
        *)
Petr Mládek 809939
            echo -n "<missing>"
Petr Mládek 809939
            check_parents "${branch}_EMBARGO" $parents 
Petr Mládek 809939
    esac
Michal Koutný 0c2b4b
done