Skip to content

Commit af9d4e5

Browse files
authored
Use git-ls-files as the source of file paths for license checks (reapplied) (#15203)
1 parent 0cecf8a commit af9d4e5

File tree

8 files changed

+281
-116
lines changed

8 files changed

+281
-116
lines changed

.hgignore

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.lucene.gradle.plugins.gitinfo;
18+
19+
import java.io.BufferedOutputStream;
20+
import java.io.ByteArrayOutputStream;
21+
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
23+
import java.util.List;
24+
import javax.inject.Inject;
25+
import org.gradle.api.GradleException;
26+
import org.gradle.api.provider.ValueSource;
27+
import org.gradle.process.ExecOperations;
28+
import org.slf4j.LoggerFactory;
29+
30+
/** Returns a list of versioned and non-versioned (but not ignored) git files. */
31+
public abstract class GitFileListValueSource
32+
implements ValueSource<List<String>, GitValueSourceParameters> {
33+
34+
@Inject
35+
public abstract ExecOperations getExecOps();
36+
37+
@Override
38+
public List<String> obtain() {
39+
try (var baos = new ByteArrayOutputStream();
40+
var out = new BufferedOutputStream(baos)) {
41+
var result =
42+
getExecOps()
43+
.exec(
44+
spec -> {
45+
spec.setStandardOutput(out);
46+
spec.setErrorOutput(out);
47+
spec.setIgnoreExitValue(true);
48+
49+
getParameters().configure(spec);
50+
51+
spec.setExecutable(getParameters().getGitExec().get());
52+
spec.args(
53+
"ls-files",
54+
// show cached
55+
"-c",
56+
// show others
57+
"-o",
58+
// apply .gitignore exclusions
59+
"--exclude-standard",
60+
// don't quote paths, 0-terminate strings.
61+
"-z");
62+
});
63+
out.flush();
64+
65+
// Assume the output is UTF-8, even if it has 0 eols.
66+
String gitOutput = baos.toString(StandardCharsets.UTF_8);
67+
68+
if (result.getExitValue() != 0) {
69+
// Something went wrong. Assume this isn't a git checkout and
70+
// return an empty list of files.
71+
LoggerFactory.getLogger(getClass())
72+
.warn(
73+
"Failed executing git (exit status: {}). An empty list of versioned files will be used (run 'git init'?).",
74+
result.getExitValue());
75+
76+
return List.of();
77+
}
78+
79+
return List.of(gitOutput.split("\u0000"));
80+
} catch (IOException e) {
81+
throw new GradleException("Errors calling git to fetch local repository status.", e);
82+
}
83+
}
84+
}

build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/gitinfo/GitInfoExtension.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.lucene.gradle.plugins.gitinfo;
1818

1919
import org.gradle.api.file.FileSystemLocation;
20+
import org.gradle.api.provider.ListProperty;
2021
import org.gradle.api.provider.MapProperty;
2122
import org.gradle.api.provider.Property;
2223

@@ -32,4 +33,10 @@ public abstract class GitInfoExtension {
3233
* @return Return the location of {@code .git} directory (or file, if worktrees are used).
3334
*/
3435
public abstract Property<FileSystemLocation> getDotGitDir();
36+
37+
/**
38+
* @return Return a set of all versioned and non-versioned files (that are not ignored by {@code
39+
* .gitignore}).
40+
*/
41+
public abstract ListProperty<String> getAllNonIgnoredProjectFiles();
3542
}

build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/gitinfo/GitInfoPlugin.java

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,68 @@
1919
import java.nio.file.Files;
2020
import java.nio.file.Path;
2121
import org.apache.lucene.gradle.plugins.LuceneGradlePlugin;
22+
import org.apache.lucene.gradle.plugins.globals.LuceneBuildGlobalsExtension;
23+
import org.gradle.api.Action;
2224
import org.gradle.api.GradleException;
2325
import org.gradle.api.Project;
2426
import org.gradle.api.file.Directory;
27+
import org.gradle.api.file.FileSystemLocation;
28+
import org.gradle.api.provider.Property;
29+
import org.gradle.api.provider.ValueSourceSpec;
2530

2631
public class GitInfoPlugin extends LuceneGradlePlugin {
2732
@Override
2833
public void apply(Project project) {
2934
applicableToRootProjectOnly(project);
3035

31-
var gitInfoProvider =
32-
project
33-
.getProviders()
34-
.of(
35-
GitInfoValueSource.class,
36-
spec -> {
37-
spec.getParameters().getRootProjectDir().set(project.getProjectDir());
38-
});
39-
4036
var gitInfoExtension =
4137
project.getExtensions().create(GitInfoExtension.NAME, GitInfoExtension.class);
38+
var providers = project.getProviders();
4239

43-
gitInfoExtension.getGitInfo().value(gitInfoProvider).finalizeValueOnRead();
44-
45-
gitInfoExtension
46-
.getDotGitDir()
40+
Property<FileSystemLocation> dotGitDir = gitInfoExtension.getDotGitDir();
41+
dotGitDir
4742
.convention(
48-
project
49-
.getProviders()
50-
.provider(
51-
() -> {
52-
Directory projectDirectory =
53-
project.getRootProject().getLayout().getProjectDirectory();
54-
Path gitLocation = projectDirectory.getAsFile().toPath().resolve(".git");
55-
if (!Files.exists(gitLocation)) {
56-
// don't return anything from the provider if we can't locate the .git
57-
// folder. This will result in the property returning false from isPresent.
58-
return null;
59-
}
43+
providers.provider(
44+
() -> {
45+
Directory projectDirectory =
46+
project.getRootProject().getLayout().getProjectDirectory();
47+
Path gitLocation = projectDirectory.getAsFile().toPath().resolve(".git");
48+
if (!Files.exists(gitLocation)) {
49+
// don't return anything from the provider if we can't locate the .git
50+
// folder. This will result in the property returning false from isPresent.
51+
return null;
52+
}
53+
54+
if (Files.isDirectory(gitLocation)) {
55+
return projectDirectory.dir(".git");
56+
} else if (Files.isRegularFile(gitLocation)) {
57+
return projectDirectory.file(".git");
58+
} else {
59+
throw new GradleException(
60+
"Panic, .git location not a directory or file: "
61+
+ gitLocation.toAbsolutePath());
62+
}
63+
}))
64+
.finalizeValueOnRead();
6065

61-
if (Files.isDirectory(gitLocation)) {
62-
return projectDirectory.dir(".git");
63-
} else if (Files.isRegularFile(gitLocation)) {
64-
return projectDirectory.file(".git");
65-
} else {
66-
throw new GradleException(
67-
"Panic, .git location not a directory or file: "
68-
+ gitLocation.toAbsolutePath());
69-
}
70-
}))
66+
var gitExec =
67+
project.getExtensions().getByType(LuceneBuildGlobalsExtension.class).externalTool("git");
68+
69+
Action<ValueSourceSpec<GitValueSourceParameters>> configureGitParams =
70+
spec -> {
71+
var params = spec.getParameters();
72+
params.getRootProjectDir().set(project.getProjectDir());
73+
params.getGitExec().set(gitExec);
74+
params.getDotDir().set(dotGitDir);
75+
};
76+
77+
gitInfoExtension
78+
.getGitInfo()
79+
.value(providers.of(GitInfoValueSource.class, configureGitParams))
7180
.finalizeValueOnRead();
81+
82+
gitInfoExtension
83+
.getAllNonIgnoredProjectFiles()
84+
.value(providers.of(GitFileListValueSource.class, configureGitParams));
7285
}
7386
}

build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/gitinfo/GitInfoValueSource.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,12 @@
2525
import java.util.regex.Matcher;
2626
import java.util.regex.Pattern;
2727
import javax.inject.Inject;
28-
import org.apache.tools.ant.taskdefs.condition.Os;
2928
import org.gradle.api.GradleException;
30-
import org.gradle.api.file.DirectoryProperty;
3129
import org.gradle.api.provider.ValueSource;
32-
import org.gradle.api.provider.ValueSourceParameters;
33-
import org.gradle.api.services.BuildServiceParameters;
3430
import org.gradle.process.ExecOperations;
3531

3632
public abstract class GitInfoValueSource
37-
implements ValueSource<Map<String, String>, GitInfoValueSource.Parameters> {
38-
public abstract static class Parameters implements BuildServiceParameters, ValueSourceParameters {
39-
public abstract DirectoryProperty getRootProjectDir();
40-
}
33+
implements ValueSource<Map<String, String>, GitValueSourceParameters> {
4134

4235
@Inject
4336
public abstract ExecOperations getExecOps();
@@ -54,9 +47,9 @@ public Map<String, String> obtain() {
5447
spec.setErrorOutput(out);
5548
spec.setIgnoreExitValue(true);
5649

57-
spec.setWorkingDir(
58-
getParameters().getRootProjectDir().getAsFile().get().getAbsolutePath());
59-
spec.setExecutable(Os.isFamily(Os.FAMILY_WINDOWS) ? "git.exe" : "git");
50+
getParameters().configure(spec);
51+
52+
spec.setExecutable(getParameters().getGitExec().get());
6053
spec.args("status", "--porcelain=v2", "--branch");
6154
});
6255
out.flush();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.lucene.gradle.plugins.gitinfo;
18+
19+
import org.gradle.api.file.DirectoryProperty;
20+
import org.gradle.api.file.FileSystemLocation;
21+
import org.gradle.api.provider.Property;
22+
import org.gradle.api.provider.ValueSourceParameters;
23+
import org.gradle.api.services.BuildServiceParameters;
24+
import org.gradle.process.ExecSpec;
25+
26+
public abstract class GitValueSourceParameters
27+
implements BuildServiceParameters, ValueSourceParameters {
28+
public abstract DirectoryProperty getRootProjectDir();
29+
30+
public abstract Property<String> getGitExec();
31+
32+
public abstract Property<FileSystemLocation> getDotDir();
33+
34+
void configure(ExecSpec spec) {
35+
String workDir = getRootProjectDir().getAsFile().get().getAbsolutePath();
36+
spec.setWorkingDir(workDir);
37+
spec.environment("GIT_DIR", getDotDir().get().getAsFile().getAbsolutePath());
38+
}
39+
}

0 commit comments

Comments
 (0)