Skip to contentSkip to conversation

gitweb: Simplify some gitweb URLs generation

PATCH In review Jakub Narebski 2 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover gitweb: Simplify some gitweb URLs generation

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(-)
Signature · 2 lines
Jakub Narebski
Poland
#1 lore raw

cover gitweb: Simplify some gitweb URLs generation

 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

#2 lore raw

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

In reply to Jakub Narebski

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.

---
 gitweb/gitweb.perl |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
gitweb/gitweb.perl +9 -0
index e36dec1..eaab895 100755
@@ -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');
Signature · 1 line
1.5.3.4
#3 lore raw

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

In reply to Jakub Narebski

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.

---
 gitweb/gitweb.perl |   44 +++++++++++++++-----------------------------
 1 files changed, 15 insertions(+), 29 deletions(-)
gitweb/gitweb.perl +15 -29
index eaab895..20d4009 100755
@@ -2518,7 +2518,7 @@ sub format_paging_nav {
 
 	if ($page > 0) {
 		$paging_nav .= " ⋅ " .
-			$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 .= " ⋅ prev";
@@ -2526,7 +2526,7 @@ sub format_paging_nav {
 
 	if ($nrevs >= (100 * ($page+1)-1)) {
 		$paging_nav .= " ⋅ " .
-			$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 .= " ⋅ 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");
 	}
 
Signature · 1 line
1.5.3.4
#4 lore raw

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

In reply to Jakub Narebski

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.

---
 gitweb/gitweb.perl |   29 +++++++++++------------------
 1 files changed, 11 insertions(+), 18 deletions(-)
gitweb/gitweb.perl +11 -18
index 20d4009..827f977 100755
@@ -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 &&
Signature · 1 line
1.5.3.4
#5 lore raw
In reply to Jakub Narebski
Quoted 4 lines · Jakub Narebski

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

Quoted 8 lines

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)?

#6 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Jakub Narebski

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

Quoted 8 lines
Quoted 3 lines

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.

Signature · 2 lines
Jakub Narebski
Poland