summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2015-06-11 02:11:22 -0700
committerH. Peter Anvin <hpa@zytor.com>2015-06-11 02:11:22 -0700
commit4edfec2e8f2b4a6583f6493978d9e79dd79e4c66 (patch)
tree26951f58b65784af96a815424f7356b9752fe744
downloadgitweb2cgit-4edfec2e8f2b4a6583f6493978d9e79dd79e4c66.tar.gz
gitweb2cgit-4edfec2e8f2b4a6583f6493978d9e79dd79e4c66.tar.xz
gitweb2cgit-4edfec2e8f2b4a6583f6493978d9e79dd79e4c66.zip
Script to convert from gitweb to cgit URLs
-rwxr-xr-xgitweb2cgit.cgi132
1 files changed, 132 insertions, 0 deletions
diff --git a/gitweb2cgit.cgi b/gitweb2cgit.cgi
new file mode 100755
index 0000000..08dd749
--- /dev/null
+++ b/gitweb2cgit.cgi
@@ -0,0 +1,132 @@
+#!/usr/bin/perl
+#
+# Convert a gitweb query into a cgit query
+#
+
+use strict;
+use warnings;
+use CGI;
+use URI;
+
+my $cgit_base = 'http://git.zytor.com/';
+
+# Redirects mapping gitweb -> cgit
+# Inspired from: http://www.clearchain.com/blog/posts/cgit-upgrade-gitweb-retired
+#
+# Gitweb uses get targets seperated by ;
+#
+# /?...;...;...
+#
+# p = Project
+# a = Action like (
+# blob,
+# blob_plain,
+# commitdiff,
+# commit,
+# history,
+# log,
+# rss|atom,
+# shortlog,
+# summary,
+# tag,
+# tree,
+# snapshot
+# )
+# h = SHA Hash
+# hb= SHA Hash Tree Base
+# f= file/dir
+# sf= snapshot format
+#
+# Cgit uses the following:
+#
+# /project/action/?...
+#
+# action ( commit, log, diff, tree, tag, patch )
+#
+# id = SHA Hash
+# id2 = SHA Hash
+# h = head
+#
+# Translation rules
+#
+# Project is a straight redirect
+# ---
+# /?p=(.+)\.git; http://cgit.openezx.org/$1/
+#
+# Action requires a mapping
+# ---
+# a=(blob|tree) /tree/
+# a=(blobdiff|commitdiff) /diff/
+# a=commit /commit/
+# a=(summary) /
+# a=(shortlog|log|history) /log/
+# a=tag /tag/
+# a=blob_plain /blob/
+# a=rss|atom /atom
+#
+# Targets require mapping
+# ---
+# h=(.+) id=$1
+# f=(.+) /$1
+# hb=(.+) id2=$1
+#
+# Now putting it all together
+#
+
+my $cgi_query = CGI->new;
+my %parm = $cgi_query->Vars;
+
+# Project
+my $p = $parm{'p'};
+delete $parm{'p'};
+
+# Action
+my %actions = (
+ 'blob' => '/tree/',
+ 'tree' => '/tree/',
+ 'blobdiff' => '/diff/',
+ 'commitdiff' => '/diff/',
+ 'commit' => '/commit/',
+ 'summary' => '/',
+ 'shortlog' => '/log/',
+ 'log' => '/log/',
+ 'history' => '/log/',
+ 'tag' => '/tag/',
+ 'blob_plain' => '/blob/',
+ 'rss' => '/atom/',
+ 'atom' => '/atom/',
+);
+
+my $a = $parm{'a'};
+my $action = $actions{$a};
+delete $parm{'a'};
+
+# Head (ID)
+my $h = $parm{'h'};
+$parm{'id'} = $h if (defined($h));
+delete $parm{'h'};
+
+my $hb = $parm{'hb'};
+$parm{'id2'} = $hb if (defined($hb));
+delete $parm{'hb'};
+
+# File
+my $f = $parm{'f'};
+delete $parm{'f'};
+
+# Build a new URI
+my $uri = URI->new($cgit_base);
+
+# Project path, action and filename in the URL
+if ( defined($f) && $action =~ /\/$/ ) {
+ $f =~ s/^\/+//;
+} else {
+ $f = '';
+}
+$uri->path( $uri->path . $p . $action . $f );
+
+# Remainder becomes a new query string
+$uri->query_form( \%parm, ';' );
+
+# Redirect to the correspin
+print $cgi_query->redirect($uri);