Git.pm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 updateGitIgnore appendRepoUserConfig);
  9. # TODO: Add info/debug logging for all subroutines
  10. sub checkPath($$) {
  11. my $path = shift;
  12. my $logger = shift;
  13. if ( ! -d $path ) {
  14. $logger->error("$path doesn't look like a dir, exiting...");
  15. exit 1;
  16. }
  17. }
  18. sub returnConfigPath($$) {
  19. my $path = shift;
  20. my $logger = shift;
  21. checkPath($path,$logger);
  22. my $gitConfigPath = $path . "/" . ".git/config";
  23. return $gitConfigPath;
  24. }
  25. sub readConfig($$) {
  26. # This sub is probably not really needed for what I'm trying to do
  27. # git itself already parses this config...but an interesting exercise non the less
  28. # and may be useful later
  29. my $path = shift;
  30. my $logger = shift;
  31. my $gitConfigPath = returnConfigPath($path,$logger);
  32. my $catCmd = findBin("cat",$logger);
  33. my @configLines = split("\n",shellex("$catCmd $gitConfigPath",$logger));
  34. # Key is config header, value is hash ref containing config values
  35. my %gitConfig;
  36. my @valueLines;
  37. my $lineCounter = 0;
  38. foreach my $line ( @configLines ) {
  39. $lineCounter++;
  40. #if ( $line =~ m/\[(.*)\]/ ) {
  41. if ( $line =~ m/\[(.*)\]/ ) {
  42. #$valueLine =~ /\t(.*)\ =\ (.*)$/;
  43. $gitConfig{$1} = "";
  44. }
  45. }
  46. # Tag each line with it's heading
  47. # Only way I could think of that worked to solve how this
  48. # There are almost certainly better ways
  49. my @taggedLines;
  50. my $tag = "NULLTAG";
  51. foreach my $line ( @configLines ) {
  52. if ( $line =~ m/\[(.*)\]/ ) {
  53. $tag = $1;
  54. } else {
  55. my $newLine = $tag . $line;
  56. push(@taggedLines,$newLine);
  57. }
  58. }
  59. # Get all of the tagged lines into a hash structure.
  60. foreach my $key ( keys %gitConfig ) {
  61. my %stash;
  62. foreach my $tl ( @taggedLines ) {
  63. if ( $tl =~ m/^($key)/ ) {
  64. $tl =~ s/^($key)//g;
  65. $tl =~ m/^\t(.*)\ \=\ (.*)$/;
  66. my $confKey = $1;
  67. my $confVal = $2;
  68. $stash{$confKey} = $confVal;
  69. }
  70. }
  71. $gitConfig{$key} = \%stash;
  72. }
  73. return %gitConfig;
  74. }
  75. sub getStatus($) {
  76. my $logger = shift;
  77. my $gitCmd = findBin("git",$logger);
  78. my $status = shellex("$gitCmd status -uall --porcelain",$logger);
  79. chomp $status;
  80. return $status;
  81. }
  82. sub returnState($) {
  83. my $logger = shift;
  84. my $gitCmd = findBin("git",$logger);
  85. my $currentStatus = getStatus($logger);
  86. my @statusLines = split("\n", $currentStatus);
  87. my @untracked;
  88. my @modified;
  89. my @added;
  90. my @deleted;
  91. foreach my $file ( @statusLines ) {
  92. $file =~ m/^\ {0,1}([A-Z?]{1,2})\ {1,2}(.*)/;
  93. my $fileAttrs = $1;
  94. my $filename = $2;
  95. my @attrs = split("",$fileAttrs);
  96. foreach my $attr ( @attrs ) {
  97. if ( $attr =~ m/\?/ ) {
  98. push(@untracked, $filename) unless grep $_ eq $filename, @untracked;
  99. }
  100. if ( $attr =~ m/[M]/ ) {
  101. push(@modified, $filename) unless grep $_ eq $filename, @modified;
  102. }
  103. if ( $attr =~ m/[A]/ ) {
  104. push(@added, $filename) unless grep $_ eq $filename, @added;
  105. }
  106. if ( $attr =~ m/[D]/ ) {
  107. push(@deleted, $filename) unless grep $_ eq $filename, @deleted;
  108. }
  109. }
  110. }
  111. return ( \@untracked, \@modified, \@added, \@deleted );
  112. }
  113. sub addFiles($$) {
  114. my $filesToAddRef = shift;
  115. my $logger = shift;
  116. my $gitCmd = findBin("git",$logger);
  117. foreach my $file ( @$filesToAddRef ) {
  118. shellex("$gitCmd add $file",$logger);
  119. }
  120. }
  121. sub commitChanges($$) {
  122. my $commitMsg = shift;
  123. chomp $commitMsg;
  124. my $logger = shift;
  125. my $gitCmd = findBin("git",$logger);
  126. shellex("$gitCmd commit -m \"$commitMsg\"",$logger);
  127. }
  128. sub pushChanges($) {
  129. my $logger = shift;
  130. my $gitCmd = findBin("git",$logger);
  131. my $output = shellex("$gitCmd push",$logger);
  132. }
  133. sub dropStash($) {
  134. my $logger = shift;
  135. my $gitCmd = findBin("git",$logger);
  136. my @stashList = split("\n", shellex("$gitCmd stash list",$logger));
  137. my $stashCount = scalar @stashList;
  138. # TODO: Don't need $stashCount, should just be able to iterate over @stashList
  139. if ( scalar @stashList == 0 ) {
  140. print "Stash is empty so not dropping\n";
  141. } else {
  142. foreach my $stashNum ( 1..$stashCount ) {
  143. shellex("$gitCmd stash drop 0",$logger);
  144. }
  145. }
  146. }
  147. sub stashAndReset($) {
  148. my $logger = shift;
  149. my $gitCmd = findBin("git",$logger);
  150. shellex("$gitCmd stash",$logger);
  151. dropStash($logger);
  152. shellex("$gitCmd rebase",$logger);
  153. }
  154. sub resetFromUpstream($) {
  155. # git stash and git reset --hard and git pull ? I think
  156. # git reset upstream/master; git stash
  157. my $logger = shift;
  158. my $gitCmd = findBin("git",$logger);
  159. my $upstream = shellex("$gitCmd config --get remote.upstream.url",$logger);
  160. if ( $upstream eq "" || ! defined $upstream ) {
  161. print "Upstream not configured, exiting\n";
  162. exit 1;
  163. }
  164. shellex("$gitCmd reset upstream/master",$logger);
  165. shellex("$gitCmd stash",$logger);
  166. dropStash($logger);
  167. print "Successful reset from upstream\n";
  168. print "Changes have not been pushed, run \'$gitCmd pull\' to revert\n";
  169. }
  170. sub updateGitIgnore($$$) {
  171. my $path = shift;
  172. # Maybe better to accept an array of values
  173. my $ignoreValue = shift;
  174. my $logger = shift;
  175. checkPath($path,$logger);
  176. my $filename = $path . "/" . ".gitignore";
  177. # Make sure we're not appending/writing if entry already exists in gitignore
  178. if ( -f $filename ) {
  179. my $catCmd = findBin("cat",$logger);
  180. my @ignoreLines = split("\n",shellex("$catCmd $filename",$logger));
  181. if ( ! grep( /^$ignoreValue$/, @ignoreLines ) ) {
  182. open(my $fh, ">>", $filename) or die $logger->error("Couldn't open $filename, exiting...");
  183. chomp $ignoreValue;
  184. print $fh "$ignoreValue\n";
  185. close $fh;
  186. }
  187. } else {
  188. open(my $fh, ">", $filename) or die $logger->error("Couldn't open $filename, exiting...");
  189. chomp $ignoreValue;
  190. print $fh "$ignoreValue\n";
  191. close $fh;
  192. }
  193. }
  194. sub appendRepoUserConfig($$$) {
  195. my $desiredName = shift;
  196. my $desiredEmail = shift;
  197. my $logger = shift;
  198. my $gitCmd = findBin("git",$logger);
  199. my $currentName = shellex("$gitCmd config --get user.name",$logger);
  200. chomp $currentName;
  201. my $currentEmail = shellex("$gitCmd config --get user.email",$logger);
  202. chomp $currentEmail;
  203. if ( $currentName eq $desiredName ) {
  204. print "Already have $desiredName configured\n";
  205. } else {
  206. shellex("$gitCmd config user.name \"$desiredName\"",$logger);
  207. print "Set $desiredName successfully\n";
  208. }
  209. if ( $currentEmail eq $desiredEmail ) {
  210. print "Already have $desiredEmail configured\n";
  211. } else {
  212. shellex("$gitCmd config user.email \"$desiredEmail\"",$logger);
  213. print "Set $desiredEmail successfully\n";
  214. }
  215. }