From 55acda898cc001cc2db00bab336719364e0798f5 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Fri, 02 Mar 2012 18:58:20 -0500
Subject: [PATCH] Whoops. x2. LucenePage.html
---
src/com/gitblit/utils/LuceneUtils.java | 65 +++++++++++++++++++++++++-------
1 files changed, 50 insertions(+), 15 deletions(-)
diff --git a/src/com/gitblit/utils/LuceneUtils.java b/src/com/gitblit/utils/LuceneUtils.java
index eaf02df..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();
@@ -473,13 +481,24 @@
tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName);
}
- List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);
- // TODO detect branch deletion
+ // detect branch deletion
+ // first assume all branches are deleted and then remove each
+ // 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 branch
+ // walk through each branches
+ List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);
for (RefModel branch : branches) {
- // determine last commit
String branchName = branch.getName();
+
+ // remove this branch from the deletedBranches set
+ deletedBranches.remove(branchName);
+
+ // determine last commit
String keyName = getBranchKey(branchName);
String lastCommit = config.getString(CONF_BRANCH, null, keyName);
@@ -496,6 +515,7 @@
Collections.reverse(revs);
for (RevCommit commit : revs) {
index(repository, branchName, commit);
+ result.commitCount += 1;
}
// update the config
@@ -504,11 +524,21 @@
config.setString(CONF_BRANCH, null, keyName, branch.getObjectId().getName());
config.save();
}
- success = true;
+
+ // the deletedBranches set will normally be empty by this point
+ // unless a branch really was deleted and no longer exists
+ if (deletedBranches.size() > 0) {
+ for (String branch : deletedBranches) {
+ IndexWriter writer = getIndexWriter(repository, false);
+ writer.deleteDocuments(new Term(FIELD_BRANCH, branch));
+ writer.commit();
+ }
+ }
+ result.success = true;
} catch (Throwable t) {
t.printStackTrace();
}
- return success;
+ return result;
}
/**
@@ -761,4 +791,9 @@
}
SEARCHERS.clear();
}
+
+ public static class IndexResult {
+ public boolean success;
+ public int commitCount;
+ }
}
--
Gitblit v1.9.1