From a002384a74382df2649d6a1f8dfba4f291af032e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 4 Oct 2022 19:12:34 +0000 Subject: git: move cloneurl + description reading here We'll be using these functions for serving coderepos natively without cgit. --- lib/PublicInbox/Git.pm | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'lib/PublicInbox/Git.pm') diff --git a/lib/PublicInbox/Git.pm b/lib/PublicInbox/Git.pm index 78b47096..2f0bb6a0 100644 --- a/lib/PublicInbox/Git.pm +++ b/lib/PublicInbox/Git.pm @@ -498,13 +498,31 @@ sub modified ($) { (split(/ /, <$fh> // time))[0] + 0; # integerize for JSON } +sub try_cat { + my ($path) = @_; + open(my $fh, '<', $path) or return ''; + local $/; + <$fh> // ''; +} + +sub cat_desc ($) { + my $desc = try_cat($_[0]); + chomp $desc; + utf8::decode($desc); + $desc =~ s/\s+/ /smg; + $desc eq '' ? undef : $desc; +} + sub description { - my $desc = ''; - if (open(my $fh, '<:utf8', "$_[0]->{git_dir}/description")) { - local $/ = "\n"; - chomp($desc = <$fh> // ''); - } - $desc eq '' ? 'Unnamed repository' : $desc; + cat_desc("$_[0]->{git_dir}/description") // 'Unnamed repository'; +} + +sub cloneurl { + my ($self) = @_; + $self->{cloneurl} // do { + my @urls = split(/\s+/s, try_cat("$self->{git_dir}/cloneurl")); + scalar(@urls) ? ($self->{cloneurl} = \@urls) : undef; + } // []; } # for grokmirror, which doesn't read gitweb.description -- cgit v1.2.3-24-ge0c7