Skip to content

Commit

Permalink
[SVN] r3291 Gradle Plugin in /Libraries/twister-plugin-gradle/
Browse files Browse the repository at this point in the history
[FIX] Keep it fast, and fail when malformed.
  • Loading branch information
TWiStErRob committed Jul 11, 2021
1 parent 910f8f9 commit 46d3e11
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,24 @@ open class GITPluginExtension : VCSExtension {
}

override val isAvailableQuick: Boolean
get() {
// Check more than just the presence of .git to lessen the possibility of detecting "git",
// but not actually having a git repository.
val gitDir = RepositoryCache.FileKey.resolve(project.rootDir, FS.DETECTED)
return gitDir != null
}
get() = project.rootDir.resolve(".git").exists()

// 'git describe --always'.execute([], project.rootDir).waitFor() == 0
override val isAvailable: Boolean
get() = try {
open().close()
true
} catch (_: FileNotFoundException) {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=572617
false
} catch (_: RepositoryNotFoundException) {
false
get() {
// Check more than just the presence of .git to lessen the possibility of detecting "git",
// but not actually having a git repository.
RepositoryCache.FileKey.resolve(project.rootDir, FS.DETECTED) ?: return false
return try {
// Actually try to open the repository now.
open().close()
true
} catch (_: FileNotFoundException) {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=572617
false
} catch (_: RepositoryNotFoundException) {
false
}
}

// 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ class GITPluginIntgTest : BaseIntgTest() {
result.assertHasOutputLine("""GIT revision: 2""")
}

@Test fun `git works malformed git directory`() {
@Test fun `fails with malformed git directory`() {
gradle.root.resolve(".git").mkdir()
@Language("gradle")
val script = """
apply plugin: 'net.twisterrob.vcs'
println("VCS.current: " + project.VCS.current)
println("GIT revision: " + project.VCS.current.revision)
""".trimIndent()

val result = gradle.run(script).withDebug(true).build()
val result = gradle.run(script).buildAndFail()

result.assertHasOutputLine("""VCS.current: ${DummyVcsExtension::class.qualifiedName}@[a-z0-9]{1,8}""".toRegex())
result.assertHasOutputLine("""VCS.current: extension '${GITPluginExtension.NAME}'""".toRegex())
result.assertHasOutputLine("""> repository not found: \Q${gradle.root.absolutePath}\E""".toRegex())
}

/**
Expand Down

0 comments on commit 46d3e11

Please sign in to comment.