blib/lib/Git/Fingerd.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 124 SCAR 37.4
CC
6
Cov
0%
user_reply
30.0
listing_reply
6.0
BEGIN
2.0
CRAP 42.0
1 use strict;
2 use warnings;
3 package Git::Fingerd 2.093522;
4 use Net::Finger::Server 0.003;
5 BEGIN { our @ISA = qw(Net::Finger::Server); }
6 # ABSTRACT: let people finger your git server for... some reason
7  
8 use Git::PurePerl;
9 use List::Util qw(max);
10 use Path::Class;
11 use SUPER;
12 use String::Truncate qw(elide);
13 use Text::Table;
14  
15 #pod =head1 DESCRIPTION
16 #pod
17 #pod This module implements a simple C<finger> server that describes the contents of
18 #pod a server that hosts git repositories. You can finger C<@servername> for a
19 #pod listing of repositories and finger C<repo@servername> for information about
20 #pod a single repository.
21 #pod
22 #pod This was meant to provide a simple example for Net::Finger::Server, but enough
23 #pod people asked for the code that I've released it as something reusable. Here's
24 #pod an example program using Git::Fingerd:
25 #pod
26 #pod #!/usr/bin/perl
27 #pod use Git::Fingerd -run => {
28 #pod isa => 'Net::Server::INET',
29 #pod basedir => '/var/lib/git',
30 #pod };
31 #pod
32 #pod This program could then run out of F<xinetd>.
33 #pod
34 #pod =for Pod::Coverage new basedir
35 #pod
36 #pod =cut
37  
38 sub new {
39 my ($class, %config) = @_;
40  
41 my $basedir = delete $config{basedir} || Carp::croak('no basedir supplied');
42 my $self = $class->SUPER(%config, log_level => 0);
43 $self->{__PACKAGE__}{basedir} = $basedir;
44  
45 return $self;
46 }
47  
48 sub basedir { $_[0]->{__PACKAGE__}{basedir} }
49  
50 sub username_regex { qr{[-a-z0-9]+}i }
51  
52 sub listing_reply {
53 my $basedir = $_[0]->basedir;
54 my @dirs = sort <$basedir/*>;
55  
56 my $table = Text::Table->new('Repository', ' Description');
57  
58 my %repo;
59  
60 for my $i (reverse 0 .. $#dirs) {
61 my $dir = $dirs[$i];
62 my $mode = (stat $dir)[2];
63 unless ($mode & 1) {
64 splice @dirs, $i, 1;
65 next;
66 }
67  
68 my $repo = $dir;
69 s{\A$basedir/}{}, s{\.git\z}{} for $repo;
70 my $desc = `cat $dir/description`;
71 chomp $desc;
72  
73 $repo{ $repo } = $desc;
74 }
75  
76 my $desc_len = 79 - 3 - (List::Util::max map { length } keys %repo);
77  
78 for my $repo (sort { lc $a cmp lc $b } keys %repo) {
79 $table->add($repo => ' ' . elide($repo{$repo}, $desc_len));
80 }
81  
82 return "$table";
83 }
84  
85 sub user_reply {
86 my ($self, $username, $arg) = @_;
87  
88 my $basedir = $self->basedir;
89 my $gitdir = "$basedir/$username.git";
90  
91 return "unknown repository\n" unless -d $gitdir;
92  
93 my $mode = (stat $gitdir)[2];
94  
95 return "unknown repository\n" unless $mode & 1;
96  
97 my $repo = Git::PurePerl->new({ gitdir => $gitdir });
98  
99 my $cloneurl = file( $gitdir, 'cloneurl' )->slurp( chomp => 1 );
100 my $desc = $repo->description;
101 chomp($cloneurl, $desc);
102  
103 my @refs = $repo->ref_names;
104 my @tags = grep { s{^refs/tags/}{} } @refs;
105 my @heads = grep { s{^refs/heads/}{} } @refs;
106  
107 my $reply = "Project : $username
108 Desc. : $desc
109 Clone URL: $cloneurl
110 ";
111  
112 $reply .= "\n[heads]\n";
113 for my $head (sort @heads) {
114 my $sha = $repo->ref_sha1("refs/heads/$head");
115 $reply .= sprintf "%-15s = %s\n", $head, $sha;
116 }
117  
118 $reply .= "\n[tags]\n";
119 for my $tag (sort @tags) {
120 my $sha = $repo->ref_sha1("refs/tags/$tag");
121 $reply .= sprintf "%-15s = %s\n", $tag, $sha;
122 }
123  
124 if (my $ref = $repo->ref("refs/heads/master")) {
125 my $tree = $ref->tree;
126 for ($tree->directory_entries) {
127 next unless $_->filename eq 'README';
128 my $obj = $_->object;
129 $reply .= "\n[README]\n" . $obj->content . "\n";
130 }
131 }
132  
133 return $reply;
134 }
135  
136 1;
137  
138 __END__
139  
140 =pod
141  
142 =encoding UTF-8
143  
144 =head1 NAME
145  
146 Git::Fingerd - let people finger your git server for... some reason
147  
148 =head1 VERSION
149  
150 version 2.093522
151  
152 =head1 DESCRIPTION
153  
154 This module implements a simple C<finger> server that describes the contents of
155 a server that hosts git repositories. You can finger C<@servername> for a
156 listing of repositories and finger C<repo@servername> for information about
157 a single repository.
158  
159 This was meant to provide a simple example for Net::Finger::Server, but enough
160 people asked for the code that I've released it as something reusable. Here's
161 an example program using Git::Fingerd:
162  
163 #!/usr/bin/perl
164 use Git::Fingerd -run => {
165 isa => 'Net::Server::INET',
166 basedir => '/var/lib/git',
167 };
168  
169 This program could then run out of F<xinetd>.
170  
171 =head1 PERL VERSION
172  
173 This library should run on perls released even a long time ago. It should work
174 on any version of perl released in the last five years.
175  
176 Although it may work on older versions of perl, no guarantee is made that the
177 minimum required version will not be increased. The version may be increased
178 for any reason, and there is no promise that patches will be accepted to lower
179 the minimum required perl.
180  
181 =for Pod::Coverage new basedir
182  
183 =head1 AUTHOR
184  
185 Ricardo SIGNES <rjbs@cpan.org>
186  
187 =head1 CONTRIBUTOR
188  
189 =for stopwords Ricardo Signes
190  
191 Ricardo Signes <rjbs@semiotic.systems>
192  
193 =head1 COPYRIGHT AND LICENSE
194  
195 This software is copyright (c) 2022 by Ricardo SIGNES.
196  
197 This is free software; you can redistribute it and/or modify it under
198 the same terms as the Perl 5 programming language system itself.
199  
200 =cut