~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.transport import get_transport
24
from bzrlib.plugin import load_plugins
25
load_plugins()
26
250 by Martin Albisetti
* Added logic to suggest search terms while typing
27
def search_revisions(branch, query_list=[], suggest=False):
28
    query_list = [query_list]
248 by Martin Albisetti
* Use open_index_branch instead of open_index_url
29
    index = _mod_index.open_index_branch(branch)
246 by Martin Albisetti
* Start integration with bzr-search
30
    query = [(query_item,) for query_item in query_list]
31
    revid_list = []
250 by Martin Albisetti
* Added logic to suggest search terms while typing
32
    index._branch.lock_read()
33
    if suggest:
34
        terms = index.suggest(query)
35
        terms = list(terms)
36
        terms.sort()
37
        return terms
38
    else:
39
        for result in index.search(query):
40
            if isinstance(result, FileTextHit):
41
                revid_list.append(result.text_key[1])
42
            elif isinstance(result, RevisionHit):
43
                revid_list.append(result.revision_key)
44
45
        if len(revid_list) == 0:
46
            raise errors.NoMatch(query_listo)
47
48
        return list(sets.Set(revid_list))
49
    index._branch.unlock()