Skip to content

Commit 9b17934

Browse files
committed
Add more ValueSource impls to ease removal of Ref providers
Fixes config cache while maintaining functionality
1 parent 2aa68bd commit 9b17934

File tree

7 files changed

+269
-84
lines changed

7 files changed

+269
-84
lines changed

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

Lines changed: 1 addition & 27 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-2024 KyoriPowered
4+
* Copyright (c) 2020-2025 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
@@ -23,9 +23,7 @@
2323
*/
2424
package net.kyori.indra.git;
2525

26-
import java.util.List;
2726
import org.eclipse.jgit.lib.ObjectId;
28-
import org.eclipse.jgit.lib.Ref;
2927
import org.gradle.api.Action;
3028
import org.gradle.api.java.archives.Manifest;
3129
import org.gradle.api.provider.Provider;
@@ -91,22 +89,6 @@ default <V, P extends RepositoryValueSource.Parameters, S extends RepositoryValu
9189
*/
9290
<V, P extends RepositoryValueSource.Parameters, S extends RepositoryValueSource<V, P>> Provider<V> repositoryValue(final Class<S> valueSource, final Action<? super ValueSourceSpec<P>> configureAction);
9391

94-
/**
95-
* Get all tags created on this repository.
96-
*
97-
* @return the tags on this repository, or an empty list if this project is not in a git repository
98-
* @since 2.0.0
99-
*/
100-
@NotNull Provider<? extends List<? extends Ref>> tags();
101-
102-
/**
103-
* Get the tag pointing to the commit checked out as {@code HEAD}.
104-
*
105-
* @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
106-
* @since 2.0.0
107-
*/
108-
@NotNull Provider<Ref> headTag();
109-
11092
/**
11193
* Get a <a href="https://git-scm.com/docs/git-describe">{@code git describe}</a> string for the project's repository.
11294
*
@@ -125,14 +107,6 @@ default <V, P extends RepositoryValueSource.Parameters, S extends RepositoryValu
125107
*/
126108
@NotNull Provider<String> branchName();
127109

128-
/**
129-
* Get an object pointing to the current branch.
130-
*
131-
* @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}.
132-
* @since 2.0.0
133-
*/
134-
@NotNull Provider<Ref> branch();
135-
136110
/**
137111
* Get the ID of the current commit.
138112
*
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This file is part of indra, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2025 KyoriPowered
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package net.kyori.indra.git;
25+
26+
import org.eclipse.jgit.api.Git;
27+
import org.jetbrains.annotations.NotNull;
28+
import org.jetbrains.annotations.Nullable;
29+
30+
/**
31+
* A {@link RepositoryValueSource} that maps an intermediary value type to a final value type.
32+
*
33+
* @param <I> intermediary (non-serializable/config-cacheable) value type
34+
* @param <V> mapped value type
35+
* @param <P> parameters type
36+
*/
37+
public abstract class MappedRepositoryValueSource<I, V, P extends RepositoryValueSource.Parameters> extends RepositoryValueSource<V, P> {
38+
@Override
39+
protected final @Nullable V obtain(final @NotNull Git repository) {
40+
final I rawValue = this.getRawValue(repository);
41+
if (rawValue == null) {
42+
return null;
43+
}
44+
return this.mapValue(repository, rawValue);
45+
}
46+
47+
protected abstract @Nullable I getRawValue(final @NotNull Git repository);
48+
49+
protected abstract @Nullable V mapValue(final @NotNull Git git, final @NotNull I value);
50+
51+
public abstract static class Parameterless<I, V> extends MappedRepositoryValueSource<I, V, Parameters> {
52+
}
53+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of indra, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2025 KyoriPowered
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package net.kyori.indra.git;
25+
26+
import java.io.IOException;
27+
import org.eclipse.jgit.api.Git;
28+
import org.eclipse.jgit.lib.Constants;
29+
import org.eclipse.jgit.lib.Ref;
30+
import org.gradle.api.logging.Logger;
31+
import org.gradle.api.logging.Logging;
32+
import org.jetbrains.annotations.NotNull;
33+
import org.jetbrains.annotations.Nullable;
34+
35+
/**
36+
* A {@link MappedRepositoryValueSource} that obtains values from the current branch,
37+
* or {@code null} if the project is not in a git repository or is checked out to a detached {@code HEAD}.
38+
*
39+
* @param <V> value type
40+
*/
41+
public abstract class QueryBranch<V> extends MappedRepositoryValueSource.Parameterless<Ref, V> {
42+
private static final Logger LOGGER = Logging.getLogger(QueryBranch.class);
43+
44+
@Override
45+
protected @Nullable Ref getRawValue(final @NotNull Git repository) {
46+
try {
47+
final @Nullable Ref ref = repository.getRepository().exactRef(Constants.HEAD);
48+
if (ref == null || !ref.isSymbolic()) return null; // no HEAD, or detached HEAD
49+
50+
return ref.getTarget();
51+
} catch(final IOException ex) {
52+
LOGGER.error("Failed to query current branch name from git:", ex);
53+
return null;
54+
}
55+
}
56+
57+
public abstract static class Name extends QueryBranch<String> {
58+
@Override
59+
protected @NotNull String mapValue(final @NotNull Git git, final @NotNull Ref ref) {
60+
return ref.getName();
61+
}
62+
}
63+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of indra, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2025 KyoriPowered
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package net.kyori.indra.git;
25+
26+
import net.kyori.indra.git.internal.IndraGitExtensionImpl;
27+
import org.eclipse.jgit.api.Git;
28+
import org.eclipse.jgit.lib.Ref;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
31+
32+
/**
33+
* A {@link MappedRepositoryValueSource} that obtains values from the tag pointing to the commit checked out as {@code HEAD},
34+
* or {@code null} if the project is not in a git repository or is not checked out to a tag
35+
*
36+
* @param <V> value type
37+
*/
38+
public abstract class QueryHeadTag<V> extends MappedRepositoryValueSource.Parameterless<Ref, V> {
39+
@Override
40+
protected @Nullable Ref getRawValue(final @NotNull Git repository) {
41+
return IndraGitExtensionImpl.headTag(repository);
42+
}
43+
44+
public abstract static class Name extends QueryHeadTag<String> {
45+
@Override
46+
protected @NotNull String mapValue(final @NotNull Git git, final @NotNull Ref ref) {
47+
return ref.getName();
48+
}
49+
}
50+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of indra, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2025 KyoriPowered
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package net.kyori.indra.git;
25+
26+
import java.util.Collections;
27+
import java.util.List;
28+
import java.util.stream.Collectors;
29+
import org.eclipse.jgit.api.Git;
30+
import org.eclipse.jgit.api.errors.GitAPIException;
31+
import org.eclipse.jgit.lib.Ref;
32+
import org.gradle.api.logging.Logger;
33+
import org.gradle.api.logging.Logging;
34+
import org.jetbrains.annotations.NotNull;
35+
36+
/**
37+
* A {@link MappedRepositoryValueSource} that queries the git repository for a list of tags.
38+
* Returns an empty list if the repository is not present or if an error occurs while querying.
39+
*
40+
* @param <V> value type
41+
*/
42+
public abstract class QueryTags<V> extends MappedRepositoryValueSource.Parameterless<List<? extends Ref>, V> {
43+
private static final Logger LOGGER = Logging.getLogger(QueryTags.class);
44+
45+
@Override
46+
protected @NotNull List<? extends Ref> getRawValue(final @NotNull Git repository) {
47+
try {
48+
return repository.tagList().call();
49+
} catch (final GitAPIException ex) {
50+
LOGGER.error("Failed to query git for a list of tags:", ex);
51+
return Collections.emptyList();
52+
}
53+
}
54+
55+
public abstract static class Names extends QueryTags<List<String>> {
56+
@Override
57+
protected @NotNull List<String> mapValue(final @NotNull Git git, final @NotNull List<? extends Ref> value) {
58+
return value.stream()
59+
.map(Ref::getName)
60+
.collect(Collectors.toList());
61+
}
62+
}
63+
}

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

Lines changed: 3 additions & 53 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-2024 KyoriPowered
4+
* Copyright (c) 2020-2025 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
@@ -25,10 +25,9 @@
2525

2626
import java.io.File;
2727
import java.io.IOException;
28-
import java.util.Collections;
29-
import java.util.List;
3028
import javax.inject.Inject;
3129
import net.kyori.indra.git.IndraGitExtension;
30+
import net.kyori.indra.git.QueryBranch;
3231
import net.kyori.indra.git.RepositoryValueSource;
3332
import net.kyori.mammoth.Configurable;
3433
import org.eclipse.jgit.api.Git;
@@ -86,23 +85,6 @@ public <V, P extends RepositoryValueSource.Parameters, S extends RepositoryValue
8685
});
8786
}
8887

89-
public static abstract class QueryTags extends RepositoryValueSource.Parameterless<List<? extends Ref>> {
90-
@Override
91-
protected @Nullable List<? extends Ref> obtain(final @NotNull Git repository) {
92-
try {
93-
return repository.tagList().call();
94-
} catch (final GitAPIException ex) {
95-
LOGGER.error("Failed to query git for a list of tags:", ex);
96-
return Collections.emptyList();
97-
}
98-
}
99-
}
100-
101-
@Override
102-
public @NotNull Provider<? extends List<? extends Ref>> tags() {
103-
return this.repositoryValue(QueryTags.class).orElse(Collections.emptyList());
104-
}
105-
10688
public static @Nullable Ref headTag(final Git git) {
10789
try {
10890
final @Nullable Ref head = git.getRepository().findRef(Constants.HEAD);
@@ -126,18 +108,6 @@ public static abstract class QueryTags extends RepositoryValueSource.Parameterle
126108
return null;
127109
}
128110

129-
public static abstract class QueryHeadTag extends RepositoryValueSource.Parameterless<Ref> {
130-
@Override
131-
protected @Nullable Ref obtain(final @NotNull Git repository) {
132-
return IndraGitExtensionImpl.headTag(repository);
133-
}
134-
}
135-
136-
@Override
137-
public @NotNull Provider<Ref> headTag() {
138-
return this.repositoryValue(QueryHeadTag.class);
139-
}
140-
141111
public static abstract class QueryDescribe extends RepositoryValueSource.Parameterless<String> {
142112
@Override
143113
protected @Nullable String obtain(final @NotNull Git repository) {
@@ -160,27 +130,7 @@ public static abstract class QueryDescribe extends RepositoryValueSource.Paramet
160130

161131
@Override
162132
public @NotNull Provider<String> branchName() {
163-
return this.branch().map(branch -> Repository.shortenRefName(branch.getName()));
164-
}
165-
166-
public static abstract class QueryBranch extends RepositoryValueSource.Parameterless<Ref> {
167-
@Override
168-
protected @Nullable Ref obtain(final @NotNull Git repository) {
169-
try {
170-
final @Nullable Ref ref = repository.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-
}
179-
}
180-
181-
@Override
182-
public @NotNull Provider<Ref> branch() {
183-
return this.repositoryValue(QueryBranch.class);
133+
return this.repositoryValue(QueryBranch.Name.class).map(Repository::shortenRefName);
184134
}
185135

186136
public static abstract class QueryCommit extends RepositoryValueSource.Parameterless<ObjectId> {

0 commit comments

Comments
 (0)