Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent failure on sbt init when JGit finds a .git with no commit #141

Merged
merged 1 commit into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 28 additions & 0 deletions src/test/scala/scalafix/internal/sbt/JGitCompletionsSuite.scala
Original file line number Diff line number Diff line change
@@ -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)
}

}