Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jobs:
- name: Build V
shell: cmd
run: |
make.bat -gcc
makev.bat -gcc
# Must run after "Build V": the script shells out to `v.exe`, which
# does not exist until V is built.
- name: Install SQLite
Expand Down
4 changes: 2 additions & 2 deletions highlight/markdown_test.v
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module highlight

const markdown = '<script> alert(true) </script> <!-- comment -->test'
const xss_markdown_sample = '<script> alert(true) </script> <!-- comment -->test'

fn test_convert_markdown_to_html() {
rendered := convert_markdown_to_html(markdown)
rendered := convert_markdown_to_html(xss_markdown_sample)
assert !rendered.contains('<script>')
assert rendered.contains('&lt;script&gt;')
assert rendered.contains('test</p>')
Expand Down
2 changes: 1 addition & 1 deletion repo/repo.v
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ fn (mut app App) search_public_repos(query string) []Repo {
// unrelated users.
fn (mut app App) search_repos(query string, user_id int) []Repo {
pattern := sql_like_pattern(query)
repo_rows := db_exec_values(mut app.db, 'select id from ${sql_table('Repo')} where is_deleted is false and (\n\t\t\tlower(name) like lower(${pattern}) or lower(user_name) like lower(${pattern}) or lower(description) like lower(${pattern})\n\t\t) order by nr_stars desc, name asc limit ${search_candidate_limit}') or {
repo_rows := db_exec_values(mut app.db, 'select id from ${sql_table('Repo')} where is_deleted is false and (\n\t\t\tlower(name) like lower(${pattern}) or lower(user_name) like lower(${pattern}) or lower(description) like lower(${pattern})\n\t\t) order by stars_count desc, name asc limit ${search_candidate_limit}') or {
app.warn('Repository search failed: ${err}')
return []
}
Expand Down
46 changes: 45 additions & 1 deletion search_routes.v
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
module main

import veb
import git

const max_search_query_len = 100

struct CodeSearchResult {
repo_user string
repo_name string
branch string
path string
}

// Preserve international project and user names while preventing callers from
// turning the search box into an unbounded SQL LIKE wildcard query.
fn normalize_search_query(query string) string {
Expand All @@ -28,7 +36,11 @@ fn normalize_search_query(query string) string {
pub fn (mut app App) search() veb.Result {
query := (ctx.query['query'] or { '' }).trim_space()
requested_type := ctx.query['type'] or { 'repos' }
search_type := if requested_type in ['repos', 'users'] { requested_type } else { 'repos' }
search_type := if requested_type in ['repos', 'users', 'code'] {
requested_type
} else {
'repos'
}
valid_query := normalize_search_query(query)

repos := if search_type == 'repos' && valid_query != '' {
Expand All @@ -43,5 +55,37 @@ pub fn (mut app App) search() veb.Result {
[]User{}
}

mut code_results := []CodeSearchResult{}
if search_type == 'code' && valid_query != '' {
code_results = app.search_code(valid_query)
}

return $veb.html()
}

fn (mut app App) search_code(query string) []CodeSearchResult {
mut results := []CodeSearchResult{}
repos := sql app.db {
select from Repo where is_public == true limit 20
} or { []Repo{} }
for repo in repos {
res := git.Git.exec_in_dir(repo.git_dir, ['grep', '-l', '-I', query, repo.primary_branch])
if res.exit_code != 0 {
continue
}
for line in res.output.split_into_lines() {
path := line.trim_space()
if path == '' {
continue
}
results << CodeSearchResult{ repo_user: repo.user_name, repo_name: repo.name, branch: repo.primary_branch, path: path }
if results.len >= 50 {
break
}
}
if results.len >= 50 {
break
}
}
return results
}
27 changes: 26 additions & 1 deletion templates/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@
<span>repositories</span>
|
<a href="/search?query=@valid_query&type=users">users</a>
@else
|
<a href="/search?query=@valid_query&type=code">code</a>
@elseif search_type == 'users'

Check failure on line 20 in templates/search.html

View workflow job for this annotation

GitHub Actions / windows-gcc

undefined ident: `elseif` (veb action: search)
<a href="/search?query=@valid_query&type=repos">repositories</a>
|
<span>users</span>
|
<a href="/search?query=@valid_query&type=code">code</a>
@else
<a href="/search?query=@valid_query&type=repos">repositories</a>
|
<a href="/search?query=@valid_query&type=users">users</a>
|
<span>code</span>
@end
</div>

Expand Down Expand Up @@ -66,6 +76,21 @@
<p class="search-info">We couldn’t find any users matching "${valid_query}"</p>
@end
@end

@if search_type == 'code'
@if code_results.len > 0
<p class="search-info">Showing ${code_results.len} code results</p>
@for r in code_results
<div class="list-item">
<a href="/@r.repo_user/@r.repo_name/blob/@r.branch/@r.path">
<h3>@r.repo_user/@r.repo_name — @r.path</h3>
</a>
</div>
@end
@else
<p class="search-info">We couldn’t find any code matching "${valid_query}"</p>
@end
@end
</div>

@include 'layout/footer.html'
Expand Down
Loading