Commit 3bdf0e29 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'compare-api' into 'master'

Compare api

Fixes #1165
parents 3553e36d c7e00aca
......@@ -131,3 +131,56 @@ GET /projects/:id/repository/archive
Parameters:
+ `id` (required) - The ID of a project
+ `sha` (optional) - The commit sha to download defaults to the tip of the default branch
## Compare branches, tags or commits
```
GET /projects/:id/repository/compare
```
Parameters:
+ `id` (required) - The ID of a project
+ `from` (required) - the commit sha or branch name
+ `to` (required) - the commit sha or branch name
```
GET /projects/:id/repository/compare?from=master&to=feature
```
Response:
```json
{
"commit": {
"id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
"short_id": "12d65c8dd2b",
"title": "JS fix",
"author_name": "Dmitriy Zaporozhets",
"author_email": "dmitriy.zaporozhets@gmail.com",
"created_at": "2014-02-27T10:27:00+02:00"
},
"commits": [{
"id": "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",
"short_id": "12d65c8dd2b",
"title": "JS fix",
"author_name": "Dmitriy Zaporozhets",
"author_email": "dmitriy.zaporozhets@gmail.com",
"created_at": "2014-02-27T10:27:00+02:00"
}],
"diffs": [{
"old_path": "files/js/application.js",
"new_path": "files/js/application.js",
"a_mode": null,
"b_mode": "100644",
"diff": "--- a/files/js/application.js\n+++ b/files/js/application.js\n@@ -24,8 +24,10 @@\n //= require g.raphael-min\n //= require g.bar-min\n //= require branch-graph\n-//= require highlightjs.min\n-//= require ace/ace\n //= require_tree .\n //= require d3\n //= require underscore\n+\n+function fix() { \n+ alert(\"Fixed\")\n+}",
"new_file": false,
"renamed_file": false,
"deleted_file": false
}],
"compare_timeout": false,
"compare_same_ref": false
}
```
......@@ -194,5 +194,30 @@ module API
class Label < Grape::Entity
expose :name
end
class RepoDiff < Grape::Entity
expose :old_path, :new_path, :a_mode, :b_mode, :diff
expose :new_file, :renamed_file, :deleted_file
end
class Compare < Grape::Entity
expose :commit, using: Entities::RepoCommit do |compare, options|
if compare.commit
Commit.new compare.commit
end
end
expose :commits, using: Entities::RepoCommit do |compare, options|
Commit.decorate compare.commits
end
expose :diffs, using: Entities::RepoDiff do |compare, options|
compare.diffs
end
expose :compare_timeout do |compare, options|
compare.timeout
end
expose :same, as: :compare_same_ref
end
end
end
......@@ -15,6 +15,7 @@ module API
not_found!
end
end
# Get a project repository tags
#
# Parameters:
......@@ -118,6 +119,21 @@ module API
not_found!
end
end
# Compare two branches, tags or commits
#
# Parameters:
# id (required) - The ID of a project
# from (required) - the commit sha or branch name
# to (required) - the commit sha or branch name
# Example Request:
# GET /projects/:id/repository/compare?from=master&to=feature
get ':id/repository/compare' do
authorize! :download_code, user_project
required_attributes! [:from, :to]
compare = Gitlab::Git::Compare.new(user_project.repository.raw_repository, params[:from], params[:to], MergeRequestDiff::COMMITS_SAFE_SIZE)
present compare, with: Entities::Compare
end
end
end
end
......@@ -112,4 +112,43 @@ describe API::API, api: true do
response.status.should == 404
end
end
describe 'GET /GET /projects/:id/repository/compare' do
it "should compare branches" do
get api("/projects/#{project.id}/repository/compare", user), from: 'master', to: 'simple_merge_request'
response.status.should == 200
json_response['commits'].should be_present
json_response['diffs'].should be_present
end
it "should compare tags" do
get api("/projects/#{project.id}/repository/compare", user), from: 'v1.0.1', to: 'v1.0.2'
response.status.should == 200
json_response['commits'].should be_present
json_response['diffs'].should be_present
end
it "should compare commits" do
get api("/projects/#{project.id}/repository/compare", user), from: 'b1e6a9dbf1c85', to: '1e689bfba395'
response.status.should == 200
json_response['commits'].should be_empty
json_response['diffs'].should be_empty
json_response['compare_same_ref'].should be_false
end
it "should compare commits in reverse order" do
get api("/projects/#{project.id}/repository/compare", user), from: '1e689bfba395', to: 'b1e6a9dbf1c85'
response.status.should == 200
json_response['commits'].should be_present
json_response['diffs'].should be_present
end
it "should compare same refs" do
get api("/projects/#{project.id}/repository/compare", user), from: 'master', to: 'master'
response.status.should == 200
json_response['commits'].should be_empty
json_response['diffs'].should be_empty
json_response['compare_same_ref'].should be_true
end
end
end
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