src/main/kotlin/de/uapcore/lightpit/vcs/HgConnector.kt

changeset 280
12b898531d1a
parent 279
d73537b925af
child 281
c15b9555ecf3
     1.1 --- a/src/main/kotlin/de/uapcore/lightpit/vcs/HgConnector.kt	Mon Jul 17 14:45:42 2023 +0200
     1.2 +++ b/src/main/kotlin/de/uapcore/lightpit/vcs/HgConnector.kt	Tue Jul 18 18:05:49 2023 +0200
     1.3 @@ -26,29 +26,56 @@
     1.4  
     1.5  package de.uapcore.lightpit.vcs
     1.6  
     1.7 -import java.util.concurrent.TimeUnit
     1.8 +import java.nio.file.Files
     1.9 +import java.nio.file.Path
    1.10 +import kotlin.io.path.ExperimentalPathApi
    1.11 +import kotlin.io.path.deleteRecursively
    1.12  
    1.13  /**
    1.14   * A connector for Mercurial repositories.
    1.15   *
    1.16   * @param path the path to the Mercurial binary
    1.17   */
    1.18 -class HgConnector(private val path: String) {
    1.19 +class HgConnector(path: String) : VcsConnector(path) {
    1.20  
    1.21      /**
    1.22       * Checks, if the specified binary is available and executable.
    1.23       */
    1.24      fun checkAvailability(): Boolean {
    1.25 -        return try {
    1.26 -            val process = ProcessBuilder(path, "--version").start()
    1.27 -            val versionInfo = String(process.inputStream.readAllBytes(), Charsets.UTF_8)
    1.28 -            if (process.waitFor(10, TimeUnit.SECONDS)) {
    1.29 -                versionInfo.contains("Mercurial")
    1.30 -            } else {
    1.31 -                false
    1.32 -            }
    1.33 -        } catch (_: Throwable) {
    1.34 -            false
    1.35 +        return when (val versionInfo = invokeCommand(Path.of("."), "--version")) {
    1.36 +            is VcsConnectorResult.Success -> versionInfo.content.contains("Mercurial")
    1.37 +            else -> false
    1.38          }
    1.39      }
    1.40 +
    1.41 +    /**
    1.42 +     * Reads the commit log and parses every reference to an issue.
    1.43 +     *
    1.44 +     * The [pathOrUrl] must be a valid path or URL recognized by the VCS binary.
    1.45 +     * Currently, no authentication is supported and the repository must therefore be publicly readable.
    1.46 +     */
    1.47 +    @OptIn(ExperimentalPathApi::class)
    1.48 +    fun readCommitLog(pathOrUrl: String): VcsConnectorResult<List<CommitRef>> {
    1.49 +        val tmpDir = try {
    1.50 +            Files.createTempDirectory("lightpit-vcs-")
    1.51 +        } catch (e: Throwable) {
    1.52 +            return VcsConnectorResult.Error("Creating temporary directory for VCS connection failed: " + e.message)
    1.53 +        }
    1.54 +        val init = invokeCommand(tmpDir, "init")
    1.55 +        if (init is VcsConnectorResult.Error) {
    1.56 +            return init
    1.57 +        }
    1.58 +
    1.59 +        val commitLogContent = when (val result = invokeCommand(
    1.60 +            tmpDir, "incoming", pathOrUrl, "-n", "--template", "::lpitref::{node}:{desc}\\n"
    1.61 +        )) {
    1.62 +            is VcsConnectorResult.Error -> return result
    1.63 +            is VcsConnectorResult.Success -> result.content
    1.64 +        }
    1.65 +
    1.66 +        val commitRefs = parseCommitRefs(commitLogContent)
    1.67 +
    1.68 +        tmpDir.deleteRecursively()
    1.69 +        return VcsConnectorResult.Success(commitRefs)
    1.70 +    }
    1.71  }
    1.72 \ No newline at end of file

mercurial