#!/usr/bin/env bash
#
# @license Apache-2.0
#
# Copyright (c) 2017 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Prints summary statistics for each commit.
#
# <weekday> <month> <day> <time> <year> <author_first> <author_last> <files_changed> <additions> <deletions>

# * `git log --format --shortstat`
#   - Extract from the commit log the author date and the author name, as well as commit summary statistics.
# * `awk '{}'`
#   - Parse the log results and output as a single line, handling edge cases due to missing data.
git log --format=format:"%ad %aN" --date=format:"%a %b %d %T %Y" --shortstat --reverse | awk '
BEGIN {
	line = ""
}
{
	if ($0 == "") {
		# Empty lines separate commits (except when no short stats accompany a commit).
	} else if (line == "") {
		# Begin processing a new commit...
		if (NF == 6) {
			# <weekday> <month> <day> <time> <year> <author>
			line = $0 OFS "(null)"
		} else {
			# <weekday> <month> <day> <time> <year> <author_first_name> <author_last_name>
			line = $0
		}
	} else if ($0 ~ /changed/) {
		# Begin processing commit statistics...
		if (NF < 5) {
			# <num_files_changed>
			line = line OFS $1 OFS 0 OFS 0
		} else if (NF < 7) {
			# <num_files_changed> <additions>
			line = line OFS $1 OFS $4 OFS 0
		} else {
			# <num_files_changed> <additions> <deletions>
			line = line OFS $1 OFS $4 OFS $6
		}
		print line
		line = ""
	} else {
		# No short stats accompanied the commit processed on the previous line (e.g., commit is a merge):
		print line OFS 0 OFS 0 OFS 0

		# Begin processing a new commit...
		if (NF == 5) {
			# <weekday> <month> <day> <time> <year> <author>
			line = $0 OFS "(null)"
		} else {
			# <weekday> <month> <day> <time> <year> <author_first_name> <author_last_name>
			line = $0
		}
	}
}
'
