@matheusbsbs / matheus-bagliano / commits / a3c8ab30a5
Fork 0

gitweb: snapshot cleanups & support for offering multiple formats

- Centralize knowledge about snapshot formats (mime types, extensions, commands) in %known_snapshot_formats and improve how some of that information is specified. In particular, zip files are no longer a special case. - Add support for offering multiple snapshot formats to the user so that he/she can download a snapshot in the format he/she prefers. The site-wide or project configuration now gives a list of formats to offer, and if more than one format is offered, the "_snapshot_" link becomes something like "snapshot (_tar.bz2_ _zip_)". - If only one format is offered, a tooltip on the "_snapshot_" link tells the user what it is. - Fix out-of-date "tarball" -> "archive" in comment. Alert for gitweb site administrators: This patch changes the format of $feature{'snapshot'}{'default'} in gitweb_config.perl from a list of three pieces of information about a single format to a list of one or more formats you wish to offer from the set ('tgz', 'tbz2', 'zip'). Update your gitweb_config.perl appropriately. There was taken care for old-style gitweb configuration to work as it used to, but this backward compatibility works only for the values which correspond to gitweb.snapshot values of 'gzip', 'bzip2' and 'zip', i.e. ['x-gzip', 'gz', 'gzip'] ['x-bzip2', 'bz2', 'bzip2'] ['x-zip', 'zip', ''] The preferred names for gitweb.snapshot in repository configuration have also changed from 'gzip' and 'bzip2' to 'tgz' and 'tbz2', but the old names are still recognized for compatibility. Signed-off-by: Matt McCutchen <hashproduct@gmail.com> Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matt McCutchen committed Jul 22, 2007 at 01:30 UTC a3c8ab30a54c30a6a434760bedf04548425416ef
1 file changed +116 -50
gitweb/gitweb.perl
+116 -50
@@ -114,6 +114,49 @@ our $fallback_encoding = 'latin1';
114 # - one might want to include '-B' option, e.g. '-B', '-M'
115 our @diff_opts = ('-M'); # taken from git_commit
116
117 +# information about snapshot formats that gitweb is capable of serving
118 +our %known_snapshot_formats = (
119 + # name => {
120 + # 'display' => display name,
121 + # 'type' => mime type,
122 + # 'suffix' => filename suffix,
123 + # 'format' => --format for git-archive,
124 + # 'compressor' => [compressor command and arguments]
125 + # (array reference, optional)}
126 + #
127 + 'tgz' => {
128 + 'display' => 'tar.gz',
129 + 'type' => 'application/x-gzip',
130 + 'suffix' => '.tar.gz',
131 + 'format' => 'tar',
132 + 'compressor' => ['gzip']},
133 +
134 + 'tbz2' => {
135 + 'display' => 'tar.bz2',
136 + 'type' => 'application/x-bzip2',
137 + 'suffix' => '.tar.bz2',
138 + 'format' => 'tar',
139 + 'compressor' => ['bzip2']},
140 +
141 + 'zip' => {
142 + 'display' => 'zip',
143 + 'type' => 'application/x-zip',
144 + 'suffix' => '.zip',
145 + 'format' => 'zip'},
146 +);
147 +
148 +# Aliases so we understand old gitweb.snapshot values in repository
149 +# configuration.
150 +our %known_snapshot_format_aliases = (
151 + 'gzip' => 'tgz',
152 + 'bzip2' => 'tbz2',
153 +
154 + # backward compatibility: legacy gitweb config support
155 + 'x-gzip' => undef, 'gz' => undef,
156 + 'x-bzip2' => undef, 'bz2' => undef,
157 + 'x-zip' => undef, '' => undef,
158 +);
159 +
160 # You define site-wide feature defaults here; override them with
161 # $GITWEB_CONFIG as necessary.
162 our %feature = (
@@ -144,20 +187,22 @@ our %feature = (
187 'override' => 0,
188 'default' => [0]},
189
147 - # Enable the 'snapshot' link, providing a compressed tarball of any
190 + # Enable the 'snapshot' link, providing a compressed archive of any
191 # tree. This can potentially generate high traffic if you have large
192 # project.
193
194 + # Value is a list of formats defined in %known_snapshot_formats that
195 + # you wish to offer.
196 # To disable system wide have in $GITWEB_CONFIG
152 - # $feature{'snapshot'}{'default'} = [undef];
197 + # $feature{'snapshot'}{'default'} = [];
198 # To have project specific config enable override in $GITWEB_CONFIG
199 # $feature{'snapshot'}{'override'} = 1;
155 - # and in project config gitweb.snapshot = none|gzip|bzip2|zip;
200 + # and in project config, a comma-separated list of formats or "none"
201 + # to disable. Example: gitweb.snapshot = tbz2,zip;
202 'snapshot' => {
203 'sub' => \&feature_snapshot,
204 'override' => 0,
159 - # => [content-encoding, suffix, program]
160 - 'default' => ['x-gzip', 'gz', 'gzip']},
205 + 'default' => ['tgz']},
206
207 # Enable text search, which will list the commits which match author,
208 # committer or commit text to a given string. Enabled by default.
@@ -256,28 +301,19 @@ sub feature_blame {
301 }
302
303 sub feature_snapshot {
259 - my ($ctype, $suffix, $command) = @_;
304 + my (@fmts) = @_;
305
306 my ($val) = git_get_project_config('snapshot');
307
263 - if ($val eq 'gzip') {
264 - return ('x-gzip', 'gz', 'gzip');
265 - } elsif ($val eq 'bzip2') {
266 - return ('x-bzip2', 'bz2', 'bzip2');
267 - } elsif ($val eq 'zip') {
268 - return ('x-zip', 'zip', '');
269 - } elsif ($val eq 'none') {
270 - return ();
308 + if ($val) {
309 + @fmts = ($val eq 'none' ? () : split /\s*[,\s]\s*/, $val);
310 + @fmts = grep { defined } map {
311 + exists $known_snapshot_format_aliases{$_} ?
312 + $known_snapshot_format_aliases{$_} : $_ } @fmts;
313 + @fmts = grep(exists $known_snapshot_formats{$_}, @fmts);
314 }
315
273 - return ($ctype, $suffix, $command);
274 -}
275 -
276 -sub gitweb_have_snapshot {
277 - my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
278 - my $have_snapshot = (defined $ctype && defined $suffix);
279 -
280 - return $have_snapshot;
316 + return @fmts;
317 }
318
319 sub feature_grep {
@@ -563,6 +599,7 @@ sub href(%) {
599 order => "o",
600 searchtext => "s",
601 searchtype => "st",
602 + snapshot_format => "sf",
603 );
604 my %mapping = @mapping;
605
@@ -1257,6 +1294,39 @@ sub format_diff_line {
1294 return "<div class=\"diff$diff_class\">" . esc_html($line, -nbsp=>1) . "</div>\n";
1295 }
1296
1297 +# Generates undef or something like "_snapshot_" or "snapshot (_tbz2_ _zip_)",
1298 +# linked. Pass the hash of the tree/commit to snapshot.
1299 +sub format_snapshot_links {
1300 + my ($hash) = @_;
1301 + my @snapshot_fmts = gitweb_check_feature('snapshot');
1302 + my $num_fmts = @snapshot_fmts;
1303 + if ($num_fmts > 1) {
1304 + # A parenthesized list of links bearing format names.
1305 + return "snapshot (" . join(' ', map
1306 + $cgi->a({
1307 + -href => href(
1308 + action=>"snapshot",
1309 + hash=>$hash,
1310 + snapshot_format=>$_
1311 + )
1312 + }, $known_snapshot_formats{$_}{'display'})
1313 + , @snapshot_fmts) . ")";
1314 + } elsif ($num_fmts == 1) {
1315 + # A single "snapshot" link whose tooltip bears the format name.
1316 + my ($fmt) = @snapshot_fmts;
1317 + return $cgi->a({
1318 + -href => href(
1319 + action=>"snapshot",
1320 + hash=>$hash,
1321 + snapshot_format=>$fmt
1322 + ),
1323 + -title => "in format: $known_snapshot_formats{$fmt}{'display'}"
1324 + }, "snapshot");
1325 + } else { # $num_fmts == 0
1326 + return undef;
1327 + }
1328 +}
1329 +
1330 ## ----------------------------------------------------------------------
1331 ## git utility subroutines, invoking git commands
1332
@@ -3321,8 +3391,6 @@ sub git_shortlog_body {
3391 # uses global variable $project
3392 my ($commitlist, $from, $to, $refs, $extra) = @_;
3393
3324 - my $have_snapshot = gitweb_have_snapshot();
3325 -
3394 $from = 0 unless defined $from;
3395 $to = $#{$commitlist} if (!defined $to || $#{$commitlist} < $to);
3396
@@ -3349,8 +3417,9 @@ sub git_shortlog_body {
3417 $cgi->a({-href => href(action=>"commit", hash=>$commit)}, "commit") . " | " .
3418 $cgi->a({-href => href(action=>"commitdiff", hash=>$commit)}, "commitdiff") . " | " .
3419 $cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree");
3352 - if ($have_snapshot) {
3353 - print " | " . $cgi->a({-href => href(action=>"snapshot", hash=>$commit)}, "snapshot");
3420 + my $snapshot_links = format_snapshot_links($commit);
3421 + if (defined $snapshot_links) {
3422 + print " | " . $snapshot_links;
3423 }
3424 print "</td>\n" .
3425 "</tr>\n";
@@ -4132,8 +4201,6 @@ sub git_blob {
4201 }
4202
4203 sub git_tree {
4135 - my $have_snapshot = gitweb_have_snapshot();
4136 -
4204 if (!defined $hash_base) {
4205 $hash_base = "HEAD";
4206 }
@@ -4167,11 +4234,10 @@ sub git_tree {
4234 hash_base=>"HEAD", file_name=>$file_name)},
4235 "HEAD"),
4236 }
4170 - if ($have_snapshot) {
4237 + my $snapshot_links = format_snapshot_links($hash);
4238 + if (defined $snapshot_links) {
4239 # FIXME: Should be available when we have no hash base as well.
4172 - push @views_nav,
4173 - $cgi->a({-href => href(action=>"snapshot", hash=>$hash)},
4174 - "snapshot");
4240 + push @views_nav, $snapshot_links;
4241 }
4242 git_print_page_nav('tree','', $hash_base, undef, undef, join(' | ', @views_nav));
4243 git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash_base);
@@ -4235,33 +4301,36 @@ sub git_tree {
4301 }
4302
4303 sub git_snapshot {
4238 - my ($ctype, $suffix, $command) = gitweb_check_feature('snapshot');
4239 - my $have_snapshot = (defined $ctype && defined $suffix);
4240 - if (!$have_snapshot) {
4241 - die_error('403 Permission denied', "Permission denied");
4304 + my @supported_fmts = gitweb_check_feature('snapshot');
4305 +
4306 + my $format = $cgi->param('sf');
4307 + unless ($format =~ m/[a-z0-9]+/
4308 + && exists($known_snapshot_formats{$format})
4309 + && grep($_ eq $format, @supported_fmts)) {
4310 + die_error(undef, "Unsupported snapshot format");
4311 }
4312
4313 if (!defined $hash) {
4314 $hash = git_get_head_hash($project);
4315 }
4316
4248 - my $git = git_cmd_str();
4317 + my $git_command = git_cmd_str();
4318 my $name = $project;
4319 $name =~ s,([^/])/*\.git$,$1,;
4320 $name = basename($name);
4321 my $filename = to_utf8($name);
4322 $name =~ s/\047/\047\\\047\047/g;
4323 my $cmd;
4255 - if ($suffix eq 'zip') {
4256 - $filename .= "-$hash.$suffix";
4257 - $cmd = "$git archive --format=zip --prefix=\'$name\'/ $hash";
4258 - } else {
4259 - $filename .= "-$hash.tar.$suffix";
4260 - $cmd = "$git archive --format=tar --prefix=\'$name\'/ $hash | $command";
4324 + $filename .= "-$hash$known_snapshot_formats{$format}{'suffix'}";
4325 + $cmd = "$git_command archive " .
4326 + "--format=$known_snapshot_formats{$format}{'format'}" .
4327 + "--prefix=\'$name\'/ $hash";
4328 + if (exists $known_snapshot_formats{$format}{'compressor'}) {
4329 + $cmd .= ' | ' . join ' ', @{$known_snapshot_formats{$format}{'compressor'}};
4330 }
4331
4332 print $cgi->header(
4264 - -type => "application/$ctype",
4333 + -type => $known_snapshot_formats{$format}{'type'},
4334 -content_disposition => 'inline; filename="' . "$filename" . '"',
4335 -status => '200 OK');
4336
@@ -4271,7 +4340,6 @@ sub git_snapshot {
4340 print <$fd>;
4341 binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi
4342 close $fd;
4274 -
4343 }
4344
4345 sub git_log {
@@ -4390,8 +4458,6 @@ sub git_commit {
4458 my $refs = git_get_references();
4459 my $ref = format_ref_marker($refs, $co{'id'});
4460
4393 - my $have_snapshot = gitweb_have_snapshot();
4394 -
4461 git_header_html(undef, $expires);
4462 git_print_page_nav('commit', '',
4463 $hash, $co{'tree'}, $hash,
@@ -4430,9 +4496,9 @@ sub git_commit {
4496 "<td class=\"link\">" .
4497 $cgi->a({-href => href(action=>"tree", hash=>$co{'tree'}, hash_base=>$hash)},
4498 "tree");
4433 - if ($have_snapshot) {
4434 - print " | " .
4435 - $cgi->a({-href => href(action=>"snapshot", hash=>$hash)}, "snapshot");
4499 + my $snapshot_links = format_snapshot_links($hash);
4500 + if (defined $snapshot_links) {
4501 + print " | " . $snapshot_links;
4502 }
4503 print "</td>" .
4504 "</tr>\n";