Skip to content

Commit d2d7eda

Browse files
committed
Support applying with Git directly optionally
1 parent 0a36d21 commit d2d7eda

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
}
1313

1414
group = 'net.minecrell'
15-
version = '0.3'
15+
version = '0.4'
1616
description = 'A Gradle plugin to manage patches for Git repositories'
1717

1818
sourceCompatibility = 1.6

src/main/groovy/net/minecrell/gitpatcher/task/patch/ApplyPatchesTask.groovy

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
*/
2222
package net.minecrell.gitpatcher.task.patch
2323

24+
import static java.lang.Boolean.parseBoolean
25+
import static java.lang.System.getProperty
2426
import static net.minecrell.gitpatcher.git.Patcher.withGit
2527
import static org.eclipse.jgit.api.Git.wrap
2628
import static org.eclipse.jgit.api.ResetCommand.ResetType.HARD
@@ -35,6 +37,8 @@ import org.gradle.api.tasks.TaskAction
3537

3638
class 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

Comments
 (0)