Skip to content

Commit 9952054

Browse files
committed
feat(git): Redesign API to align with config cache requirements
1 parent 9e16144 commit 9952054

File tree

4 files changed

+145
-108
lines changed

4 files changed

+145
-108
lines changed

indra-git/src/main/java/net/kyori/indra/git/IndraGitExtension.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This file is part of indra, licensed under the MIT License.
33
*
4-
* Copyright (c) 2020-2022 KyoriPowered
4+
* Copyright (c) 2020-2023 KyoriPowered
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -28,8 +28,8 @@
2828
import org.eclipse.jgit.lib.ObjectId;
2929
import org.eclipse.jgit.lib.Ref;
3030
import org.gradle.api.java.archives.Manifest;
31+
import org.gradle.api.provider.Provider;
3132
import org.jetbrains.annotations.NotNull;
32-
import org.jetbrains.annotations.Nullable;
3333

3434
/**
3535
* An extension exposing git information.
@@ -58,7 +58,7 @@ public interface IndraGitExtension {
5858
* @since 2.0.0
5959
*/
6060
default boolean isPresent() {
61-
return this.git() != null;
61+
return this.git().isPresent();
6262
}
6363

6464
/**
@@ -73,23 +73,23 @@ default boolean isPresent() {
7373
* @return the git repository
7474
* @since 2.0.0
7575
*/
76-
@Nullable Git git();
76+
@NotNull Provider<Git> git();
7777

7878
/**
7979
* Get all tags created on this repository.
8080
*
8181
* @return the tags on this repository, or an empty list if this project is not in a git repository
8282
* @since 2.0.0
8383
*/
84-
@NotNull List<Ref> tags();
84+
@NotNull Provider<? extends List<? extends Ref>> tags();
8585

8686
/**
8787
* Get the tag pointing to the commit checked out as {@code HEAD}.
8888
*
8989
* @return the tag at {@code HEAD}, or {@code null} if the project is not in a git repository or is not checked out to a tag
9090
* @since 2.0.0
9191
*/
92-
@Nullable Ref headTag();
92+
@NotNull Provider<Ref> headTag();
9393

9494
/**
9595
* Get a <a href="https://git-scm.com/docs/git-describe">{@code git describe}</a> string for the project's repository.
@@ -99,31 +99,31 @@ default boolean isPresent() {
9999
* @return the describe string, or {@code null} if this project is not in a git repository or if there are no tags in the project's history
100100
* @since 2.0.0
101101
*/
102-
@Nullable String describe();
102+
@NotNull Provider<String> describe();
103103

104104
/**
105105
* Get the name of the current branch.
106106
*
107107
* @return the name of the active branch, or {@code null} if the project is not in a git repository or is checked out to a detached {@code HEAD}.
108108
* @since 2.0.0
109109
*/
110-
@Nullable String branchName();
110+
@NotNull Provider<String> branchName();
111111

112112
/**
113113
* Get an object pointing to the current branch.
114114
*
115115
* @return the active branch, or {@code null} if the project is not in a git repository or is checked out to a detached {@code HEAD}.
116116
* @since 2.0.0
117117
*/
118-
@Nullable Ref branch();
118+
@NotNull Provider<Ref> branch();
119119

120120
/**
121121
* Get the ID of the current commit.
122122
*
123123
* @return the commit id, or {@code null} if the project is not in a git repository or has not had its initial commit
124124
* @since 2.0.0
125125
*/
126-
@Nullable ObjectId commit();
126+
@NotNull Provider<ObjectId> commit();
127127

128128
/**
129129
* Apply metadata about the current git state to the provided manifest.
@@ -144,10 +144,10 @@ default boolean isPresent() {
144144
default void applyVcsInformationToManifest(final Manifest manifest) {
145145
if(this.isPresent()) {
146146
// Git-Commit and Git-Branch
147-
final @Nullable ObjectId commit = this.commit();
148-
final @Nullable String branchName = this.branchName();
149-
if(commit != null) manifest.getAttributes().put(MANIFEST_ATTRIBUTE_GIT_COMMIT, commit.name());
150-
if(branchName != null) manifest.getAttributes().put(MANIFEST_ATTRIBUTE_GIT_BRANCH, branchName);
147+
final Provider<ObjectId> commit = this.commit();
148+
final Provider<String> branchName = this.branchName();
149+
if(commit.isPresent()) manifest.getAttributes().put(MANIFEST_ATTRIBUTE_GIT_COMMIT, commit.get().name());
150+
if(branchName.isPresent()) manifest.getAttributes().put(MANIFEST_ATTRIBUTE_GIT_BRANCH, branchName.get());
151151
}
152152
}
153153
}

indra-git/src/main/java/net/kyori/indra/git/internal/IndraGitExtensionImpl.java

Lines changed: 99 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.util.Collections;
2929
import java.util.List;
30+
import java.util.function.Function;
3031
import javax.inject.Inject;
3132
import net.kyori.indra.git.IndraGitExtension;
3233
import org.eclipse.jgit.api.Git;
@@ -38,39 +39,82 @@
3839
import org.eclipse.jgit.lib.Repository;
3940
import org.eclipse.jgit.revwalk.RevObject;
4041
import org.eclipse.jgit.revwalk.RevWalk;
42+
import org.gradle.api.file.DirectoryProperty;
4143
import org.gradle.api.logging.Logger;
4244
import org.gradle.api.logging.Logging;
45+
import org.gradle.api.provider.Property;
4346
import org.gradle.api.provider.Provider;
47+
import org.gradle.api.provider.ProviderFactory;
48+
import org.gradle.api.provider.ValueSource;
49+
import org.gradle.api.provider.ValueSourceParameters;
4450
import org.jetbrains.annotations.NotNull;
4551
import org.jetbrains.annotations.Nullable;
4652

4753
public class IndraGitExtensionImpl implements IndraGitExtension {
4854
private static final Logger LOGGER = Logging.getLogger(IndraGitExtensionImpl.class);
49-
private final Provider<Git> service;
55+
private final Provider<IndraGitService> service;
56+
private final ProviderFactory providers;
57+
private final File projectDir;
58+
private final String displayName;
5059

5160
@Inject
52-
public IndraGitExtensionImpl(final File projectDir, final String displayName, final Provider<IndraGitService> service) {
53-
this.service = service.map(s -> s.git(projectDir, displayName));
61+
public IndraGitExtensionImpl(final ProviderFactory providers, final File projectDir, final String displayName, final Provider<IndraGitService> service) {
62+
this.providers = providers;
63+
this.projectDir = projectDir;
64+
this.displayName = displayName;
65+
66+
this.service = service;
5467
}
5568

56-
@Override
57-
public @Nullable Git git() {
58-
return this.service.getOrNull();
69+
@SuppressWarnings("unchecked")
70+
protected <V> Provider<V> repoQuery(final Function<Git, V> provider) {
71+
return this.providers.of(GitRepoValueSource.class, spec -> {
72+
final GitRepoValueSource.Params params = spec.getParameters();
73+
74+
params.getService().set(this.service);
75+
params.getProjectDirectory().set(this.projectDir);
76+
params.getProjectDisplayName().set(this.displayName);
77+
params.getValueProvider().set(provider);
78+
}).map(v -> (V) v);
5979
}
6080

61-
@Override
62-
public @NotNull List<Ref> tags() {
63-
final @Nullable Git git = this.git();
64-
if(git == null) return Collections.emptyList();
81+
static abstract class GitRepoValueSource implements ValueSource<Object, GitRepoValueSource.Params> {
82+
interface Params extends ValueSourceParameters {
83+
Property<IndraGitService> getService();
84+
DirectoryProperty getProjectDirectory();
85+
Property<String> getProjectDisplayName();
6586

66-
try {
67-
return git.tagList().call();
68-
} catch(final GitAPIException ex) {
69-
LOGGER.error("Failed to query git for a list of tags:", ex);
70-
return Collections.emptyList();
87+
Property<Function<Git, ?>> getValueProvider();
88+
}
89+
90+
@Nullable
91+
@Override
92+
public Object obtain() {
93+
final Params params = this.getParameters();
94+
final Git git = params.getService().get().git(params.getProjectDirectory().get().getAsFile(), params.getProjectDisplayName().get());
95+
if (git == null) return null;
96+
97+
return params.getValueProvider().get().apply(git);
7198
}
7299
}
73100

101+
@Override
102+
public @NotNull Provider<Git> git() {
103+
return this.service.map(service -> service.git(this.projectDir, this.displayName));
104+
}
105+
106+
@Override
107+
public @NotNull Provider<? extends List<? extends Ref>> tags() {
108+
return this.git().<List<Ref>>map(git -> {
109+
try {
110+
return git.tagList().call();
111+
} catch(final GitAPIException ex) {
112+
LOGGER.error("Failed to query git for a list of tags:", ex);
113+
return Collections.emptyList();
114+
}
115+
}).orElse(Collections.emptyList());
116+
}
117+
74118
public static @Nullable Ref headTag(final Git git) {
75119
try {
76120
final @Nullable Ref head = git.getRepository().findRef(Constants.HEAD);
@@ -95,66 +139,57 @@ public IndraGitExtensionImpl(final File projectDir, final String displayName, fi
95139
}
96140

97141
@Override
98-
public @Nullable Ref headTag() {
99-
final @Nullable Git git = this.git();
100-
if (git == null) return null;
101-
return headTag(git);
142+
public @NotNull Provider<Ref> headTag() {
143+
return this.git().map(IndraGitExtensionImpl::headTag);
102144
}
103145

104146
@Override
105-
public @Nullable String describe() {
106-
final @Nullable Git git = this.git();
107-
if(git == null) return null;
108-
109-
try {
110-
return git.describe().setTags(true).setLong(true).call();
111-
} catch(final RefNotFoundException ex) {
112-
// there is no HEAD when in a git repo without a commit
113-
return null;
114-
} catch(final GitAPIException ex) {
115-
LOGGER.error("Failed to query git for a 'describe' result:", ex);
116-
return null;
117-
}
147+
public @NotNull Provider<String> describe() {
148+
return this.git().map(git -> {
149+
try {
150+
return git.describe().setTags(true).setLong(true).call();
151+
} catch(final RefNotFoundException ex) {
152+
// there is no HEAD when in a git repo without a commit
153+
return null;
154+
} catch(final GitAPIException ex) {
155+
LOGGER.error("Failed to query git for a 'describe' result:", ex);
156+
return null;
157+
}
158+
});
118159
}
119160

120161
@Override
121-
public @Nullable String branchName() {
122-
final @Nullable Git git = this.git();
123-
if(git == null) return null;
124-
125-
final @Nullable Ref branch = this.branch();
126-
return branch == null ? null : Repository.shortenRefName(branch.getName());
162+
public @NotNull Provider<String> branchName() {
163+
return this.branch().map(branch -> Repository.shortenRefName(branch.getName()));
127164
}
128165

129166
@Override
130-
public @Nullable Ref branch() {
131-
final @Nullable Git git = this.git();
132-
if(git == null) return null;
133-
134-
try {
135-
final @Nullable Ref ref = git.getRepository().exactRef(Constants.HEAD);
136-
if(ref == null || !ref.isSymbolic()) return null; // no HEAD, or detached HEAD
137-
138-
return ref.getTarget();
139-
} catch(final IOException ex) {
140-
LOGGER.error("Failed to query current branch name from git:", ex);
141-
return null;
142-
}
167+
public @NotNull Provider<Ref> branch() {
168+
return this.git().map(git -> {
169+
try {
170+
final @Nullable Ref ref = git.getRepository().exactRef(Constants.HEAD);
171+
if(ref == null || !ref.isSymbolic()) return null; // no HEAD, or detached HEAD
172+
173+
return ref.getTarget();
174+
} catch(final IOException ex) {
175+
LOGGER.error("Failed to query current branch name from git:", ex);
176+
return null;
177+
}
178+
});
143179
}
144180

145181
@Override
146-
public @Nullable ObjectId commit() {
147-
final @Nullable Git git = this.git();
148-
if(git == null) return null;
149-
150-
try {
151-
final @Nullable Ref head = git.getRepository().exactRef(Constants.HEAD);
152-
if(head == null) return null;
153-
154-
return head.getObjectId();
155-
} catch(final IOException ex) {
156-
LOGGER.error("Failed to query git for the current HEAD commit:", ex);
157-
return null;
158-
}
182+
public @NotNull Provider<ObjectId> commit() {
183+
return this.git().map(git -> {
184+
try {
185+
final @Nullable Ref head = git.getRepository().exactRef(Constants.HEAD);
186+
if(head == null) return null;
187+
188+
return head.getObjectId();
189+
} catch(final IOException ex) {
190+
LOGGER.error("Failed to query git for the current HEAD commit:", ex);
191+
return null;
192+
}
193+
});
159194
}
160195
}

0 commit comments

Comments
 (0)