1#!/bin/bash
2
3# Find the git commit year range for each individual author that committed changes to a given file
4# and emit those data in useful copyright format.
5
6# Typical usage:
7
8# <pathname of script> <filename> <comment_prefix>
9
10# where <filename> is the name of the file whose git logs are being analyzed by this script,
11# and <comment_prefix> is an optional comment prefix (which defaults to "//") to prefix
12# every emitted copyright line.
13
14# Copyright (C) 2019 Alan W. Irwin
15#
16# This file is part of PLplot.
17#
18# PLplot is free software; you can redistribute it and/or modify
19# it under the terms of the GNU Library General Public License as published
20# by the Free Software Foundation; either version 2 of the License, or
21# (at your option) any later version.
22#
23# PLplot is distributed in the hope that it will be useful,
24# but WITHOUT ANY WARRANTY; without even the implied warranty of
25# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26# GNU Library General Public License for more details.
27#
28# You should have received a copy of the GNU Library General Public License
29# along with PLplot; if not, write to the Free Software
30# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31
32filename="$1"
33comment_prefix="$2"
34if [ -z $comment_prefix ] ; then
35    # Use C language comment prefix by default
36    comment_prefix="//"
37fi
38
39emit_copyright () {
40    filename="$1"
41    comment_prefix="$2"
42
43    # save old IFS for restoration at the end
44    IFS_OLD=$IFS
45    # Set IFS so that spaces in authors names will not be delimiters
46    # in command substitution.
47    IFS=$'\n'
48
49    # --pretty=format used to display combination of specified attributes where
50    # %an is author name and %ad is date, and you have complete control of strings
51    # surrounding those attributes (if needed) which identify them to following grep
52    for NAME in $(git log --follow --pretty=format:'%an' $filename |sort -u |sed -e 's?.*: ??' -e 's? <.*$??') ; do
53	FIRST_DATE=$(git log --follow --date=short --pretty=format:'Author: %an DATE: %ad' $filename |grep "Author: $NAME" |tail -1 |sed -e 's?^.*: ??' -e 's?-[0-9][0-9]-[0-9][0-9]$??')
54	LAST_DATE=$(git log --follow --date=short --pretty=format:'Author: %an DATE: %ad' $filename |grep "Author: $NAME" |head -1 |sed -e 's?^.*: ??' -e 's?-[0-9][0-9]-[0-9][0-9]$??')
55
56	if [ "$FIRST_DATE" = "$LAST_DATE" ] ; then
57	    echo "$comment_prefix Copyright (C) $FIRST_DATE $NAME"
58	else
59	    echo "$comment_prefix Copyright (C) $FIRST_DATE-$LAST_DATE $NAME"
60	fi
61    done
62    # Restore IFS
63    unset IFS
64    IFS=$IFS_OLD
65}
66
67emit_copyright "$filename" "$comment_prefix" | sort
68