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

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

Introduction

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

Prototype

public ListBranchCommand branchList() 

Source Link

Document

Return a command object used to list branches

Usage

From source file:br.com.metricminer2.scm.GitRepository.java

License:Apache License

private void deleteMMBranch(Git git)
        throws GitAPIException, NotMergedException, CannotDeleteCurrentBranchException {
    List<Ref> refs = git.branchList().call();
    for (Ref r : refs) {
        if (r.getName().endsWith("mm")) {
            git.branchDelete().setBranchNames("mm").setForce(true).call();
            break;
        }/*from www  . j a  v  a  2s  . com*/
    }
}

From source file:ch.sbb.releasetrain.git.GitRepoImpl.java

License:Apache License

private boolean remoteBranchExists(final Git git) throws GitAPIException {
    List<Ref> branchRefs = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
    final String refsHeadBranch = "refs/remotes/origin/" + branch;
    for (Ref branchRef : branchRefs) {
        if (refsHeadBranch.equals(branchRef.getName())) {
            return true;
        }/*from  w  w w  . j  a v  a2  s.  c o  m*/
    }
    return false;
}

From source file:com.chungkwong.jgitgui.LocalTreeItem.java

License:Open Source License

public LocalTreeItem(Git git) throws GitAPIException {
    super(java.util.ResourceBundle.getBundle("com/chungkwong/jgitgui/text").getString("LOCAL BRANCH"));
    for (Ref ref : git.branchList().call())
        getChildren().add(new BranchTreeItem(ref));
}

From source file:com.denimgroup.threadfix.service.repository.GitServiceImpl.java

License:Mozilla Public License

@Override
public File cloneRepoToDirectory(Application application, File dirLocation) {

    if (dirLocation.exists()) {
        File gitDirectoryFile = new File(dirLocation.getAbsolutePath() + File.separator + ".git");

        try {//from   ww  w.java2s . c  o  m
            if (!gitDirectoryFile.exists()) {
                Git newRepo = clone(application, dirLocation);
                if (newRepo != null)
                    return newRepo.getRepository().getWorkTree();
            } else {
                Repository localRepo = new FileRepository(gitDirectoryFile);
                Git git = new Git(localRepo);

                if (application.getRepositoryRevision() != null
                        && !application.getRepositoryRevision().isEmpty()) {
                    //remote checkout
                    git.checkout().setCreateBranch(true).setStartPoint(application.getRepositoryRevision())
                            .setName(application.getRepositoryRevision()).call();
                } else {

                    List<Ref> refs = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();

                    String repoBranch = (application.getRepositoryBranch() != null
                            && !application.getRepositoryBranch().isEmpty()) ? application.getRepositoryBranch()
                                    : "master";

                    boolean localCheckout = false;

                    for (Ref ref : refs) {
                        String refName = ref.getName();
                        if (refName.contains(repoBranch) && !refName.contains(Constants.R_REMOTES)) {
                            localCheckout = true;
                        }
                    }

                    String HEAD = localRepo.getFullBranch();

                    if (HEAD.contains(repoBranch)) {
                        git.pull().setRemote("origin").setRemoteBranchName(repoBranch)
                                .setCredentialsProvider(getApplicationCredentials(application)).call();
                    } else {
                        if (localCheckout) {
                            //local checkout
                            git.checkout().setName(application.getRepositoryBranch()).call();
                            git.pull().setRemote("origin").setRemoteBranchName(repoBranch)
                                    .setCredentialsProvider(getApplicationCredentials(application)).call();
                        } else {
                            //remote checkout
                            git.checkout().setCreateBranch(true).setName(repoBranch)
                                    .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM)
                                    .setStartPoint("origin/" + repoBranch).call();
                        }
                    }
                }

                return git.getRepository().getWorkTree();
            }
        } catch (JGitInternalException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (IOException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (WrongRepositoryStateException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (InvalidConfigurationException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (DetachedHeadException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (InvalidRemoteException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (CanceledException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (RefNotFoundException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (NoHeadException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (RefAlreadyExistsException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (CheckoutConflictException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (InvalidRefNameException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (TransportException e) {
            log.error(EXCEPTION_MESSAGE, e);
        } catch (GitAPIException e) {
            log.error(EXCEPTION_MESSAGE, e);
        }
    } else {
        try {
            log.info("Attempting to clone application from repository.");
            Git result = clone(application, dirLocation);
            if (result != null) {
                log.info("Application was successfully cloned from repository.");
                return result.getRepository().getWorkTree();
            }
            log.error(EXCEPTION_MESSAGE);
        } catch (JGitInternalException e) {
            log.error(EXCEPTION_MESSAGE, e);
        }
    }
    return null;
}

From source file:com.GitAnalytics.BranchInfo.java

public static List<BranchInfo> getBranches(Git git, List<CommitInfo> allCommits) throws Exception {
    List<BranchInfo> list = new LinkedList<>();

    List<Ref> tags = git.tagList().call();

    for (Ref branch : git.branchList().call()) {
        LinkedList commits = new LinkedList<>();

        Set<RevCommit> branchCommits = new HashSet<>();
        for (RevCommit commit : git.log().add(branch.getObjectId()).call()) {
            branchCommits.add(commit);//from w w  w  .  ja v a 2  s.c  o m
        }

        allCommits.stream().filter((commit) -> (branchCommits.contains(commit.getCommit())))
                .forEachOrdered((commit) -> {
                    commits.add(commit);
                });

        list.add(new BranchInfo(branch, commits));
    }

    return list;
}

From source file:com.google.gct.idea.appengine.synchronization.SampleSyncTaskTest.java

License:Apache License

@Ignore
@Test//  w  ww  .j a  v  a 2s  .  c  om
public void testSync_noLocalRepo() throws IOException, GitAPIException {
    // Sync files from mock Git Hub repo to mock local Android sample template repo
    SampleSyncTask sampleSyncTask = new SampleSyncTask(mockAndroidRepoPath, mockGitHubRepoPath);
    sampleSyncTask.run();

    File mockAndroidRepoDir = new File(mockAndroidRepoPath);
    Assert.assertTrue(mockAndroidRepoDir.exists());

    Git mockAndroidRepo = Git.open(mockAndroidRepoDir);

    Assert.assertEquals("refs/heads/master", mockAndroidRepo.getRepository().getFullBranch());
    Assert.assertEquals(1,
            mockAndroidRepo.branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call().size());

    File mockGitHubRepoDir = new File(mockGitHubRepoPath);
    Assert.assertTrue(mockGitHubRepoDir.exists());

    File[] mockAndroidRepoFiles = mockAndroidRepoDir.listFiles();
    File[] mockGitHubRepoFiles = mockGitHubRepoDir.listFiles();

    Assert.assertEquals(mockGitHubRepoFiles.length, mockAndroidRepoFiles.length);

    int num = 0;
    for (File aFile : mockGitHubRepoFiles) {
        aFile.getName().equals(mockAndroidRepoFiles[0].getName());
        num++;
    }
}

From source file:com.maiereni.sling.sources.git.GitProjectDownloader.java

License:Apache License

@Override
public void doDownloadProject(final Repository repository) throws Exception {
    logger.debug("Download repository: " + repository.getUrl());
    String branchName = "master";
    Git git = getProjectRepository(repository, branchName);
    //Repository repo = git.getRepository();
    List<Ref> branches = git.branchList().call();
    Ref localBranch = null;//  w w w . j  a  v  a  2s.  c  om
    for (Ref branch : branches) {
        String bn = branch.getName();
        if (bn.equals("refs/heads/" + branchName)) {
            localBranch = branch;
            break;
        }
    }
    if (localBranch == null) {
        listener.notify(repository, "create", "Create branch");
        localBranch = git.checkout().setCreateBranch(true).setName("refs/heads/" + branchName)
                .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
                .setStartPoint("origin/" + branchName).call();
        logger.debug("Created a local clone of the project repository ");
    } else {
        listener.notify(repository, "update", "Update existing local sources");
        update(git);
    }
}

From source file:com.maiereni.synchronizer.git.utils.GitDownloaderImpl.java

License:Apache License

private GitResults downloadProject(final Git git, final GitProperties properties) throws Exception {
    GitResults ret = new GitResults();
    String refName = REF_HEADS + properties.getBranchName();
    if (StringUtils.isBlank(properties.getBranchName())) {
        refName = REF_HEADS_MASTER;/*  w ww .  java 2  s . c o  m*/
    }
    Ref tagRef = null;
    if (StringUtils.isNotBlank(properties.getTagName())) {
        String compTagName = "refs/tags/" + properties.getTagName();
        List<Ref> tags = git.tagList().call();
        for (Ref tag : tags) {
            String tn = tag.getName();
            if (tn.equals(compTagName)) {
                logger.debug("Download repository from: " + tag.getName());
                refName = tag.getName();
                tagRef = tag;
                break;
            }
        }
    }
    List<Ref> branches = git.branchList().call();
    Ref localBranch = null;
    for (Ref branch : branches) {
        String bn = branch.getName();
        if (bn.equals(REF_HEADS_LOCAL)) {
            localBranch = branch;
            break;
        }
    }
    if (localBranch == null) {
        localBranch = git.checkout().setCreateBranch(true).setName(LOCAL_BRANCH)
                .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).setStartPoint(refName).call();
        logger.debug("Created a local clone of the project repository ");
        ret.setCreated(true);
    } else {
        List<Change> updates = update(git, properties, localBranch, tagRef);
        ret.setChanges(updates);
    }
    return ret;
}

From source file:com.mortardata.git.GitUtil.java

License:Apache License

/**
 * Get all branches in a git repo./* w  ww . ja va2s.c o  m*/
 * 
 * @param git Git repository
 * 
 * @return List<Ref> all branches in the repo
 * @throws GitAPIException if error running git command
 */
public List<Ref> getBranches(Git git) throws GitAPIException {
    return git.branchList().setListMode(ListMode.ALL).call();
}

From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacadeTest.java

License:Open Source License

@Test
public void testBundleMultipleBranches() throws Exception {
    LocalRepoBean repoWithBranches = TestUtility.createTestRepoMultipleBranches();
    Path bundlePath = underTest.bundle(repoWithBranches);
    assertTrue(Files.exists(bundlePath));
    assertTrue(Files.isRegularFile(bundlePath));

    LocalRepoBean fromBundle = new LocalRepoBean();
    fromBundle.setCloneSourceURI(bundlePath.toString());
    fromBundle.setProjectKey(repoWithBranches.getProjectKey());
    fromBundle.setSlug(repoWithBranches.getSlug() + "_from_bundle");
    fromBundle.setLocalBare(true);/* w  w w  . ja v  a 2 s.co  m*/

    underTest.clone(fromBundle);

    FileRepositoryBuilder builder = new FileRepositoryBuilder();

    Repository originalRepo = builder.setGitDir(repoWithBranches.getGitConfigDirectory().toFile()).findGitDir()
            .build();
    Git original = new Git(originalRepo);
    List<Ref> originalBranches = original.branchList().call();

    Repository fromBundleRepo = builder.setGitDir(fromBundle.getGitConfigDirectory().toFile()).findGitDir()
            .build();
    Git fromBundleGit = new Git(fromBundleRepo);
    List<Ref> fromBundleBraches = fromBundleGit.branchList().call();

    int matches = 0;
    for (Ref branchRef : originalBranches) {
        for (Ref fromBundleRef : fromBundleBraches) {
            if (branchRef.getName().equals(fromBundleRef.getName())) {
                matches++;
                continue;
            }
        }
    }

    assertEquals(matches, originalBranches.size());

    TestUtility.destroyTestRepo(fromBundle);
    TestUtility.destroyTestRepo(repoWithBranches);
    Files.deleteIfExists(bundlePath);
}