Skip to content

Commit

Permalink
Clean up the contract of MetaRepo.find_commits. It shouldn't be parsi…
Browse files Browse the repository at this point in the history
…ng json.
  • Loading branch information
philc committed Aug 31, 2011
1 parent 78b6713 commit 60d01dd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
28 changes: 16 additions & 12 deletions lib/meta_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def self.grit_commit(repo_name_or_id, sha)
grit_commit
end

# Returns a page of commits based on the given options.
# options:
# - authors: a list of authors
# - repos: a list of repo paths
# - branches: a list of branch names
#
# returns: { :commits => [git commits], :count => number of results,
# :tokens => { :from => new search token, :to => new search token } }
def self.find_commits(options)
Expand All @@ -56,28 +62,26 @@ def self.find_commits(options)
# * paths
# * messages

# Need extended regexes to get |.
# Need extended regexes to be able to use the "|" operator.
git_options = { :extended_regexp => true, :regexp_ignore_case => true }
# Assuming authors is a comma-separated list.
if options[:authors] && !options[:authors].empty?
git_options[:author] = options[:authors].split(",").map(&:strip).join("|")
end

if options[:repos]
repo_search_regexes = options[:repos].split(",").map { |r| /#{r.strip}/ }
git_options[:author] = options[:authors].join("|") unless options[:authors].blank?

if options[:repos].blank?
repos = @@repos
else
repo_search_regexes = options[:repos].map { |repo| /#{repo}/ }
repos = []
@@repo_name_to_id.each do |name, id|
repos << @@repo_names_and_ids_to_repos[id] if repo_search_regexes.any? { |r| name =~ r }
repos << @@repo_names_and_ids_to_repos[id] if repo_search_regexes.any? { |regexp| name =~ regexp }
end
repos.uniq!
else
repos = @@repos
end

git_args = options[:branches].then { split(",").map(&:strip).map { |name| "origin/#{name}" } }.else { [] }
git_args = options[:branches].blank? ? [] : options[:branches].map { |name| "origin/#{name}" }
git_options[:all] = true if git_args.empty?
git_args << "--"
git_args += JSON.parse(options[:paths]) if options[:paths] && !options[:paths].empty?
git_args += options[:paths] unless options[:paths].blank?

# now, assuming options has everything set up correctly for rev-list except for limit and timestamp stuff

Expand Down
14 changes: 9 additions & 5 deletions models/saved_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ class SavedSearch < Sequel::Model

PAGE_SIZE = 10

# The list of commits this saved search represents
# The list of commits this saved search represents.
def commits(token = nil, direction = "before")
options = {}
[:repos, :branches, :authors, :paths, :messages].each { |p| options[p] = self.send(p) }
options.merge!(:token => token, :direction => direction, :limit => PAGE_SIZE)
result = MetaRepo.find_commits options
result = MetaRepo.find_commits(
:repos => repos_list,
:branches => branches_list,
:authors => authors_list,
:paths => paths_list,
:token => token,
:direction => direction,
:limit => PAGE_SIZE)
page = (result[:count] / PAGE_SIZE).to_i + 1
[result[:commits], page, result[:tokens]]
end
Expand Down

0 comments on commit 60d01dd

Please sign in to comment.