Add a method to get an `OutputDirectory` from its name.

PiperOrigin-RevId: 406566325
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java
index 85e4556..5d97969 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java
@@ -365,8 +365,8 @@
     return options.strictFilesetOutput;
   }
 
-  public String getMainRepositoryName() {
-    return mainRepositoryName.strippedName();
+  public RepositoryName getMainRepositoryName() {
+    return mainRepositoryName;
   }
 
   /**
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/OutputDirectories.java b/src/main/java/com/google/devtools/build/lib/analysis/config/OutputDirectories.java
index 86d54ae..899c39e 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/config/OutputDirectories.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/config/OutputDirectories.java
@@ -14,6 +14,7 @@
 
 package com.google.devtools.build.lib.analysis.config;
 
+import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Predicates.not;
 import static java.util.stream.Collectors.joining;
 
@@ -82,31 +83,36 @@
   public enum OutputDirectory {
     BIN("bin"),
     GENFILES("genfiles"),
-    MIDDLEMAN(true),
+    MIDDLEMAN("internal"),
     TESTLOGS("testlogs"),
     COVERAGE("coverage-metadata"),
     INCLUDE(BlazeDirectories.RELATIVE_INCLUDE_DIR),
-    OUTPUT(false);
-
-    private final String nameFragment;
-    private final boolean middleman;
+    OUTPUT("");
 
     /**
-     * This constructor is for roots without suffixes, e.g.,
-     * [[execroot/repo]/bazel-out/local-fastbuild].
+     * Returns the {@link OutputDirectory} matching the given name.
      *
-     * @param isMiddleman whether the root should be a middleman root or a "normal" derived root.
+     * <p>Throws {@link IllegalArgumentException} if the given name does not match any directory in
+     * the output tree.
      */
-    OutputDirectory(boolean isMiddleman) {
-      this.nameFragment = isMiddleman ? "internal" : "";
-      this.middleman = isMiddleman;
+    public static OutputDirectory forName(String name) {
+      checkNotNull(name);
+      for (OutputDirectory directory : values()) {
+        if (directory.name.equals(name)) {
+          return directory;
+        }
+      }
+      throw new IllegalArgumentException(name);
     }
 
+    private final String name;
+
     OutputDirectory(String name) {
-      this.nameFragment = name;
-      // Must be a legal basename for root: no segments allowed.
-      FileSystemUtils.checkBaseName(nameFragment);
-      this.middleman = false;
+      // Must be a legal basename for root - multiple segments not allowed.
+      if (!name.isEmpty()) {
+        FileSystemUtils.checkBaseName(name);
+      }
+      this.name = name;
     }
 
     public ArtifactRoot getRoot(
@@ -116,10 +122,10 @@
       // e.g., [[execroot/repo1]/bazel-out/config/bin]
       return ArtifactRoot.asDerivedRoot(
           execRoot,
-          middleman ? RootType.Middleman : RootType.Output,
+          this == MIDDLEMAN ? RootType.Middleman : RootType.Output,
           directories.getRelativeOutputPath(),
           outputDirName,
-          nameFragment);
+          name);
     }
   }
 
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/info/ExecutionRootInfoItem.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/info/ExecutionRootInfoItem.java
index 2d34190..11a2c45 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/info/ExecutionRootInfoItem.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/info/ExecutionRootInfoItem.java
@@ -35,6 +35,7 @@
       Supplier<BuildConfigurationValue> configurationSupplier, CommandEnvironment env) {
     checkNotNull(env);
     return print(
-        env.getDirectories().getExecRoot(configurationSupplier.get().getMainRepositoryName()));
+        env.getDirectories()
+            .getExecRoot(configurationSupplier.get().getMainRepositoryName().strippedName()));
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/info/OutputPathInfoItem.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/info/OutputPathInfoItem.java
index d3577ba..b426137 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/commands/info/OutputPathInfoItem.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/info/OutputPathInfoItem.java
@@ -32,6 +32,7 @@
       Supplier<BuildConfigurationValue> configurationSupplier, CommandEnvironment env) {
     checkNotNull(env);
     return print(
-        env.getDirectories().getOutputPath(configurationSupplier.get().getMainRepositoryName()));
+        env.getDirectories()
+            .getOutputPath(configurationSupplier.get().getMainRepositoryName().strippedName()));
   }
 }
diff --git a/src/test/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValueTest.java b/src/test/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValueTest.java
index 9334314..3524a31 100644
--- a/src/test/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValueTest.java
+++ b/src/test/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValueTest.java
@@ -47,7 +47,10 @@
 
     BuildConfigurationValue config = create("--cpu=piii");
     String outputDirPrefix =
-        outputBase + "/execroot/" + config.getMainRepositoryName() + "/blaze-out/.*piii-fastbuild";
+        outputBase
+            + "/execroot/"
+            + config.getMainRepositoryName().strippedName()
+            + "/blaze-out/.*piii-fastbuild";
 
     assertThat(config.getOutputDirectory(RepositoryName.MAIN).getRoot().toString())
         .matches(outputDirPrefix);
@@ -70,7 +73,7 @@
         .matches(
             outputBase
                 + "/execroot/"
-                + config.getMainRepositoryName()
+                + config.getMainRepositoryName().strippedName()
                 + "/blaze-out/.*k8-fastbuild-test");
   }