3 # InspIRCd -- Internet Relay Chat Daemon
5 # Copyright (C) 2020 Sadie Powell <sadie@witchery.services>
7 # This file is part of InspIRCd. InspIRCd is free software: you can
8 # redistribute it and/or modify it under the terms of the GNU General Public
9 # License as published by the Free Software Foundation, version 2.
11 # This program is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 unless (-f 'configure') {
24 print "Error: $0 must be run from the main source directory!\n";
31 use warnings FATAL => qw(all);
33 use File::Basename qw(dirname);
35 use FindBin qw($RealDir);
36 use List::Util qw(uniq);
37 use POSIX qw(strftime);
39 use lib dirname $RealDir;
43 my @ignored_revisions = (
44 '0b4285abd12323920d92fee51e199edd7527dbec', # adding copyright headers
45 '46a39046196f55b52336e19662bb7bac85b731ac', # adding copyright headers
46 '4a6fedd9324d87349a806c9c1d0ae6e7d3c1fd38', # mass-updating descriptions
47 '56375392ba94f2552bbeeeab4fd39e1e50295525', # sadie's name change
48 'bab14f0dd2345c9d7dcbc47c918563709e1ac094', # peavey breaking line endings
49 'f2acdbc3820f0f4f5ef76a0a64e73d2a320df91f', # peavey fixing line endings
52 my @paths = File::Util->new->list_dir('.' => { recurse => 1 });
54 for my $path (@paths) {
56 next if $path =~ /\/\./;
57 next if $path =~ /\/vendor\//;
59 if (system "git ls-files --error-unmatch -- $path 1>/dev/null 2>/dev/null") {
60 print_format "Skipping <|YELLOW $path|> as it has not been committed.\n" if defined $ENV{MKHEADERS_VERBOSE};
64 open(my $fh, $path) or print_error "unable to read from $path: $!";
65 my ($copyright, $indent, @lines);
66 for my $line (<$fh>) {
68 if ($line =~ /^([^0-9A-Za-z]+\s)Copyright\s+\(C\)\s+[^<]+\s+<[^>]+>$/) {
69 $copyright = scalar @lines;
77 if (defined $copyright) {
78 print_format "Updating copyright headers in <|GREEN $path|>.\n" if defined $ENV{MKHEADERS_VERBOSE};
79 my (%author, %authors);
80 my $ignored_args = join ' ', map { "--ignore-rev $_" } @ignored_revisions;
81 for my $line (split /\n+/, `git blame $ignored_args --incremental -w HEAD -- $path`) {
82 if ($line =~ /^([0-9a-f]{40})(?:\s\d+){3}$/) {
83 $author{COMMITS} //= [];
84 push @{$author{COMMITS}}, $1;
85 } elsif ($line =~ /^author (.+)/) {
87 } elsif ($line =~ /^author-mail <(.+)>/) {
89 } elsif ($line =~ /^author-time (.+)/) {
90 $author{YEAR} = strftime '%Y', gmtime $1;
91 } elsif ($line =~ /^filename /) {
92 next unless scalar keys %author > 1;
93 my $display = sprintf "%s <%s>", $author{NAME}, $author{EMAIL};
94 $authors{$display} //= [];
95 push @{$authors{$display}}, $author{YEAR};
96 for my $commit (uniq @{$author{COMMITS}}) {
97 my $details = `git rev-list --format=%B --max-count=1 $commit`;
98 while ($details =~ /co-authored-by: ([^<]+<[^>]+>)/gi) {
100 push @{$authors{$1}}, $author{YEAR};
108 while (my ($display, $years) = each %authors) {
109 next if $display eq 'InspIRCd Robot <noreply@inspircd.org>';
110 my ($last_year, $start_year, @year_ranges);
111 for my $year (uniq sort @$years) {
112 if (!defined $last_year) {
113 $start_year = $last_year = $year
114 } elsif ($year == $last_year + 1) {
117 if ($last_year == $start_year) {
118 push @year_ranges, $last_year;
120 push @year_ranges, "$start_year-$last_year";
122 $start_year = $last_year = $year;
125 if (defined $last_year) {
126 if ($last_year == $start_year) {
127 push @year_ranges, $last_year;
129 push @year_ranges, "$start_year-$last_year";
133 my $joined_years = join ', ', @year_ranges;
134 push @copyrights, "${\$indent}Copyright (C) $joined_years $display";
137 splice @lines, $copyright, 0, reverse sort @copyrights;
138 open(my $fh, '>', $path) or print_error "unable to write to $path: $!";
139 for my $line (@lines) {
143 push @updated, $path;
145 print_format "Skipping <|YELLOW $path|> as it contains no copyright headers.\n" if defined $ENV{MKHEADERS_VERBOSE};
149 execute 'git', 'commit',
150 '--author', 'InspIRCd Robot <noreply@inspircd.org>',
151 '--message', 'Update copyright headers.',