Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Extension property for Gradle's Project interface to figure out the Git root, if any
package dev.sebastiano.utils.gradle
import org.gradle.api.*
// Note: you need to have a dependency on `gradleApi()` in your buildSrc project
val Project.gitRoot: File?
get() {
fun File.isGitRoot() = isDirectory && File(this, ".git").isDirectory
fun File.findGitRoot(): File? = if (isGitRoot()) {
} else parentFile?.findGitRoot()
return rootDir.findGitRoot()

This comment has been minimized.

Copy link
Owner Author

@rock3r rock3r commented Nov 13, 2019

Note that this snippet will traverse the filesystem up to the volume root, and report the first Git root it finds. It has no way to verify it's the correct one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment