public static void main(String[] args) throws IOException, GitAPIException { Repository repo = Commands.getRepo(Consts.META_REPO_PATH); Git git = new Git(repo); // Create a new branch git.branchCreate().setName("newBranch").call(); // Checkout the new branch git.checkout().setName("newBranch").call(); // List the existing branches List<Ref> listRefsBranches = git.branchList().setListMode(ListMode.ALL).call(); listRefsBranches.forEach((refBranch) -> { System.out.println("Branch : " + refBranch.getName()); }); // Go back on "master" branch and remove the created one git.checkout().setName("master"); git.branchDelete().setBranchNames("newBranch"); }
@Override public List<String> retreiveBranches() { List <String> receivedListAsString = new ArrayList<>(); List <Ref> receivedList; try { receivedList = _git.branchList().setListMode(ListMode.ALL).call(); for (Ref ref : receivedList) { receivedListAsString.add(ref.getName()); } } catch (GitAPIException e) { VerigreenLogger.get().log( getClass().getName(), RuntimeUtils.getCurrentMethodName(), "Failed to receive branches list"); return null; } return receivedListAsString; }
/** * Get a list of branches based on mode * @param mode * @return */ private List<String> getBranches( ListMode mode ) { try { return git.branchList().setListMode( mode ).call().stream() .filter( ref -> !ref.getName().endsWith( Constants.HEAD ) ) .map( ref -> Repository.shortenRefName( ref.getName() ) ) .collect( Collectors.toList() ); } catch ( Exception e ) { e.printStackTrace(); } return null; }
public static Ref getBranch(String repositoryPath,ObjectId id) throws IOException, GitAPIException{ Git git = new Git(new FileRepository(repositoryPath+"/.git")); ListBranchCommand list = git.branchList().setListMode(ListMode.ALL); List<Ref> call = list.call(); Ref ref = null; for(Ref r:call){ if(r.getObjectId().equals(id)){ ref = r; } } return ref; }
public void cloneAll(String host) throws InvalidRemoteException, TransportException, GitAPIException{ Git git = Git.cloneRepository().setURI(host).setNoCheckout(true).setCloneAllBranches(true).call(); List<Ref> branches = git.branchList().setListMode( ListMode.REMOTE ).call(); for(Ref r:branches){ System.out.println(r.getName()); } }
protected void doShowBranches(Exchange exchange, String operation) throws Exception { List<Ref> result = null; try { result = git.branchList().setListMode(ListMode.ALL).call(); } catch (Exception e) { LOG.error("There was an error in Git " + operation + " operation"); throw e; } exchange.getOut().setBody(result); }
private Revision getRevision(RevCommit commit, String filePath) throws GitAPIException, IOException { List<String> commitParentsList = Stream.of(commit.getParents()).map(RevCommit::getName).collect(Collectors.toList()); return newDto(Revision.class) .withId(commit.getId().getName()) .withMessage(commit.getFullMessage()) .withCommitTime((long) commit.getCommitTime() * 1000) .withCommitter(getCommitCommitter(commit)) .withAuthor(getCommitAuthor(commit)) .withBranches(getBranchesOfCommit(commit, ListMode.ALL)) .withCommitParent(commitParentsList) .withDiffCommitFile(getCommitDiffFiles(commit, filePath)); }
private List<Branch> getBranchesOfCommit(RevCommit commit, ListMode mode) throws GitAPIException { List<Ref> branches = getGit().branchList().setListMode(mode).setContains(commit.getName()).call(); return branches .stream() .map(branch -> newDto(Branch.class).withName(branch.getName())) .collect(toList()); }
@Override public void getBranches(final List<String> names, final List<String> commits) { try { for (final Ref ref : git.branchList().setListMode(ListMode.REMOTE).call()) { names.add(ref.getName()); commits.add(ref.getObjectId().getName()); } } catch (final GitAPIException e) { if (debug) System.err.println("Git Error reading branches: " + e.getMessage()); } }
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; }
@Override public Collection<BranchModel> getBranches() { try { return git.branchList() .setListMode(ListMode.ALL) .call().stream() .map(this::transformToBranchModel) .collect(Collectors.toList()); } catch (GitAPIException e) { throw new GitException(e.getMessage(), e); } }
@Override public Stream<VersionInfo> getBranches() throws IOException { try { return refToVersionInfo(repo.branchList().setListMode(ListMode.ALL).call().stream()); } catch (GitAPIException ex) { throw new IOException(ex); } }
@Override public Optional<VersionInfo> getDefaultBaseline() { try { return refToVersionInfo( repo.branchList() .setListMode(ListMode.ALL) .setContains("HEAD") .call() .stream()).findAny(); } catch (GitAPIException ex) { return Optional.empty(); } }
@Override public List<String> getBranches() { return getBranches( ListMode.ALL ); }
@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; }
@Override public boolean push(String sourceBranch, String destinationBranch) { PushCommand command = _git.push(); boolean ret = true; RefSpec refSpec = new RefSpec().setSourceDestination(sourceBranch, destinationBranch); command.setRefSpecs(refSpec); try { List<Ref> remoteBranches = _git.branchList().setListMode(ListMode.REMOTE).call(); Iterable<PushResult> results = command.call(); for (PushResult pushResult : results) { Collection<RemoteRefUpdate> resultsCollection = pushResult.getRemoteUpdates(); Map<PushResult,RemoteRefUpdate> resultsMap = new HashMap<>(); for(RemoteRefUpdate remoteRefUpdate : resultsCollection) { resultsMap.put(pushResult, remoteRefUpdate); } RemoteRefUpdate remoteUpdate = pushResult.getRemoteUpdate(destinationBranch); if (remoteUpdate != null) { org.eclipse.jgit.transport.RemoteRefUpdate.Status status = remoteUpdate.getStatus(); ret = status.equals(org.eclipse.jgit.transport.RemoteRefUpdate.Status.OK) || status.equals(org.eclipse.jgit.transport.RemoteRefUpdate.Status.UP_TO_DATE); } if(remoteUpdate == null && !remoteBranches.toString().contains(destinationBranch)) { for(RemoteRefUpdate resultValue : resultsMap.values()) { if(resultValue.toString().contains("REJECTED_OTHER_REASON")) { ret = false; } } } } } catch (Throwable e) { throw new RuntimeException(String.format( "Failed to push [%s] into [%s]", sourceBranch, destinationBranch), e); } return ret; }
private boolean isBranch(Git git, String label) throws GitAPIException { return containsBranch(git, label, ListMode.ALL); }
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; }