commit 55da5018b7838aaf45581ddf6352a490d50b7185
Author: Spesk1 <spesk@pm.me>
Date: Tue May 21 11:53:23 2019 -0400
Added functionality to auto ignore sg log and to append to .gitignore
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..4921dcb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/.sg
diff --git a/.sg/sgLog.txt b/.sg/sgLog.txt
deleted file mode 100644
index b16f447..0000000
--- a/.sg/sgLog.txt
+++ /dev/null
@@ -1 +0,0 @@
-2019/05/20 17:26:32 ERROR /usr/bin/wc -l . returned non-zero: 256
diff --git a/lib/SimplyGit/Git.pm b/lib/SimplyGit/Git.pm
index a1002fd..5531ec8 100644
--- a/lib/SimplyGit/Git.pm
+++ b/lib/SimplyGit/Git.pm
@@ -5,13 +5,15 @@ use Log::Log4perl qw(:easy);
use lib ".";
use SimplyGit::Shellex qw(shellex findBin);
use Exporter qw(import);
-our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream);
+our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore);
-sub readConfig {
+# TODO: Handle "optional" passing of logger object
+# Need to identify which subs are called before the logger object is initialized ( if any )
+# and then required the logger object arg for all who don't fit that usecase
+# TODO: Add info/debug logging for all subroutines
+
+sub checkPath {
- # This sub is probably not really needed for what I'm trying to do
- # git itself already parses this config...but an interesting exercise non the less
- # and may be useful later
my $path = shift;
my $logger = shift;
if ( ! -d $path ) {
@@ -23,7 +25,29 @@ sub readConfig {
exit 1;
}
}
+
+}
+
+sub returnConfigPath {
+
+ my $path = shift;
+ my $logger = shift;
+ checkPath($path,$logger);
+
my $gitConfigPath = $path . "/" . ".git/config";
+ return $gitConfigPath;
+
+}
+
+
+sub readConfig {
+
+ # This sub is probably not really needed for what I'm trying to do
+ # git itself already parses this config...but an interesting exercise non the less
+ # and may be useful later
+ my $path = shift;
+ my $logger = shift;
+ my $gitConfigPath = returnConfigPath($path,$logger);
my $catCmd = findBin("cat",$logger);
my @configLines = split("\n",shellex("$catCmd $gitConfigPath",$logger));
# Key is config header, value is hash ref containing config values
@@ -176,6 +200,34 @@ sub resetFromUpstream {
}
+sub updateGitIgnore {
+
+ my $path = shift;
+ # Maybe better to accept an array of values
+ my $ignoreValue = shift;
+ my $logger = shift;
+ checkPath($path,$logger);
+ my $filename = $path . "/" . ".gitignore";
+ # Make sure we're not appending/writing if entry already exists in gitignore
+ if ( -f $filename ) {
+ my $catCmd = findBin("cat",$logger);
+ my @ignoreLines = split("\n",shellex("$catCmd $filename",$logger));
+ if ( ! grep( /^$ignoreValue$/, @ignoreLines ) ) {
+ # TODO: What if logger object is not passed in?
+ open(my $fh, ">>", $filename) or die $logger->error("Couldn't open $filename, exiting...");
+ chomp $ignoreValue;
+ print $fh "$ignoreValue\n";
+ close $fh;
+ }
+ } else {
+ open(my $fh, ">", $filename) or die $logger->error("Couldn't open $filename, exiting...");
+ chomp $ignoreValue;
+ print $fh "$ignoreValue\n";
+ close $fh;
+ }
+
+}
+
sub appendRepoUserConfig {
diff --git a/sg b/sg
index 392d8f1..1bbd6a0 100755
--- a/sg
+++ b/sg
@@ -8,8 +8,9 @@ use Log::Log4perl qw(:easy);
# TODO: This needs to be scoped properly
use lib "/usr/local/lib";
use SimplyGit::Shellex qw(shellex findBin);
-use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream);
+use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore);
+# TODO: This should maybe be more robust?
if ( ! -d ".git" ) {
print "Not a git dir, exiting...\n";
exit 1;
@@ -51,6 +52,9 @@ my $log_conf = q(
Log::Log4perl::init(\$log_conf);
my $logger = get_logger();
my $gitCmd = findBin("git",$logger);
+# Create or append to .gitignore so that we don't push the log
+# Maybe this should be an opt?
+updateGitIgnore(".","/.sg",$logger);
my %args;
GetOptions(