diff options
author | Sean McGivern <sean@mcgivern.me.uk> | 2016-11-16 15:32:18 +0000 |
---|---|---|
committer | Sean McGivern <sean@mcgivern.me.uk> | 2016-11-16 15:32:18 +0000 |
commit | 891465ba8cd57bb928e82ba070f2d7efb63f6282 (patch) | |
tree | 49fce7d9743ffb1e12569f54bd6e839b5c4cd1e0 /app/models | |
parent | 28d9645926f0c475c825b32912c678132c09e3a4 (diff) | |
parent | a5632e802b72db01c0fb0b8bec77c0fc28b41427 (diff) | |
download | gitlab-ce-891465ba8cd57bb928e82ba070f2d7efb63f6282.tar.gz |
Merge branch '23117-search-for-a-filename-in-a-project' into 'master'
Search for a filename in a project
Closes #23117
See merge request !7426
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/project.rb | 2 | ||||
-rw-r--r-- | app/models/project_wiki.rb | 2 | ||||
-rw-r--r-- | app/models/repository.rb | 17 |
3 files changed, 15 insertions, 6 deletions
diff --git a/app/models/project.rb b/app/models/project.rb index bab2f0c53ca..327f81412f8 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -879,7 +879,7 @@ class Project < ActiveRecord::Base end def empty_repo? - !repository.exists? || !repository.has_visible_content? + repository.empty_repo? end def repo diff --git a/app/models/project_wiki.rb b/app/models/project_wiki.rb index 46f70da2452..9db96347322 100644 --- a/app/models/project_wiki.rb +++ b/app/models/project_wiki.rb @@ -127,7 +127,7 @@ class ProjectWiki end def search_files(query) - repository.search_files(query, default_branch) + repository.search_files_by_content(query, default_branch) end def repository diff --git a/app/models/repository.rb b/app/models/repository.rb index 4282197faa5..a035768a6c1 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -1063,16 +1063,25 @@ class Repository merge_base(ancestor_id, descendant_id) == ancestor_id end - def search_files(query, ref) - unless exists? && has_visible_content? && query.present? - return [] - end + def empty_repo? + !exists? || !has_visible_content? + end + + def search_files_by_content(query, ref) + return [] if empty_repo? || query.blank? offset = 2 args = %W(#{Gitlab.config.git.bin_path} grep -i -I -n --before-context #{offset} --after-context #{offset} -E -e #{Regexp.escape(query)} #{ref || root_ref}) Gitlab::Popen.popen(args, path_to_repo).first.scrub.split(/^--$/) end + def search_files_by_name(query, ref) + return [] if empty_repo? || query.blank? + + args = %W(#{Gitlab.config.git.bin_path} ls-tree --full-tree -r #{ref || root_ref} --name-status | #{Regexp.escape(query)}) + Gitlab::Popen.popen(args, path_to_repo).first.lines.map(&:strip) + end + def fetch_ref(source_path, source_ref, target_ref) args = %W(#{Gitlab.config.git.bin_path} fetch --no-tags -f #{source_path} #{source_ref}:#{target_ref}) Gitlab::Popen.popen(args, path_to_repo) |