]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - tools/mkheaders
Disable auto extras when TEST_BUILD_MODULES is set in test-build.
[user/henk/code/inspircd.git] / tools / mkheaders
1 #!/usr/bin/env perl
2 #
3 # InspIRCd -- Internet Relay Chat Daemon
4 #
5 #   Copyright (C) 2020 Sadie Powell <sadie@witchery.services>
6 #
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.
10 #
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
14 # details.
15 #
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/>.
18 #
19
20
21 BEGIN {
22         require 5.10.0;
23         unless (-f 'configure') {
24                 print "Error: $0 must be run from the main source directory!\n";
25                 exit 1;
26         }
27 }
28
29 use feature ':5.10';
30 use strict;
31 use warnings FATAL => qw(all);
32
33 use File::Basename qw(dirname);
34 use File::Util     ();
35 use FindBin        qw($RealDir);
36 use List::Util     qw(uniq);
37 use POSIX          qw(strftime);
38
39 use lib dirname $RealDir;
40 use make::console;
41
42 my @ignored_revisions = (
43         '0b4285abd12323920d92fee51e199edd7527dbec', # adding copyright headers
44         '46a39046196f55b52336e19662bb7bac85b731ac', # adding copyright headers
45         '56375392ba94f2552bbeeeab4fd39e1e50295525', # sadie's name change
46         'bab14f0dd2345c9d7dcbc47c918563709e1ac094', # peavey breaking line endings
47         'f2acdbc3820f0f4f5ef76a0a64e73d2a320df91f', # peavey fixing line endings
48 );
49
50 my @paths = File::Util->new->list_dir('.' => { recurse => 1 });
51 my @updated;
52 for my $path (@paths) {
53         next unless -f $path;
54         next if $path =~ /\/\./;
55         next if $path =~ /\/vendor\//;
56
57         if (system "git ls-files --error-unmatch -- $path 1>/dev/null 2>/dev/null") {
58                 print_format "Skipping <|YELLOW $path|> as it has not been committed.\n" if defined $ENV{MKHEADERS_VERBOSE};
59                 next;
60         }
61
62         open(my $fh, $path) or print_error "unable to read from $path: $!";
63         my ($copyright, $indent, @lines);
64         for my $line (<$fh>) {
65                 chomp $line;
66                 if ($line =~ /^([^0-9A-Za-z]+\s)Copyright\s+\(C\)\s+[^<]+\s+<[^>]+>$/) {
67                         $copyright = scalar @lines;
68                         $indent = $1;
69                 } else {
70                         push @lines, $line;
71                 }
72         }
73         close $fh;
74
75         if (defined $copyright) {
76                 print_format "Updating copyright headers in <|GREEN $path|>.\n" if defined $ENV{MKHEADERS_VERBOSE};
77                 my (%author, %authors);
78                 my $ignored_args = join ' ', map { "--ignore-rev $_" } @ignored_revisions;
79                 for my $line (split /\n+/, `git blame $ignored_args --incremental -w HEAD -- $path`) {
80                         if ($line =~ /^([0-9a-f]{40})(?:\s\d+){3}$/) {
81                                 $author{COMMITS} //= [];
82                                 push @{$author{COMMITS}}, $1;
83                         } elsif ($line =~ /^author (.+)/) {
84                                 $author{NAME} = $1;
85                         } elsif ($line =~ /^author-mail <(.+)>/) {
86                                 $author{EMAIL} = $1;
87                         } elsif ($line =~ /^author-time (.+)/) {
88                                 $author{YEAR} = strftime '%Y', gmtime $1;
89                         } elsif ($line =~ /^filename /) {
90                                 next unless scalar keys %author > 1;
91                                 my $display = sprintf "%s <%s>", $author{NAME}, $author{EMAIL};
92                                 $authors{$display} //= [];
93                                 push $authors{$display}, $author{YEAR};
94                                 for my $commit (uniq @{$author{COMMITS}}) {
95                                         my $details = `git rev-list --format=%B --max-count=1 $commit`;
96                                         while ($details =~ /co-authored-by: ([^<]+<[^>]+>)/gi) {
97                                                 $authors{$1} //= [];
98                                                 push $authors{$1}, $author{YEAR};
99                                         }
100                                 }
101                                 undef %author;
102                         }
103                 }
104
105                 my @copyrights;
106                 while (my ($display, $years) = each %authors) {
107                         next if $display eq 'InspIRCd Robot <noreply@inspircd.org>';
108                         my ($last_year, $start_year, @year_ranges);
109                         for my $year (uniq sort @$years) {
110                                 if (!defined $last_year) {
111                                         $start_year = $last_year = $year
112                                 } elsif ($year == $last_year + 1) {
113                                         $last_year = $year;
114                                 } else {
115                                         if ($last_year == $start_year) {
116                                                 push @year_ranges, $last_year; 
117                                         } else {
118                                                 push @year_ranges, "$start_year-$last_year";
119                                         }
120                                         $start_year = $last_year = $year;
121                                 }
122                         }
123                         if (defined $last_year) {
124                                 if ($last_year == $start_year) {
125                                         push @year_ranges, $last_year; 
126                                 } else {
127                                         push @year_ranges, "$start_year-$last_year";
128                                 }
129                         }
130
131                         my $joined_years = join ', ', @year_ranges;
132                         push @copyrights, "${\$indent}Copyright (C) $joined_years $display";
133                 }
134
135                 splice @lines, $copyright, 0, reverse sort @copyrights;
136                 open(my $fh, '>', $path) or print_error "unable to write to $path: $!";
137                 for my $line (@lines) {
138                         say $fh $line;
139                 }
140                 close $fh;
141                 push @updated, $path;
142         } else {
143                 print_format "Skipping <|YELLOW $path|> as it contains no copyright headers.\n" if defined $ENV{MKHEADERS_VERBOSE};
144         }
145 }
146
147 system 'git', 'commit',
148         '--author', 'InspIRCd Robot <noreply@inspircd.org>',
149         '--message', 'Update copyright headers.',
150         '--', @updated;