about summary refs log tree commit homepage
path: root/lib/PublicInbox/RepoConfig.pm
blob: fb1fd584cbeef7a420e9d666047a98f0f71dcf51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright (C) 2015 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
package PublicInbox::RepoConfig;
use strict;
use warnings;
use PublicInbox::Config;
use PublicInbox::Repo;
require PublicInbox::Hval;

sub new {
	my ($class, $file) = @_;
	$file = default_file() unless defined($file);
	my $self = bless PublicInbox::Config::git_config_dump($file), $class;
	$self->{-cache} = {};

	# hard disable these with '-' prefix by default:
	$self->{'repobrowse.snapshots'} ||= '-tar.bz2 -tar.xz';

	# for root
	$self->{-groups} = { -hidden => [], -none => [] };
	$self;
}

sub default_file {
	my $f = $ENV{REPOBROWSE_CONFIG};
	return $f if defined $f;
	PublicInbox::Config::config_dir() . '/repobrowse_config';
}

# Returns something like:
# {
#	path => '/home/git/foo.git',
#	description => 'foo repo',
#	cloneurl => "git://example.com/foo.git\nhttp://example.com/foo.git",
#	publicinbox => '/home/pub/foo-public.git',
# }
sub lookup {
	my ($self, $repo_path) = @_; # "git.git"
	my $rv;

	$rv = $self->{-cache}->{$repo_path} and return $rv;

	my $path = $self->{"repo.$repo_path.path"};
	(defined $path && -d $path) or return;
	$rv->{path} = $path;
	$rv->{repo} = $repo_path;

	# snapshots:
	my $snap = (split('/', $repo_path))[-1];
	$snap =~ s/\.git\z//; # seems common for git URLs to end in ".git"
	$rv->{snapshot_re} = qr/\A\Q$snap\E[-_]/;
	$rv->{snapshot_pfx} = $snap;

	# gitweb compatibility
	foreach my $key (qw(description cloneurl)) {
		$rv->{$key} = PublicInbox::Config::try_cat("$path/$key");
	}

	$rv->{desc_html} =
		PublicInbox::Hval->new_oneline($rv->{description})->as_html;

	foreach my $key (qw(publicinbox vcs readme group snapshots)) {
		$rv->{$key} = $self->{"repo.$repo_path.$key"};
	}
	unless (defined $rv->{snapshots}) {
		$rv->{snapshots} = $self->{'repobrowse.snapshots'} || '';
	}

	my %disabled;
	foreach (split(/\s+/, $rv->{snapshots})) {
		s/\A-// and $disabled{$_} = 1;
	}
	$rv->{snapshots_disabled} = \%disabled;

	my $g = $rv->{group};
	defined $g or $g = '-none';
	if (ref($g) eq 'ARRAY') {
		push @{$self->{-groups}->{$_} ||= []}, $repo_path foreach @$g;
	} else {
		push @{$self->{-groups}->{$g} ||= []}, $repo_path;
	}

	# of course git is the default VCS
	$rv->{vcs} ||= 'git';
	$self->{-cache}->{$repo_path} = PublicInbox::Repo->new($rv);
}

1;