Skip to content

Commit

Permalink
prevent failure on sbt init when JGit finds a .git with no commit
Browse files Browse the repository at this point in the history
2ca68e4 exposed failure to lookup commits during sbt initialization,
while it would only happen on `scalafix` invocation earlier.
  • Loading branch information
github-brice-jaglin committed Jul 6, 2020
1 parent 0e65416 commit c3a7c50
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/scala/scalafix/internal/sbt/JGitCompletions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions src/test/scala/scalafix/internal/sbt/JGitCompletionsSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scalafix.internal.sbt

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()
new Git(fs.workingDirectory) // git init
val jgit = new JGitCompletion(fs.workingDirectory)
assert(jgit.last20Commits == Nil)
}

}

0 comments on commit c3a7c50

Please sign in to comment.