|
| 1 | +/* |
| 2 | + * paperweight is a Gradle plugin for the PaperMC project. |
| 3 | + * |
| 4 | + * Copyright (c) 2023 Kyle Wood (DenWav) |
| 5 | + * Contributors |
| 6 | + * |
| 7 | + * This library is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU Lesser General Public |
| 9 | + * License as published by the Free Software Foundation; |
| 10 | + * version 2.1 only, no later versions. |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with this library; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 20 | + * USA |
| 21 | + */ |
| 22 | + |
| 23 | +package io.papermc.paperweight.core.tasks.remap |
| 24 | + |
| 25 | +import io.papermc.paperweight.PaperweightException |
| 26 | +import io.papermc.paperweight.util.* |
| 27 | +import java.nio.file.Path |
| 28 | +import kotlin.io.path.* |
| 29 | + |
| 30 | +class PatchApplier( |
| 31 | + private val remappedBranch: String, |
| 32 | + private val unmappedBranch: String, |
| 33 | + private val ignoreGitIgnore: Boolean, |
| 34 | + targetDir: Path |
| 35 | +) { |
| 36 | + |
| 37 | + private val git = Git(targetDir) |
| 38 | + |
| 39 | + private var commitMessage: String? = null |
| 40 | + private var commitAuthor: String? = null |
| 41 | + private var commitTime: String? = null |
| 42 | + |
| 43 | + private val remappedBaseTag: String = "remapped-base" |
| 44 | + |
| 45 | + fun checkoutRemapped() { |
| 46 | + println("Switching to $remappedBranch without losing changes") |
| 47 | + git("symbolic-ref", "HEAD", "refs/heads/$remappedBranch").executeSilently() |
| 48 | + } |
| 49 | + |
| 50 | + fun checkoutOld() { |
| 51 | + println("Resetting back to $unmappedBranch branch") |
| 52 | + git("checkout", unmappedBranch).executeSilently() |
| 53 | + } |
| 54 | + |
| 55 | + fun commitPlain(message: String) { |
| 56 | + git(*Git.add(ignoreGitIgnore, ".")).executeSilently() |
| 57 | + git("commit", "-m", message, "--author=Initial <auto@mated.null>").executeSilently() |
| 58 | + } |
| 59 | + |
| 60 | + fun createBranches() { |
| 61 | + git("checkout", "-b", unmappedBranch).executeSilently() |
| 62 | + git("branch", remappedBranch).executeSilently() |
| 63 | + } |
| 64 | + |
| 65 | + fun commitInitialRemappedSource() { |
| 66 | + git(*Git.add(ignoreGitIgnore, ".")).executeSilently() |
| 67 | + git("commit", "-m", "Initial Remapped Source", "--author=Initial <auto@mated.null>").executeSilently() |
| 68 | + git("tag", remappedBaseTag).executeSilently() |
| 69 | + } |
| 70 | + |
| 71 | + fun recordCommit() { |
| 72 | + commitMessage = git("log", "--format=%B", "-n", "1", "HEAD").getText() |
| 73 | + commitAuthor = git("log", "--format=%an <%ae>", "-n", "1", "HEAD").getText() |
| 74 | + commitTime = git("log", "--format=%aD", "-n", "1", "HEAD").getText() |
| 75 | + } |
| 76 | + |
| 77 | + private fun clearCommit() { |
| 78 | + commitMessage = null |
| 79 | + commitAuthor = null |
| 80 | + commitTime = null |
| 81 | + } |
| 82 | + |
| 83 | + fun commitChanges() { |
| 84 | + println("Committing remapped changes to $remappedBranch") |
| 85 | + val message = commitMessage ?: throw PaperweightException("commitMessage not set") |
| 86 | + val author = commitAuthor ?: throw PaperweightException("commitAuthor not set") |
| 87 | + val time = commitTime ?: throw PaperweightException("commitTime not set") |
| 88 | + clearCommit() |
| 89 | + |
| 90 | + git(*Git.add(ignoreGitIgnore, ".")).executeSilently() |
| 91 | + git("commit", "-m", message, "--author=$author", "--date=$time").execute() |
| 92 | + } |
| 93 | + |
| 94 | + fun applyPatch(patch: Path) { |
| 95 | + val result = git("am", "--3way", "--ignore-whitespace", patch.absolutePathString()).runOut() |
| 96 | + if (result != 0) { |
| 97 | + throw RuntimeException("Patch failed to apply: $patch") |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + fun generatePatches(target: Path) { |
| 102 | + target.deleteRecursive() |
| 103 | + target.createDirectories() |
| 104 | + git("checkout", remappedBranch).executeSilently() |
| 105 | + git( |
| 106 | + "format-patch", "--diff-algorith=myers", "--zero-commit", "--full-index", "--no-signature", "--no-stat", "-N", "-o", |
| 107 | + target.absolutePathString(), remappedBaseTag |
| 108 | + ).executeOut() |
| 109 | + } |
| 110 | + |
| 111 | + fun isUnfinishedPatch(): Boolean { |
| 112 | + if (git("branch", "--show-current").getText().trim() != unmappedBranch) { |
| 113 | + return false |
| 114 | + } |
| 115 | + |
| 116 | + git("update-index", "--refresh").executeSilently() |
| 117 | + if (git("diff-index", "--diff-algorithm=myers", "--quiet", "HEAD", "--").runSilently() == 0) { |
| 118 | + return git("log", unmappedBranch, "-1", "--pretty=%B").getText().trim() != |
| 119 | + git("log", remappedBranch, "-1", "--pretty=%B").getText().trim() |
| 120 | + } |
| 121 | + |
| 122 | + throw PaperweightException("Unknown state: repo has uncommitted changes") |
| 123 | + } |
| 124 | +} |
0 commit comments