Search in sources :

Example 86 with VcsException

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

the class GitUtil method sortFilePathsByGitRoot.

/**
   * Sort files by vcs root
   *
   * @param files        files to sort.
   * @param ignoreNonGit if true, non-git files are ignored
   * @return the map from root to the files under the root
   * @throws VcsException if non git files are passed when {@code ignoreNonGit} is false
   */
@NotNull
public static Map<VirtualFile, List<FilePath>> sortFilePathsByGitRoot(@NotNull Collection<FilePath> files, boolean ignoreNonGit) throws VcsException {
    Map<VirtualFile, List<FilePath>> rc = new HashMap<>();
    for (FilePath p : files) {
        VirtualFile root = getGitRootOrNull(p);
        if (root == null) {
            if (ignoreNonGit) {
                continue;
            } else {
                throw new VcsException("The file " + p.getPath() + " is not under Git");
            }
        }
        List<FilePath> l = rc.get(root);
        if (l == null) {
            l = new ArrayList<>();
            rc.put(root, l);
        }
        l.add(p);
    }
    return rc;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AbstractVcsVirtualFile(com.intellij.openapi.vcs.vfs.AbstractVcsVirtualFile) FilePath(com.intellij.openapi.vcs.FilePath) VcsException(com.intellij.openapi.vcs.VcsException) CommittedChangeList(com.intellij.openapi.vcs.versionBrowser.CommittedChangeList) GitCommittedChangeList(git4idea.changes.GitCommittedChangeList) NotNull(org.jetbrains.annotations.NotNull) ObjectUtils.assertNotNull(com.intellij.util.ObjectUtils.assertNotNull)

Example 87 with VcsException

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

the class GitLogProvider method getCommitsMatchingFilter.

@NotNull
@Override
public List<TimedVcsCommit> getCommitsMatchingFilter(@NotNull final VirtualFile root, @NotNull VcsLogFilterCollection filterCollection, int maxCount) throws VcsException {
    if (!isRepositoryReady(root)) {
        return Collections.emptyList();
    }
    List<String> filterParameters = ContainerUtil.newArrayList();
    VcsLogBranchFilter branchFilter = filterCollection.getBranchFilter();
    if (branchFilter != null) {
        GitRepository repository = getRepository(root);
        assert repository != null : "repository is null for root " + root + " but was previously reported as 'ready'";
        Collection<GitBranch> branches = ContainerUtil.newArrayList(ContainerUtil.concat(repository.getBranches().getLocalBranches(), repository.getBranches().getRemoteBranches()));
        Collection<String> branchNames = GitBranchUtil.convertBranchesToNames(branches);
        Collection<String> predefinedNames = ContainerUtil.list("HEAD");
        boolean atLeastOneBranchExists = false;
        for (String branchName : ContainerUtil.concat(branchNames, predefinedNames)) {
            if (branchFilter.matches(branchName)) {
                filterParameters.add(branchName);
                atLeastOneBranchExists = true;
            }
        }
        if (!atLeastOneBranchExists) {
            // no such branches in this repository => filter matches nothing
            return Collections.emptyList();
        }
    } else {
        filterParameters.addAll(GitHistoryUtils.LOG_ALL);
    }
    if (filterCollection.getDateFilter() != null) {
        // assuming there is only one date filter, until filter expressions are defined
        VcsLogDateFilter filter = filterCollection.getDateFilter();
        if (filter.getAfter() != null) {
            filterParameters.add(prepareParameter("after", filter.getAfter().toString()));
        }
        if (filter.getBefore() != null) {
            filterParameters.add(prepareParameter("before", filter.getBefore().toString()));
        }
    }
    boolean regexp = true;
    boolean caseSensitive = false;
    if (filterCollection.getTextFilter() != null) {
        regexp = filterCollection.getTextFilter().isRegex();
        caseSensitive = filterCollection.getTextFilter().matchesCase();
        String textFilter = filterCollection.getTextFilter().getText();
        filterParameters.add(prepareParameter("grep", textFilter));
    }
    filterParameters.add(regexp ? "--extended-regexp" : "--fixed-strings");
    if (!caseSensitive) {
        // affects case sensitivity of any filter (except file filter)
        filterParameters.add("--regexp-ignore-case");
    }
    if (filterCollection.getUserFilter() != null) {
        Collection<String> names = ContainerUtil.map(filterCollection.getUserFilter().getUsers(root), VcsUserUtil::toExactString);
        if (regexp) {
            List<String> authors = ContainerUtil.map(names, UserNameRegex.EXTENDED_INSTANCE);
            if (GitVersionSpecialty.LOG_AUTHOR_FILTER_SUPPORTS_VERTICAL_BAR.existsIn(myVcs.getVersion())) {
                filterParameters.add(prepareParameter("author", StringUtil.join(authors, "|")));
            } else {
                filterParameters.addAll(authors.stream().map(a -> prepareParameter("author", a)).collect(Collectors.toList()));
            }
        } else {
            filterParameters.addAll(ContainerUtil.map(names, a -> prepareParameter("author", StringUtil.escapeBackSlashes(a))));
        }
    }
    if (maxCount > 0) {
        filterParameters.add(prepareParameter("max-count", String.valueOf(maxCount)));
    }
    // note: structure filter must be the last parameter, because it uses "--" which separates parameters from paths
    if (filterCollection.getStructureFilter() != null) {
        Collection<FilePath> files = filterCollection.getStructureFilter().getFiles();
        if (!files.isEmpty()) {
            filterParameters.add("--full-history");
            filterParameters.add("--simplify-merges");
            filterParameters.add("--");
            for (FilePath file : files) {
                filterParameters.add(file.getPath());
            }
        }
    }
    List<TimedVcsCommit> commits = ContainerUtil.newArrayList();
    GitHistoryUtils.readCommits(myProject, root, filterParameters, EmptyConsumer.getInstance(), EmptyConsumer.getInstance(), new CollectConsumer<>(commits));
    return commits;
}
Also used : java.util(java.util) ThrowableNotNullFunction(com.intellij.openapi.util.ThrowableNotNullFunction) VirtualFile(com.intellij.openapi.vfs.VirtualFile) LogDataImpl(com.intellij.vcs.log.impl.LogDataImpl) GitVersionSpecialty(git4idea.config.GitVersionSpecialty) GitRepositoryManager(git4idea.repo.GitRepositoryManager) THashSet(gnu.trove.THashSet) VcsKey(com.intellij.openapi.vcs.VcsKey) ContainerUtil(com.intellij.util.containers.ContainerUtil) GitBranchUtil(git4idea.branch.GitBranchUtil) GraphColorManager(com.intellij.vcs.log.graph.GraphColorManager) PermanentGraphImpl(com.intellij.vcs.log.graph.impl.facade.PermanentGraphImpl) TObjectHashingStrategy(gnu.trove.TObjectHashingStrategy) MessageBusConnection(com.intellij.util.messages.MessageBusConnection) GitHistoryUtils(git4idea.history.GitHistoryUtils) UserNameRegex(com.intellij.vcs.log.util.UserNameRegex) Project(com.intellij.openapi.project.Project) VcsFileUtil(com.intellij.vcsUtil.VcsFileUtil) Logger(com.intellij.openapi.diagnostic.Logger) VcsException(com.intellij.openapi.vcs.VcsException) GitRepository(git4idea.repo.GitRepository) FilePath(com.intellij.openapi.vcs.FilePath) git4idea(git4idea) VcsLogSorter(com.intellij.vcs.log.data.VcsLogSorter) GraphCommit(com.intellij.vcs.log.graph.GraphCommit) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.vcs.log(com.intellij.vcs.log) OpenTHashSet(com.intellij.util.containers.OpenTHashSet) Collectors(java.util.stream.Collectors) Disposable(com.intellij.openapi.Disposable) Nullable(org.jetbrains.annotations.Nullable) GitBranchesCollection(git4idea.branch.GitBranchesCollection) HashImpl(com.intellij.vcs.log.impl.HashImpl) StopWatch(com.intellij.vcs.log.util.StopWatch) VcsUserUtil(com.intellij.vcs.log.util.VcsUserUtil) Attachment(com.intellij.openapi.diagnostic.Attachment) Registry(com.intellij.openapi.util.registry.Registry) com.intellij.util(com.intellij.util) NotNull(org.jetbrains.annotations.NotNull) FilePath(com.intellij.openapi.vcs.FilePath) VcsUserUtil(com.intellij.vcs.log.util.VcsUserUtil) GitRepository(git4idea.repo.GitRepository) NotNull(org.jetbrains.annotations.NotNull)

Example 88 with VcsException

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

the class GitCheckinEnvironment method commit.

public List<VcsException> commit(@NotNull List<Change> changes, @NotNull String message, @NotNull NullableFunction<Object, Object> parametersHolder, Set<String> feedback) {
    List<VcsException> exceptions = new ArrayList<>();
    Map<VirtualFile, Collection<Change>> sortedChanges = sortChangesByGitRoot(changes, exceptions);
    LOG.assertTrue(!sortedChanges.isEmpty(), "Trying to commit an empty list of changes: " + changes);
    for (Map.Entry<VirtualFile, Collection<Change>> entry : sortedChanges.entrySet()) {
        VirtualFile root = entry.getKey();
        File messageFile;
        try {
            messageFile = createMessageFile(root, message);
        } catch (IOException ex) {
            //noinspection ThrowableInstanceNeverThrown
            exceptions.add(new VcsException("Creation of commit message file failed", ex));
            continue;
        }
        Set<FilePath> added = new HashSet<>();
        Set<FilePath> removed = new HashSet<>();
        final Set<Change> caseOnlyRenames = new HashSet<>();
        for (Change change : entry.getValue()) {
            switch(change.getType()) {
                case NEW:
                case MODIFICATION:
                    added.add(change.getAfterRevision().getFile());
                    break;
                case DELETED:
                    removed.add(change.getBeforeRevision().getFile());
                    break;
                case MOVED:
                    FilePath afterPath = change.getAfterRevision().getFile();
                    FilePath beforePath = change.getBeforeRevision().getFile();
                    if (!SystemInfo.isFileSystemCaseSensitive && GitUtil.isCaseOnlyChange(beforePath.getPath(), afterPath.getPath())) {
                        caseOnlyRenames.add(change);
                    } else {
                        added.add(afterPath);
                        removed.add(beforePath);
                    }
                    break;
                default:
                    throw new IllegalStateException("Unknown change type: " + change.getType());
            }
        }
        try {
            if (!caseOnlyRenames.isEmpty()) {
                List<VcsException> exs = commitWithCaseOnlyRename(myProject, root, caseOnlyRenames, added, removed, messageFile, myNextCommitAuthor);
                exceptions.addAll(map(exs, GitCheckinEnvironment::cleanupExceptionText));
            } else {
                try {
                    Set<FilePath> files = new HashSet<>();
                    files.addAll(added);
                    files.addAll(removed);
                    commit(myProject, root, files, messageFile);
                } catch (VcsException ex) {
                    PartialOperation partialOperation = isMergeCommit(ex);
                    if (partialOperation == PartialOperation.NONE) {
                        throw ex;
                    }
                    if (!mergeCommit(myProject, root, added, removed, messageFile, myNextCommitAuthor, exceptions, partialOperation)) {
                        throw ex;
                    }
                }
            }
        } catch (VcsException e) {
            exceptions.add(cleanupExceptionText(e));
        } finally {
            if (!messageFile.delete()) {
                LOG.warn("Failed to remove temporary file: " + messageFile);
            }
        }
    }
    if (myNextCommitIsPushed != null && myNextCommitIsPushed.booleanValue() && exceptions.isEmpty()) {
        GitRepositoryManager manager = getRepositoryManager(myProject);
        Collection<GitRepository> repositories = GitUtil.getRepositoriesFromRoots(manager, sortedChanges.keySet());
        final List<GitRepository> preselectedRepositories = newArrayList(repositories);
        GuiUtils.invokeLaterIfNeeded(() -> new VcsPushDialog(myProject, preselectedRepositories, GitBranchUtil.getCurrentRepository(myProject)).show(), ModalityState.defaultModalityState());
    }
    return exceptions;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FilePath(com.intellij.openapi.vcs.FilePath) GitRepositoryManager(git4idea.repo.GitRepositoryManager) GitRepository(git4idea.repo.GitRepository) VcsPushDialog(com.intellij.dvcs.push.ui.VcsPushDialog) VcsException(com.intellij.openapi.vcs.VcsException) VirtualFile(com.intellij.openapi.vfs.VirtualFile)

Example 89 with VcsException

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

the class GitCheckinEnvironment method cleanupExceptionText.

@NotNull
private static VcsException cleanupExceptionText(VcsException original) {
    String msg = original.getMessage();
    msg = GitUtil.cleanupErrorPrefixes(msg);
    final String DURING_EXECUTING_SUFFIX = GitSimpleHandler.DURING_EXECUTING_ERROR_MESSAGE;
    int suffix = msg.indexOf(DURING_EXECUTING_SUFFIX);
    if (suffix > 0) {
        msg = msg.substring(0, suffix);
    }
    return new VcsException(msg.trim(), original.getCause());
}
Also used : VcsException(com.intellij.openapi.vcs.VcsException) GitUtil.getLogString(git4idea.GitUtil.getLogString) NotNull(org.jetbrains.annotations.NotNull) ObjectUtils.assertNotNull(com.intellij.util.ObjectUtils.assertNotNull)

Example 90 with VcsException

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

the class GitCheckinEnvironment method scheduleMissingFileForDeletion.

public List<VcsException> scheduleMissingFileForDeletion(List<FilePath> files) {
    ArrayList<VcsException> rc = new ArrayList<>();
    Map<VirtualFile, List<FilePath>> sortedFiles;
    try {
        sortedFiles = GitUtil.sortFilePathsByGitRoot(files);
    } catch (VcsException e) {
        rc.add(e);
        return rc;
    }
    for (Map.Entry<VirtualFile, List<FilePath>> e : sortedFiles.entrySet()) {
        try {
            final VirtualFile root = e.getKey();
            GitFileUtils.delete(myProject, root, e.getValue());
            markRootDirty(root);
        } catch (VcsException ex) {
            rc.add(ex);
        }
    }
    return rc;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VcsException(com.intellij.openapi.vcs.VcsException) Arrays.asList(java.util.Arrays.asList) List(java.util.List)

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