2121 */
2222package net.minecrell.gitpatcher.task.patch
2323
24+ import static java.lang.Boolean.parseBoolean
25+ import static java.lang.System.getProperty
2426import static net.minecrell.gitpatcher.git.Patcher.withGit
2527import static org.eclipse.jgit.api.Git.wrap
2628import static org.eclipse.jgit.api.ResetCommand.ResetType.HARD
@@ -35,6 +37,8 @@ import org.gradle.api.tasks.TaskAction
3537
3638class ApplyPatchesTask extends PatchTask {
3739
40+ private static final boolean JGIT_APPLY = parseBoolean(getProperty(" jgit.apply" , " true" ))
41+
3842 @Override @InputFiles
3943 File [] getPatches () {
4044 return super . getPatches()
@@ -90,23 +94,38 @@ class ApplyPatchesTask extends PatchTask {
9094 if (patchDir. isDirectory()) {
9195 logger. lifecycle ' Applying patches from {} to {}' , patchDir, repo
9296
93- for (def file : patches) {
94- logger. lifecycle ' Applying: {}' , file. name
95-
96- def data = new ByteArrayInputStream (file. bytes)
97- def patch = MailPatch . parseHeader(data)
98- data. reset()
97+ if (! JGIT_APPLY ) {
98+ [' git' , ' am' , ' --abort' ]. execute(null as String [], repo). waitFor()
99+ }
99100
100- apply(). setPatch(data). call()
101- commit(). setAuthor(patch. author)
102- .setMessage(patch. message)
103- .setAll(true )
104- .call()
101+ for (def file : patches) {
102+ if (JGIT_APPLY ) {
103+ logger. lifecycle ' Applying: {}' , file. name
104+
105+ def data = new ByteArrayInputStream (file. bytes)
106+ def patch = MailPatch . parseHeader(data)
107+ data. reset()
108+
109+ apply(). setPatch(data). call()
110+ commit(). setAuthor(patch. author)
111+ .setMessage(patch. message)
112+ .setAll(true )
113+ .call()
114+ } else {
115+ def p = [' git' , ' am' , ' --3way' , file. absolutePath]. execute(null as String [], repo)
116+ p. consumeProcessOutput(System . out as OutputStream , System . err)
117+ def r = p. waitFor()
118+ assert r == 0 , " Process returned error code $r "
119+ }
105120 }
106121
107122 logger. lifecycle ' Successfully applied patches from {} to {}' , patchDir, repo
108123 }
109124 }
110125 }
111126
127+ private static void exec (List<String > command , File workingDir ) {
128+ command. execute(null as String [], workingDir). consumeProcessOutput()
129+ }
130+
112131}
0 commit comments