Gather.pm 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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);
  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 get_file_tree($$) {
  28. my $projectDir = shift;
  29. my $logger = shift;
  30. my $gitCmd = findBin("git",$logger);
  31. # Get files
  32. my %file_tree;
  33. foreach my $file ( split("\n", shellex("$gitCmd --git-dir=\"$projectDir\" ls-tree --full-tree -r HEAD",$logger)) ) {
  34. chomp $file;
  35. $file =~ /([a-z0-9]{40})\t(.*)$/;
  36. # Name - object id
  37. $file_tree{$2} = $1;
  38. }
  39. # Get file content
  40. my %file_content;
  41. foreach my $filename ( keys %file_tree ) {
  42. my $content = shellex("$gitCmd --git-dir=\"$projectDir\" show $file_tree{$filename}",$logger);
  43. chomp $content;
  44. # Name - file content
  45. $file_content{$filename} = $content;
  46. }
  47. # Get logs
  48. my @commit_ids;
  49. foreach my $log_line ( split("\n",shellex("$gitCmd --git-dir=\"$projectDir\" log",$logger)) ) {
  50. if ( $log_line =~ m/commit\ ([a-z0-9]{40})/ ) {
  51. push(@commit_ids,$1);
  52. }
  53. }
  54. my %commits;
  55. foreach my $commit_id ( @commit_ids ) {
  56. my $commit_info = shellex("git --git-dir=\"$projectDir\" show $commit_id",$logger);
  57. chomp $commit_info;
  58. $commits{$commit_id} = $commit_info;
  59. }
  60. return ( \%file_tree, \%file_content, \%commits );
  61. }
  62. 1;