Search in sources :

Example 6 with GitLocalBranch

use of git4idea.GitLocalBranch in project intellij-community by JetBrains.

the class GitConfig method convertBranchConfig.

@Nullable
private static GitBranchTrackInfo convertBranchConfig(@Nullable BranchConfig branchConfig, @NotNull Collection<GitLocalBranch> localBranches, @NotNull Collection<GitRemoteBranch> remoteBranches) {
    if (branchConfig == null) {
        return null;
    }
    final String branchName = branchConfig.getName();
    String remoteName = branchConfig.getBean().getRemote();
    String mergeName = branchConfig.getBean().getMerge();
    String rebaseName = branchConfig.getBean().getRebase();
    if (StringUtil.isEmptyOrSpaces(mergeName) && StringUtil.isEmptyOrSpaces(rebaseName)) {
        LOG.info("No branch." + branchName + ".merge/rebase item in the .git/config");
        return null;
    }
    if (StringUtil.isEmptyOrSpaces(remoteName)) {
        LOG.info("No branch." + branchName + ".remote item in the .git/config");
        return null;
    }
    boolean merge = mergeName != null;
    final String remoteBranchName = StringUtil.unquoteString(merge ? mergeName : rebaseName);
    GitLocalBranch localBranch = findLocalBranch(branchName, localBranches);
    GitRemoteBranch remoteBranch = findRemoteBranch(remoteBranchName, remoteName, remoteBranches);
    if (localBranch == null || remoteBranch == null) {
        // obsolete record in .git/config: local or remote branch doesn't exist, but the tracking information wasn't removed
        LOG.debug("localBranch: " + localBranch + ", remoteBranch: " + remoteBranch);
        return null;
    }
    return new GitBranchTrackInfo(localBranch, remoteBranch, merge);
}
Also used : GitLocalBranch(git4idea.GitLocalBranch) GitRemoteBranch(git4idea.GitRemoteBranch) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with GitLocalBranch

use of git4idea.GitLocalBranch in project intellij-community by JetBrains.

the class GitBranchWorker method checkoutNewBranch.

public void checkoutNewBranch(@NotNull final String name, @NotNull List<GitRepository> repositories) {
    updateInfo(repositories);
    repositories = ContainerUtil.filter(repositories, new Condition<GitRepository>() {

        @Override
        public boolean value(GitRepository repository) {
            GitLocalBranch currentBranch = repository.getCurrentBranch();
            return currentBranch == null || !currentBranch.getName().equals(name);
        }
    });
    if (!repositories.isEmpty()) {
        new GitCheckoutNewBranchOperation(myProject, myGit, myUiHandler, repositories, name).execute();
    } else {
        LOG.error("Creating new branch the same as current in all repositories: " + name);
    }
}
Also used : Condition(com.intellij.openapi.util.Condition) GitRepository(git4idea.repo.GitRepository) GitLocalBranch(git4idea.GitLocalBranch)

Example 8 with GitLocalBranch

use of git4idea.GitLocalBranch in project intellij-community by JetBrains.

the class GithubCreatePullRequestWorker method create.

@Nullable
public static GithubCreatePullRequestWorker create(@NotNull final Project project, @Nullable final VirtualFile file) {
    return GithubUtil.computeValueInModal(project, "Loading data...", indicator -> {
        Git git = ServiceManager.getService(Git.class);
        GitRepository gitRepository = GithubUtil.getGitRepository(project, file);
        if (gitRepository == null) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "Can't find git repository");
            return null;
        }
        gitRepository.update();
        Pair<GitRemote, String> remote = GithubUtil.findGithubRemote(gitRepository);
        if (remote == null) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "Can't find GitHub remote");
            return null;
        }
        String remoteName = remote.getFirst().getName();
        String remoteUrl = remote.getSecond();
        GithubFullPath path = GithubUrlUtil.getUserAndRepositoryFromRemoteUrl(remoteUrl);
        if (path == null) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "Can't process remote: " + remoteUrl);
            return null;
        }
        GitLocalBranch currentBranch = gitRepository.getCurrentBranch();
        if (currentBranch == null) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, "No current branch");
            return null;
        }
        GithubAuthDataHolder authHolder;
        try {
            authHolder = GithubUtil.getValidAuthDataHolderFromConfig(project, AuthLevel.LOGGED, indicator);
        } catch (IOException e) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, e);
            return null;
        }
        GithubCreatePullRequestWorker worker = new GithubCreatePullRequestWorker(project, git, gitRepository, authHolder, path, remoteName, remoteUrl, currentBranch.getName());
        try {
            worker.initForks(indicator);
        } catch (IOException e) {
            GithubNotifications.showError(project, CANNOT_CREATE_PULL_REQUEST, e);
            return null;
        }
        return worker;
    });
}
Also used : GitRemote(git4idea.repo.GitRemote) GitRepository(git4idea.repo.GitRepository) GitLocalBranch(git4idea.GitLocalBranch) Git(git4idea.commands.Git) IOException(java.io.IOException) GithubFullPath(org.jetbrains.plugins.github.api.GithubFullPath) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GitLocalBranch

use of git4idea.GitLocalBranch in project intellij-community by JetBrains.

the class GitPushOperation method push.

@NotNull
private Map<GitRepository, GitPushRepoResult> push(@NotNull Collection<GitRepository> repositories) {
    Map<GitRepository, GitPushRepoResult> results = ContainerUtil.newLinkedHashMap();
    for (GitRepository repository : repositories) {
        PushSpec<GitPushSource, GitPushTarget> spec = myPushSpecs.get(repository);
        ResultWithOutput resultWithOutput = doPush(repository, spec);
        LOG.debug("Pushed to " + DvcsUtil.getShortRepositoryName(repository) + ": " + resultWithOutput);
        GitLocalBranch source = spec.getSource().getBranch();
        GitPushTarget target = spec.getTarget();
        GitPushRepoResult repoResult;
        if (resultWithOutput.isError()) {
            repoResult = GitPushRepoResult.error(source, target.getBranch(), resultWithOutput.getErrorAsString());
        } else {
            List<GitPushNativeResult> result = resultWithOutput.parsedResults;
            final GitPushNativeResult branchResult = getBranchResult(result);
            if (branchResult == null) {
                LOG.error("No result for branch among: [" + result + "]\n" + "Full result: " + resultWithOutput);
                continue;
            }
            List<GitPushNativeResult> tagResults = ContainerUtil.filter(result, new Condition<GitPushNativeResult>() {

                @Override
                public boolean value(GitPushNativeResult result) {
                    return !result.equals(branchResult) && (result.getType() == GitPushNativeResult.Type.NEW_REF || result.getType() == GitPushNativeResult.Type.FORCED_UPDATE);
                }
            });
            int commits = collectNumberOfPushedCommits(repository.getRoot(), branchResult);
            repoResult = GitPushRepoResult.convertFromNative(branchResult, tagResults, commits, source, target.getBranch());
        }
        LOG.debug("Converted result: " + repoResult);
        results.put(repository, repoResult);
    }
    // fill other not-processed repositories as not-pushed
    for (GitRepository repository : repositories) {
        if (!results.containsKey(repository)) {
            PushSpec<GitPushSource, GitPushTarget> spec = myPushSpecs.get(repository);
            results.put(repository, GitPushRepoResult.notPushed(spec.getSource().getBranch(), spec.getTarget().getBranch()));
        }
    }
    return results;
}
Also used : GitLocalBranch(git4idea.GitLocalBranch) GitRepository(git4idea.repo.GitRepository) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with GitLocalBranch

use of git4idea.GitLocalBranch in project intellij-community by JetBrains.

the class GitPushOperation method doPush.

@NotNull
private ResultWithOutput doPush(@NotNull GitRepository repository, @NotNull PushSpec<GitPushSource, GitPushTarget> pushSpec) {
    GitPushTarget target = pushSpec.getTarget();
    GitLocalBranch sourceBranch = pushSpec.getSource().getBranch();
    GitRemoteBranch targetBranch = target.getBranch();
    GitLineHandlerListener progressListener = GitStandardProgressAnalyzer.createListener(myProgressIndicator);
    boolean setUpstream = pushSpec.getTarget().isNewBranchCreated() && !branchTrackingInfoIsSet(repository, sourceBranch);
    String tagMode = myTagMode == null ? null : myTagMode.getArgument();
    String spec = sourceBranch.getFullName() + ":" + targetBranch.getNameForRemoteOperations();
    GitCommandResult res = myGit.push(repository, targetBranch.getRemote(), spec, myForce, setUpstream, tagMode, progressListener);
    return new ResultWithOutput(res);
}
Also used : GitLocalBranch(git4idea.GitLocalBranch) GitLineHandlerListener(git4idea.commands.GitLineHandlerListener) GitCommandResult(git4idea.commands.GitCommandResult) GitRemoteBranch(git4idea.GitRemoteBranch) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GitLocalBranch (git4idea.GitLocalBranch)10 GitRepository (git4idea.repo.GitRepository)5 NotNull (org.jetbrains.annotations.NotNull)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 GitRemoteBranch (git4idea.GitRemoteBranch)2 GitBranchTrackInfo (git4idea.repo.GitBranchTrackInfo)2 GitRemote (git4idea.repo.GitRemote)2 Nullable (org.jetbrains.annotations.Nullable)2 OutgoingResult (com.intellij.dvcs.push.OutgoingResult)1 VcsError (com.intellij.dvcs.push.VcsError)1 Condition (com.intellij.openapi.util.Condition)1 VcsException (com.intellij.openapi.vcs.VcsException)1 Hash (com.intellij.vcs.log.Hash)1 VcsFullCommitDetails (com.intellij.vcs.log.VcsFullCommitDetails)1 GitCommit (git4idea.GitCommit)1 GitBranchPair (git4idea.branch.GitBranchPair)1 Git (git4idea.commands.Git)1 GitCommandResult (git4idea.commands.GitCommandResult)1 GitLineHandlerListener (git4idea.commands.GitLineHandlerListener)1 IOException (java.io.IOException)1