Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.travis.yml2
-rw-r--r--Gemfile2
-rw-r--r--Gemfile.lock3
-rw-r--r--app/contexts/search_context.rb9
-rw-r--r--app/controllers/search_controller.rb1
-rw-r--r--app/models/project.rb11
-rw-r--r--app/views/layouts/_search.html.haml11
-rw-r--r--app/views/search/_result.html.haml9
-rw-r--r--features/project/source/search_code.feature9
-rw-r--r--features/steps/project/project_search_code.rb16
-rw-r--r--lib/gitlab/blob_snippet.rb32
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
diff --git a/Gemfile b/Gemfile
index 04985f9c000..3f8383f308e 100644
--- a/Gemfile
+++ b/Gemfile
@@ -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