]> source.dussan.org Git - gitblit.git/commitdiff
Simplified archive generation (issue-174)
authorJames Moger <james.moger@gitblit.com>
Mon, 3 Dec 2012 22:06:28 +0000 (17:06 -0500)
committerJames Moger <james.moger@gitblit.com>
Mon, 3 Dec 2012 22:06:28 +0000 (17:06 -0500)
src/com/gitblit/utils/CompressionUtils.java

index 7b0d04711e08a14e61008d72ce1761568cc9fa84..414519061d3c56c46677e7461efcb4728418090a 100644 (file)
  */\r
 package com.gitblit.utils;\r
 \r
-import java.io.ByteArrayOutputStream;\r
 import java.io.IOException;\r
-import java.io.InputStream;\r
 import java.io.OutputStream;\r
 import java.text.MessageFormat;\r
 import java.util.ArrayList;\r
 import java.util.List;\r
-import java.util.zip.ZipEntry;\r
-import java.util.zip.ZipOutputStream;\r
 \r
 import org.apache.commons.compress.archivers.tar.TarArchiveEntry;\r
 import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;\r
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;\r
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;\r
 import org.apache.commons.compress.compressors.CompressorException;\r
 import org.apache.commons.compress.compressors.CompressorStreamFactory;\r
-import org.apache.commons.compress.utils.IOUtils;\r
 import org.eclipse.jgit.lib.Constants;\r
 import org.eclipse.jgit.lib.FileMode;\r
-import org.eclipse.jgit.lib.ObjectId;\r
+import org.eclipse.jgit.lib.MutableObjectId;\r
 import org.eclipse.jgit.lib.ObjectLoader;\r
+import org.eclipse.jgit.lib.ObjectReader;\r
 import org.eclipse.jgit.lib.Repository;\r
-import org.eclipse.jgit.revwalk.RevBlob;\r
 import org.eclipse.jgit.revwalk.RevCommit;\r
 import org.eclipse.jgit.revwalk.RevWalk;\r
 import org.eclipse.jgit.treewalk.TreeWalk;\r
@@ -99,37 +96,35 @@ public class CompressionUtils {
                RevWalk rw = new RevWalk(repository);\r
                TreeWalk tw = new TreeWalk(repository);\r
                try {\r
+                       tw.reset();\r
                        tw.addTree(commit.getTree());\r
-                       ZipOutputStream zos = new ZipOutputStream(os);\r
+                       ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);\r
                        zos.setComment("Generated by Gitblit");\r
                        if (!StringUtils.isEmpty(basePath)) {\r
                                PathFilter f = PathFilter.create(basePath);\r
                                tw.setFilter(f);\r
                        }\r
                        tw.setRecursive(true);\r
+                       MutableObjectId id = new MutableObjectId();\r
+                       ObjectReader reader = tw.getObjectReader();\r
+                       long modified = commit.getAuthorIdent().getWhen().getTime();\r
                        while (tw.next()) {\r
-                               if (tw.getFileMode(0) == FileMode.GITLINK) {\r
+                               FileMode mode = tw.getFileMode(0);\r
+                               if (mode == FileMode.GITLINK || mode == FileMode.TREE) {\r
                                        continue;\r
                                }\r
-                               ZipEntry entry = new ZipEntry(tw.getPathString());\r
-                               entry.setSize(tw.getObjectReader().getObjectSize(tw.getObjectId(0),\r
-                                               Constants.OBJ_BLOB));\r
-                               entry.setComment(commit.getName());\r
-                               zos.putNextEntry(entry);\r
+                               tw.getObjectId(id, 0);\r
 \r
-                               ObjectId entid = tw.getObjectId(0);\r
-                               FileMode entmode = tw.getFileMode(0);\r
-                               RevBlob blob = (RevBlob) rw.lookupAny(entid, entmode.getObjectType());\r
-                               rw.parseBody(blob);\r
+                               ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString());\r
+                               entry.setSize(reader.getObjectSize(id, Constants.OBJ_BLOB));\r
+                               entry.setComment(commit.getName());\r
+                               entry.setUnixMode(mode.getBits());\r
+                               entry.setTime(modified);\r
+                               zos.putArchiveEntry(entry);\r
 \r
-                               ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB);\r
-                               byte[] tmp = new byte[4096];\r
-                               InputStream in = ldr.openStream();\r
-                               int n;\r
-                               while ((n = in.read(tmp)) > 0) {\r
-                                       zos.write(tmp, 0, n);\r
-                               }\r
-                               in.close();\r
+                               ObjectLoader ldr = repository.open(id);\r
+                               ldr.copyTo(zos);\r
+                               zos.closeArchiveEntry();\r
                        }\r
                        zos.finish();\r
                        success = true;\r
@@ -250,6 +245,7 @@ public class CompressionUtils {
                RevWalk rw = new RevWalk(repository);\r
                TreeWalk tw = new TreeWalk(repository);\r
                try {\r
+                       tw.reset();\r
                        tw.addTree(commit.getTree());\r
                        TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);\r
                        tos.setAddPaxHeadersForNonAsciiNames(true);\r
@@ -259,45 +255,25 @@ public class CompressionUtils {
                                tw.setFilter(f);\r
                        }\r
                        tw.setRecursive(true);\r
+                       MutableObjectId id = new MutableObjectId();\r
+                       ObjectReader reader = tw.getObjectReader();\r
+                       long modified = commit.getAuthorIdent().getWhen().getTime();\r
                        while (tw.next()) {\r
                                FileMode mode = tw.getFileMode(0);\r
-                               if (mode == FileMode.GITLINK) {\r
+                               if (mode == FileMode.GITLINK || mode == FileMode.TREE) {\r
                                        continue;\r
                                }\r
-                               ObjectId id = tw.getObjectId(0);\r
+                               tw.getObjectId(id, 0);\r
                                \r
-                               // new entry\r
                                TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString());\r
-                               entry.setSize(tw.getObjectReader().getObjectSize(id, Constants.OBJ_BLOB));\r
+                               entry.setSize(reader.getObjectSize(id, Constants.OBJ_BLOB));\r
                                \r
-                               if (FileMode.SYMLINK.equals(mode)) {\r
-                                       // symlink\r
-                                       entry.setMode(mode.getBits());\r
-                                       \r
-                                       // read the symlink target\r
-                                       ByteArrayOutputStream bs = new ByteArrayOutputStream();\r
-                                       RevBlob blob = (RevBlob) rw.lookupAny(id, mode.getObjectType());\r
-                                       rw.parseBody(blob);                             \r
-                                       ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB);\r
-                                       IOUtils.copy(ldr.openStream(), bs);\r
-                                       entry.setLinkName(bs.toString("UTF-8"));\r
-                               } else {\r
-                                       // regular file or executable file\r
-                                       entry.setMode(mode.getBits());\r
-                               }                               \r
-                               entry.setModTime(commit.getAuthorIdent().getWhen());\r
-\r
+                               entry.setMode(mode.getBits());\r
+                               entry.setModTime(modified);\r
                                tos.putArchiveEntry(entry);\r
                                \r
-                               if (!FileMode.SYMLINK.equals(mode)) {\r
-                                       // write the blob\r
-                                       RevBlob blob = (RevBlob) rw.lookupAny(id, mode.getObjectType());\r
-                                       rw.parseBody(blob);                             \r
-                                       ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB);\r
-                                       IOUtils.copy(ldr.openStream(), tos);\r
-                               }\r
-                               \r
-                               // close entry\r
+                               ObjectLoader ldr = repository.open(id);\r
+                               ldr.copyTo(tos);                                        \r
                                tos.closeArchiveEntry();\r
                        }\r
                        tos.finish();\r