From 6c127c1ce12994ab4a235c53c254cf776e347e69 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 05 Dec 2012 17:39:11 -0500
Subject: [PATCH] Improving first-run setup of GCA and Gitblit GO

---
 src/com/gitblit/wicket/pages/RepositoryPage.java |   42 ++++++++++++++++++++++++++++++++++--------
 1 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/src/com/gitblit/wicket/pages/RepositoryPage.java b/src/com/gitblit/wicket/pages/RepositoryPage.java
index 879c432..3acf73a 100644
--- a/src/com/gitblit/wicket/pages/RepositoryPage.java
+++ b/src/com/gitblit/wicket/pages/RepositoryPage.java
@@ -48,6 +48,7 @@
 import com.gitblit.PagesServlet;
 import com.gitblit.SyndicationServlet;
 import com.gitblit.models.ProjectModel;
+import com.gitblit.models.RefModel;
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.SubmoduleModel;
 import com.gitblit.models.UserModel;
@@ -98,6 +99,26 @@
 		if (!getRepositoryModel().hasCommits) {
 			setResponsePage(EmptyRepositoryPage.class, params);
 		}
+		
+		if (getRepositoryModel().isCollectingGarbage) {
+			error(MessageFormat.format(getString("gb.busyCollectingGarbage"), getRepositoryModel().name), true);
+		}
+
+		if (objectId != null) {
+			RefModel branch = null;
+			if ((branch = JGitUtils.getBranch(getRepository(), objectId)) != null) {
+				UserModel user = GitBlitWebSession.get().getUser();
+				if (user == null) {
+					// workaround until get().getUser() is reviewed throughout the app
+					user = UserModel.ANONYMOUS;
+				}
+				boolean canAccess = user.hasBranchPermission(repositoryName,
+								branch.reference.getName());
+				if (!canAccess) {
+					error(getString("gb.accessDenied"), true);
+				}
+			}
+		}
 
 		// register the available page links for this page and user
 		registeredPages = registerPages();
@@ -133,7 +154,9 @@
 		pages.put("branches", new PageRegistration("gb.branches", BranchesPage.class, params));
 		pages.put("tags", new PageRegistration("gb.tags", TagsPage.class, params));
 		pages.put("tree", new PageRegistration("gb.tree", TreePage.class, params));
-		pages.put("forks", new PageRegistration("gb.forks", ForksPage.class, params));
+		if (GitBlit.getBoolean(Keys.web.allowForking, true)) {
+			pages.put("forks", new PageRegistration("gb.forks", ForksPage.class, params));
+		}
 
 		// conditional links
 		Repository r = getRepository();
@@ -170,7 +193,7 @@
 	}
 	
 	protected boolean allowForkControls() {
-		return true;
+		return GitBlit.getBoolean(Keys.web.allowForking, true);
 	}
 
 	@Override
@@ -196,6 +219,9 @@
 		add(new Label("pageName", pageName).setRenderBodyOnly(true));
 		
 		UserModel user = GitBlitWebSession.get().getUser();
+		if (user == null) {
+			user = UserModel.ANONYMOUS;
+		}
 
 		// indicate origin repository
 		RepositoryModel model = getRepositoryModel();
@@ -206,7 +232,7 @@
 			if (origin == null) {
 				// no origin repository
 				add(new Label("originRepository").setVisible(false));
-			} else if (!user.canViewRepository(origin)) {
+			} else if (!user.canView(origin)) {
 				// show origin repository without link
 				Fragment forkFrag = new Fragment("originRepository", "originFragment", this);
 				forkFrag.add(new Label("originRepository", StringUtils.stripDotGit(model.originRepository)));
@@ -231,7 +257,7 @@
 		}
 
 		// fork controls
-		if (!allowForkControls() || user == null) {
+		if (!allowForkControls() || user == null || !user.isAuthenticated) {
 			// must be logged-in to fork, hide all fork controls
 			add(new ExternalLink("forkLink", "").setVisible(false));
 			add(new ExternalLink("myForkLink", "").setVisible(false));
@@ -239,13 +265,13 @@
 		} else {
 			String fork = GitBlit.self().getFork(user.username, model.name);
 			boolean hasFork = fork != null;
-			boolean canFork = user.canForkRepository(model);
+			boolean canFork = user.canFork(model);
 
 			if (hasFork || !canFork) {
 				// user not allowed to fork or fork already exists or repo forbids forking
 				add(new ExternalLink("forkLink", "").setVisible(false));
 				
-				if (user.canFork && !model.allowForks) {
+				if (user.canFork() && !model.allowForks) {
 					// show forks prohibited indicator
 					Fragment wc = new Fragment("forksProhibitedIndicator", "forksProhibitedFragment", this);
 					Label lbl = new Label("forksProhibited", getString("gb.forksProhibited"));
@@ -401,7 +427,7 @@
 	}
 
 	protected String getShortObjectId(String objectId) {
-		return objectId.substring(0, 8);
+		return objectId.substring(0, GitBlit.getInteger(Keys.web.shortCommitIdLength, 6));
 	}
 
 	protected void addRefs(Repository r, RevCommit c) {
@@ -573,4 +599,4 @@
 			getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));
 		}
 	}
-}
\ No newline at end of file
+}

--
Gitblit v1.9.1