Esempio n. 1
0
  private void reactOnOpen(OpenEvent event) {
    Object selected = ((IStructuredSelection) event.getSelection()).getFirstElement();
    ITypedElement left;
    ITypedElement right;
    if (selected instanceof IContainer) {
      // open/close folder
      TreeViewer tv = (TreeViewer) event.getViewer();
      tv.setExpandedState(selected, !tv.getExpandedState(selected));
      return;
    } else if (selected instanceof IFile) {
      final IFile res = (IFile) selected;
      left =
          new EditableRevision(new LocalFileRevision(res)) {
            @Override
            public void setContent(final byte[] newContent) {
              try {
                PlatformUI.getWorkbench()
                    .getProgressService()
                    .run(
                        false,
                        false,
                        new IRunnableWithProgress() {
                          public void run(IProgressMonitor myMonitor)
                              throws InvocationTargetException, InterruptedException {
                            try {
                              res.setContents(
                                  new ByteArrayInputStream(newContent), false, true, myMonitor);
                            } catch (CoreException e) {
                              throw new InvocationTargetException(e);
                            }
                          }
                        });
              } catch (InvocationTargetException e) {
                Activator.handleError(
                    e.getTargetException().getMessage(), e.getTargetException(), true);
              } catch (InterruptedException e) {
                // ignore here
              }
            }
          };
      GitFileRevision rightRevision =
          compareVersionMap.get(new Path(repositoryMapping.getRepoRelativePath(res)));
      if (rightRevision == null) {
        right =
            new GitCompareFileRevisionEditorInput.EmptyTypedElement(
                NLS.bind(
                    UIText.CompareTreeView_ItemNotFoundInVersionMessage,
                    res.getName(),
                    getCompareVersion()));
      } else {
        String encoding = CompareCoreUtils.getResourceEncoding(res);
        right = new FileRevisionTypedElement(rightRevision, encoding);
      }
      GitCompareFileRevisionEditorInput compareInput =
          new GitCompareFileRevisionEditorInput(
              left, right, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage());
      CompareUtils.openInCompare(
          PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), compareInput);
    } else if (selected instanceof GitFileRevision) {
      GitFileRevision rightRevision = (GitFileRevision) selected;
      left =
          new GitCompareFileRevisionEditorInput.EmptyTypedElement(
              NLS.bind(
                  UIText.CompareTreeView_ItemNotFoundInVersionMessage,
                  rightRevision.getName(),
                  getBaseVersion()));
      right = new FileRevisionTypedElement(rightRevision);
    } else if (selected instanceof PathNode) {
      PathNode node = (PathNode) selected;
      switch (node.type) {
        case FILE_BOTH_SIDES_DIFFER:
          // fall through
        case FILE_BOTH_SIDES_SAME:
          {
            // open a compare editor with both sides filled
            GitFileRevision rightRevision = compareVersionMap.get(node.path);
            right = new FileRevisionTypedElement(rightRevision);
            GitFileRevision leftRevision = baseVersionMap.get(node.path);
            left = new FileRevisionTypedElement(leftRevision);
            break;
          }
        case FILE_DELETED:
          {
            // open compare editor with left side empty
            GitFileRevision rightRevision = compareVersionMap.get(node.path);
            right = new FileRevisionTypedElement(rightRevision);
            left =
                new GitCompareFileRevisionEditorInput.EmptyTypedElement(
                    NLS.bind(
                        UIText.CompareTreeView_ItemNotFoundInVersionMessage,
                        rightRevision.getName(),
                        getBaseVersion()));
            break;
          }
        case FILE_ADDED:
          {
            // open compare editor with right side empty
            GitFileRevision leftRevision = baseVersionMap.get(node.path);
            left = new FileRevisionTypedElement(leftRevision);
            right =
                new GitCompareFileRevisionEditorInput.EmptyTypedElement(
                    NLS.bind(
                        UIText.CompareTreeView_ItemNotFoundInVersionMessage,
                        leftRevision.getName(),
                        getCompareVersion()));
            break;
          }
        case FOLDER:
          // open/close folder
          TreeViewer tv = (TreeViewer) event.getViewer();
          tv.setExpandedState(selected, !tv.getExpandedState(selected));
          return;
        default:
          return;
      }

    } else if (selected instanceof PathNodeAdapter) {
      // deleted in workspace
      PathNodeAdapter node = (PathNodeAdapter) selected;
      GitFileRevision rightRevision = compareVersionMap.get(node.pathNode.path);
      right = new FileRevisionTypedElement(rightRevision);
      left =
          new GitCompareFileRevisionEditorInput.EmptyTypedElement(
              NLS.bind(
                  UIText.CompareTreeView_ItemNotFoundInVersionMessage,
                  node.pathNode.path.lastSegment(),
                  getBaseVersion()));
    } else return;

    GitCompareFileRevisionEditorInput compareInput =
        new GitCompareFileRevisionEditorInput(
            left, right, PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage());
    CompareUtils.openInCompare(
        PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), compareInput);
  }
Esempio n. 2
0
  private void buildMaps(
      Repository repository,
      RevCommit baseCommit,
      RevCommit compareCommit,
      IProgressMonitor monitor)
      throws InterruptedException, IOException {
    monitor.beginTask(UIText.CompareTreeView_AnalyzingRepositoryTaskText, IProgressMonitor.UNKNOWN);
    boolean useIndex = compareVersion.equals(INDEX_VERSION);
    deletedPaths.clear();
    equalContentPaths.clear();
    baseVersionMap.clear();
    compareVersionMap.clear();
    compareVersionPathsWithChildren.clear();
    addedPaths.clear();
    baseVersionPathsWithChildren.clear();
    boolean checkIgnored = false;
    TreeWalk tw = new TreeWalk(repository);
    try {
      int baseTreeIndex;
      if (baseCommit == null) {
        checkIgnored = true;
        baseTreeIndex =
            tw.addTree(
                new AdaptableFileTreeIterator(
                    repository, ResourcesPlugin.getWorkspace().getRoot()));
      } 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 compareTreeIndex = tw.addTree(new DirCacheIterator(repository.readDirCache()));

      if (input instanceof IResource[]) {
        IResource[] resources = (IResource[]) input;
        List<TreeFilter> orFilters = new ArrayList<TreeFilter>(resources.length);

        for (IResource resource : resources) {
          String relPath = repositoryMapping.getRepoRelativePath(resource);
          if (relPath.length() > 0) orFilters.add(PathFilter.create(relPath));
        }
        if (orFilters.size() > 1) tw.setFilter(OrTreeFilter.create(orFilters));
        else if (orFilters.size() == 1) tw.setFilter(orFilters.get(0));
      }

      tw.setRecursive(true);

      if (monitor.isCanceled()) throw new InterruptedException();
      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) {
          monitor.setTaskName(baseVersionIterator.getEntryPathString());
          IPath currentPath = new Path(baseVersionIterator.getEntryPathString());
          if (!useIndex)
            compareVersionMap.put(
                currentPath,
                GitFileRevision.inCommit(
                    repository,
                    compareCommit,
                    baseVersionIterator.getEntryPathString(),
                    tw.getObjectId(compareTreeIndex)));
          else
            compareVersionMap.put(
                currentPath,
                GitFileRevision.inIndex(repository, baseVersionIterator.getEntryPathString()));
          if (baseCommit != null)
            baseVersionMap.put(
                currentPath,
                GitFileRevision.inCommit(
                    repository,
                    baseCommit,
                    baseVersionIterator.getEntryPathString(),
                    tw.getObjectId(baseTreeIndex)));
          boolean equalContent =
              compareVersionIterator
                  .getEntryObjectId()
                  .equals(baseVersionIterator.getEntryObjectId());
          if (equalContent) equalContentPaths.add(currentPath);

          if (equalContent && !showEquals) continue;

          while (currentPath.segmentCount() > 0) {
            currentPath = currentPath.removeLastSegments(1);
            if (!baseVersionPathsWithChildren.add(currentPath)) break;
          }

        } else if (baseVersionIterator != null && compareVersionIterator == null) {
          monitor.setTaskName(baseVersionIterator.getEntryPathString());
          // only on base side
          IPath currentPath = new Path(baseVersionIterator.getEntryPathString());
          addedPaths.add(currentPath);
          if (baseCommit != null)
            baseVersionMap.put(
                currentPath,
                GitFileRevision.inCommit(
                    repository,
                    baseCommit,
                    baseVersionIterator.getEntryPathString(),
                    tw.getObjectId(baseTreeIndex)));
          while (currentPath.segmentCount() > 0) {
            currentPath = currentPath.removeLastSegments(1);
            if (!baseVersionPathsWithChildren.add(currentPath)) break;
          }

        } else if (compareVersionIterator != null && baseVersionIterator == null) {
          monitor.setTaskName(compareVersionIterator.getEntryPathString());
          // only on compare side
          IPath currentPath = new Path(compareVersionIterator.getEntryPathString());
          deletedPaths.add(currentPath);
          List<PathNodeAdapter> children =
              compareVersionPathsWithChildren.get(currentPath.removeLastSegments(1));
          if (children == null) {
            children = new ArrayList<PathNodeAdapter>(1);
            compareVersionPathsWithChildren.put(currentPath.removeLastSegments(1), children);
          }
          children.add(new PathNodeAdapter(new PathNode(currentPath, Type.FILE_DELETED)));

          if (!useIndex)
            compareVersionMap.put(
                currentPath,
                GitFileRevision.inCommit(
                    repository,
                    compareCommit,
                    compareVersionIterator.getEntryPathString(),
                    tw.getObjectId(compareTreeIndex)));
          else
            compareVersionMap.put(
                currentPath,
                GitFileRevision.inIndex(repository, compareVersionIterator.getEntryPathString()));
        }
      }
    } finally {
      tw.release();
      monitor.done();
    }
  }
  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<TreeFilter>();
        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;
    }
  }