Search in sources :

Example 36 with GitRepository

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

the class GitUncommitAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    Project project = e.getRequiredData(CommonDataKeys.PROJECT);
    VcsLogData data = (VcsLogData) e.getRequiredData(VcsLogDataKeys.VCS_LOG_DATA_PROVIDER);
    VcsLog log = e.getRequiredData(VcsLogDataKeys.VCS_LOG);
    VcsShortCommitDetails commit = assertNotNull(getFirstItem(log.getSelectedShortDetails()));
    VirtualFile root = commit.getRoot();
    Hash hash = commit.getId();
    GitRepository repository = assertNotNull(getRepositoryManager(project).getRepositoryForRoot(commit.getRoot()));
    List<String> branches = findContainingBranches(data, root, hash);
    if (!branches.contains(HEAD)) {
        Messages.showErrorDialog(project, COMMIT_NOT_IN_HEAD, FAILURE_TITLE);
        return;
    }
    // and not if pushed to a protected branch
    String protectedBranch = findProtectedRemoteBranch(repository, branches);
    if (protectedBranch != null) {
        Messages.showErrorDialog(project, COMMIT_PUSHED_TO_PROTECTED + protectedBranch, FAILURE_TITLE);
        return;
    }
    ChangeListChooser chooser = new ChangeListChooser(project, ChangeListManager.getInstance(project).getChangeListsCopy(), null, "Select Target Changelist", commit.getSubject());
    chooser.show();
    LocalChangeList selectedList = chooser.getSelectedList();
    if (selectedList != null) {
        resetInBackground(data, repository, commit, selectedList);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GitRepository(git4idea.repo.GitRepository) ChangeListChooser(com.intellij.openapi.vcs.changes.ui.ChangeListChooser)

Example 37 with GitRepository

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

the class GitUncommitAction method update.

@Override
public void update(@NotNull AnActionEvent e) {
    super.update(e);
    Project project = e.getProject();
    VcsLog log = e.getData(VcsLogDataKeys.VCS_LOG);
    VcsLogData data = (VcsLogData) e.getData(VcsLogDataKeys.VCS_LOG_DATA_PROVIDER);
    VcsLogUi ui = e.getData(VcsLogDataKeys.VCS_LOG_UI);
    if (project == null || log == null || data == null || ui == null) {
        e.getPresentation().setEnabledAndVisible(false);
        return;
    }
    int selectedCommits = log.getSelectedShortDetails().size();
    if (selectedCommits != 1) {
        e.getPresentation().setEnabledAndVisible(false);
        return;
    }
    VcsShortCommitDetails commit = log.getSelectedShortDetails().get(0);
    Hash hash = commit.getId();
    VirtualFile root = commit.getRoot();
    GitRepository repository = getRepositoryManager(project).getRepositoryForRootQuick(commit.getRoot());
    if (repository == null) {
        e.getPresentation().setEnabledAndVisible(false);
        return;
    }
    DataPackBase dataPackBase = ((VisiblePack) ui.getDataPack()).getDataPack();
    if (!(dataPackBase instanceof DataPack)) {
        e.getPresentation().setVisible(true);
        e.getPresentation().setEnabled(false);
        return;
    }
    // support undo only for the last commit in the branch
    DataPack dataPack = (DataPack) dataPackBase;
    List<Integer> children = dataPack.getPermanentGraph().getChildren(data.getCommitIndex(hash, root));
    if (!children.isEmpty()) {
        e.getPresentation().setEnabledAndVisible(false);
        return;
    }
    // undoing merge commit is not allowed
    int parents = commit.getParents().size();
    if (parents != 1) {
        e.getPresentation().setEnabled(false);
        e.getPresentation().setDescription("Selected commit has " + parents + " parents");
        return;
    }
    // allow reset only in current branch
    List<String> branches = data.getContainingBranchesGetter().getContainingBranchesFromCache(root, hash);
    if (branches != null) {
        // otherwise the information is not available yet, and we'll recheck harder in actionPerformed
        if (!branches.contains(HEAD)) {
            e.getPresentation().setEnabled(false);
            e.getPresentation().setDescription(COMMIT_NOT_IN_HEAD);
            return;
        }
        // and not if pushed to a protected branch
        String protectedBranch = findProtectedRemoteBranch(repository, branches);
        if (protectedBranch != null) {
            e.getPresentation().setEnabled(false);
            e.getPresentation().setDescription(COMMIT_PUSHED_TO_PROTECTED + protectedBranch);
            return;
        }
    }
    e.getPresentation().setEnabledAndVisible(true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VisiblePack(com.intellij.vcs.log.visible.VisiblePack) Project(com.intellij.openapi.project.Project) GitRepository(git4idea.repo.GitRepository)

Example 38 with GitRepository

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

the class GitRollbackEnvironment method rollbackChanges.

public void rollbackChanges(@NotNull List<Change> changes, final List<VcsException> exceptions, @NotNull final RollbackProgressListener listener) {
    HashMap<VirtualFile, List<FilePath>> toUnindex = new HashMap<>();
    HashMap<VirtualFile, List<FilePath>> toUnversion = new HashMap<>();
    HashMap<VirtualFile, List<FilePath>> toRevert = new HashMap<>();
    List<FilePath> toDelete = new ArrayList<>();
    listener.determinate();
    // collect changes to revert
    for (Change c : changes) {
        switch(c.getType()) {
            case NEW:
                // note that this the only change that could happen
                // for HEAD-less working directories.
                registerFile(toUnversion, c.getAfterRevision().getFile(), exceptions);
                break;
            case MOVED:
                registerFile(toRevert, c.getBeforeRevision().getFile(), exceptions);
                registerFile(toUnindex, c.getAfterRevision().getFile(), exceptions);
                toDelete.add(c.getAfterRevision().getFile());
                break;
            case MODIFICATION:
                // note that changes are also removed from index, if they got into index somehow
                registerFile(toUnindex, c.getBeforeRevision().getFile(), exceptions);
                registerFile(toRevert, c.getBeforeRevision().getFile(), exceptions);
                break;
            case DELETED:
                registerFile(toRevert, c.getBeforeRevision().getFile(), exceptions);
                break;
        }
    }
    // unindex files
    for (Map.Entry<VirtualFile, List<FilePath>> entry : toUnindex.entrySet()) {
        listener.accept(entry.getValue());
        try {
            unindex(entry.getKey(), entry.getValue(), false);
        } catch (VcsException e) {
            exceptions.add(e);
        }
    }
    // unversion files
    for (Map.Entry<VirtualFile, List<FilePath>> entry : toUnversion.entrySet()) {
        listener.accept(entry.getValue());
        try {
            unindex(entry.getKey(), entry.getValue(), true);
        } catch (VcsException e) {
            exceptions.add(e);
        }
    }
    // delete files
    for (FilePath file : toDelete) {
        listener.accept(file);
        try {
            final File ioFile = file.getIOFile();
            if (ioFile.exists()) {
                if (!ioFile.delete()) {
                    //noinspection ThrowableInstanceNeverThrown
                    exceptions.add(new VcsException("Unable to delete file: " + file));
                }
            }
        } catch (Exception e) {
            //noinspection ThrowableInstanceNeverThrown
            exceptions.add(new VcsException("Unable to delete file: " + file, e));
        }
    }
    // revert files from HEAD
    AccessToken token = DvcsUtil.workingTreeChangeStarted(myProject);
    try {
        for (Map.Entry<VirtualFile, List<FilePath>> entry : toRevert.entrySet()) {
            listener.accept(entry.getValue());
            try {
                revert(entry.getKey(), entry.getValue());
            } catch (VcsException e) {
                exceptions.add(e);
            }
        }
    } finally {
        token.finish();
    }
    LocalFileSystem lfs = LocalFileSystem.getInstance();
    HashSet<File> filesToRefresh = new HashSet<>();
    for (Change c : changes) {
        ContentRevision before = c.getBeforeRevision();
        if (before != null) {
            filesToRefresh.add(new File(before.getFile().getPath()));
        }
        ContentRevision after = c.getAfterRevision();
        if (after != null) {
            filesToRefresh.add(new File(after.getFile().getPath()));
        }
    }
    lfs.refreshIoFiles(filesToRefresh);
    for (GitRepository repo : GitUtil.getRepositoryManager(myProject).getRepositories()) {
        repo.update();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FilePath(com.intellij.openapi.vcs.FilePath) ContentRevision(com.intellij.openapi.vcs.changes.ContentRevision) Change(com.intellij.openapi.vcs.changes.Change) VcsException(com.intellij.openapi.vcs.VcsException) GitRepository(git4idea.repo.GitRepository) AccessToken(com.intellij.openapi.application.AccessToken) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) VcsException(com.intellij.openapi.vcs.VcsException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 39 with GitRepository

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

the class GitPushOperation method prepareCombinedResult.

private GitPushResult prepareCombinedResult(final Map<GitRepository, GitPushRepoResult> allRoots, final Map<GitRepository, GitUpdateResult> updatedRoots, final Map<GitRepository, String> preUpdatePositions, Label beforeUpdateLabel, Label afterUpdateLabel) {
    Map<GitRepository, GitPushRepoResult> results = ContainerUtil.newHashMap();
    UpdatedFiles updatedFiles = UpdatedFiles.create();
    for (Map.Entry<GitRepository, GitPushRepoResult> entry : allRoots.entrySet()) {
        GitRepository repository = entry.getKey();
        GitPushRepoResult simpleResult = entry.getValue();
        GitUpdateResult updateResult = updatedRoots.get(repository);
        if (updateResult == null) {
            results.put(repository, simpleResult);
        } else {
            collectUpdatedFiles(updatedFiles, repository, preUpdatePositions.get(repository));
            results.put(repository, GitPushRepoResult.addUpdateResult(simpleResult, updateResult));
        }
    }
    return new GitPushResult(results, updatedFiles, beforeUpdateLabel, afterUpdateLabel);
}
Also used : GitRepository(git4idea.repo.GitRepository) GitUpdateResult(git4idea.update.GitUpdateResult) UpdatedFiles(com.intellij.openapi.vcs.update.UpdatedFiles)

Example 40 with GitRepository

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

the class GitPushOperation method execute.

@NotNull
public GitPushResult execute() {
    PushUpdateSettings updateSettings = readPushUpdateSettings();
    Label beforePushLabel = null;
    Label afterPushLabel = null;
    Map<GitRepository, String> preUpdatePositions = updateRootInfoAndRememberPositions();
    Boolean rebaseOverMergeProblemDetected = null;
    final Map<GitRepository, GitPushRepoResult> results = ContainerUtil.newHashMap();
    Map<GitRepository, GitUpdateResult> updatedRoots = ContainerUtil.newHashMap();
    try {
        Collection<GitRepository> remainingRoots = myPushSpecs.keySet();
        for (int pushAttempt = 0; pushAttempt < MAX_PUSH_ATTEMPTS && !remainingRoots.isEmpty(); pushAttempt++, remainingRoots = getRejectedAndNotPushed(results)) {
            Map<GitRepository, GitPushRepoResult> resultMap = push(remainingRoots);
            results.putAll(resultMap);
            GroupedPushResult result = GroupedPushResult.group(resultMap);
            // stop if error happens, or if push is rejected for a custom reason (not because a pull is needed)
            if (!result.errors.isEmpty() || !result.customRejected.isEmpty()) {
                break;
            }
            // propose to update if rejected
            if (!result.rejected.isEmpty()) {
                boolean shouldUpdate = true;
                if (myForce || pushingToNotTrackedBranch(result.rejected)) {
                    shouldUpdate = false;
                } else if (pushAttempt == 0 && !mySettings.autoUpdateIfPushRejected()) {
                    // the dialog will be shown => check for rebase-over-merge problem in advance to avoid showing several dialogs in a row
                    rebaseOverMergeProblemDetected = !findRootsWithMergeCommits(getRootsToUpdate(updateSettings, result.rejected.keySet())).isEmpty();
                    updateSettings = showDialogAndGetExitCode(result.rejected.keySet(), updateSettings, rebaseOverMergeProblemDetected.booleanValue());
                    if (updateSettings != null) {
                        savePushUpdateSettings(updateSettings, rebaseOverMergeProblemDetected.booleanValue());
                    } else {
                        shouldUpdate = false;
                    }
                }
                if (!shouldUpdate) {
                    break;
                }
                if (beforePushLabel == null) {
                    // put the label only before the very first update
                    beforePushLabel = LocalHistory.getInstance().putSystemLabel(myProject, "Before push");
                }
                Collection<GitRepository> rootsToUpdate = getRootsToUpdate(updateSettings, result.rejected.keySet());
                GitUpdateResult updateResult = update(rootsToUpdate, updateSettings.getUpdateMethod(), rebaseOverMergeProblemDetected == null);
                for (GitRepository repository : rootsToUpdate) {
                    // TODO update result in GitUpdateProcess is a single for several roots
                    updatedRoots.put(repository, updateResult);
                }
                if (!updateResult.isSuccess() || updateResult == GitUpdateResult.SUCCESS_WITH_RESOLVED_CONFLICTS || updateResult == GitUpdateResult.INCOMPLETE) {
                    break;
                }
            }
        }
    } finally {
        if (beforePushLabel != null) {
            afterPushLabel = LocalHistory.getInstance().putSystemLabel(myProject, "After push");
        }
        for (GitRepository repository : myPushSpecs.keySet()) {
            repository.update();
        }
    }
    return prepareCombinedResult(results, updatedRoots, preUpdatePositions, beforePushLabel, afterPushLabel);
}
Also used : Label(com.intellij.history.Label) GitRepository(git4idea.repo.GitRepository) GitUpdateResult(git4idea.update.GitUpdateResult) NotNull(org.jetbrains.annotations.NotNull)

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