All of lore.kernel.org
 help / color / mirror / Atom feed
* [RFC] Git config file reader in Perl (WIP)
@ 2007-01-15  0:44 Jakub Narebski
  2007-01-15  7:08 ` Eric Wong
  0 siblings, 1 reply; 54+ messages in thread
From: Jakub Narebski @ 2007-01-15  0:44 UTC (permalink / raw)
  To: git

To make gitweb faster I thought about adding to it, or to Git.pm,
simple nonvalidation config file reader. Nonvalidating means that
it would accept some input which git-repo-config considers invalid.

Some of the trouble is caused because of coner cases like this 
example

[section "sub ; # sect \" ion\\"] ; "]
	key = a " b ; " ; " c ; "

which is valid, but strange.

I'm not proficent in Perl, so help is appreciated.

-- >8 --
#!/usr/bin/perl

use strict;
use warnings;

use Text::Balanced qw(extract_delimited);


sub read_config {
	my $configfile = shift;
	my $section = shift;
	my %config;

	open my $fd, $configfile
		or die "Cannot open $configfile: $!";

	my $sectfull;
	while (my $line = <$fd>) {
		chomp $line;

		if ($line =~ m/^\s*\[\s*([^][:space:]]*)\s*\](.*)$/) {
			# section without subsection

			my $sect = lc($1);

			$sectfull = $sect;

		} elsif ($line =~ m/\s*\[([^][:space:]]*)\s"((?:\\.|[^"])*)"\](.*)$/) {
			# section with subsection

			my $sect = lc($1);
			my $subsect = $2;
			$subsect =~ s/\\(.)/$1/g;

			$sectfull = "$sect.$subsect";

		} elsif ($line =~ m/\s*(\w+)\s*=\s*(.*?)\s*$/) {
			# variable assignment

			my $key = lc($1);
			my $rhs = $2;

			my $value = '';
			my ($next, $remainder, $prefix) = qw();
		DELIM: {
				do {
					($next, $remainder, $prefix) =
						extract_delimited($rhs, '"', qr/(?:\\.|[^"])*/);

					if ($prefix =~ s/\s*[;#].*$//) {
						# comment in unquoted part
						$value .= $prefix;
						last DELIM;
					} else {
						$value .= $prefix if $prefix;
						if ($next && $next =~ s/^"(.*)"$/$1/) {
							$value .= $next;
						}
					}

					$rhs = $remainder;
				} while ($rhs && $next);
			} # DELIM:

			if ($remainder) {
				$remainder =~ s/\s*[;#].*$//;
				$value .= $remainder;
			}

			$value =~ s/\\(.)/$1/g;

			if (exists $config{"$sectfull.$key"}) {
				push @{$config{"$sectfull.$key"}}, $value;
			} else {
				$config{"$sectfull.$key"} = [ $value ];
			}

		} elsif ($line =~ m/^\s*(\w+)\s*(:?[;#].*)?$/) {
			# boolean variable without value

			my $key = lc($1);

			if (exists $config{"$sectfull.$key"}) {
				push @{$config{"$sectfull.$key"}}, undef;
			} else {
				$config{"$sectfull.$key"} = [ undef ];
			}
		} # end if
	}

	close $fd
		or die "Cannot close $configfile: $!";

	return wantarray ? %config : \%config;
}

# --------------------------------------------------------------------------

my %config;

%config = read_config("~/git/.git/config");
%config = read_config("/tmp/jnareb/gitconfig");

foreach my $ckey (sort keys %config) {
	foreach my $cvalue (@{$config{$ckey}}) {
		if (defined $cvalue) {
			print "$ckey=$cvalue\n";
		} else {
			print "$ckey\n";
		}
	}
}

__END__

^ permalink raw reply	[flat|nested] 54+ messages in thread

end of thread, other threads:[~2007-01-24 14:14 UTC | newest]

Thread overview: 54+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-15  0:44 [RFC] Git config file reader in Perl (WIP) Jakub Narebski
2007-01-15  7:08 ` Eric Wong
2007-01-15  9:03   ` Jakub Narebski
2007-01-15  9:56     ` Eric Wong
2007-01-15 10:01       ` Shawn O. Pearce
2007-01-15 10:32       ` Jakub Narebski
2007-01-15 11:26         ` Eric Wong
2007-01-15 12:15           ` Johannes Schindelin
2007-01-15 15:34             ` Nikolai Weibull
2007-01-15 15:44               ` Johannes Schindelin
2007-01-15 16:22                 ` Nikolai Weibull
2007-01-15 16:00               ` Jakub Narebski
2007-01-16 10:45               ` Junio C Hamano
2007-01-16 11:12                 ` Johannes Schindelin
2007-01-16 14:14                   ` Jakub Narebski
2007-01-16 22:17                     ` Nikolai Weibull
2007-01-16 22:37                       ` Jakub Narebski
2007-01-16 22:56                         ` Johannes Schindelin
2007-01-16 23:24                           ` Jakub Narebski
2007-01-17  8:51                             ` Johannes Schindelin
2007-01-17  9:48                               ` Jakub Narebski
2007-01-17 10:44                                 ` Johannes Schindelin
2007-01-17 12:11                                   ` Jakub Narebski
2007-01-17 12:37                                     ` Johannes Schindelin
2007-01-17 14:00                                       ` Jakub Narebski
2007-01-19 12:10                                         ` Jakub Narebski
2007-01-19 12:25                                           ` Jakub Narebski
2007-01-19 13:20                                           ` Johannes Schindelin
2007-01-19 22:44                                             ` Jakub Narebski
2007-01-20  0:08                                               ` Johannes Schindelin
2007-01-20  0:59                                                 ` Jakub Narebski
2007-01-20  0:19                                               ` Junio C Hamano
2007-01-20  1:25                                                 ` [PATCH] config_set_multivar(): disallow newlines in keys Johannes Schindelin
2007-01-20  1:40                                                   ` Junio C Hamano
2007-01-22 15:06                                                   ` Alex Riesen
2007-01-22 15:21                                                     ` Johannes Schindelin
2007-01-22 15:33                                                       ` Alex Riesen
2007-01-22 15:44                                                         ` Johannes Schindelin
2007-01-22 16:09                                                           ` Alex Riesen
2007-01-23 11:26                                                             ` Johannes Schindelin
2007-01-23 12:47                                                               ` Alex Riesen
2007-01-20 14:03                                                 ` [PATCH] Documentation/config.txt: Document config file syntax better Jakub Narebski
2007-01-22 15:25                                                   ` Jakub Narebski
2007-01-24 14:14                                                     ` [PATCH 2/1] Documentation/config.txt: Correct info about subsection name Jakub Narebski
2007-01-16 22:42                       ` [RFC] Git config file reader in Perl (WIP) Johannes Schindelin
2007-01-17 18:08                         ` Nikolai Weibull
2007-01-17 19:22                           ` Jakub Narebski
2007-01-17 20:01                             ` Nikolai Weibull
2007-01-17 19:25                           ` Jakub Narebski
2007-01-18  0:50                           ` Johannes Schindelin
2007-01-16 19:09                   ` Eric Wong
2007-01-16  9:51             ` Eric Wong
2007-01-16 10:47               ` Johannes Schindelin
2007-01-16 19:53                 ` Eric Wong

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.