Example usage for org.eclipse.jgit.lib Repository getRepositoryState

List of usage examples for org.eclipse.jgit.lib Repository getRepositoryState

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository getRepositoryState.

Prototype

@NonNull
public RepositoryState getRepositoryState() 

Source Link

Document

Get the repository state

Usage

From source file:org.eclipse.egit.ui.internal.actions.CommitActionHandler.java

License:Open Source License

public Object execute(final ExecutionEvent event) throws ExecutionException {
    // let's see if there is any dirty editor around and
    // ask the user if they want to save or abort
    if (!PlatformUI.getWorkbench().saveAllEditors(true)) {
        return null;
    }/*from www.  j ava  2 s.  c  om*/

    resetState();
    final IProject[] projects = getProjectsInRepositoryOfSelectedResources(event);
    try {
        PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() {

            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                try {
                    buildIndexHeadDiffList(projects, monitor);
                } catch (IOException e) {
                    throw new InvocationTargetException(e);
                }
            }
        });
    } catch (InvocationTargetException e) {
        Activator.handleError(UIText.CommitAction_errorComputingDiffs, e.getCause(), true);
        return null;
    } catch (InterruptedException e) {
        return null;
    }

    Repository[] repos = getRepositoriesFor(getProjectsForSelectedResources(event));
    Repository repository = null;
    Repository mergeRepository = null;
    amendAllowed = repos.length == 1;
    boolean isMergedResolved = false;
    for (Repository repo : repos) {
        repository = repo;
        RepositoryState state = repo.getRepositoryState();
        if (!state.canCommit()) {
            MessageDialog.openError(getShell(event), UIText.CommitAction_cannotCommit,
                    NLS.bind(UIText.CommitAction_repositoryState, state.getDescription()));
            return null;
        } else if (state.equals(RepositoryState.MERGING_RESOLVED)) {
            isMergedResolved = true;
            mergeRepository = repo;
        }
    }

    loadPreviousCommit(event);
    if (files.isEmpty()) {
        if (amendAllowed && previousCommit != null) {
            boolean result = MessageDialog.openQuestion(getShell(event), UIText.CommitAction_noFilesToCommit,
                    UIText.CommitAction_amendCommit);
            if (!result)
                return null;
            amending = true;
        } else {
            MessageDialog.openWarning(getShell(event), UIText.CommitAction_noFilesToCommit,
                    UIText.CommitAction_amendNotPossible);
            return null;
        }
    }

    String author = null;
    String committer = null;
    if (repository != null) {
        final UserConfig config = repository.getConfig().get(UserConfig.KEY);
        author = config.getAuthorName();
        final String authorEmail = config.getAuthorEmail();
        author = author + " <" + authorEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$

        committer = config.getCommitterName();
        final String committerEmail = config.getCommitterEmail();
        committer = committer + " <" + committerEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$
    }

    CommitDialog commitDialog = new CommitDialog(getShell(event));
    commitDialog.setAmending(amending);
    commitDialog.setAmendAllowed(amendAllowed);
    commitDialog.setFileList(files, indexDiffs);
    commitDialog.setPreselectedFiles(getSelectedFiles(event));
    commitDialog.setAuthor(author);
    commitDialog.setCommitter(committer);
    commitDialog.setAllowToChangeSelection(!isMergedResolved);

    if (previousCommit != null) {
        commitDialog.setPreviousCommitMessage(previousCommit.getFullMessage());
        PersonIdent previousAuthor = previousCommit.getAuthorIdent();
        commitDialog
                .setPreviousAuthor(previousAuthor.getName() + " <" + previousAuthor.getEmailAddress() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    if (isMergedResolved) {
        commitDialog.setCommitMessage(getMergeResolveMessage(mergeRepository, event));
    }

    if (commitDialog.open() != IDialogConstants.OK_ID)
        return null;

    final CommitOperation commitOperation = new CommitOperation(commitDialog.getSelectedFiles(), notIndexed,
            notTracked, commitDialog.getAuthor(), commitDialog.getCommitter(), commitDialog.getCommitMessage());
    if (commitDialog.isAmending()) {
        commitOperation.setAmending(true);
        commitOperation.setPreviousCommit(previousCommit);
        commitOperation.setRepos(repos);
    }
    commitOperation.setComputeChangeId(commitDialog.getCreateChangeId());
    commitOperation.setCommitAll(isMergedResolved);
    if (isMergedResolved)
        commitOperation.setRepos(repos);
    String jobname = UIText.CommitAction_CommittingChanges;
    Job job = new Job(jobname) {
        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                commitOperation.execute(monitor);

                for (IProject proj : getProjectsForSelectedResources(event)) {
                    RepositoryMapping.getMapping(proj).fireRepositoryChanged();
                }
            } catch (CoreException e) {
                return Activator.createErrorStatus(UIText.CommitAction_CommittingFailed, e);
            } catch (ExecutionException e) {
                return Activator.createErrorStatus(UIText.CommitAction_CommittingFailed, e);
            } finally {
                GitLightweightDecorator.refresh();
            }
            return Status.OK_STATUS;
        }

        @Override
        public boolean belongsTo(Object family) {
            if (family.equals(JobFamilies.COMMIT))
                return true;
            return super.belongsTo(family);
        }

    };
    job.setUser(true);
    job.schedule();
    return null;
}

From source file:org.eclipse.egit.ui.internal.actions.DiscardChangesAction.java

License:Open Source License

@Override
public boolean isEnabled() {
    for (IResource res : getSelectedResources()) {
        IProject[] proj = new IProject[] { res.getProject() };
        Repository repository = getRepositoriesFor(proj)[0];
        if (!repository.getRepositoryState().equals(RepositoryState.SAFE)) {
            return false;
        }//from   w  ww  .  j  av a  2  s. c  om
    }
    return true;
}

From source file:org.eclipse.egit.ui.internal.actions.DiscardChangesActionHandler.java

License:Open Source License

@Override
public boolean isEnabled() {
    for (IResource res : getSelectedResources()) {
        IProject[] proj = new IProject[] { res.getProject() };
        Repository[] repositories = getRepositoriesFor(proj);
        if (repositories.length == 0)
            return false;
        Repository repository = repositories[0];
        if (!repository.getRepositoryState().equals(RepositoryState.SAFE)) {
            return false;
        }/* w ww.j  a v  a  2s  . c  o m*/
    }
    return true;
}

From source file:org.eclipse.egit.ui.internal.actions.MergeAction.java

License:Open Source License

private boolean canMerge(final Repository repository) {
    String message = null;/*from www .ja va2  s  . co  m*/
    try {
        Ref head = repository.getRef(Constants.HEAD);
        if (head == null || !head.isSymbolic())
            message = UIText.MergeAction_HeadIsNoBranch;
        else if (!repository.getRepositoryState().equals(RepositoryState.SAFE))
            message = NLS.bind(UIText.MergeAction_WrongRepositoryState, repository.getRepositoryState());
    } catch (IOException e) {
        Activator.logError(e.getMessage(), e);
        message = e.getMessage();
    }

    if (message != null) {
        MessageDialog.openError(getShell(), UIText.MergeAction_CannotMerge, message);
    }
    return (message == null);
}

From source file:org.eclipse.egit.ui.internal.actions.MergeActionHandler.java

License:Open Source License

@Override
public boolean isEnabled() {
    Repository repo = getRepository();
    return repo != null && repo.getRepositoryState() == RepositoryState.SAFE;
}

From source file:org.eclipse.egit.ui.internal.actions.RebaseAction.java

License:Open Source License

private boolean isInRebasingState(Repository repo) {
    if (repo == null)
        return false;

    RepositoryState state = repo.getRepositoryState();
    return state.isRebasing();
}

From source file:org.eclipse.egit.ui.internal.actions.RebaseActionHandler.java

License:Open Source License

@Override
public boolean isEnabled() {
    Repository repo = getRepository();
    if (repo == null)
        return false;

    // Either we want this to be enabled because a new rebase can be started
    // (main action) or an active rebase can be continued, skipped or
    // aborted (menu items). Even when the main action is not enabled we
    // must enable this because otherwise the menu items can not be opened.
    RepositoryState state = repo.getRepositoryState();
    return state.isRebasing() || RebaseCurrentRefCommand.isEnabledForState(repo, state);
}

From source file:org.eclipse.egit.ui.internal.actions.RepositoryActionHandler.java

License:Open Source License

/**
 * Checks if merge is possible://w  ww  . j  a v  a  2s.com
 * <ul>
 * <li>HEAD must point to a branch</li>
 * <li>Repository State must be SAFE</li>
 * </ul>
 *
 * @param repository
 * @param event
 * @return a boolean indicating if merge is possible
 * @throws ExecutionException
 */
protected boolean canMerge(final Repository repository, ExecutionEvent event) throws ExecutionException {
    String message = null;
    try {
        Ref head = repository.getRef(Constants.HEAD);
        if (head == null || !head.isSymbolic())
            message = UIText.MergeAction_HeadIsNoBranch;
        else if (!repository.getRepositoryState().equals(RepositoryState.SAFE))
            message = NLS.bind(UIText.MergeAction_WrongRepositoryState, repository.getRepositoryState());
    } catch (IOException e) {
        Activator.logError(e.getMessage(), e);
        message = e.getMessage();
    }

    if (message != null) {
        MessageDialog.openError(getShell(event), UIText.MergeAction_CannotMerge, message);
    }
    return (message == null);
}

From source file:org.eclipse.egit.ui.internal.actions.ResetAction.java

License:Open Source License

@Override
public void execute(IAction action) {
    final Repository repository = getRepository(true);
    if (repository == null)
        return;//  w  w  w  .  j  av  a2  s  .  com
    if (!repository.getRepositoryState().canResetHead()) {
        MessageDialog.openError(getShell(), UIText.ResetAction_errorResettingHead,
                NLS.bind(UIText.ResetAction_repositoryState, repository.getRepositoryState().getDescription()));
        return;
    }
    ResetTargetSelectionDialog branchSelectionDialog = new ResetTargetSelectionDialog(getShell(), repository);
    if (branchSelectionDialog.open() == IDialogConstants.OK_ID) {
        final String refName = branchSelectionDialog.getRefName();
        final ResetType type = branchSelectionDialog.getResetType();
        String jobname = NLS.bind(UIText.ResetAction_reset, refName);
        final ResetOperation operation = new ResetOperation(repository, refName, type);
        Job job = new Job(jobname) {
            @Override
            protected IStatus run(IProgressMonitor monitor) {
                try {
                    operation.execute(monitor);
                    GitLightweightDecorator.refresh();
                } catch (CoreException e) {
                    return Activator.createErrorStatus(e.getStatus().getMessage(), e);
                }
                return Status.OK_STATUS;
            }
        };
        job.setRule(operation.getSchedulingRule());
        job.setUser(true);
        job.schedule();
    }
}

From source file:org.eclipse.egit.ui.internal.actions.ResetActionHandler.java

License:Open Source License

public Object execute(ExecutionEvent event) throws ExecutionException {
    final Repository repository = getRepository(true, event);
    if (repository == null)
        return null;
    if (!repository.getRepositoryState().canResetHead()) {
        MessageDialog.openError(getShell(event), UIText.ResetAction_errorResettingHead,
                NLS.bind(UIText.ResetAction_repositoryState, repository.getRepositoryState().getDescription()));
        return null;
    }//w  w w. j a v a2s.co  m
    ResetTargetSelectionDialog branchSelectionDialog = new ResetTargetSelectionDialog(getShell(event),
            repository);
    if (branchSelectionDialog.open() == IDialogConstants.OK_ID) {
        final String refName = branchSelectionDialog.getRefName();
        final ResetType type = branchSelectionDialog.getResetType();
        String jobname = NLS.bind(UIText.ResetAction_reset, refName);
        final ResetOperation operation = new ResetOperation(repository, refName, type);
        Job job = new Job(jobname) {
            @Override
            protected IStatus run(IProgressMonitor monitor) {
                try {
                    operation.execute(monitor);
                    GitLightweightDecorator.refresh();
                } catch (CoreException e) {
                    return Activator.createErrorStatus(e.getStatus().getMessage(), e);
                }
                return Status.OK_STATUS;
            }
        };
        job.setRule(operation.getSchedulingRule());
        job.setUser(true);
        job.schedule();
    }
    return null;
}