Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 62141b34bd7ee925083456fda8ec803acfcf1cf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/perl
# ------------------------------------------------------------
# This script fixes the license headers of all Java sources
# to use the Eclipse EDL license template and updates the
# copyright statements using author information from git blame
#
# To fix this in all revisions rewrite the history
# git filter-branch --tree-filter 'fixHeaders.pl' HEAD
# ------------------------------------------------------------
use strict;

# Table of author names, start date, end date, actual copyright owner.
#
my @author_employers = (
	[ qr/spearce\@spearce.org/, 2008, 8, 9999, 12, 'Google Inc.'],

	[ qr/\@(.*\.|)google.com/, 0, 0, 9999, 12, 'Google Inc.'],
);

# License text itself.
#
my $license_text = <<'EOF';
 and other copyright owners as documented in the project's IP log.

 This program and the accompanying materials are made available
 under the terms of the Eclipse Distribution License v1.0 which
 accompanies this distribution, is reproduced below, and is
 available at http://www.eclipse.org/org/documents/edl-v10.php

 All rights reserved.

 Redistribution and use in source and binary forms, with or
 without modification, are permitted provided that the following
 conditions are met:

 - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

 - Redistributions in binary form must reproduce the above
   copyright notice, this list of conditions and the following
   disclaimer in the documentation and/or other materials provided
   with the distribution.

 - Neither the name of the Eclipse Foundation, Inc. nor the
   names of its contributors may be used to endorse or promote
   products derived from this software without specific prior
   written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
EOF

my @files = @ARGV;
unless (@files) {
	open( F, '-|', 'git ls-files' );
	@files = <F>;
	chop @files;
	close F;
}

foreach (@files) {
	if (/\.java$/ || $_ eq 'LICENSE') {
		next if $_ eq 'org.eclipse.jgit/src/org/eclipse/jgit/util/Base64.java';
		update_file(\&java_file, $_);

	} elsif (/pom\.xml$/) {
		update_file(\&pom_file, $_);

	} elsif (/\.sh$/) {
		update_file(\&sh_file, $_);
	}
}

sub java_file
{
	my $fd = shift;
	my $header = '';
	my $preamble = '';

	# header is everything before package statement
	while (<$fd>) {
		if (/^package /) {
			$preamble = $_;
			last;
		}
		$header .= $_;
	}

	# preamble is everything with blanks or imports
	while (<$fd>) {
		$preamble .= $_;
		last unless (/^import / || /^$/);
	}
	my $lineno = $. - 1;

	return ($header, $preamble, $lineno,
		"/*\n", sub { s/^/ */mg }, " */\n");
}

sub pom_file
{
	my $fd = shift;
	my $header = '';
	my $preamble = '';

	# header is everything before project
	while (<$fd>) {
		if (/<project/) {
			$preamble = $_;
			last;
		}
		$header .= $_;
	}
	my $lineno = $. - 1;

	return ($header, $preamble, $lineno,
		qq{<?xml version="1.0" encoding="UTF-8"?>\n<!--\n},
		sub { s/^(.)/  $1/mg },
		qq{-->\n});
}

sub sh_file
{
	my $fd = shift;
	my $top = <$fd>;
	my $header = '';
	my $preamble = '';

	while (<$fd>) {
		if (/^#/) {
			$header .= $_;
			next;
		}
		$preamble = $_;
		last;
	}
	my $lineno = $. - 1;

	return ($header, $preamble, $lineno, $top, sub { s/^/#/mg }, "");
}

sub update_file
{
	my $func = shift;
	my $old_file = shift;
	my $new_file = "$old_file.license.$$";

	open(I, '<', $old_file);
	my ($header, $preamble, $lineno,
		$top, $fmt, $btm) = &{$func}(\*I);

	my %all_years;
	my %author_years;
	my %minyear;
	my %maxyear;

	# find explicit copyright statements in sources
	my @lines = split( /\n/, $header );
	foreach my $line ( @lines ) {
		# * Copyright (c) 2008, Example Company Inc.
		# * Copyright (c) 2008, Joe Developer <joe.dev@example.org>
		# * Copyright (c) 2008, 2009 Joe Developer <joe.dev@example.org>
		# * Copyright (c) 2005-2009 Joe Developer <joe.dev@example.org>
		# * Copyright (c) 2008, 2009 Other Examples Inc.
		# * Copyright (c) 2008-2010 Example Company Inc.
		# * Copyright (C) 2009-2010, Yet More Examples Ltd.
		if( $line =~ m/Copyright \(c\) (\d{4})(?:\s*[,-]\s*(\d{4}))?,?\s*([^<>]+)\s*(<.*?>)?/i ) {
			my ($y, $y2, $n, $e) = ($1, $2, $3, $4);
			my $year = trim($y);
			my $author_name = trim($n);
			my $author_email = trim($e);
			my $who = $author_name;
			$who .= " $author_email" if $author_email;
			update_author_info(\%minyear, \%maxyear, \%all_years, \%author_years, $who, $year);
			if (my $year2 = $y2) {
				update_author_info(\%minyear, \%maxyear, \%all_years, \%author_years, $who, $year2);
			}
		}
	}

	if ($old_file eq 'LICENSE') {
	} else {
		# add implicit copyright statements from authors found in git blame
		my (%line_counts, %line_authors);
		my ($last_commit, $author_name, $author_email);
		my @blame_args = ('git', 'blame', "-L$lineno,", '-C', '-w', '-p');
		push(@blame_args, $ENV{'GIT_COMMIT'}) if $ENV{'GIT_COMMIT'};
		push(@blame_args, '--', $old_file);
		open( B, '-|', @blame_args);
		while (<B>) {
			chop;
			if (/^([0-9a-f]{40}) \d+ \d+ (\d+)$/) {
				$last_commit = $1;
				$line_counts{$1} += $2;
				next;
			}
			if (/^author (.*)$/) {
				$author_name = trim($1);
				next;
			}
			if (/^author-mail (<.*>)$/) {
				$author_email = trim($1);
				next;
			}
			if (/^author-time (\d+)$/) {
				# skip uncommitted changes
				my $who = "$author_name $author_email";
				next if $who eq 'Not Committed Yet <not.committed.yet>';
				my @tm = localtime($1);
				my $year = $tm[5] + 1900;
				my $mon = $tm[4] + 1;
				$who = translate_author($who, $year, $mon);
				$line_authors{$last_commit} = [$who, $year, $mon];
			}
		}
		close B;

		my %author_linecounts;
		foreach $last_commit (keys %line_counts) {
			my $who = $line_authors{$last_commit}[0];
			next unless $who;
			$author_linecounts{$who} += $line_counts{$last_commit};
		}

		my $sz = 100;
		my $count_big = 0;
		foreach (values %author_linecounts) {
			$count_big++ if $_ >= $sz;
		}

		my $added_count = 0;
		foreach (values %line_authors) {
			my ($who, $year, $mon) = @$_;
			next if ($count_big && $author_linecounts{$who} < $sz);
			$all_years{$year} = 1;
			update_author_info(\%minyear, \%maxyear, \%all_years, \%author_years, $who, $year, $mon);
		}
	}

	# rewrite file
	open( O, '>', $new_file );
	print O $top;

	my %used_author;
	foreach my $year ( sort { $a cmp $b } keys %all_years ) {
		foreach my $who ( sort keys %author_years ) {
			next if $used_author{$who}++;
			local $_ = format_copyright($minyear{$who}, $maxyear{$who}, $who);
			&{$fmt}();
			print O;
		}
	}

	local $_ = $license_text;
	&{$fmt}();
	print O;
	print O $btm;
	print O "\n";
	print O $preamble;
	print O while <I>;
	close I;
	close O;

	rename( $new_file, $old_file );
}

sub trim($)
{
	my $string = shift;
	$string =~ s/^\s+//;
	$string =~ s/\s+$//;
	return $string;
}

sub update_author_info
{
	my ($minyear_ref, $maxyear_ref, $all_years_ref, $author_years_ref, $who, $year, $mon) = @_;

	$who = translate_author($who, $year, $mon);
	$all_years_ref->{$year} = 1;
	$author_years_ref->{$who}{$year} = 1;

	my $y = $minyear_ref->{$who};
	if ($y < 1900) {
		$y = 9999;
	}
	if ($year < $y) {
		$minyear_ref->{$who} = $year;
	}
	$y = $maxyear_ref->{$who};
	if ($year > $y) {
		$maxyear_ref->{$who} = $year;
	}
}

sub date_cmp
{
	my ($a_year, $a_mon, $b_year, $b_mon) = @_;

	if ($a_year < $b_year) {
		return -1;
	} elsif ($a_year == $b_year) {
		return ($a_mon <=> $b_mon);
	} else {
		return 1;
	}
}

sub translate_author
{
	my ($who, $year, $mon) = @_;

	return $who if not defined $mon;

	foreach my $spec (@author_employers) {
		next unless $who =~ $spec->[0];
		next if (date_cmp($year, $mon, $spec->[1], $spec->[2]) < 0);
		next if (date_cmp($year, $mon, $spec->[3], $spec->[4]) > 0);
		return $spec->[5];
	}
	return $who;
}

sub format_copyright {
	my ($minyear, $maxyear, $who) = @_;
	if ($minyear < $maxyear) {
		return " Copyright (C) $minyear-$maxyear, $who\n";
	} else {
		return " Copyright (C) $minyear, $who\n";
	}
}

Back to the top