Example usage for org.eclipse.jgit.api Git getRepository

List of usage examples for org.eclipse.jgit.api Git getRepository

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git getRepository.

Prototype

public Repository getRepository() 

Source Link

Document

Get repository

Usage

From source file:org.jboss.as.controller.persistence.AbstractGitPersistenceResourceTestCase.java

License:Apache License

private List<String> listCommits(Git git, String branchName) throws IOException, GitAPIException {
    List<String> commits = new ArrayList<>();
    for (RevCommit commit : git.log().add(git.getRepository().resolve(branchName)).call()) {
        commits.add(commit.getFullMessage());
    }/*www  . java  2 s  . c  o  m*/
    return commits;
}

From source file:org.jboss.as.controller.persistence.AbstractGitPersistenceResourceTestCase.java

License:Apache License

private List<String> listTags(Git git) throws IOException, GitAPIException {
    List<String> tags = new ArrayList<>();
    for (Ref tag : git.tagList().call()) {
        RevWalk revWalk = new RevWalk(git.getRepository());
        revWalk.sort(RevSort.COMMIT_TIME_DESC, true);
        try {/*w  w w  . j a  v  a 2 s  .  c o m*/
            RevTag annotatedTag = revWalk.parseTag(tag.getObjectId());
            tags.add(annotatedTag.getTagName() + " : " + annotatedTag.getFullMessage());
        } catch (IncorrectObjectTypeException ex) {
            tags.add(tag.getName().substring("refs/tags/".length()));
        }
    }
    return tags;
}

From source file:org.jboss.as.server.controller.git.GitRepository.java

License:Apache License

private void checkoutToSelectedBranch(final Git git) throws IOException, GitAPIException {
    boolean createBranch = !ObjectId.isId(branch);
    if (createBranch) {
        Ref ref = git.getRepository().exactRef(R_HEADS + branch);
        if (ref != null) {
            createBranch = false;/*from w  w  w .  ja  v  a 2 s .c o  m*/
        }
    }
    CheckoutCommand checkout = git.checkout().setCreateBranch(createBranch).setName(branch);
    checkout.call();
    if (checkout.getResult().getStatus() == CheckoutResult.Status.ERROR) {
        throw ServerLogger.ROOT_LOGGER.failedToPullRepository(null, defaultRemoteRepository);
    }
}

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

License:Open Source License

@Override
public Git init(final DirectoryResource dir) throws IOException {
    FileResource<?> gitDir = dir.getChildDirectory(GIT_DIRECTORY).reify(FileResource.class);
    gitDir.mkdirs();/* ww w  . j ava2s  .  c o  m*/

    RepositoryBuilder db = new RepositoryBuilder().setGitDir(gitDir.getUnderlyingResourceObject()).setup();
    Git git = new Git(db.build());
    git.getRepository().create();
    return git;
}

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

License:Open Source License

@Override
public String getCurrentBranchName(final Git repo) throws IOException {
    return repo.getRepository().getBranch();
}

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

License:Open Source License

@Override
public List<String> getLogForBranch(final Git repo, String branchName) throws GitAPIException, IOException {
    String oldBranch = repo.getRepository().getBranch();
    repo.checkout().setName(branchName).call();

    List<String> results = getLogForCurrentBranch(repo);

    repo.checkout().setName(oldBranch).call();

    return results;
}

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;//w  w w  .  j  a  va2 s  .  com
    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.jboss.forge.addon.git.GitUtilsTest.java

License:Open Source License

@Test
public void testCreateRepo() throws Exception {
    Project project = projectFactory.createTempProject();

    Git repo = gitUtils.init(project.getRoot().reify(DirectoryResource.class));
    Assert.assertTrue(repo.getRepository().getDirectory().exists());
}

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

License:Open Source License

@Test
public void testGetTags() throws Exception {
    Project project = projectFactory.createTempProject();

    Git repo = gitUtils.init(project.getRoot().reify(DirectoryResource.class));
    Map<String, Ref> tags = repo.getRepository().getTags();
    Assert.assertTrue(tags.isEmpty());//from  w  w  w  .  j  ava 2  s  . c o  m
}

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

License:Open Source License

@Test
public void shouldCherryPickChanges() throws Exception {
    // git init//w w  w  .j  a va 2 s.c  o  m
    // create new branch (b2) but stay on master
    // commit new file #1
    // switch to other branch
    // commit new file #2
    // switch to master
    // cherry pick the latest commit from b2
    // verify file #2 exists
    // verify number of commits (3 on master branch)

    String[] branchNames = { "master", "branch_two" };
    String[] files = { "test1.txt", "test2.txt" };

    Project project = projectFactory.createTempProject();
    Git repo = gitUtils.init(project.getRoot().reify(DirectoryResource.class));

    gitUtils.addAll(repo);
    gitUtils.commitAll(repo, "initial commit");

    repo.branchCreate().setName(branchNames[1]).call();

    FileResource<?> file0 = project.getRoot().getChild(files[0]).reify(FileResource.class);
    file0.createNewFile();
    gitUtils.add(repo, files[0]);
    gitUtils.commit(repo, "file added on " + branchNames[0]);

    gitUtils.switchBranch(repo, branchNames[1]);

    FileResource<?> file1 = project.getRoot().getChild(files[1]).reify(FileResource.class);
    file1.createNewFile();
    gitUtils.add(repo, files[1]);
    gitUtils.commit(repo, "file added on " + branchNames[1]);

    gitUtils.getLogForCurrentBranch(repo);

    gitUtils.switchBranch(repo, branchNames[0]);
    Ref branch2Ref = repo.getRepository().findRef(branchNames[1]);
    gitUtils.cherryPick(repo, branch2Ref);

    // assert file2 exists
    Assert.assertTrue("file from cherry picked commit should exist",
            project.getRoot().getChild(files[1]).exists());

    // assert number of commits (on master). Should be 3, latest created by the merge from cherry pick
    List<String> log = gitUtils.getLogForCurrentBranch(repo);
    Assert.assertEquals("wrong number of commits", 3, log.size());
}