]> Sergey Matveev's repositories - public-inbox.git/commitdiff
git: move cloneurl + description reading here
authorEric Wong <e@80x24.org>
Tue, 4 Oct 2022 19:12:34 +0000 (19:12 +0000)
committerEric Wong <e@80x24.org>
Wed, 5 Oct 2022 20:24:44 +0000 (20:24 +0000)
We'll be using these functions for serving coderepos natively
without cgit.

lib/PublicInbox/ExtSearch.pm
lib/PublicInbox/Git.pm
lib/PublicInbox/Inbox.pm
t/init.t
t/lei-mirror.t

index a69c0e76a8afcbd1e9e7248e2dc4b1ca3d5455dd..fa49a1d0182f75d53e4408d26fdc4f2997f6ef6e 100644 (file)
@@ -108,7 +108,7 @@ sub altid_map { {} }
 sub description {
        my ($self) = @_;
        ($self->{description} //=
-               PublicInbox::Inbox::cat_desc("$self->{topdir}/description")) //
+               PublicInbox::Git::cat_desc("$self->{topdir}/description")) //
                '$EXTINDEX_DIR/description missing';
 }
 
index 78b47096f63087c8ff49da51c9eff67a09929777..2f0bb6a04745bc6a5bab6869a2e12b6fe91c5140 100644 (file)
@@ -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
index 8ac7eb302a0c6ed9ef7b9b97f53888bcc115d866..3532bb584236d4d466d12c234e338867699b5936 100644 (file)
@@ -181,33 +181,18 @@ sub over {
        } // ($req ? croak("E: $@") : undef);
 }
 
-sub try_cat {
-       my ($path) = @_;
-       open(my $fh, '<', $path) or return '';
-       local $/;
-       <$fh> // '';
-}
-
-sub cat_desc ($) {
-       my $desc = try_cat($_[0]);
-       local $/ = "\n";
-       chomp $desc;
-       utf8::decode($desc);
-       $desc =~ s/\s+/ /smg;
-       $desc eq '' ? undef : $desc;
-}
-
 sub description {
        my ($self) = @_;
-       ($self->{description} //= cat_desc("$self->{inboxdir}/description")) //
+       ($self->{description} //=
+               PublicInbox::Git::cat_desc("$self->{inboxdir}/description")) //
                '($INBOX_DIR/description missing)';
 }
 
 sub cloneurl {
        my ($self) = @_;
        $self->{cloneurl} // do {
-               my $s = try_cat("$self->{inboxdir}/cloneurl");
-               my @urls = split(/\s+/s, $s);
+               my @urls = split(/\s+/s,
+                 PublicInbox::Git::try_cat("$self->{inboxdir}/cloneurl"));
                scalar(@urls) ? ($self->{cloneurl} = \@urls) : undef;
        } // [];
 }
index 6f4c9dceeb33e60afcffe2f7ea242abc306add65..460c83f3dd604e2c2f48cc91cfbd7dc425aee416 100644 (file)
--- a/t/init.t
+++ b/t/init.t
@@ -102,7 +102,7 @@ sub quiet_fail {
        umask($umask) // xbail "umask: $!";
        ok(-d "$tmpdir/a/b/c/d", 'directory created');
        my $desc = "$tmpdir/a/b/c/d/description";
-       is(PublicInbox::Inbox::try_cat($desc),
+       is(PublicInbox::Git::try_cat($desc),
                "public inbox for abcd\@example.com\n", 'description set');
        my $mode = (stat($desc))[2];
        is(sprintf('0%03o', $mode & 0777), '0644',
index 32a5b03907147354f11bdd49d05a498f6f2bbd08..c172483b9b57387d06957ca566cabae5c5ae8038 100644 (file)
@@ -22,7 +22,7 @@ test_lei({ tmpdir => $tmpdir }, sub {
        lei_ok('add-external', $t1, '--mirror', "$http/t1/", \'--mirror v1');
        my $mm_dup = "$t1/public-inbox/msgmap.sqlite3";
        ok(-f $mm_dup, 't1-mirror indexed');
-       is(PublicInbox::Inbox::try_cat("$t1/description"),
+       is(PublicInbox::Git::try_cat("$t1/description"),
                "mirror of $http/t1/\n", 'description set');
        ok(-f "$t1/Makefile", 'convenience Makefile added (v1)');
        ok(-f "$t1/inbox.config.example", 'inbox.config.example downloaded');
@@ -43,7 +43,7 @@ test_lei({ tmpdir => $tmpdir }, sub {
        ok(-f $mm_dup, 't2-mirror indexed');
        ok(-f "$t2/description", 't2 description');
        ok(-f "$t2/Makefile", 'convenience Makefile added (v2)');
-       is(PublicInbox::Inbox::try_cat("$t2/description"),
+       is(PublicInbox::Git::try_cat("$t2/description"),
                "mirror of $http/t2/\n", 'description set');
        $tb = PublicInbox::Msgmap->new_file($mm_dup)->created_at;
        is($tb, $created{v2}, 'created_at matched in v2 mirror');
@@ -199,14 +199,14 @@ $td->join;
        my $exp = "mirror of https://example.com/src/\n";
        my $f = "$tmpdir/description";
        PublicInbox::LeiMirror::set_description($mrr);
-       is(PublicInbox::Inbox::try_cat($f), $exp, 'description set on ENOENT');
+       is(PublicInbox::Git::try_cat($f), $exp, 'description set on ENOENT');
 
        my $fh;
        (open($fh, '>', $f) and close($fh)) or xbail $!;
        PublicInbox::LeiMirror::set_description($mrr);
-       is(PublicInbox::Inbox::try_cat($f), $exp, 'description set on empty');
+       is(PublicInbox::Git::try_cat($f), $exp, 'description set on empty');
        (open($fh, '>', $f) and print $fh "x\n" and close($fh)) or xbail $!;
-       is(PublicInbox::Inbox::try_cat($f), "x\n",
+       is(PublicInbox::Git::try_cat($f), "x\n",
                'description preserved if non-default');
 }