Created
November 13, 2019 10:44
-
-
Save rock3r/0aaa9c63264befcaefd4adeab594e64e to your computer and use it in GitHub Desktop.
Extension property for Gradle's Project interface to figure out the Git root, if any
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.sebastiano.utils.gradle | |
import org.gradle.api.* | |
import java.io.* | |
// 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()) { | |
this | |
} else parentFile?.findGitRoot() | |
return rootDir.findGitRoot() | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.