Skip to content

Commit 25b2790

Browse files
Oblomovgitster
authored andcommitted
gitweb: make gitweb_check_feature a boolean wrapper
The 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 the 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 adds 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. Callers of "get" that use the call only to see if the feature is enabled are updated to call this wrapper. Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a7c5a28 commit 25b2790

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

gitweb/gitweb.perl

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ BEGIN
190190
# if there is no 'sub' key (no feature-sub), then feature cannot be
191191
# overriden
192192
#
193-
#
193+
# use gitweb_check_feature(<feature>) to check if <feature> is enabled
194194

195195
# Enable the 'blame' blob view, showing the last commit that modified
196196
# each line in the file. This can be very CPU-intensive.
@@ -344,6 +344,22 @@ sub gitweb_get_feature {
344344
return $sub->(@defaults);
345345
}
346346

347+
# A wrapper to check if a given feature is enabled.
348+
# With this, you can say
349+
#
350+
# my $bool_feat = gitweb_check_feature('bool_feat');
351+
# gitweb_check_feature('bool_feat') or somecode;
352+
#
353+
# instead of
354+
#
355+
# my ($bool_feat) = gitweb_get_feature('bool_feat');
356+
# (gitweb_get_feature('bool_feat'))[0] or somecode;
357+
#
358+
sub gitweb_check_feature {
359+
return (gitweb_get_feature(@_))[0];
360+
}
361+
362+
347363
sub feature_blame {
348364
my ($val) = git_get_project_config('blame', '--bool');
349365

@@ -810,7 +826,7 @@ (%)
810826
}
811827
}
812828

813-
my ($use_pathinfo) = gitweb_get_feature('pathinfo');
829+
my $use_pathinfo = gitweb_check_feature('pathinfo');
814830
if ($use_pathinfo) {
815831
# try to put as many parameters as possible in PATH_INFO:
816832
# - project name
@@ -2101,7 +2117,7 @@ sub git_get_projects_list {
21012117
$filter ||= '';
21022118
$filter =~ s/\.git$//;
21032119

2104-
my ($check_forks) = gitweb_get_feature('forks');
2120+
my $check_forks = gitweb_check_feature('forks');
21052121

21062122
if (-d $projects_list) {
21072123
# search in directory
@@ -2947,7 +2963,7 @@ sub git_header_html {
29472963
}
29482964
print "</div>\n";
29492965

2950-
my ($have_search) = gitweb_get_feature('search');
2966+
my $have_search = gitweb_check_feature('search');
29512967
if (defined $project && $have_search) {
29522968
if (!defined $searchtext) {
29532969
$searchtext = "";
@@ -2961,7 +2977,7 @@ sub git_header_html {
29612977
$search_hash = "HEAD";
29622978
}
29632979
my $action = $my_uri;
2964-
my ($use_pathinfo) = gitweb_get_feature('pathinfo');
2980+
my $use_pathinfo = gitweb_check_feature('pathinfo');
29652981
if ($use_pathinfo) {
29662982
$action .= "/".esc_url($project);
29672983
}
@@ -3454,7 +3470,7 @@ sub is_patch_split {
34543470
sub git_difftree_body {
34553471
my ($difftree, $hash, @parents) = @_;
34563472
my ($parent) = $parents[0];
3457-
my ($have_blame) = gitweb_get_feature('blame');
3473+
my $have_blame = gitweb_check_feature('blame');
34583474
print "<div class=\"list_head\">\n";
34593475
if ($#{$difftree} > 10) {
34603476
print(($#{$difftree} + 1) . " files changed:\n");
@@ -3914,7 +3930,7 @@ sub fill_project_list_info {
39143930
my ($projlist, $check_forks) = @_;
39153931
my @projects;
39163932

3917-
my ($show_ctags) = gitweb_get_feature('ctags');
3933+
my $show_ctags = gitweb_check_feature('ctags');
39183934
PROJECT:
39193935
foreach my $pr (@$projlist) {
39203936
my (@activity) = git_get_last_activity($pr->{'path'});
@@ -3968,7 +3984,7 @@ sub git_project_list_body {
39683984
# actually uses global variable $project
39693985
my ($projlist, $order, $from, $to, $extra, $no_header) = @_;
39703986

3971-
my ($check_forks) = gitweb_get_feature('forks');
3987+
my $check_forks = gitweb_check_feature('forks');
39723988
my @projects = fill_project_list_info($projlist, $check_forks);
39733989

39743990
$order ||= $default_projects_order;
@@ -3988,7 +4004,7 @@ sub git_project_list_body {
39884004
@projects = sort {$a->{$oi->{'key'}} <=> $b->{$oi->{'key'}}} @projects;
39894005
}
39904006

3991-
my ($show_ctags) = gitweb_get_feature('ctags');
4007+
my $show_ctags = gitweb_check_feature('ctags');
39924008
if ($show_ctags) {
39934009
my %ctags;
39944010
foreach my $p (@projects) {
@@ -4428,7 +4444,7 @@ sub git_summary {
44284444
my @taglist = git_get_tags_list(16);
44294445
my @headlist = git_get_heads_list(16);
44304446
my @forklist;
4431-
my ($check_forks) = gitweb_get_feature('forks');
4447+
my $check_forks = gitweb_check_feature('forks');
44324448

44334449
if ($check_forks) {
44344450
@forklist = git_get_projects_list($project);
@@ -4457,7 +4473,7 @@ sub git_summary {
44574473
}
44584474

44594475
# Tag cloud
4460-
my ($show_ctags) = gitweb_get_feature('ctags');
4476+
my $show_ctags = gitweb_check_feature('ctags');
44614477
if ($show_ctags) {
44624478
my $ctags = git_get_project_ctags($project);
44634479
my $cloud = git_populate_project_tagcloud($ctags);
@@ -4559,7 +4575,7 @@ sub git_blame {
45594575
my $fd;
45604576
my $ftype;
45614577

4562-
gitweb_get_feature('blame')[0]
4578+
gitweb_check_feature('blame')
45634579
or die_error(403, "Blame view not allowed");
45644580

45654581
die_error(400, "No file name given") unless $file_name;
@@ -4747,7 +4763,7 @@ sub git_blob {
47474763
$expires = "+1d";
47484764
}
47494765

4750-
my ($have_blame) = gitweb_get_feature('blame');
4766+
my $have_blame = gitweb_check_feature('blame');
47514767
open my $fd, "-|", git_cmd(), "cat-file", "blob", $hash
47524768
or die_error(500, "Couldn't cat $file_name, $hash");
47534769
my $mimetype = blob_mimetype($fd, $file_name);
@@ -4840,7 +4856,7 @@ sub git_tree {
48404856
my $ref = format_ref_marker($refs, $hash_base);
48414857
git_header_html();
48424858
my $basedir = '';
4843-
my ($have_blame) = gitweb_get_feature('blame');
4859+
my $have_blame = gitweb_check_feature('blame');
48444860
if (defined $hash_base && (my %co = parse_commit($hash_base))) {
48454861
my @views_nav = ();
48464862
if (defined $file_name) {
@@ -5610,7 +5626,7 @@ sub git_history {
56105626
}
56115627

56125628
sub git_search {
5613-
gitweb_get_feature('search')[0] or die_error(403, "Search is disabled");
5629+
gitweb_check_feature('search') or die_error(403, "Search is disabled");
56145630
if (!defined $searchtext) {
56155631
die_error(400, "Text field is empty");
56165632
}
@@ -5629,11 +5645,11 @@ sub git_search {
56295645
if ($searchtype eq 'pickaxe') {
56305646
# pickaxe may take all resources of your box and run for several minutes
56315647
# with every query - so decide by yourself how public you make this feature
5632-
gitweb_get_feature('pickaxe')[0]
5648+
gitweb_check_feature('pickaxe')
56335649
or die_error(403, "Pickaxe is disabled");
56345650
}
56355651
if ($searchtype eq 'grep') {
5636-
gitweb_get_feature('grep')[0]
5652+
gitweb_check_feature('grep')
56375653
or die_error(403, "Grep is disabled");
56385654
}
56395655

@@ -5838,7 +5854,7 @@ sub git_search_help {
58385854
<dt><b>commit</b></dt>
58395855
<dd>The commit messages and authorship information will be scanned for the given pattern.</dd>
58405856
EOT
5841-
my ($have_grep) = gitweb_get_feature('grep');
5857+
my $have_grep = gitweb_check_feature('grep');
58425858
if ($have_grep) {
58435859
print <<EOT;
58445860
<dt><b>grep</b></dt>
@@ -5855,7 +5871,7 @@ sub git_search_help {
58555871
<dt><b>committer</b></dt>
58565872
<dd>Name and e-mail of the committer and date of commit will be scanned for the given pattern.</dd>
58575873
EOT
5858-
my ($have_pickaxe) = gitweb_get_feature('pickaxe');
5874+
my $have_pickaxe = gitweb_check_feature('pickaxe');
58595875
if ($have_pickaxe) {
58605876
print <<EOT;
58615877
<dt><b>pickaxe</b></dt>
@@ -5907,7 +5923,7 @@ sub git_shortlog {
59075923

59085924
sub git_feed {
59095925
my $format = shift || 'atom';
5910-
my ($have_blame) = gitweb_get_feature('blame');
5926+
my $have_blame = gitweb_check_feature('blame');
59115927

59125928
# Atom: http://www.atomenabled.org/developers/syndication/
59135929
# RSS: http://www.notestips.com/80256B3A007F2692/1/NAMO5P9UPQ

0 commit comments

Comments
 (0)