Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete remote branch- bug 575 #644

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 51 additions & 60 deletions src/main/java/elegit/controllers/SessionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1499,73 +1499,64 @@ public synchronized void deleteBranch(BranchHelper selectedBranch) {
}
}

synchronized void deleteRemoteBranch(BranchHelper selectedBranch, BranchModel branchModel, Consumer<String> updateFn) {
try {
final RepoHelperBuilder.AuthDialogResponse credentialResponse = askUserForCredentials();
private List<Result> deleteRemoteBranchDetails(Optional<RepoHelperBuilder.AuthDialogResponse> responseOptional, BranchHelper selectedBranch,
BranchModel branchModel, Consumer<String> updateFn) {
Main.assertNotFxThread();
synchronized (globalLock) {
List<Result> results = new ArrayList<>();
try {
RepoHelper helper = theModel.getCurrentRepoHelper();
responseOptional.ifPresent(response ->
helper.setOwnerAuth(
new UsernamePasswordCredentialsProvider(response.username, response.password))
);
RemoteRefUpdate.Status deleteStatus = branchModel.deleteRemoteBranch((RemoteBranchHelper) selectedBranch);
String updateMessage = selectedBranch.getRefName();
switch (deleteStatus) {
case OK:
updateMessage += " deleted.";
break;
case NON_EXISTING:
updateMessage += " no longer\nexists on the server.\nFetch -p to remove " + updateMessage;
default:
updateMessage += " deletion\nfailed.";
}
updateFn.accept(updateMessage);

showBusyWindow("Deleting remote branch...");
RemoteBranchHelper remote = (RemoteBranchHelper) selectedBranch;
commandLineController.updateCommandText("git push origin --delete " + remote.parseBranchName());

Thread th = new Thread(new Task<Void>() {
@Override
protected Void call() {
tryCommandAgainWithHTTPAuth = false;
try {
deleteRemoteBranchDetails(credentialResponse, selectedBranch, branchModel, updateFn);
} catch (TransportException e) {
determineIfTryAgain(e);
} finally {
BusyWindow.hide();
}
} catch (TransportException e) {
results.add(new Result(ResultStatus.EXCEPTION, ResultOperation.DELETE, e));
}
catch (GitAPIException e) {
logger.warn("IO error");
this.showGenericErrorNotification(e);
}
return Collections.unmodifiableList(results);
}
}

if (tryCommandAgainWithHTTPAuth) {
Platform.runLater(() -> {
deleteRemoteBranch(selectedBranch, branchModel, updateFn);
});
}

return null;
}
});
th.setDaemon(true);
th.setName("Git delete remote branch");
th.start();

} catch (CancelledAuthorizationException e) {
this.showCommandCancelledNotification();
}
public void deleteRemoteBranch(BranchHelper selectedBranch, BranchModel branchModel, Consumer<String> updateFn) {
GitOperation gitOp = authResponse -> deleteRemoteBranchDetails(authResponse, selectedBranch, branchModel, updateFn);
Single
.fromCallable(() -> {
showBusyWindow("Deleting remote branch...");
RemoteBranchHelper remote = (RemoteBranchHelper) selectedBranch;
commandLineController.updateCommandText("git push origin --delete " + remote.parseBranchName());
return true;
})

// Note that the below is a threaded operation, and so we want to make sure that the following
// operations (hiding the window, etc) depend on it.
.flatMap(unused -> doGitOperationWhenSubscribed(gitOp))
.doOnSuccess(unused -> hideBusyWindowAndResumeRepoMonitor())
.flatMap(unused -> doGitStatusWhenSubscribed())
.subscribe(unused -> {
}, Throwable::printStackTrace);
}

private void deleteRemoteBranchDetails(RepoHelperBuilder.AuthDialogResponse response, BranchHelper selectedBranch,
BranchModel branchModel, Consumer<String> updateFn) throws TransportException {

try {
if (response != null) {
theModel.getCurrentRepoHelper().setOwnerAuth(
new UsernamePasswordCredentialsProvider(response.username, response.password));
}
RemoteRefUpdate.Status deleteStatus = branchModel.deleteRemoteBranch((RemoteBranchHelper) selectedBranch);
String updateMessage = selectedBranch.getRefName();
// There are a number of possible cases, see JGit's documentation on RemoteRefUpdate.Status
// for the full list.
switch (deleteStatus) {
case OK:
updateMessage += " deleted.";
break;
case NON_EXISTING:
updateMessage += " no longer\nexists on the server.\nFetch -p to remove " + updateMessage;
default:
updateMessage += " deletion\nfailed.";
}
updateFn.accept(updateMessage);
} catch (TransportException e) {
throw e;
} catch (GitAPIException e) {
logger.warn("IO error");
this.showGenericErrorNotification(e);
}
}


/**
Expand Down Expand Up @@ -1831,7 +1822,7 @@ public void handlePullButton() {
}

public enum ResultStatus {OK, NOCOMMITS, EXCEPTION, MERGE_FAILED, CONFLICTING};
public enum ResultOperation {FETCH, MERGE, ADD, LOAD, PUSH, CHECK_REMOTE_FOR_CHANGES, RESET, REVERT};
public enum ResultOperation {FETCH, MERGE, ADD, LOAD, PUSH, CHECK_REMOTE_FOR_CHANGES, RESET, REVERT, DELETE};

public static class Result {
public final ResultStatus status;
Expand Down