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

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

Introduction

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

Prototype

@Nullable
public final Ref findRef(String name) throws IOException 

Source Link

Document

Search for a ref by (possibly abbreviated) name.

Usage

From source file:org.jboss.forge.addon.git.GitUtilsImpl.java

License:Open Source License

@Override
public CherryPickResult cherryPickNoMerge(final Git git, Ref src)
        throws GitAPIException, CantMergeCommitException {
    // Does the same as the original git-cherryPick
    // except commiting after running merger
    Repository repo = git.getRepository();

    RevCommit newHead = null;/*from  ww w .  j  av  a 2  s.c o  m*/
    List<Ref> cherryPickedRefs = new LinkedList<Ref>();

    try (RevWalk revWalk = new RevWalk(repo)) {
        // get the head commit
        Ref headRef = repo.findRef(Constants.HEAD);
        if (headRef == null)
            throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
        RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId());

        newHead = headCommit;

        // get the commit to be cherry-picked
        // handle annotated tags
        ObjectId srcObjectId = src.getPeeledObjectId();
        if (srcObjectId == null)
            srcObjectId = src.getObjectId();
        RevCommit srcCommit = revWalk.parseCommit(srcObjectId);

        // get the parent of the commit to cherry-pick
        if (srcCommit.getParentCount() == 0)
            throw new CantMergeCommitException("Commit with zero parents cannot be merged");

        if (srcCommit.getParentCount() > 1)
            throw new MultipleParentsNotAllowedException(
                    MessageFormat.format(JGitText.get().canOnlyCherryPickCommitsWithOneParent, srcCommit.name(),
                            Integer.valueOf(srcCommit.getParentCount())));

        RevCommit srcParent = srcCommit.getParent(0);
        revWalk.parseHeaders(srcParent);

        ResolveMerger merger = (ResolveMerger) MergeStrategy.RESOLVE.newMerger(repo);
        merger.setWorkingTreeIterator(new FileTreeIterator(repo));
        merger.setBase(srcParent.getTree());
        if (merger.merge(headCommit, srcCommit)) {
            DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.getTree(), repo.lockDirCache(),
                    merger.getResultTreeId());
            dco.setFailOnConflict(true);
            dco.checkout();

            cherryPickedRefs.add(src);
        } else {
            if (merger.failed())
                return new CherryPickResult(merger.getFailingPaths());

            // there are merge conflicts
            String message = new MergeMessageFormatter().formatWithConflicts(srcCommit.getFullMessage(),
                    merger.getUnmergedPaths());

            repo.writeCherryPickHead(srcCommit.getId());
            repo.writeMergeCommitMsg(message);

            return CherryPickResult.CONFLICT;
        }
    } catch (IOException e) {
        throw new JGitInternalException(
                MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e);
    }
    return new CherryPickResult(newHead, cherryPickedRefs);
}

From source file:org.komodo.storage.git.GitStorageConnector.java

License:Open Source License

@Override
public StorageTree<String> browse() throws Exception {
    cloneRepository();//ww w.  j a  v a  2s  .  c o m

    StorageTree<String> storageTree = new StorageTree<String>();

    Repository repository = git.getRepository();
    Ref head = repository.findRef(Constants.HEAD);

    try (RevWalk walk = new RevWalk(repository)) {
        RevCommit commit = walk.parseCommit(head.getObjectId());
        RevTree tree = commit.getTree();

        try (TreeWalk treeWalk = new TreeWalk(repository)) {
            treeWalk.addTree(tree);
            treeWalk.setRecursive(false);

            StorageParent<String> parent = storageTree;
            int currentDepth = 0;
            while (treeWalk.next()) {
                if (treeWalk.getDepth() < currentDepth) {
                    //
                    // Moved back up from a subtree so
                    // decrement currentDepth and
                    // change the parent to its parent
                    //
                    currentDepth = treeWalk.getDepth();
                    parent = parent.getParent();
                }

                StorageNode<String> child = parent.addChild(treeWalk.getNameString());

                if (treeWalk.isSubtree()) {
                    //
                    // Entering a subtree so change
                    // parent to the child and
                    // increment the currentDepth
                    //
                    parent = child;
                    treeWalk.enterSubtree();
                    currentDepth = treeWalk.getDepth();
                }
            }
        }
    }

    return storageTree;
}

From source file:org.omegat.convert.ConvertProject26to37team.java

License:Open Source License

/**
 * Get version of "omegat/project_save.tmx" in GIT.
 *///  w ww  .  j  a v  a  2s . c o m
private static String getGITTmxVersion(File wc) throws Exception {
    Repository repository = Git.open(wc).getRepository();
    try (RevWalk walk = new RevWalk(repository)) {
        Ref localBranch = repository.findRef("HEAD");
        RevCommit headCommit = walk.lookupCommit(localBranch.getObjectId());
        return headCommit.getName();
    }
}

From source file:org.repodriller.scm.GitRepository.java

License:Apache License

@Override
public String getCommitFromTag(String tag) {

    try (Git git = openRepository()) {
        Repository repo = git.getRepository();

        Iterable<RevCommit> commits = git.log().add(getActualRefObjectId(repo.findRef(tag), repo)).call();

        for (RevCommit commit : commits) {
            return commit.getName().toString();
        }//from   w ww  .j  a  v a  2 s  .  c om

        throw new RuntimeException("Failed for tag " + tag); // we never arrive here, hopefully

    } catch (Exception e) {
        throw new RuntimeException("Failed for tag " + tag, e);
    }
}

From source file:util.DifferenceComputer.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    ////*from   w  w w. java 2 s. c  o  m*/
    String coreRepoPath = "/Users/Onekin/Desktop/VODPlayer-CoreAssets-2";//args[0];
    String branchToLookInto = "develop.coreAssets";
    try {
        Git git = Git.open(new File(coreRepoPath));
        Repository repository = git.getRepository();
        Ref ref = repository.findRef(branchToLookInto);
        if (ref == null) {
            ref = git.checkout().setCreateBranch(true).setName(branchToLookInto)
                    .setUpstreamMode(SetupUpstreamMode.TRACK).setStartPoint("origin/" + branchToLookInto)
                    .call();
        }

        ObjectId parentCommit = repository
                .resolve(repository.findRef(branchToLookInto).getObjectId().getName() + "^^{commit}");//http://download.eclipse.org/jgit/site/4.2.0.201601211800-r/apidocs/org/eclipse/jgit/lib/Repository.html#resolve(java.lang.String)
        ObjectId currentCommit = repository
                .resolve(repository.findRef(branchToLookInto).getObjectId().getName() + "^{commit}");

        List<DiffEntry> listOfChangedFiles = getChangedFilesBetweenTwoCommits(coreRepoPath, currentCommit,
                parentCommit);
    } catch (Exception e) {
        e.getMessage();
        e.printStackTrace();
    }

}