Gather.pm 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package Gsg::Gather;
  2. use strict;
  3. use warnings;
  4. use Log::Log4perl qw(:easy);
  5. use Shellex::Shellex qw(shellex findBin);
  6. use Exporter qw(import);
  7. our @EXPORT_OK = qw(get_file_tree get_projects trim_project_paths);
  8. sub get_projects($$$) {
  9. my $git_dir = shift;
  10. my $ignored_projects_ref = shift;
  11. my $logger = shift;
  12. my $ls_cmd = findBin("ls",$logger);
  13. my @git_project_dirs;
  14. foreach my $dir ( split("\n", shellex("$ls_cmd -d $git_dir/*/",$logger)) ) {
  15. if ( $dir !~ m/\.git/ ) {
  16. next;
  17. }
  18. if ( grep( /^$dir$/, @$ignored_projects_ref ) ) {
  19. $logger->info("Found $dir in ignore list, skipping...");
  20. next;
  21. } else {
  22. push(@git_project_dirs,$dir);
  23. }
  24. }
  25. return \@git_project_dirs;
  26. }
  27. sub trim_project_paths($$) {
  28. my $projects_ref = shift;
  29. my $logger = shift;
  30. my @trimmed_projects;
  31. foreach my $project_path ( @$projects_ref ) {
  32. # Chop parts of the path we dont need for the web root
  33. # /some/path/project.git/ -> project.git/
  34. if ( $project_path =~ m/\/?([^\/]+\.[^\.]+$)/ ) {
  35. push(@trimmed_projects, $1);
  36. }
  37. }
  38. $logger->info("Returning trimmed project paths");
  39. return \@trimmed_projects;
  40. }
  41. sub get_file_tree($$) {
  42. my $projectDir = shift;
  43. my $logger = shift;
  44. my $gitCmd = findBin("git",$logger);
  45. # Get files
  46. my %file_tree;
  47. foreach my $file ( split("\n", shellex("$gitCmd --git-dir=\"$projectDir\" ls-tree --full-tree -r HEAD",$logger)) ) {
  48. chomp $file;
  49. $file =~ /([a-z0-9]{40})\t(.*)$/;
  50. # Name - object id
  51. $file_tree{$2} = $1;
  52. }
  53. # Get file content
  54. my %file_content;
  55. foreach my $filename ( keys %file_tree ) {
  56. my $content = shellex("$gitCmd --git-dir=\"$projectDir\" show $file_tree{$filename}",$logger);
  57. chomp $content;
  58. # Name - file content
  59. $file_content{$filename} = $content;
  60. }
  61. # Get logs
  62. my @commit_ids;
  63. foreach my $log_line ( split("\n",shellex("$gitCmd --git-dir=\"$projectDir\" log",$logger)) ) {
  64. if ( $log_line =~ m/commit\ ([a-z0-9]{40})/ ) {
  65. push(@commit_ids,$1);
  66. }
  67. }
  68. my %commits;
  69. foreach my $commit_id ( @commit_ids ) {
  70. my $commit_info = shellex("git --git-dir=\"$projectDir\" show $commit_id",$logger);
  71. chomp $commit_info;
  72. $commits{$commit_id} = $commit_info;
  73. }
  74. # We return commit_ids as well to preserve ordering
  75. return ( \%file_tree, \%file_content, \%commits, \@commit_ids );
  76. }
  77. 1;