public static ArrayList<String> getBranches(final File repo) { try { final List<Ref> refs = Git.open(repo) .branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call(); final ArrayList<String> result = new ArrayList<>(); for (Ref ref : refs) result.add(ref.getName()); return result; } catch (GitAPIException | IOException e) { e.printStackTrace(); } return new ArrayList<>(); }
/** * Clone GIT repository from sourceforge. * * @param gitDirectory The directory of Git. */ private void gitClone(File gitDirectory) { try { git = Git.cloneRepository() .setDirectory(gitDirectory) .setURI("http://git.code.sf.net/p/neembuuuploader/gitcode") .setProgressMonitor(new TextProgressMonitor()).call(); for (Ref f : git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call()) { git.checkout().setName(f.getName()).call(); System.out.println("checked out branch " + f.getName() + ". HEAD: " + git.getRepository().getRef("HEAD")); } // try to checkout branches by specifying abbreviated names git.checkout().setName("master").call(); } catch (GitAPIException | IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
/** * Clone GIT repository from sourceforge. * * @param gitDirectory The directory of Git. */ private void gitClone(File gitDirectory) { try { git = Git.cloneRepository() .setDirectory(gitDirectory) .setURI(env.gitURI()) //.setURI("http://git.code.sf.net/p/neembuuuploader/gitcode") .setProgressMonitor(new TextProgressMonitor()).call(); for (Ref f : git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call()) { git.checkout().setName(f.getName()).call(); System.out.println("checked out branch " + f.getName() + ". HEAD: " + git.getRepository().getRef("HEAD")); } // try to checkout branches by specifying abbreviated names git.checkout().setName("master").call(); } catch (GitAPIException | IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
List<GitBranch> findBranches() throws IOException { Repository repository = getRepository(); List<GitBranch> result = new LinkedList<>(); try (Git git = new Git(repository)) { List<Ref> branches = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); for (Ref branchRef : branches) { GitBranch newBranch = new GitBranch (branchRef.getName(), ObjectId.toString(branchRef.getObjectId())); result.add (newBranch); } } catch (GitAPIException e) { throw new IllegalStateException("Could not read branches from Git repository '" + path + "'", e); } finally { repository.close(); } return result; }
/** * Sets the listing mode * * @antdoc.notrequired * @param listMode - optional: corresponds to the -r/-a options; by default, only local branches will be listed */ public void setListMode(String listMode) { if (!GitTaskUtils.isNullOrBlankString(listMode)) { try { this.listMode = ListBranchCommand.ListMode.valueOf(listMode); } catch (IllegalArgumentException e) { ListBranchCommand.ListMode[] listModes = ListBranchCommand.ListMode.values(); List<String> listModeValidValues = new ArrayList<String>(listModes.length); for (ListBranchCommand.ListMode aListMode : listModes) { listModeValidValues.add(aListMode.name()); } String validModes = listModeValidValues.toString(); throw new BuildException(String.format("Valid listMode options are: %s.", validModes)); } } }
private boolean containsBranch(Git git, String label, ListBranchCommand.ListMode listMode) throws GitAPIException { ListBranchCommand command = git.branchList(); if (listMode != null) { command.setListMode(listMode); } List<Ref> branches = command.call(); for (Ref ref : branches) { if (ref.getName().endsWith("/" + label)) { return true; } } return false; }
private void checkoutAllBranches(Repository repository) throws GitAPIException { final Git git = Git.wrap(repository); for (final Ref ref : git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call()) { final String refName = ref.getName(); final String branchName = refName.substring(refName.lastIndexOf('/') + 1); try { git.checkout().setCreateBranch(true).setName(branchName).setStartPoint("origin/" + branchName).call(); } catch (RefAlreadyExistsException e) { LOGGER.warning("Already exists, so ignoring " + e.getMessage()); } } }
protected List<String> doListBranches(Git git) throws Exception { SortedSet<String> names = new TreeSet<String>(); List<Ref> call = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); for (Ref ref : call) { String name = ref.getName(); int idx = name.lastIndexOf('/'); if (idx >= 0) { name = name.substring(idx + 1); } if (name.length() > 0) { names.add(name); } } return new ArrayList<String>(names); }
/** * Determines whether to return local, remote, or all branches for {@code getBranches*()} related methods. * @param branchList the branch list command * @param branchType the type of branches to include */ private static void setBranchType(ListBranchCommand branchList, BranchType branchType) { if (branchType.equals(BranchType.LOCAL)) { // to get local branches, don't set list mode return; } if (branchType.equals(BranchType.REMOTE)) { branchList.setListMode(ListBranchCommand.ListMode.REMOTE); } else { branchList.setListMode(ListBranchCommand.ListMode.ALL); } }
private boolean containsBranch(Git git, String label, ListMode listMode) throws GitAPIException { ListBranchCommand command = git.branchList(); if (listMode != null) { command.setListMode(listMode); } List<Ref> branches = command.call(); for (Ref ref : branches) { if (ref.getName().endsWith("/" + label)) { return true; } } return false; }
/** * getBranches. * * @return a {@link java.util.Set} object. * @throws hudson.plugins.git.GitException if underlying git operation fails. */ public Set<Branch> getBranches() throws GitException { try (Repository repo = getRepository()) { List<Ref> refs = git(repo).branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); Set<Branch> branches = new HashSet<>(refs.size()); for (Ref ref : refs) { branches.add(new Branch(ref)); } return branches; } catch (GitAPIException e) { throw new GitException(e); } }
/** * getRemoteBranches. * * @return a {@link java.util.Set} object. * @throws hudson.plugins.git.GitException if underlying git operation fails. */ public Set<Branch> getRemoteBranches() throws GitException { try (Repository repo = getRepository()) { List<Ref> refs = git(repo).branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call(); Set<Branch> branches = new HashSet<>(refs.size()); for (Ref ref : refs) { branches.add(new Branch(ref)); } return branches; } catch (GitAPIException e) { throw new GitException(e); } }
private boolean isBranch(Git git, String label) throws GitAPIException { return containsBranch(git, label, ListBranchCommand.ListMode.ALL); }
public static List<Ref> getBranches(Git git, BranchType branchType) throws GitAPIException { ListBranchCommand branchList = git.branchList(); setBranchType(branchList, branchType); return branchList.call(); }
@Override public List<Branch> branchList(BranchListMode listMode) throws GitException { ListBranchCommand listBranchCommand = getGit().branchList(); if (LIST_ALL == listMode || listMode == null) { listBranchCommand.setListMode(ListMode.ALL); } else if (LIST_REMOTE == 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 = 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; }
public ListBranchCommand _branchList() { return git.branchList(); }
public List<String[]> call(final GitInfoStep gitInfoStep, Git git, CredentialsProvider cp, String gitRepoUrl, File gitRepoFolder) throws InvalidRemoteException, TransportException, GitAPIException, IllegalArgumentException, IOException { final List<String[]> branchs = new ArrayList<String[]>(); final ListBranchCommand ac = git .branchList() .setListMode( ("remote".equalsIgnoreCase(gitInfoStep .environmentSubstitute(this.listMode)) ? ListMode.REMOTE : ListMode.ALL)); final List<Ref> refBranchs = ac.call(); for (Ref refBranch : refBranchs) { final String[] branch = new String[] {null, null, null, null, null, null, null, null, null, null}; branch[0] = refBranch.getObjectId().getName(); // Id branch[1] = refBranch.getName(); // Name final RevObject object = new RevWalk(git.getRepository()) .parseAny(refBranch.getObjectId()); if (object instanceof RevCommit) { branch[2] = ((RevCommit) object).getFullMessage(); // Commit // message branch[3] = ((RevCommit) object).getShortMessage(); // Commit // message branch[4] = dt.format(((RevCommit) object).getAuthorIdent() .getWhen()); // Author Date branch[5] = ((RevCommit) object).getAuthorIdent().getName(); // Author // name branch[6] = ((RevCommit) object).getAuthorIdent() .getEmailAddress(); // Author email branch[7] = dt.format(((RevCommit) object).getCommitterIdent() .getWhen()); // Committer Date branch[8] = ((RevCommit) object).getCommitterIdent().getName(); // Committer // name branch[9] = ((RevCommit) object).getCommitterIdent() .getEmailAddress(); // Committer email } branchs.add(branch); } return branchs; }
private List<Ref> getAllBranches() throws GitAPIException { return configRepo.git().branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); }
private BranchInfo refreshBranchList(String selected) throws IOException, GitAPIException { branches.clear(); ListBranchCommand bl = git.branchList(); List<Ref> refs = bl.call(); String current = repository.getFullBranch(); BranchInfo selectedInfo = null; prompt = "Manage branches from <untracked>:"; for (Ref ref : refs) { BranchInfo info = new BranchInfo(); info.ref = ref; info.name = branchName(ref); if (ref.getName().equals(current)) { info.current = true; info.uncommitted = hasUncommitted(); currentInfo = info; } if (selected == null) { if (ref.getName().equals(current)) { selectedInfo = info; } } else { if (info.name.equals(selected)) { selectedInfo = info; } } info.diffbase = gt.getDiffbase(info.name); info.remote = gt.getRemote(info.name); Ref currentRef = repository.getRef(gt.refName(info.name)); Ref diffWithRef = repository.getRef(gt.refName(info.diffbase)); ObjectId currentHead = currentRef.getObjectId(); ObjectId diffWithHead = diffWithRef.getObjectId(); List<RevCommit> localCommits = ImmutableList.copyOf(git.log().addRange(diffWithHead, currentHead).call()); List<RevCommit> remoteCommits = ImmutableList.copyOf(git.log().addRange(currentHead, diffWithHead).call()); info.commits = localCommits.size(); info.missing = remoteCommits.size(); longestBranchName = Math.max(longestBranchName, CharUtil.printableWidth(info.name)); longestRemoteName = Math.max(longestRemoteName, CharUtil.printableWidth(info.diffbase)); branches.add(info); } Comparator<BranchInfo> comparator = (l, r) -> { if (l.name.equals(gt.getDefaultBranch())) { return -1; } if (r.name.equals(gt.getDefaultBranch())) { return 1; } return l.name.compareTo(r.name); }; branches.sort(comparator); return selectedInfo == null ? currentInfo : selectedInfo; }
/** * Gets the list of branches that contain the given commit * @param git the git repository * @param commit a commit ID or ref name * @return the list of branches with the given commit * @throws GitAPIException */ public static List<Ref> getBranchesWithCommit(Git git, BranchType branchType, String commit) throws GitAPIException { ListBranchCommand branchList = git.branchList(); setBranchType(branchList, branchType); branchList.setContains(commit); return branchList.call(); }
/** * Checks if given branch is remote. * * @param git * instance. * @param branch * to check. * @param remote * name. * * @return True if it is remote, false otherwise. */ public boolean isRemoteBranch(final Git git, final String branch, final String remote) { try { final List<Ref> refs = git.branchList() .setListMode(ListBranchCommand.ListMode.REMOTE).call(); final String remoteBranch = remote + "/" + branch; return refs.stream().anyMatch(ref -> ref.getName().endsWith(remoteBranch)); } catch (GitAPIException e) { throw new IllegalStateException(e); } }