Java Code Examples for com.intellij.openapi.vcs.ProjectLevelVcsManager#getInstance()

The following examples show how to use com.intellij.openapi.vcs.ProjectLevelVcsManager#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SelectedBlockHistoryAction.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected boolean isEnabled(VcsContext context) {
  Project project = context.getProject();
  if (project == null) return false;

  VcsSelection selection = VcsSelectionUtil.getSelection(context);
  if (selection == null) return false;

  VirtualFile file = FileDocumentManager.getInstance().getFile(selection.getDocument());
  if (file == null) return false;

  final ProjectLevelVcsManagerImpl vcsManager = (ProjectLevelVcsManagerImpl)ProjectLevelVcsManager.getInstance(project);
  final BackgroundableActionEnabledHandler handler = vcsManager.getBackgroundableActionHandler(VcsBackgroundableActions.HISTORY_FOR_SELECTION);
  if (handler.isInProgress(VcsBackgroundableActions.keyFrom(file))) return false;

  AbstractVcs activeVcs = ProjectLevelVcsManager.getInstance(project).getVcsFor(file);
  if (activeVcs == null) return false;

  VcsHistoryProvider provider = activeVcs.getVcsBlockHistoryProvider();
  if (provider == null) return false;

  if (!AbstractVcs.fileInVcsByFileStatus(project, VcsUtil.getFilePath(file))) return false;
  return true;
}
 
Example 2
Source File: P4Vcs.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
public static boolean isProjectValid(@NotNull Project project) {
    if (project.isDisposed()) {
        return false;
    }
    ProjectLevelVcsManager mgr = ProjectLevelVcsManager.getInstance(project);
    return mgr.checkVcsIsActive(P4Vcs.VCS_NAME);
}
 
Example 3
Source File: DvcsUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@javax.annotation.Nullable
private static VirtualFile getVcsRootForLibraryFile(@Nonnull Project project, @Nonnull VirtualFile file) {
  ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
  // for a file inside .jar/.zip consider the .jar/.zip file itself
  VirtualFile root = vcsManager.getVcsRootFor(VfsUtilCore.getVirtualFileForJar(file));
  if (root != null) {
    LOGGER.debug("Found root for zip/jar file: " + root);
    return root;
  }

  // for other libs which don't have jars inside the project dir (such as JDK) take the owner module of the lib
  List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(file);
  Set<VirtualFile> libraryRoots = new HashSet<>();
  for (OrderEntry entry : entries) {
    if (entry instanceof LibraryOrderEntry || entry instanceof ModuleExtensionWithSdkOrderEntry) {
      VirtualFile moduleRoot = vcsManager.getVcsRootFor(entry.getOwnerModule().getModuleDir());
      if (moduleRoot != null) {
        libraryRoots.add(moduleRoot);
      }
    }
  }

  if (libraryRoots.size() == 0) {
    LOGGER.debug("No library roots");
    return null;
  }

  // if the lib is used in several modules, take the top module
  // (for modules of the same level we can't guess anything => take the first one)
  Iterator<VirtualFile> libIterator = libraryRoots.iterator();
  VirtualFile topLibraryRoot = libIterator.next();
  while (libIterator.hasNext()) {
    VirtualFile libRoot = libIterator.next();
    if (VfsUtilCore.isAncestor(libRoot, topLibraryRoot, true)) {
      topLibraryRoot = libRoot;
    }
  }
  LOGGER.debug("Several library roots, returning " + topLibraryRoot);
  return topLibraryRoot;
}
 
Example 4
Source File: AbstractShowDiffAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
protected static AbstractVcs isEnabled(final VcsContext vcsContext, @Nullable final VcsBackgroundableActions actionKey) {
  if (!(isVisible(vcsContext))) return null;

  final Project project = vcsContext.getProject();
  if (project == null) return null;
  final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);

  final VirtualFile[] selectedFilePaths = vcsContext.getSelectedFiles();
  if (selectedFilePaths == null || selectedFilePaths.length != 1) return null;

  final VirtualFile selectedFile = selectedFilePaths[0];
  if (selectedFile.isDirectory()) return null;

  if (actionKey != null) {
    final BackgroundableActionEnabledHandler handler = ((ProjectLevelVcsManagerImpl)vcsManager).getBackgroundableActionHandler(actionKey);
    if (handler.isInProgress(VcsBackgroundableActions.keyFrom(selectedFile))) return null;
  }

  final AbstractVcs vcs = vcsManager.getVcsFor(selectedFile);
  if (vcs == null) return null;

  final DiffProvider diffProvider = vcs.getDiffProvider();

  if (diffProvider == null) return null;

  if (AbstractVcs.fileInVcsByFileStatus(project, new FilePathImpl(selectedFile))) {
    return vcs;
  }
  return null;
}
 
Example 5
Source File: IgnoredFilesCompositeHolder.java    From consulo with Apache License 2.0 5 votes vote down vote up
public IgnoredFilesCompositeHolder(final Project project) {
  super();
  myProject = project;
  myVcsIgnoredHolderMap = new HashMap<>();
  myIdeIgnoredFilesHolder = new RecursiveFileHolder<>(myProject, HolderType.IGNORED);
  myVcsManager = ProjectLevelVcsManager.getInstance(myProject);
}
 
Example 6
Source File: VcsDataWrapper.java    From consulo with Apache License 2.0 5 votes vote down vote up
VcsDataWrapper(final AnActionEvent e) {
  final DataContext dataContext = e.getDataContext();
  myProject = dataContext.getData(CommonDataKeys.PROJECT);
  if (myProject == null || myProject.isDefault()) {
    myManager = null;
    myVcses = null;
    return;
  }
  myManager = ProjectLevelVcsManager.getInstance(myProject);
}
 
Example 7
Source File: BasicAction.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
/**
 * given a list of action-target files, returns ALL the files that should be
 * subject to the action Does not keep directories, but recursively adds
 * directory contents
 *
 * @param project the project subject of the action
 * @param files   the root selection
 * @return the complete set of files this action should apply to
 */
@NotNull
private List<VirtualFile> collectAffectedFiles(@NotNull Project project, @NotNull VirtualFile[] files) {
    List<VirtualFile> affectedFiles = new ArrayList<VirtualFile>(files.length);
    ProjectLevelVcsManager projectLevelVcsManager = ProjectLevelVcsManager.getInstance(project);
    for (VirtualFile file : files) {
        if (!file.isDirectory() && projectLevelVcsManager.getVcsFor(file) instanceof P4Vcs) {
            affectedFiles.add(file);
        } else if (file.isDirectory() && isRecursive()) {
            addChildren(project, affectedFiles, file);
        }
    }
    return affectedFiles;
}
 
Example 8
Source File: TfvcIntegrationEnabler.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
private void addVcsRoot(@NotNull VirtualFile root) {
    ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(myProject);
    List<VirtualFile> currentVcsRoots = Arrays.asList(vcsManager.getRootsUnderVcs(myVcs));

    List<VcsDirectoryMapping> mappings = new ArrayList<>(vcsManager.getDirectoryMappings(myVcs));
    if (!currentVcsRoots.contains(root)) {
        mappings.add(new VcsDirectoryMapping(root.getPath(), myVcs.getName()));
    }

    vcsManager.setDirectoryMappings(mappings);
}
 
Example 9
Source File: RemoteRevisionsNumbersCache.java    From consulo with Apache License 2.0 5 votes vote down vote up
RemoteRevisionsNumbersCache(final Project project) {
  myProject = project;
  myLock = new Object();
  myData = new HashMap<>();
  myRefreshingQueues = Collections.synchronizedMap(new HashMap<VcsRoot, LazyRefreshingSelfQueue<String>>());
  myLatestRevisionsMap = new HashMap<>();
  myLfs = LocalFileSystem.getInstance();
  myVcsManager = ProjectLevelVcsManager.getInstance(project);
  myVcsConfiguration = VcsConfiguration.getInstance(project);
}
 
Example 10
Source File: AbstractShowDiffAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void actionPerformed(VcsContext vcsContext) {
  final Project project = vcsContext.getProject();
  if (project == null) return;
  if (ChangeListManager.getInstance(project).isFreezedWithNotification("Can not " + vcsContext.getActionName() + " now")) return;
  final VirtualFile selectedFile = vcsContext.getSelectedFiles()[0];

  final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
  final AbstractVcs vcs = vcsManager.getVcsFor(selectedFile);
  final DiffProvider diffProvider = vcs.getDiffProvider();

  final DiffActionExecutor actionExecutor = getExecutor(diffProvider, selectedFile, project);
  actionExecutor.showDiff();
}
 
Example 11
Source File: VcsDirtyScopeVfsListener.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Inject
public VcsDirtyScopeVfsListener(@Nonnull Project project) {
  myVcsManager = ProjectLevelVcsManager.getInstance(project);

  VcsDirtyScopeManager dirtyScopeManager = VcsDirtyScopeManager.getInstance(project);

  myLock = new Object();
  myQueue = new ArrayList<>();
  myDirtReporter = () -> {
    ArrayList<FilesAndDirs> list;
    synchronized (myLock) {
      list = new ArrayList<>(myQueue);
      myQueue.clear();
    }

    HashSet<FilePath> dirtyFiles = new HashSet<>();
    HashSet<FilePath> dirtyDirs = new HashSet<>();
    for (FilesAndDirs filesAndDirs : list) {
      dirtyFiles.addAll(filesAndDirs.forcedNonRecursive);

      for (FilePath path : filesAndDirs.regular) {
        if (path.isDirectory()) {
          dirtyDirs.add(path);
        }
        else {
          dirtyFiles.add(path);
        }
      }
    }

    if (!dirtyFiles.isEmpty() || !dirtyDirs.isEmpty()) {
      dirtyScopeManager.filePathsDirty(dirtyFiles, dirtyDirs);
    }
  };
  myZipperUpdater = new ZipperUpdater(300, Alarm.ThreadToUse.POOLED_THREAD, this);
  Disposer.register(project, this);
  VirtualFileManager.getInstance().addAsyncFileListener(this, project);
}
 
Example 12
Source File: TFSVcs.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
public TFSVcs(@NotNull Project project) {
    super(project, TFVC_NAME);
    final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
    myAddConfirmation = vcsManager.getStandardConfirmation(VcsConfiguration.StandardConfirmation.ADD, this);
    myDeleteConfirmation = vcsManager.getStandardConfirmation(VcsConfiguration.StandardConfirmation.REMOVE, this);
    myCheckoutOptions = vcsManager.getStandardOption(VcsConfiguration.StandardOption.CHECKOUT, this);
}
 
Example 13
Source File: VcsHelper.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
/**
 * This method creates a RepositoryContext object from the local project context.
 * It works for TF Git or TFVC repositories. Any other type of repo will return null.
 *
 * @param project
 * @return
 */
public static RepositoryContext getRepositoryContext(final Project project) {
    ArgumentHelper.checkNotNull(project, "project");
    try {
        final String projectRootFolder = project.getBasePath();

        // Check the manager first since that's where we cache these things
        //TODO this cache doesn't include the current branch info that could have changed. We should probably only cache stuff for TFVC
        RepositoryContext context = RepositoryContextManager.getInstance().get(projectRootFolder);
        if (context != null) {
            logger.info("getRepositoryContext: cache hit: " + projectRootFolder);
            return context;
        }
        logger.info("getRepositoryContext: cache miss: " + projectRootFolder);

        final ProjectLevelVcsManager projectLevelVcsManager = ProjectLevelVcsManager.getInstance(project);
        // Check for Git, then TFVC
        if (projectLevelVcsManager.checkVcsIsActive(GitVcs.NAME)) {
            // It's Git, so get the repository and remote url to create the context from
            final GitRepository repository = getGitRepository(project);
            if (repository != null && TfGitHelper.isTfGitRepository(repository)) {
                final GitRemote gitRemote = TfGitHelper.getTfGitRemote(repository);
                final String gitRemoteUrl = Objects.requireNonNull(gitRemote.getFirstUrl());
                // TODO: Fix this HACK. There doesn't seem to be a clear way to get the full name of the current branch
                final String branch = GIT_BRANCH_PREFIX + GitBranchUtil.getDisplayableBranchText(repository);
                context = RepositoryContext.createGitContext(projectRootFolder, repository.getRoot().getName(), branch, URI.create(gitRemoteUrl));
            }
        } else if (projectLevelVcsManager.checkVcsIsActive(TFSVcs.TFVC_NAME)) {
            final Workspace workspace = CommandUtils.getPartialWorkspace(project, false);
            if (workspace != null) {
                final String projectName = getTeamProjectFromTfvcServerPath(
                        workspace.getMappings().size() > 0 ? workspace.getMappings().get(0).getServerPath() : null);
                context = RepositoryContext.createTfvcContext(projectRootFolder, workspace.getName(), projectName, workspace.getServerUri());
            }
        }

        if (context != null) {
            RepositoryContextManager.getInstance().add(context);
            return context;
        }
    } catch (Throwable t) {
        // Don't let errors bubble out here, just return null if something goes wrong
        logger.warn("Unable to get repository context for the project.", t);
    }

    logger.info("getRepositoryContext: We couldn't determine the VCS provider, so returning null.");
    return null;
}
 
Example 14
Source File: AbstractIgnoredFilesHolder.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected AbstractIgnoredFilesHolder(Project project) {
  myProject = project;
  myVcsManager = ProjectLevelVcsManager.getInstance(project);
}
 
Example 15
Source File: SortByVcsRoots.java    From consulo with Apache License 2.0 4 votes vote down vote up
public SortByVcsRoots(Project project, final Convertor<T, FilePath> convertor) {
  myProject = project;
  myVcsManager = ProjectLevelVcsManager.getInstance(project);
  myConvertor = convertor;
}
 
Example 16
Source File: BackgroundableActionLock.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static ProjectLevelVcsManagerImpl getManager(@Nonnull Project project) {
  return (ProjectLevelVcsManagerImpl)ProjectLevelVcsManager.getInstance(project);
}
 
Example 17
Source File: VcsDirtyScopeImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
public VcsDirtyScopeImpl(final AbstractVcs vcs, final Project project) {
  myProject = project;
  myVcs = vcs;
  myVcsManager = ProjectLevelVcsManager.getInstance(project);
  myWasEverythingDirty = false;
  myVcsDirtyScopeModifier = new VcsDirtyScopeModifier() {
    @Override
    public Collection<VirtualFile> getAffectedVcsRoots() {
      return Collections.unmodifiableCollection(myDirtyDirectoriesRecursively.keySet());
    }

    @Override
    public Iterator<FilePath> getDirtyFilesIterator() {
      if (myDirtyFiles.isEmpty()) {
        return Collections.<FilePath>emptyList().iterator();
      }
      final ArrayList<Iterator<FilePath>> iteratorList = new ArrayList<>(myDirtyFiles.size());
      for (THashSet<FilePath> paths : myDirtyFiles.values()) {
        iteratorList.add(paths.iterator());
      }
      return ContainerUtil.concatIterators(iteratorList);
    }

    @Nonnull
    @Override
    public Iterator<FilePath> getDirtyDirectoriesIterator(final VirtualFile root) {
      final THashSet<FilePath> filePaths = myDirtyDirectoriesRecursively.get(root);
      if (filePaths != null) {
        return filePaths.iterator();
      }
      return ContainerUtil.emptyIterator();
    }

    @Override
    public void recheckDirtyKeys() {
      recheckMap(myDirtyDirectoriesRecursively);
      recheckMap(myDirtyFiles);
    }

    private void recheckMap(Map<VirtualFile, THashSet<FilePath>> map) {
      for (Iterator<THashSet<FilePath>> iterator = map.values().iterator(); iterator.hasNext(); ) {
        final THashSet<FilePath> next = iterator.next();
        if (next.isEmpty()) {
          iterator.remove();
        }
      }
    }
  };
}
 
Example 18
Source File: MockDirtyScope.java    From consulo with Apache License 2.0 4 votes vote down vote up
public MockDirtyScope(@Nonnull Project project, @Nonnull AbstractVcs vcs) {
  myProject = project;
  myVcs = vcs;
  myVcsManager = ProjectLevelVcsManager.getInstance(myProject);
}
 
Example 19
Source File: DvcsUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static void addMappingIfSubRoot(@Nonnull Project project, @Nonnull String newRepositoryPath, @Nonnull String vcsName) {
  if (project.getBasePath() != null && FileUtil.isAncestor(project.getBasePath(), newRepositoryPath, true)) {
    ProjectLevelVcsManager manager = ProjectLevelVcsManager.getInstance(project);
    manager.setDirectoryMappings(VcsUtil.addMapping(manager.getDirectoryMappings(), newRepositoryPath, vcsName));
  }
}
 
Example 20
Source File: DvcsUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
private static VirtualFile guessRootForVcs(@Nonnull Project project, @javax.annotation.Nullable AbstractVcs vcs, @Nullable String defaultRootPathValue) {
  if (project.isDisposed()) return null;
  LOG.debug("Guessing vcs root...");
  ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
  if (vcs == null) {
    LOG.debug("Vcs not found.");
    return null;
  }
  String vcsName = vcs.getDisplayName();
  VirtualFile[] vcsRoots = vcsManager.getRootsUnderVcs(vcs);
  if (vcsRoots.length == 0) {
    LOG.debug("No " + vcsName + " roots in the project.");
    return null;
  }

  if (vcsRoots.length == 1) {
    VirtualFile onlyRoot = vcsRoots[0];
    LOG.debug("Only one " + vcsName + " root in the project, returning: " + onlyRoot);
    return onlyRoot;
  }

  // get remembered last visited repository root
  if (defaultRootPathValue != null) {
    VirtualFile recentRoot = VcsUtil.getVirtualFile(defaultRootPathValue);
    if (recentRoot != null) {
      LOG.debug("Returning the recent root: " + recentRoot);
      return recentRoot;
    }
  }

  // otherwise return the root of the project dir or the root containing the project dir, if there is such
  VirtualFile projectBaseDir = project.getBaseDir();
  if (projectBaseDir == null) {
    VirtualFile firstRoot = vcsRoots[0];
    LOG.debug("Project base dir is null, returning the first root: " + firstRoot);
    return firstRoot;
  }
  VirtualFile rootCandidate;
  for (VirtualFile root : vcsRoots) {
    if (root.equals(projectBaseDir) || VfsUtilCore.isAncestor(root, projectBaseDir, true)) {
      LOG.debug("The best candidate: " + root);
      return root;
    }
  }
  rootCandidate = vcsRoots[0];
  LOG.debug("Returning the best candidate: " + rootCandidate);
  return rootCandidate;
}