Browse Source

Trying to figure out readConfig

Simon Waton 6 years ago
parent
commit
6220d9670d
3 changed files with 62 additions and 11 deletions
  1. 5 0
      README.md
  2. 34 10
      lib/SimplyGit/Git.pm
  3. 23 1
      sg

+ 5 - 0
README.md

@@ -2,8 +2,13 @@
 
 
 Project to abstract some of the weirder git operations. Should always be able to be used alongside git, as opposed to trying to replace it outright.
 Project to abstract some of the weirder git operations. Should always be able to be used alongside git, as opposed to trying to replace it outright.
 
 
+Mainly writing to learn more about Git and Perl, unlikely to be widely useful.
+
 TODO:
 TODO:
 
 
+--read-config
+* dump git config (mainly a sub to be used internally)
+
 --reset-from-master
 --reset-from-master
 * add soft and hard functionality, right now only 'soft' is implemented
 * add soft and hard functionality, right now only 'soft' is implemented
 
 

+ 34 - 10
lib/SimplyGit/Git.pm

@@ -5,17 +5,17 @@ use Log::Log4perl qw(:easy);
 use lib ".";
 use lib ".";
 use SimplyGit::Shellex qw(shellex findBin);
 use SimplyGit::Shellex qw(shellex findBin);
 use Exporter qw(import);
 use Exporter qw(import);
-our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset);
+our @EXPORT_OK = qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream);
 
 
 sub readConfig {
 sub readConfig {
 
 
-	# TODO: This sub is probably not really needed for what I'm trying to do
-	# git itself already parses this config...
+	# 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 $path = shift;
 	my $logger = shift;
 	my $logger = shift;
-	# TODO: Should pass in logger object as opposed to printing
 	if ( ! -d $path ) {
 	if ( ! -d $path ) {
-		print "readConfig did not recieve expected dir, exiting...\n";
+		$logger->error("$path doesn't look like a dir, exiting...");
 		exit 1;
 		exit 1;
 	}
 	}
 	my $gitConfigPath = $path . "/" . ".git/config";
 	my $gitConfigPath = $path . "/" . ".git/config";
@@ -23,17 +23,33 @@ sub readConfig {
 	my @configLines = split("\n",shellex("$catCmd $gitConfigPath",$logger));
 	my @configLines = split("\n",shellex("$catCmd $gitConfigPath",$logger));
 	my %gitConfig;
 	my %gitConfig;
 	my @valueLines;
 	my @valueLines;
-	my $headerCounter = 0;
+	my $lineCounter = 0;
 	foreach my $line ( @configLines ) {
 	foreach my $line ( @configLines ) {
-		if ( $line =~ m/\[(.*)\]/ ) {
+		$lineCounter++;
+		#if ( $line =~ m/\[(.*)\]/ ) {
+		if ( $line =~ m/(\[.*\])/ ) {
 			#$valueLine =~ /\t(.*)\ =\ (.*)$/;
 			#$valueLine =~ /\t(.*)\ =\ (.*)$/;
-			$gitConfig{$1} = "";
-		} else {
-			push(@valueLines, $line);
+			$gitConfig{$1} = $lineCounter;
 		}
 		}
 
 
 	}
 	}
 
 
+	my @lineRange = values %gitConfig;
+	my @sortedRange = sort { $a <=> $b } @lineRange;
+	my $prevVal ;
+	foreach my $val ( @sortedRange ) {
+		print "Val is $val\n";
+		if ( ! defined $prevVal ) {
+			$prevVal = $val;
+			next;
+		} else {
+			print "Current var is $val and prevVar $prevVal\n";
+			my $lineDiff = ( $val - 1 ) . "-" . ( $prevVal + 1 );
+			print "lineDiff of vals is: $lineDiff \n";
+			$prevVal = $val;
+		}
+	}
+
 	return %gitConfig;
 	return %gitConfig;
 
 
 }
 }
@@ -134,5 +150,13 @@ sub stashAndReset {
 	shellex("$gitCmd rebase",$logger);
 	shellex("$gitCmd rebase",$logger);
 }
 }
 
 
+sub resetFromUpstream {
 
 
+	# git stash and git reset --hard and git pull ? I think
 
 
+}
+
+sub appendRepoUserConfig {
+
+
+}

+ 23 - 1
sg

@@ -8,7 +8,7 @@ use Log::Log4perl qw(:easy);
 # TODO: This needs to be scoped properly
 # TODO: This needs to be scoped properly
 use lib "/usr/local/lib";
 use lib "/usr/local/lib";
 use SimplyGit::Shellex qw(shellex findBin);
 use SimplyGit::Shellex qw(shellex findBin);
-use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset);
+use SimplyGit::Git qw(readConfig getStatus returnState addFiles commitChanges pushChanges stashAndReset resetFromUpstream);
 
 
 if ( ! -d ".git" ) {
 if ( ! -d ".git" ) {
 	print "Not a git dir, exiting...\n";
 	print "Not a git dir, exiting...\n";
@@ -59,8 +59,10 @@ GetOptions(
 	'interactive',
 	'interactive',
 	'view',
 	'view',
 	'reset-from-master',
 	'reset-from-master',
+	'reset-from-upstream',
 	'branch-from-master',
 	'branch-from-master',
 	'commit-msg=s',
 	'commit-msg=s',
+	'read-config',
 );
 );
 
 
 sub printHelp {
 sub printHelp {
@@ -146,6 +148,10 @@ if ( defined $args{'view'} ) {
 	}
 	}
 }
 }
 
 
+###
+# TODO: This args processing is dangerous and incomplete, need to write an args processor function
+###
+
 # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
 # This functionality is mainly here to test building out Git.pm, and doesn't really offer anything that regular git doesn't
 if ( defined $args{'push-all'} ) {
 if ( defined $args{'push-all'} ) {
 
 
@@ -229,3 +235,19 @@ if ( defined $args{'reset-from-master'} ) {
 	stashAndReset($logger);
 	stashAndReset($logger);
 
 
 }
 }
+
+if ( defined $args{'reset-from-upstream'} ) {
+
+	print "This does nothing right now\n";
+	resetFromUpstream($logger);
+
+}
+
+if ( defined $args{'read-config'} ) {
+
+	my %configHash = readConfig(".",$logger);
+	foreach my $key ( keys %configHash ) {
+		print "key is $key, vals is: $configHash{$key}\n";
+	}
+
+}