Skip to content

Commit d1cd9ac

Browse files
committed
automatically remove paper AT comments
requires fun git magic when rebuilding patches...
1 parent a7412cf commit d1cd9ac

File tree

6 files changed

+29
-13
lines changed

6 files changed

+29
-13
lines changed

.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,3 @@ ij_kotlin_import_nested_classes = false
8686

8787
[*.patch]
8888
trim_trailing_whitespace=false
89-
90-
[*.patch]
91-
trim_trailing_whitespace=false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*.patch]
2+
trim_trailing_whitespace=false
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.gradle
22
gradle
3+
build
4+
.idea

paperweight-core/src/test/resources/functional_test/fake-patches/expected/0001-Feature.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ Subject: [PATCH] Feature
55

66

77
diff --git a/Test.java b/Test.java
8-
index 8bd0bab8fc2fb2f6a4459ed9f618214ec048afc4..2d8e20f42479672b0d390febbeffc65909b9da42 100644
8+
index 372d9f877b361130deb341f1918d8f8c3ed0cf27..b24553b410c3b559c568dad6c811adc0a0bc6540 100644
99
--- a/Test.java
1010
+++ b/Test.java
1111
@@ -13,4 +13,8 @@ public class Test {
12-
private final String getTest2() {// Paper-AT: private+f getTest2()Ljava/lang/String;
12+
private final String getTest2() {
1313
return this.test + "2"; // Woo
1414
}
1515
+

paperweight-core/src/test/resources/functional_test/fake-patches/expected/Test.java.patch

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
+ return this.test; // WOOOO
99
}
1010

11-
- private final String getTest2() {
11+
private final String getTest2() {
1212
- return this.test + "2";
13-
+ private final String getTest2() {// Paper-AT: private+f getTest2()Ljava/lang/String;
1413
+ return this.test + "2"; // Woo
1514
}
1615
}

paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/softspoon/RebuildFilePatches.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import org.gradle.api.file.RegularFileProperty
4343
import org.gradle.api.provider.Property
4444
import org.gradle.api.tasks.*
4545
import org.gradle.api.tasks.options.Option
46+
import org.intellij.lang.annotations.Language
4647
import org.openrewrite.InMemoryExecutionContext
4748
import writeLF
4849

@@ -99,7 +100,7 @@ abstract class RebuildFilePatches : BaseTask() {
99100
git("checkout", "file").executeSilently(silenceErr = true)
100101

101102
// handle AT
102-
var addedNewAts = false
103+
val filesWithNewAts = mutableListOf<String>()
103104
baseDir.walk()
104105
.map { it.relativeTo(baseDir).toString().replace("\\", "/") }
105106
.filter {
@@ -110,8 +111,10 @@ abstract class RebuildFilePatches : BaseTask() {
110111
val source = inputDir.resolve(it)
111112
val decomp = baseDir.resolve(it)
112113
val className = it.replace(".java", "")
113-
addedNewAts = handleATInSource(source, ats, className) || addedNewAts
114-
handleATInBase(decomp, ats, baseDir)
114+
if (handleATInSource(source, ats, className)) {
115+
handleATInBase(decomp, ats, baseDir)
116+
filesWithNewAts.add(it)
117+
}
115118
oldAts.merge(ats)
116119
}
117120

@@ -123,7 +126,8 @@ abstract class RebuildFilePatches : BaseTask() {
123126
)
124127
}
125128

126-
if (addedNewAts) {
129+
if (filesWithNewAts.isNotEmpty()) {
130+
// we removed the comment, we need to commit this
127131
git("add", ".").executeSilently(silenceErr = true)
128132
git("commit", "--amend", "--no-edit").executeSilently(silenceErr = true)
129133
}
@@ -148,9 +152,13 @@ abstract class RebuildFilePatches : BaseTask() {
148152
.operate()
149153

150154
git("switch", "-").executeSilently(silenceErr = true)
151-
if (addedNewAts) {
155+
if (filesWithNewAts.isNotEmpty()) {
152156
try {
153-
git("rebase", "file").executeSilently()
157+
// todo detect if sed is not present (windows) and switch out sed for something else
158+
@Language("Shell Script")
159+
val sequenceEditor = "sed -i -e 0,/pick/{s/pick/drop/}"
160+
val execs = filesWithNewAts.map { it -> "sed -i -e 's|// Paper-AT:.*||g' $it && ((git add $it && git commit --amend --no-edit) || true)" }.flatMap { listOf("--exec", it) }.toTypedArray()
161+
git.withEnv(mapOf("GIT_SEQUENCE_EDITOR" to sequenceEditor))("rebase", "-i", "file", "--strategy-option=theirs", *execs).executeSilently()
154162
} catch (e: Exception) {
155163
// TODO better message to inform the user on what to do
156164
throw RuntimeException("Encountered conflicts while rebuilding file patches.", e)
@@ -193,8 +201,10 @@ abstract class RebuildFilePatches : BaseTask() {
193201
private fun handleATInSource(source: Path, newAts: AccessTransformSet, className: String): Boolean {
194202
val sourceLines = source.readLines()
195203
var foundNew = false
204+
val fixedLines = ArrayList<String>(sourceLines.size)
196205
sourceLines.forEach { line ->
197206
if (!line.contains("// Paper-AT: ")) {
207+
fixedLines.add(line)
198208
return@forEach
199209
}
200210

@@ -213,6 +223,12 @@ abstract class RebuildFilePatches : BaseTask() {
213223
atClass.mergeMethod(MethodSignature.of(name.substring(0, index), name.substring(index)), accessTransform)
214224
}
215225
logger.lifecycle("Found new AT in $className: $at -> $accessTransform")
226+
227+
fixedLines.add(split[0])
228+
}
229+
230+
if (foundNew) {
231+
source.writeText(fixedLines.joinToString("\n", postfix = "\n"), Charsets.UTF_8)
216232
}
217233

218234
return foundNew

0 commit comments

Comments
 (0)