diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt index 8de9acb5a..8bd6e2a65 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt @@ -28,6 +28,7 @@ import io.papermc.paperweight.core.tasks.SetupForkMinecraftSources import io.papermc.paperweight.core.tasks.patching.ApplyFeaturePatches import io.papermc.paperweight.core.tasks.patching.ApplyFilePatches import io.papermc.paperweight.core.tasks.patching.ApplyFilePatchesFuzzy +import io.papermc.paperweight.core.tasks.patching.FixupFeaturePatches import io.papermc.paperweight.core.tasks.patching.FixupFilePatches import io.papermc.paperweight.core.tasks.patching.RebuildFilePatches import io.papermc.paperweight.tasks.* @@ -263,6 +264,15 @@ class MinecraftPatchingTasks( upstream.set("upstream/main") } + val fixupFeaturePatches = tasks.register("fixup${namePart}FeaturePatches") { + group() + description = "Puts the currently tracked source changes into the specified $configName Minecraft feature patch commit" + + repo.set(outputSrc) + upstream.set("file") + patches.set(featurePatchDir) + } + val applyOrMoveSourcePatches = tasks.register("applyOrMove${namePart}SourcePatches") { configureApplyFilePatches() description = "Applies $configName file patches to the Minecraft sources as Git patches, moving any failed patches to the rejects dir. " + diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/PatchingTasks.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/PatchingTasks.kt index d9cf03739..dae3eb472 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/PatchingTasks.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/PatchingTasks.kt @@ -25,6 +25,7 @@ package io.papermc.paperweight.core.taskcontainers import io.papermc.paperweight.core.tasks.patching.ApplyFeaturePatches import io.papermc.paperweight.core.tasks.patching.ApplyFilePatches import io.papermc.paperweight.core.tasks.patching.ApplyFilePatchesFuzzy +import io.papermc.paperweight.core.tasks.patching.FixupFeaturePatches import io.papermc.paperweight.core.tasks.patching.FixupFilePatches import io.papermc.paperweight.core.tasks.patching.RebuildFilePatches import io.papermc.paperweight.tasks.* @@ -101,6 +102,7 @@ class PatchingTasks( val rebuildFilePatchesName = "rebuild${namePart}FilePatches" val fixupFilePatchesName = "fixup${namePart}FilePatches" + val fixupFeaturePatchesName = "fixup${namePart}FeaturePatches" val rebuildFeaturePatchesName = "rebuild${namePart}FeaturePatches" val rebuildPatchesName = "rebuild${namePart}Patches" @@ -139,6 +141,15 @@ class PatchingTasks( upstream.set("base") } + val fixupFeaturePatches = tasks.register(fixupFeaturePatchesName) { + group = taskGroup + description = "Puts the currently tracked source changes into the specified $patchSetName feature patch commit" + + repo.set(outputDir) + upstream.set("file") + patches.set(featurePatchDir) + } + val rebuildFeaturePatches = tasks.register(rebuildFeaturePatchesName) { group = taskGroup description = "Rebuilds $patchSetName feature patches" diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/FixupFeaturePatches.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/FixupFeaturePatches.kt new file mode 100644 index 000000000..6567d595c --- /dev/null +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/tasks/patching/FixupFeaturePatches.kt @@ -0,0 +1,83 @@ +/* + * paperweight is a Gradle plugin for the PaperMC project. + * + * Copyright (c) 2023 Kyle Wood (DenWav) + * Contributors + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 only, no later versions. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 + * USA + */ + +package io.papermc.paperweight.core.tasks.patching + +import io.papermc.paperweight.tasks.* +import io.papermc.paperweight.util.* +import kotlin.io.path.listDirectoryEntries +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputDirectory +import org.gradle.api.tasks.Optional +import org.gradle.api.tasks.TaskAction +import org.gradle.api.tasks.UntrackedTask +import org.gradle.api.tasks.options.Option + +@UntrackedTask(because = "Always fixup when requested") +abstract class FixupFeaturePatches : BaseTask() { + + @get:InputDirectory + abstract val repo: DirectoryProperty + + @get:Input + abstract val upstream: Property + + @get:InputDirectory + abstract val patches: DirectoryProperty + + @get:Input + @get:Optional + @get:Option(option = "patch-number", description = "Select the patch to modify") + abstract val patchNumber: Property + + @TaskAction + fun run() { + val git = Git(repo) + var index = Int.MIN_VALUE + if (patchNumber.isPresent) { + index = patchNumber.get() - 1 // -1 as the commits index starts from 0 whereas patches start from 1 + } else { + logger.lifecycle("===============================================") + logger.lifecycle("Please enter the patch number into which the current changes should be merged") + logger.lifecycle("===============================================") + val patches = patches.get().path.listDirectoryEntries("*.patch").toList().sortedBy { it.fileName.toString().substringBefore("-").toInt() } + logger.lifecycle("Possible patches:") + for (patch in patches) { + logger.lifecycle(patch.fileName.toString()) + } + logger.lifecycle("===============================================") + while (index == Int.MIN_VALUE) { + index = System.`in`.bufferedReader().readLine().toInt() - 1 + } + } + val commits = git("rev-list", "file..HEAD").getText().trim().lines().filter { it.isNotBlank() }.reversed() + if (index < 0 || index >= commits.size) { + error("Patch index out of range: $index (size=${commits.size})") + } + val selectedCommit = commits[index] + git("add", ".").executeOut() + git("commit", "--fixup", selectedCommit).executeOut() + git("-c", "sequence.editor=:", "rebase", "-i", "--autosquash", upstream.get()).executeOut() + } +}