GitIgnoreParser is an advanced Kotlin library designed to parse .gitignore files and determine file exclusions based
on gitignore specifications with efficiency and precision. This tool is particularly useful for developers looking to
programmatically apply .gitignore rules in their Kotlin-based projects.
- Kotlin-Friendly: Designed with Kotlin's modern language features in mind for a seamless integration.
- Efficient Parsing: Converts
.gitignoreglob patterns to regex for precise and fast matching. - Nested
.gitignoreSupport: Handles.gitignorefiles in subdirectories, ensuring comprehensive rule application. - Directory Specific Rules: Accurately applies rules specific to directories, adhering to
.gitignorestandards. - Optimized for Large Projects: Ideal for use in large-scale projects with extensive file structures, ensuring performance.
- Advanced Logging: Integrates sophisticated logging mechanisms for enhanced debugging and operational tracking.
Add the JitPack repository to your build.gradle.kts:
allprojects {
repositories {
...
maven(url = "https://jitpack.io")
}
}Add the dependency:
dependencies {
implementation ("dev.onelenyk:gitignore-parser:v0.1.6")
}The GitIgnoreParser library is designed to be straightforward and easy to integrate into your Kotlin projects. Below is a step-by-step guide on how to use it effectively:
-
Initialization: Start by creating an instance of the
GitIgnoreParser. You need to provide the path to your project's root directory and optionally, any custom rules as a list of strings.import dev.onelenyk.GitIgnoreParser val rootPath = Paths.get("/path/to/your/project").toAbsolutePath() val gitIgnoreParser = GitIgnoreParser(rootDirectory = rootPath)
-
Parsing .gitignore File: Invoke the
parseGitignoremethod by passing the directory path that contains the.gitignorefile. This will load and parse the rules defined in the file.gitIgnoreParser.parseGitignore(rootPath)
-
Checking File Exclusion: To determine if a specific file is excluded based on the
.gitignorerules, use thegetRulesForDirectorymethod to get the rules for the file's directory and then theexcludingPatternmethod to check for exclusion.val filePath = rootPath.resolve("src/main/Example.kt") val rules = gitIgnoreParser.getRulesForDirectory(filePath.parent) val isExcluded = rules?.excludingPattern(filePath.fileName.toString()) != null println("Is the file excluded: $isExcluded")
-
Processing Entire Directory: To process all files in a directory, you can utilize the
FileProcessorclass. It traverses the directory, checking each file against the.gitignorerules and compiling a list of included files.import dev.onelenyk.FileProcessor val fileProcessor = FileProcessor(rootDirectory = rootPath) fileProcessor.process()
-
Getting Processing Summary: After processing the files, you can get a summary of the operation, which includes details like the total number of files processed, the number of files included and excluded, and the patterns used.
fileProcessor.report()
This Kotlin implementation of the GitIgnoreParser offers a user-friendly and efficient approach to handling .gitignore rules in Kotlin-based projects.
This Kotlin adaptation of the library offers a modern and concise approach, enhancing the user experience in Kotlin-based projects.
Contributions are welcome! Please feel free to submit a pull request or open an issue for any improvements or bug fixes.
For more detailed information about how to use GitIgnoreParser as well as a comprehensive overview of its features and functionalities, please visit our documentation.
This documentation is generated automatically from the source code and is updated regularly to reflect the latest changes and features.
- onelenyk - Initial work and maintenance.
- ChatGPT by OpenAI - Assisted in development by providing code insights, optimization strategies, and documentation support.
This project is licensed under the MIT License - see the LICENSE file for details.