Browse Source

First working implementation of readConfig sub

Spesk1 5 years ago
parent
commit
4e4f21ae83
2 changed files with 38 additions and 30 deletions
  1. 28 27
      lib/SimplyGit/Git.pm
  2. 10 3
      sg

+ 28 - 27
lib/SimplyGit/Git.pm

@@ -26,47 +26,48 @@ sub readConfig {
 	my $gitConfigPath = $path . "/" . ".git/config";
 	my $catCmd = findBin("cat",$logger);
 	my @configLines = split("\n",shellex("$catCmd $gitConfigPath",$logger));
+	# Key is config header, value is hash ref containing config values
 	my %gitConfig;
 	my @valueLines;
 	my $lineCounter = 0;
 	foreach my $line ( @configLines ) {
 		$lineCounter++;
 		#if ( $line =~ m/\[(.*)\]/ ) {
-		if ( $line =~ m/(\[.*\])/ ) {
+		if ( $line =~ m/\[(.*)\]/ ) {
 			#$valueLine =~ /\t(.*)\ =\ (.*)$/;
-			$gitConfig{$1} = $lineCounter;
+			$gitConfig{$1} = "";
 		}
 
 	}
 
-	my $wcBin = findBin("wc",$logger);
-	# TODO: Clean shell call up
-	my $lastLine = shellex("$wcBin -l $gitConfigPath | awk '{print \$1}'",$logger);
-	chomp $lastLine;
-	my @lineRange = values %gitConfig;
-	push(@lineRange, $lastLine);
-	my @sortedRange = sort { $a <=> $b } @lineRange;
-	my $prevVal;
-	foreach my $val ( @sortedRange ) {
-		my @stash;
-		print "Val is $val\n";
-		if ( ! defined $prevVal ) {
-			$prevVal = $val;
-			next;
+	# Tag each line with it's heading
+	# Only way I could think of that worked to solve how this
+	# There are almost certainly better ways
+	# TODO
+	my @taggedLines;
+	my $tag = "NULLTAG";
+	foreach my $line ( @configLines ) {
+		if ( $line =~ m/\[(.*)\]/ ) {
+			$tag = $1;
 		} else {
-			print "Current var is $val and prevVar $prevVal\n";
-			my $lineDiff = ( $prevVal + 1 ) . "-" . ( $val - 1 );
-			my $lineCounter = 0;
-			foreach my $line ( @configLines ) {
-				$lineCounter++;
-				#if ( $lineCounter >= ( $prevVal + 1 ) && $lineCounter <= ( $val - 1 ) ) {
-				if ( $lineCounter >= $prevVal && $lineCounter <= $val ) {
-					print "$line\n";
-				}
+			my $newLine = $tag . $line;
+			push(@taggedLines,$newLine);
+		}
+	}
+
+	# Get all of the tagged lines into a hash structure.
+	foreach my $key ( keys %gitConfig ) {
+		my %stash;
+		foreach my $tl ( @taggedLines ) {
+			if ( $tl =~ m/^($key)/ ) {
+				$tl =~ s/^($key)//g;
+				$tl =~ m/^\t(.*)\ \=\ (.*)$/;
+				my $confKey = $1;
+				my $confVal = $2;
+				$stash{$confKey} = $confVal;
 			}
-			print "lineDiff of vals is: $lineDiff \n";
-			$prevVal = $val;
 		}
+		$gitConfig{$key} = \%stash;
 	}
 
 	return %gitConfig;

+ 10 - 3
sg

@@ -62,7 +62,7 @@ GetOptions(
 	'reset-from-upstream',
 	'branch-from-master',
 	'commit-msg=s',
-	'read-config',
+	'dump-config',
 );
 
 sub printHelp {
@@ -73,6 +73,9 @@ Usage:
 	--view
 	Display git status of files and other information
 
+	--dump-config
+	Dump .git/config to STDOUT. Not really useful but exposed for testing of reading config into internal data structure
+
 	--push-all [--commit-msg]
 	Push all untracked and modified files 
 		* (can be used with interactive mode)
@@ -243,11 +246,15 @@ if ( defined $args{'reset-from-upstream'} ) {
 
 }
 
-if ( defined $args{'read-config'} ) {
+if ( defined $args{'dump-config'} ) {
 
 	my %configHash = readConfig(".",$logger);
 	foreach my $key ( keys %configHash ) {
-		print "key is $key, vals is: $configHash{$key}\n";
+		my $hRef = $configHash{$key};
+		print "[$key]\n";
+		foreach my $ckey ( keys %$hRef ) {
+			print "\t$ckey = ${$hRef}{$ckey}\n";
+		}
 	}
 
 }