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; }
/** * Return a command object used to list branches * * @return a {@link org.eclipse.jgit.api.ListBranchCommand} */ public ListBranchCommand branchList() { return new ListBranchCommand(repo); }
/** * Set the list mode * * @param listMode * optional: corresponds to the -r/-a options; by default, only * local branches will be listed * @return this instance */ public ListBranchCommand setListMode(ListMode listMode) { checkCallable(); this.listMode = listMode; return this; }
private List<Ref> getAllBranches() throws GitAPIException { return configRepo.git().branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); } }
@Signature public Memory branchList(ArrayMemory settings) throws GitAPIException { ListBranchCommand command = getWrappedObject().branchList(); if (settings != null) { Memory listMode = settings.valueOfIndex("listMode"); if (listMode.isNotNull()) { command.setListMode(ListBranchCommand.ListMode.valueOf(listMode.toString())); } Memory contains = settings.valueOfIndex("contains"); if (contains.isNotNull()) { command.setContains(contains.toString()); } } return GitUtils.valueOfRefs(command.call()); }
@Override public synchronized PullPolicyResult doPull(GitContext context, CredentialsProvider credentialsProvider, boolean allowVersionDelete) { Repository repository = git.getRepository(); StoredConfig config = repository.getConfig(); String remoteUrl = config.getString("remote", remoteRef, "url"); git.fetch().setTimeout(gitTimeout).setCredentialsProvider(credentialsProvider).setRemote(remoteRef).call(); } catch (GitAPIException | JGitInternalException ex) { lastException = ex; try { for (Ref ref : git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call()) { if (ref.getName().startsWith("refs/heads/")) { String name = ref.getName().substring(("refs/heads/").length()); localBranches.put(name, ref); allBranches.add(name); for (Ref ref : git.lsRemote().setCredentialsProvider(credentialsProvider).setTags(false).setRemote(remoteRef).setHeads(true).call()) { if (ref.getName().startsWith("refs/heads/")) { String name = ref.getName().substring(("refs/heads/").length()); remoteBranches.put(name, ref);
try (Git git = Git.cloneRepository() .setURI(REMOTE_URL) .setDirectory(localPath) .call()) { System.out.println("Having repository: " + git.getRepository().getDirectory()); FetchResult result = git.fetch().setCheckFetchedObjects(true).call(); System.out.println("Messages: " + result.getMessages()); List<Ref> call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); call = git.branchList().setListMode(ListMode.ALL).call(); for (Ref ref : call) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
public Long commitCountOnMaster() throws GitAPIException, IncorrectObjectTypeException, MissingObjectException { // not inside a doLocked/synchronized block because we don't want to block the server status service. // we do a `git branch` because we switch branches as part of normal git operations, // and we don't care about number of commits on those branches. List<Ref> branches = git.branchList().call(); for (Ref branch : branches) { if (branch.getName().equals("refs/heads/master")) { Iterable<RevCommit> commits = git.log().add(branch.getObjectId()).call(); long count = 0; for (RevCommit commit : commits) { count++; } return count; } } return Long.valueOf(-1); } }
if (!"master".equalsIgnoreCase(git.getRepository().getBranch())) { git.checkout() .setCreateBranch(true) .setName("master") if (!branch.equalsIgnoreCase(git.getRepository().getBranch())) { boolean create = true; for (Ref ref : git.branchList().call()) { if (ref.getName().equals("refs/heads/" + branch)) { create = false;
protected boolean localBranchExists(Git git, String branch) throws GitAPIException { List<Ref> list = git.branchList().call(); String fullName = "refs/heads/" + branch; boolean localBranchExists = false; for (Ref ref : list) { String name = ref.getName(); if (Objects.equals(name, fullName)) { localBranchExists = true; } } return localBranchExists; }
public static void main(String[] args) throws IOException, GitAPIException { try (Git git = new Git(repository)) { List<Ref> call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch-Before: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); List<Ref> refs = git.branchList().call(); for(Ref ref : refs) { System.out.println("Had branch: " + ref.getName()); if(ref.getName().equals("refs/heads/testbranch")) { System.out.println("Removing branch before"); .call(); call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch-Created: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); .call(); call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch-After: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { System.out.println("Listing local branches:"); try (Git git = new Git(repository)) { List<Ref> call = git.branchList().call(); for (Ref ref : call) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); } System.out.println("Now including remote branches:"); call = git.branchList().setListMode(ListMode.ALL).call(); for (Ref ref : call) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); } } } } }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Ref> refs = git.branchList().call(); for (Ref ref : refs) { System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); listReflog(repository, ref); } List<Ref> call = git.tagList().call(); for (Ref ref : call) { System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()); listReflog(repository, ref); } } } }
sbInfo.append("currently on branch : ").append(git.getRepository().getBranch()).append("\n"); } catch (IOException e) { log.warn("not able to get the current branch name..", e); sbInfo.append("repo state : ").append(git.getRepository().getRepositoryState().name()).append("\n"); Status status = git.status().call(); sbInfo.append("Added: ").append(status.getAdded()).append("\n"); sbInfo.append("Changed: ").append(status.getChanged()).append("\n"); List<Ref> call = git.branchList().call(); for (Ref ref : call) { sbInfo.append("\t - Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()).append("\n"); List<Ref> remoteRef = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call(); for (Ref ref : remoteRef) { sbInfo.append("\t - Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName()).append("\n");
@Test public void shouldSwitchToMasterAndDeleteTempBranches() throws Exception, GitAPIException { configRepo.checkin(goConfigRevision("v1", "md5-1")); configRepo.createBranch(ConfigRepository.BRANCH_AT_HEAD, configRepo.getCurrentRevCommit()); configRepo.createBranch(ConfigRepository.BRANCH_AT_REVISION, configRepo.getCurrentRevCommit()); configRepo.git().checkout().setName(ConfigRepository.BRANCH_AT_REVISION).call(); assertThat(configRepo.git().getRepository().getBranch(), is(ConfigRepository.BRANCH_AT_REVISION)); assertThat(configRepo.git().branchList().call().size(), is(3)); configRepo.cleanAndResetToMaster(); assertThat(configRepo.git().getRepository().getBranch(), is("master")); assertThat(configRepo.git().branchList().call().size(), is(1)); }
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { List<Ref> call = git.branchList().call(); for (Ref ref : call) { List<Integer> counts = getCounts(repository, ref.getName()); System.out.println("For branch: " + ref.getName()); System.out.println("Commits ahead : " + counts.get(0)); System.out.println("Commits behind : " + counts.get(1)); System.out.println(); } } } }
@Override public Iterable<PushResult> pushPatchBranches() throws GitAPIException { List<Ref> localBranches = mainRepository.branchList().call(); List<RefSpec> toPush = new LinkedList<>(); for (Ref ref : localBranches) { if (ref.getName().startsWith("refs/heads/patches-")) { toPush.add(new RefSpec(ref.getName())); } } return mainRepository.push().setRefSpecs(toPush).setPushTags().call(); }
private static void loadGitRepositoryData() throws Exception { try (Git git = Git.open(new File("../.."))) { // the tests expect a series of remote branches and tags from origin, so if they're not present in the clone // where the test is running, we need to load them... List<RefSpec> refsToFetch = new ArrayList<>(); List<String> tagNames = git.tagList().call().stream() .map(ref -> ref.getName().replace("refs/tags/", "")) .collect(Collectors.toList()); Arrays.stream(expectedTagNames()) .filter(tagName -> !tagNames.contains(tagName)) .map(tagName -> new RefSpec("+refs/tags/" + tagName + ":refs/remotes/origin/" + tagName)) .forEach(refsToFetch::add); List<String> branchNames = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call() .stream() .map(ref -> ref.getName() .replace("refs/heads/", "") .replace("refs/remotes/origin/", "")) .collect(Collectors.toList()); Arrays.stream(expectedRemoteBranchNames()) .filter(branchName -> !branchNames.contains(branchName)) .map(branchName -> new RefSpec("+refs/heads/" + branchName + ":refs/remotes/origin/" + branchName)) .forEach(refsToFetch::add); if (!refsToFetch.isEmpty()) { // next fetch all the remote refs which we need for the test git.fetch().setRefSpecs(refsToFetch).call(); } } }
private void deleteUnfilteredBranches(Repository repository) throws GitAPIException { final org.eclipse.jgit.api.Git git = org.eclipse.jgit.api.Git.wrap(repository); final String[] toDelete = git.branchList() .call() .stream() .map(ref -> ref.getName()) .map(fullname -> fullname.substring(fullname.lastIndexOf('/') + 1)) .filter(name -> !branches.contains(name)) .toArray(String[]::new); git.branchDelete() .setBranchNames(toDelete) .setForce(true) .call(); }
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()); } } }