Search in sources :

Example 16 with GitRepository

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

the class GitCheckoutOperation method rollback.

@Override
protected void rollback() {
    GitCompoundResult checkoutResult = new GitCompoundResult(myProject);
    GitCompoundResult deleteResult = new GitCompoundResult(myProject);
    for (GitRepository repository : getSuccessfulRepositories()) {
        GitCommandResult result = myGit.checkout(repository, myCurrentHeads.get(repository), null, true, false);
        checkoutResult.append(repository, result);
        if (result.success() && myNewBranch != null) {
            /*
          force delete is needed, because we create new branch from branch other that the current one
          e.g. being on master create newBranch from feature,
          then rollback => newBranch is not fully merged to master (although it is obviously fully merged to feature).
         */
            deleteResult.append(repository, myGit.branchDelete(repository, myNewBranch, true));
        }
        refresh(repository);
    }
    if (!checkoutResult.totalSuccess() || !deleteResult.totalSuccess()) {
        StringBuilder message = new StringBuilder();
        if (!checkoutResult.totalSuccess()) {
            message.append("Errors during checkout: ");
            message.append(checkoutResult.getErrorOutputWithReposIndication());
        }
        if (!deleteResult.totalSuccess()) {
            message.append("Errors during deleting ").append(code(myNewBranch)).append(": ");
            message.append(deleteResult.getErrorOutputWithReposIndication());
        }
        VcsNotifier.getInstance(myProject).notifyError("Error during rollback", message.toString());
    }
}
Also used : GitRepository(git4idea.repo.GitRepository)

Example 17 with GitRepository

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

the class GitDeleteBranchOperation method showNotFullyMergedDialog.

/**
   * Shows a dialog "the branch is not fully merged" with the list of unmerged commits.
   * User may still want to force delete the branch.
   * In multi-repository setup collects unmerged commits for all given repositories.
   * @return true if the branch should be restored.
   */
private boolean showNotFullyMergedDialog(@NotNull Map<GitRepository, UnmergedBranchInfo> unmergedBranches) {
    Map<GitRepository, List<GitCommit>> history = new HashMap<>();
    // we display no commits for them
    for (GitRepository repository : getRepositories()) {
        if (unmergedBranches.containsKey(repository)) {
            UnmergedBranchInfo unmergedInfo = unmergedBranches.get(repository);
            history.put(repository, getUnmergedCommits(repository, unmergedInfo.myTipOfDeletedUnmergedBranch, unmergedInfo.myBaseBranch));
        } else {
            history.put(repository, Collections.<GitCommit>emptyList());
        }
    }
    Map<GitRepository, String> baseBranches = Maps.asMap(unmergedBranches.keySet(), it -> unmergedBranches.get(it).myBaseBranch);
    return myUiHandler.showBranchIsNotFullyMergedDialog(myProject, history, baseBranches, myBranchName);
}
Also used : GitRepository(git4idea.repo.GitRepository)

Example 18 with GitRepository

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

the class GitDeleteBranchOperation method execute.

@Override
public void execute() {
    boolean fatalErrorHappened = false;
    while (hasMoreRepositories() && !fatalErrorHappened) {
        final GitRepository repository = next();
        GitSimpleEventDetector notFullyMergedDetector = new GitSimpleEventDetector(GitSimpleEventDetector.Event.BRANCH_NOT_FULLY_MERGED);
        GitBranchNotMergedToUpstreamDetector notMergedToUpstreamDetector = new GitBranchNotMergedToUpstreamDetector();
        GitCommandResult result = myGit.branchDelete(repository, myBranchName, false, notFullyMergedDetector, notMergedToUpstreamDetector);
        if (result.success()) {
            refresh(repository);
            markSuccessful(repository);
        } else if (notFullyMergedDetector.hasHappened()) {
            String baseBranch = notMergedToUpstreamDetector.getBaseBranch();
            if (baseBranch == null) {
                // GitBranchNotMergedToUpstreamDetector didn't happen
                baseBranch = myCurrentHeads.get(repository);
            }
            myUnmergedToBranches.put(repository, new UnmergedBranchInfo(myDeletedBranchTips.get(repository), GitBranchUtil.stripRefsPrefix(baseBranch)));
            GitCommandResult forceDeleteResult = myGit.branchDelete(repository, myBranchName, true);
            if (forceDeleteResult.success()) {
                refresh(repository);
                markSuccessful(repository);
            } else {
                fatalError(getErrorTitle(), forceDeleteResult.getErrorOutputAsHtmlString());
                fatalErrorHappened = true;
            }
        } else {
            fatalError(getErrorTitle(), result.getErrorOutputAsJoinedString());
            fatalErrorHappened = true;
        }
    }
    if (!fatalErrorHappened) {
        notifySuccess();
    }
}
Also used : GitRepository(git4idea.repo.GitRepository)

Example 19 with GitRepository

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

the class GitDeleteBranchOperation method doRollback.

@NotNull
private GitCompoundResult doRollback() {
    GitCompoundResult result = new GitCompoundResult(myProject);
    for (GitRepository repository : getSuccessfulRepositories()) {
        GitCommandResult res = myGit.branchCreate(repository, myBranchName, myDeletedBranchTips.get(repository));
        result.append(repository, res);
        for (String trackedBranch : myTrackedBranches.keySet()) {
            if (myTrackedBranches.get(trackedBranch).contains(repository)) {
                GitCommandResult setTrackResult = setUpTracking(repository, myBranchName, trackedBranch);
                if (!setTrackResult.success()) {
                    LOG.warn("Couldn't set " + myBranchName + " to track " + trackedBranch + " in " + repository.getRoot().getName() + ": " + setTrackResult.getErrorOutputAsJoinedString());
                }
            }
        }
        refresh(repository);
    }
    return result;
}
Also used : GitRepository(git4idea.repo.GitRepository) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with GitRepository

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

the class GitDeleteRemoteBranchOperation method doDeleteRemote.

private boolean doDeleteRemote(@NotNull String branchName, @NotNull Collection<GitRepository> repositories) {
    Couple<String> pair = splitNameOfRemoteBranch(branchName);
    String remoteName = pair.getFirst();
    String branch = pair.getSecond();
    GitCompoundResult result = new GitCompoundResult(myProject);
    for (GitRepository repository : repositories) {
        GitCommandResult res;
        GitRemote remote = getRemoteByName(repository, remoteName);
        if (remote == null) {
            String error = "Couldn't find remote by name: " + remoteName;
            LOG.error(error);
            res = GitCommandResult.error(error);
        } else {
            res = pushDeletion(repository, remote, branch);
            if (!res.success() && isAlreadyDeletedError(res.getErrorOutputAsJoinedString())) {
                res = myGit.remotePrune(repository, remote);
            }
        }
        result.append(repository, res);
        repository.update();
    }
    if (!result.totalSuccess()) {
        VcsNotifier.getInstance(myProject).notifyError("Failed to delete remote branch " + branchName, result.getErrorOutputWithReposIndication());
    }
    return result.totalSuccess();
}
Also used : GitRemote(git4idea.repo.GitRemote) GitCompoundResult(git4idea.commands.GitCompoundResult) GitRepository(git4idea.repo.GitRepository) GitCommandResult(git4idea.commands.GitCommandResult)

Aggregations

GitRepository (git4idea.repo.GitRepository)123 VirtualFile (com.intellij.openapi.vfs.VirtualFile)46 NotNull (org.jetbrains.annotations.NotNull)33 Nullable (org.jetbrains.annotations.Nullable)19 Project (com.intellij.openapi.project.Project)18 GitCommandResult (git4idea.commands.GitCommandResult)14 GitRepositoryManager (git4idea.repo.GitRepositoryManager)12 VcsException (com.intellij.openapi.vcs.VcsException)11 AccessToken (com.intellij.openapi.application.AccessToken)9 File (java.io.File)8 Map (java.util.Map)8 GitRemote (git4idea.repo.GitRemote)7 FilePath (com.intellij.openapi.vcs.FilePath)6 Change (com.intellij.openapi.vcs.changes.Change)6 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)5 Task (com.intellij.openapi.progress.Task)5 ArrayList (java.util.ArrayList)5 ObjectUtils.assertNotNull (com.intellij.util.ObjectUtils.assertNotNull)4 MultiMap (com.intellij.util.containers.MultiMap)4 GitBranch (git4idea.GitBranch)4