From 1f31163e2fe9b87b518944da5ce7768db485e750 Mon Sep 17 00:00:00 2001 From: Ruslan Peshchuk Date: Wed, 10 Jun 2020 21:25:48 +0200 Subject: [PATCH] Explicit dependency on git4idea has been removed. Support of SVN (an theoretically other VCS) has been added. --- README.md | 6 --- .../crucible/connection/CrucibleApi.java | 35 ++++++++-------- .../connection/CrucibleSessionImpl.java | 23 +++-------- .../jetbrains/crucible/model/Repository.java | 4 +- .../jetbrains/crucible/model/ReviewItem.java | 2 +- src/com/jetbrains/crucible/vcs/VcsUtils.java | 41 ++++--------------- 6 files changed, 35 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index f7a91f7..79052f3 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,3 @@ # For developers Setup IntelliJ plugin SDK (see https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/using_dev_kit.html) - -Add git4idea classpath to SDK: -1. Open File -> Project structure... -2. Platform Settings -> SDKs -3. *Your version of IntelliJ IDEA IU* -> Classpath -4. Add /plugins/git4idea/lib/git4idea.jar diff --git a/src/com/jetbrains/crucible/connection/CrucibleApi.java b/src/com/jetbrains/crucible/connection/CrucibleApi.java index 984b838..1684842 100644 --- a/src/com/jetbrains/crucible/connection/CrucibleApi.java +++ b/src/com/jetbrains/crucible/connection/CrucibleApi.java @@ -15,7 +15,6 @@ import com.intellij.openapi.vcs.changes.patch.MatchPatchPaths; import com.intellij.openapi.vcs.changes.patch.TextFilePatchInProgress; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; import com.jetbrains.crucible.model.*; import org.apache.commons.httpclient.methods.RequestEntity; @@ -49,7 +48,7 @@ private static Gson initGson() { @NotNull public static BasicReview parseReview(@NotNull JsonObject item, @NotNull Project project, @NotNull CrucibleSession crucibleSession) - throws IOException { + throws IOException { ReviewRaw reviewRaw = gson.fromJson(item, ReviewRaw.class); User moderator = (reviewRaw.moderator != null) ? reviewRaw.moderator.createUser() : null; @@ -129,7 +128,8 @@ private static ReviewItem parsePatchReviewItem(ReviewItemRaw raw, Project projec return null; } - final VirtualFile repo = ProjectLevelVcsManager.getInstance(project).getVcsRootFor(new LocalFilePath(base.getAbsolutePath(), base.isDirectory())); + final VirtualFile repo = + ProjectLevelVcsManager.getInstance(project).getVcsRootFor(new LocalFilePath(base.getAbsolutePath(), base.isDirectory())); if (repo == null) { LOG.error("Couldn't find repository for base " + base); return null; @@ -137,11 +137,11 @@ private static ReviewItem parsePatchReviewItem(ReviewItemRaw raw, Project projec Map.Entry repoEntry = ContainerUtil.find(crucibleSession.getRepoHash().entrySet(), new Condition>() { - @Override - public boolean value(Map.Entry entry) { - return entry.getValue().equals(repo); - } - }); + @Override + public boolean value(Map.Entry entry) { + return entry.getValue().equals(repo); + } + }); if (repoEntry == null) { LOG.error("Couldn't find repository name for root " + repo); @@ -155,7 +155,8 @@ public boolean value(Map.Entry entry) { // temporary workaround until ReviewItem is rethinked @NotNull - private static AbstractFilePatchInProgress findBestMatchingPatchByPath(@NotNull String toPath, @NotNull List patches) { + private static AbstractFilePatchInProgress findBestMatchingPatchByPath(@NotNull String toPath, + @NotNull List patches) { int bestSimilarity = -1; AbstractFilePatchInProgress bestCandidate = null; for (AbstractFilePatchInProgress patch : patches) { @@ -253,14 +254,13 @@ else if (raw instanceof GeneralComment) { } @NotNull - public static Collection parseGitRepositories(@NotNull JsonObject object) { - Repositories repositories = gson.fromJson(object, Repositories.class); - return ContainerUtil.mapNotNull(repositories.repoData, new Function() { - @Override - public Repository fun(RepoRaw repository) { - return "git".equalsIgnoreCase(repository.type) ? new Repository(repository.name, repository.location) : null; - } - }); + public static Collection parseRepositories(@NotNull JsonObject object) { + final Repositories repositories = gson.fromJson(object, Repositories.class); + + return ContainerUtil.mapNotNull(repositories.repoData, + repository -> repository.name != null && repository.location != null + ? new Repository(repository.name, repository.location) + : null); } @NotNull @@ -477,5 +477,4 @@ private static class Stat { int leaveUnread; int read; } - } diff --git a/src/com/jetbrains/crucible/connection/CrucibleSessionImpl.java b/src/com/jetbrains/crucible/connection/CrucibleSessionImpl.java index fc5361b..a2071f7 100644 --- a/src/com/jetbrains/crucible/connection/CrucibleSessionImpl.java +++ b/src/com/jetbrains/crucible/connection/CrucibleSessionImpl.java @@ -1,6 +1,7 @@ package com.jetbrains.crucible.connection; import com.google.gson.*; +import com.intellij.dvcs.repo.VcsRepositoryManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.MessageType; @@ -11,10 +12,6 @@ import com.jetbrains.crucible.connection.exceptions.CrucibleApiLoginException; import com.jetbrains.crucible.model.*; import com.jetbrains.crucible.ui.UiUtils; -import git4idea.GitUtil; -import git4idea.repo.GitRemote; -import git4idea.repo.GitRepository; -import git4idea.repo.GitRepositoryManager; import org.apache.commons.codec.binary.Base64; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; @@ -267,7 +264,7 @@ public Comment postComment(@NotNull final Comment comment, boolean isGeneral, public void fillRepoHash() throws IOException { String url = getHostUrl() + REPOSITORIES; final JsonObject jsonObject = buildJsonResponse(url); - Collection repos = CrucibleApi.parseGitRepositories(jsonObject); + Collection repos = CrucibleApi.parseRepositories(jsonObject); for (Repository repo : repos) { VirtualFile localPath = getLocalPath(repo); @@ -278,18 +275,10 @@ public void fillRepoHash() throws IOException { @Nullable protected VirtualFile getLocalPath(@NotNull Repository repository) { - GitRepositoryManager manager = GitUtil.getRepositoryManager(myProject); - List repositories = manager.getRepositories(); - String location = repository.getUrl(); - for (GitRepository repo : repositories) { - GitRemote origin = GitUtil.findRemoteByName(repo, GitRemote.ORIGIN); - if (origin != null) { - String originFirstUrl = origin.getFirstUrl(); - if (originFirstUrl == null) continue; - if (location.equals(originFirstUrl)) { - return repo.getRoot(); - } - } + final VcsRepositoryManager manager = VcsRepositoryManager.getInstance(myProject); + final Collection repositories = manager.getRepositories(); + for (com.intellij.dvcs.repo.Repository repo : repositories) { + return repo.getRoot(); } return null; } diff --git a/src/com/jetbrains/crucible/model/Repository.java b/src/com/jetbrains/crucible/model/Repository.java index 59ca1aa..6bdba03 100644 --- a/src/com/jetbrains/crucible/model/Repository.java +++ b/src/com/jetbrains/crucible/model/Repository.java @@ -7,8 +7,8 @@ */ public class Repository { - @NotNull private String myName; - @NotNull private String myUrl; + @NotNull private final String myName; + @NotNull private final String myUrl; public Repository(@NotNull String name, @NotNull String url) { myName = name; diff --git a/src/com/jetbrains/crucible/model/ReviewItem.java b/src/com/jetbrains/crucible/model/ReviewItem.java index a3b68cb..b1f93f9 100644 --- a/src/com/jetbrains/crucible/model/ReviewItem.java +++ b/src/com/jetbrains/crucible/model/ReviewItem.java @@ -41,7 +41,7 @@ public List loadChangeLists(@NotNull Project project, @NotN if (!loadedRevisions.contains(revision)) { final VcsRevisionNumber revisionNumber = vcsFor.parseRevisionNumber(revision); if (revisionNumber != null) { - final CommittedChangeList changeList = VcsUtils.loadRevisionsFromGit(project, revisionNumber, path); + final CommittedChangeList changeList = VcsUtils.loadRevisions(project, revisionNumber, path); if (changeList != null) changeLists.add(changeList); } loadedRevisions.add(revision); diff --git a/src/com/jetbrains/crucible/vcs/VcsUtils.java b/src/com/jetbrains/crucible/vcs/VcsUtils.java index 68d6298..a7df810 100644 --- a/src/com/jetbrains/crucible/vcs/VcsUtils.java +++ b/src/com/jetbrains/crucible/vcs/VcsUtils.java @@ -1,63 +1,40 @@ package com.jetbrains.crucible.vcs; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vcs.AbstractVcs; import com.intellij.openapi.vcs.FilePath; import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vcs.history.VcsRevisionNumber; import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList; -import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ThrowableRunnable; import com.intellij.util.ui.VcsSynchronousProgressWrapper; -import com.intellij.vcs.log.VcsFullCommitDetails; -import com.intellij.vcs.log.util.VcsUserUtil; import com.intellij.vcsUtil.VcsUtil; -import git4idea.GitRevisionNumber; -import git4idea.GitVcs; -import git4idea.changes.GitCommittedChangeList; -import git4idea.history.GitHistoryUtils; -import git4idea.repo.GitRepository; -import git4idea.repo.GitRepositoryManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.*; - /** * User: ktisha */ public class VcsUtils { + private VcsUtils() { } @Nullable - public static CommittedChangeList loadRevisionsFromGit(@NotNull final Project project, - final VcsRevisionNumber number, FilePath filePath) { + public static CommittedChangeList loadRevisions(@NotNull final Project project, final VcsRevisionNumber number, final FilePath filePath) { final CommittedChangeList[] list = new CommittedChangeList[1]; final ThrowableRunnable runnable = () -> { + final AbstractVcs vcs = VcsUtil.getVcsFor(project, filePath); - FilePath lastCommitName = VcsUtil.getLastCommitPath(project, filePath); - GitRepository repository = GitRepositoryManager.getInstance(project).getRepositoryForFile(lastCommitName); - if (repository == null) { + if (vcs == null) { return; } - VirtualFile root = repository.getRoot(); - List gitCommits = new ArrayList<>(); - GitHistoryUtils.loadDetails(project, root, gitCommits::add, - GitHistoryUtils.formHashParameters(repository.getVcs(), Collections.singleton(number.asString()))); - if (gitCommits.size() != 1) { - return; - } - VcsFullCommitDetails gitCommit = gitCommits.get(0); - CommittedChangeList commit = new GitCommittedChangeList(gitCommit.getFullMessage() + " (" + gitCommit.getId().toShortString() + ")", - gitCommit.getFullMessage(), VcsUserUtil.toExactString(gitCommit.getAuthor()), - (GitRevisionNumber)number, - new Date(gitCommit.getAuthorTime()), gitCommit.getChanges(), - Objects.requireNonNull(GitVcs.getInstance(project)), true); - - list[0] = commit; + list[0] = vcs.loadRevisions(filePath.getVirtualFile(), number); }; - final boolean success = VcsSynchronousProgressWrapper.wrap(runnable, project, "Load revision contents"); + + final boolean success = VcsSynchronousProgressWrapper.wrap(runnable, project, "Load Revision Contents"); + return success ? list[0] : null; } } \ No newline at end of file