File tree 3 files changed +44
-2
lines changed
main/groovy/com/palantir/gradle/gitversion
test/groovy/com/palantir/gradle/gitversion
3 files changed +44
-2
lines changed Original file line number Diff line number Diff line change 1
1
build
2
+ out /
2
3
bin
3
4
* .class
4
5
8
9
.checkstyle
9
10
.settings
10
11
.node
12
+ * .iml
11
13
* .ipr
12
14
* .iws
13
15
.idea
Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ class GitVersionPlugin implements Plugin<Project> {
27
27
28
28
void apply (Project project ) {
29
29
project. ext. gitVersion = {
30
- File gitDir = new File (project. rootDir, ' .git ' )
30
+ File gitDir = findRootGitDir (project. rootDir)
31
31
if (! gitDir. exists()) {
32
32
throw new IllegalArgumentException (' Cannot find \' .git\' directory' )
33
33
}
@@ -46,5 +46,20 @@ class GitVersionPlugin implements Plugin<Project> {
46
46
println project. version
47
47
}
48
48
}
49
- }
50
49
50
+ private File findRootGitDir (File currentRoot ) {
51
+ File gitDir = new File (currentRoot, ' .git' )
52
+
53
+ if (gitDir. exists()) {
54
+ return gitDir
55
+ }
56
+
57
+ // stop at the root directory, return non-existing File object
58
+ if (currentRoot. parentFile == null ) {
59
+ return gitDir
60
+ }
61
+
62
+ // look in parent directory
63
+ return findRootGitDir(currentRoot. parentFile)
64
+ }
65
+ }
Original file line number Diff line number Diff line change @@ -52,6 +52,31 @@ class GitVersionPluginTests extends Specification {
52
52
buildResult. output. contains(' > Cannot find \' .git\' directory' )
53
53
}
54
54
55
+ def ' git describe works when git repo is multiple levels up' () {
56
+ given :
57
+ File rootFolder = temporaryFolder. root
58
+ projectDir = temporaryFolder. newFolder(' level1' , ' level2' )
59
+ buildFile = temporaryFolder. newFile(' level1/level2/build.gradle' )
60
+ buildFile << '''
61
+ plugins {
62
+ id 'com.palantir.git-version'
63
+ }
64
+ version gitVersion()
65
+ ''' . stripIndent()
66
+ gitIgnoreFile << ' build'
67
+ Git git = Git . init(). setDirectory(rootFolder). call();
68
+ git. add(). addFilepattern(' .' ). call()
69
+ git. commit(). setMessage(' initial commit' ). call()
70
+ git. tag(). setAnnotated(true ). setMessage(' 1.0.0' ). setName(' 1.0.0' ). call()
71
+
72
+ when :
73
+ // will build the project at projectDir
74
+ BuildResult buildResult = with(' printVersion' ). build()
75
+
76
+ then :
77
+ buildResult. output. contains(" :printVersion\n 1.0.0\n " )
78
+ }
79
+
55
80
def ' unspecified when no tags are present' () {
56
81
given :
57
82
buildFile << '''
You can’t perform that action at this time.
0 commit comments