Search in sources :

Example 21 with ObjectLoader

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

the class ConfigChangeIT method readProjectConfig.

private Config readProjectConfig() throws Exception {
    RevWalk rw = testRepo.getRevWalk();
    RevTree tree = rw.parseTree(testRepo.getRepository().resolve("HEAD"));
    RevObject obj = rw.parseAny(testRepo.get(tree, "project.config"));
    ObjectLoader loader = rw.getObjectReader().open(obj);
    String text = new String(loader.getCachedBytes(), UTF_8);
    Config cfg = new Config();
    cfg.fromText(text);
    return cfg;
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) Config(org.eclipse.jgit.lib.Config) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 22 with ObjectLoader

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

the class RevisionServlet method doGetText.

@Override
protected void doGetText(HttpServletRequest req, HttpServletResponse res) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Repository repo = ServletUtils.getRepository(req);
    try (ObjectReader reader = repo.newObjectReader()) {
        ObjectLoader loader = reader.open(view.getRevision().getId());
        if (loader.getType() != OBJ_COMMIT) {
            res.setStatus(SC_NOT_FOUND);
        } else {
            PathServlet.setTypeHeader(res, loader.getType());
            try (Writer writer = startRenderText(req, res);
                OutputStream out = BaseEncoding.base64().encodingStream(writer)) {
                loader.copyTo(out);
            }
        }
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) OutputStream(java.io.OutputStream) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) Writer(java.io.Writer)

Example 23 with ObjectLoader

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

the class PathServlet method showSymlink.

private void showSymlink(HttpServletRequest req, HttpServletResponse res, WalkResult wr) throws IOException {
    GitilesView view = ViewFilter.getView(req);
    Map<String, Object> data = Maps.newHashMap();
    ObjectLoader loader = wr.getObjectReader().open(wr.id, OBJ_BLOB);
    String target;
    try {
        target = RawParseUtils.decode(loader.getCachedBytes(TreeSoyData.MAX_SYMLINK_SIZE));
    } catch (LargeObjectException.OutOfMemory e) {
        throw e;
    } catch (LargeObjectException e) {
        data.put("sha", ObjectId.toString(wr.id));
        data.put("data", null);
        data.put("size", Long.toString(loader.getSize()));
        renderHtml(req, res, "gitiles.pathDetail", ImmutableMap.of("title", ViewFilter.getView(req).getPathPart(), "breadcrumbs", view.getBreadcrumbs(wr.hasSingleTree), "type", FileType.REGULAR_FILE.toString(), "data", data));
        return;
    }
    String url = resolveTargetUrl(GitilesView.path().copyFrom(view).setPathPart(dirname(view.getPathPart())).build(), target);
    data.put("title", view.getPathPart());
    data.put("target", target);
    if (url != null) {
        data.put("targetUrl", url);
    }
    // TODO(sop): Allow caching files by SHA-1 when no S cookie is sent.
    renderHtml(req, res, "gitiles.pathDetail", ImmutableMap.of("title", ViewFilter.getView(req).getPathPart(), "breadcrumbs", view.getBreadcrumbs(wr.hasSingleTree), "type", FileType.SYMLINK.toString(), "data", data));
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) RevObject(org.eclipse.jgit.revwalk.RevObject) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) QuotedString(org.eclipse.jgit.util.QuotedString)

Example 24 with ObjectLoader

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

the class BlobSoyData method toSoyData.

public Map<String, Object> toSoyData(String path, ObjectId blobId) throws MissingObjectException, IOException {
    Map<String, Object> data = Maps.newHashMapWithExpectedSize(4);
    data.put("sha", ObjectId.toString(blobId));
    ObjectLoader loader = reader.open(blobId, Constants.OBJ_BLOB);
    String content;
    try {
        byte[] raw = loader.getCachedBytes(MAX_FILE_SIZE);
        content = !RawText.isBinary(raw) ? RawParseUtils.decode(raw) : null;
    } catch (LargeObjectException.OutOfMemory e) {
        throw e;
    } catch (LargeObjectException e) {
        content = null;
    }
    if (content != null) {
        data.put("lines", prettify(path, content));
        if (path != null && path.endsWith(".md")) {
            data.put("docUrl", GitilesView.doc().copyFrom(view).toUrl());
        }
    } else {
        data.put("lines", null);
        data.put("size", Long.toString(loader.getSize()));
    }
    if (path != null && view.getRevision().getPeeledType() == OBJ_COMMIT) {
        data.put("fileUrl", GitilesView.path().copyFrom(view).toUrl());
        data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
        data.put("blameUrl", GitilesView.blame().copyFrom(view).toUrl());
    }
    return data;
}
Also used : LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader)

Example 25 with ObjectLoader

use of org.eclipse.jgit.lib.ObjectLoader in project archi-modelrepository-plugin by archi-contribs.

the class MergeConflictHandler method extractModel.

/**
 * Extract a model from either our latest commit or their latest online commit
 * ref = "refs/head/master" or "origin/master"
 * @throws CanceledException
 */
private IArchimateModel extractModel(String ref) throws IOException, CanceledException {
    // $NON-NLS-1$ //$NON-NLS-2$
    File tmpFolder = new File(System.getProperty("java.io.tmpdir"), "org.archicontribs.modelrepository.tmp");
    FileUtils.deleteFolder(tmpFolder);
    tmpFolder.mkdirs();
    try (Repository repository = Git.open(fArchiRepo.getLocalRepositoryFolder()).getRepository()) {
        RevCommit commit = null;
        // A RevWalk walks over commits based on some filtering that is defined
        try (RevWalk revWalk = new RevWalk(repository)) {
            // We are interested in the origin master branch
            ObjectId objectID = repository.resolve(ref);
            if (objectID != null) {
                commit = revWalk.parseCommit(objectID);
            }
            revWalk.dispose();
        }
        if (commit == null) {
            throw new IOException(Messages.MergeConflictHandler_1);
        }
        // Walk the tree and get the contents of the commit
        try (TreeWalk treeWalk = new TreeWalk(repository)) {
            treeWalk.addTree(commit.getTree());
            treeWalk.setRecursive(true);
            while (treeWalk.next()) {
                if (fProgressMonitor != null && fProgressMonitor.isCanceled()) {
                    throw new CanceledException(Messages.MergeConflictHandler_2);
                }
                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);
                File file = new File(tmpFolder, treeWalk.getPathString());
                file.getParentFile().mkdirs();
                try (FileOutputStream out = new FileOutputStream(file)) {
                    loader.copyTo(out);
                }
            }
        }
    }
    // Load it
    GraficoModelImporter importer = new GraficoModelImporter(tmpFolder);
    IArchimateModel model = importer.importAsModel();
    FileUtils.deleteFolder(tmpFolder);
    return model;
}
Also used : CanceledException(org.eclipse.jgit.api.errors.CanceledException) IArchiRepository(org.archicontribs.modelrepository.grafico.IArchiRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) FileOutputStream(java.io.FileOutputStream) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) GraficoModelImporter(org.archicontribs.modelrepository.grafico.GraficoModelImporter) IArchimateModel(com.archimatetool.model.IArchimateModel) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

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