gitweb: Make blame and snapshot a feature.
This adds blame and snapshot to the feature associative array. This also helps in enabling or disabling these features via GITWEB_CONFIG and overriding if allowed via project specfic config. Signed-off-by: Aneesh Kumar K.V <aneesh.kumar@gmail.com> Signed-off-by: Junio C Hamano <junkio@cox.net>
Aneesh Kumar K.V committed
Aug 20, 2006 at 11:53 UTC
ddb8d900489216a0bf821a26901a21f89571a1c3
1 file changed
+100
-23
gitweb/gitweb.perl
+100
-23
@@ -67,6 +67,68 @@ our $default_text_plain_charset = undef;
67
# (relative to the current git repository)
68
our $mimetypes_file = undef;
69
70
+# You define site-wide feature defaults here; override them with
71
+# $GITWEB_CONFIG as necessary.
72
+our %feature =
73
+(
74
+
75
+# feature => {'sub' => feature-sub, 'override' => allow-override, 'default' => [ default options...]
76
+
77
+'blame' => {'sub' => \&feature_blame, 'override' => 0, 'default' => [0]},
78
+'snapshot' => {'sub' => \&feature_snapshot, 'override' => 0, 'default' => ['x-gzip', 'gz', 'gzip']},
79
+
80
+);
81
+
82
+sub gitweb_check_feature {
83
+ my ($name) = @_;
84
+ return undef unless exists $feature{$name};
85
+ my ($sub, $override, @defaults) = ($feature{$name}{'sub'},
86
+ $feature{$name}{'override'},
87
+ @{$feature{$name}{'default'}});
88
+ if (!$override) { return @defaults; }
89
+ return $sub->(@defaults);
90
+}
91
+
92
+# To enable system wide have in $GITWEB_CONFIG
93
+# $feature{'blame'}{'default'} = [1];
94
+# To have project specific config enable override in $GITWEB_CONFIG
95
+# $feature{'blame'}{'override'} = 1;
96
+# and in project config gitweb.blame = 0|1;
97
+
98
+sub feature_blame {
99
+ my ($val) = git_get_project_config('blame', '--bool');
100
+
101
+ if ($val eq 'true') {
102
+ return 1;
103
+ } elsif ($val eq 'false') {
104
+ return 0;
105
+ }
106
+
107
+ return $_[0];
108
+}
109
+
110
+# To disable system wide have in $GITWEB_CONFIG
111
+# $feature{'snapshot'}{'default'} = [undef];
112
+# To have project specific config enable override in $GITWEB_CONFIG
113
+# $feature{'blame'}{'override'} = 1;
114
+# and in project config gitweb.snapshot = none|gzip|bzip2
115
+
116
+sub feature_snapshot {
117
+ my ($ctype, $suffix, $command) = @_;
118
+
119
+ my ($val) = git_get_project_config('snapshot');
120
+
121
+ if ($val eq 'gzip') {
122
+ return ('x-gzip', 'gz', 'gzip');
123
+ } elsif ($val eq 'bzip2') {
124
+ return ('x-bzip2', 'bz2', 'bzip2');
125
+ } elsif ($val eq 'none') {
126
+ return ();
127
+ }
128
+
129
+ return ($ctype, $suffix, $command);
130
+}
131
+
132
our $GITWEB_CONFIG = $ENV{'GITWEB_CONFIG'} || "++GITWEB_CONFIG++";
133
require $GITWEB_CONFIG if -e $GITWEB_CONFIG;
134
@@ -485,24 +547,21 @@ sub git_get_type {
547
}
548
549
sub git_get_project_config {
488
- my $key = shift;
550
+ my ($key, $type) = @_;
551
552
return unless ($key);
553
$key =~ s/^gitweb\.//;
554
return if ($key =~ m/\W/);
555
494
- my $val = qx($GIT repo-config --get gitweb.$key);
556
+ my @x = ($GIT, 'repo-config');
557
+ if (defined $type) { push @x, $type; }
558
+ push @x, "--get";
559
+ push @x, "gitweb.$key";
560
+ my $val = qx(@x);
561
+ chomp $val;
562
return ($val);
563
}
564
498
-sub git_get_project_config_bool {
499
- my $val = git_get_project_config (@_);
500
- if ($val and $val =~ m/true|yes|on/) {
501
- return (1);
502
- }
503
- return; # implicit false
504
-}
505
-
565
# get hash of given path at given ref
566
sub git_get_hash_by_path {
567
my $base = shift;
@@ -1397,7 +1456,10 @@ sub git_difftree_body {
1456
sub git_shortlog_body {
1457
# uses global variable $project
1458
my ($revlist, $from, $to, $refs, $extra) = @_;
1400
- my $have_snapshot = git_get_project_config_bool('snapshot');
1459
+
1460
+ my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
1461
+ my $have_snapshot = (defined $ctype && defined $suffix);
1462
+
1463
$from = 0 unless defined $from;
1464
$to = $#{$revlist} if (!defined $to || $#{$revlist} < $to);
1465
@@ -1858,7 +1920,10 @@ sub git_tag {
1920
sub git_blame2 {
1921
my $fd;
1922
my $ftype;
1861
- die_error(undef, "Permission denied") if (!git_get_project_config_bool ('blame'));
1923
+
1924
+ if (!gitweb_check_feature('blame')) {
1925
+ die_error('403 Permission denied', "Permission denied");
1926
+ }
1927
die_error('404 Not Found', "File name not defined") if (!$file_name);
1928
$hash_base ||= git_get_head_hash($project);
1929
die_error(undef, "Couldn't find base commit") unless ($hash_base);
@@ -1916,7 +1981,10 @@ sub git_blame2 {
1981
1982
sub git_blame {
1983
my $fd;
1919
- die_error('403 Permission denied', "Permission denied") if (!git_get_project_config_bool ('blame'));
1984
+
1985
+ if (!gitweb_check_feature('blame')) {
1986
+ die_error('403 Permission denied', "Permission denied");
1987
+ }
1988
die_error('404 Not Found', "File name not defined") if (!$file_name);
1989
$hash_base ||= git_get_head_hash($project);
1990
die_error(undef, "Couldn't find base commit") unless ($hash_base);
@@ -2069,7 +2137,7 @@ sub git_blob {
2137
die_error(undef, "No file name defined");
2138
}
2139
}
2072
- my $have_blame = git_get_project_config_bool ('blame');
2140
+ my $have_blame = gitweb_check_feature('blame');
2141
open my $fd, "-|", $GIT, "cat-file", "blob", $hash
2142
or die_error(undef, "Couldn't cat $file_name, $hash");
2143
my $mimetype = blob_mimetype($fd, $file_name);
@@ -2134,7 +2202,7 @@ sub git_tree {
2202
git_header_html();
2203
my %base_key = ();
2204
my $base = "";
2137
- my $have_blame = git_get_project_config_bool ('blame');
2205
+ my $have_blame = gitweb_check_feature('blame');
2206
if (defined $hash_base && (my %co = parse_commit($hash_base))) {
2207
$base_key{hash_base} = $hash_base;
2208
git_print_page_nav('tree','', $hash_base);
@@ -2195,25 +2263,31 @@ sub git_tree {
2263
2264
sub git_snapshot {
2265
2266
+ my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2267
+ my $have_snapshot = (defined $ctype && defined $suffix);
2268
+ if (!$have_snapshot) {
2269
+ die_error('403 Permission denied', "Permission denied");
2270
+ }
2271
+
2272
if (!defined $hash) {
2273
$hash = git_get_head_hash($project);
2274
}
2275
2202
- my $filename = basename($project) . "-$hash.tar.gz";
2276
+ my $filename = basename($project) . "-$hash.tar.$suffix";
2277
2278
print $cgi->header(-type => 'application/x-tar',
2205
- -content-encoding => 'x-gzip',
2206
- '-content-disposition' => "inline; filename=\"$filename\"",
2207
- -status => '200 OK');
2279
+ -content-encoding => $ctype,
2280
+ '-content-disposition' =>
2281
+ "inline; filename=\"$filename\"",
2282
+ -status => '200 OK');
2283
2209
- open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | gzip" or
2210
- die_error(undef, "Execute git-tar-tree failed.");
2284
+ open my $fd, "-|", "$GIT tar-tree $hash \'$project\' | $command" or
2285
+ die_error(undef, "Execute git-tar-tree failed.");
2286
binmode STDOUT, ':raw';
2287
print <$fd>;
2288
binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
2289
close $fd;
2290
2216
-
2291
}
2292
2293
sub git_log {
@@ -2293,7 +2367,10 @@ sub git_commit {
2367
}
2368
my $refs = git_get_references();
2369
my $ref = format_ref_marker($refs, $co{'id'});
2296
- my $have_snapshot = git_get_project_config_bool('snapshot');
2370
+
2371
+ my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
2372
+ my $have_snapshot = (defined $ctype && defined $suffix);
2373
+
2374
my $formats_nav = '';
2375
if (defined $file_name && defined $co{'parent'}) {
2376
my $parent = $co{'parent'};