Git.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. my @deleted;
  51. foreach my $file ( @statusLines ) {
  52. $file =~ m/^\ {0,1}([A-Z?]{1,2})\ {1,2}(.*)/;
  53. my $fileAttrs = $1;
  54. my $filename = $2;
  55. my @attrs = split("",$fileAttrs);
  56. foreach my $attr ( @attrs ) {
  57. if ( $attr =~ m/\?/ ) {
  58. push(@untracked, $filename) unless grep $_ eq $filename, @untracked;
  59. }
  60. if ( $attr =~ m/[M]/ ) {
  61. push(@modified, $filename) unless grep $_ eq $filename, @modified;
  62. }
  63. if ( $attr =~ m/[A]/ ) {
  64. push(@added, $filename) unless grep $_ eq $filename, @added;
  65. }
  66. if ( $attr =~ m/[D]/ ) {
  67. push(@deleted, $filename) unless grep $_ eq $filename, @deleted;
  68. }
  69. }
  70. }
  71. return ( \@untracked, \@modified, \@added, \@deleted );
  72. }
  73. sub addFiles {
  74. my $filesToAddRef = shift;
  75. my $logger = shift;
  76. my $gitCmd = findBin("git",$logger);
  77. foreach my $file ( @$filesToAddRef ) {
  78. shellex("$gitCmd add $file",$logger);
  79. }
  80. }
  81. sub commitChanges {
  82. my $commitMsg = shift;
  83. chomp $commitMsg;
  84. my $logger = shift;
  85. my $gitCmd = findBin("git",$logger);
  86. shellex("$gitCmd commit -m \"$commitMsg\"",$logger);
  87. }
  88. sub pushChanges {
  89. my $logger = shift;
  90. my $gitCmd = findBin("git",$logger);
  91. my $output = shellex("$gitCmd push",$logger);
  92. }