From 1d78b8b372f15d89f10fd32cb0227a6a7966de3c Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 17 Apr 2014 23:08:07 -0400
Subject: [PATCH] [findbugs] Clarify class used for resource loading

---
 src/main/java/com/gitblit/manager/GitblitManager.java |  229 ++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 203 insertions(+), 26 deletions(-)

diff --git a/src/main/java/com/gitblit/manager/GitblitManager.java b/src/main/java/com/gitblit/manager/GitblitManager.java
index 95d50ac..790445a 100644
--- a/src/main/java/com/gitblit/manager/GitblitManager.java
+++ b/src/main/java/com/gitblit/manager/GitblitManager.java
@@ -33,9 +33,18 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.eclipse.jgit.api.CloneCommand;
+import org.eclipse.jgit.api.FetchCommand;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.transport.RefSpec;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
+import ro.fortsoft.pf4j.PluginState;
+import ro.fortsoft.pf4j.PluginWrapper;
+import ro.fortsoft.pf4j.Version;
 
 import com.gitblit.Constants;
 import com.gitblit.Constants.AccessPermission;
@@ -50,7 +59,11 @@
 import com.gitblit.models.FederationSet;
 import com.gitblit.models.ForkModel;
 import com.gitblit.models.GitClientApplication;
+import com.gitblit.models.Mailing;
 import com.gitblit.models.Metric;
+import com.gitblit.models.PluginRegistry.InstallState;
+import com.gitblit.models.PluginRegistry.PluginRegistration;
+import com.gitblit.models.PluginRegistry.PluginRelease;
 import com.gitblit.models.ProjectModel;
 import com.gitblit.models.RegistrantAccessPermission;
 import com.gitblit.models.RepositoryModel;
@@ -61,9 +74,11 @@
 import com.gitblit.models.SettingModel;
 import com.gitblit.models.TeamModel;
 import com.gitblit.models.UserModel;
+import com.gitblit.tickets.ITicketService;
+import com.gitblit.transport.ssh.IPublicKeyManager;
+import com.gitblit.transport.ssh.SshKey;
 import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.HttpUtils;
-import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.JsonUtils;
 import com.gitblit.utils.ObjectCache;
 import com.gitblit.utils.StringUtils;
@@ -94,11 +109,15 @@
 
 	protected final IRuntimeManager runtimeManager;
 
+	protected final IPluginManager pluginManager;
+
 	protected final INotificationManager notificationManager;
 
 	protected final IUserManager userManager;
 
 	protected final IAuthenticationManager authenticationManager;
+
+	protected final IPublicKeyManager publicKeyManager;
 
 	protected final IRepositoryManager repositoryManager;
 
@@ -108,18 +127,22 @@
 
 	public GitblitManager(
 			IRuntimeManager runtimeManager,
+			IPluginManager pluginManager,
 			INotificationManager notificationManager,
 			IUserManager userManager,
 			IAuthenticationManager authenticationManager,
+			IPublicKeyManager publicKeyManager,
 			IRepositoryManager repositoryManager,
 			IProjectManager projectManager,
 			IFederationManager federationManager) {
 
 		this.settings = runtimeManager.getSettings();
 		this.runtimeManager = runtimeManager;
+		this.pluginManager = pluginManager;
 		this.notificationManager = notificationManager;
 		this.userManager = userManager;
 		this.authenticationManager = authenticationManager;
+		this.publicKeyManager = publicKeyManager;
 		this.repositoryManager = repositoryManager;
 		this.projectManager = projectManager;
 		this.federationManager = federationManager;
@@ -157,7 +180,33 @@
 
 		// clone the repository
 		try {
-			JGitUtils.cloneRepository(repositoryManager.getRepositoriesFolder(), cloneName, fromUrl, true, null);
+			Repository canonical = getRepository(repository.name);
+			File folder = new File(repositoryManager.getRepositoriesFolder(), cloneName);
+			CloneCommand clone = new CloneCommand();
+			clone.setBare(true);
+
+			// fetch branches with exclusions
+			Collection<Ref> branches = canonical.getRefDatabase().getRefs(Constants.R_HEADS).values();
+			List<String> branchesToClone = new ArrayList<String>();
+			for (Ref branch : branches) {
+				String name = branch.getName();
+				if (name.startsWith(Constants.R_TICKET)) {
+					// exclude ticket branches
+					continue;
+				}
+				branchesToClone.add(name);
+			}
+			clone.setBranchesToClone(branchesToClone);
+			clone.setURI(fromUrl);
+			clone.setDirectory(folder);
+			Git git = clone.call();
+
+			// fetch tags
+			FetchCommand fetch  = git.fetch();
+			fetch.setRefSpecs(new RefSpec("+refs/tags/*:refs/tags/*"));
+			fetch.call();
+
+			git.getRepository().close();
 		} catch (Exception e) {
 			throw new GitBlitException(e);
 		}
@@ -172,7 +221,8 @@
 		if (!ArrayUtils.isEmpty(repository.owners)) {
 			for (String owner : repository.owners) {
 				UserModel originOwner = userManager.getUserModel(owner);
-				if (originOwner != null) {
+				if (originOwner != null && !originOwner.canClone(cloneModel)) {
+					// origin owner can't yet clone fork, grant explicit clone access
 					originOwner.setRepositoryPermission(cloneName, AccessPermission.CLONE);
 					reviseUser(originOwner.username, originOwner);
 				}
@@ -185,8 +235,8 @@
 		for (String name : users) {
 			if (!name.equalsIgnoreCase(user.username)) {
 				UserModel cloneUser = userManager.getUserModel(name);
-				if (cloneUser.canClone(repository)) {
-					// origin user can clone origin, grant clone access to fork
+				if (cloneUser.canClone(repository) && !cloneUser.canClone(cloneModel)) {
+					// origin user can't yet clone fork, grant explicit clone access
 					cloneUser.setRepositoryPermission(cloneName, AccessPermission.CLONE);
 				}
 				cloneUsers.add(cloneUser);
@@ -199,8 +249,8 @@
 		List<TeamModel> cloneTeams = new ArrayList<TeamModel>();
 		for (String name : teams) {
 			TeamModel cloneTeam = userManager.getTeamModel(name);
-			if (cloneTeam.canClone(repository)) {
-				// origin team can clone origin, grant clone access to fork
+			if (cloneTeam.canClone(repository) && !cloneTeam.canClone(cloneModel)) {
+				// origin team can't yet clone fork, grant explicit clone access
 				cloneTeam.setRepositoryPermission(cloneName, AccessPermission.CLONE);
 			}
 			cloneTeams.add(cloneTeam);
@@ -289,6 +339,9 @@
 					repositoryManager.updateRepositoryModel(model.name, model, false);
 				}
 			}
+
+			// rename the user's ssh public keystore
+			getPublicKeyManager().renameUser(username, user.username);
 		}
 		if (!userManager.updateUserModel(username, user)) {
 			throw new GitBlitException("Failed to update user!");
@@ -388,7 +441,7 @@
 		// no user definitions, use system definitions
 		if (!clientApplications.hasCurrent("system", new Date(0))) {
 			try {
-				InputStream is = getClass().getResourceAsStream("/clientapps.json");
+				InputStream is = GitblitManager.class.getResourceAsStream("/clientapps.json");
 				Collection<GitClientApplication> clients = readClientApplications(is);
 				is.close();
 				if (clients != null) {
@@ -430,7 +483,7 @@
 			// Read bundled Gitblit properties to extract setting descriptions.
 			// This copy is pristine and only used for populating the setting
 			// models map.
-			InputStream is = getClass().getResourceAsStream("/reference.properties");
+			InputStream is = GitblitManager.class.getResourceAsStream("/reference.properties");
 			BufferedReader propertiesReader = new BufferedReader(new InputStreamReader(is));
 			StringBuilder description = new StringBuilder();
 			SettingModel setting = new SettingModel();
@@ -479,6 +532,20 @@
 		} catch (IOException e) {
 			logger.error("Failed to load resource copy of gitblit.properties");
 		}
+	}
+
+	/**
+	 * Throws an exception if trying to get a ticket service.
+	 *
+	 */
+	@Override
+	public ITicketService getTicketService() {
+		throw new RuntimeException("This class does not have a ticket service!");
+	}
+
+	@Override
+	public IPublicKeyManager getPublicKeyManager() {
+		return publicKeyManager;
 	}
 
 	/*
@@ -583,28 +650,13 @@
 	}
 
 	@Override
-	public void sendMail(String subject, String message, String... toAddresses) {
-		notificationManager.sendMail(subject, message, toAddresses);
-	}
-
-	@Override
 	public void sendHtmlMail(String subject, String message, Collection<String> toAddresses) {
 		notificationManager.sendHtmlMail(subject, message, toAddresses);
 	}
 
 	@Override
-	public void sendHtmlMail(String subject, String message, String... toAddresses) {
-		notificationManager.sendHtmlMail(subject, message, toAddresses);
-	}
-
-	@Override
-	public void sendHtmlMail(String from, String subject, String message, Collection<String> toAddresses) {
-		notificationManager.sendHtmlMail(from, subject, message, toAddresses);
-	}
-
-	@Override
-	public void sendHtmlMail(String from, String subject, String message, String... toAddresses) {
-		notificationManager.sendHtmlMail(from, subject, message, toAddresses);
+	public void send(Mailing mail) {
+		notificationManager.send(mail);
 	}
 
 	/*
@@ -624,6 +676,12 @@
 		}
 		return user;
 	}
+
+	@Override
+	public UserModel authenticate(String username, SshKey key) {
+		return authenticationManager.authenticate(username, key);
+	}
+
 	@Override
 	public UserModel authenticate(HttpServletRequest httpRequest, boolean requiresCertificate) {
 		UserModel user = authenticationManager.authenticate(httpRequest, requiresCertificate);
@@ -1112,4 +1170,123 @@
 	public boolean deletePendingFederationProposal(FederationProposal proposal) {
 		return federationManager.deletePendingFederationProposal(proposal);
 	}
+
+	@Override
+	public void closeAll() {
+		repositoryManager.closeAll();
+	}
+
+	@Override
+	public void close(String repository) {
+		repositoryManager.close(repository);
+	}
+
+	@Override
+	public boolean isIdle(Repository repository) {
+		return repositoryManager.isIdle(repository);
+	}
+
+	/*
+	 * PLUGIN MANAGER
+	 */
+
+	@Override
+	public Version getSystemVersion() {
+		return pluginManager.getSystemVersion();
+	}
+
+	@Override
+	public void startPlugins() {
+		pluginManager.startPlugins();
+	}
+
+	@Override
+	public void stopPlugins() {
+		pluginManager.stopPlugins();
+	}
+
+	@Override
+	public List<PluginWrapper> getPlugins() {
+		return pluginManager.getPlugins();
+	}
+
+	@Override
+	public PluginWrapper getPlugin(String pluginId) {
+		return pluginManager.getPlugin(pluginId);
+	}
+
+	@Override
+	public List<Class<?>> getExtensionClasses(String pluginId) {
+		return pluginManager.getExtensionClasses(pluginId);
+	}
+
+	@Override
+	public <T> List<T> getExtensions(Class<T> clazz) {
+		return pluginManager.getExtensions(clazz);
+	}
+
+	@Override
+	public PluginWrapper whichPlugin(Class<?> clazz) {
+		return pluginManager.whichPlugin(clazz);
+	}
+
+	@Override
+	public PluginState startPlugin(String pluginId) {
+		return pluginManager.startPlugin(pluginId);
+	}
+
+	@Override
+	public PluginState stopPlugin(String pluginId) {
+		return pluginManager.stopPlugin(pluginId);
+	}
+
+	@Override
+	public boolean disablePlugin(String pluginId) {
+		return pluginManager.disablePlugin(pluginId);
+	}
+
+	@Override
+	public boolean enablePlugin(String pluginId) {
+		return pluginManager.enablePlugin(pluginId);
+	}
+
+	@Override
+	public boolean uninstallPlugin(String pluginId) {
+		return pluginManager.uninstallPlugin(pluginId);
+	}
+
+	@Override
+	public boolean refreshRegistry(boolean verifyChecksum) {
+		return pluginManager.refreshRegistry(verifyChecksum);
+	}
+
+	@Override
+	public boolean installPlugin(String url, boolean verifyChecksum) throws IOException {
+		return pluginManager.installPlugin(url, verifyChecksum);
+	}
+
+	@Override
+	public boolean upgradePlugin(String pluginId, String url, boolean verifyChecksum) throws IOException {
+		return pluginManager.upgradePlugin(pluginId, url, verifyChecksum);
+	}
+
+	@Override
+	public List<PluginRegistration> getRegisteredPlugins() {
+		return pluginManager.getRegisteredPlugins();
+	}
+
+	@Override
+	public List<PluginRegistration> getRegisteredPlugins(InstallState state) {
+		return pluginManager.getRegisteredPlugins(state);
+	}
+
+	@Override
+	public PluginRegistration lookupPlugin(String pluginId) {
+		return pluginManager.lookupPlugin(pluginId);
+	}
+
+	@Override
+	public PluginRelease lookupRelease(String pluginId, String version) {
+		return pluginManager.lookupRelease(pluginId, version);
+	}
 }

--
Gitblit v1.9.1