Parse the workspace name when a repository is loaded

Moved RepositoryValue to RepositoryDirectoryValue so that it could be cached
(and not re-downloaded) even if the WorkspaceAST caused a Skyframe restart
(as mentioned in https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryDelegatorFunction.java#L130-L133).

--
MOS_MIGRATED_REVID=113358489
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/EnvironmentBackedRecursivePackageProvider.java b/src/main/java/com/google/devtools/build/lib/skyframe/EnvironmentBackedRecursivePackageProvider.java
index a388389..8045c59 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/EnvironmentBackedRecursivePackageProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/EnvironmentBackedRecursivePackageProvider.java
@@ -29,6 +29,7 @@
 import com.google.devtools.build.lib.packages.Target;
 import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
 import com.google.devtools.build.lib.pkgcache.RecursivePackageProvider;
+import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue;
 import com.google.devtools.build.lib.util.Preconditions;
 import com.google.devtools.build.lib.vfs.Path;
 import com.google.devtools.build.lib.vfs.PathFragment;
@@ -122,8 +123,8 @@
     if (repository.isDefault()) {
       roots.addAll(packageLocator.getPathEntries());
     } else {
-      RepositoryValue repositoryValue =
-          (RepositoryValue) env.getValue(RepositoryValue.key(repository));
+      RepositoryDirectoryValue repositoryValue =
+          (RepositoryDirectoryValue) env.getValue(RepositoryDirectoryValue.key(repository));
       if (repositoryValue == null) {
         throw new MissingDepException();
       }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/GraphBackedRecursivePackageProvider.java b/src/main/java/com/google/devtools/build/lib/skyframe/GraphBackedRecursivePackageProvider.java
index 60221ad..8c0e56c 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/GraphBackedRecursivePackageProvider.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/GraphBackedRecursivePackageProvider.java
@@ -37,6 +37,7 @@
 import com.google.devtools.build.lib.packages.Target;
 import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
 import com.google.devtools.build.lib.pkgcache.RecursivePackageProvider;
+import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue;
 import com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey;
 import com.google.devtools.build.lib.util.Preconditions;
 import com.google.devtools.build.lib.vfs.Path;
@@ -172,8 +173,8 @@
     if (repository.isDefault()) {
       roots.addAll(pkgPath.getPathEntries());
     } else {
-      RepositoryValue repositoryValue =
-            (RepositoryValue) graph.getValue(RepositoryValue.key(repository));
+      RepositoryDirectoryValue repositoryValue =
+            (RepositoryDirectoryValue) graph.getValue(RepositoryDirectoryValue.key(repository));
       if (repositoryValue == null) {
         // If this key doesn't exist, the repository is outside the universe, so we return
         // "nothing".
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternFunction.java
index 781c001..2261b77 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/PrepareDepsOfPatternFunction.java
@@ -32,6 +32,7 @@
 import com.google.devtools.build.lib.pkgcache.FilteringPolicy;
 import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
 import com.google.devtools.build.lib.pkgcache.TargetPatternResolverUtil;
+import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue;
 import com.google.devtools.build.lib.skyframe.EnvironmentBackedRecursivePackageProvider.MissingDepException;
 import com.google.devtools.build.lib.util.BatchCallback;
 import com.google.devtools.build.lib.util.BatchCallback.NullCallback;
@@ -225,8 +226,8 @@
       if (repository.isDefault()) {
         roots.addAll(pkgPath.getPathEntries());
       } else {
-        RepositoryValue repositoryValue =
-            (RepositoryValue) env.getValue(RepositoryValue.key(repository));
+        RepositoryDirectoryValue repositoryValue =
+            (RepositoryDirectoryValue) env.getValue(RepositoryDirectoryValue.key(repository));
         if (repositoryValue == null) {
           throw new MissingDepException();
         }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryValue.java
index e647b81..2f8da11 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryValue.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/RepositoryValue.java
@@ -1,4 +1,4 @@
-// Copyright 2014 The Bazel Authors. All rights reserved.
+// Copyright 2016 The Bazel Authors. All rights reserved.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -16,49 +16,31 @@
 
 import com.google.common.base.Objects;
 import com.google.devtools.build.lib.cmdline.RepositoryName;
+import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue;
 import com.google.devtools.build.lib.vfs.Path;
 import com.google.devtools.build.skyframe.SkyKey;
 import com.google.devtools.build.skyframe.SkyValue;
 
 /**
- * A local view of an external repository.
+ * A repository's name and directory.
  */
 public class RepositoryValue implements SkyValue {
-  private final Path path;
-  private final boolean fetchingDelayed;
-
-  private RepositoryValue(Path path, boolean fetchingDelayed) {
-    this.path = path;
-    this.fetchingDelayed = fetchingDelayed;
-  }
+  private final RepositoryName repositoryName;
+  private final RepositoryDirectoryValue repositoryDirectory;
 
   /**
-   * Creates an immutable external repository.
+   * Creates a repository with a given name in a certain directory.
    */
-  public static RepositoryValue create(Path repositoryDirectory) {
-    return new RepositoryValue(repositoryDirectory, false);
+  public RepositoryValue(RepositoryName repositoryName, RepositoryDirectoryValue repository) {
+    this.repositoryName = repositoryName;
+    this.repositoryDirectory = repository;
   }
 
   /**
-   * Creates a value that represents a repository whose fetching has been delayed by a
-   * {@code --nofetch} command line option.
-   */
-  public static RepositoryValue fetchingDelayed(Path repositoryDirectory) {
-    return new RepositoryValue(repositoryDirectory, true);
-  }
-
-  /**
-   * Returns the path to the directory containing the repository's contents. This directory is
-   * guaranteed to exist.  It may contain a full Bazel repository (with a WORKSPACE file,
-   * directories, and BUILD files) or simply contain a file (or set of files) for, say, a jar from
-   * Maven.
+   * Returns the path to the repository.
    */
   public Path getPath() {
-    return path;
-  }
-
-  public boolean isFetchingDelayed() {
-    return fetchingDelayed;
+    return repositoryDirectory.getPath();
   }
 
   @Override
@@ -66,28 +48,21 @@
     if (this == other) {
       return true;
     }
-
-    if (other instanceof RepositoryValue) {
-      RepositoryValue otherValue = (RepositoryValue) other;
-      return path.equals(otherValue.path);
+    if (other == null || getClass() != other.getClass()) {
+      return false;
     }
-    return false;
+
+    RepositoryValue that = (RepositoryValue) other;
+    return Objects.equal(repositoryName, that.repositoryName)
+        && Objects.equal(repositoryDirectory, that.repositoryDirectory);
   }
 
   @Override
   public int hashCode() {
-    return Objects.hashCode(path);
+    return Objects.hashCode(repositoryName, repositoryDirectory);
   }
 
-  @Override
-  public String toString() {
-    return path.getPathString();
-  }
-
-  /**
-   * Creates a key from the given repository name.
-   */
-  public static SkyKey key(RepositoryName repository) {
-    return new SkyKey(SkyFunctions.REPOSITORY, repository);
+  static SkyKey key(RepositoryName repositoryName) {
+    return new SkyKey(SkyFunctions.REPOSITORY, repositoryName);
   }
 }
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/SkyFunctions.java b/src/main/java/com/google/devtools/build/lib/skyframe/SkyFunctions.java
index 277b627..ed3508f 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/SkyFunctions.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/SkyFunctions.java
@@ -96,6 +96,8 @@
   public static final SkyFunctionName WORKSPACE_FILE = SkyFunctionName.create("WORKSPACE_FILE");
   public static final SkyFunctionName COVERAGE_REPORT = SkyFunctionName.create("COVERAGE_REPORT");
   public static final SkyFunctionName REPOSITORY = SkyFunctionName.create("REPOSITORY");
+  public static final SkyFunctionName REPOSITORY_DIRECTORY =
+      SkyFunctionName.create("REPOSITORY_DIRECTORY");
   public static final SkyFunctionName WORKSPACE_AST = SkyFunctionName.create("WORKSPACE_AST");
 
   public static Predicate<SkyKey> isSkyFunction(final SkyFunctionName functionName) {
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceASTValue.java b/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceASTValue.java
index 022541d..96f42df 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceASTValue.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/WorkspaceASTValue.java
@@ -35,7 +35,7 @@
     return ast;
   }
 
-  public SkyKey key(RootedPath path) {
+  public static SkyKey key(RootedPath path) {
     return new SkyKey(SkyFunctions.WORKSPACE_AST, path);
   }
 }