commit 2a5196fc4e1cff9628518a6a8dba9b79f56bd54d
Author: spesk1 <spesk@pm.me>
Date: Tue May 21 15:36:07 2019 -0400
Added user config opt
diff --git a/lib/SimplyGit/Git.pm b/lib/SimplyGit/Git.pm
index 5531ec8..4f14e3f 100644
--- a/lib/SimplyGit/Git.pm
+++ b/lib/SimplyGit/Git.pm
@@ -5,7 +5,7 @@ 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 updateGitIgnore);
+our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore appendRepoUserConfig);
# TODO: Handle "optional" passing of logger object
# Need to identify which subs are called before the logger object is initialized ( if any )
@@ -228,7 +228,30 @@ sub updateGitIgnore {
}
-sub appendRepoUserConfig {
+sub appendRepoUserConfig($$$) {
+ my $desiredName = shift;
+ my $desiredEmail = shift;
+ my $logger = shift;
+
+ my $gitCmd = findBin("git",$logger);
+ my $currentName = shellex("$gitCmd config --get user.name",$logger);
+ chomp $currentName;
+ my $currentEmail = shellex("$gitCmd config --get user.email",$logger);
+ chomp $currentEmail;
+
+ if ( $currentName eq $desiredName ) {
+ print "Already have $desiredName configured\n";
+ } else {
+ shellex("$gitCmd config user.name \"$desiredName\"",$logger);
+ print "Set $desiredName successfully\n";
+ }
+
+ if ( $currentEmail eq $desiredEmail ) {
+ print "Already have $desiredEmail configured\n";
+ } else {
+ shellex("$gitCmd config user.email \"$desiredEmail\"",$logger);
+ print "Set $desiredEmail successfully\n";
+ }
}
diff --git a/sg b/sg
index 506eac3..745468f 100755
--- a/sg
+++ b/sg
@@ -8,7 +8,7 @@ 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 updateGitIgnore);
+use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream updateGitIgnore appendRepoUserConfig);
# TODO: This should maybe be more robust?
if ( ! -d ".git" ) {
@@ -67,6 +67,9 @@ GetOptions(
'branch-from-master',
'commit-msg=s',
'dump-config',
+ 'configure-local-user',
+ 'user=s',
+ 'email=s',
);
sub printHelp {
@@ -93,6 +96,10 @@ Usage:
--branch-from-master
Create a new clean branch from upstream/master
+
+ --configure-local-user [--user,--email]
+ Configure local git user
+
EOF
;
@@ -137,17 +144,43 @@ sub parseArgs {
}
}
+ if ( defined $args{'configure-local-user'} ) {
+ if ( scalar keys %args < 2 ) {
+ print "Must pass either --interactive or --user AND --email to --configure-local-user\n";
+ exit 1;
+ }
+ foreach my $arg ( keys %args ) {
+ if ( $arg eq "interactive" || $arg eq "user" || $arg eq "email" || $arg eq "configure-local-user" ) {
+ next;
+ } else {
+ print "Must/can only pass --interactive, OR --user AND --email with --configure-local-user\n";
+ exit 1;
+ }
+ }
+
+ if ( ! defined $args{'interactive'} && ! defined $args{'user'} || ! defined $args{'interactive'} && ! defined $args{'email'} ) {
+ print "If not using --interactive with --configure-local-user, --user and --email MUST be defined\n";
+ exit 1;
+ }
+ }
}
parseArgs();
+#print "Args parsed successfully\n";
+#exit 0;
# TODO: This sub could be more concise with a sub to print array refs
if ( defined $args{'view'} ) {
my ( $untrackedRef, $modifiedRef, $addedRef, $deletedRef ) = returnState($logger);
my $refs = shellex("$gitCmd show-ref",$logger);
my $branch = shellex("$gitCmd show-branch",$logger);
+ my $name = shellex("$gitCmd config --get user.name",$logger);
+ chomp $name;
+ my $email = shellex("$gitCmd config --get user.email",$logger);
+ chomp $email;
+ print "Username: $name\nEmail: $email\n";
print "On [branch] @ commit: $branch";
print "$refs\n";
my $swpWarning = "\t# Likely a Vi .swp file";
@@ -295,3 +328,23 @@ if ( defined $args{'dump-config'} ) {
}
}
+
+if ( defined $args{'configure-local-user'} ) {
+
+ if ( defined $args{'interactive'} ) {
+ print "Enter user to set: ";
+ my $desiredName = <STDIN>;
+ chomp $desiredName;
+ print "Enter email to set: ";
+ my $desiredEmail = <STDIN>;
+ chomp $desiredEmail;
+
+ appendRepoUserConfig($desiredName,$desiredEmail,$logger);
+
+ } else {
+
+ appendRepoUserConfig($args{'user'},$args{'email'},$logger);
+
+ }
+
+}