App-GitWorkspaceScanner

 view release on metacpan or  search on metacpan

lib/App/GitWorkspaceScanner.pm  view on Meta::CPAN

	$self->{'allow_any_branches'} //= 0;
	croak "--allow_any_branches must be set to either 0 or 1"
		if $self->{'allow_any_branches'} !~ /\A[01]\z/;

	# --allow_untracked_files is off by default.
	$self->{'allow_untracked_files'} //= 0;
	croak "--allow_untracked_files must be set to either 0 or 1"
		if $self->{'allow_untracked_files'} !~ /\A[01]\z/;

	# Specific logic when we restrict which branches are valid.
	if ( !$self->{'allow_any_branches'} )
	{
		# It doesn't matter whether it was an explicit choice or not to
		# restrict valid branches, set the option to 0 for future tests.
		$self->{'allow_any_branches'} = 0;

		# Default --allowed_branches to master.
		$self->{'allowed_branches'} //= 'master';
	}

	# Check that the paths provided to --whitelist_repositories are valid.
	$self->{'whitelist_repositories'} //= '';
	my @whitelist_repositories = ();
	foreach my $path ( split( /,/, $self->{'whitelist_repositories'} ) )
	{
		if ( -d $path )
		{
			# Ensure a trailing slash.
			$path =~ s/\/$//;
			push( @whitelist_repositories, "$path/" );
		}
		else
		{
			print "Warning: the path >$path< provided via --whitelist_repositories is not valid and will be skipped.\n";
		}
	}
	$self->{'whitelist_repositories'} = \@whitelist_repositories;

	$self->{'verbose'} && $log->info( 'Finished parsing arguments.' );

	return;
}


=head2 get_git_repositories()

Return a list of all the git repositories on the machine.

	my $git_repositories = get_git_repositories();

=cut

sub get_git_repositories
{
	my ( $self ) = @_;

	if ( !defined( $self->{'git_repositories'} ) )
	{
		if ( $self->{'verbose'} )
		{
			$log->infof( "Running as user '%s'.", getpwuid( $< ) );
			$log->infof( "Scanning workspace '%s'.", $self->{'workspace'} );
		}

		# Find .git directories.
		# TODO: convert to not use backticks.
		# TODO: find a way to generalize to non-Unix systems.
		# TODO: generalize to handle .git repositories that are outside of their
		#       repos (rare).
		$self->{'verbose'} && $log->info( "Locate .git directories." );
		my @locate_results = `locate --basename '\\.git'`; ## no critic (InputOutput::ProhibitBacktickOperators)
		$self->{'verbose'} && $log->infof( "Found %s potential directories.", scalar( @locate_results ) );

		$self->{'git_repositories'} = [];
		foreach my $scanned_path ( @locate_results )
		{
			chomp( $scanned_path );
			$self->{'verbose'} && $log->infof( "Evaluating path %s.", $scanned_path );

			# Parse the path.
			my ( $volume, $git_repository, $file ) = File::Spec->splitpath( $scanned_path );
			if ( $file ne '.git' )
			{
				$self->{'verbose'} && $log->infof( " -> '%s' is not a .git directory after all.", $file );
				next;
			}
			if ( ! -d $git_repository )
			{
				$self->{'verbose'} && $log->infof( " -> '%s' is not a directory.", $git_repository );
				next;
			}

			# Skip paths outside of the workspace.
			if ( $git_repository !~ /^\Q$self->{'workspace'}\E/x )
			{
				$self->{'verbose'} && $log->infof( " -> '%s' is not inside the scanned space.", $git_repository );
				next;
			}

			# Skip whitelisted repositories.
			if ( scalar( grep { $_ eq $git_repository } @{ $self->{'whitelist_repositories'} } ) != 0 )
			{
				$self->{'verbose'} && $log->infof( " -> '%s' is whitelisted.", $git_repository );
				next;
			}

			push( @{ $self->{'git_repositories'} }, $git_repository );
			$self->{'verbose'} && $log->info( " -> Added to the list of repositories!" );
		}
	}

	$self->{'verbose'} && $log->infof(
		'%s relevant git directories.',
		scalar( @{ $self->{'git_repositories'} } ),
	);

	return $self->{'git_repositories'};
}


=head2 get_unclean_repositories()



( run in 0.737 second using v1.01-cache-2.11-cpan-13bb782fe5a )