Git.pm 8.2 KB

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