gitweb: Beginnings of splitting gitweb into modules
cover gitweb: Beginnings of splitting gitweb into modules
Gitweb is currently next to largest file (after gitk) in git sources, having more than 235KB, with more than 7,500 lines.
Therefore adding any large feature, like gitweb caching by J.H. and my rewrite of it, or "gitweb admin/write" [failed] GSoC 2010 project by Pavan Kumar Sunkara, would require for the new code to be added as a separate module. Otherwise gitweb would fast become unmaintainable.
Not in all cases splitting gitweb upfront would be required. If a new feature is clearly separate from the rest of gitweb we can add this new feature as a new module to be used by gitweb. This is for example the case for adding output caching support to gitweb.
What must be done however is preparing the infrastructure for modular gitweb sources; to properly test such infrastructure we need at least one gitweb module split off gitweb itself and used by it. This patch series implements this initial step.
Note that JavaScript side of gitweb is already split into many smaller files for better maintability (and concatenated on build for better performance) since 9a86dd5 (gitweb: Split JavaScript for maintability, combining on build, 2011-04-28).
The major problem with splitting gitweb code into modules (similar to SVN::Web or Gitalist) is its error handling.
Currently die_error() subroutine uses non-local jump out of subroutine to end of request. This means however that die_error() must be in the same file as run_request() subroutine, and therefore any module which must use die_error() must include module with run_request()... and one of most natural places for run_request() is a main script.
To be able to decouple die_error() and run_request() we have to change the way error handling is done.
One approach is to go back to using 'exit' (which ModPerl::Registry redefines to not quit Perl interpreter) in die_error(). But this has serious disadvantages for other persistent environments: FastCGI (with CGI::Fast) and PSGI (emulated via CGI::Emulate::PSGI). It wouldn't either solve problems with handling errors in capturing and caching layer in the future gitweb output caching support.
Another solution, used e.g. by SVN::Web, is to handle errors and generate error pages using exceptions. For handling exceptions we would need something like Try::Tiny, for throwing them something like HTTP::Exception (which in turn uses Exception::Class), or HTTP::Throwable... though the latter is probably not so suitable for non-ersistent environments like pure CGI (requires Moose and PSGI).
We should probably implement the latter solution... but this patch series doesn't do that.
The previous version of this patch series was sent to git mailing list as
[PATCHv2 0/2] gitweb: Beginnings of splitting gitweb into modules
http://thread.gmane.org/gmane.comp.version-control.git/172659
Shortlog:
~~~~~~~~~
Jakub Narebski (1):
gitweb: Prepare for splitting gitweb
Pavan Kumar Sunkara (1):
gitweb: Create Gitweb::Util module
Diffstat:
~~~~~~~~~
gitweb/Makefile | 22 ++- gitweb/gitweb.perl | 401 ++--------------------------------------- gitweb/lib/Gitweb/Util.pm | 438 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 472 insertions(+), 389 deletions(-) create mode 100755 gitweb/lib/Gitweb/Util.pm
Signature · 1 line
1.7.9
PATCH 1/2 gitweb: Prepare for splitting gitweb
Prepare gitweb for being split into modules that would be installed in gitweblibdir, by default alongside gitweb in 'lib/' subdirectory.
Gitweb would search first in 'lib/' subdirectory from where it is installed, via
use lib __DIR__.'/lib';
(This allow for tests to work with source version of gitweb without changes.) Then it searches in $(gitweblibdir) directory (set during build time), by default "$(gitwebdir)/lib", via
use lib "++GITWEBLIBDIR++";
which is set to requested dir during building of gitweb.cgi. Note that 'use lib' assures no trailing duplicate entries in @INC.
This preparatory work allows to add new module to gitweb by simply adding
GITWEB_MODULES += <module>
to gitweb/Makefile (assuming that the module is in 'gitweb/lib/' directory).
At Pavan Kumar Sankara suggestion gitweb/Makefile uses
install [OPTION]... SOURCE... DIRECTORY
format (2nd format) with single SOURCE rather than
install [OPTION]... SOURCE DEST
format (1st format) because of security reasons (race conditions). Modern GNU install has -T' / --no-target-directory' option, but we cannot rely that the $(INSTALL) we are using supports this option.
The install-modules target in gitweb/Makefile uses shell 'for' loop, instead of make's $(foreach) function, to avoid possible problem with generating a command line that exceeded the maximum argument list length.
While at it pass GITWEBLIBDIR environment variable in addition to GITWEB_TEST_INSTALLED down to $(MAKE) invocation in 'test-installed' target. This is to allow testing installed version of gitweb and installed version of modules, for future tests which would check individual gitweb (sub)modules.
---
Changes to v2:
* Lost update to gitweb/INSTALL about possibility of installing optional Perl modules for gitweb in 'lib/', alongside future gitweb modules.
gitweb/Makefile | 20 ++++++++++++++++++-- gitweb/gitweb.perl | 11 +++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-)
gitweb/Makefile +18 -2
@@ -13,6 +13,7 @@ all:: prefix ?= $(HOME) bindir ?= $(prefix)/bin gitwebdir ?= /var/www/cgi-bin +gitweblibdir ?= $(gitwebdir)/lib RM ?= rm -f INSTALL ?= install
@@ -59,6 +60,7 @@ PERL_PATH ?= /usr/bin/perl bindir_SQ = $(subst ','\'',$(bindir))#' gitwebdir_SQ = $(subst ','\'',$(gitwebdir))#' gitwebstaticdir_SQ = $(subst ','\'',$(gitwebdir)/static)#' +gitweblibdir_SQ = $(subst ','\'',$(gitweblibdir))#' SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))#' PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH))#' DESTDIR_SQ = $(subst ','\'',$(DESTDIR))#'
@@ -129,6 +131,7 @@ GITWEB_JSLIB_FILES += static/js/blame_incremental.js GITWEB_REPLACE = \ -e 's|++GIT_VERSION++|$(GIT_VERSION)|g' \ -e 's|++GIT_BINDIR++|$(bindir)|g' \ + -e 's|++GITWEBLIBDIR++|$(gitweblibdir)|g' \ -e 's|++GITWEB_CONFIG++|$(GITWEB_CONFIG)|g' \ -e 's|++GITWEB_CONFIG_SYSTEM++|$(GITWEB_CONFIG_SYSTEM)|g' \ -e 's|++GITWEB_CONFIG_COMMON++|$(GITWEB_CONFIG_COMMON)|g' \
@@ -174,16 +177,29 @@ test: test-installed: GITWEB_TEST_INSTALLED='$(DESTDIR_SQ)$(gitwebdir_SQ)' \ + GITWEBLIBDIR='$(DESTDIR_SQ)$(gitweblibdir_SQ)' \ $(MAKE) -C ../t gitweb-test ### Installation rules -install: all +install: all install-modules $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitwebdir_SQ)' $(INSTALL) -m 755 $(GITWEB_PROGRAMS) '$(DESTDIR_SQ)$(gitwebdir_SQ)' $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitwebstaticdir_SQ)' $(INSTALL) -m 644 $(GITWEB_FILES) '$(DESTDIR_SQ)$(gitwebstaticdir_SQ)' +install-modules: + $(INSTALL) -m 755 $(GITWEB_PROGRAMS) '$(DESTDIR_SQ)$(gitwebdir_SQ)/lib' + install_dirs="$(sort $(dir $(GITWEB_MODULES)))" && \ + for dir in $$install_dirs; do \ + test -d '$(DESTDIR_SQ)$(gitweblibdir_SQ)'/"$$dir" || \ + $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitweblibdir_SQ)'/"$$dir"; \ + done + gitweb_modules="$(GITWEB_MODULES)" && \ + for mod in $$gitweb_modules; do \ + $(INSTALL) -m 644 "lib/$$mod" '$(DESTDIR_SQ)$(gitweblibdir_SQ)'/"$$(dirname $$mod)"; \ + done + ### Cleaning rules clean:
@@ -191,5 +207,5 @@ clean: static/gitweb.min.js static/gitweb.min.css \ GITWEB-BUILD-OPTIONS -.PHONY: all clean install test test-installed .FORCE-GIT-VERSION-FILE FORCE +.PHONY: all clean install install-modules test test-installed .FORCE-GIT-VERSION-FILE FORCE
gitweb/gitweb.perl +11 -0
@@ -10,6 +10,17 @@ use 5.008; use strict; use warnings; + +use File::Spec; + +# __DIR__ is excerpt from Dir::Self +sub __DIR__ () { + File::Spec->rel2abs(join '', (File::Spec->splitpath(__FILE__))[0, 1]); +} +use lib "++GITWEBLIBDIR++"; +use lib __DIR__ . '/lib'; + + use CGI qw(:standard :escapeHTML -nosticky); use CGI::Util qw(unescape); use CGI::Carp qw(fatalsToBrowser set_message);
Signature · 1 line
1.7.9
PATCH 2/2 gitweb: Create Gitweb::Util module
In reply to Jakub NarebskiFrom: Pavan Kumar Sunkara <[email protected]>
Create a Gitweb::Util module, which is meant to contain internal utilities used by gitweb. Currently it includes all the quoting/unquoting and escaping subroutines that are used by the gitweb, HTML-aware string manipulation subroutines, and some file and filesystem operations.
The intent is to include in Gitweb::Util subroutines and variables that do not require git or gitweb knowledge.
Update gitweb/Makefile to install Gitweb::Util module alongside gitweb.
This was sort of part of [unfinished] Google Summer of Code 2010 project "Splitting gitweb and developing write functionalities (Integrated web client for git)" by Pavan Kumar Sunkara, mentored by Christian Couder and co-mentored by Petr Baudis.
[This reduces size of gitweb.perl by around 5%]
---
This module was originally part of unfinished GSoC 2010 project with Pavan Kumar Sunkara as a student
git://repo.or.cz/git/gsoc2010-gitweb.git
The module was originally named Gitweb::Escape.
NOTE: I wanted to check if the change is pure code movement (and
reorganization: in some places ordering of subroutines changed) with git-blame ("git blame -C -C -C -w")... but it couldn't correctly detect code movement of 5% of file.
Changes from v2:
* gitweb still needs Encoding module for marking query parameters as UTF-8 with decode_utf8 (new in gitweb).
* Move sanitize (new), chop_str, chop_and_escape_str, esc_html_hl_regions (new), matchpos_list (new), esc_html_match_hl (new), esc_html_match_hl_chopped (new), hash_set_multi, get_file_owner, insert_file, normalize_link_target to Gitweb::Util
gitweb/Makefile | 2 + gitweb/gitweb.perl | 390 +--------------------------------------- gitweb/lib/Gitweb/Util.pm | 438 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 443 insertions(+), 387 deletions(-) create mode 100755 gitweb/lib/Gitweb/Util.pm
gitweb/Makefile +2 -0
@@ -127,6 +127,8 @@ GITWEB_JSLIB_FILES += static/js/javascript-detection.js GITWEB_JSLIB_FILES += static/js/adjust-timezone.js GITWEB_JSLIB_FILES += static/js/blame_incremental.js +# Modules: Gitweb::* +GITWEB_MODULES += Gitweb/Util.pm GITWEB_REPLACE = \ -e 's|++GIT_VERSION++|$(GIT_VERSION)|g' \
gitweb/gitweb.perl +3 -387
432 lines not on this page · raw message
gitweb/lib/Gitweb/Util.pm new file +438 -0
438 lines not on this page · raw message
Signature · 1 line
1.7.9