8d4223e6a0c9266f5a5d3d64c716088f983ecac6
commit 8d4223e6a0c9266f5a5d3d64c716088f983ecac6
Author: Simon Waton <spw01@protonmail.com>
Date: Mon May 20 23:55:10 2019 -0400

First working implementation of readConfig sub

diff --git a/lib/SimplyGit/Git.pm b/lib/SimplyGit/Git.pm
index b69a246..a1002fd 100644
--- a/lib/SimplyGit/Git.pm
+++ b/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;
diff --git a/sg b/sg
index 7c6928a..392d8f1 100755
--- a/sg
+++ b/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";
+ }
}

}