diff options
author | Javier Castro <javier.alejandro.castro@gmail.com> | 2013-04-14 14:07:11 -0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-05-09 11:47:08 +0300 |
commit | 7f57fe5034f6fdfbdd4e29745b50e7bfa1576bf5 (patch) | |
tree | 29586c1f9dd79b73b88e1797e26b432f9d18fd40 | |
parent | 4353babe61a57be128bd967731e7c536c9a55ba3 (diff) | |
download | gitlab-ce-7f57fe5034f6fdfbdd4e29745b50e7bfa1576bf5.tar.gz |
Implemented code search feature
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | Gemfile | 2 | ||||
-rw-r--r-- | Gemfile.lock | 3 | ||||
-rw-r--r-- | app/contexts/search_context.rb | 9 | ||||
-rw-r--r-- | app/controllers/search_controller.rb | 1 | ||||
-rw-r--r-- | app/models/project.rb | 11 | ||||
-rw-r--r-- | app/views/layouts/_search.html.haml | 11 | ||||
-rw-r--r-- | app/views/search/_result.html.haml | 9 | ||||
-rw-r--r-- | features/project/source/search_code.feature | 9 | ||||
-rw-r--r-- | features/steps/project/project_search_code.rb | 16 | ||||
-rw-r--r-- | lib/gitlab/blob_snippet.rb | 32 |
11 files changed, 98 insertions, 7 deletions
diff --git a/.travis.yml b/.travis.yml index d7fbbc7bbcb..d8e01fd6c68 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ before_install: - gem install charlock_holmes -v="0.6.9" branches: only: - - 'master' + - 'code-search' rvm: - 1.9.3-p392 - 2.0.0 @@ -23,7 +23,7 @@ gem 'omniauth-github' # Extracting information from a git repository # We cannot use original git since some bugs -gem "grit", '~> 2.5.0', git: 'https://github.com/gitlabhq/grit.git', ref: '42297cdcee16284d2e4eff23d41377f52fc28b9d' +gem "grit", '~> 2.5.0', git: 'https://github.com/gitlabhq/grit.git' gem 'gitlab_git', '~> 1.0.6' # Ruby/Rack Git Smart-HTTP Server Handler diff --git a/Gemfile.lock b/Gemfile.lock index 77ff70faa55..0a247127c57 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,8 +8,7 @@ GIT GIT remote: https://github.com/gitlabhq/grit.git - revision: 42297cdcee16284d2e4eff23d41377f52fc28b9d - ref: 42297cdcee16284d2e4eff23d41377f52fc28b9d + revision: e873bb84ac3c4f8249311490d6a7c6ac9127625f specs: grit (2.5.0) diff-lcs (~> 1.1) diff --git a/app/contexts/search_context.rb b/app/contexts/search_context.rb index de6542e82f4..854c369ea4a 100644 --- a/app/contexts/search_context.rb +++ b/app/contexts/search_context.rb @@ -10,7 +10,11 @@ class SearchContext return result unless query.present? - result[:projects] = Project.where(id: project_ids).search(query).limit(10) + projects = Project.where(id: project_ids) + result[:projects] = projects.search(query).limit(10) + if projects.length == 1 + result[:snippets] = projects.first.files(query, params[:branch_ref]) + end result[:merge_requests] = MergeRequest.where(project_id: project_ids).search(query).limit(10) result[:issues] = Issue.where(project_id: project_ids).search(query).limit(10) result[:wiki_pages] = [] @@ -22,7 +26,8 @@ class SearchContext projects: [], merge_requests: [], issues: [], - wiki_pages: [] + wiki_pages: [], + snippets: [] } end end diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index bbd67df6c70..f369ff66dbf 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -18,5 +18,6 @@ class SearchController < ApplicationController @merge_requests = result[:merge_requests] @issues = result[:issues] @wiki_pages = result[:wiki_pages] + @snippets = result[:snippets] end end diff --git a/app/models/project.rb b/app/models/project.rb index f6d5fe4e1df..6ad3763b9af 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -411,4 +411,15 @@ class Project < ActiveRecord::Base !(forked_project_link.nil? || forked_project_link.forked_from_project.nil?) end + def files(query, treeish) + snippets = [] + tree = treeish.present? ? treeish : default_branch + if repository && !tree.nil? + greps = repository.repo.grep(query, 3, tree) + greps.each do |g| + snippets << Gitlab::BlobSnippet.new(self, tree, g.content, g.startline, g.filename) + end + end + snippets + end end diff --git a/app/views/layouts/_search.html.haml b/app/views/layouts/_search.html.haml index 16f52270902..2470314f6fc 100644 --- a/app/views/layouts/_search.html.haml +++ b/app/views/layouts/_search.html.haml @@ -3,4 +3,13 @@ = text_field_tag "search", nil, placeholder: "Search", class: "search-input" = hidden_field_tag :group_id, @group.try(:id) = hidden_field_tag :project_id, @project.try(:id) - .search-autocomplete-json.hide{:'data-autocomplete-opts' => search_autocomplete_source } + - if @ref + - @branch_ref = @ref + - else + - @branch_ref = @project.try(:default_branch) + - if @branch_ref.blank? + - @branch_ref = 'master' + = hidden_field_tag :branch_ref, @branch_ref + - if ENV['RAILS_ENV'] == 'test' + = submit_tag 'Go' + .search-autocomplete-json.hide{:'data-autocomplete-opts' => search_autocomplete_source }
\ No newline at end of file diff --git a/app/views/search/_result.html.haml b/app/views/search/_result.html.haml index bfa46075baa..33b632527f2 100644 --- a/app/views/search/_result.html.haml +++ b/app/views/search/_result.html.haml @@ -32,6 +32,15 @@ %strong.term = truncate wiki_page.title, length: 50 %span.light (#{wiki_page.project.name_with_namespace}) + - @snippets.each do |snippet| + %li + code: + = link_to project_blob_path(snippet.project, tree_join(snippet.tree, snippet.filename), :anchor => "L" + snippet.startline.to_s) do + %strong.term + = snippet.filename + .file_content.code + %div{class: user_color_scheme_class} + = raw snippet.colorize( formatter: :gitlab, options: { first_line_number: snippet.startline } ) :javascript $(function() { diff --git a/features/project/source/search_code.feature b/features/project/source/search_code.feature new file mode 100644 index 00000000000..f62adb5d056 --- /dev/null +++ b/features/project/source/search_code.feature @@ -0,0 +1,9 @@ +Feature: Project Search code + Background: + Given I sign in as a user + And I own project "Shop" + Given I visit project source page + + Scenario: Search for term "Welcome to Gitlab" + When I search for term "Welcome to Gitlab" + Then I should see files from repository containing "Welcome to Gitlab" diff --git a/features/steps/project/project_search_code.rb b/features/steps/project/project_search_code.rb new file mode 100644 index 00000000000..21a564d8c26 --- /dev/null +++ b/features/steps/project/project_search_code.rb @@ -0,0 +1,16 @@ +class ProjectSearchCode < Spinach::FeatureSteps + include SharedAuthentication + include SharedProject + include SharedPaths + + When 'I search for term "Welcome to Gitlab"' do + fill_in "search", with: "Welcome to Gitlab" + click_button "Go" + end + + Then 'I should see files from repository containing "Welcome to Gitlab"' do + page.should have_content "Welcome to Gitlab" + page.should have_content "GitLab is a free project and repository management application" + end + +end diff --git a/lib/gitlab/blob_snippet.rb b/lib/gitlab/blob_snippet.rb new file mode 100644 index 00000000000..842a1733095 --- /dev/null +++ b/lib/gitlab/blob_snippet.rb @@ -0,0 +1,32 @@ +module Gitlab + class BlobSnippet + include Linguist::BlobHelper + + attr_accessor :project + attr_accessor :tree + attr_accessor :lines + attr_accessor :filename + attr_accessor :startline + + def initialize(project, tree, lines, startline, filename) + @project, @tree, @lines, @startline, @filename = project, tree, lines, startline, filename + end + + def data + lines.join("\n") + end + + def name + filename + end + + def size + data.length + end + + def mode + nil + end + + end +end
\ No newline at end of file |