From b938aeea1e892b9c95396ca0745ac2adb79ff78e Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 28 Feb 2012 17:22:40 -0500 Subject: [PATCH] Delete branch from index. Queue index update from the PostReceiveHook. --- src/com/gitblit/GitBlit.java | 10 +++++++++ src/com/gitblit/GitServlet.java | 3 +++ src/com/gitblit/LuceneExecutor.java | 16 ++++++++++---- src/com/gitblit/utils/LuceneUtils.java | 29 ++++++++++++++++++++++---- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java index 580bf628..a7bfad4f 100644 --- a/src/com/gitblit/GitBlit.java +++ b/src/com/gitblit/GitBlit.java @@ -1696,6 +1696,15 @@ public class GitBlit implements ServletContextListener { } } + /** + * Update the Lucene index of a repository. + * + * @param repository + */ + public void updateLuceneIndex(RepositoryModel repository) { + luceneExecutor.queue(repository); + } + /** * Returns the descriptions/comments of the Gitblit config settings. * @@ -1892,5 +1901,6 @@ public class GitBlit implements ServletContextListener { public void contextDestroyed(ServletContextEvent contextEvent) { logger.info("Gitblit context destroyed by servlet container."); scheduledExecutor.shutdownNow(); + luceneExecutor.close(); } } diff --git a/src/com/gitblit/GitServlet.java b/src/com/gitblit/GitServlet.java index 3b60e9f1..473e4c90 100644 --- a/src/com/gitblit/GitServlet.java +++ b/src/com/gitblit/GitServlet.java @@ -197,6 +197,9 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet { // Experimental // runNativeScript(rp, "hooks/post-receive", commands); + + // Update the Lucene search index + GitBlit.self().updateLuceneIndex(repository); } /** diff --git a/src/com/gitblit/LuceneExecutor.java b/src/com/gitblit/LuceneExecutor.java index 32004a73..e4624efc 100644 --- a/src/com/gitblit/LuceneExecutor.java +++ b/src/com/gitblit/LuceneExecutor.java @@ -54,7 +54,7 @@ public class LuceneExecutor implements Runnable { public LuceneExecutor(IStoredSettings settings) { this.settings = settings; this.isLuceneEnabled = settings.getBoolean("lucene.enableLucene", false); - this.isPollingMode = settings.getBoolean("lucene.pollingMode", false); + this.isPollingMode = settings.getBoolean("lucene.pollingMode", false); } /** @@ -94,9 +94,9 @@ public class LuceneExecutor implements Runnable { if (!isLuceneEnabled) { return; } - + if (firstRun.get() || isPollingMode) { - // update all indexes on first run or if polling mode + // update all indexes on first run or if polling mode firstRun.set(false); queue.addAll(GitBlit.self().getRepositoryList()); } @@ -117,7 +117,7 @@ public class LuceneExecutor implements Runnable { "Lucene executor could not find repository {0}. Skipping.", repositoryName)); continue; - } + } index(repositoryName, repository); repository.close(); processed.add(repositoryName); @@ -174,4 +174,12 @@ public class LuceneExecutor implements Runnable { logger.error(MessageFormat.format("Lucene indexing failure for {0}", repositoryName), t); } } + + /** + * Close all Lucene indexers. + * + */ + public void close() { + LuceneUtils.close(); + } } diff --git a/src/com/gitblit/utils/LuceneUtils.java b/src/com/gitblit/utils/LuceneUtils.java index eaf02dfb..d463cdf1 100644 --- a/src/com/gitblit/utils/LuceneUtils.java +++ b/src/com/gitblit/utils/LuceneUtils.java @@ -473,13 +473,24 @@ public class LuceneUtils { tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName); } + // detect branch deletion + // first assume all branches are deleted and then remove each + // existing branch from deletedBranches during indexing + Set deletedBranches = new TreeSet(); + for (String alias : config.getNames(CONF_ALIAS)) { + String branch = config.getString(CONF_ALIAS, null, alias); + deletedBranches.add(branch); + } + + // walk through each branches List branches = JGitUtils.getLocalBranches(repository, true, -1); - // TODO detect branch deletion - - // walk through each branch 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); @@ -504,6 +515,16 @@ public class LuceneUtils { 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) { + for (String branch : deletedBranches) { + IndexWriter writer = getIndexWriter(repository, false); + writer.deleteDocuments(new Term(FIELD_BRANCH, branch)); + writer.commit(); + } + } success = true; } catch (Throwable t) { t.printStackTrace(); -- 2.39.5