From ad7f857d43d8fef73f2ec406c2212ad86a9574d5 Mon Sep 17 00:00:00 2001 From: Aleksey Pivovarov Date: Wed, 31 Jan 2018 18:37:54 +0300 Subject: [PATCH] git: use Hash instead of plain String * produce meaningful error message if stdout was polluted with a record from stderr --- .../src/git4idea/index/GitIndexUtil.java | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/plugins/git4idea/src/git4idea/index/GitIndexUtil.java b/plugins/git4idea/src/git4idea/index/GitIndexUtil.java index bd37418c8ebc..6e6c2ab7c515 100644 --- a/plugins/git4idea/src/git4idea/index/GitIndexUtil.java +++ b/plugins/git4idea/src/git4idea/index/GitIndexUtil.java @@ -23,6 +23,8 @@ import com.intellij.openapi.vcs.FilePath; import com.intellij.openapi.vcs.VcsException; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.containers.ContainerUtil; +import com.intellij.vcs.log.Hash; +import com.intellij.vcs.log.impl.HashImpl; import com.intellij.vcsUtil.VcsFileUtil; import com.intellij.vcsUtil.VcsUtil; import git4idea.GitUtil; @@ -95,27 +97,27 @@ public class GitIndexUtil { } @NotNull - public static String write(@NotNull GitRepository repository, - @NotNull FilePath filePath, - @NotNull byte[] bytes, - boolean executable) throws VcsException { + public static Hash write(@NotNull GitRepository repository, + @NotNull FilePath filePath, + @NotNull byte[] bytes, + boolean executable) throws VcsException { return write(repository, filePath, new ByteArrayInputStream(bytes), executable); } @NotNull - public static String write(@NotNull GitRepository repository, - @NotNull FilePath filePath, - @NotNull InputStream content, - boolean executable) throws VcsException { - String hash = hashObject(repository, filePath, content); + public static Hash write(@NotNull GitRepository repository, + @NotNull FilePath filePath, + @NotNull InputStream content, + boolean executable) throws VcsException { + Hash hash = hashObject(repository, filePath, content); updateIndex(repository, filePath, hash, executable); return hash; } @NotNull - private static String hashObject(@NotNull GitRepository repository, - @NotNull FilePath filePath, - @NotNull InputStream content) throws VcsException { + private static Hash hashObject(@NotNull GitRepository repository, + @NotNull FilePath filePath, + @NotNull InputStream content) throws VcsException { GitLineHandler h = new GitLineHandler(repository.getProject(), repository.getRoot(), GitCommand.HASH_OBJECT); h.setSilent(true); h.addParameters("-w", "--stdin"); @@ -135,19 +137,19 @@ public class GitIndexUtil { notNull(GitVcs.getInstance(repository.getProject())).showErrors(h.errors(), "Applying index modifications"); throw h.errors().get(0); } - return output.trim(); + return HashImpl.build(output.trim()); } private static void updateIndex(@NotNull GitRepository repository, @NotNull FilePath filePath, - @NotNull String blobHash, + @NotNull Hash blobHash, boolean isExecutable) throws VcsException { String mode = isExecutable ? EXECUTABLE_MODE : DEFAULT_MODE; String path = VcsFileUtil.relativePath(repository.getRoot(), filePath); GitLineHandler h = new GitLineHandler(repository.getProject(), repository.getRoot(), GitCommand.UPDATE_INDEX); h.setSilent(true); - h.addParameters("--cacheinfo", mode + "," + blobHash + "," + path); + h.addParameters("--cacheinfo", mode + "," + blobHash.asString() + "," + path); h.endOptions(); Git.getInstance().runCommandWithoutCollectingOutput(h).getOutputOrThrow(); }