Git.pm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 stashAndReset);
  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. # TODO: Possibly worth returning output for commitChanges(), pushChanges(), stashAndReset, even if I'm not using it right now
  82. sub commitChanges {
  83. my $commitMsg = shift;
  84. chomp $commitMsg;
  85. my $logger = shift;
  86. my $gitCmd = findBin("git",$logger);
  87. shellex("$gitCmd commit -m \"$commitMsg\"",$logger);
  88. }
  89. sub pushChanges {
  90. my $logger = shift;
  91. my $gitCmd = findBin("git",$logger);
  92. my $output = shellex("$gitCmd push",$logger);
  93. }
  94. sub stashAndReset {
  95. my $logger = shift;
  96. my $gitCmd = findBin("git",$logger);
  97. shellex("$gitCmd stash",$logger);
  98. my @stashList = split("\n", shellex("$gitCmd stash list",$logger));
  99. my $stashCount = scalar @stashList;
  100. foreach my $stashNum ( 1..$stashCount ) {
  101. shellex("$gitCmd stash drop 0",$logger);
  102. }
  103. # TODO: Depending on use case need to do more here
  104. shellex("$gitCmd rebase",$logger);
  105. }