Skip to content

Commit 0224b07

Browse files
authored
Make gradle hook configuration work for a submodule (#7686)
1 parent c17afc2 commit 0224b07

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

build.gradle

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,29 @@ ext {
7272

7373
javadocMemberLevel = JavadocMemberLevel.PROTECTED
7474

75-
// The local git repository, typically in the .git directory, but not for worktrees.
75+
// Path to git hooks for this checkout (works for normal repos, worktrees, and submodules).
7676
// This value is always overwritten, but Gradle needs the variable to be initialized.
77-
localRepo = ".git"
77+
localHooksDir = ".git/hooks"
7878
}
7979

80-
tasks.register("setLocalRepo", Exec) {
81-
commandLine("git", "worktree", "list")
80+
tasks.register("setLocalHooksDir", Exec) {
81+
onlyIf { rootProject.file(".git").exists() }
82+
commandLine("git", "rev-parse", "--git-path", "hooks")
8283
standardOutput = new ByteArrayOutputStream()
8384

8485
doLast {
85-
String worktreeList = standardOutput.toString()
86-
localRepo = worktreeList.substring(0, worktreeList.indexOf(" ")) + "/.git"
86+
localHooksDir = standardOutput.toString().trim()
8787
}
8888
}
8989

9090
// No group so it does not show up in the output of `gradlew tasks`
9191
tasks.register("installGitHooks", Copy) {
92-
dependsOn("setLocalRepo")
93-
description = "Copies git hooks to .git directory"
92+
onlyIf { rootProject.file(".git").exists() }
93+
dependsOn("setLocalHooksDir")
94+
description = "Copies git hooks to the local git hooks directory"
9495
from(files("checker/bin-devel/git.post-merge", "checker/bin-devel/git.pre-commit"))
9596
rename("git\\.(.*)", "\$1")
96-
into(localRepo + "/hooks")
97+
into({ localHooksDir })
9798
}
9899
subprojects { subProject ->
99100
if (subProject.name.equals("checker")

checker/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ dependencies {
8686
// https://docs.gradle.org/7.1/userguide/validation_problems.html#implicit_dependency for more
8787
// details about this problem.
8888
generateGitProperties.dependsOn(":installGitHooks")
89+
def resolvedGitDir = providers.exec {
90+
workingDir = rootProject.projectDir
91+
commandLine("git", "rev-parse", "--absolute-git-dir")
92+
}.standardOutput.asText.map { rootProject.file(it.trim()) }
93+
8994
gitProperties {
90-
dotGitDirectory = project.rootProject.layout.projectDirectory.dir(".git")
95+
dotGitDirectory.set(rootProject.layout.dir(resolvedGitDir))
9196
}
9297

9398
jar {

0 commit comments

Comments
 (0)