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

universe@279 1 /*
universe@279 2 * Copyright 2023 Mike Becker. All rights reserved.
universe@279 3 *
universe@279 4 * Redistribution and use in source and binary forms, with or without
universe@279 5 * modification, are permitted provided that the following conditions are met:
universe@279 6 *
universe@279 7 * 1. Redistributions of source code must retain the above copyright
universe@279 8 * notice, this list of conditions and the following disclaimer.
universe@279 9 *
universe@279 10 * 2. Redistributions in binary form must reproduce the above copyright
universe@279 11 * notice, this list of conditions and the following disclaimer in the
universe@279 12 * documentation and/or other materials provided with the distribution.
universe@279 13 *
universe@279 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@279 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@279 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
universe@279 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
universe@279 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
universe@279 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
universe@279 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
universe@279 21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
universe@279 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
universe@279 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
universe@279 24 *
universe@279 25 */
universe@279 26
universe@279 27 package de.uapcore.lightpit.vcs
universe@279 28
universe@280 29 import java.nio.file.Files
universe@280 30 import java.nio.file.Path
universe@280 31 import kotlin.io.path.ExperimentalPathApi
universe@280 32 import kotlin.io.path.deleteRecursively
universe@279 33
universe@279 34 /**
universe@279 35 * A connector for Mercurial repositories.
universe@279 36 *
universe@279 37 * @param path the path to the Mercurial binary
universe@279 38 */
universe@280 39 class HgConnector(path: String) : VcsConnector(path) {
universe@279 40
universe@279 41 /**
universe@279 42 * Checks, if the specified binary is available and executable.
universe@279 43 */
universe@279 44 fun checkAvailability(): Boolean {
universe@280 45 return when (val versionInfo = invokeCommand(Path.of("."), "--version")) {
universe@280 46 is VcsConnectorResult.Success -> versionInfo.content.contains("Mercurial")
universe@280 47 else -> false
universe@279 48 }
universe@279 49 }
universe@280 50
universe@280 51 /**
universe@280 52 * Reads the commit log and parses every reference to an issue.
universe@280 53 *
universe@280 54 * The [pathOrUrl] must be a valid path or URL recognized by the VCS binary.
universe@280 55 * Currently, no authentication is supported and the repository must therefore be publicly readable.
universe@280 56 */
universe@280 57 @OptIn(ExperimentalPathApi::class)
universe@280 58 fun readCommitLog(pathOrUrl: String): VcsConnectorResult<List<CommitRef>> {
universe@280 59 val tmpDir = try {
universe@280 60 Files.createTempDirectory("lightpit-vcs-")
universe@280 61 } catch (e: Throwable) {
universe@280 62 return VcsConnectorResult.Error("Creating temporary directory for VCS connection failed: " + e.message)
universe@280 63 }
universe@280 64 val init = invokeCommand(tmpDir, "init")
universe@280 65 if (init is VcsConnectorResult.Error) {
universe@280 66 return init
universe@280 67 }
universe@280 68
universe@280 69 val commitLogContent = when (val result = invokeCommand(
universe@280 70 tmpDir, "incoming", pathOrUrl, "-n", "--template", "::lpitref::{node}:{desc}\\n"
universe@280 71 )) {
universe@280 72 is VcsConnectorResult.Error -> return result
universe@280 73 is VcsConnectorResult.Success -> result.content
universe@280 74 }
universe@280 75
universe@280 76 val commitRefs = parseCommitRefs(commitLogContent)
universe@280 77
universe@280 78 tmpDir.deleteRecursively()
universe@280 79 return VcsConnectorResult.Success(commitRefs)
universe@280 80 }
universe@279 81 }

mercurial