You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fix-headers.pl 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #!/usr/bin/perl
  2. # ------------------------------------------------------------
  3. # This script fixes the license headers of all Java sources
  4. # to use the Eclipse EDL license template and updates the
  5. # copyright statements using author information from git blame
  6. #
  7. # To fix this in all revisions rewrite the history
  8. # git filter-branch --tree-filter 'fixHeaders.pl' HEAD
  9. # ------------------------------------------------------------
  10. use strict;
  11. # Table of author names, start date, end date, actual copyright owner.
  12. #
  13. my @author_employers = (
  14. [ qr/spearce\@spearce.org/, 2008, 8, 9999, 12, 'Google Inc.'],
  15. [ qr/\@(.*\.|)google.com/, 0, 0, 9999, 12, 'Google Inc.'],
  16. );
  17. # License text itself.
  18. #
  19. my $license_text = <<'EOF';
  20. and other copyright owners as documented in the project's IP log.
  21. This program and the accompanying materials are made available
  22. under the terms of the Eclipse Distribution License v1.0 which
  23. accompanies this distribution, is reproduced below, and is
  24. available at http://www.eclipse.org/org/documents/edl-v10.php
  25. All rights reserved.
  26. Redistribution and use in source and binary forms, with or
  27. without modification, are permitted provided that the following
  28. conditions are met:
  29. - Redistributions of source code must retain the above copyright
  30. notice, this list of conditions and the following disclaimer.
  31. - Redistributions in binary form must reproduce the above
  32. copyright notice, this list of conditions and the following
  33. disclaimer in the documentation and/or other materials provided
  34. with the distribution.
  35. - Neither the name of the Eclipse Foundation, Inc. nor the
  36. names of its contributors may be used to endorse or promote
  37. products derived from this software without specific prior
  38. written permission.
  39. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  40. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  41. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  42. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  44. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  48. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  51. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52. EOF
  53. my @files = @ARGV;
  54. unless (@files) {
  55. open( F, '-|', 'git ls-files' );
  56. @files = <F>;
  57. chop @files;
  58. close F;
  59. }
  60. foreach (@files) {
  61. if (/\.java$/ || $_ eq 'LICENSE') {
  62. next if $_ eq 'org.eclipse.jgit/src/org/eclipse/jgit/util/Base64.java';
  63. update_file(\&java_file, $_);
  64. } elsif (/pom\.xml$/) {
  65. update_file(\&pom_file, $_);
  66. } elsif (/\.sh$/) {
  67. update_file(\&sh_file, $_);
  68. }
  69. }
  70. sub java_file
  71. {
  72. my $fd = shift;
  73. my $header = '';
  74. my $preamble = '';
  75. # header is everything before package statement
  76. while (<$fd>) {
  77. if (/^package /) {
  78. $preamble = $_;
  79. last;
  80. }
  81. $header .= $_;
  82. }
  83. # preamble is everything with blanks or imports
  84. while (<$fd>) {
  85. $preamble .= $_;
  86. last unless (/^import / || /^$/);
  87. }
  88. my $lineno = $. - 1;
  89. return ($header, $preamble, $lineno,
  90. "/*\n", sub { s/^/ */mg }, " */\n");
  91. }
  92. sub pom_file
  93. {
  94. my $fd = shift;
  95. my $header = '';
  96. my $preamble = '';
  97. # header is everything before project
  98. while (<$fd>) {
  99. if (/<project/) {
  100. $preamble = $_;
  101. last;
  102. }
  103. $header .= $_;
  104. }
  105. my $lineno = $. - 1;
  106. return ($header, $preamble, $lineno,
  107. qq{<?xml version="1.0" encoding="UTF-8"?>\n<!--\n},
  108. sub { s/^(.)/ $1/mg },
  109. qq{-->\n});
  110. }
  111. sub sh_file
  112. {
  113. my $fd = shift;
  114. my $top = <$fd>;
  115. my $header = '';
  116. my $preamble = '';
  117. while (<$fd>) {
  118. if (/^#/) {
  119. $header .= $_;
  120. next;
  121. }
  122. $preamble = $_;
  123. last;
  124. }
  125. my $lineno = $. - 1;
  126. return ($header, $preamble, $lineno, $top, sub { s/^/#/mg }, "");
  127. }
  128. sub update_file
  129. {
  130. my $func = shift;
  131. my $old_file = shift;
  132. my $new_file = "$old_file.license.$$";
  133. open(I, '<', $old_file);
  134. my ($header, $preamble, $lineno,
  135. $top, $fmt, $btm) = &{$func}(\*I);
  136. my %all_years;
  137. my %author_years;
  138. my %minyear;
  139. my %maxyear;
  140. # find explicit copyright statements in sources
  141. my @lines = split( /\n/, $header );
  142. foreach my $line ( @lines ) {
  143. # * Copyright (c) 2008, Example Company Inc.
  144. # * Copyright (c) 2008, Joe Developer <joe.dev@example.org>
  145. # * Copyright (c) 2008, 2009 Joe Developer <joe.dev@example.org>
  146. # * Copyright (c) 2005-2009 Joe Developer <joe.dev@example.org>
  147. # * Copyright (c) 2008, 2009 Other Examples Inc.
  148. # * Copyright (c) 2008-2010 Example Company Inc.
  149. # * Copyright (C) 2009-2010, Yet More Examples Ltd.
  150. if( $line =~ m/Copyright \(c\) (\d{4})(?:\s*[,-]\s*(\d{4}))?,?\s*([^<>]+)\s*(<.*?>)?/i ) {
  151. my ($y, $y2, $n, $e) = ($1, $2, $3, $4);
  152. my $year = trim($y);
  153. my $author_name = trim($n);
  154. my $author_email = trim($e);
  155. my $who = $author_name;
  156. $who .= " $author_email" if $author_email;
  157. update_author_info(\%minyear, \%maxyear, \%all_years, \%author_years, $who, $year);
  158. if (my $year2 = $y2) {
  159. update_author_info(\%minyear, \%maxyear, \%all_years, \%author_years, $who, $year2);
  160. }
  161. }
  162. }
  163. if ($old_file eq 'LICENSE') {
  164. } else {
  165. # add implicit copyright statements from authors found in git blame
  166. my (%line_counts, %line_authors);
  167. my ($last_commit, $author_name, $author_email);
  168. my @blame_args = ('git', 'blame', "-L$lineno,", '-C', '-w', '-p');
  169. push(@blame_args, $ENV{'GIT_COMMIT'}) if $ENV{'GIT_COMMIT'};
  170. push(@blame_args, '--', $old_file);
  171. open( B, '-|', @blame_args);
  172. while (<B>) {
  173. chop;
  174. if (/^([0-9a-f]{40}) \d+ \d+ (\d+)$/) {
  175. $last_commit = $1;
  176. $line_counts{$1} += $2;
  177. next;
  178. }
  179. if (/^author (.*)$/) {
  180. $author_name = trim($1);
  181. next;
  182. }
  183. if (/^author-mail (<.*>)$/) {
  184. $author_email = trim($1);
  185. next;
  186. }
  187. if (/^author-time (\d+)$/) {
  188. # skip uncommitted changes
  189. my $who = "$author_name $author_email";
  190. next if $who eq 'Not Committed Yet <not.committed.yet>';
  191. my @tm = localtime($1);
  192. my $year = $tm[5] + 1900;
  193. my $mon = $tm[4] + 1;
  194. $who = translate_author($who, $year, $mon);
  195. $line_authors{$last_commit} = [$who, $year, $mon];
  196. }
  197. }
  198. close B;
  199. my %author_linecounts;
  200. foreach $last_commit (keys %line_counts) {
  201. my $who = $line_authors{$last_commit}[0];
  202. next unless $who;
  203. $author_linecounts{$who} += $line_counts{$last_commit};
  204. }
  205. my $sz = 100;
  206. my $count_big = 0;
  207. foreach (values %author_linecounts) {
  208. $count_big++ if $_ >= $sz;
  209. }
  210. my $added_count = 0;
  211. foreach (values %line_authors) {
  212. my ($who, $year, $mon) = @$_;
  213. next if ($count_big && $author_linecounts{$who} < $sz);
  214. $all_years{$year} = 1;
  215. update_author_info(\%minyear, \%maxyear, \%all_years, \%author_years, $who, $year, $mon);
  216. }
  217. }
  218. # rewrite file
  219. open( O, '>', $new_file );
  220. print O $top;
  221. my %used_author;
  222. foreach my $year ( sort { $a cmp $b } keys %all_years ) {
  223. foreach my $who ( sort keys %author_years ) {
  224. next if $used_author{$who}++;
  225. local $_ = format_copyright($minyear{$who}, $maxyear{$who}, $who);
  226. &{$fmt}();
  227. print O;
  228. }
  229. }
  230. local $_ = $license_text;
  231. &{$fmt}();
  232. print O;
  233. print O $btm;
  234. print O "\n";
  235. print O $preamble;
  236. print O while <I>;
  237. close I;
  238. close O;
  239. rename( $new_file, $old_file );
  240. }
  241. sub trim($)
  242. {
  243. my $string = shift;
  244. $string =~ s/^\s+//;
  245. $string =~ s/\s+$//;
  246. return $string;
  247. }
  248. sub update_author_info
  249. {
  250. my ($minyear_ref, $maxyear_ref, $all_years_ref, $author_years_ref, $who, $year, $mon) = @_;
  251. $who = translate_author($who, $year, $mon);
  252. $all_years_ref->{$year} = 1;
  253. $author_years_ref->{$who}{$year} = 1;
  254. my $y = $minyear_ref->{$who};
  255. if ($y < 1900) {
  256. $y = 9999;
  257. }
  258. if ($year < $y) {
  259. $minyear_ref->{$who} = $year;
  260. }
  261. $y = $maxyear_ref->{$who};
  262. if ($year > $y) {
  263. $maxyear_ref->{$who} = $year;
  264. }
  265. }
  266. sub date_cmp
  267. {
  268. my ($a_year, $a_mon, $b_year, $b_mon) = @_;
  269. if ($a_year < $b_year) {
  270. return -1;
  271. } elsif ($a_year == $b_year) {
  272. return ($a_mon <=> $b_mon);
  273. } else {
  274. return 1;
  275. }
  276. }
  277. sub translate_author
  278. {
  279. my ($who, $year, $mon) = @_;
  280. return $who if not defined $mon;
  281. foreach my $spec (@author_employers) {
  282. next unless $who =~ $spec->[0];
  283. next if (date_cmp($year, $mon, $spec->[1], $spec->[2]) < 0);
  284. next if (date_cmp($year, $mon, $spec->[3], $spec->[4]) > 0);
  285. return $spec->[5];
  286. }
  287. return $who;
  288. }
  289. sub format_copyright {
  290. my ($minyear, $maxyear, $who) = @_;
  291. if ($minyear < $maxyear) {
  292. return " Copyright (C) $minyear-$maxyear, $who\n";
  293. } else {
  294. return " Copyright (C) $minyear, $who\n";
  295. }
  296. }