git4idea.branch.GitBranchUtil Java Examples

The following examples show how to use git4idea.branch.GitBranchUtil. 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: CommitMessage.java    From GitCommitMessage with Apache License 2.0 6 votes vote down vote up
public static String extractBranchName(Project project) {
    String branch = "";
    ProjectLevelVcsManager instance = ProjectLevelVcsManagerImpl.getInstance(project);
    if (instance.checkVcsIsActive("Git")) {
        GitLocalBranch currentBranch = GitBranchUtil.getCurrentRepository(project).getCurrentBranch();

        if (currentBranch != null) {
            // Branch name  matches Ticket Name
            branch = currentBranch.getName().trim();
        }
    } else if (instance.checkVcsIsActive("Mercurial")) {
        branch = HgUtil.getCurrentRepository(project).getCurrentBranch().trim();
    }

    return branch;
}
 
Example #2
Source File: NodeDecorationBase.java    From GitToolBox with Apache License 2.0 5 votes vote down vote up
@NotNull
protected final String getBranchText() {
  if (repo.getState() == State.NORMAL) {
    return getNormalStateBranchText();
  } else if (repo.getState() == State.DETACHED) {
    return getDetachedStateBranchText();
  }
  return GitBranchUtil.getDisplayableBranchText(repo);
}
 
Example #3
Source File: NodeDecorationBase.java    From GitToolBox with Apache License 2.0 5 votes vote down vote up
@NotNull
private String getNormalStateBranchText() {
  RepoStatus status = repoInfo.status();
  if (status.isParentDifferentFromTracking()) {
    if (status.parentBranch() != null) {
      return getParentBranchText(status);
    }
  } else if (status.localBranch() != null) {
    return status.localBranch().getName();
  }
  return GitBranchUtil.getDisplayableBranchText(repo);
}
 
Example #4
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 #5
Source File: StatusBarManagerTest.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
@Before
public void setupLocalTests() {
    MockitoAnnotations.initMocks(this);
    buildStatusLookupOperation = new MyBuildStatusLookupOperation();

    PowerMockito.mockStatic(WindowManager.class);
    when(WindowManager.getInstance()).thenReturn(windowManager);
    when(windowManager.getStatusBar(any(Project.class))).thenReturn(statusBar);
    when(statusBar.getWidget(anyString()))
            .thenReturn(null) // First time return null
            .thenReturn(new BuildWidget()); // All other calls should return something other than null
    doNothing().when(statusBar).addWidget(any(StatusBarWidget.class));
    doNothing().when(statusBar).updateWidget(anyString());

    PowerMockito.mockStatic(ProjectManager.class);
    when(ProjectManager.getInstance()).thenReturn(projectManager);
    when(projectManager.getOpenProjects()).thenReturn(new Project[]{project});

    PowerMockito.mockStatic(VcsHelper.class);
    when(VcsHelper.getRepositoryContext(any(Project.class)))
            .thenReturn(
                    RepositoryContext.createGitContext(
                            "/root/one",
                            "repo1",
                            "branch1",
                            URI.create("http://repoUrl1")));

    PowerMockito.mockStatic(GitBranchUtil.class);
    when(GitBranchUtil.getCurrentRepository(any(Project.class))).thenReturn(gitRepository);
    when(GitBranchUtil.getDisplayableBranchText(any(GitRepository.class))).thenReturn("branch");

    when(applicationNamesInfo.getProductName()).thenReturn("IDEA");
    PowerMockito.mockStatic(ApplicationNamesInfo.class);
    when(ApplicationNamesInfo.getInstance()).thenReturn(applicationNamesInfo);

    when(gitRepository.getRemotes()).thenReturn(Collections.singletonList(
            new GitRemote("origin", Collections.singletonList("https://test.visualstudio.com/"),
                    Collections.singletonList("https://test.visualstudio.com/"),
                    Collections.singletonList("https://test.visualstudio.com/"),
                    Collections.singletonList("https://test.visualstudio.com/"))));

    PowerMockito.mockStatic(OperationFactory.class);
    when(OperationFactory.createBuildStatusLookupOperation(any(RepositoryContext.class), anyBoolean())).thenReturn(buildStatusLookupOperation);
}
 
Example #6
Source File: CurrentBranchCompletionProvider.java    From GitToolBox with Apache License 2.0 4 votes vote down vote up
@NotNull
private static Function<GitRepository, Pair<String, String>> getGitRepositoryNames() {
  return repo -> Pair.create(GitBranchUtil.getDisplayableBranchText(repo), GtUtil.name(repo));
}
 
Example #7
Source File: NodeDecorationBase.java    From GitToolBox with Apache License 2.0 4 votes vote down vote up
@NotNull
private String getDetachedStateBranchText() {
  RepoStatus status = repoInfo.status();
  return status.localShortHash() == null ? GitBranchUtil.getDisplayableBranchText(repo) : status.localShortHash();
}