Subject: [RFC/PATCH 2/2] gitweb: Add an option to show size of blobs in the tree view

From: Jakub Narebski <[email-masked]>
Date: Tue, 31 Jul 2007 13:19:50 +0200

Add support for extra option ('opt' parameter) '-l' for the 'tree'
view, to show (in separate column, between mode and filename) the size
of blobs (files).  This option is passed through to all the 'tree'
links, so from 'tree' view with size of blobs on go to 'tree' view
also with size of blobs.

For the 'tree' and 'commit' (submodule) entries, '-' is shown in place
of size.

Currently you have to add ";opt=-l" to URL by hand to start.

Signed-off-by: Jakub Narebski <[email-masked]>
---
This patch is an example of using infrastructure introduced by the
earlier patch: 
  gitweb: Allow for multivalued parameters passed to href subroutine

It allows to play with 'tree' view with blob size. Currently you
have to start browsing by adding ";opt=-l" to the gitweb URL by
hand.  There is not link which will change view from ordinary 'tree'
view to 'tree' view with blob sizes.

The 'tree' with blob size view is slightly more costly than the
ordinary, old 'tree' view, but not much more (0.018s vs 0.012s
in the hot cache case), so I don't think we need to control it
as a enabled (or disabled) feature, overrideable or not.  It
probably should not be default.

What do you think about this?

 gitweb/gitweb.css  |    5 ++++
 gitweb/gitweb.perl |   53 ++++++++++++++++++++++++++++++++++++++-------------
 2 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/gitweb/gitweb.css b/gitweb/gitweb.css
index 096313b..dd24546 100644
--- a/gitweb/gitweb.css
+++ b/gitweb/gitweb.css
@@ -326,6 +326,11 @@ td.mode {
 	font-family: monospace;
 }
 
+td.size {
+	font-family: monospace;
+	text-align: right;
+}
+
 /* styling of diffs (patchsets): commitdiff and blobdiff views */
 div.diff.header,
 div.diff.extended_header {
diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl
index 498b936..97ad1da 100755
--- a/gitweb/gitweb.perl
+++ b/gitweb/gitweb.perl
@@ -432,6 +432,7 @@ if (defined $hash_base) {
 
 my %allowed_options = (
 	"--no-merges" => [ qw(rss atom log shortlog history) ],
+	"-l" => [ qw(tree) ],
 );
 
 our @extra_options = $cgi->param('opt');
@@ -1998,16 +1999,31 @@ sub parse_ls_tree_line ($;%) {
 	my %opts = @_;
 	my %res;
 
-	#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa	panic.c'
-	$line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
+	if ($opts{'-l'}) {
+		#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa   16717	panic.c'
+		$line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\t(.+)$/s;
 
-	$res{'mode'} = $1;
-	$res{'type'} = $2;
-	$res{'hash'} = $3;
-	if ($opts{'-z'}) {
-		$res{'name'} = $4;
+		$res{'mode'} = $1;
+		$res{'type'} = $2;
+		$res{'hash'} = $3;
+		$res{'size'} = $4;
+		if ($opts{'-z'}) {
+			$res{'name'} = $5;
+		} else {
+			$res{'name'} = unquote($5);
+		}
 	} else {
-		$res{'name'} = unquote($4);
+		#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa	panic.c'
+		$line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/s;
+
+		$res{'mode'} = $1;
+		$res{'type'} = $2;
+		$res{'hash'} = $3;
+		if ($opts{'-z'}) {
+			$res{'name'} = $4;
+		} else {
+			$res{'name'} = unquote($4);
+		}
 	}
 
 	return wantarray ? %res : \%res;
@@ -2679,6 +2695,9 @@ sub git_print_tree_entry {
 	# and link is the action links of the entry.
 
 	print "<td class=\"mode\">" . mode_str($t->{'mode'}) . "</td>\n";
+	if (exists $t->{'size'}) {
+		print "<td class=\"size\">$t->{'size'}</td>\n";
+	}
 	if ($t->{'type'} eq "blob") {
 		print "<td class=\"list\">" .
 			$cgi->a({-href => href(action=>"blob", hash=>$t->{'hash'},
@@ -4268,8 +4287,9 @@ sub git_tree {
 			$hash = $hash_base;
 		}
 	}
+
 	$/ = "\0";
-	open my $fd, "-|", git_cmd(), "ls-tree", '-z', $hash
+	open my $fd, "-|", git_cmd(), "ls-tree", '-z', @extra_options, $hash
 		or die_error(undef, "Open git-ls-tree failed");
 	my @entries = map { chomp; $_ } <$fd>;
 	close $fd or die_error(undef, "Reading tree failed");
@@ -4280,6 +4300,7 @@ sub git_tree {
 	git_header_html();
 	my $basedir = '';
 	my ($have_blame) = gitweb_check_feature('blame');
+	my $show_sizes = grep(/^-l$/, @extra_options);
 	if (defined $hash_base && (my %co = parse_commit($hash_base))) {
 		my @views_nav = ();
 		if (defined $file_name) {
@@ -4288,7 +4309,8 @@ sub git_tree {
 				                       hash=>$hash, file_name=>$file_name)},
 				        "history"),
 				$cgi->a({-href => href(action=>"tree",
-				                       hash_base=>"HEAD", file_name=>$file_name)},
+				                       hash_base=>"HEAD", file_name=>$file_name,
+				                       extra_options=>\@extra_options)},
 				        "HEAD"),
 		}
 		my $snapshot_links = format_snapshot_links($hash);
@@ -4296,7 +4318,8 @@ sub git_tree {
 			# FIXME: Should be available when we have no hash base as well.
 			push @views_nav, $snapshot_links;
 		}
-		git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
+		git_print_page_nav('tree','', $hash_base, undef, undef,
+		                   join(' | ', @views_nav));
 		git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
 	} else {
 		undef $hash_base;
@@ -4330,8 +4353,10 @@ sub git_tree {
 		# based on git_print_tree_entry
 		print '<td class="mode">' . mode_str('040000') . "</td>\n";
 		print '<td class="list">';
-		print $cgi->a({-href => href(action=>"tree", hash_base=>$hash_base,
-		                             file_name=>$up)},
+		print $cgi->a({-href => href(action=>"tree",
+		                             hash_base=>$hash_base,
+		                             file_name=>$up,
+		                             extra_options=>\@extra_options)},
 		              "..");
 		print "</td>\n";
 		print "<td class=\"link\"></td>\n";
@@ -4339,7 +4364,7 @@ sub git_tree {
 		print "</tr>\n";
 	}
 	foreach my $line (@entries) {
-		my %t = parse_ls_tree_line($line, -z => 1);
+		my %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);
 
 		if ($alternate) {
 			print "<tr class=\"dark\">\n";
-- 
1.5.2.4