Move duplicate code to strip .git suffix to a utility class

Code to strip the .git suffix off project names is duplicated in
a couple of places.

Implement this code in a common utility method.

Note that we cannot use this utility method in client UI code
because it imports Jgit constants that are not allowed in GWT
according to ee720b8ab750d21ca2d219b439ff6f596432008e

Change-Id: I5e52e16d6f87aca22181e0fe0220a68635e6d789
This commit is contained in:
David Pursehouse 2013-02-13 16:51:38 +09:00 committed by Edwin Kempin
parent a73579c652
commit 65688b6c8f
3 changed files with 41 additions and 27 deletions

View File

@ -0,0 +1,36 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.common;
import org.eclipse.jgit.lib.Constants;
public class ProjectUtil {
public static String stripGitSuffix(String name) {
String nameWithoutSuffix = name;
if (nameWithoutSuffix.endsWith(Constants.DOT_GIT_EXT)) {
// Be nice and drop the trailing ".git" suffix, which we never keep
// in our database, but clients might mistakenly provide anyway.
//
nameWithoutSuffix = nameWithoutSuffix.substring(0, //
nameWithoutSuffix.length() - Constants.DOT_GIT_EXT.length());
while (nameWithoutSuffix.endsWith("/")) {
nameWithoutSuffix =
nameWithoutSuffix.substring(0, nameWithoutSuffix.length() - 1);
}
}
return nameWithoutSuffix;
}
}

View File

@ -14,13 +14,13 @@
package com.google.gerrit.server.args4j;
import com.google.gerrit.common.ProjectUtil;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.project.NoSuchProjectException;
import com.google.gerrit.server.project.ProjectControl;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.eclipse.jgit.lib.Constants;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
@ -58,18 +58,7 @@ public class ProjectControlHandler extends OptionHandler<ProjectControl> {
projectName = projectName.substring(1);
}
String nameWithoutSuffix = projectName;
if (nameWithoutSuffix.endsWith(Constants.DOT_GIT_EXT)) {
// Be nice and drop the trailing ".git" suffix, which we never keep
// in our database, but clients might mistakenly provide anyway.
//
nameWithoutSuffix = nameWithoutSuffix.substring(0, //
nameWithoutSuffix.length() - Constants.DOT_GIT_EXT.length());
while (nameWithoutSuffix.endsWith("/")) {
nameWithoutSuffix =
nameWithoutSuffix.substring(0, nameWithoutSuffix.length() - 1);
}
}
String nameWithoutSuffix = ProjectUtil.stripGitSuffix(projectName);
final ProjectControl control;
try {

View File

@ -14,6 +14,7 @@
package com.google.gerrit.server.project;
import com.google.gerrit.common.ProjectUtil;
import com.google.gerrit.common.data.AccessSection;
import com.google.gerrit.common.data.GroupDescription;
import com.google.gerrit.common.data.GroupReference;
@ -213,20 +214,8 @@ public class CreateProject {
throw new ProjectCreationFailedException("Project name is required");
}
if (createProjectArgs.getProjectName().endsWith(Constants.DOT_GIT_EXT)) {
String nameWithoutSuffix = createProjectArgs.getProjectName();
// Be nice and drop the trailing ".git" suffix, which we never keep
// in our database, but clients might mistakenly provide anyway.
//
nameWithoutSuffix = nameWithoutSuffix.substring(0, //
nameWithoutSuffix.length() - Constants.DOT_GIT_EXT.length());
while (nameWithoutSuffix.endsWith("/")) {
nameWithoutSuffix =
nameWithoutSuffix.substring(0, nameWithoutSuffix.length() - 1);
}
createProjectArgs.setProjectName(nameWithoutSuffix);
}
String nameWithoutSuffix = ProjectUtil.stripGitSuffix(createProjectArgs.getProjectName());
createProjectArgs.setProjectName(nameWithoutSuffix);
if (!currentUser.getCapabilities().canCreateProject()) {
throw new ProjectCreationFailedException(String.format(