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.<init>(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]   ...
```
This commit is contained in:
Dmitry Bushev 2022-11-17 12:07:51 +03:00 committed by GitHub
parent c868ed5efe
commit d54faab903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 78 additions and 8 deletions

View File

@ -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 =

View File

@ -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 = ()
}
}

View File

@ -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()
}
}

View File

@ -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] =

View File

@ -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))

View File

@ -170,7 +170,7 @@ class BaseServerTest
val vcsManager = system.actorOf(
VcsManager.props(
config.vcsManager,
Git(),
Git.withEmptyUserConfig(),
contentRootManagerWrapper,
zioExec
),