Search in sources :

Example 81 with FilePath

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

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

the class GitChangesParser method parseChange.

private static Change parseChange(final Project project, final VirtualFile vcsRoot, final List<GitRevisionNumber> parentRevisions, final GitLogStatusInfo statusInfo, final VcsRevisionNumber thisRevision) throws VcsException {
    final ContentRevision before;
    final ContentRevision after;
    FileStatus status = null;
    final String path = statusInfo.getFirstPath();
    @Nullable GitRevisionNumber firstParent = parentRevisions.isEmpty() ? null : parentRevisions.get(0);
    switch(statusInfo.getType()) {
        case ADDED:
            before = null;
            status = FileStatus.ADDED;
            after = GitContentRevision.createRevision(vcsRoot, path, thisRevision, project, false, false, true);
            break;
        case UNRESOLVED:
            status = FileStatus.MERGED_WITH_CONFLICTS;
        case MODIFIED:
            if (status == null) {
                status = FileStatus.MODIFIED;
            }
            final FilePath filePath = GitContentRevision.createPath(vcsRoot, path, false, true, true);
            before = GitContentRevision.createRevision(vcsRoot, path, firstParent, project, false, false, true);
            after = GitContentRevision.createRevision(filePath, thisRevision, project, null);
            break;
        case DELETED:
            status = FileStatus.DELETED;
            final FilePath filePathDeleted = GitContentRevision.createPath(vcsRoot, path, true, true, true);
            before = GitContentRevision.createRevision(filePathDeleted, firstParent, project, null);
            after = null;
            break;
        case COPIED:
        case RENAMED:
            status = FileStatus.MODIFIED;
            String secondPath = statusInfo.getSecondPath();
            final FilePath filePathAfterRename = GitContentRevision.createPath(vcsRoot, secondPath == null ? path : secondPath, false, false, true);
            before = GitContentRevision.createRevision(vcsRoot, path, firstParent, project, true, true, true);
            after = GitContentRevision.createRevision(filePathAfterRename, thisRevision, project, null);
            break;
        case TYPE_CHANGED:
            status = FileStatus.MODIFIED;
            final FilePath filePath2 = GitContentRevision.createPath(vcsRoot, path, false, true, true);
            before = GitContentRevision.createRevision(vcsRoot, path, firstParent, project, false, false, true);
            after = GitContentRevision.createRevision(filePath2, thisRevision, project, null);
            break;
        default:
            throw new AssertionError("Unknown file status: " + statusInfo);
    }
    return new Change(before, after, status);
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) FileStatus(com.intellij.openapi.vcs.FileStatus) GitRevisionNumber(git4idea.GitRevisionNumber) ContentRevision(com.intellij.openapi.vcs.changes.ContentRevision) GitContentRevision(git4idea.GitContentRevision) Change(com.intellij.openapi.vcs.changes.Change) Nullable(org.jetbrains.annotations.Nullable)

Example 83 with FilePath

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

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

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

the class GitMergeProvider method loadRevisions.

@Override
@NotNull
public MergeData loadRevisions(@NotNull final VirtualFile file) throws VcsException {
    final MergeData mergeData = new MergeData();
    final VirtualFile root = GitUtil.getGitRoot(file);
    final FilePath path = VcsUtil.getFilePath(file.getPath());
    VcsRunnable runnable = new VcsRunnable() {

        @Override
        @SuppressWarnings({ "ConstantConditions" })
        public void run() throws VcsException {
            GitFileRevision original = new GitFileRevision(myProject, path, new GitRevisionNumber(":" + ORIGINAL_REVISION_NUM));
            GitFileRevision current = new GitFileRevision(myProject, path, new GitRevisionNumber(":" + yoursRevision(root)));
            GitFileRevision last = new GitFileRevision(myProject, path, new GitRevisionNumber(":" + theirsRevision(root)));
            try {
                try {
                    mergeData.ORIGINAL = original.getContent();
                } catch (Exception ex) {
                    /// This could happen in case if rebasing.
                    try {
                        mergeData.ORIGINAL = file.contentsToByteArray();
                    } catch (IOException e) {
                        LOG.error(e);
                        mergeData.ORIGINAL = ArrayUtil.EMPTY_BYTE_ARRAY;
                    }
                }
                mergeData.CURRENT = loadRevisionCatchingErrors(current);
                mergeData.LAST = loadRevisionCatchingErrors(last);
                // TODO: can be done once for a root
                mergeData.CURRENT_REVISION_NUMBER = findCurrentRevisionNumber(root);
                mergeData.LAST_REVISION_NUMBER = findLastRevisionNumber(root);
                mergeData.ORIGINAL_REVISION_NUMBER = findOriginalRevisionNumber(root, mergeData.CURRENT_REVISION_NUMBER, mergeData.LAST_REVISION_NUMBER);
                Trinity<String, String, String> blobs = getAffectedBlobs(root, file);
                mergeData.CURRENT_FILE_PATH = getBlobPathInRevision(root, file, blobs.getFirst(), mergeData.CURRENT_REVISION_NUMBER);
                mergeData.ORIGINAL_FILE_PATH = getBlobPathInRevision(root, file, blobs.getSecond(), mergeData.ORIGINAL_REVISION_NUMBER);
                mergeData.LAST_FILE_PATH = getBlobPathInRevision(root, file, blobs.getThird(), mergeData.LAST_REVISION_NUMBER);
            } catch (IOException e) {
                throw new IllegalStateException("Failed to load file content", e);
            }
        }
    };
    VcsUtil.runVcsProcessWithProgress(runnable, GitBundle.message("merge.load.files"), false, myProject);
    return mergeData;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FilePath(com.intellij.openapi.vcs.FilePath) VcsRunnable(com.intellij.vcsUtil.VcsRunnable) GitRevisionNumber(git4idea.GitRevisionNumber) GitFileRevision(git4idea.GitFileRevision) MergeData(com.intellij.openapi.vcs.merge.MergeData) IOException(java.io.IOException) VcsException(com.intellij.openapi.vcs.VcsException) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

FilePath (com.intellij.openapi.vcs.FilePath)167 VirtualFile (com.intellij.openapi.vfs.VirtualFile)68 NotNull (org.jetbrains.annotations.NotNull)43 VcsException (com.intellij.openapi.vcs.VcsException)34 File (java.io.File)30 Change (com.intellij.openapi.vcs.changes.Change)26 Nullable (org.jetbrains.annotations.Nullable)22 Project (com.intellij.openapi.project.Project)20 ContentRevision (com.intellij.openapi.vcs.changes.ContentRevision)15 VcsFileRevision (com.intellij.openapi.vcs.history.VcsFileRevision)13 FileStatus (com.intellij.openapi.vcs.FileStatus)10 AbstractVcs (com.intellij.openapi.vcs.AbstractVcs)8 GitRepository (git4idea.repo.GitRepository)7 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Test (org.junit.Test)6 StringUtil (com.intellij.openapi.util.text.StringUtil)5 Logger (com.intellij.openapi.diagnostic.Logger)4