Search in sources :

Example 26 with GitCommandResult

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);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GitLineHandler(git4idea.commands.GitLineHandler) GitCommandResult(git4idea.commands.GitCommandResult)

Aggregations

GitCommandResult (git4idea.commands.GitCommandResult)26 GitRepository (git4idea.repo.GitRepository)14 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 AccessToken (com.intellij.openapi.application.AccessToken)5 VcsException (com.intellij.openapi.vcs.VcsException)4 GitLocalChangesWouldBeOverwrittenDetector (git4idea.commands.GitLocalChangesWouldBeOverwrittenDetector)4 GitUntrackedFilesOverwrittenByOperationDetector (git4idea.commands.GitUntrackedFilesOverwrittenByOperationDetector)4 NotNull (org.jetbrains.annotations.NotNull)4 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Git (git4idea.commands.Git)3 GitLineHandler (git4idea.commands.GitLineHandler)3 GitLineHandlerListener (git4idea.commands.GitLineHandlerListener)3 GitSimpleEventDetector (git4idea.commands.GitSimpleEventDetector)3 File (java.io.File)3 Task (com.intellij.openapi.progress.Task)2 MultiMap (com.intellij.util.containers.MultiMap)2 UIThreadUnsafe (com.virtuslab.qual.guieffect.UIThreadUnsafe)2 GitLocalBranch (git4idea.GitLocalBranch)2 GitCompoundResult (git4idea.commands.GitCompoundResult)2 Map (java.util.Map)2