Commit 7f57fe50 authored by Javier Castro's avatar Javier Castro Committed by Dmitriy Zaporozhets

Implemented code search feature

parent 4353babe
...@@ -6,7 +6,7 @@ before_install: ...@@ -6,7 +6,7 @@ before_install:
- gem install charlock_holmes -v="0.6.9" - gem install charlock_holmes -v="0.6.9"
branches: branches:
only: only:
- 'master' - 'code-search'
rvm: rvm:
- 1.9.3-p392 - 1.9.3-p392
- 2.0.0 - 2.0.0
......
...@@ -23,7 +23,7 @@ gem 'omniauth-github' ...@@ -23,7 +23,7 @@ gem 'omniauth-github'
# Extracting information from a git repository # Extracting information from a git repository
# We cannot use original git since some bugs # 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' gem 'gitlab_git', '~> 1.0.6'
# Ruby/Rack Git Smart-HTTP Server Handler # Ruby/Rack Git Smart-HTTP Server Handler
......
...@@ -8,8 +8,7 @@ GIT ...@@ -8,8 +8,7 @@ GIT
GIT GIT
remote: https://github.com/gitlabhq/grit.git remote: https://github.com/gitlabhq/grit.git
revision: 42297cdcee16284d2e4eff23d41377f52fc28b9d revision: e873bb84ac3c4f8249311490d6a7c6ac9127625f
ref: 42297cdcee16284d2e4eff23d41377f52fc28b9d
specs: specs:
grit (2.5.0) grit (2.5.0)
diff-lcs (~> 1.1) diff-lcs (~> 1.1)
......
...@@ -10,7 +10,11 @@ class SearchContext ...@@ -10,7 +10,11 @@ class SearchContext
return result unless query.present? 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[: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[:issues] = Issue.where(project_id: project_ids).search(query).limit(10)
result[:wiki_pages] = [] result[:wiki_pages] = []
...@@ -22,7 +26,8 @@ class SearchContext ...@@ -22,7 +26,8 @@ class SearchContext
projects: [], projects: [],
merge_requests: [], merge_requests: [],
issues: [], issues: [],
wiki_pages: [] wiki_pages: [],
snippets: []
} }
end end
end end
......
...@@ -18,5 +18,6 @@ class SearchController < ApplicationController ...@@ -18,5 +18,6 @@ class SearchController < ApplicationController
@merge_requests = result[:merge_requests] @merge_requests = result[:merge_requests]
@issues = result[:issues] @issues = result[:issues]
@wiki_pages = result[:wiki_pages] @wiki_pages = result[:wiki_pages]
@snippets = result[:snippets]
end end
end end
...@@ -411,4 +411,15 @@ class Project < ActiveRecord::Base ...@@ -411,4 +411,15 @@ class Project < ActiveRecord::Base
!(forked_project_link.nil? || forked_project_link.forked_from_project.nil?) !(forked_project_link.nil? || forked_project_link.forked_from_project.nil?)
end 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 end
...@@ -3,4 +3,13 @@ ...@@ -3,4 +3,13 @@
= text_field_tag "search", nil, placeholder: "Search", class: "search-input" = text_field_tag "search", nil, placeholder: "Search", class: "search-input"
= hidden_field_tag :group_id, @group.try(:id) = hidden_field_tag :group_id, @group.try(:id)
= hidden_field_tag :project_id, @project.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
...@@ -32,6 +32,15 @@ ...@@ -32,6 +32,15 @@
%strong.term %strong.term
= truncate wiki_page.title, length: 50 = truncate wiki_page.title, length: 50
%span.light (#{wiki_page.project.name_with_namespace}) %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 :javascript
$(function() { $(function() {
......
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"
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
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment