dweiss commented on code in PR #15200: URL: https://github.com/apache/lucene/pull/15200#discussion_r2359772528
########## build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/gitinfo/GitFileListValueSource.java: ########## @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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.List; +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.slf4j.LoggerFactory; + +/** Returns a list of versioned and non-versioned (but not ignored) git files. */ +public abstract class GitFileListValueSource + implements ValueSource<List<String>, GitFileListValueSource.Parameters> { + public abstract static class Parameters implements BuildServiceParameters, ValueSourceParameters { + public abstract DirectoryProperty getRootProjectDir(); + } + + @Inject + public abstract ExecOperations getExecOps(); + + @Override + public List<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( + "ls-files", + // show cached + "-c", + // show others + "-o", + // apply .gitignore exclusions + "--exclude-standard", + // don't quote paths, 0-terminate strings. + "-z"); + }); + out.flush(); + + // Assume the output is UTF-8, even if it has 0 eols. + String gitOutput = baos.toString(StandardCharsets.UTF_8); Review Comment: This seems... correct, right? If there are broken paths (non-utf8 characters) in git then we're screwed but it doesn't seem like the place to do any sophisticated recovery here... -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
