[Bps-public-commit] Config-GitLike branch, master, updated. 1.09-4-g9c16981

Alex Vandiver alexmv at bestpractical.com
Wed Nov 7 00:11:56 EST 2012


The branch, master has been updated
       via  9c1698176975cf549672f214955b1ff05b3ef005 (commit)
       via  7633fb36f2a3959475348b4eb36fbabd39f2c7d2 (commit)
       via  5ab06fe138f50781a7d323e96271b644ca31a15f (commit)
       via  57d7bfb38166a3087625792239ebb7ab5c7e8d57 (commit)
      from  d2f0cf0a70826bddab2f3e7fc8143420f315589c (commit)

Summary of changes:
 lib/Config/GitLike.pm | 67 +++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 52 insertions(+), 15 deletions(-)

- Log -----------------------------------------------------------------
commit 57d7bfb38166a3087625792239ebb7ab5c7e8d57
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Tue Nov 6 22:53:06 2012 -0500

    Factor out a method to return the canonical case of a key

diff --git a/lib/Config/GitLike.pm b/lib/Config/GitLike.pm
index 8894466..1bd3de7 100644
--- a/lib/Config/GitLike.pm
+++ b/lib/Config/GitLike.pm
@@ -520,10 +520,7 @@ sub _get {
     );
     $self->load unless $self->is_loaded;
 
-    my ($section, $subsection, $name) = _split_key($args{key});
-    $args{key} = join( '.',
-        grep { defined } (lc $section, $subsection, lc $name),
-    );
+    $args{key} = $self->canonical_case( $args{key} );
 
     return () unless exists $self->data->{$args{key}};
     my $v = $self->data->{$args{key}};
@@ -635,6 +632,15 @@ sub get_regexp {
     return wantarray ? %results : \%results;
 }
 
+sub canonical_case {
+    my $self = shift;
+    my ($key) = @_;
+    my ($section, $subsection, $name) = _split_key($key);
+    return join( '.',
+        grep { defined } (lc $section, $subsection, lc $name),
+    );
+}
+
 sub dump {
     my $self = shift;
 
@@ -751,9 +757,7 @@ sub group_set {
         my %args = %{$args_hash};
 
         my ($section, $subsection, $name) = _split_key($args{key});
-        my $key = join( '.',
-            grep { defined } (lc $section, $subsection, lc $name),
-        );
+        my $key = $self->canonical_case( $args{key} );
 
         $args{multiple} = $self->is_multiple($key)
             unless defined $args{multiple};
@@ -1753,6 +1757,12 @@ Return a string containing the key/value pair as they should be printed in the
 config file. If C<bare> is true, the returned value is not tab-indented nor
 followed by a newline.
 
+=head2 canonical_case( $name )
+
+Given a full key name, returns the canonical name of the key; this is
+the key with the section and name lower-cased; the subsection is left
+as-is.
+
 =head1 DIFFERENCES FROM GIT-CONFIG
 
 This module does the following things differently from git-config:

commit 5ab06fe138f50781a7d323e96271b644ca31a15f
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Tue Nov 6 22:55:48 2012 -0500

    Move $key assignment until after all parts of it have been checked
    
    This avoids warnings (such as were generated by t/t1300-repo-config!)
    when ->set was called with an invalid key.

diff --git a/lib/Config/GitLike.pm b/lib/Config/GitLike.pm
index 1bd3de7..51e3924 100644
--- a/lib/Config/GitLike.pm
+++ b/lib/Config/GitLike.pm
@@ -757,10 +757,6 @@ sub group_set {
         my %args = %{$args_hash};
 
         my ($section, $subsection, $name) = _split_key($args{key});
-        my $key = $self->canonical_case( $args{key} );
-
-        $args{multiple} = $self->is_multiple($key)
-            unless defined $args{multiple};
 
         die "No section given in key or invalid key $args{key}\n"
             unless defined $section;
@@ -789,6 +785,11 @@ sub group_set {
         my $new;
         my @replace;
 
+        my $key = $self->canonical_case( $args{key} );
+
+        $args{multiple} = $self->is_multiple($key)
+            unless defined $args{multiple};
+
         # use this for comparison
         my $cmp_section =
           defined $unescaped_subsection

commit 7633fb36f2a3959475348b4eb36fbabd39f2c7d2
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Tue Nov 6 22:56:39 2012 -0500

    Canonicalize case of keys passed to is_multiple and set_multiple

diff --git a/lib/Config/GitLike.pm b/lib/Config/GitLike.pm
index 51e3924..ac71a12 100644
--- a/lib/Config/GitLike.pm
+++ b/lib/Config/GitLike.pm
@@ -66,14 +66,14 @@ has 'encoding' => (
 sub set_multiple {
     my $self = shift;
     my ($name, $mult) = (@_, 1);
-    $self->multiple->{$name} = $mult;
+    $self->multiple->{ $self->canonical_case( $name ) } = $mult;
 }
 
 sub is_multiple {
     my $self = shift;
     my $name = shift;
     return if !defined $name;
-    return $self->multiple->{$name};
+    return $self->multiple->{ $self->canonical_case( $name ) };
 }
 
 sub load {

commit 9c1698176975cf549672f214955b1ff05b3ef005
Author: Alex Vandiver <alexmv at bestpractical.com>
Date:   Tue Nov 6 23:25:12 2012 -0500

    Track and allow lookup of the case that keys were defined in
    
    Git canonicalizes the section and key name into lower case for all
    lookups; subsection names are case-sensitive.  However, there are times
    when is is useful to be able to determine the original case used to set
    the key.
    
    Store the original case, and provide a 'original_case' method to
    retrieve the kase used for a given key.  This also alters the 'name'
    argument to the 'callback' of 'parse_content' to be the original case,
    and not the canonical lower case.

diff --git a/lib/Config/GitLike.pm b/lib/Config/GitLike.pm
index ac71a12..baf1931 100644
--- a/lib/Config/GitLike.pm
+++ b/lib/Config/GitLike.pm
@@ -31,6 +31,12 @@ has 'multiple' => (
     default => sub { +{} },
 );
 
+has 'casing' => (
+    is => 'rw',
+    isa => 'HashRef',
+    default => sub { +{} },
+);
+
 # filename where the definition of each key was loaded from
 has 'origins' => (
     is => 'rw',
@@ -301,7 +307,7 @@ sub parse_content {
             ) unless defined $section;
             $args{callback}->(
                 section    => $section,
-                name       => lc $1,
+                name       => $1,
                 offset     => $offset,
                 length     => ($length - length($c)) - $offset,
             );
@@ -313,7 +319,7 @@ sub parse_content {
                 content => $args{content},
                 offset  => $offset,
             ) unless defined $section;
-            my $name = lc $1;
+            my $name = $1;
             my $value = "";
             # parse the value
             while (1) {
@@ -413,6 +419,7 @@ sub define {
         @_,
     );
     return unless defined $args{section} and defined $args{name};
+    my $original_case = join(".", @args{qw/section name/});
     $args{name} = lc $args{name};
     my $key = join(".", @args{qw/section name/});
 
@@ -422,13 +429,16 @@ sub define {
         || $self->origins->{$key} eq $args{origin} ) {
         if ($self->is_multiple($key)) {
             push @{$self->data->{$key} ||= []}, $args{value};
+            push @{$self->casing->{$key} ||= []}, $original_case;
         }
         elsif (exists $self->data->{$key}) {
             $self->set_multiple($key);
             $self->data->{$key} = [$self->data->{$key}, $args{value}];
+            $self->casing->{$key}  = [$self->casing->{$key}, $original_case];
         }
         else {
             $self->data->{$key} = $args{value};
+            $self->casing->{$key} = $original_case;
         }
     }
     # we're overriding a key set previously from a different file
@@ -438,6 +448,7 @@ sub define {
 
         # set the new value
         $self->data->{$key} = $args{value};
+        $self->casing->{$key} = $original_case;
     }
     $self->origins->{$key} = $args{origin};
 }
@@ -632,6 +643,12 @@ sub get_regexp {
     return wantarray ? %results : \%results;
 }
 
+sub original_case {
+    my $self = shift;
+    my ($key) = @_;
+    return $self->casing->{ $self->canonical_case( $key ) };
+}
+
 sub canonical_case {
     my $self = shift;
     my ($key) = @_;
@@ -652,6 +669,8 @@ sub dump {
     for my $key (sort keys %{$self->data}) {
         my $str;
         if (defined $self->data->{$key}) {
+            # For git compat, we intentionally always write out in
+            # canonical (i.e. lower) case.
             $str = "$key=";
             if ( $self->is_multiple($key) ) {
                 $str .= '[';
@@ -818,7 +837,7 @@ sub group_set {
 
                 my $matched = 0;
                 # variable names are case-insensitive
-                if (lc $name eq $got{name}) {
+                if (lc $name eq lc $got{name}) {
                     if (defined $args{filter} and length $args{filter}) {
                         # copy the filter arg here since this callback may
                         # be called multiple times and we don't want to
@@ -1764,6 +1783,13 @@ Given a full key name, returns the canonical name of the key; this is
 the key with the section and name lower-cased; the subsection is left
 as-is.
 
+=head2 original_case( $name )
+
+Given a full key name, returns the casing of the name as it was last
+loaded from the file.  Note that for multiple-valued keys, this returns
+an array reference of casings, as each definition may have been provided
+in a different choice of case.
+
 =head1 DIFFERENCES FROM GIT-CONFIG
 
 This module does the following things differently from git-config:

-----------------------------------------------------------------------



More information about the Bps-public-commit mailing list