#!/bin/bash

base=$1
next=$2
rc=$3

sd=../stats

[ -d $sd ] || mkdir $sd || {
	echo "No stats dir" 1>&2
	exit 1
}

git rev-list --no-merges $base..$next | sort >$sd/$next.sha1s
git rev-list --no-merges $base..$rc | sort >$sd/$rc.sha1s

# filter out the common commits
comm -12 $sd/$next.sha1s $sd/$rc.sha1s >$sd/common_sha1s
comm -23 $sd/$next.sha1s $sd/$rc.sha1s >$sd/$next.sha1s.1
comm -13 $sd/$next.sha1s $sd/$rc.sha1s >$sd/$rc.sha1s.1

# now find commits with the same patch-id
git diff-tree -p --stdin <$sd/$next.sha1s.1 |
	git patch-id |
	sort >$sd/$next.patch_ids
git diff-tree -p --stdin <$sd/$rc.sha1s.1 |
	git patch-id |
	sort >$sd/$rc.patch_ids
join -j 1 $sd/$next.patch_ids $sd/$rc.patch_ids >$sd/common_id
join -v 1 -j 1 $sd/$next.patch_ids $sd/$rc.patch_ids |
	sed 's/^.* //' >$sd/$next.sha1s.2
join -v 2 -j 1 $sd/$next.patch_ids $sd/$rc.patch_ids |
	sed 's/^.* //' >$sd/$rc.sha1s.2

# now find commits with the same subject line
git log --stdin --no-walk --format='%s|%H' <$sd/$next.sha1s.2 |
	sed 's/|\(.*\)|/_\1|/g' |
	sort -f -k 1,1 -t '|' >$sd/$next.subj
git log --stdin --no-walk --format='%s|%H' <$sd/$rc.sha1s.2 |
	sed 's/|\(.*\)|/_\1|/g' |
	sort -f -k 1,1 -t '|' >$sd/$rc.subj
join -i -j 1 -t '|' $sd/$next.subj $sd/$rc.subj >$sd/common_subj
join -v 1 -i -j 1 -t '|' $sd/$next.subj $sd/$rc.subj |
	sed 's/^.*|//' >$sd/$next.sha1s.3
join -v 2 -i -j 1 -t '|' $sd/$next.subj $sd/$rc.subj |
	sed 's/^.*|//' >$sd/$rc.sha1s.3
