git: use Hash instead of plain String

* produce meaningful error message if stdout was polluted with a
  record from stderr
This commit is contained in:
Aleksey Pivovarov
2018-01-31 19:49:13 +03:00
parent 0dce4d44e7
commit ad7f857d43
@@ -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();
}