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

Tue, 18 Jul 2023 18:05:49 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 18 Jul 2023 18:05:49 +0200
changeset 280
12b898531d1a
parent 279
d73537b925af
child 281
c15b9555ecf3
permissions
-rw-r--r--

add working Mercurial commit log parser

fixes #274

/*
 * Copyright 2023 Mike Becker. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

package de.uapcore.lightpit.vcs

import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.deleteRecursively

/**
 * A connector for Mercurial repositories.
 *
 * @param path the path to the Mercurial binary
 */
class HgConnector(path: String) : VcsConnector(path) {

    /**
     * Checks, if the specified binary is available and executable.
     */
    fun checkAvailability(): Boolean {
        return when (val versionInfo = invokeCommand(Path.of("."), "--version")) {
            is VcsConnectorResult.Success -> versionInfo.content.contains("Mercurial")
            else -> false
        }
    }

    /**
     * Reads the commit log and parses every reference to an issue.
     *
     * The [pathOrUrl] must be a valid path or URL recognized by the VCS binary.
     * Currently, no authentication is supported and the repository must therefore be publicly readable.
     */
    @OptIn(ExperimentalPathApi::class)
    fun readCommitLog(pathOrUrl: String): VcsConnectorResult<List<CommitRef>> {
        val tmpDir = try {
            Files.createTempDirectory("lightpit-vcs-")
        } catch (e: Throwable) {
            return VcsConnectorResult.Error("Creating temporary directory for VCS connection failed: " + e.message)
        }
        val init = invokeCommand(tmpDir, "init")
        if (init is VcsConnectorResult.Error) {
            return init
        }

        val commitLogContent = when (val result = invokeCommand(
            tmpDir, "incoming", pathOrUrl, "-n", "--template", "::lpitref::{node}:{desc}\\n"
        )) {
            is VcsConnectorResult.Error -> return result
            is VcsConnectorResult.Success -> result.content
        }

        val commitRefs = parseCommitRefs(commitLogContent)

        tmpDir.deleteRecursively()
        return VcsConnectorResult.Success(commitRefs)
    }
}

mercurial