From 8f6e672ffe19c439aadc07b64e36ae4b83e78f1e Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Mon, 15 Jul 2013 08:09:20 -0400
Subject: [PATCH] Fixed url panel failure when there are no repository urls (issue 269)

---
 src/main/java/com/gitblit/wicket/pages/RepositoryPage.java |  155 +++++++++++++++++++++++++++++++++++----------------
 1 files changed, 107 insertions(+), 48 deletions(-)

diff --git a/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java b/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java
index 8314617..372b68d 100644
--- a/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java
+++ b/src/main/java/com/gitblit/wicket/pages/RepositoryPage.java
@@ -28,6 +28,7 @@
 
 import org.apache.wicket.Component;
 import org.apache.wicket.PageParameters;
+import org.apache.wicket.behavior.SimpleAttributeModifier;
 import org.apache.wicket.markup.html.basic.Label;
 import org.apache.wicket.markup.html.form.DropDownChoice;
 import org.apache.wicket.markup.html.form.TextField;
@@ -41,9 +42,12 @@
 import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.gitblit.Constants;
 import com.gitblit.GitBlit;
+import com.gitblit.GitBlitException;
 import com.gitblit.Keys;
 import com.gitblit.PagesServlet;
 import com.gitblit.SyndicationServlet;
@@ -52,8 +56,11 @@
 import com.gitblit.models.RepositoryModel;
 import com.gitblit.models.SubmoduleModel;
 import com.gitblit.models.UserModel;
+import com.gitblit.models.UserRepositoryPreferences;
 import com.gitblit.utils.ArrayUtils;
+import com.gitblit.utils.DeepCopier;
 import com.gitblit.utils.JGitUtils;
+import com.gitblit.utils.RefLogUtils;
 import com.gitblit.utils.StringUtils;
 import com.gitblit.utils.TicgitUtils;
 import com.gitblit.wicket.GitBlitWebSession;
@@ -65,8 +72,12 @@
 import com.gitblit.wicket.panels.NavigationPanel;
 import com.gitblit.wicket.panels.RefsPanel;
 
-public abstract class RepositoryPage extends BasePage {
+public abstract class RepositoryPage extends RootPage {
+	
+	private final Logger logger = LoggerFactory.getLogger(getClass());
 
+	private final String PARAM_STAR = "star";
+	
 	protected final String projectName;
 	protected final String repositoryName;
 	protected final String objectId;
@@ -103,7 +114,7 @@
 		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) {
@@ -119,13 +130,29 @@
 				}
 			}
 		}
+		
+		if (params.containsKey(PARAM_STAR)) {
+			// set starred state
+			boolean star = params.getBoolean(PARAM_STAR);
+			UserModel user = GitBlitWebSession.get().getUser();
+			if (user != null && user.isAuthenticated) {
+				UserRepositoryPreferences prefs = user.getPreferences().getRepositoryPreferences(getRepositoryModel().name);
+				prefs.starred = star;
+				try {
+					GitBlit.self().updateUserModel(user.username, user, false);
+				} catch (GitBlitException e) {
+					logger.error("Failed to update user " + user.username, e);
+					error(getString("gb.failedToUpdateUser"), false);
+				}
+			}
+		}
 
 		// register the available page links for this page and user
 		registeredPages = registerPages();
 
 		// standard page links
 		List<PageRegistration> pages = new ArrayList<PageRegistration>(registeredPages.values());
-		NavigationPanel navigationPanel = new NavigationPanel("navPanel", getClass(), pages);
+		NavigationPanel navigationPanel = new NavigationPanel("repositoryNavPanel", getRepoNavPageClass(), pages);
 		add(navigationPanel);
 
 		add(new ExternalLink("syndication", SyndicationServlet.asLink(getRequest()
@@ -139,7 +166,16 @@
 		// set stateless page preference
 		setStatelessHint(true);
 	}
+	
+	@Override
+	protected Class<? extends BasePage> getRootNavPageClass() {
+		return RepositoriesPage.class;
+	}
 
+	protected Class<? extends BasePage> getRepoNavPageClass() {
+		return getClass();
+	}
+	
 	private Map<String, PageRegistration> registerPages() {
 		PageParameters params = null;
 		if (!StringUtils.isEmpty(repositoryName)) {
@@ -147,31 +183,35 @@
 		}
 		Map<String, PageRegistration> pages = new LinkedHashMap<String, PageRegistration>();
 
-		// standard links
-		pages.put("repositories", new PageRegistration("gb.repositories", RepositoriesPage.class));
-		pages.put("summary", new PageRegistration("gb.summary", SummaryPage.class, params));
-		pages.put("log", new PageRegistration("gb.log", LogPage.class, params));
-		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));
-		if (GitBlit.getBoolean(Keys.web.allowForking, true)) {
-			pages.put("forks", new PageRegistration("gb.forks", ForksPage.class, params));
-		}
-
-		// conditional links
 		Repository r = getRepository();
 		RepositoryModel model = getRepositoryModel();
 
+		// standard links
+		if (RefLogUtils.getRefLogBranch(r) == null) {
+			pages.put("summary", new PageRegistration("gb.summary", SummaryPage.class, params));
+		} else {
+			pages.put("summary", new PageRegistration("gb.summary", SummaryPage.class, params));
+//			pages.put("overview", new PageRegistration("gb.overview", OverviewPage.class, params));
+			pages.put("reflog", new PageRegistration("gb.reflog", ReflogPage.class, params));
+		}		
+		pages.put("commits", new PageRegistration("gb.commits", LogPage.class, params));
+		pages.put("tree", new PageRegistration("gb.tree", TreePage.class, params));
+		pages.put("compare", new PageRegistration("gb.compare", ComparePage.class, params, true));
+		if (GitBlit.getBoolean(Keys.web.allowForking, true)) {
+			pages.put("forks", new PageRegistration("gb.forks", ForksPage.class, params, true));
+		}
+
+		// conditional links
 		// per-repository extra page links
 		if (model.useTickets && TicgitUtils.getTicketsBranch(r) != null) {
-			pages.put("tickets", new PageRegistration("gb.tickets", TicketsPage.class, params));
+			pages.put("tickets", new PageRegistration("gb.tickets", TicketsPage.class, params, true));
 		}
-		if (model.useDocs) {
-			pages.put("docs", new PageRegistration("gb.docs", DocsPage.class, params));
+		if (model.showReadme || model.useDocs) {
+			pages.put("docs", new PageRegistration("gb.docs", DocsPage.class, params, true));
 		}
 		if (JGitUtils.getPagesBranch(r) != null) {
 			OtherPageLink pagesLink = new OtherPageLink("gb.pages", PagesServlet.asLink(
-					getRequest().getRelativePathPrefixToContextRoot(), repositoryName, null));
+					getRequest().getRelativePathPrefixToContextRoot(), repositoryName, null), true);
 			pages.put("pages", pagesLink);
 		}
 
@@ -186,9 +226,6 @@
 		isOwner = GitBlitWebSession.get().isLoggedIn()
 				&& (model.isOwner(GitBlitWebSession.get()
 						.getUsername()));
-		if (showAdmin || isOwner) {
-			pages.put("edit", new PageRegistration("gb.edit", EditRepositoryPage.class, params));
-		}
 		return pages;
 	}
 	
@@ -246,14 +283,24 @@
 			}
 		}
 		
-		if (getRepositoryModel().isBare) {
-			add(new Label("workingCopyIndicator").setVisible(false));
+		// (un)star link allows a user to star a repository
+		if (user.isAuthenticated) {
+			PageParameters starParams = DeepCopier.copy(getPageParameters());
+			starParams.put(PARAM_STAR, !user.getPreferences().isStarredRepository(model.name));
+			String toggleStarUrl = getRequestCycle().urlFor(getClass(), starParams).toString();
+			if (user.getPreferences().isStarredRepository(model.name)) {
+				// show unstar button
+				add(new Label("starLink").setVisible(false));
+				addToolbarButton("unstarLink", "icon-star-empty", getString("gb.unstar"), toggleStarUrl);
+			} else {
+				// show star button
+				addToolbarButton("starLink", "icon-star", getString("gb.star"), toggleStarUrl);
+				add(new Label("unstarLink").setVisible(false));
+			}
 		} else {
-			Fragment wc = new Fragment("workingCopyIndicator", "workingCopyFragment", this);
-			Label lbl = new Label("workingCopy", getString("gb.workingCopy"));
-			WicketUtils.setHtmlTooltip(lbl,  getString("gb.workingCopyWarning"));
-			wc.add(lbl);
-			add(wc);
+			// anonymous user
+			add(new Label("starLink").setVisible(false));
+			add(new Label("unstarLink").setVisible(false));
 		}
 
 		// fork controls
@@ -261,7 +308,6 @@
 			// must be logged-in to fork, hide all fork controls
 			add(new ExternalLink("forkLink", "").setVisible(false));
 			add(new ExternalLink("myForkLink", "").setVisible(false));
-			add(new Label("forksProhibitedIndicator").setVisible(false));
 		} else {
 			String fork = GitBlit.self().getFork(user.username, model.name);
 			boolean hasFork = fork != null;
@@ -270,18 +316,6 @@
 			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) {
-					// show forks prohibited indicator
-					Fragment wc = new Fragment("forksProhibitedIndicator", "forksProhibitedFragment", this);
-					Label lbl = new Label("forksProhibited", getString("gb.forksProhibited"));
-					WicketUtils.setHtmlTooltip(lbl,  getString("gb.forksProhibitedWarning"));
-					wc.add(lbl);
-					add(wc);
-				} else {
-					// can not fork, no need for forks prohibited indicator
-					add(new Label("forksProhibitedIndicator").setVisible(false));
-				}
 				
 				if (hasFork && !fork.equals(model.name)) {
 					// user has fork, view my fork link
@@ -293,14 +327,30 @@
 				}
 			} else if (canFork) {
 				// can fork and we do not have one
-				add(new Label("forksProhibitedIndicator").setVisible(false));
 				add(new ExternalLink("myForkLink", "").setVisible(false));
 				String url = getRequestCycle().urlFor(ForkPage.class, WicketUtils.newRepositoryParameter(model.name)).toString();
 				add(new ExternalLink("forkLink", url));
 			}
 		}
 		
+		if (showAdmin || isOwner) {
+			String url = getRequestCycle().urlFor(EditRepositoryPage.class, WicketUtils.newRepositoryParameter(model.name)).toString();
+			add(new ExternalLink("editLink", url)); 
+		} else {
+			add(new Label("editLink").setVisible(false));
+		}
+		
 		super.setupPage(repositoryName, pageName);
+	}
+	
+	protected void addToolbarButton(String wicketId, String iconClass, String label, String url) {
+		Fragment button = new Fragment(wicketId, "toolbarLinkFragment", this);
+		Label icon = new Label("icon");
+		WicketUtils.setCssClass(icon, iconClass);
+		button.add(icon);
+		button.add(new Label("label", label));
+		button.add(new SimpleAttributeModifier("href", url));
+		add(button);
 	}
 
 	protected void addSyndicationDiscoveryLink() {
@@ -344,13 +394,14 @@
 		RevCommit commit = JGitUtils.getCommit(r, objectId);
 		if (commit == null) {
 			error(MessageFormat.format(getString("gb.failedToFindCommit"),
-					objectId, repositoryName, getPageName()), true);
+					objectId, repositoryName, getPageName()), null, LogPage.class,
+					WicketUtils.newRepositoryParameter(repositoryName));
 		}
 		getSubmodules(commit);
 		return commit;
 	}
 	
-	private Map<String, SubmoduleModel> getSubmodules(RevCommit commit) {	
+	protected Map<String, SubmoduleModel> getSubmodules(RevCommit commit) {	
 		if (submodules == null) {
 			submodules = new HashMap<String, SubmoduleModel>();
 			for (SubmoduleModel model : JGitUtils.getSubmodules(r, commit.getTree())) {
@@ -361,7 +412,10 @@
 	}
 	
 	protected SubmoduleModel getSubmodule(String path) {
-		SubmoduleModel model = submodules.get(path);
+		SubmoduleModel model = null;
+		if (submodules != null) {
+			model = submodules.get(path);
+		}
 		if (model == null) {
 			// undefined submodule?!
 			model = new SubmoduleModel(path.substring(path.lastIndexOf('/') + 1), path, path);
@@ -392,7 +446,7 @@
 			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");
+				candidates.add(candidates.get(candidates.size() - 1) + ".git");
 			}
 
 			// absolute
@@ -571,7 +625,12 @@
 		public void onSubmit() {
 			Constants.SearchType searchType = searchTypeModel.getObject();
 			String searchString = searchBoxModel.getObject();
-			if (searchString == null) {
+			if (StringUtils.isEmpty(searchString)) {
+				// redirect to self to avoid wicket page update bug 
+				PageParameters params = RepositoryPage.this.getPageParameters();
+				String relativeUrl = urlFor(RepositoryPage.this.getClass(), params).toString();
+				String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);
+				getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));
 				return;
 			}
 			for (Constants.SearchType type : Constants.SearchType.values()) {

--
Gitblit v1.9.1