#!/bin/bash

relpipe-in-hglog() {
	# on repositories with long history 
	# it is good to process just a subset of the log using hg log parameters
	hg log --template xml \
		| relpipe-in-xmltable \
			--relation hg_log \
				--records "/log/logentry" \
				--attribute "revision" integer "@revision" \
				--attribute "date" string "date" \
				--attribute "hash" string "@node" \
				--attribute "hash_short" string "substring(@node,1,12)" \
				--attribute "branch" string "branch" \
				--attribute "author_name" string "author" \
				--attribute "message" string "msg"
				# --attribute "author_email" string "author/@email" \
}

relpipe-in-hgtags() {
	hg tags \
		| sed -E 's/([^ \s]+)\s+([0-9]+):(.*)/\1\n\2\n\3/g' \
		| tr \\n \\0 \
		| relpipe-in-cli \
			--relation "hg_tags" \
				--attribute "tag" string \
				--attribute "revision" integer \
				--attribute "hash_short" string
}


# just view everything:
# (relpipe-in-hglog; relpipe-in-hgtags) | relpipe-out-tabular | less -RSi


# filter and JOIN data, do some statistics:
(relpipe-in-hglog; relpipe-in-hgtags) \
	| relpipe-tr-sql \
		--relation "tags" \
			"SELECT
				t.*,
				l.date
			FROM hg_tags AS t
			JOIN hg_log AS l USING (hash_short)" \
		--relation "filtered_log" \
			"SELECT 
				revision, 
				date, 
				hash_short,
				branch, 
				author_name
			FROM hg_log
			ORDER BY revision DESC
			LIMIT ?" \
			--parameter 10 \
		--relation "author_statistics" \
			"SELECT
				author_name,
				branch,
				count(*) AS count,
				min(date) AS first_commit,
				max(date) AS last_commit
				-- TODO: user proper data type for date
			FROM hg_log
			GROUP BY author_name, branch
			ORDER BY count" \
	| relpipe-out-tabular

