]> source.dussan.org Git - gitblit.git/commitdiff
Delete branch from index. Queue index update from the PostReceiveHook.
authorJames Moger <james.moger@gitblit.com>
Tue, 28 Feb 2012 22:22:40 +0000 (17:22 -0500)
committerJames Moger <james.moger@gitblit.com>
Tue, 28 Feb 2012 22:22:40 +0000 (17:22 -0500)
src/com/gitblit/GitBlit.java
src/com/gitblit/GitServlet.java
src/com/gitblit/LuceneExecutor.java
src/com/gitblit/utils/LuceneUtils.java

index 580bf628578e698568062b43a9cf233108668c7e..a7bfad4f3d1f9ec2f4340e3f9e4afa1d7eb410ba 100644 (file)
@@ -1696,6 +1696,15 @@ public class GitBlit implements ServletContextListener {
                }\r
        }\r
 \r
+       /**\r
+        * Update the Lucene index of a repository.\r
+        * \r
+        * @param repository\r
+        */\r
+       public void updateLuceneIndex(RepositoryModel repository) {\r
+               luceneExecutor.queue(repository);\r
+       }\r
+\r
        /**\r
         * Returns the descriptions/comments of the Gitblit config settings.\r
         * \r
@@ -1892,5 +1901,6 @@ public class GitBlit implements ServletContextListener {
        public void contextDestroyed(ServletContextEvent contextEvent) {\r
                logger.info("Gitblit context destroyed by servlet container.");\r
                scheduledExecutor.shutdownNow();\r
+               luceneExecutor.close();\r
        }\r
 }\r
index 3b60e9f1f3b29e63209204e99a88db53a70ec3ff..473e4c9096dc22a35cf05aaae683040a7c03d67e 100644 (file)
@@ -197,6 +197,9 @@ public class GitServlet extends org.eclipse.jgit.http.server.GitServlet {
 \r
                        // Experimental\r
                        // runNativeScript(rp, "hooks/post-receive", commands);\r
+                       \r
+                       // Update the Lucene search index\r
+                       GitBlit.self().updateLuceneIndex(repository);\r
                }\r
 \r
                /**\r
index 32004a738252b2cad2015e4349978d9c99e5b2ee..e4624efc147fd9f79dd0e226113602a62105f047 100644 (file)
@@ -54,7 +54,7 @@ public class LuceneExecutor implements Runnable {
        public LuceneExecutor(IStoredSettings settings) {\r
                this.settings = settings;\r
                this.isLuceneEnabled = settings.getBoolean("lucene.enableLucene", false);\r
-               this.isPollingMode = settings.getBoolean("lucene.pollingMode", false);          \r
+               this.isPollingMode = settings.getBoolean("lucene.pollingMode", false);\r
        }\r
 \r
        /**\r
@@ -94,9 +94,9 @@ public class LuceneExecutor implements Runnable {
                if (!isLuceneEnabled) {\r
                        return;\r
                }\r
-               \r
+\r
                if (firstRun.get() || isPollingMode) {\r
-                       // update all indexes on first run or if polling mode \r
+                       // update all indexes on first run or if polling mode\r
                        firstRun.set(false);\r
                        queue.addAll(GitBlit.self().getRepositoryList());\r
                }\r
@@ -117,7 +117,7 @@ public class LuceneExecutor implements Runnable {
                                                                "Lucene executor could not find repository {0}. Skipping.",\r
                                                                repositoryName));\r
                                                continue;\r
-                                       }                                       \r
+                                       }\r
                                        index(repositoryName, repository);\r
                                        repository.close();\r
                                        processed.add(repositoryName);\r
@@ -174,4 +174,12 @@ public class LuceneExecutor implements Runnable {
                        logger.error(MessageFormat.format("Lucene indexing failure for {0}", repositoryName), t);\r
                }\r
        }\r
+\r
+       /**\r
+        * Close all Lucene indexers.\r
+        * \r
+        */\r
+       public void close() {\r
+               LuceneUtils.close();\r
+       }\r
 }\r
index eaf02dfb186b1f339dd1f994e5618ec3ff6f567a..d463cdf1fdaffd64b20600f30ae000f1a55ff2cd 100644 (file)
@@ -473,13 +473,24 @@ public class LuceneUtils {
                                tags.get(tag.getReferencedObjectId().getName()).add(tag.displayName);\r
                        }\r
 \r
+                       // detect branch deletion\r
+                       // first assume all branches are deleted and then remove each\r
+                       // existing branch from deletedBranches during indexing                 \r
+                       Set<String> deletedBranches = new TreeSet<String>();\r
+                       for (String alias : config.getNames(CONF_ALIAS)) {\r
+                               String branch = config.getString(CONF_ALIAS, null, alias);\r
+                               deletedBranches.add(branch);\r
+                       }\r
+                       \r
+                       // walk through each branches\r
                        List<RefModel> branches = JGitUtils.getLocalBranches(repository, true, -1);\r
-                       // TODO detect branch deletion\r
-\r
-                       // walk through each branch\r
                        for (RefModel branch : branches) {\r
-                               // determine last commit\r
                                String branchName = branch.getName();\r
+\r
+                               // remove this branch from the deletedBranches set\r
+                               deletedBranches.remove(branchName);\r
+\r
+                               // determine last commit                                \r
                                String keyName = getBranchKey(branchName);\r
                                String lastCommit = config.getString(CONF_BRANCH, null, keyName);\r
 \r
@@ -504,6 +515,16 @@ public class LuceneUtils {
                                config.setString(CONF_BRANCH, null, keyName, branch.getObjectId().getName());\r
                                config.save();\r
                        }\r
+                       \r
+                       // the deletedBranches set will normally be empty by this point\r
+                       // unless a branch really was deleted and no longer exists\r
+                       if (deletedBranches.size() > 0) {\r
+                               for (String branch : deletedBranches) {\r
+                                       IndexWriter writer = getIndexWriter(repository, false);\r
+                                       writer.deleteDocuments(new Term(FIELD_BRANCH, branch));\r
+                                       writer.commit();\r
+                               }\r
+                       }\r
                        success = true;\r
                } catch (Throwable t) {\r
                        t.printStackTrace();\r