fixes to gitweb feature check code
v2 cover fixes to gitweb feature check code
This is v2 of the gitweb feature check fix patch, which has now been split into into a code patch and cleanup patch.
The first patch introduces git_get_feature() to clearly distinguish the feature retrieval from the boolean feature check (which is kept at git_check_feature()). The new function is used where appropriate.
The second patch cleans up use of git_check_feature(): since the function now returns a boolean instead of an array, the often-used construct
my ($somevar) = git_check_feature('somefeat');
although still valid, becomes a rather clumsy stylistic choice, as it introduces an unnecessary ambiguity. Make it clear that we're now dealing with scalars by using scalar assignment.
Giuseppe Bilotta (2):
gitweb: fixes to gitweb feature check code
gitweb: clean up git_check_feature() calls
gitweb/gitweb.perl | 52 +++++++++++++++++++++++++++++++++++----------------- 1 files changed, 35 insertions(+), 17 deletions(-)
PATCH v2 1/2 gitweb: fixes to gitweb feature check code
The gitweb_check_feature routine was being used for two different
purposes: retrieving the actual feature value (such as the list of snapshot formats or the list of additional actions), and to check if a feature was enabled.
For the latter use, since all features return an array, it led to either clumsy code or subtle bugs, with disabled features appearing enabled because (0) evaluates to 1.
We fix these bugs, and simplify the code, by separating feature (list) value retrieval (gitweb_get_feature) from boolean feature check (i.e. retrieving the first/only item in the feature value list). Usage of gitweb_check_feature is replaced by gitweb_get_feature where appropriate.
--- gitweb/gitweb.perl | 26 ++++++++++++++++++++++---- 1 files changed, 22 insertions(+), 4 deletions(-)
gitweb/gitweb.perl +22 -4
@@ -190,7 +190,9 @@ our %feature = ( # if there is no 'sub' key (no feature-sub), then feature cannot be # overriden # - # use gitweb_check_feature(<feature>) to check if <feature> is enabled + # use gitweb_get_feature(<feature>) to retrieve the <feature> value + # (an array) or gitweb_check_feature(<feature>) to check if <feature> + # is enabled # Enable the 'blame' blob view, showing the last commit that modified # each line in the file. This can be very CPU-intensive.
@@ -329,7 +331,8 @@ our %feature = ( 'default' => [0]}, ); -sub gitweb_check_feature { +# retrieve the value of a given feature, as an array +sub gitweb_get_feature { my ($name) = @_; return unless exists $feature{$name}; my ($sub, $override, @defaults) = (
@@ -344,6 +347,21 @@ sub gitweb_check_feature { return $sub->(@defaults); } +# check if a given feature is enabled or not, returning the first (and only) +# value of the feature. Comfort code, allowing the use of +# my $bool_feat = gitweb_check_feature('bool_feat'); +# or +# gitweb_check_feature('bool_feat') or somecode; +# instead of +# my ($bool_feat) = gitweb_git_feature('bool_feat'); +# or +# (gitweb_check_feature('bool_feat'))[0] or somecode; +# respectively +sub gitweb_check_feature { + return (gitweb_get_feature(@_))[0]; +} + + sub feature_blame { my ($val) = git_get_project_config('blame', '--bool');
@@ -767,7 +785,7 @@ our $git_dir; $git_dir = "$projectroot/$project" if $project; # list of supported snapshot formats -our @snapshot_fmts = gitweb_check_feature('snapshot'); +our @snapshot_fmts = gitweb_get_feature('snapshot'); @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts); # dispatch
@@ -3084,7 +3102,7 @@ sub git_print_page_nav { $arg{'tree'}{'hash'} = $treehead if defined $treehead; $arg{'tree'}{'hash_base'} = $treebase if defined $treebase; - my @actions = gitweb_check_feature('actions'); + my @actions = gitweb_get_feature('actions'); my %repl = ( '%' => '%', 'n' => $project, # project name
Signature · 1 line
1.5.6.5
PATCH v2 2/2 gitweb: clean up git_check_feature() calls
Since git_check_feature now returns a scalar, the parentheses around the variables initialized from git_check_feature() are not needed.
We remove them for stylistic consistency and to prevent unnecessary ambiguity in the code.
--- gitweb/gitweb.perl | 26 +++++++++++++------------- 1 files changed, 13 insertions(+), 13 deletions(-)
gitweb/gitweb.perl +13 -13
@@ -828,7 +828,7 @@ sub href (%) { } } - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { # try to put as many parameters as possible in PATH_INFO: # - project name
@@ -2119,7 +2119,7 @@ sub git_get_projects_list { $filter ||= ''; $filter =~ s/\.git$//; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if (-d $projects_list) { # search in directory
@@ -2965,7 +2965,7 @@ EOF } print "</div>\n"; - my ($have_search) = gitweb_check_feature('search'); + my $have_search = gitweb_check_feature('search'); if (defined $project && $have_search) { if (!defined $searchtext) { $searchtext = "";
@@ -2979,7 +2979,7 @@ EOF $search_hash = "HEAD"; } my $action = $my_uri; - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { $action .= "/".esc_url($project); }
@@ -3472,7 +3472,7 @@ sub is_patch_split { sub git_difftree_body { my ($difftree, $hash, @parents) = @_; my ($parent) = $parents[0]; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); print "<div class=\"list_head\">\n"; if ($#{$difftree} > 10) { print(($#{$difftree} + 1) . " files changed:\n");
@@ -3986,7 +3986,7 @@ sub git_project_list_body { # actually uses global variable $project my ($projlist, $order, $from, $to, $extra, $no_header) = @_; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); my @projects = fill_project_list_info($projlist, $check_forks); $order ||= $default_projects_order;
@@ -4446,7 +4446,7 @@ sub git_summary { my @taglist = git_get_tags_list(16); my @headlist = git_get_heads_list(16); my @forklist; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if ($check_forks) { @forklist = git_get_projects_list($project);
@@ -4475,7 +4475,7 @@ sub git_summary { } # Tag cloud - my $show_ctags = (gitweb_check_feature('ctags'))[0]; + my $show_ctags = gitweb_check_feature('ctags'); if ($show_ctags) { my $ctags = git_get_project_ctags($project); my $cloud = git_populate_project_tagcloud($ctags);
@@ -4765,7 +4765,7 @@ sub git_blob { $expires = "+1d"; } - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash or die_error(500, "Couldn't cat $file_name, $hash"); my $mimetype = blob_mimetype($fd, $file_name);
@@ -4858,7 +4858,7 @@ sub git_tree { my $ref = format_ref_marker($refs, $hash_base); git_header_html(); my $basedir = ''; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); if (defined $hash_base && (my %co = parse_commit($hash_base))) { my @views_nav = (); if (defined $file_name) {
@@ -5856,7 +5856,7 @@ insensitive).</p> <dt><b>commit</b></dt> <dd>The commit messages and authorship information will be scanned for the given pattern.</dd> EOT - my ($have_grep) = gitweb_check_feature('grep'); + my $have_grep = gitweb_check_feature('grep'); if ($have_grep) { print <<EOT; <dt><b>grep</b></dt>
@@ -5873,7 +5873,7 @@ EOT <dt><b>committer</b></dt> <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd> EOT - my ($have_pickaxe) = gitweb_check_feature('pickaxe'); + my $have_pickaxe = gitweb_check_feature('pickaxe'); if ($have_pickaxe) { print <<EOT; <dt><b>pickaxe</b></dt>
@@ -5925,7 +5925,7 @@ sub git_shortlog { sub git_feed { my $format = shift || 'atom'; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); # Atom: http://www.atomenabled.org/developers/syndication/ # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Signature · 1 line
1.5.6.5
PATCH v2 2/2 gitweb: clean up gitweb_check_feature() calls
Since git_check_feature now returns a scalar, the parentheses around the variables initialized from gitweb_check_feature() are not needed.
We remove them for stylistic consistency and to prevent unnecessary ambiguity in the code.
---
Fixed the commit message, use this instead of the previous one.
gitweb/gitweb.perl | 26 +++++++++++++------------- 1 files changed, 13 insertions(+), 13 deletions(-)
gitweb/gitweb.perl +13 -13
@@ -828,7 +828,7 @@ sub href (%) { } } - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { # try to put as many parameters as possible in PATH_INFO: # - project name
@@ -2119,7 +2119,7 @@ sub git_get_projects_list { $filter ||= ''; $filter =~ s/\.git$//; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if (-d $projects_list) { # search in directory
@@ -2965,7 +2965,7 @@ EOF } print "</div>\n"; - my ($have_search) = gitweb_check_feature('search'); + my $have_search = gitweb_check_feature('search'); if (defined $project && $have_search) { if (!defined $searchtext) { $searchtext = "";
@@ -2979,7 +2979,7 @@ EOF $search_hash = "HEAD"; } my $action = $my_uri; - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { $action .= "/".esc_url($project); }
@@ -3472,7 +3472,7 @@ sub is_patch_split { sub git_difftree_body { my ($difftree, $hash, @parents) = @_; my ($parent) = $parents[0]; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); print "<div class=\"list_head\">\n"; if ($#{$difftree} > 10) { print(($#{$difftree} + 1) . " files changed:\n");
@@ -3986,7 +3986,7 @@ sub git_project_list_body { # actually uses global variable $project my ($projlist, $order, $from, $to, $extra, $no_header) = @_; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); my @projects = fill_project_list_info($projlist, $check_forks); $order ||= $default_projects_order;
@@ -4446,7 +4446,7 @@ sub git_summary { my @taglist = git_get_tags_list(16); my @headlist = git_get_heads_list(16); my @forklist; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if ($check_forks) { @forklist = git_get_projects_list($project);
@@ -4475,7 +4475,7 @@ sub git_summary { } # Tag cloud - my $show_ctags = (gitweb_check_feature('ctags'))[0]; + my $show_ctags = gitweb_check_feature('ctags'); if ($show_ctags) { my $ctags = git_get_project_ctags($project); my $cloud = git_populate_project_tagcloud($ctags);
@@ -4765,7 +4765,7 @@ sub git_blob { $expires = "+1d"; } - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash or die_error(500, "Couldn't cat $file_name, $hash"); my $mimetype = blob_mimetype($fd, $file_name);
@@ -4858,7 +4858,7 @@ sub git_tree { my $ref = format_ref_marker($refs, $hash_base); git_header_html(); my $basedir = ''; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); if (defined $hash_base && (my %co = parse_commit($hash_base))) { my @views_nav = (); if (defined $file_name) {
@@ -5856,7 +5856,7 @@ insensitive).</p> <dt><b>commit</b></dt> <dd>The commit messages and authorship information will be scanned for the given pattern.</dd> EOT - my ($have_grep) = gitweb_check_feature('grep'); + my $have_grep = gitweb_check_feature('grep'); if ($have_grep) { print <<EOT; <dt><b>grep</b></dt>
@@ -5873,7 +5873,7 @@ EOT <dt><b>committer</b></dt> <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd> EOT - my ($have_pickaxe) = gitweb_check_feature('pickaxe'); + my $have_pickaxe = gitweb_check_feature('pickaxe'); if ($have_pickaxe) { print <<EOT; <dt><b>pickaxe</b></dt>
@@ -5925,7 +5925,7 @@ sub git_shortlog { sub git_feed { my $format = shift || 'atom'; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); # Atom: http://www.atomenabled.org/developers/syndication/ # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Signature · 1 line
1.5.6.5
Re: [PATCHv2 2/2] gitweb: clean up gitweb_check_feature() calls
Quoted 1 line · Giuseppe Bilotta
Since
git_check_featurenow returns a scalar, the parentheses around the
^^^
\---- missed one.Quoted 9 lines
variables initialized from
gitweb_check_feature()are not needed.We remove them for stylistic consistency and to prevent unnecessary ambiguity in the code.
Signed-off-by Giuseppe Bilotta <[email protected]>---
Fixed the commit message, use this instead of the previous one.
Signature · 2 lines
Jakub Narebski Poland
PATCH v2 2/2 gitweb: clean up gitweb_check_feature() calls
In reply to Giuseppe BilottaSince gitweb_check_feature now returns a scalar, the parentheses around the variables initialized from gitweb_check_feature() are not needed.
We remove them for stylistic consistency and to prevent unnecessary ambiguity in the code.
---
Third attempt, hopefully I got the commit message right, finally 8-P
gitweb/gitweb.perl | 26 +++++++++++++------------- 1 files changed, 13 insertions(+), 13 deletions(-)
gitweb/gitweb.perl +13 -13
@@ -828,7 +828,7 @@ sub href (%) { } } - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { # try to put as many parameters as possible in PATH_INFO: # - project name
@@ -2119,7 +2119,7 @@ sub git_get_projects_list { $filter ||= ''; $filter =~ s/\.git$//; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if (-d $projects_list) { # search in directory
@@ -2965,7 +2965,7 @@ EOF } print "</div>\n"; - my ($have_search) = gitweb_check_feature('search'); + my $have_search = gitweb_check_feature('search'); if (defined $project && $have_search) { if (!defined $searchtext) { $searchtext = "";
@@ -2979,7 +2979,7 @@ EOF $search_hash = "HEAD"; } my $action = $my_uri; - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { $action .= "/".esc_url($project); }
@@ -3472,7 +3472,7 @@ sub is_patch_split { sub git_difftree_body { my ($difftree, $hash, @parents) = @_; my ($parent) = $parents[0]; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); print "<div class=\"list_head\">\n"; if ($#{$difftree} > 10) { print(($#{$difftree} + 1) . " files changed:\n");
@@ -3986,7 +3986,7 @@ sub git_project_list_body { # actually uses global variable $project my ($projlist, $order, $from, $to, $extra, $no_header) = @_; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); my @projects = fill_project_list_info($projlist, $check_forks); $order ||= $default_projects_order;
@@ -4446,7 +4446,7 @@ sub git_summary { my @taglist = git_get_tags_list(16); my @headlist = git_get_heads_list(16); my @forklist; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if ($check_forks) { @forklist = git_get_projects_list($project);
@@ -4475,7 +4475,7 @@ sub git_summary { } # Tag cloud - my $show_ctags = (gitweb_check_feature('ctags'))[0]; + my $show_ctags = gitweb_check_feature('ctags'); if ($show_ctags) { my $ctags = git_get_project_ctags($project); my $cloud = git_populate_project_tagcloud($ctags);
@@ -4765,7 +4765,7 @@ sub git_blob { $expires = "+1d"; } - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash or die_error(500, "Couldn't cat $file_name, $hash"); my $mimetype = blob_mimetype($fd, $file_name);
@@ -4858,7 +4858,7 @@ sub git_tree { my $ref = format_ref_marker($refs, $hash_base); git_header_html(); my $basedir = ''; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); if (defined $hash_base && (my %co = parse_commit($hash_base))) { my @views_nav = (); if (defined $file_name) {
@@ -5856,7 +5856,7 @@ insensitive).</p> <dt><b>commit</b></dt> <dd>The commit messages and authorship information will be scanned for the given pattern.</dd> EOT - my ($have_grep) = gitweb_check_feature('grep'); + my $have_grep = gitweb_check_feature('grep'); if ($have_grep) { print <<EOT; <dt><b>grep</b></dt>
@@ -5873,7 +5873,7 @@ EOT <dt><b>committer</b></dt> <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd> EOT - my ($have_pickaxe) = gitweb_check_feature('pickaxe'); + my $have_pickaxe = gitweb_check_feature('pickaxe'); if ($have_pickaxe) { print <<EOT; <dt><b>pickaxe</b></dt>
@@ -5925,7 +5925,7 @@ sub git_shortlog { sub git_feed { my $format = shift || 'atom'; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); # Atom: http://www.atomenabled.org/developers/syndication/ # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Signature · 1 line
1.5.6.5
Re: [PATCHv2ter 2/2] gitweb: clean up gitweb_check_feature() calls
Quoted 2 lines · Giuseppe Bilotta
Since
gitweb_check_featurenow returns a scalar, the parentheses around the variables initialized fromgitweb_check_feature()are not needed.
Here is what I think the commit message of your [1/2] should have read:
Subject: [PATCH] gitweb: make gitweb_check_feature a boolean wrapper
The gitweb_check_feature() function retrieves the configuration parameters for the feature (such as the list of snapshot formats or the list of additional actions), but it is very often used to see if feature is enabled (which is returned as the first element in the list).
Because accepting the returned list in a scalar context by mistake yields the number of elements in the array, which is non-zero in all cases, such a mistake would result in a bug for the latter use, with disabled features appearing enabled. All existing callers that call the function for this purpose assign the return value in the list context to retrieve the first element, but this is a bug waiting to happen for future callers that are not careful.
This changes gitweb_check_feature() to a wrapper that can be called safely in the scalar context to see if a feature is enabled to reduce the risk of future bugs. A new function gitweb_get_feature() is introduced for the callers that want the list of configuration parameters. All existing callers of gitweb_check_feature() that want the configuration parameters are adjusted to call gitweb_get_feature().
Notice the difference in the tone from your [1/2]. The change is not about fixing (your proposed commit log message read "gitweb:fixes to gitweb feature check code") as nothing is broken. It is purely about futureproofing and I think we should mark it as such.
The splitting of two patches is not strictly necessary, but if you wanted to, I would do so slightly differently:
* Make [1/2] purely the rename of s/check/get/, saying "the function is not just checking if it is enabled, but it is getting the configuration parameter list. I'll introduce a new API to ask if it is enabled, which will be called 'check', hence this rename".
Obviously, after applying this patch, there should be no caller of "check".
* Make [2/2] introduce the "check" wrapper, and change the existing callers of "get" that want boolean to call "check" (and in the scalar context, as it does not matter anymore). You can steal most of the above rewrite as your commit message for this (except for the last two sentences).
Also I would rewrite the mysterious "Comfort code" comment like this:
# A wrapper to check if a given feature is enabled.
# With this, you can say
#
# my $bool_feat = gitweb_check_feature('bool_feat');
# gitweb_check_feature('bool_feat') or somecode;
#
# instead of
#
# my ($bool_feat) = gitweb_get_feature('bool_feat');
# (gitweb_get_feature('bool_feat'))[0] or somecode;(you also had a few typos in these four example calls).
Re: [PATCHv2ter 2/2] gitweb: clean up gitweb_check_feature() calls
Quoted 4 lines · Junio C Hamano
... The change is not
about fixing (your proposed commit log message read "gitweb:fixes to gitweb feature check code") as nothing is broken. It is purely about futureproofing and I think we should mark it as such.
Actually, there is a handful clueless/careless callers. Before applying any of the check => get patch, let's do this as a fix.
-- >8 --
Subject: [PATCH] gitweb: fix 'ctags' feature check and others
gitweb_check_feature() function is to retrieve the configuration parameter list and calling it in a scalar context does not give its first element that tells if the feature is enabled. This fixes all the existing callers to call the function correctly in list context.
--- gitweb/gitweb.perl | 14 +++++++------- 1 files changed, 7 insertions(+), 7 deletions(-)
gitweb/gitweb.perl +7 -7
@@ -3914,7 +3914,7 @@ sub fill_project_list_info { my ($projlist, $check_forks) = @_; my @projects; - my $show_ctags = gitweb_check_feature('ctags'); + my ($show_ctags) = gitweb_check_feature('ctags'); PROJECT: foreach my $pr (@$projlist) { my (@activity) = git_get_last_activity($pr->{'path'});
@@ -3988,7 +3988,7 @@ sub git_project_list_body { @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects; } - my $show_ctags = gitweb_check_feature('ctags'); + my ($show_ctags) = gitweb_check_feature('ctags'); if ($show_ctags) { my %ctags; foreach my $p (@projects) {
@@ -4457,7 +4457,7 @@ sub git_summary { } # Tag cloud - my $show_ctags = (gitweb_check_feature('ctags'))[0]; + my ($show_ctags) = gitweb_check_feature('ctags'); if ($show_ctags) { my $ctags = git_get_project_ctags($project); my $cloud = git_populate_project_tagcloud($ctags);
@@ -4559,7 +4559,7 @@ sub git_blame { my $fd; my $ftype; - gitweb_check_feature('blame') + gitweb_check_feature('blame')[0] or die_error(403, "Blame view not allowed"); die_error(400, "No file name given") unless $file_name;
@@ -5610,7 +5610,7 @@ sub git_history { } sub git_search { - gitweb_check_feature('search') or die_error(403, "Search is disabled"); + gitweb_check_feature('search')[0] or die_error(403, "Search is disabled"); if (!defined $searchtext) { die_error(400, "Text field is empty"); }
@@ -5629,11 +5629,11 @@ sub git_search { if ($searchtype eq 'pickaxe') { # pickaxe may take all resources of your box and run for several minutes # with every query - so decide by yourself how public you make this feature - gitweb_check_feature('pickaxe') + gitweb_check_feature('pickaxe')[0] or die_error(403, "Pickaxe is disabled"); } if ($searchtype eq 'grep') { - gitweb_check_feature('grep') + gitweb_check_feature('grep')[0] or die_error(403, "Grep is disabled"); }
Signature · 1 line
1.6.0.4.850.g6bd829
PATCH 2/3 gitweb: rename gitweb_check_feature to gitweb_get_feature
The function is about retrieving the configuration parameter list for the feature. A more robust way to check if a feature is enabled is introduced in the next patch, and the function will be called gitweb_check_feature.
---
* The earlier "fix ctags and friends" is the 1/3; this shows how I would have liked to see your patches as a reviewable series. The end result is almost identical.
gitweb/gitweb.perl | 46 +++++++++++++++++++++++----------------------- 1 files changed, 23 insertions(+), 23 deletions(-)
gitweb/gitweb.perl +23 -23
@@ -190,7 +190,7 @@ our %feature = ( # if there is no 'sub' key (no feature-sub), then feature cannot be # overriden # - # use gitweb_check_feature(<feature>) to check if <feature> is enabled + # use gitweb_get_feature(<feature>) to check if <feature> is enabled # Enable the 'blame' blob view, showing the last commit that modified # each line in the file. This can be very CPU-intensive.
@@ -329,7 +329,7 @@ our %feature = ( 'default' => [0]}, ); -sub gitweb_check_feature { +sub gitweb_get_feature { my ($name) = @_; return unless exists $feature{$name}; my ($sub, $override, @defaults) = (
@@ -767,7 +767,7 @@ our $git_dir; $git_dir = "$projectroot/$project" if $project; # list of supported snapshot formats -our @snapshot_fmts = gitweb_check_feature('snapshot'); +our @snapshot_fmts = gitweb_get_feature('snapshot'); @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts); # dispatch
@@ -810,7 +810,7 @@ sub href (%) { } } - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my ($use_pathinfo) = gitweb_get_feature('pathinfo'); if ($use_pathinfo) { # try to put as many parameters as possible in PATH_INFO: # - project name
@@ -2101,7 +2101,7 @@ sub git_get_projects_list { $filter ||= ''; $filter =~ s/\.git$//; - my ($check_forks) = gitweb_check_feature('forks'); + my ($check_forks) = gitweb_get_feature('forks'); if (-d $projects_list) { # search in directory
@@ -2947,7 +2947,7 @@ EOF } print "</div>\n"; - my ($have_search) = gitweb_check_feature('search'); + my ($have_search) = gitweb_get_feature('search'); if (defined $project && $have_search) { if (!defined $searchtext) { $searchtext = "";
@@ -2961,7 +2961,7 @@ EOF $search_hash = "HEAD"; } my $action = $my_uri; - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my ($use_pathinfo) = gitweb_get_feature('pathinfo'); if ($use_pathinfo) { $action .= "/".esc_url($project); }
@@ -3084,7 +3084,7 @@ sub git_print_page_nav { $arg{'tree'}{'hash'} = $treehead if defined $treehead; $arg{'tree'}{'hash_base'} = $treebase if defined $treebase; - my @actions = gitweb_check_feature('actions'); + my @actions = gitweb_get_feature('actions'); my %repl = ( '%' => '%', 'n' => $project, # project name
@@ -3454,7 +3454,7 @@ sub is_patch_split { sub git_difftree_body { my ($difftree, $hash, @parents) = @_; my ($parent) = $parents[0]; - my ($have_blame) = gitweb_check_feature('blame'); + my ($have_blame) = gitweb_get_feature('blame'); print "<div class=\"list_head\">\n"; if ($#{$difftree} > 10) { print(($#{$difftree} + 1) . " files changed:\n");
@@ -3914,7 +3914,7 @@ sub fill_project_list_info { my ($projlist, $check_forks) = @_; my @projects; - my ($show_ctags) = gitweb_check_feature('ctags'); + my ($show_ctags) = gitweb_get_feature('ctags'); PROJECT: foreach my $pr (@$projlist) { my (@activity) = git_get_last_activity($pr->{'path'});
@@ -3968,7 +3968,7 @@ sub git_project_list_body { # actually uses global variable $project my ($projlist, $order, $from, $to, $extra, $no_header) = @_; - my ($check_forks) = gitweb_check_feature('forks'); + my ($check_forks) = gitweb_get_feature('forks'); my @projects = fill_project_list_info($projlist, $check_forks); $order ||= $default_projects_order;
@@ -3988,7 +3988,7 @@ sub git_project_list_body { @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects; } - my ($show_ctags) = gitweb_check_feature('ctags'); + my ($show_ctags) = gitweb_get_feature('ctags'); if ($show_ctags) { my %ctags; foreach my $p (@projects) {
@@ -4428,7 +4428,7 @@ sub git_summary { my @taglist = git_get_tags_list(16); my @headlist = git_get_heads_list(16); my @forklist; - my ($check_forks) = gitweb_check_feature('forks'); + my ($check_forks) = gitweb_get_feature('forks'); if ($check_forks) { @forklist = git_get_projects_list($project);
@@ -4457,7 +4457,7 @@ sub git_summary { } # Tag cloud - my ($show_ctags) = gitweb_check_feature('ctags'); + my ($show_ctags) = gitweb_get_feature('ctags'); if ($show_ctags) { my $ctags = git_get_project_ctags($project); my $cloud = git_populate_project_tagcloud($ctags);
@@ -4559,7 +4559,7 @@ sub git_blame { my $fd; my $ftype; - gitweb_check_feature('blame')[0] + gitweb_get_feature('blame')[0] or die_error(403, "Blame view not allowed"); die_error(400, "No file name given") unless $file_name;
@@ -4747,7 +4747,7 @@ sub git_blob { $expires = "+1d"; } - my ($have_blame) = gitweb_check_feature('blame'); + my ($have_blame) = gitweb_get_feature('blame'); open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash or die_error(500, "Couldn't cat $file_name, $hash"); my $mimetype = blob_mimetype($fd, $file_name);
@@ -4840,7 +4840,7 @@ sub git_tree { my $ref = format_ref_marker($refs, $hash_base); git_header_html(); my $basedir = ''; - my ($have_blame) = gitweb_check_feature('blame'); + my ($have_blame) = gitweb_get_feature('blame'); if (defined $hash_base && (my %co = parse_commit($hash_base))) { my @views_nav = (); if (defined $file_name) {
@@ -5610,7 +5610,7 @@ sub git_history { } sub git_search { - gitweb_check_feature('search')[0] or die_error(403, "Search is disabled"); + gitweb_get_feature('search')[0] or die_error(403, "Search is disabled"); if (!defined $searchtext) { die_error(400, "Text field is empty"); }
@@ -5629,11 +5629,11 @@ sub git_search { if ($searchtype eq 'pickaxe') { # pickaxe may take all resources of your box and run for several minutes # with every query - so decide by yourself how public you make this feature - gitweb_check_feature('pickaxe')[0] + gitweb_get_feature('pickaxe')[0] or die_error(403, "Pickaxe is disabled"); } if ($searchtype eq 'grep') { - gitweb_check_feature('grep')[0] + gitweb_get_feature('grep')[0] or die_error(403, "Grep is disabled"); }
@@ -5838,7 +5838,7 @@ insensitive).</p> <dt><b>commit</b></dt> <dd>The commit messages and authorship information will be scanned for the given pattern.</dd> EOT - my ($have_grep) = gitweb_check_feature('grep'); + my ($have_grep) = gitweb_get_feature('grep'); if ($have_grep) { print <<EOT; <dt><b>grep</b></dt>
@@ -5855,7 +5855,7 @@ EOT <dt><b>committer</b></dt> <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd> EOT - my ($have_pickaxe) = gitweb_check_feature('pickaxe'); + my ($have_pickaxe) = gitweb_get_feature('pickaxe'); if ($have_pickaxe) { print <<EOT; <dt><b>pickaxe</b></dt>
@@ -5907,7 +5907,7 @@ sub git_shortlog { sub git_feed { my $format = shift || 'atom'; - my ($have_blame) = gitweb_check_feature('blame'); + my ($have_blame) = gitweb_get_feature('blame'); # Atom: http://www.atomenabled.org/developers/syndication/ # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Signature · 1 line
1.6.0.4.850.g6bd829
PATCH 3/3 gitweb: make gitweb_check_feature a boolean wrapper
In reply to Junio C HamanoThe gitweb_get_feature() function retrieves the configuration parameters for the feature (such as the list of snapshot formats or the list of additional actions), but it is very often used to see if feature is enabled (which is returned as the first element in the list).
Because accepting the returned list in a scalar context by mistake yields the number of elements in the array, which is non-zero in all cases, such a mistake would result in a bug for the latter use, with disabled features appearing enabled. All existing callers that call the function for this purpose assign the return value in the list context to retrieve the first element, but that is only because we fixed careless callers recently.
This add gitweb_check_feature() as a wrapper to gitweb_get_feature() that can be called safely in the scalar context to see if a feature is enabled to reduce the risk of future bugs.
---
* Again, this is to demonstrate how I would have liked to see your patches as a reviewable series. Notice how this naturally justifies the dropping of parentheses in many lines that begin with "my", and makes it easier to review? You can review the patch easily by knowing that callers that have s/get/check/ are now safe to use scalar context.
gitweb/gitweb.perl | 56 +++++++++++++++++++++++++++++++++------------------ 1 files changed, 36 insertions(+), 20 deletions(-)
gitweb/gitweb.perl +36 -20
@@ -190,7 +190,7 @@ our %feature = ( # if there is no 'sub' key (no feature-sub), then feature cannot be # overriden # - # use gitweb_get_feature(<feature>) to check if <feature> is enabled + # use gitweb_check_feature(<feature>) to check if <feature> is enabled # Enable the 'blame' blob view, showing the last commit that modified # each line in the file. This can be very CPU-intensive.
@@ -344,6 +344,22 @@ sub gitweb_get_feature { return $sub->(@defaults); } +# A wrapper to check if a given feature is enabled. +# With this, you can say +# +# my $bool_feat = gitweb_check_feature('bool_feat'); +# gitweb_check_feature('bool_feat') or somecode; +# +# instead of +# +# my ($bool_feat) = gitweb_get_feature('bool_feat'); +# (gitweb_get_feature('bool_feat'))[0] or somecode; +# +sub gitweb_check_feature { + return (gitweb_get_feature(@_))[0]; +} + + sub feature_blame { my ($val) = git_get_project_config('blame', '--bool');
@@ -810,7 +826,7 @@ sub href (%) { } } - my ($use_pathinfo) = gitweb_get_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { # try to put as many parameters as possible in PATH_INFO: # - project name
@@ -2101,7 +2117,7 @@ sub git_get_projects_list { $filter ||= ''; $filter =~ s/\.git$//; - my ($check_forks) = gitweb_get_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if (-d $projects_list) { # search in directory
@@ -2947,7 +2963,7 @@ EOF } print "</div>\n"; - my ($have_search) = gitweb_get_feature('search'); + my $have_search = gitweb_check_feature('search'); if (defined $project && $have_search) { if (!defined $searchtext) { $searchtext = "";
@@ -2961,7 +2977,7 @@ EOF $search_hash = "HEAD"; } my $action = $my_uri; - my ($use_pathinfo) = gitweb_get_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { $action .= "/".esc_url($project); }
@@ -3454,7 +3470,7 @@ sub is_patch_split { sub git_difftree_body { my ($difftree, $hash, @parents) = @_; my ($parent) = $parents[0]; - my ($have_blame) = gitweb_get_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); print "<div class=\"list_head\">\n"; if ($#{$difftree} > 10) { print(($#{$difftree} + 1) . " files changed:\n");
@@ -3914,7 +3930,7 @@ sub fill_project_list_info { my ($projlist, $check_forks) = @_; my @projects; - my ($show_ctags) = gitweb_get_feature('ctags'); + my $show_ctags = gitweb_check_feature('ctags'); PROJECT: foreach my $pr (@$projlist) { my (@activity) = git_get_last_activity($pr->{'path'});
@@ -3968,7 +3984,7 @@ sub git_project_list_body { # actually uses global variable $project my ($projlist, $order, $from, $to, $extra, $no_header) = @_; - my ($check_forks) = gitweb_get_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); my @projects = fill_project_list_info($projlist, $check_forks); $order ||= $default_projects_order;
@@ -3988,7 +4004,7 @@ sub git_project_list_body { @projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects; } - my ($show_ctags) = gitweb_get_feature('ctags'); + my $show_ctags = gitweb_check_feature('ctags'); if ($show_ctags) { my %ctags; foreach my $p (@projects) {
@@ -4428,7 +4444,7 @@ sub git_summary { my @taglist = git_get_tags_list(16); my @headlist = git_get_heads_list(16); my @forklist; - my ($check_forks) = gitweb_get_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if ($check_forks) { @forklist = git_get_projects_list($project);
@@ -4457,7 +4473,7 @@ sub git_summary { } # Tag cloud - my ($show_ctags) = gitweb_get_feature('ctags'); + my $show_ctags = gitweb_check_feature('ctags'); if ($show_ctags) { my $ctags = git_get_project_ctags($project); my $cloud = git_populate_project_tagcloud($ctags);
@@ -4559,7 +4575,7 @@ sub git_blame { my $fd; my $ftype; - gitweb_get_feature('blame')[0] + gitweb_check_feature('blame') or die_error(403, "Blame view not allowed"); die_error(400, "No file name given") unless $file_name;
@@ -4747,7 +4763,7 @@ sub git_blob { $expires = "+1d"; } - my ($have_blame) = gitweb_get_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash or die_error(500, "Couldn't cat $file_name, $hash"); my $mimetype = blob_mimetype($fd, $file_name);
@@ -4840,7 +4856,7 @@ sub git_tree { my $ref = format_ref_marker($refs, $hash_base); git_header_html(); my $basedir = ''; - my ($have_blame) = gitweb_get_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); if (defined $hash_base && (my %co = parse_commit($hash_base))) { my @views_nav = (); if (defined $file_name) {
@@ -5610,7 +5626,7 @@ sub git_history { } sub git_search { - gitweb_get_feature('search')[0] or die_error(403, "Search is disabled"); + gitweb_check_feature('search') or die_error(403, "Search is disabled"); if (!defined $searchtext) { die_error(400, "Text field is empty"); }
@@ -5629,11 +5645,11 @@ sub git_search { if ($searchtype eq 'pickaxe') { # pickaxe may take all resources of your box and run for several minutes # with every query - so decide by yourself how public you make this feature - gitweb_get_feature('pickaxe')[0] + gitweb_check_feature('pickaxe') or die_error(403, "Pickaxe is disabled"); } if ($searchtype eq 'grep') { - gitweb_get_feature('grep')[0] + gitweb_check_feature('grep') or die_error(403, "Grep is disabled"); }
@@ -5838,7 +5854,7 @@ insensitive).</p> <dt><b>commit</b></dt> <dd>The commit messages and authorship information will be scanned for the given pattern.</dd> EOT - my ($have_grep) = gitweb_get_feature('grep'); + my $have_grep = gitweb_check_feature('grep'); if ($have_grep) { print <<EOT; <dt><b>grep</b></dt>
@@ -5855,7 +5871,7 @@ EOT <dt><b>committer</b></dt> <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd> EOT - my ($have_pickaxe) = gitweb_get_feature('pickaxe'); + my $have_pickaxe = gitweb_check_feature('pickaxe'); if ($have_pickaxe) { print <<EOT; <dt><b>pickaxe</b></dt>
@@ -5907,7 +5923,7 @@ sub git_shortlog { sub git_feed { my $format = shift || 'atom'; - my ($have_blame) = gitweb_get_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); # Atom: http://www.atomenabled.org/developers/syndication/ # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Signature · 1 line
1.6.0.4.850.g6bd829
Re: [PATCH 3/3] gitweb: make gitweb_check_feature a boolean wrapper
Quoted 5 lines · Junio C Hamano
* Again, this is to demonstrate how I would have liked to see your patches as a reviewable series. Notice how this naturally justifies the dropping of parentheses in many lines that begin with "my", and makes it easier to review? You can review the patch easily by knowing that callers that have s/get/check/ are now safe to use scalar context.
Yes, I get the point: do less things under the hood. I also still think that three patches with code going ping-pong are way too much, though 8-P
For what it's worth, you get my Ack: to your patchset 8-)
Signature · 1 line
Giuseppe "Oblomov" Bilotta
Re: [PATCH 3/3] gitweb: make gitweb_check_feature a boolean wrapper
Quoted 7 lines · Giuseppe Bilotta
Quoted 5 lines · Junio C Hamano
* Again, this is to demonstrate how I would have liked to see your patches as a reviewable series. Notice how this naturally justifies the dropping of parentheses in many lines that begin with "my", and makes it easier to review? You can review the patch easily by knowing that callers that have s/get/check/ are now safe to use scalar context.
Quoted 5 lines
Yes, I get the point: do less things under the hood. I also still think that three patches with code going ping-pong are way too much, though 8-P
For what it's worth, you get my Ack: to your patchset 8-)
Well, I am not interested in taking credits for this series. I am very much interested in reducing my future workload by showing people how an easily reviewable series should look like.
So if you still think it is "way too much", I did not succeed what I tried to do X-<.
Re: [PATCH 3/3] gitweb: make gitweb_check_feature a boolean wrapper
Quoted 13 lines · Junio C Hamano
Quoted 11 lines · Giuseppe Bilotta
Quoted 5 lines · Junio C Hamano
* Again, this is to demonstrate how I would have liked to see your patches as a reviewable series. Notice how this naturally justifies the dropping of parentheses in many lines that begin with "my", and makes it easier to review? You can review the patch easily by knowing that callers that have s/get/check/ are now safe to use scalar context.
Yes, I get the point: do less things under the hood. I also still think that three patches with code going ping-pong are way too much, though 8-P
Quoted 8 lines
Quoted 1 line
For what it's worth, you get my Ack: to your patchset 8-)
Well, I am not interested in taking credits for this series. I am very much interested in reducing my future workload by showing people how an easily reviewable series should look like.
So if you still think it is "way too much", I did not succeed what I tried to do X-<.
At least as far as this patch goes, from my point of view, the 'clueless/careless' usages of gitweb_check_feature are the conceptually (although not code-wise) correct ones, so they shouldn't be the ones touched by the patch, considering that in addition to the futureproofing, (implicit) fixing those usages is one of the main points of the patch itself.
OTOH, I _do_ get the point about ease of review. But for example (and for future reference for myself): would you say that it would have been enough to have a cleaner commit message explicitly mentioning the fact that formerly incorrect usages of the gitweb_check_feature() were left intact because they were now correct?
Something like this http://git.oblomov.eu/git/patch/gitweb/check-feature-v3
Signature · 1 line
Giuseppe "Oblomov" Bilotta
Re: [PATCHv2ter 2/2] gitweb: clean up gitweb_check_feature() calls
In reply to Junio C HamanoQuoted 9 lines · Junio C Hamano
Quoted 4 lines · Junio C Hamano
... The change is not
about fixing (your proposed commit log message read "gitweb:fixes to gitweb feature check code") as nothing is broken. It is purely about futureproofing and I think we should mark it as such.Actually, there is a handful clueless/careless callers. Before applying any of the check => get patch, let's do this as a fix.
And this is precisely the reason why the first time I sent the patch I did the restyling in the same go: by not touching the clueless/careless callers and instead bringing gitweb_check_feature to act in scalar context, it would automatically fix those broken usages, and it made sense to convert all array-to-get-a-scalar assignmets as well.
Signature · 1 line
Giuseppe "Oblomov" Bilotta
Re: [PATCHv2ter 2/2] gitweb: clean up gitweb_check_feature() calls
Quoted 4 lines · Giuseppe Bilotta
And this is precisely the reason why the first time I sent the patch I did the restyling in the same go: by not touching the clueless/careless callers and instead bringing
gitweb_check_featureto act in scalar context, it would automatically fix those broken usages,
... which is very bad for reviewability purposes.
By explicitly fixing them before doing the "this will sweep all the potential bugs under the rug", you can demonstrate a lot more clearly why these changes are necessary.
Re: [PATCHv2ter 2/2] gitweb: clean up gitweb_check_feature() calls
Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Giuseppe Bilotta
And this is precisely the reason why the first time I sent the patch I did the restyling in the same go: by not touching the clueless/careless callers and instead bringing
gitweb_check_featureto act in scalar context, it would automatically fix those broken usages,
Quoted 5 lines
... which is very bad for reviewability purposes.
By explicitly fixing them before doing the "this will sweep all the potential bugs under the rug", you can demonstrate a lot more clearly why these changes are necessary.
Well, I think now that it would be best to split this series into _two_ patches: first Junio's patch fixing (!) gitweb_check_feature() calls, second original v1 Guiseppe's renaming gitweb_check_feature() to gitweb_get_feature(), and adding gitweb_check_feature(), and using gitweb_get_feature() only where needed; optionally fixing "style".
Pure rename IMVHO doesn't look that nice...
Signature · 2 lines
Jakub Narebski Poland
Re: [PATCHv2ter 2/2] gitweb: clean up gitweb_check_feature() calls
Quoted 6 lines · Jakub Narebski
Well, I think now that it would be best to split this series into _two_ patches: first Junio's patch fixing (!)
gitweb_check_feature()calls, second original v1 Guiseppe's renaminggitweb_check_feature()togitweb_get_feature(), and addinggitweb_check_feature(), and usinggitweb_get_feature()only where needed; optionally fixing "style".
It means: first fixup patch, then futureproof patch.
Signature · 2 lines
Jakub Narebski Poland
Re: [PATCHv2 1/2] gitweb: fixes to gitweb feature check code
In reply to Giuseppe BilottaQuoted 9 lines · Giuseppe Bilotta
The
gitweb_check_featureroutine was being used for two different
purposes: retrieving the actual feature value (such as the list of snapshot formats or the list of additional actions), and to check if a feature was enabled.For the latter use, since all features return an array, it led to either clumsy code or subtle bugs, with disabled features appearing enabled because (0) evaluates to 1.
Quoted 7 lines
We fix these bugs, and simplify the code, by separating feature (list) value retrieval (
gitweb_get_feature) from boolean feature check (i.e. retrieving the first/only item in the feature value list). Usage ofgitweb_check_featureis replaced bygitweb_get_featurewhere appropriate.Signed-off-by Giuseppe Bilotta <[email protected]>
I like the idea of futureproofing gitweb code to avoid further bugs with checking if feature is enabled. So from me this patch gets Ack.
I have thought at first that perhaps this patch could also remove requirement that 'default' field is array (reference), replacing a bit cumbersome
$feature{'blame'}{'default'} = [1];
with simpler
$feature{'blame'}{'default'} = 1;
But now I think that is separate issue, and if we think it is worth having (in spite of its disadvantages in form of more complicated code dealing among others with legacy configuration), it should be put in separate patch.
Signature · 2 lines
Jakub Narebski Poland
Quoted 7 lines · Giuseppe Bilotta
This is v2 of the gitweb feature check fix patch, which has now been split into into a code patch and cleanup patch.
The first patch introduces
git_get_feature()to clearly distinguish the feature retrieval from the boolean feature check (which is kept atgit_check_feature()). The new function is used where appropriate.
Quoted 7 lines
The second patch cleans up use of
git_check_feature(): since the function now returns a boolean instead of an array, the often-used construct
my ($somevar) =git_check_feature('somefeat');
although still valid, becomes a rather clumsy stylistic choice, as it introduces an unnecessary ambiguity. Make it clear that we're now dealing with scalars by using scalar assignment.
It is gitweb_get_feature() (for retrieval) and gitweb_check_feature() (for checking), not git_get_feature() and git_check_feature().
I really like this series; making coding easier by removing need to use special construct, and making it easier to avoid mistakes is always a good thing.
Quoted 6 lines
Giuseppe Bilotta (2):
gitweb: fixes to gitweb feature check code
gitweb: clean upgit_check_feature()callsgitweb/gitweb.perl | 52 +++++++++++++++++++++++++++++++++++----------------- 1 files changed, 35 insertions(+), 17 deletions(-)
I also like the split, as it separates improvement (much easier to use and less error prone gitweb_check_feature()) from style cleanup.
Signature · 2 lines
Jakub Narebski Poland
2008/11/29 Jakub Narebski <[email protected]>:
Quoted 8 lines
Quoted 3 lines · Giuseppe Bilotta
The first patch introduces
git_get_feature()to clearly distinguish the feature retrieval from the boolean feature check (which is kept atgit_check_feature()). The new function is used where appropriate.It is
gitweb_get_feature()(for retrieval) andgitweb_check_feature()(for checking), notgit_get_feature()andgit_check_feature().
Ahem. Right. I'll resend the second patch with a fixed subject and commit message 8-P
Signature · 1 line
Giuseppe "Oblomov" Bilotta
A message that is not in the archive (sent off-list or not yet received): Message-ID: <[email protected]
PATCH 1/1 gitweb: fixes to gitweb feature check code
The gitweb_check_feature routine was being used for two different
purposes: retrieving the actual feature value (such as the list of snapshot formats or the list of additional actions), and checking if a feature was enabled.
This led to subtle bugs in feature checking code: gitweb_check_feature would return (0) for disabled features, so its use in scalar context would return true instead of false.
To fix this issue and future-proof the code, we split feature value retrieval into its own gitweb_get_feature()function , and ensure that the boolean feature check function gitweb_check_feature() always returns a scalar (precisely, the first/only item in the feature value list).
Usage of gitweb_check_feature() across gitweb is replaced with gitweb_get_feature() where appropriate, and list evaluation of gitweb_check_feature() is demoted to scalar evaluation to prevent ambiguity. The few previously incorrect uses of gitweb_check_feature() in scalar context are left untouched because they are now correct.
--- gitweb/gitweb.perl | 52 +++++++++++++++++++++++++++++++++++----------------- 1 files changed, 35 insertions(+), 17 deletions(-)
gitweb/gitweb.perl +35 -17
@@ -190,7 +190,9 @@ our %feature = ( # if there is no 'sub' key (no feature-sub), then feature cannot be # overriden # - # use gitweb_check_feature(<feature>) to check if <feature> is enabled + # use gitweb_get_feature(<feature>) to retrieve the <feature> value + # (an array) or gitweb_check_feature(<feature>) to check if <feature> + # is enabled # Enable the 'blame' blob view, showing the last commit that modified # each line in the file. This can be very CPU-intensive.
@@ -329,7 +331,8 @@ our %feature = ( 'default' => [0]}, ); -sub gitweb_check_feature { +# retrieve the value of a given feature, as an array +sub gitweb_get_feature { my ($name) = @_; return unless exists $feature{$name}; my ($sub, $override, @defaults) = (
@@ -344,6 +347,21 @@ sub gitweb_check_feature { return $sub->(@defaults); } +# check if a given feature is enabled or not, returning the first (and only) +# value of the feature. This allows us to write +# my $bool_feat = gitweb_check_feature('bool_feat'); +# or +# gitweb_check_feature('bool_feat') or somecode; +# instead of +# my ($bool_feat) = gitweb_get_feature('bool_feat'); +# or +# (gitweb_get_feature('bool_feat'))[0] or somecode; +# respectively +sub gitweb_check_feature { + return (gitweb_get_feature(@_))[0]; +} + + sub feature_blame { my ($val) = git_get_project_config('blame', '--bool');
@@ -767,7 +785,7 @@ our $git_dir; $git_dir = "$projectroot/$project" if $project; # list of supported snapshot formats -our @snapshot_fmts = gitweb_check_feature('snapshot'); +our @snapshot_fmts = gitweb_get_feature('snapshot'); @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts); # dispatch
@@ -810,7 +828,7 @@ sub href (%) { } } - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { # try to put as many parameters as possible in PATH_INFO: # - project name
@@ -2101,7 +2119,7 @@ sub git_get_projects_list { $filter ||= ''; $filter =~ s/\.git$//; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if (-d $projects_list) { # search in directory
@@ -2947,7 +2965,7 @@ EOF } print "</div>\n"; - my ($have_search) = gitweb_check_feature('search'); + my $have_search = gitweb_check_feature('search'); if (defined $project && $have_search) { if (!defined $searchtext) { $searchtext = "";
@@ -2961,7 +2979,7 @@ EOF $search_hash = "HEAD"; } my $action = $my_uri; - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { $action .= "/".esc_url($project); }
@@ -3084,7 +3102,7 @@ sub git_print_page_nav { $arg{'tree'}{'hash'} = $treehead if defined $treehead; $arg{'tree'}{'hash_base'} = $treebase if defined $treebase; - my @actions = gitweb_check_feature('actions'); + my @actions = gitweb_get_feature('actions'); my %repl = ( '%' => '%', 'n' => $project, # project name
@@ -3454,7 +3472,7 @@ sub is_patch_split { sub git_difftree_body { my ($difftree, $hash, @parents) = @_; my ($parent) = $parents[0]; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); print "<div class=\"list_head\">\n"; if ($#{$difftree} > 10) { print(($#{$difftree} + 1) . " files changed:\n");
@@ -3968,7 +3986,7 @@ sub git_project_list_body { # actually uses global variable $project my ($projlist, $order, $from, $to, $extra, $no_header) = @_; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); my @projects = fill_project_list_info($projlist, $check_forks); $order ||= $default_projects_order;
@@ -4428,7 +4446,7 @@ sub git_summary { my @taglist = git_get_tags_list(16); my @headlist = git_get_heads_list(16); my @forklist; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if ($check_forks) { @forklist = git_get_projects_list($project);
@@ -4457,7 +4475,7 @@ sub git_summary { } # Tag cloud - my $show_ctags = (gitweb_check_feature('ctags'))[0]; + my $show_ctags = gitweb_check_feature('ctags'); if ($show_ctags) { my $ctags = git_get_project_ctags($project); my $cloud = git_populate_project_tagcloud($ctags);
@@ -4747,7 +4765,7 @@ sub git_blob { $expires = "+1d"; } - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash or die_error(500, "Couldn't cat $file_name, $hash"); my $mimetype = blob_mimetype($fd, $file_name);
@@ -4840,7 +4858,7 @@ sub git_tree { my $ref = format_ref_marker($refs, $hash_base); git_header_html(); my $basedir = ''; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); if (defined $hash_base && (my %co = parse_commit($hash_base))) { my @views_nav = (); if (defined $file_name) {
@@ -5838,7 +5856,7 @@ insensitive).</p> <dt><b>commit</b></dt> <dd>The commit messages and authorship information will be scanned for the given pattern.</dd> EOT - my ($have_grep) = gitweb_check_feature('grep'); + my $have_grep = gitweb_check_feature('grep'); if ($have_grep) { print <<EOT; <dt><b>grep</b></dt>
@@ -5855,7 +5873,7 @@ EOT <dt><b>committer</b></dt> <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd> EOT - my ($have_pickaxe) = gitweb_check_feature('pickaxe'); + my $have_pickaxe = gitweb_check_feature('pickaxe'); if ($have_pickaxe) { print <<EOT; <dt><b>pickaxe</b></dt>
@@ -5907,7 +5925,7 @@ sub git_shortlog { sub git_feed { my $format = shift || 'atom'; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); # Atom: http://www.atomenabled.org/developers/syndication/ # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Signature · 1 line
1.5.6.5
Re: [PATCH] gitweb: fixes to gitweb feature check code
Quoted 11 lines · Giuseppe Bilotta
The
gitweb_check_featureroutine was being used for two different
purposes: retrieving the actual feature value (such as the list of snapshot formats or the list of additional actions), and checking if a feature was enabled.This led to subtle bugs in feature checking code:
gitweb_check_featurewould return (0) for disabled features, so its use in scalar context would return true instead of false.To fix this issue and future-proof the code, we split feature value retrieval into its own gitweb_get_feature()function , and ensure that
retrieval into its own gitweb_get_feature() function, and ensure that
Quoted 10 lines
the boolean feature check function
gitweb_check_feature()always returns a scalar (precisely, the first/only item in the feature value list).Usage of
gitweb_check_feature()across gitweb is replaced withgitweb_get_feature()where appropriate, and list evaluation ofgitweb_check_feature()is demoted to scalar evaluation to prevent ambiguity. The few previously incorrect uses ofgitweb_check_feature()in scalar context are left untouched because they are now correct.Signed-off-by Giuseppe Bilotta <[email protected]>
What I like about having all this, i.e. fix, futureproof and style correction in one single patch is the fact that fix doesn't introduce strange looking (gitweb_check_feature('bool_feat'))[0]... well, except encapsulated in a subroutine.
Quoted 1 line
From all possible splits of this feature into series of up to three
patches I think I like the one with pure subroutine rename from *check* to *get* least...
Signature · 2 lines
Jakub Narebski Poland
[PATCHv3bis] gitweb: fixes to gitweb feature check code
The gitweb_check_feature routine was being used for two different
purposes: retrieving the actual feature value (such as the list of snapshot formats or the list of additional actions), and checking if a feature was enabled.
This led to subtle bugs in feature checking code: gitweb_check_feature would return (0) for disabled features, so its use in scalar context would return true instead of false.
To fix this issue and future-proof the code, we split feature value retrieval into its own gitweb_get_feature()function, and ensure that the boolean feature check function gitweb_check_feature() always returns a scalar (precisely, the first/only item in the feature value list).
Usage of gitweb_check_feature() across gitweb is replaced with gitweb_get_feature() where appropriate, and list evaluation of gitweb_check_feature() is demoted to scalar evaluation to prevent ambiguity. The few previously incorrect uses of gitweb_check_feature() in scalar context are left untouched because they are now correct.
---
Fixed typo in commit message.
gitweb/gitweb.perl | 52 +++++++++++++++++++++++++++++++++++----------------- 1 files changed, 35 insertions(+), 17 deletions(-)
gitweb/gitweb.perl +35 -17
@@ -190,7 +190,9 @@ our %feature = ( # if there is no 'sub' key (no feature-sub), then feature cannot be # overriden # - # use gitweb_check_feature(<feature>) to check if <feature> is enabled + # use gitweb_get_feature(<feature>) to retrieve the <feature> value + # (an array) or gitweb_check_feature(<feature>) to check if <feature> + # is enabled # Enable the 'blame' blob view, showing the last commit that modified # each line in the file. This can be very CPU-intensive.
@@ -329,7 +331,8 @@ our %feature = ( 'default' => [0]}, ); -sub gitweb_check_feature { +# retrieve the value of a given feature, as an array +sub gitweb_get_feature { my ($name) = @_; return unless exists $feature{$name}; my ($sub, $override, @defaults) = (
@@ -344,6 +347,21 @@ sub gitweb_check_feature { return $sub->(@defaults); } +# check if a given feature is enabled or not, returning the first (and only) +# value of the feature. This allows us to write +# my $bool_feat = gitweb_check_feature('bool_feat'); +# or +# gitweb_check_feature('bool_feat') or somecode; +# instead of +# my ($bool_feat) = gitweb_get_feature('bool_feat'); +# or +# (gitweb_get_feature('bool_feat'))[0] or somecode; +# respectively +sub gitweb_check_feature { + return (gitweb_get_feature(@_))[0]; +} + + sub feature_blame { my ($val) = git_get_project_config('blame', '--bool');
@@ -767,7 +785,7 @@ our $git_dir; $git_dir = "$projectroot/$project" if $project; # list of supported snapshot formats -our @snapshot_fmts = gitweb_check_feature('snapshot'); +our @snapshot_fmts = gitweb_get_feature('snapshot'); @snapshot_fmts = filter_snapshot_fmts(@snapshot_fmts); # dispatch
@@ -810,7 +828,7 @@ sub href (%) { } } - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { # try to put as many parameters as possible in PATH_INFO: # - project name
@@ -2101,7 +2119,7 @@ sub git_get_projects_list { $filter ||= ''; $filter =~ s/\.git$//; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if (-d $projects_list) { # search in directory
@@ -2947,7 +2965,7 @@ EOF } print "</div>\n"; - my ($have_search) = gitweb_check_feature('search'); + my $have_search = gitweb_check_feature('search'); if (defined $project && $have_search) { if (!defined $searchtext) { $searchtext = "";
@@ -2961,7 +2979,7 @@ EOF $search_hash = "HEAD"; } my $action = $my_uri; - my ($use_pathinfo) = gitweb_check_feature('pathinfo'); + my $use_pathinfo = gitweb_check_feature('pathinfo'); if ($use_pathinfo) { $action .= "/".esc_url($project); }
@@ -3084,7 +3102,7 @@ sub git_print_page_nav { $arg{'tree'}{'hash'} = $treehead if defined $treehead; $arg{'tree'}{'hash_base'} = $treebase if defined $treebase; - my @actions = gitweb_check_feature('actions'); + my @actions = gitweb_get_feature('actions'); my %repl = ( '%' => '%', 'n' => $project, # project name
@@ -3454,7 +3472,7 @@ sub is_patch_split { sub git_difftree_body { my ($difftree, $hash, @parents) = @_; my ($parent) = $parents[0]; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); print "<div class=\"list_head\">\n"; if ($#{$difftree} > 10) { print(($#{$difftree} + 1) . " files changed:\n");
@@ -3968,7 +3986,7 @@ sub git_project_list_body { # actually uses global variable $project my ($projlist, $order, $from, $to, $extra, $no_header) = @_; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); my @projects = fill_project_list_info($projlist, $check_forks); $order ||= $default_projects_order;
@@ -4428,7 +4446,7 @@ sub git_summary { my @taglist = git_get_tags_list(16); my @headlist = git_get_heads_list(16); my @forklist; - my ($check_forks) = gitweb_check_feature('forks'); + my $check_forks = gitweb_check_feature('forks'); if ($check_forks) { @forklist = git_get_projects_list($project);
@@ -4457,7 +4475,7 @@ sub git_summary { } # Tag cloud - my $show_ctags = (gitweb_check_feature('ctags'))[0]; + my $show_ctags = gitweb_check_feature('ctags'); if ($show_ctags) { my $ctags = git_get_project_ctags($project); my $cloud = git_populate_project_tagcloud($ctags);
@@ -4747,7 +4765,7 @@ sub git_blob { $expires = "+1d"; } - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash or die_error(500, "Couldn't cat $file_name, $hash"); my $mimetype = blob_mimetype($fd, $file_name);
@@ -4840,7 +4858,7 @@ sub git_tree { my $ref = format_ref_marker($refs, $hash_base); git_header_html(); my $basedir = ''; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); if (defined $hash_base && (my %co = parse_commit($hash_base))) { my @views_nav = (); if (defined $file_name) {
@@ -5838,7 +5856,7 @@ insensitive).</p> <dt><b>commit</b></dt> <dd>The commit messages and authorship information will be scanned for the given pattern.</dd> EOT - my ($have_grep) = gitweb_check_feature('grep'); + my $have_grep = gitweb_check_feature('grep'); if ($have_grep) { print <<EOT; <dt><b>grep</b></dt>
@@ -5855,7 +5873,7 @@ EOT <dt><b>committer</b></dt> <dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd> EOT - my ($have_pickaxe) = gitweb_check_feature('pickaxe'); + my $have_pickaxe = gitweb_check_feature('pickaxe'); if ($have_pickaxe) { print <<EOT; <dt><b>pickaxe</b></dt>
@@ -5907,7 +5925,7 @@ sub git_shortlog { sub git_feed { my $format = shift || 'atom'; - my ($have_blame) = gitweb_check_feature('blame'); + my $have_blame = gitweb_check_feature('blame'); # Atom: http://www.atomenabled.org/developers/syndication/ # RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ
Signature · 1 line
1.5.6.5
Re: [PATCHv3bis] gitweb: fixes to gitweb feature check code
Thanks, but the ones based on the previous round were already queued in 'next', so I took the change to the check/get comment (the first hunk in this squashed patch) not to lose the remaining improvements from this patch.
Re: [PATCH] gitweb: fixes to gitweb feature check code
In reply to Jakub NarebskiQuoted 8 lines · Jakub Narebski
What I like about having all this, i.e. fix, futureproof and style correction in one single patch is the fact that fix doesn't introduce strange looking (gitweb_check_feature('bool_feat'))[0]... well, except encapsulated in a subroutine.
From all possible splits of this feature into series of up to three patches I think I like the one with pure subroutine rename from *check* to *get* least...
Well, I have to say that you have a strange taste, sense of priorities, and perhaps aversion to logical progression. Let's explain one more time.
The case we had at hand was that a callee has a less-than-ideal calling convention that has caused a few bugs by callers because they did not understand the calling convention. You can argue it is not entirely caller's fault that they failed to follow the calling convention, but the fact remains that there are bugs taken as a whole.
First we fix the callers, because existing bugs get highest priority. This is a pure bugfix patch that could even go to maintenance "bugfix only" branch.
Then we fix the calling convention because we all know that the calling convention was less-than-ideal. A large part of the reason the calling convention was confusing was because the wording "check" implied it was a boolean function. Logically, s/check/get/ would be a major part of fixing that.
After calling convention is enhanced by a new function that lets callers "check" via a boolean function, we can have them use that, which makes them easier to read.
But remember that it is the order I wanted the patches to be presented for review. After people involved in review agree that the result is good, I do not have any problem in squashing the three steps into a single patch for things like this after the end result is verified to be good (which we did).
Re: [PATCH] gitweb: fixes to gitweb feature check code
Quoted 21 lines · Junio C Hamano
Quoted 8 lines · Jakub Narebski
What I like about having all this, i.e. fix, futureproof and style correction in one single patch is the fact that fix doesn't introduce strange looking (gitweb_check_feature('bool_feat'))[0]... well, except encapsulated in a subroutine.
From all possible splits of this feature into series of up to three patches I think I like the one with pure subroutine rename from *check* to *get* least...
Well, I have to say that you have a strange taste, sense of priorities, and perhaps aversion to logical progression. Let's explain one more time.
The case we had at hand was that a callee has a less-than-ideal calling convention that has caused a few bugs by callers because they did not understand the calling convention. You can argue it is not entirely caller's fault that they failed to follow the calling convention, but the fact remains that there are bugs taken as a whole.
Quoted 3 lines
First we fix the callers, because existing bugs get highest priority. This is a pure bugfix patch that could even go to maintenance "bugfix only" branch.
I agree here.
Quoted 5 lines
Then we fix the calling convention because we all know that the calling convention was less-than-ideal. A large part of the reason the calling convention was confusing was because the wording "check" implied it was a boolean function. Logically, s/check/get/ would be a major part of fixing that.
And here I slightly disagree. The lone change s/check/get/ doesn't actually do anything. Splitting wrongly named 'check' into checking if feature is enabled and actual getting config does. And it is IMVHO quote obvious which calls are to be to new semantic of 'check':
my ($var) = gitweb_check_feature('feature');
and
(gitweb_check_feature('feature'))[0]
and which are to be renamed to 'get':
my ($a, $b) = gitweb_check_feature('feature');
and
my @a = gitweb_check_feature('feature');
Perhaps it is more clear from the point of view of reviewing individual pieces...
Signature · 2 lines
Jakub Narebski Poland