Search in sources :

Example 6 with ObjectLoader

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

the class ReviewNoteMerger method merge.

@Override
public Note merge(Note base, Note ours, Note theirs, ObjectReader reader, ObjectInserter inserter) throws IOException {
    if (ours == null) {
        return theirs;
    }
    if (theirs == null) {
        return ours;
    }
    if (ours.getData().equals(theirs.getData())) {
        return ours;
    }
    ObjectLoader lo = reader.open(ours.getData());
    byte[] sep = new byte[] { '\n' };
    ObjectLoader lt = reader.open(theirs.getData());
    try (ObjectStream os = lo.openStream();
        ByteArrayInputStream b = new ByteArrayInputStream(sep);
        ObjectStream ts = lt.openStream();
        UnionInputStream union = new UnionInputStream(os, b, ts)) {
        ObjectId noteData = inserter.insert(Constants.OBJ_BLOB, lo.getSize() + sep.length + lt.getSize(), union);
        return new Note(ours, noteData);
    }
}
Also used : UnionInputStream(org.eclipse.jgit.util.io.UnionInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectStream(org.eclipse.jgit.lib.ObjectStream) Note(org.eclipse.jgit.notes.Note) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader)

Example 7 with ObjectLoader

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

the class JGitConnection method showFileContent.

@Override
public ShowFileContentResponse showFileContent(String file, String version) throws GitException {
    String content;
    ObjectId revision;
    try {
        revision = getRepository().resolve(version);
        try (RevWalk revWalk = new RevWalk(getRepository())) {
            RevCommit revCommit = revWalk.parseCommit(revision);
            RevTree tree = revCommit.getTree();
            try (TreeWalk treeWalk = new TreeWalk(getRepository())) {
                treeWalk.addTree(tree);
                treeWalk.setRecursive(true);
                treeWalk.setFilter(PathFilter.create(file));
                if (!treeWalk.next()) {
                    throw new GitException("fatal: Path '" + file + "' does not exist in '" + version + "'" + lineSeparator());
                }
                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);
                content = new String(loader.getBytes());
            }
        }
    } catch (IOException exception) {
        throw new GitException(exception.getMessage());
    }
    return newDto(ShowFileContentResponse.class).withContent(content);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) GitException(org.eclipse.che.api.git.exception.GitException) ShowFileContentResponse(org.eclipse.che.api.git.shared.ShowFileContentResponse) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 8 with ObjectLoader

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

the class JGitUtils method getByteContent.

/**
	 * Retrieves the raw byte content of a file in the specified tree.
	 *
	 * @param repository
	 * @param tree
	 *            if null, the RevTree from HEAD is assumed.
	 * @param path
	 * @return content as a byte []
	 */
public static byte[] getByteContent(Repository repository, RevTree tree, final String path, boolean throwError) {
    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
    byte[] content = null;
    try {
        if (tree == null) {
            ObjectId object = getDefaultBranch(repository);
            if (object == null)
                return null;
            RevCommit commit = rw.parseCommit(object);
            tree = commit.getTree();
        }
        tw.reset(tree);
        while (tw.next()) {
            if (tw.isSubtree() && !path.equals(tw.getPathString())) {
                tw.enterSubtree();
                continue;
            }
            ObjectId entid = tw.getObjectId(0);
            FileMode entmode = tw.getFileMode(0);
            if (entmode != FileMode.GITLINK) {
                ObjectLoader ldr = repository.open(entid, Constants.OBJ_BLOB);
                content = ldr.getCachedBytes();
            }
        }
    } catch (Throwable t) {
        if (throwError) {
            error(t, repository, "{0} can't find {1} in tree {2}", path, tree.name());
        }
    } finally {
        rw.dispose();
        tw.close();
    }
    return content;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 9 with ObjectLoader

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

the class JGitUtils method getByteContent.

/**
	 * Gets the raw byte content of the specified blob object.
	 *
	 * @param repository
	 * @param objectId
	 * @return byte [] blob content
	 */
public static byte[] getByteContent(Repository repository, String objectId) {
    RevWalk rw = new RevWalk(repository);
    byte[] content = null;
    try {
        RevBlob blob = rw.lookupBlob(ObjectId.fromString(objectId));
        ObjectLoader ldr = repository.open(blob.getId(), Constants.OBJ_BLOB);
        content = ldr.getCachedBytes();
    } catch (Throwable t) {
        error(t, repository, "{0} can't find blob {1}", objectId);
    } finally {
        rw.dispose();
    }
    return content;
}
Also used : RevBlob(org.eclipse.jgit.revwalk.RevBlob) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 10 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)

Aggregations

ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)16 RevWalk (org.eclipse.jgit.revwalk.RevWalk)11 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 ObjectId (org.eclipse.jgit.lib.ObjectId)7 ObjectReader (org.eclipse.jgit.lib.ObjectReader)6 IOException (java.io.IOException)5 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)5 FileMode (org.eclipse.jgit.lib.FileMode)5 MutableObjectId (org.eclipse.jgit.lib.MutableObjectId)3 PathFilter (org.eclipse.jgit.treewalk.filter.PathFilter)3 FilestoreModel (com.gitblit.models.FilestoreModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 OutputStream (java.io.OutputStream)2 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)2 Repository (org.eclipse.jgit.lib.Repository)2 RevObject (org.eclipse.jgit.revwalk.RevObject)2 RevTree (org.eclipse.jgit.revwalk.RevTree)2