Example usage for org.eclipse.jgit.lib ObjectId fromString

List of usage examples for org.eclipse.jgit.lib ObjectId fromString

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectId fromString.

Prototype

public static ObjectId fromString(String str) 

Source Link

Document

Convert an ObjectId from hex characters.

Usage

From source file:com.smartitengineering.version.impl.jgit.JGitImpl.java

License:Open Source License

public Resource getResourceByRevision(final String revisionId, final String resourceId) {
    try {/*from  w  ww  . j av a2  s .  c  om*/
        String trimmedResourceId = VersionAPI.trimToProperResourceId(resourceId);
        if (StringUtils.isBlank(trimmedResourceId)) {
            throw new IllegalArgumentException("Invalid resource id!");
        }
        ObjectId resourceObjectId;
        resourceObjectId = ObjectId.fromString(revisionId);
        return VersionAPI.createResource(trimmedResourceId,
                new String(readObject(ObjectId.toString(resourceObjectId))));
    } catch (Throwable ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.smartitengineering.version.impl.jgit.JGitImpl.java

License:Open Source License

public byte[] readObject(final String objectIdStr) throws IOException, IllegalArgumentException {
    if (StringUtils.isBlank(objectIdStr)) {
        throw new IllegalArgumentException("Invalid Object id!");
    }//from ww w .ja v  a 2s  .  c om
    ObjectId objectId = ObjectId.fromString(objectIdStr);
    ObjectLoader objectLoader = getReadRepository().openObject(objectId);
    if (objectLoader.getType() != Constants.OBJ_BLOB) {
        throw new IllegalArgumentException("Not a blob: " + objectIdStr);
    }
    return objectLoader.getBytes();
}

From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java

License:Open Source License

public static Blob getBlob(Repository r, String revision, String path)
        throws IOException, EntityNotFoundException {

    if (path.startsWith("/")) {
        path = path.substring(1);//from   w  ww.  j  av a  2s. co m
    }

    String id = resolve(r, r.resolve(revision), path);
    if (id == null) {
        throw new EntityNotFoundException();
    }
    ObjectId objectId = ObjectId.fromString(id);

    ObjectLoader loader = r.getObjectDatabase().open(objectId, Constants.OBJ_BLOB);

    Blob b = new Blob(id);

    if (loader.isLarge()) {
        b.setLarge(true);
        InputStream is = null;
        IOException ioex = null;
        try {
            is = loader.openStream();
            b.setBinary(RawText.isBinary(is));
        } catch (IOException ex) {
            ioex = ex;
        } finally {
            if (is != null) {
                is.close();
            }
            if (ioex != null) {
                throw ioex;
            }
        }

    } else {
        byte[] raw = loader.getBytes();

        boolean binary = RawText.isBinary(raw);

        if (binary) {
            b.setBinary(true);
            b.setLines(Collections.<String>emptyList());
        } else {

            RawText rt = new RawText(raw);
            List<String> lines = new ArrayList<String>(rt.size());

            for (int i = 0; i < rt.size(); i++) {
                lines.add(rt.getString(i));
            }

            b.setLines(lines);
        }
    }

    return b;
}

From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java

License:Open Source License

public static Item getItem(Repository r, String revision, String path)
        throws IOException, EntityNotFoundException {
    if (path.startsWith("/")) {
        path = path.substring(1);// w  ww  .  j  av a 2s.  co  m
    }

    String id = resolve(r, r.resolve(revision), path);
    if (id == null) {
        throw new EntityNotFoundException();
    }
    ObjectId objectId = ObjectId.fromString(id);

    ObjectLoader loader = r.open(objectId);

    return new Item(id, getType(loader.getType()));

}

From source file:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java

License:Open Source License

public static Commit resolveCommit(Repository r, String commitId) throws IOException {

    ObjectId oid = null;/*from   w w w .j  a v a 2s .co m*/
    Commit commit = new Commit();

    if (commitId.length() < 40) {
        ObjectReader or = r.newObjectReader();
        try {
            AbbreviatedObjectId aid = AbbreviatedObjectId.fromString(commitId);

            Collection<ObjectId> oids = or.resolve(aid);

            if (oids == null || oids.size() == 0) {
                commit.setSha("nex");
                return commit;
            } else if (oids.size() > 1) {
                commit.setSha("ambigous");
                return commit;
            } else {
                oid = oids.iterator().next();
            }
        } finally {
            or.release();
        }
    } else {
        oid = ObjectId.fromString(commitId);
    }

    RevWalk rw = new RevWalk(r);
    try {
        RevCommit rc = rw.parseCommit(oid);
        return GitDomain.createCommit(rc);
    } catch (MissingObjectException ex) {
        commit.setSha("nex");
        return commit;
    } catch (IncorrectObjectTypeException ex) {
        commit.setSha("nex");
        return commit;
    }

}

From source file:de.fau.osr.vcs.impl.GitBlameOperationTest.java

License:Open Source License

@SuppressWarnings("unchecked")
@Test//from   w w  w  .j a v  a2 s  . c o  m
public void testSimpleWordBlame() throws Exception {
    BiFunction<String, Integer, Iterator<Integer>> putBlame = (s, i) -> {
        if (s.compareTo("44ede09bf7dc2180f0ae7d614ae9fbccf549deaa") == 0)
            return Collections.singleton(1).iterator();
        return null;
    };
    AnnotatedWords w = new GitBlameOperation(git, "TestFile3", putBlame).wordBlame();
    ObjectId commit1 = ObjectId.fromString("bc87c2039d1e14d5fa0131d77780eaa3b2cc627c"),
            commit2 = ObjectId.fromString("44ede09bf7dc2180f0ae7d614ae9fbccf549deaa"),
            commit3 = ObjectId.fromString("d97ad6e826b473098b4396aa09c638487305c572"),
            commit4 = ObjectId.fromString("1c593122de47d31defa0648090a5cdfec1a5e43a");
    assertArrayEquals(new List[] { Lists.newArrayList(1, commit1), Lists.newArrayList(1, commit2),
            Lists.newArrayList(1, commit2), Lists.newArrayList(commit4), Lists.newArrayList(1, commit2),
            Lists.newArrayList(1, commit2), Lists.newArrayList(1, commit2), Collections.singletonList(commit3),
            Collections.singletonList(commit3), }, w.annotations);
    DataSource testDataSource = new DataSource() {

        @Override
        protected SetMultimap<String, String> doGetAllReqCommitRelations() throws IOException {
            return ImmutableSetMultimap.of("a", commit1.name(), "42", commit2.name(), "", commit3.name(),
                    "foobar", commit4.name());
        }

        @Override
        protected void doAddReqCommitRelation(String reqId, String commitId)
                throws IOException, OperationNotSupportedException {
            fail("add relation may not be called for lookup");
        }

        @Override
        protected void doRemoveReqCommitRelation(String reqId, String commitId)
                throws IOException, OperationNotSupportedException {
            fail("remove relation may not be called for lookup");
        }

    };
    List<AnnotatedLine> lines = AnnotatedLine.wordsToLine(w, testDataSource);
    assertEquals(Lists.newArrayList(new AnnotatedLine(Lists.newArrayList("a", "42"), "1 3"),
            new AnnotatedLine(Collections.emptyList(), ""),
            new AnnotatedLine(Lists.newArrayList("", "42"), "Test 2 100")), lines);
}

From source file:elegit.DiffHelper.java

License:Open Source License

private static AbstractTreeIterator prepareTreeParser(Repository repository, String objectId)
        throws IOException {
    // from the commit we can build the tree which allows us to construct the TreeParser
    try (RevWalk walk = new RevWalk(repository)) {
        RevCommit commit = walk.parseCommit(ObjectId.fromString(objectId));
        RevTree tree = walk.parseTree(commit.getTree().getId());

        CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
        try (ObjectReader oldReader = repository.newObjectReader()) {
            oldTreeParser.reset(oldReader, tree.getId());
        }/*from   ww  w  .j av a  2  s  .  com*/

        walk.dispose();
        return oldTreeParser;
    }
}

From source file:facade.GitFacade.java

private static AbstractTreeIterator prepareTreeParser(Repository repository, String commitId)
        throws IOException {
    RevWalk walk = new RevWalk(repository);
    RevCommit commit = walk.parseCommit(ObjectId.fromString(commitId));
    RevTree tree = walk.parseTree(commit.getTree().getId());

    CanonicalTreeParser treeParser = new CanonicalTreeParser();
    ObjectReader reader = repository.newObjectReader();
    treeParser.reset(reader, tree.getId());

    walk.dispose();//from w  w w  .j a  va 2s  .  c om
    return treeParser;
}

From source file:hudson.plugins.git.GitRevisionTokenMacroTest.java

License:Open Source License

@Test
public void testEvaluateMockBuildData() throws Exception {
    // Real test in GitSCMTest#testBasicRemotePoll
    Revision revision = new Revision(ObjectId.fromString("42ab63c2d69c012122d9b373450404244cc58e81"));
    BuildData buildData = Mockito.mock(BuildData.class);
    Mockito.when(buildData.getLastBuiltRevision()).thenReturn(revision);
    AbstractBuild build = Mockito.mock(AbstractBuild.class);
    Mockito.when(build.getAction(BuildData.class)).thenReturn(buildData);
    assertThat(tokenMacro.evaluate(build, TaskListener.NULL, "GIT_REVISION"), is(revision.getSha1String()));
}

From source file:hudson.plugins.git.GitRevisionTokenMacroTest.java

License:Open Source License

@Test
public void testEvaluateMockBuildDataLength() throws Exception {
    // Real test in GitSCMTest#testBasicRemotePoll
    Revision revision = new Revision(ObjectId.fromString("42ab63c2d69c012122d9b373450404244cc58e81"));
    BuildData buildData = Mockito.mock(BuildData.class);
    Mockito.when(buildData.getLastBuiltRevision()).thenReturn(revision);
    AbstractBuild build = Mockito.mock(AbstractBuild.class);
    Mockito.when(build.getAction(BuildData.class)).thenReturn(buildData);
    tokenMacro.length = 8;//  w  w  w  .j  a va  2  s  .  c o  m
    assertThat(tokenMacro.evaluate(build, TaskListener.NULL, "GIT_REVISION"),
            is(revision.getSha1String().substring(0, 8)));
}