Support gzip compression for snapshots
Support gzip compression for snapshots

--- a/config/gitphp.conf.php.example
+++ b/config/gitphp.conf.php.example
@@ -93,19 +93,26 @@
 $gitphp_conf['smarty_prefix'] = "smarty/";
 
 /*
- * bzsnapshots
- * If set to true, will bzcompress snapshot tars before sending them.
- * Your PHP must have been compiled with bzip2 support!
+ * snapshotformat
+ * Indicates what kind of compression will be done on the
+ * snapshot archive.  Recognized settings are:
+ *
+ * GITPHP_SNAPSHOT_BZ2 - create a tar.bz2 file (php must have bz2 support)
+ * GITPHP_SNAPSHOT_GZ - create a tar.gz file (php must have gzip support)
+ *
+ * Any other setting, or no setting, will create uncompressed tar archives
+ * If you choose a compression format and your php does not support it,
+ * gitphp will fall back to uncompressed tar archives
  */
-$gitphp_conf['bzsnapshots'] = TRUE;
+$gitphp_conf['snapshotformat'] = GITPHP_SNAPSHOT_BZ2;
 
 /*
- * bzblocksize
- * Sets the compression level for bzip2.  Ranges from 1-9, with
+ * compresslevel
+ * Sets the compression level for snapshots.  Ranges from 1-9, with
  * 9 being the most compression but requiring the most processing
- * (bzip defaults to 4)
+ * (bzip defaults to 4, gzip defaults to -1)
  */
-$gitphp_conf['bzblocksize'] = 9;
+$gitphp_conf['compresslevel'] = 9;
 
 /*
  * geshi

--- a/include/defs.constants.php
+++ b/include/defs.constants.php
@@ -11,5 +11,8 @@
 
 define("GITPHP_RSS_ITEMS", 150);
 
+define("GITPHP_SNAPSHOT_BZ2", 1);
+define("GITPHP_SNAPSHOT_GZ", 2);
+
 ?>
 

--- a/include/display.git_snapshot.php
+++ b/include/display.git_snapshot.php
@@ -7,6 +7,7 @@
  *  Copyright (C) 2008 Christopher Han <xiphux@gmail.com>
  */
 
+require_once('defs.constants.php');
  require_once('gitutil.git_tar_tree.php');
 
 function git_snapshot($projectroot,$project,$hash)
@@ -16,15 +17,21 @@
 		$hash = "HEAD";
 	$rname = str_replace(array("/",".git"),array("-",""),$project);
 	$tar = git_tar_tree($projectroot . $project, $hash, $rname);
-	if ($gitphp_conf['bzsnapshots'] && function_exists("bzcompress")) {
+	if (($gitphp_conf['snapshotformat'] == GITPHP_SNAPSHOT_BZ2) && function_exists("bzcompress")) {
 		header("Content-Type: application/x-bzip2");
 		header("Content-Disposition: attachment; filename=" . $rname . ".tar.bz2");
-		echo bzcompress($tar,$gitphp_conf['bzblocksize']);
-	} else {
-		header("Content-Type: application/x-tar");
-		header("Content-Disposition: attachment; filename=" . $rname . ".tar");
-		echo $tar;
+		echo bzcompress($tar,(isset($gitphp_conf['compresslevel'])?$gitphp_conf['compresslevel']:4));
+		return;
+	} else if (($gitphp_conf['snapshotformat'] == GITPHP_SNAPSHOT_GZ) && function_exists("gzencode")) {
+		header("Content-Type: application/x-gzip");
+		header("Content-Disposition: attachment; filename=" . $rname . ".tar.gz");
+		echo gzencode($tar,(isset($gitphp_conf['compresslevel'])?$gitphp_conf['compresslevel']:-1));
+		return;
 	}
+
+	header("Content-Type: application/x-tar");
+	header("Content-Disposition: attachment; filename=" . $rname . ".tar");
+	echo $tar;
 }
 
 ?>

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -12,6 +12,11 @@
   * Version
   */
  include_once('include/version.php');
+
+ /*
+  * Constants
+  */
+ require_once('include/defs.constants.php');
 
  /*
   * Configuration

comments