Skip to content

Instantly share code, notes, and snippets.

@rock3r
Created November 13, 2019 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rock3r/0aaa9c63264befcaefd4adeab594e64e to your computer and use it in GitHub Desktop.
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
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()
}
@rock3r
Copy link
Author

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