Browse Source

Added functionality to add upstream

spesk1 5 years ago
parent
commit
2eb4277f87
2 changed files with 28 additions and 7 deletions
  1. 1 1
      lib/SimplyGit/Shellex.pm
  2. 27 6
      sg

+ 1 - 1
lib/SimplyGit/Shellex.pm

@@ -24,7 +24,7 @@ sub shellex {
 			$logger->error("$cmd returned non-zero: $rc");
 		}
 
-		print "$cmd returned non-zero: $rc, exiting\n";
+		print "$cmd returned non-zero: $rc\n";
 		exit 1;
 	}
 

+ 27 - 6
sg

@@ -64,6 +64,7 @@ GetOptions(
 	'view',
 	'reset-from-master',
 	'reset-from-upstream',
+	'upstream-url=s',
 	'commit-msg=s',
 	'dump-config',
 	'configure-local-user',
@@ -93,8 +94,10 @@ Usage:
 	--reset-from-master
 	Reset all current changes so that the file tree matches upstream/master
 
-	--reset-from-upstream
+	--reset-from-upstream [ --upstream-url ]
 	If upstream is defined will reset local branch to match upstream ( does not push changes by default )
+		* Assumes you have an upstream configured
+		* Pass SSH/HTTPS URL to --upstream-url to add an upstream
 
 	--configure-local-user [--user,--email]
 	Configure local git user
@@ -165,9 +168,20 @@ 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;
+	if ( defined $args{'reset-from-upstream'} ) {
+		if ( scalar keys %args > 2  ) {
+			print "Can only pass --upstream-url with --reset-from-upstream\n";
+			exit 1;
+		}
+
+		foreach my $arg ( keys %args ) {
+			if ( $arg eq "reset-from-upstream" || $arg eq "upstream-url" ) {
+				next;
+			} else {
+				print "Can only pass --upstream-url with --reset-from-upstream\n";
+				exit 1;
+			}
+		}
 	}
 
 }
@@ -316,8 +330,15 @@ if ( defined $args{'reset-from-master'} ) {
 
 if ( defined $args{'reset-from-upstream'} ) {
 
-	resetFromUpstream($logger);
-
+	if ( defined $args{'upstream-url'} ) {
+		print "Setting upstream to $args{'upstream-url'}\n";
+		chomp $args{'upstream-url'};
+		shellex("$gitCmd remote add upstream $args{'upstream-url'}",$logger);
+		shellex("$gitCmd fetch upstream",$logger);
+		resetFromUpstream($logger);
+	} else {
+		resetFromUpstream($logger);
+	}
 }
 
 if ( defined $args{'dump-config'} ) {