From cb946fa57e9dd4ca0853f079331c73dc0331c1e7 Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Wed, 10 Jul 2013 09:14:29 +0200 Subject: [PATCH] Refactor logic for user repository path into one class. Gather logic for building the path/name of a personal user repository into one class. --- src/main/java/com/gitblit/GitBlit.java | 13 ++--- .../com/gitblit/client/EditTeamDialog.java | 5 +- .../com/gitblit/client/EditUserDialog.java | 3 +- .../java/com/gitblit/models/ProjectModel.java | 3 +- .../com/gitblit/models/RepositoryModel.java | 5 +- .../java/com/gitblit/models/UserModel.java | 7 +-- .../java/com/gitblit/utils/ModelUtils.java | 52 +++++++++++++++++++ .../com/gitblit/wicket/pages/RootPage.java | 3 +- .../com/gitblit/wicket/pages/RootSubPage.java | 3 +- .../com/gitblit/wicket/pages/UserPage.java | 2 +- .../wicket/panels/RepositoriesPanel.java | 5 +- 11 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/gitblit/utils/ModelUtils.java diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java index 6ea348d5..608ae7c0 100644 --- a/src/main/java/com/gitblit/GitBlit.java +++ b/src/main/java/com/gitblit/GitBlit.java @@ -125,6 +125,7 @@ import com.gitblit.utils.JGitUtils; import com.gitblit.utils.JGitUtils.LastChange; import com.gitblit.utils.JsonUtils; import com.gitblit.utils.MetricUtils; +import com.gitblit.utils.ModelUtils; import com.gitblit.utils.ObjectCache; import com.gitblit.utils.StringUtils; import com.gitblit.utils.TimeUtils; @@ -1243,8 +1244,8 @@ public class GitBlit implements ServletContextListener { // personal repository model.addOwner(user.username); String oldRepositoryName = model.name; - model.name = "~" + user.username + model.name.substring(model.projectPath.length()); - model.projectPath = "~" + user.username; + model.name = user.getPersonalPath() + model.name.substring(model.projectPath.length()); + model.projectPath = user.getPersonalPath(); updateRepositoryModel(oldRepositoryName, model, false); } else if (model.isOwner(username)) { // common/shared repo @@ -1853,8 +1854,8 @@ public class GitBlit implements ServletContextListener { ProjectModel project = configs.get(name.toLowerCase()); if (project == null) { project = new ProjectModel(name); - if (name.length() > 0 && name.charAt(0) == '~') { - UserModel user = getUserModel(name.substring(1)); + if (ModelUtils.isPersonalRepository(name)) { + UserModel user = getUserModel(ModelUtils.getUserNameFromRepoPath(name)); if (user != null) { project.title = user.getDisplayName(); project.description = "personal repositories"; @@ -2126,7 +2127,7 @@ public class GitBlit implements ServletContextListener { * @return the name of the user's fork, null otherwise */ public String getFork(String username, String origin) { - String userProject = "~" + username.toLowerCase(); + String userProject = ModelUtils.getPersonalPath(username); if (settings.getBoolean(Keys.git.cacheRepositoryList, true)) { String userPath = userProject + "/"; @@ -3818,7 +3819,7 @@ public class GitBlit implements ServletContextListener { * @throws GitBlitException */ public RepositoryModel fork(RepositoryModel repository, UserModel user) throws GitBlitException { - String cloneName = MessageFormat.format("~{0}/{1}.git", user.username, StringUtils.stripDotGit(StringUtils.getLastPathElement(repository.name))); + String cloneName = MessageFormat.format("{0}/{1}.git", user.getPersonalPath(), StringUtils.stripDotGit(StringUtils.getLastPathElement(repository.name))); String fromUrl = MessageFormat.format("file://{0}/{1}", repositoriesFolder.getAbsolutePath(), repository.name); // clone the repository diff --git a/src/main/java/com/gitblit/client/EditTeamDialog.java b/src/main/java/com/gitblit/client/EditTeamDialog.java index 4d7af261..4bd032e1 100644 --- a/src/main/java/com/gitblit/client/EditTeamDialog.java +++ b/src/main/java/com/gitblit/client/EditTeamDialog.java @@ -51,6 +51,7 @@ import com.gitblit.models.RegistrantAccessPermission; import com.gitblit.models.RepositoryModel; import com.gitblit.models.ServerSettings; import com.gitblit.models.TeamModel; +import com.gitblit.utils.ModelUtils; import com.gitblit.utils.StringUtils; public class EditTeamDialog extends JDialog { @@ -177,7 +178,7 @@ public class EditTeamDialog extends JDialog { postReceivePalette = new JPalette(true); postReceiveInherited = new JLabel(); JPanel postReceivePanel = new JPanel(new BorderLayout(5, 5)); - postReceivePanel.add(postReceivePalette, BorderLayout.CENTER); + postReceivePanel.add(postReceivePalette, BorderLayout.CENTER); postReceivePanel.add(postReceiveInherited, BorderLayout.WEST); JTabbedPane panel = new JTabbedPane(JTabbedPane.TOP); @@ -323,7 +324,7 @@ public class EditTeamDialog extends JDialog { // repositories list.add(".*"); // all repositories excluding personal repositories - list.add("[^~].*"); + if (ModelUtils.getUserRepoPrefix().length() == 1) list.add("[^" + ModelUtils.getUserRepoPrefix() +"].*"); String lastProject = null; for (String repo : restricted) { String projectPath = StringUtils.getFirstPathElement(repo); diff --git a/src/main/java/com/gitblit/client/EditUserDialog.java b/src/main/java/com/gitblit/client/EditUserDialog.java index 0400f5c9..f1d9aa6f 100644 --- a/src/main/java/com/gitblit/client/EditUserDialog.java +++ b/src/main/java/com/gitblit/client/EditUserDialog.java @@ -57,6 +57,7 @@ import com.gitblit.models.RepositoryModel; import com.gitblit.models.ServerSettings; import com.gitblit.models.TeamModel; import com.gitblit.models.UserModel; +import com.gitblit.utils.ModelUtils; import com.gitblit.utils.StringUtils; public class EditUserDialog extends JDialog { @@ -403,7 +404,7 @@ public class EditUserDialog extends JDialog { // repositories list.add(".*"); // all repositories excluding personal repositories - list.add("[^~].*"); + if (ModelUtils.getUserRepoPrefix().length() == 1) list.add("[^" + ModelUtils.getUserRepoPrefix() +"].*"); String lastProject = null; for (String repo : restricted) { String projectPath = StringUtils.getFirstPathElement(repo).toLowerCase(); diff --git a/src/main/java/com/gitblit/models/ProjectModel.java b/src/main/java/com/gitblit/models/ProjectModel.java index 9e5d5233..cd912aea 100644 --- a/src/main/java/com/gitblit/models/ProjectModel.java +++ b/src/main/java/com/gitblit/models/ProjectModel.java @@ -21,6 +21,7 @@ import java.util.Date; import java.util.HashSet; import java.util.Set; +import com.gitblit.utils.ModelUtils; import com.gitblit.utils.StringUtils; /** @@ -57,7 +58,7 @@ public class ProjectModel implements Serializable, Comparable { } public boolean isUserProject() { - return name.charAt(0) == '~'; + return ModelUtils.isPersonalRepository(name); } public boolean hasRepository(String name) { diff --git a/src/main/java/com/gitblit/models/RepositoryModel.java b/src/main/java/com/gitblit/models/RepositoryModel.java index 29962658..e28c9df3 100644 --- a/src/main/java/com/gitblit/models/RepositoryModel.java +++ b/src/main/java/com/gitblit/models/RepositoryModel.java @@ -28,6 +28,7 @@ import com.gitblit.Constants.AccessRestrictionType; import com.gitblit.Constants.AuthorizationControl; import com.gitblit.Constants.FederationStrategy; import com.gitblit.utils.ArrayUtils; +import com.gitblit.utils.ModelUtils; import com.gitblit.utils.StringUtils; /** @@ -177,11 +178,11 @@ public class RepositoryModel implements Serializable, Comparable } if (canCreate) { String projectPath = StringUtils.getFirstPathElement(repository); - if (!StringUtils.isEmpty(projectPath) && projectPath.equalsIgnoreCase("~" + username)) { + if (!StringUtils.isEmpty(projectPath) && projectPath.equalsIgnoreCase(getPersonalPath())) { // personal repository return true; } @@ -609,7 +610,7 @@ public class UserModel implements Principal, Serializable, Comparable } public String getPersonalPath() { - return "~" + username; + return ModelUtils.getPersonalPath(username); } public UserPreferences getPreferences() { @@ -675,6 +676,6 @@ public class UserModel implements Principal, Serializable, Comparable public boolean isMyPersonalRepository(String repository) { String projectPath = StringUtils.getFirstPathElement(repository); - return !StringUtils.isEmpty(projectPath) && projectPath.equalsIgnoreCase("~" + username); + return !StringUtils.isEmpty(projectPath) && projectPath.equalsIgnoreCase(getPersonalPath()); } } diff --git a/src/main/java/com/gitblit/utils/ModelUtils.java b/src/main/java/com/gitblit/utils/ModelUtils.java new file mode 100644 index 00000000..6316dc5d --- /dev/null +++ b/src/main/java/com/gitblit/utils/ModelUtils.java @@ -0,0 +1,52 @@ +package com.gitblit.utils; + +import com.gitblit.IStoredSettings; + +public class ModelUtils +{ + private static final String DEFAULT_USER_REPO_PREFIX = "~"; + + private static String userRepoPrefix = DEFAULT_USER_REPO_PREFIX; + + + + public static void setUserRepoPrefix(IStoredSettings settings) + { + userRepoPrefix = settings.getString("repo.userPrefix", DEFAULT_USER_REPO_PREFIX); + } + + + public static String getUserRepoPrefix() + { + return userRepoPrefix; + } + + + public static String getPersonalPath(String username) + { + return userRepoPrefix + username.toLowerCase(); + } + + + public static boolean isPersonalRepository(String name) + { + if ( name.startsWith(getUserRepoPrefix()) ) return true; + return false; + } + + + public static boolean isUsersPersonalRepository(String username, String name) + { + if ( name.equalsIgnoreCase(getPersonalPath(username)) ) return true; + return false; + } + + + public static String getUserNameFromRepoPath(String path) + { + if ( !isPersonalRepository(path) ) return ""; + + return path.substring(getUserRepoPrefix().length()); + } + +} diff --git a/src/main/java/com/gitblit/wicket/pages/RootPage.java b/src/main/java/com/gitblit/wicket/pages/RootPage.java index a81d63f4..ec4e9b43 100644 --- a/src/main/java/com/gitblit/wicket/pages/RootPage.java +++ b/src/main/java/com/gitblit/wicket/pages/RootPage.java @@ -51,6 +51,7 @@ import com.gitblit.Keys; import com.gitblit.models.RepositoryModel; import com.gitblit.models.TeamModel; import com.gitblit.models.UserModel; +import com.gitblit.utils.ModelUtils; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.PageRegistration; @@ -393,7 +394,7 @@ public abstract class RootPage extends BasePage { String userName = WicketUtils.getUsername(params); if (StringUtils.isEmpty(projectName)) { if (!StringUtils.isEmpty(userName)) { - projectName = "~" + userName; + projectName = ModelUtils.getPersonalPath(userName); } } String repositoryName = WicketUtils.getRepositoryName(params); diff --git a/src/main/java/com/gitblit/wicket/pages/RootSubPage.java b/src/main/java/com/gitblit/wicket/pages/RootSubPage.java index e7e12ccc..17ba4b27 100644 --- a/src/main/java/com/gitblit/wicket/pages/RootSubPage.java +++ b/src/main/java/com/gitblit/wicket/pages/RootSubPage.java @@ -27,6 +27,7 @@ import com.gitblit.Constants.AuthorizationControl; import com.gitblit.GitBlit; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; +import com.gitblit.utils.ModelUtils; import com.gitblit.utils.StringUtils; /** @@ -80,7 +81,7 @@ public abstract class RootSubPage extends RootPage { // all repositories repos.add(".*"); // all repositories excluding personal repositories - repos.add("[^~].*"); + if (ModelUtils.getUserRepoPrefix().length() == 1) repos.add("[^" + ModelUtils.getUserRepoPrefix() +"].*"); } for (String repo : GitBlit.self().getRepositoryList()) { diff --git a/src/main/java/com/gitblit/wicket/pages/UserPage.java b/src/main/java/com/gitblit/wicket/pages/UserPage.java index f4331dd1..4faee20b 100644 --- a/src/main/java/com/gitblit/wicket/pages/UserPage.java +++ b/src/main/java/com/gitblit/wicket/pages/UserPage.java @@ -84,7 +84,7 @@ public class UserPage extends RootPage { user = new UserModel(userName); } - String projectName = "~" + userName; + String projectName = user.getPersonalPath(); ProjectModel project = GitBlit.self().getProjectModel(projectName); if (project == null) { diff --git a/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.java b/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.java index 1b7d0e0e..256cc645 100644 --- a/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.java +++ b/src/main/java/com/gitblit/wicket/panels/RepositoriesPanel.java @@ -50,6 +50,7 @@ import com.gitblit.models.ProjectModel; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.utils.ArrayUtils; +import com.gitblit.utils.ModelUtils; import com.gitblit.utils.StringUtils; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.WicketUtils; @@ -169,9 +170,9 @@ public class RepositoriesPanel extends BasePanel { item.add(row); String name = groupRow.name; - if (name.charAt(0) == '~') { + if (name.startsWith(ModelUtils.getUserRepoPrefix())) { // user page - String username = name.substring(1); + String username = ModelUtils.getUserNameFromRepoPath(name); UserModel user = GitBlit.self().getUserModel(username); row.add(new LinkPanel("groupName", null, (user == null ? username : user.getDisplayName()) + " (" + groupRow.count + ")", UserPage.class, WicketUtils.newUsernameParameter(username))); row.add(new Label("groupDescription", getString("gb.personalRepositories"))); -- 2.39.5