src/main/kotlin/de/uapcore/lightpit/vcs/VcsConnector.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
child 281
c15b9555ecf3
permissions
-rw-r--r--

add working Mercurial commit log parser

fixes #274

     1 package de.uapcore.lightpit.vcs
     3 import java.nio.file.Path
     4 import java.util.concurrent.TimeUnit
     6 abstract class VcsConnector(protected val path: String) {
     7     /**
     8      * Invokes the VCS binary with the given [args] and returns the output on stdout.
     9      */
    10     protected fun invokeCommand(workingDir: Path, vararg args : String): VcsConnectorResult<String> {
    11         return try {
    12             val command = mutableListOf(path)
    13             command.addAll(args)
    14             val process = ProcessBuilder(command).directory(workingDir.toFile()).start()
    15             val stdout = String(process.inputStream.readAllBytes(), Charsets.UTF_8)
    16             if (process.waitFor(30, TimeUnit.SECONDS)) {
    17                 if (process.exitValue() == 0) {
    18                     VcsConnectorResult.Success(stdout)
    19                 } else {
    20                     VcsConnectorResult.Error("VCS process did not return successfully.")
    21                 }
    22             } else {
    23                 VcsConnectorResult.Error("VCS process did not return in time.")
    24             }
    25         } catch (e: Throwable) {
    26             VcsConnectorResult.Error("Error during process invocation: "+e.message)
    27         }
    28     }
    30     /**
    31      * Takes a [commitLog] in format `::lpitref::{node}:{desc}` and parses commit references.
    32      * Supported references are (in this example, 47 is the issue ID):
    33      *  - fixes #47
    34      *  - fix #47
    35      *  - closes #47
    36      *  - close #47
    37      *  - relates to #47
    38      */
    39     protected fun parseCommitRefs(commitLog: String): List<CommitRef> = buildList {
    40         val marker = "::lpitref::"
    41         var currentHash = ""
    42         var currentDesc = ""
    43         for (line in commitLog.split("\n")) {
    44             // see if current line contains a new log entry
    45             if (line.startsWith(marker)) {
    46                 val head = line.substring(marker.length).split(':', limit = 2)
    47                 currentHash = head[0]
    48                 currentDesc = head[1]
    49             }
    51             // skip possible preamble output
    52             if (currentHash.isEmpty()) continue
    54             // scan the lines for commit references
    55             Regex("""(?:relates to|fix(?:es)?|close(?:es)?) #(\d+)""")
    56                 .findAll(line)
    57                 .map { it.groupValues[1] }
    58                 .map { it.toIntOrNull() }
    59                 .filterNotNull()
    60                 .forEach { commitId -> add(CommitRef(currentHash, commitId, currentDesc)) }
    61         }
    62     }
    63 }

mercurial