Support .bazelignore file for external repo

Fixes https://github.com/bazelbuild/bazel/issues/10234
Fixes https://github.com/bazelbuild/bazel/issues/9080

Closes #10261.

RELNOTES[NEW]:
Similar to the [.bazelignore](https://docs.bazel.build/versions/master/guide.html#.bazelignore) in the main repository, a `.bazelignore` file in external repository will cause the specified directories to be ignored by Bazel. Bazel won't try to identify any packages under the directories, but the files can still be referenced in other BUILD files.

PiperOrigin-RevId: 285412849
diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java
index f6f9215..15054a9 100644
--- a/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java
+++ b/src/main/java/com/google/devtools/build/lib/cmdline/TargetPattern.java
@@ -300,6 +300,9 @@
     throw new IllegalStateException();
   }
 
+  /** Returns the repository name of the target pattern. */
+  public abstract RepositoryName getRepository();
+
   /**
    * Returns {@code true} iff this pattern has type {@code Type.TARGETS_BELOW_DIRECTORY} or
    * {@code Type.TARGETS_IN_PACKAGE} and the target pattern suffix specified it should match
@@ -340,6 +343,11 @@
     }
 
     @Override
+    public RepositoryName getRepository() {
+      return directory.getRepository();
+    }
+
+    @Override
     public boolean getRulesOnly() {
       return false;
     }
@@ -419,6 +427,13 @@
     }
 
     @Override
+    public RepositoryName getRepository() {
+      // InterpretPathAsTarget is validated by PackageIdentifier.createInMainRepo,
+      // therefore it must belong to the main repository.
+      return RepositoryName.MAIN;
+    }
+
+    @Override
     public boolean getRulesOnly() {
       return false;
     }
@@ -491,6 +506,11 @@
     }
 
     @Override
+    public RepositoryName getRepository() {
+      return packageIdentifier.getRepository();
+    }
+
+    @Override
     public boolean getRulesOnly() {
       return rulesOnly;
     }
@@ -622,6 +642,11 @@
     }
 
     @Override
+    public RepositoryName getRepository() {
+      return directory.getRepository();
+    }
+
+    @Override
     public boolean getRulesOnly() {
       return rulesOnly;
     }