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

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

Introduction

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

Prototype

@Nullable
public ObjectId resolve(String revstr)
        throws AmbiguousObjectException, IncorrectObjectTypeException, RevisionSyntaxException, IOException 

Source Link

Document

Parse a git revision string and return an object id.

Usage

From source file:com.searchcode.app.service.GitService.java

/**
 * Given a repository location, revision and file path will retrieve that files contents. N.B. it returns the whole
 * file so you MAY end up running into serious memory issues, and should be aware of this
 *//*from   w  w w . ja  va  2 s .  c  o m*/
public String fetchFileRevision(String repoLocation, String revision, String filePath)
        throws MissingObjectException, IncorrectObjectTypeException, IOException {
    Repository localRepository = new FileRepository(new File(repoLocation));

    ObjectId id = localRepository.resolve(revision);
    ObjectReader reader = localRepository.newObjectReader();

    try {
        RevWalk walk = new RevWalk(reader);
        RevCommit commit = walk.parseCommit(id);
        RevTree tree = commit.getTree();
        TreeWalk treewalk = TreeWalk.forPath(reader, filePath, tree);

        if (treewalk != null) {
            byte[] data = reader.open(treewalk.getObjectId(0)).getBytes();
            return new String(data, "utf-8");
        } else {
            return "";
        }
    } finally {
        reader.close();
    }
}

From source file:com.spotify.docker.DockerBuildInformation.java

License:Apache License

private void updateGitInformation(Log log) {
    try {//from   w ww . j av  a 2  s. c om
        final Repository repo = new Git().getRepo();
        if (repo != null) {
            this.repo = repo.getConfig().getString("remote", "origin", "url");
            final ObjectId head = repo.resolve("HEAD");
            if (head != null && !isNullOrEmpty(head.getName())) {
                this.commit = head.getName();
            }
        }
    } catch (IOException e) {
        log.error("Failed to read Git information", e);
    }
}

From source file:com.spotify.docker.Utils.java

License:Apache License

public static String getGitCommitId()
        throws GitAPIException, DockerException, IOException, MojoExecutionException {

    final FileRepositoryBuilder builder = new FileRepositoryBuilder();
    builder.readEnvironment(); // scan environment GIT_* variables
    builder.findGitDir(); // scan up the file system tree

    if (builder.getGitDir() == null) {
        throw new MojoExecutionException("Cannot tag with git commit ID because directory not a git repo");
    }//  www .  jav a  2s .co m

    final StringBuilder result = new StringBuilder();
    final Repository repo = builder.build();

    try {
        // get the first 7 characters of the latest commit
        final ObjectId head = repo.resolve("HEAD");
        result.append(head.getName().substring(0, 7));
        final Git git = new Git(repo);

        // append first git tag we find
        for (Ref gitTag : git.tagList().call()) {
            if (gitTag.getObjectId().equals(head)) {
                // name is refs/tag/name, so get substring after last slash
                final String name = gitTag.getName();
                result.append(".");
                result.append(name.substring(name.lastIndexOf('/') + 1));
                break;
            }
        }

        // append '.DIRTY' if any files have been modified
        final Status status = git.status().call();
        if (status.hasUncommittedChanges()) {
            result.append(".DIRTY");
        }
    } finally {
        repo.close();
    }

    return result.length() == 0 ? null : result.toString();
}

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  w  w  .  ja v a  2  s.c  o  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 Blame getBlame(Repository r, String revision, String path) throws IOException, GitAPIException {

    if (path.startsWith("/")) {
        path = path.substring(1);/*  w  w  w.j a va2s.  com*/
    }

    Git git = new Git(r);

    ObjectId revId = r.resolve(revision);

    BlameCommand bc = git.blame();
    bc.setStartCommit(revId);
    bc.setFilePath(path);
    BlameResult br = bc.call();

    Blame blame = new Blame();
    blame.path = path;
    blame.revision = revision;
    blame.commits = new ArrayList<Commit>();
    blame.lines = new ArrayList<Blame.Line>();

    Map<String, Integer> sha2idx = new HashMap<String, Integer>();

    RawText resultContents = br.getResultContents();

    for (int i = 0; i < br.getResultContents().size(); i++) {

        RevCommit sourceCommit = br.getSourceCommit(i); // XXX should it really be the source commit
        String sha = sourceCommit.getName();
        Integer cIdx = sha2idx.get(sha);
        if (cIdx == null) {
            cIdx = blame.commits.size();
            Commit commit = GitDomain.createCommit(sourceCommit);
            blame.commits.add(commit);
            sha2idx.put(sha, cIdx);
        }

        Blame.Line bl = new Blame.Line();
        bl.commit = cIdx;
        bl.text = resultContents.getString(i);
        blame.lines.add(bl);
    }

    return blame;
}

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

License:Open Source License

public static List<Commit> getLog(Repository r, String revision, String path, Region region)
        throws IOException {
    if (path.startsWith("/")) {
        path = path.substring(1);//www.j  av a2  s . c om
    }

    List<Commit> commits = new ArrayList<Commit>();

    ObjectId revId = r.resolve(revision);
    RevWalk walk = new RevWalk(r);

    walk.markStart(walk.parseCommit(revId));

    if (path != null && path.trim().length() != 0) {
        walk.setTreeFilter(AndTreeFilter.create(PathFilterGroup.createFromStrings(path), TreeFilter.ANY_DIFF));
    }

    if (region != null) {
        // if (region.getOffset() > 0 && region.getSize() > 0)
        // walk.setRevFilter(AndRevFilter.create(SkipRevFilter.create(region.getOffset()),
        // MaxCountRevFilter.create(region.getSize())));
        /* else */if (region.getOffset() > 0) {
            walk.setRevFilter(SkipRevFilter.create(region.getOffset()));
        }
        // else if (region.getSize() > 0) {
        // walk.setRevFilter(MaxCountRevFilter.create(region.getSize()));
        // }
    }

    int max = region != null && region.getSize() > 0 ? region.getSize() : -1;

    for (RevCommit revCommit : walk) {
        Commit commit = GitDomain.createCommit(revCommit);
        commit.setRepository(r.getDirectory().getName());
        commits.add(commit);
        if (max != -1) {
            if (max == 0) {
                break;
            }
            max--;
        }
    }

    return commits;
}

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  w w. ja  va  2 s.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 getMergeBase(Repository r, String revA, String revB) throws IOException {

    RevWalk walk = new RevWalk(r);

    RevCommit a = walk.parseCommit(r.resolve(revA));
    RevCommit b = walk.parseCommit(r.resolve(revB));

    RevCommit base = getBaseCommit(walk, a, b);

    return GitDomain.createCommit(base);
}

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

License:Open Source License

public static RevObject resolveRevision(Repository r, String revision)
        throws EntityNotFoundException, IOException {

    RevWalk rw = null;/*from   w  w w  .j ava2s.c  om*/

    try {

        ObjectId oid = r.resolve(revision);

        if (oid == null) {
            throw new EntityNotFoundException("Revision: " + revision + " not found.");
        }

        rw = new RevWalk(r);

        RevObject ro = rw.parseCommit(oid);

        // if (ro.getType() == Constants.OBJ_TAG) {
        // return ((RevTag) ro).getObject();
        // }

        return ro;
    } finally {
        if (rw != null) {
            rw.release();
        }
    }

}

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

License:Open Source License

@Override
public List<com.tasktop.c2c.server.scm.domain.DiffEntry> getDiffEntries(final String repositoryName,
        final String baseCommitId, final String commitId, final Integer context)
        throws EntityNotFoundException {
    try {/*  w w w .  j  a  va  2s. c o  m*/
        final Repository jgitRepo = findRepositoryByName(repositoryName);
        final ObjectId commitObjectId = jgitRepo.resolve(commitId);
        final ObjectId baseCommitObjectId = baseCommitId != null ? jgitRepo.resolve(baseCommitId) : null;

        if (commitObjectId == null) {
            throw new EntityNotFoundException();
        }

        final RevWalk walk = new RevWalk(jgitRepo);
        final RevCommit baseCommit = baseCommitId != null ? walk.parseCommit(baseCommitObjectId) : null;
        return getDiffEntries(jgitRepo, baseCommit, walk.parseCommit(commitObjectId), context);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}