#!/bin/bash

. show-missing-iter
. common

function relevant {
	check_relevant $1
	if [ $? -eq 0 ]; then
		echo R
	fi
}

function applies {
	git cherry-pick $1 &> /dev/null
	if [ $? -eq 0 ]; then
		echo "+"
		git reset --hard HEAD^ &> /dev/null
	else
		echo "-"
		git reset --hard &> /dev/null
	fi
}

function show_deps {
	app=$1
	commit=$2

	if [ "$app" = "-" ]; then
		echo "Possible dependency chain:"
		stable deps $commit 10 | sed 's/^/	/'
		if [ $? -eq 1 ]; then
			echo "	"[...]
		fi
	fi
}

function find_owning_branch {
	subj=$(git log -1 --pretty="%s" $1)
	for m in $OTHER_STABLE_TREES; do
		if [ $(git log -F --grep "$subj" --format="%H" $m) ]; then
			echo "	"$m
		fi
	done
}

function handle_stable {
	others=$(stable find-alts $1)
	app=$(applies $1)
	relevant=$(relevant $1)
	if [ "$others" != "" ]; then
		printf "[E$app$relevant] %s\n" "$(git log -1 --oneline $1)"
		find_owning_branch $1
		show_deps $app $1
	else
		printf "[M$app$relevant] %s\n" "$(git log -1 --oneline $1)"
		show_deps $app $1
	fi
	echo
}

function handle_nonstable {
	others=$(stable find-alts $1)
	if [ "$others" != "" ]; then
		app=$(applies $1)
		relevant=$(relevant $1)
		printf "[N$app$relevant] %s:\n" "$(git log -1 --oneline $1)"
		find_owning_branch $1
		show_deps $app $1
		echo
	fi
}

function do_one {
	if [ "$(git show $1 | grep -i 'stable@vger' | wc -l)" -gt 0 ]; then
		handle_stable $1
	else
		handle_nonstable $1
	fi
}

if [ "$#" -ne 1 ]; then
        echo "Usage: stable audit-range <commit range>"
        exit 1
fi

show_missing_iter $1 do_one
