Skip to content

Commit bd75a8c

Browse files
authored
Merge pull request #2994 from daddykotex/dfrancoeur/2979-gitignored
Ignore file excluded from git
2 parents 5357f28 + 226383b commit bd75a8c

File tree

4 files changed

+58
-14
lines changed

4 files changed

+58
-14
lines changed

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ lazy val core = myCrossProject("core")
121121
Dependencies.decline,
122122
Dependencies.fs2Core,
123123
Dependencies.fs2Io,
124+
Dependencies.gitignore,
124125
Dependencies.http4sCirce,
125126
Dependencies.http4sClient,
126127
Dependencies.http4sCore,

modules/core/src/main/scala/org/scalasteward/core/edit/update/ScannerAlg.scala

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package org.scalasteward.core.edit.update
1818

1919
import better.files.File
2020
import cats.effect.Concurrent
21+
import cats.syntax.functor._
22+
import com.github.arturopala.gitignore.GitIgnore
2123
import fs2.Stream
2224
import org.scalasteward.core.data.{Dependency, Repo, Version}
2325
import org.scalasteward.core.edit.update.data.{ModulePosition, VersionPosition}
@@ -56,19 +58,35 @@ final class ScannerAlg[F[_]](implicit
5658
.compile
5759
.foldMonoid
5860

61+
private def getGitIgnore(repoDir: File): F[Option[GitIgnore]] = {
62+
val gitIgnore = repoDir / ".gitignore"
63+
fileAlg.readFile(gitIgnore).map(_.map(GitIgnore.parse))
64+
}
65+
5966
private def findPathsContaining(
6067
repo: Repo,
6168
config: RepoConfig,
6269
string: String
63-
): Stream[F, FileData] =
64-
Stream.eval(workspaceAlg.repoDir(repo)).flatMap { repoDir =>
65-
val fileFilter = (file: File) => {
66-
val path = repoDir.relativize(file).toString
67-
val cond = !path.startsWith(".git/") &&
68-
config.updates.fileExtensionsOrDefault.exists(path.endsWith)
69-
Option.when(cond)(path)
70-
}
71-
val contentFilter = (content: String) => Some(content).filter(_.contains(string))
72-
fileAlg.findFiles(repoDir, fileFilter, contentFilter).map(FileData.tupled)
70+
): Stream[F, FileData] = {
71+
def fileFilter(repoDir: File, maybeGitIgnore: Option[GitIgnore]) = (file: File) => {
72+
val path = repoDir.relativize(file).toString
73+
val notDotGit = !path.startsWith(".git/")
74+
val onlyKeepConfiguredExtensions =
75+
config.updates.fileExtensionsOrDefault.exists(path.endsWith)
76+
val allowedByGitIgnore = maybeGitIgnore.map(_.isAllowed(path)).getOrElse(true)
77+
val cond = notDotGit &&
78+
onlyKeepConfiguredExtensions &&
79+
allowedByGitIgnore
80+
Option.when(cond)(path)
7381
}
82+
val contentFilter = (content: String) => Some(content).filter(_.contains(string))
83+
84+
for {
85+
repoDir <- Stream.eval(workspaceAlg.repoDir(repo))
86+
maybeRootGitIgnore <- Stream.eval(getGitIgnore(repoDir))
87+
files <- fileAlg
88+
.findFiles(repoDir, fileFilter(repoDir, maybeRootGitIgnore), contentFilter)
89+
.map(FileData.tupled)
90+
} yield files
91+
}
7492
}

modules/core/src/test/scala/org/scalasteward/core/edit/EditAlgTest.scala

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,26 @@ class EditAlgTest extends FunSuite {
2727
val update = ("org.typelevel".g % "cats-core".a % "1.2.0" %> "1.3.0").single
2828
val file1 = repoDir / "build.sbt"
2929
val file2 = repoDir / "project/Dependencies.scala"
30+
val gitignore = repoDir / ".gitignore"
3031

3132
val state = MockState.empty
32-
.addFiles(file1 -> """val catsVersion = "1.2.0"""", file2 -> "")
33+
.addFiles(file1 -> """val catsVersion = "1.2.0"""", file2 -> "", gitignore -> "")
3334
.flatMap(editAlg.applyUpdate(data, update).runS)
3435
.unsafeRunSync()
3536

3637
val expected = MockState.empty.copy(
3738
trace = Vector(
39+
Cmd("read", gitignore.pathAsString),
3840
Cmd("test", "-f", repoDir.pathAsString),
41+
Cmd("test", "-f", gitignore.pathAsString),
3942
Cmd("test", "-f", file1.pathAsString),
4043
Cmd("read", file1.pathAsString),
4144
Cmd("test", "-f", (repoDir / "project").pathAsString),
4245
Cmd("test", "-f", file2.pathAsString),
4346
Cmd("read", file2.pathAsString),
47+
Cmd("read", gitignore.pathAsString),
4448
Cmd("test", "-f", repoDir.pathAsString),
49+
Cmd("test", "-f", gitignore.pathAsString),
4550
Cmd("test", "-f", file1.pathAsString),
4651
Cmd("read", file1.pathAsString),
4752
Cmd("test", "-f", (repoDir / "project").pathAsString),
@@ -51,7 +56,7 @@ class EditAlgTest extends FunSuite {
5156
Cmd("write", file1.pathAsString),
5257
Cmd(gitStatus(repoDir))
5358
),
54-
files = Map(file1 -> """val catsVersion = "1.3.0"""", file2 -> "")
59+
files = Map(file1 -> """val catsVersion = "1.3.0"""", file2 -> "", gitignore -> "")
5560
)
5661

5762
assertEquals(state, expected)
@@ -65,30 +70,47 @@ class EditAlgTest extends FunSuite {
6570
val data = RepoData(repo, cache, RepoConfig.empty)
6671
val repoDir = workspaceAlg.repoDir(repo).unsafeRunSync()
6772
val update = ("org.scalameta".g % "scalafmt-core".a % "2.0.0" %> "2.1.0").single
73+
val gitignore = repoDir / ".gitignore"
6874
val scalafmtConf = repoDir / scalafmtConfName
6975
val scalafmtConfContent = """maxColumn = 100
7076
|version = 2.0.0
7177
|align.openParenCallSite = false
7278
|""".stripMargin
7379
val buildSbt = repoDir / "build.sbt"
80+
val target = repoDir / "target"
81+
// this file should not be read because it's under target which is git ignored
82+
val targetScalaFile = target / "SomeFile.scala"
7483

7584
val state = MockState.empty
76-
.addFiles(scalafmtConf -> scalafmtConfContent, buildSbt -> "")
85+
.addFiles(
86+
scalafmtConf -> scalafmtConfContent,
87+
buildSbt -> "",
88+
gitignore -> "target/",
89+
targetScalaFile -> ""
90+
)
7791
.flatMap(editAlg.applyUpdate(data, update).runS)
7892
.unsafeRunSync()
7993

8094
val expected = MockState.empty.copy(
8195
trace = Vector(
96+
Cmd("read", gitignore.pathAsString),
8297
Cmd("test", "-f", repoDir.pathAsString),
98+
Cmd("test", "-f", gitignore.pathAsString),
8399
Cmd("test", "-f", scalafmtConf.pathAsString),
84100
Cmd("read", scalafmtConf.pathAsString),
85101
Cmd("test", "-f", buildSbt.pathAsString),
86102
Cmd("read", buildSbt.pathAsString),
103+
Cmd("test", "-f", target.pathAsString),
104+
Cmd("test", "-f", targetScalaFile.pathAsString),
105+
Cmd("read", gitignore.pathAsString),
87106
Cmd("test", "-f", repoDir.pathAsString),
107+
Cmd("test", "-f", gitignore.pathAsString),
88108
Cmd("test", "-f", scalafmtConf.pathAsString),
89109
Cmd("read", scalafmtConf.pathAsString),
90110
Cmd("test", "-f", buildSbt.pathAsString),
91111
Cmd("read", buildSbt.pathAsString),
112+
Cmd("test", "-f", target.pathAsString),
113+
Cmd("test", "-f", targetScalaFile.pathAsString),
92114
Cmd("read", scalafmtConf.pathAsString),
93115
Cmd("write", scalafmtConf.pathAsString),
94116
Cmd(
@@ -109,7 +131,9 @@ class EditAlgTest extends FunSuite {
109131
|version = 2.1.0
110132
|align.openParenCallSite = false
111133
|""".stripMargin,
112-
buildSbt -> ""
134+
buildSbt -> "",
135+
gitignore -> "target/",
136+
targetScalaFile -> ""
113137
)
114138
)
115139

project/Dependencies.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ object Dependencies {
2020
val disciplineMunit = "org.typelevel" %% "discipline-munit" % "1.0.9"
2121
val fs2Core = "co.fs2" %% "fs2-core" % "3.6.1"
2222
val fs2Io = "co.fs2" %% "fs2-io" % fs2Core.revision
23+
val gitignore = "com.github.arturopala" %% "gitignore" % "0.4.0"
2324
val http4sCore = "org.http4s" %% "http4s-core" % "1.0.0-M39"
2425
val http4sCirce = "org.http4s" %% "http4s-circe" % http4sCore.revision
2526
val http4sClient = "org.http4s" %% "http4s-client" % http4sCore.revision

0 commit comments

Comments
 (0)