From ff53c45a0e58f1291af5f0bdb2584669d26409b6 Mon Sep 17 00:00:00 2001 From: Brice Jaglin Date: Mon, 6 Jul 2020 13:40:39 +0200 Subject: [PATCH] prevent failure on sbt init when JGit finds a .git with no commit 2ca68e4 exposed failure to lookup commits during sbt initialization, while it would only happen on `scalafix` invocation earlier. --- .../internal/sbt/JGitCompletions.scala | 4 ++- .../internal/sbt/JGitCompletionsSuite.scala | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/test/scala/scalafix/internal/sbt/JGitCompletionsSuite.scala diff --git a/src/main/scala/scalafix/internal/sbt/JGitCompletions.scala b/src/main/scala/scalafix/internal/sbt/JGitCompletions.scala index aebc90af..f473a068 100644 --- a/src/main/scala/scalafix/internal/sbt/JGitCompletions.scala +++ b/src/main/scala/scalafix/internal/sbt/JGitCompletions.scala @@ -11,6 +11,7 @@ import org.eclipse.jgit.util.GitDateFormatter import java.nio.file.Path import scala.collection.JavaConverters._ +import scala.util.Try class JGitCompletion(cwd: Path) { private val isGitRepository = @@ -24,7 +25,8 @@ class JGitCompletion(cwd: Path) { val refList0 = repo.getRefDatabase().getRefsByPrefix(RefDatabase.ALL).asScala val git = new Git(repo) - val refs0 = git.log().setMaxCount(20).call().asScala.toList + val refs0 = + Try(git.log().setMaxCount(20).call().asScala.toList).getOrElse(Nil) (refList0, refs0) } else { (Nil, Nil) diff --git a/src/test/scala/scalafix/internal/sbt/JGitCompletionsSuite.scala b/src/test/scala/scalafix/internal/sbt/JGitCompletionsSuite.scala new file mode 100644 index 00000000..54cfe36d --- /dev/null +++ b/src/test/scala/scalafix/internal/sbt/JGitCompletionsSuite.scala @@ -0,0 +1,28 @@ +package scalafix.internal.sbt + +import org.eclipse.jgit.api.{Git => JGit} +import org.scalatest.funsuite.AnyFunSuite + +class JGitCompletionsSuite extends AnyFunSuite { + + test("directory with no .git") { + val fs = new Fs() + val jgit = new JGitCompletion(fs.workingDirectory) + assert(jgit.last20Commits == Nil) + } + + test("directory with empty .git") { + val fs = new Fs() + fs.mkdir(".git") + val jgit = new JGitCompletion(fs.workingDirectory) + assert(jgit.last20Commits == Nil) + } + + test("directory with no commits") { + val fs = new Fs() + JGit.init().setDirectory(fs.workingDirectory.toFile).call() // git init + val jgit = new JGitCompletion(fs.workingDirectory) + assert(jgit.last20Commits == Nil) + } + +}