Java Code Examples for git4idea.repo.GitRepository#getRoot()

The following examples show how to use git4idea.repo.GitRepository#getRoot() . 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: DiffCompareInfoProvider.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
private GitCommitCompareInfo getCompareInfo(final Project project, final GitRepository gitRepository,
                                            final String source, final String target)
        throws VcsException {
    final VirtualFile root = gitRepository.getRoot();
    final List<GitCommit> commits1 = getUtilWrapper().history(project, root, ".." + target);
    final List<GitCommit> commits2 = getUtilWrapper().history(project, root, target + "..");

    final Collection<Change> diff = getUtilWrapper().getDiff(project, root, target, source);
    final GitCommitCompareInfo info = new GitCommitCompareInfo(GitCommitCompareInfo.InfoType.BRANCH_TO_HEAD);

    info.put(gitRepository, diff);
    info.put(gitRepository, new Pair<List<GitCommit>, List<GitCommit>>(commits1, commits2));

    return info;
}
 
Example 2
Source File: OpenFileInBrowserAction.java    From azure-devops-intellij with MIT License 5 votes vote down vote up
@Override
public void actionPerformed(final AnActionEvent anActionEvent) {
    final Project project = anActionEvent.getRequiredData(CommonDataKeys.PROJECT);
    final VirtualFile virtualFile = anActionEvent.getRequiredData(CommonDataKeys.VIRTUAL_FILE);

    final GitRepositoryManager manager = GitUtil.getRepositoryManager(project);
    final GitRepository gitRepository = manager.getRepositoryForFileQuick(virtualFile);
    if (gitRepository == null)
        return;

    final GitRemote gitRemote = TfGitHelper.getTfGitRemote(gitRepository);

    // guard for null so findbugs doesn't complain
    if (gitRemote == null || gitRepository.getRoot() == null) {
        return;
    }

    final String rootPath = gitRepository.getRoot().getPath();
    final String path = virtualFile.getPath();
    final String relativePath = path.substring(rootPath.length());

    String gitRemoteBranchName = StringUtils.EMPTY;
    final GitLocalBranch gitLocalBranch = gitRepository.getCurrentBranch();
    if (gitLocalBranch != null) {
        final GitRemoteBranch gitRemoteBranch = gitLocalBranch.findTrackedBranch(gitRepository);
        if (gitRemoteBranch != null) {
            gitRemoteBranchName = gitRemoteBranch.getNameForRemoteOperations();
        }
    }

    final URI urlToBrowseTo = UrlHelper.getFileURI(gitRemote.getFirstUrl(), encodeVirtualFilePath(relativePath), gitRemoteBranchName);
    logger.info("Browsing to url " + urlToBrowseTo.getPath());
    BrowserUtil.browse(urlToBrowseTo);
}
 
Example 3
Source File: GitStatusCalculator.java    From GitToolBox with Apache License 2.0 5 votes vote down vote up
private GitLineHandler prepareLineHandler(String localRef, String remoteRef, GitRepository repository) {
  String branches = localRef + "..." + remoteRef;
  final GitLineHandler handler = new GitLineHandler(project, repository.getRoot(), GitCommand.REV_LIST);
  handler.addParameters(branches, "--left-right");
  log.debug("Prepared count with refs: '", branches, "'");
  return handler;
}
 
Example 4
Source File: VirtualFileRepoCache.java    From GitToolBox with Apache License 2.0 5 votes vote down vote up
@Nullable
default VirtualFile getRepoRootForFile(@NotNull VirtualFile file) {
  GitRepository repo = getRepoForFile(file);
  if (repo != null) {
    return repo.getRoot();
  }
  return null;
}
 
Example 5
Source File: ImportPageModelImpl.java    From azure-devops-intellij with MIT License 4 votes vote down vote up
private boolean setupRemoteOnLocalRepo(final Project project, final GitRepository localRepository,
                                       final com.microsoft.alm.sourcecontrol.webapi.model.GitRepository remoteRepository,
                                       final ServerContext localContext, final ProgressIndicator indicator) {
    //get remotes on local repository
    indicator.setText(TfPluginBundle.message(TfPluginBundle.KEY_IMPORT_GIT_REMOTE));
    final Collection<GitRemote> gitRemotes = localRepository.getRemotes();
    final List<String> remoteParams = new ArrayList<String>();

    if (!gitRemotes.isEmpty()) {
        for (GitRemote remote : gitRemotes) {
            if (StringUtils.equalsIgnoreCase(remote.getName(), REMOTE_ORIGIN)) {
                //remote named origin exits, ask user if they want to overwrite it and proceed or cancel
                IdeaHelper.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        final boolean replaceOrigin = IdeaHelper.showConfirmationDialog(project,
                                TfPluginBundle.message(TfPluginBundle.KEY_IMPORT_ORIGIN_EXISTS),
                                TfPluginBundle.message(TfPluginBundle.KEY_IMPORT_DIALOG_TITLE),
                                Icons.VSLogoSmall,
                                TfPluginBundle.message(TfPluginBundle.KEY_IMPORT_UPDATE_ORIGIN),
                                TfPluginBundle.message(TfPluginBundle.KEY_IMPORT_CANCEL));
                        if (replaceOrigin) {
                            remoteParams.add("set-url");
                        }
                    }
                }, true, indicator.getModalityState());
                if (remoteParams.size() == 0) {
                    //user chose to cancel import
                    logger.warn("setupRemoteOnLocalRepo: User chose to cancel import for project: {}, local repo: {}",
                            project.getName(), localRepository.getGitDir().getUrl());
                    notifyImportError(project, TfPluginBundle.message(TfPluginBundle.KEY_IMPORT_CANCELED));
                    return false;
                }
                break;
            }
        }
    }

    final String remoteGitUrl = UrlHelper.getCmdLineFriendlyUrl(remoteRepository.getRemoteUrl());
    //update remotes on local repository
    final GitSimpleHandler hRemote = new GitSimpleHandler(project, localRepository.getRoot(), GitCommand.REMOTE);
    hRemote.setSilent(true);
    if (remoteParams.size() == 1) {
        hRemote.addParameters(remoteParams.get(0), REMOTE_ORIGIN, remoteGitUrl);
    } else {
        hRemote.addParameters("add", REMOTE_ORIGIN, remoteGitUrl);
    }

    GitHandlerUtil.runInCurrentThread(hRemote, null, true, TfPluginBundle.message(TfPluginBundle.KEY_IMPORT_GIT_REMOTE));
    if (hRemote.getExitCode() != 0) {
        logger.error("setupRemoteOnLocalRepo: git remote failed for project: {}, local repo: {}, error: {}, output: {}",
                project.getName(), localRepository.getRoot().getUrl(), hRemote.getStderr(), hRemote.getStdout());
        notifyImportError(project,
                TfPluginBundle.message(TfPluginBundle.KEY_IMPORT_GIT_REMOTE_ERROR, remoteGitUrl, hRemote.getStderr()));
        return false;
    }
    return true;
}