~beuno/loggerhead/bzr-search_integration

248 by Martin Albisetti
* Use open_index_branch instead of open_index_url
1
# Copyright (C) 2008  Martin Albisetti <argentina@gmail.com>
2
# Copyright (C) 2008  Robert Collins
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
#
246 by Martin Albisetti
* Start integration with bzr-search
18
import sets
19
import os
20
from bzrlib.plugins.search import errors
21
from bzrlib.plugins.search import index as _mod_index
22
from bzrlib.plugins.search.index import FileTextHit, RevisionHit
23
from bzrlib.plugin import load_plugins
24
load_plugins()
25
261 by Martin Albisetti
* Add some docstring
26
def search_revisions(branch, query_list, suggest=False):
27
    """
28
    Search using bzr-search plugin to find revisions matching the query.
29
    This can either suggest query terms, or revision ids.
30
    
31
    param branch: branch object to search in
32
    param query_list: string to search
33
    param suggest: Optional flag to request suggestions instead of results
34
    return: A list for results, either revision ids or terms
35
    """
248 by Martin Albisetti
* Use open_index_branch instead of open_index_url
36
    index = _mod_index.open_index_branch(branch)
260 by Martin Albisetti
* Remove ugly hack and generate query properly. Robert Collins FTW
37
    query = query_list.split(' ')
38
    query = [(term,) for term in query]
246 by Martin Albisetti
* Start integration with bzr-search
39
    revid_list = []
250 by Martin Albisetti
* Added logic to suggest search terms while typing
40
    index._branch.lock_read()
41
261 by Martin Albisetti
* Add some docstring
42
    try:
43
        if suggest:
44
            terms = index.suggest(query)
45
            terms = list(terms)
46
            terms.sort()
47
            return terms
48
        else:
49
            for result in index.search(query):
50
                if isinstance(result, FileTextHit):
51
                    revid_list.append(result.text_key[1])
52
                elif isinstance(result, RevisionHit):
53
                    revid_list.append(result.revision_key)
54
            return list(sets.Set(revid_list))
55
    finally:
56
        index._branch.unlock()