override fun handle()

in src/main/kotlin/org/jetbrains/mcpserverplugin/git/vcsTools.kt [29:66]


    override fun handle(project: Project, args: CommitQuery): Response {
        val queryText = args.text
        val matchingCommits = mutableListOf<String>()

        try {
            val vcs = ProjectLevelVcsManager.getInstance(project).allVcsRoots
                .mapNotNull { it.path }

            if (vcs.isEmpty()) {
                return Response("Error: No VCS configured for this project")
            }

            // Iterate over each VCS root to search for commits
            vcs.forEach { vcsRoot ->
                val repository = GitRepositoryManager.getInstance(project).getRepositoryForRoot(vcsRoot)
                    ?: return@forEach

                val gitLog = GitHistoryUtils.history(project, repository.root)

                gitLog.forEach { commit ->
                    if (commit.fullMessage.contains(queryText, ignoreCase = true)) {
                        matchingCommits.add(commit.id.toString())
                    }
                }
            }

            // Check if any matches were found
            return if (matchingCommits.isNotEmpty()) {
                Response(matchingCommits.joinToString(prefix = "[", postfix = "]", separator = ",") { "\"$it\"" })
            } else {
                Response("No commits found matching the query: $queryText")
            }

        } catch (e: Exception) {
            // Handle any errors that occur during the search
            return Response("Error while searching commits: ${e.message}")
        }
    }