diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-05-27 11:16:07 +0000 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-05-27 11:16:07 +0000 |
commit | 3bdf0e2921c4cac46084834899302b25858e6bde (patch) | |
tree | f1272e4a21f232aaad5ac1e0a5254b13ce1df040 | |
parent | 3553e36d169e18025a2409b7055fff082d89f630 (diff) | |
parent | c7e00aca2d68a15c901506f1af4242df92670b6a (diff) | |
download | gitlab-ce-3bdf0e2921c4cac46084834899302b25858e6bde.tar.gz |
Merge branch 'compare-api' into 'master'
Compare api
Fixes #1165
-rw-r--r-- | doc/api/repositories.md | 53 | ||||
-rw-r--r-- | lib/api/entities.rb | 25 | ||||
-rw-r--r-- | lib/api/repositories.rb | 16 | ||||
-rw-r--r-- | spec/requests/api/repositories_spec.rb | 39 |
4 files changed, 133 insertions, 0 deletions
diff --git a/doc/api/repositories.md b/doc/api/repositories.md index 858fad0a0b5..e9120e17bb8 100644 --- a/doc/api/repositories.md +++ b/doc/api/repositories.md @@ -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 +} +``` diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 457af52fe9d..6bad6c74bca 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -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 diff --git a/lib/api/repositories.rb b/lib/api/repositories.rb index 076a9ceeb74..a587d4a7bdf 100644 --- a/lib/api/repositories.rb +++ b/lib/api/repositories.rb @@ -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 diff --git a/spec/requests/api/repositories_spec.rb b/spec/requests/api/repositories_spec.rb index 5a5222ed3c5..a902a1542cc 100644 --- a/spec/requests/api/repositories_spec.rb +++ b/spec/requests/api/repositories_spec.rb @@ -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 |