From 793f76563d4bb3f58fa62ff53985e20561c6e330 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 01 Jun 2011 21:01:51 -0400
Subject: [PATCH] Refactored some unit tests and utils.
---
src/com/gitblit/GitBlit.java | 238 ++++++++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 178 insertions(+), 60 deletions(-)
diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java
index d0daa53..7182d9b 100644
--- a/src/com/gitblit/GitBlit.java
+++ b/src/com/gitblit/GitBlit.java
@@ -1,32 +1,48 @@
+/*
+ * Copyright 2011 gitblit.com.
+ *
+ * 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.gitblit;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
-import javax.servlet.http.Cookie;
-import org.apache.wicket.protocol.http.WebResponse;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.resolver.FileResolver;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
+import org.eclipse.jgit.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.gitblit.Constants.AccessRestrictionType;
+import com.gitblit.models.RepositoryModel;
+import com.gitblit.models.UserModel;
import com.gitblit.utils.JGitUtils;
-import com.gitblit.wicket.models.RepositoryModel;
-import com.gitblit.wicket.models.UserModel;
+import com.gitblit.utils.StringUtils;
public class GitBlit implements ServletContextListener {
- private final static GitBlit gitblit;
+ private static final GitBlit GITBLIT;
private final Logger logger = LoggerFactory.getLogger(GitBlit.class);
@@ -41,26 +57,46 @@
private IStoredSettings storedSettings;
static {
- gitblit = new GitBlit();
- }
-
- public static GitBlit self() {
- return gitblit;
+ GITBLIT = new GitBlit();
}
private GitBlit() {
}
- public IStoredSettings settings() {
- return storedSettings;
+ public static GitBlit self() {
+ return GITBLIT;
+ }
+
+ public static boolean getBoolean(String key, boolean defaultValue) {
+ return GITBLIT.storedSettings.getBoolean(key, defaultValue);
+ }
+
+ public static int getInteger(String key, int defaultValue) {
+ return GITBLIT.storedSettings.getInteger(key, defaultValue);
+ }
+
+ public static String getString(String key, String defaultValue) {
+ return GITBLIT.storedSettings.getString(key, defaultValue);
+ }
+
+ public static List<String> getStrings(String key) {
+ return GITBLIT.storedSettings.getStrings(key);
+ }
+
+ public static List<String> getAllKeys(String startingWith) {
+ return GITBLIT.storedSettings.getAllKeys(startingWith);
}
public boolean isDebugMode() {
return storedSettings.getBoolean(Keys.web.debugMode, false);
}
- public String getCloneUrl(String repositoryName) {
- return storedSettings.getString(Keys.git.cloneUrl, "https://localhost/git/") + repositoryName;
+ public List<String> getOtherCloneUrls(String repositoryName) {
+ List<String> cloneUrls = new ArrayList<String>();
+ for (String url : storedSettings.getStrings(Keys.web.otherUrls)) {
+ cloneUrls.add(MessageFormat.format(url, repositoryName));
+ }
+ return cloneUrls;
}
public void setLoginService(ILoginService loginService) {
@@ -74,41 +110,39 @@
return loginService.authenticate(username, password);
}
- public UserModel authenticate(Cookie[] cookies) {
- if (loginService == null) {
- return null;
- }
- if (cookies != null && cookies.length > 0) {
- for (Cookie cookie : cookies) {
- if (cookie.getName().equals(Constants.NAME)) {
- String value = cookie.getValue();
- return loginService.authenticate(value.toCharArray());
- }
- }
- }
- return null;
+ public List<String> getAllUsernames() {
+ List<String> names = new ArrayList<String>(loginService.getAllUsernames());
+ Collections.sort(names);
+ return names;
}
- public void setCookie(WebResponse response, UserModel user) {
- Cookie userCookie = new Cookie(Constants.NAME, user.getCookie());
- userCookie.setMaxAge(Integer.MAX_VALUE);
- userCookie.setPath("/");
- response.addCookie(userCookie);
+ public boolean deleteUser(String username) {
+ return loginService.deleteUser(username);
}
- public UserModel getUser(String username) {
+ public UserModel getUserModel(String username) {
UserModel user = loginService.getUserModel(username);
return user;
}
- public void editUserModel(UserModel user, boolean isCreate) throws GitBlitException {
- if (!loginService.updateUserModel(user)) {
+ public List<String> getRepositoryUsers(RepositoryModel repository) {
+ return loginService.getUsernamesForRole(repository.name);
+ }
+
+ public boolean setRepositoryUsers(RepositoryModel repository, List<String> repositoryUsers) {
+ return loginService.setUsernamesForRole(repository.name, repositoryUsers);
+ }
+
+ public void editUserModel(String username, UserModel user, boolean isCreate)
+ throws GitBlitException {
+ if (!loginService.updateUserModel(username, user)) {
throw new GitBlitException(isCreate ? "Failed to add user!" : "Failed to update user!");
}
}
public List<String> getRepositoryList() {
- return JGitUtils.getRepositoryList(repositoriesFolder, exportAll, storedSettings.getBoolean(Keys.git.nestedRepositories, true));
+ return JGitUtils.getRepositoryList(repositoriesFolder, exportAll,
+ storedSettings.getBoolean(Keys.git.nestedRepositories, true));
}
public Repository getRepository(String repositoryName) {
@@ -117,7 +151,8 @@
r = repositoryResolver.open(null, repositoryName);
} catch (RepositoryNotFoundException e) {
r = null;
- logger.error("GitBlit.getRepository(String) failed to find repository " + repositoryName);
+ logger.error("GitBlit.getRepository(String) failed to find "
+ + new File(repositoriesFolder, repositoryName).getAbsolutePath());
} catch (ServiceNotEnabledException e) {
r = null;
e.printStackTrace();
@@ -136,11 +171,11 @@
}
return repositories;
}
-
+
public RepositoryModel getRepositoryModel(UserModel user, String repositoryName) {
RepositoryModel model = getRepositoryModel(repositoryName);
if (model.accessRestriction.atLeast(AccessRestrictionType.VIEW)) {
- if (user != null && user.canView(model)) {
+ if (user != null && user.canAccessRepository(model.name)) {
return model;
}
return null;
@@ -151,33 +186,81 @@
public RepositoryModel getRepositoryModel(String repositoryName) {
Repository r = getRepository(repositoryName);
+ if (r == null) {
+ return null;
+ }
RepositoryModel model = new RepositoryModel();
model.name = repositoryName;
model.hasCommits = JGitUtils.hasCommits(r);
model.lastChange = JGitUtils.getLastChange(r);
StoredConfig config = JGitUtils.readConfig(r);
if (config != null) {
- model.description = config.getString("gitblit", null, "description");
- model.owner = config.getString("gitblit", null, "owner");
- model.useTickets = config.getBoolean("gitblit", "useTickets", false);
- model.useDocs = config.getBoolean("gitblit", "useDocs", false);
- model.accessRestriction = AccessRestrictionType.fromString(config.getString("gitblit", null, "accessRestriction"));
- model.showRemoteBranches = config.getBoolean("gitblit", "showRemoteBranches", false);
+ model.description = getConfig(config, "description", "");
+ model.owner = getConfig(config, "owner", "");
+ model.useTickets = getConfig(config, "useTickets", false);
+ model.useDocs = getConfig(config, "useDocs", false);
+ model.accessRestriction = AccessRestrictionType.fromName(getConfig(config,
+ "accessRestriction", null));
+ model.showRemoteBranches = getConfig(config, "showRemoteBranches", false);
+ model.isFrozen = getConfig(config, "isFrozen", false);
+ model.showReadme = getConfig(config, "showReadme", false);
}
r.close();
return model;
}
- public void editRepositoryModel(RepositoryModel repository, boolean isCreate) throws GitBlitException {
+ private String getConfig(StoredConfig config, String field, String defaultValue) {
+ String value = config.getString("gitblit", null, field);
+ if (StringUtils.isEmpty(value)) {
+ return defaultValue;
+ }
+ return value;
+ }
+
+ private boolean getConfig(StoredConfig config, String field, boolean defaultValue) {
+ return config.getBoolean("gitblit", field, defaultValue);
+ }
+
+ public void editRepositoryModel(String repositoryName, RepositoryModel repository,
+ boolean isCreate) throws GitBlitException {
Repository r = null;
if (isCreate) {
+ // ensure created repository name ends with .git
+ if (!repository.name.endsWith(org.eclipse.jgit.lib.Constants.DOT_GIT_EXT)) {
+ repository.name += org.eclipse.jgit.lib.Constants.DOT_GIT_EXT;
+ }
if (new File(repositoriesFolder, repository.name).exists()) {
- throw new GitBlitException(MessageFormat.format("Can not create repository {0} because it already exists.", repository.name));
+ throw new GitBlitException(MessageFormat.format(
+ "Can not create repository ''{0}'' because it already exists.",
+ repository.name));
}
// create repository
logger.info("create repository " + repository.name);
r = JGitUtils.createRepository(repositoriesFolder, repository.name, true);
} else {
+ // rename repository
+ if (!repositoryName.equalsIgnoreCase(repository.name)) {
+ File folder = new File(repositoriesFolder, repositoryName);
+ File destFolder = new File(repositoriesFolder, repository.name);
+ if (destFolder.exists()) {
+ throw new GitBlitException(
+ MessageFormat
+ .format("Can not rename repository ''{0}'' to ''{1}'' because ''{1}'' already exists.",
+ repositoryName, repository.name));
+ }
+ if (!folder.renameTo(destFolder)) {
+ throw new GitBlitException(MessageFormat.format(
+ "Failed to rename repository ''{0}'' to ''{1}''.", repositoryName,
+ repository.name));
+ }
+ // rename the roles
+ if (!loginService.renameRole(repositoryName, repository.name)) {
+ throw new GitBlitException(MessageFormat.format(
+ "Failed to rename repository permissions ''{0}'' to ''{1}''.",
+ repositoryName, repository.name));
+ }
+ }
+
// load repository
logger.info("edit repository " + repository.name);
try {
@@ -190,23 +273,58 @@
}
// update settings
- StoredConfig config = JGitUtils.readConfig(r);
- config.setString("gitblit", null, "description", repository.description);
- config.setString("gitblit", null, "owner", repository.owner);
- config.setBoolean("gitblit", null, "useTickets", repository.useTickets);
- config.setBoolean("gitblit", null, "useDocs", repository.useDocs);
- config.setString("gitblit", null, "accessRestriction", repository.accessRestriction.toString());
- config.setBoolean("gitblit", null, "showRemoteBranches", repository.showRemoteBranches);
- try {
- config.save();
- } catch (IOException e) {
- logger.error("Failed to save repository config!", e);
+ if (r != null) {
+ StoredConfig config = JGitUtils.readConfig(r);
+ config.setString("gitblit", null, "description", repository.description);
+ config.setString("gitblit", null, "owner", repository.owner);
+ config.setBoolean("gitblit", null, "useTickets", repository.useTickets);
+ config.setBoolean("gitblit", null, "useDocs", repository.useDocs);
+ config.setString("gitblit", null, "accessRestriction",
+ repository.accessRestriction.name());
+ config.setBoolean("gitblit", null, "showRemoteBranches", repository.showRemoteBranches);
+ config.setBoolean("gitblit", null, "isFrozen", repository.isFrozen);
+ config.setBoolean("gitblit", null, "showReadme", repository.showReadme);
+ try {
+ config.save();
+ } catch (IOException e) {
+ logger.error("Failed to save repository config!", e);
+ }
+ r.close();
}
- r.close();
+ }
+
+ public boolean deleteRepositoryModel(RepositoryModel model) {
+ return deleteRepository(model.name);
+ }
+
+ public boolean deleteRepository(String repositoryName) {
+ try {
+ File folder = new File(repositoriesFolder, repositoryName);
+ if (folder.exists() && folder.isDirectory()) {
+ FileUtils.delete(folder, FileUtils.RECURSIVE);
+ if (loginService.deleteRole(repositoryName)) {
+ return true;
+ }
+ }
+ } catch (Throwable t) {
+ logger.error(MessageFormat.format("Failed to delete repository {0}", repositoryName), t);
+ }
+ return false;
+ }
+
+ public boolean renameRepository(RepositoryModel model, String newName) {
+ File folder = new File(repositoriesFolder, model.name);
+ if (folder.exists() && folder.isDirectory()) {
+ File newFolder = new File(repositoriesFolder, newName);
+ if (folder.renameTo(newFolder)) {
+ return loginService.renameRole(model.name, newName);
+ }
+ }
+ return false;
}
public void configureContext(IStoredSettings settings) {
- logger.info("Configure GitBlit from " + settings.toString());
+ logger.info("Reading configuration from " + settings.toString());
this.storedSettings = settings;
repositoriesFolder = new File(settings.getString(Keys.git.repositoriesFolder, "repos"));
exportAll = settings.getBoolean(Keys.git.exportAll, true);
--
Gitblit v1.9.1