From 3e087ada55f8b0e83b146578b695552db9dc8d97 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 27 Apr 2011 20:55:38 -0400
Subject: [PATCH] Integrated pagelinks into RepositoryPage. Fixed string encoding.

---
 src/com/gitblit/utils/JGitUtils.java |   50 ++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java
index eddd3df..8814c1f 100644
--- a/src/com/gitblit/utils/JGitUtils.java
+++ b/src/com/gitblit/utils/JGitUtils.java
@@ -4,10 +4,12 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.Charset;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
@@ -43,8 +45,10 @@
 import org.eclipse.jgit.revwalk.filter.RevFilter;
 import org.eclipse.jgit.treewalk.TreeWalk;
 import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
+import org.eclipse.jgit.treewalk.filter.OrTreeFilter;
 import org.eclipse.jgit.treewalk.filter.PathFilter;
 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
+import org.eclipse.jgit.treewalk.filter.PathSuffixFilter;
 import org.eclipse.jgit.treewalk.filter.TreeFilter;
 import org.eclipse.jgit.util.io.DisabledOutputStream;
 import org.slf4j.Logger;
@@ -261,12 +265,14 @@
 	}
 
 	public static String getRawContentAsString(Repository r, RevBlob blob) {
-		return new String(getRawContent(r, blob));
+		byte [] content = getRawContent(r, blob);
+		return new String(content, Charset.forName("UTF-8"));
 	}
 
 	public static String getRawContentAsString(Repository r, RevCommit commit, String blobPath) {
 		RevObject obj = getRevObject(r, commit.getTree(), blobPath);
-		return new String(getRawContent(r, (RevBlob) obj));
+		byte [] content = getRawContent(r, (RevBlob) obj);
+		return new String(content, Charset.forName("UTF-8"));
 	}
 
 	public static List<PathModel> getFilesInPath(Repository r, String basePath, String objectId) {
@@ -350,6 +356,42 @@
 		} catch (Throwable t) {
 			LOGGER.error("failed to determine files in commit!", t);
 		}
+		return list;
+	}
+	
+	public static List<PathModel> getDocuments(Repository r, List<String> extensions) {
+		List<PathModel> list = new ArrayList<PathModel>();
+		RevCommit commit = getCommit(r, Constants.HEAD);		
+		final TreeWalk walk = new TreeWalk(r);
+		try {
+			walk.addTree(commit.getTree());
+			if (extensions != null && extensions.size() > 0) {
+				Collection<TreeFilter> suffixFilters = new ArrayList<TreeFilter>();
+				for (String extension:extensions) {
+					if (extension.charAt(0) == '.') {
+						suffixFilters.add(PathSuffixFilter.create(extension));
+					} else {
+						// escape the . since this is a regexp filter
+						suffixFilters.add(PathSuffixFilter.create("\\." + extension));
+					}
+				}
+				TreeFilter filter = OrTreeFilter.create(suffixFilters);
+				walk.setFilter(filter);
+				walk.setRecursive(true);
+				while (walk.next()) {
+					list.add(getPathModel(walk, null, commit));
+				}
+			} else {
+				while (walk.next()) {
+					list.add(getPathModel(walk, null, commit));
+				}
+			}
+		} catch (IOException e) {
+			LOGGER.error("Failed to get files for commit " + commit.getName(), e);
+		} finally {
+			walk.release();
+		}
+		Collections.sort(list);
 		return list;
 	}
 
@@ -895,10 +937,6 @@
 		return metrics;
 	}
 	
-	public static RefModel getDocumentsBranch(Repository r) {
-		return getTicketsBranch(r);
-	}
-
 	public static RefModel getTicketsBranch(Repository r) {
 		RefModel ticgitBranch = null;
 		try {

--
Gitblit v1.9.1