Example usage for org.eclipse.jgit.lib Repository open

List of usage examples for org.eclipse.jgit.lib Repository open

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository open.

Prototype

@NonNull
public ObjectLoader open(AnyObjectId objectId) throws MissingObjectException, IOException 

Source Link

Document

Open an object from this repository.

Usage

From source file:facade.GitFacade.java

public static Map<String, COGClass> getCOGClassesFromCommit(Repository repository, ObjectId commitID)
        throws Exception {
    Map<String, COGClass> allClasses = new TreeMap<>();
    RevWalk revWalk = new RevWalk(repository);
    RevCommit commit = revWalk.parseCommit(commitID);
    RevTree tree = commit.getTree();/*from   w  ww  .j  a v  a  2  s. com*/

    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    treeWalk.setFilter(TreeFilter.ALL);

    classesInFileMap.clear();

    while (treeWalk.next()) {
        String extension = treeWalk.getPathString().substring(treeWalk.getPathLength() - 4);
        if (!extension.equals("java"))
            continue;

        //System.out.println(treeWalk.getPathString());
        ObjectId id = treeWalk.getObjectId(0);
        ObjectLoader loader = repository.open(id);
        Map<String, COGClass> classes = Source2ClassConverter.convertFromStream(loader.openStream());
        allClasses.putAll(classes);
        classesInFileMap.put(treeWalk.getPathString(), new LinkedList<>(classes.values()));
    }
    return allClasses;
}

From source file:facade.GitFacade.java

public static String getReadmeFromCommit(Repository repository, ObjectId commitID) throws Exception {
    RevWalk revWalk = new RevWalk(repository);
    RevCommit commit = revWalk.parseCommit(commitID);
    RevTree tree = commit.getTree();/*from www  . j ava  2s  .  c o  m*/

    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    treeWalk.setFilter(PathFilter.create("README.md"));

    if (treeWalk.next()) {
        ObjectId id = treeWalk.getObjectId(0);
        ObjectLoader loader = repository.open(id);
        BufferedReader reader = new BufferedReader(new InputStreamReader(loader.openStream()));
        StringBuilder builder = new StringBuilder();
        String line = reader.readLine();
        while (line != null) {
            builder.append(line + "\n");
            line = reader.readLine();
        }
        return builder.toString();
    }
    return "Could not find readme file!";

}

From source file:io.fd.maintainer.plugin.service.MaintainersProvider.java

License:Apache License

private String findMostRecentMaintainersChangeContent(final String maintainersFileName,
        final Repository repository, final RevWalk revWalk, final RevCommit headCommit) {
    LOG.info("Starting search at {}", headCommit);

    final RevCommit parent = getRevCommit(revWalk, headCommit.getParent(0).getId());
    LOG.info("Finding most recent maintainers file in {}", parent);

    final String parentIdName = parent.getId().getName();
    LOG.info("Parent id name {}", parentIdName);

    try (TreeWalk treeWalk = new TreeWalk(repository)) {
        treeWalk.addTree(parent.getTree());
        treeWalk.setRecursive(true);// w w w  .  j a  va 2  s. co m
        treeWalk.setFilter(PathFilter.create(maintainersFileName));
        LOG.info("Attempting to find {}", maintainersFileName);

        if (treeWalk.next()) {
            LOG.info("Maintainers file found in commit {}", parent.getId());
            ObjectId objectId = treeWalk.getObjectId(0);
            ObjectLoader loader = repository.open(objectId);

            // and then one can the loader to read the file
            final ByteArrayOutputStream out = new ByteArrayOutputStream();
            loader.copyTo(out);
            revWalk.dispose();
            return new String(out.toByteArray());
        }

        LOG.info("Maintainers file not found in commit {}, going deep", parent.getId());
        if (parent.getParents() == null) {
            throw new IllegalStateException(format("Root of branch reached with commit %s", parent));
        }
        return findMostRecentMaintainersChangeContent(maintainersFileName, repository, revWalk, parent);
    } catch (IOException e) {
        throw new IllegalStateException(format("Unable to detect maintainers file in %s", parent.getId()));
    }
}

From source file:jenkins.plugins.git.GitSCMFile.java

License:Open Source License

@NonNull
@Override//www .  j  a v a  2 s.  co  m
public InputStream content() throws IOException, InterruptedException {
    return fs.invoke(new GitSCMFileSystem.FSFunction<InputStream>() {
        @Override
        public InputStream invoke(Repository repository) throws IOException, InterruptedException {
            try (RevWalk walk = new RevWalk(repository)) {
                RevCommit commit = walk.parseCommit(fs.getCommitId());
                RevTree tree = commit.getTree();
                try (TreeWalk tw = TreeWalk.forPath(repository, getPath(), tree)) {
                    if (tw == null) {
                        throw new FileNotFoundException();
                    }
                    FileMode fileMode = tw.getFileMode(0);
                    if (fileMode == FileMode.MISSING) {
                        throw new FileNotFoundException();
                    }
                    if (fileMode == FileMode.TREE) {
                        throw new IOException("Directory");
                    }
                    ObjectId objectId = tw.getObjectId(0);
                    ObjectLoader loader = repository.open(objectId);
                    return new ByteArrayInputStream(loader.getBytes());
                }
            }
        }
    });
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.GitVcsFileContentProvider.java

License:Apache License

/**
 * Load object by blob ID/*www .j a  va  2  s .  c  o  m*/
 *
 * @param r    the repository
 * @param path the path (might be null)
 * @param id   the object id
 * @return the object's bytes
 * @throws IOException in case of IO problem
 */
private byte[] loadObject(@NotNull GitVcsRoot root, Repository r, String path, ObjectId id) throws IOException {
    final ObjectLoader loader = r.open(id);
    if (loader == null) {
        throw new IOException(
                "Unable to find blob " + id + (path == null ? "" : "(" + path + ")") + " in repository " + r);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream((int) loader.getSize());
    OutputStream output = root.isAutoCrlf() ? new AutoCRLFOutputStream(out) : out;
    loader.copyTo(output);
    output.flush();
    return out.toByteArray();
}

From source file:net.erdfelt.android.sdkfido.git.internal.GitInfo.java

License:Apache License

public static void infoObjectId(Repository db, ObjectId oid) throws IOException {
    System.out.printf("ObjectID: %s%n", oid.getName());
    ObjectLoader or = db.open(oid);
    if (or == null) {
        System.out.println("  Object not found!");
    }// w w  w. j av a2  s  .  c  o m
    System.out.printf("  .type: %s%n", asObjectType(or.getType()));
    System.out.printf("  .size: %,d%n", or.getSize());
}

From source file:net.erdfelt.android.sdkfido.git.internal.GitInfo.java

License:Apache License

public static String getObjectName(Repository repo, ObjectId objectId) {
    try {//w w  w . j a  v a 2s.  c om
        ObjectLoader loader = repo.open(objectId);
        StringBuilder ret = new StringBuilder();

        if (loader.isLarge()) {
            ret.append("LARGE! ");
        }

        switch (loader.getType()) {
        case Constants.OBJ_BAD:
            ret.append("BAD ");
            break;
        case Constants.OBJ_BLOB:
            ret.append("BLOB ");
            break;
        case Constants.OBJ_COMMIT:
            ret.append("COMMIT ");
            break;
        case Constants.OBJ_EXT:
            ret.append("EXT ");
            break;
        case Constants.OBJ_OFS_DELTA:
            ret.append("OFS_DELTA ");
            break;
        case Constants.OBJ_REF_DELTA:
            ret.append("REF_DELTA ");
            break;
        case Constants.OBJ_TAG:
            ret.append("TAG ");
            break;
        case Constants.OBJ_TREE:
            ret.append("TREE ");
            break;
        case Constants.OBJ_TYPE_5:
            ret.append("TYPE_5 ");
            break;
        default:
            ret.append("UNKNOWN[").append(loader.getType()).append("] ");
            break;
        }

        ret.append(String.format("Size=%,d", loader.getSize()));
        return ret.toString();
    } catch (MissingObjectException e) {
        LOG.log(Level.WARNING, "Unable to open objectId: " + objectId, e);
        return "<missing object>";
    } catch (IOException e) {
        LOG.log(Level.WARNING, "Unable to open objectId: " + objectId, e);
        return "<unable to open object>";
    }
}

From source file:net.morimekta.idltool.IdlUtils.java

License:Apache License

public static Meta getMetaInRegistry(Repository repo) throws IOException, GitAPIException {
    // use tree and meta.json file to show available services.
    ObjectId lastCommitId = repo.resolve(Constants.HEAD);
    // now we have to get the commit
    RevWalk revWalk = new RevWalk(repo);
    RevCommit commit = revWalk.parseCommit(lastCommitId);
    // and using commit's tree find the path
    RevTree tree = commit.getTree();/*  w w  w.j  a  va 2s.c  o m*/
    TreeWalk treeWalk = new TreeWalk(repo);

    treeWalk.addTree(tree);
    treeWalk.setRecursive(true);
    treeWalk.setFilter(PathFilter.create(Idl_Constants.META_JSON));
    if (!treeWalk.next()) {
        throw new RuntimeException("No registry meta file found, should be at "
                + new File(repo.getDirectory(), Idl_Constants.META_JSON).getCanonicalFile().getAbsolutePath());
    }
    ObjectId objectId = treeWalk.getObjectId(0);
    ObjectLoader loader = repo.open(objectId);

    // and then one can use either
    InputStream in = loader.openStream();

    return new JsonSerializer().deserialize(in, Meta.kDescriptor);
}

From source file:net.riezebos.thoth.content.impl.GitContentManager.java

License:Apache License

protected String getContents(Repository repository, ObjectId id)
        throws MissingObjectException, IOException, UnsupportedEncodingException {
    if (id == null || ObjectId.zeroId().equals(id))
        return "";

    ObjectLoader loader = repository.open(id);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    loader.copyTo(out);/* w  w  w  .j  ava2  s .  c  om*/
    byte[] byteArray = out.toByteArray();
    int length = ThothCoreUtil.charLength(byteArray);

    String body;
    if (length == -1)
        body = "<Binary contents>";
    else
        body = new String(byteArray, "UTF-8");
    return body;
}

From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepository.java

License:Open Source License

@Override
public InputStream getContent(String site, String path) throws ContentNotFoundException {
    InputStream toReturn = null;/*  ww  w  .j a  v a  2  s .  c om*/
    Repository repo = helper.getRepository(site,
            StringUtils.isEmpty(site) ? GitRepositories.GLOBAL : GitRepositories.SANDBOX);

    try {
        RevTree tree = helper.getTreeForLastCommit(repo);
        try (TreeWalk tw = TreeWalk.forPath(repo, helper.getGitPath(path), tree)) {
            // Check if the array of items is not null, and since we have an absolute path to the item,
            // pick the first item in the list
            if (tw != null && tw.getObjectId(0) != null) {
                ObjectId id = tw.getObjectId(0);
                ObjectLoader objectLoader = repo.open(id);
                toReturn = objectLoader.openStream();
                tw.close();
            }
        } catch (IOException e) {
            logger.error("Error while getting content for file at site: " + site + " path: " + path, e);
        }
    } catch (IOException e) {
        logger.error("Failed to create RevTree for site: " + site + " path: " + path, e);
    }

    return toReturn;
}