List of usage examples for org.eclipse.jgit.api ListBranchCommand setListMode
public ListBranchCommand setListMode(ListMode listMode)
From source file:es.logongas.openshift.ant.impl.OpenShiftUtil.java
License:Apache License
private boolean existsBranch(String repositoryPath, String branchName) { try {//from w w w. j a v a2s .c om Git git = Git.open(new File(repositoryPath)); ListBranchCommand listBranchCommand = git.branchList(); listBranchCommand.setListMode(ListBranchCommand.ListMode.ALL); List<Ref> refs = listBranchCommand.call(); for (Ref ref : refs) { if (ref.getName().equals(branchName)) { return true; } } return false; } catch (IOException ex) { throw new RuntimeException(ex); } catch (GitAPIException ex) { throw new RuntimeException(ex); } }
From source file:org.ajoberstar.gradle.git.tasks.GitBranchList.java
License:Apache License
/** * Execute command to list branches./*www .ja v a2s.c o m*/ */ @TaskAction void branchList() { ListBranchCommand cmd = getGit().branchList(); if (getBranchType() != null) { switch (getBranchType()) { case REMOTE: cmd.setListMode(ListBranchCommand.ListMode.REMOTE); break; case ALL: cmd.setListMode(ListBranchCommand.ListMode.ALL); break; case LOCAL: break; //use default default: throw new AssertionError("Illegal branch type: " + getBranchType()); } } try { Repository repo = getGit().getRepository(); branches = new ArrayList<Branch>(); for (Ref ref : cmd.call()) { branches.add(GitUtil.refToBranch(repo, ref)); } workingBranch = GitUtil.gitNameToBranch(repo, repo.getFullBranch()); } catch (IOException e) { throw new GradleException("Problem listing branches", e); } catch (GitAPIException e) { throw new GradleException("Problem listing branches", e); } }
From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java
License:Open Source License
@Override public List<Branch> branchList(BranchListRequest request) throws GitException { String listMode = request.getListMode(); if (listMode != null && !BranchListRequest.LIST_ALL.equals(listMode) && !BranchListRequest.LIST_REMOTE.equals(listMode)) { throw new IllegalArgumentException(String.format(ERROR_UNSUPPORTED_LIST_MODE, listMode)); }/*from w w w . ja v a 2s.c o m*/ ListBranchCommand listBranchCommand = getGit().branchList(); if (BranchListRequest.LIST_ALL.equals(listMode)) { listBranchCommand.setListMode(ListMode.ALL); } else if (BranchListRequest.LIST_REMOTE.equals(listMode)) { listBranchCommand.setListMode(ListMode.REMOTE); } List<Ref> refs; String currentRef; try { refs = listBranchCommand.call(); String headBranch = getRepository().getBranch(); Optional<Ref> currentTag = getGit().tagList().call().stream() .filter(tag -> tag.getObjectId().getName().equals(headBranch)).findFirst(); if (currentTag.isPresent()) { currentRef = currentTag.get().getName(); } else { currentRef = "refs/heads/" + headBranch; } } catch (GitAPIException | IOException exception) { throw new GitException(exception.getMessage(), exception); } List<Branch> branches = new ArrayList<>(); for (Ref ref : refs) { String refName = ref.getName(); boolean isCommitOrTag = Constants.HEAD.equals(refName); String branchName = isCommitOrTag ? currentRef : refName; String branchDisplayName; if (isCommitOrTag) { branchDisplayName = "(detached from " + Repository.shortenRefName(currentRef) + ")"; } else { branchDisplayName = Repository.shortenRefName(refName); } Branch branch = newDto(Branch.class).withName(branchName) .withActive(isCommitOrTag || refName.equals(currentRef)).withDisplayName(branchDisplayName) .withRemote(refName.startsWith("refs/remotes")); branches.add(branch); } return branches; }
From source file:org.eclipse.recommenders.snipmatch.GitSnippetRepository.java
License:Open Source License
private String getCheckoutBranch(Git git) throws IOException, GitAPIException { ListBranchCommand branchList = git.branchList(); branchList.setListMode(ListMode.REMOTE); List<Ref> branches = branchList.call(); String formatVersion = FORMAT_VERSION.substring(FORMAT_PREFIX.length()); int version = Integer.parseInt(formatVersion); return getCheckoutBranch(branches, version); }
From source file:org.fedoraproject.eclipse.packager.git.FedoraPackagerGitCloneOperation.java
License:Open Source License
/** * Create local branches based on existing remotes (uses the JGit API). * /* www . j a v a2 s . c om*/ * @param monitor * @throws CoreException */ private void createLocalBranches(Git git, IProgressMonitor monitor) throws CoreException { monitor.beginTask(FedoraPackagerGitText.FedoraPackagerGitCloneWizard_createLocalBranchesJob, IProgressMonitor.UNKNOWN); try { // get a list of remote branches ListBranchCommand branchList = git.branchList(); branchList.setListMode(ListMode.REMOTE); // want all remote branches List<Ref> remoteRefs = branchList.call(); for (Ref remoteRef : remoteRefs) { String name = remoteRef.getName(); int index = (Constants.R_REMOTES + "origin/").length(); //$NON-NLS-1$ // Remove "refs/remotes/origin/" part in branch name name = name.substring(index); // Use "f14"-like branch naming if (name.endsWith("/" + Constants.MASTER)) { //$NON-NLS-1$ index = name.indexOf("/" + Constants.MASTER); //$NON-NLS-1$ name = name.substring(0, index); } // Create all remote branches, except "master" if (!name.equals(Constants.MASTER)) { CreateBranchCommand branchCreateCmd = git.branchCreate(); branchCreateCmd.setName(name); // Need to set starting point this way in order for tracking // to work properly. See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=333899 branchCreateCmd.setStartPoint(remoteRef.getName()); // Add remote tracking config in order to not confuse // fedpkg branchCreateCmd.setUpstreamMode(SetupUpstreamMode.TRACK); branchCreateCmd.call(); } } } catch (JGitInternalException e) { e.printStackTrace(); } catch (RefAlreadyExistsException e) { e.printStackTrace(); } catch (RefNotFoundException e) { e.printStackTrace(); } catch (InvalidRefNameException e) { e.printStackTrace(); } }
From source file:org.fedoraproject.eclipse.packager.git.GitUtils.java
License:Open Source License
/** * Create local branches based on existing remotes (uses the JGit API). * * @param git//from ww w.j a v a 2s . c o m * @param monitor * @throws CoreException */ public static void createLocalBranches(Git git, IProgressMonitor monitor) throws CoreException { monitor.beginTask(FedoraPackagerGitText.FedoraPackagerGitCloneWizard_createLocalBranchesJob, IProgressMonitor.UNKNOWN); try { // get a list of remote branches ListBranchCommand branchList = git.branchList(); branchList.setListMode(ListMode.REMOTE); // want all remote branches List<Ref> remoteRefs = branchList.call(); for (Ref remoteRef : remoteRefs) { String name = remoteRef.getName(); int index = (Constants.R_REMOTES + "origin/").length(); //$NON-NLS-1$ // Remove "refs/remotes/origin/" part in branch name name = name.substring(index); // Use "f14"-like branch naming if (name.endsWith("/" + Constants.MASTER)) { //$NON-NLS-1$ index = name.indexOf("/" + Constants.MASTER); //$NON-NLS-1$ name = name.substring(0, index); } // Create all remote branches, except "master" if (!name.equals(Constants.MASTER)) { CreateBranchCommand branchCreateCmd = git.branchCreate(); branchCreateCmd.setName(name); // Need to set starting point this way in order for tracking // to work properly. See: // https://bugs.eclipse.org/bugs/show_bug.cgi?id=333899 branchCreateCmd.setStartPoint(remoteRef.getName()); // Add remote tracking config in order to not confuse // fedpkg branchCreateCmd.setUpstreamMode(SetupUpstreamMode.TRACK); if (monitor.isCanceled()) { throw new OperationCanceledException(); } branchCreateCmd.call(); } } } catch (JGitInternalException e) { e.printStackTrace(); } catch (RefAlreadyExistsException e) { e.printStackTrace(); } catch (RefNotFoundException e) { e.printStackTrace(); } catch (InvalidRefNameException e) { e.printStackTrace(); } }
From source file:org.modeshape.connector.git.GitFunction.java
License:Apache License
/** * Add the names of the branches as children of the current node. * /*from www. ja v a2 s.c om*/ * @param git the Git object; may not be null * @param spec the call specification; may not be null * @param writer the document writer for the current node; may not be null * @throws GitAPIException if there is a problem accessing the Git repository */ protected void addBranchesAsChildren(Git git, CallSpecification spec, DocumentWriter writer) throws GitAPIException { Set<String> remoteBranchPrefixes = remoteBranchPrefixes(); if (remoteBranchPrefixes.isEmpty()) { // Generate the child references to the LOCAL branches, which will be sorted by name ... ListBranchCommand command = git.branchList(); List<Ref> branches = command.call(); // Reverse the sort of the branch names, since they might be version numbers ... Collections.sort(branches, REVERSE_REF_COMPARATOR); for (Ref ref : branches) { String name = ref.getName(); writer.addChild(spec.childId(name), name); } return; } // There is at least one REMOTE branch, so generate the child references to the REMOTE branches, // which will be sorted by name (by the command)... ListBranchCommand command = git.branchList(); command.setListMode(ListMode.REMOTE); List<Ref> branches = command.call(); // Reverse the sort of the branch names, since they might be version numbers ... Collections.sort(branches, REVERSE_REF_COMPARATOR); Set<String> uniqueNames = new HashSet<String>(); for (Ref ref : branches) { String name = ref.getName(); if (uniqueNames.contains(name)) continue; // We only want the branch if it matches one of the listed remotes ... boolean skip = false; for (String remoteBranchPrefix : remoteBranchPrefixes) { if (name.startsWith(remoteBranchPrefix)) { // Remove the prefix ... name = name.replaceFirst(remoteBranchPrefix, ""); break; } // Otherwise, it's a remote branch from a different remote that we don't want ... skip = true; } if (skip) continue; if (uniqueNames.add(name)) writer.addChild(spec.childId(name), name); } }
From source file:org.modeshape.connector.git.GitFunctionalTest.java
License:Apache License
@Test public void shouldGetBranchesWithLocalMode() throws Exception { // print = true; ListMode mode = null;//from ww w .j a va2 s.c o m ListBranchCommand command = git.branchList(); command.setListMode(mode); for (Ref ref : command.call()) { String fullName = ref.getName(); String name = fullName.replaceFirst("refs/heads/", ""); print(fullName + " \t--> " + name); } }
From source file:org.modeshape.connector.git.GitFunctionalTest.java
License:Apache License
@Test public void shouldGetBranchesWithAllMode() throws Exception { // print = true; ListMode mode = ListMode.ALL;/*w w w. ja v a 2 s . c o m*/ ListBranchCommand command = git.branchList(); command.setListMode(mode); for (Ref ref : command.call()) { print(ref.getName()); } }
From source file:org.springframework.cloud.config.server.environment.JGitEnvironmentRepository.java
License:Apache License
private boolean containsBranch(Git git, String label, ListMode listMode) throws GitAPIException { ListBranchCommand command = git.branchList(); if (listMode != null) { command.setListMode(listMode); }/*ww w . ja v a 2 s . c o m*/ List<Ref> branches = command.call(); for (Ref ref : branches) { if (ref.getName().endsWith("/" + label)) { return true; } } return false; }