From d9563ea5516e8e786debf223e10ec11695aee9d7 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 9 Feb 2017 01:37:03 +0000 Subject: repobrowse: shorten internal names We'll still be keeping "repobrowse" for the public API for use with .psgi files, but shortening the name means less typing and we may have command-line tools, too. --- lib/PublicInbox/RepoGit.pm | 68 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 lib/PublicInbox/RepoGit.pm (limited to 'lib/PublicInbox/RepoGit.pm') diff --git a/lib/PublicInbox/RepoGit.pm b/lib/PublicInbox/RepoGit.pm new file mode 100644 index 00000000..f7bf0b9d --- /dev/null +++ b/lib/PublicInbox/RepoGit.pm @@ -0,0 +1,68 @@ +# Copyright (C) 2015 all contributors +# License: AGPL-3.0+ (https://www.gnu.org/licenses/agpl-3.0.txt) + +# common functions used by other RepoGit* modules +package PublicInbox::RepoGit; +use strict; +use warnings; +use base qw(Exporter); +our @EXPORT_OK = qw(git_unquote git_commit_title git_dec_links); +use PublicInbox::Hval qw(utf8_html); + +my %GIT_ESC = ( + a => "\a", + b => "\b", + f => "\f", + n => "\n", + r => "\r", + t => "\t", + v => "\013", +); + +sub git_unquote { + my ($s) = @_; + return $s unless ($s =~ /\A"(.*)"\z/); + $s = $1; + $s =~ s/\\([abfnrtv])/$GIT_ESC{$1}/g; + $s =~ s/\\([0-7]{1,3})/chr(oct($1))/ge; + $s; +} + +sub git_commit_title { + my ($git, $obj) = @_; # PublicInbox::Git, $sha1hex + my $rv; + eval { + my $buf = $git->cat_file($obj); + ($rv) = ($$buf =~ /\r?\n\r?\n([^\r\n]+)\r?\n?/); + }; + $rv; +} + +# example inputs: "HEAD -> master", "tag: v1.0.0", +sub git_dec_links { + my ($rel, $D) = @_; + my @l; + foreach (split /, /, $D) { + if (/\A(\S+) -> (\S+)/) { # 'HEAD -> master' + my ($s, $h) = ($1, $2); + $s = utf8_html($s); + $h = PublicInbox::Hval->utf8($h); + my $r = $h->as_href; + $h = $h->as_html; + push @l, qq($s -> $h); + } elsif (s/\Atag: //) { + my $h = PublicInbox::Hval->utf8($_); + my $r = $h->as_href; + $h = $h->as_html; + push @l, qq($h); + } else { + my $h = PublicInbox::Hval->utf8($_); + my $r = $h->as_href; + $h = $h->as_html; + push @l, qq($h); + } + } + @l; +} + +1; -- cgit v1.2.3-24-ge0c7