https://github.com/encalmo/gitignore
Scala implementation of the .gitignore compliant filter, ready to embed in different tools without having to run git command
https://github.com/encalmo/gitignore
git gitignore scala
Last synced: 2 months ago
JSON representation
Scala implementation of the .gitignore compliant filter, ready to embed in different tools without having to run git command
- Host: GitHub
- URL: https://github.com/encalmo/gitignore
- Owner: encalmo
- License: mit
- Created: 2025-03-25T10:41:06.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-25T11:31:50.000Z (over 1 year ago)
- Last Synced: 2025-03-25T12:22:33.336Z (over 1 year ago)
- Topics: git, gitignore, scala
- Language: Scala
- Homepage: https://encalmo.github.io/gitignore/
- Size: 26.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
  
# gitignore
The [.gitignore](https://git-scm.com/docs/gitignore) has became de-facto standard filter format for project's files and folders.
This nano-library provides Scala implementation of the `.gitignore` compliant filter, ready to embed in different tools without having to run `git` command.
## Table of contents
- [Dependencies](#dependencies)
- [Usage](#usage)
- [Examples](#examples)
## Dependencies
- [Scala](https://www.scala-lang.org) >= 3.3.5
## Usage
Use with SBT
libraryDependencies += "org.encalmo" %% "gitignore" % "0.9.3"
or with SCALA-CLI
//> using dep org.encalmo::gitignore:0.9.3
## Examples
Option **1** - parse existing `.gitignore` file content
```scala
val gitIgnore = GitIgnore.fromCurrentDirectory()
gitIgnore.isAllowed(".scala-build/") shouldBe false
gitIgnore.isAllowed("GitIgnore.scala") shouldBe true
```
```scala
val gitIgnore = GitIgnore.fromFile(new File(".gitignore"))
gitIgnore.isAllowed(".scala-build/") shouldBe false
gitIgnore.isAllowed("GitIgnore.scala") shouldBe true
```
```scala
import com.github.arturopala.gitignore._
val gitignore = GitIgnore
.parse("""
|#*.json
|*.txt
|*.pdf
|!ok.*
|bar.json
|target
|""".stripMargin)
// gitignore: GitIgnore = GitIgnore(
// gitPatterns = List("*.txt", "*.pdf", "!ok.*", "bar.json", "target")
// )
gitignore.isIgnored("foo.txt")
// res0: Boolean = true
gitignore.isIgnored("bar.txt")
// res1: Boolean = true
gitignore.isIgnored("ok.txt")
// res2: Boolean = false
gitignore.isIgnored("foo.pdf")
// res3: Boolean = true
gitignore.isIgnored("bar.pdf")
// res4: Boolean = true
gitignore.isIgnored("ok.pdf")
// res5: Boolean = false
gitignore.isIgnored("foo.json")
// res6: Boolean = false
gitignore.isIgnored("bar.json")
// res7: Boolean = true
gitignore.isIgnored("ok.json")
// res8: Boolean = false
gitignore.isIgnored("target/")
// res9: Boolean = true
gitignore.isIgnored("target.json")
// res10: Boolean = false
gitignore.isAllowed("foo.txt")
// res11: Boolean = false
gitignore.isAllowed("bar.txt")
// res12: Boolean = false
gitignore.isAllowed("ok.txt")
// res13: Boolean = true
gitignore.isAllowed("foo.pdf")
// res14: Boolean = false
gitignore.isAllowed("bar.pdf")
// res15: Boolean = false
gitignore.isAllowed("ok.pdf")
// res16: Boolean = true
gitignore.isAllowed("foo.json")
// res17: Boolean = true
gitignore.isAllowed("bar.json")
// res18: Boolean = false
gitignore.isAllowed("ok.json")
// res19: Boolean = true
gitignore.isAllowed("target/")
// res20: Boolean = false
gitignore.isAllowed("target.json")
// res21: Boolean = true
```
Option **2** - pass list of rules to the constructor
```scala
import com.github.arturopala.gitignore._
val gitignore = GitIgnore(Seq(
"*.txt",
"*.pdf",
"!ok.*",
"bar.json ",
"target"))
// gitignore: GitIgnore = GitIgnore(
// gitPatterns = List("*.txt", "*.pdf", "!ok.*", "bar.json ", "target")
// )
gitignore.isIgnored("foo.txt")
// res23: Boolean = true
gitignore.isIgnored("bar.txt")
// res24: Boolean = true
gitignore.isIgnored("ok.txt")
// res25: Boolean = false
gitignore.isIgnored("foo.pdf")
// res26: Boolean = true
gitignore.isIgnored("bar.pdf")
// res27: Boolean = true
gitignore.isIgnored("ok.pdf")
// res28: Boolean = false
gitignore.isIgnored("foo.json")
// res29: Boolean = false
gitignore.isIgnored("bar.json")
// res30: Boolean = false
gitignore.isIgnored("ok.json")
// res31: Boolean = false
gitignore.isIgnored("target/")
// res32: Boolean = true
gitignore.isIgnored("target.json")
// res33: Boolean = false
gitignore.isAllowed("foo.txt")
// res34: Boolean = false
gitignore.isAllowed("bar.txt")
// res35: Boolean = false
gitignore.isAllowed("ok.txt")
// res36: Boolean = true
gitignore.isAllowed("foo.pdf")
// res37: Boolean = false
gitignore.isAllowed("bar.pdf")
// res38: Boolean = false
gitignore.isAllowed("ok.pdf")
// res39: Boolean = true
gitignore.isAllowed("foo.json")
// res40: Boolean = true
gitignore.isAllowed("bar.json")
// res41: Boolean = true
gitignore.isAllowed("ok.json")
// res42: Boolean = true
gitignore.isAllowed("target/")
// res43: Boolean = false
gitignore.isAllowed("target.json")
// res44: Boolean = true
```
## Project content
```
├── .github
│ └── workflows
│ ├── pages.yaml
│ ├── release.yaml
│ └── test.yaml
│
├── .gitignore
├── .scalafmt.conf
├── AnyWordSpecCompat.test.scala
├── Debug.scala
├── GitIgnore.scala
├── GitIgnore.test.scala
├── Glob.scala
├── Glob.test.scala
├── GlobCheck.test.scala
├── LICENSE
├── project.scala
├── README.md
├── test.sh
├── Zoom.scala
└── Zoom.test.scala
```