Search in sources :

Example 1 with VcsRunnable

use of com.intellij.vcsUtil.VcsRunnable in project intellij-community by JetBrains.

the class SvnMergeProvider method loadRevisions.

@NotNull
public MergeData loadRevisions(@NotNull final VirtualFile file) throws VcsException {
    final MergeData data = new MergeData();
    VcsRunnable runnable = () -> {
        File oldFile = null;
        File newFile = null;
        File workingFile = null;
        boolean mergeCase = false;
        SvnVcs vcs = SvnVcs.getInstance(myProject);
        Info info = vcs.getInfo(file);
        if (info != null) {
            oldFile = info.getConflictOldFile();
            newFile = info.getConflictNewFile();
            workingFile = info.getConflictWrkFile();
            mergeCase = workingFile == null || workingFile.getName().contains("working");
            // for debug
            if (workingFile == null) {
                LOG.info("Null working file when merging text conflict for " + file.getPath() + " old file: " + oldFile + " new file: " + newFile);
            }
            if (mergeCase) {
                // this is merge case
                oldFile = info.getConflictNewFile();
                newFile = info.getConflictOldFile();
                workingFile = info.getConflictWrkFile();
            }
            data.LAST_REVISION_NUMBER = new SvnRevisionNumber(info.getRevision());
        } else {
            throw new VcsException("Could not get info for " + file.getPath());
        }
        if (oldFile == null || newFile == null || workingFile == null) {
            ByteArrayOutputStream bos = getBaseRevisionContents(vcs, file);
            data.ORIGINAL = bos.toByteArray();
            data.LAST = bos.toByteArray();
            data.CURRENT = readFile(virtualToIoFile(file));
        } else {
            data.ORIGINAL = readFile(oldFile);
            data.LAST = readFile(newFile);
            data.CURRENT = readFile(workingFile);
        }
        if (mergeCase) {
            final ByteArrayOutputStream contents = getBaseRevisionContents(vcs, file);
            if (!Arrays.equals(contents.toByteArray(), data.ORIGINAL)) {
                // swap base and server: another order of merge arguments
                byte[] original = data.ORIGINAL;
                data.ORIGINAL = data.LAST;
                data.LAST = original;
            }
        }
    };
    VcsUtil.runVcsProcessWithProgress(runnable, VcsBundle.message("multiple.file.merge.loading.progress.title"), false, myProject);
    return data;
}
Also used : SvnRevisionNumber(org.jetbrains.idea.svn.SvnRevisionNumber) VcsRunnable(com.intellij.vcsUtil.VcsRunnable) MergeData(com.intellij.openapi.vcs.merge.MergeData) VcsException(com.intellij.openapi.vcs.VcsException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Info(org.jetbrains.idea.svn.info.Info) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) File(java.io.File) SvnVcs(org.jetbrains.idea.svn.SvnVcs) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with VcsRunnable

use of com.intellij.vcsUtil.VcsRunnable in project intellij-community by JetBrains.

the class HgMergeProvider method loadRevisions.

@NotNull
@Override
public MergeData loadRevisions(@NotNull final VirtualFile file) throws VcsException {
    final MergeData mergeData = new MergeData();
    final VcsRunnable runnable = new VcsRunnable() {

        @Override
        public void run() throws VcsException {
            final HgWorkingCopyRevisionsCommand command = new HgWorkingCopyRevisionsCommand(myProject);
            final VirtualFile repo = HgUtil.getHgRootOrThrow(myProject, file);
            final HgFile hgFile = new HgFile(myProject, file);
            HgRevisionNumber serverRevisionNumber;
            HgRevisionNumber localRevisionNumber;
            HgRevisionNumber baseRevisionNumber = null;
            // there are two possibilities: we have checked in local changes in the selected file or we didn't.
            if (wasFileCheckedIn(repo, file)) {
                // 1. We checked in.
                // We have a merge in progress, which means we have 2 heads (parents).
                // the second one is "their" revision pulled from the parent repo,
                // first parent is the local change.
                // to retrieve the base version we get the parent of the local change, i.e. the [only] parent of the first parent.
                //Which one is local revision depends on which one is merged with,
                // i.e if you update to 17 revision and then merge it with 23, so 17 is your local and 17->parent is your base revision.
                // This may produce misunderstanding when you update your project with merging (your update firstly to next revisions  and then
                // merge with previous). see http://hgbook.red-bean.com/read/managing-releases-and-branchy-development.html
                final Couple<HgRevisionNumber> parents = command.parents(repo, file);
                serverRevisionNumber = parents.second;
                localRevisionNumber = parents.first;
                final HgContentRevision local = HgContentRevision.create(myProject, hgFile, localRevisionNumber);
                mergeData.CURRENT = local.getContentAsBytes();
                // we are sure that we have a common ancestor, because otherwise we'll get "repository is unrelated" error while pulling,
                // due to different root changesets which is prohibited.
                // Find common ancestor of two revisions : hg debugancestor rev1 rev2
                // Using quotes may produce wrong escaping errors on Unix-type systems
                List<String> arguments = new ArrayList<>();
                String localChangeset = localRevisionNumber.getChangeset();
                String serverChangeset = serverRevisionNumber.getChangeset();
                arguments.add(StringUtil.isEmptyOrSpaces(localChangeset) ? localRevisionNumber.getRevision() : localChangeset);
                arguments.add(StringUtil.isEmptyOrSpaces(serverChangeset) ? serverRevisionNumber.getRevision() : serverChangeset);
                HgCommandResult result = new HgPromptCommandExecutor(myProject).executeInCurrentThread(repo, "debugancestor", arguments);
                if (result != null) {
                    String output = result.getRawOutput();
                    final List<String> parts = StringUtil.split(output, ":");
                    if (parts.size() < 2) {
                        LOG.info("Couldn't parse result of debugancestor command execution " + arguments);
                        new HgCommandResultNotifier(myProject).notifyError(null, HgVcsMessages.message("hg4idea.error.debugancestor.command.execution"), HgVcsMessages.message("hg4idea.error.debugancestor.command.description"));
                    } else {
                        baseRevisionNumber = HgRevisionNumber.getInstance(parts.get(0), parts.get(1));
                    }
                } else {
                    LOG.info(HgVcsMessages.message("hg4idea.error.debugancestor.command.execution") + arguments);
                    new HgCommandResultNotifier(myProject).notifyError(null, HgVcsMessages.message("hg4idea.error.debugancestor.command.execution"), HgVcsMessages.message("hg4idea.error.debugancestor.command.description"));
                }
            } else {
                // 2. local changes are not checked in.
                // then there is only one parent, which is server changes.
                // local changes are retrieved from the file system, they are not in the Mercurial yet.
                // base is the only parent of server changes.
                serverRevisionNumber = command.parents(repo, file).first;
                baseRevisionNumber = command.parents(repo, file, serverRevisionNumber).first;
                final File origFile = new File(file.getPath() + ".orig");
                mergeData.CURRENT = VcsUtil.getFileByteContent(origFile);
            }
            if (baseRevisionNumber != null) {
                final HgContentRevision base = HgContentRevision.create(myProject, hgFile, baseRevisionNumber);
                //if file doesn't exist in ancestor revision the base revision should be empty
                mergeData.ORIGINAL = base.getContent() != null ? base.getContentAsBytes() : ArrayUtil.EMPTY_BYTE_ARRAY;
            } else {
                // no base revision means that the file was added simultaneously with different content in both repositories
                mergeData.ORIGINAL = ArrayUtil.EMPTY_BYTE_ARRAY;
            }
            final HgContentRevision server = HgContentRevision.create(myProject, hgFile, serverRevisionNumber);
            mergeData.LAST = server.getContentAsBytes();
            file.refresh(false, false);
        }
    };
    VcsUtil.runVcsProcessWithProgress(runnable, VcsBundle.message("multiple.file.merge.loading.progress.title"), false, myProject);
    return mergeData;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VcsRunnable(com.intellij.vcsUtil.VcsRunnable) HgContentRevision(org.zmlx.hg4idea.HgContentRevision) HgPromptCommandExecutor(org.zmlx.hg4idea.execution.HgPromptCommandExecutor) MergeData(com.intellij.openapi.vcs.merge.MergeData) ArrayList(java.util.ArrayList) HgCommandResultNotifier(org.zmlx.hg4idea.action.HgCommandResultNotifier) HgWorkingCopyRevisionsCommand(org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand) HgCommandResult(org.zmlx.hg4idea.execution.HgCommandResult) HgFile(org.zmlx.hg4idea.HgFile) HgRevisionNumber(org.zmlx.hg4idea.HgRevisionNumber) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HgFile(org.zmlx.hg4idea.HgFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with VcsRunnable

use of com.intellij.vcsUtil.VcsRunnable 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)

Example 4 with VcsRunnable

use of com.intellij.vcsUtil.VcsRunnable in project intellij-community by JetBrains.

the class DefaultPatchBaseVersionProvider method getBaseVersionContent.

public void getBaseVersionContent(final FilePath filePath, final Processor<CharSequence> processor, final List<String> warnings) throws VcsException {
    if (myVcs == null) {
        return;
    }
    final VcsHistoryProvider historyProvider = myVcs.getVcsHistoryProvider();
    if (historyProvider == null)
        return;
    VcsRevisionNumber revision = null;
    if (myRevisionPattern != null) {
        final Matcher matcher = myRevisionPattern.matcher(myVersionId);
        if (matcher.find()) {
            revision = myVcs.parseRevisionNumber(matcher.group(1), filePath);
            final VcsRevisionNumber finalRevision = revision;
            final Boolean[] loadedExactRevision = new Boolean[1];
            if (historyProvider instanceof VcsBaseRevisionAdviser) {
                final boolean success = VcsUtil.runVcsProcessWithProgress(new VcsRunnable() {

                    public void run() throws VcsException {
                        loadedExactRevision[0] = ((VcsBaseRevisionAdviser) historyProvider).getBaseVersionContent(filePath, processor, finalRevision.asString(), warnings);
                    }
                }, VcsBundle.message("progress.text2.loading.revision", revision.asString()), true, myProject);
                // was cancelled
                if (!success)
                    return;
            } else {
                // use diff provider
                final DiffProvider diffProvider = myVcs.getDiffProvider();
                if (diffProvider != null && filePath.getVirtualFile() != null) {
                    final ContentRevision fileContent = diffProvider.createFileContent(finalRevision, filePath.getVirtualFile());
                    final boolean success = VcsUtil.runVcsProcessWithProgress(new VcsRunnable() {

                        public void run() throws VcsException {
                            loadedExactRevision[0] = !processor.process(fileContent.getContent());
                        }
                    }, VcsBundle.message("progress.text2.loading.revision", revision.asString()), true, myProject);
                    // was cancelled
                    if (!success)
                        return;
                }
            }
            if (Boolean.TRUE.equals(loadedExactRevision[0]))
                return;
        }
    }
    Date versionDate = null;
    if (revision == null) {
        try {
            final Matcher tsMatcher = ourTsPattern.matcher(myVersionId);
            if (tsMatcher.find()) {
                final Long fromTsPattern = getFromTsPattern();
                if (fromTsPattern == null)
                    return;
                versionDate = new Date(fromTsPattern);
            } else {
                versionDate = new Date(myVersionId);
            }
        } catch (IllegalArgumentException ex) {
            return;
        }
    }
    try {
        final Ref<VcsHistorySession> ref = new Ref<>();
        boolean result = VcsUtil.runVcsProcessWithProgress(new VcsRunnable() {

            public void run() throws VcsException {
                ref.set(historyProvider.createSessionFor(filePath));
            }
        }, VcsBundle.message("loading.file.history.progress"), true, myProject);
        //if not found or cancelled
        if (ref.isNull() || !result)
            return;
        final VcsHistorySession session = ref.get();
        final List<VcsFileRevision> list = session.getRevisionList();
        if (list == null)
            return;
        for (VcsFileRevision fileRevision : list) {
            boolean found;
            if (revision != null) {
                found = fileRevision.getRevisionNumber().compareTo(revision) <= 0;
            } else {
                final Date date = fileRevision instanceof VcsFileRevisionDvcsSpecific ? ((VcsFileRevisionDvcsSpecific) fileRevision).getDateForRevisionsOrdering() : fileRevision.getRevisionDate();
                found = (date != null) && (date.before(versionDate) || date.equals(versionDate));
            }
            if (found) {
                fileRevision.loadContent();
                processor.process(LoadTextUtil.getTextByBinaryPresentation(fileRevision.getContent(), myFile, false, false));
                // TODO: try to download more than one version
                break;
            }
        }
    } catch (IOException e) {
        LOG.error(e);
    }
}
Also used : VcsRunnable(com.intellij.vcsUtil.VcsRunnable) Matcher(java.util.regex.Matcher) ContentRevision(com.intellij.openapi.vcs.changes.ContentRevision) IOException(java.io.IOException) Date(java.util.Date) Ref(com.intellij.openapi.util.Ref) DiffProvider(com.intellij.openapi.vcs.diff.DiffProvider)

Aggregations

VcsRunnable (com.intellij.vcsUtil.VcsRunnable)4 MergeData (com.intellij.openapi.vcs.merge.MergeData)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 NotNull (org.jetbrains.annotations.NotNull)3 VcsException (com.intellij.openapi.vcs.VcsException)2 File (java.io.File)2 IOException (java.io.IOException)2 Ref (com.intellij.openapi.util.Ref)1 FilePath (com.intellij.openapi.vcs.FilePath)1 ContentRevision (com.intellij.openapi.vcs.changes.ContentRevision)1 DiffProvider (com.intellij.openapi.vcs.diff.DiffProvider)1 VfsUtilCore.virtualToIoFile (com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile)1 GitFileRevision (git4idea.GitFileRevision)1 GitRevisionNumber (git4idea.GitRevisionNumber)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 SvnRevisionNumber (org.jetbrains.idea.svn.SvnRevisionNumber)1 SvnVcs (org.jetbrains.idea.svn.SvnVcs)1