Skip to content

Commit 361dcbb

Browse files
mrpollobkueng
authored andcommitted
search: better search git hash and git tags
Signed-off-by: Ramon Roche <[email protected]>
1 parent a41baec commit 361dcbb

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

app/tornado_handlers/browse.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import collections
66
import sys
77
import os
8+
import re
89
from datetime import datetime
910
import json
1011
import sqlite3
@@ -21,8 +22,15 @@
2122

2223
BROWSE_TEMPLATE = 'browse.html'
2324

24-
#pylint: disable=abstract-method
25+
_TAG_PREFIX_RE = re.compile(
26+
r"""^v[0-9]+(?:\.[0-9]+){0,2} # vMAJOR[.MINOR[.PATCH]]
27+
(?:-[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)? # optional -prerelease
28+
(?:\+[0-9A-Za-z.-]+)? # optional +build
29+
""",
30+
re.IGNORECASE | re.VERBOSE,
31+
)
2532

33+
#pylint: disable=abstract-method
2634
class BrowseDataRetrievalHandler(tornado.web.RequestHandler):
2735
""" Ajax data retrieval handler """
2836

@@ -185,6 +193,27 @@ def get_columns_from_tuple(db_tuple, counter, all_overview_imgs):
185193
flight_modes
186194
], search_only_columns)
187195

196+
def is_hashish(q: str) -> bool:
197+
""" return true if the string looks like a hash """
198+
if len(q) < 4:
199+
return False
200+
hex_chars = set('0123456789abcdef')
201+
return all(c in hex_chars for c in q)
202+
203+
def is_tagish(q: str) -> bool:
204+
""" return true if the string looks like a tag """
205+
if not q or q[0] not in ('v', 'V'):
206+
return False
207+
return bool(_TAG_PREFIX_RE.match(q))
208+
209+
def _flatten_strings(maybe_list):
210+
""" flatten a list of strings or a single string into a single string list """
211+
if not maybe_list:
212+
return []
213+
if isinstance(maybe_list, (list, tuple)):
214+
return [str(x) for x in maybe_list]
215+
return [str(maybe_list)]
216+
188217
# need to fetch all here, because we will do more SQL calls while
189218
# iterating (having multiple cursor's does not seem to work)
190219
db_tuples = cur.fetchall()
@@ -209,20 +238,37 @@ def get_columns_from_tuple(db_tuple, counter, all_overview_imgs):
209238
filtered_counter = len(db_tuples)
210239
else:
211240
counter = 1
241+
hash_mode = is_hashish(search_str)
242+
tag_mode = is_tagish(search_str)
243+
212244
for db_tuple in db_tuples:
213245
counter += 1
214246

215247
columns = get_columns_from_tuple(db_tuple, counter, all_overview_imgs)
248+
216249
if columns is None:
217250
continue
218251

219-
if any(search_str in str(column).lower() for column in \
220-
(columns.columns, columns.search_only_columns)):
252+
visible = _flatten_strings(columns.columns)
253+
hidden = _flatten_strings(columns.search_only_columns)
254+
255+
prefix_hit = False
256+
if tag_mode or hash_mode:
257+
for col in hidden:
258+
if col.lower().startswith(search_str):
259+
prefix_hit = True
260+
break
261+
262+
substring_hit = False
263+
if not prefix_hit:
264+
haystack = [s.lower() for s in (visible + hidden)]
265+
substring_hit = any(search_str in s for s in haystack)
266+
267+
if prefix_hit or substring_hit:
221268
if data_start <= filtered_counter < data_start + data_length:
222269
json_output['data'].append(columns.columns)
223270
filtered_counter += 1
224271

225-
226272
cur.close()
227273
con.close()
228274

0 commit comments

Comments
 (0)