Skip to content

Commit 52fb6eb

Browse files
authored
Inline the gitinfo plugin from carrotsearch's buildinfra, use dependencychecks and build options directly. (#14794)
1 parent 75d2d4c commit 52fb6eb

File tree

9 files changed

+152
-121
lines changed

9 files changed

+152
-121
lines changed

CHANGES-record.md

Lines changed: 0 additions & 112 deletions
This file was deleted.

build-tools/build-infra-shadow/build.gradle

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ repositories {
2626
gradlePluginPortal()
2727
}
2828

29+
// Convert a plugin dependency to a regular dependency so that we can
30+
// use [plugins] section in the top-level toml but declare regular
31+
// project dependencies here.
2932
static Provider<String> plugin(Provider<PluginDependency> plugin) {
3033
return plugin.map {
31-
if (it.pluginId == "com.carrotsearch.gradle.buildinfra") {
32-
return "${it.pluginId}:${it.pluginId}.gradle.plugin:${it.version}".toString()
33-
} else if (it.pluginId == "de.thetaphi.forbiddenapis") {
34+
if (it.pluginId == "de.thetaphi.forbiddenapis") {
35+
// Uwe's forbiddenapis is on Maven Central, directly.
3436
return "de.thetaphi:forbiddenapis:${it.version}".toString()
3537
} else {
36-
return "${it.pluginId}:${it.pluginId.replaceFirst(".*\\.", "")}-plugin-gradle:${it.version}".toString()
38+
// maven artifact pattern for gradle's plugin repositories.
39+
return "${it.pluginId}:${it.pluginId}.gradle.plugin:${it.version}".toString()
40+
// return "${it.pluginId}:${it.pluginId.replaceFirst(".*\\.", "")}-plugin-gradle:${it.version}".toString()
3741
}
3842
}
3943
}
@@ -44,7 +48,8 @@ dependencies {
4448
implementation deps.commons.codec
4549
implementation deps.randomizedtesting.runner
4650

47-
implementation plugin(deps.plugins.carrotsearch.buildinfra)
51+
implementation plugin(deps.plugins.carrotsearch.buildopts)
52+
implementation plugin(deps.plugins.carrotsearch.dependencychecks)
4853
implementation plugin(deps.plugins.forbiddenapis)
4954
implementation plugin(deps.plugins.spotless)
5055
}

build-tools/build-infra/build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,15 @@ dependencies {
6969
implementation deps.flexmark.ext.autolink
7070
implementation deps.flexmark.ext.tables
7171

72-
implementation plugin(deps.plugins.carrotsearch.buildinfra)
72+
implementation plugin(deps.plugins.carrotsearch.buildopts)
73+
implementation plugin(deps.plugins.carrotsearch.dependencychecks)
7374
implementation plugin(deps.plugins.forbiddenapis)
7475
implementation plugin(deps.plugins.spotless)
7576
implementation plugin(deps.plugins.owasp.dependencycheck)
7677
implementation plugin(deps.plugins.undercouch.download)
7778
implementation plugin(deps.plugins.errorprone)
7879
implementation plugin(deps.plugins.jacocolog)
80+
implementation plugin(deps.plugins.versionCatalogUpdate)
7981
}
8082

8183
def hasJavaFlightRecorder = ModuleLayer.boot().findModule('jdk.jfr').map{ otherModule ->

build-tools/build-infra/src/main/groovy/lucene.java.jar-manifest.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import com.carrotsearch.gradle.buildinfra.environment.GitInfoExtension
18+
import org.apache.lucene.gradle.plugins.gitinfo.GitInfoExtension
1919

2020
// Configures artifact JARs (manifest entries)
2121

build-tools/build-infra/src/main/groovy/lucene.root-project.setup.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import com.carrotsearch.gradle.buildinfra.buildoptions.BuildOptionValueSource
1919
import com.carrotsearch.gradle.buildinfra.buildoptions.BuildOptionsPlugin
20-
import com.carrotsearch.gradle.buildinfra.environment.GitInfoPlugin
20+
import org.apache.lucene.gradle.plugins.gitinfo.GitInfoPlugin
2121
import com.carrotsearch.randomizedtesting.SeedUtils
2222

2323
import java.time.ZonedDateTime
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.apache.lucene.gradle.plugins.gitinfo;
2+
3+
import org.gradle.api.provider.MapProperty;
4+
5+
public abstract class GitInfoExtension {
6+
public static final String NAME = "gitinfo";
7+
8+
public abstract MapProperty<String, String> getGitInfo();
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.apache.lucene.gradle.plugins.gitinfo;
2+
3+
import org.gradle.api.GradleException;
4+
import org.gradle.api.Plugin;
5+
import org.gradle.api.Project;
6+
7+
public class GitInfoPlugin implements Plugin<Project> {
8+
@Override
9+
public void apply(Project project) {
10+
if (project != project.getRootProject()) {
11+
throw new GradleException("This plugin is applicable to the rootProject only.");
12+
}
13+
14+
var gitInfoProvider =
15+
project
16+
.getProviders()
17+
.of(
18+
GitInfoValueSource.class,
19+
spec -> {
20+
spec.getParameters().getRootProjectDir().set(project.getProjectDir());
21+
});
22+
23+
var gitInfoExtension =
24+
project.getExtensions().create(GitInfoExtension.NAME, GitInfoExtension.class);
25+
26+
gitInfoExtension.getGitInfo().value(gitInfoProvider).finalizeValueOnRead();
27+
}
28+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.apache.lucene.gradle.plugins.gitinfo;
2+
3+
import java.io.BufferedOutputStream;
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.IOException;
6+
import java.nio.charset.StandardCharsets;
7+
import java.util.Map;
8+
import java.util.TreeMap;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
11+
import javax.inject.Inject;
12+
import org.apache.tools.ant.taskdefs.condition.Os;
13+
import org.gradle.api.GradleException;
14+
import org.gradle.api.file.DirectoryProperty;
15+
import org.gradle.api.provider.ValueSource;
16+
import org.gradle.api.provider.ValueSourceParameters;
17+
import org.gradle.api.services.BuildServiceParameters;
18+
import org.gradle.process.ExecOperations;
19+
import org.jetbrains.annotations.Nullable;
20+
21+
public abstract class GitInfoValueSource
22+
implements ValueSource<Map<String, String>, GitInfoValueSource.Parameters> {
23+
public abstract static class Parameters implements BuildServiceParameters, ValueSourceParameters {
24+
public abstract DirectoryProperty getRootProjectDir();
25+
}
26+
27+
@Inject
28+
public abstract ExecOperations getExecOps();
29+
30+
@Override
31+
public @Nullable Map<String, String> obtain() {
32+
try (var baos = new ByteArrayOutputStream();
33+
var out = new BufferedOutputStream(baos)) {
34+
var result =
35+
getExecOps()
36+
.exec(
37+
spec -> {
38+
spec.setStandardOutput(out);
39+
spec.setErrorOutput(out);
40+
spec.setIgnoreExitValue(true);
41+
42+
spec.setWorkingDir(
43+
getParameters().getRootProjectDir().getAsFile().get().getAbsolutePath());
44+
spec.setExecutable(Os.isFamily(Os.FAMILY_WINDOWS) ? "git.exe" : "git");
45+
spec.args("status", "--porcelain=v2", "--branch");
46+
});
47+
out.flush();
48+
49+
// Just assume it's UTF-8. I don't know if this can break anyhow.
50+
String gitOutput = baos.toString(StandardCharsets.UTF_8);
51+
52+
if (result.getExitValue() != 0) {
53+
// Something went wrong. Assume this isn't a git checkout and
54+
// add placeholder values.
55+
return Map.ofEntries(
56+
Map.entry("git.commit", "unknown"),
57+
Map.entry("git.commit-short", "unknown"),
58+
Map.entry("git.clean", "false"),
59+
Map.entry("git.changed-files", "not a checkout?"),
60+
Map.entry("git.error-log", gitOutput));
61+
}
62+
63+
// Parse git-porcelain output.
64+
Map<String, String> properties = new TreeMap<>();
65+
66+
Pattern headerLine = Pattern.compile("# (?<key>[^ ]+) (?<value>.+)");
67+
Pattern changeLine = Pattern.compile("(?<type>[12u?!]) (?<value>.+)");
68+
StringBuilder changedLines = new StringBuilder();
69+
for (var line : gitOutput.split("\\n")) {
70+
if (line.startsWith("#")) {
71+
Matcher matcher = headerLine.matcher(line);
72+
if (matcher.matches()) {
73+
String key = matcher.group("key");
74+
String value = matcher.group("value");
75+
properties.put(key, value);
76+
77+
if (key.equals("branch.oid")) {
78+
properties.put("git.commit", value);
79+
properties.put("git.commit-short", value.substring(0, 8));
80+
}
81+
}
82+
} else if (changeLine.matcher(line).matches()) {
83+
changedLines.append(line);
84+
changedLines.append("\n");
85+
} else {
86+
// Omit other lines.
87+
}
88+
}
89+
90+
properties.put("git.changed-files", changedLines.toString());
91+
properties.put("git.clean", changedLines.isEmpty() ? "true" : "false");
92+
93+
return properties;
94+
} catch (IOException e) {
95+
throw new GradleException("Errors calling git to fetch local repository status.", e);
96+
}
97+
}
98+
}

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ zstd = { module = "com.github.luben:zstd-jni", version.ref = "zstd" }
9696

9797
[plugins]
9898
benmanes-versions = "com.github.ben-manes.versions:0.52.0"
99-
carrotsearch-buildinfra = "com.carrotsearch.gradle.buildinfra:0.0.12"
99+
carrotsearch-buildopts = "com.carrotsearch.gradle.opts:0.2.0"
100+
carrotsearch-dependencychecks = "com.carrotsearch.gradle.dependencychecks:0.1.0"
100101
errorprone = "net.ltgt.errorprone:4.2.0"
101102
forbiddenapis = "de.thetaphi.forbiddenapis:3.9"
102103
jacocolog = "org.barfuin.gradle.jacocolog:3.1.0"

0 commit comments

Comments
 (0)