trim_changelog 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #! /usr/bin/perl -w
  2. # Try to format ChangeLog in the current directory.
  3. # Remove unnecessary spaces. Add spaces when needed.
  4. use strict;
  5. my $progname = "trim_changelog";
  6. # Print message and exit (like "die", but without raising an exception).
  7. # System error is added at the end.
  8. sub error ($)
  9. {
  10. print STDERR "$progname: ERROR: " . shift(@_) . ": $!\n";
  11. exit 1;
  12. }
  13. if ($#ARGV != -1) {
  14. print STDERR "$progname accepts no arguments\n";
  15. exit 1;
  16. }
  17. open (CHANGELOG, "< ChangeLog") || error ("cannot open ChangeLog");
  18. open (CHNEW, "> ChangeLog.new") || error ("cannot open ChangeLog.new");
  19. # Convert initial spaces in ChangeLog to tabs.
  20. while (<CHANGELOG>) {
  21. # Trim trailing whitespace.
  22. s/\s*$//;
  23. # Make sure there are exactly 2 spaces before the e-mail.
  24. s/(^\w.*[^\s])\s+</$1 </;
  25. # Make sure there are exactly 2 spaces after numeric dates.
  26. s/(^[0-9]+-[0-9-]+)\s+/$1 /;
  27. # Up to 7 spaces and tab or up to 8 spaces -> tab.
  28. if (/^( {1,7}\t| {1,8})(.*)/) {
  29. $_ = "\t$2";
  30. }
  31. print CHNEW "$_\n";
  32. }
  33. close (CHANGELOG);
  34. close (CHNEW);
  35. system ("cmp ChangeLog ChangeLog.new >/dev/null 2>&1");
  36. if (($? >> 8) == 0) {
  37. unlink ("ChangeLog.new") || error ("Cannot remove ChangeLog.new");
  38. print "$progname: ChangeLog has not been changed\n";
  39. exit 0;
  40. }
  41. rename ("ChangeLog", "ChangeLog.bak") ||
  42. error ("Cannot rename ChangeLog to ChangeLog.bak");
  43. rename ("ChangeLog.new", "ChangeLog") ||
  44. error ("Cannot rename ChangeLog.new to ChangeLog");
  45. print "$progname: ChangeLog has been changed\n";