@matheusbsbs / matheus-bagliano / commits / b201927ac8
Fork 0

gitweb: Read repo config using 'git config -z -l'

Change git_get_project_config to run git-config only once per repository, without changing its signature (its calling convention). This means for example that it returns 'true' or 'false' when called with second argument '--bool', and not true or false value. Instead of calling 'git config [<type>] --get gitweb.<key>' once for each config variable, call 'git config -z -l' only once, parsing and saving its output to %config variable. This makes possible to add new per repository configuration without paying cost of forking once per variable checked. We can now allow repository description and repository URLs to be stored in config file without badly affecting gitweb performance. For now only configuration variables for 'gitweb' section are stored. Multiple values for single configuration variable are stored as anonymous array reference; configuration variable with no value is stored as undef. Converting configuration variable values to boolean or integer value are done in Perl. Results differ from git-config in the fact that no conversion error is ever raised. For boolean values no value, 'true' (any case) and 'false' (any case) are considered true, numbers are true if not zero; all other values (even invalid for bool) are considered false. For integer values value suffix of 'k', 'm', or 'g' following decimal number will cause the value to be multiplied by 1024, 1048576, or 1073741824; other values are returned as-is, only whitespace stripped. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Nov 3, 2007 at 00:41 UTC b201927ac8a4544baa067f5c5c7c7f2d6780e29a
1 file changed +108 -7
gitweb/gitweb.perl
+108 -7
@@ -1432,20 +1432,121 @@ sub git_get_type {
1432 return $type;
1433 }
1434
1435 +# repository configuration
1436 +our $config_file = '';
1437 +our %config;
1438 +
1439 +# store multiple values for single key as anonymous array reference
1440 +# single values stored directly in the hash, not as [ <value> ]
1441 +sub hash_set_multi {
1442 + my ($hash, $key, $value) = @_;
1443 +
1444 + if (!exists $hash->{$key}) {
1445 + $hash->{$key} = $value;
1446 + } elsif (!ref $hash->{$key}) {
1447 + $hash->{$key} = [ $hash->{$key}, $value ];
1448 + } else {
1449 + push @{$hash->{$key}}, $value;
1450 + }
1451 +}
1452 +
1453 +# return hash of git project configuration
1454 +# optionally limited to some section, e.g. 'gitweb'
1455 +sub git_parse_project_config {
1456 + my $section_regexp = shift;
1457 + my %config;
1458 +
1459 + local $/ = "\0";
1460 +
1461 + open my $fh, "-|", git_cmd(), "config", '-z', '-l',
1462 + or return;
1463 +
1464 + while (my $keyval = <$fh>) {
1465 + chomp $keyval;
1466 + my ($key, $value) = split(/\n/, $keyval, 2);
1467 +
1468 + hash_set_multi(\%config, $key, $value)
1469 + if (!defined $section_regexp || $key =~ /^(?:$section_regexp)\./o);
1470 + }
1471 + close $fh;
1472 +
1473 + return %config;
1474 +}
1475 +
1476 +# convert config value to boolean, 'true' or 'false'
1477 +# no value, number > 0, 'true' and 'yes' values are true
1478 +# rest of values are treated as false (never as error)
1479 +sub config_to_bool {
1480 + my $val = shift;
1481 +
1482 + # strip leading and trailing whitespace
1483 + $val =~ s/^\s+//;
1484 + $val =~ s/\s+$//;
1485 +
1486 + return (!defined $val || # section.key
1487 + ($val =~ /^\d+$/ && $val) || # section.key = 1
1488 + ($val =~ /^(?:true|yes)$/i)); # section.key = true
1489 +}
1490 +
1491 +# convert config value to simple decimal number
1492 +# an optional value suffix of 'k', 'm', or 'g' will cause the value
1493 +# to be multiplied by 1024, 1048576, or 1073741824
1494 +sub config_to_int {
1495 + my $val = shift;
1496 +
1497 + # strip leading and trailing whitespace
1498 + $val =~ s/^\s+//;
1499 + $val =~ s/\s+$//;
1500 +
1501 + if (my ($num, $unit) = ($val =~ /^([0-9]*)([kmg])$/i)) {
1502 + $unit = lc($unit);
1503 + # unknown unit is treated as 1
1504 + return $num * ($unit eq 'g' ? 1073741824 :
1505 + $unit eq 'm' ? 1048576 :
1506 + $unit eq 'k' ? 1024 : 1);
1507 + }
1508 + return $val;
1509 +}
1510 +
1511 +# convert config value to array reference, if needed
1512 +sub config_to_multi {
1513 + my $val = shift;
1514 +
1515 + return ref($val) ? $val : [ $val ];
1516 +}
1517 +
1518 sub git_get_project_config {
1519 my ($key, $type) = @_;
1520
1521 + # key sanity check
1522 return unless ($key);
1523 $key =~ s/^gitweb\.//;
1524 return if ($key =~ m/\W/);
1525
1442 - my @x = (git_cmd(), 'config');
1443 - if (defined $type) { push @x, $type; }
1444 - push @x, "--get";
1445 - push @x, "gitweb.$key";
1446 - my $val = qx(@x);
1447 - chomp $val;
1448 - return ($val);
1526 + # type sanity check
1527 + if (defined $type) {
1528 + $type =~ s/^--//;
1529 + $type = undef
1530 + unless ($type eq 'bool' || $type eq 'int');
1531 + }
1532 +
1533 + # get config
1534 + if (!defined $config_file ||
1535 + $config_file ne "$git_dir/config") {
1536 + %config = git_parse_project_config('gitweb');
1537 + $config_file = "$git_dir/config";
1538 + }
1539 +
1540 + # ensure given type
1541 + if (!defined $type) {
1542 + return $config{"gitweb.$key"};
1543 + } elsif ($type eq 'bool') {
1544 + # backward compatibility: 'git config --bool' returns true/false
1545 + return config_to_bool($config{"gitweb.$key"}) ? 'true' : 'false';
1546 + } elsif ($type eq 'int') {
1547 + return config_to_int($config{"gitweb.$key"});
1548 + }
1549 + return $config{"gitweb.$key"};
1550 }
1551
1552 # get hash of given path at given ref