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