Skip to content

Commit 4e87ceb

Browse files
committed
fix corner case: Extending a bundle by removing all the parent bundle's files
1 parent cdbf5f7 commit 4e87ceb

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

src/main/java/ai/deepcode/jbplugin/core/AnalysisData.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,17 @@ public static void updateCachedResultsForFiles(
184184
.filter(file -> !mapFile2Suggestions.containsKey(file))
185185
.collect(Collectors.toSet());
186186
if (!filesToProceed.isEmpty()) {
187-
info("Files to proceed (not found in cache): " + filesToProceed.size());
188-
// fixme debug only
189187
// deepcode ignore checkIsPresent~Optional: collection already checked to be not empty
190188
final PsiFile firstFile = filesToProceed.stream().findFirst().get();
191-
info("Hash for first file " + firstFile.getName() + " [" + getHash(firstFile) + "]");
189+
info(
190+
"Files to proceed (not found in cache): "
191+
+ filesToProceed.size()
192+
// fixme debug only
193+
+ "\nHash for first file "
194+
+ firstFile.getName()
195+
+ " ["
196+
+ getHash(firstFile)
197+
+ "]");
192198

193199
mapFile2Suggestions.putAll(retrieveSuggestions(project, filesToProceed, filesToRemove));
194200

@@ -251,10 +257,6 @@ private static Map<PsiFile, List<SuggestionForFile>> retrieveSuggestions(
251257
info(PREPARE_FILES_TEXT);
252258
ProgressManager.checkCanceled();
253259
mapPsiFile2Content.clear();
254-
List<String> removedFiles =
255-
filesToRemove.stream()
256-
.map(DeepCodeUtils::getDeepCodedFilePath)
257-
.collect(Collectors.toList());
258260
Map<String, String> mapPath2Hash = new HashMap<>();
259261
long sizePath2Hash = 0;
260262
int fileCounter = 0;
@@ -280,7 +282,7 @@ private static Map<PsiFile, List<SuggestionForFile>> retrieveSuggestions(
280282
}
281283
}
282284
// todo break removeFiles in chunks less then MAX_BANDLE_SIZE
283-
CreateBundleResponse createBundleResponse = makeNewBundle(project, mapPath2Hash, removedFiles);
285+
CreateBundleResponse createBundleResponse = makeNewBundle(project, mapPath2Hash, filesToRemove);
284286
if (isNotSucceed(project, createBundleResponse, "Bad Create/Extend Bundle request: "))
285287
return EMPTY_MAP;
286288
info(
@@ -362,9 +364,21 @@ private static Map<PsiFile, List<SuggestionForFile>> retrieveSuggestions(
362364
private static CreateBundleResponse makeNewBundle(
363365
@NotNull Project project,
364366
@NotNull Map<String, String> mapPath2Hash,
365-
@NotNull List<String> removedFiles) {
367+
@NotNull Collection<PsiFile> filesToRemove) {
366368
final FileHashRequest fileHashRequest = new FileHashRequest(mapPath2Hash);
367369
final String parentBundleId = mapProject2BundleId.getOrDefault(project, "");
370+
if (!parentBundleId.isEmpty()
371+
&& !filesToRemove.isEmpty()
372+
&& mapPath2Hash.isEmpty()
373+
&& filesToRemove.containsAll(cachedFilesOfProject(project))) {
374+
warn(
375+
"Attempt to Extending a bundle by removing all the parent bundle's files: "
376+
+ filesToRemove);
377+
}
378+
List<String> removedFiles =
379+
filesToRemove.stream()
380+
.map(DeepCodeUtils::getDeepCodedFilePath)
381+
.collect(Collectors.toList());
368382
String message =
369383
(parentBundleId.isEmpty()
370384
? "Creating new Bundle with "
@@ -385,7 +399,15 @@ private static CreateBundleResponse makeNewBundle(
385399
parentBundleId,
386400
new ExtendBundleRequest(fileHashRequest.getFiles(), removedFiles));
387401
}
388-
mapProject2BundleId.put(project, bundleResponse.getBundleId());
402+
String newBundleId = bundleResponse.getBundleId();
403+
// By man: "Extending a bundle by removing all the parent bundle's files is not allowed."
404+
// In reality new bundle returned with next bundleID:
405+
// gh/ArtsiomCh/DEEPCODE_PRIVATE_BUNDLE/0000000000000000000000000000000000000000000000000000000000000000
406+
if (newBundleId.equals(
407+
"gh/ArtsiomCh/DEEPCODE_PRIVATE_BUNDLE/0000000000000000000000000000000000000000000000000000000000000000")) {
408+
newBundleId = "";
409+
}
410+
mapProject2BundleId.put(project, newBundleId);
389411
return bundleResponse;
390412
}
391413

@@ -513,8 +535,8 @@ private static GetAnalysisResponse doRetrieveSuggestions(
513535

514536
@NotNull
515537
private static Map<PsiFile, List<SuggestionForFile>> parseGetAnalysisResponse(
516-
@NotNull Project project,
517-
@NotNull Collection<PsiFile> psiFiles,
538+
@NotNull Project project,
539+
@NotNull Collection<PsiFile> psiFiles,
518540
GetAnalysisResponse response,
519541
@NotNull ProgressIndicator progressIndicator) {
520542
Map<PsiFile, List<SuggestionForFile>> result = new HashMap<>();

0 commit comments

Comments
 (0)