[m-rev.] For review: automatically update copyright messages

Ralph Becket rafe at cs.mu.OZ.AU
Tue Nov 12 16:10:43 AEDT 2002


Fergus Henderson, Tuesday, 12 November 2002:
> On 12-Nov-2002, Ralph Becket <rafe at cs.mu.OZ.AU> wrote:
> > CVSROOT/check.pl:
> > 	Now updates the copyright message in Mercury code if necessary.
> 
> That looks OK to me, but if check.pl is going to modify the source,
> it would be nice for it to print out the changes (e.g. echoing
> the old and new lines, or renaming the old version and running diff)
> and ask the user if they still want to commit.
> The regexp substitions there do not look entirely 100% fool-proof.

I've tested the substitutions with:

Copyright (C) 1999 The University of Melbourne.
Copyright (C) 2002 The University of Melbourne.
Copyright (C) 1999-2002 The University of Melbourne.
Copyright (C) 1999-2001 The University of Melbourne.
Copyright (C) 1999-2000 The University of Melbourne.
Copyright (C) 1999-2000, 2001 The University of Melbourne.

and it all works fine.

Here's an interdiff that addresses your suggestion:

diff -u check.pl check.pl
--- check.pl	12 Nov 2002 01:57:04 -0000
+++ check.pl	12 Nov 2002 05:04:18 -0000
@@ -9,6 +9,8 @@
 
 #-----------------------------------------------------------------------------#
 
+use POSIX;
+
 # we want the files to have group mercury, which has group_id 62
 
 $wanted_group = 'mercury';
@@ -24,7 +26,7 @@
 
 #-----------------------------------------------------------------------------#
 
-sub query_force;
+sub query_user;
 sub check_copyright;
 
 #-----------------------------------------------------------------------------#
@@ -123,7 +125,7 @@
 				else
 				{
 					$line =~ s/-$last_year/-$this_year/;
-					$line =~ s/$last_year /$last_year-$this_year/;
+					$line =~ s/$last_year /$last_year-$this_year /;
 				}
 
 				$updated = 1;
@@ -140,24 +142,31 @@
 	close(ARG);
 
 	if ($updated) {
-		print "Updated the copyright message "
-			. "in file `$arg'\n";
-
-		open(ARG, ">$arg") or die "Error opening $arg for writing: $!\n";
+		$tmp = POSIX::tmpnam();
+		open(TMP, ">$tmp") or die "Error opening temporary file $tmp for writing: $!\n";
 		foreach $line (@lines)
 		{
-			print ARG $line;
+			print TMP $line;
+		}
+		close(TMP);
+
+		print "Updated the copyright message in file `$arg'\n";
+		print "Here is the diff:\n";
+		system("diff", "-u", $arg, $tmp);
+		if ( query_user("Accept the version with the updated copyright message?", 1) )
+		{
+			system("mv", $tmp, $arg);
 		}
-		close(ARG)
 	}
 
 	if (! $found) {
-		print "File `$arg' appears to have no "
-			. "copyright message\n";
 		if ($force) {
+			print "File `$arg' appears to have no "
+				. "copyright message\n";
 			print "Forcing commit anyway.\n";
 		} else {
-			if (query_force($arg)) {
+			if (query_user("File `$arg' appears to have no "
+				. "copyright message.  Commit anyway?", 0)) {
 				print "OK, if you insist!\n\n";
 			} else {
 				print "Will not commit.\n\n";
@@ -200,16 +209,25 @@
 }
 
 
-sub query_force {
+sub query_user {
+	my $query_string = shift(@_);
+	my $default_yes = shift(@_);
+	my $default_string;
+	my $response;
+
 	if (open TTY, "/dev/tty") {
-		print "Commit anyway? [n] ";
-		if (<TTY> =~ /^y/i) {
-			return 1;
-		} else {
-			return 0;
-		}
+
+		if ( $default_yes )
+		{ $default_string = "[y]" }
+		else
+		{ $default_string = "[n]" }
+
+		print $query_string . " " . $default_string . " ";
+		chomp($response = <TTY>);
+		return 1 if ( $response =~ /^y/i );
+		return 0 if ( $response =~ /^n/i );
+		return $default_yes
 	} else {
-		my $file = shift(@_);
 		my $result;
 		my $have_X_display = $ENV{DISPLAY} ne "";
 
@@ -246,8 +264,12 @@
 		# redirects X connections back to their origin.
 		#
 
-		$result = system "xmessage", "-buttons", "yes:0,no:1",
-				"Problem with `$file'.  Commit anyway?";
+		if ( $default_yes )
+		{ $default_string = "-default yes" }
+		else
+		{ $default_string = "-default no" }
+
+		$result = system("xmessage", "-buttons", "yes:0,no:1", $default_string, $query_string);
 		$errcode = $result & 0xff;
 		$exitval = $result >> 8;
 		if ($errcode == 0 && ($exitval == 0 || $exitval == 1)) {
@@ -260,7 +282,7 @@
 	}
 
 	print "\nOops: check.pl: "
-		. "query_force reached end without returning a result\n";
+		. "query_user reached end without returning a result\n";
 
 	return 0;
 }
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list