bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] update-copyright: don't trip on non-FSF copyright statements


From: Joel E. Denny
Subject: [PATCH] update-copyright: don't trip on non-FSF copyright statements
Date: Wed, 5 Aug 2009 17:38:02 -0400 (EDT)
User-agent: Alpine 1.00 (DEB 882 2007-12-20)

These patches make update-copyright functionality a little easier to 
understand and a little more powerful.  Once these are applied, I will 
feel better about writing a patch that allows "(C)" to be omitted.

>From 61c92eb1ecb5b23c667e026cdb72cce1eaf6f847 Mon Sep 17 00:00:00 2001
From: Joel E. Denny <address@hidden>
Date: Wed, 5 Aug 2009 15:31:15 -0400
Subject: [PATCH] update-copyright: clean up code a little

* build-aux/update-copyright: Append "_re" to the name of any
variable holding a regular expression.
Replace "old" and "new" with "stmt" in variable names.
Do not accept 2-digit UPDATE_COPYRIGHT_YEAR, which was not
handled correctly.
Format code more consistently.
---
 ChangeLog                  |   10 ++++++
 build-aux/update-copyright |   71 +++++++++++++++++++++++--------------------
 2 files changed, 48 insertions(+), 33 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 225df2e..b0cb2cf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
 2009-08-05  Joel E. Denny  <address@hidden>
 
+       update-copyright: clean up code a little
+       * build-aux/update-copyright: Append "_re" to the name of any
+       variable holding a regular expression.
+       Replace "old" and "new" with "stmt" in variable names.
+       Do not accept 2-digit UPDATE_COPYRIGHT_YEAR, which was not
+       handled correctly.
+       Format code more consistently.
+
+2009-08-05  Joel E. Denny  <address@hidden>
+
        update-copyright-tests: improve portability
        * tests/test-update-copyright.sh: Use cmp if diff cannot handle
        -u or /dev/null.  Suggested by Jim Meyering and Eric Blake.
diff --git a/build-aux/update-copyright b/build-aux/update-copyright
index b14dc50..48f44e4 100755
--- a/build-aux/update-copyright
+++ b/build-aux/update-copyright
@@ -101,39 +101,43 @@ my $VERSION = '2009-08-04.07:25'; # UTC
 use strict;
 use warnings;
 
-my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
-if (!$this_year || $this_year !~ m/^\d\d(\d\d)?$/) {
-  my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
-  $this_year = $year + 1900;
-}
-my $copyright = 'Copyright (?:\([cC]\)|@copyright{}|&copy;)';
+my $copyright_re = 'Copyright (?:\([cC]\)|@copyright{}|&copy;)';
 my $holder = 'Free Software Foundation, Inc.';
 my $prefix_max = 5;
 my $margin = 72;
 my $tab_width = 8;
 
+my $this_year = $ENV{UPDATE_COPYRIGHT_YEAR};
+if (!$this_year || $this_year !~ m/^\d{4}$/)
+  {
+    my ($sec, $min, $hour, $mday, $month, $year) = localtime (time ());
+    $this_year = $year + 1900;
+  }
+
 # Unless the file consistently uses "\r\n" as the EOL, use "\n" instead.
 my $eol = /(?:^|[^\r])\n/ ? "\n" : "\r\n";
 
 my $leading;
 my $prefix;
-my $ws;
-my $old_re;
-if (/(^|\n)(.{0,$prefix_max})$copyright/)
+my $ws_re;
+my $stmt_re;
+if (/(^|\n)(.{0,$prefix_max})$copyright_re/)
   {
     $leading = $1;
     $prefix = $2;
-    $ws = '[ \t\r\f]'; # \s without \n
-    $ws = "(?:$ws*(?:$ws|\\n" . quotemeta($prefix) . ")$ws*)";
-    $holder =~ s/\s/$ws/g;
-    $old_re =
-      quotemeta("$leading$prefix") . "($copyright$ws"
-      . "(?:(?:\\d\\d)?\\d\\d(,$ws?|-))*"
-      . "((?:\\d\\d)?\\d\\d)$ws$holder)";
+    $ws_re = '[ \t\r\f]'; # \s without \n
+    $ws_re =
+      "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
+    my $holder_re = $holder;
+    $holder_re =~ s/\s/$ws_re/g;
+    $stmt_re =
+      quotemeta("$leading$prefix") . "($copyright_re$ws_re"
+      . "(?:(?:\\d\\d)?\\d\\d(,$ws_re?|-))*"
+      . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re)";
   }
-if (defined $old_re && /$old_re/)
+if (defined $stmt_re && /$stmt_re/)
   {
-    my $new = $1;
+    my $stmt = $1;
     my $sep = $2 ? $2 : "";
     my $final_year_orig = $3;
 
@@ -147,37 +151,38 @@ if (defined $old_re && /$old_re/)
         # Update the year.
         if ($sep eq '-' && $final_year + 1 == $this_year)
           {
-            $new =~ s/$final_year_orig/$this_year/;
+            $stmt =~ s/$final_year_orig/$this_year/;
           }
         elsif ($sep ne '-' && $final_year + 1 == $this_year)
           {
-            $new =~ s/$final_year_orig/$final_year-$this_year/;
+            $stmt =~ s/$final_year_orig/$final_year-$this_year/;
           }
         else
           {
-            $new =~ s/$final_year_orig/$final_year, $this_year/;
+            $stmt =~ s/$final_year_orig/$final_year, $this_year/;
           }
 
         # Normalize all whitespace including newline-prefix sequences.
-        $new =~ s/$ws/ /g;
+        $stmt =~ s/$ws_re/ /g;
 
         # Put spaces after commas.
-        $new =~ s/, ?/, /g;
+        $stmt =~ s/, ?/, /g;
 
         # Format within margin.
-        my $new_wrapped;
+        my $stmt_wrapped;
         my $text_margin = $margin - length($prefix);
-        if ($prefix =~ /^(\t+)/) {
-          $text_margin -= length($1) * ($tab_width - 1);
-        }
-        while (length $new)
+        if ($prefix =~ /^(\t+)/)
+          {
+            $text_margin -= length($1) * ($tab_width - 1);
+          }
+        while (length $stmt)
           {
-            if (($new =~ s/^(.{1,$text_margin})(?: |$)//)
-                || ($new =~ s/^([\S]+)(?: |$)//))
+            if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
+                || ($stmt =~ s/^([\S]+)(?: |$)//))
               {
                 my $line = $1;
-                $new_wrapped .= $new_wrapped ? $eol : $leading;
-                $new_wrapped .= "$prefix$line";
+                $stmt_wrapped .= $stmt_wrapped ? $eol : $leading;
+                $stmt_wrapped .= "$prefix$line";
               }
             else
               {
@@ -188,7 +193,7 @@ if (defined $old_re && /$old_re/)
           }
 
         # Replace the old copyright statement.
-        s/$old_re/$new_wrapped/;
+        s/$stmt_re/$stmt_wrapped/;
       }
   }
 else
-- 
1.5.4.3


>From 3dadfc2fb1f8468014dd14d5eafc9fb7df270a9d Mon Sep 17 00:00:00 2001
From: Joel E. Denny <address@hidden>
Date: Wed, 5 Aug 2009 16:52:24 -0400
Subject: [PATCH] update-copyright: don't trip on non-FSF copyright statements

* build-aux/update-copyright: Fix so that the first correctly
formatted FSF copyright statement is recognized no matter what
appears before it.  Update documentation.
* tests/test-update-copyright.sh: Test that.
---
 ChangeLog                      |    8 +++++
 build-aux/update-copyright     |   61 +++++++++++++++++++--------------------
 tests/test-update-copyright.sh |   33 +++++++++++++++++----
 3 files changed, 64 insertions(+), 38 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index b0cb2cf..996baca 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2009-08-05  Joel E. Denny  <address@hidden>
 
+       update-copyright: don't trip on non-FSF copyright statements
+       * build-aux/update-copyright: Fix so that the first correctly
+       formatted FSF copyright statement is recognized no matter what
+       appears before it.  Update documentation.
+       * tests/test-update-copyright.sh: Test that.
+
+2009-08-05  Joel E. Denny  <address@hidden>
+
        update-copyright: clean up code a little
        * build-aux/update-copyright: Append "_re" to the name of any
        variable holding a regular expression.
diff --git a/build-aux/update-copyright b/build-aux/update-copyright
index 48f44e4..dd0d758 100755
--- a/build-aux/update-copyright
+++ b/build-aux/update-copyright
@@ -1,9 +1,9 @@
 #!/usr/bin/perl -0777 -pi
 # Update an FSF copyright year list to include the current year.
 
-my $VERSION = '2009-08-04.07:25'; # UTC
+my $VERSION = '2009-08-05.20:47'; # UTC
 
-# Copyright (C) 2009 Free Software Foundation
+# Copyright (C) 2009 Free Software Foundation, Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -42,9 +42,7 @@ my $VERSION = '2009-08-04.07:25'; # UTC
 # for every file for which no FSF copyright statement is discovered.
 #
 # Each file's FSF copyright statement must be formated correctly in
-# order to be recognized, and it must appear before other text that
-# looks like the start of a copyright statement.  For example, each of
-# these by itself is fine:
+# order to be recognized.  For example, each of these is fine:
 #
 #   Copyright @copyright{} 1990-2005, 2007-2009 Free Software
 #   Foundation, Inc.
@@ -68,35 +66,29 @@ my $VERSION = '2009-08-04.07:25'; # UTC
 #
 #   Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
 #
-# Moreover, any FSF copyright statement following either of the previous
-# copyright statements might not be recognized.
+# However, any correctly formatted FSF copyright statement following
+# either of the previous two copyright statements would be recognized.
 #
 # The exact conditions that a file's FSF copyright statement must meet
-# to be recognized are listed below.  They may seem slightly complex,
-# but you need not worry if some file in your project accidentally
-# breaks one.  The worst that can happen is that a file is not updated
-# and a warning is issued.
+# to be recognized are:
 #
 #   1. The format is "Copyright (C)" (where "(C)" can also be "(c)",
 #      "@copyright{}", or "&copy;"), then a list of copyright years, and
 #      then the name of the copyright holder, which is "Free Software
 #      Foundation, Inc.".
-#   2. "Copyright (C)" appears at the beginning of a line except that it
-#      may be prefixed by any sequence (e.g., a comment) of no more than
-#      5 characters.
-#   3. The prefix of "Copyright (C)" is the same as the prefix on the
-#      file's first occurrence of "Copyright (C)" that matches condition
-#      #2.  Stated more simply, if something that looks like the start
-#      of a copyright statement appears earlier than the FSF copyright
-#      statement, the FSF copyright statement might not be recognized.
-#      This condition might be removed in the future.
-#   4. Iff a prefix is present before "Copyright (C)", the same prefix
-#      appears at the beginning of each remaining line within the FSF
-#      copyright statement.
-#   5. Blank lines, even if preceded by the prefix, do not appear
+#   2. The "Copyright (C)" appears at the beginning of a line except
+#      that it may be prefixed by any sequence (e.g., a comment) of no
+#      more than 5 characters.
+#   3. Iff such a prefix is present, the same prefix appears at the
+#      beginning of each remaining line within the FSF copyright
+#      statement.
+#   4. Blank lines, even if preceded by the prefix, do not appear
 #      within the FSF copyright statement.
-#   6. Each copyright year is 2 or 4 digits, and years are separated by
+#   5. Each copyright year is 2 or 4 digits, and years are separated by
 #      commas or dashes.  Whitespace may occur after commas.
+#   6. It is the first FSF copyright statement that meets all of the
+#      above conditions.  Subsequent FSF copyright statements are
+#      ignored.
 
 use strict;
 use warnings;
@@ -121,7 +113,7 @@ my $leading;
 my $prefix;
 my $ws_re;
 my $stmt_re;
-if (/(^|\n)(.{0,$prefix_max})$copyright_re/)
+while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
   {
     $leading = $1;
     $prefix = $2;
@@ -130,13 +122,20 @@ if (/(^|\n)(.{0,$prefix_max})$copyright_re/)
       "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
     my $holder_re = $holder;
     $holder_re =~ s/\s/$ws_re/g;
-    $stmt_re =
-      quotemeta("$leading$prefix") . "($copyright_re$ws_re"
-      . "(?:(?:\\d\\d)?\\d\\d(,$ws_re?|-))*"
-      . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re)";
+    my $stmt_remainder_re =
+      "$ws_re(?:(?:\\d\\d)?\\d\\d(,$ws_re?|-))*"
+      . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
+    if (/\G$stmt_remainder_re/)
+      {
+        $stmt_re =
+           quotemeta("$leading$prefix")
+           . "($copyright_re$stmt_remainder_re)";
+        last;
+      }
   }
-if (defined $stmt_re && /$stmt_re/)
+if (defined $stmt_re)
   {
+    /$stmt_re/ or die; # Should never die.
     my $stmt = $1;
     my $sep = $2 ? $2 : "";
     my $final_year_orig = $3;
diff --git a/tests/test-update-copyright.sh b/tests/test-update-copyright.sh
index 3d157ad..0f5de4b 100755
--- a/tests/test-update-copyright.sh
+++ b/tests/test-update-copyright.sh
@@ -47,11 +47,18 @@ EOF
 cat > $TMP.4 <<EOF
 /* Copyright (C) 1990-2005, 2007-2009 Free Software
  * Foundation, Inc.  */
+EOF
+cat > $TMP.5 <<EOF
+Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
+EOF
+cat > $TMP.6 <<EOF
+/* Copyright (C) 1990-2005, 2007-2009 Free Software
+ * Foundation, Inc.  */
 
 Copyright (C) 1990-2005, 2007-2009 Free Software Foundation,
 Inc.
 EOF
-cat > $TMP.5 <<EOF
+cat > $TMP.7 <<EOF
 Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
 
 # Copyright (C) 1990-2005, 2007-2009 Free Software
@@ -82,11 +89,18 @@ EOF
 compare - $TMP.4 <<EOF || exit 1
 /* Copyright (C) 1990-2005, 2007-2009 Free Software
  * Foundation, Inc.  */
+EOF
+compare - $TMP.5 <<EOF || exit 1
+Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
+EOF
+compare - $TMP.6 <<EOF || exit 1
+/* Copyright (C) 1990-2005, 2007-2009 Free Software
+ * Foundation, Inc.  */
 
 Copyright (C) 1990-2005, 2007-2009 Free Software Foundation,
 Inc.
 EOF
-compare - $TMP.5 <<EOF || exit 1
+compare - $TMP.7 <<EOF || exit 1
 Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
 
 # Copyright (C) 1990-2005, 2007-2009 Free Software
@@ -115,15 +129,20 @@ EOF
 compare - $TMP.4 <<EOF || exit 1
 /* Copyright (C) 1990-2005, 2007-2009 Free Software
  * Foundation, Inc.  */
-
-Copyright (C) 1990-2005, 2007-2009 Free Software Foundation,
-Inc.
 EOF
 compare - $TMP.5 <<EOF || exit 1
 Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
+EOF
+compare - $TMP.6 <<EOF || exit 1
+/* Copyright (C) 1990-2005, 2007-2009 Free Software
+ * Foundation, Inc.  */
 
-# Copyright (C) 1990-2005, 2007-2009 Free Software
-# Foundation, Inc.
+Copyright (C) 1990-2005, 2007-2010 Free Software Foundation, Inc.
+EOF
+compare - $TMP.7 <<EOF || exit 1
+Copyright (C) 1990-2005, 2007-2009 Acme, Inc.
+
+# Copyright (C) 1990-2005, 2007-2010 Free Software Foundation, Inc.
 EOF
 
 rm $TMP*
-- 
1.5.4.3





reply via email to

[Prev in Thread] Current Thread [Next in Thread]