Search in sources :

Example 36 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project gitblit by gitblit.

the class CompressionUtils method zip.

/**
 * Zips the contents of the tree at the (optionally) specified revision and
 * the (optionally) specified basepath to the supplied outputstream.
 *
 * @param repository
 * @param basePath
 *            if unspecified, entire repository is assumed.
 * @param objectId
 *            if unspecified, HEAD is assumed.
 * @param os
 * @return true if repository was successfully zipped to supplied output
 *         stream
 */
public static boolean zip(Repository repository, IFilestoreManager filestoreManager, String basePath, String objectId, OutputStream os) {
    RevCommit commit = JGitUtils.getCommit(repository, objectId);
    if (commit == null) {
        return false;
    }
    boolean success = false;
    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    try {
        tw.reset();
        tw.addTree(commit.getTree());
        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()) {
            FileMode mode = tw.getFileMode(0);
            if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
                continue;
            }
            tw.getObjectId(id, 0);
            ObjectLoader loader = repository.open(id);
            ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString());
            FilestoreModel filestoreItem = null;
            if (JGitUtils.isPossibleFilestoreItem(loader.getSize())) {
                filestoreItem = JGitUtils.getFilestoreItem(tw.getObjectReader().open(id));
            }
            final long size = (filestoreItem == null) ? loader.getSize() : filestoreItem.getSize();
            entry.setSize(size);
            entry.setComment(commit.getName());
            entry.setUnixMode(mode.getBits());
            entry.setTime(modified);
            zos.putArchiveEntry(entry);
            if (filestoreItem == null) {
                // Copy repository stored file
                loader.copyTo(zos);
            } else {
                // Copy filestore file
                try (FileInputStream streamIn = new FileInputStream(filestoreManager.getStoragePath(filestoreItem.oid))) {
                    IOUtils.copyLarge(streamIn, zos);
                } catch (Throwable e) {
                    LOGGER.error(MessageFormat.format("Failed to archive filestore item {0}", filestoreItem.oid), e);
                    // Handle as per other errors
                    throw e;
                }
            }
            zos.closeArchiveEntry();
        }
        zos.finish();
        success = true;
    } catch (IOException e) {
        error(e, repository, "{0} failed to zip files from commit {1}", commit.getName());
    } finally {
        tw.close();
        rw.dispose();
    }
    return success;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) PathFilter(org.eclipse.jgit.treewalk.filter.PathFilter) FilestoreModel(com.gitblit.models.FilestoreModel) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FileInputStream(java.io.FileInputStream) MutableObjectId(org.eclipse.jgit.lib.MutableObjectId) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 37 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project gerrit by GerritCodeReview.

the class CreateProjectIT method readProjectConfig.

private Optional<String> readProjectConfig(String projectName) throws Exception {
    try (Repository repo = repoManager.openRepository(Project.nameKey(projectName));
        TestRepository<Repository> tr = new TestRepository<>(repo)) {
        RevWalk rw = tr.getRevWalk();
        Ref ref = repo.exactRef(RefNames.REFS_CONFIG);
        if (ref == null) {
            return Optional.empty();
        }
        ObjectLoader obj = rw.getObjectReader().open(tr.get(rw.parseTree(ref.getObjectId()), PROJECT_CONFIG), Constants.OBJ_BLOB);
        return Optional.of(new String(obj.getCachedBytes(Integer.MAX_VALUE), UTF_8));
    }
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) TestRepository(org.eclipse.jgit.junit.TestRepository) Ref(org.eclipse.jgit.lib.Ref) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Aggregations

ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)37 RevWalk (org.eclipse.jgit.revwalk.RevWalk)24 ObjectId (org.eclipse.jgit.lib.ObjectId)21 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)20 RevCommit (org.eclipse.jgit.revwalk.RevCommit)19 IOException (java.io.IOException)14 Repository (org.eclipse.jgit.lib.Repository)14 ObjectReader (org.eclipse.jgit.lib.ObjectReader)12 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)6 Ref (org.eclipse.jgit.lib.Ref)6 RevTree (org.eclipse.jgit.revwalk.RevTree)6 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)5 FileMode (org.eclipse.jgit.lib.FileMode)5 IArchimateModel (com.archimatetool.model.IArchimateModel)4 HashMap (java.util.HashMap)3 FilestoreModel (com.gitblit.models.FilestoreModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 CompileException (com.googlecode.prolog_cafe.exceptions.CompileException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2