]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
repobrowse: add /patch/ endpoint
authorEric Wong <e@80x24.org>
Thu, 24 Dec 2015 01:26:44 +0000 (01:26 +0000)
committerEric Wong <e@80x24.org>
Tue, 5 Apr 2016 18:58:27 +0000 (18:58 +0000)
This is trivial for generating patches so users can run:
"curl $URL | git am" against any instance of this.

lib/PublicInbox/RepoBrowse.pm
lib/PublicInbox/RepoBrowseGitPatch.pm [new file with mode: 0644]

index ef3bcf8783d35c797f5ad7d32900bb9bd58ab54f..3aa8b98a86e7d293863199a032445a90041d09e4 100644 (file)
@@ -23,7 +23,7 @@ use warnings;
 use URI::Escape qw(uri_escape_utf8 uri_unescape);
 use PublicInbox::RepoConfig;
 
-my %CMD = map { lc($_) => $_ } qw(Log Commit Tree);
+my %CMD = map { lc($_) => $_ } qw(Log Commit Tree Patch);
 my %VCS = (git => 'Git');
 my %LOADED;
 
diff --git a/lib/PublicInbox/RepoBrowseGitPatch.pm b/lib/PublicInbox/RepoBrowseGitPatch.pm
new file mode 100644 (file)
index 0000000..b998a86
--- /dev/null
@@ -0,0 +1,39 @@
+# Copyright (C) 2015 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# shows the /commit/ endpoint for git repositories
+package PublicInbox::RepoBrowseGitPatch;
+use strict;
+use warnings;
+use base qw(PublicInbox::RepoBrowseBase);
+use PublicInbox::Git;
+
+sub call_git_patch {
+       my ($self, $req) = @_;
+       my $repo_info = $req->{repo_info};
+       my $dir = $repo_info->{path};
+       my $git = $repo_info->{git} ||= PublicInbox::Git->new($dir);
+
+       my $q = PublicInbox::RepoBrowseQuery->new($req->{cgi});
+       my $id = $q->{id};
+       $id eq '' and $id = 'HEAD';
+       my $fp = $git->popen(qw(format-patch -M -1 --stdout --encoding=utf-8
+                               --signature=public-inbox.org), $id);
+       my ($buf, $n);
+
+       $n = read($fp, $buf, 8192);
+       return unless (defined $n && $n > 0);
+       sub {
+               my ($res) = @_; # Plack callback
+               my $fh = $res->([200, ['Content-Type'=>'text/plain']]);
+               $fh->write($buf);
+               while (1) {
+                       $n = read($fp, $buf, 8192);
+                       last unless (defined $n && $n > 0);
+                       $fh->write($buf);
+               }
+               $fh->close;
+       }
+}
+
+1;