From b170c59085538953c95eebe444a839249f25bff8 Mon Sep 17 00:00:00 2001
From: mrbytes <eguervos@msn.com>
Date: Mon, 13 Aug 2012 15:52:59 -0400
Subject: [PATCH] GitBlitWebApp_es.properties: up-to date with the last changes

---
 src/com/gitblit/wicket/pages/RepositoryPage.java |   87 ++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 85 insertions(+), 2 deletions(-)

diff --git a/src/com/gitblit/wicket/pages/RepositoryPage.java b/src/com/gitblit/wicket/pages/RepositoryPage.java
index 4b5e28d..6d33a14 100644
--- a/src/com/gitblit/wicket/pages/RepositoryPage.java
+++ b/src/com/gitblit/wicket/pages/RepositoryPage.java
@@ -19,9 +19,12 @@
 import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import org.apache.wicket.Component;
 import org.apache.wicket.PageParameters;
@@ -45,6 +48,7 @@
 import com.gitblit.PagesServlet;
 import com.gitblit.SyndicationServlet;
 import com.gitblit.models.RepositoryModel;
+import com.gitblit.models.SubmoduleModel;
 import com.gitblit.utils.ArrayUtils;
 import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.StringUtils;
@@ -62,18 +66,20 @@
 
 	protected final String repositoryName;
 	protected final String objectId;
-
+	
 	private transient Repository r;
 
 	private RepositoryModel m;
 
+	private Map<String, SubmoduleModel> submodules;
+	
 	private final Map<String, PageRegistration> registeredPages;
 
 	public RepositoryPage(PageParameters params) {
 		super(params);
 		repositoryName = WicketUtils.getRepositoryName(params);
 		objectId = WicketUtils.getObject(params);
-
+		
 		if (StringUtils.isEmpty(repositoryName)) {
 			error(MessageFormat.format(getString("gb.repositoryNotSpecifiedFor"), getPageName()), true);
 		}
@@ -206,8 +212,85 @@
 			error(MessageFormat.format(getString("gb.failedToFindCommit"),
 					objectId, repositoryName, getPageName()), true);
 		}
+		getSubmodules(commit);
 		return commit;
 	}
+	
+	private Map<String, SubmoduleModel> getSubmodules(RevCommit commit) {	
+		if (submodules == null) {
+			submodules = new HashMap<String, SubmoduleModel>();
+			for (SubmoduleModel model : JGitUtils.getSubmodules(r, commit.getTree())) {
+				submodules.put(model.path, model);
+			}
+		}
+		return submodules;
+	}
+	
+	protected Map<String, SubmoduleModel> getSubmodules() {
+		return submodules;
+	}
+	
+	protected SubmoduleModel getSubmodule(String path) {
+		SubmoduleModel model = submodules.get(path);
+		if (model == null) {
+			// undefined submodule?!
+			model = new SubmoduleModel(path.substring(path.lastIndexOf('/') + 1), path, path);
+			model.hasSubmodule = false;
+			model.gitblitPath = model.name;
+			return model;
+		} else {
+			// extract the repository name from the clone url
+			List<String> patterns = GitBlit.getStrings(Keys.git.submoduleUrlPatterns);
+			String submoduleName = StringUtils.extractRepositoryPath(model.url, patterns.toArray(new String[0]));
+			
+			// determine the current path for constructing paths relative
+			// to the current repository
+			String currentPath = "";
+			if (repositoryName.indexOf('/') > -1) {
+				currentPath = repositoryName.substring(0, repositoryName.lastIndexOf('/') + 1);
+			}
+
+			// try to locate the submodule repository
+			// prefer bare to non-bare names
+			List<String> candidates = new ArrayList<String>();
+
+			// relative
+			candidates.add(currentPath + StringUtils.stripDotGit(submoduleName));
+			candidates.add(candidates.get(candidates.size() - 1) + ".git");
+
+			// relative, no subfolder
+			if (submoduleName.lastIndexOf('/') > -1) {
+				String name = submoduleName.substring(submoduleName.lastIndexOf('/') + 1);
+				candidates.add(currentPath + StringUtils.stripDotGit(name));
+				candidates.add(currentPath + candidates.get(candidates.size() - 1) + ".git");
+			}
+
+			// absolute
+			candidates.add(StringUtils.stripDotGit(submoduleName));
+			candidates.add(candidates.get(candidates.size() - 1) + ".git");
+
+			// absolute, no subfolder
+			if (submoduleName.lastIndexOf('/') > -1) {
+				String name = submoduleName.substring(submoduleName.lastIndexOf('/') + 1);
+				candidates.add(StringUtils.stripDotGit(name));
+				candidates.add(candidates.get(candidates.size() - 1) + ".git");
+			}
+
+			// create a unique, ordered set of candidate paths
+			Set<String> paths = new LinkedHashSet<String>(candidates);
+			for (String candidate : paths) {
+				if (GitBlit.self().hasRepository(candidate)) {
+					model.hasSubmodule = true;
+					model.gitblitPath = candidate;
+					return model;
+				}
+			}
+			
+			// we do not have a copy of the submodule, but we need a path
+			model.gitblitPath = candidates.get(0);
+			return model;
+		}		
+	}
 
 	protected String getShortObjectId(String objectId) {
 		return objectId.substring(0, 8);

--
Gitblit v1.9.1