gradle-git-properties icon indicating copy to clipboard operation
gradle-git-properties copied to clipboard

Cannot find git directory when using git worktrees

Open WorkProg opened this issue 9 months ago • 6 comments

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

WorkProg avatar Mar 25 '25 09:03 WorkProg

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 avatar Mar 26 '25 23:03 jonatan-ivanov

@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.

WorkProg avatar Apr 01 '25 09:04 WorkProg

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.

jonatan-ivanov avatar Apr 01 '25 18:04 jonatan-ivanov

You might be able to read the .git file in your worktree, and get the .git folder 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.

WorkProg avatar Apr 02 '25 07:04 WorkProg

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.

jonatan-ivanov avatar Apr 02 '25 20:04 jonatan-ivanov

fyi https://github.com/n0mer/gradle-git-properties/issues/14

yewton avatar May 23 '25 00:05 yewton

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
    }
}

codeconsole avatar Aug 08 '25 23:08 codeconsole