threads / patch / 10556

patch, 3 partsgitweb: Simplify some gitweb URLs generation

Subject: [PATCH 0/3] gitweb: Simplify some gitweb URLs generation

## tl;dr

7 messages between Nov 1, 2007 and Nov 2, 2007. Diffs are folded; open one to read it.

replies: 6people: 2as markdown or json

Jakub Narebski· Nov 1, 2007, 12:06 UTC · lore

This series of patches simplifies some gitweb URLs generation by providing -replay option to href() subroutine, and then using this feature in gitweb code.

Shortlog:
 gitweb: Easier adding/changing parameters to current URL
 gitweb: Use href(-replay=>1, page=>...) to generate pagination links
 gitweb: Use href(-replay=>1, action=>...) to generate alternate views
Diffstat:
 gitweb/gitweb.perl |   82 ++++++++++++++++++++++-----------------------------
 1 files changed, 35 insertions(+), 47 deletions(-)
-- 
Jakub Narebski
Poland
Jakub Narebski· Nov 1, 2007, 12:06 UTC · re: Jakub Narebski · lore
 gitweb/gitweb.perl |   82 ++++++++++++++++++++++-----------------------------
 1 files changed, 35 insertions(+), 47 deletions(-)
 gitweb/gitweb.perl |   82 ++++++++++++++++++++++-----------------------------
 1 files changed, 35 insertions(+), 47 deletions(-)
 [PATCH 1/3] gitweb: Easier adding/changing parameters to current URL
 [PATCH 2/3] gitweb: Use href(-replay=>1, page=>...) to generate pagination links
 [PATCH 3/3] gitweb: Use href(-replay=>1, action=>...) to generate alternate views
Jakub Narebski· Nov 1, 2007, 12:06 UTC · re: Jakub Narebski · lore

[PATCH 1/3] gitweb: Easier adding/changing parameters to current URL

Add boolean option '-replay' to href() subroutine, which is used to generate links in gitweb. This option "replays" current URL, overriding it with provided parameters. It means that current value of each CGI parameter is used unless otherwise provided.

This change is meant to make it easier to generate links which differ
from current page URL only by one parameter, for example the same view
but sorted by different column:
  href(-replay=>1, order=>"age")
or view which differs by some option, e.g. in log views
  href(-replay=>1, extra_options=>"--no-merges")
or alternate view of the same object, e.g. in the 'blob' view
  href(-replay=>1, action=>"blob_plain")
Actual use of this functionality is left for later.
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
 gitweb/gitweb.perl |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
Show changes to gitweb/gitweb.perl +9 −0
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index e36dec1..eaab895 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -611,6 +611,15 @@ sub href(%) {
 	);
 	my %mapping = @mapping;
 
+	if ($params{-replay}) {
+		while (my ($name, $symbol) = each %mapping) {
+			if (!exists $params{$name}) {
+				# to allow for multivalued params we use arrayref form
+				$params{$name} = [ $cgi->param($symbol) ];
+			}
+		}
+	}
+
 	$params{'project'} = $project unless exists $params{'project'};
 
 	my ($use_pathinfo) = gitweb_check_feature('pathinfo');
-- 
1.5.3.4
Jakub Narebski· Nov 1, 2007, 12:06 UTC · re: Jakub Narebski · lore

[PATCH 2/3] gitweb: Use href(-replay=>1, page=>...) to generate pagination links

Use href(-replay=>1, page=>$page-1) and href(-replay=>1, page=>$page+1) to generate previous page and next page links.

Generate next page link only once.
Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
 gitweb/gitweb.perl |   44 +++++++++++++++-----------------------------
 1 files changed, 15 insertions(+), 29 deletions(-)
Show changes to gitweb/gitweb.perl +15 −29
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index eaab895..20d4009 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -2518,7 +2518,7 @@ sub format_paging_nav {
 
 	if ($page > 0) {
 		$paging_nav .= " &sdot; " .
-			$cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page-1),
+			$cgi->a({-href => href(-replay=>1, page=>$page-1),
 			         -accesskey => "p", -title => "Alt-p"}, "prev");
 	} else {
 		$paging_nav .= " &sdot; prev";
@@ -2526,7 +2526,7 @@ sub format_paging_nav {
 
 	if ($nrevs >= (100 * ($page+1)-1)) {
 		$paging_nav .= " &sdot; " .
-			$cgi->a({-href => href(action=>$action, hash=>$hash, page=>$page+1),
+			$cgi->a({-href => href(-replay=>1, page=>$page+1),
 			         -accesskey => "n", -title => "Alt-n"}, "next");
 	} else {
 		$paging_nav .= " &sdot; next";
@@ -4448,7 +4448,7 @@ sub git_log {
 	}
 	if ($#commitlist >= 100) {
 		print "<div class=\"page_nav\">\n";
-		print $cgi->a({-href => href(action=>"log", hash=>$hash, page=>$page+1),
+		print $cgi->a({-href => href(-replay=>1, page=>$page+1),
 			       -accesskey => "n", -title => "Alt-n"}, "next");
 		print "</div>\n";
 	}
@@ -5015,27 +5015,20 @@ sub git_history {
 			                       file_name=>$file_name)},
 			        "first");
 		$paging_nav .= " &sdot; " .
-			$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
-			                       file_name=>$file_name, page=>$page-1),
+			$cgi->a({-href => href(-replay=>1, page=>$page-1),
 			         -accesskey => "p", -title => "Alt-p"}, "prev");
 	} else {
 		$paging_nav .= "first";
 		$paging_nav .= " &sdot; prev";
 	}
-	if ($#commitlist >= 100) {
-		$paging_nav .= " &sdot; " .
-			$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
-			                       file_name=>$file_name, page=>$page+1),
-			         -accesskey => "n", -title => "Alt-n"}, "next");
-	} else {
-		$paging_nav .= " &sdot; next";
-	}
 	my $next_link = '';
 	if ($#commitlist >= 100) {
 		$next_link =
-			$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base,
-			                       file_name=>$file_name, page=>$page+1),
+			$cgi->a({-href => href(-replay=>1, page=>$page+1),
 			         -accesskey => "n", -title => "Alt-n"}, "next");
+		$paging_nav .= " &sdot; $next_link";
+	} else {
+		$paging_nav .= " &sdot; next";
 	}
 
 	git_header_html();
@@ -5105,30 +5098,23 @@ sub git_search {
 				                       searchtext=>$searchtext, searchtype=>$searchtype)},
 				        "first");
 			$paging_nav .= " &sdot; " .
-				$cgi->a({-href => href(action=>"search", hash=>$hash,
-				                       searchtext=>$searchtext, searchtype=>$searchtype,
-				                       page=>$page-1),
+				$cgi->a({-href => href(-replay=>1, page=>$page-1),
 				         -accesskey => "p", -title => "Alt-p"}, "prev");
 		} else {
 			$paging_nav .= "first";
 			$paging_nav .= " &sdot; prev";
 		}
+		my $next_link = '';
 		if ($#commitlist >= 100) {
-			$paging_nav .= " &sdot; " .
-				$cgi->a({-href => href(action=>"search", hash=>$hash,
-				                       searchtext=>$searchtext, searchtype=>$searchtype,
-				                       page=>$page+1),
+			$next_link =
+				$cgi->a({-href => href(-replay=>1, page=>$page+1),
 				         -accesskey => "n", -title => "Alt-n"}, "next");
+			$paging_nav .= " &sdot; $next_link";
 		} else {
 			$paging_nav .= " &sdot; next";
 		}
-		my $next_link = '';
+
 		if ($#commitlist >= 100) {
-			$next_link =
-				$cgi->a({-href => href(action=>"search", hash=>$hash,
-				                       searchtext=>$searchtext, searchtype=>$searchtype,
-				                       page=>$page+1),
-				         -accesskey => "n", -title => "Alt-n"}, "next");
 		}
 
 		git_print_page_nav('','', $hash,$co{'tree'},$hash, $paging_nav);
@@ -5327,7 +5313,7 @@ sub git_shortlog {
 	my $next_link = '';
 	if ($#commitlist >= 100) {
 		$next_link =
-			$cgi->a({-href => href(action=>"shortlog", hash=>$hash, page=>$page+1),
+			$cgi->a({-href => href(-replay=>1, page=>$page+1),
 			         -accesskey => "n", -title => "Alt-n"}, "next");
 	}
 
-- 
1.5.3.4
Jakub Narebski· Nov 1, 2007, 12:06 UTC · re: Jakub Narebski · lore

[PATCH 3/3] gitweb: Use href(-replay=>1, action=>...) to generate alternate views

Use href(action=>..., -replay=>1) to generate links to alternate views of current page in the $formats_nav (bottom) part of page_nav navigation bar. This form is used only when all parameters are repeated, and when the replay form is shorter.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
---
 gitweb/gitweb.perl |   29 +++++++++++------------------
 1 files changed, 11 insertions(+), 18 deletions(-)
Show changes to gitweb/gitweb.perl +11 −18
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 20d4009..827f977 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -3911,11 +3911,11 @@ sub git_blame2 {
 		or die_error(undef, "Open git-blame failed");
 	git_header_html();
 	my $formats_nav =
-		$cgi->a({-href => href(action=>"blob", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
+		$cgi->a({-href => href(action=>"blob", -replay=>1)},
 		        "blob") .
 		" | " .
-		$cgi->a({-href => href(action=>"history", hash=>$hash, hash_base=>$hash_base, file_name=>$file_name)},
-			"history") .
+		$cgi->a({-href => href(action=>"history", -replay=>1)},
+		        "history") .
 		" | " .
 		$cgi->a({-href => href(action=>"blame", file_name=>$file_name)},
 		        "HEAD");
@@ -4191,18 +4191,15 @@ sub git_blob {
 		if (defined $file_name) {
 			if ($have_blame) {
 				$formats_nav .=
-					$cgi->a({-href => href(action=>"blame", hash_base=>$hash_base,
-					                       hash=>$hash, file_name=>$file_name)},
+					$cgi->a({-href => href(action=>"blame", -replay=>1)},
 					        "blame") .
 					" | ";
 			}
 			$formats_nav .=
-				$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
-				                       hash=>$hash, file_name=>$file_name)},
+				$cgi->a({-href => href(action=>"history", -replay=>1)},
 				        "history") .
 				" | " .
-				$cgi->a({-href => href(action=>"blob_plain",
-				                       hash=>$hash, file_name=>$file_name)},
+				$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
 				        "raw") .
 				" | " .
 				$cgi->a({-href => href(action=>"blob",
@@ -4210,7 +4207,8 @@ sub git_blob {
 				        "HEAD");
 		} else {
 			$formats_nav .=
-				$cgi->a({-href => href(action=>"blob_plain", hash=>$hash)}, "raw");
+				$cgi->a({-href => href(action=>"blob_plain", -replay=>1)},
+				        "raw");
 		}
 		git_print_page_nav('','', $hash_base,$co{'tree'},$hash_base, $formats_nav);
 		git_print_header_div('commit', esc_html($co{'title'}), $hash_base);
@@ -4273,8 +4271,7 @@ sub git_tree {
 		my @views_nav = ();
 		if (defined $file_name) {
 			push @views_nav,
-				$cgi->a({-href => href(action=>"history", hash_base=>$hash_base,
-				                       hash=>$hash, file_name=>$file_name)},
+				$cgi->a({-href => href(action=>"history", -replay=>1)},
 				        "history"),
 				$cgi->a({-href => href(action=>"tree",
 				                       hash_base=>"HEAD", file_name=>$file_name)},
@@ -4742,10 +4739,7 @@ sub git_blobdiff {
 	# header
 	if ($format eq 'html') {
 		my $formats_nav =
-			$cgi->a({-href => href(action=>"blobdiff_plain",
-			                       hash=>$hash, hash_parent=>$hash_parent,
-			                       hash_base=>$hash_base, hash_parent_base=>$hash_parent_base,
-			                       file_name=>$file_name, file_parent=>$file_parent)},
+			$cgi->a({-href => href(action=>"blobdiff_plain", -replay=>1)},
 			        "raw");
 		git_header_html(undef, $expires);
 		if (defined $hash_base && (my %co = parse_commit($hash_base))) {
@@ -4819,8 +4813,7 @@ sub git_commitdiff {
 	my $formats_nav;
 	if ($format eq 'html') {
 		$formats_nav =
-			$cgi->a({-href => href(action=>"commitdiff_plain",
-			                       hash=>$hash, hash_parent=>$hash_parent)},
+			$cgi->a({-href => href(action=>"commitdiff_plain", -replay=>1)},
 			        "raw");
 
 		if (defined $hash_parent &&
-- 
1.5.3.4
Junio C Hamano· Nov 1, 2007, 22:47 UTC · re: Jakub Narebski · lore

Re: [PATCH 0/3] gitweb: Simplify some gitweb URLs generation

Jakub Narebski <jnareb@gmail.com> writes:
Show 12 quoted lines
> This series of patches simplifies some gitweb URLs generation by
> providing -replay option to href() subroutine, and then using this
> feature in gitweb code.
>
> Shortlog:
>  gitweb: Easier adding/changing parameters to current URL
>  gitweb: Use href(-replay=>1, page=>...) to generate pagination links
>  gitweb: Use href(-replay=>1, action=>...) to generate alternate views
>
> Diffstat:
>  gitweb/gitweb.perl |   82 ++++++++++++++++++++++-----------------------------
>  1 files changed, 35 insertions(+), 47 deletions(-)

If this "-replay" is used carelessly, it could add parameters that were passed to the page that the original code stripped away from passing on purpose. Have you checked if the conversion done with 2/3 and 3/3 are correct (I haven't)?

Jakub Narebski· Nov 2, 2007, 00:18 UTC · re: Junio C Hamano · lore

Re: [PATCH 0/3] gitweb: Simplify some gitweb URLs generation

Junio C Hamano wrote:
Show 19 quoted lines
> Jakub Narebski <jnareb@gmail.com> writes:
> 
>> This series of patches simplifies some gitweb URLs generation by
>> providing -replay option to href() subroutine, and then using this
>> feature in gitweb code.
>>
>> Shortlog:
>>  gitweb: Easier adding/changing parameters to current URL
>>  gitweb: Use href(-replay=>1, page=>...) to generate pagination links
>>  gitweb: Use href(-replay=>1, action=>...) to generate alternate views
>>
>> Diffstat:
>>  gitweb/gitweb.perl |   82 ++++++++++++++++++++++-----------------------------
>>  1 files changed, 35 insertions(+), 47 deletions(-)
> 
> If this "-replay" is used carelessly, it could add parameters
> that were passed to the page that the original code stripped
> away from passing on purpose.  Have you checked if the
> conversion done with 2/3 and 3/3 are correct (I haven't)?

First, I guess that I should have marked this series of patches as an RFC, but not for the reason you mentioned. This series changes somewhat gitweb behaviour (patches 2/3 and 3/3 to be more exact) in a way that before gitweb passed 'expanded' hashes to page / alternate view links. For example before this series link to second page for shortlog view starting from 'HEAD' passed literal sha1 for 'h' parameter, ans second page started where first page ended. After this patch, if refs have changed between viewing first page and clicking on the link to second page, the resulting view would be second page of _new_ 'HEAD', not of old (shown) 'HEAD'.

Second, you do not need to worry about adding parameters, because in 2/3 and 3/3 -replay is used to change view to the one which differs from current by one parameter. Please take into account that some parameters are just not important.

-- 
Jakub Narebski
Poland

← back to recent threads