# [PATCH 4/5] gitweb: optionally set project category from its pathname

6 messages from 2015-03-19 to 2015-03-27. Participants: Tony Finch, Junio C Hamano.
Thread: https://gitlist.dev/t/38857

## Tony Finch, 2015-03-19 15:39

Subject: [PATCH 2/5] gitweb: if the PATH_INFO is incomplete, use it as a project_filter
Message-ID: <alpine.LSU.2.00.1503191539570.23307@hermes-1.csi.cam.ac.uk>
URL: https://gitlist.dev/e/alpine.LSU.2.00.1503191539570.23307%40hermes-1.csi.cam.ac.uk
In-Reply-To: <5e56772f50d3d1498361d8831c4f2fba38d197b4.1426779553.git.dot@dotat.at>

```
Previously gitweb would ignore partial PATH_INFO. For example,
it would produce a project list for the top URL
	https://www.example.org/projects/
and a project summary for
	https://www.example.org/projects/git/git.git
but if you tried to list just the git-related projects with
	https://www.example.org/projects/git/
you would get a list of all projects, same as the top URL.

As well as fixing that omission, this change also makes gitweb
generate PATH_INFO-style URLs for project filter links, such
as in the breadcrumbs.

Signed-off-by: Tony Finch <dot@dotat.at>
---
 gitweb/gitweb.perl | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 7a5b23a..073f324 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -895,7 +895,17 @@ sub evaluate_path_info {
 	while ($project && !check_head_link("$projectroot/$project")) {
 		$project =~ s,/*[^/]*$,,;
 	}
-	return unless $project;
+	# If there is no project, use the PATH_INFO as a project filter if it
+	# is a directory in the projectroot. (It can't be a subdirectory of a
+	# repo because we just verified that isn't the case.)
+	unless ($project) {
+		if (-d "$projectroot/$path_info") {
+			$path_info =~ s,/+$,,;
+			$input_params{'project_filter'} = $path_info;
+			$path_info = "";
+		}
+		return;
+	}
 	$input_params{'project'} = $project;

 	# do not change any parameters if an action is given using the query string
@@ -1360,6 +1370,18 @@ sub href {
 	}

 	my $use_pathinfo = gitweb_check_feature('pathinfo');
+
+	# we have to check for a project_filter first because handling the full
+	# project-plus-parameters deletes some of the paramaters we check here
+	if (!defined $params{'project'} && $params{'project_filter'} &&
+	    $params{'action'} eq "project_list" &&
+	    (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
+		$href =~ s,/$,,;
+		$href .= "/".esc_path_info($params{'project_filter'})."/";
+		delete $params{'project_filter'};
+		delete $params{'action'};
+	}
+
 	if (defined $params{'project'} &&
 	    (exists $params{-path_info} ? $params{-path_info} : $use_pathinfo)) {
 		# try to put as many parameters as possible in PATH_INFO:
-- 
2.2.1.68.g56d9796

```

## Tony Finch, 2015-03-19 15:40

Subject: [PATCH 3/5] gitweb: add a link under the search box to clear a project filter
Message-ID: <alpine.LSU.2.00.1503191540090.23307@hermes-1.csi.cam.ac.uk>
URL: https://gitlist.dev/e/alpine.LSU.2.00.1503191540090.23307%40hermes-1.csi.cam.ac.uk
In-Reply-To: <5e56772f50d3d1498361d8831c4f2fba38d197b4.1426779553.git.dot@dotat.at>

```
Previously when a project filter was active, the only simple way
to clear it was by clicking the home link in the breadcrumbs, which
is not very obvious.

This change adds another home link under the search box which clears
both project filter and search, next to the existing link that
clears the search and keeps the project filter.

Signed-off-by: Tony Finch <dot@dotat.at>
---
 gitweb/gitweb.perl | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 073f324..9abc5bc 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -5549,10 +5549,13 @@ sub git_project_search_form {
 	      "</span>\n" .
 	      $cgi->submit(-name => 'btnS', -value => 'Search') .
 	      $cgi->end_form() . "\n" .
-	      $cgi->a({-href => href(project => undef, searchtext => undef,
-	                             project_filter => $project_filter)},
-	              esc_html("List all projects$limit")) . "<br />\n";
-	print "</div>\n";
+	      $cgi->a({-href => $my_uri}, esc_html("List all projects"));
+	print " / " .
+	      $cgi->a({-href => href(project => undef, action => "project_list",
+				     project_filter => $project_filter)},
+	              esc_html("List projects$limit"))
+	    if $project_filter;
+	print "<br />\n</div>\n";
 }

 # entry for given @keys needs filling if at least one of keys in list
-- 
2.2.1.68.g56d9796

```

## Tony Finch, 2015-03-19 15:40

Subject: [PATCH 4/5] gitweb: optionally set project category from its pathname
Message-ID: <alpine.LSU.2.00.1503191540150.23307@hermes-1.csi.cam.ac.uk>
URL: https://gitlist.dev/e/alpine.LSU.2.00.1503191540150.23307%40hermes-1.csi.cam.ac.uk
In-Reply-To: <5e56772f50d3d1498361d8831c4f2fba38d197b4.1426779553.git.dot@dotat.at>

```
When repositories are organized in a hierarchial directory tree
it is convenient if gitweb project categories can be set
automatically based on their parent directory, so that users
do not have to set the same information twice.

Signed-off-by: Tony Finch <dot@dotat.at>
---
 Documentation/gitweb.conf.txt |  6 ++++++
 gitweb/gitweb.perl            | 13 ++++++++++---
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/Documentation/gitweb.conf.txt b/Documentation/gitweb.conf.txt
index 29f1e06..7c0de18 100644
--- a/Documentation/gitweb.conf.txt
+++ b/Documentation/gitweb.conf.txt
@@ -492,6 +492,12 @@ $projects_list_group_categories::
 	`$GIT_DIR/category` file or the `gitweb.category` variable in each
 	repository's configuration.  Disabled by default (set to 0).

+$projects_list_directory_is_category::
+	Whether to set a project's category to its parent directory, i.e. its
+	pathname excluding the `/repo.git` leaf name. This is only used if
+	the repo has no explicit setting, and if the pathname has more than
+	one component. Disabled by default (set to 0).
+
 $project_list_default_category::
 	Default category for projects for which none is specified.  If this is
 	set to the empty string, such projects will remain uncategorized and
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 9abc5bc..0aab3e0 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -133,6 +133,10 @@ our $projects_list_description_width = 25;
 # (enabled if this variable evaluates to true)
 our $projects_list_group_categories = 0;

+# project's category defaults to its parent directory
+# (enabled if this variable evaluates to true)
+our $projects_list_directory_is_category = 0;
+
 # default category if none specified
 # (leave the empty string for no category)
 our $project_list_default_category = "";
@@ -2908,7 +2912,11 @@ sub git_get_project_description {

 sub git_get_project_category {
 	my $path = shift;
-	return git_get_file_or_project_config($path, 'category');
+	my $cat = git_get_file_or_project_config($path, 'category');
+	return $cat if $cat;
+	return $1 if $projects_list_directory_is_category
+		  && $path =~ m,^(.*)/[^/]*$,;
+	return $project_list_default_category;
 }


@@ -5622,8 +5630,7 @@ sub fill_project_list_info {
 		}
 		if ($projects_list_group_categories &&
 		    project_info_needs_filling($pr, $filter_set->('category'))) {
-			my $cat = git_get_project_category($pr->{'path'}) ||
-			                                   $project_list_default_category;
+			my $cat = git_get_project_category($pr->{'path'});
 			$pr->{'category'} = to_utf8($cat);
 		}

-- 
2.2.1.68.g56d9796

```

## Tony Finch, 2015-03-19 15:40

Subject: [PATCH 5/5] gitweb: make category headings into links when they are directories
Message-ID: <alpine.LSU.2.00.1503191540020.23307@hermes-1.csi.cam.ac.uk>
URL: https://gitlist.dev/e/alpine.LSU.2.00.1503191540020.23307%40hermes-1.csi.cam.ac.uk
In-Reply-To: <5e56772f50d3d1498361d8831c4f2fba38d197b4.1426779553.git.dot@dotat.at>

```
When $projects_list_category_is_directory is turned on, project
categories can be useful as project filters, so with that setting
gitweb now makes the category headings into project_filter links
(like the breadcrumbs).

Signed-off-by: Tony Finch <dot@dotat.at>
---
 gitweb/gitweb.perl | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 0aab3e0..a02f3e4 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -5838,8 +5838,18 @@ sub git_project_list_body {
 				if ($check_forks) {
 					print "<td></td>\n";
 				}
-				print "<td class=\"category\" colspan=\"5\">".esc_html($cat)."</td>\n";
-				print "</tr>\n";
+				print "<td class=\"category\" colspan=\"5\">";
+				if ($projects_list_directory_is_category) {
+					print $cgi->a({-href =>
+					    href(project => undef,
+					        project_filter => $cat,
+					        action => "project_list"),
+					    -class => "list"},
+					    esc_html($cat));
+				} else {
+					print esc_html($cat);
+				}
+				print "</td>\n</tr>\n";
 			}

 			git_project_list_rows($categories{$cat}, undef, undef, $check_forks);
-- 
2.2.1.68.g56d9796

```

## Junio C Hamano, 2015-03-26 19:49

Subject: Re: [PATCH 5/5] gitweb: make category headings into links when they are directories
Message-ID: <xmqqwq23y1xl.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqwq23y1xl.fsf%40gitster.dls.corp.google.com
In-Reply-To: <alpine.LSU.2.00.1503191540020.23307@hermes-1.csi.cam.ac.uk>

```
Any comments from those who use or have their own code in Gitweb on
this topic?

* tf/gitweb-project-listing (2015-03-19) 5 commits
 - gitweb: make category headings into links when they are directories
 - gitweb: optionally set project category from its pathname
 - gitweb: add a link under the search box to clear a project filter
 - gitweb: if the PATH_INFO is incomplete, use it as a project_filter
 - gitweb: fix typo in man page

 Update gitweb to make it more pleasant to deal with a hierarchical
 forest of repositories.

```

## Tony Finch, 2015-03-27 00:14

Subject: Re: [PATCH 5/5] gitweb: make category headings into links when they are directories
Message-ID: <28AB00ED-7EA7-43C1-84BE-BD290E09318F@dotat.at>
URL: https://gitlist.dev/e/28AB00ED-7EA7-43C1-84BE-BD290E09318F%40dotat.at
In-Reply-To: <xmqqwq23y1xl.fsf@gitster.dls.corp.google.com>

```

> On 26 Mar 2015, at 19:49, Junio C Hamano <gitster@pobox.com> wrote:
> 
> Any comments from those who use or have their own code in Gitweb on
> this topic?

Thanks for chasing up my patches. I should have written a covering letter, to say that you can see these patches in action at https://git.csx.cam.ac.uk/x/ucs/ - try clicking on the category headings, and observe the pathinfo, breadcrumbs, and links under the search box.

Tony.
-- 
f.anthony.n.finch  <dot@dotat.at>  http://dotat.at
```
