Skip to content

Commit

Permalink
minor: Allow safely checking for repo in GrgitService
Browse files Browse the repository at this point in the history
Service creation throws if the repo does not exist. Instead we should
defer this to when you get the Grgit instance from the service.
Additionally, we add a findGrgit() that catches exceptions so a user can
safely check if a repo is present for that service.

Fixes #362
  • Loading branch information
ajoberstar committed Oct 5, 2024
1 parent 3f40370 commit 414faa1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ class DoStuffTask extends DefaultTask {
println service.get().grgit.describe()
}
}
tasks.register("doStuffSafe", DoStuffSafeTask, grgitService.service)
class DoStuffSafeTask extends DefaultTask {
private final Provider<GrgitService> service
@Inject
DoStuffSafeTask(Provider<GrgitService> service) {
this.service = service
usesService(service)
}
@TaskAction
void execute() {
println service.get().findGrgit().map { it.describe() }.orElse(null)
}
}
"""
}

Expand All @@ -61,6 +78,16 @@ class DoStuffTask extends DefaultTask {
result.task(':doStuff').outcome == TaskOutcome.FAILED
}

def 'with no repo, accessing service with safe method works'() {
given:
// nothing
when:
def result = build('doStuffSafe', '--quiet', '--no-configuration-cache')
then:
result.task(':doStuffSafe')?.outcome == TaskOutcome.SUCCESS
result.output.normalize() == 'null\n'
}

def 'with repo, plugin opens the repo as grgit'() {
given:
Grgit git = Grgit.init(dir: projectDir)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.ajoberstar.grgit.gradle;

import java.io.File;
import java.util.Optional;

import javax.inject.Inject;

Expand All @@ -9,6 +10,7 @@
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.services.BuildService;
import org.gradle.api.services.BuildServiceParameters;

Expand All @@ -23,39 +25,55 @@ public interface Params extends BuildServiceParameters {
Property<Boolean> getInitIfNotExists();
}

private final Grgit grgit;

@Inject
public GrgitService() {
public GrgitService(ProviderFactory providers) {
getGrgitProperty().set(providers.provider(this::makeGrgit));
getGrgitProperty().disallowChanges();
getGrgitProperty().finalizeValueOnRead();
}

protected abstract Property<Grgit> getGrgitProperty();

public Grgit getGrgit() {
return getGrgitProperty().get();
}

public Optional<Grgit> findGrgit() {
try {
return Optional.of(getGrgit());
} catch (Exception e) {
logger.info("Failed to make grgit service.", e);
return Optional.empty();
}
}

@Override
public void close() throws Exception {
findGrgit().ifPresent(grgit -> {
logger.info("Closing Git repo: {}", grgit.getRepository().getRootDir());
grgit.close();
});
}

private Grgit makeGrgit() {
if (getParameters().getCurrentDirectory().isPresent()) {
this.grgit = Grgit.open(op -> {
return Grgit.open(op -> {
op.setCurrentDir(getParameters().getCurrentDirectory().get().getAsFile());
});
return;
}

var dir = getParameters().getDirectory().get().getAsFile();
var gitDir = new File(dir, ".git");
if (gitDir.exists()) {
this.grgit = Grgit.open(op -> {
return Grgit.open(op -> {
op.setDir(dir);
});
} else if (getParameters().getInitIfNotExists().getOrElse(false)) {
this.grgit = Grgit.init(op -> {
return Grgit.init(op -> {
op.setDir(dir);
});
} else {
throw new IllegalStateException("No Git repo exists at " + dir + " and initIfNotExists is false. Cannot proceed.");
}
}

public Grgit getGrgit() {
return grgit;
}

@Override
public void close() throws Exception {
logger.info("Closing Git repo: {}", grgit.getRepository().getRootDir());
grgit.close();
}
}

0 comments on commit 414faa1

Please sign in to comment.