diff options
author | James Moger <james.moger@gitblit.com> | 2011-05-24 17:39:38 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2011-05-24 17:39:38 -0400 |
commit | 9197d340db81a245193dff1ecb44889b8e0dfe31 (patch) | |
tree | e866bea3c64fd6714ed141c551c67e52fc2f1ad8 /src/com/gitblit/utils/JGitUtils.java | |
parent | 8a2e9c363346ef5bf48c8eba09cb8afa46fabeeb (diff) | |
download | gitblit-9197d340db81a245193dff1ecb44889b8e0dfe31.tar.gz gitblit-9197d340db81a245193dff1ecb44889b8e0dfe31.zip |
Download zip feature.
Diffstat (limited to 'src/com/gitblit/utils/JGitUtils.java')
-rw-r--r-- | 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 c9c13c56..b153c0c6 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream; 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.List; 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 @@ public class JGitUtils { 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>();
|