From 43e32ec71a508e1bb68b247fdca74f64bcf629b3 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Tue, 28 Feb 2012 22:41:31 -0500
Subject: [PATCH] Draft integration of Lucene search mechanism and web ui

---
 src/com/gitblit/utils/LuceneUtils.java |   44 +++++++++++++++++++++++++++++---------------
 1 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/src/com/gitblit/utils/LuceneUtils.java b/src/com/gitblit/utils/LuceneUtils.java
index d463cdf..e824236 100644
--- a/src/com/gitblit/utils/LuceneUtils.java
+++ b/src/com/gitblit/utils/LuceneUtils.java
@@ -52,6 +52,7 @@
 import org.eclipse.jgit.treewalk.TreeWalk;
 import org.eclipse.jgit.util.FS;
 
+import com.gitblit.GitBlit;
 import com.gitblit.models.IssueModel;
 import com.gitblit.models.IssueModel.Attachment;
 import com.gitblit.models.PathModel.PathChangeModel;
@@ -121,10 +122,13 @@
 	 * @return the repository name
 	 */
 	private static String getName(Repository repository) {
+		String rootPath = GitBlit.getRepositoriesFolder().getAbsolutePath();
 		if (repository.isBare()) {
-			return repository.getDirectory().getName();
+			return StringUtils.getRelativePath(rootPath, repository.getDirectory()
+					.getAbsolutePath());
 		} else {
-			return repository.getDirectory().getParentFile().getName();
+			return StringUtils.getRelativePath(rootPath, repository.getDirectory().getParentFile()
+					.getAbsolutePath());
 		}
 	}
 
@@ -198,11 +202,12 @@
 	 * index.
 	 * 
 	 * @param repository
-	 * @return true if the indexing has succeeded
+	 * @return IndexResult
 	 */
-	public static boolean reindex(Repository repository) {
+	public static IndexResult reindex(Repository repository) {
+		IndexResult result = new IndexResult();
 		if (!LuceneUtils.deleteIndex(repository)) {
-			return false;
+			return result;
 		}
 		try {
 			String repositoryName = getName(repository);
@@ -300,6 +305,7 @@
 							Index.NOT_ANALYZED));
 					doc.add(new Field(FIELD_BRANCH, branchName, Store.YES, Index.NOT_ANALYZED));
 					writer.addDocument(doc);
+					result.commitCount += 1;
 				}
 
 				// traverse the log and index the previous commit objects
@@ -312,6 +318,7 @@
 								Index.NOT_ANALYZED));
 						doc.add(new Field(FIELD_BRANCH, branchName, Store.YES, Index.NOT_ANALYZED));
 						writer.addDocument(doc);
+						result.commitCount += 1;
 					}
 				}
 
@@ -335,11 +342,11 @@
 			config.save();
 			resetIndexSearcher(repository);
 			writer.commit();
-			return true;
+			result.success = true;
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
-		return false;
+		return result;
 	}
 
 	/**
@@ -453,9 +460,10 @@
 	 * Updates a repository index incrementally from the last indexed commits.
 	 * 
 	 * @param repository
+	 * @return IndexResult
 	 */
-	public static boolean updateIndex(Repository repository) {
-		boolean success = false;
+	public static IndexResult updateIndex(Repository repository) {
+		IndexResult result = new IndexResult();
 		try {
 			FileBasedConfig config = getConfig(repository);
 			config.load();
@@ -475,13 +483,13 @@
 
 			// detect branch deletion
 			// first assume all branches are deleted and then remove each
-			// existing branch from deletedBranches during indexing			
+			// existing branch from deletedBranches during indexing
 			Set<String> deletedBranches = new TreeSet<String>();
 			for (String alias : config.getNames(CONF_ALIAS)) {
 				String branch = config.getString(CONF_ALIAS, null, alias);
 				deletedBranches.add(branch);
 			}
-			
+
 			// walk through each branches
 			List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);
 			for (RefModel branch : branches) {
@@ -490,7 +498,7 @@
 				// remove this branch from the deletedBranches set
 				deletedBranches.remove(branchName);
 
-				// determine last commit				
+				// determine last commit
 				String keyName = getBranchKey(branchName);
 				String lastCommit = config.getString(CONF_BRANCH, null, keyName);
 
@@ -507,6 +515,7 @@
 				Collections.reverse(revs);
 				for (RevCommit commit : revs) {
 					index(repository, branchName, commit);
+					result.commitCount += 1;
 				}
 
 				// update the config
@@ -515,7 +524,7 @@
 				config.setString(CONF_BRANCH, null, keyName, branch.getObjectId().getName());
 				config.save();
 			}
-			
+
 			// the deletedBranches set will normally be empty by this point
 			// unless a branch really was deleted and no longer exists
 			if (deletedBranches.size() > 0) {
@@ -525,11 +534,11 @@
 					writer.commit();
 				}
 			}
-			success = true;
+			result.success = true;
 		} catch (Throwable t) {
 			t.printStackTrace();
 		}
-		return success;
+		return result;
 	}
 
 	/**
@@ -782,4 +791,9 @@
 		}
 		SEARCHERS.clear();
 	}
+
+	public static class IndexResult {
+		public boolean success;
+		public int commitCount;
+	}
 }

--
Gitblit v1.9.1