Search in sources :

Example 76 with VcsException

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

the class RevertCommittedStuffAbstractAction method actionPerformed.

public void actionPerformed(final AnActionEvent e) {
    final Project project = e.getRequiredData(CommonDataKeys.PROJECT);
    final VirtualFile baseDir = project.getBaseDir();
    assert baseDir != null;
    final Change[] changes = myForPerformConvertor.convert(e);
    if (changes == null || changes.length == 0)
        return;
    final List<Change> changesList = new ArrayList<>();
    Collections.addAll(changesList, changes);
    FileDocumentManager.getInstance().saveAllDocuments();
    String defaultName = null;
    final ChangeList[] changeLists = e.getData(VcsDataKeys.CHANGE_LISTS);
    if (changeLists != null && changeLists.length > 0) {
        defaultName = VcsBundle.message("revert.changes.default.name", changeLists[0].getName());
    }
    final ChangeListChooser chooser = new ChangeListChooser(project, ChangeListManager.getInstance(project).getChangeListsCopy(), null, "Select Target Changelist", defaultName);
    if (!chooser.showAndGet()) {
        return;
    }
    final List<FilePatch> patches = new ArrayList<>();
    ProgressManager.getInstance().run(new Task.Backgroundable(project, VcsBundle.message("revert.changes.title"), true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            try {
                final List<Change> preprocessed = ChangesPreprocess.preprocessChangesRemoveDeletedForDuplicateMoved(changesList);
                patches.addAll(IdeaTextPatchBuilder.buildPatch(project, preprocessed, baseDir.getPresentableUrl(), true));
            } catch (final VcsException ex) {
                WaitForProgressToShow.runOrInvokeLaterAboveProgress(new Runnable() {

                    @Override
                    public void run() {
                        Messages.showErrorDialog(project, "Failed to revert changes: " + ex.getMessage(), VcsBundle.message("revert.changes.title"));
                    }
                }, null, myProject);
                indicator.cancel();
            }
        }

        @Override
        public void onSuccess() {
            new PatchApplier<BinaryFilePatch>(project, baseDir, patches, chooser.getSelectedList(), null, null).execute();
        }
    });
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Task(com.intellij.openapi.progress.Task) ChangeListChooser(com.intellij.openapi.vcs.changes.ui.ChangeListChooser) ArrayList(java.util.ArrayList) Change(com.intellij.openapi.vcs.changes.Change) BinaryFilePatch(com.intellij.openapi.diff.impl.patch.BinaryFilePatch) FilePatch(com.intellij.openapi.diff.impl.patch.FilePatch) Project(com.intellij.openapi.project.Project) ChangeList(com.intellij.openapi.vcs.changes.ChangeList) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VcsException(com.intellij.openapi.vcs.VcsException) PatchApplier(com.intellij.openapi.diff.impl.patch.formove.PatchApplier) ArrayList(java.util.ArrayList) List(java.util.List) ChangeList(com.intellij.openapi.vcs.changes.ChangeList)

Example 77 with VcsException

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

the class RollbackDeletionAction method processFiles.

protected List<VcsException> processFiles(final AbstractVcs vcs, final List<FilePath> files) {
    RollbackEnvironment environment = vcs.getRollbackEnvironment();
    if (environment == null)
        return Collections.emptyList();
    final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    if (indicator != null) {
        indicator.setText(vcs.getDisplayName() + ": performing rollback...");
    }
    final List<VcsException> result = new ArrayList<>(0);
    try {
        environment.rollbackMissingFileDeletion(files, result, new RollbackProgressModifier(files.size(), indicator));
    } catch (ProcessCanceledException e) {
    // for files refresh
    }
    LocalFileSystem.getInstance().refreshIoFiles(ChangesUtil.filePathsToFiles(files));
    return result;
}
Also used : RollbackProgressModifier(com.intellij.openapi.vcs.changes.ui.RollbackProgressModifier) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) VcsException(com.intellij.openapi.vcs.VcsException) ArrayList(java.util.ArrayList) RollbackEnvironment(com.intellij.openapi.vcs.rollback.RollbackEnvironment) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 78 with VcsException

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

the class ChangeDiffRequestProducer method createRequest.

@NotNull
private DiffRequest createRequest(@Nullable Project project, @NotNull Change change, @NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException {
    if (ChangesUtil.isTextConflictingChange(change)) {
        // three side diff
        // FIXME: This part is ugly as a VCS merge subsystem itself.
        FilePath path = ChangesUtil.getFilePath(change);
        VirtualFile file = path.getVirtualFile();
        if (file == null) {
            file = LocalFileSystem.getInstance().refreshAndFindFileByPath(path.getPath());
        }
        if (file == null)
            throw new DiffRequestProducerException("Can't show merge conflict - file not found");
        if (project == null) {
            throw new DiffRequestProducerException("Can't show merge conflict - project is unknown");
        }
        final AbstractVcs vcs = ChangesUtil.getVcsForChange(change, project);
        if (vcs == null || vcs.getMergeProvider() == null) {
            throw new DiffRequestProducerException("Can't show merge conflict - operation nos supported");
        }
        try {
            // FIXME: loadRevisions() can call runProcessWithProgressSynchronously() inside
            final Ref<Throwable> exceptionRef = new Ref<>();
            final Ref<MergeData> mergeDataRef = new Ref<>();
            final VirtualFile finalFile = file;
            ApplicationManager.getApplication().invokeAndWait(() -> {
                try {
                    mergeDataRef.set(vcs.getMergeProvider().loadRevisions(finalFile));
                } catch (VcsException e) {
                    exceptionRef.set(e);
                }
            });
            if (!exceptionRef.isNull()) {
                Throwable e = exceptionRef.get();
                if (e instanceof VcsException)
                    throw (VcsException) e;
                if (e instanceof Error)
                    throw (Error) e;
                if (e instanceof RuntimeException)
                    throw (RuntimeException) e;
                throw new RuntimeException(e);
            }
            MergeData mergeData = mergeDataRef.get();
            ContentRevision bRev = change.getBeforeRevision();
            ContentRevision aRev = change.getAfterRevision();
            String beforeRevisionTitle = getRevisionTitle(bRev, "Your version");
            String afterRevisionTitle = getRevisionTitle(aRev, "Server version");
            String title = DiffRequestFactory.getInstance().getTitle(file);
            List<String> titles = ContainerUtil.list(beforeRevisionTitle, "Base Version", afterRevisionTitle);
            DiffContentFactory contentFactory = DiffContentFactory.getInstance();
            List<DiffContent> contents = ContainerUtil.list(contentFactory.createFromBytes(project, mergeData.CURRENT, file), contentFactory.createFromBytes(project, mergeData.ORIGINAL, file), contentFactory.createFromBytes(project, mergeData.LAST, file));
            SimpleDiffRequest request = new SimpleDiffRequest(title, contents, titles);
            MergeUtil.putRevisionInfos(request, mergeData);
            return request;
        } catch (VcsException | IOException e) {
            LOG.info(e);
            throw new DiffRequestProducerException(e);
        }
    } else {
        ContentRevision bRev = change.getBeforeRevision();
        ContentRevision aRev = change.getAfterRevision();
        if (bRev == null && aRev == null) {
            LOG.warn("Both revision contents are empty");
            throw new DiffRequestProducerException("Bad revisions contents");
        }
        if (bRev != null)
            checkContentRevision(project, bRev, context, indicator);
        if (aRev != null)
            checkContentRevision(project, aRev, context, indicator);
        String title = getRequestTitle(change);
        indicator.setIndeterminate(true);
        DiffContent content1 = createContent(project, bRev, context, indicator);
        DiffContent content2 = createContent(project, aRev, context, indicator);
        final String userLeftRevisionTitle = (String) myChangeContext.get(DiffUserDataKeysEx.VCS_DIFF_LEFT_CONTENT_TITLE);
        String beforeRevisionTitle = userLeftRevisionTitle != null ? userLeftRevisionTitle : getRevisionTitle(bRev, "Base version");
        final String userRightRevisionTitle = (String) myChangeContext.get(DiffUserDataKeysEx.VCS_DIFF_RIGHT_CONTENT_TITLE);
        String afterRevisionTitle = userRightRevisionTitle != null ? userRightRevisionTitle : getRevisionTitle(aRev, "Your version");
        SimpleDiffRequest request = new SimpleDiffRequest(title, content1, content2, beforeRevisionTitle, afterRevisionTitle);
        boolean bRevCurrent = bRev instanceof CurrentContentRevision;
        boolean aRevCurrent = aRev instanceof CurrentContentRevision;
        if (bRevCurrent && !aRevCurrent)
            request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.LEFT);
        if (!bRevCurrent && aRevCurrent)
            request.putUserData(DiffUserDataKeys.MASTER_SIDE, Side.RIGHT);
        return request;
    }
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SimpleDiffRequest(com.intellij.diff.requests.SimpleDiffRequest) DiffRequestProducerException(com.intellij.diff.chains.DiffRequestProducerException) DiffContentFactory(com.intellij.diff.DiffContentFactory) MergeData(com.intellij.openapi.vcs.merge.MergeData) IOException(java.io.IOException) AbstractVcs(com.intellij.openapi.vcs.AbstractVcs) Ref(com.intellij.openapi.util.Ref) VcsException(com.intellij.openapi.vcs.VcsException) DiffContent(com.intellij.diff.contents.DiffContent) NotNull(org.jetbrains.annotations.NotNull)

Example 79 with VcsException

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

the class GitAnnotationProvider method parseAnnotations.

@NotNull
private GitFileAnnotation parseAnnotations(@Nullable VcsRevisionNumber revision, @NotNull VirtualFile file, @NotNull VirtualFile root, @NotNull String output) throws VcsException {
    Interner<FilePath> pathInterner = new Interner<>();
    try {
        List<LineInfo> lines = new ArrayList<>();
        HashMap<String, LineInfo> commits = new HashMap<>();
        for (StringScanner s = new StringScanner(output); s.hasMoreData(); ) {
            // parse header line
            String commitHash = s.spaceToken();
            if (commitHash.equals(GitRevisionNumber.NOT_COMMITTED_HASH)) {
                commitHash = null;
            }
            // skip revision line number
            s.spaceToken();
            String s1 = s.spaceToken();
            int lineNum = Integer.parseInt(s1);
            s.nextLine();
            // parse commit information
            LineInfo commit = commits.get(commitHash);
            if (commit != null || commitHash == null) {
                while (s.hasMoreData() && !s.startsWith('\t')) {
                    s.nextLine();
                }
            } else {
                Date committerDate = null;
                FilePath filePath = null;
                String subject = null;
                String authorName = null;
                String authorEmail = null;
                String previousRevision = null;
                FilePath previousFilePath = null;
                while (s.hasMoreData() && !s.startsWith('\t')) {
                    String key = s.spaceToken();
                    String value = s.line();
                    if (SUBJECT_KEY.equals(key)) {
                        subject = value;
                    } else if (AUTHOR_KEY.equals(key)) {
                        authorName = value;
                    } else if (COMMITTER_TIME_KEY.equals(key)) {
                        committerDate = GitUtil.parseTimestamp(value);
                    } else if (FILENAME_KEY.equals(key)) {
                        filePath = VcsUtil.getFilePath(root, value);
                    } else if (AUTHOR_EMAIL_KEY.equals(key)) {
                        authorEmail = value;
                        if (authorEmail.startsWith("<") && authorEmail.endsWith(">")) {
                            authorEmail = authorEmail.substring(1, authorEmail.length() - 1);
                        }
                    } else if (PREVIOUS_KEY.equals(key)) {
                        int index = value.indexOf(' ');
                        if (index != -1) {
                            previousRevision = value.substring(0, index);
                            previousFilePath = VcsUtil.getFilePath(root, value.substring(index + 1, value.length()));
                        }
                    }
                }
                if (committerDate == null || filePath == null || authorName == null || authorEmail == null || subject == null) {
                    throw new VcsException("Output for line " + lineNum + " lacks necessary data");
                }
                GitRevisionNumber revisionNumber = new GitRevisionNumber(commitHash, committerDate);
                VcsUser author = myUserRegistry.createUser(authorName, authorEmail);
                GitRevisionNumber previousRevisionNumber = previousRevision != null ? new GitRevisionNumber(previousRevision) : null;
                filePath = pathInterner.intern(filePath);
                if (previousFilePath != null)
                    previousFilePath = pathInterner.intern(previousFilePath);
                commit = new LineInfo(myProject, revisionNumber, filePath, committerDate, author, subject, previousRevisionNumber, previousFilePath);
                commits.put(commitHash, commit);
            }
            s.nextLine();
            int expectedLineNum = lines.size() + 1;
            if (lineNum != expectedLineNum) {
                throw new VcsException("Adding for info for line " + lineNum + " but we are expecting it to be for " + expectedLineNum);
            }
            lines.add(commit);
        }
        return new GitFileAnnotation(myProject, file, revision, lines);
    } catch (Exception e) {
        LOG.error("Couldn't parse annotation: " + e, new Attachment("output.txt", output));
        throw new VcsException(e);
    }
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) Interner(com.intellij.util.containers.Interner) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Attachment(com.intellij.openapi.diagnostic.Attachment) LineInfo(git4idea.annotate.GitFileAnnotation.LineInfo) Date(java.util.Date) VcsException(com.intellij.openapi.vcs.VcsException) GitRevisionNumber(git4idea.GitRevisionNumber) VcsUser(com.intellij.vcs.log.VcsUser) VcsException(com.intellij.openapi.vcs.VcsException) StringScanner(git4idea.util.StringScanner) NotNull(org.jetbrains.annotations.NotNull)

Example 80 with VcsException

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

the class GitAnnotationProvider method annotate.

@NotNull
public FileAnnotation annotate(@NotNull final VirtualFile file, @Nullable final VcsFileRevision revision) throws VcsException {
    if (file.isDirectory()) {
        throw new VcsException("Cannot annotate a directory");
    }
    final FilePath currentFilePath = VcsUtil.getFilePath(file.getPath());
    final FilePath realFilePath;
    if (revision == null) {
        realFilePath = GitHistoryUtils.getLastCommitName(myProject, currentFilePath);
    } else {
        realFilePath = ((VcsFileRevisionEx) revision).getPath();
    }
    VcsRevisionNumber revisionNumber = revision != null ? revision.getRevisionNumber() : null;
    return annotate(realFilePath, revisionNumber, file);
}
Also used : FilePath(com.intellij.openapi.vcs.FilePath) VcsException(com.intellij.openapi.vcs.VcsException) VcsRevisionNumber(com.intellij.openapi.vcs.history.VcsRevisionNumber) NotNull(org.jetbrains.annotations.NotNull)

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