Cannot find git directory when using git worktrees
When I create a worktree with git worktree add worktrees/tmp branch-name I will get
> Task :generateGitProperties FAILED
/absolute/path/to/project/worktrees/tmp/.git/config: Not a directory
when trying to build
I think this is caused by https://github.com/n0mer/gradle-git-properties/pull/235.
Could you please check if doing this fixes it?
gitProperties.dotGitDirectory = project.rootProject.layout.projectDirectory.dir('.git')
(or .file('.git') instead of .dir('.git'))
I think the plugin by default should check if the root project has a .git dir and use that if so or check if it has a .git file (worktree) and use the directory found in the file.
@jonatan-ivanov still happens, but different error:
Execution failed for task ':generateGitProperties'.
> Error while evaluating property 'generatedProperties' of task ':generateGitProperties'.
> gradlegitproperties.org.eclipse.jgit.errors.RepositoryNotFoundException: repository not found: /absolute/path/to/project/.git/worktrees/tmp
the directory does exists and does contain the git data though.
project/.git/worktrees/tmp is not your .git folder though, project/.git is.
You might be able to read the .git file in your worktree, and get the .git folder location from there instead.
You might be able to read the
.gitfile in your worktree, and get the.gitfolder location from there instead.
That file does point to the sub directory though:
gitdir: /absolute/path/to/project/.git/worktrees/tmp
My guess is that it points not only to the .git directory but also to where the metadata for this worktree is stored.
Yes, from there, you can figure out where the git folder is: .getParentFile().getParentFile() or cut everything after .git.
You can also execute this and set the output as the dotGitDirectory :
git rev-parse --path-format=absolute --git-common-dir
Both of them are big hacks but I'm not sure there is another option unless this is fixed in the project.
fyi https://github.com/n0mer/gradle-git-properties/issues/14
It would be nice if this was supported natively by the plugin instead of having to add something like this to build.gradle
def resolveDotGitDir(File repoRoot) {
File dotGit = new File(repoRoot, ".git")
if (dotGit.isDirectory()) return dotGit
if (dotGit.isFile()) {
def line = dotGit.readLines().find { it.startsWith("gitdir:") }
if (line) return new File(line.split(":", 2)[1].trim().replaceFirst(/\.git.*$/, ".git"))
}
return null
}
gitProperties {
def dir = resolveDotGitDir(rootDir)
if (dir?.exists()) {
dotGitDirectory = dir
} else {
failOnNoGitDirectory = false
}
}