summaryrefslogtreecommitdiffstats
path: root/scripts/remove-trailing-ws.in
diff options
context:
space:
mode:
authorAndreas Gruenbacher <agruen@suse.de>2005-06-18 18:03:57 +0000
committerAndreas Gruenbacher <agruen@suse.de>2005-06-18 18:03:57 +0000
commit31a5851d6372712fa6bafd4a6081f21ecee1c570 (patch)
treeaf8dbbacc1dfadd0423f1b443b93f037de2b0b9e /scripts/remove-trailing-ws.in
parent442b3debff33d2d0f13214abf42bd53525557ff9 (diff)
downloadquilt-31a5851d6372712fa6bafd4a6081f21ecee1c570.tar.gz
- Add fix for refresh --strip-trailing-whitespace option: now
refresh always checks for trailing whitespace and reports it per file and line number. With --strip-trailing-whitespace it strips the whitespace from the patch and from the affected files in the working tree.
Diffstat (limited to 'scripts/remove-trailing-ws.in')
-rw-r--r--scripts/remove-trailing-ws.in112
1 files changed, 112 insertions, 0 deletions
diff --git a/scripts/remove-trailing-ws.in b/scripts/remove-trailing-ws.in
new file mode 100644
index 0000000..f14de5d
--- /dev/null
+++ b/scripts/remove-trailing-ws.in
@@ -0,0 +1,112 @@
+#! @PERL@ -w
+
+# Remove trailing whitespace from modified lines in working files.
+#
+# Input: diff between original and working files (unified or context
+# format).
+
+use strict;
+use FileHandle;
+use File::Temp qw( :mktemp );
+use Getopt::Std;
+use vars qw($opt_p $opt_n);
+
+$opt_p = 0;
+getopts('np:')
+ or die "SYNOPSIS: $0 [-p num] [-n] [patch]\n";
+
+my %files;
+my $file;
+while (<>) {
+ print unless $opt_n;
+ if (/^--- ./) {
+ # unified diff
+ while (<>) {
+ print unless $opt_n;
+ if (/^\+\+\+ (.+?)(?:[ \t][^\t]*)?$/) {
+ $file = $1;
+ $file =~ s<^([^/]+/+){$opt_p}><>;
+ #print STDERR "[$file]\n";
+ } elsif ($file && /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/) {
+ my $removed = defined $2 ? $2 : 1;
+ my $added = defined $4 ? $4 : 1;
+ my $line_number = $3;
+
+ while ($removed || $added) {
+ $_ = <>;
+ defined $_
+ or die "$0: I'm confused.\n";
+ if (/^\+/) {
+ push @{$files{$file}}, $line_number
+ if s/(^\+.*?)[ \t]+$/$1/;
+ $added--;
+ $line_number++;
+ } elsif (/^-/) {
+ $removed--;
+ } elsif (/^ / || /^$/) {
+ $removed--;
+ $added--;
+ $line_number++;
+ }
+ print unless $opt_n;
+ }
+ }
+ }
+ } elsif (/^\*\*\* ./) {
+ # context diff
+ while (<>) {
+ print unless $opt_n;
+ if ($file && /^--- (\d+)(?:,(\d+))? ----$/) {
+ my $line_number = $1;
+ my $last_line = defined $2 ? $2 : $1;
+ while ($line_number <= $last_line) {
+ $_ = <>;
+ defined $_
+ or die "$0: I'm confused.\n";
+ if (s/(^[+!] .*?)[ \t]+$/$1/) {
+ push @{$files{$file}}, $line_number;
+ }
+ $line_number++;
+ print unless $opt_n;
+ last if (/^\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*$/);
+ }
+ } elsif (/^--- (.+?)(?:[ \t][^\t]*)?$/) {
+ $file = $1;
+ $file =~ s<^([^/]+/+){$opt_p}><>;
+ #print STDERR "[$file]\n";
+ }
+ }
+ }
+}
+
+foreach my $file (keys %files) {
+ my @lines = @{$files{$file}};
+ if ($opt_n) {
+ print STDERR "Warning: trailing whitespace in line";
+ } else {
+ print STDERR "Removing trailing whitespace from line";
+ }
+ print STDERR "s" if @lines > 1;
+ print STDERR " " . join(',', @lines) . " of $file\n";
+
+ unless ($opt_n) {
+ my $fh = new FileHandle("< $file")
+ or die "$file: $!\n";
+ my ($tmp, $tmpname) = mkstemp("$file.XXXXXX")
+ or die "$file.XXXXXX: $!\n";
+ while (<$fh>) {
+ if (@lines && $lines[0] == $.) {
+ s/[ \t]+$//;
+ shift @lines;
+ }
+ print $tmp $_;
+ }
+ $fh->close;
+ $tmp->close
+ or die "$tmpname: $!\n";
+ rename $tmpname, $file
+ or die "Renaming $tmpname to $file: $!\n";
+ }
+}
+
+#exit (%files ? 1 : 0);