Skip to content

Commit

Permalink
Disallow creating a repository with a .git suffix (line#957)
Browse files Browse the repository at this point in the history
Motivation:
It is undesirable for repositories to be created with a `.git` suffix due to potential conflicts or confusion with Git's repository structure and management.

Modifications:
- Disallow creating a repository whose suffix is `.git`

Result:
- You can no longer create a repository whose suffix is `.git`
- Close line#956
  • Loading branch information
minwoox authored Jun 13, 2024
1 parent 138eb29 commit f47657e
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class Util {
* End with an alphanumeric character.
*/
private static final Pattern PROJECT_AND_REPO_NAME_PATTERN =
Pattern.compile("^[0-9A-Za-z](?:[-+_0-9A-Za-z.]*[0-9A-Za-z])?$");
Pattern.compile("^(?!.*\\.git$)[0-9A-Za-z](?:[-+_0-9A-Za-z.]*[0-9A-Za-z])?$");

public static String validateFileName(String name, String paramName) {
requireNonNull(name, paramName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public final class CreateRepositoryCommand extends ProjectCommand<Void> {
@JsonProperty("author") @Nullable Author author,
@JsonProperty("projectName") String projectName,
@JsonProperty("repositoryName") String repositoryName) {

super(CommandType.CREATE_REPOSITORY, timestamp, author, projectName);
this.repositoryName = requireNonNull(repositoryName, "repositoryName");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public abstract class DirectoryBasedStorageManager<T> implements StorageManager<
Pattern.compile("^[0-9A-Za-z](?:[-+_0-9A-Za-z.]*[0-9A-Za-z])?$");
private static final String SUFFIX_REMOVED = ".removed";
private static final String SUFFIX_PURGED = ".purged";
private static final String GIT_EXTENSION = ".git";

private final String childTypeName;
private final File rootDir;
Expand Down Expand Up @@ -437,7 +438,8 @@ private static boolean isValidChildName(String name) {
return false;
}

return !name.endsWith(SUFFIX_REMOVED) && !name.endsWith(SUFFIX_PURGED);
return !name.endsWith(SUFFIX_REMOVED) && !name.endsWith(SUFFIX_PURGED) &&
!name.endsWith(GIT_EXTENSION); // Git repository whose name ends with '.git' is not allowed.
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.linecorp.centraldogma.common.Author.SYSTEM;
import static com.linecorp.centraldogma.common.Revision.HEAD;
import static com.linecorp.centraldogma.internal.Util.validateProjectName;
import static com.linecorp.centraldogma.internal.Util.validateRepositoryName;
import static com.linecorp.centraldogma.server.internal.api.ContentServiceV1.checkPush;
import static com.linecorp.centraldogma.server.internal.api.RepositoryServiceV1.increaseCounterIfOldRevisionUsed;
import static com.linecorp.centraldogma.server.internal.thrift.Converter.convert;
Expand Down Expand Up @@ -121,6 +123,7 @@ private static void handleAsVoidResult(CompletableFuture<?> future, AsyncMethodC

@Override
public void createProject(String name, AsyncMethodCallback resultHandler) {
validateProjectName(name, "name");
// ProjectInitializingCommandExecutor initializes a metadata for the specified project.
handle(projectApiManager.createProject(name, SYSTEM), resultHandler);
}
Expand Down Expand Up @@ -161,6 +164,7 @@ public void listRemovedProjects(AsyncMethodCallback resultHandler) {
@Override
public void createRepository(String projectName, String repositoryName,
AsyncMethodCallback resultHandler) {
validateRepositoryName(repositoryName, "repositoryName");
// HTTP v1 API will return '403 forbidden' in this case, but we deal it as '400 bad request' here.
if (isReservedRepoName(repositoryName)) {
resultHandler.onError(convert(RESERVED_REPOSITORY_EXCEPTION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ void createRepositoryWithSameName() {
assertThatJson(aRes.contentUtf8()).isEqualTo(expectedJson);
}

@Test
void createRepositoryWithInvalidName() {
final WebClient client = dogma.httpClient();
final AggregatedHttpResponse aRes = createRepository(client, "myRepo.git");
assertThat(aRes.headers().status()).isSameAs(HttpStatus.BAD_REQUEST);
}

@Test
void createRepositoryInAbsentProject() {
final WebClient client = dogma.httpClient();
Expand All @@ -123,7 +130,7 @@ void createRepositoryInAbsentProject() {
@Test
void removeRepository() {
final WebClient client = dogma.httpClient();
createRepository(client,"foo");
createRepository(client, "foo");
final AggregatedHttpResponse aRes = client.delete(REPOS_PREFIX + "/foo").aggregate().join();
assertThat(ResponseHeaders.of(aRes.headers()).status()).isEqualTo(HttpStatus.NO_CONTENT);
}
Expand Down

0 comments on commit f47657e

Please sign in to comment.