Git.pm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package SimplyGit::Git;
  2. use strict;
  3. use warnings;
  4. use Log::Log4perl qw(:easy);
  5. use lib ".";
  6. use SimplyGit::Shellex qw(shellex findBin);
  7. use Exporter qw(import);
  8. our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges);
  9. sub readConfig($) {
  10. # TODO: This sub is probably not really needed for what I'm trying to do
  11. # git itself already parses this config...
  12. my $path = shift;
  13. my $logger = shift;
  14. # TODO: Should pass in logger object as opposed to printing
  15. if ( ! -d $path ) {
  16. print "readConfig did not recieve expected dir, exiting...\n";
  17. exit 1;
  18. }
  19. my $gitConfigPath = $path . "/" . ".git/config";
  20. my $catCmd = findBin("cat",$logger);
  21. my @configLines = split("\n",shellex("$catCmd $gitConfigPath",$logger));
  22. my %gitConfig;
  23. my @valueLines;
  24. my $headerCounter = 0;
  25. foreach my $line ( @configLines ) {
  26. if ( $line =~ m/\[(.*)\]/ ) {
  27. #$valueLine =~ /\t(.*)\ =\ (.*)$/;
  28. $gitConfig{$1} = "";
  29. } else {
  30. push(@valueLines, $line);
  31. }
  32. }
  33. return %gitConfig;
  34. }
  35. sub getStatus {
  36. my $logger = shift;
  37. my $gitCmd = findBin("git",$logger);
  38. my $status = shellex("$gitCmd status -uall --porcelain",$logger);
  39. chomp $status;
  40. return $status;
  41. }
  42. sub returnState {
  43. my $logger = shift;
  44. my $gitCmd = findBin("git",$logger);
  45. my $currentStatus = getStatus($logger);
  46. my @statusLines = split("\n", $currentStatus);
  47. my @untracked;
  48. my @modified;
  49. my @added;
  50. foreach my $file ( @statusLines ) {
  51. $file =~ m/^([A-Z?]{1,2})\ {1,2}(.*)/;
  52. my $fileAttrs = $1;
  53. my $filename = $2;
  54. my @attrs = split("",$fileAttrs);
  55. foreach my $attr ( @attrs ) {
  56. if ( $attr =~ m/\?/ ) {
  57. push(@untracked, $filename) unless grep $_ eq $filename, @untracked;
  58. }
  59. if ( $attr =~ m/[M]/ ) {
  60. push(@modified, $filename) unless grep $_ eq $filename, @modified;
  61. }
  62. if ( $attr =~ m/[A]/ ) {
  63. push(@added, $filename) unless grep $_ eq $filename, @added;
  64. }
  65. }
  66. }
  67. return ( \@untracked, \@modified, \@added );
  68. }
  69. sub addFiles {
  70. my $filesToAddRef = shift;
  71. my $logger = shift;
  72. my $gitCmd = findBin("git",$logger);
  73. foreach my $file ( @$filesToAddRef ) {
  74. shellex("$gitCmd add $file",$logger);
  75. }
  76. }
  77. sub commitChanges {
  78. my $commitMsg = shift;
  79. chomp $commitMsg;
  80. my $logger = shift;
  81. my $gitCmd = findBin("git",$logger);
  82. shellex("$gitCmd commit -m \"$commitMsg\"",$logger);
  83. }
  84. sub pushChanges {
  85. my $logger = shift;
  86. my $gitCmd = findBin("git",$logger);
  87. shellex("$gitCmd push",$logger);
  88. }