Skip to content

Commit 8c6f1c8

Browse files
authored
Merge pull request #197 from KyoriPowered/fix/git-config-cache-jmp
Add more ValueSource impls to ease removal of `Ref` providers
2 parents 4d9fe23 + c966b30 commit 8c6f1c8

File tree

15 files changed

+356
-89
lines changed

15 files changed

+356
-89
lines changed

indra-common/src/test/resources/net/kyori/indra/javac8Build/in/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ plugins {
22
id 'net.kyori.indra'
33
}
44

5-
group 'com.example'
6-
version '1.0.0-SNAPSHOT'
5+
group = 'com.example'
6+
version = '1.0.0-SNAPSHOT'
77

88
repositories {
99
sonatype.snapshots()

indra-common/src/test/resources/net/kyori/indra/simpleBuild/in/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ plugins {
22
id 'net.kyori.indra'
33
}
44

5-
group 'com.example'
6-
version '1.0.0-SNAPSHOT'
5+
group = 'com.example'
6+
version = '1.0.0-SNAPSHOT'
77

88
repositories {
99
sonatype.snapshots()

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

Lines changed: 8 additions & 25 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,7 +25,6 @@
2525

2626
import java.util.List;
2727
import org.eclipse.jgit.lib.ObjectId;
28-
import org.eclipse.jgit.lib.Ref;
2928
import org.gradle.api.Action;
3029
import org.gradle.api.java.archives.Manifest;
3130
import org.gradle.api.provider.Provider;
@@ -91,22 +90,6 @@ default <V, P extends RepositoryValueSource.Parameters, S extends RepositoryValu
9190
*/
9291
<V, P extends RepositoryValueSource.Parameters, S extends RepositoryValueSource<V, P>> Provider<V> repositoryValue(final Class<S> valueSource, final Action<? super ValueSourceSpec<P>> configureAction);
9392

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-
11093
/**
11194
* Get a <a href="https://git-scm.com/docs/git-describe">{@code git describe}</a> string for the project's repository.
11295
*
@@ -118,20 +101,20 @@ default <V, P extends RepositoryValueSource.Parameters, S extends RepositoryValu
118101
@NotNull Provider<String> describe();
119102

120103
/**
121-
* Get the name of the current branch.
104+
* Get the names of all tags in the repository.
122105
*
123-
* @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}.
124-
* @since 2.0.0
106+
* @return tag names, or an empty list if the project is not in a git repository or has no tags
107+
* @since 4.0.0
125108
*/
126-
@NotNull Provider<String> branchName();
109+
@NotNull Provider<List<String>> tagNames();
127110

128111
/**
129-
* Get an object pointing to the current branch.
112+
* Get the name of the current branch.
130113
*
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}.
114+
* @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}.
132115
* @since 2.0.0
133116
*/
134-
@NotNull Provider<Ref> branch();
117+
@NotNull Provider<String> branchName();
135118

136119
/**
137120
* Get the ID of the current commit.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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> the intermediary (non-serializable/config-cacheable) value type
34+
* @param <V> the mapped value type
35+
* @param <P> the parameters type
36+
* @since 4.0.0
37+
*/
38+
public abstract class MappedRepositoryValueSource<I, V, P extends RepositoryValueSource.Parameters> extends RepositoryValueSource<V, P> {
39+
@Override
40+
protected final @Nullable V obtain(final @NotNull Git repository) {
41+
final I rawValue = this.getRawValue(repository);
42+
if (rawValue == null) {
43+
return null;
44+
}
45+
return this.mapValue(repository, rawValue);
46+
}
47+
48+
/**
49+
* Obtains the raw value from the repository.
50+
*
51+
* @param repository the git repository
52+
* @return the raw value, or {@code null}
53+
*/
54+
protected abstract @Nullable I getRawValue(final @NotNull Git repository);
55+
56+
/**
57+
* Maps the raw value to the final value type.
58+
*
59+
* @param git the git repository
60+
* @param value the raw value
61+
* @return the mapped value, or {@code null}
62+
*/
63+
protected abstract @Nullable V mapValue(final @NotNull Git git, final @NotNull I value);
64+
65+
/**
66+
* A {@link MappedRepositoryValueSource} that does not require any additional parameters.
67+
*
68+
* @param <I> the intermediary (non-serializable/config-cacheable) value type
69+
* @param <V> the mapped value type
70+
*/
71+
public abstract static class Parameterless<I, V> extends MappedRepositoryValueSource<I, V, Parameters> {
72+
}
73+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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> the 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+
/**
58+
* Queries the {@link Ref#getName() name} of the current branch ref.
59+
*/
60+
public abstract static class Name extends QueryBranch<String> {
61+
@Override
62+
protected @NotNull String mapValue(final @NotNull Git git, final @NotNull Ref ref) {
63+
return ref.getName();
64+
}
65+
}
66+
}
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 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> the 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+
/**
45+
* Queries the {@link Ref#getName() name} of the head tag ref.
46+
*/
47+
public abstract static class Name extends QueryHeadTag<String> {
48+
@Override
49+
protected @NotNull String mapValue(final @NotNull Git git, final @NotNull Ref ref) {
50+
return ref.getName();
51+
}
52+
}
53+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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> the 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+
/**
56+
* Queries the {@link Ref#getName() names} of the tags.
57+
*/
58+
public abstract static class Names extends QueryTags<List<String>> {
59+
@Override
60+
protected @NotNull List<String> mapValue(final @NotNull Git git, final @NotNull List<? extends Ref> value) {
61+
return value.stream()
62+
.map(Ref::getName)
63+
.collect(Collectors.toList());
64+
}
65+
}
66+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
* <p>Implementations must be abstract, and only implement {@link #obtain(Git)}.</p>
4343
*
4444
* @param <V> the value type
45-
* @param <P> the parameter type
45+
* @param <P> the parameters type
4646
* @since 4.0.0
4747
*/
4848
public abstract class RepositoryValueSource<V, P extends RepositoryValueSource.Parameters> implements ValueSource<V, P> {

0 commit comments

Comments
 (0)