use of git4idea.commands.GitCommandResult in project jetbrains-plugin by codiga.
the class CodigaGitUtils method getPatchesForWorkingDirectoryForFile.
/**
* Get all the changes for a patch for the working directory. This is the equivalent
* of doing git diff <filename>
* @param psiFile - the file we are inspecting and wanting the changes against.
* @return the list of patch or nothing.
*/
public static List<TextFilePatch> getPatchesForWorkingDirectoryForFile(PsiFile psiFile) {
Optional<VirtualFile> repositoryRoot = CodigaGitUtils.getRepositoryRoot(psiFile);
Optional<String> filePath = CodigaGitUtils.getFilePathInRepository(psiFile);
if (!repositoryRoot.isPresent()) {
LOGGER.debug("[getPatchesForWorkingDirectoryForFile] cannot find the repository root");
return ImmutableList.of();
}
if (!filePath.isPresent()) {
LOGGER.debug("[getPatchesForWorkingDirectoryForFile] cannot find the file path in the repository");
return ImmutableList.of();
}
// create a git command to execute to get the diff
GitLineHandler gitLineHandler = new GitLineHandler(psiFile.getProject(), repositoryRoot.get(), GitCommand.DIFF);
gitLineHandler.addParameters(filePath.get());
// execute the command
GitCommandResult result = Git.getInstance().runCommand(gitLineHandler);
if (!result.success()) {
LOGGER.debug("[getPatchesForWorkingDirectoryForFile] git command failed");
return ImmutableList.of();
}
String patchContent = String.join("\n", result.getOutput());
LOGGER.debug(String.format("[getPatchesForWorkingDirectoryForFile] patch content %s", patchContent));
return getTextFilePatchesFromDiff(patchContent);
}
Aggregations