Search in sources :

Example 1 with HistoryPageInput

use of org.eclipse.egit.ui.internal.history.HistoryPageInput in project egit by eclipse.

the class AbstractHistoryCommandHandler method getRepository.

protected Repository getRepository(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = getSelection(event);
    if (!selection.isEmpty()) {
        IRepositoryCommit commit = AdapterUtils.adapt(selection.getFirstElement(), IRepositoryCommit.class);
        if (commit != null) {
            return commit.getRepository();
        }
    }
    Object input = getInput(event);
    if (input == null) {
        return null;
    }
    if (input instanceof HistoryPageInput) {
        return ((HistoryPageInput) input).getRepository();
    }
    if (input instanceof RepositoryTreeNode) {
        return ((RepositoryTreeNode) input).getRepository();
    }
    Repository repo = AdapterUtils.adapt(input, Repository.class);
    if (repo != null) {
        return repo;
    }
    IResource resource = AdapterUtils.adaptToAnyResource(input);
    if (resource != null) {
        Repository repository = ResourceUtil.getRepository(resource);
        if (repository != null) {
            return repository;
        }
    }
    throw new ExecutionException(UIText.AbstractHistoryCommanndHandler_CouldNotGetRepositoryMessage);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RepositoryTreeNode(org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode) HistoryPageInput(org.eclipse.egit.ui.internal.history.HistoryPageInput) IRepositoryCommit(org.eclipse.egit.core.internal.IRepositoryCommit) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ExecutionException(org.eclipse.core.commands.ExecutionException) IResource(org.eclipse.core.resources.IResource)

Example 2 with HistoryPageInput

use of org.eclipse.egit.ui.internal.history.HistoryPageInput in project egit by eclipse.

the class CompareVersionsInTreeHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = getSelection(event);
    if (selection.size() == 2) {
        Iterator<?> it = selection.iterator();
        RevCommit commit1 = (RevCommit) it.next();
        RevCommit commit2 = (RevCommit) it.next();
        HistoryPageInput pageInput = getPage(event).getInputInternal();
        Object input = pageInput.getSingleItem();
        Repository repository = pageInput.getRepository();
        IWorkbenchPage workBenchPage = HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
        // available in this case in the UI
        if (input instanceof IFile) {
            IFile resource = (IFile) input;
            final RepositoryMapping map = RepositoryMapping.getMapping(resource);
            if (map != null) {
                final String gitPath = map.getRepoRelativePath(resource);
                final String commit1Path = getRenamedPath(gitPath, commit1);
                final String commit2Path = getRenamedPath(gitPath, commit2);
                CompareUtils.openInCompare(commit1, commit2, commit1Path, commit2Path, map.getRepository(), workBenchPage);
            }
        } else if (input instanceof File) {
            File fileInput = (File) input;
            Repository repo = getRepository(event);
            final String gitPath = getRepoRelativePath(repo, fileInput);
            final String commit1Path = getRenamedPath(gitPath, commit1);
            final String commit2Path = getRenamedPath(gitPath, commit2);
            CompareUtils.openInCompare(commit1, commit2, commit1Path, commit2Path, repo, workBenchPage);
        } else if (input instanceof IResource) {
            CompareTreeView view;
            try {
                view = (CompareTreeView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(CompareTreeView.ID);
                view.setInput(new IResource[] { (IResource) input }, commit1.getId().name(), commit2.getId().name());
            } catch (PartInitException e) {
                Activator.handleError(e.getMessage(), e, true);
            }
        } else if (input == null) {
            CompareTreeView view;
            try {
                view = (CompareTreeView) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(CompareTreeView.ID);
                view.setInput(repository, commit1.getId().name(), commit2.getId().name());
            } catch (PartInitException e) {
                Activator.handleError(e.getMessage(), e, true);
            }
        }
    }
    return null;
}
Also used : IFile(org.eclipse.core.resources.IFile) HistoryPageInput(org.eclipse.egit.ui.internal.history.HistoryPageInput) CompareTreeView(org.eclipse.egit.ui.internal.dialogs.CompareTreeView) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with HistoryPageInput

use of org.eclipse.egit.ui.internal.history.HistoryPageInput in project egit by eclipse.

the class SelectionUtils method getMostFittingInput.

/**
 * Determines the most fitting {@link HistoryPageInput} for the given
 * {@link IStructuredSelection}. The {@code mandatoryObject} must be
 * contained in the selection and in a repository.
 * <p>
 * Most fitting means that the input will contain all selected resources
 * which are contained in the same repository as the given
 * {@code mandatoryObject}.
 * </p>
 *
 * @param selection
 *            The selection for which the most fitting HistoryPageInput is
 *            to be determined.
 * @param mandatoryObject
 *            The object to which the HistoryPageInput is tailored. Must be
 *            contained in the given selection and in a repository.
 * @return The most fitting HistoryPageInput. Will return {@code null} when
 *         the {@code mandatoryObject} is not contained in the given
 *         selection or in a repository.
 */
@Nullable
public static HistoryPageInput getMostFittingInput(@NonNull IStructuredSelection selection, Object mandatoryObject) {
    Set<IResource> resources = getSelectedResourcesSet(selection);
    if (!resources.contains(mandatoryObject)) {
        return null;
    }
    Repository repository = ResourceUtil.getRepository((IResource) mandatoryObject);
    if (repository == null) {
        return null;
    }
    for (Iterator<IResource> it = resources.iterator(); it.hasNext(); ) {
        IResource resource = it.next();
        if (ResourceUtil.getRepository(resource) != repository) {
            it.remove();
        }
    }
    IResource[] resourceArray = resources.toArray(new IResource[resources.size()]);
    return new HistoryPageInput(repository, resourceArray);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) HistoryPageInput(org.eclipse.egit.ui.internal.history.HistoryPageInput) IResource(org.eclipse.core.resources.IResource) Nullable(org.eclipse.jgit.annotations.Nullable)

Example 4 with HistoryPageInput

use of org.eclipse.egit.ui.internal.history.HistoryPageInput in project egit by eclipse.

the class RepositoriesView method getHistoryPageInput.

/**
 * @param selection
 * @return the HistoryPageInput corresponding to the selection, or null
 */
private static HistoryPageInput getHistoryPageInput(IStructuredSelection selection) {
    List<File> files = new ArrayList<>();
    Repository repo = null;
    for (Object element : selection.toList()) {
        Repository nodeRepository;
        if (element instanceof FileNode) {
            FileNode fileNode = (FileNode) element;
            files.add(fileNode.getObject());
            nodeRepository = fileNode.getRepository();
        } else if (element instanceof FolderNode) {
            FolderNode folderNode = (FolderNode) element;
            files.add(folderNode.getObject());
            nodeRepository = folderNode.getRepository();
        } else {
            // Don't return input if selection is not file/folder
            return null;
        }
        if (repo == null)
            repo = nodeRepository;
        // Don't return input if nodes from different repositories are selected
        if (repo != nodeRepository)
            return null;
    }
    if (repo != null)
        return new HistoryPageInput(repo, files.toArray(new File[files.size()]));
    else
        return null;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) FolderNode(org.eclipse.egit.ui.internal.repository.tree.FolderNode) HistoryPageInput(org.eclipse.egit.ui.internal.history.HistoryPageInput) ArrayList(java.util.ArrayList) File(java.io.File) FileNode(org.eclipse.egit.ui.internal.repository.tree.FileNode)

Example 5 with HistoryPageInput

use of org.eclipse.egit.ui.internal.history.HistoryPageInput in project dbeaver by dbeaver.

the class GITHistoryHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    Repository[] repos = this.getRepositories(event);
    if (ArrayUtils.isEmpty(repos)) {
        IResource[] selectedResources = getSelectedResources(event);
        if (ArrayUtils.isEmpty(selectedResources)) {
            UIUtils.showMessageBox(HandlerUtil.getActiveShell(event), "No repository", "You need to select a resource to view Git history", SWT.ICON_WARNING);
        } else {
            String resourceNames = Arrays.stream(selectedResources).map(r -> r.getFullPath().toString()).collect(Collectors.joining(","));
            UIUtils.showMessageBox(HandlerUtil.getActiveShell(event), "No repository", "No Git repository associated with selected resource(s):\n" + resourceNames, SWT.ICON_WARNING);
        }
        return null;
    } else {
        try {
            IWorkbenchWindow activeWorkbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
            if (activeWorkbenchWindow != null) {
                IWorkbenchPage page = activeWorkbenchWindow.getActivePage();
                if (page != null) {
                    IResource[] resources = this.getSelectedResources(event);
                    IHistoryView view = (IHistoryView) page.showView("org.eclipse.team.ui.GenericHistoryView");
                    if (resources.length == 1) {
                        view.showHistoryFor(resources[0]);
                        return null;
                    }
                    HistoryPageInput list = new HistoryPageInput(repos[0], resources);
                    view.showHistoryFor(list);
                }
            }
            return null;
        } catch (PartInitException var8) {
            throw new ExecutionException(var8.getMessage(), var8);
        }
    }
}
Also used : ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) IHistoryView(org.eclipse.team.ui.history.IHistoryView) Arrays(java.util.Arrays) PullOperationUI(org.eclipse.egit.ui.internal.pull.PullOperationUI) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) Set(java.util.Set) ExecutionException(org.eclipse.core.commands.ExecutionException) UIElement(org.eclipse.ui.menus.UIElement) ArrayUtils(org.jkiss.utils.ArrayUtils) HistoryPageInput(org.eclipse.egit.ui.internal.history.HistoryPageInput) Collectors(java.util.stream.Collectors) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) HandlerUtil(org.eclipse.ui.handlers.HandlerUtil) IProject(org.eclipse.core.resources.IProject) PartInitException(org.eclipse.ui.PartInitException) UIUtils(org.jkiss.dbeaver.ui.UIUtils) IResource(org.eclipse.core.resources.IResource) SWT(org.eclipse.swt.SWT) Map(java.util.Map) GitUIUtils(org.jkiss.dbeaver.team.git.ui.utils.GitUIUtils) IElementUpdater(org.eclipse.ui.commands.IElementUpdater) Repository(org.eclipse.jgit.lib.Repository) LinkedHashSet(java.util.LinkedHashSet) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) Repository(org.eclipse.jgit.lib.Repository) HistoryPageInput(org.eclipse.egit.ui.internal.history.HistoryPageInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException) ExecutionException(org.eclipse.core.commands.ExecutionException) IHistoryView(org.eclipse.team.ui.history.IHistoryView) IResource(org.eclipse.core.resources.IResource)

Aggregations

HistoryPageInput (org.eclipse.egit.ui.internal.history.HistoryPageInput)10 Repository (org.eclipse.jgit.lib.Repository)9 IResource (org.eclipse.core.resources.IResource)6 PartInitException (org.eclipse.ui.PartInitException)5 ExecutionException (org.eclipse.core.commands.ExecutionException)4 IHistoryView (org.eclipse.team.ui.history.IHistoryView)4 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)4 File (java.io.File)3 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)3 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)3 Arrays (java.util.Arrays)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ExecutionEvent (org.eclipse.core.commands.ExecutionEvent)2 IProject (org.eclipse.core.resources.IProject)2 PullOperationUI (org.eclipse.egit.ui.internal.pull.PullOperationUI)2 SWT (org.eclipse.swt.SWT)2 IElementUpdater (org.eclipse.ui.commands.IElementUpdater)2