Skip to content

Inline the gitinfo plugin and remove dependency on carrotsearch's buildinfra #14794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 0 additions & 112 deletions CHANGES-record.md

This file was deleted.

15 changes: 10 additions & 5 deletions build-tools/build-infra-shadow/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ repositories {
gradlePluginPortal()
}

// Convert a plugin dependency to a regular dependency so that we can
// use [plugins] section in the top-level toml but declare regular
// project dependencies here.
static Provider<String> plugin(Provider<PluginDependency> plugin) {
return plugin.map {
if (it.pluginId == "com.carrotsearch.gradle.buildinfra") {
return "${it.pluginId}:${it.pluginId}.gradle.plugin:${it.version}".toString()
} else if (it.pluginId == "de.thetaphi.forbiddenapis") {
if (it.pluginId == "de.thetaphi.forbiddenapis") {
// Uwe's forbiddenapis is on Maven Central, directly.
return "de.thetaphi:forbiddenapis:${it.version}".toString()
} else {
return "${it.pluginId}:${it.pluginId.replaceFirst(".*\\.", "")}-plugin-gradle:${it.version}".toString()
// maven artifact pattern for gradle's plugin repositories.
return "${it.pluginId}:${it.pluginId}.gradle.plugin:${it.version}".toString()
// return "${it.pluginId}:${it.pluginId.replaceFirst(".*\\.", "")}-plugin-gradle:${it.version}".toString()
}
}
}
Expand All @@ -44,7 +48,8 @@ dependencies {
implementation deps.commons.codec
implementation deps.randomizedtesting.runner

implementation plugin(deps.plugins.carrotsearch.buildinfra)
implementation plugin(deps.plugins.carrotsearch.buildopts)
implementation plugin(deps.plugins.carrotsearch.dependencychecks)
implementation plugin(deps.plugins.forbiddenapis)
implementation plugin(deps.plugins.spotless)
}
Expand Down
4 changes: 3 additions & 1 deletion build-tools/build-infra/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,15 @@ dependencies {
implementation deps.flexmark.ext.autolink
implementation deps.flexmark.ext.tables

implementation plugin(deps.plugins.carrotsearch.buildinfra)
implementation plugin(deps.plugins.carrotsearch.buildopts)
implementation plugin(deps.plugins.carrotsearch.dependencychecks)
implementation plugin(deps.plugins.forbiddenapis)
implementation plugin(deps.plugins.spotless)
implementation plugin(deps.plugins.owasp.dependencycheck)
implementation plugin(deps.plugins.undercouch.download)
implementation plugin(deps.plugins.errorprone)
implementation plugin(deps.plugins.jacocolog)
implementation plugin(deps.plugins.versionCatalogUpdate)
}

def hasJavaFlightRecorder = ModuleLayer.boot().findModule('jdk.jfr').map{ otherModule ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import com.carrotsearch.gradle.buildinfra.environment.GitInfoExtension
import org.apache.lucene.gradle.plugins.gitinfo.GitInfoExtension

// Configures artifact JARs (manifest entries)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.carrotsearch.gradle.buildinfra.buildoptions.BuildOptionValueSource
import com.carrotsearch.gradle.buildinfra.buildoptions.BuildOptionsPlugin
import com.carrotsearch.gradle.buildinfra.environment.GitInfoPlugin
import org.apache.lucene.gradle.plugins.gitinfo.GitInfoPlugin
import com.carrotsearch.randomizedtesting.SeedUtils

import java.time.ZonedDateTime
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.apache.lucene.gradle.plugins.gitinfo;

import org.gradle.api.provider.MapProperty;

public abstract class GitInfoExtension {
public static final String NAME = "gitinfo";

public abstract MapProperty<String, String> getGitInfo();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.apache.lucene.gradle.plugins.gitinfo;

import org.gradle.api.GradleException;
import org.gradle.api.Plugin;
import org.gradle.api.Project;

public class GitInfoPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
if (project != project.getRootProject()) {
throw new GradleException("This plugin is applicable to the rootProject only.");
}

var gitInfoProvider =
project
.getProviders()
.of(
GitInfoValueSource.class,
spec -> {
spec.getParameters().getRootProjectDir().set(project.getProjectDir());
});

var gitInfoExtension =
project.getExtensions().create(GitInfoExtension.NAME, GitInfoExtension.class);

gitInfoExtension.getGitInfo().value(gitInfoProvider).finalizeValueOnRead();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.apache.lucene.gradle.plugins.gitinfo;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
import org.apache.tools.ant.taskdefs.condition.Os;
import org.gradle.api.GradleException;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.provider.ValueSource;
import org.gradle.api.provider.ValueSourceParameters;
import org.gradle.api.services.BuildServiceParameters;
import org.gradle.process.ExecOperations;
import org.jetbrains.annotations.Nullable;

public abstract class GitInfoValueSource
implements ValueSource<Map<String, String>, GitInfoValueSource.Parameters> {
public abstract static class Parameters implements BuildServiceParameters, ValueSourceParameters {
public abstract DirectoryProperty getRootProjectDir();
}

@Inject
public abstract ExecOperations getExecOps();

@Override
public @Nullable Map<String, String> obtain() {
try (var baos = new ByteArrayOutputStream();
var out = new BufferedOutputStream(baos)) {
var result =
getExecOps()
.exec(
spec -> {
spec.setStandardOutput(out);
spec.setErrorOutput(out);
spec.setIgnoreExitValue(true);

spec.setWorkingDir(
getParameters().getRootProjectDir().getAsFile().get().getAbsolutePath());
spec.setExecutable(Os.isFamily(Os.FAMILY_WINDOWS) ? "git.exe" : "git");
spec.args("status", "--porcelain=v2", "--branch");
});
out.flush();

// Just assume it's UTF-8. I don't know if this can break anyhow.
String gitOutput = baos.toString(StandardCharsets.UTF_8);

if (result.getExitValue() != 0) {
// Something went wrong. Assume this isn't a git checkout and
// add placeholder values.
return Map.ofEntries(
Map.entry("git.commit", "unknown"),
Map.entry("git.commit-short", "unknown"),
Map.entry("git.clean", "false"),
Map.entry("git.changed-files", "not a checkout?"),
Map.entry("git.error-log", gitOutput));
}

// Parse git-porcelain output.
Map<String, String> properties = new TreeMap<>();

Pattern headerLine = Pattern.compile("# (?<key>[^ ]+) (?<value>.+)");
Pattern changeLine = Pattern.compile("(?<type>[12u?!]) (?<value>.+)");
StringBuilder changedLines = new StringBuilder();
for (var line : gitOutput.split("\\n")) {
if (line.startsWith("#")) {
Matcher matcher = headerLine.matcher(line);
if (matcher.matches()) {
String key = matcher.group("key");
String value = matcher.group("value");
properties.put(key, value);

if (key.equals("branch.oid")) {
properties.put("git.commit", value);
properties.put("git.commit-short", value.substring(0, 8));
}
}
} else if (changeLine.matcher(line).matches()) {
changedLines.append(line);
changedLines.append("\n");
} else {
// Omit other lines.
}
}

properties.put("git.changed-files", changedLines.toString());
properties.put("git.clean", changedLines.isEmpty() ? "true" : "false");

return properties;
} catch (IOException e) {
throw new GradleException("Errors calling git to fetch local repository status.", e);
}
}
}
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ zstd = { module = "com.github.luben:zstd-jni", version.ref = "zstd" }

[plugins]
benmanes-versions = "com.github.ben-manes.versions:0.52.0"
carrotsearch-buildinfra = "com.carrotsearch.gradle.buildinfra:0.0.12"
carrotsearch-buildopts = "com.carrotsearch.gradle.opts:0.2.0"
carrotsearch-dependencychecks = "com.carrotsearch.gradle.dependencychecks:0.1.0"
errorprone = "net.ltgt.errorprone:4.2.0"
forbiddenapis = "de.thetaphi.forbiddenapis:3.9"
jacocolog = "org.barfuin.gradle.jacocolog:3.1.0"
Expand Down