Git.pm 8.5 KB

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