Skip to contentSkip to conversation

gitweb.perl - Optionally send archives as .zip files

PATCH In review Mark Levedahl 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gitweb.perl - Optionally send archives as .zip files

git-archive already knows how to generate an archive as a tar or a zip file, but gitweb did not. zip archvies are much more usable in a Windows environment due to native support and this patch allows a site admin the option to deliver zip rather than tar files. The selection is done by inserting

$feature{'snapshot'}{'default'} = ['x-zip', 'zip', ''];

in gitweb_config.perl.

Tar files remain the default option.

---
 gitweb/gitweb.perl |   25 +++++++++++++++++--------
 1 files changed, 17 insertions(+), 8 deletions(-)
gitweb/gitweb.perl +17 -8
index 21864c6..273cad2 100755
@@ -129,7 +129,7 @@ our %feature = (
 	# $feature{'snapshot'}{'default'} = [undef];
 	# To have project specific config enable override in $GITWEB_CONFIG
 	# $feature{'snapshot'}{'override'} = 1;
-	# and in project config gitweb.snapshot = none|gzip|bzip2;
+	# and in project config gitweb.snapshot = none|gzip|bzip2|zip;
 	'snapshot' => {
 		'sub' => \&feature_snapshot,
 		'override' => 0,
@@ -227,6 +227,8 @@ sub feature_snapshot {
 		return ('x-gzip', 'gz', 'gzip');
 	} elsif ($val eq 'bzip2') {
 		return ('x-bzip2', 'bz2', 'bzip2');
+	} elsif ($val eq 'zip') {
+		return ('x-zip', 'zip', '');
 	} elsif ($val eq 'none') {
 		return ();
 	}
@@ -3912,19 +3914,26 @@ sub git_snapshot {
 		$hash = git_get_head_hash($project);
 	}
 
-	my $filename = decode_utf8(basename($project)) . "-$hash.tar.$suffix";
+	my $git = git_cmd_str();
+	my $name = $project;
+	$name =~ s/\047/\047\\\047\047/g;
+	my $filename = decode_utf8(basename($project));
+	my $cmd;
+	if ($suffix eq 'zip') {
+		$filename .= "-$hash.$suffix";
+		$cmd = "$git archive --format=zip --prefix=\'$name\'/ $hash";
+	} else {
+		$filename .= "-$hash.tar.$suffix";
+		$cmd = "$git archive --format=tar --prefix=\'$name\'/ $hash | $command";
+	}
 
 	print $cgi->header(
 		-type => "application/$ctype",
 		-content_disposition => 'inline; filename="' . "$filename" . '"',
 		-status => '200 OK');
 
-	my $git = git_cmd_str();
-	my $name = $project;
-	$name =~ s/\047/\047\\\047\047/g;
-	open my $fd, "-|",
-		"$git archive --format=tar --prefix=\'$name\'/ $hash | $command"
-		or die_error(undef, "Execute git-tar-tree failed");
+	open my $fd, "-|", $cmd
+		or die_error(undef, "Execute git-archive failed");
 	binmode STDOUT, ':raw';
 	print <$fd>;
 	binmode STDOUT, ':utf8'; # as set at the beginning of gitweb.cgi

--
1.5.2.rc3.95.gb3c7e

Acked-by on v1 1/1
#1 lore raw

Hi,

Quoted 6 lines · Mark Levedahl

git-archive already knows how to generate an archive as a tar or a zip file, but gitweb did not. zip archvies are much more usable in a Windows environment due to native support and this patch allows a site admin the option to deliver zip rather than tar files. The selection is done by inserting

Quoted 7 lines

$feature{'snapshot'}{'default'} = ['x-zip', 'zip', ''];

in gitweb_config.perl.

Tar files remain the default option.

Maybe the code should decide based on program value being 'zip' rather than suffix value, but that's just dubious nit-picking.

gitweb/gitweb.perl sub git_snapshot { · line 3919

Quoted 8 lines
gitweb/gitweb.perl +3 -1
@@ -3912,19 +3914,26 @@ sub git_snapshot {
 		$hash = git_get_head_hash($project);
 	}
 
-	my $filename = decode_utf8(basename($project)) . "-$hash.tar.$suffix";
+	my $git = git_cmd_str();
+	my $name = $project;
+	$name =~ s/\047/\047\\\047\047/g;

By the way, this really looks like an entry to our very own would-be "git obfuscation of the month" contest. Why did whoever made the code not just write that ' there? (I know it's been in the original code too.)

Signature · 4 lines
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
Ever try. Ever fail. No matter. // Try again. Fail again. Fail better.
		-- Samuel Beckett
#2 lore raw
Quoted 11 lines · Petr Baudis

Hi,

Quoted 8 lines · Mark Levedahl

git-archive already knows how to generate an archive as a tar or a zip file, but gitweb did not. zip archvies are much more usable in a Windows environment due to native support and this patch allows a site admin the option to deliver zip rather than tar files. The selection is done by inserting

$feature{'snapshot'}{'default'} = ['x-zip', 'zip', ''];

Quoted 7 lines
Quoted 5 lines

in gitweb_config.perl.

Tar files remain the default option.

Ok, will apply to 'master'.