package Gsg::Gather; use strict; use warnings; use Log::Log4perl qw(:easy); use Shellex::Shellex qw(shellex findBin); use Exporter qw(import); our @EXPORT_OK = qw(get_file_tree get_projects trim_project_paths); sub get_projects($$$) { my $git_dir = shift; my $ignored_projects_ref = shift; my $logger = shift; my $ls_cmd = findBin("ls",$logger); my @git_project_dirs; foreach my $dir ( split("\n", shellex("$ls_cmd -d $git_dir/*/",$logger)) ) { if ( $dir !~ m/\.git/ ) { next; } if ( grep( /^$dir$/, @$ignored_projects_ref ) ) { $logger->info("Found $dir in ignore list, skipping..."); next; } else { push(@git_project_dirs,$dir); } } return \@git_project_dirs; } sub trim_project_paths($$) { my $projects_ref = shift; my $logger = shift; my @trimmed_projects; foreach my $project_path ( @$projects_ref ) { # Chop parts of the path we dont need for the web root # /some/path/project.git/ -> project.git/ if ( $project_path =~ m/\/?([^\/]+\.[^\.]+$)/ ) { push(@trimmed_projects, $1); } } $logger->info("Returning trimmed project paths"); return \@trimmed_projects; } sub get_file_tree($$) { my $projectDir = shift; my $logger = shift; my $gitCmd = findBin("git",$logger); # Get files my %file_tree; foreach my $file ( split("\n", shellex("$gitCmd --git-dir=\"$projectDir\" ls-tree --full-tree -r HEAD",$logger)) ) { chomp $file; $file =~ /([a-z0-9]{40})\t(.*)$/; # Name - object id $file_tree{$2} = $1; } # Get file content my %file_content; foreach my $filename ( keys %file_tree ) { my $content = shellex("$gitCmd --git-dir=\"$projectDir\" show $file_tree{$filename}",$logger); chomp $content; # Name - file content $file_content{$filename} = $content; } # Get logs my @commit_ids; foreach my $log_line ( split("\n",shellex("$gitCmd --git-dir=\"$projectDir\" log",$logger)) ) { if ( $log_line =~ m/commit\ ([a-z0-9]{40})/ ) { push(@commit_ids,$1); } } my %commits; foreach my $commit_id ( @commit_ids ) { my $commit_info = shellex("git --git-dir=\"$projectDir\" show $commit_id",$logger); chomp $commit_info; $commits{$commit_id} = $commit_info; } # We return commit_ids as well to preserve ordering return ( \%file_tree, \%file_content, \%commits, \@commit_ids ); } 1;