Commit f1126f494cf4fb2b27c43ac3d3bd69fea9c4afe4

Authored by Dmitriy Zaporozhets
1 parent 9fdcdb77

Move files search to gitlab_git

app/contexts/search_context.rb
@@ -12,12 +12,17 @@ class SearchContext @@ -12,12 +12,17 @@ class SearchContext
12 12
13 projects = Project.where(id: project_ids) 13 projects = Project.where(id: project_ids)
14 result[:projects] = projects.search(query).limit(10) 14 result[:projects] = projects.search(query).limit(10)
15 - if projects.length == 1  
16 - result[:snippets] = projects.first.files(query, params[:branch_ref]) 15 +
  16 + # Search inside singe project
  17 + result[:project] = project = projects.first if projects.length == 1
  18 +
  19 + if params[:search_code].present?
  20 + result[:blobs] = project.repository.search_files(query, params[:repository_ref]) unless project.empty_repo?
  21 + else
  22 + result[:merge_requests] = MergeRequest.where(project_id: project_ids).search(query).limit(10)
  23 + result[:issues] = Issue.where(project_id: project_ids).search(query).limit(10)
  24 + result[:wiki_pages] = []
17 end 25 end
18 - result[:merge_requests] = MergeRequest.where(project_id: project_ids).search(query).limit(10)  
19 - result[:issues] = Issue.where(project_id: project_ids).search(query).limit(10)  
20 - result[:wiki_pages] = []  
21 result 26 result
22 end 27 end
23 28
@@ -27,8 +32,7 @@ class SearchContext @@ -27,8 +32,7 @@ class SearchContext
27 merge_requests: [], 32 merge_requests: [],
28 issues: [], 33 issues: [],
29 wiki_pages: [], 34 wiki_pages: [],
30 - snippets: [] 35 + blobs: []
31 } 36 }
32 end 37 end
33 end 38 end
34 -  
app/controllers/search_controller.rb
@@ -15,9 +15,10 @@ class SearchController < ApplicationController @@ -15,9 +15,10 @@ class SearchController < ApplicationController
15 result = SearchContext.new(project_ids, params).execute 15 result = SearchContext.new(project_ids, params).execute
16 16
17 @projects = result[:projects] 17 @projects = result[:projects]
  18 + @project = result[:project]
18 @merge_requests = result[:merge_requests] 19 @merge_requests = result[:merge_requests]
19 @issues = result[:issues] 20 @issues = result[:issues]
20 @wiki_pages = result[:wiki_pages] 21 @wiki_pages = result[:wiki_pages]
21 - @snippets = result[:snippets] 22 + @blobs = result[:blobs]
22 end 23 end
23 end 24 end
app/models/project.rb
@@ -410,16 +410,4 @@ class Project < ActiveRecord::Base @@ -410,16 +410,4 @@ class Project < ActiveRecord::Base
410 def forked? 410 def forked?
411 !(forked_project_link.nil? || forked_project_link.forked_from_project.nil?) 411 !(forked_project_link.nil? || forked_project_link.forked_from_project.nil?)
412 end 412 end
413 -  
414 - def files(query, treeish)  
415 - snippets = []  
416 - tree = treeish.present? ? treeish : default_branch  
417 - if repository && !tree.nil?  
418 - greps = repository.repo.grep(query, 3, tree)  
419 - greps.each do |g|  
420 - snippets << Gitlab::BlobSnippet.new(self, tree, g.content, g.startline, g.filename)  
421 - end  
422 - end  
423 - snippets  
424 - end  
425 end 413 end
app/views/layouts/_search.html.haml
@@ -3,13 +3,6 @@ @@ -3,13 +3,6 @@
3 = text_field_tag "search", nil, placeholder: "Search", class: "search-input" 3 = text_field_tag "search", nil, placeholder: "Search", class: "search-input"
4 = hidden_field_tag :group_id, @group.try(:id) 4 = hidden_field_tag :group_id, @group.try(:id)
5 = hidden_field_tag :project_id, @project.try(:id) 5 = hidden_field_tag :project_id, @project.try(:id)
6 - - if @ref  
7 - - @branch_ref = @ref  
8 - - else  
9 - - @branch_ref = @project.try(:default_branch)  
10 - - if @branch_ref.blank?  
11 - - @branch_ref = 'master'  
12 - = hidden_field_tag :branch_ref, @branch_ref  
13 - - if ENV['RAILS_ENV'] == 'test'  
14 - = submit_tag 'Go'  
15 - .search-autocomplete-json.hide{:'data-autocomplete-opts' => search_autocomplete_source }  
16 \ No newline at end of file 6 \ No newline at end of file
  7 + = hidden_field_tag :repository_ref, @ref
  8 + = submit_tag 'Go' if ENV['RAILS_ENV'] == 'test'
  9 + .search-autocomplete-json.hide{:'data-autocomplete-opts' => search_autocomplete_source }
app/views/search/_result.html.haml
1 %fieldset 1 %fieldset
2 %legend 2 %legend
3 Search results 3 Search results
4 - %span.cgray (#{@projects.count + @merge_requests.count + @issues.count + @wiki_pages.count}) 4 + %span.cgray (#{@projects.count + @merge_requests.count + @issues.count + @wiki_pages.count + @blobs.count})
  5 +
  6 +- if @project
  7 + %ul.nav.nav-pills
  8 + %li{class: ("active" if params[:search_code].present?)}
  9 + = link_to search_path(params.merge(search_code: true)) do
  10 + Repository Code
  11 + %li{class: ("active" if params[:search_code].blank?)}
  12 + = link_to search_path(params.merge(search_code: nil)) do
  13 + Everything else
  14 +
  15 +
5 .search_results 16 .search_results
6 - %ul.well-list 17 + %ul.bordered-list
7 - @projects.each do |project| 18 - @projects.each do |project|
8 %li 19 %li
9 project: 20 project:
@@ -32,16 +43,17 @@ @@ -32,16 +43,17 @@
32 %strong.term 43 %strong.term
33 = truncate wiki_page.title, length: 50 44 = truncate wiki_page.title, length: 50
34 %span.light (#{wiki_page.project.name_with_namespace}) 45 %span.light (#{wiki_page.project.name_with_namespace})
35 - - @snippets.each do |snippet| 46 + - @blobs.each do |file|
36 %li 47 %li
37 - code:  
38 - = link_to project_blob_path(snippet.project, tree_join(snippet.tree, snippet.filename), :anchor => "L" + snippet.startline.to_s) do  
39 - %strong.term  
40 - = snippet.filename  
41 .file_holder 48 .file_holder
42 - .file_content.code 49 + .file_title
  50 + = link_to project_blob_path(@project, tree_join(file.ref, file.filename), :anchor => "L" + file.startline.to_s) do
  51 + %i.icon-file
  52 + %strong
  53 + = file.filename
  54 + .file_content.code.term
43 %div{class: user_color_scheme_class} 55 %div{class: user_color_scheme_class}
44 - = raw snippet.colorize( formatter: :gitlab, options: { first_line_number: snippet.startline } ) 56 + = raw file.colorize( formatter: :gitlab, options: { first_line_number: file.startline } )
45 57
46 :javascript 58 :javascript
47 $(function() { 59 $(function() {
app/views/search/show.html.haml
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 %span Looking for 4 %span Looking for
5 .input 5 .input
6 = search_field_tag :search, params[:search], placeholder: "issue 143", class: "input-xxlarge search-text-input", id: "dashboard_search" 6 = search_field_tag :search, params[:search], placeholder: "issue 143", class: "input-xxlarge search-text-input", id: "dashboard_search"
  7 + = hidden_field_tag :search_code, params[:search_code]
7 = submit_tag 'Search', class: "btn btn-primary wide" 8 = submit_tag 'Search', class: "btn btn-primary wide"
8 .clearfix 9 .clearfix
9 .row 10 .row