Search in sources :

Example 16 with GitRemote

use of git4idea.repo.GitRemote in project intellij-community by JetBrains.

the class GitPull method displayDialog.

@Override
protected DialogState displayDialog(@NotNull Project project, @NotNull List<VirtualFile> gitRoots, @NotNull VirtualFile defaultRoot) {
    final GitPullDialog dialog = new GitPullDialog(project, gitRoots, defaultRoot);
    if (!dialog.showAndGet()) {
        return null;
    }
    GitRepositoryManager repositoryManager = GitUtil.getRepositoryManager(project);
    GitRepository repository = repositoryManager.getRepositoryForRoot(dialog.gitRoot());
    assert repository != null : "Repository can't be null for root " + dialog.gitRoot();
    String remoteOrUrl = dialog.getRemote();
    if (remoteOrUrl == null) {
        return null;
    }
    GitRemote remote = GitUtil.findRemoteByName(repository, remoteOrUrl);
    final List<String> urls = remote == null ? Collections.singletonList(remoteOrUrl) : remote.getUrls();
    Computable<GitLineHandler> handlerProvider = new Computable<GitLineHandler>() {

        @Override
        public GitLineHandler compute() {
            return dialog.makeHandler(urls);
        }
    };
    return new DialogState(dialog.gitRoot(), GitBundle.message("pulling.title", dialog.getRemote()), handlerProvider);
}
Also used : GitRemote(git4idea.repo.GitRemote) GitRepository(git4idea.repo.GitRepository) GitPullDialog(git4idea.merge.GitPullDialog) GitLineHandler(git4idea.commands.GitLineHandler) GitRepositoryManager(git4idea.repo.GitRepositoryManager) Computable(com.intellij.openapi.util.Computable)

Example 17 with GitRemote

use of git4idea.repo.GitRemote in project intellij-community by JetBrains.

the class GitPushTarget method getFromPushSpec.

@Nullable
public static GitPushTarget getFromPushSpec(@NotNull GitRepository repository, @NotNull GitLocalBranch sourceBranch) {
    final GitRemote remote = getRemoteToPush(repository, GitBranchUtil.getTrackInfoForBranch(repository, sourceBranch));
    if (remote == null)
        return null;
    List<String> specs = remote.getPushRefSpecs();
    if (specs.isEmpty())
        return null;
    String targetRef = GitPushSpecParser.getTargetRef(repository, sourceBranch.getName(), specs);
    if (targetRef == null)
        return null;
    String remotePrefix = REFS_REMOTES_PREFIX + remote.getName() + "/";
    if (targetRef.startsWith(remotePrefix)) {
        targetRef = targetRef.substring(remotePrefix.length());
        GitRemoteBranch remoteBranch = GitUtil.findOrCreateRemoteBranch(repository, remote, targetRef);
        boolean existingBranch = repository.getBranches().getRemoteBranches().contains(remoteBranch);
        return new GitPushTarget(remoteBranch, !existingBranch, false);
    } else {
        GitRemoteBranch remoteBranch = new GitSpecialRefRemoteBranch(targetRef, remote);
        return new GitPushTarget(remoteBranch, true, true);
    }
}
Also used : GitRemote(git4idea.repo.GitRemote) GitRemoteBranch(git4idea.GitRemoteBranch) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with GitRemote

use of git4idea.repo.GitRemote in project intellij-community by JetBrains.

the class GitRefManager method groupForBranchFilter.

@NotNull
@Override
public List<RefGroup> groupForBranchFilter(@NotNull Collection<VcsRef> refs) {
    List<RefGroup> simpleGroups = ContainerUtil.newArrayList();
    List<VcsRef> localBranches = ContainerUtil.newArrayList();
    List<VcsRef> trackedBranches = ContainerUtil.newArrayList();
    MultiMap<GitRemote, VcsRef> remoteRefGroups = MultiMap.create();
    MultiMap<VirtualFile, VcsRef> refsByRoot = groupRefsByRoot(refs);
    for (Map.Entry<VirtualFile, Collection<VcsRef>> entry : refsByRoot.entrySet()) {
        VirtualFile root = entry.getKey();
        List<VcsRef> refsInRoot = ContainerUtil.sorted(entry.getValue(), myLabelsComparator);
        GitRepository repository = myRepositoryManager.getRepositoryForRoot(root);
        if (repository == null) {
            LOG.warn("No repository for root: " + root);
            continue;
        }
        Set<String> locals = getLocalBranches(repository);
        Set<String> tracked = getTrackedRemoteBranches(repository);
        Map<String, GitRemote> allRemote = getAllRemoteBranches(repository);
        for (VcsRef ref : refsInRoot) {
            if (ref.getType() == HEAD) {
                simpleGroups.add(new SingletonRefGroup(ref));
                continue;
            }
            String refName = ref.getName();
            if (locals.contains(refName)) {
                localBranches.add(ref);
            } else if (allRemote.containsKey(refName)) {
                remoteRefGroups.putValue(allRemote.get(refName), ref);
                if (tracked.contains(refName)) {
                    trackedBranches.add(ref);
                }
            } else {
                LOG.debug("Didn't find ref neither in local nor in remote branches: " + ref);
            }
        }
    }
    List<RefGroup> result = ContainerUtil.newArrayList();
    result.addAll(simpleGroups);
    if (!localBranches.isEmpty())
        result.add(new LogicalRefGroup("Local", localBranches));
    if (!trackedBranches.isEmpty())
        result.add(new LogicalRefGroup("Tracked", trackedBranches));
    for (Map.Entry<GitRemote, Collection<VcsRef>> entry : remoteRefGroups.entrySet()) {
        final GitRemote remote = entry.getKey();
        final Collection<VcsRef> branches = entry.getValue();
        result.add(new RemoteRefGroup(remote, branches));
    }
    return result;
}
Also used : GitRemote(git4idea.repo.GitRemote) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SimpleRefGroup(com.intellij.vcs.log.impl.SimpleRefGroup) SingletonRefGroup(com.intellij.vcs.log.impl.SingletonRefGroup) GitRepository(git4idea.repo.GitRepository) MultiMap(com.intellij.util.containers.MultiMap) SingletonRefGroup(com.intellij.vcs.log.impl.SingletonRefGroup) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with GitRemote

use of git4idea.repo.GitRemote in project intellij-community by JetBrains.

the class GitRefManager method createTrackedGroup.

@Nullable
private static SimpleRefGroup createTrackedGroup(@NotNull GitRepository repository, @NotNull Collection<VcsRef> references, @NotNull VcsRef localRef) {
    List<VcsRef> remoteBranches = ContainerUtil.filter(references, ref -> ref.getType().equals(REMOTE_BRANCH));
    GitBranchTrackInfo trackInfo = ContainerUtil.find(repository.getBranchTrackInfos(), info -> info.getLocalBranch().getName().equals(localRef.getName()));
    if (trackInfo != null) {
        VcsRef trackedRef = ContainerUtil.find(remoteBranches, ref -> ref.getName().equals(trackInfo.getRemoteBranch().getName()));
        if (trackedRef != null) {
            return new SimpleRefGroup(trackInfo.getRemote().getName() + REMOTE_TABLE_SEPARATOR + localRef.getName(), ContainerUtil.newArrayList(localRef, trackedRef));
        }
    }
    List<VcsRef> trackingCandidates = ContainerUtil.filter(remoteBranches, ref -> ref.getName().endsWith(SEPARATOR + localRef.getName()));
    for (GitRemote remote : repository.getRemotes()) {
        for (VcsRef candidate : trackingCandidates) {
            if (candidate.getName().equals(remote.getName() + SEPARATOR + localRef.getName())) {
                return new SimpleRefGroup(remote.getName() + REMOTE_TABLE_SEPARATOR + localRef.getName(), ContainerUtil.newArrayList(localRef, candidate));
            }
        }
    }
    return null;
}
Also used : GitRemote(git4idea.repo.GitRemote) GitBranchTrackInfo(git4idea.repo.GitBranchTrackInfo) SimpleRefGroup(com.intellij.vcs.log.impl.SimpleRefGroup) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with GitRemote

use of git4idea.repo.GitRemote in project intellij-community by JetBrains.

the class GitPullDialog method getCurrentOrDefaultRemote.

/**
   * If the current branch is a tracking branch, returns its remote.
   * Otherwise tries to guess: if there is origin, returns origin, otherwise returns the first remote in the list.
   */
@Nullable
private static GitRemote getCurrentOrDefaultRemote(@NotNull GitRepository repository) {
    Collection<GitRemote> remotes = repository.getRemotes();
    if (remotes.isEmpty()) {
        return null;
    }
    GitBranchTrackInfo trackInfo = GitUtil.getTrackInfoForCurrentBranch(repository);
    if (trackInfo != null) {
        return trackInfo.getRemote();
    } else {
        GitRemote origin = GitUtil.getDefaultRemote(remotes);
        if (origin != null) {
            return origin;
        } else {
            return remotes.iterator().next();
        }
    }
}
Also used : GitRemote(git4idea.repo.GitRemote) GitBranchTrackInfo(git4idea.repo.GitBranchTrackInfo) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

GitRemote (git4idea.repo.GitRemote)22 NotNull (org.jetbrains.annotations.NotNull)8 GitRepository (git4idea.repo.GitRepository)7 Nullable (org.jetbrains.annotations.Nullable)7 GitRemoteBranch (git4idea.GitRemoteBranch)3 GitBranchTrackInfo (git4idea.repo.GitBranchTrackInfo)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 SimpleRefGroup (com.intellij.vcs.log.impl.SimpleRefGroup)2 GitLocalBranch (git4idea.GitLocalBranch)2 GitStandardRemoteBranch (git4idea.GitStandardRemoteBranch)2 Computable (com.intellij.openapi.util.Computable)1 VcsException (com.intellij.openapi.vcs.VcsException)1 MultiMap (com.intellij.util.containers.MultiMap)1 SingletonRefGroup (com.intellij.vcs.log.impl.SingletonRefGroup)1 Git (git4idea.commands.Git)1 GitCommandResult (git4idea.commands.GitCommandResult)1 GitCompoundResult (git4idea.commands.GitCompoundResult)1 GitLineHandler (git4idea.commands.GitLineHandler)1 GitPullDialog (git4idea.merge.GitPullDialog)1 GitRepositoryManager (git4idea.repo.GitRepositoryManager)1