Search in sources :

Example 1 with FileRevisionTypedElement

use of org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement in project egit by eclipse.

the class CompareUtils method compareHeadWithWorkspace.

/**
 * Opens a compare editor. The workspace version of the given file is
 * compared with the version in the HEAD commit.
 *
 * @param repository
 * @param file
 */
public static void compareHeadWithWorkspace(Repository repository, @NonNull IFile file) {
    RepositoryMapping mapping = RepositoryMapping.getMapping(file);
    if (mapping == null) {
        Activator.error(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, file.getLocation(), repository), null);
        return;
    }
    String path = mapping.getRepoRelativePath(file);
    ITypedElement base = getHeadTypedElement(repository, path);
    if (base == null)
        return;
    IFileRevision nextFile = new WorkspaceFileRevision(file);
    String encoding = null;
    try {
        encoding = file.getCharset();
    } catch (CoreException e) {
        Activator.handleError(UIText.CompareUtils_errorGettingEncoding, e, true);
    }
    ITypedElement next = new FileRevisionTypedElement(nextFile, encoding);
    GitCompareFileRevisionEditorInput input = new GitCompareFileRevisionEditorInput(next, base, null);
    CompareUI.openCompareDialog(input);
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) RepositoryMapping(org.eclipse.egit.core.project.RepositoryMapping) IFileRevision(org.eclipse.team.core.history.IFileRevision) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) WorkspaceFileRevision(org.eclipse.egit.core.internal.storage.WorkspaceFileRevision) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement)

Example 2 with FileRevisionTypedElement

use of org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement in project egit by eclipse.

the class CompareUtils method getFileRevisionTypedElement.

/**
 * @param gitPath
 *            path within the commit's tree of the file.
 * @param commit
 *            the commit the blob was identified to be within.
 * @param db
 *            the repository this commit was loaded out of, and that this
 *            file's blob should also be reachable through.
 * @param blobId
 *            unique name of the content.
 * @return an instance of {@link ITypedElement} which can be used in
 *         {@link CompareEditorInput}
 */
public static ITypedElement getFileRevisionTypedElement(final String gitPath, final RevCommit commit, final Repository db, ObjectId blobId) {
    ITypedElement right = new GitCompareFileRevisionEditorInput.EmptyTypedElement(NLS.bind(UIText.GitHistoryPage_FileNotInCommit, getName(gitPath), truncatedRevision(commit.name())));
    try {
        IFileRevision nextFile = getFileRevision(gitPath, commit, db, blobId);
        if (nextFile != null) {
            String encoding = CompareCoreUtils.getResourceEncoding(db, gitPath);
            right = new FileRevisionTypedElement(nextFile, encoding);
        }
    } catch (IOException e) {
        Activator.error(NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, gitPath, commit.getId()), e);
    }
    return right;
}
Also used : IFileRevision(org.eclipse.team.core.history.IFileRevision) ITypedElement(org.eclipse.compare.ITypedElement) EmptyTypedElement(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput.EmptyTypedElement) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement) IOException(java.io.IOException)

Example 3 with FileRevisionTypedElement

use of org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement in project egit by eclipse.

the class GitCompareEditorInput method buildDiffContainer.

private IDiffContainer buildDiffContainer(RevCommit baseCommit, RevCommit compareCommit, IProgressMonitor monitor) throws IOException, InterruptedException {
    boolean useIndex = compareVersion.equals(CompareTreeView.INDEX_VERSION);
    boolean checkIgnored = false;
    IDiffContainer result = new DiffNode(Differencer.CONFLICTING);
    try (TreeWalk tw = new TreeWalk(repository)) {
        // filter by selected resources
        if (filterPathStrings.size() > 1) {
            List<TreeFilter> suffixFilters = new ArrayList<>();
            for (String filterPath : filterPathStrings) suffixFilters.add(PathFilter.create(filterPath));
            TreeFilter otf = OrTreeFilter.create(suffixFilters);
            tw.setFilter(otf);
        } else if (filterPathStrings.size() > 0) {
            String path = filterPathStrings.get(0);
            if (path.length() != 0)
                tw.setFilter(PathFilter.create(path));
        }
        tw.setRecursive(true);
        int baseTreeIndex;
        if (baseCommit == null) {
            // compare workspace with something
            checkIgnored = true;
            baseTreeIndex = tw.addTree(new FileTreeIterator(repository));
        } else
            baseTreeIndex = tw.addTree(new CanonicalTreeParser(null, repository.newObjectReader(), baseCommit.getTree()));
        int compareTreeIndex;
        if (!useIndex)
            compareTreeIndex = tw.addTree(new CanonicalTreeParser(null, repository.newObjectReader(), compareCommit.getTree()));
        else
            // compare something with the index
            compareTreeIndex = tw.addTree(new DirCacheIterator(repository.readDirCache()));
        while (tw.next()) {
            if (monitor.isCanceled())
                throw new InterruptedException();
            AbstractTreeIterator compareVersionIterator = tw.getTree(compareTreeIndex, AbstractTreeIterator.class);
            AbstractTreeIterator baseVersionIterator = tw.getTree(baseTreeIndex, AbstractTreeIterator.class);
            if (checkIgnored && baseVersionIterator != null && ((WorkingTreeIterator) baseVersionIterator).isEntryIgnored())
                continue;
            if (compareVersionIterator != null && baseVersionIterator != null) {
                boolean equalContent = compareVersionIterator.getEntryObjectId().equals(baseVersionIterator.getEntryObjectId());
                if (equalContent)
                    continue;
            }
            String encoding = null;
            GitFileRevision compareRev = null;
            if (compareVersionIterator != null) {
                String entryPath = compareVersionIterator.getEntryPathString();
                encoding = CompareCoreUtils.getResourceEncoding(repository, entryPath);
                if (!useIndex)
                    compareRev = GitFileRevision.inCommit(repository, compareCommit, entryPath, tw.getObjectId(compareTreeIndex));
                else
                    compareRev = GitFileRevision.inIndex(repository, entryPath);
            }
            GitFileRevision baseRev = null;
            if (baseVersionIterator != null) {
                String entryPath = baseVersionIterator.getEntryPathString();
                if (encoding == null) {
                    encoding = CompareCoreUtils.getResourceEncoding(repository, entryPath);
                }
                baseRev = GitFileRevision.inCommit(repository, baseCommit, entryPath, tw.getObjectId(baseTreeIndex));
            }
            if (compareVersionIterator != null && baseVersionIterator != null) {
                monitor.setTaskName(baseVersionIterator.getEntryPathString());
                // content exists on both sides
                add(result, baseVersionIterator.getEntryPathString(), new DiffNode(new FileRevisionTypedElement(compareRev, encoding), new FileRevisionTypedElement(baseRev, encoding)));
            } else if (baseVersionIterator != null && compareVersionIterator == null) {
                monitor.setTaskName(baseVersionIterator.getEntryPathString());
                // only on base side
                add(result, baseVersionIterator.getEntryPathString(), new DiffNode(Differencer.DELETION | Differencer.RIGHT, null, null, new FileRevisionTypedElement(baseRev, encoding)));
            } else if (compareVersionIterator != null && baseVersionIterator == null) {
                monitor.setTaskName(compareVersionIterator.getEntryPathString());
                // only on compare side
                add(result, compareVersionIterator.getEntryPathString(), new DiffNode(Differencer.ADDITION | Differencer.RIGHT, null, new FileRevisionTypedElement(compareRev, encoding), null));
            }
            if (monitor.isCanceled())
                throw new InterruptedException();
        }
        return result;
    }
}
Also used : DiffNode(org.eclipse.compare.structuremergeviewer.DiffNode) ArrayList(java.util.ArrayList) IDiffContainer(org.eclipse.compare.structuremergeviewer.IDiffContainer) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) GitFileRevision(org.eclipse.egit.core.internal.storage.GitFileRevision) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator)

Example 4 with FileRevisionTypedElement

use of org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement in project egit by eclipse.

the class GitMergeEditorInput method buildDiffContainer.

private IDiffContainer buildDiffContainer(Repository repository, RevCommit headCommit, RevCommit ancestorCommit, List<String> filterPaths, RevWalk rw, IProgressMonitor monitor) throws IOException, InterruptedException {
    monitor.setTaskName(UIText.GitMergeEditorInput_CalculatingDiffTaskName);
    IDiffContainer result = new DiffNode(Differencer.CONFLICTING);
    try (TreeWalk tw = new TreeWalk(repository)) {
        int dirCacheIndex = tw.addTree(new DirCacheIterator(repository.readDirCache()));
        int fileTreeIndex = tw.addTree(new FileTreeIterator(repository));
        int repositoryTreeIndex = tw.addTree(rw.parseTree(repository.resolve(Constants.HEAD)));
        // skip ignored resources
        NotIgnoredFilter notIgnoredFilter = new NotIgnoredFilter(fileTreeIndex);
        // filter by selected resources
        if (filterPaths.size() > 1) {
            List<TreeFilter> suffixFilters = new ArrayList<>();
            for (String filterPath : filterPaths) suffixFilters.add(PathFilter.create(filterPath));
            TreeFilter otf = OrTreeFilter.create(suffixFilters);
            tw.setFilter(AndTreeFilter.create(otf, notIgnoredFilter));
        } else if (filterPaths.size() > 0) {
            String path = filterPaths.get(0);
            if (path.length() == 0)
                tw.setFilter(notIgnoredFilter);
            else
                tw.setFilter(AndTreeFilter.create(PathFilter.create(path), notIgnoredFilter));
        } else
            tw.setFilter(notIgnoredFilter);
        tw.setRecursive(true);
        while (tw.next()) {
            if (monitor.isCanceled())
                throw new InterruptedException();
            String gitPath = tw.getPathString();
            monitor.setTaskName(gitPath);
            FileTreeIterator fit = tw.getTree(fileTreeIndex, FileTreeIterator.class);
            if (fit == null)
                continue;
            DirCacheIterator dit = tw.getTree(dirCacheIndex, DirCacheIterator.class);
            final DirCacheEntry dirCacheEntry = dit == null ? null : dit.getDirCacheEntry();
            boolean conflicting = dirCacheEntry != null && dirCacheEntry.getStage() > 0;
            AbstractTreeIterator rt = tw.getTree(repositoryTreeIndex, AbstractTreeIterator.class);
            // compare local file against HEAD to see if it was modified
            boolean modified = rt != null && !fit.getEntryObjectId().equals(rt.getEntryObjectId());
            // if this is neither conflicting nor changed, we skip it
            if (!conflicting && !modified)
                continue;
            ITypedElement right;
            if (conflicting) {
                GitFileRevision revision = GitFileRevision.inIndex(repository, gitPath, DirCacheEntry.STAGE_3);
                String encoding = CompareCoreUtils.getResourceEncoding(repository, gitPath);
                right = new FileRevisionTypedElement(revision, encoding);
            } else
                right = CompareUtils.getFileRevisionTypedElement(gitPath, headCommit, repository);
            // can this really happen?
            if (right instanceof EmptyTypedElement)
                continue;
            IFileRevision rev;
            // if the file is not conflicting (as it was auto-merged)
            // we will show the auto-merged (local) version
            Path repositoryPath = new Path(repository.getWorkTree().getAbsolutePath());
            IPath location = repositoryPath.append(fit.getEntryPathString());
            IFile file = ResourceUtil.getFileForLocation(location, false);
            if (!conflicting || useWorkspace) {
                if (file != null)
                    rev = new LocalFileRevision(file);
                else
                    rev = new WorkingTreeFileRevision(location.toFile());
            } else {
                rev = GitFileRevision.inIndex(repository, gitPath, DirCacheEntry.STAGE_2);
            }
            IRunnableContext runnableContext = getContainer();
            if (runnableContext == null)
                runnableContext = PlatformUI.getWorkbench().getProgressService();
            EditableRevision leftEditable;
            if (file != null)
                leftEditable = new ResourceEditableRevision(rev, file, runnableContext);
            else
                leftEditable = new LocationEditableRevision(rev, location, runnableContext);
            // make sure we don't need a round trip later
            try {
                leftEditable.cacheContents(monitor);
            } catch (CoreException e) {
                throw new IOException(e.getMessage());
            }
            int kind = Differencer.NO_CHANGE;
            if (conflicting)
                kind = Differencer.CONFLICTING;
            else if (modified)
                kind = Differencer.PSEUDO_CONFLICT;
            IDiffContainer fileParent = getFileParent(result, repositoryPath, file, location);
            ITypedElement anc;
            if (ancestorCommit != null)
                anc = CompareUtils.getFileRevisionTypedElement(gitPath, ancestorCommit, repository);
            else
                anc = null;
            // instead of null
            if (anc instanceof EmptyTypedElement)
                anc = null;
            // create the node as child
            new DiffNode(fileParent, kind, anc, leftEditable, right);
        }
        return result;
    }
}
Also used : IRunnableContext(org.eclipse.jface.operation.IRunnableContext) NotIgnoredFilter(org.eclipse.jgit.treewalk.filter.NotIgnoredFilter) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) IFile(org.eclipse.core.resources.IFile) DiffNode(org.eclipse.compare.structuremergeviewer.DiffNode) ArrayList(java.util.ArrayList) LocationEditableRevision(org.eclipse.egit.ui.internal.revision.LocationEditableRevision) IDiffContainer(org.eclipse.compare.structuremergeviewer.IDiffContainer) ResourceEditableRevision(org.eclipse.egit.ui.internal.revision.ResourceEditableRevision) GitFileRevision(org.eclipse.egit.core.internal.storage.GitFileRevision) WorkingTreeFileRevision(org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision) EmptyTypedElement(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput.EmptyTypedElement) DirCacheIterator(org.eclipse.jgit.dircache.DirCacheIterator) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) LocalFileRevision(org.eclipse.egit.ui.internal.revision.LocalFileRevision) IPath(org.eclipse.core.runtime.IPath) IFileRevision(org.eclipse.team.core.history.IFileRevision) ITypedElement(org.eclipse.compare.ITypedElement) IOException(java.io.IOException) AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) CoreException(org.eclipse.core.runtime.CoreException) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement) LocationEditableRevision(org.eclipse.egit.ui.internal.revision.LocationEditableRevision) ResourceEditableRevision(org.eclipse.egit.ui.internal.revision.ResourceEditableRevision) EditableRevision(org.eclipse.egit.ui.internal.revision.EditableRevision) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator)

Example 5 with FileRevisionTypedElement

use of org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement in project egit by eclipse.

the class CompareUtils method compareHeadWithWorkingTree.

/**
 * Opens a compare editor. The working tree version of the given file is
 * compared with the version in the HEAD commit. Use this method if the
 * given file is outide the workspace.
 *
 * @param repository
 * @param path
 */
public static void compareHeadWithWorkingTree(Repository repository, String path) {
    ITypedElement base = getHeadTypedElement(repository, path);
    if (base == null)
        return;
    IFileRevision nextFile;
    nextFile = new WorkingTreeFileRevision(new File(repository.getWorkTree(), path));
    String encoding = ResourcesPlugin.getEncoding();
    ITypedElement next = new FileRevisionTypedElement(nextFile, encoding);
    GitCompareFileRevisionEditorInput input = new GitCompareFileRevisionEditorInput(next, base, null);
    CompareUI.openCompareDialog(input);
}
Also used : WorkingTreeFileRevision(org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision) IFileRevision(org.eclipse.team.core.history.IFileRevision) GitCompareFileRevisionEditorInput(org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput) ITypedElement(org.eclipse.compare.ITypedElement) FileRevisionTypedElement(org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

FileRevisionTypedElement (org.eclipse.egit.ui.internal.revision.FileRevisionTypedElement)6 ITypedElement (org.eclipse.compare.ITypedElement)5 IFileRevision (org.eclipse.team.core.history.IFileRevision)5 IFile (org.eclipse.core.resources.IFile)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 DiffNode (org.eclipse.compare.structuremergeviewer.DiffNode)2 IDiffContainer (org.eclipse.compare.structuremergeviewer.IDiffContainer)2 CoreException (org.eclipse.core.runtime.CoreException)2 GitFileRevision (org.eclipse.egit.core.internal.storage.GitFileRevision)2 WorkingTreeFileRevision (org.eclipse.egit.core.internal.storage.WorkingTreeFileRevision)2 WorkspaceFileRevision (org.eclipse.egit.core.internal.storage.WorkspaceFileRevision)2 GitCompareFileRevisionEditorInput (org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput)2 EmptyTypedElement (org.eclipse.egit.ui.internal.revision.GitCompareFileRevisionEditorInput.EmptyTypedElement)2 DirCacheIterator (org.eclipse.jgit.dircache.DirCacheIterator)2 AbstractTreeIterator (org.eclipse.jgit.treewalk.AbstractTreeIterator)2 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)2 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)2 OrTreeFilter (org.eclipse.jgit.treewalk.filter.OrTreeFilter)2 TreeFilter (org.eclipse.jgit.treewalk.filter.TreeFilter)2