RemoteRefUpdate remoteUpdate = pushResult.getRemoteUpdate(destinationBranch); if (remoteUpdate != null) { org.eclipse.jgit.transport.RemoteRefUpdate.Status status =
/** * @inheritDoc */ @Override public Iterable<PushResult> push(Git git, String username, String password, String reference, String remote, boolean force) { UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password); try { Iterable<PushResult> results = git .push() .setCredentialsProvider(credentialsProvider) .add(reference) .setRemote(remote) .setForce(force) .call(); for(PushResult result : results){ RemoteRefUpdate update = result.getRemoteUpdate(reference); RemoteRefUpdate.Status status = update.getStatus(); if(!(status.equals(RemoteRefUpdate.Status.OK)||status.equals(RemoteRefUpdate.Status.UP_TO_DATE))){ throw new RuntimeException("Failed to push changes to origin"); } } return results; } catch (GitAPIException e) { throw new RuntimeException("Failed to push to origin", e); } }
/** * Use with care! */ public boolean deleteBranch() { if (!branch.startsWith("feature/")) { throw new GitException("Can only delete feature branch."); } try { final Git git = gitOpen(); git.checkout().setCreateBranch(true).setName("feature/temp_" + UUID.randomUUID()).call(); List<String> deletedBranches = git.branchDelete().setBranchNames(branch).setForce(true).call(); if (deletedBranches.size() == 1) { // delete branch 'branchToDelete' on remote 'origin' RefSpec refSpec = new RefSpec().setSource(null).setDestination("refs/heads/" + branch); Iterable<PushResult> results = git.push().setCredentialsProvider(credentialsProvider()).setRefSpecs(refSpec).setRemote("origin").call(); for (PushResult result : results) { RemoteRefUpdate myUpdate = result.getRemoteUpdate("refs/heads/" + branch); if (myUpdate.isDelete() && myUpdate.getStatus() == RemoteRefUpdate.Status.OK) { return true; } } } return false; } catch (IOException | GitAPIException e) { throw new GitException("Delete branch failed", e); } }
.call(); for (PushResult result : results) { RemoteRefUpdate myUpdate = result.getRemoteUpdate("refs/heads/" + branch); if (myUpdate.isDelete() && myUpdate.getStatus() == RemoteRefUpdate.Status.OK) { return true;