aboutsummaryrefslogtreecommitdiffstats
path: root/fix-eol
blob: 7d3bf215fde310476e272adf7bfae16189f83805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/perl
# convert end of line patterns from DOS to UNIX

use strict;
use File::Find;
use File::Temp;

die "No build root defined" unless $ENV{RPM_BUILD_ROOT};

# normalize build root
my $buildroot = $ENV{RPM_BUILD_ROOT};
$buildroot =~ s|/$||;

my %exclude_files = (
    map { $buildroot . $_ => 1 }
    split(' ', $ENV{EXCLUDE_FROM_EOL_CONVERSION})
);

find(\&convert, $buildroot);

sub convert {
    # reject symlinks
    return unless -f $_;
    # reject binary files
    return unless -T $_;
    # reject excluded files
    return if $exclude_files{$File::Find::name};

    # check if first line has less than 80 characters and ends with \r\n
    open(my $in, '<', $_) or die "Unable to open file $_: $!";
    my $line =  <$in>;
    if (length($line) <= 80 && $line =~ s/\r\n$/\n/) {
        # process all file
        my $out = File::Temp->new(DIR => '.', UNLINK => 0);
        print $out $line;
        while (($line = <$in>) && defined $line) {
            $line =~ s/\r\n$/\n/;
            print $out $line;
        }
        my $tmp = $out->filename;
        $out = undef;

        rename($tmp, $_) or die "Unable to rename $tmp to $_: $!";
    }

    close($in);
}