Git.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 resetFromUpstream);
  9. sub readConfig {
  10. # This sub is probably not really needed for what I'm trying to do
  11. # git itself already parses this config...but an interesting exercise non the less
  12. # and may be useful later
  13. my $path = shift;
  14. my $logger = shift;
  15. if ( ! -d $path ) {
  16. $logger->error("$path doesn't look like a dir, exiting...");
  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 $lineCounter = 0;
  25. foreach my $line ( @configLines ) {
  26. $lineCounter++;
  27. #if ( $line =~ m/\[(.*)\]/ ) {
  28. if ( $line =~ m/(\[.*\])/ ) {
  29. #$valueLine =~ /\t(.*)\ =\ (.*)$/;
  30. $gitConfig{$1} = $lineCounter;
  31. }
  32. }
  33. my @lineRange = values %gitConfig;
  34. my @sortedRange = sort { $a <=> $b } @lineRange;
  35. my $prevVal ;
  36. foreach my $val ( @sortedRange ) {
  37. print "Val is $val\n";
  38. if ( ! defined $prevVal ) {
  39. $prevVal = $val;
  40. next;
  41. } else {
  42. print "Current var is $val and prevVar $prevVal\n";
  43. my $lineDiff = ( $val - 1 ) . "-" . ( $prevVal + 1 );
  44. print "lineDiff of vals is: $lineDiff \n";
  45. $prevVal = $val;
  46. }
  47. }
  48. return %gitConfig;
  49. }
  50. sub getStatus {
  51. my $logger = shift;
  52. my $gitCmd = findBin("git",$logger);
  53. my $status = shellex("$gitCmd status -uall --porcelain",$logger);
  54. chomp $status;
  55. return $status;
  56. }
  57. sub returnState {
  58. my $logger = shift;
  59. my $gitCmd = findBin("git",$logger);
  60. my $currentStatus = getStatus($logger);
  61. my @statusLines = split("\n", $currentStatus);
  62. my @untracked;
  63. my @modified;
  64. my @added;
  65. my @deleted;
  66. foreach my $file ( @statusLines ) {
  67. $file =~ m/^\ {0,1}([A-Z?]{1,2})\ {1,2}(.*)/;
  68. my $fileAttrs = $1;
  69. my $filename = $2;
  70. my @attrs = split("",$fileAttrs);
  71. foreach my $attr ( @attrs ) {
  72. if ( $attr =~ m/\?/ ) {
  73. push(@untracked, $filename) unless grep $_ eq $filename, @untracked;
  74. }
  75. if ( $attr =~ m/[M]/ ) {
  76. push(@modified, $filename) unless grep $_ eq $filename, @modified;
  77. }
  78. if ( $attr =~ m/[A]/ ) {
  79. push(@added, $filename) unless grep $_ eq $filename, @added;
  80. }
  81. if ( $attr =~ m/[D]/ ) {
  82. push(@deleted, $filename) unless grep $_ eq $filename, @deleted;
  83. }
  84. }
  85. }
  86. return ( \@untracked, \@modified, \@added, \@deleted );
  87. }
  88. sub addFiles {
  89. my $filesToAddRef = shift;
  90. my $logger = shift;
  91. my $gitCmd = findBin("git",$logger);
  92. foreach my $file ( @$filesToAddRef ) {
  93. shellex("$gitCmd add $file",$logger);
  94. }
  95. }
  96. # TODO: Possibly worth returning output for commitChanges(), pushChanges(), stashAndReset, even if I'm not using it right now
  97. sub commitChanges {
  98. my $commitMsg = shift;
  99. chomp $commitMsg;
  100. my $logger = shift;
  101. my $gitCmd = findBin("git",$logger);
  102. shellex("$gitCmd commit -m \"$commitMsg\"",$logger);
  103. }
  104. sub pushChanges {
  105. my $logger = shift;
  106. my $gitCmd = findBin("git",$logger);
  107. my $output = shellex("$gitCmd push",$logger);
  108. }
  109. sub stashAndReset {
  110. my $logger = shift;
  111. my $gitCmd = findBin("git",$logger);
  112. shellex("$gitCmd stash",$logger);
  113. my @stashList = split("\n", shellex("$gitCmd stash list",$logger));
  114. my $stashCount = scalar @stashList;
  115. foreach my $stashNum ( 1..$stashCount ) {
  116. shellex("$gitCmd stash drop 0",$logger);
  117. }
  118. # TODO: Depending on use case need to do more here
  119. shellex("$gitCmd rebase",$logger);
  120. }
  121. sub resetFromUpstream {
  122. # git stash and git reset --hard and git pull ? I think
  123. }
  124. sub appendRepoUserConfig {
  125. }