5c98fd3deb05e0cde4c86a829ffd62adeadd5387
commit 5c98fd3deb05e0cde4c86a829ffd62adeadd5387
Author: spesk1 <spesk@pm.me>
Date: Tue May 21 21:35:41 2019 -0400

Added reset from upstream functionality

diff --git a/README.md b/README.md
index f07cca8..99e241e 100644
--- a/README.md
+++ b/README.md
@@ -6,15 +6,9 @@ Mainly writing to learn more about Git and Perl, unlikely to be widely useful.
TODO:

Features:
---reset-from-master
-* add soft and hard functionality, right now only 'soft' is implemented
-
--branch-from-master
* generate an up to date new branch from upstream/master (whether you're forked or cloned directly)

---configure-local-user
-* configure local user in .git/config
-
--add-upstream
* append upstream config to fork config

diff --git a/lib/SimplyGit/Git.pm b/lib/SimplyGit/Git.pm
index bd4dfba..2f8378b 100644
--- a/lib/SimplyGit/Git.pm
+++ b/lib/SimplyGit/Git.pm
@@ -179,25 +179,50 @@ sub pushChanges {

}

-sub stashAndReset {
+sub dropStash($) {

my $logger = shift;
my $gitCmd = findBin("git",$logger);
- shellex("$gitCmd stash",$logger);
my @stashList = split("\n", shellex("$gitCmd stash list",$logger));
my $stashCount = scalar @stashList;
# TODO: Don't need $stashCount, should just be able to iterate over @stashList
- foreach my $stashNum ( 1..$stashCount ) {
- shellex("$gitCmd stash drop 0",$logger);
+ if ( scalar @stashList == 0 ) {
+ print "Stash is empty so not dropping\n";
+ } else {
+ foreach my $stashNum ( 1..$stashCount ) {
+ shellex("$gitCmd stash drop 0",$logger);
+ }
}

+}
+
+sub stashAndReset {
+
+ my $logger = shift;
+ my $gitCmd = findBin("git",$logger);
+ shellex("$gitCmd stash",$logger);
+ dropStash($logger);
# TODO: Depending on use case need to do more here
shellex("$gitCmd rebase",$logger);
}

-sub resetFromUpstream {
+sub resetFromUpstream($) {

# git stash and git reset --hard and git pull ? I think
+ # git reset upstream/master; git stash
+ my $logger = shift;
+ my $gitCmd = findBin("git",$logger);
+ my $upstream = shellex("$gitCmd config --get remote.upstream.url",$logger);
+ if ( $upstream eq "" || ! defined $upstream ) {
+ print "Upstream not configured, exiting\n";
+ exit 1;
+ }
+
+ shellex("$gitCmd reset upstream/master",$logger);
+ shellex("$gitCmd stash",$logger);
+ dropStash($logger);
+ print "Successful reset from upstream\n";
+ print "Changes have not been pushed, run \'$gitCmd pull\' to revert\n";

}

diff --git a/sg b/sg
index 29ce565..1ec2459 100755
--- a/sg
+++ b/sg
@@ -64,7 +64,6 @@ GetOptions(
'view',
'reset-from-master',
'reset-from-upstream',
- 'branch-from-master',
'commit-msg=s',
'dump-config',
'configure-local-user',
@@ -94,8 +93,8 @@ Usage:
--reset-from-master
Reset all current changes so that the file tree matches upstream/master

- --branch-from-master
- Create a new clean branch from upstream/master
+ --reset-from-upstream
+ If upstream is defined will reset local branch to match upstream ( does not push changes by default )

--configure-local-user [--user,--email]
Configure local git user
@@ -166,6 +165,11 @@ sub parseArgs {
}
}

+ if ( defined $args{'reset-from-upstream'} && scalar keys %args > 1 ) {
+ print "Cannot call --reset-from-upstream with any other args\n";
+ exit 1;
+ }
+
}

parseArgs();
@@ -181,7 +185,7 @@ if ( defined $args{'view'} ) {
chomp $name;
my $email = shellex("$gitCmd config --get user.email",$logger);
chomp $email;
- print "Username: $name\nEmail: $email\n";
+ print "Username: $name Email: $email\n";
print "On [branch] @ commit: $branch";
print "$refs\n";
my $swpWarning = "\t# Likely a Vi .swp file";
@@ -312,7 +316,6 @@ if ( defined $args{'reset-from-master'} ) {

if ( defined $args{'reset-from-upstream'} ) {

- print "This does nothing right now\n";
resetFromUpstream($logger);

}
@@ -349,3 +352,5 @@ if ( defined $args{'configure-local-user'} ) {
}

}
+
+