예제 #1
0
 public Object execute(ExecutionEvent event) throws ExecutionException {
   boolean compareMode =
       Boolean.TRUE.toString().equals(event.getParameter(HistoryViewCommands.COMPARE_MODE_PARAM));
   IStructuredSelection selection = getSelection(getPage());
   if (selection.size() < 1) return null;
   Object input = getPage().getInputInternal().getSingleFile();
   if (input == null) return null;
   IWorkbenchPage workBenchPage =
       HandlerUtil.getActiveWorkbenchWindowChecked(event).getActivePage();
   boolean errorOccurred = false;
   List<ObjectId> ids = new ArrayList<ObjectId>();
   String gitPath = null;
   if (input instanceof IFile) {
     IFile resource = (IFile) input;
     final RepositoryMapping map = RepositoryMapping.getMapping(resource);
     gitPath = map.getRepoRelativePath(resource);
     Iterator<?> it = selection.iterator();
     while (it.hasNext()) {
       RevCommit commit = (RevCommit) it.next();
       String commitPath = getRenamedPath(gitPath, commit);
       IFileRevision rev = null;
       try {
         rev = CompareUtils.getFileRevision(commitPath, commit, map.getRepository(), null);
       } catch (IOException e) {
         Activator.logError(
             NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, gitPath, commit.getId()), e);
         errorOccurred = true;
       }
       if (rev != null) {
         if (compareMode) {
           ITypedElement right =
               CompareUtils.getFileRevisionTypedElement(commitPath, commit, map.getRepository());
           final GitCompareFileRevisionEditorInput in =
               new GitCompareFileRevisionEditorInput(
                   SaveableCompareEditorInput.createFileElement(resource), right, null);
           try {
             CompareUtils.openInCompare(workBenchPage, in);
           } catch (Exception e) {
             errorOccurred = true;
           }
         } else
           try {
             EgitUiEditorUtils.openEditor(
                 getPart(event).getSite().getPage(), rev, new NullProgressMonitor());
           } catch (CoreException e) {
             Activator.logError(UIText.GitHistoryPage_openFailed, e);
             errorOccurred = true;
           }
       } else ids.add(commit.getId());
     }
   }
   if (input instanceof File) {
     File fileInput = (File) input;
     Repository repo = getRepository(event);
     gitPath = getRepoRelativePath(repo, fileInput);
     Iterator<?> it = selection.iterator();
     while (it.hasNext()) {
       RevCommit commit = (RevCommit) it.next();
       String commitPath = getRenamedPath(gitPath, commit);
       IFileRevision rev = null;
       try {
         rev = CompareUtils.getFileRevision(commitPath, commit, repo, null);
       } catch (IOException e) {
         Activator.logError(
             NLS.bind(UIText.GitHistoryPage_errorLookingUpPath, commitPath, commit.getId()), e);
         errorOccurred = true;
       }
       if (rev != null) {
         if (compareMode)
           try {
             ITypedElement left =
                 CompareUtils.getFileRevisionTypedElement(
                     gitPath, new RevWalk(repo).parseCommit(repo.resolve(Constants.HEAD)), repo);
             ITypedElement right =
                 CompareUtils.getFileRevisionTypedElement(commitPath, commit, repo);
             final GitCompareFileRevisionEditorInput in =
                 new GitCompareFileRevisionEditorInput(left, right, null);
             CompareUtils.openInCompare(workBenchPage, in);
           } catch (IOException e) {
             errorOccurred = true;
           }
         else
           try {
             EgitUiEditorUtils.openEditor(
                 getPart(event).getSite().getPage(), rev, new NullProgressMonitor());
           } catch (CoreException e) {
             Activator.logError(UIText.GitHistoryPage_openFailed, e);
             errorOccurred = true;
           }
       } else ids.add(commit.getId());
     }
   }
   if (errorOccurred) Activator.showError(UIText.GitHistoryPage_openFailed, null);
   if (ids.size() > 0) {
     StringBuilder idList = new StringBuilder(""); // $NON-NLS-1$
     for (ObjectId objectId : ids) idList.append(objectId.getName()).append(' ');
     MessageDialog.openError(
         getPart(event).getSite().getShell(),
         UIText.GitHistoryPage_fileNotFound,
         NLS.bind(UIText.GitHistoryPage_notContainedInCommits, gitPath, idList.toString()));
   }
   return null;
 }
예제 #2
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);
  }