Commit 5a506996 authored by Jonathan Schoeffling's avatar Jonathan Schoeffling Committed by Michael Chmielewski

Add support for searching commit log messages

Include the log messages of recent commits in project-level search
results, providing functionality similar to 'git log --grep'.

Update repository model rspec tests to validate the output of
Repository#commits_with_log_matching.
parent 8c9e1df9
...@@ -208,6 +208,7 @@ v 8.0.0 ...@@ -208,6 +208,7 @@ v 8.0.0
- Fix references to target project issues in Merge Requests markdown preview and textareas (Francesco Levorato) - Fix references to target project issues in Merge Requests markdown preview and textareas (Francesco Levorato)
- Redirect from incorrectly cased group or project path to correct one (Francesco Levorato) - Redirect from incorrectly cased group or project path to correct one (Francesco Levorato)
- Removed API calls from CE to CI - Removed API calls from CE to CI
- Include commit logs in project search
v 7.14.3 v 7.14.3
- No changes - No changes
......
...@@ -23,8 +23,8 @@ class SearchController < ApplicationController ...@@ -23,8 +23,8 @@ class SearchController < ApplicationController
@search_results = @search_results =
if @project if @project
unless %w(blobs notes issues merge_requests milestones wiki_blobs). unless %w(blobs notes issues merge_requests milestones wiki_blobs
include?(@scope) commits).include?(@scope)
@scope = 'blobs' @scope = 'blobs'
end end
......
...@@ -87,6 +87,12 @@ class Repository ...@@ -87,6 +87,12 @@ class Repository
commits commits
end end
def commits_with_log_matching(query)
list = Gitlab::Git::Commit.where(repo: raw_repository, limit: 1000)
list = Commit.decorate(list, @project) if list.present?
list.select! { |c| c.message.match /#{query}/i }
end
def find_branch(name) def find_branch(name)
branches.find { |branch| branch.name == name } branches.find { |branch| branch.name == name }
end end
......
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
= hidden_field_tag :scope, 'merge_requests' = hidden_field_tag :scope, 'merge_requests'
- elsif current_controller?(:wikis) - elsif current_controller?(:wikis)
= hidden_field_tag :scope, 'wiki_blobs' = hidden_field_tag :scope, 'wiki_blobs'
- elsif current_controller?(:commits)
= hidden_field_tag :scope, 'commits'
- else - else
= hidden_field_tag :search_code, true = hidden_field_tag :search_code, true
......
...@@ -42,6 +42,13 @@ ...@@ -42,6 +42,13 @@
Wiki Wiki
%span.badge %span.badge
= @search_results.wiki_blobs_count = @search_results.wiki_blobs_count
%li{class: ("active" if @scope == 'commits')}
= link_to search_filter_path(scope: 'commits') do
= icon('history fw')
%span
Commit Logs
%span.badge
= @search_results.commits_count
- elsif @show_snippets - elsif @show_snippets
%li{class: ("active" if @scope == 'snippet_blobs')} %li{class: ("active" if @scope == 'snippet_blobs')}
......
.search-result-row
.commits-row-title
%strong.str-truncated
= link_to commits.title, namespace_project_commit_path(@project.namespace, @project, commits.id), class: "commit_short_id"
.pull-right
= link_to commits.short_id, namespace_project_commit_path(@project.namespace, @project, commits.id), class: "commit_short_id"
.notes_count
- if @note_counts
- note_count = @note_counts.fetch(commits.id, 0)
- else
- notes = commits.notes
- note_count = notes.user.count
- if note_count > 0
%span.light
%i.fa.fa-comments
= note_count
- if commits.description?
.commits-row-description
%pre
= preserve(gfm(escape_once(commits.description)))
.commits-row-info
= commit_author_link(commits, avatar: true, size: 24)
authored
.committed_ago
#{time_ago_with_tooltip(commits.committed_date)} &nbsp;
= link_to_browse_code(@project, commits)
%br
...@@ -20,6 +20,8 @@ module Gitlab ...@@ -20,6 +20,8 @@ module Gitlab
Kaminari.paginate_array(blobs).page(page).per(per_page) Kaminari.paginate_array(blobs).page(page).per(per_page)
when 'wiki_blobs' when 'wiki_blobs'
Kaminari.paginate_array(wiki_blobs).page(page).per(per_page) Kaminari.paginate_array(wiki_blobs).page(page).per(per_page)
when 'commits'
Kaminari.paginate_array(commits).page(page).per(per_page)
else else
super super
end end
...@@ -27,7 +29,7 @@ module Gitlab ...@@ -27,7 +29,7 @@ module Gitlab
def total_count def total_count
@total_count ||= issues_count + merge_requests_count + blobs_count + @total_count ||= issues_count + merge_requests_count + blobs_count +
notes_count + wiki_blobs_count notes_count + wiki_blobs_count + commits_count
end end
def blobs_count def blobs_count
...@@ -42,6 +44,10 @@ module Gitlab ...@@ -42,6 +44,10 @@ module Gitlab
@wiki_blobs_count ||= wiki_blobs.count @wiki_blobs_count ||= wiki_blobs.count
end end
def commits_count
@commits_count ||= commits.count
end
private private
def blobs def blobs
...@@ -70,6 +76,10 @@ module Gitlab ...@@ -70,6 +76,10 @@ module Gitlab
Note.where(project_id: limit_project_ids).user.search(query).order('updated_at DESC') Note.where(project_id: limit_project_ids).user.search(query).order('updated_at DESC')
end end
def commits
project.repository.commits_with_log_matching(query)
end
def limit_project_ids def limit_project_ids
[project.id] [project.id]
end end
......
...@@ -26,6 +26,17 @@ describe Repository do ...@@ -26,6 +26,17 @@ describe Repository do
it { is_expected.to eq('c1acaa58bbcbc3eafe538cb8274ba387047b69f8') } it { is_expected.to eq('c1acaa58bbcbc3eafe538cb8274ba387047b69f8') }
end end
describe :commits_with_log_matching do
subject { repository.commits_with_log_matching('submodule').
map{|k| k.id}
}
it { is_expected.to include('5937ac0a7beb003549fc5fd26fc247adbce4a52e') }
it { is_expected.to include('6f6d7e7ed97bb5f0054f2b1df789b39ca89b6ff9') }
it { is_expected.to include('cfe32cf61b73a0d5e9f13e774abde7ff789b1660') }
it { is_expected.not_to include('913c66a37b4a45b9769037c55c2d238bd0942d2e') }
end
describe :blob_at do describe :blob_at do
context 'blank sha' do context 'blank sha' do
subject { repository.blob_at(Gitlab::Git::BLANK_SHA, '.gitignore') } subject { repository.blob_at(Gitlab::Git::BLANK_SHA, '.gitignore') }
......
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