|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | -package com.palantir.gradle.gitversion; |
| 16 | +package com.palantir.gradle.gitversion |
| 17 | + |
| 18 | +import java.util.regex.Matcher |
17 | 19 |
|
18 | 20 | import org.eclipse.jgit.api.Git |
19 | 21 | import org.eclipse.jgit.internal.storage.file.FileRepository |
20 | 22 | import org.gradle.api.Plugin |
21 | 23 | import org.gradle.api.Project |
22 | | -import com.palantir.gradle.gitversion.VersionDetails; |
| 24 | + |
| 25 | +import com.google.common.base.Supplier |
| 26 | +import com.google.common.base.Suppliers |
23 | 27 |
|
24 | 28 | class GitVersionPlugin implements Plugin<Project> { |
25 | 29 |
|
26 | 30 | // Gradle returns 'unspecified' when no version is set |
27 | 31 | private static final String UNSPECIFIED_VERSION = 'unspecified' |
28 | 32 |
|
| 33 | + private Supplier<String> gitDesc |
| 34 | + |
29 | 35 | void apply(Project project) { |
30 | | - project.ext.gitVersion = { |
31 | | - File gitDir = getRootGitDir(project.rootDir) |
32 | | - |
33 | | - try { |
34 | | - Git git = Git.wrap(new FileRepository(gitDir)) |
35 | | - String version = git.describe().call() ?: UNSPECIFIED_VERSION |
36 | | - boolean isClean = git.status().call().isClean() |
37 | | - return version + (isClean ? '' : '.dirty') |
38 | | - } catch (Throwable t) { |
39 | | - return UNSPECIFIED_VERSION |
| 36 | + gitDesc = Suppliers.memoize(new Supplier<String>() { |
| 37 | + @Override |
| 38 | + public String get() { |
| 39 | + File gitDir = getRootGitDir(project.rootDir) |
| 40 | + try { |
| 41 | + Git git = Git.wrap(new FileRepository(gitDir)) |
| 42 | + String version = git.describe().call() ?: UNSPECIFIED_VERSION |
| 43 | + boolean isClean = git.status().call().isClean() |
| 44 | + return version + (isClean ? '' : '.dirty') |
| 45 | + } catch (Throwable t) { |
| 46 | + return UNSPECIFIED_VERSION |
| 47 | + } |
40 | 48 | } |
| 49 | + }) |
| 50 | + |
| 51 | + project.ext.gitVersion = { |
| 52 | + return gitDesc.get() |
41 | 53 | } |
42 | 54 |
|
43 | 55 | project.ext.versionDetails = { |
44 | | - File gitDir = getRootGitDir(project.rootDir) |
| 56 | + String description = gitDesc.get() |
45 | 57 |
|
46 | | - try { |
47 | | - Git git = Git.wrap(new FileRepository(gitDir)) |
48 | | - String description = git.describe().call(); |
| 58 | + if (description.equals(UNSPECIFIED_VERSION)) { |
| 59 | + return null |
| 60 | + } |
49 | 61 |
|
| 62 | + if (!(description =~ /.*g.?[0-9a-fA-F]{3,}/)) { |
50 | 63 | // Description has no git hash so it is just the tag name |
51 | | - if(!(description =~ /.*g.?[0-9a-fA-F]{3,}/)) { |
52 | | - return new VersionDetails(description, 0); |
53 | | - } |
54 | | - def match = (description =~ /(.*)-([0-9]+)-g.?[0-9a-fA-F]{3,}/) |
55 | | - String tagName = match[0][1] |
56 | | - int commitCount = match[0][2].toInteger() |
57 | | - |
58 | | - return new VersionDetails(tagName, commitCount) |
59 | | - } catch (Throwable t) { |
60 | | - return null |
| 64 | + return new VersionDetails(description, 0) |
61 | 65 | } |
| 66 | + |
| 67 | + Matcher match = (description =~ /(.*)-([0-9]+)-g.?[0-9a-fA-F]{3,}/) |
| 68 | + String tagName = match[0][1] |
| 69 | + int commitCount = Integer.valueOf(match[0][2]) |
| 70 | + |
| 71 | + return new VersionDetails(tagName, commitCount) |
62 | 72 | } |
63 | 73 |
|
64 | 74 | project.tasks.create('printVersion') << { |
65 | 75 | println project.version |
66 | 76 | } |
67 | 77 | } |
68 | 78 |
|
69 | | - private File getRootGitDir(currentRoot) { |
| 79 | + private static File getRootGitDir(currentRoot) { |
70 | 80 | File gitDir = scanForRootGitDir(currentRoot) |
71 | 81 | if (!gitDir.exists()) { |
72 | 82 | throw new IllegalArgumentException('Cannot find \'.git\' directory') |
73 | 83 | } |
74 | 84 | return gitDir |
75 | 85 | } |
76 | 86 |
|
77 | | - private File scanForRootGitDir(File currentRoot) { |
| 87 | + private static File scanForRootGitDir(File currentRoot) { |
78 | 88 | File gitDir = new File(currentRoot, '.git') |
79 | 89 |
|
80 | 90 | if (gitDir.exists()) { |
|
0 commit comments