Git.pm 7.6 KB

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