From 4fb6af1374106e082a27bd573dedbeb77ca75280 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 09 May 2013 22:49:16 -0400
Subject: [PATCH] Do not include clientapps.json in distribution files
---
src/main/java/com/gitblit/GitBlit.java | 186 ++++++++++++++++++++++++++++++++++++++++------
1 files changed, 161 insertions(+), 25 deletions(-)
diff --git a/src/main/java/com/gitblit/GitBlit.java b/src/main/java/com/gitblit/GitBlit.java
index 4e34db4..9346e0a 100644
--- a/src/main/java/com/gitblit/GitBlit.java
+++ b/src/main/java/com/gitblit/GitBlit.java
@@ -22,6 +22,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
+import java.lang.reflect.Type;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
@@ -71,7 +72,6 @@
import org.eclipse.jgit.lib.RepositoryCache.FileKey;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.storage.file.FileBasedConfig;
-import org.eclipse.jgit.storage.file.WindowCache;
import org.eclipse.jgit.storage.file.WindowCacheConfig;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
@@ -95,10 +95,12 @@
import com.gitblit.models.FederationProposal;
import com.gitblit.models.FederationSet;
import com.gitblit.models.ForkModel;
+import com.gitblit.models.GitClientApplication;
import com.gitblit.models.Metric;
import com.gitblit.models.ProjectModel;
import com.gitblit.models.RegistrantAccessPermission;
import com.gitblit.models.RepositoryModel;
+import com.gitblit.models.RepositoryUrl;
import com.gitblit.models.SearchResult;
import com.gitblit.models.ServerSettings;
import com.gitblit.models.ServerStatus;
@@ -121,6 +123,11 @@
import com.gitblit.utils.X509Utils.X509Metadata;
import com.gitblit.wicket.GitBlitWebSession;
import com.gitblit.wicket.WicketUtils;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonSyntaxException;
+import com.google.gson.reflect.TypeToken;
/**
* GitBlit is the servlet context listener singleton that acts as the core for
@@ -148,6 +155,9 @@
private final List<FederationModel> federationRegistrations = Collections
.synchronizedList(new ArrayList<FederationModel>());
+
+ private final List<GitClientApplication> clientApplications = Collections
+ .synchronizedList(new ArrayList<GitClientApplication>());
private final Map<String, FederationModel> federationPullResults = new ConcurrentHashMap<String, FederationModel>();
@@ -192,7 +202,7 @@
private FanoutService fanoutService;
private GitDaemon gitDaemon;
-
+
public GitBlit() {
if (gitblit == null) {
// set the static singleton reference
@@ -451,21 +461,143 @@
serverStatus.heapFree = Runtime.getRuntime().freeMemory();
return serverStatus;
}
+
+ /**
+ * Returns a list of repository URLs and the user access permission.
+ *
+ * @param request
+ * @param user
+ * @param repository
+ * @return a list of repository urls
+ */
+ public List<RepositoryUrl> getRepositoryUrls(HttpServletRequest request, UserModel user, RepositoryModel repository) {
+ if (user == null) {
+ user = UserModel.ANONYMOUS;
+ }
+ String username = UserModel.ANONYMOUS.equals(user) ? "" : user.username;
+
+ List<RepositoryUrl> list = new ArrayList<RepositoryUrl>();
+ // http/https url
+ if (settings.getBoolean(Keys.git.enableGitServlet, true)) {
+ AccessPermission permission = user.getRepositoryPermission(repository).permission;
+ if (permission.exceeds(AccessPermission.NONE)) {
+ list.add(new RepositoryUrl(getRepositoryUrl(request, username, repository), permission));
+ }
+ }
+
+ // git daemon url
+ String gitDaemonUrl = getGitDaemonUrl(request, user, repository);
+ if (!StringUtils.isEmpty(gitDaemonUrl)) {
+ AccessPermission permission = getGitDaemonAccessPermission(user, repository);
+ if (permission.exceeds(AccessPermission.NONE)) {
+ list.add(new RepositoryUrl(gitDaemonUrl, permission));
+ }
+ }
+
+ // add all other urls
+ // {0} = repository
+ // {1} = username
+ for (String url : settings.getStrings(Keys.web.otherUrls)) {
+ if (url.contains("{1}")) {
+ // external url requires username, only add url IF we have one
+ if(!StringUtils.isEmpty(username)) {
+ list.add(new RepositoryUrl(MessageFormat.format(url, repository.name, username), null));
+ }
+ } else {
+ // external url does not require username
+ list.add(new RepositoryUrl(MessageFormat.format(url, repository.name), null));
+ }
+ }
+ return list;
+ }
+
+ protected String getRepositoryUrl(HttpServletRequest request, String username, RepositoryModel repository) {
+ StringBuilder sb = new StringBuilder();
+ sb.append(HttpUtils.getGitblitURL(request));
+ sb.append(Constants.GIT_PATH);
+ sb.append(repository.name);
+
+ // inject username into repository url if authentication is required
+ if (repository.accessRestriction.exceeds(AccessRestrictionType.NONE)
+ && !StringUtils.isEmpty(username)) {
+ sb.insert(sb.indexOf("://") + 3, username + "@");
+ }
+ return sb.toString();
+ }
+
+ protected String getGitDaemonUrl(HttpServletRequest request, UserModel user, RepositoryModel repository) {
+ if (gitDaemon != null) {
+ String bindInterface = settings.getString(Keys.git.daemonBindInterface, "localhost");
+ if (bindInterface.equals("localhost")
+ && (!request.getServerName().equals("localhost") && !request.getServerName().equals("127.0.0.1"))) {
+ // git daemon is bound to localhost and the request is from elsewhere
+ return null;
+ }
+ if (user.canClone(repository)) {
+ String servername = request.getServerName();
+ String url = gitDaemon.formatUrl(servername, repository.name);
+ return url;
+ }
+ }
+ return null;
+ }
+
+ protected AccessPermission getGitDaemonAccessPermission(UserModel user, RepositoryModel repository) {
+ if (gitDaemon != null && user.canClone(repository)) {
+ AccessPermission gitDaemonPermission = user.getRepositoryPermission(repository).permission;
+ if (gitDaemonPermission.atLeast(AccessPermission.CLONE)) {
+ if (repository.accessRestriction.atLeast(AccessRestrictionType.CLONE)) {
+ // can not authenticate clone via anonymous git protocol
+ gitDaemonPermission = AccessPermission.NONE;
+ } else if (repository.accessRestriction.atLeast(AccessRestrictionType.PUSH)) {
+ // can not authenticate push via anonymous git protocol
+ gitDaemonPermission = AccessPermission.CLONE;
+ } else {
+ // normal user permission
+ }
+ }
+ return gitDaemonPermission;
+ }
+ return AccessPermission.NONE;
+ }
/**
- * Returns the list of non-Gitblit clone urls. This allows Gitblit to
- * advertise alternative urls for Git client repository access.
+ * Returns the list of custom client applications to be used for the
+ * repository url panel;
*
- * @param repositoryName
- * @param userName
- * @return list of non-gitblit clone urls
+ * @return a list of client applications
*/
- public List<String> getOtherCloneUrls(String repositoryName, String username) {
- List<String> cloneUrls = new ArrayList<String>();
- for (String url : settings.getStrings(Keys.web.otherUrls)) {
- cloneUrls.add(MessageFormat.format(url, repositoryName, username));
+ public List<GitClientApplication> getClientApplications() {
+ if (clientApplications.isEmpty()) {
+ try {
+ InputStream is = getClass().getResourceAsStream("/clientapps.json");
+ Collection<GitClientApplication> clients = readClientApplications(is);
+ is.close();
+ if (clients != null) {
+ clientApplications.clear();
+ clientApplications.addAll(clients);
+ }
+ } catch (IOException e) {
+ logger.error("Failed to deserialize clientapps.json resource!", e);
+ }
}
- return cloneUrls;
+ return clientApplications;
+ }
+
+ private Collection<GitClientApplication> readClientApplications(InputStream is) {
+ try {
+ Type type = new TypeToken<Collection<GitClientApplication>>() {
+ }.getType();
+ InputStreamReader reader = new InputStreamReader(is);
+ Gson gson = new GsonBuilder().create();
+ Collection<GitClientApplication> links = gson.fromJson(reader, type);
+ return links;
+ } catch (JsonIOException e) {
+ logger.error("Error deserializing client applications!", e);
+ } catch (JsonSyntaxException e) {
+ logger.error("Error deserializing client applications!", e);
+ }
+ return null;
}
/**
@@ -1295,7 +1427,15 @@
for (String repo : list) {
RepositoryModel model = getRepositoryModel(user, repo);
if (model != null) {
- repositories.add(model);
+ if (!model.hasCommits) {
+ // only add empty repositories that user can push to
+ if (UserModel.ANONYMOUS.canPush(model)
+ || user != null && user.canPush(model)) {
+ repositories.add(model);
+ }
+ } else {
+ repositories.add(model);
+ }
}
}
if (getBoolean(Keys.web.showRepositorySizes, true)) {
@@ -3006,7 +3146,7 @@
*
* @return Map<String, SettingModel>
*/
- private ServerSettings loadSettingModels(InputStream referencePropertiesInputStream) {
+ private ServerSettings loadSettingModels() {
ServerSettings settingsModel = new ServerSettings();
settingsModel.supportsCredentialChanges = userService.supportsCredentialChanges();
settingsModel.supportsDisplayNameChanges = userService.supportsDisplayNameChanges();
@@ -3016,7 +3156,7 @@
// Read bundled Gitblit properties to extract setting descriptions.
// This copy is pristine and only used for populating the setting
// models map.
- InputStream is = referencePropertiesInputStream;
+ InputStream is = getClass().getResourceAsStream("/reference.properties");
BufferedReader propertiesReader = new BufferedReader(new InputStreamReader(is));
StringBuilder description = new StringBuilder();
SettingModel setting = new SettingModel();
@@ -3186,7 +3326,7 @@
cfg.setPackedGitMMAP(settings.getBoolean(Keys.git.packedGitMmap, cfg.isPackedGitMMAP()));
try {
- WindowCache.reconfigure(cfg);
+ cfg.install();
logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.packedGitWindowSize, cfg.getPackedGitWindowSize()));
logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.packedGitLimit, cfg.getPackedGitLimit()));
logger.debug(MessageFormat.format("{0} = {1,number,0}", Keys.git.deltaBaseCacheLimit, cfg.getDeltaBaseCacheLimit()));
@@ -3227,15 +3367,15 @@
}
protected void configureGitDaemon() {
+ int port = settings.getInteger(Keys.git.daemonPort, 0);
String bindInterface = settings.getString(Keys.git.daemonBindInterface, "localhost");
- int port = settings.getInteger(Keys.git.daemonPort, GitDaemon.DEFAULT_PORT);
if (port > 0) {
try {
gitDaemon = new GitDaemon(bindInterface, port, getRepositoriesFolder());
gitDaemon.start();
- logger.info(MessageFormat.format("Git daemon is listening on {0}:{1,number,0}", bindInterface, port));
} catch (IOException e) {
- logger.error(MessageFormat.format("Failed to start Git daemon on {0}:{1,number.0}", bindInterface, port), e);
+ gitDaemon = null;
+ logger.error(MessageFormat.format("Failed to start Git daemon on {0}:{1,number,0}", bindInterface, port), e);
}
}
}
@@ -3266,11 +3406,7 @@
* @see ServletContextListener.contextInitialize(ServletContextEvent)
*/
@Override
- public void contextInitialized(ServletContextEvent contextEvent) {
- contextInitialized(contextEvent, getClass().getResourceAsStream("/reference.properties"));
- }
-
- public void contextInitialized(ServletContextEvent contextEvent, InputStream referencePropertiesInputStream) {
+ public void contextInitialized(ServletContextEvent contextEvent) {
servletContext = contextEvent.getServletContext();
if (settings == null) {
// Gitblit is running in a servlet container
@@ -3335,7 +3471,7 @@
}
}
- settingsModel = loadSettingModels(referencePropertiesInputStream);
+ settingsModel = loadSettingModels();
serverStatus.servletContainer = servletContext.getServerInfo();
}
--
Gitblit v1.9.1