Browse Source

Added stash/reset functionality and --commit-msg opt

Simon Waton 5 years ago
parent
commit
82689837bd
2 changed files with 37 additions and 5 deletions
  1. 21 1
      lib/SimplyGit/Git.pm
  2. 16 4
      sg

+ 21 - 1
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);
+our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset);
 
 sub readConfig {
 
@@ -99,6 +99,8 @@ sub addFiles {
 
 }
 
+# TODO: Possibly worth returning output for commitChanges(), pushChanges(), stashAndReset, even if I'm not using it right now
+
 sub commitChanges {
 
 	my $commitMsg = shift;
@@ -116,3 +118,21 @@ sub pushChanges {
 	my $output = shellex("$gitCmd push",$logger);
 
 }
+
+sub stashAndReset {
+
+	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;
+	foreach my $stashNum ( 1..$stashCount ) {
+		shellex("$gitCmd stash drop 0",$logger);
+	}
+
+	# TODO: Depending on use case need to do more here
+	shellex("$gitCmd rebase",$logger);
+}
+
+
+

+ 16 - 4
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);
+use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset);
 
 if ( ! -d ".git" ) {
 	print "Not a git dir, exiting...\n";
@@ -60,6 +60,7 @@ GetOptions(
 	'view',
 	'reset-from-master',
 	'branch-from-master',
+	'commit-msg=s',
 );
 
 sub printHelp {
@@ -70,8 +71,10 @@ Usage:
 	--view
 	Display git status of files and other information
 
-	--push-all
-	Push all untracked and modified files (can be used with interactive mode)
+	--push-all [--commit-msg]
+	Push all untracked and modified files 
+		* (can be used with interactive mode)
+		* can provide a commit msg with --commit-msg (otherwise a generic will be provided)
 
 	--interactive
 	Enable interactive mode with supported opts
@@ -183,11 +186,14 @@ if ( defined $args{'push-all'} ) {
 	
 	addFiles(\@filesToCommit,$logger);
 
-	if ( defined $args{'interactive'} ) {
+	if ( defined $args{'interactive'} && ! defined $args{'commit-msg'}) {
 		print "Enter a commit message: ";
 		my $input = <STDIN>;
 		chomp $input;
 		commitChanges($input,$logger);
+	} elsif ( defined $args{'commit-msg'} ) { 
+		my $commitMsg = "$args{'commit-msg'}";
+		commitChanges($commitMsg,$logger);
 	} else {
 		my $epoch = time();
 		my $commitMsg = "Generic Commit at $epoch";
@@ -217,3 +223,9 @@ if ( defined $args{'push-all'} ) {
 
 
 }
+
+if ( defined $args{'reset-from-master'} ) {
+
+	stashAndReset($logger);
+
+}