@@ -20,153 +20,89 @@ import org.eclipse.jgit.api.DescribeCommand
20
20
import org.eclipse.jgit.api.Git
21
21
import org.eclipse.jgit.internal.storage.file.FileRepository
22
22
import org.eclipse.jgit.lib.Constants
23
+ import org.eclipse.jgit.lib.ObjectId
24
+ import org.eclipse.jgit.lib.Ref
23
25
import org.gradle.api.Plugin
24
26
import org.gradle.api.Project
25
27
26
- import java.util.regex.Matcher
27
-
28
28
class GitVersionPlugin implements Plugin<Project > {
29
29
30
- // Gradle returns 'unspecified' when no version is set
31
- private static final String UNSPECIFIED_VERSION = ' unspecified'
32
30
private static final int VERSION_ABBR_LENGTH = 10
33
31
32
+ void apply (Project project ) {
33
+ project. ext. gitVersion = {
34
+ return versionDetails(project). version
35
+ }
36
+
37
+ project. ext. versionDetails = {
38
+ return versionDetails(project)
39
+ }
40
+
41
+ project. tasks. create(' printVersion' ) {
42
+ group = ' Versioning'
43
+ description = ' Prints the project\' s configured version to standard out'
44
+ doLast {
45
+ println project. version
46
+ }
47
+ }
48
+ }
49
+
34
50
@Memoized
35
- private File gitDir (Project project ) {
36
- return getRootGitDir(project. rootDir)
51
+ private VersionDetails versionDetails (Project project ) {
52
+ String description = gitDescribe(project)
53
+ String hash = gitHash(project)
54
+ String branchName = gitBranchName(project)
55
+ boolean isClean = isClean(project)
56
+
57
+ return new VersionDetails (description, hash, branchName, isClean)
37
58
}
38
59
39
60
@Memoized
40
61
private Git gitRepo (Project project ) {
41
- return Git . wrap(new FileRepository (gitDir(project)))
62
+ File gitDir = GitCli . getRootGitDir(project. rootDir);
63
+ return Git . wrap(new FileRepository (gitDir))
42
64
}
43
65
44
66
@Memoized
45
- private String gitDesc (Project project ) {
67
+ private String gitDescribe (Project project ) {
46
68
// verify that "git" command exists (throws exception if it does not)
47
- verifyGitCommandExists()
69
+ GitCli . verifyGitCommandExists()
48
70
49
71
Git git = gitRepo(project)
50
72
try {
51
73
// back-compat: the JGit "describe" command throws an exception in repositories with no commits, so call it
52
74
// first to preserve this behavior in cases where this call would fail but native "git" call does not.
53
75
new DescribeCommand (git. getRepository()). call()
54
76
55
- String version = runGitCommand(project. rootDir, " describe" , " --tags" , " --always" , " --first-parent" ) ?: UNSPECIFIED_VERSION
56
- boolean isClean = git. status(). call(). isClean()
57
- return version + (isClean ? ' ' : ' .dirty' )
77
+ return GitCli . runGitCommand(project. rootDir, " describe" , " --tags" , " --always" , " --first-parent" )
58
78
} catch (Throwable t) {
59
- return UNSPECIFIED_VERSION
79
+ return null
60
80
}
61
81
}
62
82
63
83
@Memoized
64
84
private String gitHash (Project project ) {
65
85
Git git = gitRepo(project)
66
- return git. getRepository(). getRef(" HEAD" ). getObjectId(). abbreviate(VERSION_ABBR_LENGTH ). name()
86
+ ObjectId objectId = git. getRepository(). getRef(" HEAD" ). getObjectId();
87
+ if (objectId == null ) {
88
+ return null
89
+ }
90
+ return objectId. abbreviate(VERSION_ABBR_LENGTH ). name()
67
91
}
68
92
69
93
@Memoized
70
94
private String gitBranchName (Project project ) {
71
95
Git git = gitRepo(project)
72
- def ref = git. repository . getRef(git. repository. branch)
96
+ Ref ref = git. getRepository() . getRef(git. repository. branch)
73
97
if (ref == null ) {
74
98
return null
75
- } else {
76
- return ref. getName(). substring(Constants . R_HEADS . length())
77
99
}
100
+ return ref. getName(). substring(Constants . R_HEADS . length())
78
101
}
79
102
80
- void apply (Project project ) {
81
- project. ext. gitVersion = {
82
- return gitDesc(project)
83
- }
84
-
85
- project. ext. versionDetails = {
86
- String description = gitDesc(project)
87
- if (description. equals(UNSPECIFIED_VERSION )) {
88
- return null
89
- }
90
-
91
- String hash = gitHash(project)
92
- String branchName = gitBranchName(project)
93
-
94
- if (! (description =~ / .*g.?[0-9a-fA-F]{3,}/ )) {
95
- // Description has no git hash so it is just the tag name
96
- return new VersionDetails (description, 0 , hash, branchName)
97
- }
98
-
99
- Matcher match = (description =~ / (.*)-([0-9]+)-g.?[0-9a-fA-F]{3,}/ )
100
- String tagName = match[0 ][1 ]
101
- int commitCount = Integer . valueOf(match[0 ][2 ])
102
-
103
- return new VersionDetails (tagName, commitCount, hash, branchName)
104
- }
105
-
106
- project. tasks. create(' printVersion' ) {
107
- group = ' Versioning'
108
- description = ' Prints the project\' s configured version to standard out'
109
- doLast {
110
- println project. version
111
- }
112
- }
113
- }
114
-
115
- private static File getRootGitDir (currentRoot ) {
116
- File gitDir = scanForRootGitDir(currentRoot)
117
- if (! gitDir. exists()) {
118
- throw new IllegalArgumentException (' Cannot find \' .git\' directory' )
119
- }
120
- return gitDir
121
- }
122
-
123
- private static File scanForRootGitDir (File currentRoot ) {
124
- File gitDir = new File (currentRoot, ' .git' )
125
-
126
- if (gitDir. exists()) {
127
- return gitDir
128
- }
129
-
130
- // stop at the root directory, return non-existing File object
131
- if (currentRoot. parentFile == null ) {
132
- return gitDir
133
- }
134
-
135
- // look in parent directory
136
- return scanForRootGitDir(currentRoot. parentFile)
137
- }
138
-
139
- private static void verifyGitCommandExists () {
140
- Process gitVersionProcess = new ProcessBuilder (" git" , " version" ). start()
141
- if (gitVersionProcess. waitFor() != 0 ) {
142
- throw new IllegalStateException (" error invoking git command" )
143
- }
144
- }
145
-
146
- private static String runGitCommand (File dir , String ...commands ) {
147
- List<String > cmdInput = new ArrayList<> ()
148
- cmdInput. add(" git" )
149
- cmdInput. addAll(commands)
150
- ProcessBuilder pb = new ProcessBuilder (cmdInput)
151
- pb. directory(dir)
152
- pb. redirectErrorStream(true )
153
-
154
- Process process = pb. start()
155
- BufferedReader reader = new BufferedReader (new InputStreamReader (process. getInputStream()))
156
-
157
- StringBuilder builder = new StringBuilder ()
158
- String line = null
159
- while ((line = reader. readLine()) != null ) {
160
- builder. append(line)
161
- builder. append(System . getProperty(" line.separator" ))
162
- }
163
-
164
- int exitCode = process. waitFor()
165
- if (exitCode != 0 ) {
166
- return " "
167
- }
168
-
169
- return builder. toString(). trim()
103
+ @Memoized
104
+ private boolean isClean (Project project ) {
105
+ Git git = gitRepo(project)
106
+ return git. status(). call(). isClean();
170
107
}
171
-
172
108
}
0 commit comments