Skip to content

Commit 5870247

Browse files
authored
Merge pull request #88 from sghill/error-messaging
List Files Considered Changed When !isClean()
2 parents 46a1129 + 08c1032 commit 5870247

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package nebula.plugin.release
2+
3+
import groovy.transform.CompileStatic
4+
import org.ajoberstar.grgit.Status
5+
6+
@CompileStatic
7+
class ErrorMessageFormatter {
8+
static final String ROOT_CAUSE = 'Final and candidate builds require all changes to be committed into Git.'
9+
static final String NEW_LINE = sprintf("%n")
10+
11+
String format(Status status) {
12+
if (status.isClean()) {
13+
return ""
14+
}
15+
StringBuilder sb = new StringBuilder(ROOT_CAUSE)
16+
if (status.staged.allChanges) {
17+
sb.append(header('staged changes'))
18+
sb.append(annotate(status.staged))
19+
}
20+
if (status.unstaged.allChanges) {
21+
sb.append(header('unstaged changes'))
22+
sb.append(annotate(status.unstaged))
23+
}
24+
if(status.conflicts) {
25+
sb.append(header('conflicts'))
26+
def conflicts = status.conflicts.collect { " $it" }
27+
sb.append(conflicts.join(NEW_LINE))
28+
}
29+
return sb.toString()
30+
}
31+
32+
private static String header(String name) {
33+
[NEW_LINE, "Found $name:", NEW_LINE].join('')
34+
}
35+
36+
private static String annotate(Status.Changes changes) {
37+
def added = changes.added.collect { " [+] $it" }
38+
def modified = changes.modified.collect { " [M] $it"}
39+
def removed = changes.removed.collect { " [-] $it"}
40+
return [added, modified, removed].flatten().join(NEW_LINE)
41+
}
42+
43+
}

src/main/groovy/nebula/plugin/release/ReleasePlugin.groovy

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import nebula.core.ProjectType
2020
import org.ajoberstar.gradle.git.release.base.BaseReleasePlugin
2121
import org.ajoberstar.gradle.git.release.base.ReleasePluginExtension
2222
import org.ajoberstar.grgit.Grgit
23+
import org.ajoberstar.grgit.Status
2324
import org.eclipse.jgit.errors.RepositoryNotFoundException
2425
import org.gradle.api.GradleException
2526
import org.gradle.api.Plugin
@@ -200,10 +201,10 @@ class ReleasePlugin implements Plugin<Project> {
200201

201202
private void checkStateForStage() {
202203
if (!project.tasks.releaseCheck.isSnapshotRelease) {
203-
def status = git.status()
204-
def uncommittedChangesFound = [status.staged, status.unstaged].any { it.getAllChanges().size() > 0 }
205-
if (uncommittedChangesFound) {
206-
throw new GradleException('Final and candidate builds require all changes to be committed into Git.')
204+
Status status = git.status()
205+
if (!status.isClean()) {
206+
String message = new ErrorMessageFormatter().format(status)
207+
throw new GradleException(message)
207208
}
208209
}
209210
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package nebula.plugin.release
2+
3+
import org.ajoberstar.grgit.Status
4+
import spock.lang.Specification
5+
6+
class ErrorMessageFormatterSpec extends Specification {
7+
ErrorMessageFormatter formatter = new ErrorMessageFormatter()
8+
9+
def 'should not have a message for clean status'() {
10+
given:
11+
def status = new Status()
12+
13+
expect:
14+
status.isClean()
15+
formatter.format(status) == ""
16+
}
17+
18+
def 'should list staged files'() {
19+
given:
20+
def status = new Status(['staged':['added': ['add.txt'], 'modified': ['path/to/mod.txt', 'mod.b.txt'], 'removed': ['folder/rm.txt']]])
21+
String expected = [
22+
ErrorMessageFormatter.ROOT_CAUSE,
23+
"Found staged changes:",
24+
" [+] add.txt",
25+
" [M] path/to/mod.txt",
26+
" [M] mod.b.txt",
27+
" [-] folder/rm.txt",
28+
].join(sprintf(ErrorMessageFormatter.NEW_LINE))
29+
30+
when:
31+
String message = formatter.format(status)
32+
33+
then:
34+
!status.isClean()
35+
message == expected
36+
}
37+
38+
def 'should list unstaged files'() {
39+
given:
40+
def status = new Status(['unstaged':['added': ['z.txt'], 'modified': ['a/b/c.txt'], 'removed': ['sub/folder/file.txt', 'version.txt']]])
41+
String expected = [
42+
ErrorMessageFormatter.ROOT_CAUSE,
43+
"Found unstaged changes:",
44+
" [+] z.txt",
45+
" [M] a/b/c.txt",
46+
" [-] sub/folder/file.txt",
47+
" [-] version.txt",
48+
].join(sprintf(ErrorMessageFormatter.NEW_LINE))
49+
50+
when:
51+
String message = formatter.format(status)
52+
53+
then:
54+
!status.isClean()
55+
message == expected
56+
}
57+
58+
def 'should list conflict files'() {
59+
given:
60+
def status = new Status(['conflicts':['a.txt', 'b.java']])
61+
String expected = [
62+
ErrorMessageFormatter.ROOT_CAUSE,
63+
"Found conflicts:",
64+
" a.txt",
65+
" b.java",
66+
].join(sprintf(ErrorMessageFormatter.NEW_LINE))
67+
68+
when:
69+
String message = formatter.format(status)
70+
71+
then:
72+
!status.isClean()
73+
message == expected
74+
}
75+
}

0 commit comments

Comments
 (0)