From f92da491e7bdce8aeac3a167ca38abdd61cdb9bd Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Sat, 7 Nov 2015 17:27:34 +0100 Subject: Distinguish gitweb and cgit --- Makefile | 1 + index.cgi | 2 +- vcsbrowsers.py | 43 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 31c39d8..ee037e7 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ VERSION=$(shell grep ^VERSION index.cgi | sed -e s'/.*\="\([0-9.]\+\)".*/\1/') PKG=cl2vcs all: + nosetests --with-doctest clean: rm -f *.pyc diff --git a/index.cgi b/index.cgi index a083166..5af08fd 100755 --- a/index.cgi +++ b/index.cgi @@ -75,7 +75,7 @@ def parse_pts_xhtml(pts): def get_vcsbrowser(vcs, vcs_url): if vcs == "git": - return vcsbrowsers.GitWebBrowser(vcs_url) + return vcsbrowsers.guess_git_repo(vcs_url) elif vcs == "Mercurial": return vcsbrowsers.HgBrowser(vcs_url) diff --git a/vcsbrowsers.py b/vcsbrowsers.py index ea7931c..ba33cb1 100644 --- a/vcsbrowsers.py +++ b/vcsbrowsers.py @@ -1,8 +1,9 @@ -# convenience wrappers to construct links +# convenience wrappers to construct links # into the webinterfaces of different VCSs import re + class VCSBrowser(object): def __init__(self, url): self.url = url.rstrip('/') @@ -16,18 +17,52 @@ class VCSBrowser(object): class GitWebBrowser(VCSBrowser): """ + GitWeb based repo browser + URLs for gitweb: e.g. http://git.debian.org/?p=pkg-libvirt/gtk-vnc.git """ + repotype = "gitweb" + def __init__(self, url): url = re.sub(r';a=summary$', '', url) VCSBrowser.__init__(self, url) + @classmethod + def check(cls, url): + return True if '/?p=' in url else False + def commit(self, commitid): return "%s;a=commitdiff;h=%s" % (self.url, commitid) - def branch(self, branch): - return "%s;a=shortlog;h=refs/heads/%s" % (self.url, branch) + +class CGitBrowser(VCSBrowser): + repotype = "cgit" + + def __init__(self, url): + VCSBrowser.__init__(self, url) + + @classmethod + def check(cls, url): + return True if '/cgit' in url else False + + def commit(self, commitid): + return "%s/commit/?id=%s" % (self.url, commitid) + + +def guess_git_repo(url): + """ + >>> guess_git_repo("http://example.com/?p=foo").repotype + 'gitweb' + >>> guess_git_repo("http://example.com/cgit/foo").repotype + 'cgit' + >>> guess_git_repo("http://example.com/bar/foo").repotype + 'cgit' + """ + for repotype in [CGitBrowser, GitWebBrowser]: + if repotype.check(url): + return repotype(url) + return CGitBrowser(url) class HgBrowser(VCSBrowser): @@ -38,5 +73,3 @@ class HgBrowser(VCSBrowser): def commit(self, commitid): return "%s?cs=%s" % (self.url, commitid) - - -- cgit v1.2.3