From dd70235f5a81e52725365365a554cf7c8cfa37e6 Mon Sep 17 00:00:00 2001 From: Martin Waitz Date: Sat, 16 Sep 2006 23:08:32 +0200 Subject: gitweb: more support for PATH_INFO based URLs Now three types of path based URLs are supported: gitweb.cgi/project.git gitweb.cgi/project.git/branch gitweb.cgi/project.git/branch/filename The first one (show project summary) was already supported for a long time now. The other two are new: they show the shortlog of a branch or the plain file contents of some file contained in the repository. This is especially useful to support project web pages for small projects: just create an html branch and then use an URL like gitweb.cgi/project.git/html/index.html. Signed-off-by: Martin Waitz Acked-by: Jakub Narebski Signed-off-by: Junio C Hamano --- gitweb/gitweb.perl | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'gitweb/gitweb.perl') diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index a81c8d4cf2..645ae795c7 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -196,12 +196,7 @@ sub feature_pickaxe { } } -our $project = ($cgi->param('p') || $ENV{'PATH_INFO'}); -if (defined $project) { - $project =~ s|^/||; - $project =~ s|/$||; - $project = undef unless $project; -} +our $project = $cgi->param('p'); if (defined $project) { if (!validate_input($project)) { die_error(undef, "Invalid project parameter"); @@ -212,7 +207,6 @@ sub feature_pickaxe { if (!(-e "$projectroot/$project/HEAD")) { die_error(undef, "No such project"); } - $git_dir = "$projectroot/$project"; } our $file_name = $cgi->param('f'); @@ -272,6 +266,32 @@ sub feature_pickaxe { $searchtext = quotemeta $searchtext; } +# now read PATH_INFO and use it as alternative to parameters +our $path_info = $ENV{"PATH_INFO"}; +$path_info =~ s|^/||; +$path_info =~ s|/$||; +if (validate_input($path_info) && !defined $project) { + $project = $path_info; + while ($project && !-e "$projectroot/$project/HEAD") { + $project =~ s,/*[^/]*$,,; + } + if (defined $project) { + $project = undef unless $project; + } + if ($path_info =~ m,^$project/([^/]+)/(.+)$,) { + # we got "project.git/branch/filename" + $action ||= "blob_plain"; + $hash_base ||= $1; + $file_name ||= $2; + } elsif ($path_info =~ m,^$project/([^/]+)$,) { + # we got "project.git/branch" + $action ||= "shortlog"; + $hash ||= $1; + } +} + +$git_dir = "$projectroot/$project"; + # dispatch my %actions = ( "blame" => \&git_blame2, -- cgit 1.2.3-korg From 800764cf335cb4de289269c919e865c536635885 Mon Sep 17 00:00:00 2001 From: Martin Waitz Date: Sat, 16 Sep 2006 23:09:02 +0200 Subject: gitweb: fix uninitialized variable warning. Perl spit out a varning when "blob" or "blob_plain" actions were used without a $hash parameter. Signed-off-by: Martin Waitz Signed-off-by: Junio C Hamano --- gitweb/gitweb.perl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'gitweb/gitweb.perl') diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index 645ae795c7..250138520f 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -2547,11 +2547,7 @@ sub git_heads { } sub git_blob_plain { - # blobs defined by non-textual hash id's can be cached my $expires; - if ($hash =~ m/^[0-9a-fA-F]{40}$/) { - $expires = "+1d"; - } if (!defined $hash) { if (defined $file_name) { @@ -2561,7 +2557,11 @@ sub git_blob_plain { } else { die_error(undef, "No file name defined"); } + } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) { + # blobs defined by non-textual hash id's can be cached + $expires = "+1d"; } + my $type = shift; open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash or die_error(undef, "Couldn't cat $file_name, $hash"); @@ -2589,11 +2589,7 @@ sub git_blob_plain { } sub git_blob { - # blobs defined by non-textual hash id's can be cached my $expires; - if ($hash =~ m/^[0-9a-fA-F]{40}$/) { - $expires = "+1d"; - } if (!defined $hash) { if (defined $file_name) { @@ -2603,7 +2599,11 @@ sub git_blob { } else { die_error(undef, "No file name defined"); } + } elsif ($hash =~ m/^[0-9a-fA-F]{40}$/) { + # blobs defined by non-textual hash id's can be cached + $expires = "+1d"; } + my ($have_blame) = gitweb_check_feature('blame'); open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash or die_error(undef, "Couldn't cat $file_name, $hash"); -- cgit 1.2.3-korg From 7939fe44b8abedb4d8bb5090aec7a92eb1aa7a4b Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Sun, 17 Sep 2006 00:30:27 +0200 Subject: gitweb: do not use 'No such directory' error message undef $project; to prevent a file named description to be read. Signed-off-by: Junio C Hamano --- gitweb/gitweb.perl | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'gitweb/gitweb.perl') diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index 250138520f..fa657578fb 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -198,13 +198,10 @@ sub feature_pickaxe { our $project = $cgi->param('p'); if (defined $project) { - if (!validate_input($project)) { - die_error(undef, "Invalid project parameter"); - } - if (!(-d "$projectroot/$project")) { - die_error(undef, "No such directory"); - } - if (!(-e "$projectroot/$project/HEAD")) { + if (!validate_input($project) || + !(-d "$projectroot/$project") || + !(-e "$projectroot/$project/HEAD")) { + undef $project; die_error(undef, "No such project"); } } -- cgit 1.2.3-korg From 32f4aaccaa45fcd79bacd424c7d69051156bb766 Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Sun, 17 Sep 2006 00:31:01 +0200 Subject: gitweb: export options $export_ok: If this variable evaluates to true it is checked if a file with this name exists in the repository. If it does not exist the repository cannot be viewed from gitweb. (Similar to git-daemon-export-ok for git-daemon). $strict_export: If this variable evaluates to true only repositories listed on the project-list-page of gitweb can be accessed. Signed-off-by: Junio C Hamano --- Makefile | 4 ++++ gitweb/gitweb.perl | 23 ++++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) (limited to 'gitweb/gitweb.perl') diff --git a/Makefile b/Makefile index 7b3114f3aa..b9938acd48 100644 --- a/Makefile +++ b/Makefile @@ -126,6 +126,8 @@ GITWEB_CONFIG = gitweb_config.perl GITWEB_HOME_LINK_STR = projects GITWEB_SITENAME = GITWEB_PROJECTROOT = /pub/git +GITWEB_EXPORT_OK = +GITWEB_STRICT_EXPORT = GITWEB_BASE_URL = GITWEB_LIST = GITWEB_HOMETEXT = indextext.html @@ -631,6 +633,8 @@ gitweb/gitweb.cgi: gitweb/gitweb.perl -e 's|++GITWEB_HOME_LINK_STR++|$(GITWEB_HOME_LINK_STR)|g' \ -e 's|++GITWEB_SITENAME++|$(GITWEB_SITENAME)|g' \ -e 's|++GITWEB_PROJECTROOT++|$(GITWEB_PROJECTROOT)|g' \ + -e 's|++GITWEB_EXPORT_OK++|$(GITWEB_EXPORT_OK)|g' \ + -e 's|++GITWEB_STRICT_EXPORT++|$(GITWEB_STRICT_EXPORT)|g' \ -e 's|++GITWEB_BASE_URL++|$(GITWEB_BASE_URL)|g' \ -e 's|++GITWEB_LIST++|$(GITWEB_LIST)|g' \ -e 's|++GITWEB_HOMETEXT++|$(GITWEB_HOMETEXT)|g' \ diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index fa657578fb..497129aefc 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -54,6 +54,13 @@ # source of projects list our $projects_list = "++GITWEB_LIST++"; +# show repository only if this file exists +# (only effective if this variable evaluates to true) +our $export_ok = "++GITWEB_EXPORT_OK++"; + +# only allow viewing of repositories also shown on the overview page +our $strict_export = "++GITWEB_STRICT_EXPORT++"; + # list of git base URLs used for URL to where fetch project from, # i.e. full URL is "$git_base_url/$project" our @git_base_url_list = ("++GITWEB_BASE_URL++"); @@ -200,7 +207,9 @@ sub feature_pickaxe { if (defined $project) { if (!validate_input($project) || !(-d "$projectroot/$project") || - !(-e "$projectroot/$project/HEAD")) { + !(-e "$projectroot/$project/HEAD") || + ($export_ok && !(-e "$projectroot/$project/$export_ok")) || + ($strict_export && !project_in_list($project))) { undef $project; die_error(undef, "No such project"); } @@ -422,6 +431,12 @@ sub untabify { return $line; } +sub project_in_list { + my $project = shift; + my @list = git_get_projects_list(); + return @list && scalar(grep { $_->{'path'} eq $project } @list); +} + ## ---------------------------------------------------------------------- ## HTML aware string manipulation @@ -734,7 +749,8 @@ sub git_get_projects_list { my $subdir = substr($File::Find::name, $pfxlen + 1); # we check related file in $projectroot - if (-e "$projectroot/$subdir/HEAD") { + if (-e "$projectroot/$subdir/HEAD" && (!$export_ok || + -e "$projectroot/$subdir/$export_ok")) { push @list, { path => $subdir }; $File::Find::prune = 1; } @@ -755,7 +771,8 @@ sub git_get_projects_list { if (!defined $path) { next; } - if (-e "$projectroot/$path/HEAD") { + if (-e "$projectroot/$path/HEAD" && (!$export_ok || + -e "$projectroot/$path/$export_ok")) { my $pr = { path => $path, owner => decode("utf8", $owner, Encode::FB_DEFAULT), -- cgit 1.2.3-korg From 645927cec82253a3592ca738c4a405427a71eb31 Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Sun, 17 Sep 2006 15:29:48 +0200 Subject: gitweb: fix warnings in PATH_INFO code and add export_ok/strict_export Signed-off-by: Junio C Hamano --- gitweb/gitweb.perl | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'gitweb/gitweb.perl') diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index 497129aefc..0fb86385c9 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -189,9 +189,6 @@ sub feature_pickaxe { # version of the core git binary our $git_version = qx($GIT --version) =~ m/git version (.*)$/ ? $1 : "unknown"; -# path to the current git repository -our $git_dir; - $projects_list ||= $projectroot; # ====================================================================== @@ -273,30 +270,41 @@ sub feature_pickaxe { } # now read PATH_INFO and use it as alternative to parameters -our $path_info = $ENV{"PATH_INFO"}; -$path_info =~ s|^/||; -$path_info =~ s|/$||; -if (validate_input($path_info) && !defined $project) { +sub evaluate_path_info { + return if defined $project; + my $path_info = $ENV{"PATH_INFO"}; + return if !$path_info; + $path_info =~ s,(^/|/$),,gs; + $path_info = validate_input($path_info); + return if !$path_info; $project = $path_info; while ($project && !-e "$projectroot/$project/HEAD") { $project =~ s,/*[^/]*$,,; } - if (defined $project) { - $project = undef unless $project; + if (!$project || + ($export_ok && !-e "$projectroot/$project/$export_ok") || + ($strict_export && !project_in_list($project))) { + undef $project; + return; } + # do not change any parameters if an action is given using the query string + return if $action; if ($path_info =~ m,^$project/([^/]+)/(.+)$,) { # we got "project.git/branch/filename" $action ||= "blob_plain"; - $hash_base ||= $1; - $file_name ||= $2; + $hash_base ||= validate_input($1); + $file_name ||= validate_input($2); } elsif ($path_info =~ m,^$project/([^/]+)$,) { # we got "project.git/branch" $action ||= "shortlog"; - $hash ||= $1; + $hash ||= validate_input($1); } } +evaluate_path_info(); -$git_dir = "$projectroot/$project"; +# path to the current git repository +our $git_dir; +$git_dir = "$projectroot/$project" if $project; # dispatch my %actions = ( -- cgit 1.2.3-korg From ac8e3f2bb8a790ca7810df18c3282d07e84ae345 Mon Sep 17 00:00:00 2001 From: Matthias Lederhofer Date: Sun, 17 Sep 2006 13:52:45 +0200 Subject: gitweb fix validating pg (page) parameter Currently it is possible to give any string ending with a number as page. -1 for example is quite bad (error log shows probably 100 warnings). Signed-off-by: Junio C Hamano --- gitweb/gitweb.perl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'gitweb/gitweb.perl') diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index 0fb86385c9..c77270c7cd 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -256,7 +256,7 @@ sub feature_pickaxe { our $page = $cgi->param('pg'); if (defined $page) { - if ($page =~ m/[^0-9]$/) { + if ($page =~ m/[^0-9]/) { die_error(undef, "Invalid page parameter"); } } -- cgit 1.2.3-korg