about summary refs log tree commit homepage
path: root/lib/PublicInbox/Git.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2022-10-04 19:12:34 +0000
committerEric Wong <e@80x24.org>2022-10-05 20:24:44 +0000
commita002384a74382df2649d6a1f8dfba4f291af032e (patch)
tree1106206df2c10fc21adec29b1c3dcbcff9075d3d /lib/PublicInbox/Git.pm
parenta93c7e471ca70bdb476c2285cf894e319b38f160 (diff)
downloadpublic-inbox-a002384a74382df2649d6a1f8dfba4f291af032e.tar.gz
We'll be using these functions for serving coderepos natively
without cgit.
Diffstat (limited to 'lib/PublicInbox/Git.pm')
-rw-r--r--lib/PublicInbox/Git.pm30
1 files changed, 24 insertions, 6 deletions
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