Search in sources :

Example 1 with GitPreservingProcess

use of git4idea.util.GitPreservingProcess in project intellij-community by JetBrains.

the class GitCheckoutOperation method smartCheckout.

// stash - checkout - unstash
private boolean smartCheckout(@NotNull final List<GitRepository> repositories, @NotNull final String reference, @Nullable final String newBranch, @NotNull ProgressIndicator indicator) {
    final AtomicBoolean result = new AtomicBoolean();
    GitPreservingProcess preservingProcess = new GitPreservingProcess(myProject, myGit, GitUtil.getRootsFromRepositories(repositories), "checkout", reference, GitVcsSettings.UpdateChangesPolicy.STASH, indicator, new Runnable() {

        @Override
        public void run() {
            result.set(checkoutOrNotify(repositories, reference, newBranch, false));
        }
    });
    preservingProcess.execute();
    return result.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GitPreservingProcess(git4idea.util.GitPreservingProcess)

Example 2 with GitPreservingProcess

use of git4idea.util.GitPreservingProcess in project intellij-community by JetBrains.

the class GitResetOperation method proposeSmartReset.

private GitCommandResult proposeSmartReset(@NotNull GitLocalChangesWouldBeOverwrittenDetector detector, @NotNull final GitRepository repository, @NotNull final String target) {
    Collection<String> absolutePaths = GitUtil.toAbsolute(repository.getRoot(), detector.getRelativeFilePaths());
    List<Change> affectedChanges = GitUtil.findLocalChangesForPaths(myProject, repository.getRoot(), absolutePaths, false);
    int choice = myUiHandler.showSmartOperationDialog(myProject, affectedChanges, absolutePaths, "reset", "&Hard Reset");
    if (choice == GitSmartOperationDialog.SMART_EXIT_CODE) {
        final Ref<GitCommandResult> result = Ref.create();
        new GitPreservingProcess(myProject, myGit, Collections.singleton(repository.getRoot()), "reset", target, GitVcsSettings.UpdateChangesPolicy.STASH, myIndicator, new Runnable() {

            @Override
            public void run() {
                result.set(myGit.reset(repository, myMode, target));
            }
        }).execute();
        return result.get();
    }
    if (choice == GitSmartOperationDialog.FORCE_EXIT_CODE) {
        return myGit.reset(repository, GitResetMode.HARD, target);
    }
    return null;
}
Also used : Change(com.intellij.openapi.vcs.changes.Change) GitCommandResult(git4idea.commands.GitCommandResult) GitPreservingProcess(git4idea.util.GitPreservingProcess)

Example 3 with GitPreservingProcess

use of git4idea.util.GitPreservingProcess in project intellij-community by JetBrains.

the class GitUpdateProcess method updateImpl.

@NotNull
private GitUpdateResult updateImpl(@NotNull UpdateMethod updateMethod) {
    Map<VirtualFile, GitUpdater> updaters;
    try {
        updaters = defineUpdaters(updateMethod);
    } catch (VcsException e) {
        LOG.info(e);
        notifyError(myProject, "Git update failed", e.getMessage(), true, e);
        return GitUpdateResult.ERROR;
    }
    if (updaters.isEmpty()) {
        return GitUpdateResult.NOTHING_TO_UPDATE;
    }
    updaters = tryFastForwardMergeForRebaseUpdaters(updaters);
    if (updaters.isEmpty()) {
        // everything was updated via the fast-forward merge
        return GitUpdateResult.SUCCESS;
    }
    if (myCheckRebaseOverMergeProblem) {
        Collection<VirtualFile> problematicRoots = findRootsRebasingOverMerge(updaters);
        if (!problematicRoots.isEmpty()) {
            GitRebaseOverMergeProblem.Decision decision = GitRebaseOverMergeProblem.showDialog();
            if (decision == GitRebaseOverMergeProblem.Decision.MERGE_INSTEAD) {
                for (VirtualFile root : problematicRoots) {
                    updaters.put(root, new GitMergeUpdater(myProject, myGit, root, myTrackedBranches, myProgressIndicator, myUpdatedFiles));
                }
            } else if (decision == GitRebaseOverMergeProblem.Decision.CANCEL_OPERATION) {
                return GitUpdateResult.CANCEL;
            }
        }
    }
    // save local changes if needed (update via merge may perform without saving).
    final Collection<VirtualFile> myRootsToSave = ContainerUtil.newArrayList();
    LOG.info("updateImpl: identifying if save is needed...");
    for (Map.Entry<VirtualFile, GitUpdater> entry : updaters.entrySet()) {
        VirtualFile root = entry.getKey();
        GitUpdater updater = entry.getValue();
        if (updater.isSaveNeeded()) {
            myRootsToSave.add(root);
            LOG.info("update| root " + root + " needs save");
        }
    }
    LOG.info("updateImpl: saving local changes...");
    final Ref<Boolean> incomplete = Ref.create(false);
    final Ref<GitUpdateResult> compoundResult = Ref.create();
    final Map<VirtualFile, GitUpdater> finalUpdaters = updaters;
    new GitPreservingProcess(myProject, myGit, myRootsToSave, "Update", "Remote", GitVcsSettings.getInstance(myProject).updateChangesPolicy(), myProgressIndicator, new Runnable() {

        @Override
        public void run() {
            LOG.info("updateImpl: updating...");
            VirtualFile currentlyUpdatedRoot = null;
            try {
                for (Map.Entry<VirtualFile, GitUpdater> entry : finalUpdaters.entrySet()) {
                    currentlyUpdatedRoot = entry.getKey();
                    GitUpdater updater = entry.getValue();
                    GitUpdateResult res = updater.update();
                    LOG.info("updating root " + currentlyUpdatedRoot + " finished: " + res);
                    if (res == GitUpdateResult.INCOMPLETE) {
                        incomplete.set(true);
                    }
                    compoundResult.set(joinResults(compoundResult.get(), res));
                }
            } catch (VcsException e) {
                String rootName = (currentlyUpdatedRoot == null) ? "" : currentlyUpdatedRoot.getName();
                LOG.info("Error updating changes for root " + currentlyUpdatedRoot, e);
                notifyImportantError(myProject, "Error updating " + rootName, "Updating " + rootName + " failed with an error: " + e.getLocalizedMessage());
            }
        }
    }).execute(new Computable<Boolean>() {

        @Override
        public Boolean compute() {
            // In this case we don't restore local changes either, because update failed.
            return !incomplete.get() && !compoundResult.isNull() && compoundResult.get().isSuccess();
        }
    });
    // GitPreservingProcess#save may fail due index.lock presence
    return ObjectUtils.notNull(compoundResult.get(), GitUpdateResult.ERROR);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitPreservingProcess(git4idea.util.GitPreservingProcess) VcsException(com.intellij.openapi.vcs.VcsException) HashMap(java.util.HashMap) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with GitPreservingProcess

use of git4idea.util.GitPreservingProcess in project intellij-community by JetBrains.

the class GitMergeOperation method doSmartMerge.

private boolean doSmartMerge(@NotNull final Collection<GitRepository> repositories) {
    final AtomicBoolean success = new AtomicBoolean();
    myPreservingProcess = new GitPreservingProcess(myProject, myGit, GitUtil.getRootsFromRepositories(repositories), "merge", myBranchToMerge, GitVcsSettings.UpdateChangesPolicy.STASH, getIndicator(), new Runnable() {

        @Override
        public void run() {
            success.set(doMerge(repositories));
        }
    });
    myPreservingProcess.execute(new Computable<Boolean>() {

        @Override
        public Boolean compute() {
            return myConflictedRepositories.isEmpty();
        }
    });
    return success.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GitPreservingProcess(git4idea.util.GitPreservingProcess)

Example 5 with GitPreservingProcess

use of git4idea.util.GitPreservingProcess in project intellij-community by JetBrains.

the class GitMergeOperation method smartRollback.

@NotNull
private GitCompoundResult smartRollback(@NotNull final Collection<GitRepository> repositories) {
    LOG.info("Starting smart rollback...");
    final GitCompoundResult result = new GitCompoundResult(myProject);
    Collection<VirtualFile> roots = GitUtil.getRootsFromRepositories(repositories);
    GitPreservingProcess preservingProcess = new GitPreservingProcess(myProject, myGit, roots, "merge", myBranchToMerge, GitVcsSettings.UpdateChangesPolicy.STASH, getIndicator(), new Runnable() {

        @Override
        public void run() {
            for (GitRepository repository : repositories) {
                result.append(repository, rollback(repository));
            }
        }
    });
    preservingProcess.execute();
    LOG.info("Smart rollback completed.");
    return result;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitRepository(git4idea.repo.GitRepository) GitPreservingProcess(git4idea.util.GitPreservingProcess) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GitPreservingProcess (git4idea.util.GitPreservingProcess)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 NotNull (org.jetbrains.annotations.NotNull)2 AccessToken (com.intellij.openapi.application.AccessToken)1 VcsException (com.intellij.openapi.vcs.VcsException)1 Change (com.intellij.openapi.vcs.changes.Change)1 GitCommandResult (git4idea.commands.GitCommandResult)1 GitRepository (git4idea.repo.GitRepository)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1