Search in sources :

Example 36 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class RepositoryActionHandler method getHeadCommit.

protected RevCommit getHeadCommit(IResource resource) throws IOException {
    Repository repository = getRepository();
    if (resource == null) {
        return null;
    }
    RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
    if (mapping == null) {
        return null;
    }
    String path = mapping.getRepoRelativePath(resource);
    if (path == null) {
        return null;
    }
    try (RevWalk rw = new RevWalk(repository)) {
        rw.sort(RevSort.COMMIT_TIME_DESC, true);
        rw.sort(RevSort.BOUNDARY, true);
        if (path.length() > 0) {
            DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
            FollowFilter filter = FollowFilter.create(path, diffConfig);
            rw.setTreeFilter(filter);
        }
        Ref head = repository.findRef(Constants.HEAD);
        if (head == null) {
            return null;
        }
        RevCommit headCommit = rw.parseCommit(head.getObjectId());
        rw.close();
        return headCommit;
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) FollowFilter(org.eclipse.jgit.revwalk.FollowFilter) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffConfig(org.eclipse.jgit.diff.DiffConfig) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 37 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class RepositoryActionHandler method findPreviousCommits.

/**
 * Returns the previous commit of the given resources.
 *
 * @param resources
 *            The {@link IResource} for which the previous commit shall be
 *            determined.
 * @return The second to last commit which touched any of the given
 *         resources.
 * @throws IOException
 *             When the commit can not be parsed.
 */
protected List<RevCommit> findPreviousCommits(Collection<IResource> resources) throws IOException {
    List<RevCommit> result = new ArrayList<>();
    Repository repository = getRepository();
    RepositoryMapping mapping = RepositoryMapping.getMapping(resources.iterator().next().getProject());
    if (mapping == null) {
        return result;
    }
    try (RevWalk rw = new RevWalk(repository)) {
        rw.sort(RevSort.COMMIT_TIME_DESC, true);
        rw.sort(RevSort.BOUNDARY, true);
        List<TreeFilter> filters = new ArrayList<>();
        DiffConfig diffConfig = repository.getConfig().get(DiffConfig.KEY);
        for (IResource resource : resources) {
            String path = mapping.getRepoRelativePath(resource);
            if (path != null && path.length() > 0) {
                filters.add(FollowFilter.create(path, diffConfig));
            }
        }
        if (filters.size() >= 2) {
            TreeFilter filter = OrTreeFilter.create(filters);
            rw.setTreeFilter(filter);
        } else if (filters.size() == 1) {
            rw.setTreeFilter(filters.get(0));
        }
        Ref head = repository.findRef(Constants.HEAD);
        if (head == null) {
            return result;
        }
        RevCommit headCommit = rw.parseCommit(head.getObjectId());
        rw.markStart(headCommit);
        headCommit = rw.next();
        if (headCommit == null)
            return result;
        List<RevCommit> directParents = Arrays.asList(headCommit.getParents());
        RevCommit previousCommit = rw.next();
        while (previousCommit != null && result.size() < directParents.size()) {
            if (directParents.contains(previousCommit)) {
                result.add(previousCommit);
            }
            previousCommit = rw.next();
        }
        rw.dispose();
    }
    return result;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) ArrayList(java.util.ArrayList) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) RevWalk(org.eclipse.jgit.revwalk.RevWalk) DiffConfig(org.eclipse.jgit.diff.DiffConfig) IResource(org.eclipse.core.resources.IResource) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 38 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class ShowBlameActionHandler method execute.

/**
 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
 */
@Override
public Object execute(final ExecutionEvent event) throws ExecutionException {
    IStructuredSelection selection = getSelection(event);
    if (selection.size() != 1) {
        return null;
    }
    Object element = selection.getFirstElement();
    IResource resource = AdapterUtils.adapt(element, IResource.class);
    if (resource instanceof IFile) {
        RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
        if (mapping != null) {
            String repoRelativePath = mapping.getRepoRelativePath(resource);
            Shell shell = HandlerUtil.getActiveShell(event);
            IWorkbenchPage page = HandlerUtil.getActiveSite(event).getPage();
            JobUtil.scheduleUserJob(new BlameOperation(mapping.getRepository(), (IFile) resource, repoRelativePath, null, shell, page), UIText.ShowBlameHandler_JobName, JobFamilies.BLAME);
        }
    } else if (element instanceof CommitFileRevision) {
        Shell shell = HandlerUtil.getActiveShell(event);
        IWorkbenchPage page = HandlerUtil.getActiveSite(event).getPage();
        JobUtil.scheduleUserJob(new BlameOperation((CommitFileRevision) element, shell, page), UIText.ShowBlameHandler_JobName, JobFamilies.BLAME);
    }
    return null;
}
Also used : CommitFileRevision(org.eclipse.egit.core.internal.storage.CommitFileRevision) Shell(org.eclipse.swt.widgets.Shell) IFile(org.eclipse.core.resources.IFile) BlameOperation(org.eclipse.egit.ui.internal.blame.BlameOperation) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IResource(org.eclipse.core.resources.IResource)

Example 39 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class SynchronizeWithMenu method fill.

@Override
public void fill(final Menu menu, int index) {
    if (srv == null)
        return;
    final IResource selectedResource = getSelection();
    if (selectedResource == null || selectedResource.isLinked(IResource.CHECK_ANCESTORS))
        return;
    RepositoryMapping mapping = RepositoryMapping.getMapping(selectedResource.getProject());
    if (mapping == null)
        return;
    final Repository repo = mapping.getRepository();
    if (repo == null)
        return;
    List<Ref> refs = new LinkedList<>();
    RefDatabase refDatabase = repo.getRefDatabase();
    try {
        refs.addAll(refDatabase.getAdditionalRefs());
    } catch (IOException e) {
    // do nothing
    }
    try {
        refs.addAll(refDatabase.getRefs(RefDatabase.ALL).values());
    } catch (IOException e) {
    // do nothing
    }
    Collections.sort(refs, CommonUtils.REF_ASCENDING_COMPARATOR);
    String currentBranch;
    try {
        currentBranch = repo.getFullBranch();
    } catch (IOException e) {
        // $NON-NLS-1$
        currentBranch = "";
    }
    int count = 0;
    String oldName = null;
    int refsLength = R_REFS.length();
    int tagsLength = R_TAGS.substring(refsLength).length();
    for (Ref ref : refs) {
        final String name = ref.getName();
        if (name.equals(Constants.HEAD) || name.equals(currentBranch) || excludeTag(ref, repo))
            continue;
        if (name.startsWith(R_REFS) && oldName != null && !oldName.regionMatches(refsLength, name, refsLength, tagsLength))
            new MenuItem(menu, SWT.SEPARATOR);
        MenuItem item = new MenuItem(menu, SWT.PUSH);
        item.setText(name);
        if (name.startsWith(Constants.R_TAGS))
            item.setImage(tagImage);
        else if (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_REMOTES))
            item.setImage(branchImage);
        item.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent event) {
                GitSynchronizeData data;
                try {
                    data = new GitSynchronizeData(repo, HEAD, name, true);
                    if (!(selectedResource instanceof IProject)) {
                        HashSet<IResource> resources = new HashSet<>();
                        resources.add(selectedResource);
                        data.setIncludedResources(resources);
                    }
                    GitModelSynchronize.launch(data, new IResource[] { selectedResource });
                } catch (IOException e) {
                    Activator.logError(e.getMessage(), e);
                }
            }
        });
        if (++count == MAX_NUM_MENU_ENTRIES)
            break;
        oldName = name;
    }
    if (count > 1)
        new MenuItem(menu, SWT.SEPARATOR);
    MenuItem custom = new MenuItem(menu, SWT.PUSH);
    custom.setText(UIText.SynchronizeWithMenu_custom);
    custom.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            GitSynchronizeWizard gitWizard = new GitSynchronizeWizard();
            WizardDialog wizard = new WizardDialog(menu.getShell(), gitWizard);
            wizard.create();
            wizard.open();
        }
    });
}
Also used : GitSynchronizeData(org.eclipse.egit.core.synchronize.dto.GitSynchronizeData) GitSynchronizeWizard(org.eclipse.egit.ui.internal.synchronize.GitSynchronizeWizard) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) IOException(java.io.IOException) RefDatabase(org.eclipse.jgit.lib.RefDatabase) LinkedList(java.util.LinkedList) IProject(org.eclipse.core.resources.IProject) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) SelectionEvent(org.eclipse.swt.events.SelectionEvent) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IResource(org.eclipse.core.resources.IResource) HashSet(java.util.HashSet)

Example 40 with RepositoryMapping

use of org.eclipse.egit.core.project.RepositoryMapping in project egit by eclipse.

the class ResourcePropertyTester method internalTest.

private boolean internalTest(Object receiver, String property) {
    if (!(receiver instanceof IResource))
        return false;
    IResource res = (IResource) receiver;
    if ("isContainer".equals(property)) {
        // $NON-NLS-1$
        int type = res.getType();
        return type == IResource.FOLDER || type == IResource.PROJECT;
    }
    RepositoryMapping mapping = RepositoryMapping.getMapping(res);
    if (mapping != null) {
        Repository repository = mapping.getRepository();
        return testRepositoryState(repository, property);
    }
    return false;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IResource(org.eclipse.core.resources.IResource)

Aggregations

RepositoryMapping (org.eclipse.egit.core.project.RepositoryMapping)87 Repository (org.eclipse.jgit.lib.Repository)40 IResource (org.eclipse.core.resources.IResource)31 IProject (org.eclipse.core.resources.IProject)23 IPath (org.eclipse.core.runtime.IPath)20 IOException (java.io.IOException)18 File (java.io.File)17 CoreException (org.eclipse.core.runtime.CoreException)15 IFile (org.eclipse.core.resources.IFile)12 Path (org.eclipse.core.runtime.Path)12 RevCommit (org.eclipse.jgit.revwalk.RevCommit)11 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)10 GitProjectData (org.eclipse.egit.core.project.GitProjectData)9 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 HashSet (java.util.HashSet)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 RevWalk (org.eclipse.jgit.revwalk.RevWalk)6 HashMap (java.util.HashMap)5