update-copyright: fix bug for 2-digit last year and add tests
[gnulib.git] / build-aux / update-copyright
1 #!/usr/bin/perl -0777 -pi
2 # Update an FSF copyright year list to include the current year.
3
4 my $VERSION = '2009-08-03.23:03'; # UTC
5
6 # Copyright (C) 2009 Free Software Foundation
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3, or (at your option)
11 # any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 # Written by Jim Meyering
22
23 # The arguments to this script should be names of files that contain FSF
24 # copyright statements to be updated.  For example, you may wish to
25 # place a target like the following in the top-level makefile in your
26 # project:
27 #
28 #   .PHONY: update-copyright
29 #   update-copyright:
30 #       if test -d .git; then                                   \
31 #         git grep -l -w Copyright                              \
32 #           | grep -v -E '(^|/)(COPYING|ChangeLog)'             \
33 #           | xargs $(srcdir)/build-aux/$@;                     \
34 #       fi
35 #
36 # In the second grep, you can build a list of files to skip within your
37 # project.
38 #
39 # Iff an FSF copyright statement is discovered in a file and the final
40 # year is not the current year, the statement is updated for the new
41 # year and reformatted to fit within 72 columns.  A warning is printed
42 # for every file for which no FSF copyright statement is discovered.
43 #
44 # Each file's FSF copyright statement must be formated correctly in
45 # order to be recognized, and it must appear before other text that
46 # looks like the start of a copyright statement.  For example, each of
47 # these by itself is fine:
48 #
49 #   Copyright (C) 1990-2005, 2007-2009 Free Software Foundation,
50 #   Inc.
51 #
52 #   # Copyright (c) 1990-2005, 2007-2009 Free Software
53 #   # Foundation, Inc.
54 #
55 #   /*
56 #    * Copyright (C) 90,2005,2007-2009 Free Software
57 #    * Foundation, Inc.
58 #    */
59 #
60 # However, the following format is not recognized because the line
61 # prefix changes after the first line:
62 #
63 #   /* Copyright (C) 1990-2005, 2007-2009 Free Software
64 #    * Foundation, Inc.  */
65 #
66 # The following copyright statement is not recognized because the
67 # copyright holder is not the FSF:
68 #
69 #   Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
70 #
71 # Moreover, any FSF copyright statement following either of the previous
72 # copyright statements might not be recognized.
73 #
74 # The exact conditions that a file's FSF copyright statement must meet
75 # to be recognized are listed below.  They may seem slightly complex,
76 # but you need not worry if some file in your project accidentally
77 # breaks one.  The worst that can happen is that a file is not updated
78 # and a warning is issued.
79 #
80 #   1. The format is "Copyright (C)" (where "(C)" can be "(c)"), then a
81 #      list of copyright years, and then the name of the copyright
82 #      holder, which is "Free Software Foundation, Inc.".
83 #   2. "Copyright (C)" appears at the beginning of a line except that it
84 #      may be prefixed by any sequence (e.g., a comment) of no more than
85 #      5 characters.
86 #   3. The prefix of "Copyright (C)" is the same as the prefix on the
87 #      file's first occurrence of "Copyright (C)" that matches condition
88 #      #2.  Stated more simply, if something that looks like the start
89 #      of a copyright statement appears earlier than the FSF copyright
90 #      statement, the FSF copyright statement might not be recognized.
91 #      This condition might be removed in the future.
92 #   4. Iff a prefix is present before "Copyright (C)", the same prefix
93 #      appears at the beginning of each remaining line within the FSF
94 #      copyright statement.
95 #   5. Blank lines, even if preceded by the prefix, do not appear
96 #      within the FSF copyright statement.
97 #   6. Each copyright year is 2 or 4 digits, and years are separated by
98 #      commas or dashes.  Whitespace may occur after commas.
99
100 use strict;
101 use warnings;
102
103 my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
104 if (!$this_year || $this_year !~ m/^\d\d(\d\d)?$/) {
105   my ($sec, $min, $hour, $mday, $month, $year) = localtime (time());
106   $this_year = $year + 1900;
107 }
108 my $copyright = 'Copyright \([cC]\)';
109 my $holder = 'Free Software Foundation, Inc.';
110 my $prefix_max = 5;
111 my $margin = 72;
112 my $tab_width = 8;
113
114 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
115 my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
116
117 my $leading;
118 my $prefix;
119 my $ws;
120 my $old;
121 if (/(^|\n)(.{0,$prefix_max})$copyright/)
122   {
123     $leading = $1;
124     $prefix = $2;
125     $ws = '[ \t\r\f]'; # \s without \n
126     $ws = "(?:$ws*(?:$ws|\\n" . quotemeta($prefix) . ")$ws*)";
127     $holder =~ s/\s/$ws/g;
128     $old =
129       quotemeta("$leading$prefix") . "($copyright$ws"
130       . "(?:(?:\\d\\d)?\\d\\d(,$ws?|-))*"
131       . "((?:\\d\\d)?\\d\\d)$ws$holder)";
132   }
133 if (defined($old) && /$old/)
134   {
135     my $new = $1;
136     my $sep = $2 ? $2 : "";
137     my $last_year = $3;
138
139     # Handle two-digit year numbers like "98" and "99".
140     my $last_c_year = $last_year;
141     $last_c_year <= 99
142       and $last_c_year += 1900;
143
144     if ($last_c_year != $this_year)
145       {
146         # Update the year.
147         if ($sep eq '-' && $last_c_year + 1 == $this_year)
148           {
149             $new =~ s/$last_year/$this_year/;
150           }
151         elsif ($sep ne '-' && $last_c_year + 1 == $this_year)
152           {
153             $new =~ s/$last_year/$last_c_year-$this_year/;
154           }
155         else
156           {
157             $new =~ s/$last_year/$last_c_year, $this_year/;
158           }
159
160         # Normalize all whitespace including newline-prefix sequences.
161         $new =~ s/$ws/ /g;
162
163         # Put spaces after commas.
164         $new =~ s/, ?/, /g;
165
166         # Format within margin.
167         my $new_wrapped;
168         my $text_margin = $margin - length($prefix);
169         if ($prefix =~ /^(\t+)/) {
170           $text_margin -= length($1) * ($tab_width-1);
171         }
172         while (length($new))
173           {
174             if (($new =~ s/^(.{1,$text_margin})(?: |$)//)
175                 || ($new =~ s/^([\S]+)(?: |$)//))
176               {
177                 my $line = $1;
178                 $new_wrapped .= $new_wrapped ? $eol : $leading;
179                 $new_wrapped .= "$prefix$line";
180               }
181             else
182               {
183                 # Should be unreachable, but we don't want an infinite
184                 # loop if it can be reached.
185                 die;
186               }
187           }
188
189         # Replace the old copyright statement.
190         s/$old/$new_wrapped/;
191       }
192   }
193 else
194   {
195     print STDERR "$ARGV: warning: FSF copyright statement not found\n";
196   }
197
198 # Local variables:
199 # indent-tabs-mode: nil
200 # eval: (add-hook 'write-file-hooks 'time-stamp)
201 # time-stamp-start: "my $VERSION = '"
202 # time-stamp-format: "%:y-%02m-%02d.%02H:%02M"
203 # time-stamp-time-zone: "UTC"
204 # time-stamp-end: "'; # UTC"
205 # End: