{"thread":{"id":"43806","subject":"[PATCH/RFC] gitweb: Add 'show-sizes' feature to show blob sizes in tree view","startedAt":"2009-09-07T12:00:26Z","lastAt":"2009-09-07T12:00:26Z","messageCount":1,"participants":["Jakub Narebski"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"298987","messageId":"20090907114809.27552.2354.stgit@localhost.localdomain","threadId":"43806","inReplyTo":null,"subject":"[PATCH/RFC] gitweb: Add 'show-sizes' feature to show blob sizes in tree view","fromName":"Jakub Narebski","fromEmail":"jnareb@gmail.com","sentAt":"2009-09-07T12:00:26Z","receivedAt":"2009-09-07T12:00:26Z","isPatch":true,"sender":{"key":"jnareb@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2706?v=4"},"body":"Add support for 'show-sizes' feature to show (in separate column,\nbetween mode and filename) the size of blobs (files) in the 'tree'\nview.  It passes '-l' option to \"git ls-tree\" invocation.\n\nFor the 'tree' and 'commit' (submodule) entries, '-' is shown in place\nof size; for generated '..' \"up directory\" entry nothing is shown.\n\nThe 'show-sizes' feature is enabled by default.\n\nSigned-off-by: Jakub Narebski <jnareb@gmail.com>\n---\nI don't quite like 'show-sizes' as a name for this feature.  \nDoes anyone has any better ideas?\n\nNot tested extensively (just that it works, and looks correct),\nin particular I didn't test that having 'gitweb.showsizes' as\nconfig variable name while 'show-sizes' as feature name works\nas expected.\n\nPrevious version of this idea was presented in\n  http://thread.gmane.org/gmane.comp.version-control.git/54335/focus=54334\nIt required to pass '-l' to 'tree' action via 'opt' (extra options)\nparameter.  This one uses features mechanism.\n\nThis patch might have @extra_options passed unnecessary...\n\n gitweb/gitweb.css  |    6 +++++\n gitweb/gitweb.perl |   69 +++++++++++++++++++++++++++++++++++++++++-----------\n 2 files changed, 60 insertions(+), 15 deletions(-)\n\ndiff --git a/gitweb/gitweb.css b/gitweb/gitweb.css\nindex 8f68fe3..d60bfc1 100644\n--- a/gitweb/gitweb.css\n+++ b/gitweb/gitweb.css\n@@ -341,6 +341,12 @@ td.mode {\n \tfont-family: monospace;\n }\n \n+/* format of (optional) objects size in 'tree' view */\n+td.size {\n+\tfont-family: monospace;\n+\ttext-align: right;\n+}\n+\n /* styling of diffs (patchsets): commitdiff and blobdiff views */\n div.diff.header,\n div.diff.extended_header {\ndiff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl\nindex 24b2193..7b1c60e 100755\n--- a/gitweb/gitweb.perl\n+++ b/gitweb/gitweb.perl\n@@ -297,6 +297,19 @@ our %feature = (\n \t\t'override' => 0,\n \t\t'default' => [1]},\n \n+\t# Enable showing size of blobs in a 'tree' view, in a separate\n+\t# column, similar to what 'ls -l' does.  This cost a bit of IO.\n+\n+\t# To disable system wide have in $GITWEB_CONFIG\n+\t# $feature{'show-sizes'}{'default'} = [0];\n+\t# To have project specific config enable override in $GITWEB_CONFIG\n+\t# $feature{'show-sizes'}{'override'} = 1;\n+\t# and in project config gitweb.showsizes = 0|1;\n+\t'show-sizes' => {\n+\t\t'sub' => sub { feature_bool('showsizes', @_) },\n+\t\t'override' => 0,\n+\t\t'default' => [1]},\n+\n \t# Make gitweb use an alternative format of the URLs which can be\n \t# more readable and natural-looking: project name is embedded\n \t# directly in the path and the query string contains other\n@@ -2764,16 +2777,31 @@ sub parse_ls_tree_line {\n \tmy %opts = @_;\n \tmy %res;\n \n-\t#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa\tpanic.c'\n-\t$line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\\t(.+)$/s;\n+\tif ($opts{'-l'}) {\n+\t\t#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa   16717\tpanic.c'\n+\t\t$line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40}) +(-|[0-9]+)\\t(.+)$/s;\n \n-\t$res{'mode'} = $1;\n-\t$res{'type'} = $2;\n-\t$res{'hash'} = $3;\n-\tif ($opts{'-z'}) {\n-\t\t$res{'name'} = $4;\n+\t\t$res{'mode'} = $1;\n+\t\t$res{'type'} = $2;\n+\t\t$res{'hash'} = $3;\n+\t\t$res{'size'} = $4;\n+\t\tif ($opts{'-z'}) {\n+\t\t\t$res{'name'} = $5;\n+\t\t} else {\n+\t\t\t$res{'name'} = unquote($5);\n+\t\t}\n \t} else {\n-\t\t$res{'name'} = unquote($4);\n+\t\t#'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa\tpanic.c'\n+\t\t$line =~ m/^([0-9]+) (.+) ([0-9a-fA-F]{40})\\t(.+)$/s;\n+\n+\t\t$res{'mode'} = $1;\n+\t\t$res{'type'} = $2;\n+\t\t$res{'hash'} = $3;\n+\t\tif ($opts{'-z'}) {\n+\t\t\t$res{'name'} = $4;\n+\t\t} else {\n+\t\t\t$res{'name'} = unquote($4);\n+\t\t}\n \t}\n \n \treturn wantarray ? %res : \\%res;\n@@ -3564,6 +3592,9 @@ sub git_print_tree_entry {\n \t# and link is the action links of the entry.\n \n \tprint \"<td class=\\\"mode\\\">\" . mode_str($t->{'mode'}) . \"</td>\\n\";\n+\tif (exists $t->{'size'}) {\n+\t\tprint \"<td class=\\\"size\\\">$t->{'size'}</td>\\n\";\n+\t}\n \tif ($t->{'type'} eq \"blob\") {\n \t\tprint \"<td class=\\\"list\\\">\" .\n \t\t\t$cgi->a({-href => href(action=>\"blob\", hash=>$t->{'hash'},\n@@ -3609,12 +3640,14 @@ sub git_print_tree_entry {\n \t} elsif ($t->{'type'} eq \"tree\") {\n \t\tprint \"<td class=\\\"list\\\">\";\n \t\tprint $cgi->a({-href => href(action=>\"tree\", hash=>$t->{'hash'},\n-\t\t                             file_name=>\"$basedir$t->{'name'}\", %base_key)},\n+\t\t                             file_name=>\"$basedir$t->{'name'}\",\n+\t\t                             %base_key)},\n \t\t              esc_path($t->{'name'}));\n \t\tprint \"</td>\\n\";\n \t\tprint \"<td class=\\\"link\\\">\";\n \t\tprint $cgi->a({-href => href(action=>\"tree\", hash=>$t->{'hash'},\n-\t\t                             file_name=>\"$basedir$t->{'name'}\", %base_key)},\n+\t\t                             file_name=>\"$basedir$t->{'name'}\",\n+\t\t                             %base_key)},\n \t\t              \"tree\");\n \t\tif (defined $hash_base) {\n \t\t\tprint \" | \" .\n@@ -5088,10 +5121,14 @@ sub git_tree {\n \t}\n \tdie_error(404, \"No such tree\") unless defined($hash);\n \n+\tmy $show_sizes = gitweb_check_feature('show-sizes');\n+\tmy $have_blame = gitweb_check_feature('blame');\n+\n \tmy @entries = ();\n \t{\n \t\tlocal $/ = \"\\0\";\n-\t\topen my $fd, \"-|\", git_cmd(), \"ls-tree\", '-z', $hash\n+\t\topen my $fd, \"-|\", git_cmd(), \"ls-tree\", '-z',\n+\t\t\t($show_sizes ? '-l' : ()), @extra_options, $hash\n \t\t\tor die_error(500, \"Open git-ls-tree failed\");\n \t\t@entries = map { chomp; $_ } <$fd>;\n \t\tclose $fd\n@@ -5102,7 +5139,6 @@ sub git_tree {\n \tmy $ref = format_ref_marker($refs, $hash_base);\n \tgit_header_html();\n \tmy $basedir = '';\n-\tmy $have_blame = gitweb_check_feature('blame');\n \tif (defined $hash_base && (my %co = parse_commit($hash_base))) {\n \t\tmy @views_nav = ();\n \t\tif (defined $file_name) {\n@@ -5118,7 +5154,8 @@ sub git_tree {\n \t\t\t# FIXME: Should be available when we have no hash base as well.\n \t\t\tpush @views_nav, $snapshot_links;\n \t\t}\n-\t\tgit_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));\n+\t\tgit_print_page_nav('tree','', $hash_base, undef, undef,\n+\t\t                   join(' | ', @views_nav));\n \t\tgit_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);\n \t} else {\n \t\tundef $hash_base;\n@@ -5151,8 +5188,10 @@ sub git_tree {\n \t\tundef $up unless $up;\n \t\t# based on git_print_tree_entry\n \t\tprint '<td class=\"mode\">' . mode_str('040000') . \"</td>\\n\";\n+\t\tprint '<td class=\"size\">&nbsp;</td>'.\"\\n\" if $show_sizes;\n \t\tprint '<td class=\"list\">';\n-\t\tprint $cgi->a({-href => href(action=>\"tree\", hash_base=>$hash_base,\n+\t\tprint $cgi->a({-href => href(action=>\"tree\",\n+\t\t                             hash_base=>$hash_base,\n \t\t                             file_name=>$up)},\n \t\t              \"..\");\n \t\tprint \"</td>\\n\";\n@@ -5161,7 +5200,7 @@ sub git_tree {\n \t\tprint \"</tr>\\n\";\n \t}\n \tforeach my $line (@entries) {\n-\t\tmy %t = parse_ls_tree_line($line, -z => 1);\n+\t\tmy %t = parse_ls_tree_line($line, -z => 1, -l => $show_sizes);\n \n \t\tif ($alternate) {\n \t\t\tprint \"<tr class=\\\"dark\\\">\\n\";\n\n\n"}]}