Prechádzať zdrojové kódy

Dpkg::Changelog::Debian: keep blank lines and trailing whitespace unchanged

In order to be able to output the very same changelog file, we want to
keep whitespaces while parsing and simply drop them in outputs where
they should not appear.

This commit is a good start towards this goal.
Raphaël Hertzog 17 rokov pred
rodič
commit
c8fc6e9315
2 zmenil súbory, kde vykonal 22 pridanie a 15 odobranie
  1. 3 1
      scripts/Dpkg/Changelog.pm
  2. 19 14
      scripts/Dpkg/Changelog/Debian.pm

+ 3 - 1
scripts/Dpkg/Changelog.pm

@@ -772,9 +772,11 @@ in the output format of C<dpkg-parsechangelog>.
 
 
 sub get_dpkg_changes {
 sub get_dpkg_changes {
     my $entry = shift;
     my $entry = shift;
+    $entry->{Header} =~ s/\s+$// if defined $entry->{Header};
     my $changes = "\n " . ($entry->{Header} || '') . "\n .\n";
     my $changes = "\n " . ($entry->{Header} || '') . "\n .\n";
     foreach my $line (@{$entry->{Changes}}) {
     foreach my $line (@{$entry->{Changes}}) {
-	if ($line =~ /^\s*$/) {
+	$line =~ s/\s+$//;
+	if ($line eq "") {
 	    $changes .= " .\n";
 	    $changes .= " .\n";
 	} else {
 	} else {
 	    $changes .= " $line\n";
 	    $changes .= " $line\n";

+ 19 - 14
scripts/Dpkg/Changelog/Debian.pm

@@ -128,11 +128,11 @@ sub parse {
 # based on /usr/lib/dpkg/parsechangelog/debian
 # based on /usr/lib/dpkg/parsechangelog/debian
     my $expect='first heading';
     my $expect='first heading';
     my $entry = new Dpkg::Changelog::Entry;
     my $entry = new Dpkg::Changelog::Entry;
-    my $blanklines = 0;
+    my @blanklines = ();
     my $unknowncounter = 1; # to make version unique, e.g. for using as id
     my $unknowncounter = 1; # to make version unique, e.g. for using as id
 
 
     while (<$fh>) {
     while (<$fh>) {
-	s/\s*\n$//;
+	chomp;
 #	printf(STDERR "%-39.39s %-39.39s\n",$expect,$_);
 #	printf(STDERR "%-39.39s %-39.39s\n",$expect,$_);
 	my $name_chars = qr/[-+0-9a-z.]/i;
 	my $name_chars = qr/[-+0-9a-z.]/i;
 	if (m/^(\w$name_chars*) \(([^\(\) \t]+)\)((\s+$name_chars+)+)\;/i) {
 	if (m/^(\w$name_chars*) \(([^\(\) \t]+)\)((\s+$name_chars+)+)\;/i) {
@@ -189,7 +189,7 @@ sub parse {
 		}
 		}
 	    }
 	    }
 	    $expect= 'start of change data';
 	    $expect= 'start of change data';
-	    $blanklines = 0;
+	    @blanklines = ();
 	} elsif (m/^(;;\s*)?Local variables:/io) {
 	} elsif (m/^(;;\s*)?Local variables:/io) {
 	    last; # skip Emacs variables at end of file
 	    last; # skip Emacs variables at end of file
 	} elsif (m/^vim:/io) {
 	} elsif (m/^vim:/io) {
@@ -205,9 +205,9 @@ sub parse {
 		 || m/^(\w[-+0-9a-z.]*) \(([^\(\) \t]+)\)\;?/io
 		 || m/^(\w[-+0-9a-z.]*) \(([^\(\) \t]+)\)\;?/io
 		 || m/^([\w.+-]+)(-| )(\S+) Debian (\S+)/io
 		 || m/^([\w.+-]+)(-| )(\S+) Debian (\S+)/io
 		 || m/^Changes from version (.*) to (.*):/io
 		 || m/^Changes from version (.*) to (.*):/io
-		 || m/^Changes for [\w.+-]+-[\w.+-]+:?$/io
-		 || m/^Old Changelog:$/io
-		 || m/^(?:\d+:)?\w[\w.+~-]*:?$/o) {
+		 || m/^Changes for [\w.+-]+-[\w.+-]+:?\s*$/io
+		 || m/^Old Changelog:\s*$/io
+		 || m/^(?:\d+:)?\w[\w.+~-]*:?\s*$/o) {
 	    # save entries on old changelog format verbatim
 	    # save entries on old changelog format verbatim
 	    # we assume the rest of the file will be in old format once we
 	    # we assume the rest of the file will be in old format once we
 	    # hit it for the first time
 	    # hit it for the first time
@@ -216,7 +216,7 @@ sub parse {
 	} elsif (m/^\S/) {
 	} elsif (m/^\S/) {
 	    $self->_do_parse_error($file, $NR,
 	    $self->_do_parse_error($file, $NR,
 				  _g("badly formatted heading line"), "$_");
 				  _g("badly formatted heading line"), "$_");
-	} elsif (m/^ \-\- (.*) <(.*)>(  ?)((\w+\,\s*)?\d{1,2}\s+\w+\s+\d{4}\s+\d{1,2}:\d\d:\d\d\s+[-+]\d{4}(\s+\([^\\\(\)]\))?)$/o) {
+	} elsif (m/^ \-\- (.*) <(.*)>(  ?)((\w+\,\s*)?\d{1,2}\s+\w+\s+\d{4}\s+\d{1,2}:\d\d:\d\d\s+[-+]\d{4}(\s+\([^\\\(\)]\))?)\s*$/o) {
 	    $expect eq 'more change data or trailer' ||
 	    $expect eq 'more change data or trailer' ||
 		$self->_do_parse_error($file, $NR,
 		$self->_do_parse_error($file, $NR,
 				       sprintf(_g("found trailer where expected %s"),
 				       sprintf(_g("found trailer where expected %s"),
@@ -269,14 +269,16 @@ sub parse {
 		    }
 		    }
 		};
 		};
 	    # Keep raw changes
 	    # Keep raw changes
-	    push @{$entry->{'Changes'}}, ("") x $blanklines, $_;
+	    push @{$entry->{'Changes'}}, @blanklines, $_;
 	    if (!$entry->{'Items'} || ($1 eq '*')) {
 	    if (!$entry->{'Items'} || ($1 eq '*')) {
 		$entry->{'Items'} ||= [];
 		$entry->{'Items'} ||= [];
 		push @{$entry->{'Items'}}, "$_\n";
 		push @{$entry->{'Items'}}, "$_\n";
 	    } else {
 	    } else {
-		$entry->{'Items'}[-1] .= (" .\n" x $blanklines)." $_\n";
+		my $blank = '';
+		$blank = join("\n", @blanklines) . "\n" if scalar @blanklines;
+		$entry->{'Items'}[-1] .= "$blank$_\n";
 	    }
 	    }
-	    $blanklines = 0;
+	    @blanklines = ();
 	    $expect = 'more change data or trailer';
 	    $expect = 'more change data or trailer';
 	} elsif (!m/\S/) {
 	} elsif (!m/\S/) {
 	    next if $expect eq 'start of change data'
 	    next if $expect eq 'start of change data'
@@ -285,7 +287,7 @@ sub parse {
 		|| $self->_do_parse_error($file, $NR,
 		|| $self->_do_parse_error($file, $NR,
 					  sprintf(_g("found blank line where expected %s"),
 					  sprintf(_g("found blank line where expected %s"),
 						  $expect));
 						  $expect));
-	    $blanklines++;
+	    push @blanklines, $_;
 	} else {
 	} else {
 	    $self->_do_parse_error($file, $NR, _g( "unrecognised line" ),
 	    $self->_do_parse_error($file, $NR, _g( "unrecognised line" ),
 				   "$_");
 				   "$_");
@@ -293,14 +295,17 @@ sub parse {
 		|| $expect eq 'more change data or trailer')
 		|| $expect eq 'more change data or trailer')
 		&& do {
 		&& do {
 		    # lets assume change data if we expected it
 		    # lets assume change data if we expected it
-		    push @{$entry->{'Changes'}}, ("") x $blanklines, $_;
+		    push @{$entry->{'Changes'}}, @blanklines, $_;
 		    if (!$entry->{'Items'}) {
 		    if (!$entry->{'Items'}) {
 			$entry->{'Items'} ||= [];
 			$entry->{'Items'} ||= [];
 			push @{$entry->{'Items'}}, "$_\n";
 			push @{$entry->{'Items'}}, "$_\n";
 		    } else {
 		    } else {
-			$entry->{'Items'}[-1] .= (" .\n" x $blanklines)." $_\n";
+			my $blank = '';
+			$blank = join("\n", @blanklines) . "\n"
+				if scalar @blanklines;
+			$entry->{'Items'}[-1] .= "$blank$_\n";
 		    }
 		    }
-		    $blanklines = 0;
+		    @blanklines = ();
 		    $expect = 'more change data or trailer';
 		    $expect = 'more change data or trailer';
 		    $entry->{ERROR} = [ $file, $NR, _g( "unrecognised line" ),
 		    $entry->{ERROR} = [ $file, $NR, _g( "unrecognised line" ),
 					"$_" ];
 					"$_" ];