diff --git a/plugins/jps-cache/src/com/intellij/jps/cache/JpsCachesPluginUtil.java b/plugins/jps-cache/src/com/intellij/jps/cache/JpsCachesPluginUtil.java index 001fdd03967c..d288ef9f7612 100644 --- a/plugins/jps-cache/src/com/intellij/jps/cache/JpsCachesPluginUtil.java +++ b/plugins/jps-cache/src/com/intellij/jps/cache/JpsCachesPluginUtil.java @@ -1,8 +1,14 @@ package com.intellij.jps.cache; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; import com.intellij.util.concurrency.AppExecutorUtil; +import git4idea.repo.GitRepository; +import git4idea.repo.GitRepositoryManager; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.concurrent.ExecutorService; import static com.intellij.execution.process.ProcessIOExecutorService.INSTANCE; @@ -19,4 +25,13 @@ public class JpsCachesPluginUtil { LOG.info("Executor service will be configured with " + threadsCount + " threads"); return threadsCount; } + + public static @Nullable String getCurrentGitBranch(@NotNull Project project) { + List repositories = GitRepositoryManager.getInstance(project).getRepositories(); + if (repositories.size() != 1) { + LOG.warn("Unable to detect current branch for the project with the multiple git repositories"); + return null; + } + return repositories.get(0).getCurrentBranchName(); + } } diff --git a/plugins/jps-cache/src/com/intellij/jps/cache/JpsCachesProjectStateListener.java b/plugins/jps-cache/src/com/intellij/jps/cache/JpsCachesProjectStateListener.java index 5de05ca01c6c..bd27aa24d505 100644 --- a/plugins/jps-cache/src/com/intellij/jps/cache/JpsCachesProjectStateListener.java +++ b/plugins/jps-cache/src/com/intellij/jps/cache/JpsCachesProjectStateListener.java @@ -27,6 +27,7 @@ public class JpsCachesProjectStateListener implements GitRepositoryChangeListene previousCommitId = currentRevision; LOG.info("Remote repository commit changed to " + currentRevision); JpsOutputLoaderManager outputLoaderManager = JpsOutputLoaderManager.getInstance(repository.getProject()); - JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.notifyAboutNearestCache()); + JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> + outputLoaderManager.notifyAboutNearestCache(branch.getName())); } } diff --git a/plugins/jps-cache/src/com/intellij/jps/cache/action/JpsForceUpdateCachesAction.java b/plugins/jps-cache/src/com/intellij/jps/cache/action/JpsForceUpdateCachesAction.java index bdb2e16a4e1d..62dbe1fd5221 100644 --- a/plugins/jps-cache/src/com/intellij/jps/cache/action/JpsForceUpdateCachesAction.java +++ b/plugins/jps-cache/src/com/intellij/jps/cache/action/JpsForceUpdateCachesAction.java @@ -1,5 +1,6 @@ package com.intellij.jps.cache.action; +import com.intellij.jps.cache.JpsCachesPluginUtil; import com.intellij.jps.cache.client.JpsServerAuthExtension; import com.intellij.jps.cache.loader.JpsOutputLoaderManager; import com.intellij.openapi.actionSystem.AnActionEvent; @@ -12,7 +13,9 @@ public class JpsForceUpdateCachesAction extends DumbAwareAction { public void actionPerformed(@NotNull AnActionEvent actionEvent) { Project project = actionEvent.getProject(); if (project == null) return; + String branch = JpsCachesPluginUtil.getCurrentGitBranch(project); + if (branch == null) return; JpsOutputLoaderManager outputLoaderManager = JpsOutputLoaderManager.getInstance(project); - JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.load(true)); + JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.load(true, branch)); } } diff --git a/plugins/jps-cache/src/com/intellij/jps/cache/action/JpsUpdateCachesAction.java b/plugins/jps-cache/src/com/intellij/jps/cache/action/JpsUpdateCachesAction.java index d86c86eea843..d8a1f11ef9c2 100644 --- a/plugins/jps-cache/src/com/intellij/jps/cache/action/JpsUpdateCachesAction.java +++ b/plugins/jps-cache/src/com/intellij/jps/cache/action/JpsUpdateCachesAction.java @@ -1,5 +1,6 @@ package com.intellij.jps.cache.action; +import com.intellij.jps.cache.JpsCachesPluginUtil; import com.intellij.jps.cache.client.JpsServerAuthExtension; import com.intellij.jps.cache.loader.JpsOutputLoaderManager; import com.intellij.openapi.actionSystem.AnActionEvent; @@ -12,7 +13,9 @@ public class JpsUpdateCachesAction extends DumbAwareAction { public void actionPerformed(@NotNull AnActionEvent actionEvent) { Project project = actionEvent.getProject(); if (project == null) return; + String branch = JpsCachesPluginUtil.getCurrentGitBranch(project); + if (branch == null) return; JpsOutputLoaderManager outputLoaderManager = JpsOutputLoaderManager.getInstance(project); - JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.load(false)); + JpsServerAuthExtension.checkAuthenticatedInBackgroundThread(outputLoaderManager, () -> outputLoaderManager.load(false, branch)); } } \ No newline at end of file diff --git a/plugins/jps-cache/src/com/intellij/jps/cache/client/JpsServerClient.java b/plugins/jps-cache/src/com/intellij/jps/cache/client/JpsServerClient.java index c84353c9bce9..e230589b0b19 100644 --- a/plugins/jps-cache/src/com/intellij/jps/cache/client/JpsServerClient.java +++ b/plugins/jps-cache/src/com/intellij/jps/cache/client/JpsServerClient.java @@ -13,7 +13,7 @@ import java.util.Set; public interface JpsServerClient { @NotNull - Set getAllCacheKeys(@NotNull Project project); + Set getAllCacheKeys(@NotNull Project project, @NotNull String branchName); @Nullable File downloadMetadataById(@NotNull String metadataId, @NotNull File targetDir); File downloadCacheById(@NotNull SegmentedProgressIndicatorManager downloadIndicatorManager, @NotNull String cacheId, diff --git a/plugins/jps-cache/src/com/intellij/jps/cache/client/TemporaryCacheServerClient.java b/plugins/jps-cache/src/com/intellij/jps/cache/client/TemporaryCacheServerClient.java index 6732fa9c74c4..3c32ab64dca4 100644 --- a/plugins/jps-cache/src/com/intellij/jps/cache/client/TemporaryCacheServerClient.java +++ b/plugins/jps-cache/src/com/intellij/jps/cache/client/TemporaryCacheServerClient.java @@ -43,7 +43,6 @@ public class TemporaryCacheServerClient implements JpsServerClient { private static final Logger LOG = Logger.getInstance("com.intellij.jps.cache.client.TemporaryCacheServerClient"); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); static final TemporaryCacheServerClient INSTANCE = new TemporaryCacheServerClient(); - private static final String REPOSITORY_NAME = ""; private final String stringThree; private TemporaryCacheServerClient() { @@ -53,8 +52,8 @@ public class TemporaryCacheServerClient implements JpsServerClient { @NotNull @Override - public Set getAllCacheKeys(@NotNull Project project) { - Map> response = doGetRequest(project, getRequestHeaders()); + public Set getAllCacheKeys(@NotNull Project project, @NotNull String branchName) { + Map> response = doGetRequest(project, branchName, getRequestHeaders()); if (response == null) return Collections.emptySet(); return response.values().stream().flatMap(Collection::stream).collect(Collectors.toSet()); } @@ -62,7 +61,7 @@ public class TemporaryCacheServerClient implements JpsServerClient { @Nullable @Override public File downloadMetadataById(@NotNull String metadataId, @NotNull File targetDir) { - String downloadUrl = stringThree + REPOSITORY_NAME + "/metadata/" + metadataId; + String downloadUrl = stringThree + "/metadata/" + metadataId; DownloadableFileService service = DownloadableFileService.getInstance(); String fileName = "metadata.json"; DownloadableFileDescription description = service.createFileDescription(downloadUrl, fileName); @@ -93,7 +92,7 @@ public class TemporaryCacheServerClient implements JpsServerClient { @Override public File downloadCacheById(@NotNull SegmentedProgressIndicatorManager downloadIndicatorManager, @NotNull String cacheId, @NotNull File targetDir) { - String downloadUrl = stringThree + REPOSITORY_NAME + "/caches/" + cacheId; + String downloadUrl = stringThree + "/caches/" + cacheId; String fileName = "portable-build-cache.zip"; DownloadableFileService service = DownloadableFileService.getInstance(); DownloadableFileDescription description = service.createFileDescription(downloadUrl, fileName); @@ -125,7 +124,7 @@ public class TemporaryCacheServerClient implements JpsServerClient { targetDir.mkdirs(); Map urlToModuleNameMap = affectedModules.stream().collect(Collectors.toMap( - module -> stringThree + REPOSITORY_NAME + "/" + module.getType() + "/" + module.getName() + "/" + module.getHash(), + module -> stringThree + "/" + module.getType() + "/" + module.getName() + "/" + module.getHash(), module -> module)); DownloadableFileService service = DownloadableFileService.getInstance(); @@ -155,9 +154,10 @@ public class TemporaryCacheServerClient implements JpsServerClient { } } - private Map> doGetRequest(@NotNull Project project, @NotNull Map headers) { + private Map> doGetRequest(@NotNull Project project, @NotNull String branchName, @NotNull Map headers) { try { - return HttpRequests.request(stringThree + REPOSITORY_NAME + "/commit_history.json") + String urlPrefix = !branchName.equals("master") ? "/" + branchName : ""; + return HttpRequests.request(stringThree + urlPrefix + "/commit_history.json") .tuner(tuner -> headers.forEach((k, v) -> tuner.addRequestProperty(k, v))) .connect(it -> { URLConnection connection = it.getConnection(); diff --git a/plugins/jps-cache/src/com/intellij/jps/cache/loader/JpsOutputLoaderManager.java b/plugins/jps-cache/src/com/intellij/jps/cache/loader/JpsOutputLoaderManager.java index 3c5cdb9f193f..c8248c57546e 100644 --- a/plugins/jps-cache/src/com/intellij/jps/cache/loader/JpsOutputLoaderManager.java +++ b/plugins/jps-cache/src/com/intellij/jps/cache/loader/JpsOutputLoaderManager.java @@ -83,11 +83,11 @@ public class JpsOutputLoaderManager implements Disposable { if (!buildManager.isGeneratePortableCachesEnabled()) buildManager.setGeneratePortableCachesEnabled(true); } - public void load(boolean isForceUpdate) { + public void load(boolean isForceUpdate, @NotNull String branchName) { Task.Backgroundable task = new Task.Backgroundable(myProject, PROGRESS_TITLE) { @Override public void run(@NotNull ProgressIndicator indicator) { - Pair commitInfo = getNearestCommit(isForceUpdate); + Pair commitInfo = getNearestCommit(isForceUpdate, branchName); if (commitInfo != null) { // Drop JPS metadata to force plugin for downloading all compilation outputs if (isForceUpdate) myMetadataLoader.dropCurrentProjectMetadata(); @@ -103,8 +103,8 @@ public class JpsOutputLoaderManager implements Disposable { ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, processIndicator); } - public void notifyAboutNearestCache() { - Pair commitInfo = getNearestCommit(false); + public void notifyAboutNearestCache(@NotNull String branchName) { + Pair commitInfo = getNearestCommit(false, branchName); if (commitInfo == null) return; String notificationContent = commitInfo.second == 1 @@ -118,15 +118,15 @@ public class JpsOutputLoaderManager implements Disposable { .addAction(NotificationAction.createSimple(JpsCacheBundle.messagePointer( "action.NotificationAction.JpsOutputLoaderManager.text.update.caches"), () -> { notification.expire(); - load(false); + load(false, branchName); })); Notifications.Bus.notify(notification, myProject); }); } @Nullable - private Pair getNearestCommit(boolean isForceUpdate) { - Set allCacheKeys = myServerClient.getAllCacheKeys(myProject); + private Pair getNearestCommit(boolean isForceUpdate, @NotNull String branchName) { + Set allCacheKeys = myServerClient.getAllCacheKeys(myProject, branchName); String previousCommitId = PropertiesComponent.getInstance().getValue(LATEST_COMMIT_ID); List> repositoryList = GitRepositoryUtil.getCommitsIterator(myProject);