fun findGitRepositoryFolder()

in src/main/kotlin/org/jetbrains/qodana/utils.kt [29:55]


    fun findGitRepositoryFolder(folderPath: String): File? {
        try {
            var parentFolder: File? = File(folderPath).absoluteFile
            while (parentFolder != null) {
                val gitFolder = File(parentFolder, ".git")
                if (gitFolder.exists() && gitFolder.isDirectory) {
                    return parentFolder
                } else if (gitFolder.exists() && gitFolder.isFile) {
                    // it could be submodule, in this case we need to look up the symlink
                    val lines = gitFolder.readLines()
                    for (line in lines) {
                        if (line.startsWith("gitdir: ")) {
                            val relativeGitDir = line.removePrefix("gitdir: ").trim()
                            return Paths.get(gitFolder.parentFile.absolutePath, relativeGitDir).normalize().toFile()
                        }
                    }
                    // we know that we've found nothing
                    return null
                }
                parentFolder = parentFolder.parentFile
            }
            return null
        } catch (e: Exception) {
            return null // we may get RBAC problems here
            // TODO: add reporting to the client
        }
    }