Skip to content

Commit 52f10a5

Browse files
committed
Improve up-to-date check for patch generation
1 parent 45370d4 commit 52f10a5

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

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

+33-21
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,8 @@ class MakePatchesTask extends PatchTask {
7979

8080
didWork = false
8181
for (def patch : patches) {
82-
def diff = git.diff('--no-color', '-U1', '--staged', patch.absolutePath).text.readLines()
83-
84-
boolean upToDate = false
85-
86-
if (diff.empty) {
87-
upToDate = true
88-
} else if (!diff.contains('--- /dev/null')) {
89-
def first = diff.findIndexOf(HUNK)
90-
if (first >= 0 && diff[first + 1].startsWith('From', 1)) {
91-
def last = diff.findLastIndexOf(HUNK)
92-
if (first == last) { // There is just one hunk, so probably just the hash changed
93-
upToDate = true
94-
} else if (last >= 0 && diff[last + 1].startsWith('--', 1)) {
95-
if (!diff.subList(first + 4, last).find(HUNK)) {
96-
upToDate = true
97-
}
98-
}
99-
}
100-
}
101-
102-
if (upToDate) {
82+
List<String> diff = git.diff('--no-color', '-U1', '--staged', patch.absolutePath).text.readLines()
83+
if (isUpToDate(diff)) {
10384
logger.lifecycle 'Skipping {} (up-to-date)', patch.name
10485
git.reset('HEAD', patch.absolutePath) >> null
10586
git.checkout('--', patch.absolutePath) >> null
@@ -110,4 +91,35 @@ class MakePatchesTask extends PatchTask {
11091
}
11192
}
11293

94+
private static boolean isUpToDate(List<String> diff) {
95+
if (diff.empty) {
96+
return true
97+
}
98+
99+
if (diff.contains('--- /dev/null')) {
100+
return false
101+
}
102+
103+
// Check if there are max. 2 diff hunks (once for the hash, and once for the Git version)
104+
def count = diff.count(HUNK)
105+
if (count == 0) {
106+
return true
107+
}
108+
109+
if (count > 2) {
110+
return false
111+
}
112+
113+
for (def i = 0; i < diff.size(); i++) {
114+
if (HUNK(diff[i])) {
115+
def change = diff[i + 1]
116+
if (!change.startsWith('From', 1) && !change.startsWith('--', 1)) {
117+
return false
118+
}
119+
}
120+
}
121+
122+
return true
123+
}
124+
113125
}

0 commit comments

Comments
 (0)