summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Andrinopoulos <geoandri@gmail.com>2017-02-05 11:30:36 +0200
committerOswaldo Ferreira <oswaldo@gitlab.com>2017-03-07 22:56:31 -0300
commitc46f933bb7cb6eccdf648112a57872dc24ebf3ad (patch)
tree6ee07d0695655e1bd91ef083c2c7a8cf3b2caad3
parent473cab818aff034d072f0f6c8537a584bc5aa41c (diff)
downloadgitlab-ce-c46f933bb7cb6eccdf648112a57872dc24ebf3ad.tar.gz
Fix pagination headers for repository commits api endpoint
-rw-r--r--changelogs/unreleased/13605-add-pagination-headers-for-repository-commits-api-endpoint.yml4
-rw-r--r--lib/api/commits.rb5
-rw-r--r--lib/gitlab/git/repository.rb11
-rw-r--r--spec/lib/gitlab/git/repository_spec.rb29
4 files changed, 45 insertions, 4 deletions
diff --git a/changelogs/unreleased/13605-add-pagination-headers-for-repository-commits-api-endpoint.yml b/changelogs/unreleased/13605-add-pagination-headers-for-repository-commits-api-endpoint.yml
new file mode 100644
index 00000000000..723510c219c
--- /dev/null
+++ b/changelogs/unreleased/13605-add-pagination-headers-for-repository-commits-api-endpoint.yml
@@ -0,0 +1,4 @@
+---
+title: Fix pagination headers for repository commits api endpoint
+merge_request:
+author: George Andrinopoulos
diff --git a/lib/api/commits.rb b/lib/api/commits.rb
index 6205bff3bc0..330ad4e3d3b 100644
--- a/lib/api/commits.rb
+++ b/lib/api/commits.rb
@@ -24,7 +24,7 @@ module API
end
get ":id/repository/commits" do
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,
path: params[:path],
@@ -33,8 +33,7 @@ module API
after: params[:since],
before: params[:until])
- commit_count = user_project.repository.commit_count_for_ref(ref)
- paginated_commits = Kaminari.paginate_array(commits, total_count: commit_count)
+ paginated_commits = Kaminari.paginate_array(commits, total_count: commits.size)
present paginate(paginated_commits), with: Entities::RepoCommit
end
diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb
index 6540730ca7a..0f092d4c4ab 100644
--- a/lib/gitlab/git/repository.rb
+++ b/lib/gitlab/git/repository.rb
@@ -292,7 +292,6 @@ module Gitlab
}
options = default_options.merge(options)
- options[:limit] ||= 0
options[:offset] ||= 0
actual_ref = options[:ref] || root_ref
begin
@@ -354,6 +353,16 @@ module Gitlab
lines.map! { |c| Rugged::Commit.new(rugged, c.strip) }
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)
rev_parse_target(ref).oid
end
diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb
index 3f11f0a4516..2c0e005e942 100644
--- a/spec/lib/gitlab/git/repository_spec.rb
+++ b/spec/lib/gitlab/git/repository_spec.rb
@@ -824,6 +824,35 @@ describe Gitlab::Git::Repository, seed_helper: true do
it { is_expected.to eq(17) }
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
subject { repository.branch_names_contains(SeedRepo::LastCommit::ID) }