Skip to content

Commit

Permalink
Ensure JGitIgnore closes scala.io.Source-s
Browse files Browse the repository at this point in the history
  • Loading branch information
er1c committed May 21, 2020
1 parent 2fdcbe7 commit 9abea8d
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions library/src/main/scala/giter8/JGitIgnore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,39 @@ case class JGitIgnore(patterns: String*) {
}

object JGitIgnore {
private implicit class RichLine(val line: String) extends AnyVal {
def isCommentLine: Boolean = (line.nonEmpty && line(0) == '#') || line.trim.isEmpty
}

def apply(in: InputStream): JGitIgnore = {
val patterns = Source.fromInputStream(in).getLines().toIndexedSeq
val source = Source.fromInputStream(in)
val patterns = source.getLines().filterNot(_.isCommentLine).toSeq
// close here will cause scripted tests to fail
//source.close()

new JGitIgnore(patterns: _*)
}

def apply(file: File): JGitIgnore = {
val patterns = Source.fromFile(file).getLines.filterNot(_.startsWith("#")).filterNot(_.trim.isEmpty).toIndexedSeq
val source = Source.fromFile(file)
val patterns = source.getLines.filterNot(_.isCommentLine).toSeq
source.close()
JGitIgnore(patterns: _*)
}

def fromFiles(files: File*): JGitIgnore = {
val patterns: IndexedSeq[String] = files.foldLeft[IndexedSeq[String]](IndexedSeq.empty) {
case (m, file) =>
m ++ Source.fromFile(file).getLines().filterNot(_.startsWith("#")).filterNot(_.trim.isEmpty).toIndexedSeq
val patterns: IndexedSeq[String] = {
val builder = Vector.newBuilder[String]

files.foreach { file: File =>
val source = Source.fromFile(file)
builder ++= source.getLines.filterNot(_.isCommentLine)
source.close()
}

builder.result
}

JGitIgnore(patterns: _*)
}
}

0 comments on commit 9abea8d

Please sign in to comment.