소스 검색

Fix accessing main files in transport version resources (#133349) (#133436)

Ryan Ernst 1 개월 전
부모
커밋
6e865c3f90

+ 4 - 2
build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionDefinition.java

@@ -9,11 +9,13 @@
 
 package org.elasticsearch.gradle.internal.transport;
 
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.List;
 
 record TransportVersionDefinition(String name, List<TransportVersionId> ids) {
-    public static TransportVersionDefinition fromString(String filename, String contents) {
+    public static TransportVersionDefinition fromString(Path file, String contents) {
+        String filename = file.getFileName().toString();
         assert filename.endsWith(".csv");
         String name = filename.substring(0, filename.length() - 4);
         List<TransportVersionId> ids = new ArrayList<>();
@@ -23,7 +25,7 @@ record TransportVersionDefinition(String name, List<TransportVersionId> ids) {
                 try {
                     ids.add(TransportVersionId.fromString(rawId));
                 } catch (NumberFormatException e) {
-                    throw new IllegalStateException("Failed to parse id " + rawId + " in " + filename, e);
+                    throw new IllegalStateException("Failed to parse id " + rawId + " in " + file, e);
                 }
             }
         }

+ 5 - 2
build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionLatest.java

@@ -9,14 +9,17 @@
 
 package org.elasticsearch.gradle.internal.transport;
 
+import java.nio.file.Path;
+
 record TransportVersionLatest(String branch, String name, TransportVersionId id) {
-    public static TransportVersionLatest fromString(String filename, String contents) {
+    public static TransportVersionLatest fromString(Path file, String contents) {
+        String filename = file.getFileName().toString();
         assert filename.endsWith(".csv");
         String branch = filename.substring(0, filename.length() - 4);
 
         String[] parts = contents.split(",");
         if (parts.length != 2) {
-            throw new IllegalStateException("Invalid transport version latest file [" + filename + "]: " + contents);
+            throw new IllegalStateException("Invalid transport version latest file [" + file + "]: " + contents);
         }
 
         return new TransportVersionLatest(branch, parts[0], TransportVersionId.fromString(parts[1]));

+ 11 - 11
build-tools-internal/src/main/java/org/elasticsearch/gradle/internal/transport/TransportVersionResourcesService.java

@@ -16,7 +16,6 @@ import org.gradle.process.ExecOperations;
 import org.gradle.process.ExecResult;
 
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
@@ -104,7 +103,7 @@ public abstract class TransportVersionResourcesService implements BuildService<T
 
     /** Get a named definition from main if it exists there, or null otherwise */
     TransportVersionDefinition getNamedDefinitionFromMain(String name) {
-        String resourcePath = getNamedDefinitionRelativePath(name).toString();
+        Path resourcePath = getNamedDefinitionRelativePath(name);
         return getMainFile(resourcePath, TransportVersionDefinition::fromString);
     }
 
@@ -130,7 +129,7 @@ public abstract class TransportVersionResourcesService implements BuildService<T
 
     /** Get a named definition from main if it exists there, or null otherwise */
     TransportVersionDefinition getUnreferencedDefinitionFromMain(String name) {
-        String resourcePath = getUnreferencedDefinitionRelativePath(name).toString();
+        Path resourcePath = getUnreferencedDefinitionRelativePath(name);
         return getMainFile(resourcePath, TransportVersionDefinition::fromString);
     }
 
@@ -145,7 +144,7 @@ public abstract class TransportVersionResourcesService implements BuildService<T
         try (var stream = Files.list(transportResourcesDir.resolve(LATEST_DIR))) {
             for (var latestFile : stream.toList()) {
                 String contents = Files.readString(latestFile, StandardCharsets.UTF_8).strip();
-                var latest = TransportVersionLatest.fromString(latestFile.getFileName().toString(), contents);
+                var latest = TransportVersionLatest.fromString(latestFile, contents);
                 latests.put(latest.name(), latest);
             }
         }
@@ -154,7 +153,7 @@ public abstract class TransportVersionResourcesService implements BuildService<T
 
     /** Retrieve the latest transport version for the given release branch on main */
     TransportVersionLatest getLatestFromMain(String releaseBranch) {
-        String resourcePath = getLatestRelativePath(releaseBranch).toString();
+        Path resourcePath = getLatestRelativePath(releaseBranch);
         return getMainFile(resourcePath, TransportVersionLatest::fromString);
     }
 
@@ -174,7 +173,7 @@ public abstract class TransportVersionResourcesService implements BuildService<T
                 String output = gitCommand("ls-tree", "--name-only", "-r", "main", ".");
 
                 HashSet<String> resources = new HashSet<>();
-                Collections.addAll(resources, output.split(System.lineSeparator()));
+                Collections.addAll(resources, output.split("\n")); // git always outputs LF
                 mainResources.set(resources);
             }
         }
@@ -188,7 +187,7 @@ public abstract class TransportVersionResourcesService implements BuildService<T
                 String output = gitCommand("diff", "--name-only", "main", ".");
 
                 HashSet<String> resources = new HashSet<>();
-                Collections.addAll(resources, output.split(System.lineSeparator()));
+                Collections.addAll(resources, output.split("\n")); // git always outputs LF
                 changedResources.set(resources);
             }
         }
@@ -196,12 +195,13 @@ public abstract class TransportVersionResourcesService implements BuildService<T
     }
 
     // Read a transport version resource from the main branch, or return null if it doesn't exist on main
-    private <T> T getMainFile(String resourcePath, BiFunction<String, String, T> parser) {
-        if (getMainResources().contains(resourcePath) == false) {
+    private <T> T getMainFile(Path resourcePath, BiFunction<Path, String, T> parser) {
+        String pathString = resourcePath.toString().replace('\\', '/'); // normalize to forward slash that git uses
+        if (getMainResources().contains(pathString) == false) {
             return null;
         }
 
-        String content = gitCommand("show", "main:." + File.separator + resourcePath).strip();
+        String content = gitCommand("show", "main:./" + pathString).strip();
         return parser.apply(resourcePath, content);
     }
 
@@ -213,7 +213,7 @@ public abstract class TransportVersionResourcesService implements BuildService<T
         try (var definitionsStream = Files.list(dir)) {
             for (var definitionFile : definitionsStream.toList()) {
                 String contents = Files.readString(definitionFile, StandardCharsets.UTF_8).strip();
-                var definition = TransportVersionDefinition.fromString(definitionFile.getFileName().toString(), contents);
+                var definition = TransportVersionDefinition.fromString(definitionFile, contents);
                 definitions.put(definition.name(), definition);
             }
         }