]> Savannah Git Hosting - gnulib.git/commitdiff
update-copyright: handle more cases
authorJim Meyering <meyering@meta.com>
Mon, 15 Jan 2024 18:36:34 +0000 (10:36 -0800)
committerJim Meyering <meyering@meta.com>
Mon, 15 Jan 2024 20:01:31 +0000 (12:01 -0800)
* build-aux/update-copyright: Handle cases like gunzip.in
with its two copyright lines.
Making it work required two changes: don't stop upon first match.
And then, when continuing, a little trickiness: knowing that the
final substitution would reset m{}g's "pos()", so save and restore
that, to avoid an infinite loop in some cases.
* tests/test-update-copyright.sh: Add a test for this.

ChangeLog
build-aux/update-copyright
tests/test-update-copyright.sh

index d3a7f4cb3a0dc88cf43348976b4c07000b5b021a..e42d2c31abf0513ec3ad1759b130fd307e0571b9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-01-07  Jim Meyering  <meyering@meta.com>
+
+       update-copyright: handle more cases
+       * build-aux/update-copyright: Handle cases like gunzip.in
+       with its two copyright lines.
+       Making it work required two changes: don't stop upon first match.
+       And then, when continuing, a little trickiness: knowing that the
+       final substitution would reset m{}g's "pos()", so save and restore
+       that, to avoid an infinite loop in some cases.
+       * tests/test-update-copyright.sh: Add a test for this.
+
 2024-01-15  Paul Eggert  <eggert@cs.ucla.edu>
 
        test-framework-sh: fix spelling typo
index 50eccfe3cc6f9c9477883d68e539ffa53081d61a..ea3e46fe60fc3be9c2be2a2f08cb19b8762fd90e 100755 (executable)
 eval 'exec perl -wSx -0777 -pi "$0" "$@"'
      if 0;
 
-my $VERSION = '2024-01-07.04:02'; # UTC
+my $VERSION = '2024-01-15.18:30'; # UTC
 # The definition above must lie within the first 8 lines in order
 # for the Emacs time-stamp write hook (at end) to update it.
 # If you change this file with Emacs, please let the write hook
@@ -170,14 +170,13 @@ if (!$this_year || $this_year !~ m/^\d{4}$/)
 # 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_re;
 my $stmt_re;
-while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
+my $found;
+while (/(^|\n)(.{0,$prefix_max})$copyright_re/cg)
   {
-    $leading = "$1$2";
-    $prefix = $2;
+    my $pos=pos();
+    my $leading = "$1$2";
+    my $prefix = $2;
     if ($prefix =~ /^(\s*\/)\*(\s*)$/)
       {
         $prefix =~ s,/, ,;
@@ -188,7 +187,7 @@ while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
             $prefix = $prefix_ws;
           }
       }
-    $ws_re = '[ \t\r\f]'; # \s without \n
+    my $ws_re = '[ \t\r\f]'; # \s without \n
     $ws_re =
       "(?:$ws_re*(?:$ws_re|\\n" . quotemeta($prefix) . ")$ws_re*)";
     my $holder_re = $holder;
@@ -199,96 +198,97 @@ while (/(^|\n)(.{0,$prefix_max})$copyright_re/g)
       . "((?:\\d\\d)?\\d\\d)$ws_re$holder_re";
     if (/\G$stmt_remainder_re/)
       {
+        $found = 1;
         $stmt_re =
           quotemeta($leading) . "($copyright_re$stmt_remainder_re)";
-        last;
-      }
-  }
-if (defined $stmt_re)
-  {
-    /$stmt_re/ or die; # Should never die.
-    my $stmt = $1;
-    my $final_year_orig = $2;
 
-    # Handle two-digit year numbers like "98" and "99".
-    my $final_year = $final_year_orig;
-    $final_year <= 99
-      and $final_year += 1900;
+        /$stmt_re/ or die; # Should never die.
+        my $stmt = $1;
+        my $final_year_orig = $2;
 
-    if ($final_year != $this_year)
-      {
-        # Update the year.
-        $stmt =~ s/(^|[^\d])$final_year_orig\b/$1$final_year, $this_year/;
-      }
-    if ($final_year != $this_year || $ENV{'UPDATE_COPYRIGHT_FORCE'})
-      {
-        # Normalize all whitespace including newline-prefix sequences.
-        $stmt =~ s/$ws_re/ /g;
+        # Handle two-digit year numbers like "98" and "99".
+        my $final_year = $final_year_orig;
+        $final_year <= 99
+          and $final_year += 1900;
 
-        # Put spaces after commas.
-        $stmt =~ s/, ?/, /g;
-
-        # Convert 2-digit to 4-digit years.
-        $stmt =~ s/(\b\d\d\b)/19$1/g;
-
-        # Make the use of intervals consistent.
-        if (!$ENV{UPDATE_COPYRIGHT_USE_INTERVALS})
+        if ($final_year != $this_year)
           {
-            $stmt =~ s/(\d{4})$ndash_re(\d{4})/join(', ', $1..$2)/eg;
+            # Update the year.
+            $stmt =~ s/(^|[^\d])$final_year_orig\b/$1$final_year, $this_year/;
           }
-        else
+        if ($final_year != $this_year || $ENV{'UPDATE_COPYRIGHT_FORCE'})
           {
-            my $ndash = ($ARGV =~ /\.tex(i(nfo)?)?$/ ? "--"
-                         : $ARGV =~ /\.(\d[a-z]*|man)$/ ? "\\(en"
-                         : "-");
+            # Normalize all whitespace including newline-prefix sequences.
+            $stmt =~ s/$ws_re/ /g;
 
-            $stmt =~
-              s/
-                (\d{4})
-                (?:
-                  (,\ |$ndash_re)
-                  ((??{
-                    if   ($2 ne ', ') { '\d{4}'; }
-                    elsif (!$3)       { $1 + 1;  }
-                    else              { $3 + 1;  }
-                  }))
-                )+
-              /$1$ndash$3/gx;
+            # Put spaces after commas.
+            $stmt =~ s/, ?/, /g;
 
-            # When it's 2, emit a single range encompassing all year numbers.
-            $ENV{UPDATE_COPYRIGHT_USE_INTERVALS} == 2
-              and $stmt =~ s/(^|[^\d])(\d{4})\b.*(?:[^\d])(\d{4})\b/$1$2$ndash$3/;
-          }
+            # Convert 2-digit to 4-digit years.
+            $stmt =~ s/(\b\d\d\b)/19$1/g;
 
-        # Format within margin.
-        my $stmt_wrapped;
-        my $text_margin = $margin - length($prefix);
-        if ($prefix =~ /^(\t+)/)
-          {
-            $text_margin -= length($1) * ($tab_width - 1);
-          }
-        while (length $stmt)
-          {
-            if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
-                || ($stmt =~ s/^([\S]+)(?: |$)//))
+            # Make the use of intervals consistent.
+            if (!$ENV{UPDATE_COPYRIGHT_USE_INTERVALS})
               {
-                my $line = $1;
-                $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
-                $stmt_wrapped .= $line;
+                $stmt =~ s/(\d{4})$ndash_re(\d{4})/join(', ', $1..$2)/eg;
               }
             else
               {
-                # Should be unreachable, but we don't want an infinite
-                # loop if it can be reached.
-                die;
+                my $ndash = ($ARGV =~ /\.tex(i(nfo)?)?$/ ? "--"
+                             : $ARGV =~ /\.(\d[a-z]*|man)$/ ? "\\(en"
+                             : "-");
+
+                $stmt =~
+                  s/
+                    (\d{4})
+                    (?:
+                      (,\ |$ndash_re)
+                      ((??{
+                        if   ($2 ne ', ') { '\d{4}'; }
+                        elsif (!$3)       { $1 + 1;  }
+                        else              { $3 + 1;  }
+                      }))
+                    )+
+                  /$1$ndash$3/gx;
+
+                # When it's 2, emit a single range encompassing all year numbers.
+                $ENV{UPDATE_COPYRIGHT_USE_INTERVALS} == 2
+                  and $stmt =~ s/(^|[^\d])(\d{4})\b.*(?:[^\d])(\d{4})\b/$1$2$ndash$3/;
               }
-          }
 
-        # Replace the old copyright statement.
-        s/$stmt_re/$stmt_wrapped/g;
+            # Format within margin.
+            my $stmt_wrapped;
+            my $text_margin = $margin - length($prefix);
+            if ($prefix =~ /^(\t+)/)
+              {
+                $text_margin -= length($1) * ($tab_width - 1);
+              }
+            while (length $stmt)
+              {
+                if (($stmt =~ s/^(.{1,$text_margin})(?: |$)//)
+                    || ($stmt =~ s/^([\S]+)(?: |$)//))
+                  {
+                    my $line = $1;
+                    $stmt_wrapped .= $stmt_wrapped ? "$eol$prefix" : $leading;
+                    $stmt_wrapped .= $line;
+                  }
+                else
+                  {
+                    # Should be unreachable, but we don't want an infinite
+                    # loop if it can be reached.
+                    die;
+                  }
+              }
+
+            # Replace the old copyright statement.
+            my $p = pos();
+            s/$stmt_re/$stmt_wrapped/g;
+            pos() = $p;
+          }
       }
   }
-else
+
+if (!$found)
   {
     print STDERR "$ARGV: warning: copyright statement not found\n";
   }
index b2a7f2df8a77641e91eecb227afdb739081572c1..329b5f7381408cdd7f24e4e94c92330618c699fd 100755 (executable)
@@ -122,6 +122,13 @@ Copy$ight (C) 1990-2005, 2007-2009 Acme, Inc.
 EOF
 cat > $TMP-8 <<EOF
 Copy$ight (C) 2008 Free Software Foundation, Inc.
+Copy$ight (C) 2008 Free Software Foundation, Inc.
+EOF
+cat > $TMP-9 <<EOF
+# Copy$ight (C) 2008 Free Software Foundation, Inc.
+
+Copy$ight (C) 2007 Free Software Foundation, Inc.
+
 Copy$ight (C) 2008 Free Software Foundation, Inc.
 EOF
 
@@ -171,6 +178,20 @@ Copy$ight (C) 2008, 2009 Free Software Foundation, Inc.
 Copy$ight (C) 2008, 2009 Free Software Foundation, Inc.
 EOF
 
+# Note that expecting "2007, 2009" on the third copyright line below
+# looks wrong since the corresponding copyright year was "2008".
+# However, the premise of this test (having inconsistent copyright dates
+# with the same holder) is not worth handling. What happens is that the
+# xform for the preceding line matches any list of year numbers with the
+# same prefix and holder.
+compare - $TMP-9 <<EOF || exit 1
+# Copy$ight (C) 2008, 2009 Free Software Foundation, Inc.
+
+Copy$ight (C) 2007, 2009 Free Software Foundation, Inc.
+
+Copy$ight (C) 2007, 2009 Free Software Foundation, Inc.
+EOF
+
 UPDATE_COPYRIGHT_YEAR=2010 UPDATE_COPYRIGHT_USE_INTERVALS=1 \
   update-copyright $TMP-? 1> $TMP-stdout 2> $TMP-stderr
 compare /dev/null $TMP-stdout || exit 1