Search in sources :

Example 1 with IResourceDiff

use of org.eclipse.team.core.mapping.IResourceDiff in project linuxtools by eclipse.

the class PrepareChangeLogAction method getChangedLines.

private void getChangedLines(Subscriber s, PatchFile p, IProgressMonitor monitor) {
    try {
        // For an outgoing changed resource, find out which lines
        // differ from the local file and its previous local version
        // (i.e. we don't want to force a diff with the repository).
        IDiff d = s.getDiff(p.getResource());
        if (d instanceof IThreeWayDiff && ((IThreeWayDiff) d).getDirection() == IThreeWayDiff.OUTGOING) {
            IThreeWayDiff diff = (IThreeWayDiff) d;
            monitor.beginTask(null, 100);
            IResourceDiff localDiff = (IResourceDiff) diff.getLocalChange();
            IResource resource = localDiff.getResource();
            if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                // $NON-NLS-1$
                monitor.subTask(Messages.getString("ChangeLog.MergingDiffs"));
                String osEncoding = file.getCharset();
                IFileRevision ancestorState = localDiff.getBeforeState();
                IStorage ancestorStorage;
                if (ancestorState != null) {
                    ancestorStorage = ancestorState.getStorage(monitor);
                    p.setStorage(ancestorStorage);
                } else {
                    return;
                }
                try {
                    // We compare using a standard differencer to get ranges
                    // of changes.  We modify them to be document-based (i.e.
                    // first line is line 1) and store them for later parsing.
                    LineComparator left = new LineComparator(ancestorStorage.getContents(), osEncoding);
                    LineComparator right = new LineComparator(file.getContents(), osEncoding);
                    for (RangeDifference tmp : RangeDifferencer.findDifferences(left, right)) {
                        if (tmp.kind() == RangeDifference.CHANGE) {
                            // Right side of diff are all changes found in local file.
                            int rightLength = tmp.rightLength() > 0 ? tmp.rightLength() : tmp.rightLength() + 1;
                            // We also want to store left side of the diff which are changes to the ancestor as it may contain
                            // functions/methods that have been removed.
                            int leftLength = tmp.leftLength() > 0 ? tmp.leftLength() : tmp.leftLength() + 1;
                            // Only store left side changes if the storage exists and we add one to the start line number
                            if (p.getStorage() != null)
                                p.addLineRange(tmp.leftStart(), tmp.leftStart() + leftLength, false);
                            p.addLineRange(tmp.rightStart(), tmp.rightStart() + rightLength, true);
                        }
                    }
                } catch (UnsupportedEncodingException e) {
                // do nothing for now
                }
            }
            monitor.done();
        }
    } catch (CoreException e) {
    // Do nothing if error occurs
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IFileRevision(org.eclipse.team.core.history.IFileRevision) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IDiff(org.eclipse.team.core.diff.IDiff) IResourceDiff(org.eclipse.team.core.mapping.IResourceDiff) IStorage(org.eclipse.core.resources.IStorage) RangeDifference(org.eclipse.compare.rangedifferencer.RangeDifference) LineComparator(org.eclipse.linuxtools.internal.changelog.core.LineComparator) CoreException(org.eclipse.core.runtime.CoreException) IThreeWayDiff(org.eclipse.team.core.diff.IThreeWayDiff) IResource(org.eclipse.core.resources.IResource)

Example 2 with IResourceDiff

use of org.eclipse.team.core.mapping.IResourceDiff in project linuxtools by eclipse.

the class PrepareCommitHandler method loadClipboard.

private void loadClipboard(IProgressMonitor monitor) {
    IEditorPart currentEditor;
    try {
        currentEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    } catch (Exception e) {
        // no editor is active now so do nothing
        return;
    }
    if (currentEditor == null) {
        return;
    }
    IFile changelog = getChangelogFile(getDocumentLocation(currentEditor, false));
    if (changelog == null) {
        return;
    }
    String diffResult = "";
    IProject project = null;
    IResource[] resources = new IResource[] { changelog };
    project = changelog.getProject();
    RepositoryProvider r = RepositoryProvider.getProvider(project);
    if (r == null) {
        // There is no repository provider for this project, i.e
        return;
    // it's not shared.
    }
    SyncInfoSet set = new SyncInfoSet();
    Subscriber s = r.getSubscriber();
    try {
        s.refresh(resources, IResource.DEPTH_ZERO, monitor);
    } catch (TeamException e1) {
    // Ignore, continue anyways
    }
    s.collectOutOfSync(resources, IResource.DEPTH_ZERO, set, monitor);
    SyncInfo[] infos = set.getSyncInfos();
    if (infos.length == 1) {
        int kind = SyncInfo.getChange(infos[0].getKind());
        if (kind == SyncInfo.CHANGE) {
            try {
                IDiff d = s.getDiff(infos[0].getLocal());
                if (d instanceof IThreeWayDiff && ((IThreeWayDiff) d).getDirection() == IThreeWayDiff.OUTGOING) {
                    IThreeWayDiff diff = (IThreeWayDiff) d;
                    monitor.beginTask(null, 100);
                    IResourceDiff localDiff = (IResourceDiff) diff.getLocalChange();
                    IFile file = (IFile) localDiff.getResource();
                    monitor.subTask(Messages.getString(// $NON-NLS-1$
                    "ChangeLog.MergingDiffs"));
                    String osEncoding = file.getCharset();
                    IFileRevision ancestorState = localDiff.getBeforeState();
                    IStorage ancestorStorage;
                    if (ancestorState != null)
                        ancestorStorage = ancestorState.getStorage(monitor);
                    else {
                        ancestorStorage = null;
                        return;
                    }
                    try {
                        LineComparator left = new LineComparator(ancestorStorage.getContents(), osEncoding);
                        LineComparator right = new LineComparator(file.getContents(), osEncoding);
                        for (RangeDifference tmp : RangeDifferencer.findDifferences(left, right)) {
                            if (tmp.kind() == RangeDifference.CHANGE) {
                                LineNumberReader l = new LineNumberReader(new InputStreamReader(file.getContents()));
                                int rightLength = tmp.rightLength() > 0 ? tmp.rightLength() : tmp.rightLength() + 1;
                                String line0 = null;
                                String preDiffResult = "";
                                for (int i = 0; i < tmp.rightStart(); ++i) {
                                    // usage if needed.
                                    try {
                                        String line = l.readLine();
                                        if (line0 == null)
                                            line0 = line;
                                        preDiffResult += line + "\n";
                                    } catch (IOException e) {
                                        break;
                                    }
                                }
                                for (int i = 0; i < rightLength; ++i) {
                                    try {
                                        String line = l.readLine();
                                        // used as a commit comment.
                                        if (i == rightLength - tmp.rightStart()) {
                                            if (tmp.rightStart() != 0 && line.equals(line0)) {
                                                diffResult = preDiffResult += diffResult;
                                                // stop
                                                i = rightLength;
                                            // loop
                                            } else
                                                diffResult += line + "\n";
                                        } else
                                            // $NON-NLS-1$
                                            diffResult += line + "\n";
                                    } catch (IOException e) {
                                    // do nothing
                                    }
                                }
                            }
                        }
                    } catch (UnsupportedEncodingException e) {
                    // do nothing for now
                    }
                    monitor.done();
                }
            } catch (CoreException e) {
            // do nothing
            }
        }
    }
    if (!diffResult.equals(""))
        populateClipboardBuffer(diffResult);
}
Also used : IFile(org.eclipse.core.resources.IFile) IDiff(org.eclipse.team.core.diff.IDiff) LineNumberReader(java.io.LineNumberReader) TeamException(org.eclipse.team.core.TeamException) RangeDifference(org.eclipse.compare.rangedifferencer.RangeDifference) LineComparator(org.eclipse.linuxtools.internal.changelog.core.LineComparator) Subscriber(org.eclipse.team.core.subscribers.Subscriber) SyncInfo(org.eclipse.team.core.synchronize.SyncInfo) IThreeWayDiff(org.eclipse.team.core.diff.IThreeWayDiff) SyncInfoSet(org.eclipse.team.core.synchronize.SyncInfoSet) InputStreamReader(java.io.InputStreamReader) IFileRevision(org.eclipse.team.core.history.IFileRevision) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IEditorPart(org.eclipse.ui.IEditorPart) IResourceDiff(org.eclipse.team.core.mapping.IResourceDiff) IOException(java.io.IOException) IStorage(org.eclipse.core.resources.IStorage) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TeamException(org.eclipse.team.core.TeamException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) RepositoryProvider(org.eclipse.team.core.RepositoryProvider) IResource(org.eclipse.core.resources.IResource)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 RangeDifference (org.eclipse.compare.rangedifferencer.RangeDifference)2 IFile (org.eclipse.core.resources.IFile)2 IResource (org.eclipse.core.resources.IResource)2 IStorage (org.eclipse.core.resources.IStorage)2 CoreException (org.eclipse.core.runtime.CoreException)2 LineComparator (org.eclipse.linuxtools.internal.changelog.core.LineComparator)2 IDiff (org.eclipse.team.core.diff.IDiff)2 IThreeWayDiff (org.eclipse.team.core.diff.IThreeWayDiff)2 IFileRevision (org.eclipse.team.core.history.IFileRevision)2 IResourceDiff (org.eclipse.team.core.mapping.IResourceDiff)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 IProject (org.eclipse.core.resources.IProject)1 RepositoryProvider (org.eclipse.team.core.RepositoryProvider)1 TeamException (org.eclipse.team.core.TeamException)1 Subscriber (org.eclipse.team.core.subscribers.Subscriber)1 SyncInfo (org.eclipse.team.core.synchronize.SyncInfo)1