From 9197d340db81a245193dff1ecb44889b8e0dfe31 Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Tue, 24 May 2011 17:39:38 -0400 Subject: [PATCH] Download zip feature. --- src/com/gitblit/utils/JGitUtils.java | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 51 insertions(+), 1 deletions(-) diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index c9c13c5..b153c0c 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.Charset; import java.text.DateFormat; import java.text.ParseException; @@ -17,6 +18,8 @@ import java.util.Map; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.diff.DiffEntry; @@ -808,10 +811,57 @@ return null; } + public static boolean zip(Repository r, String basePath, String objectId, OutputStream os) throws Exception { + RevCommit commit = getCommit(r, objectId); + if (commit == null) { + return false; + } + final RevWalk rw = new RevWalk(r); + final TreeWalk walk = new TreeWalk(r); + try { + walk.addTree(commit.getTree()); + ZipOutputStream zos = new ZipOutputStream(os); + zos.setComment("Generated by Git:Blit"); + if (basePath != null && basePath.length() > 0) { + PathFilter f = PathFilter.create(basePath); + walk.setFilter(f); + } + walk.setRecursive(true); + while (walk.next()) { + ZipEntry entry = new ZipEntry(walk.getPathString()); + entry.setSize(walk.getObjectReader().getObjectSize(walk.getObjectId(0), Constants.OBJ_BLOB)); + entry.setComment(commit.getName()); + zos.putNextEntry(entry); + + ObjectId entid = walk.getObjectId(0); + FileMode entmode = walk.getFileMode(0); + RevBlob blob = (RevBlob) rw.lookupAny(entid, entmode.getObjectType()); + rw.parseBody(blob); + + ObjectLoader ldr = r.open(blob.getId(), Constants.OBJ_BLOB); + byte[] tmp = new byte[4096]; + InputStream in = ldr.openStream(); + int n; + while ((n = in.read(tmp)) > 0) { + zos.write(tmp, 0, n); + } + in.close(); + } + zos.finish(); + return true; + } catch (IOException e) { + LOGGER.error("Failed to zip files from commit " + commit.getName(), e); + } finally { + walk.release(); + rw.dispose(); + } + return false; + } + public static List<Metric> getDateMetrics(Repository r) { Metric total = new Metric("TOTAL"); final Map<String, Metric> metricMap = new HashMap<String, Metric>(); - + if (hasCommits(r)) { final List<RefModel> tags = getTags(r, -1); final Map<ObjectId, RefModel> tagMap = new HashMap<ObjectId, RefModel>(); -- Gitblit v1.9.1