From eb1405f736f2f98e14215774dd53eea9b9a77017 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Mon, 01 Oct 2012 20:45:19 -0400
Subject: [PATCH] Show fork links according to user permissions. Improve fork detection.

---
 src/com/gitblit/GitBlit.java |   76 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java
index 699bbac..f86c66a 100644
--- a/src/com/gitblit/GitBlit.java
+++ b/src/com/gitblit/GitBlit.java
@@ -32,6 +32,7 @@
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -1325,6 +1326,81 @@
 		r.close();
 		return true;
 	}
+	
+	/**
+	 * Determines if the specified user has a fork of the specified origin
+	 * repository.
+	 * 
+	 * @param username
+	 * @param origin
+	 * @return true the if the user has a fork
+	 */
+	public boolean hasFork(String username, String origin) {
+		return getFork(username, origin) != null;
+	}
+	
+	/**
+	 * Gets the name of a user's fork of the specified origin
+	 * repository.
+	 * 
+	 * @param username
+	 * @param origin
+	 * @return the name of the user's fork, null otherwise
+	 */
+	public String getFork(String username, String origin) {
+		String userProject = "~" + username.toLowerCase();
+		if (settings.getBoolean(Keys.git.cacheRepositoryList, true)) {
+			String userPath = userProject + "/";
+
+			// collect all origin nodes in fork network
+			Set<String> roots = new HashSet<String>();
+			roots.add(origin);
+			RepositoryModel originModel = repositoryListCache.get(origin);
+			while (originModel != null) {
+				if (!ArrayUtils.isEmpty(originModel.forks)) {
+					for (String fork : originModel.forks) {
+						if (!fork.startsWith(userPath)) {
+							roots.add(fork);
+						}
+					}
+				}
+				
+				if (originModel.originRepository != null) {
+					roots.add(originModel.originRepository);
+					originModel = repositoryListCache.get(originModel.originRepository);
+				} else {
+					// break
+					originModel = null;
+				}
+			}
+			
+			for (String repository : repositoryListCache.keySet()) {
+				if (repository.toLowerCase().startsWith(userPath)) {
+					RepositoryModel model = repositoryListCache.get(repository);
+					if (!StringUtils.isEmpty(model.originRepository)) {
+						if (roots.contains(model.originRepository)) {
+							// user has a fork in this graph
+							return model.name;
+						}
+					}
+				}
+			}
+		} else {
+			// not caching
+			ProjectModel project = getProjectModel(userProject);
+			for (String repository : project.repositories) {
+				if (repository.toLowerCase().startsWith(userProject)) {
+					RepositoryModel model = repositoryListCache.get(repository);
+					if (model.originRepository.equalsIgnoreCase(origin)) {
+						// user has a fork
+						return model.name;
+					}
+				}
+			}
+		}
+		// user does not have a fork
+		return null;
+	}
 
 	/**
 	 * Returns the size in bytes of the repository. Gitblit caches the

--
Gitblit v1.9.1