Search in sources :

Example 96 with VcsException

use of com.intellij.openapi.vcs.VcsException in project intellij-community by JetBrains.

the class GitConflictResolver method unmergedFiles.

/**
   * Parse changes from lines
   *
   *
   * @param root    the git root
   * @return a set of unmerged files
   * @throws com.intellij.openapi.vcs.VcsException if the input format does not matches expected format
   */
private List<VirtualFile> unmergedFiles(final VirtualFile root) throws VcsException {
    GitRepository repository = myRepositoryManager.getRepositoryForRoot(root);
    if (repository == null) {
        LOG.error("Repository not found for root " + root);
        return Collections.emptyList();
    }
    GitCommandResult result = myGit.getUnmergedFiles(repository);
    if (!result.success()) {
        throw new VcsException(result.getErrorOutputAsJoinedString());
    }
    String output = StringUtil.join(result.getOutput(), "\n");
    HashSet<String> unmergedPaths = ContainerUtil.newHashSet();
    for (StringScanner s = new StringScanner(output); s.hasMoreData(); ) {
        if (s.isEol()) {
            s.nextLine();
            continue;
        }
        s.boundedToken('\t');
        String relative = s.line();
        unmergedPaths.add(GitUtil.unescapePath(relative));
    }
    if (unmergedPaths.size() == 0) {
        return Collections.emptyList();
    } else {
        List<File> files = ContainerUtil.map(unmergedPaths, new Function<String, File>() {

            @Override
            public File fun(String path) {
                return new File(root.getPath(), path);
            }
        });
        return sortVirtualFilesByPresentation(findVirtualFilesWithRefresh(files));
    }
}
Also used : GitRepository(git4idea.repo.GitRepository) VcsException(com.intellij.openapi.vcs.VcsException) GitCommandResult(git4idea.commands.GitCommandResult) StringScanner(git4idea.util.StringScanner) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 97 with VcsException

use of com.intellij.openapi.vcs.VcsException in project intellij-community by JetBrains.

the class GitRollbackEnvironment method registerFile.

/**
   * Register file in the map under appropriate root
   *
   * @param files      a map to use
   * @param file       a file to register
   * @param exceptions the list of exceptions to update
   */
private static void registerFile(Map<VirtualFile, List<FilePath>> files, FilePath file, List<VcsException> exceptions) {
    final VirtualFile root;
    try {
        root = GitUtil.getGitRoot(file);
    } catch (VcsException e) {
        exceptions.add(e);
        return;
    }
    List<FilePath> paths = files.get(root);
    if (paths == null) {
        paths = new ArrayList<>();
        files.put(root, paths);
    }
    paths.add(file);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FilePath(com.intellij.openapi.vcs.FilePath) VcsException(com.intellij.openapi.vcs.VcsException)

Example 98 with VcsException

use of com.intellij.openapi.vcs.VcsException 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 99 with VcsException

use of com.intellij.openapi.vcs.VcsException in project intellij-community by JetBrains.

the class GitOutgoingCommitsProvider method getOutgoingCommits.

@NotNull
@Override
public OutgoingResult getOutgoingCommits(@NotNull GitRepository repository, @NotNull PushSpec<GitPushSource, GitPushTarget> pushSpec, boolean initial) {
    GitLocalBranch branch = pushSpec.getSource().getBranch();
    String source = branch.equals(repository.getCurrentBranch()) ? HEAD : branch.getFullName();
    GitPushTarget target = pushSpec.getTarget();
    String destination = target.getBranch().getFullName();
    try {
        List<GitCommit> commits;
        if (!target.isNewBranchCreated()) {
            commits = GitHistoryUtils.history(myProject, repository.getRoot(), destination + ".." + source);
        } else {
            commits = GitHistoryUtils.history(myProject, repository.getRoot(), source, "--not", "--remotes=" + target.getBranch().getRemote().getName(), "--max-count=" + 1000);
        }
        return new OutgoingResult(commits, Collections.<VcsError>emptyList());
    } catch (VcsException e) {
        return new OutgoingResult(Collections.<VcsFullCommitDetails>emptyList(), Collections.singletonList(new VcsError(GitUtil.cleanupErrorPrefixes(e.getMessage()))));
    }
}
Also used : GitLocalBranch(git4idea.GitLocalBranch) GitCommit(git4idea.GitCommit) VcsException(com.intellij.openapi.vcs.VcsException) OutgoingResult(com.intellij.dvcs.push.OutgoingResult) VcsFullCommitDetails(com.intellij.vcs.log.VcsFullCommitDetails) VcsError(com.intellij.dvcs.push.VcsError) NotNull(org.jetbrains.annotations.NotNull)

Example 100 with VcsException

use of com.intellij.openapi.vcs.VcsException in project intellij-community by JetBrains.

the class GitPushOperation method collectUpdatedFiles.

private void collectUpdatedFiles(@NotNull UpdatedFiles updatedFiles, @NotNull GitRepository repository, @NotNull String preUpdatePosition) {
    MergeChangeCollector collector = new MergeChangeCollector(myProject, repository.getRoot(), new GitRevisionNumber(preUpdatePosition));
    ArrayList<VcsException> exceptions = new ArrayList<>();
    collector.collect(updatedFiles, exceptions);
    for (VcsException exception : exceptions) {
        LOG.info(exception);
    }
}
Also used : MergeChangeCollector(git4idea.merge.MergeChangeCollector) GitRevisionNumber(git4idea.GitRevisionNumber) VcsException(com.intellij.openapi.vcs.VcsException)

Aggregations

VcsException (com.intellij.openapi.vcs.VcsException)200 VirtualFile (com.intellij.openapi.vfs.VirtualFile)89 File (java.io.File)48 NotNull (org.jetbrains.annotations.NotNull)42 FilePath (com.intellij.openapi.vcs.FilePath)35 Change (com.intellij.openapi.vcs.changes.Change)33 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)26 ArrayList (java.util.ArrayList)24 Nullable (org.jetbrains.annotations.Nullable)23 IOException (java.io.IOException)20 SVNException (org.tmatesoft.svn.core.SVNException)19 Project (com.intellij.openapi.project.Project)17 Ref (com.intellij.openapi.util.Ref)16 Test (org.junit.Test)14 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)13 GitRepository (git4idea.repo.GitRepository)12 Task (com.intellij.openapi.progress.Task)11 List (java.util.List)11 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)10 ContentRevision (com.intellij.openapi.vcs.changes.ContentRevision)10