Git.pm 8.5 KB

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