@Override
 public Image getImage(Object element) {
   RepositoryTreeNode node = (RepositoryTreeNode) element;
   if (node.getType() == RepositoryTreeNodeType.TAG) {
     // determine if we have a lightweight tag and
     // use the corresponding icon
     RevObject any;
     try {
       ObjectId id = node.getRepository().resolve(((Ref) node.getObject()).getName());
       if (id == null) return null;
       any = new RevWalk(node.getRepository()).parseAny(id);
     } catch (MissingObjectException e) {
       Activator.logError(e.getMessage(), e);
       return null;
     } catch (IOException e) {
       Activator.logError(e.getMessage(), e);
       return null;
     }
     if (any instanceof RevCommit)
       // lightweight tag
       return decorateImage(lightweightTagImage, element);
     else
       // annotated or signed tag
       return decorateImage(node.getType().getIcon(), element);
   } else return decorateImage(node.getType().getIcon(), element);
 }
Exemplo n.º 2
0
  public Object execute(ExecutionEvent event) throws ExecutionException {
    final RepositoryTreeNode node = getSelectedNodes(event).get(0);

    if (node.getType() == RepositoryTreeNodeType.ADDITIONALREF) {
      Ref ref = (Ref) node.getObject();
      try {
        RevCommit baseCommit =
            new RevWalk(node.getRepository()).parseCommit(ref.getLeaf().getObjectId());
        WizardDialog dlg =
            new WizardDialog(
                getShell(event), new CreateBranchWizard(node.getRepository(), baseCommit.name()));
        dlg.setHelpAvailable(false);
        dlg.open();
      } catch (IOException e) {
        Activator.handleError(e.getMessage(), e, true);
      }
      return null;
    }
    String base = null;
    if (node.getObject() instanceof Ref) base = ((Ref) node.getObject()).getName();
    new WizardDialog(getShell(event), new CreateBranchWizard(node.getRepository(), base)).open();
    return null;
  }
  public StyledString getStyledText(Object element) {
    if (!(element instanceof RepositoryTreeNode)) return null;

    RepositoryTreeNode node = (RepositoryTreeNode) element;

    try {
      switch (node.getType()) {
        case REPO:
          Repository repository = (Repository) node.getObject();
          File directory = repository.getDirectory();
          StyledString string = new StyledString(directory.getParentFile().getName());
          string.append(
              " - " + directory.getAbsolutePath(), StyledString.QUALIFIER_STYLER); // $NON-NLS-1$
          String branch = repository.getBranch();
          if (repository.getRepositoryState() != RepositoryState.SAFE)
            branch += " - " + repository.getRepositoryState().getDescription(); // $NON-NLS-1$
          string.append(
              " [" + branch + "]", StyledString.DECORATIONS_STYLER); // $NON-NLS-1$//$NON-NLS-2$
          return string;
        case ADDITIONALREF:
          Ref ref = (Ref) node.getObject();
          // shorten the name
          StyledString refName = new StyledString(Repository.shortenRefName(ref.getName()));
          if (ref.isSymbolic()) {
            refName.append(" - ", StyledString.QUALIFIER_STYLER); // $NON-NLS-1$
            refName.append(ref.getLeaf().getName(), StyledString.QUALIFIER_STYLER);
            refName.append(" - ", StyledString.QUALIFIER_STYLER); // $NON-NLS-1$
            refName.append(
                ObjectId.toString(ref.getLeaf().getObjectId()), StyledString.QUALIFIER_STYLER);
          } else {
            refName.append(" - ", StyledString.QUALIFIER_STYLER); // $NON-NLS-1$
            refName.append(ObjectId.toString(ref.getObjectId()), StyledString.QUALIFIER_STYLER);
          }
          return refName;
        case WORKINGDIR:
          StyledString dirString = new StyledString(UIText.RepositoriesView_WorkingDir_treenode);
          dirString.append(" - ", StyledString.QUALIFIER_STYLER); // $NON-NLS-1$
          if (node.getRepository().isBare()) {
            dirString.append(
                UIText.RepositoriesViewLabelProvider_BareRepositoryMessage,
                StyledString.QUALIFIER_STYLER);
          } else {
            dirString.append(
                node.getRepository().getWorkTree().getAbsolutePath(),
                StyledString.QUALIFIER_STYLER);
          }
          return dirString;
        case PUSH:
          // fall through
        case FETCH:
          // fall through
        case FILE:
          // fall through
        case FOLDER:
          // fall through
        case BRANCHES:
          // fall through
        case LOCAL:
          // fall through
        case REMOTETRACKING:
          // fall through
        case BRANCHHIERARCHY:
          // fall through
        case TAGS:
          // fall through;
        case ADDITIONALREFS:
          // fall through
        case REMOTES:
          // fall through
        case REMOTE:
          // fall through
        case ERROR:
          // fall through
        case REF:
          // fall through
        case TAG:
          {
            String label = getSimpleText(node);
            if (label != null) return new StyledString(label);
          }
      }
    } catch (IOException e) {
      Activator.logError(e.getMessage(), e);
    }

    return null;
  }
 private String getSimpleText(RepositoryTreeNode node) {
   switch (node.getType()) {
     case REPO:
       File directory = ((Repository) node.getObject()).getDirectory();
       StringBuilder sb = new StringBuilder();
       sb.append(directory.getParentFile().getName());
       sb.append(" - "); // $NON-NLS-1$
       sb.append(directory.getAbsolutePath());
       return sb.toString();
     case FILE:
       // fall through
     case FOLDER:
       return ((File) node.getObject()).getName();
     case BRANCHES:
       return UIText.RepositoriesView_Branches_Nodetext;
     case LOCAL:
       return UIText.RepositoriesViewLabelProvider_LocalNodetext;
     case REMOTETRACKING:
       return UIText.RepositoriesViewLabelProvider_RemoteTrackingNodetext;
     case BRANCHHIERARCHY:
       IPath fullPath = (IPath) node.getObject();
       return fullPath.lastSegment();
     case TAGS:
       return UIText.RepositoriesViewLabelProvider_TagsNodeText;
     case ADDITIONALREFS:
       return UIText.RepositoriesViewLabelProvider_SymbolicRefNodeText;
     case REMOTES:
       return UIText.RepositoriesView_RemotesNodeText;
     case REF:
       // fall through
     case TAG:
       {
         Ref ref = (Ref) node.getObject();
         // shorten the name
         String refName = Repository.shortenRefName(ref.getName());
         if (node.getParent().getType() == RepositoryTreeNodeType.BRANCHHIERARCHY) {
           int index = refName.lastIndexOf('/');
           refName = refName.substring(index + 1);
         }
         return refName;
       }
     case ADDITIONALREF:
       {
         Ref ref = (Ref) node.getObject();
         // shorten the name
         String refName = Repository.shortenRefName(ref.getName());
         if (ref.isSymbolic()) {
           refName =
               refName
                   + " - " //$NON-NLS-1$
                   + ref.getLeaf().getName()
                   + " - "
                   + ObjectId.toString(ref.getLeaf().getObjectId()); // $NON-NLS-1$
         } else {
           refName =
               refName
                   + " - " //$NON-NLS-1$
                   + ObjectId.toString(ref.getObjectId());
         }
         return refName;
       }
     case WORKINGDIR:
       if (node.getRepository().isBare())
         return UIText.RepositoriesView_WorkingDir_treenode
             + " - " //$NON-NLS-1$
             + UIText.RepositoriesViewLabelProvider_BareRepositoryMessage;
       else
         return UIText.RepositoriesView_WorkingDir_treenode
             + " - " //$NON-NLS-1$
             + node.getRepository().getWorkTree().getAbsolutePath();
     case REMOTE:
       // fall through
     case PUSH:
       // fall through
     case FETCH:
       // fall through
     case ERROR:
       return (String) node.getObject();
   }
   return null;
 }
  private Image decorateImage(final Image image, Object element) {

    RepositoryTreeNode node = (RepositoryTreeNode) element;
    switch (node.getType()) {
      case TAG:
        // fall through
      case ADDITIONALREF:
        // fall through
      case REF:
        // if the branch or tag is checked out,
        // we want to decorate the corresponding
        // node with a little check indicator
        String refName = ((Ref) node.getObject()).getName();
        Ref leaf = ((Ref) node.getObject()).getLeaf();

        String branchName;
        String compareString;

        try {
          branchName = node.getRepository().getFullBranch();
          if (branchName == null) return image;
          if (refName.startsWith(Constants.R_HEADS)) {
            // local branch: HEAD would be on the branch
            compareString = refName;
          } else if (refName.startsWith(Constants.R_TAGS)) {
            // tag: HEAD would be on the commit id to which the tag is
            // pointing
            ObjectId id = node.getRepository().resolve(refName);
            if (id == null) return image;
            RevWalk rw = new RevWalk(node.getRepository());
            RevTag tag = rw.parseTag(id);
            compareString = tag.getObject().name();

          } else if (refName.startsWith(Constants.R_REMOTES)) {
            // remote branch: HEAD would be on the commit id to which
            // the branch is pointing
            ObjectId id = node.getRepository().resolve(refName);
            if (id == null) return image;
            RevWalk rw = new RevWalk(node.getRepository());
            RevCommit commit = rw.parseCommit(id);
            compareString = commit.getId().name();
          } else if (refName.equals(Constants.HEAD)) return getDecoratedImage(image);
          else {
            String leafname = leaf.getName();
            if (leafname.startsWith(Constants.R_REFS)
                && leafname.equals(node.getRepository().getFullBranch()))
              return getDecoratedImage(image);
            else if (leaf.getObjectId().equals(node.getRepository().resolve(Constants.HEAD)))
              return getDecoratedImage(image);
            // some other symbolic reference
            return image;
          }
        } catch (IOException e1) {
          return image;
        }

        if (compareString.equals(branchName)) {
          return getDecoratedImage(image);
        }

        return image;

      default:
        return image;
    }
  }