All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.com.datadog.gradle.plugin.internal.GitRepositoryDetector.kt Maven / Gradle / Ivy

There is a newer version: 1.15.0
Show newest version
package com.datadog.gradle.plugin.internal

import com.datadog.gradle.plugin.DdAndroidGradlePlugin.Companion.LOGGER
import com.datadog.gradle.plugin.RepositoryDetector
import com.datadog.gradle.plugin.RepositoryInfo
import java.io.File
import org.gradle.api.Project
import org.gradle.process.internal.ExecException

// TODO RUMM-1095 handle git submodules
// TODO RUMM-1096 handle git subtrees
// TODO RUMM-1093 let customer override `origin` with custom remote name
internal class GitRepositoryDetector : RepositoryDetector {

    @Suppress("StringLiteralDuplication")
    override fun detectRepositories(
        project: Project,
        sourceSetRoots: List
    ): List {
        try {
            project.execShell("git", "rev-parse", "--is-inside-work-tree")
        } catch (e: ExecException) {
            LOGGER.error("Project is not a git repository", e)
            return emptyList()
        }

        val remoteUrl = project.execShell("git", "remote", "get-url", "origin").trim()
        val commitHash = project.execShell("git", "rev-parse", "HEAD").trim()
        val rootFolder = project.execShell("git", "rev-parse", "--show-toplevel").trim()
        val rootPathPrefix = rootFolder + File.separator

        val trackedFiles = listTrackedFilesPath(sourceSetRoots, rootPathPrefix)

        return listOf(
            RepositoryInfo(
                remoteUrl,
                commitHash,
                trackedFiles
            )
        )
    }

    // region Internal

    private fun listTrackedFilesPath(
        sourceSetRoots: List,
        rootPathPrefix: String
    ): List {
        val files = mutableListOf()
        sourceSetRoots.forEach { root ->
            if (root.exists() && root.isDirectory) {
                listFilePathsInFolder(root, rootPathPrefix, files)
            }
        }
        return files
    }

    private fun listFilePathsInFolder(
        root: File,
        rootPathPrefix: String,
        files: MutableList
    ) {
        // TODO RUMM-1115 Prevent listing files which are not tracked
        // We need to use `git ls-files`
        // because some files might be added even if match a .gitignore pattern
        root.walkTopDown()
            .forEach {
                if (it.isFile) {
                    val localPath = it.absolutePath.substringAfter(rootPathPrefix)
                    files.add(localPath)
                }
            }
    }

    // endregion

    companion object {
        private const val GIT_CMD = "git"
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy