Commit c46f933b authored by George Andrinopoulos's avatar George Andrinopoulos Committed by Oswaldo Ferreira

Fix pagination headers for repository commits api endpoint

parent 473cab81
---
title: Fix pagination headers for repository commits api endpoint
merge_request:
author: George Andrinopoulos
...@@ -24,7 +24,7 @@ module API ...@@ -24,7 +24,7 @@ module API
end end
get ":id/repository/commits" do get ":id/repository/commits" do
ref = params[:ref_name] || user_project.try(:default_branch) || 'master' ref = params[:ref_name] || user_project.try(:default_branch) || 'master'
offset = params[:page] * params[:per_page] offset = (params[:page] - 1) * params[:per_page]
commits = user_project.repository.commits(ref, commits = user_project.repository.commits(ref,
path: params[:path], path: params[:path],
...@@ -33,8 +33,7 @@ module API ...@@ -33,8 +33,7 @@ module API
after: params[:since], after: params[:since],
before: params[:until]) before: params[:until])
commit_count = user_project.repository.commit_count_for_ref(ref) paginated_commits = Kaminari.paginate_array(commits, total_count: commits.size)
paginated_commits = Kaminari.paginate_array(commits, total_count: commit_count)
present paginate(paginated_commits), with: Entities::RepoCommit present paginate(paginated_commits), with: Entities::RepoCommit
end end
......
...@@ -292,7 +292,6 @@ module Gitlab ...@@ -292,7 +292,6 @@ module Gitlab
} }
options = default_options.merge(options) options = default_options.merge(options)
options[:limit] ||= 0
options[:offset] ||= 0 options[:offset] ||= 0
actual_ref = options[:ref] || root_ref actual_ref = options[:ref] || root_ref
begin begin
...@@ -354,6 +353,16 @@ module Gitlab ...@@ -354,6 +353,16 @@ module Gitlab
lines.map! { |c| Rugged::Commit.new(rugged, c.strip) } lines.map! { |c| Rugged::Commit.new(rugged, c.strip) }
end end
def count_commits(options)
cmd = %W(#{Gitlab.config.git.bin_path} --git-dir=#{path} rev-list)
cmd += %W(--after=#{options[:after].iso8601}) if options[:after]
cmd += %W(--before=#{options[:before].iso8601}) if options[:before]
cmd += %W(--count #{options[:ref]})
cmd += %W(-- #{options[:path]}) if options[:path].present?
raw_output = IO.popen(cmd) {|io| io.read }
raw_output.to_i
end
def sha_from_ref(ref) def sha_from_ref(ref)
rev_parse_target(ref).oid rev_parse_target(ref).oid
end end
......
...@@ -824,6 +824,35 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -824,6 +824,35 @@ describe Gitlab::Git::Repository, seed_helper: true do
it { is_expected.to eq(17) } it { is_expected.to eq(17) }
end end
describe '#count_commits' do
context 'with after timestamp' do
options = { ref: 'master', limit: nil, after: Time.iso8601('2013-03-03T20:15:01+00:00') }
it 'returns the number of commits after timestamp' do
commits = repository.log(options)
expect(repository.count_commits(options)).to eq(commits.size)
end
end
context 'with before timestamp' do
options = { ref: 'feature', limit: nil, before: Time.iso8601('2015-03-03T20:15:01+00:00') }
it 'returns the number of commits after timestamp' do
commits = repository.log(options)
expect(repository.count_commits(options)).to eq(commits.size)
end
end
context 'with path' do
options = { ref: 'master', limit: nil, path: "encoding" }
it 'returns the number of commits with path ' do
commits = repository.log(options)
expect(repository.count_commits(options)).to eq(commits.size)
end
end
end
describe "branch_names_contains" do describe "branch_names_contains" do
subject { repository.branch_names_contains(SeedRepo::LastCommit::ID) } subject { repository.branch_names_contains(SeedRepo::LastCommit::ID) }
......
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