aboutsummaryrefslogtreecommitdiff
path: root/dokka-integration-tests/utilities/src/main/kotlin/org/jetbrains/dokka/it/gitSubmoduleUtils.kt
blob: f8f103be2f74b2845bda264f3b653edd1920b48e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
 */

package org.jetbrains.dokka.it

import org.eclipse.jgit.api.Git
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
import java.io.File
import java.nio.file.Path

public fun AbstractIntegrationTest.copyAndApplyGitDiff(diffFile: File) {
    copyGitDiffFileToParent(diffFile).let(::applyGitDiffFromFile)
}

public fun AbstractIntegrationTest.copyGitDiffFileToParent(originalDiffFile: File): File =
    originalDiffFile.copyTo(File(projectDir.parent, originalDiffFile.name))

public fun AbstractIntegrationTest.applyGitDiffFromFile(diffFile: File) {
    val projectGitFile = projectDir.resolve(".git")
    val git = if (projectGitFile.exists()) {
        if (projectGitFile.isFile) {
            println(".git file inside project directory exists, removing")
            removeGitFile(projectDir.toPath())
            Git.init().setDirectory(projectDir).call()
        } else {
            println(".git directory inside project directory exists, reusing")
            FileRepositoryBuilder().apply {
                isMustExist = true
                gitDir = projectDir
            }.let { Git(it.build()) }
        }
    } else {
        Git.init().setDirectory(projectDir).call()
    }
    git.apply().setPatch(diffFile.inputStream()).call()
}

private fun removeGitFile(repository: Path) =
    repository.toFile()
        .listFiles().orEmpty()
        .filter { it.name.equals(".git", ignoreCase = true) }
        .forEach { it.delete() }