Commit 9c45b726 authored by David Kim's avatar David Kim

Use Compare instead of Git::Compare to apply limits

Also, handles missing refs with 404
parent c93c10ea
......@@ -1247,20 +1247,20 @@ module API
end
class Compare < Grape::Entity
expose :commit, using: Entities::Commit do |compare, options|
::Commit.decorate(compare.commits, nil).last
expose :commit, using: Entities::Commit do |compare, _|
compare.commits.last
end
expose :commits, using: Entities::Commit do |compare, options|
::Commit.decorate(compare.commits, nil)
expose :commits, using: Entities::Commit do |compare, _|
compare.commits
end
expose :diffs, using: Entities::Diff do |compare, options|
compare.diffs(limits: false).to_a
expose :diffs, using: Entities::Diff do |compare, _|
compare.diffs.diffs.to_a
end
expose :compare_timeout do |compare, options|
compare.diffs.overflow?
expose :compare_timeout do |compare, _|
compare.diffs.diffs.overflow?
end
expose :same, as: :compare_same_ref
......
......@@ -103,8 +103,13 @@ module API
optional :straight, type: Boolean, desc: 'Comparison method, `true` for direct comparison between `from` and `to` (`from`..`to`), `false` to compare using merge base (`from`...`to`)', default: false
end
get ':id/repository/compare' do
compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to], straight: params[:straight])
present compare, with: Entities::Compare
compare = CompareService.new(user_project, params[:to]).execute(user_project, params[:from], straight: params[:straight])
if compare
present compare, with: Entities::Compare
else
not_found!("Ref")
end
end
desc 'Get repository contributors' do
......
......@@ -362,6 +362,29 @@ describe API::Repositories do
expect(json_response['diffs']).to be_empty
expect(json_response['compare_same_ref']).to be_truthy
end
it "returns an empty string when the diff overflows" do
stub_const('Gitlab::Git::DiffCollection::DEFAULT_LIMITS', { max_files: 2, max_lines: 2 })
get api(route, current_user), params: { from: 'master', to: 'feature' }
expect(response).to have_gitlab_http_status(200)
expect(json_response['commits']).to be_present
expect(json_response['diffs']).to be_present
expect(json_response['diffs'].first['diff']).to be_empty
end
it "returns a 404 when from ref is unknown" do
get api(route, current_user), params: { from: 'unknown_ref', to: 'master' }
expect(response).to have_gitlab_http_status(404)
end
it "returns a 404 when to ref is unknown" do
get api(route, current_user), params: { from: 'master', to: 'unknown_ref' }
expect(response).to have_gitlab_http_status(404)
end
end
context 'when unauthenticated', 'and project is public' do
......
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