Search in sources :

Example 1 with HgWorkingCopyRevisionsCommand

use of org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand 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 2 with HgWorkingCopyRevisionsCommand

use of org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand in project intellij-community by JetBrains.

the class HgAnnotationProvider method annotate.

@NotNull
public FileAnnotation annotate(@NotNull VirtualFile file, VcsFileRevision revision) throws VcsException {
    final VirtualFile vcsRoot = VcsUtil.getVcsRootFor(myProject, VcsUtil.getFilePath(file.getPath()));
    if (vcsRoot == null) {
        throw new VcsException("vcs root is null for " + file);
    }
    HgRevisionNumber revisionNumber = revision != null ? (HgRevisionNumber) revision.getRevisionNumber() : null;
    final HgFile hgFile = new HgFile(vcsRoot, VfsUtilCore.virtualToIoFile(file));
    HgFile fileToAnnotate = revision instanceof HgFileRevision ? HgUtil.getFileNameInTargetRevision(myProject, revisionNumber, hgFile) : new HgFile(vcsRoot, HgUtil.getOriginalFileName(hgFile.toFilePath(), ChangeListManager.getInstance(myProject)));
    final List<HgAnnotationLine> annotationResult = (new HgAnnotateCommand(myProject)).execute(fileToAnnotate, revisionNumber);
    //for uncommitted renamed file we should provide local name otherwise --follow will fail
    final List<HgFileRevision> logResult = HgHistoryProvider.getHistory(revision == null ? hgFile.toFilePath() : fileToAnnotate.toFilePath(), vcsRoot, myProject, null, -1);
    return new HgAnnotation(myProject, hgFile, annotationResult, logResult, revisionNumber != null ? revisionNumber : new HgWorkingCopyRevisionsCommand(myProject).tip(vcsRoot));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) HgFile(org.zmlx.hg4idea.HgFile) HgRevisionNumber(org.zmlx.hg4idea.HgRevisionNumber) HgFileRevision(org.zmlx.hg4idea.HgFileRevision) VcsException(com.intellij.openapi.vcs.VcsException) HgAnnotateCommand(org.zmlx.hg4idea.command.HgAnnotateCommand) HgWorkingCopyRevisionsCommand(org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with HgWorkingCopyRevisionsCommand

use of org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand in project intellij-community by JetBrains.

the class HgRollbackEnvironment method revert.

private void revert(@NotNull List<FilePath> filePaths) {
    for (Map.Entry<VirtualFile, Collection<FilePath>> entry : HgUtil.groupFilePathsByHgRoots(project, filePaths).entrySet()) {
        final VirtualFile repo = entry.getKey();
        final Collection<FilePath> files = entry.getValue();
        HgRevisionNumber revisionNumber = new HgWorkingCopyRevisionsCommand(project).firstParent(repo);
        for (List<String> chunk : VcsFileUtil.chunkPaths(repo, files)) {
            HgCommandResult revertResult = new HgRevertCommand(project).execute(repo, chunk, revisionNumber, false);
            if (HgErrorUtil.hasUncommittedChangesConflict(revertResult)) {
                String message = String.format("<html>Revert failed due to uncommitted merge.<br>" + "Would you like to discard all changes for repository <it><b>%s</b></it>?</html>", repo.getPresentableName());
                int exitCode = HgUpdateCommand.showDiscardChangesConfirmation(project, message);
                if (exitCode == Messages.OK) {
                    //discard all changes for this repository//
                    HgUpdateCommand updateCommand = new HgUpdateCommand(project, repo);
                    updateCommand.setClean(true);
                    updateCommand.setRevision(".");
                    updateCommand.execute();
                }
                break;
            }
            new HgResolveCommand(project).markResolved(repo, files);
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FilePath(com.intellij.openapi.vcs.FilePath) HgResolveCommand(org.zmlx.hg4idea.command.HgResolveCommand) HgWorkingCopyRevisionsCommand(org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand) HgRevertCommand(org.zmlx.hg4idea.command.HgRevertCommand) HgUpdateCommand(org.zmlx.hg4idea.command.HgUpdateCommand) HgCommandResult(org.zmlx.hg4idea.execution.HgCommandResult) HgRevisionNumber(org.zmlx.hg4idea.HgRevisionNumber)

Example 4 with HgWorkingCopyRevisionsCommand

use of org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand in project intellij-community by JetBrains.

the class HgChangeProvider method processUnsavedChanges.

/**
   * Finds modified but unsaved files in the given list of dirty files and notifies the builder about MODIFIED changes.
   * Changes contained in <code>alreadyProcessed</code> are skipped - they have already been processed as modified, or else.
   */
public void processUnsavedChanges(ChangelistBuilder builder, Set<FilePath> dirtyFiles, Collection<HgChange> alreadyProcessed) {
    // exclude already processed
    for (HgChange c : alreadyProcessed) {
        dirtyFiles.remove(c.beforeFile().toFilePath());
        dirtyFiles.remove(c.afterFile().toFilePath());
    }
    final ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(myProject);
    final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
    for (FilePath filePath : dirtyFiles) {
        final VirtualFile vf = filePath.getVirtualFile();
        if (vf != null && fileDocumentManager.isFileModified(vf)) {
            final VirtualFile root = vcsManager.getVcsRootFor(vf);
            if (root != null && HgUtil.isHgRoot(root)) {
                final HgRevisionNumber beforeRevisionNumber = new HgWorkingCopyRevisionsCommand(myProject).firstParent(root);
                final ContentRevision beforeRevision = (beforeRevisionNumber == null ? null : HgContentRevision.create(myProject, new HgFile(myProject, vf), beforeRevisionNumber));
                builder.processChange(new Change(beforeRevision, CurrentContentRevision.create(filePath), FileStatus.MODIFIED), myVcsKey);
            }
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) HgWorkingCopyRevisionsCommand(org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand)

Example 5 with HgWorkingCopyRevisionsCommand

use of org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand in project intellij-community by JetBrains.

the class HgChangeProvider method process.

private Collection<HgChange> process(ChangelistBuilder builder, Collection<FilePath> files) {
    final Set<HgChange> hgChanges = new HashSet<>();
    for (Map.Entry<VirtualFile, Collection<FilePath>> entry : HgUtil.groupFilePathsByHgRoots(myProject, files).entrySet()) {
        VirtualFile repo = entry.getKey();
        final HgRevisionNumber workingRevision = new HgWorkingCopyRevisionsCommand(myProject).identify(repo).getFirst();
        final HgRevisionNumber parentRevision = new HgWorkingCopyRevisionsCommand(myProject).firstParent(repo);
        final Map<HgFile, HgResolveStatusEnum> list = new HgResolveCommand(myProject).getListSynchronously(repo);
        hgChanges.addAll(new HgStatusCommand.Builder(true).ignored(false).build(myProject).executeInCurrentThread(repo, entry.getValue()));
        final HgRepository hgRepo = HgUtil.getRepositoryForFile(myProject, repo);
        if (hgRepo != null && hgRepo.hasSubrepos()) {
            hgChanges.addAll(ContainerUtil.mapNotNull(hgRepo.getSubrepos(), info -> findChange(hgRepo, info)));
        }
        sendChanges(builder, hgChanges, list, workingRevision, parentRevision);
    }
    return hgChanges;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VcsUtil(com.intellij.vcsUtil.VcsUtil) HgStatusCommand(org.zmlx.hg4idea.command.HgStatusCommand) java.util(java.util) org.zmlx.hg4idea(org.zmlx.hg4idea) HgResolveStatusEnum(org.zmlx.hg4idea.command.HgResolveStatusEnum) HgWorkingCopyRevisionsCommand(org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand) VirtualFile(com.intellij.openapi.vfs.VirtualFile) com.intellij.openapi.vcs.changes(com.intellij.openapi.vcs.changes) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) ContainerUtil(com.intellij.util.containers.ContainerUtil) File(java.io.File) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) HgResolveCommand(org.zmlx.hg4idea.command.HgResolveCommand) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) Project(com.intellij.openapi.project.Project) VfsUtil(com.intellij.openapi.vfs.VfsUtil) com.intellij.openapi.vcs(com.intellij.openapi.vcs) NotNull(org.jetbrains.annotations.NotNull) HgRepository(org.zmlx.hg4idea.repo.HgRepository) HgUtil(org.zmlx.hg4idea.util.HgUtil) JBColor(com.intellij.ui.JBColor) HgResolveCommand(org.zmlx.hg4idea.command.HgResolveCommand) HgResolveStatusEnum(org.zmlx.hg4idea.command.HgResolveStatusEnum) HgWorkingCopyRevisionsCommand(org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand) HgRepository(org.zmlx.hg4idea.repo.HgRepository)

Aggregations

HgWorkingCopyRevisionsCommand (org.zmlx.hg4idea.command.HgWorkingCopyRevisionsCommand)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)7 NotNull (org.jetbrains.annotations.NotNull)4 HgRevisionNumber (org.zmlx.hg4idea.HgRevisionNumber)4 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)2 FilePath (com.intellij.openapi.vcs.FilePath)2 File (java.io.File)2 HgFile (org.zmlx.hg4idea.HgFile)2 HgResolveCommand (org.zmlx.hg4idea.command.HgResolveCommand)2 HgCommandResult (org.zmlx.hg4idea.execution.HgCommandResult)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Project (com.intellij.openapi.project.Project)1 com.intellij.openapi.vcs (com.intellij.openapi.vcs)1 VcsException (com.intellij.openapi.vcs.VcsException)1 com.intellij.openapi.vcs.changes (com.intellij.openapi.vcs.changes)1 ItemLatestState (com.intellij.openapi.vcs.diff.ItemLatestState)1 MergeData (com.intellij.openapi.vcs.merge.MergeData)1 VfsUtil (com.intellij.openapi.vfs.VfsUtil)1 JBColor (com.intellij.ui.JBColor)1 ContainerUtil (com.intellij.util.containers.ContainerUtil)1