#!/bin/sh
# Copyright 2009-2018 Canonical, Ltd.
# Authors:
#  Jamie Strandboge <jamie@canonical.com>
#  Kees Cook <kees@ubuntu.com>
#  Marc Deslauriers <marc.deslauriers@canonical.com>
#  Steve Beattie <steve.beattie@canonical.com>

# Based mostly on the kernel_team_merge() from process_cves, except
# without the actual merging; i.e. it doesn't make any changes to the
# local checkout.

set -e

if [ -z "${UCT}" ] ; then
	echo "\$UCT is not set, aborting"
	exit 1
fi

if ! [ -d "${UCT}" ] ; then
	echo "${UCT} is not a directory, aborting"
	exit 1
fi

cd "${UCT}"

short_output=

help() {
    cat <<EOM
usage: report-missing-kmerge [-s]

Report unmerged commits from the kernel team for triaging kernel cves

    -s  short output: only show git commit messages, not full
        diff output
    -o  oneline output: only show git --online output, not full diff
    -h  this message
EOM
}

while getopts "sho" opt ; do
    case "$opt" in
        s) short_output="yes";;
	o) oneline_output="yes";;
	h) help; exit 0;;
	?) help; exit 1;;
    esac
done

kernel_team_merge() {
    kernel_remote='kernel-team'
    kernel_url="https://git.launchpad.net/~canonical-kernel-team/ubuntu-cve-tracker"
    git_show_args=

    if ! (git remote | grep -q "^${kernel_remote}$") ; then
        git remote add "${kernel_remote}" "${kernel_url}"
    fi

    if [ "${oneline_output}" = "yes" ] ; then
        git_show_args="-s --oneline"
    elif [ "${short_output}" = "yes" ] ; then
        git_show_args="-s"
    fi

    git fetch -q "${kernel_remote}"
    if ! [ "$(git rev-list HEAD..${kernel_remote}/master --count)" -eq 0 ] ; then
        echo "Missing merge commits from the Kernel Team's CVE Tree"
        git rev-list HEAD..${kernel_remote}/master | xargs git show $git_show_args

	echo ""
	echo "Please use uct_kernel_merge_commit() (from scripts/dot.uct-functions.sh)"
	echo "after reviewing these changes to merge these change"
    fi
}

kernel_team_merge
