Skip to content

Commit 12f425a

Browse files
committed
Merge pull request #29 from palantir/feature/memoize
Memoize git describe and status calls
2 parents 245dc15 + 96be9d4 commit 12f425a

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ repositories {
2727
dependencies {
2828
compile gradleApi()
2929
compile 'org.eclipse.jgit:org.eclipse.jgit:4.1.1.201511131810-r'
30+
compile 'com.google.guava:guava:19.0'
3031

3132
testCompile gradleTestKit()
3233
testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {

src/main/groovy/com/palantir/gradle/gitversion/GitVersionPlugin.groovy

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,78 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.palantir.gradle.gitversion;
16+
package com.palantir.gradle.gitversion
17+
18+
import java.util.regex.Matcher
1719

1820
import org.eclipse.jgit.api.Git
1921
import org.eclipse.jgit.internal.storage.file.FileRepository
2022
import org.gradle.api.Plugin
2123
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
2327

2428
class GitVersionPlugin implements Plugin<Project> {
2529

2630
// Gradle returns 'unspecified' when no version is set
2731
private static final String UNSPECIFIED_VERSION = 'unspecified'
2832

33+
private Supplier<String> gitDesc
34+
2935
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+
}
4048
}
49+
})
50+
51+
project.ext.gitVersion = {
52+
return gitDesc.get()
4153
}
4254

4355
project.ext.versionDetails = {
44-
File gitDir = getRootGitDir(project.rootDir)
56+
String description = gitDesc.get()
4557

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+
}
4961

62+
if (!(description =~ /.*g.?[0-9a-fA-F]{3,}/)) {
5063
// 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)
6165
}
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)
6272
}
6373

6474
project.tasks.create('printVersion') << {
6575
println project.version
6676
}
6777
}
6878

69-
private File getRootGitDir(currentRoot) {
79+
private static File getRootGitDir(currentRoot) {
7080
File gitDir = scanForRootGitDir(currentRoot)
7181
if (!gitDir.exists()) {
7282
throw new IllegalArgumentException('Cannot find \'.git\' directory')
7383
}
7484
return gitDir
7585
}
7686

77-
private File scanForRootGitDir(File currentRoot) {
87+
private static File scanForRootGitDir(File currentRoot) {
7888
File gitDir = new File(currentRoot, '.git')
7989

8090
if (gitDir.exists()) {

0 commit comments

Comments
 (0)