File tree Expand file tree Collapse file tree 3 files changed +44
-2
lines changed
main/groovy/com/palantir/gradle/gitversion
test/groovy/com/palantir/gradle/gitversion Expand file tree Collapse file tree 3 files changed +44
-2
lines changed Original file line number Diff line number Diff line change 11build
2+ out /
23bin
34* .class
45
89.checkstyle
910.settings
1011.node
12+ * .iml
1113* .ipr
1214* .iws
1315.idea
Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ class GitVersionPlugin implements Plugin<Project> {
2727
2828 void apply (Project project ) {
2929 project. ext. gitVersion = {
30- File gitDir = new File (project. rootDir, ' .git ' )
30+ File gitDir = findRootGitDir (project. rootDir)
3131 if (! gitDir. exists()) {
3232 throw new IllegalArgumentException (' Cannot find \' .git\' directory' )
3333 }
@@ -46,5 +46,20 @@ class GitVersionPlugin implements Plugin<Project> {
4646 println project. version
4747 }
4848 }
49- }
5049
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 {
5252 buildResult. output. contains(' > Cannot find \' .git\' directory' )
5353 }
5454
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+
5580 def ' unspecified when no tags are present' () {
5681 given :
5782 buildFile << '''
You can’t perform that action at this time.
0 commit comments