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