Skip to content

Commit b6778d5

Browse files
committed
Merge pull request #24 from jmcampanini/find-git-root
add functionality to search parent dirs for git repo
2 parents 5f46273 + e9a2a82 commit b6778d5

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
build
2+
out/
23
bin
34
*.class
45

@@ -8,6 +9,7 @@ bin
89
.checkstyle
910
.settings
1011
.node
12+
*.iml
1113
*.ipr
1214
*.iws
1315
.idea

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

+17-2
Original file line numberDiff line numberDiff 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+
}

src/test/groovy/com/palantir/gradle/gitversion/GitVersionPluginTests.groovy

+25
Original file line numberDiff line numberDiff 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\n1.0.0\n")
78+
}
79+
5580
def 'unspecified when no tags are present' () {
5681
given:
5782
buildFile << '''

0 commit comments

Comments
 (0)