Skip to content

Commit

Permalink
Item13135: Tweaked script
Browse files Browse the repository at this point in the history
  • Loading branch information
Jlevens committed Feb 27, 2015
1 parent 46f2556 commit fde24bc
Showing 1 changed file with 102 additions and 28 deletions.
130 changes: 102 additions & 28 deletions core/tools/git_excludes.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use strict;
use warnings;
use File::Spec;
use List::Util;
use Cwd;

our $distro;
Expand All @@ -21,7 +22,7 @@ BEGIN
($toolsDir) = Cwd::abs_path($toolsDir) =~ /(.*)/;
@INC = ( $toolsDir, grep { $_ ne $toolsDir } @INC );
my $binDir = Cwd::abs_path( File::Spec->catdir( $toolsDir, "..", "bin" ) );
$distro = Cwd::abs_path( File::Spec->catdir( $toolsDir, "..//..", "" ) );
$distro = Cwd::abs_path( File::Spec->catdir( $toolsDir, "../..", "" ) );
my ($setlib) =
File::Spec->catpath( $volume, $binDir, 'setlib.cfg' ) =~ /(.*)/;
require $setlib;
Expand All @@ -41,47 +42,120 @@ sub slurp {
}

sub recurseDirectories {
my ( $dir, $sub ) = @_;
my ( $dirs, $nodeSub, $data, $exitSub ) = @_;

my @dirs = ( glob("$dir/*"), glob("$dir/.*") );
my $depth = scalar @{ $dirs };
my $thisDir = File::Spec->catdir( @{ $dirs } );

for my $f ( sort @dirs ) {
next if $f =~ m{.*?/\.{1,2}$};
&$sub($f);
recurseDirectories( $f, $sub ) if -d $f && !-l $f;
opendir(my $DH, $thisDir) or die "Error: failed to open $thisDir $!";
while( my $leaf = readdir $DH ) { push @leaves, $leaf; }
closedir($DH);

for my $leaf ( sort @leaves ) {
if( &$nodeSub( [ @{$dirs}, $leaf ], $data) ) {
recurseDirectories( [ @{$dirs}, $leaf ], $nodeSub, $data, $exitSub );
}
}
&$exitSub($dirs, $data);
}

my @symlinks;
my checkNode {
my ( $dirs, $data ) = @_;

my $depth = scalar @{ $dirs };

recurseDirectories(
$distro,
sub {
my $link = $_[0];
$link =~ s/^$distro//;
push @symlinks, $link if -l $_[0];
return 0 if any { eq $leaf } ( '.', '..', '.gitexcludes' );

if( $depth == 3 && $dirs->[3] eq '.git' ) {
push @{ $data->{extensions} }, $dirs->[2];
return 0;
}

my $thisDir = File::Spec->catdir( @{ $dirs } );
my $node = "$thisDir/$leaf";

if( -l $node ) {
push @{ $data->{symlinks}[$depth] }, $node };
return 0;
}
);
return -d $node;
}

my @nodes = sort( glob("$distro/*"), glob("$distro/.*") );
my @symlinks;
my @extensions;

mkdir("$distro/gitexcludes");
open( my $symFH, '>' . "$distro/gitexcludes/symlinks" );
for my $node (@nodes) {
my ($dir, $file) = $node =~ m{(.*)/(.*?)$};

{
local $" = "\n";
print $symFH "@symlinks\n";
push @symlinks, $node;
next;
}
next if !-d $node;

next if $file eq '.';
next if $file eq '..';
next if $file eq '.gitexcludes';
next if $file eq '.git';

# We know $node is a Directory of interest
my @leaves = sort( glob("$node/*"), glob("$node/.*") );
push @extensions, $node if any { eq '.git' } @leaves;

push @symlinks, scanDirectory( $node, \@leaves );
}

close($symFH);
addExclude( $distro, 'symlinks', \@symlinks );
addExclude( $distro, 'extensions', \@extensions );
concatenateExcludes( "$distro/info/exclude" );

sub scanDirectory {
my ($dir, $leaves) =@_;
my @files = @{ $leaves };
my @symlinks;

my @ignores = glob("$distro/gitexcludes/*");
for my $file (@files) {
my $node = "$dir/$file";

my $out = undef;
for my $f ( sort @ignores ) {
$out .= slurp("$f");
if( -l $node ) {
push @symlinks, $node;
next;
}

next if !-d $node;

next if $file eq '.';
next if $file eq '..';
next if $file eq '.gitexcludes';
next if $file eq '.git';

push @symlinks, scanDirectory($node);
}
addExclude( $dir, 'symlinks', \@symlinks ) if $depth == 1;
}

sub addExclude {
my ($location, $name, $nodes);
my @symlinks = @{ $nodes };

mkdir("$location/.gitexcludes");
open( my $FH, '>' . "$location/.gitexcludes/$name" );
for my $node (@nodes) {
my $link =~ s/$location//;
print $FH "$link\n";
}
close($FH);
}

open( my $info_exclude, '>', "$distro/.git/info/exclude" );
print $info_exclude $out;
close $info_exclude;
#my @ignores = glob("$distro/gitexcludes/*");

#my $out = undef;
#for my $f ( sort @ignores ) {
# $out .= slurp("$f");
#}

#open( my $info_exclude, '>', "$distro/.git/info/exclude" );
#print $info_exclude $out;
#close $info_exclude;

exit 0;

0 comments on commit fde24bc

Please sign in to comment.