-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
allow to optionally apply/format source patches using git
- Loading branch information
1 parent
af1bbec
commit f941ce4
Showing
4 changed files
with
104 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ import io.papermc.paperweight.tasks.* | |
import io.papermc.paperweight.util.* | ||
import java.io.PrintStream | ||
import java.nio.file.Path | ||
import java.time.Instant | ||
import kotlin.io.path.* | ||
import org.eclipse.jgit.api.Git | ||
import org.eclipse.jgit.lib.PersonIdent | ||
|
@@ -64,9 +65,14 @@ abstract class ApplyFilePatches : BaseTask() { | |
@get:OutputDirectory | ||
abstract val rejects: DirectoryProperty | ||
|
||
@get:Optional | ||
@get:Input | ||
abstract val gitFilePatches: Property<Boolean> | ||
|
||
init { | ||
run { | ||
verbose.convention(false) | ||
gitFilePatches.convention(false) | ||
} | ||
} | ||
|
||
|
@@ -81,6 +87,31 @@ abstract class ApplyFilePatches : BaseTask() { | |
|
||
setupGitHook(outputPath) | ||
|
||
val result = if (gitFilePatches.get()) { | ||
applyWithGit(outputPath) | ||
} else { | ||
applyWithDiffPatch() | ||
} | ||
|
||
if (!verbose.get()) { | ||
logger.lifecycle("Applied $result patches") | ||
} | ||
} | ||
|
||
private fun applyWithGit(outputPath: Path): Int { | ||
val git = Git(outputPath) | ||
val patches = patches.convertToPath().filesMatchingRecursive("*.patch") | ||
val patchStrings = patches.map { outputPath.relativize(it).pathString } | ||
patchStrings.chunked(12).forEach { | ||
git("apply", "--3way", *it.toTypedArray()).executeSilently(silenceOut = !verbose.get(), silenceErr = !verbose.get()) | ||
} | ||
|
||
commit() | ||
|
||
return patches.size | ||
} | ||
|
||
private fun applyWithDiffPatch(): Int { | ||
val printStream = PrintStream(LoggingOutputStream(logger, LogLevel.LIFECYCLE)) | ||
val builder = PatchOperation.builder() | ||
.logTo(printStream) | ||
|
@@ -96,6 +127,7 @@ abstract class ApplyFilePatches : BaseTask() { | |
if (rejects.isPresent) { | ||
builder.rejectsPath(rejects.convertToPath()) | ||
} | ||
|
||
val result = builder.build().operate() | ||
|
||
commit() | ||
|
@@ -106,9 +138,7 @@ abstract class ApplyFilePatches : BaseTask() { | |
throw Exception("Failed to apply ${result.summary.failedMatches}/$total hunks") | ||
} | ||
|
||
if (!verbose.get()) { | ||
logger.lifecycle("Applied ${result.summary.changedFiles} patches") | ||
} | ||
return result.summary.changedFiles | ||
} | ||
|
||
private fun setupGitHook(outputPath: Path) { | ||
|
@@ -119,7 +149,7 @@ abstract class ApplyFilePatches : BaseTask() { | |
} | ||
|
||
private fun commit() { | ||
val ident = PersonIdent("File", "[email protected]") | ||
val ident = PersonIdent(PersonIdent("File", "[email protected]"), Instant.parse("1997-04-20T13:37:42.69Z")) | ||
val git = Git.open(output.convertToPath().toFile()) | ||
git.add().addFilepattern(".").call() | ||
git.commit() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters