From d54faab90358f99b0764ed5ae884361d01ea2f03 Mon Sep 17 00:00:00 2001 From: Dmitry Bushev Date: Thu, 17 Nov 2022 12:07:51 +0300 Subject: [PATCH] Git should ignore user config (#3883) Ignore gitconfig located in the user's home directory when creating Git instance. For example, I setup git to automatically sign commits. And all git related tests are failing because of that: ``` [info] - should return last X commits *** FAILED *** [info] org.eclipse.jgit.api.errors.ServiceUnavailableException: Signing service is not available [info] at org.eclipse.jgit.api.CommitCommand.sign(CommitCommand.java:328) [info] at org.eclipse.jgit.api.CommitCommand.call(CommitCommand.java:283) [info] at org.enso.languageserver.vcsmanager.GitSpec$InitialRepoSetup.setup(GitSpec.scala:360) [info] at org.enso.languageserver.vcsmanager.GitSpec$InitialRepoSetup.setup$(GitSpec.scala:348) [info] at org.enso.languageserver.vcsmanager.GitSpec$$anon$16.setup(GitSpec.scala:275) [info] at org.enso.languageserver.vcsmanager.GitSpec$InitialRepoSetup.$init$(GitSpec.scala:346) [info] at org.enso.languageserver.vcsmanager.GitSpec$$anon$16.(GitSpec.scala:275) [info] at org.enso.languageserver.vcsmanager.GitSpec.$anonfun$new$35(GitSpec.scala:275) [info] at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85) [info] at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83) [info] ... ``` --- .../enso/languageserver/boot/MainModule.scala | 2 +- .../vcsmanager/EmptyUserConfigReader.scala | 65 +++++++++++++++++++ .../enso/languageserver/vcsmanager/Git.scala | 13 ++-- .../vcsmanager/VcsManager.scala | 2 +- .../languageserver/vcsmanager/GitSpec.scala | 2 +- .../websocket/json/BaseServerTest.scala | 2 +- 6 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/EmptyUserConfigReader.scala diff --git a/engine/language-server/src/main/scala/org/enso/languageserver/boot/MainModule.scala b/engine/language-server/src/main/scala/org/enso/languageserver/boot/MainModule.scala index 206150c7069..640026968e3 100644 --- a/engine/language-server/src/main/scala/org/enso/languageserver/boot/MainModule.scala +++ b/engine/language-server/src/main/scala/org/enso/languageserver/boot/MainModule.scala @@ -105,7 +105,7 @@ class MainModule(serverConfig: LanguageServerConfig, logLevel: LogLevel) { val fileSystem: FileSystem = new FileSystem log.trace("Created file system [{}].", fileSystem) - val git = Git() + val git = Git.withEmptyUserConfig() log.trace("Created git [{}].", git) implicit val versionCalculator: ContentBasedVersioning = diff --git a/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/EmptyUserConfigReader.scala b/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/EmptyUserConfigReader.scala new file mode 100644 index 00000000000..c7916f49b39 --- /dev/null +++ b/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/EmptyUserConfigReader.scala @@ -0,0 +1,65 @@ +package org.enso.languageserver.vcsmanager + +import org.apache.commons.io.FileUtils +import org.eclipse.jgit.lib.Config +import org.eclipse.jgit.storage.file.FileBasedConfig +import org.eclipse.jgit.util.{FS, SystemReader} + +import java.io.File +import java.nio.file.Files + +/** Config reader that ignores gitconfig file in user's home directory. + */ +final class EmptyUserConfigReader extends SystemReader { + + import EmptyUserConfigReader._ + + val proxy: SystemReader = SystemReader.getInstance() + + /** @inheritdoc */ + override def getHostname: String = + proxy.getHostname + + /** @inheritdoc */ + override def getenv(variable: String): String = + proxy.getenv(variable) + + /** @inheritdoc */ + override def getProperty(key: String): String = + proxy.getProperty(key) + + /** @inheritdoc */ + override def openUserConfig(parent: Config, fs: FS): FileBasedConfig = + new EmptyConfig(parent, fs) + + /** @inheritdoc */ + override def openSystemConfig(parent: Config, fs: FS): FileBasedConfig = + proxy.openSystemConfig(parent, fs) + + /** @inheritdoc */ + override def openJGitConfig(parent: Config, fs: FS): FileBasedConfig = + proxy.openJGitConfig(parent, fs) + + /** @inheritdoc */ + override def getCurrentTime: Long = + proxy.getCurrentTime + + /** @inheritdoc */ + override def getTimezone(when: Long): Int = + proxy.getTimezone(when) +} + +object EmptyUserConfigReader { + + private val gitconfig: File = + Files.createTempFile("gitconfig", null).toFile + + sys.addShutdownHook(FileUtils.deleteQuietly(gitconfig)) + + final private class EmptyConfig(parent: Config, fs: FS) + extends FileBasedConfig(parent, gitconfig, fs) { + override def load(): Unit = () + override def save(): Unit = () + override def clear(): Unit = () + } +} diff --git a/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/Git.scala b/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/Git.scala index d8461922381..cd1dc8410da 100644 --- a/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/Git.scala +++ b/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/Git.scala @@ -15,6 +15,7 @@ import org.eclipse.jgit.errors.{ import org.eclipse.jgit.lib.{ObjectId, Repository} import org.eclipse.jgit.storage.file.FileRepositoryBuilder import org.eclipse.jgit.revwalk.{RevCommit, RevWalk} +import org.eclipse.jgit.util.SystemReader import org.enso.languageserver.vcsmanager.Git.{ AuthorEmail, AuthorName, @@ -31,7 +32,7 @@ import java.time.Instant private class Git extends VcsApi[BlockingIO] { private def repository(path: Path): Repository = { - val builder = new FileRepositoryBuilder(); + val builder = new FileRepositoryBuilder() builder .setGitDir(path.resolve(Git.DefaultGitRepoDir).toFile) .setMustExist(true) @@ -171,7 +172,7 @@ private class Git extends VcsApi[BlockingIO] { val logCmd = jgit.log() limit .filter(_ > 0) - .map(logCmd.setMaxCount(_)) + .map(logCmd.setMaxCount) .getOrElse(logCmd) .call() .asScala @@ -197,7 +198,11 @@ object Git { private class RepoExists extends Exception - /** Returns a Git implementation of VcsApi + /** Returns a Git implementation of VcsApi that ignores gitconfig file in + * user's home directory. */ - def apply(): VcsApi[BlockingIO] = new Git() + def withEmptyUserConfig(): VcsApi[BlockingIO] = { + SystemReader.setInstance(new EmptyUserConfigReader) + new Git() + } } diff --git a/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/VcsManager.scala b/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/VcsManager.scala index dbe8768f8ac..ef694faab19 100644 --- a/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/VcsManager.scala +++ b/engine/language-server/src/main/scala/org/enso/languageserver/vcsmanager/VcsManager.scala @@ -37,7 +37,7 @@ class VcsManager( .absolve private def toVcsError: FileSystemFailure => VcsFailure = { - case ex: FileSystemFailure => ProjectNotFound(ex.toString) + ex: FileSystemFailure => ProjectNotFound(ex.toString) } private def resolvePath(path: Path): IO[VcsFailure, File] = diff --git a/engine/language-server/src/test/scala/org/enso/languageserver/vcsmanager/GitSpec.scala b/engine/language-server/src/test/scala/org/enso/languageserver/vcsmanager/GitSpec.scala index 90ac44eebe1..8bef0dac78d 100644 --- a/engine/language-server/src/test/scala/org/enso/languageserver/vcsmanager/GitSpec.scala +++ b/engine/language-server/src/test/scala/org/enso/languageserver/vcsmanager/GitSpec.scala @@ -312,7 +312,7 @@ class GitSpec extends AnyWordSpecLike with Matchers with Effects { .build() } - val vcs = Git() + val vcs = Git.withEmptyUserConfig() def listCommits(repoDir: Path): List[RevCommit] = { listCommits(testRepo(repoDir)) diff --git a/engine/language-server/src/test/scala/org/enso/languageserver/websocket/json/BaseServerTest.scala b/engine/language-server/src/test/scala/org/enso/languageserver/websocket/json/BaseServerTest.scala index e23f053bbeb..6e81a9c483b 100644 --- a/engine/language-server/src/test/scala/org/enso/languageserver/websocket/json/BaseServerTest.scala +++ b/engine/language-server/src/test/scala/org/enso/languageserver/websocket/json/BaseServerTest.scala @@ -170,7 +170,7 @@ class BaseServerTest val vcsManager = system.actorOf( VcsManager.props( config.vcsManager, - Git(), + Git.withEmptyUserConfig(), contentRootManagerWrapper, zioExec ),