Skip to content

Commit

Permalink
patch: Fix initIfNotExists handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ajoberstar committed Oct 5, 2024
1 parent 9269110 commit 3f40370
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<GrgitService> service
@Inject
DoStuffTask(Provider<GrgitService> 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class GrgitServicePluginMultiProjectCompatTest extends Specification {
@TempDir File tempDir
File projectDir
File settingsFile
File buildRootFile
File build1File
File build2File

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.ajoberstar.grgit.gradle;

import java.io.File;

import javax.inject.Inject;

import org.ajoberstar.grgit.Grgit;
Expand Down Expand Up @@ -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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class GrgitServicePlugin implements Plugin<Project> {
public void apply(Project project) {
Provider<GrgitService> 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);
});

Expand Down

0 comments on commit 3f40370

Please sign in to comment.