From 3f5b8f5d9203aa7ffb7fbe9cdbaf9dba3da6cae6 Mon Sep 17 00:00:00 2001
From: Hybris95 <hybris_95@hotmail.com>
Date: Thu, 01 May 2014 16:14:15 -0400
Subject: [PATCH] Fixes sort, page building and search functions on "my tickets" page.

---
 src/main/java/com/gitblit/wicket/panels/HistoryPanel.java |  145 +++++++++++++++++++++++++++---------------------
 1 files changed, 82 insertions(+), 63 deletions(-)

diff --git a/src/main/java/com/gitblit/wicket/panels/HistoryPanel.java b/src/main/java/com/gitblit/wicket/panels/HistoryPanel.java
index 4d27589..21f3838 100644
--- a/src/main/java/com/gitblit/wicket/panels/HistoryPanel.java
+++ b/src/main/java/com/gitblit/wicket/panels/HistoryPanel.java
@@ -15,6 +15,7 @@
  */
 package com.gitblit.wicket.panels;
 
+import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
@@ -37,15 +38,17 @@
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.treewalk.TreeWalk;
 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.gitblit.Constants;
-import com.gitblit.GitBlit;
 import com.gitblit.Keys;
 import com.gitblit.models.PathModel;
-import com.gitblit.models.SubmoduleModel;
 import com.gitblit.models.PathModel.PathChangeModel;
 import com.gitblit.models.RefModel;
+import com.gitblit.models.SubmoduleModel;
 import com.gitblit.utils.JGitUtils;
+import com.gitblit.utils.MarkdownUtils;
 import com.gitblit.utils.StringUtils;
 import com.gitblit.wicket.WicketUtils;
 import com.gitblit.wicket.pages.BlobDiffPage;
@@ -60,60 +63,74 @@
 
 	private static final long serialVersionUID = 1L;
 
+	private final Logger log = LoggerFactory.getLogger(getClass());
+
 	private boolean hasMore;
 
 	public HistoryPanel(String wicketId, final String repositoryName, final String objectId,
 			final String path, Repository r, int limit, int pageOffset, boolean showRemoteRefs) {
 		super(wicketId);
 		boolean pageResults = limit <= 0;
-		int itemsPerPage = GitBlit.getInteger(Keys.web.itemsPerPage, 50);
+		int itemsPerPage = app().settings().getInteger(Keys.web.itemsPerPage, 50);
 		if (itemsPerPage <= 1) {
 			itemsPerPage = 50;
 		}
 
 		RevCommit commit = JGitUtils.getCommit(r, objectId);
-		List<PathChangeModel> paths = JGitUtils.getFilesInCommit(r, commit);
-
-		Map<String, SubmoduleModel> submodules = new HashMap<String, SubmoduleModel>();
-		for (SubmoduleModel model : JGitUtils.getSubmodules(r, commit.getTree())) {
-			submodules.put(model.path, model);
-		}
-
 		PathModel matchingPath = null;
-		for (PathModel p : paths) {
-			if (p.path.equals(path)) {
-				matchingPath = p;
-				break;
+		Map<String, SubmoduleModel> submodules = new HashMap<String, SubmoduleModel>();
+
+		if (commit == null) {
+			// commit missing
+			String msg = MessageFormat.format("Failed to find history of **{0}** *{1}*",
+					path, objectId);
+			log.error(msg + " " + repositoryName);
+			add(new Label("commitHeader", MarkdownUtils.transformMarkdown(msg)).setEscapeModelStrings(false));
+			add(new Label("breadcrumbs"));
+		} else {
+			// commit found
+			List<PathChangeModel> paths = JGitUtils.getFilesInCommit(r, commit);
+			add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
+			add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, path, objectId));
+			for (SubmoduleModel model : JGitUtils.getSubmodules(r, commit.getTree())) {
+				submodules.put(model.path, model);
 			}
-		}
-		if (matchingPath == null) {
-			// path not in commit
-			// manually locate path in tree
-			TreeWalk tw = new TreeWalk(r);
-			tw.reset();
-			tw.setRecursive(true);
-			try {
-				tw.addTree(commit.getTree());
-				tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
-				while (tw.next()) {
-					if (tw.getPathString().equals(path)) {
-						matchingPath = new PathChangeModel(tw.getPathString(), tw.getPathString(), 0, tw
-							.getRawMode(0), tw.getObjectId(0).getName(), commit.getId().getName(),
-							ChangeType.MODIFY);
-					}
+
+			for (PathModel p : paths) {
+				if (p.path.equals(path)) {
+					matchingPath = p;
+					break;
 				}
-			} catch (Exception e) {
-			} finally {
-				tw.release();
+			}
+			if (matchingPath == null) {
+				// path not in commit
+				// manually locate path in tree
+				TreeWalk tw = new TreeWalk(r);
+				tw.reset();
+				tw.setRecursive(true);
+				try {
+					tw.addTree(commit.getTree());
+					tw.setFilter(PathFilterGroup.createFromStrings(Collections.singleton(path)));
+					while (tw.next()) {
+						if (tw.getPathString().equals(path)) {
+							matchingPath = new PathChangeModel(tw.getPathString(), tw.getPathString(), 0, tw
+								.getRawMode(0), tw.getObjectId(0).getName(), commit.getId().getName(),
+								ChangeType.MODIFY);
+						}
+					}
+				} catch (Exception e) {
+				} finally {
+					tw.release();
+				}
 			}
 		}
-		
+
 		final boolean isTree = matchingPath == null ? true : matchingPath.isTree();
 		final boolean isSubmodule = matchingPath == null ? false : matchingPath.isSubmodule();
 
 		// submodule
 		final String submodulePath;
-		final boolean hasSubmodule; 
+		final boolean hasSubmodule;
 		if (isSubmodule) {
 			SubmoduleModel submodule = getSubmodule(submodules, repositoryName, matchingPath == null ? null : matchingPath.path);
 			submodulePath = submodule.gitblitPath;
@@ -122,7 +139,7 @@
 			submodulePath = "";
 			hasSubmodule = false;
 		}
-		
+
 		final Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(r, showRemoteRefs);
 		List<RevCommit> commits;
 		if (pageResults) {
@@ -138,17 +155,13 @@
 		// works unless commits.size() represents the exact end.
 		hasMore = commits.size() >= itemsPerPage;
 
-		add(new CommitHeaderPanel("commitHeader", repositoryName, commit));
-
-		// breadcrumbs
-		add(new PathBreadcrumbsPanel("breadcrumbs", repositoryName, path, objectId));
-
-		final int hashLen = GitBlit.getInteger(Keys.web.shortCommitIdLength, 6);
+		final int hashLen = app().settings().getInteger(Keys.web.shortCommitIdLength, 6);
 		ListDataProvider<RevCommit> dp = new ListDataProvider<RevCommit>(commits);
 		DataView<RevCommit> logView = new DataView<RevCommit>("commit", dp) {
 			private static final long serialVersionUID = 1L;
 			int counter;
 
+			@Override
 			public void populateItem(final Item<RevCommit> item) {
 				final RevCommit entry = item.getModelObject();
 				final Date date = JGitUtils.getCommitDate(entry);
@@ -159,7 +172,7 @@
 				String author = entry.getAuthorIdent().getName();
 				LinkPanel authorLink = new LinkPanel("commitAuthor", "list", author,
 						GitSearchPage.class,
-						WicketUtils.newSearchParameter(repositoryName, objectId,
+						WicketUtils.newSearchParameter(repositoryName, null,
 								author, Constants.SearchType.AUTHOR));
 				setPersonSearchTooltip(authorLink, author, Constants.SearchType.AUTHOR);
 				item.add(authorLink);
@@ -195,31 +208,37 @@
 							TreePage.class, WicketUtils.newObjectParameter(
 									repositoryName, entry.getName()));
 					WicketUtils.setCssClass(commitHash, "shortsha1");
-					WicketUtils.setHtmlTooltip(commitHash, entry.getName());					
+					WicketUtils.setHtmlTooltip(commitHash, entry.getName());
 					item.add(commitHash);
-					
+
 					Fragment links = new Fragment("historyLinks", "treeLinks", this);
 					links.add(new BookmarkablePageLink<Void>("commitdiff", CommitDiffPage.class,
 							WicketUtils.newObjectParameter(repositoryName, entry.getName())));
 					item.add(links);
 				} else if (isSubmodule) {
 					// submodule
-					item.add(new Label("hashLabel", submodulePath + "@"));
-					Repository repository = GitBlit.self().getRepository(repositoryName);
+					Repository repository = app().repositories().getRepository(repositoryName);
 					String submoduleId = JGitUtils.getSubmoduleCommitId(repository, path, entry);
 					repository.close();
-					LinkPanel commitHash = new LinkPanel("hashLink", null, submoduleId.substring(0, hashLen),
-							TreePage.class, WicketUtils.newObjectParameter(
-									submodulePath, submoduleId));
-					WicketUtils.setCssClass(commitHash, "shortsha1");
-					WicketUtils.setHtmlTooltip(commitHash, submoduleId);					
-					item.add(commitHash.setEnabled(hasSubmodule));
-					
+					if (StringUtils.isEmpty(submoduleId)) {
+						// not a submodule at this commit, just a matching path
+						item.add(new Label("hashLabel").setVisible(false));
+						item.add(new Label("hashLink").setVisible(false));
+					} else {
+						// really a submodule
+						item.add(new Label("hashLabel", submodulePath + "@"));
+						LinkPanel commitHash = new LinkPanel("hashLink", null, submoduleId.substring(0, hashLen),
+								TreePage.class, WicketUtils.newObjectParameter(
+										submodulePath, submoduleId));
+						WicketUtils.setCssClass(commitHash, "shortsha1");
+						WicketUtils.setHtmlTooltip(commitHash, submoduleId);
+						item.add(commitHash.setEnabled(hasSubmodule));
+					}
 					Fragment links = new Fragment("historyLinks", "treeLinks", this);
 					links.add(new BookmarkablePageLink<Void>("commitdiff", CommitDiffPage.class,
 							WicketUtils.newObjectParameter(repositoryName, entry.getName())));
 					item.add(links);
-				} else {					
+				} else {
 					// commit
 					item.add(new Label("hashLabel", getString("gb.blob") + "@"));
 					LinkPanel commitHash = new LinkPanel("hashLink", null, entry.getName().substring(0, hashLen),
@@ -228,7 +247,7 @@
 					WicketUtils.setCssClass(commitHash, "sha1");
 					WicketUtils.setHtmlTooltip(commitHash, entry.getName());
 					item.add(commitHash);
-					
+
 					Fragment links = new Fragment("historyLinks", "blobLinks", this);
 					links.add(new BookmarkablePageLink<Void>("commitdiff", CommitDiffPage.class,
 							WicketUtils.newObjectParameter(repositoryName, entry.getName())));
@@ -270,7 +289,7 @@
 	public boolean hasMore() {
 		return hasMore;
 	}
-	
+
 	protected SubmoduleModel getSubmodule(Map<String, SubmoduleModel> submodules, String repositoryName, String path) {
 		SubmoduleModel model = submodules.get(path);
 		if (model == null) {
@@ -281,9 +300,9 @@
 			return model;
 		} else {
 			// extract the repository name from the clone url
-			List<String> patterns = GitBlit.getStrings(Keys.git.submoduleUrlPatterns);
+			List<String> patterns = app().settings().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 = "";
@@ -303,7 +322,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
@@ -320,16 +339,16 @@
 			// create a unique, ordered set of candidate paths
 			Set<String> paths = new LinkedHashSet<String>(candidates);
 			for (String candidate : paths) {
-				if (GitBlit.self().hasRepository(candidate)) {
+				if (app().repositories().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;
-		}		
+		}
 	}
 }

--
Gitblit v1.9.1