]> source.dussan.org Git - gitblit.git/commitdiff
Properly index blobs on a branch
authorJames Moger <james.moger@gitblit.com>
Sun, 19 Feb 2012 20:27:53 +0000 (15:27 -0500)
committerJames Moger <james.moger@gitblit.com>
Sun, 19 Feb 2012 20:27:53 +0000 (15:27 -0500)
src/com/gitblit/models/SearchResult.java
src/com/gitblit/utils/LuceneUtils.java

index 4a03a70b0ebdfabaa4fb7d44eca033dc87c84c03..2fa0db424454218fd244a22d5b943a7c2b51a4c5 100644 (file)
@@ -27,6 +27,8 @@ public class SearchResult implements Serializable {
        public String summary;\r
        \r
        public String repository;\r
+       \r
+       public String branch;\r
 \r
        public String id;\r
 \r
@@ -39,6 +41,6 @@ public class SearchResult implements Serializable {
 \r
        @Override\r
        public String toString() {\r
-               return  score + " : " + type.name() + " : " + repository + " : " + id;\r
+               return  score + " : " + type.name() + " : " + repository + " : " + id + " (" + branch + ")";\r
        }\r
 }
\ No newline at end of file
index 483537d086bfdf7b25229da9be3420bc31ed4cd1..738382a43c6172d154038e8cbde7827cc770cb3b 100644 (file)
@@ -38,6 +38,7 @@ import org.apache.lucene.search.TopScoreDocCollector;
 import org.apache.lucene.store.Directory;\r
 import org.apache.lucene.store.FSDirectory;\r
 import org.apache.lucene.util.Version;\r
+import org.eclipse.jgit.diff.DiffEntry.ChangeType;\r
 import org.eclipse.jgit.lib.Constants;\r
 import org.eclipse.jgit.lib.FileMode;\r
 import org.eclipse.jgit.lib.ObjectId;\r
@@ -50,6 +51,7 @@ import org.eclipse.jgit.treewalk.TreeWalk;
 \r
 import com.gitblit.models.IssueModel;\r
 import com.gitblit.models.IssueModel.Attachment;\r
+import com.gitblit.models.PathModel.PathChangeModel;\r
 import com.gitblit.models.RefModel;\r
 import com.gitblit.models.SearchResult;\r
 \r
@@ -82,6 +84,7 @@ public class LuceneUtils {
 \r
        private static final String FIELD_OBJECT_TYPE = "type";\r
        private static final String FIELD_OBJECT_ID = "id";\r
+       private static final String FIELD_BRANCH = "branch";\r
        private static final String FIELD_REPOSITORY = "repository";\r
        private static final String FIELD_SUMMARY = "summary";\r
        private static final String FIELD_CONTENT = "content";\r
@@ -163,6 +166,7 @@ public class LuceneUtils {
                                if (excludedBranches.contains(branch.getName())) {\r
                                        continue;\r
                                }\r
+                               String branchName = branch.getName();\r
                                RevWalk revWalk = new RevWalk(repository);\r
                                RevCommit rev = revWalk.parseCommit(branch.getObjectId());\r
 \r
@@ -180,6 +184,8 @@ public class LuceneUtils {
                                                        Index.NOT_ANALYZED_NO_NORMS));\r
                                        doc.add(new Field(FIELD_REPOSITORY, repositoryName, Store.YES,\r
                                                        Index.NOT_ANALYZED));\r
+                                       doc.add(new Field(FIELD_BRANCH, branchName, Store.YES,\r
+                                                       Index.NOT_ANALYZED));\r
                                        doc.add(new Field(FIELD_OBJECT_ID, treeWalk.getPathString(), Store.YES,\r
                                                        Index.NOT_ANALYZED));\r
                                        doc.add(new Field(FIELD_DATE, revDate, Store.YES, Index.NO));\r
@@ -227,6 +233,8 @@ public class LuceneUtils {
                                        Document doc = createDocument(rev, tags.get(head));\r
                                        doc.add(new Field(FIELD_REPOSITORY, repositoryName, Store.YES,\r
                                                        Index.NOT_ANALYZED));\r
+                                       doc.add(new Field(FIELD_BRANCH, branchName, Store.YES,\r
+                                                       Index.NOT_ANALYZED));\r
                                        writer.addDocument(doc);\r
                                }\r
 \r
@@ -238,6 +246,8 @@ public class LuceneUtils {
                                                Document doc = createDocument(rev, tags.get(hash));\r
                                                doc.add(new Field(FIELD_REPOSITORY, repositoryName, Store.YES,\r
                                                                Index.NOT_ANALYZED));\r
+                                               doc.add(new Field(FIELD_BRANCH, branchName, Store.YES,\r
+                                                               Index.NOT_ANALYZED));\r
                                                writer.addDocument(doc);\r
                                        }\r
                                }\r
@@ -272,11 +282,69 @@ public class LuceneUtils {
         * repository.\r
         * \r
         * @param repository\r
+        * @param branch\r
+        *            the fully qualified branch name (e.g. refs/heads/master)\r
         * @param commit\r
         * @return true, if successful\r
         */\r
-       public static boolean index(Repository repository, RevCommit commit) {\r
-               try {\r
+       public static boolean index(Repository repository, String branch, RevCommit commit) {\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
+                                       return index(repository, issue, true);\r
+                               }\r
+                               return false;\r
+                       }\r
+                       List<PathChangeModel> changedPaths = JGitUtils.getFilesInCommit(repository, commit);\r
+                       String repositoryName = getName(repository);\r
+                       String revDate = DateTools.timeToString(commit.getCommitTime() * 1000L,\r
+                                       Resolution.MINUTE);\r
+                       IndexWriter writer = getIndexWriter(repository, false);\r
+                       for (PathChangeModel path : changedPaths) {\r
+                               // delete the indexed blob\r
+                               writer.deleteDocuments(new Term(FIELD_OBJECT_TYPE, ObjectType.blob.name()),\r
+                                               new Term(FIELD_BRANCH, branch),\r
+                                               new Term(FIELD_OBJECT_ID, path.path));\r
+                               \r
+                               // re-index the blob\r
+                               if (!ChangeType.DELETE.equals(path.changeType)) {\r
+                                       Document doc = new Document();\r
+                                       doc.add(new Field(FIELD_OBJECT_TYPE, ObjectType.blob.name(), Store.YES,\r
+                                                       Index.NOT_ANALYZED_NO_NORMS));\r
+                                       doc.add(new Field(FIELD_REPOSITORY, repositoryName, Store.YES,\r
+                                                       Index.NOT_ANALYZED));\r
+                                       doc.add(new Field(FIELD_BRANCH, branch, Store.YES, Index.NOT_ANALYZED));\r
+                                       doc.add(new Field(FIELD_OBJECT_ID, path.path, Store.YES,\r
+                                                       Index.NOT_ANALYZED));\r
+                                       doc.add(new Field(FIELD_DATE, revDate, Store.YES, Index.NO));\r
+                                       doc.add(new Field(FIELD_AUTHOR, commit.getAuthorIdent().getName(), Store.YES,\r
+                                                       Index.NOT_ANALYZED_NO_NORMS));\r
+                                       doc.add(new Field(FIELD_COMMITTER, commit.getCommitterIdent().getName(),\r
+                                                       Store.YES, Index.NOT_ANALYZED_NO_NORMS));\r
+                                       doc.add(new Field(FIELD_LABEL, branch, Store.YES, Index.ANALYZED));\r
+\r
+                                       // determine extension to compare to the extension\r
+                                       // blacklist\r
+                                       String ext = null;\r
+                                       String name = path.name.toLowerCase();\r
+                                       if (name.indexOf('.') > -1) {\r
+                                               ext = name.substring(name.lastIndexOf('.') + 1);\r
+                                       }\r
+\r
+                                       if (StringUtils.isEmpty(ext) || !excludedExtensions.contains(ext)) {\r
+                                               // read the blob content\r
+                                               String str = JGitUtils.getStringContent(repository, \r
+                                                               commit.getTree(), path.path);\r
+                                               doc.add(new Field(FIELD_CONTENT, str, Store.NO, Index.ANALYZED));\r
+                                               writer.addDocument(doc);\r
+                                       }\r
+                               }\r
+                       }\r
+                       writer.commit();\r
+                       \r
                        Document doc = createDocument(commit, null);\r
                        return index(repository, doc);\r
                } catch (Exception e) {\r
@@ -324,6 +392,7 @@ public class LuceneUtils {
                doc.add(new Field(FIELD_OBJECT_TYPE, ObjectType.issue.name(), Store.YES,\r
                                Field.Index.NOT_ANALYZED_NO_NORMS));\r
                doc.add(new Field(FIELD_OBJECT_ID, issue.id, Store.YES, Index.NOT_ANALYZED));\r
+               doc.add(new Field(FIELD_BRANCH, IssueUtils.GB_ISSUES, Store.YES, Index.NOT_ANALYZED));\r
                doc.add(new Field(FIELD_DATE, DateTools.dateToString(issue.created, Resolution.MINUTE),\r
                                Store.YES, Field.Index.NO));\r
                doc.add(new Field(FIELD_AUTHOR, issue.reporter, Store.YES, Index.NOT_ANALYZED_NO_NORMS));\r
@@ -399,6 +468,7 @@ public class LuceneUtils {
                result.committer = doc.get(FIELD_COMMITTER);\r
                result.type = ObjectType.fromName(doc.get(FIELD_OBJECT_TYPE));\r
                result.repository = doc.get(FIELD_REPOSITORY);\r
+               result.branch = doc.get(FIELD_BRANCH);\r
                result.id = doc.get(FIELD_OBJECT_ID);\r
                if (doc.get(FIELD_LABEL) != null) {\r
                        result.labels = StringUtils.getStringsFromValue(doc.get(FIELD_LABEL));\r