Skip to content

Commit

Permalink
refactor method to overload
Browse files Browse the repository at this point in the history
  • Loading branch information
AjayvirS committed Jan 14, 2025
1 parent 2ae98eb commit e96a9d2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public PyrisProgrammingExerciseDTO toPyrisProgrammingExerciseDTO(ProgrammingExer
catch (GitAPIException e) {
log.error("Could not fetch existing test repository", e);
}
var testsRepositoryContents = testRepo.map((Repository repository) -> repositoryService.getFilesContentFromWorkingCopy(repository, false)).orElse(Map.of());
var testsRepositoryContents = testRepo.map((Repository repository) -> repositoryService.getFilesContentFromWorkingCopy(repository)).orElse(Map.of());

return new PyrisProgrammingExerciseDTO(exercise.getId(), exercise.getTitle(), exercise.getProgrammingLanguage(), templateRepositoryContents, solutionRepositoryContents,
testsRepositoryContents, exercise.getProblemStatement(), toInstant(exercise.getReleaseDate()), toInstant(exercise.getDueDate()));
Expand Down Expand Up @@ -157,7 +157,7 @@ private Map<String, String> getRepositoryContents(ProgrammingExerciseParticipati
}).orElse(Map.of());
}
else {
return Optional.ofNullable(gitService.getOrCheckoutRepository(repositoryUri, true)).map(repo -> repositoryService.getFilesContentFromWorkingCopy(repo, false))
return Optional.ofNullable(gitService.getOrCheckoutRepository(repositoryUri, true)).map(repo -> repositoryService.getFilesContentFromWorkingCopy(repo))
.orElse(Map.of());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -140,7 +141,7 @@ public Map<String, String> getFilesContentAtCommit(ProgrammingExercise programmi
repository = gitService.checkoutRepositoryAtCommit(participation.getVcsRepositoryUri(), commitId, true);
}
// Get the files content from the working copy of the repository
Map<String, String> filesWithContent = getFilesContentFromWorkingCopy(repository, false);
Map<String, String> filesWithContent = getFilesContentFromWorkingCopy(repository);
// Switch back to the default branch head
gitService.switchBackToDefaultBranchHead(repository);
return filesWithContent;
Expand All @@ -159,6 +160,10 @@ public Map<String, String> getFilesContentAtCommit(ProgrammingExercise programmi
*/
public Map<String, String> getFilesContentFromWorkingCopy(Repository repository, boolean omitBinaries) {
var files = gitService.listFilesAndFolders(repository, omitBinaries).entrySet().stream().filter(entry -> entry.getValue() == FileType.FILE).map(Map.Entry::getKey).toList();
return getFileListWithContent(files);
}

private Map<String, String> getFileListWithContent(List<File> files) {
Map<String, String> fileListWithContent = new HashMap<>();

files.forEach(file -> {
Expand All @@ -172,6 +177,11 @@ public Map<String, String> getFilesContentFromWorkingCopy(Repository repository,
return fileListWithContent;
}

public Map<String, String> getFilesContentFromWorkingCopy(Repository repository) {
var files = gitService.listFilesAndFolders(repository, false).entrySet().stream().filter(entry -> entry.getValue() == FileType.FILE).map(Map.Entry::getKey).toList();
return getFileListWithContent(files);
}

/**
* Retrieves a mapping of file paths to their content for a specific commit in a bare Git repository.
* This method extracts file content by traversing the repository's tree from the specified commit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private Map<String, Integer> getLineCountByFilePath(ProgrammingSubmission submis
var solutionRepo = gitService.getOrCheckoutRepository(solutionParticipation.getVcsRepositoryUri(), true);
gitService.resetToOriginHead(solutionRepo);
gitService.pullIgnoreConflicts(solutionRepo);
var solutionFiles = repositoryService.getFilesContentFromWorkingCopy(solutionRepo, false);
var solutionFiles = repositoryService.getFilesContentFromWorkingCopy(solutionRepo);
var result = new HashMap<String, Integer>();
solutionFiles.forEach((filePath, value) -> {
// do not count lines for non-java/kotlin files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private Map<String, String> readSolutionRepo(ProgrammingExercise programmingExer
gitService.resetToOriginHead(solutionRepo);
gitService.pullIgnoreConflicts(solutionRepo);

return repositoryService.getFilesContentFromWorkingCopy(solutionRepo, false);
return repositoryService.getFilesContentFromWorkingCopy(solutionRepo);
}
catch (GitAPIException e) {
throw new BehavioralSolutionEntryGenerationException("Error while reading solution repository", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ public ModelAndView redirectGetSolutionRepositoryFiles(@PathVariable Long exerci
var participation = solutionProgrammingExerciseParticipationRepository.findByProgrammingExerciseIdElseThrow(exerciseId);

// TODO: We want to get rid of ModelAndView and use ResponseEntity instead. Define an appropriate service method and then call it here and in the referenced endpoint.
return new ModelAndView("forward:/api/repository/" + participation.getId() + "/files-content" + "?omitBinaries=" + omitBinaries);
return new ModelAndView("forward:/api/repository/" + participation.getId() + "/files-content" + (omitBinaries ? "?omitBinaries=" + omitBinaries : ""));
}

/**
Expand Down

0 comments on commit e96a9d2

Please sign in to comment.