]> source.dussan.org Git - gitblit.git/commitdiff
Repository-branch Lucene indexing is now opt-in
authorJames Moger <james.moger@gitblit.com>
Fri, 16 Mar 2012 21:01:19 +0000 (17:01 -0400)
committerJames Moger <james.moger@gitblit.com>
Fri, 16 Mar 2012 21:01:19 +0000 (17:01 -0400)
src/com/gitblit/GitBlit.java
src/com/gitblit/LuceneExecutor.java
src/com/gitblit/models/RepositoryModel.java
src/com/gitblit/wicket/GitBlitWebApp.properties
src/com/gitblit/wicket/pages/EditRepositoryPage.html
src/com/gitblit/wicket/pages/EditRepositoryPage.java
src/com/gitblit/wicket/pages/LucenePage.java
tests/com/gitblit/tests/LuceneExecutorTest.java

index 15b69ec0c471ae1b3ee1057d3272fb72941ba1f8..272630c1897a1f38461cc07fb69d5908ad5ac4d5 100644 (file)
@@ -812,6 +812,8 @@ public class GitBlit implements ServletContextListener {
                                        "gitblit", null, "postReceiveScript")));\r
                        model.mailingLists = new ArrayList<String>(Arrays.asList(config.getStringList(\r
                                        "gitblit", null, "mailingList")));\r
+                       model.indexedBranches = new ArrayList<String>(Arrays.asList(config.getStringList(\r
+                                       "gitblit", null, "indexBranch")));\r
                }\r
                model.HEAD = JGitUtils.getHEADRef(r);\r
                model.availableRefs = JGitUtils.getAvailableHeadTargets(r);\r
@@ -955,6 +957,12 @@ public class GitBlit implements ServletContextListener {
                        // create repository\r
                        logger.info("create repository " + repository.name);\r
                        r = JGitUtils.createRepository(repositoriesFolder, repository.name);\r
+                       \r
+                       // automatically index master branch if Lucene integration is enabled\r
+                       if (luceneExecutor.isReady()) {\r
+                               repository.indexedBranches = new ArrayList<String>();\r
+                               repository.indexedBranches.add("refs/heads/master");\r
+                       }\r
                } else {\r
                        // rename repository\r
                        if (!repositoryName.equalsIgnoreCase(repository.name)) {\r
@@ -1063,6 +1071,9 @@ public class GitBlit implements ServletContextListener {
                if (!ArrayUtils.isEmpty(repository.mailingLists)) {\r
                        config.setStringList("gitblit", null, "mailingList", repository.mailingLists);\r
                }\r
+               if (!ArrayUtils.isEmpty(repository.indexedBranches)) {\r
+                       config.setStringList("gitblit", null, "indexBranch", repository.indexedBranches);\r
+               }\r
                try {\r
                        config.save();\r
                } catch (IOException e) {\r
index 0b90b74c9eb13826bbb0ba9216d512f44f145ebc..e4ee0b67704c47e3587c76b66f9978dbb8400668 100644 (file)
@@ -89,6 +89,7 @@ import com.gitblit.models.IssueModel;
 import com.gitblit.models.IssueModel.Attachment;\r
 import com.gitblit.models.PathModel.PathChangeModel;\r
 import com.gitblit.models.RefModel;\r
+import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.SearchResult;\r
 import com.gitblit.utils.ArrayUtils;\r
 import com.gitblit.utils.IssueUtils;\r
@@ -141,9 +142,6 @@ public class LuceneExecutor implements Runnable {
                        "arj", "bin", "bmp", "dll", "doc", "docx", "exe", "gif", "gz", "jar", "jpg", "lib",\r
                        "lzh", "odg", "pdf", "ppt", "png", "so", "swf", "xcf", "xls", "xlsx", "zip"));\r
 \r
-       private final Set<String> excludedBranches = new TreeSet<String>(\r
-                       Arrays.asList("/refs/heads/gb-issues"));\r
-       \r
        public LuceneExecutor(IStoredSettings settings, File repositoriesFolder) {\r
                this.storedSettings = settings;\r
                this.repositoriesFolder = repositoriesFolder;\r
@@ -170,18 +168,14 @@ public class LuceneExecutor implements Runnable {
                        return;\r
                }\r
 \r
-               for (String repositoryName : GitBlit.self().getRepositoryList()) {\r
-                       Repository repository = GitBlit.self().getRepository(repositoryName);\r
-                       if (repository == null) {\r
-                               logger.warn(MessageFormat.format(\r
-                                               "Lucene executor could not find repository {0}. Skipping.",\r
-                                               repositoryName));\r
-                               continue;\r
+               for (String repositoryName: GitBlit.self().getRepositoryList()) {\r
+                       RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);\r
+                       if (model.hasCommits && !ArrayUtils.isEmpty(model.indexedBranches)) {\r
+                               Repository repository = GitBlit.self().getRepository(model.name);\r
+                               index(model, repository);                               \r
+                               repository.close();\r
+                               System.gc();\r
                        }\r
-                       // TODO allow repository to bypass Lucene indexing                              \r
-                       index(repositoryName, repository);\r
-                       repository.close();\r
-                       System.gc();\r
                }\r
        }\r
 \r
@@ -194,43 +188,38 @@ public class LuceneExecutor implements Runnable {
         * @param repository\r
         *            the repository object\r
         */\r
-       protected void index(String name, Repository repository) {\r
+       protected void index(RepositoryModel model, Repository repository) {\r
                try {\r
-                       if (JGitUtils.hasCommits(repository)) {\r
-                               if (shouldReindex(repository)) {\r
-                                       // (re)build the entire index                                   \r
-                                       IndexResult result = reindex(name, repository);\r
-                                       \r
-                                       if (result.success) {\r
-                                               if (result.commitCount > 0) {\r
-                                                       String msg = "Built {0} Lucene index from {1} commits and {2} files across {3} branches in {4} secs";\r
-                                                       logger.info(MessageFormat.format(msg, name,\r
-                                                                       result.commitCount, result.blobCount, result.branchCount, result.duration()));\r
-                                               }\r
-                                       } else {\r
-                                               String msg = "Could not build {0} Lucene index!";\r
-                                               logger.error(MessageFormat.format(msg, name));\r
+                       if (shouldReindex(repository)) {\r
+                               // (re)build the entire index\r
+                               IndexResult result = reindex(model, repository);\r
+\r
+                               if (result.success) {\r
+                                       if (result.commitCount > 0) {\r
+                                               String msg = "Built {0} Lucene index from {1} commits and {2} files across {3} branches in {4} secs";\r
+                                               logger.info(MessageFormat.format(msg, model.name, result.commitCount,\r
+                                                               result.blobCount, result.branchCount, result.duration()));\r
                                        }\r
                                } else {\r
-                                       // update the index with latest commits                                 \r
-                                       IndexResult result = updateIndex(name, repository);\r
-                                       if (result.success) {\r
-                                               if (result.commitCount > 0) {\r
-                                                       String msg = "Updated {0} Lucene index with {1} commits and {2} files across {3} branches in {4} secs";\r
-                                                       logger.info(MessageFormat.format(msg, name,\r
-                                                                       result.commitCount, result.blobCount, result.branchCount, result.duration()));\r
-                                               }\r
-                                       } else {\r
-                                               String msg = "Could not update {0} Lucene index!";\r
-                                               logger.error(MessageFormat.format(msg, name));\r
-                                       }\r
+                                       String msg = "Could not build {0} Lucene index!";\r
+                                       logger.error(MessageFormat.format(msg, model.name));\r
                                }\r
                        } else {\r
-                               logger.info(MessageFormat.format("Skipped Lucene index of empty repository {0}",\r
-                                               name));\r
+                               // update the index with latest commits\r
+                               IndexResult result = updateIndex(model, repository);\r
+                               if (result.success) {\r
+                                       if (result.commitCount > 0) {\r
+                                               String msg = "Updated {0} Lucene index with {1} commits and {2} files across {3} branches in {4} secs";\r
+                                               logger.info(MessageFormat.format(msg, model.name, result.commitCount,\r
+                                                               result.blobCount, result.branchCount, result.duration()));\r
+                                       }\r
+                               } else {\r
+                                       String msg = "Could not update {0} Lucene index!";\r
+                                       logger.error(MessageFormat.format(msg, model.name));\r
+                               }\r
                        }\r
                } catch (Throwable t) {\r
-                       logger.error(MessageFormat.format("Lucene indexing failure for {0}", name), t);\r
+                       logger.error(MessageFormat.format("Lucene indexing failure for {0}", model.name), t);\r
                }\r
        }\r
        \r
@@ -430,15 +419,15 @@ public class LuceneExecutor implements Runnable {
         * @param repository\r
         * @return IndexResult\r
         */\r
-       public IndexResult reindex(String repositoryName, Repository repository) {\r
+       public IndexResult reindex(RepositoryModel model, Repository repository) {\r
                IndexResult result = new IndexResult();\r
-               if (!deleteIndex(repositoryName)) {\r
+               if (!deleteIndex(model.name)) {\r
                        return result;\r
                }\r
                try {                   \r
                        FileBasedConfig config = getConfig(repository);\r
                        Set<String> indexedCommits = new TreeSet<String>();\r
-                       IndexWriter writer = getIndexWriter(repositoryName);\r
+                       IndexWriter writer = getIndexWriter(model.name);\r
                        // build a quick lookup of tags\r
                        Map<String, List<String>> tags = new HashMap<String, List<String>>();\r
                        for (RefModel tag : JGitUtils.getTags(repository, false, -1)) {\r
@@ -479,7 +468,9 @@ public class LuceneExecutor implements Runnable {
                        \r
                        // walk through each branch\r
                        for (RefModel branch : branches) {\r
-                               if (excludedBranches.contains(branch.getName())) {\r
+\r
+                               // if this branch is not specifically indexed then skip\r
+                               if (!model.indexedBranches.contains(branch.getName())) {\r
                                        continue;\r
                                }\r
 \r
@@ -624,11 +615,11 @@ public class LuceneExecutor implements Runnable {
                        // commit all changes and reset the searcher\r
                        config.setInt(CONF_INDEX, null, CONF_VERSION, INDEX_VERSION);\r
                        config.save();\r
-                       resetIndexSearcher(repositoryName);\r
+                       resetIndexSearcher(model.name);\r
                        writer.commit();\r
                        result.success();\r
                } catch (Exception e) {\r
-                       logger.error("Exception while reindexing " + repositoryName, e);\r
+                       logger.error("Exception while reindexing " + model.name, e);\r
                }\r
                return result;\r
        }\r
@@ -648,24 +639,6 @@ public class LuceneExecutor implements Runnable {
                        String branch, RevCommit commit) {\r
                IndexResult result = new IndexResult();\r
                try {\r
-                       if (excludedBranches.contains(branch)) {\r
-                               if (IssueUtils.GB_ISSUES.equals(branch)) {\r
-                                       // index an issue\r
-                                       String issueId = commit.getShortMessage().substring(2).trim();\r
-                                       IssueModel issue = IssueUtils.getIssue(repository, issueId);\r
-                                       if (issue == null) {\r
-                                               // issue was deleted, remove from index\r
-                                               deleteIssue(repositoryName, issueId);\r
-                                               result.success = true;\r
-                                               return result;\r
-                                       }\r
-                                       result.success = index(repositoryName, issue);\r
-                                       result.issueCount++;\r
-                                       return result;\r
-                                       \r
-                               }\r
-                               return result;\r
-                       }\r
                        List<PathChangeModel> changedPaths = JGitUtils.getFilesInCommit(repository, commit);\r
                        String revDate = DateTools.timeToString(commit.getCommitTime() * 1000L,\r
                                        Resolution.MINUTE);\r
@@ -779,11 +752,11 @@ public class LuceneExecutor implements Runnable {
        /**\r
         * Updates a repository index incrementally from the last indexed commits.\r
         * \r
-        * @param repositoryName\r
+        * @param model\r
         * @param repository\r
         * @return IndexResult\r
         */\r
-       protected IndexResult updateIndex(String repositoryName, Repository repository) {\r
+       protected IndexResult updateIndex(RepositoryModel model, Repository repository) {\r
                IndexResult result = new IndexResult();\r
                try {\r
                        FileBasedConfig config = getConfig(repository);\r
@@ -816,6 +789,12 @@ public class LuceneExecutor implements Runnable {
                        for (RefModel branch : branches) {\r
                                String branchName = branch.getName();\r
 \r
+                               // determine if we should skip this branch\r
+                               if (!IssueUtils.GB_ISSUES.equals(branch)\r
+                                               && !model.indexedBranches.contains(branch.getName())) {\r
+                                       continue;\r
+                               }\r
+                               \r
                                // remove this branch from the deletedBranches set\r
                                deletedBranches.remove(branchName);\r
 \r
@@ -836,10 +815,33 @@ public class LuceneExecutor implements Runnable {
                                        result.branchCount += 1;\r
                                }\r
                                \r
+                               // track the issue ids that we have already indexed\r
+                               Set<String> indexedIssues = new TreeSet<String>();\r
+                               \r
                                // reverse the list of commits so we start with the first commit                                \r
                                Collections.reverse(revs);\r
-                               for (RevCommit commit : revs) {\r
-                                       result.add(index(repositoryName, repository, branchName, commit));                                      \r
+                               for (RevCommit commit : revs) {                                 \r
+                                       if (IssueUtils.GB_ISSUES.equals(branch)) {\r
+                                               // only index an issue once during updateIndex\r
+                                               String issueId = commit.getShortMessage().substring(2).trim();\r
+                                               if (indexedIssues.contains(issueId)) {\r
+                                                       continue;\r
+                                               }\r
+                                               indexedIssues.add(issueId);\r
+                                               \r
+                                               IssueModel issue = IssueUtils.getIssue(repository, issueId);\r
+                                               if (issue == null) {\r
+                                                       // issue was deleted, remove from index\r
+                                                       deleteIssue(model.name, issueId);\r
+                                               } else {\r
+                                                       // issue was updated\r
+                                                       index(model.name, issue);\r
+                                                       result.issueCount++;\r
+                                               }\r
+                                       } else {\r
+                                               // index a commit\r
+                                               result.add(index(model.name, repository, branchName, commit));\r
+                                       }\r
                                }\r
 \r
                                // update the config\r
@@ -853,14 +855,14 @@ public class LuceneExecutor implements Runnable {
                        // unless a branch really was deleted and no longer exists\r
                        if (deletedBranches.size() > 0) {\r
                                for (String branch : deletedBranches) {\r
-                                       IndexWriter writer = getIndexWriter(repositoryName);\r
+                                       IndexWriter writer = getIndexWriter(model.name);\r
                                        writer.deleteDocuments(new Term(FIELD_BRANCH, branch));\r
                                        writer.commit();\r
                                }\r
                        }\r
                        result.success = true;\r
                } catch (Throwable t) {\r
-                       logger.error(MessageFormat.format("Exception while updating {0} Lucene index", repositoryName), t);\r
+                       logger.error(MessageFormat.format("Exception while updating {0} Lucene index", model.name), t);\r
                }\r
                return result;\r
        }\r
index 10dcbc684307ffebff3040b1da754c84d2a4838b..c087a358d8a62b68bdcdf0a4e69cf4b7efe3af5b 100644 (file)
@@ -57,6 +57,7 @@ public class RepositoryModel implements Serializable, Comparable<RepositoryModel
        public String origin;\r
        public String HEAD;\r
        public List<String> availableRefs;\r
+       public List<String> indexedBranches;\r
        public String size;\r
        public List<String> preReceiveScripts;\r
        public List<String> postReceiveScripts;\r
@@ -74,7 +75,7 @@ public class RepositoryModel implements Serializable, Comparable<RepositoryModel
                this.lastChange = lastchange;\r
                this.accessRestriction = AccessRestrictionType.NONE;\r
                this.federationSets = new ArrayList<String>();\r
-               this.federationStrategy = FederationStrategy.FEDERATE_THIS;\r
+               this.federationStrategy = FederationStrategy.FEDERATE_THIS;             \r
        }\r
 \r
        @Override\r
index 9ce4df5df671e93955956b5ace6e4e9d71287d08..0952b573f70eb8a1233f09e008edf2c3d85f883e 100644 (file)
@@ -216,4 +216,9 @@ gb.workingCopyWarning = this repository has a working copy and can not receive p
 gb.query = query\r
 gb.queryHelp = write brief explanation of Lucene syntax here\r
 gb.authored = authored\r
-gb.committed = committed
\ No newline at end of file
+gb.committed = committed\r
+gb.indexedBranches = indexed branches\r
+gb.indexedBranchesDescription = select the branches to include in your Lucene index\r
+gb.noIndexedRepositoriesWarning = none of your repositories are configured for Lucene indexing\r
+gb.undefinedQueryWarning = query is undefined!\r
+gb.gb.noSelectedRepositoriesWarning = please select one or more repositories!
\ No newline at end of file
index 2a1ae70ca812140393c48f3adf0b073f49613edf..26ef37c878661e5799647124fcd321ebac3f3149 100644 (file)
@@ -31,6 +31,8 @@
                                <tr><td colspan="2"><h3><wicket:message key="gb.federation"></wicket:message> &nbsp;<small><wicket:message key="gb.federationRepositoryDescription"></wicket:message></small></h3></td></tr>    \r
                                <tr><th><wicket:message key="gb.federationStrategy"></wicket:message></th><td class="edit"><select class="span4" wicket:id="federationStrategy" tabindex="15" /></td></tr>\r
                                <tr><th style="vertical-align: top;"><wicket:message key="gb.federationSets"></wicket:message></th><td style="padding:2px;"><span wicket:id="federationSets"></span></td></tr>\r
+                               <tr><td colspan="2"><h3><wicket:message key="gb.search"></wicket:message> &nbsp;<small><wicket:message key="gb.indexedBranchesDescription"></wicket:message></small></h3></td></tr>     \r
+                               <tr><th style="vertical-align: top;"><wicket:message key="gb.indexedBranches"></wicket:message></th><td style="padding:2px;"><span wicket:id="indexedBranches"></span></td></tr>\r
                                <tr><td colspan="2"><h3><wicket:message key="gb.hookScripts"></wicket:message> &nbsp;<small><wicket:message key="gb.hookScriptsDescription"></wicket:message></small></h3></td></tr>    \r
                                <tr><th style="vertical-align: top;"><wicket:message key="gb.preReceiveScripts"></wicket:message><p></p><span wicket:id="inheritedPreReceive"></span></th><td style="padding:2px;"><span wicket:id="preReceiveScripts"></span></td></tr>\r
                                <tr><th style="vertical-align: top;"><wicket:message key="gb.postReceiveScripts"></wicket:message><p></p><span wicket:id="inheritedPostReceive"></span></th><td style="padding:2px;"><span wicket:id="postReceiveScripts"></span></td></tr>\r
index 1eae2c986e953ec7d8b8114d00b5d8afb5608148..172859f1374f429792799686fc4f2e3b4616fb03 100644 (file)
@@ -39,6 +39,7 @@ import org.apache.wicket.model.IModel;
 import org.apache.wicket.model.Model;\r
 import org.apache.wicket.model.util.CollectionModel;\r
 import org.apache.wicket.model.util.ListModel;\r
+import org.eclipse.jgit.lib.Constants;\r
 \r
 import com.gitblit.Constants.AccessRestrictionType;\r
 import com.gitblit.Constants.FederationStrategy;\r
@@ -82,6 +83,7 @@ public class EditRepositoryPage extends RootSubPage {
                // ensure this user can create or edit this repository\r
                checkPermissions(repositoryModel);\r
 \r
+               List<String> indexedBranches = new ArrayList<String>();\r
                List<String> federationSets = new ArrayList<String>();\r
                List<String> repositoryUsers = new ArrayList<String>();\r
                List<String> repositoryTeams = new ArrayList<String>();\r
@@ -98,6 +100,9 @@ public class EditRepositoryPage extends RootSubPage {
                                Collections.sort(repositoryUsers);\r
                        }\r
                        federationSets.addAll(repositoryModel.federationSets);\r
+                       if (!ArrayUtils.isEmpty(repositoryModel.indexedBranches)) {\r
+                               indexedBranches.addAll(repositoryModel.indexedBranches);\r
+                       }\r
                }\r
 \r
                final String oldName = repositoryModel.name;\r
@@ -109,13 +114,28 @@ public class EditRepositoryPage extends RootSubPage {
                // teams palette\r
                final Palette<String> teamsPalette = new Palette<String>("teams", new ListModel<String>(\r
                                repositoryTeams), new CollectionModel<String>(GitBlit.self().getAllTeamnames()),\r
-                               new StringChoiceRenderer(), 5, false);\r
+                               new StringChoiceRenderer(), 8, false);\r
 \r
+               // indexed local branches palette\r
+               List<String> allLocalBranches = new ArrayList<String>();\r
+               if (!ArrayUtils.isEmpty(repositoryModel.availableRefs)) {\r
+                       for (String ref : repositoryModel.availableRefs) {\r
+                               if (ref.startsWith(Constants.R_HEADS)) {\r
+                                       allLocalBranches.add(ref);\r
+                               }\r
+                       }\r
+               }\r
+               final Palette<String> indexedBranchesPalette = new Palette<String>("indexedBranches", new ListModel<String>(\r
+                               indexedBranches), new CollectionModel<String>(allLocalBranches),\r
+                               new StringChoiceRenderer(), 8, false);\r
+               indexedBranchesPalette.setEnabled(allLocalBranches.size() > 0);\r
+\r
+               \r
                // federation sets palette\r
                List<String> sets = GitBlit.getStrings(Keys.federation.sets);\r
                final Palette<String> federationSetsPalette = new Palette<String>("federationSets",\r
                                new ListModel<String>(federationSets), new CollectionModel<String>(sets),\r
-                               new StringChoiceRenderer(), 5, false);\r
+                               new StringChoiceRenderer(), 8, false);\r
 \r
                // pre-receive palette\r
                if (!ArrayUtils.isEmpty(repositoryModel.preReceiveScripts)) {\r
@@ -211,6 +231,14 @@ public class EditRepositoryPage extends RootSubPage {
                                                repositoryModel.mailingLists = new ArrayList<String>(list);\r
                                        }\r
 \r
+                                       // indexed branches\r
+                                       List<String> indexedBranches = new ArrayList<String>();\r
+                                       Iterator<String> branches = indexedBranchesPalette.getSelectedChoices();\r
+                                       while (branches.hasNext()) {\r
+                                               indexedBranches.add(branches.next());\r
+                                       }\r
+                                       repositoryModel.indexedBranches = indexedBranches;\r
+\r
                                        // pre-receive scripts\r
                                        List<String> preReceiveScripts = new ArrayList<String>();\r
                                        Iterator<String> pres = preReceivePalette.getSelectedChoices();\r
@@ -293,7 +321,7 @@ public class EditRepositoryPage extends RootSubPage {
                form.add(new DropDownChoice<FederationStrategy>("federationStrategy", federationStrategies,\r
                                new FederationTypeRenderer()));\r
                form.add(new CheckBox("useTickets"));\r
-               form.add(new CheckBox("useDocs"));\r
+               form.add(new CheckBox("useDocs"));              \r
                form.add(new CheckBox("showRemoteBranches"));\r
                form.add(new CheckBox("showReadme"));\r
                form.add(new CheckBox("skipSizeCalculation"));\r
@@ -301,6 +329,7 @@ public class EditRepositoryPage extends RootSubPage {
                mailingLists = new Model<String>(ArrayUtils.isEmpty(repositoryModel.mailingLists) ? ""\r
                                : StringUtils.flattenStrings(repositoryModel.mailingLists, " "));\r
                form.add(new TextField<String>("mailingLists", mailingLists));\r
+               form.add(indexedBranchesPalette);\r
                form.add(usersPalette);\r
                form.add(teamsPalette);\r
                form.add(federationSetsPalette);\r
index 51456b1250c8bd6375ca6f2d338b7f9bc19a4959..099471a82a196f106b9e4735e13b56981bd88c72 100644 (file)
@@ -88,9 +88,30 @@ public class LucenePage extends RootPage {
                        }\r
                }\r
                \r
+               // display user-accessible selections\r
+               UserModel user = GitBlitWebSession.get().getUser();\r
+               List<String> availableRepositories = new ArrayList<String>();\r
+               for (RepositoryModel model : GitBlit.self().getRepositoryModels(user)) {\r
+                       if (model.hasCommits && !ArrayUtils.isEmpty(model.indexedBranches)) {\r
+                               availableRepositories.add(model.name);\r
+                       }\r
+               }\r
+               \r
+               if (availableRepositories.size() == 0) {\r
+                       info(getString("gb.noIndexedRepositoriesWarning"));\r
+               }\r
+\r
+               // enforce user-accessible repository selections\r
+               ArrayList<String> searchRepositories = new ArrayList<String>();\r
+               for (String selectedRepository : repositories) {\r
+                       if (availableRepositories.contains(selectedRepository)) {\r
+                               searchRepositories.add(selectedRepository);\r
+                       }\r
+               }\r
+               \r
                // search form\r
                final Model<String> queryModel = new Model<String>(query);\r
-               final Model<ArrayList<String>> repositoriesModel = new Model<ArrayList<String>>(repositories);\r
+               final Model<ArrayList<String>> repositoriesModel = new Model<ArrayList<String>>(searchRepositories);\r
                StatelessForm<Void> form = new StatelessForm<Void>("searchForm") {\r
                        \r
                        private static final long serialVersionUID = 1L;\r
@@ -99,11 +120,11 @@ public class LucenePage extends RootPage {
                        public void onSubmit() {\r
                                String q = queryModel.getObject();\r
                                if (StringUtils.isEmpty(q)) {\r
-                                       error("Query is empty!");\r
+                                       error(getString("gb.undefinedQueryWarning"));\r
                                        return;\r
                                }                               \r
                                if (repositoriesModel.getObject().size() == 0) {\r
-                                       error("Please select one or more repositories!");\r
+                                       error(getString("gb.noSelectedRepositoriesWarning"));\r
                                        return;\r
                                }\r
                                PageParameters params = new PageParameters();\r
@@ -112,14 +133,7 @@ public class LucenePage extends RootPage {
                                setResponsePage(LucenePage.class, params);\r
                        }\r
                };\r
-               \r
-               UserModel user = GitBlitWebSession.get().getUser();\r
-               List<String> availableRepositories = new ArrayList<String>();\r
-               for (RepositoryModel model : GitBlit.self().getRepositoryModels(user)) {\r
-                       if (model.hasCommits) {\r
-                               availableRepositories.add(model.name);\r
-                       }\r
-               }\r
+                               \r
                ListMultipleChoice<String> selections = new ListMultipleChoice<String>("repositories", \r
                                repositoriesModel, availableRepositories, new StringChoiceRenderer());\r
                selections.setMaxRows(10);\r
@@ -129,8 +143,8 @@ public class LucenePage extends RootPage {
                                \r
                // execute search\r
                final List<SearchResult> results = new ArrayList<SearchResult>();\r
-               if (!ArrayUtils.isEmpty(repositories) && !StringUtils.isEmpty(query)) {\r
-                       results.addAll(GitBlit.self().search(query, 100, repositories));\r
+               if (!ArrayUtils.isEmpty(searchRepositories) && !StringUtils.isEmpty(query)) {\r
+                       results.addAll(GitBlit.self().search(query, 100, searchRepositories));\r
                }\r
                \r
                // search results view\r
index ddc9bf85fb8ee2f54fbe7dc5171666bd2cf215da..7a171dbc3cac1bd5f482b8594b403e388c342b0e 100644 (file)
@@ -24,7 +24,10 @@ import org.eclipse.jgit.lib.Repository;
 import org.junit.Test;\r
 \r
 import com.gitblit.LuceneExecutor;\r
+import com.gitblit.models.RefModel;\r
+import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.SearchResult;\r
+import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.StringUtils;\r
 \r
 /**\r
@@ -39,9 +42,18 @@ public class LuceneExecutorTest {
                return new LuceneExecutor(null, GitBlitSuite.REPOSITORIES);\r
        }\r
        \r
-       private String getName(Repository repository) {\r
-               return StringUtils.getRelativePath(GitBlitSuite.REPOSITORIES.getAbsolutePath(),\r
+       private RepositoryModel newRepositoryModel(Repository repository) {             \r
+               RepositoryModel model = new RepositoryModel();\r
+               model.name = StringUtils.getRelativePath(GitBlitSuite.REPOSITORIES.getAbsolutePath(),\r
                                repository.getDirectory().getAbsolutePath());\r
+               model.hasCommits = JGitUtils.hasCommits(repository);\r
+               \r
+               // index all local branches\r
+               model.indexedBranches = new ArrayList<String>();\r
+               for (RefModel ref : JGitUtils.getLocalBranches(repository, true, -1)) {\r
+                       model.indexedBranches.add(ref.getName());\r
+               }\r
+               return model;\r
        }\r
        \r
        @Test\r
@@ -50,25 +62,25 @@ public class LuceneExecutorTest {
                \r
                // reindex helloworld\r
                Repository repository = GitBlitSuite.getHelloworldRepository();\r
-               String name = getName(repository);\r
-               lucene.reindex(name, repository);\r
+               RepositoryModel model = newRepositoryModel(repository);\r
+               lucene.reindex(model, repository);\r
                repository.close();\r
                \r
-               SearchResult result = lucene.search("type:blob AND path:bit.bit", 1, name).get(0);              \r
+               SearchResult result = lucene.search("type:blob AND path:bit.bit", 1, model.name).get(0);                \r
                assertEquals("Mike Donaghy", result.author);\r
-               result = lucene.search("type:blob AND path:clipper.prg", 1, name).get(0);               \r
+               result = lucene.search("type:blob AND path:clipper.prg", 1, model.name).get(0);         \r
                assertEquals("tinogomes", result.author);               \r
 \r
                // reindex theoretical physics\r
                repository = GitBlitSuite.getTheoreticalPhysicsRepository();\r
-               name = getName(repository);\r
-               lucene.reindex(name, repository);\r
+               model = newRepositoryModel(repository);\r
+               lucene.reindex(model, repository);\r
                repository.close();\r
                \r
                // reindex JGit\r
                repository = GitBlitSuite.getJGitRepository();\r
-               name = getName(repository);\r
-               lucene.reindex(name, repository);\r
+               model = newRepositoryModel(repository);\r
+               lucene.reindex(model, repository);\r
                repository.close();\r
                \r
                lucene.close();\r
@@ -80,51 +92,51 @@ public class LuceneExecutorTest {
                \r
                // 2 occurrences on the master branch\r
                Repository repository = GitBlitSuite.getHelloworldRepository();                         \r
-               String name = getName(repository);\r
+               RepositoryModel model = newRepositoryModel(repository);\r
                repository.close();\r
                \r
-               List<SearchResult> results = lucene.search("ada", 10, name);\r
+               List<SearchResult> results = lucene.search("ada", 10, model.name);\r
                assertEquals(2, results.size());\r
                for (SearchResult res : results) {\r
                        assertEquals("refs/heads/master", res.branch);\r
                }\r
 \r
                // author test\r
-               results = lucene.search("author: tinogomes AND type:commit", 10, name);\r
+               results = lucene.search("author: tinogomes AND type:commit", 10, model.name);\r
                assertEquals(2, results.size());\r
                \r
                // blob test\r
-               results = lucene.search("type: blob AND \"import std.stdio\"", 10, name);\r
+               results = lucene.search("type: blob AND \"import std.stdio\"", 10, model.name);\r
                assertEquals(1, results.size());\r
                assertEquals("d.D", results.get(0).path);\r
                \r
                // 1 occurrence on the gh-pages branch\r
                repository = GitBlitSuite.getTheoreticalPhysicsRepository();\r
-               name = getName(repository);\r
+               model = newRepositoryModel(repository);\r
                repository.close();\r
                \r
-               results = lucene.search("\"add the .nojekyll file\"", 10, name);\r
+               results = lucene.search("\"add the .nojekyll file\"", 10, model.name);\r
                assertEquals(1, results.size());\r
                assertEquals("Ondrej Certik", results.get(0).author);\r
                assertEquals("2648c0c98f2101180715b4d432fc58d0e21a51d7", results.get(0).commitId);\r
                assertEquals("refs/heads/gh-pages", results.get(0).branch);\r
                \r
-               results = lucene.search("type:blob AND \"src/intro.rst\"", 10, name);\r
+               results = lucene.search("type:blob AND \"src/intro.rst\"", 10, model.name);\r
                assertEquals(4, results.size());\r
                \r
                // hash id tests\r
-               results = lucene.search("commit:57c4f26f157ece24b02f4f10f5f68db1d2ce7ff5", 10, name);\r
+               results = lucene.search("commit:57c4f26f157ece24b02f4f10f5f68db1d2ce7ff5", 10, model.name);\r
                assertEquals(1, results.size());\r
 \r
-               results = lucene.search("commit:57c4f26f157*", 10, name);\r
+               results = lucene.search("commit:57c4f26f157*", 10, model.name);\r
                assertEquals(1, results.size());                \r
                \r
                // annotated tag test\r
                repository = GitBlitSuite.getJGitRepository();\r
-               name = getName(repository);\r
+               model = newRepositoryModel(repository);\r
                repository.close();\r
                \r
-               results = lucene.search("I663208919f297836a9c16bf458e4a43ffaca4c12", 10, name);\r
+               results = lucene.search("I663208919f297836a9c16bf458e4a43ffaca4c12", 10, model.name);\r
                assertEquals(1, results.size());\r
                assertEquals("[v1.3.0.201202151440-r]", results.get(0).tags.toString());                \r
                \r
@@ -136,11 +148,11 @@ public class LuceneExecutorTest {
                LuceneExecutor lucene = newLuceneExecutor();\r
                List<String> list = new ArrayList<String>();\r
                Repository repository = GitBlitSuite.getHelloworldRepository();\r
-               list.add(getName(repository));\r
+               list.add(newRepositoryModel(repository).name);\r
                repository.close();\r
 \r
                repository = GitBlitSuite.getJGitRepository();\r
-               list.add(getName(repository));\r
+               list.add(newRepositoryModel(repository).name);\r
                repository.close();\r
 \r
                List<SearchResult> results = lucene.search("test", 10, list);\r