Skip to content

Commit 414faa1

Browse files
committed
minor: Allow safely checking for repo in GrgitService
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
1 parent 3f40370 commit 414faa1

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

grgit-gradle/src/compatTest/groovy/org/ajoberstar/grgit/gradle/GrgitServicePluginCompatTest.groovy

+27
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ class DoStuffTask extends DefaultTask {
4949
println service.get().grgit.describe()
5050
}
5151
}
52+
53+
tasks.register("doStuffSafe", DoStuffSafeTask, grgitService.service)
54+
55+
class DoStuffSafeTask extends DefaultTask {
56+
private final Provider<GrgitService> service
57+
58+
@Inject
59+
DoStuffSafeTask(Provider<GrgitService> service) {
60+
this.service = service
61+
usesService(service)
62+
}
63+
64+
@TaskAction
65+
void execute() {
66+
println service.get().findGrgit().map { it.describe() }.orElse(null)
67+
}
68+
}
5269
"""
5370
}
5471

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

81+
def 'with no repo, accessing service with safe method works'() {
82+
given:
83+
// nothing
84+
when:
85+
def result = build('doStuffSafe', '--quiet', '--no-configuration-cache')
86+
then:
87+
result.task(':doStuffSafe')?.outcome == TaskOutcome.SUCCESS
88+
result.output.normalize() == 'null\n'
89+
}
90+
6491
def 'with repo, plugin opens the repo as grgit'() {
6592
given:
6693
Grgit git = Grgit.init(dir: projectDir)
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.ajoberstar.grgit.gradle;
22

33
import java.io.File;
4+
import java.util.Optional;
45

56
import javax.inject.Inject;
67

@@ -9,6 +10,7 @@
910
import org.gradle.api.logging.Logger;
1011
import org.gradle.api.logging.Logging;
1112
import org.gradle.api.provider.Property;
13+
import org.gradle.api.provider.ProviderFactory;
1214
import org.gradle.api.services.BuildService;
1315
import org.gradle.api.services.BuildServiceParameters;
1416

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

26-
private final Grgit grgit;
27-
2828
@Inject
29-
public GrgitService() {
29+
public GrgitService(ProviderFactory providers) {
30+
getGrgitProperty().set(providers.provider(this::makeGrgit));
31+
getGrgitProperty().disallowChanges();
32+
getGrgitProperty().finalizeValueOnRead();
33+
}
34+
35+
protected abstract Property<Grgit> getGrgitProperty();
36+
37+
public Grgit getGrgit() {
38+
return getGrgitProperty().get();
39+
}
40+
41+
public Optional<Grgit> findGrgit() {
42+
try {
43+
return Optional.of(getGrgit());
44+
} catch (Exception e) {
45+
logger.info("Failed to make grgit service.", e);
46+
return Optional.empty();
47+
}
48+
}
49+
50+
@Override
51+
public void close() throws Exception {
52+
findGrgit().ifPresent(grgit -> {
53+
logger.info("Closing Git repo: {}", grgit.getRepository().getRootDir());
54+
grgit.close();
55+
});
56+
}
57+
58+
private Grgit makeGrgit() {
3059
if (getParameters().getCurrentDirectory().isPresent()) {
31-
this.grgit = Grgit.open(op -> {
60+
return Grgit.open(op -> {
3261
op.setCurrentDir(getParameters().getCurrentDirectory().get().getAsFile());
3362
});
34-
return;
3563
}
3664

3765
var dir = getParameters().getDirectory().get().getAsFile();
3866
var gitDir = new File(dir, ".git");
3967
if (gitDir.exists()) {
40-
this.grgit = Grgit.open(op -> {
68+
return Grgit.open(op -> {
4169
op.setDir(dir);
4270
});
4371
} else if (getParameters().getInitIfNotExists().getOrElse(false)) {
44-
this.grgit = Grgit.init(op -> {
72+
return Grgit.init(op -> {
4573
op.setDir(dir);
4674
});
4775
} else {
4876
throw new IllegalStateException("No Git repo exists at " + dir + " and initIfNotExists is false. Cannot proceed.");
4977
}
5078
}
51-
52-
public Grgit getGrgit() {
53-
return grgit;
54-
}
55-
56-
@Override
57-
public void close() throws Exception {
58-
logger.info("Closing Git repo: {}", grgit.getRepository().getRootDir());
59-
grgit.close();
60-
}
6179
}

0 commit comments

Comments
 (0)