Skip to content

Commit 502d5e5

Browse files
committed
Add checkIgnore to GitAlg
1 parent 145d46b commit 502d5e5

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,13 @@ final class HookExecutor[F[_]](implicit
9595
oldContent <- fileAlg.readFile(file)
9696
newContent = oldContent.fold(newLines)(_ + "\n" + newLines)
9797
_ <- fileAlg.writeFile(file, newContent)
98-
_ <- gitAlg.add(repo, file.pathAsString)
98+
pathAsString = file.pathAsString
99+
_ <- gitAlg
100+
.checkIgnore(repo, pathAsString)
101+
.ifM(
102+
logger.warn(s"Impossible to add '$pathAsString' because it is git ignored."),
103+
gitAlg.add(repo, pathAsString)
104+
)
99105
blameIgnoreCommitMsg = CommitMsg(s"Add '${commitMsg.title}' to $gitBlameIgnoreRevsName")
100106
maybeBlameIgnoreCommit <- gitAlg.commitAllIfDirty(repo, blameIgnoreCommitMsg)
101107
} yield maybeBlameIgnoreCommit

modules/core/src/main/scala/org/scalasteward/core/git/FileGitAlg.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import cats.syntax.all._
2222
import org.http4s.Uri
2323
import org.scalasteward.core.application.Config.GitCfg
2424
import org.scalasteward.core.git.FileGitAlg.{dotdot, gitCmd}
25-
import org.scalasteward.core.io.process.SlurpOptions
25+
import org.scalasteward.core.io.process.{ProcessFailedException, SlurpOptions}
2626
import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
2727
import org.scalasteward.core.util.Nel
2828

@@ -47,6 +47,11 @@ final class FileGitAlg[F[_]](config: GitCfg)(implicit
4747
override def checkoutBranch(repo: File, branch: Branch): F[Unit] =
4848
git_("checkout", branch.name)(repo).void
4949

50+
override def checkIgnore(repo: File, file: String): F[Boolean] =
51+
git_("check-ignore", file)(repo)
52+
.as(true)
53+
.recover { case _: ProcessFailedException => false }
54+
5055
override def clone(repo: File, url: Uri): F[Unit] =
5156
for {
5257
rootDir <- workspaceAlg.rootDir

modules/core/src/main/scala/org/scalasteward/core/git/GenGitAlg.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ trait GenGitAlg[F[_], Repo] {
3434

3535
def checkoutBranch(repo: Repo, branch: Branch): F[Unit]
3636

37+
def checkIgnore(repo: Repo, file: String): F[Boolean]
38+
3739
def clone(repo: Repo, url: Uri): F[Unit]
3840

3941
def cloneExists(repo: Repo): F[Boolean]
@@ -105,6 +107,9 @@ trait GenGitAlg[F[_], Repo] {
105107
override def checkoutBranch(repo: A, branch: Branch): F[Unit] =
106108
f(repo).flatMap(self.checkoutBranch(_, branch))
107109

110+
override def checkIgnore(repo: A, file: String): F[Boolean] =
111+
f(repo).flatMap(self.checkIgnore(_, file))
112+
108113
override def clone(repo: A, url: Uri): F[Unit] =
109114
f(repo).flatMap(self.clone(_, url))
110115

modules/core/src/test/scala/org/scalasteward/core/git/FileGitAlgTest.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,32 @@ import org.scalasteward.core.util.Nel
1616
class FileGitAlgTest extends CatsEffectSuite {
1717
private val rootDir = mockRoot / "git-tests"
1818

19+
test("add with .gitignore") {
20+
val repo = rootDir / "branchgitignpreAuthors"
21+
for {
22+
_ <- ioAuxGitAlg.createRepo(repo)
23+
24+
gitIgnoreFile = repo / ".gitignore"
25+
_ <- ioFileAlg.writeFile(gitIgnoreFile, "ignored.txt")
26+
_ <- ioAuxGitAlg.addFiles(repo, gitIgnoreFile)
27+
28+
ignoredFile = repo / "ignored.txt"
29+
_ <- ioFileAlg.writeFile(ignoredFile, "irrelevant")
30+
ignoredFileCheck <- ioGitAlg.checkIgnore(repo, ignoredFile.pathAsString)
31+
32+
notIgnoredFile = repo / "not-ignored.txt"
33+
_ <- ioFileAlg.writeFile(notIgnoredFile, "irrelevant")
34+
notIgnoredFileCheck <- ioGitAlg.checkIgnore(repo, notIgnoredFile.pathAsString)
35+
36+
} yield {
37+
assert(ignoredFileCheck, "The file is in .gitignore, checkIgnore should return true.")
38+
assert(
39+
!notIgnoredFileCheck,
40+
"The file is not in .gitignore, checkIgnore should return false."
41+
)
42+
}
43+
}
44+
1945
test("branchAuthors") {
2046
val repo = rootDir / "branchAuthors"
2147
for {

0 commit comments

Comments
 (0)