Skip to content

Commit

Permalink
Get rid of the '@' in RepositoryName
Browse files Browse the repository at this point in the history
This CL is purely an internal refactor with no visible behavior changes.

In preparation for #15593

PiperOrigin-RevId: 451879164
Change-Id: I3642455cc324891f23ed58f4c1187ebb9ccaba78
  • Loading branch information
Wyverald authored and copybara-github committed May 30, 2022
1 parent dadee3d commit 3c30fbc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static Parts parse(String rawLabel) throws LabelSyntaxException {

private static void validateRepoName(@Nullable String repo) throws LabelSyntaxException {
if (repo != null) {
RepositoryName.validate('@' + repo);
RepositoryName.validate(repo);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,11 @@
public final class RepositoryName {

@SerializationConstant
public static final RepositoryName BAZEL_TOOLS = new RepositoryName("@bazel_tools");
public static final RepositoryName BAZEL_TOOLS = new RepositoryName("bazel_tools");

@SerializationConstant
public static final RepositoryName LOCAL_CONFIG_PLATFORM =
new RepositoryName("@local_config_platform");

@SerializationConstant public static final RepositoryName MAIN = new RepositoryName("@");
@SerializationConstant public static final RepositoryName MAIN = new RepositoryName("");

private static final Pattern VALID_REPO_NAME = Pattern.compile("@[\\w\\-.]*");
private static final Pattern VALID_REPO_NAME = Pattern.compile("[\\w\\-.]*");

private static final LoadingCache<String, RepositoryName> repositoryNameCache =
Caffeine.newBuilder()
Expand All @@ -62,9 +58,8 @@ public static RepositoryName create(String name) throws LabelSyntaxException {
if (name.isEmpty()) {
return MAIN;
}
// TODO(b/200024947): Get rid of the '@' in the #name field.
try {
return repositoryNameCache.get('@' + name);
return repositoryNameCache.get(name);
} catch (CompletionException e) {
Throwables.propagateIfPossible(e.getCause(), LabelSyntaxException.class);
throw e;
Expand All @@ -82,7 +77,7 @@ public static RepositoryName createUnvalidated(String name) {
// reference equality instead of #equals().
return MAIN;
}
return repositoryNameCache.get("@" + name);
return repositoryNameCache.get(name);
}

/**
Expand Down Expand Up @@ -140,27 +135,27 @@ private RepositoryName(String name) {
* message is sanitized.
*/
static void validate(String name) throws LabelSyntaxException {
if (name.isEmpty() || name.equals("@")) {
if (name.isEmpty()) {
return;
}

// Some special cases for more user-friendly error messages.
if (name.equals("@.") || name.equals("@..")) {
if (name.equals(".") || name.equals("..")) {
throw LabelParser.syntaxErrorf(
"invalid repository name '%s': repo names are not allowed to be '%s'", name, name);
"invalid repository name '@%s': repo names are not allowed to be '@%s'", name, name);
}

if (!VALID_REPO_NAME.matcher(name).matches()) {
throw LabelParser.syntaxErrorf(
"invalid repository name '%s': repo names may contain only A-Z, a-z, 0-9, '-', '_' and"
"invalid repository name '@%s': repo names may contain only A-Z, a-z, 0-9, '-', '_' and"
+ " '.'",
StringUtilities.sanitizeControlChars(name));
}
}

/** Returns the repository name without the leading "{@literal @}". */
/** Returns the bare repository name without the leading "{@literal @}". */
public String getName() {
return name.substring(1);
return name;
}

/**
Expand All @@ -184,12 +179,12 @@ public String getOwnerRepoIfNotVisible() {

/** Returns if this is the main repository, that is, {@link #getName} is empty. */
public boolean isMain() {
return name.equals("@");
return name.isEmpty();
}

/** Returns the repository name, with leading "{@literal @}". */
public String getNameWithAt() {
return name;
return '@' + name;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public void testBadRepoName() throws Exception {
ProcessResult result = context().bazel().shouldFail().build("//...");
assertThat(result.errString())
.contains(
"invalid repository name '@@a': repo names may contain only "
+ "A-Z, a-z, 0-9, '-', '_' and '.'");
"invalid repository name '@@a': repo names may contain only A-Z, a-z, 0-9, '-', '_' and"
+ " '.'");
}
}

0 comments on commit 3c30fbc

Please sign in to comment.