Search in sources :

Example 1 with CannotDeleteCurrentBranchException

use of org.eclipse.jgit.api.errors.CannotDeleteCurrentBranchException in project egit by eclipse.

the class DeleteBranchOperation method execute.

@Override
public void execute(IProgressMonitor monitor) throws CoreException {
    IWorkspaceRunnable action = new IWorkspaceRunnable() {

        @Override
        public void run(IProgressMonitor actMonitor) throws CoreException {
            String taskName;
            if (branches.size() == 1)
                taskName = NLS.bind(CoreText.DeleteBranchOperation_TaskName, branches.iterator().next().getName());
            else {
                StringBuilder names = new StringBuilder();
                for (Iterator<Ref> it = branches.iterator(); it.hasNext(); ) {
                    Ref ref = it.next();
                    names.append(ref.getName());
                    if (it.hasNext())
                        // $NON-NLS-1$
                        names.append(", ");
                }
                taskName = NLS.bind(CoreText.DeleteBranchOperation_TaskName, names);
            }
            SubMonitor progress = SubMonitor.convert(actMonitor, taskName, branches.size());
            for (Ref branch : branches) {
                if (progress.isCanceled()) {
                    throw new OperationCanceledException(CoreText.DeleteBranchOperation_Canceled);
                }
                try (Git git = new Git(repository)) {
                    git.branchDelete().setBranchNames(branch.getName()).setForce(force).call();
                    status = OK;
                } catch (NotMergedException e) {
                    status = REJECTED_UNMERGED;
                    break;
                } catch (CannotDeleteCurrentBranchException e) {
                    status = REJECTED_CURRENT;
                    break;
                } catch (JGitInternalException e) {
                    throw new CoreException(Activator.error(e.getMessage(), e));
                } catch (GitAPIException e) {
                    throw new CoreException(Activator.error(e.getMessage(), e));
                }
                progress.worked(1);
            }
        }
    };
    // lock workspace to protect working tree changes
    ResourcesPlugin.getWorkspace().run(action, getSchedulingRule(), IWorkspace.AVOID_UPDATE, monitor);
}
Also used : IWorkspaceRunnable(org.eclipse.core.resources.IWorkspaceRunnable) NotMergedException(org.eclipse.jgit.api.errors.NotMergedException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) CoreException(org.eclipse.core.runtime.CoreException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) CannotDeleteCurrentBranchException(org.eclipse.jgit.api.errors.CannotDeleteCurrentBranchException)

Example 2 with CannotDeleteCurrentBranchException

use of org.eclipse.jgit.api.errors.CannotDeleteCurrentBranchException in project MGit by maks.

the class BranchChooserActivity method onActionItemClicked.

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_mode_rename_branch:
            Bundle pathArg = new Bundle();
            pathArg.putString(RenameBranchDialog.FROM_COMMIT, mChosenCommit);
            pathArg.putSerializable(Repo.TAG, mRepo);
            mode.finish();
            RenameBranchDialog rbd = new RenameBranchDialog();
            rbd.setArguments(pathArg);
            rbd.show(getFragmentManager(), "rename-dialog");
            return true;
        case R.id.action_mode_delete:
            AlertDialog.Builder alert = new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(getString(R.string.dialog_branch_delete) + " " + mChosenCommit).setMessage(R.string.dialog_branch_delete_msg).setPositiveButton(R.string.label_delete, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    int commitType = Repo.getCommitType(mChosenCommit);
                    try {
                        switch(commitType) {
                            case Repo.COMMIT_TYPE_HEAD:
                                mRepo.getGit().branchDelete().setBranchNames(mChosenCommit).setForce(true).call();
                                break;
                            case Repo.COMMIT_TYPE_TAG:
                                mRepo.getGit().tagDelete().setTags(mChosenCommit).call();
                                break;
                        }
                    } catch (StopTaskException e) {
                        Log.e(LOGTAG, "can't delete " + mChosenCommit, e);
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(BranchChooserActivity.this, getString(R.string.cannot_delete_branch, mChosenCommit), Toast.LENGTH_LONG).show();
                            }
                        });
                    } catch (CannotDeleteCurrentBranchException e) {
                        Log.e(LOGTAG, "can't delete " + mChosenCommit, e);
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(BranchChooserActivity.this, getString(R.string.cannot_delete_current_branch, mChosenCommit), Toast.LENGTH_LONG).show();
                            }
                        });
                    } catch (GitAPIException e) {
                        Log.e(LOGTAG, "can't delete " + mChosenCommit, e);
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(BranchChooserActivity.this, getString(R.string.cannot_delete_branch, mChosenCommit), Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                    refreshList();
                }
            }).setNegativeButton(R.string.label_cancel, null);
            mode.finish();
            alert.show();
            return true;
        default:
            return false;
    }
}
Also used : AlertDialog(android.app.AlertDialog) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RenameBranchDialog(me.sheimi.sgit.dialogs.RenameBranchDialog) DialogInterface(android.content.DialogInterface) Bundle(android.os.Bundle) StopTaskException(me.sheimi.sgit.exception.StopTaskException) CannotDeleteCurrentBranchException(org.eclipse.jgit.api.errors.CannotDeleteCurrentBranchException)

Aggregations

CannotDeleteCurrentBranchException (org.eclipse.jgit.api.errors.CannotDeleteCurrentBranchException)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Bundle (android.os.Bundle)1 RenameBranchDialog (me.sheimi.sgit.dialogs.RenameBranchDialog)1 StopTaskException (me.sheimi.sgit.exception.StopTaskException)1 IWorkspaceRunnable (org.eclipse.core.resources.IWorkspaceRunnable)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 Git (org.eclipse.jgit.api.Git)1 JGitInternalException (org.eclipse.jgit.api.errors.JGitInternalException)1 NotMergedException (org.eclipse.jgit.api.errors.NotMergedException)1 Ref (org.eclipse.jgit.lib.Ref)1