From 3f403706fb84cffed637e7d161b5cff6e3ed6b53 Mon Sep 17 00:00:00 2001 From: Andrew Oberstar Date: Sat, 5 Oct 2024 13:32:09 -0500 Subject: [PATCH] patch: Fix initIfNotExists handling As reported by @Vampire, initIfNotExists didn't correctly detect there was no repo and initialize. It needed to look for presence of the .git directory. This is not handling bare repos. Fixes #391 --- .../gradle/GrgitServiceCompatTest.groovy | 127 ++++++++++++++++++ ...ServicePluginMultiProjectCompatTest.groovy | 1 - .../ajoberstar/grgit/gradle/GrgitService.java | 7 +- .../grgit/gradle/GrgitServicePlugin.java | 1 - 4 files changed, 132 insertions(+), 4 deletions(-) create mode 100644 grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServiceCompatTest.groovy diff --git a/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServiceCompatTest.groovy b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServiceCompatTest.groovy new file mode 100644 index 00000000..c27bca82 --- /dev/null +++ b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServiceCompatTest.groovy @@ -0,0 +1,127 @@ +package org.ajoberstar.grgit.gradle + +import org.ajoberstar.grgit.Grgit +import org.gradle.testkit.runner.BuildResult +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome +import spock.lang.Specification +import spock.lang.TempDir + +class GrgitServiceCompatTest extends Specification { + @TempDir File tempDir + File projectDir + File settingsFile + File buildFile + + def setup() { + projectDir = new File(tempDir, 'project') + settingsFile = projectFile('settings.gradle') + settingsFile << '''\ +pluginManagement { + repositories { + mavenCentral() + mavenLocal() + } +} +''' + buildFile = projectFile('build.gradle') + buildFile << """\ +import org.ajoberstar.grgit.gradle.GrgitService +import org.eclipse.jgit.api.errors.RefNotFoundException + +plugins { + id 'org.ajoberstar.grgit.service' version '${System.properties['compat.plugin.version']}' apply false +} + +def customService = gradle.sharedServices.registerIfAbsent('customGrgit', GrgitService.class) { + parameters { + directory = layout.projectDirectory + initIfNotExists = true + } +} + +tasks.register("doStuff", DoStuffTask, customService) + +class DoStuffTask extends DefaultTask { + private final Provider service + + @Inject + DoStuffTask(Provider service) { + this.service = service + usesService(service) + } + + @TaskAction + void execute() { + try { + println service.get().grgit.describe() + } catch (RefNotFoundException e) { + println 'null' + } + } +} +""" + } + + def 'with no repo but initIfNotExists true, accessing service works'() { + given: + // nothing + when: + def result = build('doStuff', '--quiet', '--no-configuration-cache') + then: + result.task(':doStuff')?.outcome == TaskOutcome.SUCCESS + result.output.normalize() == 'null\n' + } + + def 'with repo, plugin opens the repo as grgit'() { + given: + Grgit git = Grgit.init(dir: projectDir) + projectFile('1.txt') << '1' + git.add(patterns: ['1.txt']) + git.commit(message: 'yay') + git.tag.add(name: '1.0.0') + when: + def result = build('doStuff', '--quiet', '--no-configuration-cache') + then: + result.task(':doStuff')?.outcome == TaskOutcome.SUCCESS + result.output.normalize() == '1.0.0\n' + } + + def 'with repo, plugin closes the repo after build is finished'() { + given: + Grgit git = Grgit.init(dir: projectDir) + projectFile('1.txt') << '1' + git.add(patterns: ['1.txt']) + git.commit(message: 'yay') + git.tag.add(name: '1.0.0') + when: + def result = build('doStuff', '--info', '--no-configuration-cache') + then: + result.task(':doStuff')?.outcome == TaskOutcome.SUCCESS + result.output.contains('Closing Git repo') + } + + private BuildResult build(String... args) { + return GradleRunner.create() + .withGradleVersion(System.properties['compat.gradle.version']) + .withProjectDir(projectDir) + .forwardOutput() + .withArguments((args + '--stacktrace') as String[]) + .build() + } + + private BuildResult buildAndFail(String... args) { + return GradleRunner.create() + .withGradleVersion(System.properties['compat.gradle.version']) + .withProjectDir(projectDir) + .forwardOutput() + .withArguments((args + '--stacktrace') as String[]) + .buildAndFail() + } + + private File projectFile(String path) { + File file = new File(projectDir, path) + file.parentFile.mkdirs() + return file + } +} diff --git a/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginMultiProjectCompatTest.groovy b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginMultiProjectCompatTest.groovy index c9984ccb..94c888c9 100644 --- a/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginMultiProjectCompatTest.groovy +++ b/grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginMultiProjectCompatTest.groovy @@ -12,7 +12,6 @@ class GrgitServicePluginMultiProjectCompatTest extends Specification { @TempDir File tempDir File projectDir File settingsFile - File buildRootFile File build1File File build2File diff --git a/grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitService.java b/grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitService.java index 1d3d8ff7..06c7034a 100644 --- a/grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitService.java +++ b/grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitService.java @@ -1,5 +1,7 @@ package org.ajoberstar.grgit.gradle; +import java.io.File; + import javax.inject.Inject; import org.ajoberstar.grgit.Grgit; @@ -33,11 +35,12 @@ public GrgitService() { } var dir = getParameters().getDirectory().get().getAsFile(); - if (dir.exists()) { + var gitDir = new File(dir, ".git"); + if (gitDir.exists()) { this.grgit = Grgit.open(op -> { op.setDir(dir); }); - } else if (getParameters().getInitIfNotExists().get()) { + } else if (getParameters().getInitIfNotExists().getOrElse(false)) { this.grgit = Grgit.init(op -> { op.setDir(dir); }); diff --git a/grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitServicePlugin.java b/grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitServicePlugin.java index bb0fac2b..b82b51b1 100644 --- a/grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitServicePlugin.java +++ b/grgit-gradle/src/main/java/org/ajoberstar/grgit/gradle/GrgitServicePlugin.java @@ -9,7 +9,6 @@ public class GrgitServicePlugin implements Plugin { public void apply(Project project) { Provider serviceProvider = project.getGradle().getSharedServices().registerIfAbsent("grgit", GrgitService.class, spec -> { spec.getParameters().getCurrentDirectory().set(project.getLayout().getProjectDirectory()); - spec.getParameters().getInitIfNotExists().set(false); spec.getMaxParallelUsages().set(1); });