Eric Blake <ebl...@redhat.com> writes:

> On 06/27/2016 02:50 AM, Markus Armbruster wrote:
>
>> With an ugly Perl script, of course %-/
>> 
>
>> I'm afraid my script and its usage is too brittle to be of much use
>> later on.  I attach it anyway.
>
> A zero byte file map-include-to-header.pl~.  Infinite compression :)

Looks like my fingers were even more reluctant to show off their ugly
work than my brain was...

#!/usr/bin/perl -w

use strict;

my %ihdr = ();
my %idir = ();
my @sall = ();
my @sinc = ();

open(my $fh, "-|", "git grep '^[ \t]*#[ \t]*include[ \t]'")
    or die "can't run git grep: $!";
while (<$fh>) {
    m,^([^:/]*)/?[^:/]*:[ \t]*#[ \t]*include[ \t]*(["<])([^">]*),;
    my $dir = $1 // ".";
    my $delim = $2;
    my $h = $3;
    $ihdr{$h} |= 1 << ($delim eq "<");
    if (exists $idir{$h}) {
        my $aref = $idir{$h};
        push @$aref, $dir unless grep($_ eq $dir, @$aref);
    } else {
        $idir{$h} = [$dir];
    }
}
close ($fh);

open($fh, "-|", "git ls-tree -r --name-only HEAD")
    or die "can't run git ls-tree: $!";
while (<$fh>) {
    chomp;
    push @sall, $_;
}
close ($fh);

@sinc = grep(/^include\//, @sall);

sub pr {
    my ($h, $fn, $src) = @_;

    print "$h -> $fn";
    if ($ihdr{$h} == 3) {
        print " (included inconsistently)";
    } elsif ($src) {
        print " (included with <>)" if ($ihdr{$h} != 1);
    } else {
        print " (included with \"\")" if ($ihdr{$h} != 2);
    }
    print "\n";
}

for my $h (keys %ihdr) {
    $h =~ m,^(\.\./)*(include/)?(.*), or die;
    my $hh = $3;
    my @fn = grep(/^include\/\Q$hh\E$/, @sinc);
    if (@fn) {
        pr($h, $fn[0], 1);
        next;
    }
    @fn = grep(/^\Q$hh\E$/, @sall);
    if (@fn) {
        pr($h, $fn[0], 1);
        next;
    }
    for my $dir (@{$idir{$h}}) {
        next if $dir eq ".";
        @fn = grep(/^\Q$dir\/$hh\E$/, @sall);
        if (@fn) {
            pr($h, $fn[0], 1);
        } else {
            pr($h, "? (in $dir)", 0);
        }
    }
}

Reply via email to